mdio-moxart.c 4.6 KB

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