xgmac_mdio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * QorIQ 10G MDIO Controller
  3. *
  4. * Copyright 2012 Freescale Semiconductor, Inc.
  5. *
  6. * Authors: Andy Fleming <afleming@freescale.com>
  7. * Timur Tabi <timur@freescale.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public License
  10. * version 2. This program is licensed "as is" without any warranty of any
  11. * kind, whether express or implied.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/phy.h>
  18. #include <linux/mdio.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_mdio.h>
  21. /* Number of microseconds to wait for a register to respond */
  22. #define TIMEOUT 1000
  23. struct tgec_mdio_controller {
  24. __be32 reserved[12];
  25. __be32 mdio_stat; /* MDIO configuration and status */
  26. __be32 mdio_ctl; /* MDIO control */
  27. __be32 mdio_data; /* MDIO data */
  28. __be32 mdio_addr; /* MDIO address */
  29. } __packed;
  30. #define MDIO_STAT_CLKDIV(x) (((x>>1) & 0xff) << 8)
  31. #define MDIO_STAT_BSY (1 << 0)
  32. #define MDIO_STAT_RD_ER (1 << 1)
  33. #define MDIO_CTL_DEV_ADDR(x) (x & 0x1f)
  34. #define MDIO_CTL_PORT_ADDR(x) ((x & 0x1f) << 5)
  35. #define MDIO_CTL_PRE_DIS (1 << 10)
  36. #define MDIO_CTL_SCAN_EN (1 << 11)
  37. #define MDIO_CTL_POST_INC (1 << 14)
  38. #define MDIO_CTL_READ (1 << 15)
  39. #define MDIO_DATA(x) (x & 0xffff)
  40. #define MDIO_DATA_BSY (1 << 31)
  41. /*
  42. * Wait untill the MDIO bus is free
  43. */
  44. static int xgmac_wait_until_free(struct device *dev,
  45. struct tgec_mdio_controller __iomem *regs)
  46. {
  47. uint32_t status;
  48. /* Wait till the bus is free */
  49. status = spin_event_timeout(
  50. !((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY), TIMEOUT, 0);
  51. if (!status) {
  52. dev_err(dev, "timeout waiting for bus to be free\n");
  53. return -ETIMEDOUT;
  54. }
  55. return 0;
  56. }
  57. /*
  58. * Wait till the MDIO read or write operation is complete
  59. */
  60. static int xgmac_wait_until_done(struct device *dev,
  61. struct tgec_mdio_controller __iomem *regs)
  62. {
  63. uint32_t status;
  64. /* Wait till the MDIO write is complete */
  65. status = spin_event_timeout(
  66. !((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY), TIMEOUT, 0);
  67. if (!status) {
  68. dev_err(dev, "timeout waiting for operation to complete\n");
  69. return -ETIMEDOUT;
  70. }
  71. return 0;
  72. }
  73. /*
  74. * Write value to the PHY for this device to the register at regnum,waiting
  75. * until the write is done before it returns. All PHY configuration has to be
  76. * done through the TSEC1 MIIM regs.
  77. */
  78. static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value)
  79. {
  80. struct tgec_mdio_controller __iomem *regs = bus->priv;
  81. uint16_t dev_addr = regnum >> 16;
  82. int ret;
  83. /* Setup the MII Mgmt clock speed */
  84. out_be32(&regs->mdio_stat, MDIO_STAT_CLKDIV(100));
  85. ret = xgmac_wait_until_free(&bus->dev, regs);
  86. if (ret)
  87. return ret;
  88. /* Set the port and dev addr */
  89. out_be32(&regs->mdio_ctl,
  90. MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr));
  91. /* Set the register address */
  92. out_be32(&regs->mdio_addr, regnum & 0xffff);
  93. ret = xgmac_wait_until_free(&bus->dev, regs);
  94. if (ret)
  95. return ret;
  96. /* Write the value to the register */
  97. out_be32(&regs->mdio_data, MDIO_DATA(value));
  98. ret = xgmac_wait_until_done(&bus->dev, regs);
  99. if (ret)
  100. return ret;
  101. return 0;
  102. }
  103. /*
  104. * Reads from register regnum in the PHY for device dev, returning the value.
  105. * Clears miimcom first. All PHY configuration has to be done through the
  106. * TSEC1 MIIM regs.
  107. */
  108. static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
  109. {
  110. struct tgec_mdio_controller __iomem *regs = bus->priv;
  111. uint16_t dev_addr = regnum >> 16;
  112. uint32_t mdio_ctl;
  113. uint16_t value;
  114. int ret;
  115. /* Setup the MII Mgmt clock speed */
  116. out_be32(&regs->mdio_stat, MDIO_STAT_CLKDIV(100));
  117. ret = xgmac_wait_until_free(&bus->dev, regs);
  118. if (ret)
  119. return ret;
  120. /* Set the Port and Device Addrs */
  121. mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
  122. out_be32(&regs->mdio_ctl, mdio_ctl);
  123. /* Set the register address */
  124. out_be32(&regs->mdio_addr, regnum & 0xffff);
  125. ret = xgmac_wait_until_free(&bus->dev, regs);
  126. if (ret)
  127. return ret;
  128. /* Initiate the read */
  129. out_be32(&regs->mdio_ctl, mdio_ctl | MDIO_CTL_READ);
  130. ret = xgmac_wait_until_done(&bus->dev, regs);
  131. if (ret)
  132. return ret;
  133. /* Return all Fs if nothing was there */
  134. if (in_be32(&regs->mdio_stat) & MDIO_STAT_RD_ER) {
  135. dev_err(&bus->dev, "MDIO read error\n");
  136. return 0xffff;
  137. }
  138. value = in_be32(&regs->mdio_data) & 0xffff;
  139. dev_dbg(&bus->dev, "read %04x\n", value);
  140. return value;
  141. }
  142. /* Reset the MIIM registers, and wait for the bus to free */
  143. static int xgmac_mdio_reset(struct mii_bus *bus)
  144. {
  145. struct tgec_mdio_controller __iomem *regs = bus->priv;
  146. int ret;
  147. mutex_lock(&bus->mdio_lock);
  148. /* Setup the MII Mgmt clock speed */
  149. out_be32(&regs->mdio_stat, MDIO_STAT_CLKDIV(100));
  150. ret = xgmac_wait_until_free(&bus->dev, regs);
  151. mutex_unlock(&bus->mdio_lock);
  152. return ret;
  153. }
  154. static int xgmac_mdio_probe(struct platform_device *pdev)
  155. {
  156. struct device_node *np = pdev->dev.of_node;
  157. struct mii_bus *bus;
  158. struct resource res;
  159. int ret;
  160. ret = of_address_to_resource(np, 0, &res);
  161. if (ret) {
  162. dev_err(&pdev->dev, "could not obtain address\n");
  163. return ret;
  164. }
  165. bus = mdiobus_alloc_size(PHY_MAX_ADDR * sizeof(int));
  166. if (!bus)
  167. return -ENOMEM;
  168. bus->name = "Freescale XGMAC MDIO Bus";
  169. bus->read = xgmac_mdio_read;
  170. bus->write = xgmac_mdio_write;
  171. bus->reset = xgmac_mdio_reset;
  172. bus->irq = bus->priv;
  173. bus->parent = &pdev->dev;
  174. snprintf(bus->id, MII_BUS_ID_SIZE, "%llx", (unsigned long long)res.start);
  175. /* Set the PHY base address */
  176. bus->priv = of_iomap(np, 0);
  177. if (!bus->priv) {
  178. ret = -ENOMEM;
  179. goto err_ioremap;
  180. }
  181. ret = of_mdiobus_register(bus, np);
  182. if (ret) {
  183. dev_err(&pdev->dev, "cannot register MDIO bus\n");
  184. goto err_registration;
  185. }
  186. dev_set_drvdata(&pdev->dev, bus);
  187. return 0;
  188. err_registration:
  189. iounmap(bus->priv);
  190. err_ioremap:
  191. mdiobus_free(bus);
  192. return ret;
  193. }
  194. static int xgmac_mdio_remove(struct platform_device *pdev)
  195. {
  196. struct mii_bus *bus = dev_get_drvdata(&pdev->dev);
  197. mdiobus_unregister(bus);
  198. iounmap(bus->priv);
  199. mdiobus_free(bus);
  200. return 0;
  201. }
  202. static struct of_device_id xgmac_mdio_match[] = {
  203. {
  204. .compatible = "fsl,fman-xmdio",
  205. },
  206. {},
  207. };
  208. MODULE_DEVICE_TABLE(of, xgmac_mdio_match);
  209. static struct platform_driver xgmac_mdio_driver = {
  210. .driver = {
  211. .name = "fsl-fman_xmdio",
  212. .of_match_table = xgmac_mdio_match,
  213. },
  214. .probe = xgmac_mdio_probe,
  215. .remove = xgmac_mdio_remove,
  216. };
  217. module_platform_driver(xgmac_mdio_driver);
  218. MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller");
  219. MODULE_LICENSE("GPL v2");