mdio-bcm-unimac.c 7.5 KB

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