ll_temac_mdio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. u32 bus_hz;
  52. int clk_div;
  53. int rc;
  54. struct resource res;
  55. /* Calculate a reasonable divisor for the clock rate */
  56. clk_div = 0x3f; /* worst-case default setting */
  57. if (of_property_read_u32(np, "clock-frequency", &bus_hz) == 0) {
  58. clk_div = bus_hz / (2500 * 1000 * 2) - 1;
  59. if (clk_div < 1)
  60. clk_div = 1;
  61. if (clk_div > 0x3f)
  62. clk_div = 0x3f;
  63. }
  64. /* Enable the MDIO bus by asserting the enable bit and writing
  65. * in the clock config */
  66. mutex_lock(&lp->indirect_mutex);
  67. temac_indirect_out32(lp, XTE_MC_OFFSET, 1 << 6 | clk_div);
  68. mutex_unlock(&lp->indirect_mutex);
  69. bus = mdiobus_alloc();
  70. if (!bus)
  71. return -ENOMEM;
  72. of_address_to_resource(np, 0, &res);
  73. snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
  74. (unsigned long long)res.start);
  75. bus->priv = lp;
  76. bus->name = "Xilinx TEMAC MDIO";
  77. bus->read = temac_mdio_read;
  78. bus->write = temac_mdio_write;
  79. bus->parent = lp->dev;
  80. bus->irq = lp->mdio_irqs; /* preallocated IRQ table */
  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. kfree(lp->mii_bus->irq);
  98. mdiobus_free(lp->mii_bus);
  99. lp->mii_bus = NULL;
  100. }