xgmac_mdio.c 6.6 KB

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