mvmdio.c 7.5 KB

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