mdio-sun4i.c 4.7 KB

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