e1000.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063
  1. /**************************************************************************
  2. Intel Pro 1000 for ppcboot/das-u-boot
  3. Drivers are port from Intel's Linux driver e1000-4.3.15
  4. and from Etherboot pro 1000 driver by mrakes at vivato dot net
  5. tested on both gig copper and gig fiber boards
  6. ***************************************************************************/
  7. /*******************************************************************************
  8. Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
  9. This program is free software; you can redistribute it and/or modify it
  10. under the terms of the GNU General Public License as published by the Free
  11. Software Foundation; either version 2 of the License, or (at your option)
  12. any later version.
  13. This program is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. more details.
  17. You should have received a copy of the GNU General Public License along with
  18. this program; if not, write to the Free Software Foundation, Inc., 59
  19. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. The full GNU General Public License is included in this distribution in the
  21. file called LICENSE.
  22. Contact Information:
  23. Linux NICS <linux.nics@intel.com>
  24. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *******************************************************************************/
  26. /*
  27. * Copyright (C) Archway Digital Solutions.
  28. *
  29. * written by Chrsitopher Li <cli at arcyway dot com> or <chrisl at gnuchina dot org>
  30. * 2/9/2002
  31. *
  32. * Copyright (C) Linux Networx.
  33. * Massive upgrade to work with the new intel gigabit NICs.
  34. * <ebiederman at lnxi dot com>
  35. */
  36. #include "e1000.h"
  37. #define TOUT_LOOP 100000
  38. #undef virt_to_bus
  39. #define virt_to_bus(x) ((unsigned long)x)
  40. #define bus_to_phys(devno, a) pci_mem_to_phys(devno, a)
  41. #define mdelay(n) udelay((n)*1000)
  42. #define E1000_DEFAULT_PBA 0x00000030
  43. /* NIC specific static variables go here */
  44. static char tx_pool[128 + 16];
  45. static char rx_pool[128 + 16];
  46. static char packet[2096];
  47. static struct e1000_tx_desc *tx_base;
  48. static struct e1000_rx_desc *rx_base;
  49. static int tx_tail;
  50. static int rx_tail, rx_last;
  51. static struct pci_device_id supported[] = {
  52. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82542},
  53. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_FIBER},
  54. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82543GC_COPPER},
  55. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_COPPER},
  56. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544EI_FIBER},
  57. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_COPPER},
  58. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82544GC_LOM},
  59. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM},
  60. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_COPPER},
  61. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545GM_COPPER},
  62. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_COPPER},
  63. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82545EM_FIBER},
  64. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82546EB_FIBER},
  65. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82540EM_LOM},
  66. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541ER},
  67. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82541GI_LF},
  68. };
  69. /* Function forward declarations */
  70. static int e1000_setup_link(struct eth_device *nic);
  71. static int e1000_setup_fiber_link(struct eth_device *nic);
  72. static int e1000_setup_copper_link(struct eth_device *nic);
  73. static int e1000_phy_setup_autoneg(struct e1000_hw *hw);
  74. static void e1000_config_collision_dist(struct e1000_hw *hw);
  75. static int e1000_config_mac_to_phy(struct e1000_hw *hw);
  76. static int e1000_config_fc_after_link_up(struct e1000_hw *hw);
  77. static int e1000_check_for_link(struct eth_device *nic);
  78. static int e1000_wait_autoneg(struct e1000_hw *hw);
  79. static void e1000_get_speed_and_duplex(struct e1000_hw *hw, uint16_t * speed,
  80. uint16_t * duplex);
  81. static int e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
  82. uint16_t * phy_data);
  83. static int e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr,
  84. uint16_t phy_data);
  85. static void e1000_phy_hw_reset(struct e1000_hw *hw);
  86. static int e1000_phy_reset(struct e1000_hw *hw);
  87. static int e1000_detect_gig_phy(struct e1000_hw *hw);
  88. #define E1000_WRITE_REG(a, reg, value) (writel((value), ((a)->hw_addr + E1000_##reg)))
  89. #define E1000_READ_REG(a, reg) (readl((a)->hw_addr + E1000_##reg))
  90. #define E1000_WRITE_REG_ARRAY(a, reg, offset, value) (\
  91. writel((value), ((a)->hw_addr + E1000_##reg + ((offset) << 2))))
  92. #define E1000_READ_REG_ARRAY(a, reg, offset) ( \
  93. readl((a)->hw_addr + E1000_##reg + ((offset) << 2)))
  94. #define E1000_WRITE_FLUSH(a) {uint32_t x; x = E1000_READ_REG(a, STATUS);}
  95. #ifndef CONFIG_AP1000 /* remove for warnings */
  96. /******************************************************************************
  97. * Raises the EEPROM's clock input.
  98. *
  99. * hw - Struct containing variables accessed by shared code
  100. * eecd - EECD's current value
  101. *****************************************************************************/
  102. static void
  103. e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
  104. {
  105. /* Raise the clock input to the EEPROM (by setting the SK bit), and then
  106. * wait 50 microseconds.
  107. */
  108. *eecd = *eecd | E1000_EECD_SK;
  109. E1000_WRITE_REG(hw, EECD, *eecd);
  110. E1000_WRITE_FLUSH(hw);
  111. udelay(50);
  112. }
  113. /******************************************************************************
  114. * Lowers the EEPROM's clock input.
  115. *
  116. * hw - Struct containing variables accessed by shared code
  117. * eecd - EECD's current value
  118. *****************************************************************************/
  119. static void
  120. e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t * eecd)
  121. {
  122. /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
  123. * wait 50 microseconds.
  124. */
  125. *eecd = *eecd & ~E1000_EECD_SK;
  126. E1000_WRITE_REG(hw, EECD, *eecd);
  127. E1000_WRITE_FLUSH(hw);
  128. udelay(50);
  129. }
  130. /******************************************************************************
  131. * Shift data bits out to the EEPROM.
  132. *
  133. * hw - Struct containing variables accessed by shared code
  134. * data - data to send to the EEPROM
  135. * count - number of bits to shift out
  136. *****************************************************************************/
  137. static void
  138. e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data, uint16_t count)
  139. {
  140. uint32_t eecd;
  141. uint32_t mask;
  142. /* We need to shift "count" bits out to the EEPROM. So, value in the
  143. * "data" parameter will be shifted out to the EEPROM one bit at a time.
  144. * In order to do this, "data" must be broken down into bits.
  145. */
  146. mask = 0x01 << (count - 1);
  147. eecd = E1000_READ_REG(hw, EECD);
  148. eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
  149. do {
  150. /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
  151. * and then raising and then lowering the clock (the SK bit controls
  152. * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
  153. * by setting "DI" to "0" and then raising and then lowering the clock.
  154. */
  155. eecd &= ~E1000_EECD_DI;
  156. if (data & mask)
  157. eecd |= E1000_EECD_DI;
  158. E1000_WRITE_REG(hw, EECD, eecd);
  159. E1000_WRITE_FLUSH(hw);
  160. udelay(50);
  161. e1000_raise_ee_clk(hw, &eecd);
  162. e1000_lower_ee_clk(hw, &eecd);
  163. mask = mask >> 1;
  164. } while (mask);
  165. /* We leave the "DI" bit set to "0" when we leave this routine. */
  166. eecd &= ~E1000_EECD_DI;
  167. E1000_WRITE_REG(hw, EECD, eecd);
  168. }
  169. /******************************************************************************
  170. * Shift data bits in from the EEPROM
  171. *
  172. * hw - Struct containing variables accessed by shared code
  173. *****************************************************************************/
  174. static uint16_t
  175. e1000_shift_in_ee_bits(struct e1000_hw *hw)
  176. {
  177. uint32_t eecd;
  178. uint32_t i;
  179. uint16_t data;
  180. /* In order to read a register from the EEPROM, we need to shift 16 bits
  181. * in from the EEPROM. Bits are "shifted in" by raising the clock input to
  182. * the EEPROM (setting the SK bit), and then reading the value of the "DO"
  183. * bit. During this "shifting in" process the "DI" bit should always be
  184. * clear..
  185. */
  186. eecd = E1000_READ_REG(hw, EECD);
  187. eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
  188. data = 0;
  189. for (i = 0; i < 16; i++) {
  190. data = data << 1;
  191. e1000_raise_ee_clk(hw, &eecd);
  192. eecd = E1000_READ_REG(hw, EECD);
  193. eecd &= ~(E1000_EECD_DI);
  194. if (eecd & E1000_EECD_DO)
  195. data |= 1;
  196. e1000_lower_ee_clk(hw, &eecd);
  197. }
  198. return data;
  199. }
  200. /******************************************************************************
  201. * Prepares EEPROM for access
  202. *
  203. * hw - Struct containing variables accessed by shared code
  204. *
  205. * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
  206. * function should be called before issuing a command to the EEPROM.
  207. *****************************************************************************/
  208. static void
  209. e1000_setup_eeprom(struct e1000_hw *hw)
  210. {
  211. uint32_t eecd;
  212. eecd = E1000_READ_REG(hw, EECD);
  213. /* Clear SK and DI */
  214. eecd &= ~(E1000_EECD_SK | E1000_EECD_DI);
  215. E1000_WRITE_REG(hw, EECD, eecd);
  216. /* Set CS */
  217. eecd |= E1000_EECD_CS;
  218. E1000_WRITE_REG(hw, EECD, eecd);
  219. }
  220. /******************************************************************************
  221. * Returns EEPROM to a "standby" state
  222. *
  223. * hw - Struct containing variables accessed by shared code
  224. *****************************************************************************/
  225. static void
  226. e1000_standby_eeprom(struct e1000_hw *hw)
  227. {
  228. uint32_t eecd;
  229. eecd = E1000_READ_REG(hw, EECD);
  230. /* Deselct EEPROM */
  231. eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
  232. E1000_WRITE_REG(hw, EECD, eecd);
  233. E1000_WRITE_FLUSH(hw);
  234. udelay(50);
  235. /* Clock high */
  236. eecd |= E1000_EECD_SK;
  237. E1000_WRITE_REG(hw, EECD, eecd);
  238. E1000_WRITE_FLUSH(hw);
  239. udelay(50);
  240. /* Select EEPROM */
  241. eecd |= E1000_EECD_CS;
  242. E1000_WRITE_REG(hw, EECD, eecd);
  243. E1000_WRITE_FLUSH(hw);
  244. udelay(50);
  245. /* Clock low */
  246. eecd &= ~E1000_EECD_SK;
  247. E1000_WRITE_REG(hw, EECD, eecd);
  248. E1000_WRITE_FLUSH(hw);
  249. udelay(50);
  250. }
  251. /******************************************************************************
  252. * Reads a 16 bit word from the EEPROM.
  253. *
  254. * hw - Struct containing variables accessed by shared code
  255. * offset - offset of word in the EEPROM to read
  256. * data - word read from the EEPROM
  257. *****************************************************************************/
  258. static int
  259. e1000_read_eeprom(struct e1000_hw *hw, uint16_t offset, uint16_t * data)
  260. {
  261. uint32_t eecd;
  262. uint32_t i = 0;
  263. int large_eeprom = FALSE;
  264. /* Request EEPROM Access */
  265. if (hw->mac_type > e1000_82544) {
  266. eecd = E1000_READ_REG(hw, EECD);
  267. if (eecd & E1000_EECD_SIZE)
  268. large_eeprom = TRUE;
  269. eecd |= E1000_EECD_REQ;
  270. E1000_WRITE_REG(hw, EECD, eecd);
  271. eecd = E1000_READ_REG(hw, EECD);
  272. while ((!(eecd & E1000_EECD_GNT)) && (i < 100)) {
  273. i++;
  274. udelay(10);
  275. eecd = E1000_READ_REG(hw, EECD);
  276. }
  277. if (!(eecd & E1000_EECD_GNT)) {
  278. eecd &= ~E1000_EECD_REQ;
  279. E1000_WRITE_REG(hw, EECD, eecd);
  280. DEBUGOUT("Could not acquire EEPROM grant\n");
  281. return -E1000_ERR_EEPROM;
  282. }
  283. }
  284. /* Prepare the EEPROM for reading */
  285. e1000_setup_eeprom(hw);
  286. /* Send the READ command (opcode + addr) */
  287. e1000_shift_out_ee_bits(hw, EEPROM_READ_OPCODE, 3);
  288. e1000_shift_out_ee_bits(hw, offset, (large_eeprom) ? 8 : 6);
  289. /* Read the data */
  290. *data = e1000_shift_in_ee_bits(hw);
  291. /* End this read operation */
  292. e1000_standby_eeprom(hw);
  293. /* Stop requesting EEPROM access */
  294. if (hw->mac_type > e1000_82544) {
  295. eecd = E1000_READ_REG(hw, EECD);
  296. eecd &= ~E1000_EECD_REQ;
  297. E1000_WRITE_REG(hw, EECD, eecd);
  298. }
  299. return 0;
  300. }
  301. #if 0
  302. static void
  303. e1000_eeprom_cleanup(struct e1000_hw *hw)
  304. {
  305. uint32_t eecd;
  306. eecd = E1000_READ_REG(hw, EECD);
  307. eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
  308. E1000_WRITE_REG(hw, EECD, eecd);
  309. e1000_raise_ee_clk(hw, &eecd);
  310. e1000_lower_ee_clk(hw, &eecd);
  311. }
  312. static uint16_t
  313. e1000_wait_eeprom_done(struct e1000_hw *hw)
  314. {
  315. uint32_t eecd;
  316. uint32_t i;
  317. e1000_standby_eeprom(hw);
  318. for (i = 0; i < 200; i++) {
  319. eecd = E1000_READ_REG(hw, EECD);
  320. if (eecd & E1000_EECD_DO)
  321. return (TRUE);
  322. udelay(5);
  323. }
  324. return (FALSE);
  325. }
  326. static int
  327. e1000_write_eeprom(struct e1000_hw *hw, uint16_t Reg, uint16_t Data)
  328. {
  329. uint32_t eecd;
  330. int large_eeprom = FALSE;
  331. int i = 0;
  332. /* Request EEPROM Access */
  333. if (hw->mac_type > e1000_82544) {
  334. eecd = E1000_READ_REG(hw, EECD);
  335. if (eecd & E1000_EECD_SIZE)
  336. large_eeprom = TRUE;
  337. eecd |= E1000_EECD_REQ;
  338. E1000_WRITE_REG(hw, EECD, eecd);
  339. eecd = E1000_READ_REG(hw, EECD);
  340. while ((!(eecd & E1000_EECD_GNT)) && (i < 100)) {
  341. i++;
  342. udelay(5);
  343. eecd = E1000_READ_REG(hw, EECD);
  344. }
  345. if (!(eecd & E1000_EECD_GNT)) {
  346. eecd &= ~E1000_EECD_REQ;
  347. E1000_WRITE_REG(hw, EECD, eecd);
  348. DEBUGOUT("Could not acquire EEPROM grant\n");
  349. return FALSE;
  350. }
  351. }
  352. e1000_setup_eeprom(hw);
  353. e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE, 5);
  354. e1000_shift_out_ee_bits(hw, Reg, (large_eeprom) ? 6 : 4);
  355. e1000_standby_eeprom(hw);
  356. e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE, 3);
  357. e1000_shift_out_ee_bits(hw, Reg, (large_eeprom) ? 8 : 6);
  358. e1000_shift_out_ee_bits(hw, Data, 16);
  359. if (!e1000_wait_eeprom_done(hw)) {
  360. return FALSE;
  361. }
  362. e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE, 5);
  363. e1000_shift_out_ee_bits(hw, Reg, (large_eeprom) ? 6 : 4);
  364. e1000_eeprom_cleanup(hw);
  365. /* Stop requesting EEPROM access */
  366. if (hw->mac_type > e1000_82544) {
  367. eecd = E1000_READ_REG(hw, EECD);
  368. eecd &= ~E1000_EECD_REQ;
  369. E1000_WRITE_REG(hw, EECD, eecd);
  370. }
  371. i = 0;
  372. eecd = E1000_READ_REG(hw, EECD);
  373. while (((eecd & E1000_EECD_GNT)) && (i < 500)) {
  374. i++;
  375. udelay(10);
  376. eecd = E1000_READ_REG(hw, EECD);
  377. }
  378. if ((eecd & E1000_EECD_GNT)) {
  379. DEBUGOUT("Could not release EEPROM grant\n");
  380. }
  381. return TRUE;
  382. }
  383. #endif
  384. /******************************************************************************
  385. * Verifies that the EEPROM has a valid checksum
  386. *
  387. * hw - Struct containing variables accessed by shared code
  388. *
  389. * Reads the first 64 16 bit words of the EEPROM and sums the values read.
  390. * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
  391. * valid.
  392. *****************************************************************************/
  393. static int
  394. e1000_validate_eeprom_checksum(struct eth_device *nic)
  395. {
  396. struct e1000_hw *hw = nic->priv;
  397. uint16_t checksum = 0;
  398. uint16_t i, eeprom_data;
  399. DEBUGFUNC();
  400. for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
  401. if (e1000_read_eeprom(hw, i, &eeprom_data) < 0) {
  402. DEBUGOUT("EEPROM Read Error\n");
  403. return -E1000_ERR_EEPROM;
  404. }
  405. checksum += eeprom_data;
  406. }
  407. if (checksum == (uint16_t) EEPROM_SUM) {
  408. return 0;
  409. } else {
  410. DEBUGOUT("EEPROM Checksum Invalid\n");
  411. return -E1000_ERR_EEPROM;
  412. }
  413. }
  414. #endif /* #ifndef CONFIG_AP1000 */
  415. /******************************************************************************
  416. * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
  417. * second function of dual function devices
  418. *
  419. * nic - Struct containing variables accessed by shared code
  420. *****************************************************************************/
  421. static int
  422. e1000_read_mac_addr(struct eth_device *nic)
  423. {
  424. #ifndef CONFIG_AP1000
  425. struct e1000_hw *hw = nic->priv;
  426. uint16_t offset;
  427. uint16_t eeprom_data;
  428. int i;
  429. DEBUGFUNC();
  430. for (i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
  431. offset = i >> 1;
  432. if (e1000_read_eeprom(hw, offset, &eeprom_data) < 0) {
  433. DEBUGOUT("EEPROM Read Error\n");
  434. return -E1000_ERR_EEPROM;
  435. }
  436. nic->enetaddr[i] = eeprom_data & 0xff;
  437. nic->enetaddr[i + 1] = (eeprom_data >> 8) & 0xff;
  438. }
  439. if ((hw->mac_type == e1000_82546) &&
  440. (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
  441. /* Invert the last bit if this is the second device */
  442. nic->enetaddr[5] += 1;
  443. }
  444. #ifdef CONFIG_E1000_FALLBACK_MAC
  445. if ( *(u32*)(nic->enetaddr) == 0 || *(u32*)(nic->enetaddr) == ~0 ) {
  446. unsigned char fb_mac[NODE_ADDRESS_SIZE] = CONFIG_E1000_FALLBACK_MAC;
  447. memcpy (nic->enetaddr, fb_mac, NODE_ADDRESS_SIZE);
  448. }
  449. #endif
  450. #else
  451. /*
  452. * The AP1000's e1000 has no eeprom; the MAC address is stored in the
  453. * environment variables. Currently this does not support the addition
  454. * of a PMC e1000 card, which is certainly a possibility, so this should
  455. * be updated to properly use the env variable only for the onboard e1000
  456. */
  457. int ii;
  458. char *s, *e;
  459. DEBUGFUNC();
  460. s = getenv ("ethaddr");
  461. if (s == NULL) {
  462. return -E1000_ERR_EEPROM;
  463. } else {
  464. for(ii = 0; ii < 6; ii++) {
  465. nic->enetaddr[ii] = s ? simple_strtoul (s, &e, 16) : 0;
  466. if (s){
  467. s = (*e) ? e + 1 : e;
  468. }
  469. }
  470. }
  471. #endif
  472. return 0;
  473. }
  474. /******************************************************************************
  475. * Initializes receive address filters.
  476. *
  477. * hw - Struct containing variables accessed by shared code
  478. *
  479. * Places the MAC address in receive address register 0 and clears the rest
  480. * of the receive addresss registers. Clears the multicast table. Assumes
  481. * the receiver is in reset when the routine is called.
  482. *****************************************************************************/
  483. static void
  484. e1000_init_rx_addrs(struct eth_device *nic)
  485. {
  486. struct e1000_hw *hw = nic->priv;
  487. uint32_t i;
  488. uint32_t addr_low;
  489. uint32_t addr_high;
  490. DEBUGFUNC();
  491. /* Setup the receive address. */
  492. DEBUGOUT("Programming MAC Address into RAR[0]\n");
  493. addr_low = (nic->enetaddr[0] |
  494. (nic->enetaddr[1] << 8) |
  495. (nic->enetaddr[2] << 16) | (nic->enetaddr[3] << 24));
  496. addr_high = (nic->enetaddr[4] | (nic->enetaddr[5] << 8) | E1000_RAH_AV);
  497. E1000_WRITE_REG_ARRAY(hw, RA, 0, addr_low);
  498. E1000_WRITE_REG_ARRAY(hw, RA, 1, addr_high);
  499. /* Zero out the other 15 receive addresses. */
  500. DEBUGOUT("Clearing RAR[1-15]\n");
  501. for (i = 1; i < E1000_RAR_ENTRIES; i++) {
  502. E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
  503. E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
  504. }
  505. }
  506. /******************************************************************************
  507. * Clears the VLAN filer table
  508. *
  509. * hw - Struct containing variables accessed by shared code
  510. *****************************************************************************/
  511. static void
  512. e1000_clear_vfta(struct e1000_hw *hw)
  513. {
  514. uint32_t offset;
  515. for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
  516. E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
  517. }
  518. /******************************************************************************
  519. * Set the mac type member in the hw struct.
  520. *
  521. * hw - Struct containing variables accessed by shared code
  522. *****************************************************************************/
  523. static int
  524. e1000_set_mac_type(struct e1000_hw *hw)
  525. {
  526. DEBUGFUNC();
  527. switch (hw->device_id) {
  528. case E1000_DEV_ID_82542:
  529. switch (hw->revision_id) {
  530. case E1000_82542_2_0_REV_ID:
  531. hw->mac_type = e1000_82542_rev2_0;
  532. break;
  533. case E1000_82542_2_1_REV_ID:
  534. hw->mac_type = e1000_82542_rev2_1;
  535. break;
  536. default:
  537. /* Invalid 82542 revision ID */
  538. return -E1000_ERR_MAC_TYPE;
  539. }
  540. break;
  541. case E1000_DEV_ID_82543GC_FIBER:
  542. case E1000_DEV_ID_82543GC_COPPER:
  543. hw->mac_type = e1000_82543;
  544. break;
  545. case E1000_DEV_ID_82544EI_COPPER:
  546. case E1000_DEV_ID_82544EI_FIBER:
  547. case E1000_DEV_ID_82544GC_COPPER:
  548. case E1000_DEV_ID_82544GC_LOM:
  549. hw->mac_type = e1000_82544;
  550. break;
  551. case E1000_DEV_ID_82540EM:
  552. case E1000_DEV_ID_82540EM_LOM:
  553. hw->mac_type = e1000_82540;
  554. break;
  555. case E1000_DEV_ID_82545EM_COPPER:
  556. case E1000_DEV_ID_82545GM_COPPER:
  557. case E1000_DEV_ID_82545EM_FIBER:
  558. hw->mac_type = e1000_82545;
  559. break;
  560. case E1000_DEV_ID_82546EB_COPPER:
  561. case E1000_DEV_ID_82546EB_FIBER:
  562. hw->mac_type = e1000_82546;
  563. break;
  564. case E1000_DEV_ID_82541ER:
  565. case E1000_DEV_ID_82541GI_LF:
  566. hw->mac_type = e1000_82541_rev_2;
  567. break;
  568. default:
  569. /* Should never have loaded on this device */
  570. return -E1000_ERR_MAC_TYPE;
  571. }
  572. return E1000_SUCCESS;
  573. }
  574. /******************************************************************************
  575. * Reset the transmit and receive units; mask and clear all interrupts.
  576. *
  577. * hw - Struct containing variables accessed by shared code
  578. *****************************************************************************/
  579. void
  580. e1000_reset_hw(struct e1000_hw *hw)
  581. {
  582. uint32_t ctrl;
  583. uint32_t ctrl_ext;
  584. uint32_t icr;
  585. uint32_t manc;
  586. DEBUGFUNC();
  587. /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
  588. if (hw->mac_type == e1000_82542_rev2_0) {
  589. DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
  590. pci_write_config_word(hw->pdev, PCI_COMMAND,
  591. hw->
  592. pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
  593. }
  594. /* Clear interrupt mask to stop board from generating interrupts */
  595. DEBUGOUT("Masking off all interrupts\n");
  596. E1000_WRITE_REG(hw, IMC, 0xffffffff);
  597. /* Disable the Transmit and Receive units. Then delay to allow
  598. * any pending transactions to complete before we hit the MAC with
  599. * the global reset.
  600. */
  601. E1000_WRITE_REG(hw, RCTL, 0);
  602. E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
  603. E1000_WRITE_FLUSH(hw);
  604. /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
  605. hw->tbi_compatibility_on = FALSE;
  606. /* Delay to allow any outstanding PCI transactions to complete before
  607. * resetting the device
  608. */
  609. mdelay(10);
  610. /* Issue a global reset to the MAC. This will reset the chip's
  611. * transmit, receive, DMA, and link units. It will not effect
  612. * the current PCI configuration. The global reset bit is self-
  613. * clearing, and should clear within a microsecond.
  614. */
  615. DEBUGOUT("Issuing a global reset to MAC\n");
  616. ctrl = E1000_READ_REG(hw, CTRL);
  617. #if 0
  618. if (hw->mac_type > e1000_82543)
  619. E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
  620. else
  621. #endif
  622. E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
  623. /* Force a reload from the EEPROM if necessary */
  624. if (hw->mac_type < e1000_82540) {
  625. /* Wait for reset to complete */
  626. udelay(10);
  627. ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
  628. ctrl_ext |= E1000_CTRL_EXT_EE_RST;
  629. E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
  630. E1000_WRITE_FLUSH(hw);
  631. /* Wait for EEPROM reload */
  632. mdelay(2);
  633. } else {
  634. /* Wait for EEPROM reload (it happens automatically) */
  635. mdelay(4);
  636. /* Dissable HW ARPs on ASF enabled adapters */
  637. manc = E1000_READ_REG(hw, MANC);
  638. manc &= ~(E1000_MANC_ARP_EN);
  639. E1000_WRITE_REG(hw, MANC, manc);
  640. }
  641. /* Clear interrupt mask to stop board from generating interrupts */
  642. DEBUGOUT("Masking off all interrupts\n");
  643. E1000_WRITE_REG(hw, IMC, 0xffffffff);
  644. /* Clear any pending interrupt events. */
  645. icr = E1000_READ_REG(hw, ICR);
  646. /* If MWI was previously enabled, reenable it. */
  647. if (hw->mac_type == e1000_82542_rev2_0) {
  648. pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
  649. }
  650. }
  651. /******************************************************************************
  652. * Performs basic configuration of the adapter.
  653. *
  654. * hw - Struct containing variables accessed by shared code
  655. *
  656. * Assumes that the controller has previously been reset and is in a
  657. * post-reset uninitialized state. Initializes the receive address registers,
  658. * multicast table, and VLAN filter table. Calls routines to setup link
  659. * configuration and flow control settings. Clears all on-chip counters. Leaves
  660. * the transmit and receive units disabled and uninitialized.
  661. *****************************************************************************/
  662. static int
  663. e1000_init_hw(struct eth_device *nic)
  664. {
  665. struct e1000_hw *hw = nic->priv;
  666. uint32_t ctrl, status;
  667. uint32_t i;
  668. int32_t ret_val;
  669. uint16_t pcix_cmd_word;
  670. uint16_t pcix_stat_hi_word;
  671. uint16_t cmd_mmrbc;
  672. uint16_t stat_mmrbc;
  673. e1000_bus_type bus_type = e1000_bus_type_unknown;
  674. DEBUGFUNC();
  675. #if 0
  676. /* Initialize Identification LED */
  677. ret_val = e1000_id_led_init(hw);
  678. if (ret_val < 0) {
  679. DEBUGOUT("Error Initializing Identification LED\n");
  680. return ret_val;
  681. }
  682. #endif
  683. /* Set the Media Type and exit with error if it is not valid. */
  684. if (hw->mac_type != e1000_82543) {
  685. /* tbi_compatibility is only valid on 82543 */
  686. hw->tbi_compatibility_en = FALSE;
  687. }
  688. if (hw->mac_type >= e1000_82543) {
  689. status = E1000_READ_REG(hw, STATUS);
  690. if (status & E1000_STATUS_TBIMODE) {
  691. hw->media_type = e1000_media_type_fiber;
  692. /* tbi_compatibility not valid on fiber */
  693. hw->tbi_compatibility_en = FALSE;
  694. } else {
  695. hw->media_type = e1000_media_type_copper;
  696. }
  697. } else {
  698. /* This is an 82542 (fiber only) */
  699. hw->media_type = e1000_media_type_fiber;
  700. }
  701. /* Disabling VLAN filtering. */
  702. DEBUGOUT("Initializing the IEEE VLAN\n");
  703. E1000_WRITE_REG(hw, VET, 0);
  704. e1000_clear_vfta(hw);
  705. /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
  706. if (hw->mac_type == e1000_82542_rev2_0) {
  707. DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
  708. pci_write_config_word(hw->pdev, PCI_COMMAND,
  709. hw->
  710. pci_cmd_word & ~PCI_COMMAND_INVALIDATE);
  711. E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
  712. E1000_WRITE_FLUSH(hw);
  713. mdelay(5);
  714. }
  715. /* Setup the receive address. This involves initializing all of the Receive
  716. * Address Registers (RARs 0 - 15).
  717. */
  718. e1000_init_rx_addrs(nic);
  719. /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
  720. if (hw->mac_type == e1000_82542_rev2_0) {
  721. E1000_WRITE_REG(hw, RCTL, 0);
  722. E1000_WRITE_FLUSH(hw);
  723. mdelay(1);
  724. pci_write_config_word(hw->pdev, PCI_COMMAND, hw->pci_cmd_word);
  725. }
  726. /* Zero out the Multicast HASH table */
  727. DEBUGOUT("Zeroing the MTA\n");
  728. for (i = 0; i < E1000_MC_TBL_SIZE; i++)
  729. E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
  730. #if 0
  731. /* Set the PCI priority bit correctly in the CTRL register. This
  732. * determines if the adapter gives priority to receives, or if it
  733. * gives equal priority to transmits and receives.
  734. */
  735. if (hw->dma_fairness) {
  736. ctrl = E1000_READ_REG(hw, CTRL);
  737. E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PRIOR);
  738. }
  739. #endif
  740. if (hw->mac_type >= e1000_82543) {
  741. status = E1000_READ_REG(hw, STATUS);
  742. bus_type = (status & E1000_STATUS_PCIX_MODE) ?
  743. e1000_bus_type_pcix : e1000_bus_type_pci;
  744. }
  745. /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
  746. if (bus_type == e1000_bus_type_pcix) {
  747. pci_read_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
  748. &pcix_cmd_word);
  749. pci_read_config_word(hw->pdev, PCIX_STATUS_REGISTER_HI,
  750. &pcix_stat_hi_word);
  751. cmd_mmrbc =
  752. (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
  753. PCIX_COMMAND_MMRBC_SHIFT;
  754. stat_mmrbc =
  755. (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
  756. PCIX_STATUS_HI_MMRBC_SHIFT;
  757. if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
  758. stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
  759. if (cmd_mmrbc > stat_mmrbc) {
  760. pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
  761. pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
  762. pci_write_config_word(hw->pdev, PCIX_COMMAND_REGISTER,
  763. pcix_cmd_word);
  764. }
  765. }
  766. /* Call a subroutine to configure the link and setup flow control. */
  767. ret_val = e1000_setup_link(nic);
  768. /* Set the transmit descriptor write-back policy */
  769. if (hw->mac_type > e1000_82544) {
  770. ctrl = E1000_READ_REG(hw, TXDCTL);
  771. ctrl =
  772. (ctrl & ~E1000_TXDCTL_WTHRESH) |
  773. E1000_TXDCTL_FULL_TX_DESC_WB;
  774. E1000_WRITE_REG(hw, TXDCTL, ctrl);
  775. }
  776. #if 0
  777. /* Clear all of the statistics registers (clear on read). It is
  778. * important that we do this after we have tried to establish link
  779. * because the symbol error count will increment wildly if there
  780. * is no link.
  781. */
  782. e1000_clear_hw_cntrs(hw);
  783. #endif
  784. return ret_val;
  785. }
  786. /******************************************************************************
  787. * Configures flow control and link settings.
  788. *
  789. * hw - Struct containing variables accessed by shared code
  790. *
  791. * Determines which flow control settings to use. Calls the apropriate media-
  792. * specific link configuration function. Configures the flow control settings.
  793. * Assuming the adapter has a valid link partner, a valid link should be
  794. * established. Assumes the hardware has previously been reset and the
  795. * transmitter and receiver are not enabled.
  796. *****************************************************************************/
  797. static int
  798. e1000_setup_link(struct eth_device *nic)
  799. {
  800. struct e1000_hw *hw = nic->priv;
  801. uint32_t ctrl_ext;
  802. int32_t ret_val;
  803. uint16_t eeprom_data;
  804. DEBUGFUNC();
  805. #ifndef CONFIG_AP1000
  806. /* Read and store word 0x0F of the EEPROM. This word contains bits
  807. * that determine the hardware's default PAUSE (flow control) mode,
  808. * a bit that determines whether the HW defaults to enabling or
  809. * disabling auto-negotiation, and the direction of the
  810. * SW defined pins. If there is no SW over-ride of the flow
  811. * control setting, then the variable hw->fc will
  812. * be initialized based on a value in the EEPROM.
  813. */
  814. if (e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, &eeprom_data) < 0) {
  815. DEBUGOUT("EEPROM Read Error\n");
  816. return -E1000_ERR_EEPROM;
  817. }
  818. #else
  819. /* we have to hardcode the proper value for our hardware. */
  820. /* this value is for the 82540EM pci card used for prototyping, and it works. */
  821. eeprom_data = 0xb220;
  822. #endif
  823. if (hw->fc == e1000_fc_default) {
  824. if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
  825. hw->fc = e1000_fc_none;
  826. else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
  827. EEPROM_WORD0F_ASM_DIR)
  828. hw->fc = e1000_fc_tx_pause;
  829. else
  830. hw->fc = e1000_fc_full;
  831. }
  832. /* We want to save off the original Flow Control configuration just
  833. * in case we get disconnected and then reconnected into a different
  834. * hub or switch with different Flow Control capabilities.
  835. */
  836. if (hw->mac_type == e1000_82542_rev2_0)
  837. hw->fc &= (~e1000_fc_tx_pause);
  838. if ((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
  839. hw->fc &= (~e1000_fc_rx_pause);
  840. hw->original_fc = hw->fc;
  841. DEBUGOUT("After fix-ups FlowControl is now = %x\n", hw->fc);
  842. /* Take the 4 bits from EEPROM word 0x0F that determine the initial
  843. * polarity value for the SW controlled pins, and setup the
  844. * Extended Device Control reg with that info.
  845. * This is needed because one of the SW controlled pins is used for
  846. * signal detection. So this should be done before e1000_setup_pcs_link()
  847. * or e1000_phy_setup() is called.
  848. */
  849. if (hw->mac_type == e1000_82543) {
  850. ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
  851. SWDPIO__EXT_SHIFT);
  852. E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
  853. }
  854. /* Call the necessary subroutine to configure the link. */
  855. ret_val = (hw->media_type == e1000_media_type_fiber) ?
  856. e1000_setup_fiber_link(nic) : e1000_setup_copper_link(nic);
  857. if (ret_val < 0) {
  858. return ret_val;
  859. }
  860. /* Initialize the flow control address, type, and PAUSE timer
  861. * registers to their default values. This is done even if flow
  862. * control is disabled, because it does not hurt anything to
  863. * initialize these registers.
  864. */
  865. DEBUGOUT
  866. ("Initializing the Flow Control address, type and timer regs\n");
  867. E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
  868. E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
  869. E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
  870. E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
  871. /* Set the flow control receive threshold registers. Normally,
  872. * these registers will be set to a default threshold that may be
  873. * adjusted later by the driver's runtime code. However, if the
  874. * ability to transmit pause frames in not enabled, then these
  875. * registers will be set to 0.
  876. */
  877. if (!(hw->fc & e1000_fc_tx_pause)) {
  878. E1000_WRITE_REG(hw, FCRTL, 0);
  879. E1000_WRITE_REG(hw, FCRTH, 0);
  880. } else {
  881. /* We need to set up the Receive Threshold high and low water marks
  882. * as well as (optionally) enabling the transmission of XON frames.
  883. */
  884. if (hw->fc_send_xon) {
  885. E1000_WRITE_REG(hw, FCRTL,
  886. (hw->fc_low_water | E1000_FCRTL_XONE));
  887. E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
  888. } else {
  889. E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
  890. E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
  891. }
  892. }
  893. return ret_val;
  894. }
  895. /******************************************************************************
  896. * Sets up link for a fiber based adapter
  897. *
  898. * hw - Struct containing variables accessed by shared code
  899. *
  900. * Manipulates Physical Coding Sublayer functions in order to configure
  901. * link. Assumes the hardware has been previously reset and the transmitter
  902. * and receiver are not enabled.
  903. *****************************************************************************/
  904. static int
  905. e1000_setup_fiber_link(struct eth_device *nic)
  906. {
  907. struct e1000_hw *hw = nic->priv;
  908. uint32_t ctrl;
  909. uint32_t status;
  910. uint32_t txcw = 0;
  911. uint32_t i;
  912. uint32_t signal;
  913. int32_t ret_val;
  914. DEBUGFUNC();
  915. /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
  916. * set when the optics detect a signal. On older adapters, it will be
  917. * cleared when there is a signal
  918. */
  919. ctrl = E1000_READ_REG(hw, CTRL);
  920. if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
  921. signal = E1000_CTRL_SWDPIN1;
  922. else
  923. signal = 0;
  924. printf("signal for %s is %x (ctrl %08x)!!!!\n", nic->name, signal,
  925. ctrl);
  926. /* Take the link out of reset */
  927. ctrl &= ~(E1000_CTRL_LRST);
  928. e1000_config_collision_dist(hw);
  929. /* Check for a software override of the flow control settings, and setup
  930. * the device accordingly. If auto-negotiation is enabled, then software
  931. * will have to set the "PAUSE" bits to the correct value in the Tranmsit
  932. * Config Word Register (TXCW) and re-start auto-negotiation. However, if
  933. * auto-negotiation is disabled, then software will have to manually
  934. * configure the two flow control enable bits in the CTRL register.
  935. *
  936. * The possible values of the "fc" parameter are:
  937. * 0: Flow control is completely disabled
  938. * 1: Rx flow control is enabled (we can receive pause frames, but
  939. * not send pause frames).
  940. * 2: Tx flow control is enabled (we can send pause frames but we do
  941. * not support receiving pause frames).
  942. * 3: Both Rx and TX flow control (symmetric) are enabled.
  943. */
  944. switch (hw->fc) {
  945. case e1000_fc_none:
  946. /* Flow control is completely disabled by a software over-ride. */
  947. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
  948. break;
  949. case e1000_fc_rx_pause:
  950. /* RX Flow control is enabled and TX Flow control is disabled by a
  951. * software over-ride. Since there really isn't a way to advertise
  952. * that we are capable of RX Pause ONLY, we will advertise that we
  953. * support both symmetric and asymmetric RX PAUSE. Later, we will
  954. * disable the adapter's ability to send PAUSE frames.
  955. */
  956. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
  957. break;
  958. case e1000_fc_tx_pause:
  959. /* TX Flow control is enabled, and RX Flow control is disabled, by a
  960. * software over-ride.
  961. */
  962. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
  963. break;
  964. case e1000_fc_full:
  965. /* Flow control (both RX and TX) is enabled by a software over-ride. */
  966. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
  967. break;
  968. default:
  969. DEBUGOUT("Flow control param set incorrectly\n");
  970. return -E1000_ERR_CONFIG;
  971. break;
  972. }
  973. /* Since auto-negotiation is enabled, take the link out of reset (the link
  974. * will be in reset, because we previously reset the chip). This will
  975. * restart auto-negotiation. If auto-neogtiation is successful then the
  976. * link-up status bit will be set and the flow control enable bits (RFCE
  977. * and TFCE) will be set according to their negotiated value.
  978. */
  979. DEBUGOUT("Auto-negotiation enabled (%#x)\n", txcw);
  980. E1000_WRITE_REG(hw, TXCW, txcw);
  981. E1000_WRITE_REG(hw, CTRL, ctrl);
  982. E1000_WRITE_FLUSH(hw);
  983. hw->txcw = txcw;
  984. mdelay(1);
  985. /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
  986. * indication in the Device Status Register. Time-out if a link isn't
  987. * seen in 500 milliseconds seconds (Auto-negotiation should complete in
  988. * less than 500 milliseconds even if the other end is doing it in SW).
  989. */
  990. if ((E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
  991. DEBUGOUT("Looking for Link\n");
  992. for (i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
  993. mdelay(10);
  994. status = E1000_READ_REG(hw, STATUS);
  995. if (status & E1000_STATUS_LU)
  996. break;
  997. }
  998. if (i == (LINK_UP_TIMEOUT / 10)) {
  999. /* AutoNeg failed to achieve a link, so we'll call
  1000. * e1000_check_for_link. This routine will force the link up if we
  1001. * detect a signal. This will allow us to communicate with
  1002. * non-autonegotiating link partners.
  1003. */
  1004. DEBUGOUT("Never got a valid link from auto-neg!!!\n");
  1005. hw->autoneg_failed = 1;
  1006. ret_val = e1000_check_for_link(nic);
  1007. if (ret_val < 0) {
  1008. DEBUGOUT("Error while checking for link\n");
  1009. return ret_val;
  1010. }
  1011. hw->autoneg_failed = 0;
  1012. } else {
  1013. hw->autoneg_failed = 0;
  1014. DEBUGOUT("Valid Link Found\n");
  1015. }
  1016. } else {
  1017. DEBUGOUT("No Signal Detected\n");
  1018. return -E1000_ERR_NOLINK;
  1019. }
  1020. return 0;
  1021. }
  1022. /******************************************************************************
  1023. * Detects which PHY is present and the speed and duplex
  1024. *
  1025. * hw - Struct containing variables accessed by shared code
  1026. ******************************************************************************/
  1027. static int
  1028. e1000_setup_copper_link(struct eth_device *nic)
  1029. {
  1030. struct e1000_hw *hw = nic->priv;
  1031. uint32_t ctrl;
  1032. int32_t ret_val;
  1033. uint16_t i;
  1034. uint16_t phy_data;
  1035. DEBUGFUNC();
  1036. ctrl = E1000_READ_REG(hw, CTRL);
  1037. /* With 82543, we need to force speed and duplex on the MAC equal to what
  1038. * the PHY speed and duplex configuration is. In addition, we need to
  1039. * perform a hardware reset on the PHY to take it out of reset.
  1040. */
  1041. if (hw->mac_type > e1000_82543) {
  1042. ctrl |= E1000_CTRL_SLU;
  1043. ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
  1044. E1000_WRITE_REG(hw, CTRL, ctrl);
  1045. } else {
  1046. ctrl |=
  1047. (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
  1048. E1000_WRITE_REG(hw, CTRL, ctrl);
  1049. e1000_phy_hw_reset(hw);
  1050. }
  1051. /* Make sure we have a valid PHY */
  1052. ret_val = e1000_detect_gig_phy(hw);
  1053. if (ret_val < 0) {
  1054. DEBUGOUT("Error, did not detect valid phy.\n");
  1055. return ret_val;
  1056. }
  1057. DEBUGOUT("Phy ID = %x \n", hw->phy_id);
  1058. /* Enable CRS on TX. This must be set for half-duplex operation. */
  1059. if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data) < 0) {
  1060. DEBUGOUT("PHY Read Error\n");
  1061. return -E1000_ERR_PHY;
  1062. }
  1063. phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
  1064. #if 0
  1065. /* Options:
  1066. * MDI/MDI-X = 0 (default)
  1067. * 0 - Auto for all speeds
  1068. * 1 - MDI mode
  1069. * 2 - MDI-X mode
  1070. * 3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
  1071. */
  1072. phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
  1073. switch (hw->mdix) {
  1074. case 1:
  1075. phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
  1076. break;
  1077. case 2:
  1078. phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
  1079. break;
  1080. case 3:
  1081. phy_data |= M88E1000_PSCR_AUTO_X_1000T;
  1082. break;
  1083. case 0:
  1084. default:
  1085. phy_data |= M88E1000_PSCR_AUTO_X_MODE;
  1086. break;
  1087. }
  1088. #else
  1089. phy_data |= M88E1000_PSCR_AUTO_X_MODE;
  1090. #endif
  1091. #if 0
  1092. /* Options:
  1093. * disable_polarity_correction = 0 (default)
  1094. * Automatic Correction for Reversed Cable Polarity
  1095. * 0 - Disabled
  1096. * 1 - Enabled
  1097. */
  1098. phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
  1099. if (hw->disable_polarity_correction == 1)
  1100. phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
  1101. #else
  1102. phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
  1103. #endif
  1104. if (e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data) < 0) {
  1105. DEBUGOUT("PHY Write Error\n");
  1106. return -E1000_ERR_PHY;
  1107. }
  1108. /* Force TX_CLK in the Extended PHY Specific Control Register
  1109. * to 25MHz clock.
  1110. */
  1111. if (e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data) < 0) {
  1112. DEBUGOUT("PHY Read Error\n");
  1113. return -E1000_ERR_PHY;
  1114. }
  1115. phy_data |= M88E1000_EPSCR_TX_CLK_25;
  1116. /* Configure Master and Slave downshift values */
  1117. phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
  1118. M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
  1119. phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
  1120. M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
  1121. if (e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data) < 0) {
  1122. DEBUGOUT("PHY Write Error\n");
  1123. return -E1000_ERR_PHY;
  1124. }
  1125. /* SW Reset the PHY so all changes take effect */
  1126. ret_val = e1000_phy_reset(hw);
  1127. if (ret_val < 0) {
  1128. DEBUGOUT("Error Resetting the PHY\n");
  1129. return ret_val;
  1130. }
  1131. /* Options:
  1132. * autoneg = 1 (default)
  1133. * PHY will advertise value(s) parsed from
  1134. * autoneg_advertised and fc
  1135. * autoneg = 0
  1136. * PHY will be set to 10H, 10F, 100H, or 100F
  1137. * depending on value parsed from forced_speed_duplex.
  1138. */
  1139. /* Is autoneg enabled? This is enabled by default or by software override.
  1140. * If so, call e1000_phy_setup_autoneg routine to parse the
  1141. * autoneg_advertised and fc options. If autoneg is NOT enabled, then the
  1142. * user should have provided a speed/duplex override. If so, then call
  1143. * e1000_phy_force_speed_duplex to parse and set this up.
  1144. */
  1145. /* Perform some bounds checking on the hw->autoneg_advertised
  1146. * parameter. If this variable is zero, then set it to the default.
  1147. */
  1148. hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
  1149. /* If autoneg_advertised is zero, we assume it was not defaulted
  1150. * by the calling code so we set to advertise full capability.
  1151. */
  1152. if (hw->autoneg_advertised == 0)
  1153. hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
  1154. DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
  1155. ret_val = e1000_phy_setup_autoneg(hw);
  1156. if (ret_val < 0) {
  1157. DEBUGOUT("Error Setting up Auto-Negotiation\n");
  1158. return ret_val;
  1159. }
  1160. DEBUGOUT("Restarting Auto-Neg\n");
  1161. /* Restart auto-negotiation by setting the Auto Neg Enable bit and
  1162. * the Auto Neg Restart bit in the PHY control register.
  1163. */
  1164. if (e1000_read_phy_reg(hw, PHY_CTRL, &phy_data) < 0) {
  1165. DEBUGOUT("PHY Read Error\n");
  1166. return -E1000_ERR_PHY;
  1167. }
  1168. phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
  1169. if (e1000_write_phy_reg(hw, PHY_CTRL, phy_data) < 0) {
  1170. DEBUGOUT("PHY Write Error\n");
  1171. return -E1000_ERR_PHY;
  1172. }
  1173. #if 0
  1174. /* Does the user want to wait for Auto-Neg to complete here, or
  1175. * check at a later time (for example, callback routine).
  1176. */
  1177. if (hw->wait_autoneg_complete) {
  1178. ret_val = e1000_wait_autoneg(hw);
  1179. if (ret_val < 0) {
  1180. DEBUGOUT
  1181. ("Error while waiting for autoneg to complete\n");
  1182. return ret_val;
  1183. }
  1184. }
  1185. #else
  1186. /* If we do not wait for autonegtation to complete I
  1187. * do not see a valid link status.
  1188. */
  1189. ret_val = e1000_wait_autoneg(hw);
  1190. if (ret_val < 0) {
  1191. DEBUGOUT("Error while waiting for autoneg to complete\n");
  1192. return ret_val;
  1193. }
  1194. #endif
  1195. /* Check link status. Wait up to 100 microseconds for link to become
  1196. * valid.
  1197. */
  1198. for (i = 0; i < 10; i++) {
  1199. if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
  1200. DEBUGOUT("PHY Read Error\n");
  1201. return -E1000_ERR_PHY;
  1202. }
  1203. if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
  1204. DEBUGOUT("PHY Read Error\n");
  1205. return -E1000_ERR_PHY;
  1206. }
  1207. if (phy_data & MII_SR_LINK_STATUS) {
  1208. /* We have link, so we need to finish the config process:
  1209. * 1) Set up the MAC to the current PHY speed/duplex
  1210. * if we are on 82543. If we
  1211. * are on newer silicon, we only need to configure
  1212. * collision distance in the Transmit Control Register.
  1213. * 2) Set up flow control on the MAC to that established with
  1214. * the link partner.
  1215. */
  1216. if (hw->mac_type >= e1000_82544) {
  1217. e1000_config_collision_dist(hw);
  1218. } else {
  1219. ret_val = e1000_config_mac_to_phy(hw);
  1220. if (ret_val < 0) {
  1221. DEBUGOUT
  1222. ("Error configuring MAC to PHY settings\n");
  1223. return ret_val;
  1224. }
  1225. }
  1226. ret_val = e1000_config_fc_after_link_up(hw);
  1227. if (ret_val < 0) {
  1228. DEBUGOUT("Error Configuring Flow Control\n");
  1229. return ret_val;
  1230. }
  1231. DEBUGOUT("Valid link established!!!\n");
  1232. return 0;
  1233. }
  1234. udelay(10);
  1235. }
  1236. DEBUGOUT("Unable to establish link!!!\n");
  1237. return -E1000_ERR_NOLINK;
  1238. }
  1239. /******************************************************************************
  1240. * Configures PHY autoneg and flow control advertisement settings
  1241. *
  1242. * hw - Struct containing variables accessed by shared code
  1243. ******************************************************************************/
  1244. static int
  1245. e1000_phy_setup_autoneg(struct e1000_hw *hw)
  1246. {
  1247. uint16_t mii_autoneg_adv_reg;
  1248. uint16_t mii_1000t_ctrl_reg;
  1249. DEBUGFUNC();
  1250. /* Read the MII Auto-Neg Advertisement Register (Address 4). */
  1251. if (e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg) < 0) {
  1252. DEBUGOUT("PHY Read Error\n");
  1253. return -E1000_ERR_PHY;
  1254. }
  1255. /* Read the MII 1000Base-T Control Register (Address 9). */
  1256. if (e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg) < 0) {
  1257. DEBUGOUT("PHY Read Error\n");
  1258. return -E1000_ERR_PHY;
  1259. }
  1260. /* Need to parse both autoneg_advertised and fc and set up
  1261. * the appropriate PHY registers. First we will parse for
  1262. * autoneg_advertised software override. Since we can advertise
  1263. * a plethora of combinations, we need to check each bit
  1264. * individually.
  1265. */
  1266. /* First we clear all the 10/100 mb speed bits in the Auto-Neg
  1267. * Advertisement Register (Address 4) and the 1000 mb speed bits in
  1268. * the 1000Base-T Control Register (Address 9).
  1269. */
  1270. mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
  1271. mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
  1272. DEBUGOUT("autoneg_advertised %x\n", hw->autoneg_advertised);
  1273. /* Do we want to advertise 10 Mb Half Duplex? */
  1274. if (hw->autoneg_advertised & ADVERTISE_10_HALF) {
  1275. DEBUGOUT("Advertise 10mb Half duplex\n");
  1276. mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
  1277. }
  1278. /* Do we want to advertise 10 Mb Full Duplex? */
  1279. if (hw->autoneg_advertised & ADVERTISE_10_FULL) {
  1280. DEBUGOUT("Advertise 10mb Full duplex\n");
  1281. mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
  1282. }
  1283. /* Do we want to advertise 100 Mb Half Duplex? */
  1284. if (hw->autoneg_advertised & ADVERTISE_100_HALF) {
  1285. DEBUGOUT("Advertise 100mb Half duplex\n");
  1286. mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
  1287. }
  1288. /* Do we want to advertise 100 Mb Full Duplex? */
  1289. if (hw->autoneg_advertised & ADVERTISE_100_FULL) {
  1290. DEBUGOUT("Advertise 100mb Full duplex\n");
  1291. mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
  1292. }
  1293. /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
  1294. if (hw->autoneg_advertised & ADVERTISE_1000_HALF) {
  1295. DEBUGOUT
  1296. ("Advertise 1000mb Half duplex requested, request denied!\n");
  1297. }
  1298. /* Do we want to advertise 1000 Mb Full Duplex? */
  1299. if (hw->autoneg_advertised & ADVERTISE_1000_FULL) {
  1300. DEBUGOUT("Advertise 1000mb Full duplex\n");
  1301. mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
  1302. }
  1303. /* Check for a software override of the flow control settings, and
  1304. * setup the PHY advertisement registers accordingly. If
  1305. * auto-negotiation is enabled, then software will have to set the
  1306. * "PAUSE" bits to the correct value in the Auto-Negotiation
  1307. * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
  1308. *
  1309. * The possible values of the "fc" parameter are:
  1310. * 0: Flow control is completely disabled
  1311. * 1: Rx flow control is enabled (we can receive pause frames
  1312. * but not send pause frames).
  1313. * 2: Tx flow control is enabled (we can send pause frames
  1314. * but we do not support receiving pause frames).
  1315. * 3: Both Rx and TX flow control (symmetric) are enabled.
  1316. * other: No software override. The flow control configuration
  1317. * in the EEPROM is used.
  1318. */
  1319. switch (hw->fc) {
  1320. case e1000_fc_none: /* 0 */
  1321. /* Flow control (RX & TX) is completely disabled by a
  1322. * software over-ride.
  1323. */
  1324. mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  1325. break;
  1326. case e1000_fc_rx_pause: /* 1 */
  1327. /* RX Flow control is enabled, and TX Flow control is
  1328. * disabled, by a software over-ride.
  1329. */
  1330. /* Since there really isn't a way to advertise that we are
  1331. * capable of RX Pause ONLY, we will advertise that we
  1332. * support both symmetric and asymmetric RX PAUSE. Later
  1333. * (in e1000_config_fc_after_link_up) we will disable the
  1334. *hw's ability to send PAUSE frames.
  1335. */
  1336. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  1337. break;
  1338. case e1000_fc_tx_pause: /* 2 */
  1339. /* TX Flow control is enabled, and RX Flow control is
  1340. * disabled, by a software over-ride.
  1341. */
  1342. mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
  1343. mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
  1344. break;
  1345. case e1000_fc_full: /* 3 */
  1346. /* Flow control (both RX and TX) is enabled by a software
  1347. * over-ride.
  1348. */
  1349. mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
  1350. break;
  1351. default:
  1352. DEBUGOUT("Flow control param set incorrectly\n");
  1353. return -E1000_ERR_CONFIG;
  1354. }
  1355. if (e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg) < 0) {
  1356. DEBUGOUT("PHY Write Error\n");
  1357. return -E1000_ERR_PHY;
  1358. }
  1359. DEBUGOUT("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
  1360. if (e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg) < 0) {
  1361. DEBUGOUT("PHY Write Error\n");
  1362. return -E1000_ERR_PHY;
  1363. }
  1364. return 0;
  1365. }
  1366. /******************************************************************************
  1367. * Sets the collision distance in the Transmit Control register
  1368. *
  1369. * hw - Struct containing variables accessed by shared code
  1370. *
  1371. * Link should have been established previously. Reads the speed and duplex
  1372. * information from the Device Status register.
  1373. ******************************************************************************/
  1374. static void
  1375. e1000_config_collision_dist(struct e1000_hw *hw)
  1376. {
  1377. uint32_t tctl;
  1378. tctl = E1000_READ_REG(hw, TCTL);
  1379. tctl &= ~E1000_TCTL_COLD;
  1380. tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
  1381. E1000_WRITE_REG(hw, TCTL, tctl);
  1382. E1000_WRITE_FLUSH(hw);
  1383. }
  1384. /******************************************************************************
  1385. * Sets MAC speed and duplex settings to reflect the those in the PHY
  1386. *
  1387. * hw - Struct containing variables accessed by shared code
  1388. * mii_reg - data to write to the MII control register
  1389. *
  1390. * The contents of the PHY register containing the needed information need to
  1391. * be passed in.
  1392. ******************************************************************************/
  1393. static int
  1394. e1000_config_mac_to_phy(struct e1000_hw *hw)
  1395. {
  1396. uint32_t ctrl;
  1397. uint16_t phy_data;
  1398. DEBUGFUNC();
  1399. /* Read the Device Control Register and set the bits to Force Speed
  1400. * and Duplex.
  1401. */
  1402. ctrl = E1000_READ_REG(hw, CTRL);
  1403. ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
  1404. ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
  1405. /* Set up duplex in the Device Control and Transmit Control
  1406. * registers depending on negotiated values.
  1407. */
  1408. if (e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data) < 0) {
  1409. DEBUGOUT("PHY Read Error\n");
  1410. return -E1000_ERR_PHY;
  1411. }
  1412. if (phy_data & M88E1000_PSSR_DPLX)
  1413. ctrl |= E1000_CTRL_FD;
  1414. else
  1415. ctrl &= ~E1000_CTRL_FD;
  1416. e1000_config_collision_dist(hw);
  1417. /* Set up speed in the Device Control register depending on
  1418. * negotiated values.
  1419. */
  1420. if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
  1421. ctrl |= E1000_CTRL_SPD_1000;
  1422. else if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
  1423. ctrl |= E1000_CTRL_SPD_100;
  1424. /* Write the configured values back to the Device Control Reg. */
  1425. E1000_WRITE_REG(hw, CTRL, ctrl);
  1426. return 0;
  1427. }
  1428. /******************************************************************************
  1429. * Forces the MAC's flow control settings.
  1430. *
  1431. * hw - Struct containing variables accessed by shared code
  1432. *
  1433. * Sets the TFCE and RFCE bits in the device control register to reflect
  1434. * the adapter settings. TFCE and RFCE need to be explicitly set by
  1435. * software when a Copper PHY is used because autonegotiation is managed
  1436. * by the PHY rather than the MAC. Software must also configure these
  1437. * bits when link is forced on a fiber connection.
  1438. *****************************************************************************/
  1439. static int
  1440. e1000_force_mac_fc(struct e1000_hw *hw)
  1441. {
  1442. uint32_t ctrl;
  1443. DEBUGFUNC();
  1444. /* Get the current configuration of the Device Control Register */
  1445. ctrl = E1000_READ_REG(hw, CTRL);
  1446. /* Because we didn't get link via the internal auto-negotiation
  1447. * mechanism (we either forced link or we got link via PHY
  1448. * auto-neg), we have to manually enable/disable transmit an
  1449. * receive flow control.
  1450. *
  1451. * The "Case" statement below enables/disable flow control
  1452. * according to the "hw->fc" parameter.
  1453. *
  1454. * The possible values of the "fc" parameter are:
  1455. * 0: Flow control is completely disabled
  1456. * 1: Rx flow control is enabled (we can receive pause
  1457. * frames but not send pause frames).
  1458. * 2: Tx flow control is enabled (we can send pause frames
  1459. * frames but we do not receive pause frames).
  1460. * 3: Both Rx and TX flow control (symmetric) is enabled.
  1461. * other: No other values should be possible at this point.
  1462. */
  1463. switch (hw->fc) {
  1464. case e1000_fc_none:
  1465. ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
  1466. break;
  1467. case e1000_fc_rx_pause:
  1468. ctrl &= (~E1000_CTRL_TFCE);
  1469. ctrl |= E1000_CTRL_RFCE;
  1470. break;
  1471. case e1000_fc_tx_pause:
  1472. ctrl &= (~E1000_CTRL_RFCE);
  1473. ctrl |= E1000_CTRL_TFCE;
  1474. break;
  1475. case e1000_fc_full:
  1476. ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
  1477. break;
  1478. default:
  1479. DEBUGOUT("Flow control param set incorrectly\n");
  1480. return -E1000_ERR_CONFIG;
  1481. }
  1482. /* Disable TX Flow Control for 82542 (rev 2.0) */
  1483. if (hw->mac_type == e1000_82542_rev2_0)
  1484. ctrl &= (~E1000_CTRL_TFCE);
  1485. E1000_WRITE_REG(hw, CTRL, ctrl);
  1486. return 0;
  1487. }
  1488. /******************************************************************************
  1489. * Configures flow control settings after link is established
  1490. *
  1491. * hw - Struct containing variables accessed by shared code
  1492. *
  1493. * Should be called immediately after a valid link has been established.
  1494. * Forces MAC flow control settings if link was forced. When in MII/GMII mode
  1495. * and autonegotiation is enabled, the MAC flow control settings will be set
  1496. * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
  1497. * and RFCE bits will be automaticaly set to the negotiated flow control mode.
  1498. *****************************************************************************/
  1499. static int
  1500. e1000_config_fc_after_link_up(struct e1000_hw *hw)
  1501. {
  1502. int32_t ret_val;
  1503. uint16_t mii_status_reg;
  1504. uint16_t mii_nway_adv_reg;
  1505. uint16_t mii_nway_lp_ability_reg;
  1506. uint16_t speed;
  1507. uint16_t duplex;
  1508. DEBUGFUNC();
  1509. /* Check for the case where we have fiber media and auto-neg failed
  1510. * so we had to force link. In this case, we need to force the
  1511. * configuration of the MAC to match the "fc" parameter.
  1512. */
  1513. if ((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) {
  1514. ret_val = e1000_force_mac_fc(hw);
  1515. if (ret_val < 0) {
  1516. DEBUGOUT("Error forcing flow control settings\n");
  1517. return ret_val;
  1518. }
  1519. }
  1520. /* Check for the case where we have copper media and auto-neg is
  1521. * enabled. In this case, we need to check and see if Auto-Neg
  1522. * has completed, and if so, how the PHY and link partner has
  1523. * flow control configured.
  1524. */
  1525. if (hw->media_type == e1000_media_type_copper) {
  1526. /* Read the MII Status Register and check to see if AutoNeg
  1527. * has completed. We read this twice because this reg has
  1528. * some "sticky" (latched) bits.
  1529. */
  1530. if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
  1531. DEBUGOUT("PHY Read Error \n");
  1532. return -E1000_ERR_PHY;
  1533. }
  1534. if (e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg) < 0) {
  1535. DEBUGOUT("PHY Read Error \n");
  1536. return -E1000_ERR_PHY;
  1537. }
  1538. if (mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
  1539. /* The AutoNeg process has completed, so we now need to
  1540. * read both the Auto Negotiation Advertisement Register
  1541. * (Address 4) and the Auto_Negotiation Base Page Ability
  1542. * Register (Address 5) to determine how flow control was
  1543. * negotiated.
  1544. */
  1545. if (e1000_read_phy_reg
  1546. (hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg) < 0) {
  1547. DEBUGOUT("PHY Read Error\n");
  1548. return -E1000_ERR_PHY;
  1549. }
  1550. if (e1000_read_phy_reg
  1551. (hw, PHY_LP_ABILITY,
  1552. &mii_nway_lp_ability_reg) < 0) {
  1553. DEBUGOUT("PHY Read Error\n");
  1554. return -E1000_ERR_PHY;
  1555. }
  1556. /* Two bits in the Auto Negotiation Advertisement Register
  1557. * (Address 4) and two bits in the Auto Negotiation Base
  1558. * Page Ability Register (Address 5) determine flow control
  1559. * for both the PHY and the link partner. The following
  1560. * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
  1561. * 1999, describes these PAUSE resolution bits and how flow
  1562. * control is determined based upon these settings.
  1563. * NOTE: DC = Don't Care
  1564. *
  1565. * LOCAL DEVICE | LINK PARTNER
  1566. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
  1567. *-------|---------|-------|---------|--------------------
  1568. * 0 | 0 | DC | DC | e1000_fc_none
  1569. * 0 | 1 | 0 | DC | e1000_fc_none
  1570. * 0 | 1 | 1 | 0 | e1000_fc_none
  1571. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  1572. * 1 | 0 | 0 | DC | e1000_fc_none
  1573. * 1 | DC | 1 | DC | e1000_fc_full
  1574. * 1 | 1 | 0 | 0 | e1000_fc_none
  1575. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  1576. *
  1577. */
  1578. /* Are both PAUSE bits set to 1? If so, this implies
  1579. * Symmetric Flow Control is enabled at both ends. The
  1580. * ASM_DIR bits are irrelevant per the spec.
  1581. *
  1582. * For Symmetric Flow Control:
  1583. *
  1584. * LOCAL DEVICE | LINK PARTNER
  1585. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  1586. *-------|---------|-------|---------|--------------------
  1587. * 1 | DC | 1 | DC | e1000_fc_full
  1588. *
  1589. */
  1590. if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  1591. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
  1592. /* Now we need to check if the user selected RX ONLY
  1593. * of pause frames. In this case, we had to advertise
  1594. * FULL flow control because we could not advertise RX
  1595. * ONLY. Hence, we must now check to see if we need to
  1596. * turn OFF the TRANSMISSION of PAUSE frames.
  1597. */
  1598. if (hw->original_fc == e1000_fc_full) {
  1599. hw->fc = e1000_fc_full;
  1600. DEBUGOUT("Flow Control = FULL.\r\n");
  1601. } else {
  1602. hw->fc = e1000_fc_rx_pause;
  1603. DEBUGOUT
  1604. ("Flow Control = RX PAUSE frames only.\r\n");
  1605. }
  1606. }
  1607. /* For receiving PAUSE frames ONLY.
  1608. *
  1609. * LOCAL DEVICE | LINK PARTNER
  1610. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  1611. *-------|---------|-------|---------|--------------------
  1612. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  1613. *
  1614. */
  1615. else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  1616. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  1617. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  1618. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
  1619. {
  1620. hw->fc = e1000_fc_tx_pause;
  1621. DEBUGOUT
  1622. ("Flow Control = TX PAUSE frames only.\r\n");
  1623. }
  1624. /* For transmitting PAUSE frames ONLY.
  1625. *
  1626. * LOCAL DEVICE | LINK PARTNER
  1627. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  1628. *-------|---------|-------|---------|--------------------
  1629. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  1630. *
  1631. */
  1632. else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  1633. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  1634. !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  1635. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR))
  1636. {
  1637. hw->fc = e1000_fc_rx_pause;
  1638. DEBUGOUT
  1639. ("Flow Control = RX PAUSE frames only.\r\n");
  1640. }
  1641. /* Per the IEEE spec, at this point flow control should be
  1642. * disabled. However, we want to consider that we could
  1643. * be connected to a legacy switch that doesn't advertise
  1644. * desired flow control, but can be forced on the link
  1645. * partner. So if we advertised no flow control, that is
  1646. * what we will resolve to. If we advertised some kind of
  1647. * receive capability (Rx Pause Only or Full Flow Control)
  1648. * and the link partner advertised none, we will configure
  1649. * ourselves to enable Rx Flow Control only. We can do
  1650. * this safely for two reasons: If the link partner really
  1651. * didn't want flow control enabled, and we enable Rx, no
  1652. * harm done since we won't be receiving any PAUSE frames
  1653. * anyway. If the intent on the link partner was to have
  1654. * flow control enabled, then by us enabling RX only, we
  1655. * can at least receive pause frames and process them.
  1656. * This is a good idea because in most cases, since we are
  1657. * predominantly a server NIC, more times than not we will
  1658. * be asked to delay transmission of packets than asking
  1659. * our link partner to pause transmission of frames.
  1660. */
  1661. else if (hw->original_fc == e1000_fc_none ||
  1662. hw->original_fc == e1000_fc_tx_pause) {
  1663. hw->fc = e1000_fc_none;
  1664. DEBUGOUT("Flow Control = NONE.\r\n");
  1665. } else {
  1666. hw->fc = e1000_fc_rx_pause;
  1667. DEBUGOUT
  1668. ("Flow Control = RX PAUSE frames only.\r\n");
  1669. }
  1670. /* Now we need to do one last check... If we auto-
  1671. * negotiated to HALF DUPLEX, flow control should not be
  1672. * enabled per IEEE 802.3 spec.
  1673. */
  1674. e1000_get_speed_and_duplex(hw, &speed, &duplex);
  1675. if (duplex == HALF_DUPLEX)
  1676. hw->fc = e1000_fc_none;
  1677. /* Now we call a subroutine to actually force the MAC
  1678. * controller to use the correct flow control settings.
  1679. */
  1680. ret_val = e1000_force_mac_fc(hw);
  1681. if (ret_val < 0) {
  1682. DEBUGOUT
  1683. ("Error forcing flow control settings\n");
  1684. return ret_val;
  1685. }
  1686. } else {
  1687. DEBUGOUT
  1688. ("Copper PHY and Auto Neg has not completed.\r\n");
  1689. }
  1690. }
  1691. return 0;
  1692. }
  1693. /******************************************************************************
  1694. * Checks to see if the link status of the hardware has changed.
  1695. *
  1696. * hw - Struct containing variables accessed by shared code
  1697. *
  1698. * Called by any function that needs to check the link status of the adapter.
  1699. *****************************************************************************/
  1700. static int
  1701. e1000_check_for_link(struct eth_device *nic)
  1702. {
  1703. struct e1000_hw *hw = nic->priv;
  1704. uint32_t rxcw;
  1705. uint32_t ctrl;
  1706. uint32_t status;
  1707. uint32_t rctl;
  1708. uint32_t signal;
  1709. int32_t ret_val;
  1710. uint16_t phy_data;
  1711. uint16_t lp_capability;
  1712. DEBUGFUNC();
  1713. /* On adapters with a MAC newer that 82544, SW Defineable pin 1 will be
  1714. * set when the optics detect a signal. On older adapters, it will be
  1715. * cleared when there is a signal
  1716. */
  1717. ctrl = E1000_READ_REG(hw, CTRL);
  1718. if ((hw->mac_type > e1000_82544) && !(ctrl & E1000_CTRL_ILOS))
  1719. signal = E1000_CTRL_SWDPIN1;
  1720. else
  1721. signal = 0;
  1722. status = E1000_READ_REG(hw, STATUS);
  1723. rxcw = E1000_READ_REG(hw, RXCW);
  1724. DEBUGOUT("ctrl: %#08x status %#08x rxcw %#08x\n", ctrl, status, rxcw);
  1725. /* If we have a copper PHY then we only want to go out to the PHY
  1726. * registers to see if Auto-Neg has completed and/or if our link
  1727. * status has changed. The get_link_status flag will be set if we
  1728. * receive a Link Status Change interrupt or we have Rx Sequence
  1729. * Errors.
  1730. */
  1731. if ((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
  1732. /* First we want to see if the MII Status Register reports
  1733. * link. If so, then we want to get the current speed/duplex
  1734. * of the PHY.
  1735. * Read the register twice since the link bit is sticky.
  1736. */
  1737. if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
  1738. DEBUGOUT("PHY Read Error\n");
  1739. return -E1000_ERR_PHY;
  1740. }
  1741. if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
  1742. DEBUGOUT("PHY Read Error\n");
  1743. return -E1000_ERR_PHY;
  1744. }
  1745. if (phy_data & MII_SR_LINK_STATUS) {
  1746. hw->get_link_status = FALSE;
  1747. } else {
  1748. /* No link detected */
  1749. return -E1000_ERR_NOLINK;
  1750. }
  1751. /* We have a M88E1000 PHY and Auto-Neg is enabled. If we
  1752. * have Si on board that is 82544 or newer, Auto
  1753. * Speed Detection takes care of MAC speed/duplex
  1754. * configuration. So we only need to configure Collision
  1755. * Distance in the MAC. Otherwise, we need to force
  1756. * speed/duplex on the MAC to the current PHY speed/duplex
  1757. * settings.
  1758. */
  1759. if (hw->mac_type >= e1000_82544)
  1760. e1000_config_collision_dist(hw);
  1761. else {
  1762. ret_val = e1000_config_mac_to_phy(hw);
  1763. if (ret_val < 0) {
  1764. DEBUGOUT
  1765. ("Error configuring MAC to PHY settings\n");
  1766. return ret_val;
  1767. }
  1768. }
  1769. /* Configure Flow Control now that Auto-Neg has completed. First, we
  1770. * need to restore the desired flow control settings because we may
  1771. * have had to re-autoneg with a different link partner.
  1772. */
  1773. ret_val = e1000_config_fc_after_link_up(hw);
  1774. if (ret_val < 0) {
  1775. DEBUGOUT("Error configuring flow control\n");
  1776. return ret_val;
  1777. }
  1778. /* At this point we know that we are on copper and we have
  1779. * auto-negotiated link. These are conditions for checking the link
  1780. * parter capability register. We use the link partner capability to
  1781. * determine if TBI Compatibility needs to be turned on or off. If
  1782. * the link partner advertises any speed in addition to Gigabit, then
  1783. * we assume that they are GMII-based, and TBI compatibility is not
  1784. * needed. If no other speeds are advertised, we assume the link
  1785. * partner is TBI-based, and we turn on TBI Compatibility.
  1786. */
  1787. if (hw->tbi_compatibility_en) {
  1788. if (e1000_read_phy_reg
  1789. (hw, PHY_LP_ABILITY, &lp_capability) < 0) {
  1790. DEBUGOUT("PHY Read Error\n");
  1791. return -E1000_ERR_PHY;
  1792. }
  1793. if (lp_capability & (NWAY_LPAR_10T_HD_CAPS |
  1794. NWAY_LPAR_10T_FD_CAPS |
  1795. NWAY_LPAR_100TX_HD_CAPS |
  1796. NWAY_LPAR_100TX_FD_CAPS |
  1797. NWAY_LPAR_100T4_CAPS)) {
  1798. /* If our link partner advertises anything in addition to
  1799. * gigabit, we do not need to enable TBI compatibility.
  1800. */
  1801. if (hw->tbi_compatibility_on) {
  1802. /* If we previously were in the mode, turn it off. */
  1803. rctl = E1000_READ_REG(hw, RCTL);
  1804. rctl &= ~E1000_RCTL_SBP;
  1805. E1000_WRITE_REG(hw, RCTL, rctl);
  1806. hw->tbi_compatibility_on = FALSE;
  1807. }
  1808. } else {
  1809. /* If TBI compatibility is was previously off, turn it on. For
  1810. * compatibility with a TBI link partner, we will store bad
  1811. * packets. Some frames have an additional byte on the end and
  1812. * will look like CRC errors to to the hardware.
  1813. */
  1814. if (!hw->tbi_compatibility_on) {
  1815. hw->tbi_compatibility_on = TRUE;
  1816. rctl = E1000_READ_REG(hw, RCTL);
  1817. rctl |= E1000_RCTL_SBP;
  1818. E1000_WRITE_REG(hw, RCTL, rctl);
  1819. }
  1820. }
  1821. }
  1822. }
  1823. /* If we don't have link (auto-negotiation failed or link partner cannot
  1824. * auto-negotiate), the cable is plugged in (we have signal), and our
  1825. * link partner is not trying to auto-negotiate with us (we are receiving
  1826. * idles or data), we need to force link up. We also need to give
  1827. * auto-negotiation time to complete, in case the cable was just plugged
  1828. * in. The autoneg_failed flag does this.
  1829. */
  1830. else if ((hw->media_type == e1000_media_type_fiber) &&
  1831. (!(status & E1000_STATUS_LU)) &&
  1832. ((ctrl & E1000_CTRL_SWDPIN1) == signal) &&
  1833. (!(rxcw & E1000_RXCW_C))) {
  1834. if (hw->autoneg_failed == 0) {
  1835. hw->autoneg_failed = 1;
  1836. return 0;
  1837. }
  1838. DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
  1839. /* Disable auto-negotiation in the TXCW register */
  1840. E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
  1841. /* Force link-up and also force full-duplex. */
  1842. ctrl = E1000_READ_REG(hw, CTRL);
  1843. ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
  1844. E1000_WRITE_REG(hw, CTRL, ctrl);
  1845. /* Configure Flow Control after forcing link up. */
  1846. ret_val = e1000_config_fc_after_link_up(hw);
  1847. if (ret_val < 0) {
  1848. DEBUGOUT("Error configuring flow control\n");
  1849. return ret_val;
  1850. }
  1851. }
  1852. /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
  1853. * auto-negotiation in the TXCW register and disable forced link in the
  1854. * Device Control register in an attempt to auto-negotiate with our link
  1855. * partner.
  1856. */
  1857. else if ((hw->media_type == e1000_media_type_fiber) &&
  1858. (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
  1859. DEBUGOUT
  1860. ("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
  1861. E1000_WRITE_REG(hw, TXCW, hw->txcw);
  1862. E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
  1863. }
  1864. return 0;
  1865. }
  1866. /******************************************************************************
  1867. * Detects the current speed and duplex settings of the hardware.
  1868. *
  1869. * hw - Struct containing variables accessed by shared code
  1870. * speed - Speed of the connection
  1871. * duplex - Duplex setting of the connection
  1872. *****************************************************************************/
  1873. static void
  1874. e1000_get_speed_and_duplex(struct e1000_hw *hw,
  1875. uint16_t * speed, uint16_t * duplex)
  1876. {
  1877. uint32_t status;
  1878. DEBUGFUNC();
  1879. if (hw->mac_type >= e1000_82543) {
  1880. status = E1000_READ_REG(hw, STATUS);
  1881. if (status & E1000_STATUS_SPEED_1000) {
  1882. *speed = SPEED_1000;
  1883. DEBUGOUT("1000 Mbs, ");
  1884. } else if (status & E1000_STATUS_SPEED_100) {
  1885. *speed = SPEED_100;
  1886. DEBUGOUT("100 Mbs, ");
  1887. } else {
  1888. *speed = SPEED_10;
  1889. DEBUGOUT("10 Mbs, ");
  1890. }
  1891. if (status & E1000_STATUS_FD) {
  1892. *duplex = FULL_DUPLEX;
  1893. DEBUGOUT("Full Duplex\r\n");
  1894. } else {
  1895. *duplex = HALF_DUPLEX;
  1896. DEBUGOUT(" Half Duplex\r\n");
  1897. }
  1898. } else {
  1899. DEBUGOUT("1000 Mbs, Full Duplex\r\n");
  1900. *speed = SPEED_1000;
  1901. *duplex = FULL_DUPLEX;
  1902. }
  1903. }
  1904. /******************************************************************************
  1905. * Blocks until autoneg completes or times out (~4.5 seconds)
  1906. *
  1907. * hw - Struct containing variables accessed by shared code
  1908. ******************************************************************************/
  1909. static int
  1910. e1000_wait_autoneg(struct e1000_hw *hw)
  1911. {
  1912. uint16_t i;
  1913. uint16_t phy_data;
  1914. DEBUGFUNC();
  1915. DEBUGOUT("Waiting for Auto-Neg to complete.\n");
  1916. /* We will wait for autoneg to complete or 4.5 seconds to expire. */
  1917. for (i = PHY_AUTO_NEG_TIME; i > 0; i--) {
  1918. /* Read the MII Status Register and wait for Auto-Neg
  1919. * Complete bit to be set.
  1920. */
  1921. if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
  1922. DEBUGOUT("PHY Read Error\n");
  1923. return -E1000_ERR_PHY;
  1924. }
  1925. if (e1000_read_phy_reg(hw, PHY_STATUS, &phy_data) < 0) {
  1926. DEBUGOUT("PHY Read Error\n");
  1927. return -E1000_ERR_PHY;
  1928. }
  1929. if (phy_data & MII_SR_AUTONEG_COMPLETE) {
  1930. DEBUGOUT("Auto-Neg complete.\n");
  1931. return 0;
  1932. }
  1933. mdelay(100);
  1934. }
  1935. DEBUGOUT("Auto-Neg timedout.\n");
  1936. return -E1000_ERR_TIMEOUT;
  1937. }
  1938. /******************************************************************************
  1939. * Raises the Management Data Clock
  1940. *
  1941. * hw - Struct containing variables accessed by shared code
  1942. * ctrl - Device control register's current value
  1943. ******************************************************************************/
  1944. static void
  1945. e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
  1946. {
  1947. /* Raise the clock input to the Management Data Clock (by setting the MDC
  1948. * bit), and then delay 2 microseconds.
  1949. */
  1950. E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
  1951. E1000_WRITE_FLUSH(hw);
  1952. udelay(2);
  1953. }
  1954. /******************************************************************************
  1955. * Lowers the Management Data Clock
  1956. *
  1957. * hw - Struct containing variables accessed by shared code
  1958. * ctrl - Device control register's current value
  1959. ******************************************************************************/
  1960. static void
  1961. e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t * ctrl)
  1962. {
  1963. /* Lower the clock input to the Management Data Clock (by clearing the MDC
  1964. * bit), and then delay 2 microseconds.
  1965. */
  1966. E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
  1967. E1000_WRITE_FLUSH(hw);
  1968. udelay(2);
  1969. }
  1970. /******************************************************************************
  1971. * Shifts data bits out to the PHY
  1972. *
  1973. * hw - Struct containing variables accessed by shared code
  1974. * data - Data to send out to the PHY
  1975. * count - Number of bits to shift out
  1976. *
  1977. * Bits are shifted out in MSB to LSB order.
  1978. ******************************************************************************/
  1979. static void
  1980. e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data, uint16_t count)
  1981. {
  1982. uint32_t ctrl;
  1983. uint32_t mask;
  1984. /* We need to shift "count" number of bits out to the PHY. So, the value
  1985. * in the "data" parameter will be shifted out to the PHY one bit at a
  1986. * time. In order to do this, "data" must be broken down into bits.
  1987. */
  1988. mask = 0x01;
  1989. mask <<= (count - 1);
  1990. ctrl = E1000_READ_REG(hw, CTRL);
  1991. /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
  1992. ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
  1993. while (mask) {
  1994. /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
  1995. * then raising and lowering the Management Data Clock. A "0" is
  1996. * shifted out to the PHY by setting the MDIO bit to "0" and then
  1997. * raising and lowering the clock.
  1998. */
  1999. if (data & mask)
  2000. ctrl |= E1000_CTRL_MDIO;
  2001. else
  2002. ctrl &= ~E1000_CTRL_MDIO;
  2003. E1000_WRITE_REG(hw, CTRL, ctrl);
  2004. E1000_WRITE_FLUSH(hw);
  2005. udelay(2);
  2006. e1000_raise_mdi_clk(hw, &ctrl);
  2007. e1000_lower_mdi_clk(hw, &ctrl);
  2008. mask = mask >> 1;
  2009. }
  2010. }
  2011. /******************************************************************************
  2012. * Shifts data bits in from the PHY
  2013. *
  2014. * hw - Struct containing variables accessed by shared code
  2015. *
  2016. * Bits are shifted in in MSB to LSB order.
  2017. ******************************************************************************/
  2018. static uint16_t
  2019. e1000_shift_in_mdi_bits(struct e1000_hw *hw)
  2020. {
  2021. uint32_t ctrl;
  2022. uint16_t data = 0;
  2023. uint8_t i;
  2024. /* In order to read a register from the PHY, we need to shift in a total
  2025. * of 18 bits from the PHY. The first two bit (turnaround) times are used
  2026. * to avoid contention on the MDIO pin when a read operation is performed.
  2027. * These two bits are ignored by us and thrown away. Bits are "shifted in"
  2028. * by raising the input to the Management Data Clock (setting the MDC bit),
  2029. * and then reading the value of the MDIO bit.
  2030. */
  2031. ctrl = E1000_READ_REG(hw, CTRL);
  2032. /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
  2033. ctrl &= ~E1000_CTRL_MDIO_DIR;
  2034. ctrl &= ~E1000_CTRL_MDIO;
  2035. E1000_WRITE_REG(hw, CTRL, ctrl);
  2036. E1000_WRITE_FLUSH(hw);
  2037. /* Raise and Lower the clock before reading in the data. This accounts for
  2038. * the turnaround bits. The first clock occurred when we clocked out the
  2039. * last bit of the Register Address.
  2040. */
  2041. e1000_raise_mdi_clk(hw, &ctrl);
  2042. e1000_lower_mdi_clk(hw, &ctrl);
  2043. for (data = 0, i = 0; i < 16; i++) {
  2044. data = data << 1;
  2045. e1000_raise_mdi_clk(hw, &ctrl);
  2046. ctrl = E1000_READ_REG(hw, CTRL);
  2047. /* Check to see if we shifted in a "1". */
  2048. if (ctrl & E1000_CTRL_MDIO)
  2049. data |= 1;
  2050. e1000_lower_mdi_clk(hw, &ctrl);
  2051. }
  2052. e1000_raise_mdi_clk(hw, &ctrl);
  2053. e1000_lower_mdi_clk(hw, &ctrl);
  2054. return data;
  2055. }
  2056. /*****************************************************************************
  2057. * Reads the value from a PHY register
  2058. *
  2059. * hw - Struct containing variables accessed by shared code
  2060. * reg_addr - address of the PHY register to read
  2061. ******************************************************************************/
  2062. static int
  2063. e1000_read_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t * phy_data)
  2064. {
  2065. uint32_t i;
  2066. uint32_t mdic = 0;
  2067. const uint32_t phy_addr = 1;
  2068. if (reg_addr > MAX_PHY_REG_ADDRESS) {
  2069. DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
  2070. return -E1000_ERR_PARAM;
  2071. }
  2072. if (hw->mac_type > e1000_82543) {
  2073. /* Set up Op-code, Phy Address, and register address in the MDI
  2074. * Control register. The MAC will take care of interfacing with the
  2075. * PHY to retrieve the desired data.
  2076. */
  2077. mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
  2078. (phy_addr << E1000_MDIC_PHY_SHIFT) |
  2079. (E1000_MDIC_OP_READ));
  2080. E1000_WRITE_REG(hw, MDIC, mdic);
  2081. /* Poll the ready bit to see if the MDI read completed */
  2082. for (i = 0; i < 64; i++) {
  2083. udelay(10);
  2084. mdic = E1000_READ_REG(hw, MDIC);
  2085. if (mdic & E1000_MDIC_READY)
  2086. break;
  2087. }
  2088. if (!(mdic & E1000_MDIC_READY)) {
  2089. DEBUGOUT("MDI Read did not complete\n");
  2090. return -E1000_ERR_PHY;
  2091. }
  2092. if (mdic & E1000_MDIC_ERROR) {
  2093. DEBUGOUT("MDI Error\n");
  2094. return -E1000_ERR_PHY;
  2095. }
  2096. *phy_data = (uint16_t) mdic;
  2097. } else {
  2098. /* We must first send a preamble through the MDIO pin to signal the
  2099. * beginning of an MII instruction. This is done by sending 32
  2100. * consecutive "1" bits.
  2101. */
  2102. e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
  2103. /* Now combine the next few fields that are required for a read
  2104. * operation. We use this method instead of calling the
  2105. * e1000_shift_out_mdi_bits routine five different times. The format of
  2106. * a MII read instruction consists of a shift out of 14 bits and is
  2107. * defined as follows:
  2108. * <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
  2109. * followed by a shift in of 18 bits. This first two bits shifted in
  2110. * are TurnAround bits used to avoid contention on the MDIO pin when a
  2111. * READ operation is performed. These two bits are thrown away
  2112. * followed by a shift in of 16 bits which contains the desired data.
  2113. */
  2114. mdic = ((reg_addr) | (phy_addr << 5) |
  2115. (PHY_OP_READ << 10) | (PHY_SOF << 12));
  2116. e1000_shift_out_mdi_bits(hw, mdic, 14);
  2117. /* Now that we've shifted out the read command to the MII, we need to
  2118. * "shift in" the 16-bit value (18 total bits) of the requested PHY
  2119. * register address.
  2120. */
  2121. *phy_data = e1000_shift_in_mdi_bits(hw);
  2122. }
  2123. return 0;
  2124. }
  2125. /******************************************************************************
  2126. * Writes a value to a PHY register
  2127. *
  2128. * hw - Struct containing variables accessed by shared code
  2129. * reg_addr - address of the PHY register to write
  2130. * data - data to write to the PHY
  2131. ******************************************************************************/
  2132. static int
  2133. e1000_write_phy_reg(struct e1000_hw *hw, uint32_t reg_addr, uint16_t phy_data)
  2134. {
  2135. uint32_t i;
  2136. uint32_t mdic = 0;
  2137. const uint32_t phy_addr = 1;
  2138. if (reg_addr > MAX_PHY_REG_ADDRESS) {
  2139. DEBUGOUT("PHY Address %d is out of range\n", reg_addr);
  2140. return -E1000_ERR_PARAM;
  2141. }
  2142. if (hw->mac_type > e1000_82543) {
  2143. /* Set up Op-code, Phy Address, register address, and data intended
  2144. * for the PHY register in the MDI Control register. The MAC will take
  2145. * care of interfacing with the PHY to send the desired data.
  2146. */
  2147. mdic = (((uint32_t) phy_data) |
  2148. (reg_addr << E1000_MDIC_REG_SHIFT) |
  2149. (phy_addr << E1000_MDIC_PHY_SHIFT) |
  2150. (E1000_MDIC_OP_WRITE));
  2151. E1000_WRITE_REG(hw, MDIC, mdic);
  2152. /* Poll the ready bit to see if the MDI read completed */
  2153. for (i = 0; i < 64; i++) {
  2154. udelay(10);
  2155. mdic = E1000_READ_REG(hw, MDIC);
  2156. if (mdic & E1000_MDIC_READY)
  2157. break;
  2158. }
  2159. if (!(mdic & E1000_MDIC_READY)) {
  2160. DEBUGOUT("MDI Write did not complete\n");
  2161. return -E1000_ERR_PHY;
  2162. }
  2163. } else {
  2164. /* We'll need to use the SW defined pins to shift the write command
  2165. * out to the PHY. We first send a preamble to the PHY to signal the
  2166. * beginning of the MII instruction. This is done by sending 32
  2167. * consecutive "1" bits.
  2168. */
  2169. e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
  2170. /* Now combine the remaining required fields that will indicate a
  2171. * write operation. We use this method instead of calling the
  2172. * e1000_shift_out_mdi_bits routine for each field in the command. The
  2173. * format of a MII write instruction is as follows:
  2174. * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
  2175. */
  2176. mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
  2177. (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
  2178. mdic <<= 16;
  2179. mdic |= (uint32_t) phy_data;
  2180. e1000_shift_out_mdi_bits(hw, mdic, 32);
  2181. }
  2182. return 0;
  2183. }
  2184. /******************************************************************************
  2185. * Returns the PHY to the power-on reset state
  2186. *
  2187. * hw - Struct containing variables accessed by shared code
  2188. ******************************************************************************/
  2189. static void
  2190. e1000_phy_hw_reset(struct e1000_hw *hw)
  2191. {
  2192. uint32_t ctrl;
  2193. uint32_t ctrl_ext;
  2194. DEBUGFUNC();
  2195. DEBUGOUT("Resetting Phy...\n");
  2196. if (hw->mac_type > e1000_82543) {
  2197. /* Read the device control register and assert the E1000_CTRL_PHY_RST
  2198. * bit. Then, take it out of reset.
  2199. */
  2200. ctrl = E1000_READ_REG(hw, CTRL);
  2201. E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
  2202. E1000_WRITE_FLUSH(hw);
  2203. mdelay(10);
  2204. E1000_WRITE_REG(hw, CTRL, ctrl);
  2205. E1000_WRITE_FLUSH(hw);
  2206. } else {
  2207. /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
  2208. * bit to put the PHY into reset. Then, take it out of reset.
  2209. */
  2210. ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
  2211. ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
  2212. ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
  2213. E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
  2214. E1000_WRITE_FLUSH(hw);
  2215. mdelay(10);
  2216. ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
  2217. E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
  2218. E1000_WRITE_FLUSH(hw);
  2219. }
  2220. udelay(150);
  2221. }
  2222. /******************************************************************************
  2223. * Resets the PHY
  2224. *
  2225. * hw - Struct containing variables accessed by shared code
  2226. *
  2227. * Sets bit 15 of the MII Control regiser
  2228. ******************************************************************************/
  2229. static int
  2230. e1000_phy_reset(struct e1000_hw *hw)
  2231. {
  2232. uint16_t phy_data;
  2233. DEBUGFUNC();
  2234. if (e1000_read_phy_reg(hw, PHY_CTRL, &phy_data) < 0) {
  2235. DEBUGOUT("PHY Read Error\n");
  2236. return -E1000_ERR_PHY;
  2237. }
  2238. phy_data |= MII_CR_RESET;
  2239. if (e1000_write_phy_reg(hw, PHY_CTRL, phy_data) < 0) {
  2240. DEBUGOUT("PHY Write Error\n");
  2241. return -E1000_ERR_PHY;
  2242. }
  2243. udelay(1);
  2244. return 0;
  2245. }
  2246. static int e1000_set_phy_type (struct e1000_hw *hw)
  2247. {
  2248. DEBUGFUNC ();
  2249. if (hw->mac_type == e1000_undefined)
  2250. return -E1000_ERR_PHY_TYPE;
  2251. switch (hw->phy_id) {
  2252. case M88E1000_E_PHY_ID:
  2253. case M88E1000_I_PHY_ID:
  2254. case M88E1011_I_PHY_ID:
  2255. hw->phy_type = e1000_phy_m88;
  2256. break;
  2257. case IGP01E1000_I_PHY_ID:
  2258. if (hw->mac_type == e1000_82541 ||
  2259. hw->mac_type == e1000_82541_rev_2) {
  2260. hw->phy_type = e1000_phy_igp;
  2261. break;
  2262. }
  2263. /* Fall Through */
  2264. default:
  2265. /* Should never have loaded on this device */
  2266. hw->phy_type = e1000_phy_undefined;
  2267. return -E1000_ERR_PHY_TYPE;
  2268. }
  2269. return E1000_SUCCESS;
  2270. }
  2271. /******************************************************************************
  2272. * Probes the expected PHY address for known PHY IDs
  2273. *
  2274. * hw - Struct containing variables accessed by shared code
  2275. ******************************************************************************/
  2276. static int
  2277. e1000_detect_gig_phy(struct e1000_hw *hw)
  2278. {
  2279. int32_t phy_init_status;
  2280. uint16_t phy_id_high, phy_id_low;
  2281. int match = FALSE;
  2282. DEBUGFUNC();
  2283. /* Read the PHY ID Registers to identify which PHY is onboard. */
  2284. if (e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high) < 0) {
  2285. DEBUGOUT("PHY Read Error\n");
  2286. return -E1000_ERR_PHY;
  2287. }
  2288. hw->phy_id = (uint32_t) (phy_id_high << 16);
  2289. udelay(2);
  2290. if (e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low) < 0) {
  2291. DEBUGOUT("PHY Read Error\n");
  2292. return -E1000_ERR_PHY;
  2293. }
  2294. hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
  2295. switch (hw->mac_type) {
  2296. case e1000_82543:
  2297. if (hw->phy_id == M88E1000_E_PHY_ID)
  2298. match = TRUE;
  2299. break;
  2300. case e1000_82544:
  2301. if (hw->phy_id == M88E1000_I_PHY_ID)
  2302. match = TRUE;
  2303. break;
  2304. case e1000_82540:
  2305. case e1000_82545:
  2306. case e1000_82546:
  2307. if (hw->phy_id == M88E1011_I_PHY_ID)
  2308. match = TRUE;
  2309. break;
  2310. case e1000_82541_rev_2:
  2311. if(hw->phy_id == IGP01E1000_I_PHY_ID)
  2312. match = TRUE;
  2313. break;
  2314. default:
  2315. DEBUGOUT("Invalid MAC type %d\n", hw->mac_type);
  2316. return -E1000_ERR_CONFIG;
  2317. }
  2318. phy_init_status = e1000_set_phy_type(hw);
  2319. if ((match) && (phy_init_status == E1000_SUCCESS)) {
  2320. DEBUGOUT("PHY ID 0x%X detected\n", hw->phy_id);
  2321. return 0;
  2322. }
  2323. DEBUGOUT("Invalid PHY ID 0x%X\n", hw->phy_id);
  2324. return -E1000_ERR_PHY;
  2325. }
  2326. /**
  2327. * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
  2328. *
  2329. * e1000_sw_init initializes the Adapter private data structure.
  2330. * Fields are initialized based on PCI device information and
  2331. * OS network device settings (MTU size).
  2332. **/
  2333. static int
  2334. e1000_sw_init(struct eth_device *nic, int cardnum)
  2335. {
  2336. struct e1000_hw *hw = (typeof(hw)) nic->priv;
  2337. int result;
  2338. /* PCI config space info */
  2339. pci_read_config_word(hw->pdev, PCI_VENDOR_ID, &hw->vendor_id);
  2340. pci_read_config_word(hw->pdev, PCI_DEVICE_ID, &hw->device_id);
  2341. pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_VENDOR_ID,
  2342. &hw->subsystem_vendor_id);
  2343. pci_read_config_word(hw->pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_id);
  2344. pci_read_config_byte(hw->pdev, PCI_REVISION_ID, &hw->revision_id);
  2345. pci_read_config_word(hw->pdev, PCI_COMMAND, &hw->pci_cmd_word);
  2346. /* identify the MAC */
  2347. result = e1000_set_mac_type(hw);
  2348. if (result) {
  2349. E1000_ERR("Unknown MAC Type\n");
  2350. return result;
  2351. }
  2352. /* lan a vs. lan b settings */
  2353. if (hw->mac_type == e1000_82546)
  2354. /*this also works w/ multiple 82546 cards */
  2355. /*but not if they're intermingled /w other e1000s */
  2356. hw->lan_loc = (cardnum % 2) ? e1000_lan_b : e1000_lan_a;
  2357. else
  2358. hw->lan_loc = e1000_lan_a;
  2359. /* flow control settings */
  2360. hw->fc_high_water = E1000_FC_HIGH_THRESH;
  2361. hw->fc_low_water = E1000_FC_LOW_THRESH;
  2362. hw->fc_pause_time = E1000_FC_PAUSE_TIME;
  2363. hw->fc_send_xon = 1;
  2364. /* Media type - copper or fiber */
  2365. if (hw->mac_type >= e1000_82543) {
  2366. uint32_t status = E1000_READ_REG(hw, STATUS);
  2367. if (status & E1000_STATUS_TBIMODE) {
  2368. DEBUGOUT("fiber interface\n");
  2369. hw->media_type = e1000_media_type_fiber;
  2370. } else {
  2371. DEBUGOUT("copper interface\n");
  2372. hw->media_type = e1000_media_type_copper;
  2373. }
  2374. } else {
  2375. hw->media_type = e1000_media_type_fiber;
  2376. }
  2377. if (hw->mac_type < e1000_82543)
  2378. hw->report_tx_early = 0;
  2379. else
  2380. hw->report_tx_early = 1;
  2381. hw->tbi_compatibility_en = TRUE;
  2382. #if 0
  2383. hw->wait_autoneg_complete = FALSE;
  2384. hw->adaptive_ifs = TRUE;
  2385. /* Copper options */
  2386. if (hw->media_type == e1000_media_type_copper) {
  2387. hw->mdix = AUTO_ALL_MODES;
  2388. hw->disable_polarity_correction = FALSE;
  2389. }
  2390. #endif
  2391. return E1000_SUCCESS;
  2392. }
  2393. void
  2394. fill_rx(struct e1000_hw *hw)
  2395. {
  2396. struct e1000_rx_desc *rd;
  2397. rx_last = rx_tail;
  2398. rd = rx_base + rx_tail;
  2399. rx_tail = (rx_tail + 1) % 8;
  2400. memset(rd, 0, 16);
  2401. rd->buffer_addr = cpu_to_le64((u32) & packet);
  2402. E1000_WRITE_REG(hw, RDT, rx_tail);
  2403. }
  2404. /**
  2405. * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
  2406. * @adapter: board private structure
  2407. *
  2408. * Configure the Tx unit of the MAC after a reset.
  2409. **/
  2410. static void
  2411. e1000_configure_tx(struct e1000_hw *hw)
  2412. {
  2413. unsigned long ptr;
  2414. unsigned long tctl;
  2415. unsigned long tipg;
  2416. ptr = (u32) tx_pool;
  2417. if (ptr & 0xf)
  2418. ptr = (ptr + 0x10) & (~0xf);
  2419. tx_base = (typeof(tx_base)) ptr;
  2420. E1000_WRITE_REG(hw, TDBAL, (u32) tx_base);
  2421. E1000_WRITE_REG(hw, TDBAH, 0);
  2422. E1000_WRITE_REG(hw, TDLEN, 128);
  2423. /* Setup the HW Tx Head and Tail descriptor pointers */
  2424. E1000_WRITE_REG(hw, TDH, 0);
  2425. E1000_WRITE_REG(hw, TDT, 0);
  2426. tx_tail = 0;
  2427. /* Set the default values for the Tx Inter Packet Gap timer */
  2428. switch (hw->mac_type) {
  2429. case e1000_82542_rev2_0:
  2430. case e1000_82542_rev2_1:
  2431. tipg = DEFAULT_82542_TIPG_IPGT;
  2432. tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
  2433. tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
  2434. break;
  2435. default:
  2436. if (hw->media_type == e1000_media_type_fiber)
  2437. tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
  2438. else
  2439. tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
  2440. tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
  2441. tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
  2442. }
  2443. E1000_WRITE_REG(hw, TIPG, tipg);
  2444. #if 0
  2445. /* Set the Tx Interrupt Delay register */
  2446. E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
  2447. if (hw->mac_type >= e1000_82540)
  2448. E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
  2449. #endif
  2450. /* Program the Transmit Control Register */
  2451. tctl = E1000_READ_REG(hw, TCTL);
  2452. tctl &= ~E1000_TCTL_CT;
  2453. tctl |= E1000_TCTL_EN | E1000_TCTL_PSP |
  2454. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  2455. E1000_WRITE_REG(hw, TCTL, tctl);
  2456. e1000_config_collision_dist(hw);
  2457. #if 0
  2458. /* Setup Transmit Descriptor Settings for this adapter */
  2459. adapter->txd_cmd = E1000_TXD_CMD_IFCS | E1000_TXD_CMD_IDE;
  2460. if (adapter->hw.report_tx_early == 1)
  2461. adapter->txd_cmd |= E1000_TXD_CMD_RS;
  2462. else
  2463. adapter->txd_cmd |= E1000_TXD_CMD_RPS;
  2464. #endif
  2465. }
  2466. /**
  2467. * e1000_setup_rctl - configure the receive control register
  2468. * @adapter: Board private structure
  2469. **/
  2470. static void
  2471. e1000_setup_rctl(struct e1000_hw *hw)
  2472. {
  2473. uint32_t rctl;
  2474. rctl = E1000_READ_REG(hw, RCTL);
  2475. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  2476. rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF; /* |
  2477. (hw.mc_filter_type << E1000_RCTL_MO_SHIFT); */
  2478. if (hw->tbi_compatibility_on == 1)
  2479. rctl |= E1000_RCTL_SBP;
  2480. else
  2481. rctl &= ~E1000_RCTL_SBP;
  2482. rctl &= ~(E1000_RCTL_SZ_4096);
  2483. #if 0
  2484. switch (adapter->rx_buffer_len) {
  2485. case E1000_RXBUFFER_2048:
  2486. default:
  2487. #endif
  2488. rctl |= E1000_RCTL_SZ_2048;
  2489. rctl &= ~(E1000_RCTL_BSEX | E1000_RCTL_LPE);
  2490. #if 0
  2491. break;
  2492. case E1000_RXBUFFER_4096:
  2493. rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
  2494. break;
  2495. case E1000_RXBUFFER_8192:
  2496. rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
  2497. break;
  2498. case E1000_RXBUFFER_16384:
  2499. rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX | E1000_RCTL_LPE;
  2500. break;
  2501. }
  2502. #endif
  2503. E1000_WRITE_REG(hw, RCTL, rctl);
  2504. }
  2505. /**
  2506. * e1000_configure_rx - Configure 8254x Receive Unit after Reset
  2507. * @adapter: board private structure
  2508. *
  2509. * Configure the Rx unit of the MAC after a reset.
  2510. **/
  2511. static void
  2512. e1000_configure_rx(struct e1000_hw *hw)
  2513. {
  2514. unsigned long ptr;
  2515. unsigned long rctl;
  2516. #if 0
  2517. unsigned long rxcsum;
  2518. #endif
  2519. rx_tail = 0;
  2520. /* make sure receives are disabled while setting up the descriptors */
  2521. rctl = E1000_READ_REG(hw, RCTL);
  2522. E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
  2523. #if 0
  2524. /* set the Receive Delay Timer Register */
  2525. E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
  2526. #endif
  2527. if (hw->mac_type >= e1000_82540) {
  2528. #if 0
  2529. E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
  2530. #endif
  2531. /* Set the interrupt throttling rate. Value is calculated
  2532. * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */
  2533. #define MAX_INTS_PER_SEC 8000
  2534. #define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256)
  2535. E1000_WRITE_REG(hw, ITR, DEFAULT_ITR);
  2536. }
  2537. /* Setup the Base and Length of the Rx Descriptor Ring */
  2538. ptr = (u32) rx_pool;
  2539. if (ptr & 0xf)
  2540. ptr = (ptr + 0x10) & (~0xf);
  2541. rx_base = (typeof(rx_base)) ptr;
  2542. E1000_WRITE_REG(hw, RDBAL, (u32) rx_base);
  2543. E1000_WRITE_REG(hw, RDBAH, 0);
  2544. E1000_WRITE_REG(hw, RDLEN, 128);
  2545. /* Setup the HW Rx Head and Tail Descriptor Pointers */
  2546. E1000_WRITE_REG(hw, RDH, 0);
  2547. E1000_WRITE_REG(hw, RDT, 0);
  2548. #if 0
  2549. /* Enable 82543 Receive Checksum Offload for TCP and UDP */
  2550. if ((adapter->hw.mac_type >= e1000_82543) && (adapter->rx_csum == TRUE)) {
  2551. rxcsum = E1000_READ_REG(hw, RXCSUM);
  2552. rxcsum |= E1000_RXCSUM_TUOFL;
  2553. E1000_WRITE_REG(hw, RXCSUM, rxcsum);
  2554. }
  2555. #endif
  2556. /* Enable Receives */
  2557. E1000_WRITE_REG(hw, RCTL, rctl);
  2558. fill_rx(hw);
  2559. }
  2560. /**************************************************************************
  2561. POLL - Wait for a frame
  2562. ***************************************************************************/
  2563. static int
  2564. e1000_poll(struct eth_device *nic)
  2565. {
  2566. struct e1000_hw *hw = nic->priv;
  2567. struct e1000_rx_desc *rd;
  2568. /* return true if there's an ethernet packet ready to read */
  2569. rd = rx_base + rx_last;
  2570. if (!(le32_to_cpu(rd->status)) & E1000_RXD_STAT_DD)
  2571. return 0;
  2572. /*DEBUGOUT("recv: packet len=%d \n", rd->length); */
  2573. NetReceive((uchar *)packet, le32_to_cpu(rd->length));
  2574. fill_rx(hw);
  2575. return 1;
  2576. }
  2577. /**************************************************************************
  2578. TRANSMIT - Transmit a frame
  2579. ***************************************************************************/
  2580. static int
  2581. e1000_transmit(struct eth_device *nic, volatile void *packet, int length)
  2582. {
  2583. struct e1000_hw *hw = nic->priv;
  2584. struct e1000_tx_desc *txp;
  2585. int i = 0;
  2586. txp = tx_base + tx_tail;
  2587. tx_tail = (tx_tail + 1) % 8;
  2588. txp->buffer_addr = cpu_to_le64(virt_to_bus(packet));
  2589. txp->lower.data = cpu_to_le32(E1000_TXD_CMD_RPS | E1000_TXD_CMD_EOP |
  2590. E1000_TXD_CMD_IFCS | length);
  2591. txp->upper.data = 0;
  2592. E1000_WRITE_REG(hw, TDT, tx_tail);
  2593. while (!(le32_to_cpu(txp->upper.data) & E1000_TXD_STAT_DD)) {
  2594. if (i++ > TOUT_LOOP) {
  2595. DEBUGOUT("e1000: tx timeout\n");
  2596. return 0;
  2597. }
  2598. udelay(10); /* give the nic a chance to write to the register */
  2599. }
  2600. return 1;
  2601. }
  2602. /*reset function*/
  2603. static inline int
  2604. e1000_reset(struct eth_device *nic)
  2605. {
  2606. struct e1000_hw *hw = nic->priv;
  2607. e1000_reset_hw(hw);
  2608. if (hw->mac_type >= e1000_82544) {
  2609. E1000_WRITE_REG(hw, WUC, 0);
  2610. }
  2611. return e1000_init_hw(nic);
  2612. }
  2613. /**************************************************************************
  2614. DISABLE - Turn off ethernet interface
  2615. ***************************************************************************/
  2616. static void
  2617. e1000_disable(struct eth_device *nic)
  2618. {
  2619. struct e1000_hw *hw = nic->priv;
  2620. /* Turn off the ethernet interface */
  2621. E1000_WRITE_REG(hw, RCTL, 0);
  2622. E1000_WRITE_REG(hw, TCTL, 0);
  2623. /* Clear the transmit ring */
  2624. E1000_WRITE_REG(hw, TDH, 0);
  2625. E1000_WRITE_REG(hw, TDT, 0);
  2626. /* Clear the receive ring */
  2627. E1000_WRITE_REG(hw, RDH, 0);
  2628. E1000_WRITE_REG(hw, RDT, 0);
  2629. /* put the card in its initial state */
  2630. #if 0
  2631. E1000_WRITE_REG(hw, CTRL, E1000_CTRL_RST);
  2632. #endif
  2633. mdelay(10);
  2634. }
  2635. /**************************************************************************
  2636. INIT - set up ethernet interface(s)
  2637. ***************************************************************************/
  2638. static int
  2639. e1000_init(struct eth_device *nic, bd_t * bis)
  2640. {
  2641. struct e1000_hw *hw = nic->priv;
  2642. int ret_val = 0;
  2643. ret_val = e1000_reset(nic);
  2644. if (ret_val < 0) {
  2645. if ((ret_val == -E1000_ERR_NOLINK) ||
  2646. (ret_val == -E1000_ERR_TIMEOUT)) {
  2647. E1000_ERR("Valid Link not detected\n");
  2648. } else {
  2649. E1000_ERR("Hardware Initialization Failed\n");
  2650. }
  2651. return 0;
  2652. }
  2653. e1000_configure_tx(hw);
  2654. e1000_setup_rctl(hw);
  2655. e1000_configure_rx(hw);
  2656. return 1;
  2657. }
  2658. /**************************************************************************
  2659. PROBE - Look for an adapter, this routine's visible to the outside
  2660. You should omit the last argument struct pci_device * for a non-PCI NIC
  2661. ***************************************************************************/
  2662. int
  2663. e1000_initialize(bd_t * bis)
  2664. {
  2665. pci_dev_t devno;
  2666. int card_number = 0;
  2667. struct eth_device *nic = NULL;
  2668. struct e1000_hw *hw = NULL;
  2669. u32 iobase;
  2670. int idx = 0;
  2671. u32 PciCommandWord;
  2672. while (1) { /* Find PCI device(s) */
  2673. if ((devno = pci_find_devices(supported, idx++)) < 0) {
  2674. break;
  2675. }
  2676. pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &iobase);
  2677. iobase &= ~0xf; /* Mask the bits that say "this is an io addr" */
  2678. DEBUGOUT("e1000#%d: iobase 0x%08x\n", card_number, iobase);
  2679. pci_write_config_dword(devno, PCI_COMMAND,
  2680. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  2681. /* Check if I/O accesses and Bus Mastering are enabled. */
  2682. pci_read_config_dword(devno, PCI_COMMAND, &PciCommandWord);
  2683. if (!(PciCommandWord & PCI_COMMAND_MEMORY)) {
  2684. printf("Error: Can not enable MEM access.\n");
  2685. continue;
  2686. } else if (!(PciCommandWord & PCI_COMMAND_MASTER)) {
  2687. printf("Error: Can not enable Bus Mastering.\n");
  2688. continue;
  2689. }
  2690. nic = (struct eth_device *) malloc(sizeof (*nic));
  2691. hw = (struct e1000_hw *) malloc(sizeof (*hw));
  2692. hw->pdev = devno;
  2693. nic->priv = hw;
  2694. nic->iobase = bus_to_phys(devno, iobase);
  2695. sprintf(nic->name, "e1000#%d", card_number);
  2696. /* Are these variables needed? */
  2697. #if 0
  2698. hw->fc = e1000_fc_none;
  2699. hw->original_fc = e1000_fc_none;
  2700. #else
  2701. hw->fc = e1000_fc_default;
  2702. hw->original_fc = e1000_fc_default;
  2703. #endif
  2704. hw->autoneg_failed = 0;
  2705. hw->get_link_status = TRUE;
  2706. hw->hw_addr = (typeof(hw->hw_addr)) iobase;
  2707. hw->mac_type = e1000_undefined;
  2708. /* MAC and Phy settings */
  2709. if (e1000_sw_init(nic, card_number) < 0) {
  2710. free(hw);
  2711. free(nic);
  2712. return 0;
  2713. }
  2714. #if !(defined(CONFIG_AP1000) || defined(CONFIG_MVBC_1G))
  2715. if (e1000_validate_eeprom_checksum(nic) < 0) {
  2716. printf("The EEPROM Checksum Is Not Valid\n");
  2717. free(hw);
  2718. free(nic);
  2719. return 0;
  2720. }
  2721. #endif
  2722. e1000_read_mac_addr(nic);
  2723. E1000_WRITE_REG(hw, PBA, E1000_DEFAULT_PBA);
  2724. printf("e1000: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2725. nic->enetaddr[0], nic->enetaddr[1], nic->enetaddr[2],
  2726. nic->enetaddr[3], nic->enetaddr[4], nic->enetaddr[5]);
  2727. nic->init = e1000_init;
  2728. nic->recv = e1000_poll;
  2729. nic->send = e1000_transmit;
  2730. nic->halt = e1000_disable;
  2731. eth_register(nic);
  2732. card_number++;
  2733. }
  2734. return 1;
  2735. }