2 * QorIQ 10G MDIO Controller
4 * Copyright 2012 Freescale Semiconductor, Inc.
6 * Authors: Andy Fleming <afleming@freescale.com>
7 * Timur Tabi <timur@freescale.com>
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/phy.h>
19 #include <linux/mdio.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22 #include <linux/of_mdio.h>
24 /* Number of microseconds to wait for a register to respond */
27 struct tgec_mdio_controller {
29 __be32 mdio_stat; /* MDIO configuration and status */
30 __be32 mdio_ctl; /* MDIO control */
31 __be32 mdio_data; /* MDIO data */
32 __be32 mdio_addr; /* MDIO address */
35 #define MDIO_STAT_ENC BIT(6)
36 #define MDIO_STAT_CLKDIV(x) (((x>>1) & 0xff) << 8)
37 #define MDIO_STAT_BSY (1 << 0)
38 #define MDIO_STAT_RD_ER (1 << 1)
39 #define MDIO_CTL_DEV_ADDR(x) (x & 0x1f)
40 #define MDIO_CTL_PORT_ADDR(x) ((x & 0x1f) << 5)
41 #define MDIO_CTL_PRE_DIS (1 << 10)
42 #define MDIO_CTL_SCAN_EN (1 << 11)
43 #define MDIO_CTL_POST_INC (1 << 14)
44 #define MDIO_CTL_READ (1 << 15)
46 #define MDIO_DATA(x) (x & 0xffff)
47 #define MDIO_DATA_BSY (1 << 31)
50 * Wait until the MDIO bus is free
52 static int xgmac_wait_until_free(struct device *dev,
53 struct tgec_mdio_controller __iomem *regs)
57 /* Wait till the bus is free */
58 status = spin_event_timeout(
59 !((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY), TIMEOUT, 0);
61 dev_err(dev, "timeout waiting for bus to be free\n");
69 * Wait till the MDIO read or write operation is complete
71 static int xgmac_wait_until_done(struct device *dev,
72 struct tgec_mdio_controller __iomem *regs)
76 /* Wait till the MDIO write is complete */
77 status = spin_event_timeout(
78 !((in_be32(®s->mdio_data)) & MDIO_DATA_BSY), TIMEOUT, 0);
80 dev_err(dev, "timeout waiting for operation to complete\n");
88 * Write value to the PHY for this device to the register at regnum,waiting
89 * until the write is done before it returns. All PHY configuration has to be
90 * done through the TSEC1 MIIM regs.
92 static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value)
94 struct tgec_mdio_controller __iomem *regs = bus->priv;
96 u32 mdio_ctl, mdio_stat;
99 mdio_stat = in_be32(®s->mdio_stat);
100 if (regnum & MII_ADDR_C45) {
101 /* Clause 45 (ie 10G) */
102 dev_addr = (regnum >> 16) & 0x1f;
103 mdio_stat |= MDIO_STAT_ENC;
105 /* Clause 22 (ie 1G) */
106 dev_addr = regnum & 0x1f;
107 mdio_stat &= ~MDIO_STAT_ENC;
110 out_be32(®s->mdio_stat, mdio_stat);
112 ret = xgmac_wait_until_free(&bus->dev, regs);
116 /* Set the port and dev addr */
117 mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
118 out_be32(®s->mdio_ctl, mdio_ctl);
120 /* Set the register address */
121 if (regnum & MII_ADDR_C45) {
122 out_be32(®s->mdio_addr, regnum & 0xffff);
124 ret = xgmac_wait_until_free(&bus->dev, regs);
129 /* Write the value to the register */
130 out_be32(®s->mdio_data, MDIO_DATA(value));
132 ret = xgmac_wait_until_done(&bus->dev, regs);
140 * Reads from register regnum in the PHY for device dev, returning the value.
141 * Clears miimcom first. All PHY configuration has to be done through the
144 static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
146 struct tgec_mdio_controller __iomem *regs = bus->priv;
153 mdio_stat = in_be32(®s->mdio_stat);
154 if (regnum & MII_ADDR_C45) {
155 dev_addr = (regnum >> 16) & 0x1f;
156 mdio_stat |= MDIO_STAT_ENC;
158 dev_addr = regnum & 0x1f;
159 mdio_stat &= ~MDIO_STAT_ENC;
162 out_be32(®s->mdio_stat, mdio_stat);
164 ret = xgmac_wait_until_free(&bus->dev, regs);
168 /* Set the Port and Device Addrs */
169 mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
170 out_be32(®s->mdio_ctl, mdio_ctl);
172 /* Set the register address */
173 if (regnum & MII_ADDR_C45) {
174 out_be32(®s->mdio_addr, regnum & 0xffff);
176 ret = xgmac_wait_until_free(&bus->dev, regs);
181 /* Initiate the read */
182 out_be32(®s->mdio_ctl, mdio_ctl | MDIO_CTL_READ);
184 ret = xgmac_wait_until_done(&bus->dev, regs);
188 /* Return all Fs if nothing was there */
189 if (in_be32(®s->mdio_stat) & MDIO_STAT_RD_ER) {
191 "Error while reading PHY%d reg at %d.%hhu\n",
192 phy_id, dev_addr, regnum);
196 value = in_be32(®s->mdio_data) & 0xffff;
197 dev_dbg(&bus->dev, "read %04x\n", value);
202 static int xgmac_mdio_probe(struct platform_device *pdev)
204 struct device_node *np = pdev->dev.of_node;
209 ret = of_address_to_resource(np, 0, &res);
211 dev_err(&pdev->dev, "could not obtain address\n");
215 bus = mdiobus_alloc();
219 bus->name = "Freescale XGMAC MDIO Bus";
220 bus->read = xgmac_mdio_read;
221 bus->write = xgmac_mdio_write;
222 bus->parent = &pdev->dev;
223 snprintf(bus->id, MII_BUS_ID_SIZE, "%llx", (unsigned long long)res.start);
225 /* Set the PHY base address */
226 bus->priv = of_iomap(np, 0);
232 ret = of_mdiobus_register(bus, np);
234 dev_err(&pdev->dev, "cannot register MDIO bus\n");
235 goto err_registration;
238 platform_set_drvdata(pdev, bus);
251 static int xgmac_mdio_remove(struct platform_device *pdev)
253 struct mii_bus *bus = platform_get_drvdata(pdev);
255 mdiobus_unregister(bus);
262 static struct of_device_id xgmac_mdio_match[] = {
264 .compatible = "fsl,fman-xmdio",
267 .compatible = "fsl,fman-memac-mdio",
271 MODULE_DEVICE_TABLE(of, xgmac_mdio_match);
273 static struct platform_driver xgmac_mdio_driver = {
275 .name = "fsl-fman_xmdio",
276 .of_match_table = xgmac_mdio_match,
278 .probe = xgmac_mdio_probe,
279 .remove = xgmac_mdio_remove,
282 module_platform_driver(xgmac_mdio_driver);
284 MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller");
285 MODULE_LICENSE("GPL v2");