1ec21e2ecSJeff Kirsher /*
2ec21e2ecSJeff Kirsher * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
3ec21e2ecSJeff Kirsher *
4ec21e2ecSJeff Kirsher * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
5ec21e2ecSJeff Kirsher * Copyright (C) 2008 Wolfram Sang, Pengutronix
6ec21e2ecSJeff Kirsher *
7ec21e2ecSJeff Kirsher * This file is licensed under the terms of the GNU General Public License
8ec21e2ecSJeff Kirsher * version 2. This program is licensed "as is" without any warranty of any
9ec21e2ecSJeff Kirsher * kind, whether express or implied.
10ec21e2ecSJeff Kirsher */
11ec21e2ecSJeff Kirsher
12ec21e2ecSJeff Kirsher #include <linux/kernel.h>
13ec21e2ecSJeff Kirsher #include <linux/module.h>
14ec21e2ecSJeff Kirsher #include <linux/netdevice.h>
15ec21e2ecSJeff Kirsher #include <linux/phy.h>
16ec21e2ecSJeff Kirsher #include <linux/slab.h>
173d40aed8SRob Herring #include <linux/of.h>
186bff3ffcSChristophe Leroy #include <linux/of_address.h>
19ec21e2ecSJeff Kirsher #include <linux/of_mdio.h>
203d40aed8SRob Herring #include <linux/platform_device.h>
21ec21e2ecSJeff Kirsher #include <asm/io.h>
22ec21e2ecSJeff Kirsher #include <asm/mpc52xx.h>
23ec21e2ecSJeff Kirsher #include "fec_mpc52xx.h"
24ec21e2ecSJeff Kirsher
25ec21e2ecSJeff Kirsher struct mpc52xx_fec_mdio_priv {
26ec21e2ecSJeff Kirsher struct mpc52xx_fec __iomem *regs;
27ec21e2ecSJeff Kirsher };
28ec21e2ecSJeff Kirsher
mpc52xx_fec_mdio_transfer(struct mii_bus * bus,int phy_id,int reg,u32 value)29ec21e2ecSJeff Kirsher static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
30ec21e2ecSJeff Kirsher int reg, u32 value)
31ec21e2ecSJeff Kirsher {
32ec21e2ecSJeff Kirsher struct mpc52xx_fec_mdio_priv *priv = bus->priv;
33ec21e2ecSJeff Kirsher struct mpc52xx_fec __iomem *fec = priv->regs;
34ec21e2ecSJeff Kirsher int tries = 3;
35ec21e2ecSJeff Kirsher
36ec21e2ecSJeff Kirsher value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
37ec21e2ecSJeff Kirsher value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
38ec21e2ecSJeff Kirsher
39ec21e2ecSJeff Kirsher out_be32(&fec->ievent, FEC_IEVENT_MII);
40ec21e2ecSJeff Kirsher out_be32(&fec->mii_data, value);
41ec21e2ecSJeff Kirsher
42ec21e2ecSJeff Kirsher /* wait for it to finish, this takes about 23 us on lite5200b */
43ec21e2ecSJeff Kirsher while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
44ec21e2ecSJeff Kirsher msleep(1);
45ec21e2ecSJeff Kirsher
46ec21e2ecSJeff Kirsher if (!tries)
47ec21e2ecSJeff Kirsher return -ETIMEDOUT;
48ec21e2ecSJeff Kirsher
49ec21e2ecSJeff Kirsher return value & FEC_MII_DATA_OP_RD ?
50ec21e2ecSJeff Kirsher in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
51ec21e2ecSJeff Kirsher }
52ec21e2ecSJeff Kirsher
mpc52xx_fec_mdio_read(struct mii_bus * bus,int phy_id,int reg)53ec21e2ecSJeff Kirsher static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
54ec21e2ecSJeff Kirsher {
55ec21e2ecSJeff Kirsher return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
56ec21e2ecSJeff Kirsher }
57ec21e2ecSJeff Kirsher
mpc52xx_fec_mdio_write(struct mii_bus * bus,int phy_id,int reg,u16 data)58ec21e2ecSJeff Kirsher static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
59ec21e2ecSJeff Kirsher u16 data)
60ec21e2ecSJeff Kirsher {
61ec21e2ecSJeff Kirsher return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
62ec21e2ecSJeff Kirsher data | FEC_MII_WRITE_FRAME);
63ec21e2ecSJeff Kirsher }
64ec21e2ecSJeff Kirsher
mpc52xx_fec_mdio_probe(struct platform_device * of)65ec21e2ecSJeff Kirsher static int mpc52xx_fec_mdio_probe(struct platform_device *of)
66ec21e2ecSJeff Kirsher {
67ec21e2ecSJeff Kirsher struct device *dev = &of->dev;
68ec21e2ecSJeff Kirsher struct device_node *np = of->dev.of_node;
69ec21e2ecSJeff Kirsher struct mii_bus *bus;
70ec21e2ecSJeff Kirsher struct mpc52xx_fec_mdio_priv *priv;
71ec21e2ecSJeff Kirsher struct resource res;
72ec21e2ecSJeff Kirsher int err;
73ec21e2ecSJeff Kirsher
74ec21e2ecSJeff Kirsher bus = mdiobus_alloc();
75ec21e2ecSJeff Kirsher if (bus == NULL)
76ec21e2ecSJeff Kirsher return -ENOMEM;
77ec21e2ecSJeff Kirsher priv = kzalloc(sizeof(*priv), GFP_KERNEL);
78ec21e2ecSJeff Kirsher if (priv == NULL) {
79ec21e2ecSJeff Kirsher err = -ENOMEM;
80ec21e2ecSJeff Kirsher goto out_free;
81ec21e2ecSJeff Kirsher }
82ec21e2ecSJeff Kirsher
83ec21e2ecSJeff Kirsher bus->name = "mpc52xx MII bus";
84ec21e2ecSJeff Kirsher bus->read = mpc52xx_fec_mdio_read;
85ec21e2ecSJeff Kirsher bus->write = mpc52xx_fec_mdio_write;
86ec21e2ecSJeff Kirsher
87ec21e2ecSJeff Kirsher /* setup registers */
88ec21e2ecSJeff Kirsher err = of_address_to_resource(np, 0, &res);
89ec21e2ecSJeff Kirsher if (err)
90ec21e2ecSJeff Kirsher goto out_free;
91ec21e2ecSJeff Kirsher priv->regs = ioremap(res.start, resource_size(&res));
92ec21e2ecSJeff Kirsher if (priv->regs == NULL) {
93ec21e2ecSJeff Kirsher err = -ENOMEM;
94ec21e2ecSJeff Kirsher goto out_free;
95ec21e2ecSJeff Kirsher }
96ec21e2ecSJeff Kirsher
97*6c591b8dSSimon Horman snprintf(bus->id, MII_BUS_ID_SIZE, "%pa", &res.start);
98ec21e2ecSJeff Kirsher bus->priv = priv;
99ec21e2ecSJeff Kirsher
100ec21e2ecSJeff Kirsher bus->parent = dev;
101ec21e2ecSJeff Kirsher dev_set_drvdata(dev, bus);
102ec21e2ecSJeff Kirsher
103ec21e2ecSJeff Kirsher /* set MII speed */
104de06fba6SAndy Shevchenko out_be32(&priv->regs->mii_speed, ((mpc5xxx_get_bus_frequency(dev) >> 20) / 5) << 1);
105ec21e2ecSJeff Kirsher
106ec21e2ecSJeff Kirsher err = of_mdiobus_register(bus, np);
107ec21e2ecSJeff Kirsher if (err)
108ec21e2ecSJeff Kirsher goto out_unmap;
109ec21e2ecSJeff Kirsher
110ec21e2ecSJeff Kirsher return 0;
111ec21e2ecSJeff Kirsher
112ec21e2ecSJeff Kirsher out_unmap:
113ec21e2ecSJeff Kirsher iounmap(priv->regs);
114ec21e2ecSJeff Kirsher out_free:
115ec21e2ecSJeff Kirsher kfree(priv);
116ec21e2ecSJeff Kirsher mdiobus_free(bus);
117ec21e2ecSJeff Kirsher
118ec21e2ecSJeff Kirsher return err;
119ec21e2ecSJeff Kirsher }
120ec21e2ecSJeff Kirsher
mpc52xx_fec_mdio_remove(struct platform_device * of)12112d6cc19SUwe Kleine-König static void mpc52xx_fec_mdio_remove(struct platform_device *of)
122ec21e2ecSJeff Kirsher {
1234014e360SLibo Chen struct mii_bus *bus = platform_get_drvdata(of);
124ec21e2ecSJeff Kirsher struct mpc52xx_fec_mdio_priv *priv = bus->priv;
125ec21e2ecSJeff Kirsher
126ec21e2ecSJeff Kirsher mdiobus_unregister(bus);
127ec21e2ecSJeff Kirsher iounmap(priv->regs);
128ec21e2ecSJeff Kirsher kfree(priv);
129ec21e2ecSJeff Kirsher mdiobus_free(bus);
130ec21e2ecSJeff Kirsher }
131ec21e2ecSJeff Kirsher
13294e5a2a8SFabian Frederick static const struct of_device_id mpc52xx_fec_mdio_match[] = {
133ec21e2ecSJeff Kirsher { .compatible = "fsl,mpc5200b-mdio", },
134ec21e2ecSJeff Kirsher { .compatible = "fsl,mpc5200-mdio", },
135ec21e2ecSJeff Kirsher { .compatible = "mpc5200b-fec-phy", },
136ec21e2ecSJeff Kirsher {}
137ec21e2ecSJeff Kirsher };
138ec21e2ecSJeff Kirsher MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
139ec21e2ecSJeff Kirsher
140ec21e2ecSJeff Kirsher struct platform_driver mpc52xx_fec_mdio_driver = {
141ec21e2ecSJeff Kirsher .driver = {
142ec21e2ecSJeff Kirsher .name = "mpc5200b-fec-phy",
143ec21e2ecSJeff Kirsher .owner = THIS_MODULE,
144ec21e2ecSJeff Kirsher .of_match_table = mpc52xx_fec_mdio_match,
145ec21e2ecSJeff Kirsher },
146ec21e2ecSJeff Kirsher .probe = mpc52xx_fec_mdio_probe,
14712d6cc19SUwe Kleine-König .remove_new = mpc52xx_fec_mdio_remove,
148ec21e2ecSJeff Kirsher };
149ec21e2ecSJeff Kirsher
150ec21e2ecSJeff Kirsher /* let fec driver call it, since this has to be registered before it */
151ec21e2ecSJeff Kirsher EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
152ec21e2ecSJeff Kirsher
153ec21e2ecSJeff Kirsher MODULE_LICENSE("Dual BSD/GPL");
154