e1000_phy.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2007 - 2018 Intel Corporation. */
  3. #include <linux/bitfield.h>
  4. #include <linux/delay.h>
  5. #include <linux/if_ether.h>
  6. #include "e1000_mac.h"
  7. #include "e1000_phy.h"
  8. static s32 igb_phy_setup_autoneg(struct e1000_hw *hw);
  9. static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
  10. u16 *phy_ctrl);
  11. static s32 igb_wait_autoneg(struct e1000_hw *hw);
  12. static s32 igb_set_master_slave_mode(struct e1000_hw *hw);
  13. /* Cable length tables */
  14. static const u16 e1000_m88_cable_length_table[] = {
  15. 0, 50, 80, 110, 140, 140, E1000_CABLE_LENGTH_UNDEFINED };
  16. static const u16 e1000_igp_2_cable_length_table[] = {
  17. 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
  18. 0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
  19. 6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
  20. 21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
  21. 40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
  22. 60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
  23. 83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
  24. 104, 109, 114, 118, 121, 124};
  25. /**
  26. * igb_check_reset_block - Check if PHY reset is blocked
  27. * @hw: pointer to the HW structure
  28. *
  29. * Read the PHY management control register and check whether a PHY reset
  30. * is blocked. If a reset is not blocked return 0, otherwise
  31. * return E1000_BLK_PHY_RESET (12).
  32. **/
  33. s32 igb_check_reset_block(struct e1000_hw *hw)
  34. {
  35. u32 manc;
  36. manc = rd32(E1000_MANC);
  37. return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ? E1000_BLK_PHY_RESET : 0;
  38. }
  39. /**
  40. * igb_get_phy_id - Retrieve the PHY ID and revision
  41. * @hw: pointer to the HW structure
  42. *
  43. * Reads the PHY registers and stores the PHY ID and possibly the PHY
  44. * revision in the hardware structure.
  45. **/
  46. s32 igb_get_phy_id(struct e1000_hw *hw)
  47. {
  48. struct e1000_phy_info *phy = &hw->phy;
  49. s32 ret_val = 0;
  50. u16 phy_id;
  51. /* ensure PHY page selection to fix misconfigured i210 */
  52. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
  53. phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0);
  54. ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
  55. if (ret_val)
  56. goto out;
  57. phy->id = (u32)(phy_id << 16);
  58. udelay(20);
  59. ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
  60. if (ret_val)
  61. goto out;
  62. phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
  63. phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
  64. out:
  65. return ret_val;
  66. }
  67. /**
  68. * igb_phy_reset_dsp - Reset PHY DSP
  69. * @hw: pointer to the HW structure
  70. *
  71. * Reset the digital signal processor.
  72. **/
  73. static s32 igb_phy_reset_dsp(struct e1000_hw *hw)
  74. {
  75. s32 ret_val = 0;
  76. if (!(hw->phy.ops.write_reg))
  77. goto out;
  78. ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xC1);
  79. if (ret_val)
  80. goto out;
  81. ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0);
  82. out:
  83. return ret_val;
  84. }
  85. /**
  86. * igb_read_phy_reg_mdic - Read MDI control register
  87. * @hw: pointer to the HW structure
  88. * @offset: register offset to be read
  89. * @data: pointer to the read data
  90. *
  91. * Reads the MDI control register in the PHY at offset and stores the
  92. * information read to data.
  93. **/
  94. s32 igb_read_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 *data)
  95. {
  96. struct e1000_phy_info *phy = &hw->phy;
  97. u32 i, mdic = 0;
  98. s32 ret_val = 0;
  99. if (offset > MAX_PHY_REG_ADDRESS) {
  100. hw_dbg("PHY Address %d is out of range\n", offset);
  101. ret_val = -E1000_ERR_PARAM;
  102. goto out;
  103. }
  104. /* Set up Op-code, Phy Address, and register offset in the MDI
  105. * Control register. The MAC will take care of interfacing with the
  106. * PHY to retrieve the desired data.
  107. */
  108. mdic = ((offset << E1000_MDIC_REG_SHIFT) |
  109. (phy->addr << E1000_MDIC_PHY_SHIFT) |
  110. (E1000_MDIC_OP_READ));
  111. wr32(E1000_MDIC, mdic);
  112. /* Poll the ready bit to see if the MDI read completed
  113. * Increasing the time out as testing showed failures with
  114. * the lower time out
  115. */
  116. for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
  117. udelay(50);
  118. mdic = rd32(E1000_MDIC);
  119. if (mdic & E1000_MDIC_READY)
  120. break;
  121. }
  122. if (!(mdic & E1000_MDIC_READY)) {
  123. hw_dbg("MDI Read did not complete\n");
  124. ret_val = -E1000_ERR_PHY;
  125. goto out;
  126. }
  127. if (mdic & E1000_MDIC_ERROR) {
  128. hw_dbg("MDI Error\n");
  129. ret_val = -E1000_ERR_PHY;
  130. goto out;
  131. }
  132. *data = (u16) mdic;
  133. out:
  134. return ret_val;
  135. }
  136. /**
  137. * igb_write_phy_reg_mdic - Write MDI control register
  138. * @hw: pointer to the HW structure
  139. * @offset: register offset to write to
  140. * @data: data to write to register at offset
  141. *
  142. * Writes data to MDI control register in the PHY at offset.
  143. **/
  144. s32 igb_write_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 data)
  145. {
  146. struct e1000_phy_info *phy = &hw->phy;
  147. u32 i, mdic = 0;
  148. s32 ret_val = 0;
  149. if (offset > MAX_PHY_REG_ADDRESS) {
  150. hw_dbg("PHY Address %d is out of range\n", offset);
  151. ret_val = -E1000_ERR_PARAM;
  152. goto out;
  153. }
  154. /* Set up Op-code, Phy Address, and register offset in the MDI
  155. * Control register. The MAC will take care of interfacing with the
  156. * PHY to retrieve the desired data.
  157. */
  158. mdic = (((u32)data) |
  159. (offset << E1000_MDIC_REG_SHIFT) |
  160. (phy->addr << E1000_MDIC_PHY_SHIFT) |
  161. (E1000_MDIC_OP_WRITE));
  162. wr32(E1000_MDIC, mdic);
  163. /* Poll the ready bit to see if the MDI read completed
  164. * Increasing the time out as testing showed failures with
  165. * the lower time out
  166. */
  167. for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
  168. udelay(50);
  169. mdic = rd32(E1000_MDIC);
  170. if (mdic & E1000_MDIC_READY)
  171. break;
  172. }
  173. if (!(mdic & E1000_MDIC_READY)) {
  174. hw_dbg("MDI Write did not complete\n");
  175. ret_val = -E1000_ERR_PHY;
  176. goto out;
  177. }
  178. if (mdic & E1000_MDIC_ERROR) {
  179. hw_dbg("MDI Error\n");
  180. ret_val = -E1000_ERR_PHY;
  181. goto out;
  182. }
  183. out:
  184. return ret_val;
  185. }
  186. /**
  187. * igb_read_phy_reg_i2c - Read PHY register using i2c
  188. * @hw: pointer to the HW structure
  189. * @offset: register offset to be read
  190. * @data: pointer to the read data
  191. *
  192. * Reads the PHY register at offset using the i2c interface and stores the
  193. * retrieved information in data.
  194. **/
  195. s32 igb_read_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 *data)
  196. {
  197. struct e1000_phy_info *phy = &hw->phy;
  198. u32 i, i2ccmd = 0;
  199. /* Set up Op-code, Phy Address, and register address in the I2CCMD
  200. * register. The MAC will take care of interfacing with the
  201. * PHY to retrieve the desired data.
  202. */
  203. i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
  204. (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
  205. (E1000_I2CCMD_OPCODE_READ));
  206. wr32(E1000_I2CCMD, i2ccmd);
  207. /* Poll the ready bit to see if the I2C read completed */
  208. for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
  209. udelay(50);
  210. i2ccmd = rd32(E1000_I2CCMD);
  211. if (i2ccmd & E1000_I2CCMD_READY)
  212. break;
  213. }
  214. if (!(i2ccmd & E1000_I2CCMD_READY)) {
  215. hw_dbg("I2CCMD Read did not complete\n");
  216. return -E1000_ERR_PHY;
  217. }
  218. if (i2ccmd & E1000_I2CCMD_ERROR) {
  219. hw_dbg("I2CCMD Error bit set\n");
  220. return -E1000_ERR_PHY;
  221. }
  222. /* Need to byte-swap the 16-bit value. */
  223. *data = ((i2ccmd >> 8) & 0x00FF) | FIELD_PREP(0xFF00, i2ccmd);
  224. return 0;
  225. }
  226. /**
  227. * igb_write_phy_reg_i2c - Write PHY register using i2c
  228. * @hw: pointer to the HW structure
  229. * @offset: register offset to write to
  230. * @data: data to write at register offset
  231. *
  232. * Writes the data to PHY register at the offset using the i2c interface.
  233. **/
  234. s32 igb_write_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 data)
  235. {
  236. struct e1000_phy_info *phy = &hw->phy;
  237. u32 i, i2ccmd = 0;
  238. u16 phy_data_swapped;
  239. /* Prevent overwriting SFP I2C EEPROM which is at A0 address.*/
  240. if ((hw->phy.addr == 0) || (hw->phy.addr > 7)) {
  241. hw_dbg("PHY I2C Address %d is out of range.\n",
  242. hw->phy.addr);
  243. return -E1000_ERR_CONFIG;
  244. }
  245. /* Swap the data bytes for the I2C interface */
  246. phy_data_swapped = ((data >> 8) & 0x00FF) | FIELD_PREP(0xFF00, data);
  247. /* Set up Op-code, Phy Address, and register address in the I2CCMD
  248. * register. The MAC will take care of interfacing with the
  249. * PHY to retrieve the desired data.
  250. */
  251. i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
  252. (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
  253. E1000_I2CCMD_OPCODE_WRITE |
  254. phy_data_swapped);
  255. wr32(E1000_I2CCMD, i2ccmd);
  256. /* Poll the ready bit to see if the I2C read completed */
  257. for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
  258. udelay(50);
  259. i2ccmd = rd32(E1000_I2CCMD);
  260. if (i2ccmd & E1000_I2CCMD_READY)
  261. break;
  262. }
  263. if (!(i2ccmd & E1000_I2CCMD_READY)) {
  264. hw_dbg("I2CCMD Write did not complete\n");
  265. return -E1000_ERR_PHY;
  266. }
  267. if (i2ccmd & E1000_I2CCMD_ERROR) {
  268. hw_dbg("I2CCMD Error bit set\n");
  269. return -E1000_ERR_PHY;
  270. }
  271. return 0;
  272. }
  273. /**
  274. * igb_read_sfp_data_byte - Reads SFP module data.
  275. * @hw: pointer to the HW structure
  276. * @offset: byte location offset to be read
  277. * @data: read data buffer pointer
  278. *
  279. * Reads one byte from SFP module data stored
  280. * in SFP resided EEPROM memory or SFP diagnostic area.
  281. * Function should be called with
  282. * E1000_I2CCMD_SFP_DATA_ADDR(<byte offset>) for SFP module database access
  283. * E1000_I2CCMD_SFP_DIAG_ADDR(<byte offset>) for SFP diagnostics parameters
  284. * access
  285. **/
  286. s32 igb_read_sfp_data_byte(struct e1000_hw *hw, u16 offset, u8 *data)
  287. {
  288. u32 i = 0;
  289. u32 i2ccmd = 0;
  290. u32 data_local = 0;
  291. if (offset > E1000_I2CCMD_SFP_DIAG_ADDR(255)) {
  292. hw_dbg("I2CCMD command address exceeds upper limit\n");
  293. return -E1000_ERR_PHY;
  294. }
  295. /* Set up Op-code, EEPROM Address,in the I2CCMD
  296. * register. The MAC will take care of interfacing with the
  297. * EEPROM to retrieve the desired data.
  298. */
  299. i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
  300. E1000_I2CCMD_OPCODE_READ);
  301. wr32(E1000_I2CCMD, i2ccmd);
  302. /* Poll the ready bit to see if the I2C read completed */
  303. for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
  304. udelay(50);
  305. data_local = rd32(E1000_I2CCMD);
  306. if (data_local & E1000_I2CCMD_READY)
  307. break;
  308. }
  309. if (!(data_local & E1000_I2CCMD_READY)) {
  310. hw_dbg("I2CCMD Read did not complete\n");
  311. return -E1000_ERR_PHY;
  312. }
  313. if (data_local & E1000_I2CCMD_ERROR) {
  314. hw_dbg("I2CCMD Error bit set\n");
  315. return -E1000_ERR_PHY;
  316. }
  317. *data = (u8) data_local & 0xFF;
  318. return 0;
  319. }
  320. /**
  321. * igb_read_phy_reg_igp - Read igp PHY register
  322. * @hw: pointer to the HW structure
  323. * @offset: register offset to be read
  324. * @data: pointer to the read data
  325. *
  326. * Acquires semaphore, if necessary, then reads the PHY register at offset
  327. * and storing the retrieved information in data. Release any acquired
  328. * semaphores before exiting.
  329. **/
  330. s32 igb_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data)
  331. {
  332. s32 ret_val = 0;
  333. if (!(hw->phy.ops.acquire))
  334. goto out;
  335. ret_val = hw->phy.ops.acquire(hw);
  336. if (ret_val)
  337. goto out;
  338. if (offset > MAX_PHY_MULTI_PAGE_REG) {
  339. ret_val = igb_write_phy_reg_mdic(hw,
  340. IGP01E1000_PHY_PAGE_SELECT,
  341. (u16)offset);
  342. if (ret_val) {
  343. hw->phy.ops.release(hw);
  344. goto out;
  345. }
  346. }
  347. ret_val = igb_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
  348. data);
  349. hw->phy.ops.release(hw);
  350. out:
  351. return ret_val;
  352. }
  353. /**
  354. * igb_write_phy_reg_igp - Write igp PHY register
  355. * @hw: pointer to the HW structure
  356. * @offset: register offset to write to
  357. * @data: data to write at register offset
  358. *
  359. * Acquires semaphore, if necessary, then writes the data to PHY register
  360. * at the offset. Release any acquired semaphores before exiting.
  361. **/
  362. s32 igb_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data)
  363. {
  364. s32 ret_val = 0;
  365. if (!(hw->phy.ops.acquire))
  366. goto out;
  367. ret_val = hw->phy.ops.acquire(hw);
  368. if (ret_val)
  369. goto out;
  370. if (offset > MAX_PHY_MULTI_PAGE_REG) {
  371. ret_val = igb_write_phy_reg_mdic(hw,
  372. IGP01E1000_PHY_PAGE_SELECT,
  373. (u16)offset);
  374. if (ret_val) {
  375. hw->phy.ops.release(hw);
  376. goto out;
  377. }
  378. }
  379. ret_val = igb_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
  380. data);
  381. hw->phy.ops.release(hw);
  382. out:
  383. return ret_val;
  384. }
  385. /**
  386. * igb_copper_link_setup_82580 - Setup 82580 PHY for copper link
  387. * @hw: pointer to the HW structure
  388. *
  389. * Sets up Carrier-sense on Transmit and downshift values.
  390. **/
  391. s32 igb_copper_link_setup_82580(struct e1000_hw *hw)
  392. {
  393. struct e1000_phy_info *phy = &hw->phy;
  394. s32 ret_val;
  395. u16 phy_data;
  396. if (phy->reset_disable) {
  397. ret_val = 0;
  398. goto out;
  399. }
  400. if (phy->type == e1000_phy_82580) {
  401. ret_val = hw->phy.ops.reset(hw);
  402. if (ret_val) {
  403. hw_dbg("Error resetting the PHY.\n");
  404. goto out;
  405. }
  406. }
  407. /* Enable CRS on TX. This must be set for half-duplex operation. */
  408. ret_val = phy->ops.read_reg(hw, I82580_CFG_REG, &phy_data);
  409. if (ret_val)
  410. goto out;
  411. phy_data |= I82580_CFG_ASSERT_CRS_ON_TX;
  412. /* Enable downshift */
  413. phy_data |= I82580_CFG_ENABLE_DOWNSHIFT;
  414. ret_val = phy->ops.write_reg(hw, I82580_CFG_REG, phy_data);
  415. if (ret_val)
  416. goto out;
  417. /* Set MDI/MDIX mode */
  418. ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
  419. if (ret_val)
  420. goto out;
  421. phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
  422. /* Options:
  423. * 0 - Auto (default)
  424. * 1 - MDI mode
  425. * 2 - MDI-X mode
  426. */
  427. switch (hw->phy.mdix) {
  428. case 1:
  429. break;
  430. case 2:
  431. phy_data |= I82580_PHY_CTRL2_MANUAL_MDIX;
  432. break;
  433. case 0:
  434. default:
  435. phy_data |= I82580_PHY_CTRL2_AUTO_MDI_MDIX;
  436. break;
  437. }
  438. ret_val = hw->phy.ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
  439. out:
  440. return ret_val;
  441. }
  442. /**
  443. * igb_copper_link_setup_m88 - Setup m88 PHY's for copper link
  444. * @hw: pointer to the HW structure
  445. *
  446. * Sets up MDI/MDI-X and polarity for m88 PHY's. If necessary, transmit clock
  447. * and downshift values are set also.
  448. **/
  449. s32 igb_copper_link_setup_m88(struct e1000_hw *hw)
  450. {
  451. struct e1000_phy_info *phy = &hw->phy;
  452. s32 ret_val;
  453. u16 phy_data;
  454. if (phy->reset_disable) {
  455. ret_val = 0;
  456. goto out;
  457. }
  458. /* Enable CRS on TX. This must be set for half-duplex operation. */
  459. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  460. if (ret_val)
  461. goto out;
  462. phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
  463. /* Options:
  464. * MDI/MDI-X = 0 (default)
  465. * 0 - Auto for all speeds
  466. * 1 - MDI mode
  467. * 2 - MDI-X mode
  468. * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
  469. */
  470. phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
  471. switch (phy->mdix) {
  472. case 1:
  473. phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
  474. break;
  475. case 2:
  476. phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
  477. break;
  478. case 3:
  479. phy_data |= M88E1000_PSCR_AUTO_X_1000T;
  480. break;
  481. case 0:
  482. default:
  483. phy_data |= M88E1000_PSCR_AUTO_X_MODE;
  484. break;
  485. }
  486. /* Options:
  487. * disable_polarity_correction = 0 (default)
  488. * Automatic Correction for Reversed Cable Polarity
  489. * 0 - Disabled
  490. * 1 - Enabled
  491. */
  492. phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
  493. if (phy->disable_polarity_correction == 1)
  494. phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
  495. ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
  496. if (ret_val)
  497. goto out;
  498. if (phy->revision < E1000_REVISION_4) {
  499. /* Force TX_CLK in the Extended PHY Specific Control Register
  500. * to 25MHz clock.
  501. */
  502. ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
  503. &phy_data);
  504. if (ret_val)
  505. goto out;
  506. phy_data |= M88E1000_EPSCR_TX_CLK_25;
  507. if ((phy->revision == E1000_REVISION_2) &&
  508. (phy->id == M88E1111_I_PHY_ID)) {
  509. /* 82573L PHY - set the downshift counter to 5x. */
  510. phy_data &= ~M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK;
  511. phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
  512. } else {
  513. /* Configure Master and Slave downshift values */
  514. phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
  515. M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
  516. phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
  517. M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
  518. }
  519. ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
  520. phy_data);
  521. if (ret_val)
  522. goto out;
  523. }
  524. /* Commit the changes. */
  525. ret_val = igb_phy_sw_reset(hw);
  526. if (ret_val) {
  527. hw_dbg("Error committing the PHY changes\n");
  528. goto out;
  529. }
  530. out:
  531. return ret_val;
  532. }
  533. /**
  534. * igb_copper_link_setup_m88_gen2 - Setup m88 PHY's for copper link
  535. * @hw: pointer to the HW structure
  536. *
  537. * Sets up MDI/MDI-X and polarity for i347-AT4, m88e1322 and m88e1112 PHY's.
  538. * Also enables and sets the downshift parameters.
  539. **/
  540. s32 igb_copper_link_setup_m88_gen2(struct e1000_hw *hw)
  541. {
  542. struct e1000_phy_info *phy = &hw->phy;
  543. s32 ret_val;
  544. u16 phy_data;
  545. if (phy->reset_disable)
  546. return 0;
  547. /* Enable CRS on Tx. This must be set for half-duplex operation. */
  548. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  549. if (ret_val)
  550. return ret_val;
  551. /* Options:
  552. * MDI/MDI-X = 0 (default)
  553. * 0 - Auto for all speeds
  554. * 1 - MDI mode
  555. * 2 - MDI-X mode
  556. * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
  557. */
  558. phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
  559. switch (phy->mdix) {
  560. case 1:
  561. phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
  562. break;
  563. case 2:
  564. phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
  565. break;
  566. case 3:
  567. /* M88E1112 does not support this mode) */
  568. if (phy->id != M88E1112_E_PHY_ID) {
  569. phy_data |= M88E1000_PSCR_AUTO_X_1000T;
  570. break;
  571. }
  572. fallthrough;
  573. case 0:
  574. default:
  575. phy_data |= M88E1000_PSCR_AUTO_X_MODE;
  576. break;
  577. }
  578. /* Options:
  579. * disable_polarity_correction = 0 (default)
  580. * Automatic Correction for Reversed Cable Polarity
  581. * 0 - Disabled
  582. * 1 - Enabled
  583. */
  584. phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
  585. if (phy->disable_polarity_correction == 1)
  586. phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
  587. /* Enable downshift and setting it to X6 */
  588. if (phy->id == M88E1543_E_PHY_ID) {
  589. phy_data &= ~I347AT4_PSCR_DOWNSHIFT_ENABLE;
  590. ret_val =
  591. phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
  592. if (ret_val)
  593. return ret_val;
  594. ret_val = igb_phy_sw_reset(hw);
  595. if (ret_val) {
  596. hw_dbg("Error committing the PHY changes\n");
  597. return ret_val;
  598. }
  599. }
  600. phy_data &= ~I347AT4_PSCR_DOWNSHIFT_MASK;
  601. phy_data |= I347AT4_PSCR_DOWNSHIFT_6X;
  602. phy_data |= I347AT4_PSCR_DOWNSHIFT_ENABLE;
  603. ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
  604. if (ret_val)
  605. return ret_val;
  606. /* Commit the changes. */
  607. ret_val = igb_phy_sw_reset(hw);
  608. if (ret_val) {
  609. hw_dbg("Error committing the PHY changes\n");
  610. return ret_val;
  611. }
  612. ret_val = igb_set_master_slave_mode(hw);
  613. if (ret_val)
  614. return ret_val;
  615. return 0;
  616. }
  617. /**
  618. * igb_copper_link_setup_igp - Setup igp PHY's for copper link
  619. * @hw: pointer to the HW structure
  620. *
  621. * Sets up LPLU, MDI/MDI-X, polarity, Smartspeed and Master/Slave config for
  622. * igp PHY's.
  623. **/
  624. s32 igb_copper_link_setup_igp(struct e1000_hw *hw)
  625. {
  626. struct e1000_phy_info *phy = &hw->phy;
  627. s32 ret_val;
  628. u16 data;
  629. if (phy->reset_disable) {
  630. ret_val = 0;
  631. goto out;
  632. }
  633. ret_val = phy->ops.reset(hw);
  634. if (ret_val) {
  635. hw_dbg("Error resetting the PHY.\n");
  636. goto out;
  637. }
  638. /* Wait 100ms for MAC to configure PHY from NVM settings, to avoid
  639. * timeout issues when LFS is enabled.
  640. */
  641. msleep(100);
  642. /* The NVM settings will configure LPLU in D3 for
  643. * non-IGP1 PHYs.
  644. */
  645. if (phy->type == e1000_phy_igp) {
  646. /* disable lplu d3 during driver init */
  647. if (phy->ops.set_d3_lplu_state)
  648. ret_val = phy->ops.set_d3_lplu_state(hw, false);
  649. if (ret_val) {
  650. hw_dbg("Error Disabling LPLU D3\n");
  651. goto out;
  652. }
  653. }
  654. /* disable lplu d0 during driver init */
  655. ret_val = phy->ops.set_d0_lplu_state(hw, false);
  656. if (ret_val) {
  657. hw_dbg("Error Disabling LPLU D0\n");
  658. goto out;
  659. }
  660. /* Configure mdi-mdix settings */
  661. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &data);
  662. if (ret_val)
  663. goto out;
  664. data &= ~IGP01E1000_PSCR_AUTO_MDIX;
  665. switch (phy->mdix) {
  666. case 1:
  667. data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
  668. break;
  669. case 2:
  670. data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
  671. break;
  672. case 0:
  673. default:
  674. data |= IGP01E1000_PSCR_AUTO_MDIX;
  675. break;
  676. }
  677. ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, data);
  678. if (ret_val)
  679. goto out;
  680. /* set auto-master slave resolution settings */
  681. if (hw->mac.autoneg) {
  682. /* when autonegotiation advertisement is only 1000Mbps then we
  683. * should disable SmartSpeed and enable Auto MasterSlave
  684. * resolution as hardware default.
  685. */
  686. if (phy->autoneg_advertised == ADVERTISE_1000_FULL) {
  687. /* Disable SmartSpeed */
  688. ret_val = phy->ops.read_reg(hw,
  689. IGP01E1000_PHY_PORT_CONFIG,
  690. &data);
  691. if (ret_val)
  692. goto out;
  693. data &= ~IGP01E1000_PSCFR_SMART_SPEED;
  694. ret_val = phy->ops.write_reg(hw,
  695. IGP01E1000_PHY_PORT_CONFIG,
  696. data);
  697. if (ret_val)
  698. goto out;
  699. /* Set auto Master/Slave resolution process */
  700. ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
  701. if (ret_val)
  702. goto out;
  703. data &= ~CR_1000T_MS_ENABLE;
  704. ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
  705. if (ret_val)
  706. goto out;
  707. }
  708. ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
  709. if (ret_val)
  710. goto out;
  711. /* load defaults for future use */
  712. phy->original_ms_type = (data & CR_1000T_MS_ENABLE) ?
  713. ((data & CR_1000T_MS_VALUE) ?
  714. e1000_ms_force_master :
  715. e1000_ms_force_slave) :
  716. e1000_ms_auto;
  717. switch (phy->ms_type) {
  718. case e1000_ms_force_master:
  719. data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
  720. break;
  721. case e1000_ms_force_slave:
  722. data |= CR_1000T_MS_ENABLE;
  723. data &= ~(CR_1000T_MS_VALUE);
  724. break;
  725. case e1000_ms_auto:
  726. data &= ~CR_1000T_MS_ENABLE;
  727. break;
  728. default:
  729. break;
  730. }
  731. ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
  732. if (ret_val)
  733. goto out;
  734. }
  735. out:
  736. return ret_val;
  737. }
  738. /**
  739. * igb_copper_link_autoneg - Setup/Enable autoneg for copper link
  740. * @hw: pointer to the HW structure
  741. *
  742. * Performs initial bounds checking on autoneg advertisement parameter, then
  743. * configure to advertise the full capability. Setup the PHY to autoneg
  744. * and restart the negotiation process between the link partner. If
  745. * autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
  746. **/
  747. static s32 igb_copper_link_autoneg(struct e1000_hw *hw)
  748. {
  749. struct e1000_phy_info *phy = &hw->phy;
  750. s32 ret_val;
  751. u16 phy_ctrl;
  752. /* Perform some bounds checking on the autoneg advertisement
  753. * parameter.
  754. */
  755. phy->autoneg_advertised &= phy->autoneg_mask;
  756. /* If autoneg_advertised is zero, we assume it was not defaulted
  757. * by the calling code so we set to advertise full capability.
  758. */
  759. if (phy->autoneg_advertised == 0)
  760. phy->autoneg_advertised = phy->autoneg_mask;
  761. hw_dbg("Reconfiguring auto-neg advertisement params\n");
  762. ret_val = igb_phy_setup_autoneg(hw);
  763. if (ret_val) {
  764. hw_dbg("Error Setting up Auto-Negotiation\n");
  765. goto out;
  766. }
  767. hw_dbg("Restarting Auto-Neg\n");
  768. /* Restart auto-negotiation by setting the Auto Neg Enable bit and
  769. * the Auto Neg Restart bit in the PHY control register.
  770. */
  771. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
  772. if (ret_val)
  773. goto out;
  774. phy_ctrl |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
  775. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
  776. if (ret_val)
  777. goto out;
  778. /* Does the user want to wait for Auto-Neg to complete here, or
  779. * check at a later time (for example, callback routine).
  780. */
  781. if (phy->autoneg_wait_to_complete) {
  782. ret_val = igb_wait_autoneg(hw);
  783. if (ret_val) {
  784. hw_dbg("Error while waiting for autoneg to complete\n");
  785. goto out;
  786. }
  787. }
  788. hw->mac.get_link_status = true;
  789. out:
  790. return ret_val;
  791. }
  792. /**
  793. * igb_phy_setup_autoneg - Configure PHY for auto-negotiation
  794. * @hw: pointer to the HW structure
  795. *
  796. * Reads the MII auto-neg advertisement register and/or the 1000T control
  797. * register and if the PHY is already setup for auto-negotiation, then
  798. * return successful. Otherwise, setup advertisement and flow control to
  799. * the appropriate values for the wanted auto-negotiation.
  800. **/
  801. static s32 igb_phy_setup_autoneg(struct e1000_hw *hw)
  802. {
  803. struct e1000_phy_info *phy = &hw->phy;
  804. s32 ret_val;
  805. u16 mii_autoneg_adv_reg;
  806. u16 mii_1000t_ctrl_reg = 0;
  807. phy->autoneg_advertised &= phy->autoneg_mask;
  808. /* Read the MII Auto-Neg Advertisement Register (Address 4). */
  809. ret_val = phy->ops.read_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
  810. if (ret_val)
  811. goto out;
  812. if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
  813. /* Read the MII 1000Base-T Control Register (Address 9). */
  814. ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL,
  815. &mii_1000t_ctrl_reg);
  816. if (ret_val)
  817. goto out;
  818. }
  819. /* Need to parse both autoneg_advertised and fc and set up
  820. * the appropriate PHY registers. First we will parse for
  821. * autoneg_advertised software override. Since we can advertise
  822. * a plethora of combinations, we need to check each bit
  823. * individually.
  824. */
  825. /* First we clear all the 10/100 mb speed bits in the Auto-Neg
  826. * Advertisement Register (Address 4) and the 1000 mb speed bits in
  827. * the 1000Base-T Control Register (Address 9).
  828. */
  829. mii_autoneg_adv_reg &= ~(NWAY_AR_100TX_FD_CAPS |
  830. NWAY_AR_100TX_HD_CAPS |
  831. NWAY_AR_10T_FD_CAPS |
  832. NWAY_AR_10T_HD_CAPS);
  833. mii_1000t_ctrl_reg &= ~(CR_1000T_HD_CAPS | CR_1000T_FD_CAPS);
  834. hw_dbg("autoneg_advertised %x\n", phy->autoneg_advertised);
  835. /* Do we want to advertise 10 Mb Half Duplex? */
  836. if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
  837. hw_dbg("Advertise 10mb Half duplex\n");
  838. mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
  839. }
  840. /* Do we want to advertise 10 Mb Full Duplex? */
  841. if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
  842. hw_dbg("Advertise 10mb Full duplex\n");
  843. mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
  844. }
  845. /* Do we want to advertise 100 Mb Half Duplex? */
  846. if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
  847. hw_dbg("Advertise 100mb Half duplex\n");
  848. mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
  849. }
  850. /* Do we want to advertise 100 Mb Full Duplex? */
  851. if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
  852. hw_dbg("Advertise 100mb Full duplex\n");
  853. mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
  854. }
  855. /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
  856. if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
  857. hw_dbg("Advertise 1000mb Half duplex request denied!\n");
  858. /* Do we want to advertise 1000 Mb Full Duplex? */
  859. if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
  860. hw_dbg("Advertise 1000mb Full duplex\n");
  861. mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
  862. }
  863. /* Check for a software override of the flow control settings, and
  864. * setup the PHY advertisement registers accordingly. If
  865. * auto-negotiation is enabled, then software will have to set the
  866. * "PAUSE" bits to the correct value in the Auto-Negotiation
  867. * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-
  868. * negotiation.
  869. *
  870. * The possible values of the "fc" parameter are:
  871. * 0: Flow control is completely disabled
  872. * 1: Rx flow control is enabled (we can receive pause frames
  873. * but not send pause frames).
  874. * 2: Tx flow control is enabled (we can send pause frames
  875. * but we do not support receiving pause frames).
  876. * 3: Both Rx and TX flow control (symmetric) are enabled.
  877. * other: No software override. The flow control configuration
  878. * in the EEPROM is used.
  879. */
  880. switch (hw->fc.current_mode) {
  881. case e1000_fc_none:
  882. /* Flow control (RX & TX) is completely disabled by a
  883. * software over-ride.
  884. */
  885. mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  886. break;
  887. case e1000_fc_rx_pause:
  888. /* RX Flow control is enabled, and TX Flow control is
  889. * disabled, by a software over-ride.
  890. *
  891. * Since there really isn't a way to advertise that we are
  892. * capable of RX Pause ONLY, we will advertise that we
  893. * support both symmetric and asymmetric RX PAUSE. Later
  894. * (in e1000_config_fc_after_link_up) we will disable the
  895. * hw's ability to send PAUSE frames.
  896. */
  897. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  898. break;
  899. case e1000_fc_tx_pause:
  900. /* TX Flow control is enabled, and RX Flow control is
  901. * disabled, by a software over-ride.
  902. */
  903. mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
  904. mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
  905. break;
  906. case e1000_fc_full:
  907. /* Flow control (both RX and TX) is enabled by a software
  908. * over-ride.
  909. */
  910. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  911. break;
  912. default:
  913. hw_dbg("Flow control param set incorrectly\n");
  914. ret_val = -E1000_ERR_CONFIG;
  915. goto out;
  916. }
  917. ret_val = phy->ops.write_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
  918. if (ret_val)
  919. goto out;
  920. hw_dbg("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
  921. if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
  922. ret_val = phy->ops.write_reg(hw,
  923. PHY_1000T_CTRL,
  924. mii_1000t_ctrl_reg);
  925. if (ret_val)
  926. goto out;
  927. }
  928. out:
  929. return ret_val;
  930. }
  931. /**
  932. * igb_setup_copper_link - Configure copper link settings
  933. * @hw: pointer to the HW structure
  934. *
  935. * Calls the appropriate function to configure the link for auto-neg or forced
  936. * speed and duplex. Then we check for link, once link is established calls
  937. * to configure collision distance and flow control are called. If link is
  938. * not established, we return -E1000_ERR_PHY (-2).
  939. **/
  940. s32 igb_setup_copper_link(struct e1000_hw *hw)
  941. {
  942. s32 ret_val;
  943. bool link;
  944. if (hw->mac.autoneg) {
  945. /* Setup autoneg and flow control advertisement and perform
  946. * autonegotiation.
  947. */
  948. ret_val = igb_copper_link_autoneg(hw);
  949. if (ret_val)
  950. goto out;
  951. } else {
  952. /* PHY will be set to 10H, 10F, 100H or 100F
  953. * depending on user settings.
  954. */
  955. hw_dbg("Forcing Speed and Duplex\n");
  956. ret_val = hw->phy.ops.force_speed_duplex(hw);
  957. if (ret_val) {
  958. hw_dbg("Error Forcing Speed and Duplex\n");
  959. goto out;
  960. }
  961. }
  962. /* Check link status. Wait up to 100 microseconds for link to become
  963. * valid.
  964. */
  965. ret_val = igb_phy_has_link(hw, COPPER_LINK_UP_LIMIT, 10, &link);
  966. if (ret_val)
  967. goto out;
  968. if (link) {
  969. hw_dbg("Valid link established!!!\n");
  970. igb_config_collision_dist(hw);
  971. ret_val = igb_config_fc_after_link_up(hw);
  972. } else {
  973. hw_dbg("Unable to establish link!!!\n");
  974. }
  975. out:
  976. return ret_val;
  977. }
  978. /**
  979. * igb_phy_force_speed_duplex_igp - Force speed/duplex for igp PHY
  980. * @hw: pointer to the HW structure
  981. *
  982. * Calls the PHY setup function to force speed and duplex. Clears the
  983. * auto-crossover to force MDI manually. Waits for link and returns
  984. * successful if link up is successful, else -E1000_ERR_PHY (-2).
  985. **/
  986. s32 igb_phy_force_speed_duplex_igp(struct e1000_hw *hw)
  987. {
  988. struct e1000_phy_info *phy = &hw->phy;
  989. s32 ret_val;
  990. u16 phy_data;
  991. bool link;
  992. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
  993. if (ret_val)
  994. goto out;
  995. igb_phy_force_speed_duplex_setup(hw, &phy_data);
  996. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
  997. if (ret_val)
  998. goto out;
  999. /* Clear Auto-Crossover to force MDI manually. IGP requires MDI
  1000. * forced whenever speed and duplex are forced.
  1001. */
  1002. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
  1003. if (ret_val)
  1004. goto out;
  1005. phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
  1006. phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
  1007. ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
  1008. if (ret_val)
  1009. goto out;
  1010. hw_dbg("IGP PSCR: %X\n", phy_data);
  1011. udelay(1);
  1012. if (phy->autoneg_wait_to_complete) {
  1013. hw_dbg("Waiting for forced speed/duplex link on IGP phy.\n");
  1014. ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
  1015. if (ret_val)
  1016. goto out;
  1017. if (!link)
  1018. hw_dbg("Link taking longer than expected.\n");
  1019. /* Try once more */
  1020. ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
  1021. if (ret_val)
  1022. goto out;
  1023. }
  1024. out:
  1025. return ret_val;
  1026. }
  1027. /**
  1028. * igb_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY
  1029. * @hw: pointer to the HW structure
  1030. *
  1031. * Calls the PHY setup function to force speed and duplex. Clears the
  1032. * auto-crossover to force MDI manually. Resets the PHY to commit the
  1033. * changes. If time expires while waiting for link up, we reset the DSP.
  1034. * After reset, TX_CLK and CRS on TX must be set. Return successful upon
  1035. * successful completion, else return corresponding error code.
  1036. **/
  1037. s32 igb_phy_force_speed_duplex_m88(struct e1000_hw *hw)
  1038. {
  1039. struct e1000_phy_info *phy = &hw->phy;
  1040. s32 ret_val;
  1041. u16 phy_data;
  1042. bool link;
  1043. /* I210 and I211 devices support Auto-Crossover in forced operation. */
  1044. if (phy->type != e1000_phy_i210) {
  1045. /* Clear Auto-Crossover to force MDI manually. M88E1000
  1046. * requires MDI forced whenever speed and duplex are forced.
  1047. */
  1048. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL,
  1049. &phy_data);
  1050. if (ret_val)
  1051. goto out;
  1052. phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
  1053. ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL,
  1054. phy_data);
  1055. if (ret_val)
  1056. goto out;
  1057. hw_dbg("M88E1000 PSCR: %X\n", phy_data);
  1058. }
  1059. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
  1060. if (ret_val)
  1061. goto out;
  1062. igb_phy_force_speed_duplex_setup(hw, &phy_data);
  1063. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
  1064. if (ret_val)
  1065. goto out;
  1066. /* Reset the phy to commit changes. */
  1067. ret_val = igb_phy_sw_reset(hw);
  1068. if (ret_val)
  1069. goto out;
  1070. if (phy->autoneg_wait_to_complete) {
  1071. hw_dbg("Waiting for forced speed/duplex link on M88 phy.\n");
  1072. ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
  1073. if (ret_val)
  1074. goto out;
  1075. if (!link) {
  1076. bool reset_dsp = true;
  1077. switch (hw->phy.id) {
  1078. case I347AT4_E_PHY_ID:
  1079. case M88E1112_E_PHY_ID:
  1080. case M88E1543_E_PHY_ID:
  1081. case M88E1512_E_PHY_ID:
  1082. case I210_I_PHY_ID:
  1083. reset_dsp = false;
  1084. break;
  1085. default:
  1086. if (hw->phy.type != e1000_phy_m88)
  1087. reset_dsp = false;
  1088. break;
  1089. }
  1090. if (!reset_dsp) {
  1091. hw_dbg("Link taking longer than expected.\n");
  1092. } else {
  1093. /* We didn't get link.
  1094. * Reset the DSP and cross our fingers.
  1095. */
  1096. ret_val = phy->ops.write_reg(hw,
  1097. M88E1000_PHY_PAGE_SELECT,
  1098. 0x001d);
  1099. if (ret_val)
  1100. goto out;
  1101. ret_val = igb_phy_reset_dsp(hw);
  1102. if (ret_val)
  1103. goto out;
  1104. }
  1105. }
  1106. /* Try once more */
  1107. ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT,
  1108. 100000, &link);
  1109. if (ret_val)
  1110. goto out;
  1111. }
  1112. if (hw->phy.type != e1000_phy_m88 ||
  1113. hw->phy.id == I347AT4_E_PHY_ID ||
  1114. hw->phy.id == M88E1112_E_PHY_ID ||
  1115. hw->phy.id == M88E1543_E_PHY_ID ||
  1116. hw->phy.id == M88E1512_E_PHY_ID ||
  1117. hw->phy.id == I210_I_PHY_ID)
  1118. goto out;
  1119. ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
  1120. if (ret_val)
  1121. goto out;
  1122. /* Resetting the phy means we need to re-force TX_CLK in the
  1123. * Extended PHY Specific Control Register to 25MHz clock from
  1124. * the reset value of 2.5MHz.
  1125. */
  1126. phy_data |= M88E1000_EPSCR_TX_CLK_25;
  1127. ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
  1128. if (ret_val)
  1129. goto out;
  1130. /* In addition, we must re-enable CRS on Tx for both half and full
  1131. * duplex.
  1132. */
  1133. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  1134. if (ret_val)
  1135. goto out;
  1136. phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
  1137. ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
  1138. out:
  1139. return ret_val;
  1140. }
  1141. /**
  1142. * igb_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex
  1143. * @hw: pointer to the HW structure
  1144. * @phy_ctrl: pointer to current value of PHY_CONTROL
  1145. *
  1146. * Forces speed and duplex on the PHY by doing the following: disable flow
  1147. * control, force speed/duplex on the MAC, disable auto speed detection,
  1148. * disable auto-negotiation, configure duplex, configure speed, configure
  1149. * the collision distance, write configuration to CTRL register. The
  1150. * caller must write to the PHY_CONTROL register for these settings to
  1151. * take affect.
  1152. **/
  1153. static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
  1154. u16 *phy_ctrl)
  1155. {
  1156. struct e1000_mac_info *mac = &hw->mac;
  1157. u32 ctrl;
  1158. /* Turn off flow control when forcing speed/duplex */
  1159. hw->fc.current_mode = e1000_fc_none;
  1160. /* Force speed/duplex on the mac */
  1161. ctrl = rd32(E1000_CTRL);
  1162. ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
  1163. ctrl &= ~E1000_CTRL_SPD_SEL;
  1164. /* Disable Auto Speed Detection */
  1165. ctrl &= ~E1000_CTRL_ASDE;
  1166. /* Disable autoneg on the phy */
  1167. *phy_ctrl &= ~MII_CR_AUTO_NEG_EN;
  1168. /* Forcing Full or Half Duplex? */
  1169. if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {
  1170. ctrl &= ~E1000_CTRL_FD;
  1171. *phy_ctrl &= ~MII_CR_FULL_DUPLEX;
  1172. hw_dbg("Half Duplex\n");
  1173. } else {
  1174. ctrl |= E1000_CTRL_FD;
  1175. *phy_ctrl |= MII_CR_FULL_DUPLEX;
  1176. hw_dbg("Full Duplex\n");
  1177. }
  1178. /* Forcing 10mb or 100mb? */
  1179. if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {
  1180. ctrl |= E1000_CTRL_SPD_100;
  1181. *phy_ctrl |= MII_CR_SPEED_100;
  1182. *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
  1183. hw_dbg("Forcing 100mb\n");
  1184. } else {
  1185. ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
  1186. *phy_ctrl |= MII_CR_SPEED_10;
  1187. *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
  1188. hw_dbg("Forcing 10mb\n");
  1189. }
  1190. igb_config_collision_dist(hw);
  1191. wr32(E1000_CTRL, ctrl);
  1192. }
  1193. /**
  1194. * igb_set_d3_lplu_state - Sets low power link up state for D3
  1195. * @hw: pointer to the HW structure
  1196. * @active: boolean used to enable/disable lplu
  1197. *
  1198. * Success returns 0, Failure returns 1
  1199. *
  1200. * The low power link up (lplu) state is set to the power management level D3
  1201. * and SmartSpeed is disabled when active is true, else clear lplu for D3
  1202. * and enable Smartspeed. LPLU and Smartspeed are mutually exclusive. LPLU
  1203. * is used during Dx states where the power conservation is most important.
  1204. * During driver activity, SmartSpeed should be enabled so performance is
  1205. * maintained.
  1206. **/
  1207. s32 igb_set_d3_lplu_state(struct e1000_hw *hw, bool active)
  1208. {
  1209. struct e1000_phy_info *phy = &hw->phy;
  1210. s32 ret_val = 0;
  1211. u16 data;
  1212. if (!(hw->phy.ops.read_reg))
  1213. goto out;
  1214. ret_val = phy->ops.read_reg(hw, IGP02E1000_PHY_POWER_MGMT, &data);
  1215. if (ret_val)
  1216. goto out;
  1217. if (!active) {
  1218. data &= ~IGP02E1000_PM_D3_LPLU;
  1219. ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
  1220. data);
  1221. if (ret_val)
  1222. goto out;
  1223. /* LPLU and SmartSpeed are mutually exclusive. LPLU is used
  1224. * during Dx states where the power conservation is most
  1225. * important. During driver activity we should enable
  1226. * SmartSpeed, so performance is maintained.
  1227. */
  1228. if (phy->smart_speed == e1000_smart_speed_on) {
  1229. ret_val = phy->ops.read_reg(hw,
  1230. IGP01E1000_PHY_PORT_CONFIG,
  1231. &data);
  1232. if (ret_val)
  1233. goto out;
  1234. data |= IGP01E1000_PSCFR_SMART_SPEED;
  1235. ret_val = phy->ops.write_reg(hw,
  1236. IGP01E1000_PHY_PORT_CONFIG,
  1237. data);
  1238. if (ret_val)
  1239. goto out;
  1240. } else if (phy->smart_speed == e1000_smart_speed_off) {
  1241. ret_val = phy->ops.read_reg(hw,
  1242. IGP01E1000_PHY_PORT_CONFIG,
  1243. &data);
  1244. if (ret_val)
  1245. goto out;
  1246. data &= ~IGP01E1000_PSCFR_SMART_SPEED;
  1247. ret_val = phy->ops.write_reg(hw,
  1248. IGP01E1000_PHY_PORT_CONFIG,
  1249. data);
  1250. if (ret_val)
  1251. goto out;
  1252. }
  1253. } else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||
  1254. (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||
  1255. (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {
  1256. data |= IGP02E1000_PM_D3_LPLU;
  1257. ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
  1258. data);
  1259. if (ret_val)
  1260. goto out;
  1261. /* When LPLU is enabled, we should disable SmartSpeed */
  1262. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
  1263. &data);
  1264. if (ret_val)
  1265. goto out;
  1266. data &= ~IGP01E1000_PSCFR_SMART_SPEED;
  1267. ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
  1268. data);
  1269. }
  1270. out:
  1271. return ret_val;
  1272. }
  1273. /**
  1274. * igb_check_downshift - Checks whether a downshift in speed occurred
  1275. * @hw: pointer to the HW structure
  1276. *
  1277. * Success returns 0, Failure returns 1
  1278. *
  1279. * A downshift is detected by querying the PHY link health.
  1280. **/
  1281. s32 igb_check_downshift(struct e1000_hw *hw)
  1282. {
  1283. struct e1000_phy_info *phy = &hw->phy;
  1284. s32 ret_val;
  1285. u16 phy_data, offset, mask;
  1286. switch (phy->type) {
  1287. case e1000_phy_i210:
  1288. case e1000_phy_m88:
  1289. case e1000_phy_gg82563:
  1290. offset = M88E1000_PHY_SPEC_STATUS;
  1291. mask = M88E1000_PSSR_DOWNSHIFT;
  1292. break;
  1293. case e1000_phy_igp_2:
  1294. case e1000_phy_igp:
  1295. case e1000_phy_igp_3:
  1296. offset = IGP01E1000_PHY_LINK_HEALTH;
  1297. mask = IGP01E1000_PLHR_SS_DOWNGRADE;
  1298. break;
  1299. default:
  1300. /* speed downshift not supported */
  1301. phy->speed_downgraded = false;
  1302. ret_val = 0;
  1303. goto out;
  1304. }
  1305. ret_val = phy->ops.read_reg(hw, offset, &phy_data);
  1306. if (!ret_val)
  1307. phy->speed_downgraded = (phy_data & mask) ? true : false;
  1308. out:
  1309. return ret_val;
  1310. }
  1311. /**
  1312. * igb_check_polarity_m88 - Checks the polarity.
  1313. * @hw: pointer to the HW structure
  1314. *
  1315. * Success returns 0, Failure returns -E1000_ERR_PHY (-2)
  1316. *
  1317. * Polarity is determined based on the PHY specific status register.
  1318. **/
  1319. s32 igb_check_polarity_m88(struct e1000_hw *hw)
  1320. {
  1321. struct e1000_phy_info *phy = &hw->phy;
  1322. s32 ret_val;
  1323. u16 data;
  1324. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &data);
  1325. if (!ret_val)
  1326. phy->cable_polarity = (data & M88E1000_PSSR_REV_POLARITY)
  1327. ? e1000_rev_polarity_reversed
  1328. : e1000_rev_polarity_normal;
  1329. return ret_val;
  1330. }
  1331. /**
  1332. * igb_check_polarity_igp - Checks the polarity.
  1333. * @hw: pointer to the HW structure
  1334. *
  1335. * Success returns 0, Failure returns -E1000_ERR_PHY (-2)
  1336. *
  1337. * Polarity is determined based on the PHY port status register, and the
  1338. * current speed (since there is no polarity at 100Mbps).
  1339. **/
  1340. static s32 igb_check_polarity_igp(struct e1000_hw *hw)
  1341. {
  1342. struct e1000_phy_info *phy = &hw->phy;
  1343. s32 ret_val;
  1344. u16 data, offset, mask;
  1345. /* Polarity is determined based on the speed of
  1346. * our connection.
  1347. */
  1348. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
  1349. if (ret_val)
  1350. goto out;
  1351. if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
  1352. IGP01E1000_PSSR_SPEED_1000MBPS) {
  1353. offset = IGP01E1000_PHY_PCS_INIT_REG;
  1354. mask = IGP01E1000_PHY_POLARITY_MASK;
  1355. } else {
  1356. /* This really only applies to 10Mbps since
  1357. * there is no polarity for 100Mbps (always 0).
  1358. */
  1359. offset = IGP01E1000_PHY_PORT_STATUS;
  1360. mask = IGP01E1000_PSSR_POLARITY_REVERSED;
  1361. }
  1362. ret_val = phy->ops.read_reg(hw, offset, &data);
  1363. if (!ret_val)
  1364. phy->cable_polarity = (data & mask)
  1365. ? e1000_rev_polarity_reversed
  1366. : e1000_rev_polarity_normal;
  1367. out:
  1368. return ret_val;
  1369. }
  1370. /**
  1371. * igb_wait_autoneg - Wait for auto-neg completion
  1372. * @hw: pointer to the HW structure
  1373. *
  1374. * Waits for auto-negotiation to complete or for the auto-negotiation time
  1375. * limit to expire, which ever happens first.
  1376. **/
  1377. static s32 igb_wait_autoneg(struct e1000_hw *hw)
  1378. {
  1379. s32 ret_val = 0;
  1380. u16 i, phy_status;
  1381. /* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
  1382. for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
  1383. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1384. if (ret_val)
  1385. break;
  1386. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1387. if (ret_val)
  1388. break;
  1389. if (phy_status & MII_SR_AUTONEG_COMPLETE)
  1390. break;
  1391. msleep(100);
  1392. }
  1393. /* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
  1394. * has completed.
  1395. */
  1396. return ret_val;
  1397. }
  1398. /**
  1399. * igb_phy_has_link - Polls PHY for link
  1400. * @hw: pointer to the HW structure
  1401. * @iterations: number of times to poll for link
  1402. * @usec_interval: delay between polling attempts
  1403. * @success: pointer to whether polling was successful or not
  1404. *
  1405. * Polls the PHY status register for link, 'iterations' number of times.
  1406. **/
  1407. s32 igb_phy_has_link(struct e1000_hw *hw, u32 iterations,
  1408. u32 usec_interval, bool *success)
  1409. {
  1410. s32 ret_val = 0;
  1411. u16 i, phy_status;
  1412. for (i = 0; i < iterations; i++) {
  1413. /* Some PHYs require the PHY_STATUS register to be read
  1414. * twice due to the link bit being sticky. No harm doing
  1415. * it across the board.
  1416. */
  1417. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1418. if (ret_val && usec_interval > 0) {
  1419. /* If the first read fails, another entity may have
  1420. * ownership of the resources, wait and try again to
  1421. * see if they have relinquished the resources yet.
  1422. */
  1423. if (usec_interval >= 1000)
  1424. mdelay(usec_interval/1000);
  1425. else
  1426. udelay(usec_interval);
  1427. }
  1428. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
  1429. if (ret_val)
  1430. break;
  1431. if (phy_status & MII_SR_LINK_STATUS)
  1432. break;
  1433. if (usec_interval >= 1000)
  1434. mdelay(usec_interval/1000);
  1435. else
  1436. udelay(usec_interval);
  1437. }
  1438. *success = (i < iterations) ? true : false;
  1439. return ret_val;
  1440. }
  1441. /**
  1442. * igb_get_cable_length_m88 - Determine cable length for m88 PHY
  1443. * @hw: pointer to the HW structure
  1444. *
  1445. * Reads the PHY specific status register to retrieve the cable length
  1446. * information. The cable length is determined by averaging the minimum and
  1447. * maximum values to get the "average" cable length. The m88 PHY has four
  1448. * possible cable length values, which are:
  1449. * Register Value Cable Length
  1450. * 0 < 50 meters
  1451. * 1 50 - 80 meters
  1452. * 2 80 - 110 meters
  1453. * 3 110 - 140 meters
  1454. * 4 > 140 meters
  1455. **/
  1456. s32 igb_get_cable_length_m88(struct e1000_hw *hw)
  1457. {
  1458. struct e1000_phy_info *phy = &hw->phy;
  1459. s32 ret_val;
  1460. u16 phy_data, index;
  1461. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
  1462. if (ret_val)
  1463. goto out;
  1464. index = FIELD_GET(M88E1000_PSSR_CABLE_LENGTH, phy_data);
  1465. if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
  1466. ret_val = -E1000_ERR_PHY;
  1467. goto out;
  1468. }
  1469. phy->min_cable_length = e1000_m88_cable_length_table[index];
  1470. phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
  1471. phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
  1472. out:
  1473. return ret_val;
  1474. }
  1475. s32 igb_get_cable_length_m88_gen2(struct e1000_hw *hw)
  1476. {
  1477. struct e1000_phy_info *phy = &hw->phy;
  1478. s32 ret_val;
  1479. u16 phy_data, phy_data2, index, default_page, is_cm;
  1480. int len_tot = 0;
  1481. u16 len_min;
  1482. u16 len_max;
  1483. switch (hw->phy.id) {
  1484. case M88E1543_E_PHY_ID:
  1485. case M88E1512_E_PHY_ID:
  1486. case I347AT4_E_PHY_ID:
  1487. case I210_I_PHY_ID:
  1488. /* Remember the original page select and set it to 7 */
  1489. ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
  1490. &default_page);
  1491. if (ret_val)
  1492. goto out;
  1493. ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x07);
  1494. if (ret_val)
  1495. goto out;
  1496. /* Check if the unit of cable length is meters or cm */
  1497. ret_val = phy->ops.read_reg(hw, I347AT4_PCDC, &phy_data2);
  1498. if (ret_val)
  1499. goto out;
  1500. is_cm = !(phy_data2 & I347AT4_PCDC_CABLE_LENGTH_UNIT);
  1501. /* Get cable length from Pair 0 length Regs */
  1502. ret_val = phy->ops.read_reg(hw, I347AT4_PCDL0, &phy_data);
  1503. if (ret_val)
  1504. goto out;
  1505. phy->pair_length[0] = phy_data / (is_cm ? 100 : 1);
  1506. len_tot = phy->pair_length[0];
  1507. len_min = phy->pair_length[0];
  1508. len_max = phy->pair_length[0];
  1509. /* Get cable length from Pair 1 length Regs */
  1510. ret_val = phy->ops.read_reg(hw, I347AT4_PCDL1, &phy_data);
  1511. if (ret_val)
  1512. goto out;
  1513. phy->pair_length[1] = phy_data / (is_cm ? 100 : 1);
  1514. len_tot += phy->pair_length[1];
  1515. len_min = min(len_min, phy->pair_length[1]);
  1516. len_max = max(len_max, phy->pair_length[1]);
  1517. /* Get cable length from Pair 2 length Regs */
  1518. ret_val = phy->ops.read_reg(hw, I347AT4_PCDL2, &phy_data);
  1519. if (ret_val)
  1520. goto out;
  1521. phy->pair_length[2] = phy_data / (is_cm ? 100 : 1);
  1522. len_tot += phy->pair_length[2];
  1523. len_min = min(len_min, phy->pair_length[2]);
  1524. len_max = max(len_max, phy->pair_length[2]);
  1525. /* Get cable length from Pair 3 length Regs */
  1526. ret_val = phy->ops.read_reg(hw, I347AT4_PCDL3, &phy_data);
  1527. if (ret_val)
  1528. goto out;
  1529. phy->pair_length[3] = phy_data / (is_cm ? 100 : 1);
  1530. len_tot += phy->pair_length[3];
  1531. len_min = min(len_min, phy->pair_length[3]);
  1532. len_max = max(len_max, phy->pair_length[3]);
  1533. /* Populate the phy structure with cable length in meters */
  1534. phy->min_cable_length = len_min;
  1535. phy->max_cable_length = len_max;
  1536. phy->cable_length = len_tot / 4;
  1537. /* Reset the page selec to its original value */
  1538. ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
  1539. default_page);
  1540. if (ret_val)
  1541. goto out;
  1542. break;
  1543. case M88E1112_E_PHY_ID:
  1544. /* Remember the original page select and set it to 5 */
  1545. ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
  1546. &default_page);
  1547. if (ret_val)
  1548. goto out;
  1549. ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x05);
  1550. if (ret_val)
  1551. goto out;
  1552. ret_val = phy->ops.read_reg(hw, M88E1112_VCT_DSP_DISTANCE,
  1553. &phy_data);
  1554. if (ret_val)
  1555. goto out;
  1556. index = FIELD_GET(M88E1000_PSSR_CABLE_LENGTH, phy_data);
  1557. if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
  1558. ret_val = -E1000_ERR_PHY;
  1559. goto out;
  1560. }
  1561. phy->min_cable_length = e1000_m88_cable_length_table[index];
  1562. phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
  1563. phy->cable_length = (phy->min_cable_length +
  1564. phy->max_cable_length) / 2;
  1565. /* Reset the page select to its original value */
  1566. ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
  1567. default_page);
  1568. if (ret_val)
  1569. goto out;
  1570. break;
  1571. default:
  1572. ret_val = -E1000_ERR_PHY;
  1573. goto out;
  1574. }
  1575. out:
  1576. return ret_val;
  1577. }
  1578. /**
  1579. * igb_get_cable_length_igp_2 - Determine cable length for igp2 PHY
  1580. * @hw: pointer to the HW structure
  1581. *
  1582. * The automatic gain control (agc) normalizes the amplitude of the
  1583. * received signal, adjusting for the attenuation produced by the
  1584. * cable. By reading the AGC registers, which represent the
  1585. * combination of coarse and fine gain value, the value can be put
  1586. * into a lookup table to obtain the approximate cable length
  1587. * for each channel.
  1588. **/
  1589. s32 igb_get_cable_length_igp_2(struct e1000_hw *hw)
  1590. {
  1591. struct e1000_phy_info *phy = &hw->phy;
  1592. s32 ret_val = 0;
  1593. u16 phy_data, i, agc_value = 0;
  1594. u16 cur_agc_index, max_agc_index = 0;
  1595. u16 min_agc_index = ARRAY_SIZE(e1000_igp_2_cable_length_table) - 1;
  1596. static const u16 agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] = {
  1597. IGP02E1000_PHY_AGC_A,
  1598. IGP02E1000_PHY_AGC_B,
  1599. IGP02E1000_PHY_AGC_C,
  1600. IGP02E1000_PHY_AGC_D
  1601. };
  1602. /* Read the AGC registers for all channels */
  1603. for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
  1604. ret_val = phy->ops.read_reg(hw, agc_reg_array[i], &phy_data);
  1605. if (ret_val)
  1606. goto out;
  1607. /* Getting bits 15:9, which represent the combination of
  1608. * coarse and fine gain values. The result is a number
  1609. * that can be put into the lookup table to obtain the
  1610. * approximate cable length.
  1611. */
  1612. cur_agc_index = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
  1613. IGP02E1000_AGC_LENGTH_MASK;
  1614. /* Array index bound check. */
  1615. if ((cur_agc_index >= ARRAY_SIZE(e1000_igp_2_cable_length_table)) ||
  1616. (cur_agc_index == 0)) {
  1617. ret_val = -E1000_ERR_PHY;
  1618. goto out;
  1619. }
  1620. /* Remove min & max AGC values from calculation. */
  1621. if (e1000_igp_2_cable_length_table[min_agc_index] >
  1622. e1000_igp_2_cable_length_table[cur_agc_index])
  1623. min_agc_index = cur_agc_index;
  1624. if (e1000_igp_2_cable_length_table[max_agc_index] <
  1625. e1000_igp_2_cable_length_table[cur_agc_index])
  1626. max_agc_index = cur_agc_index;
  1627. agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
  1628. }
  1629. agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
  1630. e1000_igp_2_cable_length_table[max_agc_index]);
  1631. agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
  1632. /* Calculate cable length with the error range of +/- 10 meters. */
  1633. phy->min_cable_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
  1634. (agc_value - IGP02E1000_AGC_RANGE) : 0;
  1635. phy->max_cable_length = agc_value + IGP02E1000_AGC_RANGE;
  1636. phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
  1637. out:
  1638. return ret_val;
  1639. }
  1640. /**
  1641. * igb_get_phy_info_m88 - Retrieve PHY information
  1642. * @hw: pointer to the HW structure
  1643. *
  1644. * Valid for only copper links. Read the PHY status register (sticky read)
  1645. * to verify that link is up. Read the PHY special control register to
  1646. * determine the polarity and 10base-T extended distance. Read the PHY
  1647. * special status register to determine MDI/MDIx and current speed. If
  1648. * speed is 1000, then determine cable length, local and remote receiver.
  1649. **/
  1650. s32 igb_get_phy_info_m88(struct e1000_hw *hw)
  1651. {
  1652. struct e1000_phy_info *phy = &hw->phy;
  1653. s32 ret_val;
  1654. u16 phy_data;
  1655. bool link;
  1656. if (phy->media_type != e1000_media_type_copper) {
  1657. hw_dbg("Phy info is only valid for copper media\n");
  1658. ret_val = -E1000_ERR_CONFIG;
  1659. goto out;
  1660. }
  1661. ret_val = igb_phy_has_link(hw, 1, 0, &link);
  1662. if (ret_val)
  1663. goto out;
  1664. if (!link) {
  1665. hw_dbg("Phy info is only valid if link is up\n");
  1666. ret_val = -E1000_ERR_CONFIG;
  1667. goto out;
  1668. }
  1669. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
  1670. if (ret_val)
  1671. goto out;
  1672. phy->polarity_correction = (phy_data & M88E1000_PSCR_POLARITY_REVERSAL)
  1673. ? true : false;
  1674. ret_val = igb_check_polarity_m88(hw);
  1675. if (ret_val)
  1676. goto out;
  1677. ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
  1678. if (ret_val)
  1679. goto out;
  1680. phy->is_mdix = (phy_data & M88E1000_PSSR_MDIX) ? true : false;
  1681. if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
  1682. ret_val = phy->ops.get_cable_length(hw);
  1683. if (ret_val)
  1684. goto out;
  1685. ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &phy_data);
  1686. if (ret_val)
  1687. goto out;
  1688. phy->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS)
  1689. ? e1000_1000t_rx_status_ok
  1690. : e1000_1000t_rx_status_not_ok;
  1691. phy->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS)
  1692. ? e1000_1000t_rx_status_ok
  1693. : e1000_1000t_rx_status_not_ok;
  1694. } else {
  1695. /* Set values to "undefined" */
  1696. phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
  1697. phy->local_rx = e1000_1000t_rx_status_undefined;
  1698. phy->remote_rx = e1000_1000t_rx_status_undefined;
  1699. }
  1700. out:
  1701. return ret_val;
  1702. }
  1703. /**
  1704. * igb_get_phy_info_igp - Retrieve igp PHY information
  1705. * @hw: pointer to the HW structure
  1706. *
  1707. * Read PHY status to determine if link is up. If link is up, then
  1708. * set/determine 10base-T extended distance and polarity correction. Read
  1709. * PHY port status to determine MDI/MDIx and speed. Based on the speed,
  1710. * determine on the cable length, local and remote receiver.
  1711. **/
  1712. s32 igb_get_phy_info_igp(struct e1000_hw *hw)
  1713. {
  1714. struct e1000_phy_info *phy = &hw->phy;
  1715. s32 ret_val;
  1716. u16 data;
  1717. bool link;
  1718. ret_val = igb_phy_has_link(hw, 1, 0, &link);
  1719. if (ret_val)
  1720. goto out;
  1721. if (!link) {
  1722. hw_dbg("Phy info is only valid if link is up\n");
  1723. ret_val = -E1000_ERR_CONFIG;
  1724. goto out;
  1725. }
  1726. phy->polarity_correction = true;
  1727. ret_val = igb_check_polarity_igp(hw);
  1728. if (ret_val)
  1729. goto out;
  1730. ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
  1731. if (ret_val)
  1732. goto out;
  1733. phy->is_mdix = (data & IGP01E1000_PSSR_MDIX) ? true : false;
  1734. if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
  1735. IGP01E1000_PSSR_SPEED_1000MBPS) {
  1736. ret_val = phy->ops.get_cable_length(hw);
  1737. if (ret_val)
  1738. goto out;
  1739. ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
  1740. if (ret_val)
  1741. goto out;
  1742. phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
  1743. ? e1000_1000t_rx_status_ok
  1744. : e1000_1000t_rx_status_not_ok;
  1745. phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
  1746. ? e1000_1000t_rx_status_ok
  1747. : e1000_1000t_rx_status_not_ok;
  1748. } else {
  1749. phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
  1750. phy->local_rx = e1000_1000t_rx_status_undefined;
  1751. phy->remote_rx = e1000_1000t_rx_status_undefined;
  1752. }
  1753. out:
  1754. return ret_val;
  1755. }
  1756. /**
  1757. * igb_phy_sw_reset - PHY software reset
  1758. * @hw: pointer to the HW structure
  1759. *
  1760. * Does a software reset of the PHY by reading the PHY control register and
  1761. * setting/write the control register reset bit to the PHY.
  1762. **/
  1763. s32 igb_phy_sw_reset(struct e1000_hw *hw)
  1764. {
  1765. s32 ret_val = 0;
  1766. u16 phy_ctrl;
  1767. if (!(hw->phy.ops.read_reg))
  1768. goto out;
  1769. ret_val = hw->phy.ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
  1770. if (ret_val)
  1771. goto out;
  1772. phy_ctrl |= MII_CR_RESET;
  1773. ret_val = hw->phy.ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
  1774. if (ret_val)
  1775. goto out;
  1776. udelay(1);
  1777. out:
  1778. return ret_val;
  1779. }
  1780. /**
  1781. * igb_phy_hw_reset - PHY hardware reset
  1782. * @hw: pointer to the HW structure
  1783. *
  1784. * Verify the reset block is not blocking us from resetting. Acquire
  1785. * semaphore (if necessary) and read/set/write the device control reset
  1786. * bit in the PHY. Wait the appropriate delay time for the device to
  1787. * reset and release the semaphore (if necessary).
  1788. **/
  1789. s32 igb_phy_hw_reset(struct e1000_hw *hw)
  1790. {
  1791. struct e1000_phy_info *phy = &hw->phy;
  1792. s32 ret_val;
  1793. u32 ctrl;
  1794. ret_val = igb_check_reset_block(hw);
  1795. if (ret_val) {
  1796. ret_val = 0;
  1797. goto out;
  1798. }
  1799. ret_val = phy->ops.acquire(hw);
  1800. if (ret_val)
  1801. goto out;
  1802. ctrl = rd32(E1000_CTRL);
  1803. wr32(E1000_CTRL, ctrl | E1000_CTRL_PHY_RST);
  1804. wrfl();
  1805. udelay(phy->reset_delay_us);
  1806. wr32(E1000_CTRL, ctrl);
  1807. wrfl();
  1808. udelay(150);
  1809. phy->ops.release(hw);
  1810. ret_val = phy->ops.get_cfg_done(hw);
  1811. out:
  1812. return ret_val;
  1813. }
  1814. /**
  1815. * igb_phy_init_script_igp3 - Inits the IGP3 PHY
  1816. * @hw: pointer to the HW structure
  1817. *
  1818. * Initializes a Intel Gigabit PHY3 when an EEPROM is not present.
  1819. **/
  1820. s32 igb_phy_init_script_igp3(struct e1000_hw *hw)
  1821. {
  1822. hw_dbg("Running IGP 3 PHY init script\n");
  1823. /* PHY init IGP 3 */
  1824. /* Enable rise/fall, 10-mode work in class-A */
  1825. hw->phy.ops.write_reg(hw, 0x2F5B, 0x9018);
  1826. /* Remove all caps from Replica path filter */
  1827. hw->phy.ops.write_reg(hw, 0x2F52, 0x0000);
  1828. /* Bias trimming for ADC, AFE and Driver (Default) */
  1829. hw->phy.ops.write_reg(hw, 0x2FB1, 0x8B24);
  1830. /* Increase Hybrid poly bias */
  1831. hw->phy.ops.write_reg(hw, 0x2FB2, 0xF8F0);
  1832. /* Add 4% to TX amplitude in Giga mode */
  1833. hw->phy.ops.write_reg(hw, 0x2010, 0x10B0);
  1834. /* Disable trimming (TTT) */
  1835. hw->phy.ops.write_reg(hw, 0x2011, 0x0000);
  1836. /* Poly DC correction to 94.6% + 2% for all channels */
  1837. hw->phy.ops.write_reg(hw, 0x20DD, 0x249A);
  1838. /* ABS DC correction to 95.9% */
  1839. hw->phy.ops.write_reg(hw, 0x20DE, 0x00D3);
  1840. /* BG temp curve trim */
  1841. hw->phy.ops.write_reg(hw, 0x28B4, 0x04CE);
  1842. /* Increasing ADC OPAMP stage 1 currents to max */
  1843. hw->phy.ops.write_reg(hw, 0x2F70, 0x29E4);
  1844. /* Force 1000 ( required for enabling PHY regs configuration) */
  1845. hw->phy.ops.write_reg(hw, 0x0000, 0x0140);
  1846. /* Set upd_freq to 6 */
  1847. hw->phy.ops.write_reg(hw, 0x1F30, 0x1606);
  1848. /* Disable NPDFE */
  1849. hw->phy.ops.write_reg(hw, 0x1F31, 0xB814);
  1850. /* Disable adaptive fixed FFE (Default) */
  1851. hw->phy.ops.write_reg(hw, 0x1F35, 0x002A);
  1852. /* Enable FFE hysteresis */
  1853. hw->phy.ops.write_reg(hw, 0x1F3E, 0x0067);
  1854. /* Fixed FFE for short cable lengths */
  1855. hw->phy.ops.write_reg(hw, 0x1F54, 0x0065);
  1856. /* Fixed FFE for medium cable lengths */
  1857. hw->phy.ops.write_reg(hw, 0x1F55, 0x002A);
  1858. /* Fixed FFE for long cable lengths */
  1859. hw->phy.ops.write_reg(hw, 0x1F56, 0x002A);
  1860. /* Enable Adaptive Clip Threshold */
  1861. hw->phy.ops.write_reg(hw, 0x1F72, 0x3FB0);
  1862. /* AHT reset limit to 1 */
  1863. hw->phy.ops.write_reg(hw, 0x1F76, 0xC0FF);
  1864. /* Set AHT master delay to 127 msec */
  1865. hw->phy.ops.write_reg(hw, 0x1F77, 0x1DEC);
  1866. /* Set scan bits for AHT */
  1867. hw->phy.ops.write_reg(hw, 0x1F78, 0xF9EF);
  1868. /* Set AHT Preset bits */
  1869. hw->phy.ops.write_reg(hw, 0x1F79, 0x0210);
  1870. /* Change integ_factor of channel A to 3 */
  1871. hw->phy.ops.write_reg(hw, 0x1895, 0x0003);
  1872. /* Change prop_factor of channels BCD to 8 */
  1873. hw->phy.ops.write_reg(hw, 0x1796, 0x0008);
  1874. /* Change cg_icount + enable integbp for channels BCD */
  1875. hw->phy.ops.write_reg(hw, 0x1798, 0xD008);
  1876. /* Change cg_icount + enable integbp + change prop_factor_master
  1877. * to 8 for channel A
  1878. */
  1879. hw->phy.ops.write_reg(hw, 0x1898, 0xD918);
  1880. /* Disable AHT in Slave mode on channel A */
  1881. hw->phy.ops.write_reg(hw, 0x187A, 0x0800);
  1882. /* Enable LPLU and disable AN to 1000 in non-D0a states,
  1883. * Enable SPD+B2B
  1884. */
  1885. hw->phy.ops.write_reg(hw, 0x0019, 0x008D);
  1886. /* Enable restart AN on an1000_dis change */
  1887. hw->phy.ops.write_reg(hw, 0x001B, 0x2080);
  1888. /* Enable wh_fifo read clock in 10/100 modes */
  1889. hw->phy.ops.write_reg(hw, 0x0014, 0x0045);
  1890. /* Restart AN, Speed selection is 1000 */
  1891. hw->phy.ops.write_reg(hw, 0x0000, 0x1340);
  1892. return 0;
  1893. }
  1894. /**
  1895. * igb_initialize_M88E1512_phy - Initialize M88E1512 PHY
  1896. * @hw: pointer to the HW structure
  1897. *
  1898. * Initialize Marvel 1512 to work correctly with Avoton.
  1899. **/
  1900. s32 igb_initialize_M88E1512_phy(struct e1000_hw *hw)
  1901. {
  1902. struct e1000_phy_info *phy = &hw->phy;
  1903. s32 ret_val = 0;
  1904. /* Switch to PHY page 0xFF. */
  1905. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
  1906. if (ret_val)
  1907. goto out;
  1908. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
  1909. if (ret_val)
  1910. goto out;
  1911. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
  1912. if (ret_val)
  1913. goto out;
  1914. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
  1915. if (ret_val)
  1916. goto out;
  1917. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
  1918. if (ret_val)
  1919. goto out;
  1920. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
  1921. if (ret_val)
  1922. goto out;
  1923. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
  1924. if (ret_val)
  1925. goto out;
  1926. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xCC0C);
  1927. if (ret_val)
  1928. goto out;
  1929. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
  1930. if (ret_val)
  1931. goto out;
  1932. /* Switch to PHY page 0xFB. */
  1933. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
  1934. if (ret_val)
  1935. goto out;
  1936. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x000D);
  1937. if (ret_val)
  1938. goto out;
  1939. /* Switch to PHY page 0x12. */
  1940. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
  1941. if (ret_val)
  1942. goto out;
  1943. /* Change mode to SGMII-to-Copper */
  1944. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
  1945. if (ret_val)
  1946. goto out;
  1947. /* Return the PHY to page 0. */
  1948. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
  1949. if (ret_val)
  1950. goto out;
  1951. ret_val = igb_phy_sw_reset(hw);
  1952. if (ret_val) {
  1953. hw_dbg("Error committing the PHY changes\n");
  1954. return ret_val;
  1955. }
  1956. /* msec_delay(1000); */
  1957. usleep_range(1000, 2000);
  1958. out:
  1959. return ret_val;
  1960. }
  1961. /**
  1962. * igb_initialize_M88E1543_phy - Initialize M88E1512 PHY
  1963. * @hw: pointer to the HW structure
  1964. *
  1965. * Initialize Marvell 1543 to work correctly with Avoton.
  1966. **/
  1967. s32 igb_initialize_M88E1543_phy(struct e1000_hw *hw)
  1968. {
  1969. struct e1000_phy_info *phy = &hw->phy;
  1970. s32 ret_val = 0;
  1971. /* Switch to PHY page 0xFF. */
  1972. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
  1973. if (ret_val)
  1974. goto out;
  1975. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
  1976. if (ret_val)
  1977. goto out;
  1978. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
  1979. if (ret_val)
  1980. goto out;
  1981. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
  1982. if (ret_val)
  1983. goto out;
  1984. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
  1985. if (ret_val)
  1986. goto out;
  1987. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
  1988. if (ret_val)
  1989. goto out;
  1990. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
  1991. if (ret_val)
  1992. goto out;
  1993. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xDC0C);
  1994. if (ret_val)
  1995. goto out;
  1996. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
  1997. if (ret_val)
  1998. goto out;
  1999. /* Switch to PHY page 0xFB. */
  2000. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
  2001. if (ret_val)
  2002. goto out;
  2003. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x0C0D);
  2004. if (ret_val)
  2005. goto out;
  2006. /* Switch to PHY page 0x12. */
  2007. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
  2008. if (ret_val)
  2009. goto out;
  2010. /* Change mode to SGMII-to-Copper */
  2011. ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
  2012. if (ret_val)
  2013. goto out;
  2014. /* Switch to PHY page 1. */
  2015. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x1);
  2016. if (ret_val)
  2017. goto out;
  2018. /* Change mode to 1000BASE-X/SGMII and autoneg enable */
  2019. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_FIBER_CTRL, 0x9140);
  2020. if (ret_val)
  2021. goto out;
  2022. /* Return the PHY to page 0. */
  2023. ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
  2024. if (ret_val)
  2025. goto out;
  2026. ret_val = igb_phy_sw_reset(hw);
  2027. if (ret_val) {
  2028. hw_dbg("Error committing the PHY changes\n");
  2029. return ret_val;
  2030. }
  2031. /* msec_delay(1000); */
  2032. usleep_range(1000, 2000);
  2033. out:
  2034. return ret_val;
  2035. }
  2036. /**
  2037. * igb_power_up_phy_copper - Restore copper link in case of PHY power down
  2038. * @hw: pointer to the HW structure
  2039. *
  2040. * In the case of a PHY power down to save power, or to turn off link during a
  2041. * driver unload, restore the link to previous settings.
  2042. **/
  2043. void igb_power_up_phy_copper(struct e1000_hw *hw)
  2044. {
  2045. u16 mii_reg = 0;
  2046. /* The PHY will retain its settings across a power down/up cycle */
  2047. hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
  2048. mii_reg &= ~MII_CR_POWER_DOWN;
  2049. hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
  2050. }
  2051. /**
  2052. * igb_power_down_phy_copper - Power down copper PHY
  2053. * @hw: pointer to the HW structure
  2054. *
  2055. * Power down PHY to save power when interface is down and wake on lan
  2056. * is not enabled.
  2057. **/
  2058. void igb_power_down_phy_copper(struct e1000_hw *hw)
  2059. {
  2060. u16 mii_reg = 0;
  2061. /* The PHY will retain its settings across a power down/up cycle */
  2062. hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
  2063. mii_reg |= MII_CR_POWER_DOWN;
  2064. hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
  2065. usleep_range(1000, 2000);
  2066. }
  2067. /**
  2068. * igb_check_polarity_82580 - Checks the polarity.
  2069. * @hw: pointer to the HW structure
  2070. *
  2071. * Success returns 0, Failure returns -E1000_ERR_PHY (-2)
  2072. *
  2073. * Polarity is determined based on the PHY specific status register.
  2074. **/
  2075. static s32 igb_check_polarity_82580(struct e1000_hw *hw)
  2076. {
  2077. struct e1000_phy_info *phy = &hw->phy;
  2078. s32 ret_val;
  2079. u16 data;
  2080. ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
  2081. if (!ret_val)
  2082. phy->cable_polarity = (data & I82580_PHY_STATUS2_REV_POLARITY)
  2083. ? e1000_rev_polarity_reversed
  2084. : e1000_rev_polarity_normal;
  2085. return ret_val;
  2086. }
  2087. /**
  2088. * igb_phy_force_speed_duplex_82580 - Force speed/duplex for I82580 PHY
  2089. * @hw: pointer to the HW structure
  2090. *
  2091. * Calls the PHY setup function to force speed and duplex. Clears the
  2092. * auto-crossover to force MDI manually. Waits for link and returns
  2093. * successful if link up is successful, else -E1000_ERR_PHY (-2).
  2094. **/
  2095. s32 igb_phy_force_speed_duplex_82580(struct e1000_hw *hw)
  2096. {
  2097. struct e1000_phy_info *phy = &hw->phy;
  2098. s32 ret_val;
  2099. u16 phy_data;
  2100. bool link;
  2101. ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
  2102. if (ret_val)
  2103. goto out;
  2104. igb_phy_force_speed_duplex_setup(hw, &phy_data);
  2105. ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
  2106. if (ret_val)
  2107. goto out;
  2108. /* Clear Auto-Crossover to force MDI manually. 82580 requires MDI
  2109. * forced whenever speed and duplex are forced.
  2110. */
  2111. ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
  2112. if (ret_val)
  2113. goto out;
  2114. phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
  2115. ret_val = phy->ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
  2116. if (ret_val)
  2117. goto out;
  2118. hw_dbg("I82580_PHY_CTRL_2: %X\n", phy_data);
  2119. udelay(1);
  2120. if (phy->autoneg_wait_to_complete) {
  2121. hw_dbg("Waiting for forced speed/duplex link on 82580 phy\n");
  2122. ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
  2123. if (ret_val)
  2124. goto out;
  2125. if (!link)
  2126. hw_dbg("Link taking longer than expected.\n");
  2127. /* Try once more */
  2128. ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
  2129. if (ret_val)
  2130. goto out;
  2131. }
  2132. out:
  2133. return ret_val;
  2134. }
  2135. /**
  2136. * igb_get_phy_info_82580 - Retrieve I82580 PHY information
  2137. * @hw: pointer to the HW structure
  2138. *
  2139. * Read PHY status to determine if link is up. If link is up, then
  2140. * set/determine 10base-T extended distance and polarity correction. Read
  2141. * PHY port status to determine MDI/MDIx and speed. Based on the speed,
  2142. * determine on the cable length, local and remote receiver.
  2143. **/
  2144. s32 igb_get_phy_info_82580(struct e1000_hw *hw)
  2145. {
  2146. struct e1000_phy_info *phy = &hw->phy;
  2147. s32 ret_val;
  2148. u16 data;
  2149. bool link;
  2150. ret_val = igb_phy_has_link(hw, 1, 0, &link);
  2151. if (ret_val)
  2152. goto out;
  2153. if (!link) {
  2154. hw_dbg("Phy info is only valid if link is up\n");
  2155. ret_val = -E1000_ERR_CONFIG;
  2156. goto out;
  2157. }
  2158. phy->polarity_correction = true;
  2159. ret_val = igb_check_polarity_82580(hw);
  2160. if (ret_val)
  2161. goto out;
  2162. ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
  2163. if (ret_val)
  2164. goto out;
  2165. phy->is_mdix = (data & I82580_PHY_STATUS2_MDIX) ? true : false;
  2166. if ((data & I82580_PHY_STATUS2_SPEED_MASK) ==
  2167. I82580_PHY_STATUS2_SPEED_1000MBPS) {
  2168. ret_val = hw->phy.ops.get_cable_length(hw);
  2169. if (ret_val)
  2170. goto out;
  2171. ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
  2172. if (ret_val)
  2173. goto out;
  2174. phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
  2175. ? e1000_1000t_rx_status_ok
  2176. : e1000_1000t_rx_status_not_ok;
  2177. phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
  2178. ? e1000_1000t_rx_status_ok
  2179. : e1000_1000t_rx_status_not_ok;
  2180. } else {
  2181. phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
  2182. phy->local_rx = e1000_1000t_rx_status_undefined;
  2183. phy->remote_rx = e1000_1000t_rx_status_undefined;
  2184. }
  2185. out:
  2186. return ret_val;
  2187. }
  2188. /**
  2189. * igb_get_cable_length_82580 - Determine cable length for 82580 PHY
  2190. * @hw: pointer to the HW structure
  2191. *
  2192. * Reads the diagnostic status register and verifies result is valid before
  2193. * placing it in the phy_cable_length field.
  2194. **/
  2195. s32 igb_get_cable_length_82580(struct e1000_hw *hw)
  2196. {
  2197. struct e1000_phy_info *phy = &hw->phy;
  2198. s32 ret_val;
  2199. u16 phy_data, length;
  2200. ret_val = phy->ops.read_reg(hw, I82580_PHY_DIAG_STATUS, &phy_data);
  2201. if (ret_val)
  2202. goto out;
  2203. length = FIELD_GET(I82580_DSTATUS_CABLE_LENGTH, phy_data);
  2204. if (length == E1000_CABLE_LENGTH_UNDEFINED)
  2205. ret_val = -E1000_ERR_PHY;
  2206. phy->cable_length = length;
  2207. out:
  2208. return ret_val;
  2209. }
  2210. /**
  2211. * igb_set_master_slave_mode - Setup PHY for Master/slave mode
  2212. * @hw: pointer to the HW structure
  2213. *
  2214. * Sets up Master/slave mode
  2215. **/
  2216. static s32 igb_set_master_slave_mode(struct e1000_hw *hw)
  2217. {
  2218. s32 ret_val;
  2219. u16 phy_data;
  2220. /* Resolve Master/Slave mode */
  2221. ret_val = hw->phy.ops.read_reg(hw, PHY_1000T_CTRL, &phy_data);
  2222. if (ret_val)
  2223. return ret_val;
  2224. /* load defaults for future use */
  2225. hw->phy.original_ms_type = (phy_data & CR_1000T_MS_ENABLE) ?
  2226. ((phy_data & CR_1000T_MS_VALUE) ?
  2227. e1000_ms_force_master :
  2228. e1000_ms_force_slave) : e1000_ms_auto;
  2229. switch (hw->phy.ms_type) {
  2230. case e1000_ms_force_master:
  2231. phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
  2232. break;
  2233. case e1000_ms_force_slave:
  2234. phy_data |= CR_1000T_MS_ENABLE;
  2235. phy_data &= ~(CR_1000T_MS_VALUE);
  2236. break;
  2237. case e1000_ms_auto:
  2238. phy_data &= ~CR_1000T_MS_ENABLE;
  2239. fallthrough;
  2240. default:
  2241. break;
  2242. }
  2243. return hw->phy.ops.write_reg(hw, PHY_1000T_CTRL, phy_data);
  2244. }