emac-phy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include "emac-phy.h"
  24. #include "emac-sgmii.h"
  25. /* EMAC base register offsets */
  26. #define EMAC_MDIO_CTRL 0x001414
  27. #define EMAC_PHY_STS 0x001418
  28. #define EMAC_MDIO_EX_CTRL 0x001440
  29. /* EMAC_MDIO_CTRL */
  30. #define MDIO_MODE BIT(30)
  31. #define MDIO_PR BIT(29)
  32. #define MDIO_AP_EN BIT(28)
  33. #define MDIO_BUSY BIT(27)
  34. #define MDIO_CLK_SEL_BMSK 0x7000000
  35. #define MDIO_CLK_SEL_SHFT 24
  36. #define MDIO_START BIT(23)
  37. #define SUP_PREAMBLE BIT(22)
  38. #define MDIO_RD_NWR BIT(21)
  39. #define MDIO_REG_ADDR_BMSK 0x1f0000
  40. #define MDIO_REG_ADDR_SHFT 16
  41. #define MDIO_DATA_BMSK 0xffff
  42. #define MDIO_DATA_SHFT 0
  43. /* EMAC_PHY_STS */
  44. #define PHY_ADDR_BMSK 0x1f0000
  45. #define PHY_ADDR_SHFT 16
  46. #define MDIO_CLK_25_4 0
  47. #define MDIO_CLK_25_28 7
  48. #define MDIO_WAIT_TIMES 1000
  49. #define EMAC_LINK_SPEED_DEFAULT (\
  50. EMAC_LINK_SPEED_10_HALF |\
  51. EMAC_LINK_SPEED_10_FULL |\
  52. EMAC_LINK_SPEED_100_HALF |\
  53. EMAC_LINK_SPEED_100_FULL |\
  54. EMAC_LINK_SPEED_1GB_FULL)
  55. /**
  56. * emac_phy_mdio_autopoll_disable() - disable mdio autopoll
  57. * @adpt: the emac adapter
  58. *
  59. * The autopoll feature takes over the MDIO bus. In order for
  60. * the PHY driver to be able to talk to the PHY over the MDIO
  61. * bus, we need to temporarily disable the autopoll feature.
  62. */
  63. static int emac_phy_mdio_autopoll_disable(struct emac_adapter *adpt)
  64. {
  65. u32 val;
  66. /* disable autopoll */
  67. emac_reg_update32(adpt->base + EMAC_MDIO_CTRL, MDIO_AP_EN, 0);
  68. /* wait for any mdio polling to complete */
  69. if (!readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, val,
  70. !(val & MDIO_BUSY), 100, MDIO_WAIT_TIMES * 100))
  71. return 0;
  72. /* failed to disable; ensure it is enabled before returning */
  73. emac_reg_update32(adpt->base + EMAC_MDIO_CTRL, 0, MDIO_AP_EN);
  74. return -EBUSY;
  75. }
  76. /**
  77. * emac_phy_mdio_autopoll_disable() - disable mdio autopoll
  78. * @adpt: the emac adapter
  79. *
  80. * The EMAC has the ability to poll the external PHY on the MDIO
  81. * bus for link state changes. This eliminates the need for the
  82. * driver to poll the phy. If if the link state does change,
  83. * the EMAC issues an interrupt on behalf of the PHY.
  84. */
  85. static void emac_phy_mdio_autopoll_enable(struct emac_adapter *adpt)
  86. {
  87. emac_reg_update32(adpt->base + EMAC_MDIO_CTRL, 0, MDIO_AP_EN);
  88. }
  89. static int emac_mdio_read(struct mii_bus *bus, int addr, int regnum)
  90. {
  91. struct emac_adapter *adpt = bus->priv;
  92. u32 reg;
  93. int ret;
  94. ret = emac_phy_mdio_autopoll_disable(adpt);
  95. if (ret)
  96. return ret;
  97. emac_reg_update32(adpt->base + EMAC_PHY_STS, PHY_ADDR_BMSK,
  98. (addr << PHY_ADDR_SHFT));
  99. reg = SUP_PREAMBLE |
  100. ((MDIO_CLK_25_4 << MDIO_CLK_SEL_SHFT) & MDIO_CLK_SEL_BMSK) |
  101. ((regnum << MDIO_REG_ADDR_SHFT) & MDIO_REG_ADDR_BMSK) |
  102. MDIO_START | MDIO_RD_NWR;
  103. writel(reg, adpt->base + EMAC_MDIO_CTRL);
  104. if (readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, reg,
  105. !(reg & (MDIO_START | MDIO_BUSY)),
  106. 100, MDIO_WAIT_TIMES * 100))
  107. ret = -EIO;
  108. else
  109. ret = (reg >> MDIO_DATA_SHFT) & MDIO_DATA_BMSK;
  110. emac_phy_mdio_autopoll_enable(adpt);
  111. return ret;
  112. }
  113. static int emac_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val)
  114. {
  115. struct emac_adapter *adpt = bus->priv;
  116. u32 reg;
  117. int ret;
  118. ret = emac_phy_mdio_autopoll_disable(adpt);
  119. if (ret)
  120. return ret;
  121. emac_reg_update32(adpt->base + EMAC_PHY_STS, PHY_ADDR_BMSK,
  122. (addr << PHY_ADDR_SHFT));
  123. reg = SUP_PREAMBLE |
  124. ((MDIO_CLK_25_4 << MDIO_CLK_SEL_SHFT) & MDIO_CLK_SEL_BMSK) |
  125. ((regnum << MDIO_REG_ADDR_SHFT) & MDIO_REG_ADDR_BMSK) |
  126. ((val << MDIO_DATA_SHFT) & MDIO_DATA_BMSK) |
  127. MDIO_START;
  128. writel(reg, adpt->base + EMAC_MDIO_CTRL);
  129. if (readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, reg,
  130. !(reg & (MDIO_START | MDIO_BUSY)), 100,
  131. MDIO_WAIT_TIMES * 100))
  132. ret = -EIO;
  133. emac_phy_mdio_autopoll_enable(adpt);
  134. return ret;
  135. }
  136. /* Configure the MDIO bus and connect the external PHY */
  137. int emac_phy_config(struct platform_device *pdev, struct emac_adapter *adpt)
  138. {
  139. struct device_node *np = pdev->dev.of_node;
  140. struct mii_bus *mii_bus;
  141. int ret;
  142. /* Create the mii_bus object for talking to the MDIO bus */
  143. adpt->mii_bus = mii_bus = devm_mdiobus_alloc(&pdev->dev);
  144. if (!mii_bus)
  145. return -ENOMEM;
  146. mii_bus->name = "emac-mdio";
  147. snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
  148. mii_bus->read = emac_mdio_read;
  149. mii_bus->write = emac_mdio_write;
  150. mii_bus->parent = &pdev->dev;
  151. mii_bus->priv = adpt;
  152. if (has_acpi_companion(&pdev->dev)) {
  153. u32 phy_addr;
  154. ret = mdiobus_register(mii_bus);
  155. if (ret) {
  156. dev_err(&pdev->dev, "could not register mdio bus\n");
  157. return ret;
  158. }
  159. ret = device_property_read_u32(&pdev->dev, "phy-channel",
  160. &phy_addr);
  161. if (ret)
  162. /* If we can't read a valid phy address, then assume
  163. * that there is only one phy on this mdio bus.
  164. */
  165. adpt->phydev = phy_find_first(mii_bus);
  166. else
  167. adpt->phydev = mdiobus_get_phy(mii_bus, phy_addr);
  168. } else {
  169. struct device_node *phy_np;
  170. ret = of_mdiobus_register(mii_bus, np);
  171. if (ret) {
  172. dev_err(&pdev->dev, "could not register mdio bus\n");
  173. return ret;
  174. }
  175. phy_np = of_parse_phandle(np, "phy-handle", 0);
  176. adpt->phydev = of_phy_find_device(phy_np);
  177. of_node_put(phy_np);
  178. }
  179. if (!adpt->phydev) {
  180. dev_err(&pdev->dev, "could not find external phy\n");
  181. mdiobus_unregister(mii_bus);
  182. return -ENODEV;
  183. }
  184. if (adpt->phydev->drv)
  185. phy_attached_print(adpt->phydev, NULL);
  186. return 0;
  187. }