mdio-sun4i.c 4.6 KB

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