2 * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
4 * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
5 * Copyright (C) 2008 Wolfram Sang, Pengutronix
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/phy.h>
16 #include <linux/slab.h>
18 #include <linux/of_address.h>
19 #include <linux/of_mdio.h>
20 #include <linux/platform_device.h>
22 #include <asm/mpc52xx.h>
23 #include "fec_mpc52xx.h"
25 struct mpc52xx_fec_mdio_priv {
26 struct mpc52xx_fec __iomem *regs;
29 static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
32 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
33 struct mpc52xx_fec __iomem *fec = priv->regs;
36 value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
37 value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
39 out_be32(&fec->ievent, FEC_IEVENT_MII);
40 out_be32(&fec->mii_data, value);
42 /* wait for it to finish, this takes about 23 us on lite5200b */
43 while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
49 return value & FEC_MII_DATA_OP_RD ?
50 in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
53 static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
55 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
58 static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
61 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
62 data | FEC_MII_WRITE_FRAME);
65 static int mpc52xx_fec_mdio_probe(struct platform_device *of)
67 struct device *dev = &of->dev;
68 struct device_node *np = of->dev.of_node;
70 struct mpc52xx_fec_mdio_priv *priv;
74 bus = mdiobus_alloc();
77 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
83 bus->name = "mpc52xx MII bus";
84 bus->read = mpc52xx_fec_mdio_read;
85 bus->write = mpc52xx_fec_mdio_write;
88 err = of_address_to_resource(np, 0, &res);
91 priv->regs = ioremap(res.start, resource_size(&res));
92 if (priv->regs == NULL) {
97 snprintf(bus->id, MII_BUS_ID_SIZE, "%pa", &res.start);
101 dev_set_drvdata(dev, bus);
104 out_be32(&priv->regs->mii_speed, ((mpc5xxx_get_bus_frequency(dev) >> 20) / 5) << 1);
106 err = of_mdiobus_register(bus, np);
121 static void mpc52xx_fec_mdio_remove(struct platform_device *of)
123 struct mii_bus *bus = platform_get_drvdata(of);
124 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
126 mdiobus_unregister(bus);
132 static const struct of_device_id mpc52xx_fec_mdio_match[] = {
133 { .compatible = "fsl,mpc5200b-mdio", },
134 { .compatible = "fsl,mpc5200-mdio", },
135 { .compatible = "mpc5200b-fec-phy", },
138 MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
140 struct platform_driver mpc52xx_fec_mdio_driver = {
142 .name = "mpc5200b-fec-phy",
143 .owner = THIS_MODULE,
144 .of_match_table = mpc52xx_fec_mdio_match,
146 .probe = mpc52xx_fec_mdio_probe,
147 .remove = mpc52xx_fec_mdio_remove,
150 /* let fec driver call it, since this has to be registered before it */
151 EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
153 MODULE_LICENSE("Dual BSD/GPL");