blob: 31e6d62b785d81beb13ea7b4f76792975a385127 [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>
Grant Likelyca816d92009-04-25 12:53:02 +000017#include <linux/of_mdio.h>
Domen Puncer5d031e92007-10-26 18:07:49 +020018#include <asm/io.h>
19#include <asm/mpc52xx.h>
20#include "fec_mpc52xx.h"
21
22struct mpc52xx_fec_mdio_priv {
23 struct mpc52xx_fec __iomem *regs;
Grant Likelyca816d92009-04-25 12:53:02 +000024 int mdio_irqs[PHY_MAX_ADDR];
Domen Puncer5d031e92007-10-26 18:07:49 +020025};
26
Wolfram Sang51d98612008-10-15 11:10:00 -060027static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
28 int reg, u32 value)
Domen Puncer5d031e92007-10-26 18:07:49 +020029{
30 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
31 struct mpc52xx_fec __iomem *fec;
Grant Likelyca816d92009-04-25 12:53:02 +000032 int tries = 3;
Wolfram Sang51d98612008-10-15 11:10:00 -060033
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;
Domen Puncer5d031e92007-10-26 18:07:49 +020036
37 fec = priv->regs;
38 out_be32(&fec->ievent, FEC_IEVENT_MII);
Wolfram Sang51d98612008-10-15 11:10:00 -060039 out_be32(&priv->regs->mii_data, value);
Domen Puncer5d031e92007-10-26 18:07:49 +020040
41 /* wait for it to finish, this takes about 23 us on lite5200b */
42 while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
Grant Likelyca816d92009-04-25 12:53:02 +000043 msleep(1);
Domen Puncer5d031e92007-10-26 18:07:49 +020044
Wolfram Sang51d98612008-10-15 11:10:00 -060045 if (!tries)
Domen Puncer5d031e92007-10-26 18:07:49 +020046 return -ETIMEDOUT;
47
Wolfram Sang51d98612008-10-15 11:10:00 -060048 return value & FEC_MII_DATA_OP_RD ?
49 in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK : 0;
Domen Puncer5d031e92007-10-26 18:07:49 +020050}
51
Wolfram Sang51d98612008-10-15 11:10:00 -060052static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
Domen Puncer5d031e92007-10-26 18:07:49 +020053{
Wolfram Sang51d98612008-10-15 11:10:00 -060054 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
Domen Puncer5d031e92007-10-26 18:07:49 +020055}
56
Wolfram Sang51d98612008-10-15 11:10:00 -060057static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
58 u16 data)
59{
60 return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
61 data | FEC_MII_WRITE_FRAME);
62}
63
64static int mpc52xx_fec_mdio_probe(struct of_device *of,
65 const struct of_device_id *match)
Domen Puncer5d031e92007-10-26 18:07:49 +020066{
67 struct device *dev = &of->dev;
68 struct device_node *np = of->node;
Domen Puncer5d031e92007-10-26 18:07:49 +020069 struct mii_bus *bus;
70 struct mpc52xx_fec_mdio_priv *priv;
71 struct resource res = {};
72 int err;
73 int i;
74
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070075 bus = mdiobus_alloc();
Domen Puncer5d031e92007-10-26 18:07:49 +020076 if (bus == NULL)
77 return -ENOMEM;
78 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
79 if (priv == NULL) {
80 err = -ENOMEM;
81 goto out_free;
82 }
83
84 bus->name = "mpc52xx MII bus";
85 bus->read = mpc52xx_fec_mdio_read;
86 bus->write = mpc52xx_fec_mdio_write;
87
88 /* setup irqs */
Grant Likelyca816d92009-04-25 12:53:02 +000089 bus->irq = priv->mdio_irqs;
Domen Puncer5d031e92007-10-26 18:07:49 +020090
91 /* setup registers */
92 err = of_address_to_resource(np, 0, &res);
93 if (err)
94 goto out_free;
95 priv->regs = ioremap(res.start, res.end - res.start + 1);
96 if (priv->regs == NULL) {
97 err = -ENOMEM;
98 goto out_free;
99 }
100
Andy Fleming9d9326d2008-04-09 19:38:13 -0500101 snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
Domen Puncer5d031e92007-10-26 18:07:49 +0200102 bus->priv = priv;
103
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000104 bus->parent = dev;
Domen Puncer5d031e92007-10-26 18:07:49 +0200105 dev_set_drvdata(dev, bus);
106
107 /* set MII speed */
Wolfram Sang51d98612008-10-15 11:10:00 -0600108 out_be32(&priv->regs->mii_speed,
Wolfgang Denk87c441e2009-06-17 00:30:22 -0600109 ((mpc5xxx_get_bus_frequency(of->node) >> 20) / 5) << 1);
Domen Puncer5d031e92007-10-26 18:07:49 +0200110
Grant Likelyca816d92009-04-25 12:53:02 +0000111 err = of_mdiobus_register(bus, np);
Domen Puncer5d031e92007-10-26 18:07:49 +0200112 if (err)
113 goto out_unmap;
114
115 return 0;
116
117 out_unmap:
118 iounmap(priv->regs);
119 out_free:
120 for (i=0; i<PHY_MAX_ADDR; i++)
121 if (bus->irq[i] != PHY_POLL)
122 irq_dispose_mapping(bus->irq[i]);
123 kfree(bus->irq);
124 kfree(priv);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700125 mdiobus_free(bus);
Domen Puncer5d031e92007-10-26 18:07:49 +0200126
127 return err;
128}
129
130static int mpc52xx_fec_mdio_remove(struct of_device *of)
131{
132 struct device *dev = &of->dev;
133 struct mii_bus *bus = dev_get_drvdata(dev);
134 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
135 int i;
136
137 mdiobus_unregister(bus);
138 dev_set_drvdata(dev, NULL);
139
140 iounmap(priv->regs);
141 for (i=0; i<PHY_MAX_ADDR; i++)
Wolfram Sange51f47a2008-12-21 02:54:30 -0700142 if (bus->irq[i] != PHY_POLL)
Domen Puncer5d031e92007-10-26 18:07:49 +0200143 irq_dispose_mapping(bus->irq[i]);
144 kfree(priv);
145 kfree(bus->irq);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700146 mdiobus_free(bus);
Domen Puncer5d031e92007-10-26 18:07:49 +0200147
148 return 0;
149}
150
151
152static struct of_device_id mpc52xx_fec_mdio_match[] = {
Grant Likely66ffbe42008-01-24 22:25:31 -0700153 { .compatible = "fsl,mpc5200b-mdio", },
René Bürgel8d813942008-04-03 19:58:37 +1100154 { .compatible = "fsl,mpc5200-mdio", },
Grant Likely66ffbe42008-01-24 22:25:31 -0700155 { .compatible = "mpc5200b-fec-phy", },
156 {}
Domen Puncer5d031e92007-10-26 18:07:49 +0200157};
158
159struct 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,
164};
165
166/* let fec driver call it, since this has to be registered before it */
167EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
168
169
170MODULE_LICENSE("Dual BSD/GPL");