ll_temac_mdio.c 3.0 KB

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