blob: 9de526891da0040df6c833a882a45a047f08abaf [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
35#define MDIO_STAT_CLKDIV(x) (((x>>1) & 0xff) << 8)
36#define MDIO_STAT_BSY (1 << 0)
37#define MDIO_STAT_RD_ER (1 << 1)
38#define MDIO_CTL_DEV_ADDR(x) (x & 0x1f)
39#define MDIO_CTL_PORT_ADDR(x) ((x & 0x1f) << 5)
40#define MDIO_CTL_PRE_DIS (1 << 10)
41#define MDIO_CTL_SCAN_EN (1 << 11)
42#define MDIO_CTL_POST_INC (1 << 14)
43#define MDIO_CTL_READ (1 << 15)
44
45#define MDIO_DATA(x) (x & 0xffff)
46#define MDIO_DATA_BSY (1 << 31)
47
48/*
Madalin Bucurc1543d32014-07-29 14:47:25 -050049 * Wait until the MDIO bus is free
Timur Tabi9f35a732012-08-20 09:26:39 +000050 */
51static int xgmac_wait_until_free(struct device *dev,
52 struct tgec_mdio_controller __iomem *regs)
53{
54 uint32_t status;
55
56 /* Wait till the bus is free */
57 status = spin_event_timeout(
58 !((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY), TIMEOUT, 0);
59 if (!status) {
60 dev_err(dev, "timeout waiting for bus to be free\n");
61 return -ETIMEDOUT;
62 }
63
64 return 0;
65}
66
67/*
68 * Wait till the MDIO read or write operation is complete
69 */
70static int xgmac_wait_until_done(struct device *dev,
71 struct tgec_mdio_controller __iomem *regs)
72{
73 uint32_t status;
74
75 /* Wait till the MDIO write is complete */
76 status = spin_event_timeout(
77 !((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY), TIMEOUT, 0);
78 if (!status) {
79 dev_err(dev, "timeout waiting for operation to complete\n");
80 return -ETIMEDOUT;
81 }
82
83 return 0;
84}
85
86/*
87 * Write value to the PHY for this device to the register at regnum,waiting
88 * until the write is done before it returns. All PHY configuration has to be
89 * done through the TSEC1 MIIM regs.
90 */
91static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value)
92{
93 struct tgec_mdio_controller __iomem *regs = bus->priv;
94 uint16_t dev_addr = regnum >> 16;
95 int ret;
96
97 /* Setup the MII Mgmt clock speed */
98 out_be32(&regs->mdio_stat, MDIO_STAT_CLKDIV(100));
99
100 ret = xgmac_wait_until_free(&bus->dev, regs);
101 if (ret)
102 return ret;
103
104 /* Set the port and dev addr */
105 out_be32(&regs->mdio_ctl,
106 MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr));
107
108 /* Set the register address */
109 out_be32(&regs->mdio_addr, regnum & 0xffff);
110
111 ret = xgmac_wait_until_free(&bus->dev, regs);
112 if (ret)
113 return ret;
114
115 /* Write the value to the register */
116 out_be32(&regs->mdio_data, MDIO_DATA(value));
117
118 ret = xgmac_wait_until_done(&bus->dev, regs);
119 if (ret)
120 return ret;
121
122 return 0;
123}
124
125/*
126 * Reads from register regnum in the PHY for device dev, returning the value.
127 * Clears miimcom first. All PHY configuration has to be done through the
128 * TSEC1 MIIM regs.
129 */
130static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
131{
132 struct tgec_mdio_controller __iomem *regs = bus->priv;
133 uint16_t dev_addr = regnum >> 16;
134 uint32_t mdio_ctl;
135 uint16_t value;
136 int ret;
137
138 /* Setup the MII Mgmt clock speed */
139 out_be32(&regs->mdio_stat, MDIO_STAT_CLKDIV(100));
140
141 ret = xgmac_wait_until_free(&bus->dev, regs);
142 if (ret)
143 return ret;
144
145 /* Set the Port and Device Addrs */
146 mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
147 out_be32(&regs->mdio_ctl, mdio_ctl);
148
149 /* Set the register address */
150 out_be32(&regs->mdio_addr, regnum & 0xffff);
151
152 ret = xgmac_wait_until_free(&bus->dev, regs);
153 if (ret)
154 return ret;
155
156 /* Initiate the read */
157 out_be32(&regs->mdio_ctl, mdio_ctl | MDIO_CTL_READ);
158
159 ret = xgmac_wait_until_done(&bus->dev, regs);
160 if (ret)
161 return ret;
162
163 /* Return all Fs if nothing was there */
164 if (in_be32(&regs->mdio_stat) & MDIO_STAT_RD_ER) {
Shruti Kanetkar55fd3642014-06-11 13:41:40 -0500165 dev_err(&bus->dev,
Shruti Kanetkar9e6492e2014-07-29 14:53:03 -0500166 "Error while reading PHY%d reg at %d.%hhu\n",
Shruti Kanetkar55fd3642014-06-11 13:41:40 -0500167 phy_id, dev_addr, regnum);
Timur Tabi9f35a732012-08-20 09:26:39 +0000168 return 0xffff;
169 }
170
171 value = in_be32(&regs->mdio_data) & 0xffff;
172 dev_dbg(&bus->dev, "read %04x\n", value);
173
174 return value;
175}
176
Bill Pemberton33897cc2012-12-03 09:23:58 -0500177static int xgmac_mdio_probe(struct platform_device *pdev)
Timur Tabi9f35a732012-08-20 09:26:39 +0000178{
179 struct device_node *np = pdev->dev.of_node;
180 struct mii_bus *bus;
181 struct resource res;
182 int ret;
183
184 ret = of_address_to_resource(np, 0, &res);
185 if (ret) {
186 dev_err(&pdev->dev, "could not obtain address\n");
187 return ret;
188 }
189
Shaohui Xieaa842472014-12-30 16:28:00 +0800190 bus = mdiobus_alloc();
Timur Tabi9f35a732012-08-20 09:26:39 +0000191 if (!bus)
192 return -ENOMEM;
193
194 bus->name = "Freescale XGMAC MDIO Bus";
195 bus->read = xgmac_mdio_read;
196 bus->write = xgmac_mdio_write;
Timur Tabi9f35a732012-08-20 09:26:39 +0000197 bus->parent = &pdev->dev;
198 snprintf(bus->id, MII_BUS_ID_SIZE, "%llx", (unsigned long long)res.start);
199
200 /* Set the PHY base address */
201 bus->priv = of_iomap(np, 0);
202 if (!bus->priv) {
203 ret = -ENOMEM;
204 goto err_ioremap;
205 }
206
207 ret = of_mdiobus_register(bus, np);
208 if (ret) {
209 dev_err(&pdev->dev, "cannot register MDIO bus\n");
210 goto err_registration;
211 }
212
Jingoo Han8513fbd2013-05-23 00:52:31 +0000213 platform_set_drvdata(pdev, bus);
Timur Tabi9f35a732012-08-20 09:26:39 +0000214
215 return 0;
216
217err_registration:
218 iounmap(bus->priv);
219
220err_ioremap:
221 mdiobus_free(bus);
222
223 return ret;
224}
225
Bill Pemberton33897cc2012-12-03 09:23:58 -0500226static int xgmac_mdio_remove(struct platform_device *pdev)
Timur Tabi9f35a732012-08-20 09:26:39 +0000227{
Jingoo Han8513fbd2013-05-23 00:52:31 +0000228 struct mii_bus *bus = platform_get_drvdata(pdev);
Timur Tabi9f35a732012-08-20 09:26:39 +0000229
230 mdiobus_unregister(bus);
231 iounmap(bus->priv);
232 mdiobus_free(bus);
233
234 return 0;
235}
236
237static struct of_device_id xgmac_mdio_match[] = {
238 {
239 .compatible = "fsl,fman-xmdio",
240 },
241 {},
242};
243MODULE_DEVICE_TABLE(of, xgmac_mdio_match);
244
245static struct platform_driver xgmac_mdio_driver = {
246 .driver = {
247 .name = "fsl-fman_xmdio",
248 .of_match_table = xgmac_mdio_match,
249 },
250 .probe = xgmac_mdio_probe,
251 .remove = xgmac_mdio_remove,
252};
253
254module_platform_driver(xgmac_mdio_driver);
255
256MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller");
257MODULE_LICENSE("GPL v2");