dp83tg720.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Driver for the Texas Instruments DP83TG720 PHY
  3. * Copyright (c) 2023 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/ethtool_netlink.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/phy.h>
  10. #include "open_alliance_helpers.h"
  11. #define DP83TG720S_PHY_ID 0x2000a284
  12. /* MDIO_MMD_VEND2 registers */
  13. #define DP83TG720S_MII_REG_10 0x10
  14. #define DP83TG720S_STS_MII_INT BIT(7)
  15. #define DP83TG720S_LINK_STATUS BIT(0)
  16. /* TDR Configuration Register (0x1E) */
  17. #define DP83TG720S_TDR_CFG 0x1e
  18. /* 1b = TDR start, 0b = No TDR */
  19. #define DP83TG720S_TDR_START BIT(15)
  20. /* 1b = TDR auto on link down, 0b = Manual TDR start */
  21. #define DP83TG720S_CFG_TDR_AUTO_RUN BIT(14)
  22. /* 1b = TDR done, 0b = TDR in progress */
  23. #define DP83TG720S_TDR_DONE BIT(1)
  24. /* 1b = TDR fail, 0b = TDR success */
  25. #define DP83TG720S_TDR_FAIL BIT(0)
  26. #define DP83TG720S_PHY_RESET 0x1f
  27. #define DP83TG720S_HW_RESET BIT(15)
  28. #define DP83TG720S_LPS_CFG3 0x18c
  29. /* Power modes are documented as bit fields but used as values */
  30. /* Power Mode 0 is Normal mode */
  31. #define DP83TG720S_LPS_CFG3_PWR_MODE_0 BIT(0)
  32. /* Open Aliance 1000BaseT1 compatible HDD.TDR Fault Status Register */
  33. #define DP83TG720S_TDR_FAULT_STATUS 0x30f
  34. /* Register 0x0301: TDR Configuration 2 */
  35. #define DP83TG720S_TDR_CFG2 0x301
  36. /* Register 0x0303: TDR Configuration 3 */
  37. #define DP83TG720S_TDR_CFG3 0x303
  38. /* Register 0x0304: TDR Configuration 4 */
  39. #define DP83TG720S_TDR_CFG4 0x304
  40. /* Register 0x0405: Unknown Register */
  41. #define DP83TG720S_UNKNOWN_0405 0x405
  42. /* Register 0x0576: TDR Master Link Down Control */
  43. #define DP83TG720S_TDR_MASTER_LINK_DOWN 0x576
  44. #define DP83TG720S_RGMII_DELAY_CTRL 0x602
  45. /* In RGMII mode, Enable or disable the internal delay for RXD */
  46. #define DP83TG720S_RGMII_RX_CLK_SEL BIT(1)
  47. /* In RGMII mode, Enable or disable the internal delay for TXD */
  48. #define DP83TG720S_RGMII_TX_CLK_SEL BIT(0)
  49. /* Register 0x083F: Unknown Register */
  50. #define DP83TG720S_UNKNOWN_083F 0x83f
  51. #define DP83TG720S_SQI_REG_1 0x871
  52. #define DP83TG720S_SQI_OUT_WORST GENMASK(7, 5)
  53. #define DP83TG720S_SQI_OUT GENMASK(3, 1)
  54. #define DP83TG720_SQI_MAX 7
  55. /**
  56. * dp83tg720_cable_test_start - Start the cable test for the DP83TG720 PHY.
  57. * @phydev: Pointer to the phy_device structure.
  58. *
  59. * This sequence is based on the documented procedure for the DP83TG720 PHY.
  60. *
  61. * Returns: 0 on success, a negative error code on failure.
  62. */
  63. static int dp83tg720_cable_test_start(struct phy_device *phydev)
  64. {
  65. int ret;
  66. /* Initialize the PHY to run the TDR test as described in the
  67. * "DP83TG720S-Q1: Configuring for Open Alliance Specification
  68. * Compliance (Rev. B)" application note.
  69. * Most of the registers are not documented. Some of register names
  70. * are guessed by comparing the register offsets with the DP83TD510E.
  71. */
  72. /* Force master link down */
  73. ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2,
  74. DP83TG720S_TDR_MASTER_LINK_DOWN, 0x0400);
  75. if (ret)
  76. return ret;
  77. ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_TDR_CFG2,
  78. 0xa008);
  79. if (ret)
  80. return ret;
  81. ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_TDR_CFG3,
  82. 0x0928);
  83. if (ret)
  84. return ret;
  85. ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_TDR_CFG4,
  86. 0x0004);
  87. if (ret)
  88. return ret;
  89. ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_UNKNOWN_0405,
  90. 0x6400);
  91. if (ret)
  92. return ret;
  93. ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_UNKNOWN_083F,
  94. 0x3003);
  95. if (ret)
  96. return ret;
  97. /* Start the TDR */
  98. ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_TDR_CFG,
  99. DP83TG720S_TDR_START);
  100. if (ret)
  101. return ret;
  102. return 0;
  103. }
  104. /**
  105. * dp83tg720_cable_test_get_status - Get the status of the cable test for the
  106. * DP83TG720 PHY.
  107. * @phydev: Pointer to the phy_device structure.
  108. * @finished: Pointer to a boolean that indicates whether the test is finished.
  109. *
  110. * The function sets the @finished flag to true if the test is complete.
  111. *
  112. * Returns: 0 on success or a negative error code on failure.
  113. */
  114. static int dp83tg720_cable_test_get_status(struct phy_device *phydev,
  115. bool *finished)
  116. {
  117. int ret, stat;
  118. *finished = false;
  119. /* Read the TDR status */
  120. ret = phy_read_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_TDR_CFG);
  121. if (ret < 0)
  122. return ret;
  123. /* Check if the TDR test is done */
  124. if (!(ret & DP83TG720S_TDR_DONE))
  125. return 0;
  126. /* Check for TDR test failure */
  127. if (!(ret & DP83TG720S_TDR_FAIL)) {
  128. int location;
  129. /* Read fault status */
  130. ret = phy_read_mmd(phydev, MDIO_MMD_VEND2,
  131. DP83TG720S_TDR_FAULT_STATUS);
  132. if (ret < 0)
  133. return ret;
  134. /* Get fault type */
  135. stat = oa_1000bt1_get_ethtool_cable_result_code(ret);
  136. /* Determine fault location */
  137. location = oa_1000bt1_get_tdr_distance(ret);
  138. if (location > 0)
  139. ethnl_cable_test_fault_length(phydev,
  140. ETHTOOL_A_CABLE_PAIR_A,
  141. location);
  142. } else {
  143. /* Active link partner or other issues */
  144. stat = ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
  145. }
  146. *finished = true;
  147. ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A, stat);
  148. return phy_init_hw(phydev);
  149. }
  150. static int dp83tg720_config_aneg(struct phy_device *phydev)
  151. {
  152. int ret;
  153. /* Autoneg is not supported and this PHY supports only one speed.
  154. * We need to care only about master/slave configuration if it was
  155. * changed by user.
  156. */
  157. ret = genphy_c45_pma_baset1_setup_master_slave(phydev);
  158. if (ret)
  159. return ret;
  160. /* Re-read role configuration to make changes visible even if
  161. * the link is in administrative down state.
  162. */
  163. return genphy_c45_pma_baset1_read_master_slave(phydev);
  164. }
  165. static int dp83tg720_read_status(struct phy_device *phydev)
  166. {
  167. u16 phy_sts;
  168. int ret;
  169. phydev->pause = 0;
  170. phydev->asym_pause = 0;
  171. /* Most of Clause 45 registers are not present, so we can't use
  172. * genphy_c45_read_status() here.
  173. */
  174. phy_sts = phy_read(phydev, DP83TG720S_MII_REG_10);
  175. phydev->link = !!(phy_sts & DP83TG720S_LINK_STATUS);
  176. if (!phydev->link) {
  177. /* According to the "DP83TC81x, DP83TG72x Software
  178. * Implementation Guide", the PHY needs to be reset after a
  179. * link loss or if no link is created after at least 100ms.
  180. *
  181. * Currently we are polling with the PHY_STATE_TIME (1000ms)
  182. * interval, which is still enough for not automotive use cases.
  183. */
  184. ret = phy_init_hw(phydev);
  185. if (ret)
  186. return ret;
  187. /* After HW reset we need to restore master/slave configuration.
  188. * genphy_c45_pma_baset1_read_master_slave() call will be done
  189. * by the dp83tg720_config_aneg() function.
  190. */
  191. ret = dp83tg720_config_aneg(phydev);
  192. if (ret)
  193. return ret;
  194. phydev->speed = SPEED_UNKNOWN;
  195. phydev->duplex = DUPLEX_UNKNOWN;
  196. } else {
  197. /* PMA/PMD control 1 register (Register 1.0) is present, but it
  198. * doesn't contain the link speed information.
  199. * So genphy_c45_read_pma() can't be used here.
  200. */
  201. ret = genphy_c45_pma_baset1_read_master_slave(phydev);
  202. if (ret)
  203. return ret;
  204. phydev->duplex = DUPLEX_FULL;
  205. phydev->speed = SPEED_1000;
  206. }
  207. return 0;
  208. }
  209. static int dp83tg720_get_sqi(struct phy_device *phydev)
  210. {
  211. int ret;
  212. if (!phydev->link)
  213. return 0;
  214. ret = phy_read_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_SQI_REG_1);
  215. if (ret < 0)
  216. return ret;
  217. return FIELD_GET(DP83TG720S_SQI_OUT, ret);
  218. }
  219. static int dp83tg720_get_sqi_max(struct phy_device *phydev)
  220. {
  221. return DP83TG720_SQI_MAX;
  222. }
  223. static int dp83tg720_config_rgmii_delay(struct phy_device *phydev)
  224. {
  225. u16 rgmii_delay_mask;
  226. u16 rgmii_delay = 0;
  227. switch (phydev->interface) {
  228. case PHY_INTERFACE_MODE_RGMII:
  229. rgmii_delay = 0;
  230. break;
  231. case PHY_INTERFACE_MODE_RGMII_ID:
  232. rgmii_delay = DP83TG720S_RGMII_RX_CLK_SEL |
  233. DP83TG720S_RGMII_TX_CLK_SEL;
  234. break;
  235. case PHY_INTERFACE_MODE_RGMII_RXID:
  236. rgmii_delay = DP83TG720S_RGMII_RX_CLK_SEL;
  237. break;
  238. case PHY_INTERFACE_MODE_RGMII_TXID:
  239. rgmii_delay = DP83TG720S_RGMII_TX_CLK_SEL;
  240. break;
  241. default:
  242. return 0;
  243. }
  244. rgmii_delay_mask = DP83TG720S_RGMII_RX_CLK_SEL |
  245. DP83TG720S_RGMII_TX_CLK_SEL;
  246. return phy_modify_mmd(phydev, MDIO_MMD_VEND2,
  247. DP83TG720S_RGMII_DELAY_CTRL, rgmii_delay_mask,
  248. rgmii_delay);
  249. }
  250. static int dp83tg720_config_init(struct phy_device *phydev)
  251. {
  252. int ret;
  253. /* Software Restart is not enough to recover from a link failure.
  254. * Using Hardware Reset instead.
  255. */
  256. ret = phy_write(phydev, DP83TG720S_PHY_RESET, DP83TG720S_HW_RESET);
  257. if (ret)
  258. return ret;
  259. /* Wait until MDC can be used again.
  260. * The wait value of one 1ms is documented in "DP83TG720S-Q1 1000BASE-T1
  261. * Automotive Ethernet PHY with SGMII and RGMII" datasheet.
  262. */
  263. usleep_range(1000, 2000);
  264. if (phy_interface_is_rgmii(phydev)) {
  265. ret = dp83tg720_config_rgmii_delay(phydev);
  266. if (ret)
  267. return ret;
  268. }
  269. /* In case the PHY is bootstrapped in managed mode, we need to
  270. * wake it.
  271. */
  272. ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, DP83TG720S_LPS_CFG3,
  273. DP83TG720S_LPS_CFG3_PWR_MODE_0);
  274. if (ret)
  275. return ret;
  276. /* Make role configuration visible for ethtool on init and after
  277. * rest.
  278. */
  279. return genphy_c45_pma_baset1_read_master_slave(phydev);
  280. }
  281. static struct phy_driver dp83tg720_driver[] = {
  282. {
  283. PHY_ID_MATCH_MODEL(DP83TG720S_PHY_ID),
  284. .name = "TI DP83TG720S",
  285. .flags = PHY_POLL_CABLE_TEST,
  286. .config_aneg = dp83tg720_config_aneg,
  287. .read_status = dp83tg720_read_status,
  288. .get_features = genphy_c45_pma_read_ext_abilities,
  289. .config_init = dp83tg720_config_init,
  290. .get_sqi = dp83tg720_get_sqi,
  291. .get_sqi_max = dp83tg720_get_sqi_max,
  292. .cable_test_start = dp83tg720_cable_test_start,
  293. .cable_test_get_status = dp83tg720_cable_test_get_status,
  294. .suspend = genphy_suspend,
  295. .resume = genphy_resume,
  296. } };
  297. module_phy_driver(dp83tg720_driver);
  298. static struct mdio_device_id __maybe_unused dp83tg720_tbl[] = {
  299. { PHY_ID_MATCH_MODEL(DP83TG720S_PHY_ID) },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(mdio, dp83tg720_tbl);
  303. MODULE_DESCRIPTION("Texas Instruments DP83TG720S PHY driver");
  304. MODULE_AUTHOR("Oleksij Rempel <kernel@pengutronix.de>");
  305. MODULE_LICENSE("GPL");