xgmac_mdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * QorIQ 10G MDIO Controller
  3. *
  4. * Copyright 2012 Freescale Semiconductor, Inc.
  5. * Copyright 2021 NXP
  6. *
  7. * Authors: Andy Fleming <afleming@freescale.com>
  8. * Timur Tabi <timur@freescale.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public License
  11. * version 2. This program is licensed "as is" without any warranty of any
  12. * kind, whether express or implied.
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/acpi_mdio.h>
  16. #include <linux/clk.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mdio.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_mdio.h>
  23. #include <linux/phy.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. /* Number of microseconds to wait for a register to respond */
  27. #define TIMEOUT 1000
  28. struct tgec_mdio_controller {
  29. __be32 reserved[12];
  30. __be32 mdio_stat; /* MDIO configuration and status */
  31. __be32 mdio_ctl; /* MDIO control */
  32. __be32 mdio_data; /* MDIO data */
  33. __be32 mdio_addr; /* MDIO address */
  34. } __packed;
  35. #define MDIO_STAT_ENC BIT(6)
  36. #define MDIO_STAT_CLKDIV(x) (((x) & 0x1ff) << 7)
  37. #define MDIO_STAT_BSY BIT(0)
  38. #define MDIO_STAT_RD_ER BIT(1)
  39. #define MDIO_STAT_PRE_DIS BIT(5)
  40. #define MDIO_CTL_DEV_ADDR(x) (x & 0x1f)
  41. #define MDIO_CTL_PORT_ADDR(x) ((x & 0x1f) << 5)
  42. #define MDIO_CTL_PRE_DIS BIT(10)
  43. #define MDIO_CTL_SCAN_EN BIT(11)
  44. #define MDIO_CTL_POST_INC BIT(14)
  45. #define MDIO_CTL_READ BIT(15)
  46. #define MDIO_DATA(x) (x & 0xffff)
  47. struct mdio_fsl_priv {
  48. struct tgec_mdio_controller __iomem *mdio_base;
  49. struct clk *enet_clk;
  50. u32 mdc_freq;
  51. bool is_little_endian;
  52. bool has_a009885;
  53. bool has_a011043;
  54. };
  55. static u32 xgmac_read32(void __iomem *regs,
  56. bool is_little_endian)
  57. {
  58. if (is_little_endian)
  59. return ioread32(regs);
  60. else
  61. return ioread32be(regs);
  62. }
  63. static void xgmac_write32(u32 value,
  64. void __iomem *regs,
  65. bool is_little_endian)
  66. {
  67. if (is_little_endian)
  68. iowrite32(value, regs);
  69. else
  70. iowrite32be(value, regs);
  71. }
  72. /*
  73. * Wait until the MDIO bus is free
  74. */
  75. static int xgmac_wait_until_free(struct device *dev,
  76. struct tgec_mdio_controller __iomem *regs,
  77. bool is_little_endian)
  78. {
  79. unsigned int timeout;
  80. /* Wait till the bus is free */
  81. timeout = TIMEOUT;
  82. while ((xgmac_read32(&regs->mdio_stat, is_little_endian) &
  83. MDIO_STAT_BSY) && timeout) {
  84. cpu_relax();
  85. timeout--;
  86. }
  87. if (!timeout) {
  88. dev_err(dev, "timeout waiting for bus to be free\n");
  89. return -ETIMEDOUT;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Wait till the MDIO read or write operation is complete
  95. */
  96. static int xgmac_wait_until_done(struct device *dev,
  97. struct tgec_mdio_controller __iomem *regs,
  98. bool is_little_endian)
  99. {
  100. unsigned int timeout;
  101. /* Wait till the MDIO write is complete */
  102. timeout = TIMEOUT;
  103. while ((xgmac_read32(&regs->mdio_stat, is_little_endian) &
  104. MDIO_STAT_BSY) && timeout) {
  105. cpu_relax();
  106. timeout--;
  107. }
  108. if (!timeout) {
  109. dev_err(dev, "timeout waiting for operation to complete\n");
  110. return -ETIMEDOUT;
  111. }
  112. return 0;
  113. }
  114. static int xgmac_mdio_write_c22(struct mii_bus *bus, int phy_id, int regnum,
  115. u16 value)
  116. {
  117. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  118. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  119. bool endian = priv->is_little_endian;
  120. u16 dev_addr = regnum & 0x1f;
  121. u32 mdio_ctl, mdio_stat;
  122. int ret;
  123. mdio_stat = xgmac_read32(&regs->mdio_stat, endian);
  124. mdio_stat &= ~MDIO_STAT_ENC;
  125. xgmac_write32(mdio_stat, &regs->mdio_stat, endian);
  126. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  127. if (ret)
  128. return ret;
  129. /* Set the port and dev addr */
  130. mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
  131. xgmac_write32(mdio_ctl, &regs->mdio_ctl, endian);
  132. /* Write the value to the register */
  133. xgmac_write32(MDIO_DATA(value), &regs->mdio_data, endian);
  134. ret = xgmac_wait_until_done(&bus->dev, regs, endian);
  135. if (ret)
  136. return ret;
  137. return 0;
  138. }
  139. static int xgmac_mdio_write_c45(struct mii_bus *bus, int phy_id, int dev_addr,
  140. int regnum, u16 value)
  141. {
  142. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  143. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  144. bool endian = priv->is_little_endian;
  145. u32 mdio_ctl, mdio_stat;
  146. int ret;
  147. mdio_stat = xgmac_read32(&regs->mdio_stat, endian);
  148. mdio_stat |= MDIO_STAT_ENC;
  149. xgmac_write32(mdio_stat, &regs->mdio_stat, endian);
  150. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  151. if (ret)
  152. return ret;
  153. /* Set the port and dev addr */
  154. mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
  155. xgmac_write32(mdio_ctl, &regs->mdio_ctl, endian);
  156. /* Set the register address */
  157. xgmac_write32(regnum & 0xffff, &regs->mdio_addr, endian);
  158. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  159. if (ret)
  160. return ret;
  161. /* Write the value to the register */
  162. xgmac_write32(MDIO_DATA(value), &regs->mdio_data, endian);
  163. ret = xgmac_wait_until_done(&bus->dev, regs, endian);
  164. if (ret)
  165. return ret;
  166. return 0;
  167. }
  168. /* Reads from register regnum in the PHY for device dev, returning the value.
  169. * Clears miimcom first. All PHY configuration has to be done through the
  170. * TSEC1 MIIM regs.
  171. */
  172. static int xgmac_mdio_read_c22(struct mii_bus *bus, int phy_id, int regnum)
  173. {
  174. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  175. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  176. bool endian = priv->is_little_endian;
  177. u16 dev_addr = regnum & 0x1f;
  178. unsigned long flags;
  179. uint32_t mdio_stat;
  180. uint32_t mdio_ctl;
  181. int ret;
  182. mdio_stat = xgmac_read32(&regs->mdio_stat, endian);
  183. mdio_stat &= ~MDIO_STAT_ENC;
  184. xgmac_write32(mdio_stat, &regs->mdio_stat, endian);
  185. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  186. if (ret)
  187. return ret;
  188. /* Set the Port and Device Addrs */
  189. mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
  190. xgmac_write32(mdio_ctl, &regs->mdio_ctl, endian);
  191. if (priv->has_a009885)
  192. /* Once the operation completes, i.e. MDIO_STAT_BSY clears, we
  193. * must read back the data register within 16 MDC cycles.
  194. */
  195. local_irq_save(flags);
  196. /* Initiate the read */
  197. xgmac_write32(mdio_ctl | MDIO_CTL_READ, &regs->mdio_ctl, endian);
  198. ret = xgmac_wait_until_done(&bus->dev, regs, endian);
  199. if (ret)
  200. goto irq_restore;
  201. /* Return all Fs if nothing was there */
  202. if ((xgmac_read32(&regs->mdio_stat, endian) & MDIO_STAT_RD_ER) &&
  203. !priv->has_a011043) {
  204. dev_dbg(&bus->dev,
  205. "Error while reading PHY%d reg at %d.%d\n",
  206. phy_id, dev_addr, regnum);
  207. ret = 0xffff;
  208. } else {
  209. ret = xgmac_read32(&regs->mdio_data, endian) & 0xffff;
  210. dev_dbg(&bus->dev, "read %04x\n", ret);
  211. }
  212. irq_restore:
  213. if (priv->has_a009885)
  214. local_irq_restore(flags);
  215. return ret;
  216. }
  217. /* Reads from register regnum in the PHY for device dev, returning the value.
  218. * Clears miimcom first. All PHY configuration has to be done through the
  219. * TSEC1 MIIM regs.
  220. */
  221. static int xgmac_mdio_read_c45(struct mii_bus *bus, int phy_id, int dev_addr,
  222. int regnum)
  223. {
  224. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  225. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  226. bool endian = priv->is_little_endian;
  227. u32 mdio_stat, mdio_ctl;
  228. unsigned long flags;
  229. int ret;
  230. mdio_stat = xgmac_read32(&regs->mdio_stat, endian);
  231. mdio_stat |= MDIO_STAT_ENC;
  232. xgmac_write32(mdio_stat, &regs->mdio_stat, endian);
  233. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  234. if (ret)
  235. return ret;
  236. /* Set the Port and Device Addrs */
  237. mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
  238. xgmac_write32(mdio_ctl, &regs->mdio_ctl, endian);
  239. /* Set the register address */
  240. xgmac_write32(regnum & 0xffff, &regs->mdio_addr, endian);
  241. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  242. if (ret)
  243. return ret;
  244. if (priv->has_a009885)
  245. /* Once the operation completes, i.e. MDIO_STAT_BSY clears, we
  246. * must read back the data register within 16 MDC cycles.
  247. */
  248. local_irq_save(flags);
  249. /* Initiate the read */
  250. xgmac_write32(mdio_ctl | MDIO_CTL_READ, &regs->mdio_ctl, endian);
  251. ret = xgmac_wait_until_done(&bus->dev, regs, endian);
  252. if (ret)
  253. goto irq_restore;
  254. /* Return all Fs if nothing was there */
  255. if ((xgmac_read32(&regs->mdio_stat, endian) & MDIO_STAT_RD_ER) &&
  256. !priv->has_a011043) {
  257. dev_dbg(&bus->dev,
  258. "Error while reading PHY%d reg at %d.%d\n",
  259. phy_id, dev_addr, regnum);
  260. ret = 0xffff;
  261. } else {
  262. ret = xgmac_read32(&regs->mdio_data, endian) & 0xffff;
  263. dev_dbg(&bus->dev, "read %04x\n", ret);
  264. }
  265. irq_restore:
  266. if (priv->has_a009885)
  267. local_irq_restore(flags);
  268. return ret;
  269. }
  270. static int xgmac_mdio_set_mdc_freq(struct mii_bus *bus)
  271. {
  272. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  273. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  274. struct device *dev = bus->parent;
  275. u32 mdio_stat, div;
  276. if (device_property_read_u32(dev, "clock-frequency", &priv->mdc_freq))
  277. return 0;
  278. priv->enet_clk = devm_clk_get(dev, NULL);
  279. if (IS_ERR(priv->enet_clk)) {
  280. dev_err(dev, "Input clock unknown, not changing MDC frequency");
  281. return PTR_ERR(priv->enet_clk);
  282. }
  283. div = ((clk_get_rate(priv->enet_clk) / priv->mdc_freq) - 1) / 2;
  284. if (div < 5 || div > 0x1ff) {
  285. dev_err(dev, "Requested MDC frequency is out of range, ignoring");
  286. return -EINVAL;
  287. }
  288. mdio_stat = xgmac_read32(&regs->mdio_stat, priv->is_little_endian);
  289. mdio_stat &= ~MDIO_STAT_CLKDIV(0x1ff);
  290. mdio_stat |= MDIO_STAT_CLKDIV(div);
  291. xgmac_write32(mdio_stat, &regs->mdio_stat, priv->is_little_endian);
  292. return 0;
  293. }
  294. static void xgmac_mdio_set_suppress_preamble(struct mii_bus *bus)
  295. {
  296. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  297. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  298. struct device *dev = bus->parent;
  299. u32 mdio_stat;
  300. if (!device_property_read_bool(dev, "suppress-preamble"))
  301. return;
  302. mdio_stat = xgmac_read32(&regs->mdio_stat, priv->is_little_endian);
  303. mdio_stat |= MDIO_STAT_PRE_DIS;
  304. xgmac_write32(mdio_stat, &regs->mdio_stat, priv->is_little_endian);
  305. }
  306. static int xgmac_mdio_probe(struct platform_device *pdev)
  307. {
  308. struct fwnode_handle *fwnode;
  309. struct mdio_fsl_priv *priv;
  310. struct resource *res;
  311. struct mii_bus *bus;
  312. int ret;
  313. /* In DPAA-1, MDIO is one of the many FMan sub-devices. The FMan
  314. * defines a register space that spans a large area, covering all the
  315. * subdevice areas. Therefore, MDIO cannot claim exclusive access to
  316. * this register area.
  317. */
  318. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  319. if (!res) {
  320. dev_err(&pdev->dev, "could not obtain address\n");
  321. return -EINVAL;
  322. }
  323. bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(struct mdio_fsl_priv));
  324. if (!bus)
  325. return -ENOMEM;
  326. bus->name = "Freescale XGMAC MDIO Bus";
  327. bus->read = xgmac_mdio_read_c22;
  328. bus->write = xgmac_mdio_write_c22;
  329. bus->read_c45 = xgmac_mdio_read_c45;
  330. bus->write_c45 = xgmac_mdio_write_c45;
  331. bus->parent = &pdev->dev;
  332. snprintf(bus->id, MII_BUS_ID_SIZE, "%pa", &res->start);
  333. priv = bus->priv;
  334. priv->mdio_base = devm_ioremap(&pdev->dev, res->start,
  335. resource_size(res));
  336. if (!priv->mdio_base)
  337. return -ENOMEM;
  338. /* For both ACPI and DT cases, endianness of MDIO controller
  339. * needs to be specified using "little-endian" property.
  340. */
  341. priv->is_little_endian = device_property_read_bool(&pdev->dev,
  342. "little-endian");
  343. priv->has_a009885 = device_property_read_bool(&pdev->dev,
  344. "fsl,erratum-a009885");
  345. priv->has_a011043 = device_property_read_bool(&pdev->dev,
  346. "fsl,erratum-a011043");
  347. xgmac_mdio_set_suppress_preamble(bus);
  348. ret = xgmac_mdio_set_mdc_freq(bus);
  349. if (ret)
  350. return ret;
  351. fwnode = dev_fwnode(&pdev->dev);
  352. if (is_of_node(fwnode))
  353. ret = of_mdiobus_register(bus, to_of_node(fwnode));
  354. else if (is_acpi_node(fwnode))
  355. ret = acpi_mdiobus_register(bus, fwnode);
  356. else
  357. ret = -EINVAL;
  358. if (ret) {
  359. dev_err(&pdev->dev, "cannot register MDIO bus\n");
  360. return ret;
  361. }
  362. platform_set_drvdata(pdev, bus);
  363. return 0;
  364. }
  365. static const struct of_device_id xgmac_mdio_match[] = {
  366. {
  367. .compatible = "fsl,fman-xmdio",
  368. },
  369. {
  370. .compatible = "fsl,fman-memac-mdio",
  371. },
  372. {},
  373. };
  374. MODULE_DEVICE_TABLE(of, xgmac_mdio_match);
  375. static const struct acpi_device_id xgmac_acpi_match[] = {
  376. { "NXP0006" },
  377. { }
  378. };
  379. MODULE_DEVICE_TABLE(acpi, xgmac_acpi_match);
  380. static struct platform_driver xgmac_mdio_driver = {
  381. .driver = {
  382. .name = "fsl-fman_xmdio",
  383. .of_match_table = xgmac_mdio_match,
  384. .acpi_match_table = xgmac_acpi_match,
  385. },
  386. .probe = xgmac_mdio_probe,
  387. };
  388. module_platform_driver(xgmac_mdio_driver);
  389. MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller");
  390. MODULE_LICENSE("GPL v2");