mdio-bcm-unimac.c 8.9 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/kernel.h>
  8. #include <linux/phy.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/sched.h>
  11. #include <linux/module.h>
  12. #include <linux/io.h>
  13. #include <linux/delay.h>
  14. #include <linux/clk.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of_mdio.h>
  18. #include <linux/platform_data/mdio-bcm-unimac.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 inline unsigned int unimac_mdio_busy(struct unimac_mdio_priv *priv)
  68. {
  69. return unimac_mdio_readl(priv, MDIO_CMD) & MDIO_START_BUSY;
  70. }
  71. static int unimac_mdio_poll(void *wait_func_data)
  72. {
  73. struct unimac_mdio_priv *priv = wait_func_data;
  74. unsigned int timeout = 1000;
  75. do {
  76. if (!unimac_mdio_busy(priv))
  77. return 0;
  78. usleep_range(1000, 2000);
  79. } while (--timeout);
  80. return -ETIMEDOUT;
  81. }
  82. static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  83. {
  84. struct unimac_mdio_priv *priv = bus->priv;
  85. int ret;
  86. u32 cmd;
  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. return ret;
  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. return -EIO;
  103. return cmd & 0xffff;
  104. }
  105. static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
  106. int reg, u16 val)
  107. {
  108. struct unimac_mdio_priv *priv = bus->priv;
  109. u32 cmd;
  110. /* Prepare the write operation */
  111. cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
  112. (reg << MDIO_REG_SHIFT) | (0xffff & val);
  113. unimac_mdio_writel(priv, cmd, MDIO_CMD);
  114. unimac_mdio_start(priv);
  115. return priv->wait_func(priv->wait_func_data);
  116. }
  117. /* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with
  118. * their internal MDIO management controller making them fail to successfully
  119. * be read from or written to for the first transaction. We insert a dummy
  120. * BMSR read here to make sure that phy_get_device() and get_phy_id() can
  121. * correctly read the PHY MII_PHYSID1/2 registers and successfully register a
  122. * PHY device for this peripheral.
  123. *
  124. * Once the PHY driver is registered, we can workaround subsequent reads from
  125. * there (e.g: during system-wide power management).
  126. *
  127. * bus->reset is invoked before mdiobus_scan during mdiobus_register and is
  128. * therefore the right location to stick that workaround. Since we do not want
  129. * to read from non-existing PHYs, we either use bus->phy_mask or do a manual
  130. * Device Tree scan to limit the search area.
  131. */
  132. static int unimac_mdio_reset(struct mii_bus *bus)
  133. {
  134. struct device_node *np = bus->dev.of_node;
  135. struct device_node *child;
  136. u32 read_mask = 0;
  137. int addr;
  138. if (!np) {
  139. read_mask = ~bus->phy_mask;
  140. } else {
  141. for_each_available_child_of_node(np, child) {
  142. addr = of_mdio_parse_addr(&bus->dev, child);
  143. if (addr < 0)
  144. continue;
  145. read_mask |= 1 << addr;
  146. }
  147. }
  148. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  149. if (read_mask & 1 << addr) {
  150. dev_dbg(&bus->dev, "Workaround for PHY @ %d\n", addr);
  151. mdiobus_read(bus, addr, MII_BMSR);
  152. }
  153. }
  154. return 0;
  155. }
  156. static void unimac_mdio_clk_set(struct unimac_mdio_priv *priv)
  157. {
  158. unsigned long rate;
  159. u32 reg, div;
  160. /* Keep the hardware default values */
  161. if (!priv->clk_freq)
  162. return;
  163. if (!priv->clk)
  164. rate = 250000000;
  165. else
  166. rate = clk_get_rate(priv->clk);
  167. div = (rate / (2 * priv->clk_freq)) - 1;
  168. if (div & ~MDIO_CLK_DIV_MASK) {
  169. pr_warn("Incorrect MDIO clock frequency, ignoring\n");
  170. return;
  171. }
  172. /* The MDIO clock is the reference clock (typicaly 250Mhz) divided by
  173. * 2 x (MDIO_CLK_DIV + 1)
  174. */
  175. reg = unimac_mdio_readl(priv, MDIO_CFG);
  176. reg &= ~(MDIO_CLK_DIV_MASK << MDIO_CLK_DIV_SHIFT);
  177. reg |= div << MDIO_CLK_DIV_SHIFT;
  178. unimac_mdio_writel(priv, reg, MDIO_CFG);
  179. }
  180. static int unimac_mdio_probe(struct platform_device *pdev)
  181. {
  182. struct unimac_mdio_pdata *pdata = pdev->dev.platform_data;
  183. struct unimac_mdio_priv *priv;
  184. struct device_node *np;
  185. struct mii_bus *bus;
  186. struct resource *r;
  187. int ret;
  188. np = pdev->dev.of_node;
  189. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  190. if (!priv)
  191. return -ENOMEM;
  192. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  193. if (!r)
  194. return -EINVAL;
  195. /* Just ioremap, as this MDIO block is usually integrated into an
  196. * Ethernet MAC controller register range
  197. */
  198. priv->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  199. if (!priv->base) {
  200. dev_err(&pdev->dev, "failed to remap register\n");
  201. return -ENOMEM;
  202. }
  203. priv->clk = devm_clk_get_optional(&pdev->dev, NULL);
  204. if (IS_ERR(priv->clk))
  205. return PTR_ERR(priv->clk);
  206. ret = clk_prepare_enable(priv->clk);
  207. if (ret)
  208. return ret;
  209. if (of_property_read_u32(np, "clock-frequency", &priv->clk_freq))
  210. priv->clk_freq = 0;
  211. unimac_mdio_clk_set(priv);
  212. priv->mii_bus = mdiobus_alloc();
  213. if (!priv->mii_bus) {
  214. ret = -ENOMEM;
  215. goto out_clk_disable;
  216. }
  217. bus = priv->mii_bus;
  218. bus->priv = priv;
  219. if (pdata) {
  220. bus->name = pdata->bus_name;
  221. priv->wait_func = pdata->wait_func;
  222. priv->wait_func_data = pdata->wait_func_data;
  223. bus->phy_mask = ~pdata->phy_mask;
  224. } else {
  225. bus->name = "unimac MII bus";
  226. priv->wait_func_data = priv;
  227. priv->wait_func = unimac_mdio_poll;
  228. }
  229. bus->parent = &pdev->dev;
  230. bus->read = unimac_mdio_read;
  231. bus->write = unimac_mdio_write;
  232. bus->reset = unimac_mdio_reset;
  233. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
  234. ret = of_mdiobus_register(bus, np);
  235. if (ret) {
  236. dev_err(&pdev->dev, "MDIO bus registration failed\n");
  237. goto out_mdio_free;
  238. }
  239. platform_set_drvdata(pdev, priv);
  240. dev_info(&pdev->dev, "Broadcom UniMAC MDIO bus\n");
  241. return 0;
  242. out_mdio_free:
  243. mdiobus_free(bus);
  244. out_clk_disable:
  245. clk_disable_unprepare(priv->clk);
  246. return ret;
  247. }
  248. static int unimac_mdio_remove(struct platform_device *pdev)
  249. {
  250. struct unimac_mdio_priv *priv = platform_get_drvdata(pdev);
  251. mdiobus_unregister(priv->mii_bus);
  252. mdiobus_free(priv->mii_bus);
  253. clk_disable_unprepare(priv->clk);
  254. return 0;
  255. }
  256. static int __maybe_unused unimac_mdio_suspend(struct device *d)
  257. {
  258. struct unimac_mdio_priv *priv = dev_get_drvdata(d);
  259. clk_disable_unprepare(priv->clk);
  260. return 0;
  261. }
  262. static int __maybe_unused unimac_mdio_resume(struct device *d)
  263. {
  264. struct unimac_mdio_priv *priv = dev_get_drvdata(d);
  265. int ret;
  266. ret = clk_prepare_enable(priv->clk);
  267. if (ret)
  268. return ret;
  269. unimac_mdio_clk_set(priv);
  270. return 0;
  271. }
  272. static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,
  273. unimac_mdio_suspend, unimac_mdio_resume);
  274. static const struct of_device_id unimac_mdio_ids[] = {
  275. { .compatible = "brcm,genet-mdio-v5", },
  276. { .compatible = "brcm,genet-mdio-v4", },
  277. { .compatible = "brcm,genet-mdio-v3", },
  278. { .compatible = "brcm,genet-mdio-v2", },
  279. { .compatible = "brcm,genet-mdio-v1", },
  280. { .compatible = "brcm,unimac-mdio", },
  281. { /* sentinel */ },
  282. };
  283. MODULE_DEVICE_TABLE(of, unimac_mdio_ids);
  284. static struct platform_driver unimac_mdio_driver = {
  285. .driver = {
  286. .name = UNIMAC_MDIO_DRV_NAME,
  287. .of_match_table = unimac_mdio_ids,
  288. .pm = &unimac_mdio_pm_ops,
  289. },
  290. .probe = unimac_mdio_probe,
  291. .remove = unimac_mdio_remove,
  292. };
  293. module_platform_driver(unimac_mdio_driver);
  294. MODULE_AUTHOR("Broadcom Corporation");
  295. MODULE_DESCRIPTION("Broadcom UniMAC MDIO bus controller");
  296. MODULE_LICENSE("GPL");
  297. MODULE_ALIAS("platform:" UNIMAC_MDIO_DRV_NAME);