blob: 360a578c2bb7feffb7e36592a4750f577574fd52 [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;
Wolfram Sange9509552010-06-02 03:45:22 -070032 struct mpc52xx_fec __iomem *fec = priv->regs;
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
Domen Puncer5d031e92007-10-26 18:07:49 +020038 out_be32(&fec->ievent, FEC_IEVENT_MII);
Wolfram Sange9509552010-06-02 03:45:22 -070039 out_be32(&fec->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 ?
Wolfram Sange9509552010-06-02 03:45:22 -070049 in_be32(&fec->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
Grant Likely74888762011-02-22 21:05:51 -070064static int mpc52xx_fec_mdio_probe(struct platform_device *of)
Domen Puncer5d031e92007-10-26 18:07:49 +020065{
66 struct device *dev = &of->dev;
Grant Likely61c7a082010-04-13 16:12:29 -070067 struct device_node *np = of->dev.of_node;
Domen Puncer5d031e92007-10-26 18:07:49 +020068 struct mii_bus *bus;
69 struct mpc52xx_fec_mdio_priv *priv;
Wolfram Sange9509552010-06-02 03:45:22 -070070 struct resource res;
Domen Puncer5d031e92007-10-26 18:07:49 +020071 int err;
Domen Puncer5d031e92007-10-26 18:07:49 +020072
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -070073 bus = mdiobus_alloc();
Domen Puncer5d031e92007-10-26 18:07:49 +020074 if (bus == NULL)
75 return -ENOMEM;
76 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
77 if (priv == NULL) {
78 err = -ENOMEM;
79 goto out_free;
80 }
81
82 bus->name = "mpc52xx MII bus";
83 bus->read = mpc52xx_fec_mdio_read;
84 bus->write = mpc52xx_fec_mdio_write;
85
86 /* setup irqs */
Grant Likelyca816d92009-04-25 12:53:02 +000087 bus->irq = priv->mdio_irqs;
Domen Puncer5d031e92007-10-26 18:07:49 +020088
89 /* setup registers */
90 err = of_address_to_resource(np, 0, &res);
91 if (err)
92 goto out_free;
Wolfram Sange9509552010-06-02 03:45:22 -070093 priv->regs = ioremap(res.start, resource_size(&res));
Domen Puncer5d031e92007-10-26 18:07:49 +020094 if (priv->regs == NULL) {
95 err = -ENOMEM;
96 goto out_free;
97 }
98
Andy Fleming9d9326d2008-04-09 19:38:13 -050099 snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
Domen Puncer5d031e92007-10-26 18:07:49 +0200100 bus->priv = priv;
101
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000102 bus->parent = dev;
Domen Puncer5d031e92007-10-26 18:07:49 +0200103 dev_set_drvdata(dev, bus);
104
105 /* set MII speed */
Wolfram Sang51d98612008-10-15 11:10:00 -0600106 out_be32(&priv->regs->mii_speed,
Grant Likely61c7a082010-04-13 16:12:29 -0700107 ((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 20) / 5) << 1);
Domen Puncer5d031e92007-10-26 18:07:49 +0200108
Grant Likelyca816d92009-04-25 12:53:02 +0000109 err = of_mdiobus_register(bus, np);
Domen Puncer5d031e92007-10-26 18:07:49 +0200110 if (err)
111 goto out_unmap;
112
113 return 0;
114
115 out_unmap:
116 iounmap(priv->regs);
117 out_free:
Domen Puncer5d031e92007-10-26 18:07:49 +0200118 kfree(priv);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700119 mdiobus_free(bus);
Domen Puncer5d031e92007-10-26 18:07:49 +0200120
121 return err;
122}
123
Grant Likely2dc11582010-08-06 09:25:50 -0600124static int mpc52xx_fec_mdio_remove(struct platform_device *of)
Domen Puncer5d031e92007-10-26 18:07:49 +0200125{
126 struct device *dev = &of->dev;
127 struct mii_bus *bus = dev_get_drvdata(dev);
128 struct mpc52xx_fec_mdio_priv *priv = bus->priv;
Domen Puncer5d031e92007-10-26 18:07:49 +0200129
130 mdiobus_unregister(bus);
131 dev_set_drvdata(dev, NULL);
Domen Puncer5d031e92007-10-26 18:07:49 +0200132 iounmap(priv->regs);
Domen Puncer5d031e92007-10-26 18:07:49 +0200133 kfree(priv);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700134 mdiobus_free(bus);
Domen Puncer5d031e92007-10-26 18:07:49 +0200135
136 return 0;
137}
138
Domen Puncer5d031e92007-10-26 18:07:49 +0200139static struct of_device_id mpc52xx_fec_mdio_match[] = {
Grant Likely66ffbe42008-01-24 22:25:31 -0700140 { .compatible = "fsl,mpc5200b-mdio", },
René Bürgel8d813942008-04-03 19:58:37 +1100141 { .compatible = "fsl,mpc5200-mdio", },
Grant Likely66ffbe42008-01-24 22:25:31 -0700142 { .compatible = "mpc5200b-fec-phy", },
143 {}
Domen Puncer5d031e92007-10-26 18:07:49 +0200144};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700145MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
Domen Puncer5d031e92007-10-26 18:07:49 +0200146
Grant Likely74888762011-02-22 21:05:51 -0700147struct platform_driver mpc52xx_fec_mdio_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700148 .driver = {
149 .name = "mpc5200b-fec-phy",
150 .owner = THIS_MODULE,
151 .of_match_table = mpc52xx_fec_mdio_match,
152 },
Domen Puncer5d031e92007-10-26 18:07:49 +0200153 .probe = mpc52xx_fec_mdio_probe,
154 .remove = mpc52xx_fec_mdio_remove,
Domen Puncer5d031e92007-10-26 18:07:49 +0200155};
156
157/* let fec driver call it, since this has to be registered before it */
158EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
159
Domen Puncer5d031e92007-10-26 18:07:49 +0200160MODULE_LICENSE("Dual BSD/GPL");