mdio-sun4i.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Allwinner EMAC MDIO interface driver
  4. *
  5. * Copyright 2012-2013 Stefan Roese <sr@denx.de>
  6. * Copyright 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * Based on the Linux driver provided by Allwinner:
  9. * Copyright (C) 1997 Sten Wang
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_mdio.h>
  17. #include <linux/phy.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #define EMAC_MAC_MCMD_REG (0x00)
  21. #define EMAC_MAC_MADR_REG (0x04)
  22. #define EMAC_MAC_MWTD_REG (0x08)
  23. #define EMAC_MAC_MRDD_REG (0x0c)
  24. #define EMAC_MAC_MIND_REG (0x10)
  25. #define EMAC_MAC_SSRR_REG (0x14)
  26. #define MDIO_TIMEOUT (msecs_to_jiffies(100))
  27. struct sun4i_mdio_data {
  28. void __iomem *membase;
  29. struct regulator *regulator;
  30. };
  31. static int sun4i_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  32. {
  33. struct sun4i_mdio_data *data = bus->priv;
  34. unsigned long timeout_jiffies;
  35. int value;
  36. /* issue the phy address and reg */
  37. writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
  38. /* pull up the phy io line */
  39. writel(0x1, data->membase + EMAC_MAC_MCMD_REG);
  40. /* Wait read complete */
  41. timeout_jiffies = jiffies + MDIO_TIMEOUT;
  42. while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
  43. if (time_is_before_jiffies(timeout_jiffies))
  44. return -ETIMEDOUT;
  45. msleep(1);
  46. }
  47. /* push down the phy io line */
  48. writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
  49. /* and read data */
  50. value = readl(data->membase + EMAC_MAC_MRDD_REG);
  51. return value;
  52. }
  53. static int sun4i_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  54. u16 value)
  55. {
  56. struct sun4i_mdio_data *data = bus->priv;
  57. unsigned long timeout_jiffies;
  58. /* issue the phy address and reg */
  59. writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
  60. /* pull up the phy io line */
  61. writel(0x1, data->membase + EMAC_MAC_MCMD_REG);
  62. /* Wait read complete */
  63. timeout_jiffies = jiffies + MDIO_TIMEOUT;
  64. while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
  65. if (time_is_before_jiffies(timeout_jiffies))
  66. return -ETIMEDOUT;
  67. msleep(1);
  68. }
  69. /* push down the phy io line */
  70. writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
  71. /* and write data */
  72. writel(value, data->membase + EMAC_MAC_MWTD_REG);
  73. return 0;
  74. }
  75. static int sun4i_mdio_probe(struct platform_device *pdev)
  76. {
  77. struct device_node *np = pdev->dev.of_node;
  78. struct mii_bus *bus;
  79. struct sun4i_mdio_data *data;
  80. int ret;
  81. bus = mdiobus_alloc_size(sizeof(*data));
  82. if (!bus)
  83. return -ENOMEM;
  84. bus->name = "sun4i_mii_bus";
  85. bus->read = &sun4i_mdio_read;
  86. bus->write = &sun4i_mdio_write;
  87. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
  88. bus->parent = &pdev->dev;
  89. data = bus->priv;
  90. data->membase = devm_platform_ioremap_resource(pdev, 0);
  91. if (IS_ERR(data->membase)) {
  92. ret = PTR_ERR(data->membase);
  93. goto err_out_free_mdiobus;
  94. }
  95. data->regulator = devm_regulator_get(&pdev->dev, "phy");
  96. if (IS_ERR(data->regulator)) {
  97. if (PTR_ERR(data->regulator) == -EPROBE_DEFER) {
  98. ret = -EPROBE_DEFER;
  99. goto err_out_free_mdiobus;
  100. }
  101. dev_info(&pdev->dev, "no regulator found\n");
  102. data->regulator = NULL;
  103. } else {
  104. ret = regulator_enable(data->regulator);
  105. if (ret)
  106. goto err_out_free_mdiobus;
  107. }
  108. ret = of_mdiobus_register(bus, np);
  109. if (ret < 0)
  110. goto err_out_disable_regulator;
  111. platform_set_drvdata(pdev, bus);
  112. return 0;
  113. err_out_disable_regulator:
  114. if (data->regulator)
  115. regulator_disable(data->regulator);
  116. err_out_free_mdiobus:
  117. mdiobus_free(bus);
  118. return ret;
  119. }
  120. static int sun4i_mdio_remove(struct platform_device *pdev)
  121. {
  122. struct mii_bus *bus = platform_get_drvdata(pdev);
  123. struct sun4i_mdio_data *data = bus->priv;
  124. mdiobus_unregister(bus);
  125. if (data->regulator)
  126. regulator_disable(data->regulator);
  127. mdiobus_free(bus);
  128. return 0;
  129. }
  130. static const struct of_device_id sun4i_mdio_dt_ids[] = {
  131. { .compatible = "allwinner,sun4i-a10-mdio" },
  132. /* Deprecated */
  133. { .compatible = "allwinner,sun4i-mdio" },
  134. { }
  135. };
  136. MODULE_DEVICE_TABLE(of, sun4i_mdio_dt_ids);
  137. static struct platform_driver sun4i_mdio_driver = {
  138. .probe = sun4i_mdio_probe,
  139. .remove = sun4i_mdio_remove,
  140. .driver = {
  141. .name = "sun4i-mdio",
  142. .of_match_table = sun4i_mdio_dt_ids,
  143. },
  144. };
  145. module_platform_driver(sun4i_mdio_driver);
  146. MODULE_DESCRIPTION("Allwinner EMAC MDIO interface driver");
  147. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  148. MODULE_LICENSE("GPL v2");