mdio-moxart.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /* Setting PHY_IGNORE_INTERRUPT here even if it has no effect,
  102. * of_mdiobus_register() sets these PHY_POLL.
  103. * Ideally, the interrupt from MAC controller could be used to
  104. * detect link state changes, not polling, i.e. if there was
  105. * a way phy_driver could set PHY_HAS_INTERRUPT but have that
  106. * interrupt handled in ethernet drivercode.
  107. */
  108. for (i = 0; i < PHY_MAX_ADDR; i++)
  109. bus->irq[i] = PHY_IGNORE_INTERRUPT;
  110. data = bus->priv;
  111. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. data->base = devm_ioremap_resource(&pdev->dev, res);
  113. if (IS_ERR(data->base)) {
  114. ret = PTR_ERR(data->base);
  115. goto err_out_free_mdiobus;
  116. }
  117. ret = of_mdiobus_register(bus, np);
  118. if (ret < 0)
  119. goto err_out_free_mdiobus;
  120. platform_set_drvdata(pdev, bus);
  121. return 0;
  122. err_out_free_mdiobus:
  123. mdiobus_free(bus);
  124. return ret;
  125. }
  126. static int moxart_mdio_remove(struct platform_device *pdev)
  127. {
  128. struct mii_bus *bus = platform_get_drvdata(pdev);
  129. mdiobus_unregister(bus);
  130. mdiobus_free(bus);
  131. return 0;
  132. }
  133. static const struct of_device_id moxart_mdio_dt_ids[] = {
  134. { .compatible = "moxa,moxart-mdio" },
  135. { }
  136. };
  137. MODULE_DEVICE_TABLE(of, moxart_mdio_dt_ids);
  138. static struct platform_driver moxart_mdio_driver = {
  139. .probe = moxart_mdio_probe,
  140. .remove = moxart_mdio_remove,
  141. .driver = {
  142. .name = "moxart-mdio",
  143. .of_match_table = moxart_mdio_dt_ids,
  144. },
  145. };
  146. module_platform_driver(moxart_mdio_driver);
  147. MODULE_DESCRIPTION("MOXA ART MDIO interface driver");
  148. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
  149. MODULE_LICENSE("GPL");