ll_temac_mdio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * MDIO bus driver for the Xilinx TEMAC device
  3. *
  4. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  5. */
  6. #include <linux/io.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/mutex.h>
  9. #include <linux/phy.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/of_address.h>
  13. #include <linux/slab.h>
  14. #include <linux/of_mdio.h>
  15. #include "ll_temac.h"
  16. /* ---------------------------------------------------------------------
  17. * MDIO Bus functions
  18. */
  19. static int temac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  20. {
  21. struct temac_local *lp = bus->priv;
  22. u32 rc;
  23. /* Write the PHY address to the MIIM Access Initiator register.
  24. * When the transfer completes, the PHY register value will appear
  25. * in the LSW0 register */
  26. mutex_lock(&lp->indirect_mutex);
  27. temac_iow(lp, XTE_LSW0_OFFSET, (phy_id << 5) | reg);
  28. rc = temac_indirect_in32(lp, XTE_MIIMAI_OFFSET);
  29. mutex_unlock(&lp->indirect_mutex);
  30. dev_dbg(lp->dev, "temac_mdio_read(phy_id=%i, reg=%x) == %x\n",
  31. phy_id, reg, rc);
  32. return rc;
  33. }
  34. static int temac_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
  35. {
  36. struct temac_local *lp = bus->priv;
  37. dev_dbg(lp->dev, "temac_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
  38. phy_id, reg, val);
  39. /* First write the desired value into the write data register
  40. * and then write the address into the access initiator register
  41. */
  42. mutex_lock(&lp->indirect_mutex);
  43. temac_indirect_out32(lp, XTE_MGTDR_OFFSET, val);
  44. temac_indirect_out32(lp, XTE_MIIMAI_OFFSET, (phy_id << 5) | reg);
  45. mutex_unlock(&lp->indirect_mutex);
  46. return 0;
  47. }
  48. int temac_mdio_setup(struct temac_local *lp, struct device_node *np)
  49. {
  50. struct mii_bus *bus;
  51. const u32 *bus_hz;
  52. int clk_div;
  53. int rc, size;
  54. struct resource res;
  55. /* Calculate a reasonable divisor for the clock rate */
  56. clk_div = 0x3f; /* worst-case default setting */
  57. bus_hz = of_get_property(np, "clock-frequency", &size);
  58. if (bus_hz && size >= sizeof(*bus_hz)) {
  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. bus->irq = lp->mdio_irqs; /* preallocated IRQ table */
  82. lp->mii_bus = bus;
  83. rc = of_mdiobus_register(bus, np);
  84. if (rc)
  85. goto err_register;
  86. mutex_lock(&lp->indirect_mutex);
  87. dev_dbg(lp->dev, "MDIO bus registered; MC:%x\n",
  88. temac_indirect_in32(lp, XTE_MC_OFFSET));
  89. mutex_unlock(&lp->indirect_mutex);
  90. return 0;
  91. err_register:
  92. mdiobus_free(bus);
  93. return rc;
  94. }
  95. void temac_mdio_teardown(struct temac_local *lp)
  96. {
  97. mdiobus_unregister(lp->mii_bus);
  98. kfree(lp->mii_bus->irq);
  99. mdiobus_free(lp->mii_bus);
  100. lp->mii_bus = NULL;
  101. }