mdio-bcm-unimac.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Broadcom UniMAC MDIO bus controller driver
  4. *
  5. * Copyright (C) 2014-2017 Broadcom
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_mdio.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/phy.h>
  16. #include <linux/platform_data/mdio-bcm-unimac.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/sched.h>
  19. #define MDIO_CMD 0x00
  20. #define MDIO_START_BUSY (1 << 29)
  21. #define MDIO_READ_FAIL (1 << 28)
  22. #define MDIO_RD (2 << 26)
  23. #define MDIO_WR (1 << 26)
  24. #define MDIO_PMD_SHIFT 21
  25. #define MDIO_PMD_MASK 0x1F
  26. #define MDIO_REG_SHIFT 16
  27. #define MDIO_REG_MASK 0x1F
  28. #define MDIO_CFG 0x04
  29. #define MDIO_C22 (1 << 0)
  30. #define MDIO_C45 0
  31. #define MDIO_CLK_DIV_SHIFT 4
  32. #define MDIO_CLK_DIV_MASK 0x3F
  33. #define MDIO_SUPP_PREAMBLE (1 << 12)
  34. struct unimac_mdio_priv {
  35. struct mii_bus *mii_bus;
  36. void __iomem *base;
  37. int (*wait_func) (void *wait_func_data);
  38. void *wait_func_data;
  39. struct clk *clk;
  40. u32 clk_freq;
  41. };
  42. static inline u32 unimac_mdio_readl(struct unimac_mdio_priv *priv, u32 offset)
  43. {
  44. /* MIPS chips strapped for BE will automagically configure the
  45. * peripheral registers for CPU-native byte order.
  46. */
  47. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  48. return __raw_readl(priv->base + offset);
  49. else
  50. return readl_relaxed(priv->base + offset);
  51. }
  52. static inline void unimac_mdio_writel(struct unimac_mdio_priv *priv, u32 val,
  53. u32 offset)
  54. {
  55. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  56. __raw_writel(val, priv->base + offset);
  57. else
  58. writel_relaxed(val, priv->base + offset);
  59. }
  60. static inline void unimac_mdio_start(struct unimac_mdio_priv *priv)
  61. {
  62. u32 reg;
  63. reg = unimac_mdio_readl(priv, MDIO_CMD);
  64. reg |= MDIO_START_BUSY;
  65. unimac_mdio_writel(priv, reg, MDIO_CMD);
  66. }
  67. static int unimac_mdio_poll(void *wait_func_data)
  68. {
  69. struct unimac_mdio_priv *priv = wait_func_data;
  70. u32 val;
  71. /*
  72. * C22 transactions should take ~25 usec, will need to adjust
  73. * if C45 support is added.
  74. */
  75. udelay(30);
  76. return read_poll_timeout(unimac_mdio_readl, val, !(val & MDIO_START_BUSY),
  77. 2000, 100000, false, priv, MDIO_CMD);
  78. }
  79. static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  80. {
  81. struct unimac_mdio_priv *priv = bus->priv;
  82. int ret;
  83. u32 cmd;
  84. ret = clk_prepare_enable(priv->clk);
  85. if (ret)
  86. return ret;
  87. /* Prepare the read operation */
  88. cmd = MDIO_RD | (phy_id << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);
  89. unimac_mdio_writel(priv, cmd, MDIO_CMD);
  90. /* Start MDIO transaction */
  91. unimac_mdio_start(priv);
  92. ret = priv->wait_func(priv->wait_func_data);
  93. if (ret)
  94. goto out;
  95. cmd = unimac_mdio_readl(priv, MDIO_CMD);
  96. /* Some broken devices are known not to release the line during
  97. * turn-around, e.g: Broadcom BCM53125 external switches, so check for
  98. * that condition here and ignore the MDIO controller read failure
  99. * indication.
  100. */
  101. if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (cmd & MDIO_READ_FAIL)) {
  102. ret = -EIO;
  103. goto out;
  104. }
  105. ret = cmd & 0xffff;
  106. out:
  107. clk_disable_unprepare(priv->clk);
  108. return ret;
  109. }
  110. static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
  111. int reg, u16 val)
  112. {
  113. struct unimac_mdio_priv *priv = bus->priv;
  114. u32 cmd;
  115. int ret;
  116. ret = clk_prepare_enable(priv->clk);
  117. if (ret)
  118. return ret;
  119. /* Prepare the write operation */
  120. cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
  121. (reg << MDIO_REG_SHIFT) | (0xffff & val);
  122. unimac_mdio_writel(priv, cmd, MDIO_CMD);
  123. unimac_mdio_start(priv);
  124. ret = priv->wait_func(priv->wait_func_data);
  125. clk_disable_unprepare(priv->clk);
  126. return ret;
  127. }
  128. /* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with
  129. * their internal MDIO management controller making them fail to successfully
  130. * be read from or written to for the first transaction. We insert a dummy
  131. * BMSR read here to make sure that phy_get_device() and get_phy_id() can
  132. * correctly read the PHY MII_PHYSID1/2 registers and successfully register a
  133. * PHY device for this peripheral.
  134. *
  135. * Once the PHY driver is registered, we can workaround subsequent reads from
  136. * there (e.g: during system-wide power management).
  137. *
  138. * bus->reset is invoked before mdiobus_scan during mdiobus_register and is
  139. * therefore the right location to stick that workaround. Since we do not want
  140. * to read from non-existing PHYs, we either use bus->phy_mask or do a manual
  141. * Device Tree scan to limit the search area.
  142. */
  143. static int unimac_mdio_reset(struct mii_bus *bus)
  144. {
  145. struct device_node *np = bus->dev.of_node;
  146. struct device_node *child;
  147. u32 read_mask = 0;
  148. int addr;
  149. if (!np) {
  150. read_mask = ~bus->phy_mask;
  151. } else {
  152. for_each_available_child_of_node(np, child) {
  153. addr = of_mdio_parse_addr(&bus->dev, child);
  154. if (addr < 0)
  155. continue;
  156. read_mask |= 1 << addr;
  157. }
  158. }
  159. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  160. if (read_mask & 1 << addr) {
  161. dev_dbg(&bus->dev, "Workaround for PHY @ %d\n", addr);
  162. mdiobus_read(bus, addr, MII_BMSR);
  163. }
  164. }
  165. return 0;
  166. }
  167. static int unimac_mdio_clk_set(struct unimac_mdio_priv *priv)
  168. {
  169. unsigned long rate;
  170. u32 reg, div;
  171. int ret;
  172. /* Keep the hardware default values */
  173. if (!priv->clk_freq)
  174. return 0;
  175. ret = clk_prepare_enable(priv->clk);
  176. if (ret)
  177. return ret;
  178. rate = clk_get_rate(priv->clk);
  179. if (!rate)
  180. rate = 250000000;
  181. div = (rate / (2 * priv->clk_freq)) - 1;
  182. if (div & ~MDIO_CLK_DIV_MASK) {
  183. pr_warn("Incorrect MDIO clock frequency, ignoring\n");
  184. ret = 0;
  185. goto out;
  186. }
  187. /* The MDIO clock is the reference clock (typically 250Mhz) divided by
  188. * 2 x (MDIO_CLK_DIV + 1)
  189. */
  190. reg = unimac_mdio_readl(priv, MDIO_CFG);
  191. reg &= ~(MDIO_CLK_DIV_MASK << MDIO_CLK_DIV_SHIFT);
  192. reg |= div << MDIO_CLK_DIV_SHIFT;
  193. unimac_mdio_writel(priv, reg, MDIO_CFG);
  194. out:
  195. clk_disable_unprepare(priv->clk);
  196. return ret;
  197. }
  198. static int unimac_mdio_probe(struct platform_device *pdev)
  199. {
  200. struct unimac_mdio_pdata *pdata = pdev->dev.platform_data;
  201. struct unimac_mdio_priv *priv;
  202. struct device_node *np;
  203. struct mii_bus *bus;
  204. struct resource *r;
  205. int ret;
  206. np = pdev->dev.of_node;
  207. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  208. if (!priv)
  209. return -ENOMEM;
  210. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  211. if (!r)
  212. return -EINVAL;
  213. /* Just ioremap, as this MDIO block is usually integrated into an
  214. * Ethernet MAC controller register range
  215. */
  216. priv->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  217. if (!priv->base) {
  218. dev_err(&pdev->dev, "failed to remap register\n");
  219. return -ENOMEM;
  220. }
  221. if (of_property_read_u32(np, "clock-frequency", &priv->clk_freq))
  222. priv->clk_freq = 0;
  223. priv->mii_bus = mdiobus_alloc();
  224. if (!priv->mii_bus)
  225. return -ENOMEM;
  226. bus = priv->mii_bus;
  227. bus->priv = priv;
  228. if (pdata) {
  229. bus->name = pdata->bus_name;
  230. priv->wait_func = pdata->wait_func;
  231. priv->wait_func_data = pdata->wait_func_data;
  232. bus->phy_mask = ~pdata->phy_mask;
  233. priv->clk = pdata->clk;
  234. } else {
  235. bus->name = "unimac MII bus";
  236. priv->wait_func_data = priv;
  237. priv->wait_func = unimac_mdio_poll;
  238. priv->clk = devm_clk_get_optional(&pdev->dev, NULL);
  239. }
  240. if (IS_ERR(priv->clk)) {
  241. ret = PTR_ERR(priv->clk);
  242. goto out_mdio_free;
  243. }
  244. bus->parent = &pdev->dev;
  245. bus->read = unimac_mdio_read;
  246. bus->write = unimac_mdio_write;
  247. bus->reset = unimac_mdio_reset;
  248. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
  249. ret = unimac_mdio_clk_set(priv);
  250. if (ret)
  251. goto out_mdio_free;
  252. ret = of_mdiobus_register(bus, np);
  253. if (ret) {
  254. dev_err(&pdev->dev, "MDIO bus registration failed\n");
  255. goto out_mdio_free;
  256. }
  257. platform_set_drvdata(pdev, priv);
  258. dev_info(&pdev->dev, "Broadcom UniMAC MDIO bus\n");
  259. return 0;
  260. out_mdio_free:
  261. mdiobus_free(bus);
  262. return ret;
  263. }
  264. static void unimac_mdio_remove(struct platform_device *pdev)
  265. {
  266. struct unimac_mdio_priv *priv = platform_get_drvdata(pdev);
  267. mdiobus_unregister(priv->mii_bus);
  268. mdiobus_free(priv->mii_bus);
  269. }
  270. static int __maybe_unused unimac_mdio_resume(struct device *d)
  271. {
  272. struct unimac_mdio_priv *priv = dev_get_drvdata(d);
  273. return unimac_mdio_clk_set(priv);
  274. }
  275. static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,
  276. NULL, unimac_mdio_resume);
  277. static const struct of_device_id unimac_mdio_ids[] = {
  278. { .compatible = "brcm,asp-v2.2-mdio", },
  279. { .compatible = "brcm,asp-v2.1-mdio", },
  280. { .compatible = "brcm,asp-v2.0-mdio", },
  281. { .compatible = "brcm,bcm6846-mdio", },
  282. { .compatible = "brcm,genet-mdio-v5", },
  283. { .compatible = "brcm,genet-mdio-v4", },
  284. { .compatible = "brcm,genet-mdio-v3", },
  285. { .compatible = "brcm,genet-mdio-v2", },
  286. { .compatible = "brcm,genet-mdio-v1", },
  287. { .compatible = "brcm,unimac-mdio", },
  288. { /* sentinel */ },
  289. };
  290. MODULE_DEVICE_TABLE(of, unimac_mdio_ids);
  291. static struct platform_driver unimac_mdio_driver = {
  292. .driver = {
  293. .name = UNIMAC_MDIO_DRV_NAME,
  294. .of_match_table = unimac_mdio_ids,
  295. .pm = &unimac_mdio_pm_ops,
  296. },
  297. .probe = unimac_mdio_probe,
  298. .remove_new = unimac_mdio_remove,
  299. };
  300. module_platform_driver(unimac_mdio_driver);
  301. MODULE_AUTHOR("Broadcom Corporation");
  302. MODULE_DESCRIPTION("Broadcom UniMAC MDIO bus controller");
  303. MODULE_LICENSE("GPL");
  304. MODULE_ALIAS("platform:" UNIMAC_MDIO_DRV_NAME);