igc_phy.c 21 KB

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