igc_phy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018 Intel Corporation */
  3. #include <linux/bitfield.h>
  4. #include "igc_phy.h"
  5. /**
  6. * igc_check_reset_block - Check if PHY reset is blocked
  7. * @hw: pointer to the HW structure
  8. *
  9. * Read the PHY management control register and check whether a PHY reset
  10. * is blocked. If a reset is not blocked return 0, otherwise
  11. * return IGC_ERR_BLK_PHY_RESET (12).
  12. */
  13. s32 igc_check_reset_block(struct igc_hw *hw)
  14. {
  15. u32 manc;
  16. manc = rd32(IGC_MANC);
  17. return (manc & IGC_MANC_BLK_PHY_RST_ON_IDE) ?
  18. IGC_ERR_BLK_PHY_RESET : 0;
  19. }
  20. /**
  21. * igc_get_phy_id - Retrieve the PHY ID and revision
  22. * @hw: pointer to the HW structure
  23. *
  24. * Reads the PHY registers and stores the PHY ID and possibly the PHY
  25. * revision in the hardware structure.
  26. */
  27. s32 igc_get_phy_id(struct igc_hw *hw)
  28. {
  29. struct igc_phy_info *phy = &hw->phy;
  30. s32 ret_val = 0;
  31. u16 phy_id;
  32. ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
  33. if (ret_val)
  34. goto out;
  35. phy->id = (u32)(phy_id << 16);
  36. usleep_range(200, 500);
  37. ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
  38. if (ret_val)
  39. goto out;
  40. phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
  41. phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
  42. out:
  43. return ret_val;
  44. }
  45. /**
  46. * igc_phy_has_link - Polls PHY for link
  47. * @hw: pointer to the HW structure
  48. * @iterations: number of times to poll for link
  49. * @usec_interval: delay between polling attempts
  50. * @success: pointer to whether polling was successful or not
  51. *
  52. * Polls the PHY status register for link, 'iterations' number of times.
  53. */
  54. s32 igc_phy_has_link(struct igc_hw *hw, u32 iterations,
  55. u32 usec_interval, bool *success)
  56. {
  57. u16 i, phy_status;
  58. s32 ret_val = 0;
  59. for (i = 0; i < iterations; i++) {
  60. /* Some PHYs require the PHY_STATUS register to be read
  61. * twice due to the link bit being sticky. No harm doing
  62. * it across the board.
  63. */
  64. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  65. if (ret_val && usec_interval > 0) {
  66. /* If the first read fails, another entity may have
  67. * ownership of the resources, wait and try again to
  68. * see if they have relinquished the resources yet.
  69. */
  70. if (usec_interval >= 1000)
  71. mdelay(usec_interval / 1000);
  72. else
  73. udelay(usec_interval);
  74. }
  75. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  76. if (ret_val)
  77. break;
  78. if (phy_status & MII_SR_LINK_STATUS)
  79. break;
  80. if (usec_interval >= 1000)
  81. mdelay(usec_interval / 1000);
  82. else
  83. udelay(usec_interval);
  84. }
  85. *success = (i < iterations) ? true : false;
  86. return ret_val;
  87. }
  88. /**
  89. * igc_power_up_phy_copper - Restore copper link in case of PHY power down
  90. * @hw: pointer to the HW structure
  91. *
  92. * In the case of a PHY power down to save power, or to turn off link during a
  93. * driver unload, restore the link to previous settings.
  94. */
  95. void igc_power_up_phy_copper(struct igc_hw *hw)
  96. {
  97. u16 mii_reg = 0;
  98. /* The PHY will retain its settings across a power down/up cycle */
  99. hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
  100. mii_reg &= ~MII_CR_POWER_DOWN;
  101. hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
  102. }
  103. /**
  104. * igc_power_down_phy_copper - Power down copper PHY
  105. * @hw: pointer to the HW structure
  106. *
  107. * Power down PHY to save power when interface is down and wake on lan
  108. * is not enabled.
  109. */
  110. void igc_power_down_phy_copper(struct igc_hw *hw)
  111. {
  112. u16 mii_reg = 0;
  113. /* The PHY will retain its settings across a power down/up cycle */
  114. hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
  115. mii_reg |= MII_CR_POWER_DOWN;
  116. hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
  117. usleep_range(1000, 2000);
  118. }
  119. /**
  120. * igc_check_downshift - Checks whether a downshift in speed occurred
  121. * @hw: pointer to the HW structure
  122. *
  123. * A downshift is detected by querying the PHY link health.
  124. */
  125. void igc_check_downshift(struct igc_hw *hw)
  126. {
  127. struct igc_phy_info *phy = &hw->phy;
  128. /* speed downshift not supported */
  129. phy->speed_downgraded = false;
  130. }
  131. /**
  132. * igc_phy_hw_reset - PHY hardware reset
  133. * @hw: pointer to the HW structure
  134. *
  135. * Verify the reset block is not blocking us from resetting. Acquire
  136. * semaphore (if necessary) and read/set/write the device control reset
  137. * bit in the PHY. Wait the appropriate delay time for the device to
  138. * reset and release the semaphore (if necessary).
  139. */
  140. s32 igc_phy_hw_reset(struct igc_hw *hw)
  141. {
  142. struct igc_phy_info *phy = &hw->phy;
  143. u32 phpm = 0, timeout = 10000;
  144. s32 ret_val;
  145. u32 ctrl;
  146. ret_val = igc_check_reset_block(hw);
  147. if (ret_val) {
  148. ret_val = 0;
  149. goto out;
  150. }
  151. ret_val = phy->ops.acquire(hw);
  152. if (ret_val)
  153. goto out;
  154. phpm = rd32(IGC_I225_PHPM);
  155. ctrl = rd32(IGC_CTRL);
  156. wr32(IGC_CTRL, ctrl | IGC_CTRL_PHY_RST);
  157. wrfl();
  158. udelay(phy->reset_delay_us);
  159. wr32(IGC_CTRL, ctrl);
  160. wrfl();
  161. /* SW should guarantee 100us for the completion of the PHY reset */
  162. usleep_range(100, 150);
  163. do {
  164. phpm = rd32(IGC_I225_PHPM);
  165. timeout--;
  166. udelay(1);
  167. } while (!(phpm & IGC_PHY_RST_COMP) && timeout);
  168. if (!timeout)
  169. hw_dbg("Timeout is expired after a phy reset\n");
  170. usleep_range(100, 150);
  171. phy->ops.release(hw);
  172. out:
  173. return ret_val;
  174. }
  175. /**
  176. * igc_phy_setup_autoneg - Configure PHY for auto-negotiation
  177. * @hw: pointer to the HW structure
  178. *
  179. * Reads the MII auto-neg advertisement register and/or the 1000T control
  180. * register and if the PHY is already setup for auto-negotiation, then
  181. * return successful. Otherwise, setup advertisement and flow control to
  182. * the appropriate values for the wanted auto-negotiation.
  183. */
  184. static s32 igc_phy_setup_autoneg(struct igc_hw *hw)
  185. {
  186. struct igc_phy_info *phy = &hw->phy;
  187. u16 aneg_multigbt_an_ctrl = 0;
  188. u16 mii_1000t_ctrl_reg = 0;
  189. u16 mii_autoneg_adv_reg;
  190. s32 ret_val;
  191. phy->autoneg_advertised &= phy->autoneg_mask;
  192. /* Read the MII Auto-Neg Advertisement Register (Address 4). */
  193. ret_val = phy->ops.read_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
  194. if (ret_val)
  195. return ret_val;
  196. if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
  197. /* Read the MII 1000Base-T Control Register (Address 9). */
  198. ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL,
  199. &mii_1000t_ctrl_reg);
  200. if (ret_val)
  201. return ret_val;
  202. }
  203. if (phy->autoneg_mask & ADVERTISE_2500_FULL) {
  204. /* Read the MULTI GBT AN Control Register - reg 7.32 */
  205. ret_val = phy->ops.read_reg(hw, (STANDARD_AN_REG_MASK <<
  206. MMD_DEVADDR_SHIFT) |
  207. IGC_ANEG_MULTIGBT_AN_CTRL,
  208. &aneg_multigbt_an_ctrl);
  209. if (ret_val)
  210. return ret_val;
  211. }
  212. /* Need to parse both autoneg_advertised and fc and set up
  213. * the appropriate PHY registers. First we will parse for
  214. * autoneg_advertised software override. Since we can advertise
  215. * a plethora of combinations, we need to check each bit
  216. * individually.
  217. */
  218. /* First we clear all the 10/100 mb speed bits in the Auto-Neg
  219. * Advertisement Register (Address 4) and the 1000 mb speed bits in
  220. * the 1000Base-T Control Register (Address 9).
  221. */
  222. mii_autoneg_adv_reg &= ~(NWAY_AR_100TX_FD_CAPS |
  223. NWAY_AR_100TX_HD_CAPS |
  224. NWAY_AR_10T_FD_CAPS |
  225. NWAY_AR_10T_HD_CAPS);
  226. mii_1000t_ctrl_reg &= ~(CR_1000T_HD_CAPS | CR_1000T_FD_CAPS);
  227. hw_dbg("autoneg_advertised %x\n", phy->autoneg_advertised);
  228. /* Do we want to advertise 10 Mb Half Duplex? */
  229. if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
  230. hw_dbg("Advertise 10mb Half duplex\n");
  231. mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
  232. }
  233. /* Do we want to advertise 10 Mb Full Duplex? */
  234. if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
  235. hw_dbg("Advertise 10mb Full duplex\n");
  236. mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
  237. }
  238. /* Do we want to advertise 100 Mb Half Duplex? */
  239. if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
  240. hw_dbg("Advertise 100mb Half duplex\n");
  241. mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
  242. }
  243. /* Do we want to advertise 100 Mb Full Duplex? */
  244. if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
  245. hw_dbg("Advertise 100mb Full duplex\n");
  246. mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
  247. }
  248. /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
  249. if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
  250. hw_dbg("Advertise 1000mb Half duplex request denied!\n");
  251. /* Do we want to advertise 1000 Mb Full Duplex? */
  252. if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
  253. hw_dbg("Advertise 1000mb Full duplex\n");
  254. mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
  255. }
  256. /* We do not allow the Phy to advertise 2500 Mb Half Duplex */
  257. if (phy->autoneg_advertised & ADVERTISE_2500_HALF)
  258. hw_dbg("Advertise 2500mb Half duplex request denied!\n");
  259. /* Do we want to advertise 2500 Mb Full Duplex? */
  260. if (phy->autoneg_advertised & ADVERTISE_2500_FULL) {
  261. hw_dbg("Advertise 2500mb Full duplex\n");
  262. aneg_multigbt_an_ctrl |= CR_2500T_FD_CAPS;
  263. } else {
  264. aneg_multigbt_an_ctrl &= ~CR_2500T_FD_CAPS;
  265. }
  266. /* Check for a software override of the flow control settings, and
  267. * setup the PHY advertisement registers accordingly. If
  268. * auto-negotiation is enabled, then software will have to set the
  269. * "PAUSE" bits to the correct value in the Auto-Negotiation
  270. * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-
  271. * negotiation.
  272. *
  273. * The possible values of the "fc" parameter are:
  274. * 0: Flow control is completely disabled
  275. * 1: Rx flow control is enabled (we can receive pause frames
  276. * but not send pause frames).
  277. * 2: Tx flow control is enabled (we can send pause frames
  278. * but we do not support receiving pause frames).
  279. * 3: Both Rx and Tx flow control (symmetric) are enabled.
  280. * other: No software override. The flow control configuration
  281. * in the EEPROM is used.
  282. */
  283. switch (hw->fc.current_mode) {
  284. case igc_fc_none:
  285. /* Flow control (Rx & Tx) is completely disabled by a
  286. * software over-ride.
  287. */
  288. mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  289. break;
  290. case igc_fc_rx_pause:
  291. /* Rx Flow control is enabled, and Tx Flow control is
  292. * disabled, by a software over-ride.
  293. *
  294. * Since there really isn't a way to advertise that we are
  295. * capable of Rx Pause ONLY, we will advertise that we
  296. * support both symmetric and asymmetric Rx PAUSE. Later
  297. * (in igc_config_fc_after_link_up) we will disable the
  298. * hw's ability to send PAUSE frames.
  299. */
  300. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  301. break;
  302. case igc_fc_tx_pause:
  303. /* Tx Flow control is enabled, and Rx Flow control is
  304. * disabled, by a software over-ride.
  305. */
  306. mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
  307. mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
  308. break;
  309. case igc_fc_full:
  310. /* Flow control (both Rx and Tx) is enabled by a software
  311. * over-ride.
  312. */
  313. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  314. break;
  315. default:
  316. hw_dbg("Flow control param set incorrectly\n");
  317. return -IGC_ERR_CONFIG;
  318. }
  319. ret_val = phy->ops.write_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
  320. if (ret_val)
  321. return ret_val;
  322. hw_dbg("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
  323. if (phy->autoneg_mask & ADVERTISE_1000_FULL)
  324. ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL,
  325. mii_1000t_ctrl_reg);
  326. if (phy->autoneg_mask & ADVERTISE_2500_FULL)
  327. ret_val = phy->ops.write_reg(hw,
  328. (STANDARD_AN_REG_MASK <<
  329. MMD_DEVADDR_SHIFT) |
  330. IGC_ANEG_MULTIGBT_AN_CTRL,
  331. aneg_multigbt_an_ctrl);
  332. return ret_val;
  333. }
  334. /**
  335. * igc_wait_autoneg - Wait for auto-neg completion
  336. * @hw: pointer to the HW structure
  337. *
  338. * Waits for auto-negotiation to complete or for the auto-negotiation time
  339. * limit to expire, which ever happens first.
  340. */
  341. static s32 igc_wait_autoneg(struct igc_hw *hw)
  342. {
  343. u16 i, phy_status;
  344. s32 ret_val = 0;
  345. /* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
  346. for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
  347. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  348. if (ret_val)
  349. break;
  350. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  351. if (ret_val)
  352. break;
  353. if (phy_status & MII_SR_AUTONEG_COMPLETE)
  354. break;
  355. msleep(100);
  356. }
  357. /* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
  358. * has completed.
  359. */
  360. return ret_val;
  361. }
  362. /**
  363. * igc_copper_link_autoneg - Setup/Enable autoneg for copper link
  364. * @hw: pointer to the HW structure
  365. *
  366. * Performs initial bounds checking on autoneg advertisement parameter, then
  367. * configure to advertise the full capability. Setup the PHY to autoneg
  368. * and restart the negotiation process between the link partner. If
  369. * autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
  370. */
  371. static s32 igc_copper_link_autoneg(struct igc_hw *hw)
  372. {
  373. struct igc_phy_info *phy = &hw->phy;
  374. u16 phy_ctrl;
  375. s32 ret_val;
  376. /* Perform some bounds checking on the autoneg advertisement
  377. * parameter.
  378. */
  379. phy->autoneg_advertised &= phy->autoneg_mask;
  380. /* If autoneg_advertised is zero, we assume it was not defaulted
  381. * by the calling code so we set to advertise full capability.
  382. */
  383. if (phy->autoneg_advertised == 0)
  384. phy->autoneg_advertised = phy->autoneg_mask;
  385. hw_dbg("Reconfiguring auto-neg advertisement params\n");
  386. ret_val = igc_phy_setup_autoneg(hw);
  387. if (ret_val) {
  388. hw_dbg("Error Setting up Auto-Negotiation\n");
  389. goto out;
  390. }
  391. hw_dbg("Restarting Auto-Neg\n");
  392. /* Restart auto-negotiation by setting the Auto Neg Enable bit and
  393. * the Auto Neg Restart bit in the PHY control register.
  394. */
  395. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
  396. if (ret_val)
  397. goto out;
  398. phy_ctrl |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
  399. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
  400. if (ret_val)
  401. goto out;
  402. /* Does the user want to wait for Auto-Neg to complete here, or
  403. * check at a later time (for example, callback routine).
  404. */
  405. if (phy->autoneg_wait_to_complete) {
  406. ret_val = igc_wait_autoneg(hw);
  407. if (ret_val) {
  408. hw_dbg("Error while waiting for autoneg to complete\n");
  409. goto out;
  410. }
  411. }
  412. hw->mac.get_link_status = true;
  413. out:
  414. return ret_val;
  415. }
  416. /**
  417. * igc_setup_copper_link - Configure copper link settings
  418. * @hw: pointer to the HW structure
  419. *
  420. * Calls the appropriate function to configure the link for auto-neg or forced
  421. * speed and duplex. Then we check for link, once link is established calls
  422. * to configure collision distance and flow control are called. If link is
  423. * not established, we return -IGC_ERR_PHY (-2).
  424. */
  425. s32 igc_setup_copper_link(struct igc_hw *hw)
  426. {
  427. s32 ret_val = 0;
  428. bool link;
  429. if (hw->mac.autoneg) {
  430. /* Setup autoneg and flow control advertisement and perform
  431. * autonegotiation.
  432. */
  433. ret_val = igc_copper_link_autoneg(hw);
  434. if (ret_val)
  435. goto out;
  436. } else {
  437. /* PHY will be set to 10H, 10F, 100H or 100F
  438. * depending on user settings.
  439. */
  440. hw_dbg("Forcing Speed and Duplex\n");
  441. ret_val = hw->phy.ops.force_speed_duplex(hw);
  442. if (ret_val) {
  443. hw_dbg("Error Forcing Speed and Duplex\n");
  444. goto out;
  445. }
  446. }
  447. /* Check link status. Wait up to 100 microseconds for link to become
  448. * valid.
  449. */
  450. ret_val = igc_phy_has_link(hw, COPPER_LINK_UP_LIMIT, 10, &link);
  451. if (ret_val)
  452. goto out;
  453. if (link) {
  454. hw_dbg("Valid link established!!!\n");
  455. igc_config_collision_dist(hw);
  456. ret_val = igc_config_fc_after_link_up(hw);
  457. } else {
  458. hw_dbg("Unable to establish link!!!\n");
  459. }
  460. out:
  461. return ret_val;
  462. }
  463. /**
  464. * igc_read_phy_reg_mdic - Read MDI control register
  465. * @hw: pointer to the HW structure
  466. * @offset: register offset to be read
  467. * @data: pointer to the read data
  468. *
  469. * Reads the MDI control register in the PHY at offset and stores the
  470. * information read to data.
  471. */
  472. static s32 igc_read_phy_reg_mdic(struct igc_hw *hw, u32 offset, u16 *data)
  473. {
  474. struct igc_phy_info *phy = &hw->phy;
  475. u32 i, mdic = 0;
  476. s32 ret_val = 0;
  477. if (offset > MAX_PHY_REG_ADDRESS) {
  478. hw_dbg("PHY Address %d is out of range\n", offset);
  479. ret_val = -IGC_ERR_PARAM;
  480. goto out;
  481. }
  482. /* Set up Op-code, Phy Address, and register offset in the MDI
  483. * Control register. The MAC will take care of interfacing with the
  484. * PHY to retrieve the desired data.
  485. */
  486. mdic = ((offset << IGC_MDIC_REG_SHIFT) |
  487. (phy->addr << IGC_MDIC_PHY_SHIFT) |
  488. (IGC_MDIC_OP_READ));
  489. wr32(IGC_MDIC, mdic);
  490. /* Poll the ready bit to see if the MDI read completed
  491. * Increasing the time out as testing showed failures with
  492. * the lower time out
  493. */
  494. for (i = 0; i < IGC_GEN_POLL_TIMEOUT; i++) {
  495. udelay(50);
  496. mdic = rd32(IGC_MDIC);
  497. if (mdic & IGC_MDIC_READY)
  498. break;
  499. }
  500. if (!(mdic & IGC_MDIC_READY)) {
  501. hw_dbg("MDI Read did not complete\n");
  502. ret_val = -IGC_ERR_PHY;
  503. goto out;
  504. }
  505. if (mdic & IGC_MDIC_ERROR) {
  506. hw_dbg("MDI Error\n");
  507. ret_val = -IGC_ERR_PHY;
  508. goto out;
  509. }
  510. *data = (u16)mdic;
  511. out:
  512. return ret_val;
  513. }
  514. /**
  515. * igc_write_phy_reg_mdic - Write MDI control register
  516. * @hw: pointer to the HW structure
  517. * @offset: register offset to write to
  518. * @data: data to write to register at offset
  519. *
  520. * Writes data to MDI control register in the PHY at offset.
  521. */
  522. static s32 igc_write_phy_reg_mdic(struct igc_hw *hw, u32 offset, u16 data)
  523. {
  524. struct igc_phy_info *phy = &hw->phy;
  525. u32 i, mdic = 0;
  526. s32 ret_val = 0;
  527. if (offset > MAX_PHY_REG_ADDRESS) {
  528. hw_dbg("PHY Address %d is out of range\n", offset);
  529. ret_val = -IGC_ERR_PARAM;
  530. goto out;
  531. }
  532. /* Set up Op-code, Phy Address, and register offset in the MDI
  533. * Control register. The MAC will take care of interfacing with the
  534. * PHY to write the desired data.
  535. */
  536. mdic = (((u32)data) |
  537. (offset << IGC_MDIC_REG_SHIFT) |
  538. (phy->addr << IGC_MDIC_PHY_SHIFT) |
  539. (IGC_MDIC_OP_WRITE));
  540. wr32(IGC_MDIC, mdic);
  541. /* Poll the ready bit to see if the MDI read completed
  542. * Increasing the time out as testing showed failures with
  543. * the lower time out
  544. */
  545. for (i = 0; i < IGC_GEN_POLL_TIMEOUT; i++) {
  546. udelay(50);
  547. mdic = rd32(IGC_MDIC);
  548. if (mdic & IGC_MDIC_READY)
  549. break;
  550. }
  551. if (!(mdic & IGC_MDIC_READY)) {
  552. hw_dbg("MDI Write did not complete\n");
  553. ret_val = -IGC_ERR_PHY;
  554. goto out;
  555. }
  556. if (mdic & IGC_MDIC_ERROR) {
  557. hw_dbg("MDI Error\n");
  558. ret_val = -IGC_ERR_PHY;
  559. goto out;
  560. }
  561. out:
  562. return ret_val;
  563. }
  564. /**
  565. * __igc_access_xmdio_reg - Read/write XMDIO register
  566. * @hw: pointer to the HW structure
  567. * @address: XMDIO address to program
  568. * @dev_addr: device address to program
  569. * @data: pointer to value to read/write from/to the XMDIO address
  570. * @read: boolean flag to indicate read or write
  571. */
  572. static s32 __igc_access_xmdio_reg(struct igc_hw *hw, u16 address,
  573. u8 dev_addr, u16 *data, bool read)
  574. {
  575. s32 ret_val;
  576. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAC, dev_addr);
  577. if (ret_val)
  578. return ret_val;
  579. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAAD, address);
  580. if (ret_val)
  581. return ret_val;
  582. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAC, IGC_MMDAC_FUNC_DATA |
  583. dev_addr);
  584. if (ret_val)
  585. return ret_val;
  586. if (read)
  587. ret_val = hw->phy.ops.read_reg(hw, IGC_MMDAAD, data);
  588. else
  589. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAAD, *data);
  590. if (ret_val)
  591. return ret_val;
  592. /* Recalibrate the device back to 0 */
  593. ret_val = hw->phy.ops.write_reg(hw, IGC_MMDAC, 0);
  594. if (ret_val)
  595. return ret_val;
  596. return ret_val;
  597. }
  598. /**
  599. * igc_read_xmdio_reg - Read XMDIO register
  600. * @hw: pointer to the HW structure
  601. * @addr: XMDIO address to program
  602. * @dev_addr: device address to program
  603. * @data: value to be read from the EMI address
  604. */
  605. static s32 igc_read_xmdio_reg(struct igc_hw *hw, u16 addr,
  606. u8 dev_addr, u16 *data)
  607. {
  608. return __igc_access_xmdio_reg(hw, addr, dev_addr, data, true);
  609. }
  610. /**
  611. * igc_write_xmdio_reg - Write XMDIO register
  612. * @hw: pointer to the HW structure
  613. * @addr: XMDIO address to program
  614. * @dev_addr: device address to program
  615. * @data: value to be written to the XMDIO address
  616. */
  617. static s32 igc_write_xmdio_reg(struct igc_hw *hw, u16 addr,
  618. u8 dev_addr, u16 data)
  619. {
  620. return __igc_access_xmdio_reg(hw, addr, dev_addr, &data, false);
  621. }
  622. /**
  623. * igc_write_phy_reg_gpy - Write GPY PHY register
  624. * @hw: pointer to the HW structure
  625. * @offset: register offset to write to
  626. * @data: data to write at register offset
  627. *
  628. * Acquires semaphore, if necessary, then writes the data to PHY register
  629. * at the offset. Release any acquired semaphores before exiting.
  630. */
  631. s32 igc_write_phy_reg_gpy(struct igc_hw *hw, u32 offset, u16 data)
  632. {
  633. u8 dev_addr = FIELD_GET(GPY_MMD_MASK, offset);
  634. s32 ret_val;
  635. offset = offset & GPY_REG_MASK;
  636. if (!dev_addr) {
  637. ret_val = hw->phy.ops.acquire(hw);
  638. if (ret_val)
  639. return ret_val;
  640. ret_val = igc_write_phy_reg_mdic(hw, offset, data);
  641. hw->phy.ops.release(hw);
  642. } else {
  643. ret_val = igc_write_xmdio_reg(hw, (u16)offset, dev_addr,
  644. data);
  645. }
  646. return ret_val;
  647. }
  648. /**
  649. * igc_read_phy_reg_gpy - Read GPY PHY register
  650. * @hw: pointer to the HW structure
  651. * @offset: lower half is register offset to read to
  652. * upper half is MMD to use.
  653. * @data: data to read at register offset
  654. *
  655. * Acquires semaphore, if necessary, then reads the data in the PHY register
  656. * at the offset. Release any acquired semaphores before exiting.
  657. */
  658. s32 igc_read_phy_reg_gpy(struct igc_hw *hw, u32 offset, u16 *data)
  659. {
  660. u8 dev_addr = FIELD_GET(GPY_MMD_MASK, offset);
  661. s32 ret_val;
  662. offset = offset & GPY_REG_MASK;
  663. if (!dev_addr) {
  664. ret_val = hw->phy.ops.acquire(hw);
  665. if (ret_val)
  666. return ret_val;
  667. ret_val = igc_read_phy_reg_mdic(hw, offset, data);
  668. hw->phy.ops.release(hw);
  669. } else {
  670. ret_val = igc_read_xmdio_reg(hw, (u16)offset, dev_addr,
  671. data);
  672. }
  673. return ret_val;
  674. }
  675. /**
  676. * igc_read_phy_fw_version - Read gPHY firmware version
  677. * @hw: pointer to the HW structure
  678. */
  679. u16 igc_read_phy_fw_version(struct igc_hw *hw)
  680. {
  681. struct igc_phy_info *phy = &hw->phy;
  682. u16 gphy_version = 0;
  683. u16 ret_val;
  684. /* NVM image version is reported as firmware version for i225 device */
  685. ret_val = phy->ops.read_reg(hw, IGC_GPHY_VERSION, &gphy_version);
  686. if (ret_val)
  687. hw_dbg("igc_phy: read wrong gphy version\n");
  688. return gphy_version;
  689. }