791-v6.3-09-net-phy-Add-driver-for-Motorcomm-yt8531-gigabit-ethernet.patch 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. From 4ac94f728a588e7096dd5010cd7141a309ea7805 Mon Sep 17 00:00:00 2001
  2. From: Frank Sae <[email protected]>
  3. Date: Thu, 2 Feb 2023 11:00:37 +0800
  4. Subject: [PATCH] net: phy: Add driver for Motorcomm yt8531 gigabit ethernet
  5. phy
  6. Add a driver for the motorcomm yt8531 gigabit ethernet phy. We have
  7. verified the driver on AM335x platform with yt8531 board. On the
  8. board, yt8531 gigabit ethernet phy works in utp mode, RGMII
  9. interface, supports 1000M/100M/10M speeds, and wol(magic package).
  10. Signed-off-by: Frank Sae <[email protected]>
  11. Reviewed-by: Andrew Lunn <[email protected]>
  12. Signed-off-by: David S. Miller <[email protected]>
  13. ---
  14. drivers/net/phy/Kconfig | 2 +-
  15. drivers/net/phy/motorcomm.c | 208 +++++++++++++++++++++++++++++++++++-
  16. 2 files changed, 207 insertions(+), 3 deletions(-)
  17. --- a/drivers/net/phy/Kconfig
  18. +++ b/drivers/net/phy/Kconfig
  19. @@ -257,7 +257,7 @@ config MOTORCOMM_PHY
  20. tristate "Motorcomm PHYs"
  21. help
  22. Enables support for Motorcomm network PHYs.
  23. - Currently supports the YT8511, YT8521, YT8531S Gigabit Ethernet PHYs.
  24. + Currently supports YT85xx Gigabit Ethernet PHYs.
  25. config NATIONAL_PHY
  26. tristate "National Semiconductor PHYs"
  27. --- a/drivers/net/phy/motorcomm.c
  28. +++ b/drivers/net/phy/motorcomm.c
  29. @@ -1,6 +1,6 @@
  30. // SPDX-License-Identifier: GPL-2.0+
  31. /*
  32. - * Motorcomm 8511/8521/8531S PHY driver.
  33. + * Motorcomm 8511/8521/8531/8531S PHY driver.
  34. *
  35. * Author: Peter Geis <[email protected]>
  36. * Author: Frank <[email protected]>
  37. @@ -14,6 +14,7 @@
  38. #define PHY_ID_YT8511 0x0000010a
  39. #define PHY_ID_YT8521 0x0000011a
  40. +#define PHY_ID_YT8531 0x4f51e91b
  41. #define PHY_ID_YT8531S 0x4f51e91a
  42. /* YT8521/YT8531S Register Overview
  43. @@ -517,6 +518,61 @@ err_restore_page:
  44. return phy_restore_page(phydev, old_page, ret);
  45. }
  46. +static int yt8531_set_wol(struct phy_device *phydev,
  47. + struct ethtool_wolinfo *wol)
  48. +{
  49. + const u16 mac_addr_reg[] = {
  50. + YTPHY_WOL_MACADDR2_REG,
  51. + YTPHY_WOL_MACADDR1_REG,
  52. + YTPHY_WOL_MACADDR0_REG,
  53. + };
  54. + const u8 *mac_addr;
  55. + u16 mask, val;
  56. + int ret;
  57. + u8 i;
  58. +
  59. + if (wol->wolopts & WAKE_MAGIC) {
  60. + mac_addr = phydev->attached_dev->dev_addr;
  61. +
  62. + /* Store the device address for the magic packet */
  63. + for (i = 0; i < 3; i++) {
  64. + ret = ytphy_write_ext_with_lock(phydev, mac_addr_reg[i],
  65. + ((mac_addr[i * 2] << 8)) |
  66. + (mac_addr[i * 2 + 1]));
  67. + if (ret < 0)
  68. + return ret;
  69. + }
  70. +
  71. + /* Enable WOL feature */
  72. + mask = YTPHY_WCR_PULSE_WIDTH_MASK | YTPHY_WCR_INTR_SEL;
  73. + val = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
  74. + val |= YTPHY_WCR_TYPE_PULSE | YTPHY_WCR_PULSE_WIDTH_672MS;
  75. + ret = ytphy_modify_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG,
  76. + mask, val);
  77. + if (ret < 0)
  78. + return ret;
  79. +
  80. + /* Enable WOL interrupt */
  81. + ret = phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG, 0,
  82. + YTPHY_IER_WOL);
  83. + if (ret < 0)
  84. + return ret;
  85. + } else {
  86. + /* Disable WOL feature */
  87. + mask = YTPHY_WCR_ENABLE | YTPHY_WCR_INTR_SEL;
  88. + ret = ytphy_modify_ext_with_lock(phydev, YTPHY_WOL_CONFIG_REG,
  89. + mask, 0);
  90. +
  91. + /* Disable WOL interrupt */
  92. + ret = phy_modify(phydev, YTPHY_INTERRUPT_ENABLE_REG,
  93. + YTPHY_IER_WOL, 0);
  94. + if (ret < 0)
  95. + return ret;
  96. + }
  97. +
  98. + return 0;
  99. +}
  100. +
  101. static int yt8511_read_page(struct phy_device *phydev)
  102. {
  103. return __phy_read(phydev, YT8511_PAGE_SELECT);
  104. @@ -767,6 +823,17 @@ static int ytphy_rgmii_clk_delay_config(
  105. return ytphy_modify_ext(phydev, YT8521_RGMII_CONFIG1_REG, mask, val);
  106. }
  107. +static int ytphy_rgmii_clk_delay_config_with_lock(struct phy_device *phydev)
  108. +{
  109. + int ret;
  110. +
  111. + phy_lock_mdio_bus(phydev);
  112. + ret = ytphy_rgmii_clk_delay_config(phydev);
  113. + phy_unlock_mdio_bus(phydev);
  114. +
  115. + return ret;
  116. +}
  117. +
  118. /**
  119. * yt8521_probe() - read chip config then set suitable polling_mode
  120. * @phydev: a pointer to a &struct phy_device
  121. @@ -891,6 +958,43 @@ static int yt8521_probe(struct phy_devic
  122. val);
  123. }
  124. +static int yt8531_probe(struct phy_device *phydev)
  125. +{
  126. + struct device_node *node = phydev->mdio.dev.of_node;
  127. + u16 mask, val;
  128. + u32 freq;
  129. +
  130. + if (of_property_read_u32(node, "motorcomm,clk-out-frequency-hz", &freq))
  131. + freq = YTPHY_DTS_OUTPUT_CLK_DIS;
  132. +
  133. + switch (freq) {
  134. + case YTPHY_DTS_OUTPUT_CLK_DIS:
  135. + mask = YT8531_SCR_SYNCE_ENABLE;
  136. + val = 0;
  137. + break;
  138. + case YTPHY_DTS_OUTPUT_CLK_25M:
  139. + mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
  140. + YT8531_SCR_CLK_FRE_SEL_125M;
  141. + val = YT8531_SCR_SYNCE_ENABLE |
  142. + FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
  143. + YT8531_SCR_CLK_SRC_REF_25M);
  144. + break;
  145. + case YTPHY_DTS_OUTPUT_CLK_125M:
  146. + mask = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_SRC_MASK |
  147. + YT8531_SCR_CLK_FRE_SEL_125M;
  148. + val = YT8531_SCR_SYNCE_ENABLE | YT8531_SCR_CLK_FRE_SEL_125M |
  149. + FIELD_PREP(YT8531_SCR_CLK_SRC_MASK,
  150. + YT8531_SCR_CLK_SRC_PLL_125M);
  151. + break;
  152. + default:
  153. + phydev_warn(phydev, "Freq err:%u\n", freq);
  154. + return -EINVAL;
  155. + }
  156. +
  157. + return ytphy_modify_ext_with_lock(phydev, YTPHY_SYNCE_CFG_REG, mask,
  158. + val);
  159. +}
  160. +
  161. /**
  162. * ytphy_utp_read_lpa() - read LPA then setup lp_advertising for utp
  163. * @phydev: a pointer to a &struct phy_device
  164. @@ -1387,6 +1491,94 @@ err_restore_page:
  165. return phy_restore_page(phydev, old_page, ret);
  166. }
  167. +static int yt8531_config_init(struct phy_device *phydev)
  168. +{
  169. + struct device_node *node = phydev->mdio.dev.of_node;
  170. + int ret;
  171. +
  172. + ret = ytphy_rgmii_clk_delay_config_with_lock(phydev);
  173. + if (ret < 0)
  174. + return ret;
  175. +
  176. + if (of_property_read_bool(node, "motorcomm,auto-sleep-disabled")) {
  177. + /* disable auto sleep */
  178. + ret = ytphy_modify_ext_with_lock(phydev,
  179. + YT8521_EXTREG_SLEEP_CONTROL1_REG,
  180. + YT8521_ESC1R_SLEEP_SW, 0);
  181. + if (ret < 0)
  182. + return ret;
  183. + }
  184. +
  185. + if (of_property_read_bool(node, "motorcomm,keep-pll-enabled")) {
  186. + /* enable RXC clock when no wire plug */
  187. + ret = ytphy_modify_ext_with_lock(phydev,
  188. + YT8521_CLOCK_GATING_REG,
  189. + YT8521_CGR_RX_CLK_EN, 0);
  190. + if (ret < 0)
  191. + return ret;
  192. + }
  193. +
  194. + return 0;
  195. +}
  196. +
  197. +/**
  198. + * yt8531_link_change_notify() - Adjust the tx clock direction according to
  199. + * the current speed and dts config.
  200. + * @phydev: a pointer to a &struct phy_device
  201. + *
  202. + * NOTE: This function is only used to adapt to VF2 with JH7110 SoC. Please
  203. + * keep "motorcomm,tx-clk-adj-enabled" not exist in dts when the soc is not
  204. + * JH7110.
  205. + */
  206. +static void yt8531_link_change_notify(struct phy_device *phydev)
  207. +{
  208. + struct device_node *node = phydev->mdio.dev.of_node;
  209. + bool tx_clk_adj_enabled = false;
  210. + bool tx_clk_1000_inverted;
  211. + bool tx_clk_100_inverted;
  212. + bool tx_clk_10_inverted;
  213. + u16 val = 0;
  214. + int ret;
  215. +
  216. + if (of_property_read_bool(node, "motorcomm,tx-clk-adj-enabled"))
  217. + tx_clk_adj_enabled = true;
  218. +
  219. + if (!tx_clk_adj_enabled)
  220. + return;
  221. +
  222. + if (of_property_read_bool(node, "motorcomm,tx-clk-10-inverted"))
  223. + tx_clk_10_inverted = true;
  224. + if (of_property_read_bool(node, "motorcomm,tx-clk-100-inverted"))
  225. + tx_clk_100_inverted = true;
  226. + if (of_property_read_bool(node, "motorcomm,tx-clk-1000-inverted"))
  227. + tx_clk_1000_inverted = true;
  228. +
  229. + if (phydev->speed < 0)
  230. + return;
  231. +
  232. + switch (phydev->speed) {
  233. + case SPEED_1000:
  234. + if (tx_clk_1000_inverted)
  235. + val = YT8521_RC1R_TX_CLK_SEL_INVERTED;
  236. + break;
  237. + case SPEED_100:
  238. + if (tx_clk_100_inverted)
  239. + val = YT8521_RC1R_TX_CLK_SEL_INVERTED;
  240. + break;
  241. + case SPEED_10:
  242. + if (tx_clk_10_inverted)
  243. + val = YT8521_RC1R_TX_CLK_SEL_INVERTED;
  244. + break;
  245. + default:
  246. + return;
  247. + }
  248. +
  249. + ret = ytphy_modify_ext_with_lock(phydev, YT8521_RGMII_CONFIG1_REG,
  250. + YT8521_RC1R_TX_CLK_SEL_INVERTED, val);
  251. + if (ret < 0)
  252. + phydev_warn(phydev, "Modify TX_CLK_SEL err:%d\n", ret);
  253. +}
  254. +
  255. /**
  256. * yt8521_prepare_fiber_features() - A small helper function that setup
  257. * fiber's features.
  258. @@ -1970,6 +2162,17 @@ static struct phy_driver motorcomm_phy_d
  259. .resume = yt8521_resume,
  260. },
  261. {
  262. + PHY_ID_MATCH_EXACT(PHY_ID_YT8531),
  263. + .name = "YT8531 Gigabit Ethernet",
  264. + .probe = yt8531_probe,
  265. + .config_init = yt8531_config_init,
  266. + .suspend = genphy_suspend,
  267. + .resume = genphy_resume,
  268. + .get_wol = ytphy_get_wol,
  269. + .set_wol = yt8531_set_wol,
  270. + .link_change_notify = yt8531_link_change_notify,
  271. + },
  272. + {
  273. PHY_ID_MATCH_EXACT(PHY_ID_YT8531S),
  274. .name = "YT8531S Gigabit Ethernet",
  275. .get_features = yt8521_get_features,
  276. @@ -1990,7 +2193,7 @@ static struct phy_driver motorcomm_phy_d
  277. module_phy_driver(motorcomm_phy_drvs);
  278. -MODULE_DESCRIPTION("Motorcomm 8511/8521/8531S PHY driver");
  279. +MODULE_DESCRIPTION("Motorcomm 8511/8521/8531/8531S PHY driver");
  280. MODULE_AUTHOR("Peter Geis");
  281. MODULE_AUTHOR("Frank");
  282. MODULE_LICENSE("GPL");
  283. @@ -1998,6 +2201,7 @@ MODULE_LICENSE("GPL");
  284. static const struct mdio_device_id __maybe_unused motorcomm_tbl[] = {
  285. { PHY_ID_MATCH_EXACT(PHY_ID_YT8511) },
  286. { PHY_ID_MATCH_EXACT(PHY_ID_YT8521) },
  287. + { PHY_ID_MATCH_EXACT(PHY_ID_YT8531) },
  288. { PHY_ID_MATCH_EXACT(PHY_ID_YT8531S) },
  289. { /* sentinel */ }
  290. };