xgmac_mdio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * QorIQ 10G MDIO Controller
  3. *
  4. * Copyright 2012 Freescale Semiconductor, Inc.
  5. * Copyright 2021 NXP
  6. *
  7. * Authors: Andy Fleming <[email protected]>
  8. * Timur Tabi <[email protected]>
  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_address.h>
  22. #include <linux/of_mdio.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/phy.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. /*
  115. * Write value to the PHY for this device to the register at regnum,waiting
  116. * until the write is done before it returns. All PHY configuration has to be
  117. * done through the TSEC1 MIIM regs.
  118. */
  119. static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value)
  120. {
  121. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  122. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  123. uint16_t dev_addr;
  124. u32 mdio_ctl, mdio_stat;
  125. int ret;
  126. bool endian = priv->is_little_endian;
  127. mdio_stat = xgmac_read32(&regs->mdio_stat, endian);
  128. if (regnum & MII_ADDR_C45) {
  129. /* Clause 45 (ie 10G) */
  130. dev_addr = (regnum >> 16) & 0x1f;
  131. mdio_stat |= MDIO_STAT_ENC;
  132. } else {
  133. /* Clause 22 (ie 1G) */
  134. dev_addr = regnum & 0x1f;
  135. mdio_stat &= ~MDIO_STAT_ENC;
  136. }
  137. xgmac_write32(mdio_stat, &regs->mdio_stat, endian);
  138. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  139. if (ret)
  140. return ret;
  141. /* Set the port and dev addr */
  142. mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
  143. xgmac_write32(mdio_ctl, &regs->mdio_ctl, endian);
  144. /* Set the register address */
  145. if (regnum & MII_ADDR_C45) {
  146. xgmac_write32(regnum & 0xffff, &regs->mdio_addr, endian);
  147. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  148. if (ret)
  149. return ret;
  150. }
  151. /* Write the value to the register */
  152. xgmac_write32(MDIO_DATA(value), &regs->mdio_data, endian);
  153. ret = xgmac_wait_until_done(&bus->dev, regs, endian);
  154. if (ret)
  155. return ret;
  156. return 0;
  157. }
  158. /*
  159. * Reads from register regnum in the PHY for device dev, returning the value.
  160. * Clears miimcom first. All PHY configuration has to be done through the
  161. * TSEC1 MIIM regs.
  162. */
  163. static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
  164. {
  165. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  166. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  167. unsigned long flags;
  168. uint16_t dev_addr;
  169. uint32_t mdio_stat;
  170. uint32_t mdio_ctl;
  171. int ret;
  172. bool endian = priv->is_little_endian;
  173. mdio_stat = xgmac_read32(&regs->mdio_stat, endian);
  174. if (regnum & MII_ADDR_C45) {
  175. dev_addr = (regnum >> 16) & 0x1f;
  176. mdio_stat |= MDIO_STAT_ENC;
  177. } else {
  178. dev_addr = regnum & 0x1f;
  179. mdio_stat &= ~MDIO_STAT_ENC;
  180. }
  181. xgmac_write32(mdio_stat, &regs->mdio_stat, endian);
  182. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  183. if (ret)
  184. return ret;
  185. /* Set the Port and Device Addrs */
  186. mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
  187. xgmac_write32(mdio_ctl, &regs->mdio_ctl, endian);
  188. /* Set the register address */
  189. if (regnum & MII_ADDR_C45) {
  190. xgmac_write32(regnum & 0xffff, &regs->mdio_addr, endian);
  191. ret = xgmac_wait_until_free(&bus->dev, regs, endian);
  192. if (ret)
  193. return ret;
  194. }
  195. if (priv->has_a009885)
  196. /* Once the operation completes, i.e. MDIO_STAT_BSY clears, we
  197. * must read back the data register within 16 MDC cycles.
  198. */
  199. local_irq_save(flags);
  200. /* Initiate the read */
  201. xgmac_write32(mdio_ctl | MDIO_CTL_READ, &regs->mdio_ctl, endian);
  202. ret = xgmac_wait_until_done(&bus->dev, regs, endian);
  203. if (ret)
  204. goto irq_restore;
  205. /* Return all Fs if nothing was there */
  206. if ((xgmac_read32(&regs->mdio_stat, endian) & MDIO_STAT_RD_ER) &&
  207. !priv->has_a011043) {
  208. dev_dbg(&bus->dev,
  209. "Error while reading PHY%d reg at %d.%d\n",
  210. phy_id, dev_addr, regnum);
  211. ret = 0xffff;
  212. } else {
  213. ret = xgmac_read32(&regs->mdio_data, endian) & 0xffff;
  214. dev_dbg(&bus->dev, "read %04x\n", ret);
  215. }
  216. irq_restore:
  217. if (priv->has_a009885)
  218. local_irq_restore(flags);
  219. return ret;
  220. }
  221. static int xgmac_mdio_set_mdc_freq(struct mii_bus *bus)
  222. {
  223. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  224. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  225. struct device *dev = bus->parent;
  226. u32 mdio_stat, div;
  227. if (device_property_read_u32(dev, "clock-frequency", &priv->mdc_freq))
  228. return 0;
  229. priv->enet_clk = devm_clk_get(dev, NULL);
  230. if (IS_ERR(priv->enet_clk)) {
  231. dev_err(dev, "Input clock unknown, not changing MDC frequency");
  232. return PTR_ERR(priv->enet_clk);
  233. }
  234. div = ((clk_get_rate(priv->enet_clk) / priv->mdc_freq) - 1) / 2;
  235. if (div < 5 || div > 0x1ff) {
  236. dev_err(dev, "Requested MDC frequency is out of range, ignoring");
  237. return -EINVAL;
  238. }
  239. mdio_stat = xgmac_read32(&regs->mdio_stat, priv->is_little_endian);
  240. mdio_stat &= ~MDIO_STAT_CLKDIV(0x1ff);
  241. mdio_stat |= MDIO_STAT_CLKDIV(div);
  242. xgmac_write32(mdio_stat, &regs->mdio_stat, priv->is_little_endian);
  243. return 0;
  244. }
  245. static void xgmac_mdio_set_suppress_preamble(struct mii_bus *bus)
  246. {
  247. struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv;
  248. struct tgec_mdio_controller __iomem *regs = priv->mdio_base;
  249. struct device *dev = bus->parent;
  250. u32 mdio_stat;
  251. if (!device_property_read_bool(dev, "suppress-preamble"))
  252. return;
  253. mdio_stat = xgmac_read32(&regs->mdio_stat, priv->is_little_endian);
  254. mdio_stat |= MDIO_STAT_PRE_DIS;
  255. xgmac_write32(mdio_stat, &regs->mdio_stat, priv->is_little_endian);
  256. }
  257. static int xgmac_mdio_probe(struct platform_device *pdev)
  258. {
  259. struct fwnode_handle *fwnode;
  260. struct mdio_fsl_priv *priv;
  261. struct resource *res;
  262. struct mii_bus *bus;
  263. int ret;
  264. /* In DPAA-1, MDIO is one of the many FMan sub-devices. The FMan
  265. * defines a register space that spans a large area, covering all the
  266. * subdevice areas. Therefore, MDIO cannot claim exclusive access to
  267. * this register area.
  268. */
  269. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  270. if (!res) {
  271. dev_err(&pdev->dev, "could not obtain address\n");
  272. return -EINVAL;
  273. }
  274. bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(struct mdio_fsl_priv));
  275. if (!bus)
  276. return -ENOMEM;
  277. bus->name = "Freescale XGMAC MDIO Bus";
  278. bus->read = xgmac_mdio_read;
  279. bus->write = xgmac_mdio_write;
  280. bus->parent = &pdev->dev;
  281. bus->probe_capabilities = MDIOBUS_C22_C45;
  282. snprintf(bus->id, MII_BUS_ID_SIZE, "%pa", &res->start);
  283. priv = bus->priv;
  284. priv->mdio_base = devm_ioremap(&pdev->dev, res->start,
  285. resource_size(res));
  286. if (!priv->mdio_base)
  287. return -ENOMEM;
  288. /* For both ACPI and DT cases, endianness of MDIO controller
  289. * needs to be specified using "little-endian" property.
  290. */
  291. priv->is_little_endian = device_property_read_bool(&pdev->dev,
  292. "little-endian");
  293. priv->has_a009885 = device_property_read_bool(&pdev->dev,
  294. "fsl,erratum-a009885");
  295. priv->has_a011043 = device_property_read_bool(&pdev->dev,
  296. "fsl,erratum-a011043");
  297. xgmac_mdio_set_suppress_preamble(bus);
  298. ret = xgmac_mdio_set_mdc_freq(bus);
  299. if (ret)
  300. return ret;
  301. fwnode = dev_fwnode(&pdev->dev);
  302. if (is_of_node(fwnode))
  303. ret = of_mdiobus_register(bus, to_of_node(fwnode));
  304. else if (is_acpi_node(fwnode))
  305. ret = acpi_mdiobus_register(bus, fwnode);
  306. else
  307. ret = -EINVAL;
  308. if (ret) {
  309. dev_err(&pdev->dev, "cannot register MDIO bus\n");
  310. return ret;
  311. }
  312. platform_set_drvdata(pdev, bus);
  313. return 0;
  314. }
  315. static const struct of_device_id xgmac_mdio_match[] = {
  316. {
  317. .compatible = "fsl,fman-xmdio",
  318. },
  319. {
  320. .compatible = "fsl,fman-memac-mdio",
  321. },
  322. {},
  323. };
  324. MODULE_DEVICE_TABLE(of, xgmac_mdio_match);
  325. static const struct acpi_device_id xgmac_acpi_match[] = {
  326. { "NXP0006" },
  327. { }
  328. };
  329. MODULE_DEVICE_TABLE(acpi, xgmac_acpi_match);
  330. static struct platform_driver xgmac_mdio_driver = {
  331. .driver = {
  332. .name = "fsl-fman_xmdio",
  333. .of_match_table = xgmac_mdio_match,
  334. .acpi_match_table = xgmac_acpi_match,
  335. },
  336. .probe = xgmac_mdio_probe,
  337. };
  338. module_platform_driver(xgmac_mdio_driver);
  339. MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller");
  340. MODULE_LICENSE("GPL v2");