mvmdio.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Driver for the MDIO interface of Marvell network interfaces.
  3. *
  4. * Since the MDIO interface of Marvell network interfaces is shared
  5. * between all network interfaces, having a single driver allows to
  6. * handle concurrent accesses properly (you may have four Ethernet
  7. * ports, but they in fact share the same SMI interface to access
  8. * the MDIO bus). This driver is currently used by the mvneta and
  9. * mv643xx_eth drivers.
  10. *
  11. * Copyright (C) 2012 Marvell
  12. *
  13. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  14. *
  15. * This file is licensed under the terms of the GNU General Public
  16. * License version 2. This program is licensed "as is" without any
  17. * warranty of any kind, whether express or implied.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/phy.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/delay.h>
  27. #include <linux/io.h>
  28. #include <linux/clk.h>
  29. #include <linux/of_mdio.h>
  30. #include <linux/sched.h>
  31. #include <linux/wait.h>
  32. #define MVMDIO_SMI_DATA_SHIFT 0
  33. #define MVMDIO_SMI_PHY_ADDR_SHIFT 16
  34. #define MVMDIO_SMI_PHY_REG_SHIFT 21
  35. #define MVMDIO_SMI_READ_OPERATION BIT(26)
  36. #define MVMDIO_SMI_WRITE_OPERATION 0
  37. #define MVMDIO_SMI_READ_VALID BIT(27)
  38. #define MVMDIO_SMI_BUSY BIT(28)
  39. #define MVMDIO_ERR_INT_CAUSE 0x007C
  40. #define MVMDIO_ERR_INT_SMI_DONE 0x00000010
  41. #define MVMDIO_ERR_INT_MASK 0x0080
  42. /*
  43. * SMI Timeout measurements:
  44. * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt)
  45. * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled)
  46. */
  47. #define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */
  48. #define MVMDIO_SMI_POLL_INTERVAL_MIN 45
  49. #define MVMDIO_SMI_POLL_INTERVAL_MAX 55
  50. struct orion_mdio_dev {
  51. struct mutex lock;
  52. void __iomem *regs;
  53. struct clk *clk;
  54. /*
  55. * If we have access to the error interrupt pin (which is
  56. * somewhat misnamed as it not only reflects internal errors
  57. * but also reflects SMI completion), use that to wait for
  58. * SMI access completion instead of polling the SMI busy bit.
  59. */
  60. int err_interrupt;
  61. wait_queue_head_t smi_busy_wait;
  62. };
  63. static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
  64. {
  65. return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
  66. }
  67. /* Wait for the SMI unit to be ready for another operation
  68. */
  69. static int orion_mdio_wait_ready(struct mii_bus *bus)
  70. {
  71. struct orion_mdio_dev *dev = bus->priv;
  72. unsigned long timeout = usecs_to_jiffies(MVMDIO_SMI_TIMEOUT);
  73. unsigned long end = jiffies + timeout;
  74. int timedout = 0;
  75. while (1) {
  76. if (orion_mdio_smi_is_done(dev))
  77. return 0;
  78. else if (timedout)
  79. break;
  80. if (dev->err_interrupt <= 0) {
  81. usleep_range(MVMDIO_SMI_POLL_INTERVAL_MIN,
  82. MVMDIO_SMI_POLL_INTERVAL_MAX);
  83. if (time_is_before_jiffies(end))
  84. ++timedout;
  85. } else {
  86. wait_event_timeout(dev->smi_busy_wait,
  87. orion_mdio_smi_is_done(dev),
  88. timeout);
  89. ++timedout;
  90. }
  91. }
  92. dev_err(bus->parent, "Timeout: SMI busy for too long\n");
  93. return -ETIMEDOUT;
  94. }
  95. static int orion_mdio_read(struct mii_bus *bus, int mii_id,
  96. int regnum)
  97. {
  98. struct orion_mdio_dev *dev = bus->priv;
  99. u32 val;
  100. int ret;
  101. mutex_lock(&dev->lock);
  102. ret = orion_mdio_wait_ready(bus);
  103. if (ret < 0)
  104. goto out;
  105. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  106. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  107. MVMDIO_SMI_READ_OPERATION),
  108. dev->regs);
  109. ret = orion_mdio_wait_ready(bus);
  110. if (ret < 0)
  111. goto out;
  112. val = readl(dev->regs);
  113. if (!(val & MVMDIO_SMI_READ_VALID)) {
  114. dev_err(bus->parent, "SMI bus read not valid\n");
  115. ret = -ENODEV;
  116. goto out;
  117. }
  118. ret = val & 0xFFFF;
  119. out:
  120. mutex_unlock(&dev->lock);
  121. return ret;
  122. }
  123. static int orion_mdio_write(struct mii_bus *bus, int mii_id,
  124. int regnum, u16 value)
  125. {
  126. struct orion_mdio_dev *dev = bus->priv;
  127. int ret;
  128. mutex_lock(&dev->lock);
  129. ret = orion_mdio_wait_ready(bus);
  130. if (ret < 0)
  131. goto out;
  132. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  133. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  134. MVMDIO_SMI_WRITE_OPERATION |
  135. (value << MVMDIO_SMI_DATA_SHIFT)),
  136. dev->regs);
  137. out:
  138. mutex_unlock(&dev->lock);
  139. return ret;
  140. }
  141. static int orion_mdio_reset(struct mii_bus *bus)
  142. {
  143. return 0;
  144. }
  145. static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
  146. {
  147. struct orion_mdio_dev *dev = dev_id;
  148. if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
  149. MVMDIO_ERR_INT_SMI_DONE) {
  150. writel(~MVMDIO_ERR_INT_SMI_DONE,
  151. dev->regs + MVMDIO_ERR_INT_CAUSE);
  152. wake_up(&dev->smi_busy_wait);
  153. return IRQ_HANDLED;
  154. }
  155. return IRQ_NONE;
  156. }
  157. static int orion_mdio_probe(struct platform_device *pdev)
  158. {
  159. struct resource *r;
  160. struct mii_bus *bus;
  161. struct orion_mdio_dev *dev;
  162. int i, ret;
  163. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  164. if (!r) {
  165. dev_err(&pdev->dev, "No SMI register address given\n");
  166. return -ENODEV;
  167. }
  168. bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
  169. if (!bus) {
  170. dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
  171. return -ENOMEM;
  172. }
  173. bus->name = "orion_mdio_bus";
  174. bus->read = orion_mdio_read;
  175. bus->write = orion_mdio_write;
  176. bus->reset = orion_mdio_reset;
  177. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
  178. dev_name(&pdev->dev));
  179. bus->parent = &pdev->dev;
  180. bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  181. if (!bus->irq) {
  182. mdiobus_free(bus);
  183. return -ENOMEM;
  184. }
  185. for (i = 0; i < PHY_MAX_ADDR; i++)
  186. bus->irq[i] = PHY_POLL;
  187. dev = bus->priv;
  188. dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  189. if (!dev->regs) {
  190. dev_err(&pdev->dev, "Unable to remap SMI register\n");
  191. ret = -ENODEV;
  192. goto out_mdio;
  193. }
  194. init_waitqueue_head(&dev->smi_busy_wait);
  195. dev->clk = devm_clk_get(&pdev->dev, NULL);
  196. if (!IS_ERR(dev->clk))
  197. clk_prepare_enable(dev->clk);
  198. dev->err_interrupt = platform_get_irq(pdev, 0);
  199. if (dev->err_interrupt != -ENXIO) {
  200. ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
  201. orion_mdio_err_irq,
  202. IRQF_SHARED, pdev->name, dev);
  203. if (ret)
  204. goto out_mdio;
  205. writel(MVMDIO_ERR_INT_SMI_DONE,
  206. dev->regs + MVMDIO_ERR_INT_MASK);
  207. }
  208. mutex_init(&dev->lock);
  209. if (pdev->dev.of_node)
  210. ret = of_mdiobus_register(bus, pdev->dev.of_node);
  211. else
  212. ret = mdiobus_register(bus);
  213. if (ret < 0) {
  214. dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
  215. goto out_mdio;
  216. }
  217. platform_set_drvdata(pdev, bus);
  218. return 0;
  219. out_mdio:
  220. if (!IS_ERR(dev->clk))
  221. clk_disable_unprepare(dev->clk);
  222. kfree(bus->irq);
  223. mdiobus_free(bus);
  224. return ret;
  225. }
  226. static int orion_mdio_remove(struct platform_device *pdev)
  227. {
  228. struct mii_bus *bus = platform_get_drvdata(pdev);
  229. struct orion_mdio_dev *dev = bus->priv;
  230. writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
  231. mdiobus_unregister(bus);
  232. kfree(bus->irq);
  233. mdiobus_free(bus);
  234. if (!IS_ERR(dev->clk))
  235. clk_disable_unprepare(dev->clk);
  236. return 0;
  237. }
  238. static const struct of_device_id orion_mdio_match[] = {
  239. { .compatible = "marvell,orion-mdio" },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(of, orion_mdio_match);
  243. static struct platform_driver orion_mdio_driver = {
  244. .probe = orion_mdio_probe,
  245. .remove = orion_mdio_remove,
  246. .driver = {
  247. .name = "orion-mdio",
  248. .of_match_table = orion_mdio_match,
  249. },
  250. };
  251. module_platform_driver(orion_mdio_driver);
  252. MODULE_DESCRIPTION("Marvell MDIO interface driver");
  253. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  254. MODULE_LICENSE("GPL");
  255. MODULE_ALIAS("platform:orion-mdio");