ll_temac_mdio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "ll_temac.h"
  17. /* ---------------------------------------------------------------------
  18. * MDIO Bus functions
  19. */
  20. static int temac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  21. {
  22. struct temac_local *lp = bus->priv;
  23. u32 rc;
  24. /* Write the PHY address to the MIIM Access Initiator register.
  25. * When the transfer completes, the PHY register value will appear
  26. * in the LSW0 register */
  27. mutex_lock(&lp->indirect_mutex);
  28. temac_iow(lp, XTE_LSW0_OFFSET, (phy_id << 5) | reg);
  29. rc = temac_indirect_in32(lp, XTE_MIIMAI_OFFSET);
  30. mutex_unlock(&lp->indirect_mutex);
  31. dev_dbg(lp->dev, "temac_mdio_read(phy_id=%i, reg=%x) == %x\n",
  32. phy_id, reg, rc);
  33. return rc;
  34. }
  35. static int temac_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
  36. {
  37. struct temac_local *lp = bus->priv;
  38. dev_dbg(lp->dev, "temac_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
  39. phy_id, reg, val);
  40. /* First write the desired value into the write data register
  41. * and then write the address into the access initiator register
  42. */
  43. mutex_lock(&lp->indirect_mutex);
  44. temac_indirect_out32(lp, XTE_MGTDR_OFFSET, val);
  45. temac_indirect_out32(lp, XTE_MIIMAI_OFFSET, (phy_id << 5) | reg);
  46. mutex_unlock(&lp->indirect_mutex);
  47. return 0;
  48. }
  49. int temac_mdio_setup(struct temac_local *lp, struct device_node *np)
  50. {
  51. struct mii_bus *bus;
  52. u32 bus_hz;
  53. int clk_div;
  54. int rc;
  55. struct resource res;
  56. /* Calculate a reasonable divisor for the clock rate */
  57. clk_div = 0x3f; /* worst-case default setting */
  58. if (of_property_read_u32(np, "clock-frequency", &bus_hz) == 0) {
  59. clk_div = bus_hz / (2500 * 1000 * 2) - 1;
  60. if (clk_div < 1)
  61. clk_div = 1;
  62. if (clk_div > 0x3f)
  63. clk_div = 0x3f;
  64. }
  65. /* Enable the MDIO bus by asserting the enable bit and writing
  66. * in the clock config */
  67. mutex_lock(&lp->indirect_mutex);
  68. temac_indirect_out32(lp, XTE_MC_OFFSET, 1 << 6 | clk_div);
  69. mutex_unlock(&lp->indirect_mutex);
  70. bus = mdiobus_alloc();
  71. if (!bus)
  72. return -ENOMEM;
  73. of_address_to_resource(np, 0, &res);
  74. snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
  75. (unsigned long long)res.start);
  76. bus->priv = lp;
  77. bus->name = "Xilinx TEMAC MDIO";
  78. bus->read = temac_mdio_read;
  79. bus->write = temac_mdio_write;
  80. bus->parent = lp->dev;
  81. lp->mii_bus = bus;
  82. rc = of_mdiobus_register(bus, np);
  83. if (rc)
  84. goto err_register;
  85. mutex_lock(&lp->indirect_mutex);
  86. dev_dbg(lp->dev, "MDIO bus registered; MC:%x\n",
  87. temac_indirect_in32(lp, XTE_MC_OFFSET));
  88. mutex_unlock(&lp->indirect_mutex);
  89. return 0;
  90. err_register:
  91. mdiobus_free(bus);
  92. return rc;
  93. }
  94. void temac_mdio_teardown(struct temac_local *lp)
  95. {
  96. mdiobus_unregister(lp->mii_bus);
  97. mdiobus_free(lp->mii_bus);
  98. lp->mii_bus = NULL;
  99. }