microchip_t1.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Microchip Technology
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/delay.h>
  6. #include <linux/mii.h>
  7. #include <linux/phy.h>
  8. #include <linux/ethtool.h>
  9. #include <linux/ethtool_netlink.h>
  10. #include <linux/bitfield.h>
  11. #define PHY_ID_LAN87XX 0x0007c150
  12. #define PHY_ID_LAN937X 0x0007c180
  13. /* External Register Control Register */
  14. #define LAN87XX_EXT_REG_CTL (0x14)
  15. #define LAN87XX_EXT_REG_CTL_RD_CTL (0x1000)
  16. #define LAN87XX_EXT_REG_CTL_WR_CTL (0x0800)
  17. #define LAN87XX_REG_BANK_SEL_MASK GENMASK(10, 8)
  18. #define LAN87XX_REG_ADDR_MASK GENMASK(7, 0)
  19. /* External Register Read Data Register */
  20. #define LAN87XX_EXT_REG_RD_DATA (0x15)
  21. /* External Register Write Data Register */
  22. #define LAN87XX_EXT_REG_WR_DATA (0x16)
  23. /* Interrupt Source Register */
  24. #define LAN87XX_INTERRUPT_SOURCE (0x18)
  25. #define LAN87XX_INTERRUPT_SOURCE_2 (0x08)
  26. /* Interrupt Mask Register */
  27. #define LAN87XX_INTERRUPT_MASK (0x19)
  28. #define LAN87XX_MASK_LINK_UP (0x0004)
  29. #define LAN87XX_MASK_LINK_DOWN (0x0002)
  30. #define LAN87XX_INTERRUPT_MASK_2 (0x09)
  31. #define LAN87XX_MASK_COMM_RDY BIT(10)
  32. /* MISC Control 1 Register */
  33. #define LAN87XX_CTRL_1 (0x11)
  34. #define LAN87XX_MASK_RGMII_TXC_DLY_EN (0x4000)
  35. #define LAN87XX_MASK_RGMII_RXC_DLY_EN (0x2000)
  36. /* phyaccess nested types */
  37. #define PHYACC_ATTR_MODE_READ 0
  38. #define PHYACC_ATTR_MODE_WRITE 1
  39. #define PHYACC_ATTR_MODE_MODIFY 2
  40. #define PHYACC_ATTR_MODE_POLL 3
  41. #define PHYACC_ATTR_BANK_SMI 0
  42. #define PHYACC_ATTR_BANK_MISC 1
  43. #define PHYACC_ATTR_BANK_PCS 2
  44. #define PHYACC_ATTR_BANK_AFE 3
  45. #define PHYACC_ATTR_BANK_DSP 4
  46. #define PHYACC_ATTR_BANK_MAX 7
  47. /* measurement defines */
  48. #define LAN87XX_CABLE_TEST_OK 0
  49. #define LAN87XX_CABLE_TEST_OPEN 1
  50. #define LAN87XX_CABLE_TEST_SAME_SHORT 2
  51. /* T1 Registers */
  52. #define T1_AFE_PORT_CFG1_REG 0x0B
  53. #define T1_POWER_DOWN_CONTROL_REG 0x1A
  54. #define T1_SLV_FD_MULT_CFG_REG 0x18
  55. #define T1_CDR_CFG_PRE_LOCK_REG 0x05
  56. #define T1_CDR_CFG_POST_LOCK_REG 0x06
  57. #define T1_LCK_STG2_MUFACT_CFG_REG 0x1A
  58. #define T1_LCK_STG3_MUFACT_CFG_REG 0x1B
  59. #define T1_POST_LCK_MUFACT_CFG_REG 0x1C
  60. #define T1_TX_RX_FIFO_CFG_REG 0x02
  61. #define T1_TX_LPF_FIR_CFG_REG 0x55
  62. #define T1_COEF_CLK_PWR_DN_CFG 0x04
  63. #define T1_COEF_RW_CTL_CFG 0x0D
  64. #define T1_SQI_CONFIG_REG 0x2E
  65. #define T1_SQI_CONFIG2_REG 0x4A
  66. #define T1_DCQ_SQI_REG 0xC3
  67. #define T1_DCQ_SQI_MSK GENMASK(3, 1)
  68. #define T1_MDIO_CONTROL2_REG 0x10
  69. #define T1_INTERRUPT_SOURCE_REG 0x18
  70. #define T1_INTERRUPT2_SOURCE_REG 0x08
  71. #define T1_EQ_FD_STG1_FRZ_CFG 0x69
  72. #define T1_EQ_FD_STG2_FRZ_CFG 0x6A
  73. #define T1_EQ_FD_STG3_FRZ_CFG 0x6B
  74. #define T1_EQ_FD_STG4_FRZ_CFG 0x6C
  75. #define T1_EQ_WT_FD_LCK_FRZ_CFG 0x6D
  76. #define T1_PST_EQ_LCK_STG1_FRZ_CFG 0x6E
  77. #define T1_MODE_STAT_REG 0x11
  78. #define T1_LINK_UP_MSK BIT(0)
  79. /* SQI defines */
  80. #define LAN87XX_MAX_SQI 0x07
  81. #define DRIVER_AUTHOR "Nisar Sayed <[email protected]>"
  82. #define DRIVER_DESC "Microchip LAN87XX/LAN937x T1 PHY driver"
  83. struct access_ereg_val {
  84. u8 mode;
  85. u8 bank;
  86. u8 offset;
  87. u16 val;
  88. u16 mask;
  89. };
  90. static int lan937x_dsp_workaround(struct phy_device *phydev, u16 ereg, u8 bank)
  91. {
  92. u8 prev_bank;
  93. int rc = 0;
  94. u16 val;
  95. mutex_lock(&phydev->lock);
  96. /* Read previous selected bank */
  97. rc = phy_read(phydev, LAN87XX_EXT_REG_CTL);
  98. if (rc < 0)
  99. goto out_unlock;
  100. /* store the prev_bank */
  101. prev_bank = FIELD_GET(LAN87XX_REG_BANK_SEL_MASK, rc);
  102. if (bank != prev_bank && bank == PHYACC_ATTR_BANK_DSP) {
  103. val = ereg & ~LAN87XX_REG_ADDR_MASK;
  104. val &= ~LAN87XX_EXT_REG_CTL_WR_CTL;
  105. val |= LAN87XX_EXT_REG_CTL_RD_CTL;
  106. /* access twice for DSP bank change,dummy access */
  107. rc = phy_write(phydev, LAN87XX_EXT_REG_CTL, val);
  108. }
  109. out_unlock:
  110. mutex_unlock(&phydev->lock);
  111. return rc;
  112. }
  113. static int access_ereg(struct phy_device *phydev, u8 mode, u8 bank,
  114. u8 offset, u16 val)
  115. {
  116. u16 ereg = 0;
  117. int rc = 0;
  118. if (mode > PHYACC_ATTR_MODE_WRITE || bank > PHYACC_ATTR_BANK_MAX)
  119. return -EINVAL;
  120. if (bank == PHYACC_ATTR_BANK_SMI) {
  121. if (mode == PHYACC_ATTR_MODE_WRITE)
  122. rc = phy_write(phydev, offset, val);
  123. else
  124. rc = phy_read(phydev, offset);
  125. return rc;
  126. }
  127. if (mode == PHYACC_ATTR_MODE_WRITE) {
  128. ereg = LAN87XX_EXT_REG_CTL_WR_CTL;
  129. rc = phy_write(phydev, LAN87XX_EXT_REG_WR_DATA, val);
  130. if (rc < 0)
  131. return rc;
  132. } else {
  133. ereg = LAN87XX_EXT_REG_CTL_RD_CTL;
  134. }
  135. ereg |= (bank << 8) | offset;
  136. /* DSP bank access workaround for lan937x */
  137. if (phydev->phy_id == PHY_ID_LAN937X) {
  138. rc = lan937x_dsp_workaround(phydev, ereg, bank);
  139. if (rc < 0)
  140. return rc;
  141. }
  142. rc = phy_write(phydev, LAN87XX_EXT_REG_CTL, ereg);
  143. if (rc < 0)
  144. return rc;
  145. if (mode == PHYACC_ATTR_MODE_READ)
  146. rc = phy_read(phydev, LAN87XX_EXT_REG_RD_DATA);
  147. return rc;
  148. }
  149. static int access_ereg_modify_changed(struct phy_device *phydev,
  150. u8 bank, u8 offset, u16 val, u16 mask)
  151. {
  152. int new = 0, rc = 0;
  153. if (bank > PHYACC_ATTR_BANK_MAX)
  154. return -EINVAL;
  155. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ, bank, offset, val);
  156. if (rc < 0)
  157. return rc;
  158. new = val | (rc & (mask ^ 0xFFFF));
  159. rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE, bank, offset, new);
  160. return rc;
  161. }
  162. static int access_smi_poll_timeout(struct phy_device *phydev,
  163. u8 offset, u16 mask, u16 clr)
  164. {
  165. int val;
  166. return phy_read_poll_timeout(phydev, offset, val, (val & mask) == clr,
  167. 150, 30000, true);
  168. }
  169. static int lan87xx_config_rgmii_delay(struct phy_device *phydev)
  170. {
  171. int rc;
  172. if (!phy_interface_is_rgmii(phydev))
  173. return 0;
  174. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  175. PHYACC_ATTR_BANK_MISC, LAN87XX_CTRL_1, 0);
  176. if (rc < 0)
  177. return rc;
  178. switch (phydev->interface) {
  179. case PHY_INTERFACE_MODE_RGMII:
  180. rc &= ~LAN87XX_MASK_RGMII_TXC_DLY_EN;
  181. rc &= ~LAN87XX_MASK_RGMII_RXC_DLY_EN;
  182. break;
  183. case PHY_INTERFACE_MODE_RGMII_ID:
  184. rc |= LAN87XX_MASK_RGMII_TXC_DLY_EN;
  185. rc |= LAN87XX_MASK_RGMII_RXC_DLY_EN;
  186. break;
  187. case PHY_INTERFACE_MODE_RGMII_RXID:
  188. rc &= ~LAN87XX_MASK_RGMII_TXC_DLY_EN;
  189. rc |= LAN87XX_MASK_RGMII_RXC_DLY_EN;
  190. break;
  191. case PHY_INTERFACE_MODE_RGMII_TXID:
  192. rc |= LAN87XX_MASK_RGMII_TXC_DLY_EN;
  193. rc &= ~LAN87XX_MASK_RGMII_RXC_DLY_EN;
  194. break;
  195. default:
  196. return 0;
  197. }
  198. return access_ereg(phydev, PHYACC_ATTR_MODE_WRITE,
  199. PHYACC_ATTR_BANK_MISC, LAN87XX_CTRL_1, rc);
  200. }
  201. static int lan87xx_phy_init(struct phy_device *phydev)
  202. {
  203. static const struct access_ereg_val init[] = {
  204. /* TXPD/TXAMP6 Configs */
  205. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_AFE,
  206. T1_AFE_PORT_CFG1_REG, 0x002D, 0 },
  207. /* HW_Init Hi and Force_ED */
  208. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
  209. T1_POWER_DOWN_CONTROL_REG, 0x0308, 0 },
  210. /* Equalizer Full Duplex Freeze - T1 Slave */
  211. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  212. T1_EQ_FD_STG1_FRZ_CFG, 0x0002, 0 },
  213. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  214. T1_EQ_FD_STG2_FRZ_CFG, 0x0002, 0 },
  215. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  216. T1_EQ_FD_STG3_FRZ_CFG, 0x0002, 0 },
  217. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  218. T1_EQ_FD_STG4_FRZ_CFG, 0x0002, 0 },
  219. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  220. T1_EQ_WT_FD_LCK_FRZ_CFG, 0x0002, 0 },
  221. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  222. T1_PST_EQ_LCK_STG1_FRZ_CFG, 0x0002, 0 },
  223. /* Slave Full Duplex Multi Configs */
  224. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  225. T1_SLV_FD_MULT_CFG_REG, 0x0D53, 0 },
  226. /* CDR Pre and Post Lock Configs */
  227. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  228. T1_CDR_CFG_PRE_LOCK_REG, 0x0AB2, 0 },
  229. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  230. T1_CDR_CFG_POST_LOCK_REG, 0x0AB3, 0 },
  231. /* Lock Stage 2-3 Multi Factor Config */
  232. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  233. T1_LCK_STG2_MUFACT_CFG_REG, 0x0AEA, 0 },
  234. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  235. T1_LCK_STG3_MUFACT_CFG_REG, 0x0AEB, 0 },
  236. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  237. T1_POST_LCK_MUFACT_CFG_REG, 0x0AEB, 0 },
  238. /* Pointer delay */
  239. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  240. T1_TX_RX_FIFO_CFG_REG, 0x1C00, 0 },
  241. /* Tx iir edits */
  242. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  243. T1_TX_LPF_FIR_CFG_REG, 0x1000, 0 },
  244. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  245. T1_TX_LPF_FIR_CFG_REG, 0x1861, 0 },
  246. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  247. T1_TX_LPF_FIR_CFG_REG, 0x1061, 0 },
  248. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  249. T1_TX_LPF_FIR_CFG_REG, 0x1922, 0 },
  250. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  251. T1_TX_LPF_FIR_CFG_REG, 0x1122, 0 },
  252. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  253. T1_TX_LPF_FIR_CFG_REG, 0x1983, 0 },
  254. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  255. T1_TX_LPF_FIR_CFG_REG, 0x1183, 0 },
  256. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  257. T1_TX_LPF_FIR_CFG_REG, 0x1944, 0 },
  258. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  259. T1_TX_LPF_FIR_CFG_REG, 0x1144, 0 },
  260. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  261. T1_TX_LPF_FIR_CFG_REG, 0x18c5, 0 },
  262. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  263. T1_TX_LPF_FIR_CFG_REG, 0x10c5, 0 },
  264. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  265. T1_TX_LPF_FIR_CFG_REG, 0x1846, 0 },
  266. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  267. T1_TX_LPF_FIR_CFG_REG, 0x1046, 0 },
  268. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  269. T1_TX_LPF_FIR_CFG_REG, 0x1807, 0 },
  270. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  271. T1_TX_LPF_FIR_CFG_REG, 0x1007, 0 },
  272. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  273. T1_TX_LPF_FIR_CFG_REG, 0x1808, 0 },
  274. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  275. T1_TX_LPF_FIR_CFG_REG, 0x1008, 0 },
  276. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  277. T1_TX_LPF_FIR_CFG_REG, 0x1809, 0 },
  278. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  279. T1_TX_LPF_FIR_CFG_REG, 0x1009, 0 },
  280. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  281. T1_TX_LPF_FIR_CFG_REG, 0x180A, 0 },
  282. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  283. T1_TX_LPF_FIR_CFG_REG, 0x100A, 0 },
  284. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  285. T1_TX_LPF_FIR_CFG_REG, 0x180B, 0 },
  286. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  287. T1_TX_LPF_FIR_CFG_REG, 0x100B, 0 },
  288. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  289. T1_TX_LPF_FIR_CFG_REG, 0x180C, 0 },
  290. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  291. T1_TX_LPF_FIR_CFG_REG, 0x100C, 0 },
  292. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  293. T1_TX_LPF_FIR_CFG_REG, 0x180D, 0 },
  294. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  295. T1_TX_LPF_FIR_CFG_REG, 0x100D, 0 },
  296. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  297. T1_TX_LPF_FIR_CFG_REG, 0x180E, 0 },
  298. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  299. T1_TX_LPF_FIR_CFG_REG, 0x100E, 0 },
  300. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  301. T1_TX_LPF_FIR_CFG_REG, 0x180F, 0 },
  302. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  303. T1_TX_LPF_FIR_CFG_REG, 0x100F, 0 },
  304. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  305. T1_TX_LPF_FIR_CFG_REG, 0x1810, 0 },
  306. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  307. T1_TX_LPF_FIR_CFG_REG, 0x1010, 0 },
  308. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  309. T1_TX_LPF_FIR_CFG_REG, 0x1811, 0 },
  310. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  311. T1_TX_LPF_FIR_CFG_REG, 0x1011, 0 },
  312. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  313. T1_TX_LPF_FIR_CFG_REG, 0x1000, 0 },
  314. /* Setup SQI measurement */
  315. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  316. T1_COEF_CLK_PWR_DN_CFG, 0x16d6, 0 },
  317. /* SQI enable */
  318. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  319. T1_SQI_CONFIG_REG, 0x9572, 0 },
  320. /* SQI select mode 5 */
  321. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  322. T1_SQI_CONFIG2_REG, 0x0001, 0 },
  323. /* Throws the first SQI reading */
  324. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
  325. T1_COEF_RW_CTL_CFG, 0x0301, 0 },
  326. { PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_DSP,
  327. T1_DCQ_SQI_REG, 0, 0 },
  328. /* Flag LPS and WUR as idle errors */
  329. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
  330. T1_MDIO_CONTROL2_REG, 0x0014, 0 },
  331. /* HW_Init toggle, undo force ED, TXPD off */
  332. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
  333. T1_POWER_DOWN_CONTROL_REG, 0x0200, 0 },
  334. /* Reset PCS to trigger hardware initialization */
  335. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
  336. T1_MDIO_CONTROL2_REG, 0x0094, 0 },
  337. /* Poll till Hardware is initialized */
  338. { PHYACC_ATTR_MODE_POLL, PHYACC_ATTR_BANK_SMI,
  339. T1_MDIO_CONTROL2_REG, 0x0080, 0 },
  340. /* Tx AMP - 0x06 */
  341. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_AFE,
  342. T1_AFE_PORT_CFG1_REG, 0x000C, 0 },
  343. /* Read INTERRUPT_SOURCE Register */
  344. { PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI,
  345. T1_INTERRUPT_SOURCE_REG, 0, 0 },
  346. /* Read INTERRUPT_SOURCE Register */
  347. { PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_MISC,
  348. T1_INTERRUPT2_SOURCE_REG, 0, 0 },
  349. /* HW_Init Hi */
  350. { PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
  351. T1_POWER_DOWN_CONTROL_REG, 0x0300, 0 },
  352. };
  353. int rc, i;
  354. /* phy Soft reset */
  355. rc = genphy_soft_reset(phydev);
  356. if (rc < 0)
  357. return rc;
  358. /* PHY Initialization */
  359. for (i = 0; i < ARRAY_SIZE(init); i++) {
  360. if (init[i].mode == PHYACC_ATTR_MODE_POLL &&
  361. init[i].bank == PHYACC_ATTR_BANK_SMI) {
  362. rc = access_smi_poll_timeout(phydev,
  363. init[i].offset,
  364. init[i].val,
  365. init[i].mask);
  366. } else {
  367. rc = access_ereg(phydev, init[i].mode, init[i].bank,
  368. init[i].offset, init[i].val);
  369. }
  370. if (rc < 0)
  371. return rc;
  372. }
  373. return lan87xx_config_rgmii_delay(phydev);
  374. }
  375. static int lan87xx_phy_config_intr(struct phy_device *phydev)
  376. {
  377. int rc, val = 0;
  378. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  379. /* clear all interrupt */
  380. rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, val);
  381. if (rc < 0)
  382. return rc;
  383. rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
  384. if (rc < 0)
  385. return rc;
  386. rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE,
  387. PHYACC_ATTR_BANK_MISC,
  388. LAN87XX_INTERRUPT_MASK_2, val);
  389. if (rc < 0)
  390. return rc;
  391. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  392. PHYACC_ATTR_BANK_MISC,
  393. LAN87XX_INTERRUPT_SOURCE_2, 0);
  394. if (rc < 0)
  395. return rc;
  396. /* enable link down and comm ready interrupt */
  397. val = LAN87XX_MASK_LINK_DOWN;
  398. rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, val);
  399. if (rc < 0)
  400. return rc;
  401. val = LAN87XX_MASK_COMM_RDY;
  402. rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE,
  403. PHYACC_ATTR_BANK_MISC,
  404. LAN87XX_INTERRUPT_MASK_2, val);
  405. } else {
  406. rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, val);
  407. if (rc < 0)
  408. return rc;
  409. rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
  410. if (rc < 0)
  411. return rc;
  412. rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE,
  413. PHYACC_ATTR_BANK_MISC,
  414. LAN87XX_INTERRUPT_MASK_2, val);
  415. if (rc < 0)
  416. return rc;
  417. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  418. PHYACC_ATTR_BANK_MISC,
  419. LAN87XX_INTERRUPT_SOURCE_2, 0);
  420. }
  421. return rc < 0 ? rc : 0;
  422. }
  423. static irqreturn_t lan87xx_handle_interrupt(struct phy_device *phydev)
  424. {
  425. int irq_status;
  426. irq_status = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  427. PHYACC_ATTR_BANK_MISC,
  428. LAN87XX_INTERRUPT_SOURCE_2, 0);
  429. if (irq_status < 0) {
  430. phy_error(phydev);
  431. return IRQ_NONE;
  432. }
  433. irq_status = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
  434. if (irq_status < 0) {
  435. phy_error(phydev);
  436. return IRQ_NONE;
  437. }
  438. if (irq_status == 0)
  439. return IRQ_NONE;
  440. phy_trigger_machine(phydev);
  441. return IRQ_HANDLED;
  442. }
  443. static int lan87xx_config_init(struct phy_device *phydev)
  444. {
  445. int rc = lan87xx_phy_init(phydev);
  446. return rc < 0 ? rc : 0;
  447. }
  448. static int microchip_cable_test_start_common(struct phy_device *phydev)
  449. {
  450. int bmcr, bmsr, ret;
  451. /* If auto-negotiation is enabled, but not complete, the cable
  452. * test never completes. So disable auto-neg.
  453. */
  454. bmcr = phy_read(phydev, MII_BMCR);
  455. if (bmcr < 0)
  456. return bmcr;
  457. bmsr = phy_read(phydev, MII_BMSR);
  458. if (bmsr < 0)
  459. return bmsr;
  460. if (bmcr & BMCR_ANENABLE) {
  461. ret = phy_modify(phydev, MII_BMCR, BMCR_ANENABLE, 0);
  462. if (ret < 0)
  463. return ret;
  464. ret = genphy_soft_reset(phydev);
  465. if (ret < 0)
  466. return ret;
  467. }
  468. /* If the link is up, allow it some time to go down */
  469. if (bmsr & BMSR_LSTATUS)
  470. msleep(1500);
  471. return 0;
  472. }
  473. static int lan87xx_cable_test_start(struct phy_device *phydev)
  474. {
  475. static const struct access_ereg_val cable_test[] = {
  476. /* min wait */
  477. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP, 93,
  478. 0, 0},
  479. /* max wait */
  480. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP, 94,
  481. 10, 0},
  482. /* pulse cycle */
  483. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP, 95,
  484. 90, 0},
  485. /* cable diag thresh */
  486. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP, 92,
  487. 60, 0},
  488. /* max gain */
  489. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP, 79,
  490. 31, 0},
  491. /* clock align for each iteration */
  492. {PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_DSP, 55,
  493. 0, 0x0038},
  494. /* max cycle wait config */
  495. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP, 94,
  496. 70, 0},
  497. /* start cable diag*/
  498. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP, 90,
  499. 1, 0},
  500. };
  501. int rc, i;
  502. rc = microchip_cable_test_start_common(phydev);
  503. if (rc < 0)
  504. return rc;
  505. /* start cable diag */
  506. /* check if part is alive - if not, return diagnostic error */
  507. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI,
  508. 0x00, 0);
  509. if (rc < 0)
  510. return rc;
  511. /* master/slave specific configs */
  512. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI,
  513. 0x0A, 0);
  514. if (rc < 0)
  515. return rc;
  516. if ((rc & 0x4000) != 0x4000) {
  517. /* DUT is Slave */
  518. rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_AFE,
  519. 0x0E, 0x5, 0x7);
  520. if (rc < 0)
  521. return rc;
  522. rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
  523. 0x1A, 0x8, 0x8);
  524. if (rc < 0)
  525. return rc;
  526. } else {
  527. /* DUT is Master */
  528. rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
  529. 0x10, 0x8, 0x40);
  530. if (rc < 0)
  531. return rc;
  532. }
  533. for (i = 0; i < ARRAY_SIZE(cable_test); i++) {
  534. if (cable_test[i].mode == PHYACC_ATTR_MODE_MODIFY) {
  535. rc = access_ereg_modify_changed(phydev,
  536. cable_test[i].bank,
  537. cable_test[i].offset,
  538. cable_test[i].val,
  539. cable_test[i].mask);
  540. /* wait 50ms */
  541. msleep(50);
  542. } else {
  543. rc = access_ereg(phydev, cable_test[i].mode,
  544. cable_test[i].bank,
  545. cable_test[i].offset,
  546. cable_test[i].val);
  547. }
  548. if (rc < 0)
  549. return rc;
  550. }
  551. /* cable diag started */
  552. return 0;
  553. }
  554. static int lan87xx_cable_test_report_trans(u32 result)
  555. {
  556. switch (result) {
  557. case LAN87XX_CABLE_TEST_OK:
  558. return ETHTOOL_A_CABLE_RESULT_CODE_OK;
  559. case LAN87XX_CABLE_TEST_OPEN:
  560. return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
  561. case LAN87XX_CABLE_TEST_SAME_SHORT:
  562. return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
  563. default:
  564. /* DIAGNOSTIC_ERROR */
  565. return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
  566. }
  567. }
  568. static int lan87xx_cable_test_report(struct phy_device *phydev)
  569. {
  570. int pos_peak_cycle = 0, pos_peak_in_phases = 0, pos_peak_phase = 0;
  571. int neg_peak_cycle = 0, neg_peak_in_phases = 0, neg_peak_phase = 0;
  572. int noise_margin = 20, time_margin = 89, jitter_var = 30;
  573. int min_time_diff = 96, max_time_diff = 96 + time_margin;
  574. bool fault = false, check_a = false, check_b = false;
  575. int gain_idx = 0, pos_peak = 0, neg_peak = 0;
  576. int pos_peak_time = 0, neg_peak_time = 0;
  577. int pos_peak_in_phases_hybrid = 0;
  578. int detect = -1;
  579. gain_idx = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  580. PHYACC_ATTR_BANK_DSP, 151, 0);
  581. /* read non-hybrid results */
  582. pos_peak = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  583. PHYACC_ATTR_BANK_DSP, 153, 0);
  584. neg_peak = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  585. PHYACC_ATTR_BANK_DSP, 154, 0);
  586. pos_peak_time = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  587. PHYACC_ATTR_BANK_DSP, 156, 0);
  588. neg_peak_time = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  589. PHYACC_ATTR_BANK_DSP, 157, 0);
  590. pos_peak_cycle = (pos_peak_time >> 7) & 0x7F;
  591. /* calculate non-hybrid values */
  592. pos_peak_phase = pos_peak_time & 0x7F;
  593. pos_peak_in_phases = (pos_peak_cycle * 96) + pos_peak_phase;
  594. neg_peak_cycle = (neg_peak_time >> 7) & 0x7F;
  595. neg_peak_phase = neg_peak_time & 0x7F;
  596. neg_peak_in_phases = (neg_peak_cycle * 96) + neg_peak_phase;
  597. /* process values */
  598. check_a =
  599. ((pos_peak_in_phases - neg_peak_in_phases) >= min_time_diff) &&
  600. ((pos_peak_in_phases - neg_peak_in_phases) < max_time_diff) &&
  601. pos_peak_in_phases_hybrid < pos_peak_in_phases &&
  602. (pos_peak_in_phases_hybrid < (neg_peak_in_phases + jitter_var));
  603. check_b =
  604. ((neg_peak_in_phases - pos_peak_in_phases) >= min_time_diff) &&
  605. ((neg_peak_in_phases - pos_peak_in_phases) < max_time_diff) &&
  606. pos_peak_in_phases_hybrid < neg_peak_in_phases &&
  607. (pos_peak_in_phases_hybrid < (pos_peak_in_phases + jitter_var));
  608. if (pos_peak_in_phases > neg_peak_in_phases && check_a)
  609. detect = 2;
  610. else if ((neg_peak_in_phases > pos_peak_in_phases) && check_b)
  611. detect = 1;
  612. if (pos_peak > noise_margin && neg_peak > noise_margin &&
  613. gain_idx >= 0) {
  614. if (detect == 1 || detect == 2)
  615. fault = true;
  616. }
  617. if (!fault)
  618. detect = 0;
  619. ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
  620. lan87xx_cable_test_report_trans(detect));
  621. return 0;
  622. }
  623. static int lan87xx_cable_test_get_status(struct phy_device *phydev,
  624. bool *finished)
  625. {
  626. int rc = 0;
  627. *finished = false;
  628. /* check if cable diag was finished */
  629. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_DSP,
  630. 90, 0);
  631. if (rc < 0)
  632. return rc;
  633. if ((rc & 2) == 2) {
  634. /* stop cable diag*/
  635. rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE,
  636. PHYACC_ATTR_BANK_DSP,
  637. 90, 0);
  638. if (rc < 0)
  639. return rc;
  640. *finished = true;
  641. return lan87xx_cable_test_report(phydev);
  642. }
  643. return 0;
  644. }
  645. static int lan87xx_read_status(struct phy_device *phydev)
  646. {
  647. int rc = 0;
  648. rc = phy_read(phydev, T1_MODE_STAT_REG);
  649. if (rc < 0)
  650. return rc;
  651. if (rc & T1_LINK_UP_MSK)
  652. phydev->link = 1;
  653. else
  654. phydev->link = 0;
  655. phydev->speed = SPEED_UNKNOWN;
  656. phydev->duplex = DUPLEX_UNKNOWN;
  657. phydev->pause = 0;
  658. phydev->asym_pause = 0;
  659. rc = genphy_read_master_slave(phydev);
  660. if (rc < 0)
  661. return rc;
  662. rc = genphy_read_status_fixed(phydev);
  663. if (rc < 0)
  664. return rc;
  665. return rc;
  666. }
  667. static int lan87xx_config_aneg(struct phy_device *phydev)
  668. {
  669. u16 ctl = 0;
  670. switch (phydev->master_slave_set) {
  671. case MASTER_SLAVE_CFG_MASTER_FORCE:
  672. ctl |= CTL1000_AS_MASTER;
  673. break;
  674. case MASTER_SLAVE_CFG_SLAVE_FORCE:
  675. break;
  676. case MASTER_SLAVE_CFG_UNKNOWN:
  677. case MASTER_SLAVE_CFG_UNSUPPORTED:
  678. return 0;
  679. default:
  680. phydev_warn(phydev, "Unsupported Master/Slave mode\n");
  681. return -EOPNOTSUPP;
  682. }
  683. return phy_modify_changed(phydev, MII_CTRL1000, CTL1000_AS_MASTER, ctl);
  684. }
  685. static int lan87xx_get_sqi(struct phy_device *phydev)
  686. {
  687. u8 sqi_value = 0;
  688. int rc;
  689. rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE,
  690. PHYACC_ATTR_BANK_DSP, T1_COEF_RW_CTL_CFG, 0x0301);
  691. if (rc < 0)
  692. return rc;
  693. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  694. PHYACC_ATTR_BANK_DSP, T1_DCQ_SQI_REG, 0x0);
  695. if (rc < 0)
  696. return rc;
  697. sqi_value = FIELD_GET(T1_DCQ_SQI_MSK, rc);
  698. return sqi_value;
  699. }
  700. static int lan87xx_get_sqi_max(struct phy_device *phydev)
  701. {
  702. return LAN87XX_MAX_SQI;
  703. }
  704. static struct phy_driver microchip_t1_phy_driver[] = {
  705. {
  706. PHY_ID_MATCH_MODEL(PHY_ID_LAN87XX),
  707. .name = "Microchip LAN87xx T1",
  708. .flags = PHY_POLL_CABLE_TEST,
  709. .features = PHY_BASIC_T1_FEATURES,
  710. .config_init = lan87xx_config_init,
  711. .config_intr = lan87xx_phy_config_intr,
  712. .handle_interrupt = lan87xx_handle_interrupt,
  713. .suspend = genphy_suspend,
  714. .resume = genphy_resume,
  715. .config_aneg = lan87xx_config_aneg,
  716. .read_status = lan87xx_read_status,
  717. .get_sqi = lan87xx_get_sqi,
  718. .get_sqi_max = lan87xx_get_sqi_max,
  719. .cable_test_start = lan87xx_cable_test_start,
  720. .cable_test_get_status = lan87xx_cable_test_get_status,
  721. },
  722. {
  723. PHY_ID_MATCH_MODEL(PHY_ID_LAN937X),
  724. .name = "Microchip LAN937x T1",
  725. .flags = PHY_POLL_CABLE_TEST,
  726. .features = PHY_BASIC_T1_FEATURES,
  727. .config_init = lan87xx_config_init,
  728. .config_intr = lan87xx_phy_config_intr,
  729. .handle_interrupt = lan87xx_handle_interrupt,
  730. .suspend = genphy_suspend,
  731. .resume = genphy_resume,
  732. .config_aneg = lan87xx_config_aneg,
  733. .read_status = lan87xx_read_status,
  734. .get_sqi = lan87xx_get_sqi,
  735. .get_sqi_max = lan87xx_get_sqi_max,
  736. .cable_test_start = lan87xx_cable_test_start,
  737. .cable_test_get_status = lan87xx_cable_test_get_status,
  738. }
  739. };
  740. module_phy_driver(microchip_t1_phy_driver);
  741. static struct mdio_device_id __maybe_unused microchip_t1_tbl[] = {
  742. { PHY_ID_MATCH_MODEL(PHY_ID_LAN87XX) },
  743. { PHY_ID_MATCH_MODEL(PHY_ID_LAN937X) },
  744. { }
  745. };
  746. MODULE_DEVICE_TABLE(mdio, microchip_t1_tbl);
  747. MODULE_AUTHOR(DRIVER_AUTHOR);
  748. MODULE_DESCRIPTION(DRIVER_DESC);
  749. MODULE_LICENSE("GPL");