e1000.c 92 KB

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