e1000.c 93 KB

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