xgmac_mdio.c 7.8 KB

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