mvmdio.c 7.2 KB

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