mdio-bcm-unimac.c 9.2 KB

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