mdio_rt2880.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; version 2 of the License
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Copyright (C) 2009-2015 John Crispin <[email protected]>
  11. * Copyright (C) 2009-2015 Felix Fietkau <[email protected]>
  12. * Copyright (C) 2013-2015 Michael Lee <[email protected]>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/of_net.h>
  18. #include <linux/of_mdio.h>
  19. #include "mtk_eth_soc.h"
  20. #include "mdio_rt2880.h"
  21. #include "mdio.h"
  22. #define FE_MDIO_RETRY 1000
  23. static unsigned char *rt2880_speed_str(struct fe_priv *priv)
  24. {
  25. switch (priv->phy->speed[0]) {
  26. case SPEED_1000:
  27. return "1000";
  28. case SPEED_100:
  29. return "100";
  30. case SPEED_10:
  31. return "10";
  32. }
  33. return "?";
  34. }
  35. void rt2880_mdio_link_adjust(struct fe_priv *priv, int port)
  36. {
  37. u32 mdio_cfg;
  38. if (!priv->link[0]) {
  39. netif_carrier_off(priv->netdev);
  40. netdev_info(priv->netdev, "link down\n");
  41. return;
  42. }
  43. mdio_cfg = FE_MDIO_CFG_TX_CLK_SKEW_200 |
  44. FE_MDIO_CFG_RX_CLK_SKEW_200 |
  45. FE_MDIO_CFG_GP1_FRC_EN;
  46. if (priv->phy->duplex[0] == DUPLEX_FULL)
  47. mdio_cfg |= FE_MDIO_CFG_GP1_DUPLEX;
  48. if (priv->phy->tx_fc[0])
  49. mdio_cfg |= FE_MDIO_CFG_GP1_FC_TX;
  50. if (priv->phy->rx_fc[0])
  51. mdio_cfg |= FE_MDIO_CFG_GP1_FC_RX;
  52. switch (priv->phy->speed[0]) {
  53. case SPEED_10:
  54. mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_10;
  55. break;
  56. case SPEED_100:
  57. mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_100;
  58. break;
  59. case SPEED_1000:
  60. mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_1000;
  61. break;
  62. default:
  63. BUG();
  64. }
  65. fe_w32(mdio_cfg, FE_MDIO_CFG);
  66. netif_carrier_on(priv->netdev);
  67. netdev_info(priv->netdev, "link up (%sMbps/%s duplex)\n",
  68. rt2880_speed_str(priv),
  69. (priv->phy->duplex[0] == DUPLEX_FULL) ? "Full" : "Half");
  70. }
  71. static int rt2880_mdio_wait_ready(struct fe_priv *priv)
  72. {
  73. int retries;
  74. retries = FE_MDIO_RETRY;
  75. while (1) {
  76. u32 t;
  77. t = fe_r32(FE_MDIO_ACCESS);
  78. if ((t & BIT(31)) == 0)
  79. return 0;
  80. if (retries-- == 0)
  81. break;
  82. udelay(1);
  83. }
  84. dev_err(priv->device, "MDIO operation timed out\n");
  85. return -ETIMEDOUT;
  86. }
  87. int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
  88. {
  89. struct fe_priv *priv = bus->priv;
  90. int err;
  91. u32 t;
  92. err = rt2880_mdio_wait_ready(priv);
  93. if (err)
  94. return 0xffff;
  95. t = (phy_addr << 24) | (phy_reg << 16);
  96. fe_w32(t, FE_MDIO_ACCESS);
  97. t |= BIT(31);
  98. fe_w32(t, FE_MDIO_ACCESS);
  99. err = rt2880_mdio_wait_ready(priv);
  100. if (err)
  101. return 0xffff;
  102. pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
  103. phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
  104. return fe_r32(FE_MDIO_ACCESS) & 0xffff;
  105. }
  106. int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
  107. {
  108. struct fe_priv *priv = bus->priv;
  109. int err;
  110. u32 t;
  111. pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
  112. phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
  113. err = rt2880_mdio_wait_ready(priv);
  114. if (err)
  115. return err;
  116. t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
  117. fe_w32(t, FE_MDIO_ACCESS);
  118. t |= BIT(31);
  119. fe_w32(t, FE_MDIO_ACCESS);
  120. return rt2880_mdio_wait_ready(priv);
  121. }
  122. void rt2880_port_init(struct fe_priv *priv, struct device_node *np)
  123. {
  124. const __be32 *id = of_get_property(np, "reg", NULL);
  125. const __be32 *link;
  126. int size;
  127. int phy_mode;
  128. if (!id || (be32_to_cpu(*id) != 0)) {
  129. pr_err("%s: invalid port id\n", np->name);
  130. return;
  131. }
  132. priv->phy->phy_fixed[0] = of_get_property(np,
  133. "mediatek,fixed-link", &size);
  134. if (priv->phy->phy_fixed[0] &&
  135. (size != (4 * sizeof(*priv->phy->phy_fixed[0])))) {
  136. pr_err("%s: invalid fixed link property\n", np->name);
  137. priv->phy->phy_fixed[0] = NULL;
  138. return;
  139. }
  140. phy_mode = of_get_phy_mode(np);
  141. switch (phy_mode) {
  142. case PHY_INTERFACE_MODE_RGMII:
  143. break;
  144. case PHY_INTERFACE_MODE_MII:
  145. break;
  146. case PHY_INTERFACE_MODE_RMII:
  147. break;
  148. default:
  149. if (!priv->phy->phy_fixed[0])
  150. dev_err(priv->device, "port %d - invalid phy mode\n",
  151. priv->phy->speed[0]);
  152. break;
  153. }
  154. priv->phy->phy_node[0] = of_parse_phandle(np, "phy-handle", 0);
  155. if (!priv->phy->phy_node[0] && !priv->phy->phy_fixed[0])
  156. return;
  157. if (priv->phy->phy_fixed[0]) {
  158. link = priv->phy->phy_fixed[0];
  159. priv->phy->speed[0] = be32_to_cpup(link++);
  160. priv->phy->duplex[0] = be32_to_cpup(link++);
  161. priv->phy->tx_fc[0] = be32_to_cpup(link++);
  162. priv->phy->rx_fc[0] = be32_to_cpup(link++);
  163. priv->link[0] = 1;
  164. switch (priv->phy->speed[0]) {
  165. case SPEED_10:
  166. break;
  167. case SPEED_100:
  168. break;
  169. case SPEED_1000:
  170. break;
  171. default:
  172. dev_err(priv->device, "invalid link speed: %d\n",
  173. priv->phy->speed[0]);
  174. priv->phy->phy_fixed[0] = 0;
  175. return;
  176. }
  177. dev_info(priv->device, "using fixed link parameters\n");
  178. rt2880_mdio_link_adjust(priv, 0);
  179. return;
  180. }
  181. if (priv->phy->phy_node[0] && mdiobus_get_phy(priv->mii_bus, 0))
  182. fe_connect_phy_node(priv, priv->phy->phy_node[0]);
  183. }