igc_phy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018 Intel Corporation */
  3. #include "igc_phy.h"
  4. /**
  5. * igc_check_reset_block - Check if PHY reset is blocked
  6. * @hw: pointer to the HW structure
  7. *
  8. * Read the PHY management control register and check whether a PHY reset
  9. * is blocked. If a reset is not blocked return 0, otherwise
  10. * return IGC_ERR_BLK_PHY_RESET (12).
  11. */
  12. s32 igc_check_reset_block(struct igc_hw *hw)
  13. {
  14. u32 manc;
  15. manc = rd32(IGC_MANC);
  16. return (manc & IGC_MANC_BLK_PHY_RST_ON_IDE) ?
  17. IGC_ERR_BLK_PHY_RESET : 0;
  18. }
  19. /**
  20. * igc_get_phy_id - Retrieve the PHY ID and revision
  21. * @hw: pointer to the HW structure
  22. *
  23. * Reads the PHY registers and stores the PHY ID and possibly the PHY
  24. * revision in the hardware structure.
  25. */
  26. s32 igc_get_phy_id(struct igc_hw *hw)
  27. {
  28. struct igc_phy_info *phy = &hw->phy;
  29. s32 ret_val = 0;
  30. u16 phy_id;
  31. ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
  32. if (ret_val)
  33. goto out;
  34. phy->id = (u32)(phy_id << 16);
  35. usleep_range(200, 500);
  36. ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
  37. if (ret_val)
  38. goto out;
  39. phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
  40. phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
  41. out:
  42. return ret_val;
  43. }
  44. /**
  45. * igc_phy_has_link - Polls PHY for link
  46. * @hw: pointer to the HW structure
  47. * @iterations: number of times to poll for link
  48. * @usec_interval: delay between polling attempts
  49. * @success: pointer to whether polling was successful or not
  50. *
  51. * Polls the PHY status register for link, 'iterations' number of times.
  52. */
  53. s32 igc_phy_has_link(struct igc_hw *hw, u32 iterations,
  54. u32 usec_interval, bool *success)
  55. {
  56. u16 i, phy_status;
  57. s32 ret_val = 0;
  58. for (i = 0; i < iterations; i++) {
  59. /* Some PHYs require the PHY_STATUS register to be read
  60. * twice due to the link bit being sticky. No harm doing
  61. * it across the board.
  62. */
  63. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  64. if (ret_val && usec_interval > 0) {
  65. /* If the first read fails, another entity may have
  66. * ownership of the resources, wait and try again to
  67. * see if they have relinquished the resources yet.
  68. */
  69. if (usec_interval >= 1000)
  70. mdelay(usec_interval / 1000);
  71. else
  72. udelay(usec_interval);
  73. }
  74. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  75. if (ret_val)
  76. break;
  77. if (phy_status & MII_SR_LINK_STATUS)
  78. break;
  79. if (usec_interval >= 1000)
  80. mdelay(usec_interval / 1000);
  81. else
  82. udelay(usec_interval);
  83. }
  84. *success = (i < iterations) ? true : false;
  85. return ret_val;
  86. }
  87. /**
  88. * igc_power_up_phy_copper - Restore copper link in case of PHY power down
  89. * @hw: pointer to the HW structure
  90. *
  91. * In the case of a PHY power down to save power, or to turn off link during a
  92. * driver unload, restore the link to previous settings.
  93. */
  94. void igc_power_up_phy_copper(struct igc_hw *hw)
  95. {
  96. u16 mii_reg = 0;
  97. /* The PHY will retain its settings across a power down/up cycle */
  98. hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
  99. mii_reg &= ~MII_CR_POWER_DOWN;
  100. hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
  101. }
  102. /**
  103. * igc_power_down_phy_copper - Power down copper PHY
  104. * @hw: pointer to the HW structure
  105. *
  106. * Power down PHY to save power when interface is down and wake on lan
  107. * is not enabled.
  108. */
  109. void igc_power_down_phy_copper(struct igc_hw *hw)
  110. {
  111. u16 mii_reg = 0;
  112. /* The PHY will retain its settings across a power down/up cycle */
  113. hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
  114. mii_reg |= MII_CR_POWER_DOWN;
  115. /* Temporary workaround - should be removed when PHY will implement
  116. * IEEE registers as properly
  117. */
  118. /* hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);*/
  119. usleep_range(1000, 2000);
  120. }
  121. /**
  122. * igc_check_downshift - Checks whether a downshift in speed occurred
  123. * @hw: pointer to the HW structure
  124. *
  125. * Success returns 0, Failure returns 1
  126. *
  127. * A downshift is detected by querying the PHY link health.
  128. */
  129. s32 igc_check_downshift(struct igc_hw *hw)
  130. {
  131. struct igc_phy_info *phy = &hw->phy;
  132. u16 phy_data, offset, mask;
  133. s32 ret_val;
  134. switch (phy->type) {
  135. case igc_phy_i225:
  136. default:
  137. /* speed downshift not supported */
  138. phy->speed_downgraded = false;
  139. ret_val = 0;
  140. goto out;
  141. }
  142. ret_val = phy->ops.read_reg(hw, offset, &phy_data);
  143. if (!ret_val)
  144. phy->speed_downgraded = (phy_data & mask) ? true : false;
  145. out:
  146. return ret_val;
  147. }
  148. /**
  149. * igc_phy_hw_reset - PHY hardware reset
  150. * @hw: pointer to the HW structure
  151. *
  152. * Verify the reset block is not blocking us from resetting. Acquire
  153. * semaphore (if necessary) and read/set/write the device control reset
  154. * bit in the PHY. Wait the appropriate delay time for the device to
  155. * reset and release the semaphore (if necessary).
  156. */
  157. s32 igc_phy_hw_reset(struct igc_hw *hw)
  158. {
  159. struct igc_phy_info *phy = &hw->phy;
  160. s32 ret_val;
  161. u32 ctrl;
  162. ret_val = igc_check_reset_block(hw);
  163. if (ret_val) {
  164. ret_val = 0;
  165. goto out;
  166. }
  167. ret_val = phy->ops.acquire(hw);
  168. if (ret_val)
  169. goto out;
  170. ctrl = rd32(IGC_CTRL);
  171. wr32(IGC_CTRL, ctrl | IGC_CTRL_PHY_RST);
  172. wrfl();
  173. udelay(phy->reset_delay_us);
  174. wr32(IGC_CTRL, ctrl);
  175. wrfl();
  176. usleep_range(1500, 2000);
  177. phy->ops.release(hw);
  178. out:
  179. return ret_val;
  180. }
  181. /**
  182. * igc_read_phy_reg_mdic - Read MDI control register
  183. * @hw: pointer to the HW structure
  184. * @offset: register offset to be read
  185. * @data: pointer to the read data
  186. *
  187. * Reads the MDI control register in the PHY at offset and stores the
  188. * information read to data.
  189. */
  190. static s32 igc_read_phy_reg_mdic(struct igc_hw *hw, u32 offset, u16 *data)
  191. {
  192. struct igc_phy_info *phy = &hw->phy;
  193. u32 i, mdic = 0;
  194. s32 ret_val = 0;
  195. if (offset > MAX_PHY_REG_ADDRESS) {
  196. hw_dbg("PHY Address %d is out of range\n", offset);
  197. ret_val = -IGC_ERR_PARAM;
  198. goto out;
  199. }
  200. /* Set up Op-code, Phy Address, and register offset in the MDI
  201. * Control register. The MAC will take care of interfacing with the
  202. * PHY to retrieve the desired data.
  203. */
  204. mdic = ((offset << IGC_MDIC_REG_SHIFT) |
  205. (phy->addr << IGC_MDIC_PHY_SHIFT) |
  206. (IGC_MDIC_OP_READ));
  207. wr32(IGC_MDIC, mdic);
  208. /* Poll the ready bit to see if the MDI read completed
  209. * Increasing the time out as testing showed failures with
  210. * the lower time out
  211. */
  212. for (i = 0; i < IGC_GEN_POLL_TIMEOUT; i++) {
  213. usleep_range(500, 1000);
  214. mdic = rd32(IGC_MDIC);
  215. if (mdic & IGC_MDIC_READY)
  216. break;
  217. }
  218. if (!(mdic & IGC_MDIC_READY)) {
  219. hw_dbg("MDI Read did not complete\n");
  220. ret_val = -IGC_ERR_PHY;
  221. goto out;
  222. }
  223. if (mdic & IGC_MDIC_ERROR) {
  224. hw_dbg("MDI Error\n");
  225. ret_val = -IGC_ERR_PHY;
  226. goto out;
  227. }
  228. *data = (u16)mdic;
  229. out:
  230. return ret_val;
  231. }
  232. /**
  233. * igc_write_phy_reg_mdic - Write MDI control register
  234. * @hw: pointer to the HW structure
  235. * @offset: register offset to write to
  236. * @data: data to write to register at offset
  237. *
  238. * Writes data to MDI control register in the PHY at offset.
  239. */
  240. static s32 igc_write_phy_reg_mdic(struct igc_hw *hw, u32 offset, u16 data)
  241. {
  242. struct igc_phy_info *phy = &hw->phy;
  243. u32 i, mdic = 0;
  244. s32 ret_val = 0;
  245. if (offset > MAX_PHY_REG_ADDRESS) {
  246. hw_dbg("PHY Address %d is out of range\n", offset);
  247. ret_val = -IGC_ERR_PARAM;
  248. goto out;
  249. }
  250. /* Set up Op-code, Phy Address, and register offset in the MDI
  251. * Control register. The MAC will take care of interfacing with the
  252. * PHY to write the desired data.
  253. */
  254. mdic = (((u32)data) |
  255. (offset << IGC_MDIC_REG_SHIFT) |
  256. (phy->addr << IGC_MDIC_PHY_SHIFT) |
  257. (IGC_MDIC_OP_WRITE));
  258. wr32(IGC_MDIC, mdic);
  259. /* Poll the ready bit to see if the MDI read completed
  260. * Increasing the time out as testing showed failures with
  261. * the lower time out
  262. */
  263. for (i = 0; i < IGC_GEN_POLL_TIMEOUT; i++) {
  264. usleep_range(500, 1000);
  265. mdic = rd32(IGC_MDIC);
  266. if (mdic & IGC_MDIC_READY)
  267. break;
  268. }
  269. if (!(mdic & IGC_MDIC_READY)) {
  270. hw_dbg("MDI Write did not complete\n");
  271. ret_val = -IGC_ERR_PHY;
  272. goto out;
  273. }
  274. if (mdic & IGC_MDIC_ERROR) {
  275. hw_dbg("MDI Error\n");
  276. ret_val = -IGC_ERR_PHY;
  277. goto out;
  278. }
  279. out:
  280. return ret_val;
  281. }
  282. /**
  283. * __igc_access_xmdio_reg - Read/write XMDIO register
  284. * @hw: pointer to the HW structure
  285. * @address: XMDIO address to program
  286. * @dev_addr: device address to program
  287. * @data: pointer to value to read/write from/to the XMDIO address
  288. * @read: boolean flag to indicate read or write
  289. */
  290. static s32 __igc_access_xmdio_reg(struct igc_hw *hw, u16 address,
  291. u8 dev_addr, u16 *data, bool read)
  292. {
  293. s32 ret_val;
  294. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAC, dev_addr);
  295. if (ret_val)
  296. return ret_val;
  297. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAAD, address);
  298. if (ret_val)
  299. return ret_val;
  300. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAC, IGC_MMDAC_FUNC_DATA |
  301. dev_addr);
  302. if (ret_val)
  303. return ret_val;
  304. if (read)
  305. ret_val = hw->phy.ops.read_reg(hw, IGC_MMDAAD, data);
  306. else
  307. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAAD, *data);
  308. if (ret_val)
  309. return ret_val;
  310. /* Recalibrate the device back to 0 */
  311. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAC, 0);
  312. if (ret_val)
  313. return ret_val;
  314. return ret_val;
  315. }
  316. /**
  317. * igc_read_xmdio_reg - Read XMDIO register
  318. * @hw: pointer to the HW structure
  319. * @addr: XMDIO address to program
  320. * @dev_addr: device address to program
  321. * @data: value to be read from the EMI address
  322. */
  323. static s32 igc_read_xmdio_reg(struct igc_hw *hw, u16 addr,
  324. u8 dev_addr, u16 *data)
  325. {
  326. return __igc_access_xmdio_reg(hw, addr, dev_addr, data, true);
  327. }
  328. /**
  329. * igc_write_xmdio_reg - Write XMDIO register
  330. * @hw: pointer to the HW structure
  331. * @addr: XMDIO address to program
  332. * @dev_addr: device address to program
  333. * @data: value to be written to the XMDIO address
  334. */
  335. static s32 igc_write_xmdio_reg(struct igc_hw *hw, u16 addr,
  336. u8 dev_addr, u16 data)
  337. {
  338. return __igc_access_xmdio_reg(hw, addr, dev_addr, &data, false);
  339. }
  340. /**
  341. * igc_write_phy_reg_gpy - Write GPY PHY register
  342. * @hw: pointer to the HW structure
  343. * @offset: register offset to write to
  344. * @data: data to write at register offset
  345. *
  346. * Acquires semaphore, if necessary, then writes the data to PHY register
  347. * at the offset. Release any acquired semaphores before exiting.
  348. */
  349. s32 igc_write_phy_reg_gpy(struct igc_hw *hw, u32 offset, u16 data)
  350. {
  351. u8 dev_addr = (offset & GPY_MMD_MASK) >> GPY_MMD_SHIFT;
  352. s32 ret_val;
  353. offset = offset & GPY_REG_MASK;
  354. if (!dev_addr) {
  355. ret_val = hw->phy.ops.acquire(hw);
  356. if (ret_val)
  357. return ret_val;
  358. ret_val = igc_write_phy_reg_mdic(hw, offset, data);
  359. if (ret_val)
  360. return ret_val;
  361. hw->phy.ops.release(hw);
  362. } else {
  363. ret_val = igc_write_xmdio_reg(hw, (u16)offset, dev_addr,
  364. data);
  365. }
  366. return ret_val;
  367. }
  368. /**
  369. * igc_read_phy_reg_gpy - Read GPY PHY register
  370. * @hw: pointer to the HW structure
  371. * @offset: lower half is register offset to read to
  372. * upper half is MMD to use.
  373. * @data: data to read at register offset
  374. *
  375. * Acquires semaphore, if necessary, then reads the data in the PHY register
  376. * at the offset. Release any acquired semaphores before exiting.
  377. */
  378. s32 igc_read_phy_reg_gpy(struct igc_hw *hw, u32 offset, u16 *data)
  379. {
  380. u8 dev_addr = (offset & GPY_MMD_MASK) >> GPY_MMD_SHIFT;
  381. s32 ret_val;
  382. offset = offset & GPY_REG_MASK;
  383. if (!dev_addr) {
  384. ret_val = hw->phy.ops.acquire(hw);
  385. if (ret_val)
  386. return ret_val;
  387. ret_val = igc_read_phy_reg_mdic(hw, offset, data);
  388. if (ret_val)
  389. return ret_val;
  390. hw->phy.ops.release(hw);
  391. } else {
  392. ret_val = igc_read_xmdio_reg(hw, (u16)offset, dev_addr,
  393. data);
  394. }
  395. return ret_val;
  396. }