1 From 126e6f022c749ac1bf3a607269a106ccd87d0594 Mon Sep 17 00:00:00 2001
2 From: Claudiu Manoil <claudiu.manoil@nxp.com>
3 Date: Mon, 12 Aug 2019 20:26:42 +0300
4 Subject: [PATCH] enetc: Make MDIO accessors more generic and export to
7 Within the LS1028A SoC, the register map for the ENETC MDIO controller
8 is instantiated a few times: for the central (external) MDIO controller,
9 for the internal bus of each standalone ENETC port, and for the internal
10 bus of the Felix switch.
12 Refactoring is needed to support multiple MDIO buses from multiple
13 drivers. The enetc_hw structure is made an opaque type and a smaller
14 enetc_mdio_priv is created.
16 'mdio_base' - MDIO registers base address - is being parameterized, to
17 be able to work with different MDIO register bases.
19 The ENETC MDIO bus operations are exported from the fsl-enetc-mdio
20 kernel object, the same that registers the central MDIO controller (the
21 dedicated PF). The ENETC main driver has been changed to select it, and
22 use its exported helpers to further register its private MDIO bus. The
23 DSA Felix driver will do the same.
25 Signed-off-by: Claudiu Manoil <claudiu.manoil@nxp.com>
26 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
29 drivers/net/ethernet/freescale/enetc/enetc_mdio.c
30 drivers/net/ethernet/freescale/enetc/enetc_mdio.h
31 drivers/net/ethernet/freescale/enetc/enetc_pci_mdio.c
32 drivers/net/ethernet/freescale/enetc/enetc_pf.c
33 drivers/net/ethernet/freescale/enetc/enetc_pf.h
35 mostly with the previous (downstream version of this commit) patch
36 572ee5d842da ("enetc: Make mdio accessors more generic"), which couldn't
37 be reverted cleanly due to the existing downstream workaround for the
40 drivers/net/ethernet/freescale/enetc/Kconfig | 1 +
41 drivers/net/ethernet/freescale/enetc/Makefile | 2 +-
42 drivers/net/ethernet/freescale/enetc/enetc_mdio.c | 76 ++++------------------
43 drivers/net/ethernet/freescale/enetc/enetc_mdio.h | 12 ----
44 .../net/ethernet/freescale/enetc/enetc_pci_mdio.c | 41 +++++++-----
45 drivers/net/ethernet/freescale/enetc/enetc_pf.c | 71 ++++++++++++++++++++
46 drivers/net/ethernet/freescale/enetc/enetc_pf.h | 5 --
47 include/linux/fsl/enetc_mdio.h | 55 ++++++++++++++++
48 8 files changed, 163 insertions(+), 100 deletions(-)
49 delete mode 100644 drivers/net/ethernet/freescale/enetc/enetc_mdio.h
50 create mode 100644 include/linux/fsl/enetc_mdio.h
52 --- a/drivers/net/ethernet/freescale/enetc/Kconfig
53 +++ b/drivers/net/ethernet/freescale/enetc/Kconfig
56 tristate "ENETC PF driver"
57 depends on PCI && PCI_MSI && (ARCH_LAYERSCAPE || COMPILE_TEST)
58 + select FSL_ENETC_MDIO
61 This driver supports NXP ENETC gigabit ethernet controller PCIe
62 --- a/drivers/net/ethernet/freescale/enetc/Makefile
63 +++ b/drivers/net/ethernet/freescale/enetc/Makefile
65 common-objs := enetc.o enetc_cbdr.o enetc_ethtool.o
67 obj-$(CONFIG_FSL_ENETC) += fsl-enetc.o
68 -fsl-enetc-y := enetc_pf.o enetc_mdio.o $(common-objs)
69 +fsl-enetc-y := enetc_pf.o $(common-objs)
70 fsl-enetc-$(CONFIG_PCI_IOV) += enetc_msg.o
71 fsl-enetc-$(CONFIG_FSL_ENETC_QOS) += enetc_qos.o
72 fsl-enetc-$(CONFIG_ENETC_TSN) += enetc_tsn.o
73 --- a/drivers/net/ethernet/freescale/enetc/enetc_mdio.c
74 +++ b/drivers/net/ethernet/freescale/enetc/enetc_mdio.c
76 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
77 /* Copyright 2019 NXP */
79 +#include <linux/fsl/enetc_mdio.h>
80 #include <linux/mdio.h>
81 #include <linux/of_mdio.h>
82 #include <linux/iopoll.h>
86 -#include "enetc_mdio.h"
88 #define ENETC_MDIO_CFG 0x0 /* MDIO configuration and status */
89 #define ENETC_MDIO_CTL 0x4 /* MDIO control */
90 @@ -99,6 +99,7 @@ int enetc_mdio_write(struct mii_bus *bus
94 +EXPORT_SYMBOL_GPL(enetc_mdio_write);
96 int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
98 @@ -154,73 +155,18 @@ int enetc_mdio_read(struct mii_bus *bus,
102 +EXPORT_SYMBOL_GPL(enetc_mdio_read);
104 -int enetc_mdio_probe(struct enetc_pf *pf)
105 +struct enetc_hw *enetc_hw_alloc(struct device *dev, void __iomem *port_regs)
107 - struct device *dev = &pf->si->pdev->dev;
108 - struct enetc_mdio_priv *mdio_priv;
109 - struct device_node *np;
110 - struct mii_bus *bus;
113 - bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
117 - bus->name = "Freescale ENETC MDIO Bus";
118 - bus->read = enetc_mdio_read;
119 - bus->write = enetc_mdio_write;
121 - mdio_priv = bus->priv;
122 - mdio_priv->hw = &pf->si->hw;
123 - mdio_priv->mdio_base = ENETC_EMDIO_BASE;
124 - snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
126 - np = of_get_child_by_name(dev->of_node, "mdio");
128 - dev_err(dev, "MDIO node missing\n");
132 - err = of_mdiobus_register(bus, np);
135 - dev_err(dev, "cannot register MDIO bus\n");
144 + struct enetc_hw *hw;
146 -void enetc_mdio_remove(struct enetc_pf *pf)
149 - mdiobus_unregister(pf->mdio);
152 -int enetc_imdio_init(struct enetc_pf *pf)
154 - struct device *dev = &pf->si->pdev->dev;
155 - struct enetc_mdio_priv *mdio_priv;
156 - struct mii_bus *bus;
157 + hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
159 + return ERR_PTR(-ENOMEM);
161 - bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
164 + hw->port = port_regs;
166 - bus->name = "FSL ENETC internal MDIO Bus";
167 - bus->read = enetc_mdio_read;
168 - bus->write = enetc_mdio_write;
170 - mdio_priv = bus->priv;
171 - mdio_priv->hw = &pf->si->hw;
172 - mdio_priv->mdio_base = ENETC_PM_IMDIO_BASE;
173 - snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
180 +EXPORT_SYMBOL_GPL(enetc_hw_alloc);
181 --- a/drivers/net/ethernet/freescale/enetc/enetc_mdio.h
184 -/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
185 -/* Copyright 2019 NXP */
187 -#include <linux/phy.h>
189 -struct enetc_mdio_priv {
190 - struct enetc_hw *hw;
194 -int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value);
195 -int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum);
196 --- a/drivers/net/ethernet/freescale/enetc/enetc_pci_mdio.c
197 +++ b/drivers/net/ethernet/freescale/enetc/enetc_pci_mdio.c
199 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
200 /* Copyright 2019 NXP */
201 +#include <linux/fsl/enetc_mdio.h>
202 #include <linux/of_mdio.h>
203 #include "enetc_pf.h"
204 -#include "enetc_mdio.h"
206 #define ENETC_MDIO_DEV_ID 0xee01
207 #define ENETC_MDIO_DEV_NAME "FSL PCIe IE Central MDIO"
208 @@ -14,17 +14,29 @@ static int enetc_pci_mdio_probe(struct p
210 struct enetc_mdio_priv *mdio_priv;
211 struct device *dev = &pdev->dev;
212 + void __iomem *port_regs;
217 - hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
220 + port_regs = pci_iomap(pdev, 0, 0);
222 + dev_err(dev, "iomap failed\n");
227 + hw = enetc_hw_alloc(dev, port_regs);
228 + if (IS_ERR(enetc_hw_alloc)) {
233 bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
238 + goto err_mdiobus_alloc;
241 bus->name = ENETC_MDIO_BUS_NAME;
242 bus->read = enetc_mdio_read;
243 @@ -39,7 +51,7 @@ static int enetc_pci_mdio_probe(struct p
244 err = pci_enable_device_mem(pdev);
246 dev_err(dev, "device enable failed\n");
248 + goto err_pci_enable;
251 err = pci_request_region(pdev, 0, KBUILD_MODNAME);
252 @@ -48,13 +60,6 @@ static int enetc_pci_mdio_probe(struct p
253 goto err_pci_mem_reg;
256 - hw->port = pci_iomap(pdev, 0, 0);
259 - dev_err(dev, "iomap failed\n");
263 err = of_mdiobus_register(bus, dev->of_node);
265 goto err_mdiobus_reg;
266 @@ -64,12 +69,14 @@ static int enetc_pci_mdio_probe(struct p
270 - iounmap(mdio_priv->hw->port);
272 pci_release_mem_regions(pdev);
274 pci_disable_device(pdev);
278 + iounmap(port_regs);
284 --- a/drivers/net/ethernet/freescale/enetc/enetc_pf.c
285 +++ b/drivers/net/ethernet/freescale/enetc/enetc_pf.c
287 /* Copyright 2017-2019 NXP */
289 #include <linux/module.h>
290 +#include <linux/fsl/enetc_mdio.h>
291 #include <linux/of_mdio.h>
292 #include <linux/of_net.h>
293 #include "enetc_pf.h"
294 @@ -760,6 +761,52 @@ static void enetc_pf_netdev_setup(struct
295 enetc_get_primary_mac_addr(&si->hw, ndev->dev_addr);
298 +static int enetc_mdio_probe(struct enetc_pf *pf)
300 + struct device *dev = &pf->si->pdev->dev;
301 + struct enetc_mdio_priv *mdio_priv;
302 + struct device_node *np;
303 + struct mii_bus *bus;
306 + bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
310 + bus->name = "Freescale ENETC MDIO Bus";
311 + bus->read = enetc_mdio_read;
312 + bus->write = enetc_mdio_write;
314 + mdio_priv = bus->priv;
315 + mdio_priv->hw = &pf->si->hw;
316 + mdio_priv->mdio_base = ENETC_EMDIO_BASE;
317 + snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
319 + np = of_get_child_by_name(dev->of_node, "mdio");
321 + dev_err(dev, "MDIO node missing\n");
325 + err = of_mdiobus_register(bus, np);
328 + dev_err(dev, "cannot register MDIO bus\n");
338 +static void enetc_mdio_remove(struct enetc_pf *pf)
341 + mdiobus_unregister(pf->mdio);
344 static int enetc_of_get_phy(struct enetc_pf *pf)
346 struct device *dev = &pf->si->pdev->dev;
347 @@ -846,6 +893,30 @@ static void enetc_configure_sxgmii(struc
348 ENETC_PCS_CR_LANE_RESET | ENETC_PCS_CR_RESET_AN);
351 +static int enetc_imdio_init(struct enetc_pf *pf)
353 + struct device *dev = &pf->si->pdev->dev;
354 + struct enetc_mdio_priv *mdio_priv;
355 + struct mii_bus *bus;
357 + bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
361 + bus->name = "FSL ENETC internal MDIO Bus";
362 + bus->read = enetc_mdio_read;
363 + bus->write = enetc_mdio_write;
365 + mdio_priv = bus->priv;
366 + mdio_priv->hw = &pf->si->hw;
367 + mdio_priv->mdio_base = ENETC_PM_IMDIO_BASE;
368 + snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
375 static int enetc_configure_serdes(struct enetc_ndev_priv *priv)
377 struct enetc_pf *pf = enetc_si_priv(priv->si);
378 --- a/drivers/net/ethernet/freescale/enetc/enetc_pf.h
379 +++ b/drivers/net/ethernet/freescale/enetc/enetc_pf.h
380 @@ -53,8 +53,3 @@ struct enetc_pf {
381 int enetc_msg_psi_init(struct enetc_pf *pf);
382 void enetc_msg_psi_free(struct enetc_pf *pf);
383 void enetc_msg_handle_rxmsg(struct enetc_pf *pf, int mbox_id, u16 *status);
386 -int enetc_mdio_probe(struct enetc_pf *pf);
387 -void enetc_mdio_remove(struct enetc_pf *pf);
388 -int enetc_imdio_init(struct enetc_pf *pf);
390 +++ b/include/linux/fsl/enetc_mdio.h
392 +/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
393 +/* Copyright 2019 NXP */
395 +#ifndef _FSL_ENETC_MDIO_H_
396 +#define _FSL_ENETC_MDIO_H_
398 +#include <linux/phy.h>
401 +#define ENETC_PCS_LINK_TIMER1 0x12
402 +#define ENETC_PCS_LINK_TIMER1_VAL 0x06a0
403 +#define ENETC_PCS_LINK_TIMER2 0x13
404 +#define ENETC_PCS_LINK_TIMER2_VAL 0x0003
405 +#define ENETC_PCS_IF_MODE 0x14
406 +#define ENETC_PCS_IF_MODE_SGMII_EN BIT(0)
407 +#define ENETC_PCS_IF_MODE_USE_SGMII_AN BIT(1)
408 +#define ENETC_PCS_IF_MODE_SGMII_SPEED(x) (((x) << 2) & GENMASK(3, 2))
410 +/* Not a mistake, the SerDes PLL needs to be set at 3.125 GHz by Reset
411 + * Configuration Word (RCW, outside Linux control) for 2.5G SGMII mode. The PCS
412 + * still thinks it's at gigabit.
414 +enum enetc_pcs_speed {
415 + ENETC_PCS_SPEED_10 = 0,
416 + ENETC_PCS_SPEED_100 = 1,
417 + ENETC_PCS_SPEED_1000 = 2,
418 + ENETC_PCS_SPEED_2500 = 2,
423 +struct enetc_mdio_priv {
424 + struct enetc_hw *hw;
428 +#if IS_REACHABLE(CONFIG_FSL_ENETC_MDIO)
430 +int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum);
431 +int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value);
432 +struct enetc_hw *enetc_hw_alloc(struct device *dev, void __iomem *port_regs);
436 +static inline int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
438 +static inline int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum,
441 +struct enetc_hw *enetc_hw_alloc(struct device *dev, void __iomem *port_regs)
442 +{ return ERR_PTR(-EINVAL); }