mvmdio.c 7.5 KB

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