ll_temac_mdio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MDIO bus driver for the Xilinx TEMAC device
  4. *
  5. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  6. */
  7. #include <linux/io.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/mutex.h>
  10. #include <linux/phy.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_address.h>
  14. #include <linux/slab.h>
  15. #include <linux/of_mdio.h>
  16. #include <linux/platform_data/xilinx-ll-temac.h>
  17. #include "ll_temac.h"
  18. /* ---------------------------------------------------------------------
  19. * MDIO Bus functions
  20. */
  21. static int temac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  22. {
  23. struct temac_local *lp = bus->priv;
  24. u32 rc;
  25. unsigned long flags;
  26. /* Write the PHY address to the MIIM Access Initiator register.
  27. * When the transfer completes, the PHY register value will appear
  28. * in the LSW0 register */
  29. spin_lock_irqsave(lp->indirect_lock, flags);
  30. temac_iow(lp, XTE_LSW0_OFFSET, (phy_id << 5) | reg);
  31. rc = temac_indirect_in32_locked(lp, XTE_MIIMAI_OFFSET);
  32. spin_unlock_irqrestore(lp->indirect_lock, flags);
  33. dev_dbg(lp->dev, "temac_mdio_read(phy_id=%i, reg=%x) == %x\n",
  34. phy_id, reg, rc);
  35. return rc;
  36. }
  37. static int temac_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
  38. {
  39. struct temac_local *lp = bus->priv;
  40. unsigned long flags;
  41. dev_dbg(lp->dev, "temac_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
  42. phy_id, reg, val);
  43. /* First write the desired value into the write data register
  44. * and then write the address into the access initiator register
  45. */
  46. spin_lock_irqsave(lp->indirect_lock, flags);
  47. temac_indirect_out32_locked(lp, XTE_MGTDR_OFFSET, val);
  48. temac_indirect_out32_locked(lp, XTE_MIIMAI_OFFSET, (phy_id << 5) | reg);
  49. spin_unlock_irqrestore(lp->indirect_lock, flags);
  50. return 0;
  51. }
  52. int temac_mdio_setup(struct temac_local *lp, struct platform_device *pdev)
  53. {
  54. struct ll_temac_platform_data *pdata = dev_get_platdata(&pdev->dev);
  55. struct device_node *np = dev_of_node(&pdev->dev);
  56. struct mii_bus *bus;
  57. u32 bus_hz;
  58. int clk_div;
  59. int rc;
  60. struct resource res;
  61. /* Get MDIO bus frequency (if specified) */
  62. bus_hz = 0;
  63. if (np)
  64. of_property_read_u32(np, "clock-frequency", &bus_hz);
  65. else if (pdata)
  66. bus_hz = pdata->mdio_clk_freq;
  67. /* Calculate a reasonable divisor for the clock rate */
  68. clk_div = 0x3f; /* worst-case default setting */
  69. if (bus_hz != 0) {
  70. clk_div = bus_hz / (2500 * 1000 * 2) - 1;
  71. if (clk_div < 1)
  72. clk_div = 1;
  73. if (clk_div > 0x3f)
  74. clk_div = 0x3f;
  75. }
  76. /* Enable the MDIO bus by asserting the enable bit and writing
  77. * in the clock config */
  78. temac_indirect_out32(lp, XTE_MC_OFFSET, 1 << 6 | clk_div);
  79. bus = devm_mdiobus_alloc(&pdev->dev);
  80. if (!bus)
  81. return -ENOMEM;
  82. if (np) {
  83. of_address_to_resource(np, 0, &res);
  84. snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
  85. (unsigned long long)res.start);
  86. } else if (pdata) {
  87. snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
  88. pdata->mdio_bus_id);
  89. }
  90. bus->priv = lp;
  91. bus->name = "Xilinx TEMAC MDIO";
  92. bus->read = temac_mdio_read;
  93. bus->write = temac_mdio_write;
  94. bus->parent = lp->dev;
  95. lp->mii_bus = bus;
  96. rc = of_mdiobus_register(bus, np);
  97. if (rc)
  98. return rc;
  99. dev_dbg(lp->dev, "MDIO bus registered; MC:%x\n",
  100. temac_indirect_in32(lp, XTE_MC_OFFSET));
  101. return 0;
  102. }
  103. void temac_mdio_teardown(struct temac_local *lp)
  104. {
  105. mdiobus_unregister(lp->mii_bus);
  106. }