Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / net / fec_mpc52xx_phy.c
blob1837584c4504dd82f56db27d9326d9763b3ffe83
1 /*
2 * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
4 * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/phy.h>
15 #include <linux/of_platform.h>
16 #include <asm/io.h>
17 #include <asm/mpc52xx.h>
18 #include "fec_mpc52xx.h"
20 struct mpc52xx_fec_mdio_priv {
21 struct mpc52xx_fec __iomem *regs;
24 static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
26 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
27 struct mpc52xx_fec __iomem *fec;
28 int tries = 100;
29 u32 request = FEC_MII_READ_FRAME;
31 fec = priv->regs;
32 out_be32(&fec->ievent, FEC_IEVENT_MII);
34 request |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
35 request |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
37 out_be32(&priv->regs->mii_data, request);
39 /* wait for it to finish, this takes about 23 us on lite5200b */
40 while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
41 udelay(5);
43 if (tries == 0)
44 return -ETIMEDOUT;
46 return in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK;
49 static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 data)
51 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
52 struct mpc52xx_fec __iomem *fec;
53 u32 value = data;
54 int tries = 100;
56 fec = priv->regs;
57 out_be32(&fec->ievent, FEC_IEVENT_MII);
59 value |= FEC_MII_WRITE_FRAME;
60 value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
61 value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
63 out_be32(&priv->regs->mii_data, value);
65 /* wait for request to finish */
66 while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
67 udelay(5);
69 if (tries == 0)
70 return -ETIMEDOUT;
72 return 0;
75 static int mpc52xx_fec_mdio_probe(struct of_device *of, const struct of_device_id *match)
77 struct device *dev = &of->dev;
78 struct device_node *np = of->node;
79 struct device_node *child = NULL;
80 struct mii_bus *bus;
81 struct mpc52xx_fec_mdio_priv *priv;
82 struct resource res = {};
83 int err;
84 int i;
86 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
87 if (bus == NULL)
88 return -ENOMEM;
89 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
90 if (priv == NULL) {
91 err = -ENOMEM;
92 goto out_free;
95 bus->name = "mpc52xx MII bus";
96 bus->read = mpc52xx_fec_mdio_read;
97 bus->write = mpc52xx_fec_mdio_write;
99 /* setup irqs */
100 bus->irq = kmalloc(sizeof(bus->irq[0]) * PHY_MAX_ADDR, GFP_KERNEL);
101 if (bus->irq == NULL) {
102 err = -ENOMEM;
103 goto out_free;
105 for (i=0; i<PHY_MAX_ADDR; i++)
106 bus->irq[i] = PHY_POLL;
108 while ((child = of_get_next_child(np, child)) != NULL) {
109 int irq = irq_of_parse_and_map(child, 0);
110 if (irq != NO_IRQ) {
111 const u32 *id = of_get_property(child, "reg", NULL);
112 bus->irq[*id] = irq;
116 /* setup registers */
117 err = of_address_to_resource(np, 0, &res);
118 if (err)
119 goto out_free;
120 priv->regs = ioremap(res.start, res.end - res.start + 1);
121 if (priv->regs == NULL) {
122 err = -ENOMEM;
123 goto out_free;
126 bus->id = res.start;
127 bus->priv = priv;
129 bus->dev = dev;
130 dev_set_drvdata(dev, bus);
132 /* set MII speed */
133 out_be32(&priv->regs->mii_speed, ((mpc52xx_find_ipb_freq(of->node) >> 20) / 5) << 1);
135 /* enable MII interrupt */
136 out_be32(&priv->regs->imask, in_be32(&priv->regs->imask) | FEC_IMASK_MII);
138 err = mdiobus_register(bus);
139 if (err)
140 goto out_unmap;
142 return 0;
144 out_unmap:
145 iounmap(priv->regs);
146 out_free:
147 for (i=0; i<PHY_MAX_ADDR; i++)
148 if (bus->irq[i] != PHY_POLL)
149 irq_dispose_mapping(bus->irq[i]);
150 kfree(bus->irq);
151 kfree(priv);
152 kfree(bus);
154 return err;
157 static int mpc52xx_fec_mdio_remove(struct of_device *of)
159 struct device *dev = &of->dev;
160 struct mii_bus *bus = dev_get_drvdata(dev);
161 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
162 int i;
164 mdiobus_unregister(bus);
165 dev_set_drvdata(dev, NULL);
167 iounmap(priv->regs);
168 for (i=0; i<PHY_MAX_ADDR; i++)
169 if (bus->irq[i])
170 irq_dispose_mapping(bus->irq[i]);
171 kfree(priv);
172 kfree(bus->irq);
173 kfree(bus);
175 return 0;
179 static struct of_device_id mpc52xx_fec_mdio_match[] = {
180 { .compatible = "fsl,mpc5200b-mdio", },
181 { .compatible = "mpc5200b-fec-phy", },
185 struct of_platform_driver mpc52xx_fec_mdio_driver = {
186 .name = "mpc5200b-fec-phy",
187 .probe = mpc52xx_fec_mdio_probe,
188 .remove = mpc52xx_fec_mdio_remove,
189 .match_table = mpc52xx_fec_mdio_match,
192 /* let fec driver call it, since this has to be registered before it */
193 EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
196 MODULE_LICENSE("Dual BSD/GPL");