x86/nmi: Fix use of unallocated cpumask_var_t
[cascardo/linux.git] / drivers / net / ethernet / broadcom / genet / bcmmii.c
1 /*
2  * Broadcom GENET MDIO routines
3  *
4  * Copyright (c) 2014 Broadcom Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11
12 #include <linux/types.h>
13 #include <linux/delay.h>
14 #include <linux/wait.h>
15 #include <linux/mii.h>
16 #include <linux/ethtool.h>
17 #include <linux/bitops.h>
18 #include <linux/netdevice.h>
19 #include <linux/platform_device.h>
20 #include <linux/phy.h>
21 #include <linux/phy_fixed.h>
22 #include <linux/brcmphy.h>
23 #include <linux/of.h>
24 #include <linux/of_net.h>
25 #include <linux/of_mdio.h>
26
27 #include "bcmgenet.h"
28
29 /* read a value from the MII */
30 static int bcmgenet_mii_read(struct mii_bus *bus, int phy_id, int location)
31 {
32         int ret;
33         struct net_device *dev = bus->priv;
34         struct bcmgenet_priv *priv = netdev_priv(dev);
35         u32 reg;
36
37         bcmgenet_umac_writel(priv, (MDIO_RD | (phy_id << MDIO_PMD_SHIFT) |
38                              (location << MDIO_REG_SHIFT)), UMAC_MDIO_CMD);
39         /* Start MDIO transaction*/
40         reg = bcmgenet_umac_readl(priv, UMAC_MDIO_CMD);
41         reg |= MDIO_START_BUSY;
42         bcmgenet_umac_writel(priv, reg, UMAC_MDIO_CMD);
43         wait_event_timeout(priv->wq,
44                            !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
45                            & MDIO_START_BUSY),
46                            HZ / 100);
47         ret = bcmgenet_umac_readl(priv, UMAC_MDIO_CMD);
48
49         if (ret & MDIO_READ_FAIL)
50                 return -EIO;
51
52         return ret & 0xffff;
53 }
54
55 /* write a value to the MII */
56 static int bcmgenet_mii_write(struct mii_bus *bus, int phy_id,
57                               int location, u16 val)
58 {
59         struct net_device *dev = bus->priv;
60         struct bcmgenet_priv *priv = netdev_priv(dev);
61         u32 reg;
62
63         bcmgenet_umac_writel(priv, (MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
64                              (location << MDIO_REG_SHIFT) | (0xffff & val)),
65                              UMAC_MDIO_CMD);
66         reg = bcmgenet_umac_readl(priv, UMAC_MDIO_CMD);
67         reg |= MDIO_START_BUSY;
68         bcmgenet_umac_writel(priv, reg, UMAC_MDIO_CMD);
69         wait_event_timeout(priv->wq,
70                            !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD) &
71                            MDIO_START_BUSY),
72                            HZ / 100);
73
74         return 0;
75 }
76
77 /* setup netdev link state when PHY link status change and
78  * update UMAC and RGMII block when link up
79  */
80 static void bcmgenet_mii_setup(struct net_device *dev)
81 {
82         struct bcmgenet_priv *priv = netdev_priv(dev);
83         struct phy_device *phydev = priv->phydev;
84         u32 reg, cmd_bits = 0;
85         bool status_changed = false;
86
87         if (priv->old_link != phydev->link) {
88                 status_changed = true;
89                 priv->old_link = phydev->link;
90         }
91
92         if (phydev->link) {
93                 /* check speed/duplex/pause changes */
94                 if (priv->old_speed != phydev->speed) {
95                         status_changed = true;
96                         priv->old_speed = phydev->speed;
97                 }
98
99                 if (priv->old_duplex != phydev->duplex) {
100                         status_changed = true;
101                         priv->old_duplex = phydev->duplex;
102                 }
103
104                 if (priv->old_pause != phydev->pause) {
105                         status_changed = true;
106                         priv->old_pause = phydev->pause;
107                 }
108
109                 /* done if nothing has changed */
110                 if (!status_changed)
111                         return;
112
113                 /* speed */
114                 if (phydev->speed == SPEED_1000)
115                         cmd_bits = UMAC_SPEED_1000;
116                 else if (phydev->speed == SPEED_100)
117                         cmd_bits = UMAC_SPEED_100;
118                 else
119                         cmd_bits = UMAC_SPEED_10;
120                 cmd_bits <<= CMD_SPEED_SHIFT;
121
122                 /* duplex */
123                 if (phydev->duplex != DUPLEX_FULL)
124                         cmd_bits |= CMD_HD_EN;
125
126                 /* pause capability */
127                 if (!phydev->pause)
128                         cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
129
130                 /*
131                  * Program UMAC and RGMII block based on established
132                  * link speed, duplex, and pause. The speed set in
133                  * umac->cmd tell RGMII block which clock to use for
134                  * transmit -- 25MHz(100Mbps) or 125MHz(1Gbps).
135                  * Receive clock is provided by the PHY.
136                  */
137                 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
138                 reg &= ~OOB_DISABLE;
139                 reg |= RGMII_LINK;
140                 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
141
142                 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
143                 reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
144                                CMD_HD_EN |
145                                CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE);
146                 reg |= cmd_bits;
147                 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
148         } else {
149                 /* done if nothing has changed */
150                 if (!status_changed)
151                         return;
152
153                 /* needed for MoCA fixed PHY to reflect correct link status */
154                 netif_carrier_off(dev);
155         }
156
157         phy_print_status(phydev);
158 }
159
160 void bcmgenet_mii_reset(struct net_device *dev)
161 {
162         struct bcmgenet_priv *priv = netdev_priv(dev);
163
164         if (priv->phydev) {
165                 phy_init_hw(priv->phydev);
166                 phy_start_aneg(priv->phydev);
167         }
168 }
169
170 static void bcmgenet_ephy_power_up(struct net_device *dev)
171 {
172         struct bcmgenet_priv *priv = netdev_priv(dev);
173         u32 reg = 0;
174
175         /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
176         if (!GENET_IS_V4(priv))
177                 return;
178
179         reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
180         reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
181         reg |= EXT_GPHY_RESET;
182         bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
183         mdelay(2);
184
185         reg &= ~EXT_GPHY_RESET;
186         bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
187         udelay(20);
188 }
189
190 static void bcmgenet_internal_phy_setup(struct net_device *dev)
191 {
192         struct bcmgenet_priv *priv = netdev_priv(dev);
193         u32 reg;
194
195         /* Power up EPHY */
196         bcmgenet_ephy_power_up(dev);
197         /* enable APD */
198         reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
199         reg |= EXT_PWR_DN_EN_LD;
200         bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
201         bcmgenet_mii_reset(dev);
202 }
203
204 static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
205 {
206         u32 reg;
207
208         /* Speed settings are set in bcmgenet_mii_setup() */
209         reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
210         reg |= LED_ACT_SOURCE_MAC;
211         bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
212 }
213
214 int bcmgenet_mii_config(struct net_device *dev)
215 {
216         struct bcmgenet_priv *priv = netdev_priv(dev);
217         struct phy_device *phydev = priv->phydev;
218         struct device *kdev = &priv->pdev->dev;
219         const char *phy_name = NULL;
220         u32 id_mode_dis = 0;
221         u32 port_ctrl;
222         u32 reg;
223
224         priv->ext_phy = !phy_is_internal(priv->phydev) &&
225                         (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
226
227         if (phy_is_internal(priv->phydev))
228                 priv->phy_interface = PHY_INTERFACE_MODE_NA;
229
230         switch (priv->phy_interface) {
231         case PHY_INTERFACE_MODE_NA:
232         case PHY_INTERFACE_MODE_MOCA:
233                 /* Irrespective of the actually configured PHY speed (100 or
234                  * 1000) GENETv4 only has an internal GPHY so we will just end
235                  * up masking the Gigabit features from what we support, not
236                  * switching to the EPHY
237                  */
238                 if (GENET_IS_V4(priv))
239                         port_ctrl = PORT_MODE_INT_GPHY;
240                 else
241                         port_ctrl = PORT_MODE_INT_EPHY;
242
243                 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
244
245                 if (phy_is_internal(priv->phydev)) {
246                         phy_name = "internal PHY";
247                         bcmgenet_internal_phy_setup(dev);
248                 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
249                         phy_name = "MoCA";
250                         bcmgenet_moca_phy_setup(priv);
251                 }
252                 break;
253
254         case PHY_INTERFACE_MODE_MII:
255                 phy_name = "external MII";
256                 phydev->supported &= PHY_BASIC_FEATURES;
257                 bcmgenet_sys_writel(priv,
258                                     PORT_MODE_EXT_EPHY, SYS_PORT_CTRL);
259                 break;
260
261         case PHY_INTERFACE_MODE_REVMII:
262                 phy_name = "external RvMII";
263                 /* of_mdiobus_register took care of reading the 'max-speed'
264                  * PHY property for us, effectively limiting the PHY supported
265                  * capabilities, use that knowledge to also configure the
266                  * Reverse MII interface correctly.
267                  */
268                 if ((priv->phydev->supported & PHY_BASIC_FEATURES) ==
269                                 PHY_BASIC_FEATURES)
270                         port_ctrl = PORT_MODE_EXT_RVMII_25;
271                 else
272                         port_ctrl = PORT_MODE_EXT_RVMII_50;
273                 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
274                 break;
275
276         case PHY_INTERFACE_MODE_RGMII:
277                 /* RGMII_NO_ID: TXC transitions at the same time as TXD
278                  *              (requires PCB or receiver-side delay)
279                  * RGMII:       Add 2ns delay on TXC (90 degree shift)
280                  *
281                  * ID is implicitly disabled for 100Mbps (RG)MII operation.
282                  */
283                 id_mode_dis = BIT(16);
284                 /* fall through */
285         case PHY_INTERFACE_MODE_RGMII_TXID:
286                 if (id_mode_dis)
287                         phy_name = "external RGMII (no delay)";
288                 else
289                         phy_name = "external RGMII (TX delay)";
290                 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
291                 reg |= RGMII_MODE_EN | id_mode_dis;
292                 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
293                 bcmgenet_sys_writel(priv,
294                                     PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
295                 break;
296         default:
297                 dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
298                 return -EINVAL;
299         }
300
301         dev_info(kdev, "configuring instance for %s\n", phy_name);
302
303         return 0;
304 }
305
306 static int bcmgenet_mii_probe(struct net_device *dev)
307 {
308         struct bcmgenet_priv *priv = netdev_priv(dev);
309         struct device_node *dn = priv->pdev->dev.of_node;
310         struct phy_device *phydev;
311         u32 phy_flags;
312         int ret;
313
314         if (priv->phydev) {
315                 pr_info("PHY already attached\n");
316                 return 0;
317         }
318
319         /* In the case of a fixed PHY, the DT node associated
320          * to the PHY is the Ethernet MAC DT node.
321          */
322         if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
323                 ret = of_phy_register_fixed_link(dn);
324                 if (ret)
325                         return ret;
326
327                 priv->phy_dn = of_node_get(dn);
328         }
329
330         /* Communicate the integrated PHY revision */
331         phy_flags = priv->gphy_rev;
332
333         /* Initialize link state variables that bcmgenet_mii_setup() uses */
334         priv->old_link = -1;
335         priv->old_speed = -1;
336         priv->old_duplex = -1;
337         priv->old_pause = -1;
338
339         phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
340                                 phy_flags, priv->phy_interface);
341         if (!phydev) {
342                 pr_err("could not attach to PHY\n");
343                 return -ENODEV;
344         }
345
346         priv->phydev = phydev;
347
348         /* Configure port multiplexer based on what the probed PHY device since
349          * reading the 'max-speed' property determines the maximum supported
350          * PHY speed which is needed for bcmgenet_mii_config() to configure
351          * things appropriately.
352          */
353         ret = bcmgenet_mii_config(dev);
354         if (ret) {
355                 phy_disconnect(priv->phydev);
356                 return ret;
357         }
358
359         phydev->advertising = phydev->supported;
360
361         /* The internal PHY has its link interrupts routed to the
362          * Ethernet MAC ISRs
363          */
364         if (phy_is_internal(priv->phydev))
365                 priv->mii_bus->irq[phydev->addr] = PHY_IGNORE_INTERRUPT;
366         else
367                 priv->mii_bus->irq[phydev->addr] = PHY_POLL;
368
369         pr_info("attached PHY at address %d [%s]\n",
370                 phydev->addr, phydev->drv->name);
371
372         return 0;
373 }
374
375 static int bcmgenet_mii_alloc(struct bcmgenet_priv *priv)
376 {
377         struct mii_bus *bus;
378
379         if (priv->mii_bus)
380                 return 0;
381
382         priv->mii_bus = mdiobus_alloc();
383         if (!priv->mii_bus) {
384                 pr_err("failed to allocate\n");
385                 return -ENOMEM;
386         }
387
388         bus = priv->mii_bus;
389         bus->priv = priv->dev;
390         bus->name = "bcmgenet MII bus";
391         bus->parent = &priv->pdev->dev;
392         bus->read = bcmgenet_mii_read;
393         bus->write = bcmgenet_mii_write;
394         snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d",
395                  priv->pdev->name, priv->pdev->id);
396
397         bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
398         if (!bus->irq) {
399                 mdiobus_free(priv->mii_bus);
400                 return -ENOMEM;
401         }
402
403         return 0;
404 }
405
406 static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
407 {
408         struct device_node *dn = priv->pdev->dev.of_node;
409         struct device *kdev = &priv->pdev->dev;
410         struct device_node *mdio_dn;
411         char *compat;
412         int ret;
413
414         compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
415         if (!compat)
416                 return -ENOMEM;
417
418         mdio_dn = of_find_compatible_node(dn, NULL, compat);
419         kfree(compat);
420         if (!mdio_dn) {
421                 dev_err(kdev, "unable to find MDIO bus node\n");
422                 return -ENODEV;
423         }
424
425         ret = of_mdiobus_register(priv->mii_bus, mdio_dn);
426         if (ret) {
427                 dev_err(kdev, "failed to register MDIO bus\n");
428                 return ret;
429         }
430
431         /* Fetch the PHY phandle */
432         priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
433
434         /* Get the link mode */
435         priv->phy_interface = of_get_phy_mode(dn);
436
437         return 0;
438 }
439
440 int bcmgenet_mii_init(struct net_device *dev)
441 {
442         struct bcmgenet_priv *priv = netdev_priv(dev);
443         int ret;
444
445         ret = bcmgenet_mii_alloc(priv);
446         if (ret)
447                 return ret;
448
449         ret = bcmgenet_mii_of_init(priv);
450         if (ret)
451                 goto out_free;
452
453         ret = bcmgenet_mii_probe(dev);
454         if (ret)
455                 goto out;
456
457         return 0;
458
459 out:
460         of_node_put(priv->phy_dn);
461         mdiobus_unregister(priv->mii_bus);
462 out_free:
463         kfree(priv->mii_bus->irq);
464         mdiobus_free(priv->mii_bus);
465         return ret;
466 }
467
468 void bcmgenet_mii_exit(struct net_device *dev)
469 {
470         struct bcmgenet_priv *priv = netdev_priv(dev);
471
472         of_node_put(priv->phy_dn);
473         mdiobus_unregister(priv->mii_bus);
474         kfree(priv->mii_bus->irq);
475         mdiobus_free(priv->mii_bus);
476 }