xgmac_mdio.c 6.4 KB

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