mvmdio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/of_address.h>
  27. #include <linux/of_mdio.h>
  28. #include <linux/platform_device.h>
  29. #include <asm/delay.h>
  30. #define MVMDIO_SMI_DATA_SHIFT 0
  31. #define MVMDIO_SMI_PHY_ADDR_SHIFT 16
  32. #define MVMDIO_SMI_PHY_REG_SHIFT 21
  33. #define MVMDIO_SMI_READ_OPERATION BIT(26)
  34. #define MVMDIO_SMI_WRITE_OPERATION 0
  35. #define MVMDIO_SMI_READ_VALID BIT(27)
  36. #define MVMDIO_SMI_BUSY BIT(28)
  37. struct orion_mdio_dev {
  38. struct mutex lock;
  39. void __iomem *smireg;
  40. };
  41. /*
  42. * Wait for the SMI unit to be ready for another operation
  43. */
  44. static int orion_mdio_wait_ready(struct mii_bus *bus)
  45. {
  46. struct orion_mdio_dev *dev = bus->priv;
  47. int count;
  48. u32 val;
  49. count = 0;
  50. while (1) {
  51. val = readl(dev->smireg);
  52. if (!(val & MVMDIO_SMI_BUSY))
  53. break;
  54. if (count > 100) {
  55. dev_err(bus->parent, "Timeout: SMI busy for too long\n");
  56. return -ETIMEDOUT;
  57. }
  58. udelay(10);
  59. count++;
  60. }
  61. return 0;
  62. }
  63. static int orion_mdio_read(struct mii_bus *bus, int mii_id,
  64. int regnum)
  65. {
  66. struct orion_mdio_dev *dev = bus->priv;
  67. int count;
  68. u32 val;
  69. int ret;
  70. mutex_lock(&dev->lock);
  71. ret = orion_mdio_wait_ready(bus);
  72. if (ret < 0) {
  73. mutex_unlock(&dev->lock);
  74. return ret;
  75. }
  76. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  77. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  78. MVMDIO_SMI_READ_OPERATION),
  79. dev->smireg);
  80. /* Wait for the value to become available */
  81. count = 0;
  82. while (1) {
  83. val = readl(dev->smireg);
  84. if (val & MVMDIO_SMI_READ_VALID)
  85. break;
  86. if (count > 100) {
  87. dev_err(bus->parent, "Timeout when reading PHY\n");
  88. mutex_unlock(&dev->lock);
  89. return -ETIMEDOUT;
  90. }
  91. udelay(10);
  92. count++;
  93. }
  94. mutex_unlock(&dev->lock);
  95. return val & 0xFFFF;
  96. }
  97. static int orion_mdio_write(struct mii_bus *bus, int mii_id,
  98. int regnum, u16 value)
  99. {
  100. struct orion_mdio_dev *dev = bus->priv;
  101. int ret;
  102. mutex_lock(&dev->lock);
  103. ret = orion_mdio_wait_ready(bus);
  104. if (ret < 0) {
  105. mutex_unlock(&dev->lock);
  106. return ret;
  107. }
  108. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  109. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  110. MVMDIO_SMI_WRITE_OPERATION |
  111. (value << MVMDIO_SMI_DATA_SHIFT)),
  112. dev->smireg);
  113. mutex_unlock(&dev->lock);
  114. return 0;
  115. }
  116. static int orion_mdio_reset(struct mii_bus *bus)
  117. {
  118. return 0;
  119. }
  120. static int __devinit orion_mdio_probe(struct platform_device *pdev)
  121. {
  122. struct device_node *np = pdev->dev.of_node;
  123. struct mii_bus *bus;
  124. struct orion_mdio_dev *dev;
  125. int i, ret;
  126. bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
  127. if (!bus) {
  128. dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
  129. return -ENOMEM;
  130. }
  131. bus->name = "orion_mdio_bus";
  132. bus->read = orion_mdio_read;
  133. bus->write = orion_mdio_write;
  134. bus->reset = orion_mdio_reset;
  135. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
  136. dev_name(&pdev->dev));
  137. bus->parent = &pdev->dev;
  138. bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  139. if (!bus->irq) {
  140. dev_err(&pdev->dev, "Cannot allocate PHY IRQ array\n");
  141. mdiobus_free(bus);
  142. return -ENOMEM;
  143. }
  144. for (i = 0; i < PHY_MAX_ADDR; i++)
  145. bus->irq[i] = PHY_POLL;
  146. dev = bus->priv;
  147. dev->smireg = of_iomap(pdev->dev.of_node, 0);
  148. if (!dev->smireg) {
  149. dev_err(&pdev->dev, "No SMI register address given in DT\n");
  150. kfree(bus->irq);
  151. mdiobus_free(bus);
  152. return -ENODEV;
  153. }
  154. mutex_init(&dev->lock);
  155. ret = of_mdiobus_register(bus, np);
  156. if (ret < 0) {
  157. dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
  158. iounmap(dev->smireg);
  159. kfree(bus->irq);
  160. mdiobus_free(bus);
  161. return ret;
  162. }
  163. platform_set_drvdata(pdev, bus);
  164. return 0;
  165. }
  166. static int __devexit orion_mdio_remove(struct platform_device *pdev)
  167. {
  168. struct mii_bus *bus = platform_get_drvdata(pdev);
  169. mdiobus_unregister(bus);
  170. kfree(bus->irq);
  171. mdiobus_free(bus);
  172. return 0;
  173. }
  174. static const struct of_device_id orion_mdio_match[] = {
  175. { .compatible = "marvell,orion-mdio" },
  176. { }
  177. };
  178. MODULE_DEVICE_TABLE(of, orion_mdio_match);
  179. static struct platform_driver orion_mdio_driver = {
  180. .probe = orion_mdio_probe,
  181. .remove = __devexit_p(orion_mdio_remove),
  182. .driver = {
  183. .name = "orion-mdio",
  184. .of_match_table = orion_mdio_match,
  185. },
  186. };
  187. module_platform_driver(orion_mdio_driver);
  188. MODULE_DESCRIPTION("Marvell MDIO interface driver");
  189. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  190. MODULE_LICENSE("GPL");