mvmdio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
  147. {
  148. struct orion_mdio_dev *dev = dev_id;
  149. if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
  150. MVMDIO_ERR_INT_SMI_DONE) {
  151. writel(~MVMDIO_ERR_INT_SMI_DONE,
  152. dev->regs + MVMDIO_ERR_INT_CAUSE);
  153. wake_up(&dev->smi_busy_wait);
  154. return IRQ_HANDLED;
  155. }
  156. return IRQ_NONE;
  157. }
  158. static int orion_mdio_probe(struct platform_device *pdev)
  159. {
  160. struct resource *r;
  161. struct mii_bus *bus;
  162. struct orion_mdio_dev *dev;
  163. int i, ret;
  164. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  165. if (!r) {
  166. dev_err(&pdev->dev, "No SMI register address given\n");
  167. return -ENODEV;
  168. }
  169. bus = devm_mdiobus_alloc_size(&pdev->dev,
  170. sizeof(struct orion_mdio_dev));
  171. if (!bus)
  172. return -ENOMEM;
  173. bus->name = "orion_mdio_bus";
  174. bus->read = orion_mdio_read;
  175. bus->write = orion_mdio_write;
  176. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
  177. dev_name(&pdev->dev));
  178. bus->parent = &pdev->dev;
  179. bus->irq = devm_kmalloc_array(&pdev->dev, PHY_MAX_ADDR, sizeof(int),
  180. GFP_KERNEL);
  181. if (!bus->irq)
  182. return -ENOMEM;
  183. for (i = 0; i < PHY_MAX_ADDR; i++)
  184. bus->irq[i] = PHY_POLL;
  185. dev = bus->priv;
  186. dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  187. if (!dev->regs) {
  188. dev_err(&pdev->dev, "Unable to remap SMI register\n");
  189. ret = -ENODEV;
  190. goto out_mdio;
  191. }
  192. init_waitqueue_head(&dev->smi_busy_wait);
  193. dev->clk = devm_clk_get(&pdev->dev, NULL);
  194. if (!IS_ERR(dev->clk))
  195. clk_prepare_enable(dev->clk);
  196. dev->err_interrupt = platform_get_irq(pdev, 0);
  197. if (dev->err_interrupt > 0) {
  198. ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
  199. orion_mdio_err_irq,
  200. IRQF_SHARED, pdev->name, dev);
  201. if (ret)
  202. goto out_mdio;
  203. writel(MVMDIO_ERR_INT_SMI_DONE,
  204. dev->regs + MVMDIO_ERR_INT_MASK);
  205. } else if (dev->err_interrupt == -EPROBE_DEFER) {
  206. ret = -EPROBE_DEFER;
  207. goto out_mdio;
  208. }
  209. mutex_init(&dev->lock);
  210. if (pdev->dev.of_node)
  211. ret = of_mdiobus_register(bus, pdev->dev.of_node);
  212. else
  213. ret = mdiobus_register(bus);
  214. if (ret < 0) {
  215. dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
  216. goto out_mdio;
  217. }
  218. platform_set_drvdata(pdev, bus);
  219. return 0;
  220. out_mdio:
  221. if (!IS_ERR(dev->clk))
  222. clk_disable_unprepare(dev->clk);
  223. return ret;
  224. }
  225. static int orion_mdio_remove(struct platform_device *pdev)
  226. {
  227. struct mii_bus *bus = platform_get_drvdata(pdev);
  228. struct orion_mdio_dev *dev = bus->priv;
  229. writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
  230. mdiobus_unregister(bus);
  231. if (!IS_ERR(dev->clk))
  232. clk_disable_unprepare(dev->clk);
  233. return 0;
  234. }
  235. static const struct of_device_id orion_mdio_match[] = {
  236. { .compatible = "marvell,orion-mdio" },
  237. { }
  238. };
  239. MODULE_DEVICE_TABLE(of, orion_mdio_match);
  240. static struct platform_driver orion_mdio_driver = {
  241. .probe = orion_mdio_probe,
  242. .remove = orion_mdio_remove,
  243. .driver = {
  244. .name = "orion-mdio",
  245. .of_match_table = orion_mdio_match,
  246. },
  247. };
  248. module_platform_driver(orion_mdio_driver);
  249. MODULE_DESCRIPTION("Marvell MDIO interface driver");
  250. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  251. MODULE_LICENSE("GPL");
  252. MODULE_ALIAS("platform:orion-mdio");