blob: e0fc3d1115ea35e31332b78da0d6b2a672df831a [file] [log] [blame]
Timur Tabi9f35a732012-08-20 09:26:39 +00001/*
2 * QorIQ 10G MDIO Controller
3 *
4 * Copyright 2012 Freescale Semiconductor, Inc.
5 *
6 * Authors: Andy Fleming <afleming@freescale.com>
7 * Timur Tabi <timur@freescale.com>
8 *
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.
12 */
13
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>
Rob Herring5af50732013-09-17 14:28:33 -050020#include <linux/of_address.h>
Timur Tabi9f35a732012-08-20 09:26:39 +000021#include <linux/of_platform.h>
22#include <linux/of_mdio.h>
23
24/* Number of microseconds to wait for a register to respond */
25#define TIMEOUT 1000
26
27struct tgec_mdio_controller {
28 __be32 reserved[12];
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 */
33} __packed;
34
Andy Fleming1fcf77c2015-01-04 17:36:02 +080035#define MDIO_STAT_ENC BIT(6)
Timur Tabi9f35a732012-08-20 09:26:39 +000036#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)
45
46#define MDIO_DATA(x) (x & 0xffff)
47#define MDIO_DATA_BSY (1 << 31)
48
49/*
Madalin Bucurc1543d32014-07-29 14:47:25 -050050 * Wait until the MDIO bus is free
Timur Tabi9f35a732012-08-20 09:26:39 +000051 */
52static int xgmac_wait_until_free(struct device *dev,
53 struct tgec_mdio_controller __iomem *regs)
54{
55 uint32_t status;
56
57 /* Wait till the bus is free */
58 status = spin_event_timeout(
59 !((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY), TIMEOUT, 0);
60 if (!status) {
61 dev_err(dev, "timeout waiting for bus to be free\n");
62 return -ETIMEDOUT;
63 }
64
65 return 0;
66}
67
68/*
69 * Wait till the MDIO read or write operation is complete
70 */
71static int xgmac_wait_until_done(struct device *dev,
72 struct tgec_mdio_controller __iomem *regs)
73{
74 uint32_t status;
75
76 /* Wait till the MDIO write is complete */
77 status = spin_event_timeout(
78 !((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY), TIMEOUT, 0);
79 if (!status) {
80 dev_err(dev, "timeout waiting for operation to complete\n");
81 return -ETIMEDOUT;
82 }
83
84 return 0;
85}
86
87/*
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.
91 */
92static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value)
93{
94 struct tgec_mdio_controller __iomem *regs = bus->priv;
Andy Fleming1fcf77c2015-01-04 17:36:02 +080095 uint16_t dev_addr;
96 u32 mdio_ctl, mdio_stat;
Timur Tabi9f35a732012-08-20 09:26:39 +000097 int ret;
98
Andy Fleming1fcf77c2015-01-04 17:36:02 +080099 mdio_stat = in_be32(&regs->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;
104 } else {
105 /* Clause 22 (ie 1G) */
106 dev_addr = regnum & 0x1f;
107 mdio_stat &= ~MDIO_STAT_ENC;
108 }
Timur Tabi9f35a732012-08-20 09:26:39 +0000109
Andy Fleming1fcf77c2015-01-04 17:36:02 +0800110 out_be32(&regs->mdio_stat, mdio_stat);
Timur Tabi9f35a732012-08-20 09:26:39 +0000111
112 ret = xgmac_wait_until_free(&bus->dev, regs);
113 if (ret)
114 return ret;
115
Andy Fleming1fcf77c2015-01-04 17:36:02 +0800116 /* Set the port and dev addr */
117 mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
118 out_be32(&regs->mdio_ctl, mdio_ctl);
119
120 /* Set the register address */
121 if (regnum & MII_ADDR_C45) {
122 out_be32(&regs->mdio_addr, regnum & 0xffff);
123
124 ret = xgmac_wait_until_free(&bus->dev, regs);
125 if (ret)
126 return ret;
127 }
128
Timur Tabi9f35a732012-08-20 09:26:39 +0000129 /* Write the value to the register */
130 out_be32(&regs->mdio_data, MDIO_DATA(value));
131
132 ret = xgmac_wait_until_done(&bus->dev, regs);
133 if (ret)
134 return ret;
135
136 return 0;
137}
138
139/*
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
142 * TSEC1 MIIM regs.
143 */
144static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
145{
146 struct tgec_mdio_controller __iomem *regs = bus->priv;
Andy Fleming1fcf77c2015-01-04 17:36:02 +0800147 uint16_t dev_addr;
148 uint32_t mdio_stat;
Timur Tabi9f35a732012-08-20 09:26:39 +0000149 uint32_t mdio_ctl;
150 uint16_t value;
151 int ret;
152
Andy Fleming1fcf77c2015-01-04 17:36:02 +0800153 mdio_stat = in_be32(&regs->mdio_stat);
154 if (regnum & MII_ADDR_C45) {
155 dev_addr = (regnum >> 16) & 0x1f;
156 mdio_stat |= MDIO_STAT_ENC;
157 } else {
158 dev_addr = regnum & 0x1f;
159 mdio_stat = ~MDIO_STAT_ENC;
160 }
161
162 out_be32(&regs->mdio_stat, mdio_stat);
163
164 ret = xgmac_wait_until_free(&bus->dev, regs);
165 if (ret)
166 return ret;
167
Timur Tabi9f35a732012-08-20 09:26:39 +0000168 /* Set the Port and Device Addrs */
169 mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
170 out_be32(&regs->mdio_ctl, mdio_ctl);
171
172 /* Set the register address */
Andy Fleming1fcf77c2015-01-04 17:36:02 +0800173 if (regnum & MII_ADDR_C45) {
174 out_be32(&regs->mdio_addr, regnum & 0xffff);
Timur Tabi9f35a732012-08-20 09:26:39 +0000175
Andy Fleming1fcf77c2015-01-04 17:36:02 +0800176 ret = xgmac_wait_until_free(&bus->dev, regs);
177 if (ret)
178 return ret;
179 }
Timur Tabi9f35a732012-08-20 09:26:39 +0000180
181 /* Initiate the read */
182 out_be32(&regs->mdio_ctl, mdio_ctl | MDIO_CTL_READ);
183
184 ret = xgmac_wait_until_done(&bus->dev, regs);
185 if (ret)
186 return ret;
187
188 /* Return all Fs if nothing was there */
189 if (in_be32(&regs->mdio_stat) & MDIO_STAT_RD_ER) {
Shruti Kanetkar55fd3642014-06-11 13:41:40 -0500190 dev_err(&bus->dev,
Shruti Kanetkar9e6492e2014-07-29 14:53:03 -0500191 "Error while reading PHY%d reg at %d.%hhu\n",
Shruti Kanetkar55fd3642014-06-11 13:41:40 -0500192 phy_id, dev_addr, regnum);
Timur Tabi9f35a732012-08-20 09:26:39 +0000193 return 0xffff;
194 }
195
196 value = in_be32(&regs->mdio_data) & 0xffff;
197 dev_dbg(&bus->dev, "read %04x\n", value);
198
199 return value;
200}
201
Bill Pemberton33897cc2012-12-03 09:23:58 -0500202static int xgmac_mdio_probe(struct platform_device *pdev)
Timur Tabi9f35a732012-08-20 09:26:39 +0000203{
204 struct device_node *np = pdev->dev.of_node;
205 struct mii_bus *bus;
206 struct resource res;
207 int ret;
208
209 ret = of_address_to_resource(np, 0, &res);
210 if (ret) {
211 dev_err(&pdev->dev, "could not obtain address\n");
212 return ret;
213 }
214
Shaohui Xieaa842472014-12-30 16:28:00 +0800215 bus = mdiobus_alloc();
Timur Tabi9f35a732012-08-20 09:26:39 +0000216 if (!bus)
217 return -ENOMEM;
218
219 bus->name = "Freescale XGMAC MDIO Bus";
220 bus->read = xgmac_mdio_read;
221 bus->write = xgmac_mdio_write;
Timur Tabi9f35a732012-08-20 09:26:39 +0000222 bus->parent = &pdev->dev;
223 snprintf(bus->id, MII_BUS_ID_SIZE, "%llx", (unsigned long long)res.start);
224
225 /* Set the PHY base address */
226 bus->priv = of_iomap(np, 0);
227 if (!bus->priv) {
228 ret = -ENOMEM;
229 goto err_ioremap;
230 }
231
232 ret = of_mdiobus_register(bus, np);
233 if (ret) {
234 dev_err(&pdev->dev, "cannot register MDIO bus\n");
235 goto err_registration;
236 }
237
Jingoo Han8513fbd2013-05-23 00:52:31 +0000238 platform_set_drvdata(pdev, bus);
Timur Tabi9f35a732012-08-20 09:26:39 +0000239
240 return 0;
241
242err_registration:
243 iounmap(bus->priv);
244
245err_ioremap:
246 mdiobus_free(bus);
247
248 return ret;
249}
250
Bill Pemberton33897cc2012-12-03 09:23:58 -0500251static int xgmac_mdio_remove(struct platform_device *pdev)
Timur Tabi9f35a732012-08-20 09:26:39 +0000252{
Jingoo Han8513fbd2013-05-23 00:52:31 +0000253 struct mii_bus *bus = platform_get_drvdata(pdev);
Timur Tabi9f35a732012-08-20 09:26:39 +0000254
255 mdiobus_unregister(bus);
256 iounmap(bus->priv);
257 mdiobus_free(bus);
258
259 return 0;
260}
261
262static struct of_device_id xgmac_mdio_match[] = {
263 {
264 .compatible = "fsl,fman-xmdio",
265 },
Andy Fleming1fcf77c2015-01-04 17:36:02 +0800266 {
267 .compatible = "fsl,fman-memac-mdio",
268 },
Timur Tabi9f35a732012-08-20 09:26:39 +0000269 {},
270};
271MODULE_DEVICE_TABLE(of, xgmac_mdio_match);
272
273static struct platform_driver xgmac_mdio_driver = {
274 .driver = {
275 .name = "fsl-fman_xmdio",
276 .of_match_table = xgmac_mdio_match,
277 },
278 .probe = xgmac_mdio_probe,
279 .remove = xgmac_mdio_remove,
280};
281
282module_platform_driver(xgmac_mdio_driver);
283
284MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller");
285MODULE_LICENSE("GPL v2");