blob: 7658a082e390cd4c8d86c066cdf0d76aa1d72d80 [file] [log] [blame]
Domen Puncer5d031e92007-10-26 18:07:49 +02001/*
2 * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
3 *
4 * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
Wolfram Sang51d98612008-10-15 11:10:00 -06005 * Copyright (C) 2008 Wolfram Sang, Pengutronix
Domen Puncer5d031e92007-10-26 18:07:49 +02006 *
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.
10 */
11
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Grant Likelyca816d92009-04-25 12:53:02 +000018#include <linux/of_mdio.h>
Domen Puncer5d031e92007-10-26 18:07:49 +020019#include <asm/io.h>
20#include <asm/mpc52xx.h>
21#include "fec_mpc52xx.h"
22
23struct mpc52xx_fec_mdio_priv {
24 struct mpc52xx_fec __iomem *regs;
Grant Likelyca816d92009-04-25 12:53:02 +000025 int mdio_irqs[PHY_MAX_ADDR];
Domen Puncer5d031e92007-10-26 18:07:49 +020026};
27
Wolfram Sang51d98612008-10-15 11:10:00 -060028static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
29 int reg, u32 value)
Domen Puncer5d031e92007-10-26 18:07:49 +020030{
31 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
32 struct mpc52xx_fec __iomem *fec;
Grant Likelyca816d92009-04-25 12:53:02 +000033 int tries = 3;
Wolfram Sang51d98612008-10-15 11:10:00 -060034
35 value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
36 value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
Domen Puncer5d031e92007-10-26 18:07:49 +020037
38 fec = priv->regs;
39 out_be32(&fec->ievent, FEC_IEVENT_MII);
Wolfram Sang51d98612008-10-15 11:10:00 -060040 out_be32(&priv->regs->mii_data, value);
Domen Puncer5d031e92007-10-26 18:07:49 +020041
42 /* wait for it to finish, this takes about 23 us on lite5200b */
43 while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
Grant Likelyca816d92009-04-25 12:53:02 +000044 msleep(1);
Domen Puncer5d031e92007-10-26 18:07:49 +020045
Wolfram Sang51d98612008-10-15 11:10:00 -060046 if (!tries)
Domen Puncer5d031e92007-10-26 18:07:49 +020047 return -ETIMEDOUT;
48
Wolfram Sang51d98612008-10-15 11:10:00 -060049 return value & FEC_MII_DATA_OP_RD ?
50 in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK : 0;
Domen Puncer5d031e92007-10-26 18:07:49 +020051}
52
Wolfram Sang51d98612008-10-15 11:10:00 -060053static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
Domen Puncer5d031e92007-10-26 18:07:49 +020054{
Wolfram Sang51d98612008-10-15 11:10:00 -060055 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
Domen Puncer5d031e92007-10-26 18:07:49 +020056}
57
Wolfram Sang51d98612008-10-15 11:10:00 -060058static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
59 u16 data)
60{
61 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
62 data | FEC_MII_WRITE_FRAME);
63}
64
65static int mpc52xx_fec_mdio_probe(struct of_device *of,
66 const struct of_device_id *match)
Domen Puncer5d031e92007-10-26 18:07:49 +020067{
68 struct device *dev = &of->dev;
69 struct device_node *np = of->node;
Domen Puncer5d031e92007-10-26 18:07:49 +020070 struct mii_bus *bus;
71 struct mpc52xx_fec_mdio_priv *priv;
72 struct resource res = {};
73 int err;
74 int i;
75
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -070076 bus = mdiobus_alloc();
Domen Puncer5d031e92007-10-26 18:07:49 +020077 if (bus == NULL)
78 return -ENOMEM;
79 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
80 if (priv == NULL) {
81 err = -ENOMEM;
82 goto out_free;
83 }
84
85 bus->name = "mpc52xx MII bus";
86 bus->read = mpc52xx_fec_mdio_read;
87 bus->write = mpc52xx_fec_mdio_write;
88
89 /* setup irqs */
Grant Likelyca816d92009-04-25 12:53:02 +000090 bus->irq = priv->mdio_irqs;
Domen Puncer5d031e92007-10-26 18:07:49 +020091
92 /* setup registers */
93 err = of_address_to_resource(np, 0, &res);
94 if (err)
95 goto out_free;
96 priv->regs = ioremap(res.start, res.end - res.start + 1);
97 if (priv->regs == NULL) {
98 err = -ENOMEM;
99 goto out_free;
100 }
101
Andy Fleming9d9326d2008-04-09 19:38:13 -0500102 snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
Domen Puncer5d031e92007-10-26 18:07:49 +0200103 bus->priv = priv;
104
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000105 bus->parent = dev;
Domen Puncer5d031e92007-10-26 18:07:49 +0200106 dev_set_drvdata(dev, bus);
107
108 /* set MII speed */
Wolfram Sang51d98612008-10-15 11:10:00 -0600109 out_be32(&priv->regs->mii_speed,
Wolfgang Denk87c441e2009-06-17 00:30:22 -0600110 ((mpc5xxx_get_bus_frequency(of->node) >> 20) / 5) << 1);
Domen Puncer5d031e92007-10-26 18:07:49 +0200111
Grant Likelyca816d92009-04-25 12:53:02 +0000112 err = of_mdiobus_register(bus, np);
Domen Puncer5d031e92007-10-26 18:07:49 +0200113 if (err)
114 goto out_unmap;
115
116 return 0;
117
118 out_unmap:
119 iounmap(priv->regs);
120 out_free:
121 for (i=0; i<PHY_MAX_ADDR; i++)
122 if (bus->irq[i] != PHY_POLL)
123 irq_dispose_mapping(bus->irq[i]);
124 kfree(bus->irq);
125 kfree(priv);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700126 mdiobus_free(bus);
Domen Puncer5d031e92007-10-26 18:07:49 +0200127
128 return err;
129}
130
131static int mpc52xx_fec_mdio_remove(struct of_device *of)
132{
133 struct device *dev = &of->dev;
134 struct mii_bus *bus = dev_get_drvdata(dev);
135 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
136 int i;
137
138 mdiobus_unregister(bus);
139 dev_set_drvdata(dev, NULL);
140
141 iounmap(priv->regs);
142 for (i=0; i<PHY_MAX_ADDR; i++)
Wolfram Sange51f47a2008-12-21 02:54:30 -0700143 if (bus->irq[i] != PHY_POLL)
Domen Puncer5d031e92007-10-26 18:07:49 +0200144 irq_dispose_mapping(bus->irq[i]);
145 kfree(priv);
146 kfree(bus->irq);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700147 mdiobus_free(bus);
Domen Puncer5d031e92007-10-26 18:07:49 +0200148
149 return 0;
150}
151
152
153static struct of_device_id mpc52xx_fec_mdio_match[] = {
Grant Likely66ffbe42008-01-24 22:25:31 -0700154 { .compatible = "fsl,mpc5200b-mdio", },
René Bürgel8d813942008-04-03 19:58:37 +1100155 { .compatible = "fsl,mpc5200-mdio", },
Grant Likely66ffbe42008-01-24 22:25:31 -0700156 { .compatible = "mpc5200b-fec-phy", },
157 {}
Domen Puncer5d031e92007-10-26 18:07:49 +0200158};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700159MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
Domen Puncer5d031e92007-10-26 18:07:49 +0200160
161struct of_platform_driver mpc52xx_fec_mdio_driver = {
162 .name = "mpc5200b-fec-phy",
163 .probe = mpc52xx_fec_mdio_probe,
164 .remove = mpc52xx_fec_mdio_remove,
165 .match_table = mpc52xx_fec_mdio_match,
166};
167
168/* let fec driver call it, since this has to be registered before it */
169EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
170
171
172MODULE_LICENSE("Dual BSD/GPL");