mdio-bcm-unimac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Broadcom UniMAC MDIO bus controller driver
  3. *
  4. * Copyright (C) 2014, Broadcom Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/phy.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/sched.h>
  15. #include <linux/module.h>
  16. #include <linux/io.h>
  17. #include <linux/delay.h>
  18. #include <linux/of.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_mdio.h>
  21. #define MDIO_CMD 0x00
  22. #define MDIO_START_BUSY (1 << 29)
  23. #define MDIO_READ_FAIL (1 << 28)
  24. #define MDIO_RD (2 << 26)
  25. #define MDIO_WR (1 << 26)
  26. #define MDIO_PMD_SHIFT 21
  27. #define MDIO_PMD_MASK 0x1F
  28. #define MDIO_REG_SHIFT 16
  29. #define MDIO_REG_MASK 0x1F
  30. #define MDIO_CFG 0x04
  31. #define MDIO_C22 (1 << 0)
  32. #define MDIO_C45 0
  33. #define MDIO_CLK_DIV_SHIFT 4
  34. #define MDIO_CLK_DIV_MASK 0x3F
  35. #define MDIO_SUPP_PREAMBLE (1 << 12)
  36. struct unimac_mdio_priv {
  37. struct mii_bus *mii_bus;
  38. void __iomem *base;
  39. };
  40. static inline void unimac_mdio_start(struct unimac_mdio_priv *priv)
  41. {
  42. u32 reg;
  43. reg = __raw_readl(priv->base + MDIO_CMD);
  44. reg |= MDIO_START_BUSY;
  45. __raw_writel(reg, priv->base + MDIO_CMD);
  46. }
  47. static inline unsigned int unimac_mdio_busy(struct unimac_mdio_priv *priv)
  48. {
  49. return __raw_readl(priv->base + MDIO_CMD) & MDIO_START_BUSY;
  50. }
  51. static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  52. {
  53. struct unimac_mdio_priv *priv = bus->priv;
  54. unsigned int timeout = 1000;
  55. u32 cmd;
  56. /* Prepare the read operation */
  57. cmd = MDIO_RD | (phy_id << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);
  58. __raw_writel(cmd, priv->base + MDIO_CMD);
  59. /* Start MDIO transaction */
  60. unimac_mdio_start(priv);
  61. do {
  62. if (!unimac_mdio_busy(priv))
  63. break;
  64. usleep_range(1000, 2000);
  65. } while (timeout--);
  66. if (!timeout)
  67. return -ETIMEDOUT;
  68. cmd = __raw_readl(priv->base + MDIO_CMD);
  69. /* Some broken devices are known not to release the line during
  70. * turn-around, e.g: Broadcom BCM53125 external switches, so check for
  71. * that condition here and ignore the MDIO controller read failure
  72. * indication.
  73. */
  74. if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (cmd & MDIO_READ_FAIL))
  75. return -EIO;
  76. return cmd & 0xffff;
  77. }
  78. static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
  79. int reg, u16 val)
  80. {
  81. struct unimac_mdio_priv *priv = bus->priv;
  82. unsigned int timeout = 1000;
  83. u32 cmd;
  84. /* Prepare the write operation */
  85. cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
  86. (reg << MDIO_REG_SHIFT) | (0xffff & val);
  87. __raw_writel(cmd, priv->base + MDIO_CMD);
  88. unimac_mdio_start(priv);
  89. do {
  90. if (!unimac_mdio_busy(priv))
  91. break;
  92. usleep_range(1000, 2000);
  93. } while (timeout--);
  94. if (!timeout)
  95. return -ETIMEDOUT;
  96. return 0;
  97. }
  98. /* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with
  99. * their internal MDIO management controller making them fail to successfully
  100. * be read from or written to for the first transaction. We insert a dummy
  101. * BMSR read here to make sure that phy_get_device() and get_phy_id() can
  102. * correctly read the PHY MII_PHYSID1/2 registers and successfully register a
  103. * PHY device for this peripheral.
  104. *
  105. * Once the PHY driver is registered, we can workaround subsequent reads from
  106. * there (e.g: during system-wide power management).
  107. *
  108. * bus->reset is invoked before mdiobus_scan during mdiobus_register and is
  109. * therefore the right location to stick that workaround. Since we do not want
  110. * to read from non-existing PHYs, we either use bus->phy_mask or do a manual
  111. * Device Tree scan to limit the search area.
  112. */
  113. static int unimac_mdio_reset(struct mii_bus *bus)
  114. {
  115. struct device_node *np = bus->dev.of_node;
  116. struct device_node *child;
  117. u32 read_mask = 0;
  118. int addr;
  119. if (!np) {
  120. read_mask = ~bus->phy_mask;
  121. } else {
  122. for_each_available_child_of_node(np, child) {
  123. addr = of_mdio_parse_addr(&bus->dev, child);
  124. if (addr < 0)
  125. continue;
  126. read_mask |= 1 << addr;
  127. }
  128. }
  129. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  130. if (read_mask & 1 << addr)
  131. mdiobus_read(bus, addr, MII_BMSR);
  132. }
  133. return 0;
  134. }
  135. static int unimac_mdio_probe(struct platform_device *pdev)
  136. {
  137. struct unimac_mdio_priv *priv;
  138. struct device_node *np;
  139. struct mii_bus *bus;
  140. struct resource *r;
  141. int ret;
  142. np = pdev->dev.of_node;
  143. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  144. if (!priv)
  145. return -ENOMEM;
  146. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. /* Just ioremap, as this MDIO block is usually integrated into an
  148. * Ethernet MAC controller register range
  149. */
  150. priv->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  151. if (!priv->base) {
  152. dev_err(&pdev->dev, "failed to remap register\n");
  153. return -ENOMEM;
  154. }
  155. priv->mii_bus = mdiobus_alloc();
  156. if (!priv->mii_bus)
  157. return -ENOMEM;
  158. bus = priv->mii_bus;
  159. bus->priv = priv;
  160. bus->name = "unimac MII bus";
  161. bus->parent = &pdev->dev;
  162. bus->read = unimac_mdio_read;
  163. bus->write = unimac_mdio_write;
  164. bus->reset = unimac_mdio_reset;
  165. snprintf(bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
  166. bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  167. if (!bus->irq) {
  168. ret = -ENOMEM;
  169. goto out_mdio_free;
  170. }
  171. ret = of_mdiobus_register(bus, np);
  172. if (ret) {
  173. dev_err(&pdev->dev, "MDIO bus registration failed\n");
  174. goto out_mdio_irq;
  175. }
  176. platform_set_drvdata(pdev, priv);
  177. dev_info(&pdev->dev, "Broadcom UniMAC MDIO bus at 0x%p\n", priv->base);
  178. return 0;
  179. out_mdio_irq:
  180. kfree(bus->irq);
  181. out_mdio_free:
  182. mdiobus_free(bus);
  183. return ret;
  184. }
  185. static int unimac_mdio_remove(struct platform_device *pdev)
  186. {
  187. struct unimac_mdio_priv *priv = platform_get_drvdata(pdev);
  188. mdiobus_unregister(priv->mii_bus);
  189. kfree(priv->mii_bus->irq);
  190. mdiobus_free(priv->mii_bus);
  191. return 0;
  192. }
  193. static const struct of_device_id unimac_mdio_ids[] = {
  194. { .compatible = "brcm,genet-mdio-v4", },
  195. { .compatible = "brcm,genet-mdio-v3", },
  196. { .compatible = "brcm,genet-mdio-v2", },
  197. { .compatible = "brcm,genet-mdio-v1", },
  198. { .compatible = "brcm,unimac-mdio", },
  199. { /* sentinel */ },
  200. };
  201. MODULE_DEVICE_TABLE(of, unimac_mdio_ids);
  202. static struct platform_driver unimac_mdio_driver = {
  203. .driver = {
  204. .name = "unimac-mdio",
  205. .of_match_table = unimac_mdio_ids,
  206. },
  207. .probe = unimac_mdio_probe,
  208. .remove = unimac_mdio_remove,
  209. };
  210. module_platform_driver(unimac_mdio_driver);
  211. MODULE_AUTHOR("Broadcom Corporation");
  212. MODULE_DESCRIPTION("Broadcom UniMAC MDIO bus controller");
  213. MODULE_LICENSE("GPL");
  214. MODULE_ALIAS("platform:unimac-mdio");