blob: ea76129dafc2bc6e6e0d602bb1da9a66b39810e1 [file] [log] [blame]
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001/*******************************************************************************
2 STMMAC Ethernet Driver -- MDIO bus implementation
3 Provides Bus interface for MII registers
4
5 Copyright (C) 2007-2009 STMicroelectronics Ltd
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms and conditions of the GNU General Public License,
9 version 2, as published by the Free Software Foundation.
10
11 This program is distributed in the hope it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19
20 The full GNU General Public License is included in this distribution in
21 the file called "COPYING".
22
23 Author: Carl Shaw <carl.shaw@st.com>
24 Maintainer: Giuseppe Cavallaro <peppe.cavallaro@st.com>
25*******************************************************************************/
26
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070027#include <linux/mii.h>
28#include <linux/phy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Srinivas Kandagatla0e076472013-07-04 10:35:48 +010030#include <linux/of.h>
31#include <linux/of_gpio.h>
Phil Reide34d6562015-12-14 11:31:59 +080032#include <linux/of_mdio.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000033#include <asm/io.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070034
35#include "stmmac.h"
36
37#define MII_BUSY 0x00000001
38#define MII_WRITE 0x00000002
39
Deepak SIKRI39b401d2012-04-04 04:33:24 +000040static int stmmac_mdio_busy_wait(void __iomem *ioaddr, unsigned int mii_addr)
41{
42 unsigned long curr;
43 unsigned long finish = jiffies + 3 * HZ;
44
45 do {
46 curr = jiffies;
47 if (readl(ioaddr + mii_addr) & MII_BUSY)
48 cpu_relax();
49 else
50 return 0;
51 } while (!time_after_eq(curr, finish));
52
53 return -EBUSY;
54}
55
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070056/**
57 * stmmac_mdio_read
58 * @bus: points to the mii_bus structure
59 * @phyaddr: MII addr reg bits 15-11
60 * @phyreg: MII addr reg bits 10-6
61 * Description: it reads data from the MII register from within the phy device.
62 * For the 7111 GMAC, we must set the bit 0 in the MII address register while
63 * accessing the PHY registers.
64 * Fortunately, it seems this has no drawback for the 7109 MAC.
65 */
66static int stmmac_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
67{
68 struct net_device *ndev = bus->priv;
69 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +000070 unsigned int mii_address = priv->hw->mii.addr;
71 unsigned int mii_data = priv->hw->mii.data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070072
73 int data;
74 u16 regValue = (((phyaddr << 11) & (0x0000F800)) |
75 ((phyreg << 6) & (0x000007C0)));
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +000076 regValue |= MII_BUSY | ((priv->clk_csr & 0xF) << 2);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070077
Deepak SIKRI39b401d2012-04-04 04:33:24 +000078 if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
79 return -EBUSY;
80
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000081 writel(regValue, priv->ioaddr + mii_address);
Deepak SIKRI39b401d2012-04-04 04:33:24 +000082
83 if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
84 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070085
86 /* Read the data from the MII data register */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000087 data = (int)readl(priv->ioaddr + mii_data);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070088
89 return data;
90}
91
92/**
93 * stmmac_mdio_write
94 * @bus: points to the mii_bus structure
95 * @phyaddr: MII addr reg bits 15-11
96 * @phyreg: MII addr reg bits 10-6
97 * @phydata: phy data
98 * Description: it writes the data into the MII register from within the device.
99 */
100static int stmmac_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
101 u16 phydata)
102{
103 struct net_device *ndev = bus->priv;
104 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000105 unsigned int mii_address = priv->hw->mii.addr;
106 unsigned int mii_data = priv->hw->mii.data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700107
108 u16 value =
109 (((phyaddr << 11) & (0x0000F800)) | ((phyreg << 6) & (0x000007C0)))
110 | MII_WRITE;
111
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000112 value |= MII_BUSY | ((priv->clk_csr & 0xF) << 2);
Giuseppe CAVALLAROdfb8fb92010-09-17 03:23:39 +0000113
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700114 /* Wait until any existing MII operation is complete */
Deepak SIKRI39b401d2012-04-04 04:33:24 +0000115 if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
116 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700117
118 /* Set the MII address register to write */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000119 writel(phydata, priv->ioaddr + mii_data);
120 writel(value, priv->ioaddr + mii_address);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700121
122 /* Wait until any existing MII operation is complete */
Deepak SIKRI39b401d2012-04-04 04:33:24 +0000123 return stmmac_mdio_busy_wait(priv->ioaddr, mii_address);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700124}
125
126/**
127 * stmmac_mdio_reset
128 * @bus: points to the mii_bus structure
129 * Description: reset the MII bus
130 */
Srinivas Kandagatla073752a2014-01-16 10:52:27 +0000131int stmmac_mdio_reset(struct mii_bus *bus)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700132{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000133#if defined(CONFIG_STMMAC_PLATFORM)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700134 struct net_device *ndev = bus->priv;
135 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000136 unsigned int mii_address = priv->hw->mii.addr;
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100137 struct stmmac_mdio_bus_data *data = priv->plat->mdio_bus_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700138
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100139#ifdef CONFIG_OF
140 if (priv->device->of_node) {
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100141
142 if (data->reset_gpio < 0) {
143 struct device_node *np = priv->device->of_node;
144 if (!np)
145 return 0;
146
147 data->reset_gpio = of_get_named_gpio(np,
148 "snps,reset-gpio", 0);
149 if (data->reset_gpio < 0)
150 return 0;
151
152 data->active_low = of_property_read_bool(np,
153 "snps,reset-active-low");
154 of_property_read_u32_array(np,
155 "snps,reset-delays-us", data->delays, 3);
Giuseppe CAVALLAROae26c1c2015-11-26 08:35:44 +0100156
157 if (gpio_request(data->reset_gpio, "mdio-reset"))
158 return 0;
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100159 }
160
Giuseppe CAVALLAROae26c1c2015-11-26 08:35:44 +0100161 gpio_direction_output(data->reset_gpio,
162 data->active_low ? 1 : 0);
163 if (data->delays[0])
164 msleep(DIV_ROUND_UP(data->delays[0], 1000));
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100165
Giuseppe CAVALLAROae26c1c2015-11-26 08:35:44 +0100166 gpio_set_value(data->reset_gpio, data->active_low ? 0 : 1);
167 if (data->delays[1])
168 msleep(DIV_ROUND_UP(data->delays[1], 1000));
Sjoerd Simons892aa012015-09-11 22:25:48 +0200169
Giuseppe CAVALLAROae26c1c2015-11-26 08:35:44 +0100170 gpio_set_value(data->reset_gpio, data->active_low ? 1 : 0);
171 if (data->delays[2])
172 msleep(DIV_ROUND_UP(data->delays[2], 1000));
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100173 }
174#endif
175
176 if (data->phy_reset) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700177 pr_debug("stmmac_mdio_reset: calling phy_reset\n");
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100178 data->phy_reset(priv->plat->bsp_priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700179 }
180
181 /* This is a workaround for problems with the STE101P PHY.
182 * It doesn't complete its reset until at least one clock cycle
183 * on MDC, so perform a dummy mdio read.
184 */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000185 writel(0, priv->ioaddr + mii_address);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000186#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700187 return 0;
188}
189
190/**
191 * stmmac_mdio_register
192 * @ndev: net device structure
193 * Description: it registers the MII bus
194 */
195int stmmac_mdio_register(struct net_device *ndev)
196{
197 int err = 0;
198 struct mii_bus *new_bus;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700199 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000200 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700201 int addr, found;
Gabriel Fernandez88f8b1b2016-02-29 17:18:22 +0100202 struct device_node *mdio_node = priv->plat->mdio_node;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700203
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000204 if (!mdio_bus_data)
205 return 0;
206
Phil Reide34d6562015-12-14 11:31:59 +0800207 if (IS_ENABLED(CONFIG_OF)) {
Phil Reide34d6562015-12-14 11:31:59 +0800208 if (mdio_node) {
209 netdev_dbg(ndev, "FOUND MDIO subnode\n");
210 } else {
Romain Perier6c672c92016-01-07 21:13:28 +0100211 netdev_warn(ndev, "No MDIO subnode found\n");
Phil Reide34d6562015-12-14 11:31:59 +0800212 }
213 }
214
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700215 new_bus = mdiobus_alloc();
216 if (new_bus == NULL)
217 return -ENOMEM;
218
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100219 if (mdio_bus_data->irqs)
220 memcpy(new_bus->irq, mdio_bus_data, sizeof(new_bus->irq));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700221
Srinivas Kandagatla0e076472013-07-04 10:35:48 +0100222#ifdef CONFIG_OF
223 if (priv->device->of_node)
224 mdio_bus_data->reset_gpio = -1;
225#endif
226
Alessandro Rubini90b9a5452012-01-23 23:26:48 +0000227 new_bus->name = "stmmac";
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700228 new_bus->read = &stmmac_mdio_read;
229 new_bus->write = &stmmac_mdio_write;
230 new_bus->reset = &stmmac_mdio_reset;
Florian Fainellidb8857b2012-01-09 23:59:20 +0000231 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000232 new_bus->name, priv->plat->bus_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700233 new_bus->priv = ndev;
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000234 new_bus->phy_mask = mdio_bus_data->phy_mask;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700235 new_bus->parent = priv->device;
Phil Reide34d6562015-12-14 11:31:59 +0800236
Romain Perier6c672c92016-01-07 21:13:28 +0100237 if (mdio_node)
238 err = of_mdiobus_register(new_bus, mdio_node);
239 else
240 err = mdiobus_register(new_bus);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700241 if (err != 0) {
242 pr_err("%s: Cannot register as MDIO bus\n", new_bus->name);
243 goto bus_register_fail;
244 }
245
Phil Reidcc2fa612016-03-15 15:34:33 +0800246 if (priv->plat->phy_node || mdio_node)
247 goto bus_register_done;
248
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700249 found = 0;
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000250 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
Andrew Lunn7f854422016-01-06 20:11:18 +0100251 struct phy_device *phydev = mdiobus_get_phy(new_bus, addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700252 if (phydev) {
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000253 int act = 0;
254 char irq_num[4];
255 char *irq_str;
256
257 /*
258 * If an IRQ was provided to be assigned after
259 * the bus probe, do it here.
260 */
261 if ((mdio_bus_data->irqs == NULL) &&
262 (mdio_bus_data->probed_phy_irq > 0)) {
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100263 new_bus->irq[addr] =
264 mdio_bus_data->probed_phy_irq;
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000265 phydev->irq = mdio_bus_data->probed_phy_irq;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700266 }
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000267
268 /*
Pavel Macheka77e4ac2014-08-25 13:31:16 +0200269 * If we're going to bind the MAC to this PHY bus,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000270 * and no PHY number was provided to the MAC,
271 * use the one probed here.
272 */
Srinivas Kandagatlad56631a2012-08-30 05:50:43 +0000273 if (priv->plat->phy_addr == -1)
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000274 priv->plat->phy_addr = addr;
275
Srinivas Kandagatlad56631a2012-08-30 05:50:43 +0000276 act = (priv->plat->phy_addr == addr);
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000277 switch (phydev->irq) {
278 case PHY_POLL:
279 irq_str = "POLL";
280 break;
281 case PHY_IGNORE_INTERRUPT:
282 irq_str = "IGNORE";
283 break;
284 default:
285 sprintf(irq_num, "%d", phydev->irq);
286 irq_str = irq_num;
287 break;
288 }
289 pr_info("%s: PHY ID %08x at %d IRQ %s (%s)%s\n",
290 ndev->name, phydev->phy_id, addr,
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100291 irq_str, phydev_name(phydev),
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000292 act ? " active" : "");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700293 found = 1;
294 }
295 }
296
Phil Reide34d6562015-12-14 11:31:59 +0800297 if (!found && !mdio_node) {
Joe Perchesfe3881c2014-09-09 20:27:44 -0700298 pr_warn("%s: No PHY found\n", ndev->name);
Giuseppe CAVALLARO3955b22b2013-02-06 20:47:52 +0000299 mdiobus_unregister(new_bus);
300 mdiobus_free(new_bus);
301 return -ENODEV;
302 }
303
Phil Reidcc2fa612016-03-15 15:34:33 +0800304bus_register_done:
Giuseppe CAVALLARO3955b22b2013-02-06 20:47:52 +0000305 priv->mii = new_bus;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700306
307 return 0;
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000308
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700309bus_register_fail:
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000310 mdiobus_free(new_bus);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700311 return err;
312}
313
314/**
315 * stmmac_mdio_unregister
316 * @ndev: net device structure
317 * Description: it unregisters the MII bus
318 */
319int stmmac_mdio_unregister(struct net_device *ndev)
320{
321 struct stmmac_priv *priv = netdev_priv(ndev);
322
Srinivas Kandagatlaa5cf5ce2012-08-30 05:49:58 +0000323 if (!priv->mii)
324 return 0;
325
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700326 mdiobus_unregister(priv->mii);
327 priv->mii->priv = NULL;
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000328 mdiobus_free(priv->mii);
329 priv->mii = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700330
331 return 0;
332}