e1000_phy.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007-2009 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include <linux/if_ether.h>
  21. #include <linux/delay.h>
  22. #include "e1000_mac.h"
  23. #include "e1000_phy.h"
  24. static s32 igb_phy_setup_autoneg(struct e1000_hw *hw);
  25. static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
  26. u16 *phy_ctrl);
  27. static s32 igb_wait_autoneg(struct e1000_hw *hw);
  28. /* Cable length tables */
  29. static const u16 e1000_m88_cable_length_table[] =
  30. { 0, 50, 80, 110, 140, 140, E1000_CABLE_LENGTH_UNDEFINED };
  31. static const u16 e1000_igp_2_cable_length_table[] =
  32. { 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
  33. 0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
  34. 6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
  35. 21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
  36. 40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
  37. 60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
  38. 83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
  39. 104, 109, 114, 118, 121, 124};
  40. #define IGP02E1000_CABLE_LENGTH_TABLE_SIZE \
  41. (sizeof(e1000_igp_2_cable_length_table) / \
  42. sizeof(e1000_igp_2_cable_length_table[0]))
  43. /**
  44. * igb_check_reset_block - Check if PHY reset is blocked
  45. * @hw: pointer to the HW structure
  46. *
  47. * Read the PHY management control register and check whether a PHY reset
  48. * is blocked. If a reset is not blocked return 0, otherwise
  49. * return E1000_BLK_PHY_RESET (12).
  50. **/
  51. s32 igb_check_reset_block(struct e1000_hw *hw)
  52. {
  53. u32 manc;
  54. manc = rd32(E1000_MANC);
  55. return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
  56. E1000_BLK_PHY_RESET : 0;
  57. }
  58. /**
  59. * igb_get_phy_id - Retrieve the PHY ID and revision
  60. * @hw: pointer to the HW structure
  61. *
  62. * Reads the PHY registers and stores the PHY ID and possibly the PHY
  63. * revision in the hardware structure.
  64. **/
  65. s32 igb_get_phy_id(struct e1000_hw *hw)
  66. {
  67. struct e1000_phy_info *phy = &hw->phy;
  68. s32 ret_val = 0;
  69. u16 phy_id;
  70. ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
  71. if (ret_val)
  72. goto out;
  73. phy->id = (u32)(phy_id << 16);
  74. udelay(20);
  75. ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
  76. if (ret_val)
  77. goto out;
  78. phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
  79. phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
  80. out:
  81. return ret_val;
  82. }
  83. /**
  84. * igb_phy_reset_dsp - Reset PHY DSP
  85. * @hw: pointer to the HW structure
  86. *
  87. * Reset the digital signal processor.
  88. **/
  89. static s32 igb_phy_reset_dsp(struct e1000_hw *hw)
  90. {
  91. s32 ret_val;
  92. ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xC1);
  93. if (ret_val)
  94. goto out;
  95. ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0);
  96. out:
  97. return ret_val;
  98. }
  99. /**
  100. * igb_read_phy_reg_mdic - Read MDI control register
  101. * @hw: pointer to the HW structure
  102. * @offset: register offset to be read
  103. * @data: pointer to the read data
  104. *
  105. * Reads the MDI control regsiter in the PHY at offset and stores the
  106. * information read to data.
  107. **/
  108. static s32 igb_read_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 *data)
  109. {
  110. struct e1000_phy_info *phy = &hw->phy;
  111. u32 i, mdic = 0;
  112. s32 ret_val = 0;
  113. if (offset > MAX_PHY_REG_ADDRESS) {
  114. hw_dbg("PHY Address %d is out of range\n", offset);
  115. ret_val = -E1000_ERR_PARAM;
  116. goto out;
  117. }
  118. /*
  119. * Set up Op-code, Phy Address, and register offset in the MDI
  120. * Control register. The MAC will take care of interfacing with the
  121. * PHY to retrieve the desired data.
  122. */
  123. mdic = ((offset << E1000_MDIC_REG_SHIFT) |
  124. (phy->addr << E1000_MDIC_PHY_SHIFT) |
  125. (E1000_MDIC_OP_READ));
  126. wr32(E1000_MDIC, mdic);
  127. /*
  128. * Poll the ready bit to see if the MDI read completed
  129. * Increasing the time out as testing showed failures with
  130. * the lower time out
  131. */
  132. for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
  133. udelay(50);
  134. mdic = rd32(E1000_MDIC);
  135. if (mdic & E1000_MDIC_READY)
  136. break;
  137. }
  138. if (!(mdic & E1000_MDIC_READY)) {
  139. hw_dbg("MDI Read did not complete\n");
  140. ret_val = -E1000_ERR_PHY;
  141. goto out;
  142. }
  143. if (mdic & E1000_MDIC_ERROR) {
  144. hw_dbg("MDI Error\n");
  145. ret_val = -E1000_ERR_PHY;
  146. goto out;
  147. }
  148. *data = (u16) mdic;
  149. out:
  150. return ret_val;
  151. }
  152. /**
  153. * igb_write_phy_reg_mdic - Write MDI control register
  154. * @hw: pointer to the HW structure
  155. * @offset: register offset to write to
  156. * @data: data to write to register at offset
  157. *
  158. * Writes data to MDI control register in the PHY at offset.
  159. **/
  160. static s32 igb_write_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 data)
  161. {
  162. struct e1000_phy_info *phy = &hw->phy;
  163. u32 i, mdic = 0;
  164. s32 ret_val = 0;
  165. if (offset > MAX_PHY_REG_ADDRESS) {
  166. hw_dbg("PHY Address %d is out of range\n", offset);
  167. ret_val = -E1000_ERR_PARAM;
  168. goto out;
  169. }
  170. /*
  171. * Set up Op-code, Phy Address, and register offset in the MDI
  172. * Control register. The MAC will take care of interfacing with the
  173. * PHY to retrieve the desired data.
  174. */
  175. mdic = (((u32)data) |
  176. (offset << E1000_MDIC_REG_SHIFT) |
  177. (phy->addr << E1000_MDIC_PHY_SHIFT) |
  178. (E1000_MDIC_OP_WRITE));
  179. wr32(E1000_MDIC, mdic);
  180. /*
  181. * Poll the ready bit to see if the MDI read completed
  182. * Increasing the time out as testing showed failures with
  183. * the lower time out
  184. */
  185. for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
  186. udelay(50);
  187. mdic = rd32(E1000_MDIC);
  188. if (mdic & E1000_MDIC_READY)
  189. break;
  190. }
  191. if (!(mdic & E1000_MDIC_READY)) {
  192. hw_dbg("MDI Write did not complete\n");
  193. ret_val = -E1000_ERR_PHY;
  194. goto out;
  195. }
  196. if (mdic & E1000_MDIC_ERROR) {
  197. hw_dbg("MDI Error\n");
  198. ret_val = -E1000_ERR_PHY;
  199. goto out;
  200. }
  201. out:
  202. return ret_val;
  203. }
  204. /**
  205. * igb_read_phy_reg_igp - Read igp PHY register
  206. * @hw: pointer to the HW structure
  207. * @offset: register offset to be read
  208. * @data: pointer to the read data
  209. *
  210. * Acquires semaphore, if necessary, then reads the PHY register at offset
  211. * and storing the retrieved information in data. Release any acquired
  212. * semaphores before exiting.
  213. **/
  214. s32 igb_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data)
  215. {
  216. s32 ret_val = 0;
  217. if (!(hw->phy.ops.acquire))
  218. goto out;
  219. ret_val = hw->phy.ops.acquire(hw);
  220. if (ret_val)
  221. goto out;
  222. if (offset > MAX_PHY_MULTI_PAGE_REG) {
  223. ret_val = igb_write_phy_reg_mdic(hw,
  224. IGP01E1000_PHY_PAGE_SELECT,
  225. (u16)offset);
  226. if (ret_val) {
  227. hw->phy.ops.release(hw);
  228. goto out;
  229. }
  230. }
  231. ret_val = igb_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
  232. data);
  233. hw->phy.ops.release(hw);
  234. out:
  235. return ret_val;
  236. }
  237. /**
  238. * igb_write_phy_reg_igp - Write igp PHY register
  239. * @hw: pointer to the HW structure
  240. * @offset: register offset to write to
  241. * @data: data to write at register offset
  242. *
  243. * Acquires semaphore, if necessary, then writes the data to PHY register
  244. * at the offset. Release any acquired semaphores before exiting.
  245. **/
  246. s32 igb_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data)
  247. {
  248. s32 ret_val = 0;
  249. if (!(hw->phy.ops.acquire))
  250. goto out;
  251. ret_val = hw->phy.ops.acquire(hw);
  252. if (ret_val)
  253. goto out;
  254. if (offset > MAX_PHY_MULTI_PAGE_REG) {
  255. ret_val = igb_write_phy_reg_mdic(hw,
  256. IGP01E1000_PHY_PAGE_SELECT,
  257. (u16)offset);
  258. if (ret_val) {
  259. hw->phy.ops.release(hw);
  260. goto out;
  261. }
  262. }
  263. ret_val = igb_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
  264. data);
  265. hw->phy.ops.release(hw);
  266. out:
  267. return ret_val;
  268. }
  269. /**
  270. * igb_copper_link_setup_m88 - Setup m88 PHY's for copper link
  271. * @hw: pointer to the HW structure
  272. *
  273. * Sets up MDI/MDI-X and polarity for m88 PHY's. If necessary, transmit clock
  274. * and downshift values are set also.
  275. **/
  276. s32 igb_copper_link_setup_m88(struct e1000_hw *hw)
  277. {
  278. struct e1000_phy_info *phy = &hw->phy;
  279. s32 ret_val;
  280. u16 phy_data;
  281. if (phy->reset_disable) {
  282. ret_val = 0;
  283. goto out;
  284. }
  285. /* Enable CRS on TX. This must be set for half-duplex operation. */
  286. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  287. if (ret_val)
  288. goto out;
  289. phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
  290. /*
  291. * Options:
  292. * MDI/MDI-X = 0 (default)
  293. * 0 - Auto for all speeds
  294. * 1 - MDI mode
  295. * 2 - MDI-X mode
  296. * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
  297. */
  298. phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
  299. switch (phy->mdix) {
  300. case 1:
  301. phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
  302. break;
  303. case 2:
  304. phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
  305. break;
  306. case 3:
  307. phy_data |= M88E1000_PSCR_AUTO_X_1000T;
  308. break;
  309. case 0:
  310. default:
  311. phy_data |= M88E1000_PSCR_AUTO_X_MODE;
  312. break;
  313. }
  314. /*
  315. * Options:
  316. * disable_polarity_correction = 0 (default)
  317. * Automatic Correction for Reversed Cable Polarity
  318. * 0 - Disabled
  319. * 1 - Enabled
  320. */
  321. phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
  322. if (phy->disable_polarity_correction == 1)
  323. phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
  324. ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
  325. if (ret_val)
  326. goto out;
  327. if (phy->revision < E1000_REVISION_4) {
  328. /*
  329. * Force TX_CLK in the Extended PHY Specific Control Register
  330. * to 25MHz clock.
  331. */
  332. ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
  333. &phy_data);
  334. if (ret_val)
  335. goto out;
  336. phy_data |= M88E1000_EPSCR_TX_CLK_25;
  337. if ((phy->revision == E1000_REVISION_2) &&
  338. (phy->id == M88E1111_I_PHY_ID)) {
  339. /* 82573L PHY - set the downshift counter to 5x. */
  340. phy_data &= ~M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK;
  341. phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
  342. } else {
  343. /* Configure Master and Slave downshift values */
  344. phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
  345. M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
  346. phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
  347. M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
  348. }
  349. ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
  350. phy_data);
  351. if (ret_val)
  352. goto out;
  353. }
  354. /* Commit the changes. */
  355. ret_val = igb_phy_sw_reset(hw);
  356. if (ret_val) {
  357. hw_dbg("Error committing the PHY changes\n");
  358. goto out;
  359. }
  360. out:
  361. return ret_val;
  362. }
  363. /**
  364. * igb_copper_link_setup_igp - Setup igp PHY's for copper link
  365. * @hw: pointer to the HW structure
  366. *
  367. * Sets up LPLU, MDI/MDI-X, polarity, Smartspeed and Master/Slave config for
  368. * igp PHY's.
  369. **/
  370. s32 igb_copper_link_setup_igp(struct e1000_hw *hw)
  371. {
  372. struct e1000_phy_info *phy = &hw->phy;
  373. s32 ret_val;
  374. u16 data;
  375. if (phy->reset_disable) {
  376. ret_val = 0;
  377. goto out;
  378. }
  379. ret_val = phy->ops.reset(hw);
  380. if (ret_val) {
  381. hw_dbg("Error resetting the PHY.\n");
  382. goto out;
  383. }
  384. /* Wait 15ms for MAC to configure PHY from NVM settings. */
  385. msleep(15);
  386. /*
  387. * The NVM settings will configure LPLU in D3 for
  388. * non-IGP1 PHYs.
  389. */
  390. if (phy->type == e1000_phy_igp) {
  391. /* disable lplu d3 during driver init */
  392. if (phy->ops.set_d3_lplu_state)
  393. ret_val = phy->ops.set_d3_lplu_state(hw, false);
  394. if (ret_val) {
  395. hw_dbg("Error Disabling LPLU D3\n");
  396. goto out;
  397. }
  398. }
  399. /* disable lplu d0 during driver init */
  400. ret_val = phy->ops.set_d0_lplu_state(hw, false);
  401. if (ret_val) {
  402. hw_dbg("Error Disabling LPLU D0\n");
  403. goto out;
  404. }
  405. /* Configure mdi-mdix settings */
  406. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &data);
  407. if (ret_val)
  408. goto out;
  409. data &= ~IGP01E1000_PSCR_AUTO_MDIX;
  410. switch (phy->mdix) {
  411. case 1:
  412. data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
  413. break;
  414. case 2:
  415. data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
  416. break;
  417. case 0:
  418. default:
  419. data |= IGP01E1000_PSCR_AUTO_MDIX;
  420. break;
  421. }
  422. ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, data);
  423. if (ret_val)
  424. goto out;
  425. /* set auto-master slave resolution settings */
  426. if (hw->mac.autoneg) {
  427. /*
  428. * when autonegotiation advertisement is only 1000Mbps then we
  429. * should disable SmartSpeed and enable Auto MasterSlave
  430. * resolution as hardware default.
  431. */
  432. if (phy->autoneg_advertised == ADVERTISE_1000_FULL) {
  433. /* Disable SmartSpeed */
  434. ret_val = phy->ops.read_reg(hw,
  435. IGP01E1000_PHY_PORT_CONFIG,
  436. &data);
  437. if (ret_val)
  438. goto out;
  439. data &= ~IGP01E1000_PSCFR_SMART_SPEED;
  440. ret_val = phy->ops.write_reg(hw,
  441. IGP01E1000_PHY_PORT_CONFIG,
  442. data);
  443. if (ret_val)
  444. goto out;
  445. /* Set auto Master/Slave resolution process */
  446. ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
  447. if (ret_val)
  448. goto out;
  449. data &= ~CR_1000T_MS_ENABLE;
  450. ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
  451. if (ret_val)
  452. goto out;
  453. }
  454. ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
  455. if (ret_val)
  456. goto out;
  457. /* load defaults for future use */
  458. phy->original_ms_type = (data & CR_1000T_MS_ENABLE) ?
  459. ((data & CR_1000T_MS_VALUE) ?
  460. e1000_ms_force_master :
  461. e1000_ms_force_slave) :
  462. e1000_ms_auto;
  463. switch (phy->ms_type) {
  464. case e1000_ms_force_master:
  465. data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
  466. break;
  467. case e1000_ms_force_slave:
  468. data |= CR_1000T_MS_ENABLE;
  469. data &= ~(CR_1000T_MS_VALUE);
  470. break;
  471. case e1000_ms_auto:
  472. data &= ~CR_1000T_MS_ENABLE;
  473. default:
  474. break;
  475. }
  476. ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
  477. if (ret_val)
  478. goto out;
  479. }
  480. out:
  481. return ret_val;
  482. }
  483. /**
  484. * igb_copper_link_autoneg - Setup/Enable autoneg for copper link
  485. * @hw: pointer to the HW structure
  486. *
  487. * Performs initial bounds checking on autoneg advertisement parameter, then
  488. * configure to advertise the full capability. Setup the PHY to autoneg
  489. * and restart the negotiation process between the link partner. If
  490. * autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
  491. **/
  492. s32 igb_copper_link_autoneg(struct e1000_hw *hw)
  493. {
  494. struct e1000_phy_info *phy = &hw->phy;
  495. s32 ret_val;
  496. u16 phy_ctrl;
  497. /*
  498. * Perform some bounds checking on the autoneg advertisement
  499. * parameter.
  500. */
  501. phy->autoneg_advertised &= phy->autoneg_mask;
  502. /*
  503. * If autoneg_advertised is zero, we assume it was not defaulted
  504. * by the calling code so we set to advertise full capability.
  505. */
  506. if (phy->autoneg_advertised == 0)
  507. phy->autoneg_advertised = phy->autoneg_mask;
  508. hw_dbg("Reconfiguring auto-neg advertisement params\n");
  509. ret_val = igb_phy_setup_autoneg(hw);
  510. if (ret_val) {
  511. hw_dbg("Error Setting up Auto-Negotiation\n");
  512. goto out;
  513. }
  514. hw_dbg("Restarting Auto-Neg\n");
  515. /*
  516. * Restart auto-negotiation by setting the Auto Neg Enable bit and
  517. * the Auto Neg Restart bit in the PHY control register.
  518. */
  519. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
  520. if (ret_val)
  521. goto out;
  522. phy_ctrl |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
  523. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
  524. if (ret_val)
  525. goto out;
  526. /*
  527. * Does the user want to wait for Auto-Neg to complete here, or
  528. * check at a later time (for example, callback routine).
  529. */
  530. if (phy->autoneg_wait_to_complete) {
  531. ret_val = igb_wait_autoneg(hw);
  532. if (ret_val) {
  533. hw_dbg("Error while waiting for "
  534. "autoneg to complete\n");
  535. goto out;
  536. }
  537. }
  538. hw->mac.get_link_status = true;
  539. out:
  540. return ret_val;
  541. }
  542. /**
  543. * igb_phy_setup_autoneg - Configure PHY for auto-negotiation
  544. * @hw: pointer to the HW structure
  545. *
  546. * Reads the MII auto-neg advertisement register and/or the 1000T control
  547. * register and if the PHY is already setup for auto-negotiation, then
  548. * return successful. Otherwise, setup advertisement and flow control to
  549. * the appropriate values for the wanted auto-negotiation.
  550. **/
  551. static s32 igb_phy_setup_autoneg(struct e1000_hw *hw)
  552. {
  553. struct e1000_phy_info *phy = &hw->phy;
  554. s32 ret_val;
  555. u16 mii_autoneg_adv_reg;
  556. u16 mii_1000t_ctrl_reg = 0;
  557. phy->autoneg_advertised &= phy->autoneg_mask;
  558. /* Read the MII Auto-Neg Advertisement Register (Address 4). */
  559. ret_val = phy->ops.read_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
  560. if (ret_val)
  561. goto out;
  562. if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
  563. /* Read the MII 1000Base-T Control Register (Address 9). */
  564. ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL,
  565. &mii_1000t_ctrl_reg);
  566. if (ret_val)
  567. goto out;
  568. }
  569. /*
  570. * Need to parse both autoneg_advertised and fc and set up
  571. * the appropriate PHY registers. First we will parse for
  572. * autoneg_advertised software override. Since we can advertise
  573. * a plethora of combinations, we need to check each bit
  574. * individually.
  575. */
  576. /*
  577. * First we clear all the 10/100 mb speed bits in the Auto-Neg
  578. * Advertisement Register (Address 4) and the 1000 mb speed bits in
  579. * the 1000Base-T Control Register (Address 9).
  580. */
  581. mii_autoneg_adv_reg &= ~(NWAY_AR_100TX_FD_CAPS |
  582. NWAY_AR_100TX_HD_CAPS |
  583. NWAY_AR_10T_FD_CAPS |
  584. NWAY_AR_10T_HD_CAPS);
  585. mii_1000t_ctrl_reg &= ~(CR_1000T_HD_CAPS | CR_1000T_FD_CAPS);
  586. hw_dbg("autoneg_advertised %x\n", phy->autoneg_advertised);
  587. /* Do we want to advertise 10 Mb Half Duplex? */
  588. if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
  589. hw_dbg("Advertise 10mb Half duplex\n");
  590. mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
  591. }
  592. /* Do we want to advertise 10 Mb Full Duplex? */
  593. if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
  594. hw_dbg("Advertise 10mb Full duplex\n");
  595. mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
  596. }
  597. /* Do we want to advertise 100 Mb Half Duplex? */
  598. if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
  599. hw_dbg("Advertise 100mb Half duplex\n");
  600. mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
  601. }
  602. /* Do we want to advertise 100 Mb Full Duplex? */
  603. if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
  604. hw_dbg("Advertise 100mb Full duplex\n");
  605. mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
  606. }
  607. /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
  608. if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
  609. hw_dbg("Advertise 1000mb Half duplex request denied!\n");
  610. /* Do we want to advertise 1000 Mb Full Duplex? */
  611. if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
  612. hw_dbg("Advertise 1000mb Full duplex\n");
  613. mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
  614. }
  615. /*
  616. * Check for a software override of the flow control settings, and
  617. * setup the PHY advertisement registers accordingly. If
  618. * auto-negotiation is enabled, then software will have to set the
  619. * "PAUSE" bits to the correct value in the Auto-Negotiation
  620. * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-
  621. * negotiation.
  622. *
  623. * The possible values of the "fc" parameter are:
  624. * 0: Flow control is completely disabled
  625. * 1: Rx flow control is enabled (we can receive pause frames
  626. * but not send pause frames).
  627. * 2: Tx flow control is enabled (we can send pause frames
  628. * but we do not support receiving pause frames).
  629. * 3: Both Rx and TX flow control (symmetric) are enabled.
  630. * other: No software override. The flow control configuration
  631. * in the EEPROM is used.
  632. */
  633. switch (hw->fc.type) {
  634. case e1000_fc_none:
  635. /*
  636. * Flow control (RX & TX) is completely disabled by a
  637. * software over-ride.
  638. */
  639. mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  640. break;
  641. case e1000_fc_rx_pause:
  642. /*
  643. * RX Flow control is enabled, and TX Flow control is
  644. * disabled, by a software over-ride.
  645. *
  646. * Since there really isn't a way to advertise that we are
  647. * capable of RX Pause ONLY, we will advertise that we
  648. * support both symmetric and asymmetric RX PAUSE. Later
  649. * (in e1000_config_fc_after_link_up) we will disable the
  650. * hw's ability to send PAUSE frames.
  651. */
  652. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  653. break;
  654. case e1000_fc_tx_pause:
  655. /*
  656. * TX Flow control is enabled, and RX Flow control is
  657. * disabled, by a software over-ride.
  658. */
  659. mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
  660. mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
  661. break;
  662. case e1000_fc_full:
  663. /*
  664. * Flow control (both RX and TX) is enabled by a software
  665. * over-ride.
  666. */
  667. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  668. break;
  669. default:
  670. hw_dbg("Flow control param set incorrectly\n");
  671. ret_val = -E1000_ERR_CONFIG;
  672. goto out;
  673. }
  674. ret_val = phy->ops.write_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
  675. if (ret_val)
  676. goto out;
  677. hw_dbg("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
  678. if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
  679. ret_val = phy->ops.write_reg(hw,
  680. PHY_1000T_CTRL,
  681. mii_1000t_ctrl_reg);
  682. if (ret_val)
  683. goto out;
  684. }
  685. out:
  686. return ret_val;
  687. }
  688. /**
  689. * igb_phy_force_speed_duplex_igp - Force speed/duplex for igp PHY
  690. * @hw: pointer to the HW structure
  691. *
  692. * Calls the PHY setup function to force speed and duplex. Clears the
  693. * auto-crossover to force MDI manually. Waits for link and returns
  694. * successful if link up is successful, else -E1000_ERR_PHY (-2).
  695. **/
  696. s32 igb_phy_force_speed_duplex_igp(struct e1000_hw *hw)
  697. {
  698. struct e1000_phy_info *phy = &hw->phy;
  699. s32 ret_val;
  700. u16 phy_data;
  701. bool link;
  702. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
  703. if (ret_val)
  704. goto out;
  705. igb_phy_force_speed_duplex_setup(hw, &phy_data);
  706. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
  707. if (ret_val)
  708. goto out;
  709. /*
  710. * Clear Auto-Crossover to force MDI manually. IGP requires MDI
  711. * forced whenever speed and duplex are forced.
  712. */
  713. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
  714. if (ret_val)
  715. goto out;
  716. phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
  717. phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
  718. ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
  719. if (ret_val)
  720. goto out;
  721. hw_dbg("IGP PSCR: %X\n", phy_data);
  722. udelay(1);
  723. if (phy->autoneg_wait_to_complete) {
  724. hw_dbg("Waiting for forced speed/duplex link on IGP phy.\n");
  725. ret_val = igb_phy_has_link(hw,
  726. PHY_FORCE_LIMIT,
  727. 100000,
  728. &link);
  729. if (ret_val)
  730. goto out;
  731. if (!link)
  732. hw_dbg("Link taking longer than expected.\n");
  733. /* Try once more */
  734. ret_val = igb_phy_has_link(hw,
  735. PHY_FORCE_LIMIT,
  736. 100000,
  737. &link);
  738. if (ret_val)
  739. goto out;
  740. }
  741. out:
  742. return ret_val;
  743. }
  744. /**
  745. * igb_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY
  746. * @hw: pointer to the HW structure
  747. *
  748. * Calls the PHY setup function to force speed and duplex. Clears the
  749. * auto-crossover to force MDI manually. Resets the PHY to commit the
  750. * changes. If time expires while waiting for link up, we reset the DSP.
  751. * After reset, TX_CLK and CRS on TX must be set. Return successful upon
  752. * successful completion, else return corresponding error code.
  753. **/
  754. s32 igb_phy_force_speed_duplex_m88(struct e1000_hw *hw)
  755. {
  756. struct e1000_phy_info *phy = &hw->phy;
  757. s32 ret_val;
  758. u16 phy_data;
  759. bool link;
  760. /*
  761. * Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
  762. * forced whenever speed and duplex are forced.
  763. */
  764. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  765. if (ret_val)
  766. goto out;
  767. phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
  768. ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
  769. if (ret_val)
  770. goto out;
  771. hw_dbg("M88E1000 PSCR: %X\n", phy_data);
  772. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
  773. if (ret_val)
  774. goto out;
  775. igb_phy_force_speed_duplex_setup(hw, &phy_data);
  776. /* Reset the phy to commit changes. */
  777. phy_data |= MII_CR_RESET;
  778. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
  779. if (ret_val)
  780. goto out;
  781. udelay(1);
  782. if (phy->autoneg_wait_to_complete) {
  783. hw_dbg("Waiting for forced speed/duplex link on M88 phy.\n");
  784. ret_val = igb_phy_has_link(hw,
  785. PHY_FORCE_LIMIT,
  786. 100000,
  787. &link);
  788. if (ret_val)
  789. goto out;
  790. if (!link) {
  791. /*
  792. * We didn't get link.
  793. * Reset the DSP and cross our fingers.
  794. */
  795. ret_val = phy->ops.write_reg(hw,
  796. M88E1000_PHY_PAGE_SELECT,
  797. 0x001d);
  798. if (ret_val)
  799. goto out;
  800. ret_val = igb_phy_reset_dsp(hw);
  801. if (ret_val)
  802. goto out;
  803. }
  804. /* Try once more */
  805. ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT,
  806. 100000, &link);
  807. if (ret_val)
  808. goto out;
  809. }
  810. ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
  811. if (ret_val)
  812. goto out;
  813. /*
  814. * Resetting the phy means we need to re-force TX_CLK in the
  815. * Extended PHY Specific Control Register to 25MHz clock from
  816. * the reset value of 2.5MHz.
  817. */
  818. phy_data |= M88E1000_EPSCR_TX_CLK_25;
  819. ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
  820. if (ret_val)
  821. goto out;
  822. /*
  823. * In addition, we must re-enable CRS on Tx for both half and full
  824. * duplex.
  825. */
  826. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  827. if (ret_val)
  828. goto out;
  829. phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
  830. ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
  831. out:
  832. return ret_val;
  833. }
  834. /**
  835. * igb_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex
  836. * @hw: pointer to the HW structure
  837. * @phy_ctrl: pointer to current value of PHY_CONTROL
  838. *
  839. * Forces speed and duplex on the PHY by doing the following: disable flow
  840. * control, force speed/duplex on the MAC, disable auto speed detection,
  841. * disable auto-negotiation, configure duplex, configure speed, configure
  842. * the collision distance, write configuration to CTRL register. The
  843. * caller must write to the PHY_CONTROL register for these settings to
  844. * take affect.
  845. **/
  846. static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
  847. u16 *phy_ctrl)
  848. {
  849. struct e1000_mac_info *mac = &hw->mac;
  850. u32 ctrl;
  851. /* Turn off flow control when forcing speed/duplex */
  852. hw->fc.type = e1000_fc_none;
  853. /* Force speed/duplex on the mac */
  854. ctrl = rd32(E1000_CTRL);
  855. ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
  856. ctrl &= ~E1000_CTRL_SPD_SEL;
  857. /* Disable Auto Speed Detection */
  858. ctrl &= ~E1000_CTRL_ASDE;
  859. /* Disable autoneg on the phy */
  860. *phy_ctrl &= ~MII_CR_AUTO_NEG_EN;
  861. /* Forcing Full or Half Duplex? */
  862. if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {
  863. ctrl &= ~E1000_CTRL_FD;
  864. *phy_ctrl &= ~MII_CR_FULL_DUPLEX;
  865. hw_dbg("Half Duplex\n");
  866. } else {
  867. ctrl |= E1000_CTRL_FD;
  868. *phy_ctrl |= MII_CR_FULL_DUPLEX;
  869. hw_dbg("Full Duplex\n");
  870. }
  871. /* Forcing 10mb or 100mb? */
  872. if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {
  873. ctrl |= E1000_CTRL_SPD_100;
  874. *phy_ctrl |= MII_CR_SPEED_100;
  875. *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
  876. hw_dbg("Forcing 100mb\n");
  877. } else {
  878. ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
  879. *phy_ctrl |= MII_CR_SPEED_10;
  880. *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
  881. hw_dbg("Forcing 10mb\n");
  882. }
  883. igb_config_collision_dist(hw);
  884. wr32(E1000_CTRL, ctrl);
  885. }
  886. /**
  887. * igb_set_d3_lplu_state - Sets low power link up state for D3
  888. * @hw: pointer to the HW structure
  889. * @active: boolean used to enable/disable lplu
  890. *
  891. * Success returns 0, Failure returns 1
  892. *
  893. * The low power link up (lplu) state is set to the power management level D3
  894. * and SmartSpeed is disabled when active is true, else clear lplu for D3
  895. * and enable Smartspeed. LPLU and Smartspeed are mutually exclusive. LPLU
  896. * is used during Dx states where the power conservation is most important.
  897. * During driver activity, SmartSpeed should be enabled so performance is
  898. * maintained.
  899. **/
  900. s32 igb_set_d3_lplu_state(struct e1000_hw *hw, bool active)
  901. {
  902. struct e1000_phy_info *phy = &hw->phy;
  903. s32 ret_val;
  904. u16 data;
  905. ret_val = phy->ops.read_reg(hw, IGP02E1000_PHY_POWER_MGMT, &data);
  906. if (ret_val)
  907. goto out;
  908. if (!active) {
  909. data &= ~IGP02E1000_PM_D3_LPLU;
  910. ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
  911. data);
  912. if (ret_val)
  913. goto out;
  914. /*
  915. * LPLU and SmartSpeed are mutually exclusive. LPLU is used
  916. * during Dx states where the power conservation is most
  917. * important. During driver activity we should enable
  918. * SmartSpeed, so performance is maintained.
  919. */
  920. if (phy->smart_speed == e1000_smart_speed_on) {
  921. ret_val = phy->ops.read_reg(hw,
  922. IGP01E1000_PHY_PORT_CONFIG,
  923. &data);
  924. if (ret_val)
  925. goto out;
  926. data |= IGP01E1000_PSCFR_SMART_SPEED;
  927. ret_val = phy->ops.write_reg(hw,
  928. IGP01E1000_PHY_PORT_CONFIG,
  929. data);
  930. if (ret_val)
  931. goto out;
  932. } else if (phy->smart_speed == e1000_smart_speed_off) {
  933. ret_val = phy->ops.read_reg(hw,
  934. IGP01E1000_PHY_PORT_CONFIG,
  935. &data);
  936. if (ret_val)
  937. goto out;
  938. data &= ~IGP01E1000_PSCFR_SMART_SPEED;
  939. ret_val = phy->ops.write_reg(hw,
  940. IGP01E1000_PHY_PORT_CONFIG,
  941. data);
  942. if (ret_val)
  943. goto out;
  944. }
  945. } else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||
  946. (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||
  947. (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {
  948. data |= IGP02E1000_PM_D3_LPLU;
  949. ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
  950. data);
  951. if (ret_val)
  952. goto out;
  953. /* When LPLU is enabled, we should disable SmartSpeed */
  954. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
  955. &data);
  956. if (ret_val)
  957. goto out;
  958. data &= ~IGP01E1000_PSCFR_SMART_SPEED;
  959. ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
  960. data);
  961. }
  962. out:
  963. return ret_val;
  964. }
  965. /**
  966. * igb_check_downshift - Checks whether a downshift in speed occured
  967. * @hw: pointer to the HW structure
  968. *
  969. * Success returns 0, Failure returns 1
  970. *
  971. * A downshift is detected by querying the PHY link health.
  972. **/
  973. s32 igb_check_downshift(struct e1000_hw *hw)
  974. {
  975. struct e1000_phy_info *phy = &hw->phy;
  976. s32 ret_val;
  977. u16 phy_data, offset, mask;
  978. switch (phy->type) {
  979. case e1000_phy_m88:
  980. case e1000_phy_gg82563:
  981. offset = M88E1000_PHY_SPEC_STATUS;
  982. mask = M88E1000_PSSR_DOWNSHIFT;
  983. break;
  984. case e1000_phy_igp_2:
  985. case e1000_phy_igp:
  986. case e1000_phy_igp_3:
  987. offset = IGP01E1000_PHY_LINK_HEALTH;
  988. mask = IGP01E1000_PLHR_SS_DOWNGRADE;
  989. break;
  990. default:
  991. /* speed downshift not supported */
  992. phy->speed_downgraded = false;
  993. ret_val = 0;
  994. goto out;
  995. }
  996. ret_val = phy->ops.read_reg(hw, offset, &phy_data);
  997. if (!ret_val)
  998. phy->speed_downgraded = (phy_data & mask) ? true : false;
  999. out:
  1000. return ret_val;
  1001. }
  1002. /**
  1003. * igb_check_polarity_m88 - Checks the polarity.
  1004. * @hw: pointer to the HW structure
  1005. *
  1006. * Success returns 0, Failure returns -E1000_ERR_PHY (-2)
  1007. *
  1008. * Polarity is determined based on the PHY specific status register.
  1009. **/
  1010. static s32 igb_check_polarity_m88(struct e1000_hw *hw)
  1011. {
  1012. struct e1000_phy_info *phy = &hw->phy;
  1013. s32 ret_val;
  1014. u16 data;
  1015. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &data);
  1016. if (!ret_val)
  1017. phy->cable_polarity = (data & M88E1000_PSSR_REV_POLARITY)
  1018. ? e1000_rev_polarity_reversed
  1019. : e1000_rev_polarity_normal;
  1020. return ret_val;
  1021. }
  1022. /**
  1023. * igb_check_polarity_igp - Checks the polarity.
  1024. * @hw: pointer to the HW structure
  1025. *
  1026. * Success returns 0, Failure returns -E1000_ERR_PHY (-2)
  1027. *
  1028. * Polarity is determined based on the PHY port status register, and the
  1029. * current speed (since there is no polarity at 100Mbps).
  1030. **/
  1031. static s32 igb_check_polarity_igp(struct e1000_hw *hw)
  1032. {
  1033. struct e1000_phy_info *phy = &hw->phy;
  1034. s32 ret_val;
  1035. u16 data, offset, mask;
  1036. /*
  1037. * Polarity is determined based on the speed of
  1038. * our connection.
  1039. */
  1040. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
  1041. if (ret_val)
  1042. goto out;
  1043. if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
  1044. IGP01E1000_PSSR_SPEED_1000MBPS) {
  1045. offset = IGP01E1000_PHY_PCS_INIT_REG;
  1046. mask = IGP01E1000_PHY_POLARITY_MASK;
  1047. } else {
  1048. /*
  1049. * This really only applies to 10Mbps since
  1050. * there is no polarity for 100Mbps (always 0).
  1051. */
  1052. offset = IGP01E1000_PHY_PORT_STATUS;
  1053. mask = IGP01E1000_PSSR_POLARITY_REVERSED;
  1054. }
  1055. ret_val = phy->ops.read_reg(hw, offset, &data);
  1056. if (!ret_val)
  1057. phy->cable_polarity = (data & mask)
  1058. ? e1000_rev_polarity_reversed
  1059. : e1000_rev_polarity_normal;
  1060. out:
  1061. return ret_val;
  1062. }
  1063. /**
  1064. * igb_wait_autoneg - Wait for auto-neg compeletion
  1065. * @hw: pointer to the HW structure
  1066. *
  1067. * Waits for auto-negotiation to complete or for the auto-negotiation time
  1068. * limit to expire, which ever happens first.
  1069. **/
  1070. static s32 igb_wait_autoneg(struct e1000_hw *hw)
  1071. {
  1072. s32 ret_val = 0;
  1073. u16 i, phy_status;
  1074. /* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
  1075. for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
  1076. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1077. if (ret_val)
  1078. break;
  1079. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1080. if (ret_val)
  1081. break;
  1082. if (phy_status & MII_SR_AUTONEG_COMPLETE)
  1083. break;
  1084. msleep(100);
  1085. }
  1086. /*
  1087. * PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
  1088. * has completed.
  1089. */
  1090. return ret_val;
  1091. }
  1092. /**
  1093. * igb_phy_has_link - Polls PHY for link
  1094. * @hw: pointer to the HW structure
  1095. * @iterations: number of times to poll for link
  1096. * @usec_interval: delay between polling attempts
  1097. * @success: pointer to whether polling was successful or not
  1098. *
  1099. * Polls the PHY status register for link, 'iterations' number of times.
  1100. **/
  1101. s32 igb_phy_has_link(struct e1000_hw *hw, u32 iterations,
  1102. u32 usec_interval, bool *success)
  1103. {
  1104. s32 ret_val = 0;
  1105. u16 i, phy_status;
  1106. for (i = 0; i < iterations; i++) {
  1107. /*
  1108. * Some PHYs require the PHY_STATUS register to be read
  1109. * twice due to the link bit being sticky. No harm doing
  1110. * it across the board.
  1111. */
  1112. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1113. if (ret_val)
  1114. break;
  1115. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1116. if (ret_val)
  1117. break;
  1118. if (phy_status & MII_SR_LINK_STATUS)
  1119. break;
  1120. if (usec_interval >= 1000)
  1121. mdelay(usec_interval/1000);
  1122. else
  1123. udelay(usec_interval);
  1124. }
  1125. *success = (i < iterations) ? true : false;
  1126. return ret_val;
  1127. }
  1128. /**
  1129. * igb_get_cable_length_m88 - Determine cable length for m88 PHY
  1130. * @hw: pointer to the HW structure
  1131. *
  1132. * Reads the PHY specific status register to retrieve the cable length
  1133. * information. The cable length is determined by averaging the minimum and
  1134. * maximum values to get the "average" cable length. The m88 PHY has four
  1135. * possible cable length values, which are:
  1136. * Register Value Cable Length
  1137. * 0 < 50 meters
  1138. * 1 50 - 80 meters
  1139. * 2 80 - 110 meters
  1140. * 3 110 - 140 meters
  1141. * 4 > 140 meters
  1142. **/
  1143. s32 igb_get_cable_length_m88(struct e1000_hw *hw)
  1144. {
  1145. struct e1000_phy_info *phy = &hw->phy;
  1146. s32 ret_val;
  1147. u16 phy_data, index;
  1148. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
  1149. if (ret_val)
  1150. goto out;
  1151. index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
  1152. M88E1000_PSSR_CABLE_LENGTH_SHIFT;
  1153. phy->min_cable_length = e1000_m88_cable_length_table[index];
  1154. phy->max_cable_length = e1000_m88_cable_length_table[index+1];
  1155. phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
  1156. out:
  1157. return ret_val;
  1158. }
  1159. /**
  1160. * igb_get_cable_length_igp_2 - Determine cable length for igp2 PHY
  1161. * @hw: pointer to the HW structure
  1162. *
  1163. * The automatic gain control (agc) normalizes the amplitude of the
  1164. * received signal, adjusting for the attenuation produced by the
  1165. * cable. By reading the AGC registers, which represent the
  1166. * combination of coarse and fine gain value, the value can be put
  1167. * into a lookup table to obtain the approximate cable length
  1168. * for each channel.
  1169. **/
  1170. s32 igb_get_cable_length_igp_2(struct e1000_hw *hw)
  1171. {
  1172. struct e1000_phy_info *phy = &hw->phy;
  1173. s32 ret_val = 0;
  1174. u16 phy_data, i, agc_value = 0;
  1175. u16 cur_agc_index, max_agc_index = 0;
  1176. u16 min_agc_index = IGP02E1000_CABLE_LENGTH_TABLE_SIZE - 1;
  1177. u16 agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] =
  1178. {IGP02E1000_PHY_AGC_A,
  1179. IGP02E1000_PHY_AGC_B,
  1180. IGP02E1000_PHY_AGC_C,
  1181. IGP02E1000_PHY_AGC_D};
  1182. /* Read the AGC registers for all channels */
  1183. for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
  1184. ret_val = phy->ops.read_reg(hw, agc_reg_array[i], &phy_data);
  1185. if (ret_val)
  1186. goto out;
  1187. /*
  1188. * Getting bits 15:9, which represent the combination of
  1189. * coarse and fine gain values. The result is a number
  1190. * that can be put into the lookup table to obtain the
  1191. * approximate cable length.
  1192. */
  1193. cur_agc_index = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
  1194. IGP02E1000_AGC_LENGTH_MASK;
  1195. /* Array index bound check. */
  1196. if ((cur_agc_index >= IGP02E1000_CABLE_LENGTH_TABLE_SIZE) ||
  1197. (cur_agc_index == 0)) {
  1198. ret_val = -E1000_ERR_PHY;
  1199. goto out;
  1200. }
  1201. /* Remove min & max AGC values from calculation. */
  1202. if (e1000_igp_2_cable_length_table[min_agc_index] >
  1203. e1000_igp_2_cable_length_table[cur_agc_index])
  1204. min_agc_index = cur_agc_index;
  1205. if (e1000_igp_2_cable_length_table[max_agc_index] <
  1206. e1000_igp_2_cable_length_table[cur_agc_index])
  1207. max_agc_index = cur_agc_index;
  1208. agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
  1209. }
  1210. agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
  1211. e1000_igp_2_cable_length_table[max_agc_index]);
  1212. agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
  1213. /* Calculate cable length with the error range of +/- 10 meters. */
  1214. phy->min_cable_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
  1215. (agc_value - IGP02E1000_AGC_RANGE) : 0;
  1216. phy->max_cable_length = agc_value + IGP02E1000_AGC_RANGE;
  1217. phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
  1218. out:
  1219. return ret_val;
  1220. }
  1221. /**
  1222. * igb_get_phy_info_m88 - Retrieve PHY information
  1223. * @hw: pointer to the HW structure
  1224. *
  1225. * Valid for only copper links. Read the PHY status register (sticky read)
  1226. * to verify that link is up. Read the PHY special control register to
  1227. * determine the polarity and 10base-T extended distance. Read the PHY
  1228. * special status register to determine MDI/MDIx and current speed. If
  1229. * speed is 1000, then determine cable length, local and remote receiver.
  1230. **/
  1231. s32 igb_get_phy_info_m88(struct e1000_hw *hw)
  1232. {
  1233. struct e1000_phy_info *phy = &hw->phy;
  1234. s32 ret_val;
  1235. u16 phy_data;
  1236. bool link;
  1237. if (phy->media_type != e1000_media_type_copper) {
  1238. hw_dbg("Phy info is only valid for copper media\n");
  1239. ret_val = -E1000_ERR_CONFIG;
  1240. goto out;
  1241. }
  1242. ret_val = igb_phy_has_link(hw, 1, 0, &link);
  1243. if (ret_val)
  1244. goto out;
  1245. if (!link) {
  1246. hw_dbg("Phy info is only valid if link is up\n");
  1247. ret_val = -E1000_ERR_CONFIG;
  1248. goto out;
  1249. }
  1250. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  1251. if (ret_val)
  1252. goto out;
  1253. phy->polarity_correction = (phy_data & M88E1000_PSCR_POLARITY_REVERSAL)
  1254. ? true : false;
  1255. ret_val = igb_check_polarity_m88(hw);
  1256. if (ret_val)
  1257. goto out;
  1258. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
  1259. if (ret_val)
  1260. goto out;
  1261. phy->is_mdix = (phy_data & M88E1000_PSSR_MDIX) ? true : false;
  1262. if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
  1263. ret_val = phy->ops.get_cable_length(hw);
  1264. if (ret_val)
  1265. goto out;
  1266. ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &phy_data);
  1267. if (ret_val)
  1268. goto out;
  1269. phy->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS)
  1270. ? e1000_1000t_rx_status_ok
  1271. : e1000_1000t_rx_status_not_ok;
  1272. phy->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS)
  1273. ? e1000_1000t_rx_status_ok
  1274. : e1000_1000t_rx_status_not_ok;
  1275. } else {
  1276. /* Set values to "undefined" */
  1277. phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
  1278. phy->local_rx = e1000_1000t_rx_status_undefined;
  1279. phy->remote_rx = e1000_1000t_rx_status_undefined;
  1280. }
  1281. out:
  1282. return ret_val;
  1283. }
  1284. /**
  1285. * igb_get_phy_info_igp - Retrieve igp PHY information
  1286. * @hw: pointer to the HW structure
  1287. *
  1288. * Read PHY status to determine if link is up. If link is up, then
  1289. * set/determine 10base-T extended distance and polarity correction. Read
  1290. * PHY port status to determine MDI/MDIx and speed. Based on the speed,
  1291. * determine on the cable length, local and remote receiver.
  1292. **/
  1293. s32 igb_get_phy_info_igp(struct e1000_hw *hw)
  1294. {
  1295. struct e1000_phy_info *phy = &hw->phy;
  1296. s32 ret_val;
  1297. u16 data;
  1298. bool link;
  1299. ret_val = igb_phy_has_link(hw, 1, 0, &link);
  1300. if (ret_val)
  1301. goto out;
  1302. if (!link) {
  1303. hw_dbg("Phy info is only valid if link is up\n");
  1304. ret_val = -E1000_ERR_CONFIG;
  1305. goto out;
  1306. }
  1307. phy->polarity_correction = true;
  1308. ret_val = igb_check_polarity_igp(hw);
  1309. if (ret_val)
  1310. goto out;
  1311. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
  1312. if (ret_val)
  1313. goto out;
  1314. phy->is_mdix = (data & IGP01E1000_PSSR_MDIX) ? true : false;
  1315. if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
  1316. IGP01E1000_PSSR_SPEED_1000MBPS) {
  1317. ret_val = phy->ops.get_cable_length(hw);
  1318. if (ret_val)
  1319. goto out;
  1320. ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
  1321. if (ret_val)
  1322. goto out;
  1323. phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
  1324. ? e1000_1000t_rx_status_ok
  1325. : e1000_1000t_rx_status_not_ok;
  1326. phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
  1327. ? e1000_1000t_rx_status_ok
  1328. : e1000_1000t_rx_status_not_ok;
  1329. } else {
  1330. phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
  1331. phy->local_rx = e1000_1000t_rx_status_undefined;
  1332. phy->remote_rx = e1000_1000t_rx_status_undefined;
  1333. }
  1334. out:
  1335. return ret_val;
  1336. }
  1337. /**
  1338. * igb_phy_sw_reset - PHY software reset
  1339. * @hw: pointer to the HW structure
  1340. *
  1341. * Does a software reset of the PHY by reading the PHY control register and
  1342. * setting/write the control register reset bit to the PHY.
  1343. **/
  1344. s32 igb_phy_sw_reset(struct e1000_hw *hw)
  1345. {
  1346. s32 ret_val;
  1347. u16 phy_ctrl;
  1348. ret_val = hw->phy.ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
  1349. if (ret_val)
  1350. goto out;
  1351. phy_ctrl |= MII_CR_RESET;
  1352. ret_val = hw->phy.ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
  1353. if (ret_val)
  1354. goto out;
  1355. udelay(1);
  1356. out:
  1357. return ret_val;
  1358. }
  1359. /**
  1360. * igb_phy_hw_reset - PHY hardware reset
  1361. * @hw: pointer to the HW structure
  1362. *
  1363. * Verify the reset block is not blocking us from resetting. Acquire
  1364. * semaphore (if necessary) and read/set/write the device control reset
  1365. * bit in the PHY. Wait the appropriate delay time for the device to
  1366. * reset and relase the semaphore (if necessary).
  1367. **/
  1368. s32 igb_phy_hw_reset(struct e1000_hw *hw)
  1369. {
  1370. struct e1000_phy_info *phy = &hw->phy;
  1371. s32 ret_val;
  1372. u32 ctrl;
  1373. ret_val = igb_check_reset_block(hw);
  1374. if (ret_val) {
  1375. ret_val = 0;
  1376. goto out;
  1377. }
  1378. ret_val = phy->ops.acquire(hw);
  1379. if (ret_val)
  1380. goto out;
  1381. ctrl = rd32(E1000_CTRL);
  1382. wr32(E1000_CTRL, ctrl | E1000_CTRL_PHY_RST);
  1383. wrfl();
  1384. udelay(phy->reset_delay_us);
  1385. wr32(E1000_CTRL, ctrl);
  1386. wrfl();
  1387. udelay(150);
  1388. phy->ops.release(hw);
  1389. ret_val = phy->ops.get_cfg_done(hw);
  1390. out:
  1391. return ret_val;
  1392. }
  1393. /**
  1394. * igb_phy_init_script_igp3 - Inits the IGP3 PHY
  1395. * @hw: pointer to the HW structure
  1396. *
  1397. * Initializes a Intel Gigabit PHY3 when an EEPROM is not present.
  1398. **/
  1399. s32 igb_phy_init_script_igp3(struct e1000_hw *hw)
  1400. {
  1401. hw_dbg("Running IGP 3 PHY init script\n");
  1402. /* PHY init IGP 3 */
  1403. /* Enable rise/fall, 10-mode work in class-A */
  1404. hw->phy.ops.write_reg(hw, 0x2F5B, 0x9018);
  1405. /* Remove all caps from Replica path filter */
  1406. hw->phy.ops.write_reg(hw, 0x2F52, 0x0000);
  1407. /* Bias trimming for ADC, AFE and Driver (Default) */
  1408. hw->phy.ops.write_reg(hw, 0x2FB1, 0x8B24);
  1409. /* Increase Hybrid poly bias */
  1410. hw->phy.ops.write_reg(hw, 0x2FB2, 0xF8F0);
  1411. /* Add 4% to TX amplitude in Giga mode */
  1412. hw->phy.ops.write_reg(hw, 0x2010, 0x10B0);
  1413. /* Disable trimming (TTT) */
  1414. hw->phy.ops.write_reg(hw, 0x2011, 0x0000);
  1415. /* Poly DC correction to 94.6% + 2% for all channels */
  1416. hw->phy.ops.write_reg(hw, 0x20DD, 0x249A);
  1417. /* ABS DC correction to 95.9% */
  1418. hw->phy.ops.write_reg(hw, 0x20DE, 0x00D3);
  1419. /* BG temp curve trim */
  1420. hw->phy.ops.write_reg(hw, 0x28B4, 0x04CE);
  1421. /* Increasing ADC OPAMP stage 1 currents to max */
  1422. hw->phy.ops.write_reg(hw, 0x2F70, 0x29E4);
  1423. /* Force 1000 ( required for enabling PHY regs configuration) */
  1424. hw->phy.ops.write_reg(hw, 0x0000, 0x0140);
  1425. /* Set upd_freq to 6 */
  1426. hw->phy.ops.write_reg(hw, 0x1F30, 0x1606);
  1427. /* Disable NPDFE */
  1428. hw->phy.ops.write_reg(hw, 0x1F31, 0xB814);
  1429. /* Disable adaptive fixed FFE (Default) */
  1430. hw->phy.ops.write_reg(hw, 0x1F35, 0x002A);
  1431. /* Enable FFE hysteresis */
  1432. hw->phy.ops.write_reg(hw, 0x1F3E, 0x0067);
  1433. /* Fixed FFE for short cable lengths */
  1434. hw->phy.ops.write_reg(hw, 0x1F54, 0x0065);
  1435. /* Fixed FFE for medium cable lengths */
  1436. hw->phy.ops.write_reg(hw, 0x1F55, 0x002A);
  1437. /* Fixed FFE for long cable lengths */
  1438. hw->phy.ops.write_reg(hw, 0x1F56, 0x002A);
  1439. /* Enable Adaptive Clip Threshold */
  1440. hw->phy.ops.write_reg(hw, 0x1F72, 0x3FB0);
  1441. /* AHT reset limit to 1 */
  1442. hw->phy.ops.write_reg(hw, 0x1F76, 0xC0FF);
  1443. /* Set AHT master delay to 127 msec */
  1444. hw->phy.ops.write_reg(hw, 0x1F77, 0x1DEC);
  1445. /* Set scan bits for AHT */
  1446. hw->phy.ops.write_reg(hw, 0x1F78, 0xF9EF);
  1447. /* Set AHT Preset bits */
  1448. hw->phy.ops.write_reg(hw, 0x1F79, 0x0210);
  1449. /* Change integ_factor of channel A to 3 */
  1450. hw->phy.ops.write_reg(hw, 0x1895, 0x0003);
  1451. /* Change prop_factor of channels BCD to 8 */
  1452. hw->phy.ops.write_reg(hw, 0x1796, 0x0008);
  1453. /* Change cg_icount + enable integbp for channels BCD */
  1454. hw->phy.ops.write_reg(hw, 0x1798, 0xD008);
  1455. /*
  1456. * Change cg_icount + enable integbp + change prop_factor_master
  1457. * to 8 for channel A
  1458. */
  1459. hw->phy.ops.write_reg(hw, 0x1898, 0xD918);
  1460. /* Disable AHT in Slave mode on channel A */
  1461. hw->phy.ops.write_reg(hw, 0x187A, 0x0800);
  1462. /*
  1463. * Enable LPLU and disable AN to 1000 in non-D0a states,
  1464. * Enable SPD+B2B
  1465. */
  1466. hw->phy.ops.write_reg(hw, 0x0019, 0x008D);
  1467. /* Enable restart AN on an1000_dis change */
  1468. hw->phy.ops.write_reg(hw, 0x001B, 0x2080);
  1469. /* Enable wh_fifo read clock in 10/100 modes */
  1470. hw->phy.ops.write_reg(hw, 0x0014, 0x0045);
  1471. /* Restart AN, Speed selection is 1000 */
  1472. hw->phy.ops.write_reg(hw, 0x0000, 0x1340);
  1473. return 0;
  1474. }