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/of_platform.h>
17 #include <linux/of_mdio.h>
19 #include <asm/mpc52xx.h>
20 #include "fec_mpc52xx.h"
22 struct mpc52xx_fec_mdio_priv {
23 struct mpc52xx_fec __iomem *regs;
24 int mdio_irqs[PHY_MAX_ADDR];
27 static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
30 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
31 struct mpc52xx_fec __iomem *fec;
34 value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
35 value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
38 out_be32(&fec->ievent, FEC_IEVENT_MII);
39 out_be32(&priv->regs->mii_data, value);
41 /* wait for it to finish, this takes about 23 us on lite5200b */
42 while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
48 return value & FEC_MII_DATA_OP_RD ?
49 in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK : 0;
52 static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
54 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
57 static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
60 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
61 data | FEC_MII_WRITE_FRAME);
64 static int mpc52xx_fec_mdio_probe(struct of_device *of,
65 const struct of_device_id *match)
67 struct device *dev = &of->dev;
68 struct device_node *np = of->node;
70 struct mpc52xx_fec_mdio_priv *priv;
71 struct resource res = {};
75 bus = mdiobus_alloc();
78 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
84 bus->name = "mpc52xx MII bus";
85 bus->read = mpc52xx_fec_mdio_read;
86 bus->write = mpc52xx_fec_mdio_write;
89 bus->irq = priv->mdio_irqs;
92 err = of_address_to_resource(np, 0, &res);
95 priv->regs = ioremap(res.start, res.end - res.start + 1);
96 if (priv->regs == NULL) {
101 snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
105 dev_set_drvdata(dev, bus);
108 out_be32(&priv->regs->mii_speed,
109 ((mpc52xx_find_ipb_freq(of->node) >> 20) / 5) << 1);
111 err = of_mdiobus_register(bus, np);
120 for (i=0; i<PHY_MAX_ADDR; i++)
121 if (bus->irq[i] != PHY_POLL)
122 irq_dispose_mapping(bus->irq[i]);
130 static int mpc52xx_fec_mdio_remove(struct of_device *of)
132 struct device *dev = &of->dev;
133 struct mii_bus *bus = dev_get_drvdata(dev);
134 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
137 mdiobus_unregister(bus);
138 dev_set_drvdata(dev, NULL);
141 for (i=0; i<PHY_MAX_ADDR; i++)
142 if (bus->irq[i] != PHY_POLL)
143 irq_dispose_mapping(bus->irq[i]);
152 static struct of_device_id mpc52xx_fec_mdio_match[] = {
153 { .compatible = "fsl,mpc5200b-mdio", },
154 { .compatible = "fsl,mpc5200-mdio", },
155 { .compatible = "mpc5200b-fec-phy", },
159 struct of_platform_driver mpc52xx_fec_mdio_driver = {
160 .name = "mpc5200b-fec-phy",
161 .probe = mpc52xx_fec_mdio_probe,
162 .remove = mpc52xx_fec_mdio_remove,
163 .match_table = mpc52xx_fec_mdio_match,
166 /* let fec driver call it, since this has to be registered before it */
167 EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
170 MODULE_LICENSE("Dual BSD/GPL");