e1000.c 91 KB

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