mdio-moxart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* MOXA ART Ethernet (RTL8201CP) MDIO interface driver
  2. *
  3. * Copyright (C) 2013 Jonas Jensen <jonas.jensen@gmail.com>
  4. *
  5. * This file is licensed under the terms of the GNU General Public
  6. * License version 2. This program is licensed "as is" without any
  7. * warranty of any kind, whether express or implied.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/phy.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regulator/consumer.h>
  18. #define REG_PHY_CTRL 0
  19. #define REG_PHY_WRITE_DATA 4
  20. /* REG_PHY_CTRL */
  21. #define MIIWR BIT(27) /* init write sequence (auto cleared)*/
  22. #define MIIRD BIT(26)
  23. #define REGAD_MASK 0x3e00000
  24. #define PHYAD_MASK 0x1f0000
  25. #define MIIRDATA_MASK 0xffff
  26. /* REG_PHY_WRITE_DATA */
  27. #define MIIWDATA_MASK 0xffff
  28. struct moxart_mdio_data {
  29. void __iomem *base;
  30. };
  31. static int moxart_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  32. {
  33. struct moxart_mdio_data *data = bus->priv;
  34. u32 ctrl = 0;
  35. unsigned int count = 5;
  36. dev_dbg(&bus->dev, "%s\n", __func__);
  37. ctrl |= MIIRD | ((mii_id << 16) & PHYAD_MASK) |
  38. ((regnum << 21) & REGAD_MASK);
  39. writel(ctrl, data->base + REG_PHY_CTRL);
  40. do {
  41. ctrl = readl(data->base + REG_PHY_CTRL);
  42. if (!(ctrl & MIIRD))
  43. return ctrl & MIIRDATA_MASK;
  44. mdelay(10);
  45. count--;
  46. } while (count > 0);
  47. dev_dbg(&bus->dev, "%s timed out\n", __func__);
  48. return -ETIMEDOUT;
  49. }
  50. static int moxart_mdio_write(struct mii_bus *bus, int mii_id,
  51. int regnum, u16 value)
  52. {
  53. struct moxart_mdio_data *data = bus->priv;
  54. u32 ctrl = 0;
  55. unsigned int count = 5;
  56. dev_dbg(&bus->dev, "%s\n", __func__);
  57. ctrl |= MIIWR | ((mii_id << 16) & PHYAD_MASK) |
  58. ((regnum << 21) & REGAD_MASK);
  59. value &= MIIWDATA_MASK;
  60. writel(value, data->base + REG_PHY_WRITE_DATA);
  61. writel(ctrl, data->base + REG_PHY_CTRL);
  62. do {
  63. ctrl = readl(data->base + REG_PHY_CTRL);
  64. if (!(ctrl & MIIWR))
  65. return 0;
  66. mdelay(10);
  67. count--;
  68. } while (count > 0);
  69. dev_dbg(&bus->dev, "%s timed out\n", __func__);
  70. return -ETIMEDOUT;
  71. }
  72. static int moxart_mdio_reset(struct mii_bus *bus)
  73. {
  74. int data, i;
  75. for (i = 0; i < PHY_MAX_ADDR; i++) {
  76. data = moxart_mdio_read(bus, i, MII_BMCR);
  77. if (data < 0)
  78. continue;
  79. data |= BMCR_RESET;
  80. if (moxart_mdio_write(bus, i, MII_BMCR, data) < 0)
  81. continue;
  82. }
  83. return 0;
  84. }
  85. static int moxart_mdio_probe(struct platform_device *pdev)
  86. {
  87. struct device_node *np = pdev->dev.of_node;
  88. struct mii_bus *bus;
  89. struct moxart_mdio_data *data;
  90. struct resource *res;
  91. int ret, i;
  92. bus = mdiobus_alloc_size(sizeof(*data));
  93. if (!bus)
  94. return -ENOMEM;
  95. bus->name = "MOXA ART Ethernet MII";
  96. bus->read = &moxart_mdio_read;
  97. bus->write = &moxart_mdio_write;
  98. bus->reset = &moxart_mdio_reset;
  99. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d-mii", pdev->name, pdev->id);
  100. bus->parent = &pdev->dev;
  101. bus->irq = devm_kzalloc(&pdev->dev, sizeof(int) * PHY_MAX_ADDR,
  102. GFP_KERNEL);
  103. if (!bus->irq) {
  104. ret = -ENOMEM;
  105. goto err_out_free_mdiobus;
  106. }
  107. /* Setting PHY_IGNORE_INTERRUPT here even if it has no effect,
  108. * of_mdiobus_register() sets these PHY_POLL.
  109. * Ideally, the interrupt from MAC controller could be used to
  110. * detect link state changes, not polling, i.e. if there was
  111. * a way phy_driver could set PHY_HAS_INTERRUPT but have that
  112. * interrupt handled in ethernet drivercode.
  113. */
  114. for (i = 0; i < PHY_MAX_ADDR; i++)
  115. bus->irq[i] = PHY_IGNORE_INTERRUPT;
  116. data = bus->priv;
  117. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  118. data->base = devm_ioremap_resource(&pdev->dev, res);
  119. if (IS_ERR(data->base)) {
  120. ret = PTR_ERR(data->base);
  121. goto err_out_free_mdiobus;
  122. }
  123. ret = of_mdiobus_register(bus, np);
  124. if (ret < 0)
  125. goto err_out_free_mdiobus;
  126. platform_set_drvdata(pdev, bus);
  127. return 0;
  128. err_out_free_mdiobus:
  129. mdiobus_free(bus);
  130. return ret;
  131. }
  132. static int moxart_mdio_remove(struct platform_device *pdev)
  133. {
  134. struct mii_bus *bus = platform_get_drvdata(pdev);
  135. mdiobus_unregister(bus);
  136. mdiobus_free(bus);
  137. return 0;
  138. }
  139. static const struct of_device_id moxart_mdio_dt_ids[] = {
  140. { .compatible = "moxa,moxart-mdio" },
  141. { }
  142. };
  143. MODULE_DEVICE_TABLE(of, moxart_mdio_dt_ids);
  144. static struct platform_driver moxart_mdio_driver = {
  145. .probe = moxart_mdio_probe,
  146. .remove = moxart_mdio_remove,
  147. .driver = {
  148. .name = "moxart-mdio",
  149. .of_match_table = moxart_mdio_dt_ids,
  150. },
  151. };
  152. module_platform_driver(moxart_mdio_driver);
  153. MODULE_DESCRIPTION("MOXA ART MDIO interface driver");
  154. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
  155. MODULE_LICENSE("GPL");