emac-phy.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* Qualcomm Technologies, Inc. EMAC PHY Controller driver.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_net.h>
  17. #include <linux/of_mdio.h>
  18. #include <linux/phy.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/acpi.h>
  21. #include "emac.h"
  22. #include "emac-mac.h"
  23. /* EMAC base register offsets */
  24. #define EMAC_MDIO_CTRL 0x001414
  25. #define EMAC_PHY_STS 0x001418
  26. #define EMAC_MDIO_EX_CTRL 0x001440
  27. /* EMAC_MDIO_CTRL */
  28. #define MDIO_MODE BIT(30)
  29. #define MDIO_PR BIT(29)
  30. #define MDIO_AP_EN BIT(28)
  31. #define MDIO_BUSY BIT(27)
  32. #define MDIO_CLK_SEL_BMSK 0x7000000
  33. #define MDIO_CLK_SEL_SHFT 24
  34. #define MDIO_START BIT(23)
  35. #define SUP_PREAMBLE BIT(22)
  36. #define MDIO_RD_NWR BIT(21)
  37. #define MDIO_REG_ADDR_BMSK 0x1f0000
  38. #define MDIO_REG_ADDR_SHFT 16
  39. #define MDIO_DATA_BMSK 0xffff
  40. #define MDIO_DATA_SHFT 0
  41. /* EMAC_PHY_STS */
  42. #define PHY_ADDR_BMSK 0x1f0000
  43. #define PHY_ADDR_SHFT 16
  44. #define MDIO_CLK_25_4 0
  45. #define MDIO_CLK_25_28 7
  46. #define MDIO_WAIT_TIMES 1000
  47. #define EMAC_LINK_SPEED_DEFAULT (\
  48. EMAC_LINK_SPEED_10_HALF |\
  49. EMAC_LINK_SPEED_10_FULL |\
  50. EMAC_LINK_SPEED_100_HALF |\
  51. EMAC_LINK_SPEED_100_FULL |\
  52. EMAC_LINK_SPEED_1GB_FULL)
  53. /**
  54. * emac_phy_mdio_autopoll_disable() - disable mdio autopoll
  55. * @adpt: the emac adapter
  56. *
  57. * The autopoll feature takes over the MDIO bus. In order for
  58. * the PHY driver to be able to talk to the PHY over the MDIO
  59. * bus, we need to temporarily disable the autopoll feature.
  60. */
  61. static int emac_phy_mdio_autopoll_disable(struct emac_adapter *adpt)
  62. {
  63. u32 val;
  64. /* disable autopoll */
  65. emac_reg_update32(adpt->base + EMAC_MDIO_CTRL, MDIO_AP_EN, 0);
  66. /* wait for any mdio polling to complete */
  67. if (!readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, val,
  68. !(val & MDIO_BUSY), 100, MDIO_WAIT_TIMES * 100))
  69. return 0;
  70. /* failed to disable; ensure it is enabled before returning */
  71. emac_reg_update32(adpt->base + EMAC_MDIO_CTRL, 0, MDIO_AP_EN);
  72. return -EBUSY;
  73. }
  74. /**
  75. * emac_phy_mdio_autopoll_disable() - disable mdio autopoll
  76. * @adpt: the emac adapter
  77. *
  78. * The EMAC has the ability to poll the external PHY on the MDIO
  79. * bus for link state changes. This eliminates the need for the
  80. * driver to poll the phy. If if the link state does change,
  81. * the EMAC issues an interrupt on behalf of the PHY.
  82. */
  83. static void emac_phy_mdio_autopoll_enable(struct emac_adapter *adpt)
  84. {
  85. emac_reg_update32(adpt->base + EMAC_MDIO_CTRL, 0, MDIO_AP_EN);
  86. }
  87. static int emac_mdio_read(struct mii_bus *bus, int addr, int regnum)
  88. {
  89. struct emac_adapter *adpt = bus->priv;
  90. u32 reg;
  91. int ret;
  92. ret = emac_phy_mdio_autopoll_disable(adpt);
  93. if (ret)
  94. return ret;
  95. emac_reg_update32(adpt->base + EMAC_PHY_STS, PHY_ADDR_BMSK,
  96. (addr << PHY_ADDR_SHFT));
  97. reg = SUP_PREAMBLE |
  98. ((MDIO_CLK_25_4 << MDIO_CLK_SEL_SHFT) & MDIO_CLK_SEL_BMSK) |
  99. ((regnum << MDIO_REG_ADDR_SHFT) & MDIO_REG_ADDR_BMSK) |
  100. MDIO_START | MDIO_RD_NWR;
  101. writel(reg, adpt->base + EMAC_MDIO_CTRL);
  102. if (readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, reg,
  103. !(reg & (MDIO_START | MDIO_BUSY)),
  104. 100, MDIO_WAIT_TIMES * 100))
  105. ret = -EIO;
  106. else
  107. ret = (reg >> MDIO_DATA_SHFT) & MDIO_DATA_BMSK;
  108. emac_phy_mdio_autopoll_enable(adpt);
  109. return ret;
  110. }
  111. static int emac_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val)
  112. {
  113. struct emac_adapter *adpt = bus->priv;
  114. u32 reg;
  115. int ret;
  116. ret = emac_phy_mdio_autopoll_disable(adpt);
  117. if (ret)
  118. return ret;
  119. emac_reg_update32(adpt->base + EMAC_PHY_STS, PHY_ADDR_BMSK,
  120. (addr << PHY_ADDR_SHFT));
  121. reg = SUP_PREAMBLE |
  122. ((MDIO_CLK_25_4 << MDIO_CLK_SEL_SHFT) & MDIO_CLK_SEL_BMSK) |
  123. ((regnum << MDIO_REG_ADDR_SHFT) & MDIO_REG_ADDR_BMSK) |
  124. ((val << MDIO_DATA_SHFT) & MDIO_DATA_BMSK) |
  125. MDIO_START;
  126. writel(reg, adpt->base + EMAC_MDIO_CTRL);
  127. if (readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, reg,
  128. !(reg & (MDIO_START | MDIO_BUSY)), 100,
  129. MDIO_WAIT_TIMES * 100))
  130. ret = -EIO;
  131. emac_phy_mdio_autopoll_enable(adpt);
  132. return ret;
  133. }
  134. /* Configure the MDIO bus and connect the external PHY */
  135. int emac_phy_config(struct platform_device *pdev, struct emac_adapter *adpt)
  136. {
  137. struct device_node *np = pdev->dev.of_node;
  138. struct mii_bus *mii_bus;
  139. int ret;
  140. /* Create the mii_bus object for talking to the MDIO bus */
  141. adpt->mii_bus = mii_bus = devm_mdiobus_alloc(&pdev->dev);
  142. if (!mii_bus)
  143. return -ENOMEM;
  144. mii_bus->name = "emac-mdio";
  145. snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
  146. mii_bus->read = emac_mdio_read;
  147. mii_bus->write = emac_mdio_write;
  148. mii_bus->parent = &pdev->dev;
  149. mii_bus->priv = adpt;
  150. if (has_acpi_companion(&pdev->dev)) {
  151. u32 phy_addr;
  152. ret = mdiobus_register(mii_bus);
  153. if (ret) {
  154. dev_err(&pdev->dev, "could not register mdio bus\n");
  155. return ret;
  156. }
  157. ret = device_property_read_u32(&pdev->dev, "phy-channel",
  158. &phy_addr);
  159. if (ret)
  160. /* If we can't read a valid phy address, then assume
  161. * that there is only one phy on this mdio bus.
  162. */
  163. adpt->phydev = phy_find_first(mii_bus);
  164. else
  165. adpt->phydev = mdiobus_get_phy(mii_bus, phy_addr);
  166. /* of_phy_find_device() claims a reference to the phydev,
  167. * so we do that here manually as well. When the driver
  168. * later unloads, it can unilaterally drop the reference
  169. * without worrying about ACPI vs DT.
  170. */
  171. if (adpt->phydev)
  172. get_device(&adpt->phydev->mdio.dev);
  173. } else {
  174. struct device_node *phy_np;
  175. ret = of_mdiobus_register(mii_bus, np);
  176. if (ret) {
  177. dev_err(&pdev->dev, "could not register mdio bus\n");
  178. return ret;
  179. }
  180. phy_np = of_parse_phandle(np, "phy-handle", 0);
  181. adpt->phydev = of_phy_find_device(phy_np);
  182. of_node_put(phy_np);
  183. }
  184. if (!adpt->phydev) {
  185. dev_err(&pdev->dev, "could not find external phy\n");
  186. mdiobus_unregister(mii_bus);
  187. return -ENODEV;
  188. }
  189. return 0;
  190. }