1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3 STMMAC Ethernet Driver -- MDIO bus implementation
4 Provides Bus interface for MII registers
6 Copyright (C) 2007-2009 STMicroelectronics Ltd
9 Author: Carl Shaw <carl.shaw@st.com>
10 Maintainer: Giuseppe Cavallaro <peppe.cavallaro@st.com>
11 *******************************************************************************/
13 #include <linux/gpio/consumer.h>
15 #include <linux/iopoll.h>
16 #include <linux/mii.h>
17 #include <linux/of_mdio.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/phy.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
26 #define MII_BUSY 0x00000001
27 #define MII_WRITE 0x00000002
28 #define MII_DATA_MASK GENMASK(15, 0)
31 #define MII_GMAC4_GOC_SHIFT 2
32 #define MII_GMAC4_REG_ADDR_SHIFT 16
33 #define MII_GMAC4_WRITE (1 << MII_GMAC4_GOC_SHIFT)
34 #define MII_GMAC4_READ (3 << MII_GMAC4_GOC_SHIFT)
35 #define MII_GMAC4_C45E BIT(1)
38 #define MII_XGMAC_SADDR BIT(18)
39 #define MII_XGMAC_CMD_SHIFT 16
40 #define MII_XGMAC_WRITE (1 << MII_XGMAC_CMD_SHIFT)
41 #define MII_XGMAC_READ (3 << MII_XGMAC_CMD_SHIFT)
42 #define MII_XGMAC_BUSY BIT(22)
43 #define MII_XGMAC_MAX_C22ADDR 3
44 #define MII_XGMAC_C22P_MASK GENMASK(MII_XGMAC_MAX_C22ADDR, 0)
45 #define MII_XGMAC_PA_SHIFT 16
46 #define MII_XGMAC_DA_SHIFT 21
48 static void stmmac_xgmac2_c45_format(struct stmmac_priv *priv, int phyaddr,
49 int devad, int phyreg, u32 *hw_addr)
53 /* Set port as Clause 45 */
54 tmp = readl(priv->ioaddr + XGMAC_MDIO_C22P);
56 writel(tmp, priv->ioaddr + XGMAC_MDIO_C22P);
58 *hw_addr = (phyaddr << MII_XGMAC_PA_SHIFT) | (phyreg & 0xffff);
59 *hw_addr |= devad << MII_XGMAC_DA_SHIFT;
62 static void stmmac_xgmac2_c22_format(struct stmmac_priv *priv, int phyaddr,
63 int phyreg, u32 *hw_addr)
67 /* Set port as Clause 22 */
68 tmp = readl(priv->ioaddr + XGMAC_MDIO_C22P);
69 tmp &= ~MII_XGMAC_C22P_MASK;
71 writel(tmp, priv->ioaddr + XGMAC_MDIO_C22P);
73 *hw_addr = (phyaddr << MII_XGMAC_PA_SHIFT) | (phyreg & 0x1f);
76 static int stmmac_xgmac2_mdio_read(struct stmmac_priv *priv, u32 addr,
79 unsigned int mii_address = priv->hw->mii.addr;
80 unsigned int mii_data = priv->hw->mii.data;
84 ret = pm_runtime_resume_and_get(priv->device);
88 /* Wait until any existing MII operation is complete */
89 if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
90 !(tmp & MII_XGMAC_BUSY), 100, 10000)) {
92 goto err_disable_clks;
95 value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
96 & priv->hw->mii.clk_csr_mask;
97 value |= MII_XGMAC_READ;
99 /* Wait until any existing MII operation is complete */
100 if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
101 !(tmp & MII_XGMAC_BUSY), 100, 10000)) {
103 goto err_disable_clks;
106 /* Set the MII address register to read */
107 writel(addr, priv->ioaddr + mii_address);
108 writel(value, priv->ioaddr + mii_data);
110 /* Wait until any existing MII operation is complete */
111 if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
112 !(tmp & MII_XGMAC_BUSY), 100, 10000)) {
114 goto err_disable_clks;
117 /* Read the data from the MII data register */
118 ret = (int)readl(priv->ioaddr + mii_data) & GENMASK(15, 0);
121 pm_runtime_put(priv->device);
126 static int stmmac_xgmac2_mdio_read_c22(struct mii_bus *bus, int phyaddr,
129 struct net_device *ndev = bus->priv;
130 struct stmmac_priv *priv;
133 priv = netdev_priv(ndev);
135 /* HW does not support C22 addr >= 4 */
136 if (phyaddr > MII_XGMAC_MAX_C22ADDR)
139 stmmac_xgmac2_c22_format(priv, phyaddr, phyreg, &addr);
141 return stmmac_xgmac2_mdio_read(priv, addr, MII_XGMAC_BUSY);
144 static int stmmac_xgmac2_mdio_read_c45(struct mii_bus *bus, int phyaddr,
145 int devad, int phyreg)
147 struct net_device *ndev = bus->priv;
148 struct stmmac_priv *priv;
151 priv = netdev_priv(ndev);
153 stmmac_xgmac2_c45_format(priv, phyaddr, devad, phyreg, &addr);
155 return stmmac_xgmac2_mdio_read(priv, addr, MII_XGMAC_BUSY);
158 static int stmmac_xgmac2_mdio_write(struct stmmac_priv *priv, u32 addr,
159 u32 value, u16 phydata)
161 unsigned int mii_address = priv->hw->mii.addr;
162 unsigned int mii_data = priv->hw->mii.data;
166 ret = pm_runtime_resume_and_get(priv->device);
170 /* Wait until any existing MII operation is complete */
171 if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
172 !(tmp & MII_XGMAC_BUSY), 100, 10000)) {
174 goto err_disable_clks;
177 value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
178 & priv->hw->mii.clk_csr_mask;
180 value |= MII_XGMAC_WRITE;
182 /* Wait until any existing MII operation is complete */
183 if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
184 !(tmp & MII_XGMAC_BUSY), 100, 10000)) {
186 goto err_disable_clks;
189 /* Set the MII address register to write */
190 writel(addr, priv->ioaddr + mii_address);
191 writel(value, priv->ioaddr + mii_data);
193 /* Wait until any existing MII operation is complete */
194 ret = readl_poll_timeout(priv->ioaddr + mii_data, tmp,
195 !(tmp & MII_XGMAC_BUSY), 100, 10000);
198 pm_runtime_put(priv->device);
203 static int stmmac_xgmac2_mdio_write_c22(struct mii_bus *bus, int phyaddr,
204 int phyreg, u16 phydata)
206 struct net_device *ndev = bus->priv;
207 struct stmmac_priv *priv;
210 priv = netdev_priv(ndev);
212 /* HW does not support C22 addr >= 4 */
213 if (phyaddr > MII_XGMAC_MAX_C22ADDR)
216 stmmac_xgmac2_c22_format(priv, phyaddr, phyreg, &addr);
218 return stmmac_xgmac2_mdio_write(priv, addr,
219 MII_XGMAC_BUSY | MII_XGMAC_SADDR, phydata);
222 static int stmmac_xgmac2_mdio_write_c45(struct mii_bus *bus, int phyaddr,
223 int devad, int phyreg, u16 phydata)
225 struct net_device *ndev = bus->priv;
226 struct stmmac_priv *priv;
229 priv = netdev_priv(ndev);
231 stmmac_xgmac2_c45_format(priv, phyaddr, devad, phyreg, &addr);
233 return stmmac_xgmac2_mdio_write(priv, addr, MII_XGMAC_BUSY,
237 static int stmmac_mdio_read(struct stmmac_priv *priv, int data, u32 value)
239 unsigned int mii_address = priv->hw->mii.addr;
240 unsigned int mii_data = priv->hw->mii.data;
243 if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
247 writel(data, priv->ioaddr + mii_data);
248 writel(value, priv->ioaddr + mii_address);
250 if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
254 /* Read the data from the MII data register */
255 return readl(priv->ioaddr + mii_data) & MII_DATA_MASK;
259 * stmmac_mdio_read_c22
260 * @bus: points to the mii_bus structure
263 * Description: it reads data from the MII register from within the phy device.
264 * For the 7111 GMAC, we must set the bit 0 in the MII address register while
265 * accessing the PHY registers.
266 * Fortunately, it seems this has no drawback for the 7109 MAC.
268 static int stmmac_mdio_read_c22(struct mii_bus *bus, int phyaddr, int phyreg)
270 struct net_device *ndev = bus->priv;
271 struct stmmac_priv *priv = netdev_priv(ndev);
272 u32 value = MII_BUSY;
275 data = pm_runtime_resume_and_get(priv->device);
279 value |= (phyaddr << priv->hw->mii.addr_shift)
280 & priv->hw->mii.addr_mask;
281 value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
282 value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
283 & priv->hw->mii.clk_csr_mask;
284 if (priv->plat->has_gmac4)
285 value |= MII_GMAC4_READ;
287 data = stmmac_mdio_read(priv, data, value);
289 pm_runtime_put(priv->device);
295 * stmmac_mdio_read_c45
296 * @bus: points to the mii_bus structure
298 * @devad: device address to read
300 * Description: it reads data from the MII register from within the phy device.
301 * For the 7111 GMAC, we must set the bit 0 in the MII address register while
302 * accessing the PHY registers.
303 * Fortunately, it seems this has no drawback for the 7109 MAC.
305 static int stmmac_mdio_read_c45(struct mii_bus *bus, int phyaddr, int devad,
308 struct net_device *ndev = bus->priv;
309 struct stmmac_priv *priv = netdev_priv(ndev);
310 u32 value = MII_BUSY;
313 data = pm_runtime_get_sync(priv->device);
315 pm_runtime_put_noidle(priv->device);
319 value |= (phyaddr << priv->hw->mii.addr_shift)
320 & priv->hw->mii.addr_mask;
321 value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
322 value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
323 & priv->hw->mii.clk_csr_mask;
324 value |= MII_GMAC4_READ;
325 value |= MII_GMAC4_C45E;
326 value &= ~priv->hw->mii.reg_mask;
327 value |= (devad << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
329 data |= phyreg << MII_GMAC4_REG_ADDR_SHIFT;
331 data = stmmac_mdio_read(priv, data, value);
333 pm_runtime_put(priv->device);
338 static int stmmac_mdio_write(struct stmmac_priv *priv, int data, u32 value)
340 unsigned int mii_address = priv->hw->mii.addr;
341 unsigned int mii_data = priv->hw->mii.data;
344 /* Wait until any existing MII operation is complete */
345 if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
349 /* Set the MII address register to write */
350 writel(data, priv->ioaddr + mii_data);
351 writel(value, priv->ioaddr + mii_address);
353 /* Wait until any existing MII operation is complete */
354 return readl_poll_timeout(priv->ioaddr + mii_address, v,
355 !(v & MII_BUSY), 100, 10000);
359 * stmmac_mdio_write_c22
360 * @bus: points to the mii_bus structure
364 * Description: it writes the data into the MII register from within the device.
366 static int stmmac_mdio_write_c22(struct mii_bus *bus, int phyaddr, int phyreg,
369 struct net_device *ndev = bus->priv;
370 struct stmmac_priv *priv = netdev_priv(ndev);
371 int ret, data = phydata;
372 u32 value = MII_BUSY;
374 ret = pm_runtime_resume_and_get(priv->device);
378 value |= (phyaddr << priv->hw->mii.addr_shift)
379 & priv->hw->mii.addr_mask;
380 value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
382 value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
383 & priv->hw->mii.clk_csr_mask;
384 if (priv->plat->has_gmac4)
385 value |= MII_GMAC4_WRITE;
389 ret = stmmac_mdio_write(priv, data, value);
391 pm_runtime_put(priv->device);
397 * stmmac_mdio_write_c45
398 * @bus: points to the mii_bus structure
401 * @devad: device address to read
403 * Description: it writes the data into the MII register from within the device.
405 static int stmmac_mdio_write_c45(struct mii_bus *bus, int phyaddr,
406 int devad, int phyreg, u16 phydata)
408 struct net_device *ndev = bus->priv;
409 struct stmmac_priv *priv = netdev_priv(ndev);
410 int ret, data = phydata;
411 u32 value = MII_BUSY;
413 ret = pm_runtime_get_sync(priv->device);
415 pm_runtime_put_noidle(priv->device);
419 value |= (phyaddr << priv->hw->mii.addr_shift)
420 & priv->hw->mii.addr_mask;
421 value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
423 value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
424 & priv->hw->mii.clk_csr_mask;
426 value |= MII_GMAC4_WRITE;
427 value |= MII_GMAC4_C45E;
428 value &= ~priv->hw->mii.reg_mask;
429 value |= (devad << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
431 data |= phyreg << MII_GMAC4_REG_ADDR_SHIFT;
433 ret = stmmac_mdio_write(priv, data, value);
435 pm_runtime_put(priv->device);
442 * @bus: points to the mii_bus structure
443 * Description: reset the MII bus
445 int stmmac_mdio_reset(struct mii_bus *bus)
447 #if IS_ENABLED(CONFIG_STMMAC_PLATFORM)
448 struct net_device *ndev = bus->priv;
449 struct stmmac_priv *priv = netdev_priv(ndev);
450 unsigned int mii_address = priv->hw->mii.addr;
453 if (priv->device->of_node) {
454 struct gpio_desc *reset_gpio;
455 u32 delays[3] = { 0, 0, 0 };
457 reset_gpio = devm_gpiod_get_optional(priv->device,
460 if (IS_ERR(reset_gpio))
461 return PTR_ERR(reset_gpio);
463 device_property_read_u32_array(priv->device,
464 "snps,reset-delays-us",
465 delays, ARRAY_SIZE(delays));
468 msleep(DIV_ROUND_UP(delays[0], 1000));
470 gpiod_set_value_cansleep(reset_gpio, 1);
472 msleep(DIV_ROUND_UP(delays[1], 1000));
474 gpiod_set_value_cansleep(reset_gpio, 0);
476 msleep(DIV_ROUND_UP(delays[2], 1000));
480 /* This is a workaround for problems with the STE101P PHY.
481 * It doesn't complete its reset until at least one clock cycle
482 * on MDC, so perform a dummy mdio read. To be updated for GMAC4
485 if (!priv->plat->has_gmac4)
486 writel(0, priv->ioaddr + mii_address);
491 int stmmac_xpcs_setup(struct mii_bus *bus)
493 struct net_device *ndev = bus->priv;
494 struct stmmac_priv *priv;
495 struct dw_xpcs *xpcs;
498 priv = netdev_priv(ndev);
499 mode = priv->plat->phy_interface;
501 /* Try to probe the XPCS by scanning all addresses. */
502 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
503 xpcs = xpcs_create_mdiodev(bus, addr, mode);
507 priv->hw->xpcs = xpcs;
511 if (!priv->hw->xpcs) {
512 dev_warn(priv->device, "No xPCS found\n");
520 * stmmac_mdio_register
521 * @ndev: net device structure
522 * Description: it registers the MII bus
524 int stmmac_mdio_register(struct net_device *ndev)
527 struct mii_bus *new_bus;
528 struct stmmac_priv *priv = netdev_priv(ndev);
529 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
530 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
531 struct device_node *mdio_node = priv->plat->mdio_node;
532 struct device *dev = ndev->dev.parent;
533 struct fwnode_handle *fixed_node;
534 int addr, found, max_addr;
539 new_bus = mdiobus_alloc();
543 if (mdio_bus_data->irqs)
544 memcpy(new_bus->irq, mdio_bus_data->irqs, sizeof(new_bus->irq));
546 new_bus->name = "stmmac";
548 if (priv->plat->has_xgmac) {
549 new_bus->read = &stmmac_xgmac2_mdio_read_c22;
550 new_bus->write = &stmmac_xgmac2_mdio_write_c22;
551 new_bus->read_c45 = &stmmac_xgmac2_mdio_read_c45;
552 new_bus->write_c45 = &stmmac_xgmac2_mdio_write_c45;
554 /* Right now only C22 phys are supported */
555 max_addr = MII_XGMAC_MAX_C22ADDR + 1;
557 /* Check if DT specified an unsupported phy addr */
558 if (priv->plat->phy_addr > MII_XGMAC_MAX_C22ADDR)
559 dev_err(dev, "Unsupported phy_addr (max=%d)\n",
560 MII_XGMAC_MAX_C22ADDR);
562 new_bus->read = &stmmac_mdio_read_c22;
563 new_bus->write = &stmmac_mdio_write_c22;
564 if (priv->plat->has_gmac4) {
565 new_bus->read_c45 = &stmmac_mdio_read_c45;
566 new_bus->write_c45 = &stmmac_mdio_write_c45;
569 max_addr = PHY_MAX_ADDR;
572 if (mdio_bus_data->needs_reset)
573 new_bus->reset = &stmmac_mdio_reset;
575 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-%x",
576 new_bus->name, priv->plat->bus_id);
577 new_bus->priv = ndev;
578 new_bus->phy_mask = mdio_bus_data->phy_mask;
579 new_bus->parent = priv->device;
581 err = of_mdiobus_register(new_bus, mdio_node);
583 dev_err_probe(dev, err, "Cannot register the MDIO bus\n");
584 goto bus_register_fail;
587 /* Looks like we need a dummy read for XGMAC only and C45 PHYs */
588 if (priv->plat->has_xgmac)
589 stmmac_xgmac2_mdio_read_c45(new_bus, 0, 0, 0);
591 /* If fixed-link is set, skip PHY scanning */
593 fwnode = dev_fwnode(priv->device);
596 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
598 fwnode_handle_put(fixed_node);
599 goto bus_register_done;
603 if (priv->plat->phy_node || mdio_node)
604 goto bus_register_done;
607 for (addr = 0; addr < max_addr; addr++) {
608 struct phy_device *phydev = mdiobus_get_phy(new_bus, addr);
614 * If an IRQ was provided to be assigned after
615 * the bus probe, do it here.
617 if (!mdio_bus_data->irqs &&
618 (mdio_bus_data->probed_phy_irq > 0)) {
619 new_bus->irq[addr] = mdio_bus_data->probed_phy_irq;
620 phydev->irq = mdio_bus_data->probed_phy_irq;
624 * If we're going to bind the MAC to this PHY bus,
625 * and no PHY number was provided to the MAC,
626 * use the one probed here.
628 if (priv->plat->phy_addr == -1)
629 priv->plat->phy_addr = addr;
631 phy_attached_info(phydev);
635 if (!found && !mdio_node) {
636 dev_warn(dev, "No PHY found\n");
647 mdiobus_unregister(new_bus);
649 mdiobus_free(new_bus);
654 * stmmac_mdio_unregister
655 * @ndev: net device structure
656 * Description: it unregisters the MII bus
658 int stmmac_mdio_unregister(struct net_device *ndev)
660 struct stmmac_priv *priv = netdev_priv(ndev);
666 xpcs_destroy(priv->hw->xpcs);
668 mdiobus_unregister(priv->mii);
669 priv->mii->priv = NULL;
670 mdiobus_free(priv->mii);