mvmdio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. int count;
  102. u32 val;
  103. int ret;
  104. mutex_lock(&dev->lock);
  105. ret = orion_mdio_wait_ready(bus);
  106. if (ret < 0) {
  107. mutex_unlock(&dev->lock);
  108. return ret;
  109. }
  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. /* Wait for the value to become available */
  115. count = 0;
  116. while (1) {
  117. val = readl(dev->regs);
  118. if (val & MVMDIO_SMI_READ_VALID)
  119. break;
  120. if (count > 100) {
  121. dev_err(bus->parent, "Timeout when reading PHY\n");
  122. mutex_unlock(&dev->lock);
  123. return -ETIMEDOUT;
  124. }
  125. udelay(10);
  126. count++;
  127. }
  128. mutex_unlock(&dev->lock);
  129. return val & 0xFFFF;
  130. }
  131. static int orion_mdio_write(struct mii_bus *bus, int mii_id,
  132. int regnum, u16 value)
  133. {
  134. struct orion_mdio_dev *dev = bus->priv;
  135. int ret;
  136. mutex_lock(&dev->lock);
  137. ret = orion_mdio_wait_ready(bus);
  138. if (ret < 0) {
  139. mutex_unlock(&dev->lock);
  140. return ret;
  141. }
  142. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  143. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  144. MVMDIO_SMI_WRITE_OPERATION |
  145. (value << MVMDIO_SMI_DATA_SHIFT)),
  146. dev->regs);
  147. mutex_unlock(&dev->lock);
  148. return 0;
  149. }
  150. static int orion_mdio_reset(struct mii_bus *bus)
  151. {
  152. return 0;
  153. }
  154. static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
  155. {
  156. struct orion_mdio_dev *dev = dev_id;
  157. if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
  158. MVMDIO_ERR_INT_SMI_DONE) {
  159. writel(~MVMDIO_ERR_INT_SMI_DONE,
  160. dev->regs + MVMDIO_ERR_INT_CAUSE);
  161. wake_up(&dev->smi_busy_wait);
  162. return IRQ_HANDLED;
  163. }
  164. return IRQ_NONE;
  165. }
  166. static int orion_mdio_probe(struct platform_device *pdev)
  167. {
  168. struct resource *r;
  169. struct mii_bus *bus;
  170. struct orion_mdio_dev *dev;
  171. int i, ret;
  172. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  173. if (!r) {
  174. dev_err(&pdev->dev, "No SMI register address given\n");
  175. return -ENODEV;
  176. }
  177. bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
  178. if (!bus) {
  179. dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
  180. return -ENOMEM;
  181. }
  182. bus->name = "orion_mdio_bus";
  183. bus->read = orion_mdio_read;
  184. bus->write = orion_mdio_write;
  185. bus->reset = orion_mdio_reset;
  186. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
  187. dev_name(&pdev->dev));
  188. bus->parent = &pdev->dev;
  189. bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  190. if (!bus->irq) {
  191. mdiobus_free(bus);
  192. return -ENOMEM;
  193. }
  194. for (i = 0; i < PHY_MAX_ADDR; i++)
  195. bus->irq[i] = PHY_POLL;
  196. dev = bus->priv;
  197. dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  198. if (!dev->regs) {
  199. dev_err(&pdev->dev, "Unable to remap SMI register\n");
  200. ret = -ENODEV;
  201. goto out_mdio;
  202. }
  203. init_waitqueue_head(&dev->smi_busy_wait);
  204. dev->clk = devm_clk_get(&pdev->dev, NULL);
  205. if (!IS_ERR(dev->clk))
  206. clk_prepare_enable(dev->clk);
  207. dev->err_interrupt = platform_get_irq(pdev, 0);
  208. if (dev->err_interrupt != -ENXIO) {
  209. ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
  210. orion_mdio_err_irq,
  211. IRQF_SHARED, pdev->name, dev);
  212. if (ret)
  213. goto out_mdio;
  214. writel(MVMDIO_ERR_INT_SMI_DONE,
  215. dev->regs + MVMDIO_ERR_INT_MASK);
  216. }
  217. mutex_init(&dev->lock);
  218. if (pdev->dev.of_node)
  219. ret = of_mdiobus_register(bus, pdev->dev.of_node);
  220. else
  221. ret = mdiobus_register(bus);
  222. if (ret < 0) {
  223. dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
  224. goto out_mdio;
  225. }
  226. platform_set_drvdata(pdev, bus);
  227. return 0;
  228. out_mdio:
  229. if (!IS_ERR(dev->clk))
  230. clk_disable_unprepare(dev->clk);
  231. kfree(bus->irq);
  232. mdiobus_free(bus);
  233. return ret;
  234. }
  235. static int orion_mdio_remove(struct platform_device *pdev)
  236. {
  237. struct mii_bus *bus = platform_get_drvdata(pdev);
  238. struct orion_mdio_dev *dev = bus->priv;
  239. writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
  240. mdiobus_unregister(bus);
  241. kfree(bus->irq);
  242. mdiobus_free(bus);
  243. if (!IS_ERR(dev->clk))
  244. clk_disable_unprepare(dev->clk);
  245. return 0;
  246. }
  247. static const struct of_device_id orion_mdio_match[] = {
  248. { .compatible = "marvell,orion-mdio" },
  249. { }
  250. };
  251. MODULE_DEVICE_TABLE(of, orion_mdio_match);
  252. static struct platform_driver orion_mdio_driver = {
  253. .probe = orion_mdio_probe,
  254. .remove = orion_mdio_remove,
  255. .driver = {
  256. .name = "orion-mdio",
  257. .of_match_table = orion_mdio_match,
  258. },
  259. };
  260. module_platform_driver(orion_mdio_driver);
  261. MODULE_DESCRIPTION("Marvell MDIO interface driver");
  262. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  263. MODULE_LICENSE("GPL");
  264. MODULE_ALIAS("platform:orion-mdio");