Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
0003  *
0004  * Copyright (c) 2003 Intracom S.A.
0005  *  by Pantelis Antoniou <panto@intracom.gr>
0006  *
0007  * 2005 (c) MontaVista Software, Inc.
0008  * Vitaly Bordug <vbordug@ru.mvista.com>
0009  *
0010  * This file is licensed under the terms of the GNU General Public License
0011  * version 2. This program is licensed "as is" without any warranty of any
0012  * kind, whether express or implied.
0013  */
0014 
0015 #include <linux/module.h>
0016 #include <linux/ioport.h>
0017 #include <linux/slab.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/netdevice.h>
0020 #include <linux/etherdevice.h>
0021 #include <linux/mii.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/mdio-bitbang.h>
0024 #include <linux/of_address.h>
0025 #include <linux/of_mdio.h>
0026 #include <linux/of_platform.h>
0027 
0028 #include "fs_enet.h"
0029 
0030 struct bb_info {
0031     struct mdiobb_ctrl ctrl;
0032     __be32 __iomem *dir;
0033     __be32 __iomem *dat;
0034     u32 mdio_msk;
0035     u32 mdc_msk;
0036 };
0037 
0038 /* FIXME: If any other users of GPIO crop up, then these will have to
0039  * have some sort of global synchronization to avoid races with other
0040  * pins on the same port.  The ideal solution would probably be to
0041  * bind the ports to a GPIO driver, and have this be a client of it.
0042  */
0043 static inline void bb_set(u32 __iomem *p, u32 m)
0044 {
0045     out_be32(p, in_be32(p) | m);
0046 }
0047 
0048 static inline void bb_clr(u32 __iomem *p, u32 m)
0049 {
0050     out_be32(p, in_be32(p) & ~m);
0051 }
0052 
0053 static inline int bb_read(u32 __iomem *p, u32 m)
0054 {
0055     return (in_be32(p) & m) != 0;
0056 }
0057 
0058 static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
0059 {
0060     struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
0061 
0062     if (dir)
0063         bb_set(bitbang->dir, bitbang->mdio_msk);
0064     else
0065         bb_clr(bitbang->dir, bitbang->mdio_msk);
0066 
0067     /* Read back to flush the write. */
0068     in_be32(bitbang->dir);
0069 }
0070 
0071 static inline int mdio_read(struct mdiobb_ctrl *ctrl)
0072 {
0073     struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
0074     return bb_read(bitbang->dat, bitbang->mdio_msk);
0075 }
0076 
0077 static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
0078 {
0079     struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
0080 
0081     if (what)
0082         bb_set(bitbang->dat, bitbang->mdio_msk);
0083     else
0084         bb_clr(bitbang->dat, bitbang->mdio_msk);
0085 
0086     /* Read back to flush the write. */
0087     in_be32(bitbang->dat);
0088 }
0089 
0090 static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
0091 {
0092     struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
0093 
0094     if (what)
0095         bb_set(bitbang->dat, bitbang->mdc_msk);
0096     else
0097         bb_clr(bitbang->dat, bitbang->mdc_msk);
0098 
0099     /* Read back to flush the write. */
0100     in_be32(bitbang->dat);
0101 }
0102 
0103 static const struct mdiobb_ops bb_ops = {
0104     .owner = THIS_MODULE,
0105     .set_mdc = mdc,
0106     .set_mdio_dir = mdio_dir,
0107     .set_mdio_data = mdio,
0108     .get_mdio_data = mdio_read,
0109 };
0110 
0111 static int fs_mii_bitbang_init(struct mii_bus *bus, struct device_node *np)
0112 {
0113     struct resource res;
0114     const u32 *data;
0115     int mdio_pin, mdc_pin, len;
0116     struct bb_info *bitbang = bus->priv;
0117 
0118     int ret = of_address_to_resource(np, 0, &res);
0119     if (ret)
0120         return ret;
0121 
0122     if (resource_size(&res) <= 13)
0123         return -ENODEV;
0124 
0125     /* This should really encode the pin number as well, but all
0126      * we get is an int, and the odds of multiple bitbang mdio buses
0127      * is low enough that it's not worth going too crazy.
0128      */
0129     snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
0130 
0131     data = of_get_property(np, "fsl,mdio-pin", &len);
0132     if (!data || len != 4)
0133         return -ENODEV;
0134     mdio_pin = *data;
0135 
0136     data = of_get_property(np, "fsl,mdc-pin", &len);
0137     if (!data || len != 4)
0138         return -ENODEV;
0139     mdc_pin = *data;
0140 
0141     bitbang->dir = ioremap(res.start, resource_size(&res));
0142     if (!bitbang->dir)
0143         return -ENOMEM;
0144 
0145     bitbang->dat = bitbang->dir + 4;
0146     bitbang->mdio_msk = 1 << (31 - mdio_pin);
0147     bitbang->mdc_msk = 1 << (31 - mdc_pin);
0148 
0149     return 0;
0150 }
0151 
0152 static int fs_enet_mdio_probe(struct platform_device *ofdev)
0153 {
0154     struct mii_bus *new_bus;
0155     struct bb_info *bitbang;
0156     int ret = -ENOMEM;
0157 
0158     bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
0159     if (!bitbang)
0160         goto out;
0161 
0162     bitbang->ctrl.ops = &bb_ops;
0163 
0164     new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
0165     if (!new_bus)
0166         goto out_free_priv;
0167 
0168     new_bus->name = "CPM2 Bitbanged MII",
0169 
0170     ret = fs_mii_bitbang_init(new_bus, ofdev->dev.of_node);
0171     if (ret)
0172         goto out_free_bus;
0173 
0174     new_bus->phy_mask = ~0;
0175 
0176     new_bus->parent = &ofdev->dev;
0177     platform_set_drvdata(ofdev, new_bus);
0178 
0179     ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
0180     if (ret)
0181         goto out_unmap_regs;
0182 
0183     return 0;
0184 
0185 out_unmap_regs:
0186     iounmap(bitbang->dir);
0187 out_free_bus:
0188     free_mdio_bitbang(new_bus);
0189 out_free_priv:
0190     kfree(bitbang);
0191 out:
0192     return ret;
0193 }
0194 
0195 static int fs_enet_mdio_remove(struct platform_device *ofdev)
0196 {
0197     struct mii_bus *bus = platform_get_drvdata(ofdev);
0198     struct bb_info *bitbang = bus->priv;
0199 
0200     mdiobus_unregister(bus);
0201     free_mdio_bitbang(bus);
0202     iounmap(bitbang->dir);
0203     kfree(bitbang);
0204 
0205     return 0;
0206 }
0207 
0208 static const struct of_device_id fs_enet_mdio_bb_match[] = {
0209     {
0210         .compatible = "fsl,cpm2-mdio-bitbang",
0211     },
0212     {},
0213 };
0214 MODULE_DEVICE_TABLE(of, fs_enet_mdio_bb_match);
0215 
0216 static struct platform_driver fs_enet_bb_mdio_driver = {
0217     .driver = {
0218         .name = "fsl-bb-mdio",
0219         .of_match_table = fs_enet_mdio_bb_match,
0220     },
0221     .probe = fs_enet_mdio_probe,
0222     .remove = fs_enet_mdio_remove,
0223 };
0224 
0225 module_platform_driver(fs_enet_bb_mdio_driver);
0226 MODULE_LICENSE("GPL");