mvmdio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/of_mdio.h>
  31. #include <linux/sched.h>
  32. #include <linux/wait.h>
  33. #define MVMDIO_SMI_DATA_SHIFT 0
  34. #define MVMDIO_SMI_PHY_ADDR_SHIFT 16
  35. #define MVMDIO_SMI_PHY_REG_SHIFT 21
  36. #define MVMDIO_SMI_READ_OPERATION BIT(26)
  37. #define MVMDIO_SMI_WRITE_OPERATION 0
  38. #define MVMDIO_SMI_READ_VALID BIT(27)
  39. #define MVMDIO_SMI_BUSY BIT(28)
  40. #define MVMDIO_ERR_INT_CAUSE 0x007C
  41. #define MVMDIO_ERR_INT_SMI_DONE 0x00000010
  42. #define MVMDIO_ERR_INT_MASK 0x0080
  43. struct orion_mdio_dev {
  44. struct mutex lock;
  45. void __iomem *regs;
  46. /*
  47. * If we have access to the error interrupt pin (which is
  48. * somewhat misnamed as it not only reflects internal errors
  49. * but also reflects SMI completion), use that to wait for
  50. * SMI access completion instead of polling the SMI busy bit.
  51. */
  52. int err_interrupt;
  53. wait_queue_head_t smi_busy_wait;
  54. };
  55. static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
  56. {
  57. return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
  58. }
  59. /* Wait for the SMI unit to be ready for another operation
  60. */
  61. static int orion_mdio_wait_ready(struct mii_bus *bus)
  62. {
  63. struct orion_mdio_dev *dev = bus->priv;
  64. int count;
  65. if (dev->err_interrupt <= 0) {
  66. count = 0;
  67. while (1) {
  68. if (orion_mdio_smi_is_done(dev))
  69. break;
  70. if (count > 100) {
  71. dev_err(bus->parent,
  72. "Timeout: SMI busy for too long\n");
  73. return -ETIMEDOUT;
  74. }
  75. udelay(10);
  76. count++;
  77. }
  78. } else {
  79. if (!orion_mdio_smi_is_done(dev)) {
  80. wait_event_timeout(dev->smi_busy_wait,
  81. orion_mdio_smi_is_done(dev),
  82. msecs_to_jiffies(100));
  83. if (!orion_mdio_smi_is_done(dev))
  84. return -ETIMEDOUT;
  85. }
  86. }
  87. return 0;
  88. }
  89. static int orion_mdio_read(struct mii_bus *bus, int mii_id,
  90. int regnum)
  91. {
  92. struct orion_mdio_dev *dev = bus->priv;
  93. int count;
  94. u32 val;
  95. int ret;
  96. mutex_lock(&dev->lock);
  97. ret = orion_mdio_wait_ready(bus);
  98. if (ret < 0) {
  99. mutex_unlock(&dev->lock);
  100. return ret;
  101. }
  102. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  103. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  104. MVMDIO_SMI_READ_OPERATION),
  105. dev->regs);
  106. /* Wait for the value to become available */
  107. count = 0;
  108. while (1) {
  109. val = readl(dev->regs);
  110. if (val & MVMDIO_SMI_READ_VALID)
  111. break;
  112. if (count > 100) {
  113. dev_err(bus->parent, "Timeout when reading PHY\n");
  114. mutex_unlock(&dev->lock);
  115. return -ETIMEDOUT;
  116. }
  117. udelay(10);
  118. count++;
  119. }
  120. mutex_unlock(&dev->lock);
  121. return val & 0xFFFF;
  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. mutex_unlock(&dev->lock);
  132. return ret;
  133. }
  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. mutex_unlock(&dev->lock);
  140. return 0;
  141. }
  142. static int orion_mdio_reset(struct mii_bus *bus)
  143. {
  144. return 0;
  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 = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
  170. if (!bus) {
  171. dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
  172. return -ENOMEM;
  173. }
  174. bus->name = "orion_mdio_bus";
  175. bus->read = orion_mdio_read;
  176. bus->write = orion_mdio_write;
  177. bus->reset = orion_mdio_reset;
  178. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
  179. dev_name(&pdev->dev));
  180. bus->parent = &pdev->dev;
  181. bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  182. if (!bus->irq) {
  183. mdiobus_free(bus);
  184. return -ENOMEM;
  185. }
  186. for (i = 0; i < PHY_MAX_ADDR; i++)
  187. bus->irq[i] = PHY_POLL;
  188. dev = bus->priv;
  189. dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  190. if (!dev->regs) {
  191. dev_err(&pdev->dev, "Unable to remap SMI register\n");
  192. ret = -ENODEV;
  193. goto out_mdio;
  194. }
  195. init_waitqueue_head(&dev->smi_busy_wait);
  196. dev->err_interrupt = platform_get_irq(pdev, 0);
  197. if (dev->err_interrupt != -ENXIO) {
  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. }
  206. mutex_init(&dev->lock);
  207. if (pdev->dev.of_node)
  208. ret = of_mdiobus_register(bus, pdev->dev.of_node);
  209. else
  210. ret = mdiobus_register(bus);
  211. if (ret < 0) {
  212. dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
  213. goto out_mdio;
  214. }
  215. platform_set_drvdata(pdev, bus);
  216. return 0;
  217. out_mdio:
  218. kfree(bus->irq);
  219. mdiobus_free(bus);
  220. return ret;
  221. }
  222. static int orion_mdio_remove(struct platform_device *pdev)
  223. {
  224. struct mii_bus *bus = platform_get_drvdata(pdev);
  225. struct orion_mdio_dev *dev = bus->priv;
  226. writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
  227. mdiobus_unregister(bus);
  228. kfree(bus->irq);
  229. mdiobus_free(bus);
  230. return 0;
  231. }
  232. static const struct of_device_id orion_mdio_match[] = {
  233. { .compatible = "marvell,orion-mdio" },
  234. { }
  235. };
  236. MODULE_DEVICE_TABLE(of, orion_mdio_match);
  237. static struct platform_driver orion_mdio_driver = {
  238. .probe = orion_mdio_probe,
  239. .remove = orion_mdio_remove,
  240. .driver = {
  241. .name = "orion-mdio",
  242. .of_match_table = orion_mdio_match,
  243. },
  244. };
  245. module_platform_driver(orion_mdio_driver);
  246. MODULE_DESCRIPTION("Marvell MDIO interface driver");
  247. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  248. MODULE_LICENSE("GPL");