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