xgmac_mdio.c 7.8 KB

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