Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232
  1. /*
  2. * Copyright (c) 2008 Marty Connor <mdc@etherboot.org>
  3. * Copyright (c) 2008 Entity Cyber, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * This driver is based on rtl8169 data sheets and work by:
  20. *
  21. * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
  22. * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
  23. * Copyright (c) a lot of people too. Please respect their work.
  24. */
  25. FILE_LICENCE ( GPL2_OR_LATER );
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <assert.h>
  32. #include <byteswap.h>
  33. #include <errno.h>
  34. #include <ipxe/ethernet.h>
  35. #include <ipxe/if_ether.h>
  36. #include <ipxe/io.h>
  37. #include <ipxe/iobuf.h>
  38. #include <ipxe/malloc.h>
  39. #include <ipxe/netdevice.h>
  40. #include <ipxe/pci.h>
  41. #include <ipxe/timer.h>
  42. #include <mii.h>
  43. #include "r8169.h"
  44. /*** Low level hardware routines ***/
  45. static void mdio_write(void *ioaddr, int reg_addr, int value)
  46. {
  47. int i;
  48. DBGP ( "mdio_write\n" );
  49. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  50. for (i = 20; i > 0; i--) {
  51. /*
  52. * Check if the RTL8169 has completed writing to the specified
  53. * MII register.
  54. */
  55. if (!(RTL_R32(PHYAR) & 0x80000000))
  56. break;
  57. udelay(25);
  58. }
  59. }
  60. static int mdio_read(void *ioaddr, int reg_addr)
  61. {
  62. int i, value = -1;
  63. DBGP ( "mdio_read\n" );
  64. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  65. for (i = 20; i > 0; i--) {
  66. /*
  67. * Check if the RTL8169 has completed retrieving data from
  68. * the specified MII register.
  69. */
  70. if (RTL_R32(PHYAR) & 0x80000000) {
  71. value = RTL_R32(PHYAR) & 0xffff;
  72. break;
  73. }
  74. udelay(25);
  75. }
  76. return value;
  77. }
  78. static void mdio_patch(void *ioaddr, int reg_addr, int value)
  79. {
  80. DBGP ( "mdio_patch\n" );
  81. mdio_write(ioaddr, reg_addr, mdio_read(ioaddr, reg_addr) | value);
  82. }
  83. static void rtl_ephy_write(void *ioaddr, int reg_addr, int value)
  84. {
  85. unsigned int i;
  86. DBGP ( "rtl_ephy_write\n" );
  87. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  88. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  89. for (i = 0; i < 100; i++) {
  90. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  91. break;
  92. udelay(10);
  93. }
  94. }
  95. static u16 rtl_ephy_read(void *ioaddr, int reg_addr)
  96. {
  97. u16 value = 0xffff;
  98. unsigned int i;
  99. DBGP ( "rtl_ephy_read\n" );
  100. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  101. for (i = 0; i < 100; i++) {
  102. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  103. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  104. break;
  105. }
  106. udelay(10);
  107. }
  108. return value;
  109. }
  110. static void rtl_csi_write(void *ioaddr, int addr, int value)
  111. {
  112. unsigned int i;
  113. DBGP ( "rtl_csi_write\n" );
  114. RTL_W32(CSIDR, value);
  115. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  116. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  117. for (i = 0; i < 100; i++) {
  118. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  119. break;
  120. udelay(10);
  121. }
  122. }
  123. static u32 rtl_csi_read(void *ioaddr, int addr)
  124. {
  125. u32 value = ~0x00;
  126. unsigned int i;
  127. DBGP ( "rtl_csi_read\n" );
  128. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  129. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  130. for (i = 0; i < 100; i++) {
  131. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  132. value = RTL_R32(CSIDR);
  133. break;
  134. }
  135. udelay(10);
  136. }
  137. return value;
  138. }
  139. static void rtl8169_irq_mask_and_ack(void *ioaddr)
  140. {
  141. DBGP ( "rtl8169_irq_mask_and_ack\n" );
  142. RTL_W16(IntrMask, 0x0000);
  143. RTL_W16(IntrStatus, 0xffff);
  144. }
  145. static unsigned int rtl8169_tbi_reset_pending(void *ioaddr)
  146. {
  147. DBGP ( "rtl8169_tbi_reset_pending\n" );
  148. return RTL_R32(TBICSR) & TBIReset;
  149. }
  150. static unsigned int rtl8169_xmii_reset_pending(void *ioaddr)
  151. {
  152. DBGP ( "rtl8169_xmii_reset_pending\n" );
  153. return mdio_read(ioaddr, MII_BMCR) & BMCR_RESET;
  154. }
  155. static unsigned int rtl8169_tbi_link_ok(void *ioaddr)
  156. {
  157. DBGP ( "rtl8169_tbi_link_ok\n" );
  158. return RTL_R32(TBICSR) & TBILinkOk;
  159. }
  160. static unsigned int rtl8169_xmii_link_ok(void *ioaddr)
  161. {
  162. DBGP ( "rtl8169_xmii_link_ok\n" );
  163. return RTL_R8(PHYstatus) & LinkStatus;
  164. }
  165. static void rtl8169_tbi_reset_enable(void *ioaddr)
  166. {
  167. DBGP ( "rtl8169_tbi_reset_enable\n" );
  168. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  169. }
  170. static void rtl8169_xmii_reset_enable(void *ioaddr)
  171. {
  172. unsigned int val;
  173. DBGP ( "rtl8169_xmii_reset_enable\n" );
  174. val = mdio_read(ioaddr, MII_BMCR) | BMCR_RESET;
  175. mdio_write(ioaddr, MII_BMCR, val & 0xffff);
  176. }
  177. static int rtl8169_set_speed_tbi(struct net_device *dev,
  178. u8 autoneg, u16 speed, u8 duplex)
  179. {
  180. struct rtl8169_private *tp = netdev_priv(dev);
  181. void *ioaddr = tp->mmio_addr;
  182. int ret = 0;
  183. u32 reg;
  184. DBGP ( "rtl8169_set_speed_tbi\n" );
  185. reg = RTL_R32(TBICSR);
  186. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  187. (duplex == DUPLEX_FULL)) {
  188. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  189. } else if (autoneg == AUTONEG_ENABLE)
  190. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  191. else {
  192. DBG ( "incorrect speed setting refused in TBI mode\n" );
  193. ret = -EOPNOTSUPP;
  194. }
  195. return ret;
  196. }
  197. static int rtl8169_set_speed_xmii(struct net_device *dev,
  198. u8 autoneg, u16 speed, u8 duplex)
  199. {
  200. struct rtl8169_private *tp = netdev_priv(dev);
  201. void *ioaddr = tp->mmio_addr;
  202. int auto_nego, giga_ctrl;
  203. DBGP ( "rtl8169_set_speed_xmii\n" );
  204. auto_nego = mdio_read(ioaddr, MII_ADVERTISE);
  205. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  206. ADVERTISE_100HALF | ADVERTISE_100FULL);
  207. giga_ctrl = mdio_read(ioaddr, MII_CTRL1000);
  208. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  209. if (autoneg == AUTONEG_ENABLE) {
  210. auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  211. ADVERTISE_100HALF | ADVERTISE_100FULL);
  212. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  213. } else {
  214. if (speed == SPEED_10)
  215. auto_nego |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  216. else if (speed == SPEED_100)
  217. auto_nego |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  218. else if (speed == SPEED_1000)
  219. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  220. if (duplex == DUPLEX_HALF)
  221. auto_nego &= ~(ADVERTISE_10FULL | ADVERTISE_100FULL);
  222. if (duplex == DUPLEX_FULL)
  223. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_100HALF);
  224. /* This tweak comes straight from Realtek's driver. */
  225. if ((speed == SPEED_100) && (duplex == DUPLEX_HALF) &&
  226. ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  227. (tp->mac_version == RTL_GIGA_MAC_VER_16))) {
  228. auto_nego = ADVERTISE_100HALF | ADVERTISE_CSMA;
  229. }
  230. }
  231. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  232. if ((tp->mac_version == RTL_GIGA_MAC_VER_07) ||
  233. (tp->mac_version == RTL_GIGA_MAC_VER_08) ||
  234. (tp->mac_version == RTL_GIGA_MAC_VER_09) ||
  235. (tp->mac_version == RTL_GIGA_MAC_VER_10) ||
  236. (tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  237. (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
  238. (tp->mac_version == RTL_GIGA_MAC_VER_15) ||
  239. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  240. if ((giga_ctrl & (ADVERTISE_1000FULL | ADVERTISE_1000HALF))) {
  241. DBG ( "PHY does not support 1000Mbps.\n" );
  242. }
  243. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  244. }
  245. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  246. if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
  247. (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
  248. (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
  249. /*
  250. * Wake up the PHY.
  251. * Vendor specific (0x1f) and reserved (0x0e) MII registers.
  252. */
  253. mdio_write(ioaddr, 0x1f, 0x0000);
  254. mdio_write(ioaddr, 0x0e, 0x0000);
  255. }
  256. tp->phy_auto_nego_reg = auto_nego;
  257. tp->phy_1000_ctrl_reg = giga_ctrl;
  258. mdio_write(ioaddr, MII_ADVERTISE, auto_nego);
  259. mdio_write(ioaddr, MII_CTRL1000, giga_ctrl);
  260. mdio_write(ioaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  261. return 0;
  262. }
  263. static int rtl8169_set_speed(struct net_device *dev,
  264. u8 autoneg, u16 speed, u8 duplex)
  265. {
  266. struct rtl8169_private *tp = netdev_priv(dev);
  267. int ret;
  268. DBGP ( "rtl8169_set_speed\n" );
  269. ret = tp->set_speed(dev, autoneg, speed, duplex);
  270. return ret;
  271. }
  272. static void rtl8169_write_gmii_reg_bit(void *ioaddr, int reg,
  273. int bitnum, int bitval)
  274. {
  275. int val;
  276. DBGP ( "rtl8169_write_gmii_reg_bit\n" );
  277. val = mdio_read(ioaddr, reg);
  278. val = (bitval == 1) ?
  279. val | (bitval << bitnum) : val & ~(0x0001 << bitnum);
  280. mdio_write(ioaddr, reg, val & 0xffff);
  281. }
  282. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  283. void *ioaddr)
  284. {
  285. /*
  286. * The driver currently handles the 8168Bf and the 8168Be identically
  287. * but they can be identified more specifically through the test below
  288. * if needed:
  289. *
  290. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  291. *
  292. * Same thing for the 8101Eb and the 8101Ec:
  293. *
  294. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  295. */
  296. const struct {
  297. u32 mask;
  298. u32 val;
  299. int mac_version;
  300. } mac_info[] = {
  301. /* 8168D family. */
  302. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_25 },
  303. /* 8168C family. */
  304. { 0x7cf00000, 0x3ca00000, RTL_GIGA_MAC_VER_24 },
  305. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  306. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  307. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  308. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  309. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  310. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  311. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  312. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  313. /* 8168B family. */
  314. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  315. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  316. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  317. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  318. /* 8101 family. */
  319. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  320. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  321. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  322. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  323. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  324. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  325. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  326. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  327. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  328. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  329. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  330. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  331. /* FIXME: where did these entries come from ? -- FR */
  332. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  333. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  334. /* 8110 family. */
  335. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  336. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  337. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  338. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  339. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  340. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  341. { 0x00000000, 0x00000000, RTL_GIGA_MAC_VER_01 } /* Catch-all */
  342. }, *p = mac_info;
  343. u32 reg;
  344. DBGP ( "rtl8169_get_mac_version\n" );
  345. reg = RTL_R32(TxConfig);
  346. while ((reg & p->mask) != p->val)
  347. p++;
  348. tp->mac_version = p->mac_version;
  349. DBG ( "tp->mac_version = %d\n", tp->mac_version );
  350. if (p->mask == 0x00000000) {
  351. DBG ( "unknown MAC (%08x)\n", reg );
  352. }
  353. }
  354. struct phy_reg {
  355. u16 reg;
  356. u16 val;
  357. };
  358. static void rtl_phy_write(void *ioaddr, struct phy_reg *regs, int len)
  359. {
  360. DBGP ( "rtl_phy_write\n" );
  361. while (len-- > 0) {
  362. mdio_write(ioaddr, regs->reg, regs->val);
  363. regs++;
  364. }
  365. }
  366. static void rtl8169s_hw_phy_config(void *ioaddr)
  367. {
  368. struct {
  369. u16 regs[5]; /* Beware of bit-sign propagation */
  370. } phy_magic[5] = { {
  371. { 0x0000, //w 4 15 12 0
  372. 0x00a1, //w 3 15 0 00a1
  373. 0x0008, //w 2 15 0 0008
  374. 0x1020, //w 1 15 0 1020
  375. 0x1000 } },{ //w 0 15 0 1000
  376. { 0x7000, //w 4 15 12 7
  377. 0xff41, //w 3 15 0 ff41
  378. 0xde60, //w 2 15 0 de60
  379. 0x0140, //w 1 15 0 0140
  380. 0x0077 } },{ //w 0 15 0 0077
  381. { 0xa000, //w 4 15 12 a
  382. 0xdf01, //w 3 15 0 df01
  383. 0xdf20, //w 2 15 0 df20
  384. 0xff95, //w 1 15 0 ff95
  385. 0xfa00 } },{ //w 0 15 0 fa00
  386. { 0xb000, //w 4 15 12 b
  387. 0xff41, //w 3 15 0 ff41
  388. 0xde20, //w 2 15 0 de20
  389. 0x0140, //w 1 15 0 0140
  390. 0x00bb } },{ //w 0 15 0 00bb
  391. { 0xf000, //w 4 15 12 f
  392. 0xdf01, //w 3 15 0 df01
  393. 0xdf20, //w 2 15 0 df20
  394. 0xff95, //w 1 15 0 ff95
  395. 0xbf00 } //w 0 15 0 bf00
  396. }
  397. }, *p = phy_magic;
  398. unsigned int i;
  399. DBGP ( "rtl8169s_hw_phy_config\n" );
  400. mdio_write(ioaddr, 0x1f, 0x0001); //w 31 2 0 1
  401. mdio_write(ioaddr, 0x15, 0x1000); //w 21 15 0 1000
  402. mdio_write(ioaddr, 0x18, 0x65c7); //w 24 15 0 65c7
  403. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  404. for (i = 0; i < ARRAY_SIZE(phy_magic); i++, p++) {
  405. int val, pos = 4;
  406. val = (mdio_read(ioaddr, pos) & 0x0fff) | (p->regs[0] & 0xffff);
  407. mdio_write(ioaddr, pos, val);
  408. while (--pos >= 0)
  409. mdio_write(ioaddr, pos, p->regs[4 - pos] & 0xffff);
  410. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 1); //w 4 11 11 1
  411. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  412. }
  413. mdio_write(ioaddr, 0x1f, 0x0000); //w 31 2 0 0
  414. }
  415. static void rtl8169sb_hw_phy_config(void *ioaddr)
  416. {
  417. struct phy_reg phy_reg_init[] = {
  418. { 0x1f, 0x0002 },
  419. { 0x01, 0x90d0 },
  420. { 0x1f, 0x0000 }
  421. };
  422. DBGP ( "rtl8169sb_hw_phy_config\n" );
  423. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  424. }
  425. static void rtl8168bb_hw_phy_config(void *ioaddr)
  426. {
  427. struct phy_reg phy_reg_init[] = {
  428. { 0x10, 0xf41b },
  429. { 0x1f, 0x0000 }
  430. };
  431. mdio_write(ioaddr, 0x1f, 0x0001);
  432. mdio_patch(ioaddr, 0x16, 1 << 0);
  433. DBGP ( "rtl8168bb_hw_phy_config\n" );
  434. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  435. }
  436. static void rtl8168bef_hw_phy_config(void *ioaddr)
  437. {
  438. struct phy_reg phy_reg_init[] = {
  439. { 0x1f, 0x0001 },
  440. { 0x10, 0xf41b },
  441. { 0x1f, 0x0000 }
  442. };
  443. DBGP ( "rtl8168bef_hw_phy_config\n" );
  444. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  445. }
  446. static void rtl8168cp_1_hw_phy_config(void *ioaddr)
  447. {
  448. struct phy_reg phy_reg_init[] = {
  449. { 0x1f, 0x0000 },
  450. { 0x1d, 0x0f00 },
  451. { 0x1f, 0x0002 },
  452. { 0x0c, 0x1ec8 },
  453. { 0x1f, 0x0000 }
  454. };
  455. DBGP ( "rtl8168cp_1_hw_phy_config\n" );
  456. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  457. }
  458. static void rtl8168cp_2_hw_phy_config(void *ioaddr)
  459. {
  460. struct phy_reg phy_reg_init[] = {
  461. { 0x1f, 0x0001 },
  462. { 0x1d, 0x3d98 },
  463. { 0x1f, 0x0000 }
  464. };
  465. DBGP ( "rtl8168cp_2_hw_phy_config\n" );
  466. mdio_write(ioaddr, 0x1f, 0x0000);
  467. mdio_patch(ioaddr, 0x14, 1 << 5);
  468. mdio_patch(ioaddr, 0x0d, 1 << 5);
  469. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  470. }
  471. static void rtl8168c_1_hw_phy_config(void *ioaddr)
  472. {
  473. struct phy_reg phy_reg_init[] = {
  474. { 0x1f, 0x0001 },
  475. { 0x12, 0x2300 },
  476. { 0x1f, 0x0002 },
  477. { 0x00, 0x88d4 },
  478. { 0x01, 0x82b1 },
  479. { 0x03, 0x7002 },
  480. { 0x08, 0x9e30 },
  481. { 0x09, 0x01f0 },
  482. { 0x0a, 0x5500 },
  483. { 0x0c, 0x00c8 },
  484. { 0x1f, 0x0003 },
  485. { 0x12, 0xc096 },
  486. { 0x16, 0x000a },
  487. { 0x1f, 0x0000 },
  488. { 0x1f, 0x0000 },
  489. { 0x09, 0x2000 },
  490. { 0x09, 0x0000 }
  491. };
  492. DBGP ( "rtl8168c_1_hw_phy_config\n" );
  493. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  494. mdio_patch(ioaddr, 0x14, 1 << 5);
  495. mdio_patch(ioaddr, 0x0d, 1 << 5);
  496. mdio_write(ioaddr, 0x1f, 0x0000);
  497. }
  498. static void rtl8168c_2_hw_phy_config(void *ioaddr)
  499. {
  500. struct phy_reg phy_reg_init[] = {
  501. { 0x1f, 0x0001 },
  502. { 0x12, 0x2300 },
  503. { 0x03, 0x802f },
  504. { 0x02, 0x4f02 },
  505. { 0x01, 0x0409 },
  506. { 0x00, 0xf099 },
  507. { 0x04, 0x9800 },
  508. { 0x04, 0x9000 },
  509. { 0x1d, 0x3d98 },
  510. { 0x1f, 0x0002 },
  511. { 0x0c, 0x7eb8 },
  512. { 0x06, 0x0761 },
  513. { 0x1f, 0x0003 },
  514. { 0x16, 0x0f0a },
  515. { 0x1f, 0x0000 }
  516. };
  517. DBGP ( "rtl8168c_2_hw_phy_config\n" );
  518. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  519. mdio_patch(ioaddr, 0x16, 1 << 0);
  520. mdio_patch(ioaddr, 0x14, 1 << 5);
  521. mdio_patch(ioaddr, 0x0d, 1 << 5);
  522. mdio_write(ioaddr, 0x1f, 0x0000);
  523. }
  524. static void rtl8168c_3_hw_phy_config(void *ioaddr)
  525. {
  526. struct phy_reg phy_reg_init[] = {
  527. { 0x1f, 0x0001 },
  528. { 0x12, 0x2300 },
  529. { 0x1d, 0x3d98 },
  530. { 0x1f, 0x0002 },
  531. { 0x0c, 0x7eb8 },
  532. { 0x06, 0x5461 },
  533. { 0x1f, 0x0003 },
  534. { 0x16, 0x0f0a },
  535. { 0x1f, 0x0000 }
  536. };
  537. DBGP ( "rtl8168c_3_hw_phy_config\n" );
  538. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  539. mdio_patch(ioaddr, 0x16, 1 << 0);
  540. mdio_patch(ioaddr, 0x14, 1 << 5);
  541. mdio_patch(ioaddr, 0x0d, 1 << 5);
  542. mdio_write(ioaddr, 0x1f, 0x0000);
  543. }
  544. static void rtl8168c_4_hw_phy_config(void *ioaddr)
  545. {
  546. DBGP ( "rtl8168c_4_hw_phy_config\n" );
  547. rtl8168c_3_hw_phy_config(ioaddr);
  548. }
  549. static void rtl8168d_hw_phy_config(void *ioaddr)
  550. {
  551. struct phy_reg phy_reg_init_0[] = {
  552. { 0x1f, 0x0001 },
  553. { 0x09, 0x2770 },
  554. { 0x08, 0x04d0 },
  555. { 0x0b, 0xad15 },
  556. { 0x0c, 0x5bf0 },
  557. { 0x1c, 0xf101 },
  558. { 0x1f, 0x0003 },
  559. { 0x14, 0x94d7 },
  560. { 0x12, 0xf4d6 },
  561. { 0x09, 0xca0f },
  562. { 0x1f, 0x0002 },
  563. { 0x0b, 0x0b10 },
  564. { 0x0c, 0xd1f7 },
  565. { 0x1f, 0x0002 },
  566. { 0x06, 0x5461 },
  567. { 0x1f, 0x0002 },
  568. { 0x05, 0x6662 },
  569. { 0x1f, 0x0000 },
  570. { 0x14, 0x0060 },
  571. { 0x1f, 0x0000 },
  572. { 0x0d, 0xf8a0 },
  573. { 0x1f, 0x0005 },
  574. { 0x05, 0xffc2 }
  575. };
  576. DBGP ( "rtl8168d_hw_phy_config\n" );
  577. rtl_phy_write(ioaddr, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  578. if (mdio_read(ioaddr, 0x06) == 0xc400) {
  579. struct phy_reg phy_reg_init_1[] = {
  580. { 0x1f, 0x0005 },
  581. { 0x01, 0x0300 },
  582. { 0x1f, 0x0000 },
  583. { 0x11, 0x401c },
  584. { 0x16, 0x4100 },
  585. { 0x1f, 0x0005 },
  586. { 0x07, 0x0010 },
  587. { 0x05, 0x83dc },
  588. { 0x06, 0x087d },
  589. { 0x05, 0x8300 },
  590. { 0x06, 0x0101 },
  591. { 0x06, 0x05f8 },
  592. { 0x06, 0xf9fa },
  593. { 0x06, 0xfbef },
  594. { 0x06, 0x79e2 },
  595. { 0x06, 0x835f },
  596. { 0x06, 0xe0f8 },
  597. { 0x06, 0x9ae1 },
  598. { 0x06, 0xf89b },
  599. { 0x06, 0xef31 },
  600. { 0x06, 0x3b65 },
  601. { 0x06, 0xaa07 },
  602. { 0x06, 0x81e4 },
  603. { 0x06, 0xf89a },
  604. { 0x06, 0xe5f8 },
  605. { 0x06, 0x9baf },
  606. { 0x06, 0x06ae },
  607. { 0x05, 0x83dc },
  608. { 0x06, 0x8300 },
  609. };
  610. rtl_phy_write(ioaddr, phy_reg_init_1,
  611. ARRAY_SIZE(phy_reg_init_1));
  612. }
  613. mdio_write(ioaddr, 0x1f, 0x0000);
  614. }
  615. static void rtl8102e_hw_phy_config(void *ioaddr)
  616. {
  617. struct phy_reg phy_reg_init[] = {
  618. { 0x1f, 0x0003 },
  619. { 0x08, 0x441d },
  620. { 0x01, 0x9100 },
  621. { 0x1f, 0x0000 }
  622. };
  623. DBGP ( "rtl8102e_hw_phy_config\n" );
  624. mdio_write(ioaddr, 0x1f, 0x0000);
  625. mdio_patch(ioaddr, 0x11, 1 << 12);
  626. mdio_patch(ioaddr, 0x19, 1 << 13);
  627. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  628. }
  629. static void rtl_hw_phy_config(struct net_device *dev)
  630. {
  631. struct rtl8169_private *tp = netdev_priv(dev);
  632. void *ioaddr = tp->mmio_addr;
  633. DBGP ( "rtl_hw_phy_config\n" );
  634. DBG ( "mac_version = 0x%02x\n", tp->mac_version );
  635. switch (tp->mac_version) {
  636. case RTL_GIGA_MAC_VER_01:
  637. break;
  638. case RTL_GIGA_MAC_VER_02:
  639. case RTL_GIGA_MAC_VER_03:
  640. rtl8169s_hw_phy_config(ioaddr);
  641. break;
  642. case RTL_GIGA_MAC_VER_04:
  643. rtl8169sb_hw_phy_config(ioaddr);
  644. break;
  645. case RTL_GIGA_MAC_VER_07:
  646. case RTL_GIGA_MAC_VER_08:
  647. case RTL_GIGA_MAC_VER_09:
  648. rtl8102e_hw_phy_config(ioaddr);
  649. break;
  650. case RTL_GIGA_MAC_VER_11:
  651. rtl8168bb_hw_phy_config(ioaddr);
  652. break;
  653. case RTL_GIGA_MAC_VER_12:
  654. rtl8168bef_hw_phy_config(ioaddr);
  655. break;
  656. case RTL_GIGA_MAC_VER_17:
  657. rtl8168bef_hw_phy_config(ioaddr);
  658. break;
  659. case RTL_GIGA_MAC_VER_18:
  660. rtl8168cp_1_hw_phy_config(ioaddr);
  661. break;
  662. case RTL_GIGA_MAC_VER_19:
  663. rtl8168c_1_hw_phy_config(ioaddr);
  664. break;
  665. case RTL_GIGA_MAC_VER_20:
  666. rtl8168c_2_hw_phy_config(ioaddr);
  667. break;
  668. case RTL_GIGA_MAC_VER_21:
  669. rtl8168c_3_hw_phy_config(ioaddr);
  670. break;
  671. case RTL_GIGA_MAC_VER_22:
  672. rtl8168c_4_hw_phy_config(ioaddr);
  673. break;
  674. case RTL_GIGA_MAC_VER_23:
  675. case RTL_GIGA_MAC_VER_24:
  676. rtl8168cp_2_hw_phy_config(ioaddr);
  677. break;
  678. case RTL_GIGA_MAC_VER_25:
  679. rtl8168d_hw_phy_config(ioaddr);
  680. break;
  681. default:
  682. break;
  683. }
  684. }
  685. static void rtl8169_phy_reset(struct net_device *dev __unused,
  686. struct rtl8169_private *tp)
  687. {
  688. void *ioaddr = tp->mmio_addr;
  689. unsigned int i;
  690. DBGP ( "rtl8169_phy_reset\n" );
  691. tp->phy_reset_enable(ioaddr);
  692. for (i = 0; i < 100; i++) {
  693. if (!tp->phy_reset_pending(ioaddr))
  694. return;
  695. mdelay ( 1 );
  696. }
  697. DBG ( "PHY reset failed.\n" );
  698. }
  699. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  700. {
  701. void *ioaddr = tp->mmio_addr;
  702. DBGP ( "rtl8169_init_phy\n" );
  703. rtl_hw_phy_config(dev);
  704. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  705. DBG ( "Set MAC Reg C+CR Offset 0x82h = 0x01h\n" );
  706. RTL_W8(0x82, 0x01);
  707. }
  708. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  709. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  710. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  711. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  712. DBG ( "Set MAC Reg C+CR Offset 0x82h = 0x01h\n" );
  713. RTL_W8(0x82, 0x01);
  714. DBG ( "Set PHY Reg 0x0bh = 0x00h\n" );
  715. mdio_write(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
  716. }
  717. rtl8169_phy_reset(dev, tp);
  718. /*
  719. * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
  720. * only 8101. Don't panic.
  721. */
  722. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
  723. if ((RTL_R8(PHYstatus) & TBI_Enable))
  724. DBG ( "TBI auto-negotiating\n" );
  725. }
  726. static const struct rtl_cfg_info {
  727. void (*hw_start)(struct net_device *);
  728. unsigned int region;
  729. unsigned int align;
  730. u16 intr_event;
  731. u16 napi_event;
  732. unsigned features;
  733. } rtl_cfg_infos [] = {
  734. [RTL_CFG_0] = {
  735. .hw_start = rtl_hw_start_8169,
  736. .region = 1,
  737. .align = 0,
  738. .intr_event = SYSErr | LinkChg | RxOverflow |
  739. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  740. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  741. .features = RTL_FEATURE_GMII
  742. },
  743. [RTL_CFG_1] = {
  744. .hw_start = rtl_hw_start_8168,
  745. .region = 2,
  746. .align = 8,
  747. .intr_event = SYSErr | LinkChg | RxOverflow |
  748. TxErr | TxOK | RxOK | RxErr,
  749. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  750. .features = RTL_FEATURE_GMII
  751. },
  752. [RTL_CFG_2] = {
  753. .hw_start = rtl_hw_start_8101,
  754. .region = 2,
  755. .align = 8,
  756. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  757. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  758. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  759. }
  760. };
  761. static void rtl8169_hw_reset(void *ioaddr)
  762. {
  763. DBGP ( "rtl8169_hw_reset\n" );
  764. /* Disable interrupts */
  765. rtl8169_irq_mask_and_ack(ioaddr);
  766. /* Reset the chipset */
  767. RTL_W8(ChipCmd, CmdReset);
  768. /* PCI commit */
  769. RTL_R8(ChipCmd);
  770. }
  771. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  772. {
  773. void *ioaddr = tp->mmio_addr;
  774. u32 cfg = rtl8169_rx_config;
  775. DBGP ( "rtl_set_rx_tx_config_registers\n" );
  776. cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  777. RTL_W32(RxConfig, cfg);
  778. /* Set DMA burst size and Interframe Gap Time */
  779. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  780. (InterFrameGap << TxInterFrameGapShift));
  781. }
  782. static void rtl_soft_reset ( struct net_device *dev )
  783. {
  784. struct rtl8169_private *tp = netdev_priv(dev);
  785. void *ioaddr = tp->mmio_addr;
  786. unsigned int i;
  787. DBGP ( "rtl_hw_soft_reset\n" );
  788. /* Soft reset the chip. */
  789. RTL_W8(ChipCmd, CmdReset);
  790. /* Check that the chip has finished the reset. */
  791. for (i = 0; i < 100; i++) {
  792. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  793. break;
  794. mdelay ( 1 );
  795. }
  796. if ( i == 100 ) {
  797. DBG ( "Reset Failed! (> 100 iterations)\n" );
  798. }
  799. }
  800. static void rtl_hw_start ( struct net_device *dev )
  801. {
  802. struct rtl8169_private *tp = netdev_priv ( dev );
  803. DBGP ( "rtl_hw_start\n" );
  804. /* Soft reset NIC */
  805. rtl_soft_reset ( dev );
  806. tp->hw_start ( dev );
  807. }
  808. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  809. void *ioaddr)
  810. {
  811. DBGP ( "rtl_set_rx_tx_desc_registers\n" );
  812. /*
  813. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  814. * register to be written before TxDescAddrLow to work.
  815. * Switching from MMIO to I/O access fixes the issue as well.
  816. */
  817. RTL_W32 ( TxDescStartAddrHigh, 0 );
  818. RTL_W32 ( TxDescStartAddrLow, virt_to_bus ( tp->tx_base ) );
  819. RTL_W32 ( RxDescAddrHigh, 0 );
  820. RTL_W32 ( RxDescAddrLow, virt_to_bus ( tp->rx_base ) );
  821. }
  822. static u16 rtl_rw_cpluscmd(void *ioaddr)
  823. {
  824. u16 cmd;
  825. DBGP ( "rtl_rw_cpluscmd\n" );
  826. cmd = RTL_R16(CPlusCmd);
  827. RTL_W16(CPlusCmd, cmd);
  828. return cmd;
  829. }
  830. static void rtl_set_rx_max_size(void *ioaddr)
  831. {
  832. DBGP ( "rtl_set_rx_max_size\n" );
  833. RTL_W16 ( RxMaxSize, RX_BUF_SIZE );
  834. }
  835. static void rtl8169_set_magic_reg(void *ioaddr, unsigned mac_version)
  836. {
  837. struct {
  838. u32 mac_version;
  839. u32 clk;
  840. u32 val;
  841. } cfg2_info [] = {
  842. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  843. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  844. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  845. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  846. }, *p = cfg2_info;
  847. unsigned int i;
  848. u32 clk;
  849. DBGP ( "rtl8169_set_magic_reg\n" );
  850. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  851. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  852. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  853. RTL_W32(0x7c, p->val);
  854. break;
  855. }
  856. }
  857. }
  858. static void rtl_set_rx_mode ( struct net_device *netdev )
  859. {
  860. struct rtl8169_private *tp = netdev_priv ( netdev );
  861. void *ioaddr = tp->mmio_addr;
  862. u32 tmp;
  863. DBGP ( "rtl_set_rx_mode\n" );
  864. /* Accept all Multicast Packets */
  865. RTL_W32 ( MAR0 + 0, 0xffffffff );
  866. RTL_W32 ( MAR0 + 4, 0xffffffff );
  867. tmp = rtl8169_rx_config | AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  868. ( RTL_R32 ( RxConfig ) & rtl_chip_info[tp->chipset].RxConfigMask );
  869. RTL_W32 ( RxConfig, tmp );
  870. }
  871. static void rtl_hw_start_8169(struct net_device *dev)
  872. {
  873. struct rtl8169_private *tp = netdev_priv(dev);
  874. void *ioaddr = tp->mmio_addr;
  875. struct pci_device *pdev = tp->pci_dev;
  876. DBGP ( "rtl_hw_start_8169\n" );
  877. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  878. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  879. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  880. }
  881. RTL_W8(Cfg9346, Cfg9346_Unlock);
  882. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  883. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  884. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  885. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  886. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  887. RTL_W8(EarlyTxThres, EarlyTxThld);
  888. rtl_set_rx_max_size(ioaddr);
  889. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  890. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  891. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  892. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  893. rtl_set_rx_tx_config_registers(tp);
  894. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  895. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  896. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  897. DBG ( "Set MAC Reg C+CR Offset 0xE0. "
  898. "Bit-3 and bit-14 MUST be 1\n" );
  899. tp->cp_cmd |= (1 << 14);
  900. }
  901. RTL_W16(CPlusCmd, tp->cp_cmd);
  902. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  903. /*
  904. * Undocumented corner. Supposedly:
  905. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  906. */
  907. RTL_W16(IntrMitigate, 0x0000);
  908. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  909. if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
  910. (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
  911. (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
  912. (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
  913. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  914. rtl_set_rx_tx_config_registers(tp);
  915. }
  916. RTL_W8(Cfg9346, Cfg9346_Lock);
  917. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  918. RTL_R8(IntrMask);
  919. RTL_W32(RxMissed, 0);
  920. rtl_set_rx_mode(dev);
  921. /* no early-rx interrupts */
  922. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  923. // RTL_W16(IntrMask, tp->intr_event);
  924. }
  925. static void rtl_tx_performance_tweak(struct pci_device *pdev, u16 force)
  926. {
  927. struct net_device *dev = pci_get_drvdata(pdev);
  928. struct rtl8169_private *tp = netdev_priv(dev);
  929. int cap = tp->pcie_cap;
  930. DBGP ( "rtl_tx_performance_tweak\n" );
  931. if (cap) {
  932. u16 ctl;
  933. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  934. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  935. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  936. }
  937. }
  938. static void rtl_csi_access_enable(void *ioaddr)
  939. {
  940. u32 csi;
  941. DBGP ( "rtl_csi_access_enable\n" );
  942. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  943. rtl_csi_write(ioaddr, 0x070c, csi | 0x27000000);
  944. }
  945. struct ephy_info {
  946. unsigned int offset;
  947. u16 mask;
  948. u16 bits;
  949. };
  950. static void rtl_ephy_init(void *ioaddr, struct ephy_info *e, int len)
  951. {
  952. u16 w;
  953. DBGP ( "rtl_ephy_init\n" );
  954. while (len-- > 0) {
  955. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  956. rtl_ephy_write(ioaddr, e->offset, w);
  957. e++;
  958. }
  959. }
  960. static void rtl_disable_clock_request(struct pci_device *pdev)
  961. {
  962. struct net_device *dev = pci_get_drvdata(pdev);
  963. struct rtl8169_private *tp = netdev_priv(dev);
  964. int cap = tp->pcie_cap;
  965. DBGP ( "rtl_disable_clock_request\n" );
  966. if (cap) {
  967. u16 ctl;
  968. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  969. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  970. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  971. }
  972. }
  973. #define R8168_CPCMD_QUIRK_MASK (\
  974. EnableBist | \
  975. Mac_dbgo_oe | \
  976. Force_half_dup | \
  977. Force_rxflow_en | \
  978. Force_txflow_en | \
  979. Cxpl_dbg_sel | \
  980. ASF | \
  981. PktCntrDisable | \
  982. Mac_dbgo_sel)
  983. static void rtl_hw_start_8168bb(void *ioaddr, struct pci_device *pdev)
  984. {
  985. DBGP ( "rtl_hw_start_8168bb\n" );
  986. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  987. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  988. rtl_tx_performance_tweak(pdev,
  989. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  990. }
  991. static void rtl_hw_start_8168bef(void *ioaddr, struct pci_device *pdev)
  992. {
  993. DBGP ( "rtl_hw_start_8168bef\n" );
  994. rtl_hw_start_8168bb(ioaddr, pdev);
  995. RTL_W8(EarlyTxThres, EarlyTxThld);
  996. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  997. }
  998. static void __rtl_hw_start_8168cp(void *ioaddr, struct pci_device *pdev)
  999. {
  1000. DBGP ( "__rtl_hw_start_8168cp\n" );
  1001. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  1002. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  1003. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  1004. rtl_disable_clock_request(pdev);
  1005. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  1006. }
  1007. static void rtl_hw_start_8168cp_1(void *ioaddr, struct pci_device *pdev)
  1008. {
  1009. static struct ephy_info e_info_8168cp[] = {
  1010. { 0x01, 0, 0x0001 },
  1011. { 0x02, 0x0800, 0x1000 },
  1012. { 0x03, 0, 0x0042 },
  1013. { 0x06, 0x0080, 0x0000 },
  1014. { 0x07, 0, 0x2000 }
  1015. };
  1016. DBGP ( "rtl_hw_start_8168cp_1\n" );
  1017. rtl_csi_access_enable(ioaddr);
  1018. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  1019. __rtl_hw_start_8168cp(ioaddr, pdev);
  1020. }
  1021. static void rtl_hw_start_8168cp_2(void *ioaddr, struct pci_device *pdev)
  1022. {
  1023. DBGP ( "rtl_hw_start_8168cp_2\n" );
  1024. rtl_csi_access_enable(ioaddr);
  1025. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  1026. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  1027. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  1028. }
  1029. static void rtl_hw_start_8168cp_3(void *ioaddr, struct pci_device *pdev)
  1030. {
  1031. DBGP ( "rtl_hw_start_8168cp_3\n" );
  1032. rtl_csi_access_enable(ioaddr);
  1033. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  1034. /* Magic. */
  1035. RTL_W8(DBG_REG, 0x20);
  1036. RTL_W8(EarlyTxThres, EarlyTxThld);
  1037. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  1038. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  1039. }
  1040. static void rtl_hw_start_8168c_1(void *ioaddr, struct pci_device *pdev)
  1041. {
  1042. static struct ephy_info e_info_8168c_1[] = {
  1043. { 0x02, 0x0800, 0x1000 },
  1044. { 0x03, 0, 0x0002 },
  1045. { 0x06, 0x0080, 0x0000 }
  1046. };
  1047. DBGP ( "rtl_hw_start_8168c_1\n" );
  1048. rtl_csi_access_enable(ioaddr);
  1049. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  1050. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  1051. __rtl_hw_start_8168cp(ioaddr, pdev);
  1052. }
  1053. static void rtl_hw_start_8168c_2(void *ioaddr, struct pci_device *pdev)
  1054. {
  1055. static struct ephy_info e_info_8168c_2[] = {
  1056. { 0x01, 0, 0x0001 },
  1057. { 0x03, 0x0400, 0x0220 }
  1058. };
  1059. DBGP ( "rtl_hw_start_8168c_2\n" );
  1060. rtl_csi_access_enable(ioaddr);
  1061. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  1062. __rtl_hw_start_8168cp(ioaddr, pdev);
  1063. }
  1064. static void rtl_hw_start_8168c_3(void *ioaddr, struct pci_device *pdev)
  1065. {
  1066. DBGP ( "rtl_hw_start_8168c_3\n" );
  1067. rtl_hw_start_8168c_2(ioaddr, pdev);
  1068. }
  1069. static void rtl_hw_start_8168c_4(void *ioaddr, struct pci_device *pdev)
  1070. {
  1071. DBGP ( "rtl_hw_start_8168c_4\n" );
  1072. rtl_csi_access_enable(ioaddr);
  1073. __rtl_hw_start_8168cp(ioaddr, pdev);
  1074. }
  1075. static void rtl_hw_start_8168d(void *ioaddr, struct pci_device *pdev)
  1076. {
  1077. DBGP ( "rtl_hw_start_8168d\n" );
  1078. rtl_csi_access_enable(ioaddr);
  1079. rtl_disable_clock_request(pdev);
  1080. RTL_W8(EarlyTxThres, EarlyTxThld);
  1081. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  1082. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  1083. }
  1084. static void rtl_hw_start_8168(struct net_device *dev)
  1085. {
  1086. struct rtl8169_private *tp = netdev_priv(dev);
  1087. void *ioaddr = tp->mmio_addr;
  1088. struct pci_device *pdev = tp->pci_dev;
  1089. DBGP ( "rtl_hw_start_8168\n" );
  1090. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1091. RTL_W8(EarlyTxThres, EarlyTxThld);
  1092. rtl_set_rx_max_size(ioaddr);
  1093. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  1094. RTL_W16(CPlusCmd, tp->cp_cmd);
  1095. RTL_W16(IntrMitigate, 0x5151);
  1096. /* Work around for RxFIFO overflow. */
  1097. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  1098. tp->intr_event |= RxFIFOOver | PCSTimeout;
  1099. tp->intr_event &= ~RxOverflow;
  1100. }
  1101. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  1102. rtl_set_rx_mode(dev);
  1103. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  1104. (InterFrameGap << TxInterFrameGapShift));
  1105. RTL_R8(IntrMask);
  1106. switch (tp->mac_version) {
  1107. case RTL_GIGA_MAC_VER_11:
  1108. rtl_hw_start_8168bb(ioaddr, pdev);
  1109. break;
  1110. case RTL_GIGA_MAC_VER_12:
  1111. case RTL_GIGA_MAC_VER_17:
  1112. rtl_hw_start_8168bef(ioaddr, pdev);
  1113. break;
  1114. case RTL_GIGA_MAC_VER_18:
  1115. rtl_hw_start_8168cp_1(ioaddr, pdev);
  1116. break;
  1117. case RTL_GIGA_MAC_VER_19:
  1118. rtl_hw_start_8168c_1(ioaddr, pdev);
  1119. break;
  1120. case RTL_GIGA_MAC_VER_20:
  1121. rtl_hw_start_8168c_2(ioaddr, pdev);
  1122. break;
  1123. case RTL_GIGA_MAC_VER_21:
  1124. rtl_hw_start_8168c_3(ioaddr, pdev);
  1125. break;
  1126. case RTL_GIGA_MAC_VER_22:
  1127. rtl_hw_start_8168c_4(ioaddr, pdev);
  1128. break;
  1129. case RTL_GIGA_MAC_VER_23:
  1130. rtl_hw_start_8168cp_2(ioaddr, pdev);
  1131. break;
  1132. case RTL_GIGA_MAC_VER_24:
  1133. rtl_hw_start_8168cp_3(ioaddr, pdev);
  1134. break;
  1135. case RTL_GIGA_MAC_VER_25:
  1136. rtl_hw_start_8168d(ioaddr, pdev);
  1137. break;
  1138. default:
  1139. DBG ( "Unknown chipset (mac_version = %d).\n",
  1140. tp->mac_version );
  1141. break;
  1142. }
  1143. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1144. RTL_W8(Cfg9346, Cfg9346_Lock);
  1145. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  1146. // RTL_W16(IntrMask, tp->intr_event);
  1147. }
  1148. #define R810X_CPCMD_QUIRK_MASK (\
  1149. EnableBist | \
  1150. Mac_dbgo_oe | \
  1151. Force_half_dup | \
  1152. Force_half_dup | \
  1153. Force_txflow_en | \
  1154. Cxpl_dbg_sel | \
  1155. ASF | \
  1156. PktCntrDisable | \
  1157. PCIDAC | \
  1158. PCIMulRW)
  1159. static void rtl_hw_start_8102e_1(void *ioaddr, struct pci_device *pdev)
  1160. {
  1161. static struct ephy_info e_info_8102e_1[] = {
  1162. { 0x01, 0, 0x6e65 },
  1163. { 0x02, 0, 0x091f },
  1164. { 0x03, 0, 0xc2f9 },
  1165. { 0x06, 0, 0xafb5 },
  1166. { 0x07, 0, 0x0e00 },
  1167. { 0x19, 0, 0xec80 },
  1168. { 0x01, 0, 0x2e65 },
  1169. { 0x01, 0, 0x6e65 }
  1170. };
  1171. u8 cfg1;
  1172. DBGP ( "rtl_hw_start_8102e_1\n" );
  1173. rtl_csi_access_enable(ioaddr);
  1174. RTL_W8(DBG_REG, FIX_NAK_1);
  1175. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  1176. RTL_W8(Config1,
  1177. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  1178. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  1179. cfg1 = RTL_R8(Config1);
  1180. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  1181. RTL_W8(Config1, cfg1 & ~LEDS0);
  1182. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  1183. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  1184. }
  1185. static void rtl_hw_start_8102e_2(void *ioaddr, struct pci_device *pdev)
  1186. {
  1187. DBGP ( "rtl_hw_start_8102e_2\n" );
  1188. rtl_csi_access_enable(ioaddr);
  1189. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  1190. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  1191. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  1192. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  1193. }
  1194. static void rtl_hw_start_8102e_3(void *ioaddr, struct pci_device *pdev)
  1195. {
  1196. DBGP ( "rtl_hw_start_8102e_3\n" );
  1197. rtl_hw_start_8102e_2(ioaddr, pdev);
  1198. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  1199. }
  1200. static void rtl_hw_start_8101(struct net_device *dev)
  1201. {
  1202. struct rtl8169_private *tp = netdev_priv(dev);
  1203. void *ioaddr = tp->mmio_addr;
  1204. struct pci_device *pdev = tp->pci_dev;
  1205. DBGP ( "rtl_hw_start_8101\n" );
  1206. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  1207. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  1208. int cap = tp->pcie_cap;
  1209. if (cap) {
  1210. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  1211. PCI_EXP_DEVCTL_NOSNOOP_EN);
  1212. }
  1213. }
  1214. switch (tp->mac_version) {
  1215. case RTL_GIGA_MAC_VER_07:
  1216. rtl_hw_start_8102e_1(ioaddr, pdev);
  1217. break;
  1218. case RTL_GIGA_MAC_VER_08:
  1219. rtl_hw_start_8102e_3(ioaddr, pdev);
  1220. break;
  1221. case RTL_GIGA_MAC_VER_09:
  1222. rtl_hw_start_8102e_2(ioaddr, pdev);
  1223. break;
  1224. }
  1225. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1226. RTL_W8(EarlyTxThres, EarlyTxThld);
  1227. rtl_set_rx_max_size(ioaddr);
  1228. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  1229. RTL_W16(CPlusCmd, tp->cp_cmd);
  1230. RTL_W16(IntrMitigate, 0x0000);
  1231. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  1232. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1233. rtl_set_rx_tx_config_registers(tp);
  1234. RTL_W8(Cfg9346, Cfg9346_Lock);
  1235. RTL_R8(IntrMask);
  1236. rtl_set_rx_mode(dev);
  1237. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1238. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  1239. // RTL_W16(IntrMask, tp->intr_event);
  1240. }
  1241. /*** iPXE API Support Routines ***/
  1242. /**
  1243. * setup_tx_resources - allocate tx resources (descriptors)
  1244. *
  1245. * @v tp Driver private storage
  1246. *
  1247. * @ret rc Returns 0 on success, negative on failure
  1248. **/
  1249. static int
  1250. rtl8169_setup_tx_resources ( struct rtl8169_private *tp )
  1251. {
  1252. DBGP ( "rtl8169_setup_tx_resources\n" );
  1253. tp->tx_base = malloc_dma ( R8169_TX_RING_BYTES, TX_RING_ALIGN );
  1254. if ( ! tp->tx_base ) {
  1255. return -ENOMEM;
  1256. }
  1257. memset ( tp->tx_base, 0, R8169_TX_RING_BYTES );
  1258. DBG ( "tp->tx_base = %#08lx\n", virt_to_bus ( tp->tx_base ) );
  1259. tp->tx_fill_ctr = 0;
  1260. tp->tx_curr = 0;
  1261. tp->tx_tail = 0;
  1262. return 0;
  1263. }
  1264. static void
  1265. rtl8169_process_tx_packets ( struct net_device *netdev )
  1266. {
  1267. struct rtl8169_private *tp = netdev_priv ( netdev );
  1268. uint32_t tx_status;
  1269. struct TxDesc *tx_curr_desc;
  1270. DBGP ( "rtl8169_process_tx_packets\n" );
  1271. while ( tp->tx_tail != tp->tx_curr ) {
  1272. tx_curr_desc = tp->tx_base + tp->tx_tail;
  1273. tx_status = tx_curr_desc->opts1;
  1274. DBG2 ( "Before DescOwn check tx_status: %#08x\n", tx_status );
  1275. /* if the packet at tx_tail is not owned by hardware it is for us */
  1276. if ( tx_status & DescOwn )
  1277. break;
  1278. DBG ( "Transmitted packet.\n" );
  1279. DBG ( "tp->tx_fill_ctr = %d\n", tp->tx_fill_ctr );
  1280. DBG ( "tp->tx_tail = %d\n", tp->tx_tail );
  1281. DBG ( "tp->tx_curr = %d\n", tp->tx_curr );
  1282. DBG ( "tx_status = %d\n", tx_status );
  1283. DBG ( "tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
  1284. /* Pass packet to core for processing */
  1285. netdev_tx_complete ( netdev, tp->tx_iobuf[tp->tx_tail] );
  1286. memset ( tx_curr_desc, 0, sizeof ( *tx_curr_desc ) );
  1287. /* Decrement count of used descriptors */
  1288. tp->tx_fill_ctr--;
  1289. /* Increment sent packets index */
  1290. tp->tx_tail = ( tp->tx_tail + 1 ) % NUM_TX_DESC;
  1291. }
  1292. }
  1293. static void
  1294. rtl8169_free_tx_resources ( struct rtl8169_private *tp )
  1295. {
  1296. DBGP ( "rtl8169_free_tx_resources\n" );
  1297. free_dma ( tp->tx_base, R8169_TX_RING_BYTES );
  1298. }
  1299. static void
  1300. rtl8169_populate_rx_descriptor ( struct rtl8169_private *tp, struct RxDesc *rx_desc, uint32_t index )
  1301. {
  1302. DBGP ( "rtl8169_populate_rx_descriptor\n" );
  1303. DBG ( "Populating rx descriptor %d\n", index );
  1304. memset ( rx_desc, 0, sizeof ( *rx_desc ) );
  1305. rx_desc->addr_hi = 0;
  1306. rx_desc->addr_lo = virt_to_bus ( tp->rx_iobuf[index]->data );
  1307. rx_desc->opts2 = 0;
  1308. rx_desc->opts1 = ( index == ( NUM_RX_DESC - 1 ) ? RingEnd : 0 ) |
  1309. RX_BUF_SIZE;
  1310. rx_desc->opts1 |= DescOwn;
  1311. }
  1312. /**
  1313. * Refill descriptor ring
  1314. *
  1315. * @v netdev Net device
  1316. */
  1317. static void rtl8169_refill_rx_ring ( struct rtl8169_private *tp )
  1318. {
  1319. struct RxDesc *rx_curr_desc;
  1320. int i;
  1321. DBGP ( "rtl8169_refill_rx_ring\n" );
  1322. for ( i = 0; i < NUM_RX_DESC; i++ ) {
  1323. rx_curr_desc = ( tp->rx_base ) + i;
  1324. /* Don't touch descriptors owned by the NIC */
  1325. if ( rx_curr_desc->opts1 & DescOwn )
  1326. continue;
  1327. /* Don't touch descriptors with iobufs, they still need to be
  1328. processed by the poll routine */
  1329. if ( tp->rx_iobuf[tp->rx_curr] != NULL )
  1330. continue;
  1331. /** If we can't get an iobuf for this descriptor
  1332. try again later (next poll).
  1333. */
  1334. if ( ! ( tp->rx_iobuf[i] = alloc_iob ( RX_BUF_SIZE ) ) ) {
  1335. DBG ( "Refill rx ring failed!!\n" );
  1336. break;
  1337. }
  1338. rtl8169_populate_rx_descriptor ( tp, rx_curr_desc, i );
  1339. }
  1340. }
  1341. /**
  1342. * setup_rx_resources - allocate Rx resources (Descriptors)
  1343. *
  1344. * @v tp: Driver private structure
  1345. *
  1346. * @ret rc Returns 0 on success, negative on failure
  1347. *
  1348. **/
  1349. static int
  1350. rtl8169_setup_rx_resources ( struct rtl8169_private *tp )
  1351. {
  1352. DBGP ( "rtl8169_setup_rx_resources\n" );
  1353. tp->rx_base = malloc_dma ( R8169_RX_RING_BYTES, RX_RING_ALIGN );
  1354. DBG ( "tp->rx_base = %#08lx\n", virt_to_bus ( tp->rx_base ) );
  1355. if ( ! tp->rx_base ) {
  1356. return -ENOMEM;
  1357. }
  1358. memset ( tp->rx_base, 0, R8169_RX_RING_BYTES );
  1359. rtl8169_refill_rx_ring ( tp );
  1360. tp->rx_curr = 0;
  1361. return 0;
  1362. }
  1363. static void
  1364. rtl8169_process_rx_packets ( struct net_device *netdev )
  1365. {
  1366. struct rtl8169_private *tp = netdev_priv ( netdev );
  1367. uint32_t rx_status;
  1368. uint16_t rx_len;
  1369. struct RxDesc *rx_curr_desc;
  1370. int i;
  1371. DBGP ( "rtl8169_process_rx_packets\n" );
  1372. for ( i = 0; i < NUM_RX_DESC; i++ ) {
  1373. rx_curr_desc = tp->rx_base + tp->rx_curr;
  1374. rx_status = rx_curr_desc->opts1;
  1375. DBG2 ( "Before DescOwn check rx_status: %#08x\n", rx_status );
  1376. /* Hardware still owns the descriptor */
  1377. if ( rx_status & DescOwn )
  1378. break;
  1379. /* We own the descriptor, but it has not been refilled yet */
  1380. if ( tp->rx_iobuf[tp->rx_curr] == NULL )
  1381. break;
  1382. rx_len = rx_status & 0x3fff;
  1383. DBG ( "Received packet.\n" );
  1384. DBG ( "tp->rx_curr = %d\n", tp->rx_curr );
  1385. DBG ( "rx_len = %d\n", rx_len );
  1386. DBG ( "rx_status = %#08x\n", rx_status );
  1387. DBG ( "rx_curr_desc = %#08lx\n", virt_to_bus ( rx_curr_desc ) );
  1388. if ( rx_status & RxRES ) {
  1389. netdev_rx_err ( netdev, tp->rx_iobuf[tp->rx_curr], -EINVAL );
  1390. DBG ( "rtl8169_poll: Corrupted packet received!\n"
  1391. " rx_status: %#08x\n", rx_status );
  1392. } else {
  1393. /* Adjust size of the iobuf to reflect received data */
  1394. iob_put ( tp->rx_iobuf[tp->rx_curr], rx_len );
  1395. /* Add this packet to the receive queue. */
  1396. netdev_rx ( netdev, tp->rx_iobuf[tp->rx_curr] );
  1397. }
  1398. /* Invalidate this iobuf and descriptor */
  1399. tp->rx_iobuf[tp->rx_curr] = NULL;
  1400. memset ( rx_curr_desc, 0, sizeof ( *rx_curr_desc ) );
  1401. /* Update pointer to next available rx descriptor */
  1402. tp->rx_curr = ( tp->rx_curr + 1 ) % NUM_RX_DESC;
  1403. }
  1404. rtl8169_refill_rx_ring ( tp );
  1405. }
  1406. static void
  1407. rtl8169_free_rx_resources ( struct rtl8169_private *tp )
  1408. {
  1409. int i;
  1410. DBGP ( "rtl8169_free_rx_resources\n" );
  1411. free_dma ( tp->rx_base, R8169_RX_RING_BYTES );
  1412. for ( i = 0; i < NUM_RX_DESC; i++ ) {
  1413. free_iob ( tp->rx_iobuf[i] );
  1414. tp->rx_iobuf[i] = NULL;
  1415. }
  1416. }
  1417. static void rtl8169_irq_enable ( struct rtl8169_private *tp )
  1418. {
  1419. void *ioaddr = tp->mmio_addr;
  1420. DBGP ( "rtl8169_irq_enable\n" );
  1421. RTL_W16 ( IntrMask, tp->intr_event );
  1422. }
  1423. static void rtl8169_irq_disable ( struct rtl8169_private *tp )
  1424. {
  1425. void *ioaddr = tp->mmio_addr;
  1426. DBGP ( "rtl8169_irq_disable\n" );
  1427. RTL_W16 ( IntrMask, 0x0000 );
  1428. }
  1429. /*** iPXE Core API Routines ***/
  1430. /**
  1431. * open - Called when a network interface is made active
  1432. *
  1433. * @v netdev network interface device structure
  1434. * @ret rc Return status code, 0 on success, negative value on failure
  1435. *
  1436. **/
  1437. static int
  1438. rtl8169_open ( struct net_device *netdev )
  1439. {
  1440. struct rtl8169_private *tp = netdev_priv ( netdev );
  1441. void *ioaddr = tp->mmio_addr;
  1442. int rc;
  1443. DBGP ( "rtl8169_open\n" );
  1444. /* allocate transmit descriptors */
  1445. rc = rtl8169_setup_tx_resources ( tp );
  1446. if ( rc ) {
  1447. DBG ( "Error setting up TX resources!\n" );
  1448. goto err_setup_tx;
  1449. }
  1450. /* allocate receive descriptors */
  1451. rc = rtl8169_setup_rx_resources ( tp );
  1452. if ( rc ) {
  1453. DBG ( "Error setting up RX resources!\n" );
  1454. goto err_setup_rx;
  1455. }
  1456. rtl_hw_start ( netdev );
  1457. DBG ( "TxDescStartAddrHigh = %#08lx\n", RTL_R32 ( TxDescStartAddrHigh ) );
  1458. DBG ( "TxDescStartAddrLow = %#08lx\n", RTL_R32 ( TxDescStartAddrLow ) );
  1459. DBG ( "RxDescAddrHigh = %#08lx\n", RTL_R32 ( RxDescAddrHigh ) );
  1460. DBG ( "RxDescAddrLow = %#08lx\n", RTL_R32 ( RxDescAddrLow ) );
  1461. return 0;
  1462. err_setup_rx:
  1463. rtl8169_free_tx_resources ( tp );
  1464. err_setup_tx:
  1465. rtl8169_hw_reset ( ioaddr );
  1466. return rc;
  1467. }
  1468. /**
  1469. * transmit - Transmit a packet
  1470. *
  1471. * @v netdev Network device
  1472. * @v iobuf I/O buffer
  1473. *
  1474. * @ret rc Returns 0 on success, negative on failure
  1475. */
  1476. static int
  1477. rtl8169_transmit ( struct net_device *netdev, struct io_buffer *iobuf )
  1478. {
  1479. struct rtl8169_private *tp = netdev_priv ( netdev );
  1480. void *ioaddr = tp->mmio_addr;
  1481. uint32_t tx_len = iob_len ( iobuf );
  1482. struct TxDesc *tx_curr_desc;
  1483. DBGP ("rtl8169_transmit\n");
  1484. if ( tp->tx_fill_ctr == NUM_TX_DESC ) {
  1485. DBG ("TX overflow\n");
  1486. return -ENOBUFS;
  1487. }
  1488. /**
  1489. * The rtl8169 family automatically pads short packets to a
  1490. * minimum size, but if it did not, like some older cards,
  1491. * we could do:
  1492. * iob_pad ( iobuf, ETH_ZLEN );
  1493. */
  1494. /* Save pointer to this iobuf we have been given to transmit so
  1495. we can pass it to netdev_tx_complete() later */
  1496. tp->tx_iobuf[tp->tx_curr] = iobuf;
  1497. tx_curr_desc = tp->tx_base + tp->tx_curr;
  1498. DBG ( "tp->tx_fill_ctr = %d\n", tp->tx_fill_ctr );
  1499. DBG ( "tp->tx_curr = %d\n", tp->tx_curr );
  1500. DBG ( "tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
  1501. DBG ( "iobuf->data = %#08lx\n", virt_to_bus ( iobuf->data ) );
  1502. DBG ( "tx_len = %d\n", tx_len );
  1503. /* Configure current descriptor to transmit supplied packet */
  1504. tx_curr_desc->addr_hi = 0;
  1505. tx_curr_desc->addr_lo = virt_to_bus ( iobuf->data );
  1506. tx_curr_desc->opts2 = 0;
  1507. tx_curr_desc->opts1 = FirstFrag | LastFrag |
  1508. ( tp->tx_curr == ( NUM_TX_DESC - 1 ) ? RingEnd : 0 ) |
  1509. tx_len;
  1510. /* Mark descriptor as owned by NIC */
  1511. tx_curr_desc->opts1 |= DescOwn;
  1512. DBG ( "tx_curr_desc->opts1 = %#08x\n", tx_curr_desc->opts1 );
  1513. DBG ( "tx_curr_desc->opts2 = %#08x\n", tx_curr_desc->opts2 );
  1514. DBG ( "tx_curr_desc->addr_hi = %#08x\n", tx_curr_desc->addr_hi );
  1515. DBG ( "tx_curr_desc->addr_lo = %#08x\n", tx_curr_desc->addr_lo );
  1516. RTL_W8 ( TxPoll, NPQ ); /* set polling bit */
  1517. /* Point to next free descriptor */
  1518. tp->tx_curr = ( tp->tx_curr + 1 ) % NUM_TX_DESC;
  1519. /* Increment number of tx descriptors in use */
  1520. tp->tx_fill_ctr++;
  1521. return 0;
  1522. }
  1523. /**
  1524. * poll - Poll for received packets
  1525. *
  1526. * @v netdev Network device
  1527. */
  1528. static void
  1529. rtl8169_poll ( struct net_device *netdev )
  1530. {
  1531. struct rtl8169_private *tp = netdev_priv ( netdev );
  1532. void *ioaddr = tp->mmio_addr;
  1533. uint16_t intr_status;
  1534. uint16_t intr_mask;
  1535. DBGP ( "rtl8169_poll\n" );
  1536. intr_status = RTL_R16 ( IntrStatus );
  1537. intr_mask = RTL_R16 ( IntrMask );
  1538. DBG2 ( "rtl8169_poll (before): intr_mask = %#04x intr_status = %#04x\n",
  1539. intr_mask, intr_status );
  1540. RTL_W16 ( IntrStatus, 0xffff );
  1541. /* hotplug / major error / no more work / shared irq */
  1542. if ( intr_status == 0xffff )
  1543. return;
  1544. /* Process transmitted packets */
  1545. rtl8169_process_tx_packets ( netdev );
  1546. /* Process received packets */
  1547. rtl8169_process_rx_packets ( netdev );
  1548. }
  1549. /**
  1550. * close - Disable network interface
  1551. *
  1552. * @v netdev network interface device structure
  1553. *
  1554. **/
  1555. static void
  1556. rtl8169_close ( struct net_device *netdev )
  1557. {
  1558. struct rtl8169_private *tp = netdev_priv ( netdev );
  1559. void *ioaddr = tp->mmio_addr;
  1560. DBGP ( "r8169_close\n" );
  1561. rtl8169_hw_reset ( ioaddr );
  1562. rtl8169_free_tx_resources ( tp );
  1563. rtl8169_free_rx_resources ( tp );
  1564. }
  1565. /**
  1566. * irq - enable or Disable interrupts
  1567. *
  1568. * @v netdev network adapter
  1569. * @v action requested interrupt action
  1570. *
  1571. **/
  1572. static void
  1573. rtl8169_irq ( struct net_device *netdev, int action )
  1574. {
  1575. struct rtl8169_private *tp = netdev_priv ( netdev );
  1576. DBGP ( "rtl8169_irq\n" );
  1577. switch ( action ) {
  1578. case 0 :
  1579. rtl8169_irq_disable ( tp );
  1580. break;
  1581. default :
  1582. rtl8169_irq_enable ( tp );
  1583. break;
  1584. }
  1585. }
  1586. static struct net_device_operations rtl8169_operations = {
  1587. .open = rtl8169_open,
  1588. .transmit = rtl8169_transmit,
  1589. .poll = rtl8169_poll,
  1590. .close = rtl8169_close,
  1591. .irq = rtl8169_irq,
  1592. };
  1593. /**
  1594. * probe - Initial configuration of NIC
  1595. *
  1596. * @v pci PCI device
  1597. * @v id PCI IDs
  1598. *
  1599. * @ret rc Return status code
  1600. **/
  1601. static int
  1602. rtl8169_probe ( struct pci_device *pdev )
  1603. {
  1604. int i, rc;
  1605. struct net_device *netdev;
  1606. struct rtl8169_private *tp;
  1607. void *ioaddr;
  1608. const struct rtl_cfg_info *cfg = rtl_cfg_infos + pdev->id->driver_data;
  1609. DBGP ( "rtl8169_probe\n" );
  1610. DBG ( "id->vendor = %#04x, id->device = %#04x\n",
  1611. pdev->id->vendor, pdev->id->device );
  1612. DBG ( "cfg->intr_event = %#04x\n", cfg->intr_event );
  1613. rc = -ENOMEM;
  1614. /* Allocate net device ( also allocates memory for netdev->priv
  1615. and makes netdev-priv point to it )
  1616. */
  1617. netdev = alloc_etherdev ( sizeof ( *tp ) );
  1618. if ( ! netdev )
  1619. goto err_alloc_etherdev;
  1620. /* Associate driver-specific network operations with
  1621. generic network device layer
  1622. */
  1623. netdev_init ( netdev, &rtl8169_operations );
  1624. /* Associate this network device with the given PCI device */
  1625. pci_set_drvdata ( pdev, netdev );
  1626. netdev->dev = &pdev->dev;
  1627. /* Initialize driver private storage */
  1628. tp = netdev_priv ( netdev );
  1629. memset ( tp, 0, ( sizeof ( *tp ) ) );
  1630. tp->pci_dev = pdev;
  1631. tp->irqno = pdev->irq;
  1632. tp->netdev = netdev;
  1633. tp->intr_event = cfg->intr_event;
  1634. tp->cp_cmd = PCIMulRW;
  1635. tp->hw_start = cfg->hw_start;
  1636. rc = -EIO;
  1637. adjust_pci_device ( pdev );
  1638. /* ioremap MMIO region */
  1639. ioaddr = ioremap ( pdev->membase, R8169_REGS_SIZE );
  1640. if ( ! ioaddr ) {
  1641. DBG ( "cannot remap MMIO\n" );
  1642. rc = -EIO;
  1643. goto err_ioremap;
  1644. }
  1645. tp->mmio_addr = ioaddr;
  1646. tp->pcie_cap = pci_find_capability ( pdev, PCI_CAP_ID_EXP );
  1647. if ( tp->pcie_cap ) {
  1648. DBG ( "PCI Express capability\n" );
  1649. } else {
  1650. DBG ( "No PCI Express capability\n" );
  1651. }
  1652. /* Mask interrupts just in case */
  1653. rtl8169_irq_mask_and_ack ( ioaddr );
  1654. /* Soft reset NIC */
  1655. rtl_soft_reset ( netdev );
  1656. /* Identify chip attached to board */
  1657. rtl8169_get_mac_version ( tp, ioaddr );
  1658. for ( i = 0; (u32) i < ARRAY_SIZE ( rtl_chip_info ); i++ ) {
  1659. if ( tp->mac_version == rtl_chip_info[i].mac_version )
  1660. break;
  1661. }
  1662. if ( i == ARRAY_SIZE(rtl_chip_info ) ) {
  1663. /* Unknown chip: assume array element #0, original RTL-8169 */
  1664. DBG ( "Unknown chip version, assuming %s\n", rtl_chip_info[0].name );
  1665. i = 0;
  1666. }
  1667. tp->chipset = i;
  1668. if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
  1669. (RTL_R8(PHYstatus) & TBI_Enable)) {
  1670. tp->set_speed = rtl8169_set_speed_tbi;
  1671. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  1672. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  1673. tp->link_ok = rtl8169_tbi_link_ok;
  1674. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  1675. } else {
  1676. tp->set_speed = rtl8169_set_speed_xmii;
  1677. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  1678. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  1679. tp->link_ok = rtl8169_xmii_link_ok;
  1680. }
  1681. /* Get MAC address */
  1682. for ( i = 0; i < MAC_ADDR_LEN; i++ )
  1683. netdev->hw_addr[i] = RTL_R8 ( MAC0 + i );
  1684. DBG ( "%s\n", eth_ntoa ( netdev->hw_addr ) );
  1685. rtl8169_init_phy ( netdev, tp );
  1686. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  1687. goto err_register;
  1688. /* Mark as link up; we don't yet handle link state */
  1689. netdev_link_up ( netdev );
  1690. DBG ( "rtl8169_probe succeeded!\n" );
  1691. /* No errors, return success */
  1692. return 0;
  1693. /* Error return paths */
  1694. err_register:
  1695. err_ioremap:
  1696. netdev_put ( netdev );
  1697. err_alloc_etherdev:
  1698. return rc;
  1699. }
  1700. /**
  1701. * remove - Device Removal Routine
  1702. *
  1703. * @v pdev PCI device information struct
  1704. *
  1705. **/
  1706. static void
  1707. rtl8169_remove ( struct pci_device *pdev )
  1708. {
  1709. struct net_device *netdev = pci_get_drvdata ( pdev );
  1710. struct rtl8169_private *tp = netdev_priv ( netdev );
  1711. void *ioaddr = tp->mmio_addr;
  1712. DBGP ( "rtl8169_remove\n" );
  1713. rtl8169_hw_reset ( ioaddr );
  1714. unregister_netdev ( netdev );
  1715. netdev_nullify ( netdev );
  1716. netdev_put ( netdev );
  1717. }
  1718. static struct pci_device_id rtl8169_nics[] = {
  1719. PCI_ROM(0x10ec, 0x8129, "rtl8169-0x8129", "rtl8169-0x8129", RTL_CFG_0),
  1720. PCI_ROM(0x10ec, 0x8136, "rtl8169-0x8136", "rtl8169-0x8136", RTL_CFG_2),
  1721. PCI_ROM(0x10ec, 0x8167, "rtl8169-0x8167", "rtl8169-0x8167", RTL_CFG_0),
  1722. PCI_ROM(0x10ec, 0x8168, "rtl8169-0x8168", "rtl8169-0x8168", RTL_CFG_1),
  1723. PCI_ROM(0x10ec, 0x8169, "rtl8169-0x8169", "rtl8169-0x8169", RTL_CFG_0),
  1724. PCI_ROM(0x1186, 0x4300, "rtl8169-0x4300", "rtl8169-0x4300", RTL_CFG_0),
  1725. PCI_ROM(0x1259, 0xc107, "rtl8169-0xc107", "rtl8169-0xc107", RTL_CFG_0),
  1726. PCI_ROM(0x16ec, 0x0116, "rtl8169-0x0116", "rtl8169-0x0116", RTL_CFG_0),
  1727. PCI_ROM(0x1737, 0x1032, "rtl8169-0x1032", "rtl8169-0x1032", RTL_CFG_0),
  1728. PCI_ROM(0x0001, 0x8168, "rtl8169-0x8168", "rtl8169-0x8168", RTL_CFG_2),
  1729. };
  1730. struct pci_driver rtl8169_driver __pci_driver = {
  1731. .ids = rtl8169_nics,
  1732. .id_count = ( sizeof ( rtl8169_nics ) / sizeof ( rtl8169_nics[0] ) ),
  1733. .probe = rtl8169_probe,
  1734. .remove = rtl8169_remove,
  1735. };
  1736. /*
  1737. * Local variables:
  1738. * c-basic-offset: 8
  1739. * c-indent-level: 8
  1740. * tab-width: 8
  1741. * End:
  1742. */