Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

r8169.c 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /**************************************************************************
  2. * r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
  3. * Written 2003 by Timothy Legge <tlegge@rogers.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU 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. * Portions of this code based on:
  20. * r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
  21. * for Linux kernel 2.4.x.
  22. *
  23. * Written 2002 ShuChen <shuchen@realtek.com.tw>
  24. * See Linux Driver for full information
  25. *
  26. * Linux Driver Versions:
  27. * 1.27a, 10.02.2002
  28. * RTL8169_VERSION "2.2" <2004/08/09>
  29. *
  30. * Thanks to:
  31. * Jean Chen of RealTek Semiconductor Corp. for
  32. * providing the evaluation NIC used to develop
  33. * this driver. RealTek's support for Etherboot
  34. * is appreciated.
  35. *
  36. * REVISION HISTORY:
  37. * ================
  38. *
  39. * v1.0 11-26-2003 timlegge Initial port of Linux driver
  40. * v1.5 01-17-2004 timlegge Initial driver output cleanup
  41. * v1.6 03-27-2004 timlegge Additional Cleanup
  42. * v1.7 11-22-2005 timlegge Update to RealTek Driver Version 2.2
  43. *
  44. * 03-19-2008 Hilko Bengen Cleanups and fixes for newer cards
  45. * (successfully tested with 8110SC-d onboard NIC)
  46. *
  47. * Indent Options: indent -kr -i8
  48. ***************************************************************************/
  49. #include "etherboot.h"
  50. #include "nic.h"
  51. #include <gpxe/pci.h>
  52. #include <gpxe/ethernet.h>
  53. #include <gpxe/malloc.h>
  54. #define drv_version "v1.7+"
  55. #define drv_date "03-19-2008"
  56. #define HZ 1000
  57. static u32 ioaddr;
  58. /* Condensed operations for readability. */
  59. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  60. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  61. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  62. #undef RTL8169_DEBUG
  63. #undef RTL8169_JUMBO_FRAME_SUPPORT
  64. #undef RTL8169_HW_FLOW_CONTROL_SUPPORT
  65. #undef RTL8169_IOCTL_SUPPORT
  66. #undef RTL8169_DYNAMIC_CONTROL
  67. #define RTL8169_USE_IO
  68. #if 0
  69. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  70. static int max_interrupt_work = 20;
  71. #endif
  72. #if 0
  73. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  74. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  75. static int multicast_filter_limit = 32;
  76. #endif
  77. /* MAC address length*/
  78. #define MAC_ADDR_LEN 6
  79. /* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
  80. #define MAX_ETH_FRAME_SIZE 1536
  81. #define TX_FIFO_THRESH 256 /* In bytes */
  82. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  83. #define RX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
  84. #define TX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
  85. #define ETTh 0x3F /* 0x3F means NO threshold */
  86. #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
  87. #define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
  88. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  89. #define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
  90. #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
  91. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  92. #define RTL_MIN_IO_SIZE 0x80
  93. #define TX_TIMEOUT (6*HZ)
  94. #define RTL8169_TIMER_EXPIRE_TIME 100 //100
  95. #define ETH_HDR_LEN 14
  96. #define DEFAULT_MTU 1500
  97. #define DEFAULT_RX_BUF_LEN 1536
  98. #ifdef RTL8169_JUMBO_FRAME_SUPPORT
  99. #define MAX_JUMBO_FRAME_MTU ( 10000 )
  100. #define MAX_RX_SKBDATA_SIZE ( MAX_JUMBO_FRAME_MTU + ETH_HDR_LEN )
  101. #else
  102. #define MAX_RX_SKBDATA_SIZE 1600
  103. #endif //end #ifdef RTL8169_JUMBO_FRAME_SUPPORT
  104. #ifdef RTL8169_USE_IO
  105. #define RTL_W8(reg, val8) outb ((val8), ioaddr + (reg))
  106. #define RTL_W16(reg, val16) outw ((val16), ioaddr + (reg))
  107. #define RTL_W32(reg, val32) outl ((val32), ioaddr + (reg))
  108. #define RTL_R8(reg) inb (ioaddr + (reg))
  109. #define RTL_R16(reg) inw (ioaddr + (reg))
  110. #define RTL_R32(reg) ((unsigned long) inl (ioaddr + (reg)))
  111. #else
  112. /* write/read MMIO register */
  113. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  114. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  115. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  116. #define RTL_R8(reg) readb (ioaddr + (reg))
  117. #define RTL_R16(reg) readw (ioaddr + (reg))
  118. #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
  119. #endif
  120. enum mac_version {
  121. RTL_GIGA_MAC_VER_01 = 0x01, // 8169
  122. RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
  123. RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
  124. RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
  125. RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
  126. RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
  127. RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
  128. RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
  129. RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
  130. RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
  131. RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
  132. RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
  133. RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
  134. RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
  135. RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
  136. RTL_GIGA_MAC_VER_20 = 0x14 // 8168C
  137. };
  138. enum cfg_version {
  139. RTL_CFG_0 = 0x00,
  140. RTL_CFG_1,
  141. RTL_CFG_2
  142. };
  143. static struct {
  144. const char *name;
  145. u8 mac_version; /* depend on RTL8169 docs */
  146. u32 RxConfigMask; /* should clear the bits supported by this chip */
  147. } rtl_chip_info[] = {
  148. {"RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880}, // 8169
  149. {"RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880}, // 8169S
  150. {"RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880}, // 8110S
  151. {"RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880}, // 8169SB
  152. {"RTL8169sc/8110sc-d",RTL_GIGA_MAC_VER_05, 0xff7e1880}, // 8110SCd
  153. {"RTL8169sc/8110sc-e",RTL_GIGA_MAC_VER_06, 0xff7e1880}, // 8110SCe
  154. {"RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880}, // PCI-E
  155. {"RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880}, // PCI-E
  156. {"RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880}, // PCI-E 8139
  157. {"RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880}, // PCI-E 8139
  158. {"RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880}, // PCI-E 8139
  159. {"RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880}, // PCI-E
  160. {"RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880}, // PCI-E
  161. {"RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880}, // PCI-E
  162. {"RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880}, // PCI-E
  163. {"RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880}, // PCI-E
  164. };
  165. enum RTL8169_registers {
  166. MAC0 = 0x0, /* Ethernet hardware address. */
  167. MAR0 = 0x8, /* Multicast filter. */
  168. TxDescAddrLow = 0x20,
  169. TxDescAddrHigh = 0x24,
  170. TxHDescStartAddr = 0x28,
  171. FLASH = 0x30,
  172. ERSR = 0x36,
  173. ChipCmd = 0x37,
  174. TxPoll = 0x38,
  175. IntrMask = 0x3C,
  176. IntrStatus = 0x3E,
  177. TxConfig = 0x40,
  178. RxConfig = 0x44,
  179. RxMissed = 0x4C,
  180. Cfg9346 = 0x50,
  181. Config0 = 0x51,
  182. Config1 = 0x52,
  183. Config2 = 0x53,
  184. Config3 = 0x54,
  185. Config4 = 0x55,
  186. Config5 = 0x56,
  187. MultiIntr = 0x5C,
  188. PHYAR = 0x60,
  189. TBICSR = 0x64,
  190. TBI_ANAR = 0x68,
  191. TBI_LPAR = 0x6A,
  192. PHYstatus = 0x6C,
  193. RxMaxSize = 0xda,
  194. CPlusCmd = 0xe0,
  195. IntrMitigate = 0xe2,
  196. RxDescAddrLow = 0xe4,
  197. RxDescAddrHigh = 0xe8,
  198. ETThReg = 0xEC,
  199. FuncEvent = 0xF0,
  200. FuncEventMask = 0xF4,
  201. FuncPresetState = 0xF8,
  202. FuncForceEvent = 0xFC,
  203. };
  204. enum RTL8169_register_content {
  205. /*InterruptStatusBits */
  206. SYSErr = 0x8000,
  207. PCSTimeout = 0x4000,
  208. SWInt = 0x0100,
  209. TxDescUnavail = 0x80,
  210. RxFIFOOver = 0x40,
  211. LinkChg = 0x20,
  212. RxOverflow = 0x10,
  213. TxErr = 0x08,
  214. TxOK = 0x04,
  215. RxErr = 0x02,
  216. RxOK = 0x01,
  217. /*RxStatusDesc */
  218. RxRES = 0x00200000,
  219. RxCRC = 0x00080000,
  220. RxRUNT = 0x00100000,
  221. RxRWT = 0x00400000,
  222. /*ChipCmdBits */
  223. CmdReset = 0x10,
  224. CmdRxEnb = 0x08,
  225. CmdTxEnb = 0x04,
  226. RxBufEmpty = 0x01,
  227. /*Cfg9346Bits */
  228. Cfg9346_Lock = 0x00,
  229. Cfg9346_Unlock = 0xC0,
  230. /*rx_mode_bits */
  231. AcceptErr = 0x20,
  232. AcceptRunt = 0x10,
  233. AcceptBroadcast = 0x08,
  234. AcceptMulticast = 0x04,
  235. AcceptMyPhys = 0x02,
  236. AcceptAllPhys = 0x01,
  237. /*RxConfigBits */
  238. RxCfgFIFOShift = 13,
  239. RxCfgDMAShift = 8,
  240. /*TxConfigBits */
  241. TxInterFrameGapShift = 24,
  242. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  243. /*rtl8169_PHYstatus */
  244. TBI_Enable = 0x80,
  245. TxFlowCtrl = 0x40,
  246. RxFlowCtrl = 0x20,
  247. _1000bpsF = 0x10,
  248. _100bps = 0x08,
  249. _10bps = 0x04,
  250. LinkStatus = 0x02,
  251. FullDup = 0x01,
  252. /*GIGABIT_PHY_registers */
  253. PHY_CTRL_REG = 0,
  254. PHY_STAT_REG = 1,
  255. PHY_AUTO_NEGO_REG = 4,
  256. PHY_1000_CTRL_REG = 9,
  257. /*GIGABIT_PHY_REG_BIT */
  258. PHY_Restart_Auto_Nego = 0x0200,
  259. PHY_Enable_Auto_Nego = 0x1000,
  260. /* PHY_STAT_REG = 1; */
  261. PHY_Auto_Neco_Comp = 0x0020,
  262. /* PHY_AUTO_NEGO_REG = 4; */
  263. PHY_Cap_10_Half = 0x0020,
  264. PHY_Cap_10_Full = 0x0040,
  265. PHY_Cap_100_Half = 0x0080,
  266. PHY_Cap_100_Full = 0x0100,
  267. /* PHY_1000_CTRL_REG = 9; */
  268. PHY_Cap_1000_Full = 0x0200,
  269. PHY_Cap_1000_Half = 0x0100,
  270. PHY_Cap_PAUSE = 0x0400,
  271. PHY_Cap_ASYM_PAUSE = 0x0800,
  272. PHY_Cap_Null = 0x0,
  273. /*_MediaType*/
  274. _10_Half = 0x01,
  275. _10_Full = 0x02,
  276. _100_Half = 0x04,
  277. _100_Full = 0x08,
  278. _1000_Full = 0x10,
  279. /*_TBICSRBit*/
  280. TBILinkOK = 0x02000000,
  281. };
  282. enum _DescStatusBit {
  283. OWNbit = 0x80000000,
  284. EORbit = 0x40000000,
  285. FSbit = 0x20000000,
  286. LSbit = 0x10000000,
  287. };
  288. struct TxDesc {
  289. u32 status;
  290. u32 vlan_tag;
  291. u32 buf_addr;
  292. u32 buf_Haddr;
  293. };
  294. struct RxDesc {
  295. u32 status;
  296. u32 vlan_tag;
  297. u32 buf_addr;
  298. u32 buf_Haddr;
  299. };
  300. /* The descriptors for this card are required to be aligned on 256
  301. * byte boundaries. As the align attribute does not do more than 16
  302. * bytes of alignment it requires some extra steps. Add 256 to the
  303. * size of the array and the init_ring adjusts the alignment.
  304. *
  305. * UPDATE: This is no longer true; we can request arbitrary alignment.
  306. */
  307. /* Define the TX and RX Descriptors and Buffers */
  308. #define __align_256 __attribute__ (( aligned ( 256 ) ))
  309. struct {
  310. struct TxDesc tx_ring[NUM_TX_DESC] __align_256;
  311. unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];
  312. struct RxDesc rx_ring[NUM_RX_DESC] __align_256;
  313. unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
  314. } *r8169_bufs;
  315. #define tx_ring r8169_bufs->tx_ring
  316. #define rx_ring r8169_bufs->rx_ring
  317. #define txb r8169_bufs->txb
  318. #define rxb r8169_bufs->rxb
  319. static struct rtl8169_private {
  320. void *mmio_addr; /* memory map physical address */
  321. int chipset;
  322. int pcfg;
  323. int mac_version;
  324. unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  325. unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  326. struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
  327. struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
  328. unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
  329. unsigned char *Tx_skbuff[NUM_TX_DESC];
  330. } tpx;
  331. static const u16 rtl8169_intr_mask =
  332. LinkChg | RxOverflow | RxFIFOOver | TxErr | TxOK | RxErr | RxOK;
  333. static const unsigned int rtl8169_rx_config =
  334. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift) |
  335. 0x0000000E;
  336. static void rtl8169_hw_phy_config(struct nic *nic __unused);
  337. //static void rtl8169_hw_phy_reset(struct net_device *dev);
  338. #define RTL8169_WRITE_GMII_REG_BIT( ioaddr, reg, bitnum, bitval )\
  339. { \
  340. int val; \
  341. if( bitval == 1 ){ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) | (bitval<<bitnum) ) & 0xffff ; } \
  342. else{ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) & (~(0x0001<<bitnum)) ) & 0xffff ; } \
  343. RTL8169_WRITE_GMII_REG( ioaddr, reg, val ); \
  344. }
  345. //=================================================================
  346. // PHYAR
  347. // bit Symbol
  348. // 31 Flag
  349. // 30-21 reserved
  350. // 20-16 5-bit GMII/MII register address
  351. // 15-0 16-bit GMII/MII register data
  352. //=================================================================
  353. static void RTL8169_WRITE_GMII_REG(unsigned long ioaddr, int RegAddr, int value)
  354. {
  355. int i;
  356. RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
  357. udelay(1000);
  358. for (i = 2000; i > 0; i--) {
  359. // Check if the RTL8169 has completed writing to the specified MII register
  360. if (!(RTL_R32(PHYAR) & 0x80000000)) {
  361. break;
  362. } else {
  363. udelay(100);
  364. } // end of if( ! (RTL_R32(PHYAR)&0x80000000) )
  365. } // end of for() loop
  366. }
  367. //=================================================================
  368. static int RTL8169_READ_GMII_REG(unsigned long ioaddr, int RegAddr)
  369. {
  370. int i, value = -1;
  371. RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
  372. udelay(1000);
  373. for (i = 2000; i > 0; i--) {
  374. // Check if the RTL8169 has completed retrieving data from the specified MII register
  375. if (RTL_R32(PHYAR) & 0x80000000) {
  376. value = (int) (RTL_R32(PHYAR) & 0xFFFF);
  377. break;
  378. } else {
  379. udelay(100);
  380. } // end of if( RTL_R32(PHYAR) & 0x80000000 )
  381. } // end of for() loop
  382. return value;
  383. }
  384. #if 0
  385. static void mdio_write(int RegAddr, int value)
  386. {
  387. int i;
  388. RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
  389. udelay(1000);
  390. for (i = 2000; i > 0; i--) {
  391. /* Check if the RTL8169 has completed writing to the specified MII register */
  392. if (!(RTL_R32(PHYAR) & 0x80000000)) {
  393. break;
  394. } else {
  395. udelay(100);
  396. }
  397. }
  398. }
  399. static int mdio_read(int RegAddr)
  400. {
  401. int i, value = -1;
  402. RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
  403. udelay(1000);
  404. for (i = 2000; i > 0; i--) {
  405. /* Check if the RTL8169 has completed retrieving data from the specified MII register */
  406. if (RTL_R32(PHYAR) & 0x80000000) {
  407. value = (int) (RTL_R32(PHYAR) & 0xFFFF);
  408. break;
  409. } else {
  410. udelay(100);
  411. }
  412. }
  413. return value;
  414. }
  415. #endif
  416. static void rtl8169_get_mac_version( struct rtl8169_private *tp,
  417. u32 ioaddr )
  418. {
  419. /*
  420. * The driver currently handles the 8168Bf and the 8168Be identically
  421. * but they can be identified more specifically through the test below
  422. * if needed:
  423. *
  424. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  425. *
  426. * Same thing for the 8101Eb and the 8101Ec:
  427. *
  428. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  429. */
  430. const struct {
  431. u32 mask;
  432. u32 val;
  433. int mac_version;
  434. } mac_info[] = {
  435. /* 8168B family. */
  436. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  437. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  438. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  439. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_20 },
  440. /* 8168B family. */
  441. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  442. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  443. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  444. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  445. /* 8101 family. */
  446. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  447. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  448. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  449. /* FIXME: where did these entries come from ? -- FR */
  450. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  451. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  452. /* 8110 family. */
  453. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  454. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  455. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  456. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  457. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  458. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  459. { 0x00000000, 0x00000000, RTL_GIGA_MAC_VER_01 } /* Catch-all */
  460. }, *p = mac_info;
  461. unsigned long rv;
  462. rv = (RTL_R32(TxConfig));
  463. while ((rv & p->mask) != p->val)
  464. p++;
  465. tp->mac_version = p->mac_version;
  466. if (p->mask == 0x00000000) {
  467. DBG("unknown MAC (%08lx)\n", rv);
  468. }
  469. }
  470. #define IORESOURCE_MEM 0x00000200
  471. static int rtl8169_init_board(struct pci_device *pdev)
  472. {
  473. int i;
  474. // unsigned long mmio_end, mmio_flags
  475. unsigned long mmio_start, mmio_len;
  476. struct rtl8169_private *tp = &tpx;
  477. adjust_pci_device(pdev);
  478. mmio_start = pci_bar_start(pdev, PCI_BASE_ADDRESS_1);
  479. // mmio_end = pci_resource_end (pdev, 1);
  480. // mmio_flags = pci_resource_flags (pdev, PCI_BASE_ADDRESS_1);
  481. mmio_len = pci_bar_size(pdev, PCI_BASE_ADDRESS_1);
  482. // make sure PCI base addr 1 is MMIO
  483. // if (!(mmio_flags & IORESOURCE_MEM)) {
  484. // printf ("region #1 not an MMIO resource, aborting\n");
  485. // return 0;
  486. // }
  487. // check for weird/broken PCI region reporting
  488. if (mmio_len < RTL_MIN_IO_SIZE) {
  489. printf("Invalid PCI region size(s), aborting\n");
  490. return 0;
  491. }
  492. #ifdef RTL8169_USE_IO
  493. ioaddr = pci_bar_start(pdev, PCI_BASE_ADDRESS_0);
  494. #else
  495. // ioremap MMIO region
  496. ioaddr = (unsigned long) ioremap(mmio_start, mmio_len);
  497. if (ioaddr == 0) {
  498. printk("cannot remap MMIO, aborting\n");
  499. return 0;
  500. }
  501. #endif
  502. tp->mmio_addr = (void*)ioaddr;
  503. /* Soft reset the chip. */
  504. RTL_W8(ChipCmd, CmdReset);
  505. /* Check that the chip has finished the reset. */
  506. for (i = 1000; i > 0; i--)
  507. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  508. break;
  509. else
  510. udelay(10);
  511. /* Identify chip attached to board */
  512. rtl8169_get_mac_version( tp, ioaddr );
  513. // rtl8169_print_mac_version(tp);
  514. {
  515. unsigned char val8 =
  516. (unsigned char) (RTL8169_READ_GMII_REG(ioaddr, 3) &
  517. 0x000f);
  518. if (val8 == 0x00) {
  519. tp->pcfg = RTL_CFG_0;
  520. } else if (val8 == 0x01) {
  521. tp->pcfg = RTL_CFG_1;
  522. } else if (val8 == 0x02) {
  523. tp->pcfg = RTL_CFG_2;
  524. } else {
  525. tp->pcfg = RTL_CFG_2;
  526. }
  527. }
  528. /* identify chip attached to board */
  529. for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--)
  530. if (tp->mac_version == rtl_chip_info[i].mac_version) {
  531. tp->chipset = i;
  532. goto match;
  533. }
  534. /* if unknown chip, assume array element #0, original RTL-8169 in this case */
  535. DBG ( "PCI device: unknown chip version, assuming RTL-8169\n" );
  536. DBG ( "PCI device: TxConfig = %#lX\n", ( unsigned long ) RTL_R32 ( TxConfig ) );
  537. tp->chipset = 0;
  538. return 1;
  539. match:
  540. return 0;
  541. }
  542. /**************************************************************************
  543. IRQ - Wait for a frame
  544. ***************************************************************************/
  545. static void r8169_irq(struct nic *nic __unused, irq_action_t action)
  546. {
  547. int intr_status = 0;
  548. int interested = RxOverflow | RxFIFOOver | RxErr | RxOK;
  549. switch (action) {
  550. case DISABLE:
  551. case ENABLE:
  552. intr_status = RTL_R16(IntrStatus);
  553. /* h/w no longer present (hotplug?) or major error,
  554. bail */
  555. if (intr_status == 0xFFFF)
  556. break;
  557. intr_status = intr_status & ~interested;
  558. if (action == ENABLE)
  559. intr_status = intr_status | interested;
  560. RTL_W16(IntrMask, intr_status);
  561. break;
  562. case FORCE:
  563. RTL_W8(TxPoll, (RTL_R8(TxPoll) | 0x01));
  564. break;
  565. }
  566. }
  567. /**************************************************************************
  568. POLL - Wait for a frame
  569. ***************************************************************************/
  570. static int r8169_poll(struct nic *nic, int retrieve)
  571. {
  572. /* return true if there's an ethernet packet ready to read */
  573. /* nic->packet should contain data on return */
  574. /* nic->packetlen should contain length of data */
  575. int cur_rx;
  576. unsigned int intr_status = 0;
  577. struct rtl8169_private *tp = &tpx;
  578. cur_rx = tp->cur_rx;
  579. if ((tp->RxDescArray[cur_rx].status & OWNbit) == 0) {
  580. /* There is a packet ready */
  581. DBG("r8169_poll(): packet ready\n");
  582. if (!retrieve)
  583. return 1;
  584. intr_status = RTL_R16(IntrStatus);
  585. /* h/w no longer present (hotplug?) or major error,
  586. bail */
  587. if (intr_status == 0xFFFF) {
  588. DBG("r8169_poll(): unknown error\n");
  589. return 0;
  590. }
  591. RTL_W16(IntrStatus, intr_status &
  592. ~(RxFIFOOver | RxOverflow | RxOK));
  593. if (!(tp->RxDescArray[cur_rx].status & RxRES)) {
  594. nic->packetlen = (int) (tp->RxDescArray[cur_rx].
  595. status & 0x00001FFF) - 4;
  596. memcpy(nic->packet, tp->RxBufferRing[cur_rx],
  597. nic->packetlen);
  598. if (cur_rx == NUM_RX_DESC - 1)
  599. tp->RxDescArray[cur_rx].status =
  600. (OWNbit | EORbit) + RX_BUF_SIZE;
  601. else
  602. tp->RxDescArray[cur_rx].status =
  603. OWNbit + RX_BUF_SIZE;
  604. tp->RxDescArray[cur_rx].buf_addr =
  605. virt_to_bus(tp->RxBufferRing[cur_rx]);
  606. tp->RxDescArray[cur_rx].buf_Haddr = 0;
  607. } else
  608. printf("Error Rx");
  609. /* FIXME: shouldn't I reset the status on an error */
  610. cur_rx = (cur_rx + 1) % NUM_RX_DESC;
  611. tp->cur_rx = cur_rx;
  612. RTL_W16(IntrStatus, intr_status &
  613. (RxFIFOOver | RxOverflow | RxOK));
  614. return 1;
  615. }
  616. tp->cur_rx = cur_rx;
  617. /* FIXME: There is no reason to do this as cur_rx did not change */
  618. return (0); /* initially as this is called to flush the input */
  619. }
  620. /**************************************************************************
  621. TRANSMIT - Transmit a frame
  622. ***************************************************************************/
  623. static void r8169_transmit(struct nic *nic, const char *d, /* Destination */
  624. unsigned int t, /* Type */
  625. unsigned int s, /* size */
  626. const char *p)
  627. { /* Packet */
  628. /* send the packet to destination */
  629. u16 nstype;
  630. u32 to;
  631. u8 *ptxb;
  632. struct rtl8169_private *tp = &tpx;
  633. int entry = tp->cur_tx % NUM_TX_DESC;
  634. /* point to the current txb incase multiple tx_rings are used */
  635. ptxb = tp->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
  636. memcpy(ptxb, d, ETH_ALEN);
  637. memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN);
  638. nstype = htons((u16) t);
  639. memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
  640. memcpy(ptxb + ETH_HLEN, p, s);
  641. s += ETH_HLEN;
  642. s &= 0x0FFF;
  643. while (s < ETH_ZLEN)
  644. ptxb[s++] = '\0';
  645. tp->TxDescArray[entry].buf_addr = virt_to_bus(ptxb);
  646. tp->TxDescArray[entry].buf_Haddr = 0;
  647. if (entry != (NUM_TX_DESC - 1))
  648. tp->TxDescArray[entry].status =
  649. (OWNbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s :
  650. ETH_ZLEN);
  651. else
  652. tp->TxDescArray[entry].status =
  653. (OWNbit | EORbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s
  654. : ETH_ZLEN);
  655. RTL_W8(TxPoll, 0x40); /* set polling bit */
  656. tp->cur_tx++;
  657. to = currticks() + TX_TIMEOUT;
  658. while ((tp->TxDescArray[entry].status & OWNbit) && (currticks() < to)); /* wait */
  659. if (currticks() >= to) {
  660. printf("TX Time Out");
  661. }
  662. }
  663. static void rtl8169_set_rx_mode(struct nic *nic __unused)
  664. {
  665. u32 mc_filter[2]; /* Multicast hash filter */
  666. int rx_mode;
  667. u32 tmp = 0;
  668. struct rtl8169_private *tp = &tpx;
  669. /* IFF_ALLMULTI */
  670. /* Too many to filter perfectly -- accept all multicasts. */
  671. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  672. mc_filter[1] = mc_filter[0] = 0xffffffff;
  673. tmp =
  674. rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
  675. rtl_chip_info[tp->chipset].
  676. RxConfigMask);
  677. RTL_W32(RxConfig, tmp);
  678. RTL_W32(MAR0 + 0, mc_filter[0]);
  679. RTL_W32(MAR0 + 4, mc_filter[1]);
  680. }
  681. static void rtl8169_hw_start(struct nic *nic)
  682. {
  683. u32 i;
  684. struct rtl8169_private *tp = &tpx;
  685. /* Soft reset the chip. */
  686. RTL_W8(ChipCmd, CmdReset);
  687. /* Check that the chip has finished the reset. */
  688. for (i = 1000; i > 0; i--) {
  689. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  690. break;
  691. else
  692. udelay(10);
  693. }
  694. RTL_W8(Cfg9346, Cfg9346_Unlock);
  695. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  696. RTL_W8(ETThReg, ETTh);
  697. /* For gigabit rtl8169 */
  698. RTL_W16(RxMaxSize, RxPacketMaxSize);
  699. /* Set Rx Config register */
  700. i = rtl8169_rx_config | (RTL_R32(RxConfig) &
  701. rtl_chip_info[tp->chipset].RxConfigMask);
  702. RTL_W32(RxConfig, i);
  703. /* Set DMA burst size and Interframe Gap Time */
  704. RTL_W32(TxConfig,
  705. (TX_DMA_BURST << TxDMAShift) | (InterFrameGap <<
  706. TxInterFrameGapShift));
  707. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd));
  708. if (tp->mac_version == RTL_GIGA_MAC_VER_02 || tp->mac_version == RTL_GIGA_MAC_VER_03) {
  709. RTL_W16(CPlusCmd,
  710. (RTL_R16(CPlusCmd) | (1 << 14) | (1 << 3)));
  711. DBG
  712. ("Set MAC Reg C+CR Offset 0xE0: bit-3 and bit-14\n");
  713. } else {
  714. RTL_W16(CPlusCmd, (RTL_R16(CPlusCmd) | (1 << 3)));
  715. DBG("Set MAC Reg C+CR Offset 0xE0: bit-3.\n");
  716. }
  717. {
  718. //RTL_W16(IntrMitigate, 0x1517);
  719. //RTL_W16(IntrMitigate, 0x152a);
  720. //RTL_W16(IntrMitigate, 0x282a);
  721. RTL_W16(IntrMitigate, 0x0000);
  722. }
  723. tp->cur_rx = 0;
  724. RTL_W32(TxDescAddrLow, virt_to_le32desc(tp->TxDescArray));
  725. RTL_W32(TxDescAddrHigh, virt_to_le32desc(NULL));
  726. RTL_W32(RxDescAddrLow, virt_to_le32desc(tp->RxDescArray));
  727. RTL_W32(RxDescAddrHigh, virt_to_le32desc(NULL));
  728. RTL_W8(Cfg9346, Cfg9346_Lock);
  729. udelay(10);
  730. RTL_W32(RxMissed, 0);
  731. rtl8169_set_rx_mode(nic);
  732. /* no early-rx interrupts */
  733. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  734. RTL_W16(IntrMask, rtl8169_intr_mask);
  735. }
  736. static void rtl8169_init_ring(struct nic *nic __unused)
  737. {
  738. int i;
  739. struct rtl8169_private *tp = &tpx;
  740. tp->cur_rx = 0;
  741. tp->cur_tx = 0;
  742. memset(tp->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
  743. memset(tp->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
  744. for (i = 0; i < NUM_TX_DESC; i++) {
  745. tp->Tx_skbuff[i] = &txb[i];
  746. }
  747. for (i = 0; i < NUM_RX_DESC; i++) {
  748. if (i == (NUM_RX_DESC - 1))
  749. tp->RxDescArray[i].status =
  750. (OWNbit | EORbit) | RX_BUF_SIZE;
  751. else
  752. tp->RxDescArray[i].status = OWNbit | RX_BUF_SIZE;
  753. tp->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
  754. tp->RxDescArray[i].buf_addr =
  755. virt_to_bus(tp->RxBufferRing[i]);
  756. tp->RxDescArray[i].buf_Haddr = 0;
  757. }
  758. }
  759. /**************************************************************************
  760. RESET - Finish setting up the ethernet interface
  761. ***************************************************************************/
  762. static void r8169_reset(struct nic *nic)
  763. {
  764. int i;
  765. struct rtl8169_private *tp = &tpx;
  766. tp->TxDescArray = tx_ring;
  767. tp->RxDescArray = rx_ring;
  768. rtl8169_init_ring(nic);
  769. rtl8169_hw_start(nic);
  770. /* Construct a perfect filter frame with the mac address as first match
  771. * and broadcast for all others */
  772. for (i = 0; i < 192; i++)
  773. txb[i] = 0xFF;
  774. txb[0] = nic->node_addr[0];
  775. txb[1] = nic->node_addr[1];
  776. txb[2] = nic->node_addr[2];
  777. txb[3] = nic->node_addr[3];
  778. txb[4] = nic->node_addr[4];
  779. txb[5] = nic->node_addr[5];
  780. }
  781. /**************************************************************************
  782. DISABLE - Turn off ethernet interface
  783. ***************************************************************************/
  784. static void r8169_disable ( struct nic *nic __unused ) {
  785. int i;
  786. struct rtl8169_private *tp = &tpx;
  787. /* Stop the chip's Tx and Rx DMA processes. */
  788. RTL_W8(ChipCmd, 0x00);
  789. /* Disable interrupts by clearing the interrupt mask. */
  790. RTL_W16(IntrMask, 0x0000);
  791. RTL_W32(RxMissed, 0);
  792. tp->TxDescArray = NULL;
  793. tp->RxDescArray = NULL;
  794. for (i = 0; i < NUM_RX_DESC; i++) {
  795. tp->RxBufferRing[i] = NULL;
  796. }
  797. }
  798. static struct nic_operations r8169_operations = {
  799. .connect = dummy_connect,
  800. .poll = r8169_poll,
  801. .transmit = r8169_transmit,
  802. .irq = r8169_irq,
  803. };
  804. static struct pci_device_id r8169_nics[] = {
  805. PCI_ROM(0x10ec, 0x8169, "r8169", "RealTek RTL8169 Gigabit Ethernet"),
  806. PCI_ROM(0x16ec, 0x0116, "usr-r8169", "US Robotics RTL8169 Gigabit Ethernet"),
  807. PCI_ROM(0x1186, 0x4300, "dlink-r8169", "D-Link RTL8169 Gigabit Ethernet"),
  808. PCI_ROM(0x1737, 0x1032, "linksys-r8169", "Linksys RTL8169 Gigabit Ethernet"),
  809. PCI_ROM(0x10ec, 0x8129, "r8169-8129", "RealTek RT8129 Fast Ethernet Adapter"),
  810. PCI_ROM(0x10ec, 0x8136, "r8169-8101e", "RealTek RTL8101E PCI Express Fast Ethernet controller"),
  811. PCI_ROM(0x10ec, 0x8167, "r8169-8110sc/8169sc", "RealTek RTL-8110SC/8169SC Gigabit Ethernet"),
  812. PCI_ROM(0x10ec, 0x8168, "r8169-8168b", "RealTek RTL8111/8168B PCI Express Gigabit Ethernet controller"),
  813. };
  814. PCI_DRIVER ( r8169_driver, r8169_nics, PCI_NO_CLASS );
  815. /**************************************************************************
  816. PROBE - Look for an adapter, this routine's visible to the outside
  817. ***************************************************************************/
  818. #define board_found 1
  819. #define valid_link 0
  820. static int r8169_probe ( struct nic *nic, struct pci_device *pci ) {
  821. static int board_idx = -1;
  822. static int printed_version = 0;
  823. struct rtl8169_private *tp = &tpx;
  824. int i, rc;
  825. int option = -1, Cap10_100 = 0, Cap1000 = 0;
  826. printf ( "r8169.c: Found %s, Vendor=%hX Device=%hX\n",
  827. pci->driver_name, pci->vendor, pci->device );
  828. board_idx++;
  829. printed_version = 1;
  830. /* Quick and very dirty hack to get r8169 driver working
  831. * again, pre-rewrite
  832. */
  833. if ( ! r8169_bufs )
  834. r8169_bufs = malloc_dma ( sizeof ( *r8169_bufs ), 256 );
  835. if ( ! r8169_bufs )
  836. return 0;
  837. memset ( r8169_bufs, 0, sizeof ( *r8169_bufs ) );
  838. rc = rtl8169_init_board(pci); /* Return code is meaningless */
  839. /* Get MAC address. FIXME: read EEPROM */
  840. for (i = 0; i < MAC_ADDR_LEN; i++)
  841. nic->node_addr[i] = RTL_R8(MAC0 + i);
  842. DBG ( "%s: Identified chip type is '%s'.\n", pci->driver_name,
  843. rtl_chip_info[tp->chipset].name );
  844. /* Print out some hardware info */
  845. DBG ( "%s: %s at ioaddr %#hx, ", pci->driver_name, eth_ntoa ( nic->node_addr ),
  846. (unsigned int) ioaddr );
  847. /* Config PHY */
  848. rtl8169_hw_phy_config(nic);
  849. DBG("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  850. RTL_W8(0x82, 0x01);
  851. pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0x40);
  852. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  853. pci_write_config_byte(pci, PCI_CACHE_LINE_SIZE, 0x08);
  854. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  855. DBG("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  856. RTL_W8(0x82, 0x01);
  857. DBG("Set PHY Reg 0x0bh = 0x00h\n");
  858. RTL8169_WRITE_GMII_REG(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
  859. }
  860. r8169_reset(nic);
  861. /* if TBI is not endbled */
  862. if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
  863. int val = RTL8169_READ_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG);
  864. #ifdef RTL8169_HW_FLOW_CONTROL_SUPPORT
  865. val |= PHY_Cap_PAUSE | PHY_Cap_ASYM_PAUSE;
  866. #endif //end #define RTL8169_HW_FLOW_CONTROL_SUPPORT
  867. /* Force RTL8169 in 10/100/1000 Full/Half mode. */
  868. if (option > 0) {
  869. printf(" Force-mode Enabled.\n");
  870. Cap10_100 = 0, Cap1000 = 0;
  871. switch (option) {
  872. case _10_Half:
  873. Cap10_100 = PHY_Cap_10_Half;
  874. Cap1000 = PHY_Cap_Null;
  875. break;
  876. case _10_Full:
  877. Cap10_100 = PHY_Cap_10_Full;
  878. Cap1000 = PHY_Cap_Null;
  879. break;
  880. case _100_Half:
  881. Cap10_100 = PHY_Cap_100_Half;
  882. Cap1000 = PHY_Cap_Null;
  883. break;
  884. case _100_Full:
  885. Cap10_100 = PHY_Cap_100_Full;
  886. Cap1000 = PHY_Cap_Null;
  887. break;
  888. case _1000_Full:
  889. Cap10_100 = PHY_Cap_Null;
  890. Cap1000 = PHY_Cap_1000_Full;
  891. break;
  892. default:
  893. break;
  894. }
  895. RTL8169_WRITE_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG, Cap10_100 | (val & 0xC1F)); //leave PHY_AUTO_NEGO_REG bit4:0 unchanged
  896. RTL8169_WRITE_GMII_REG(ioaddr, PHY_1000_CTRL_REG,
  897. Cap1000);
  898. } else {
  899. DBG ( "%s: Auto-negotiation Enabled.\n", pci->driver_name );
  900. // enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged
  901. RTL8169_WRITE_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG,
  902. PHY_Cap_10_Half |
  903. PHY_Cap_10_Full |
  904. PHY_Cap_100_Half |
  905. PHY_Cap_100_Full | (val &
  906. 0xC1F));
  907. // enable 1000 Full Mode
  908. // RTL8169_WRITE_GMII_REG( ioaddr, PHY_1000_CTRL_REG, PHY_Cap_1000_Full );
  909. RTL8169_WRITE_GMII_REG(ioaddr, PHY_1000_CTRL_REG, PHY_Cap_1000_Full | PHY_Cap_1000_Half); //rtl8168
  910. } // end of if( option > 0 )
  911. // Enable auto-negotiation and restart auto-nigotiation
  912. RTL8169_WRITE_GMII_REG(ioaddr, PHY_CTRL_REG,
  913. PHY_Enable_Auto_Nego |
  914. PHY_Restart_Auto_Nego);
  915. udelay(100);
  916. // wait for auto-negotiation process
  917. for (i = 10000; i > 0; i--) {
  918. //check if auto-negotiation complete
  919. if (RTL8169_READ_GMII_REG(ioaddr, PHY_STAT_REG) &
  920. PHY_Auto_Neco_Comp) {
  921. udelay(100);
  922. option = RTL_R8(PHYstatus);
  923. if (option & _1000bpsF) {
  924. printf
  925. ("1000Mbps Full-duplex operation.\n");
  926. } else {
  927. printf
  928. ("%sMbps %s-duplex operation.\n",
  929. (option & _100bps) ? "100" :
  930. "10",
  931. (option & FullDup) ? "Full" :
  932. "Half");
  933. }
  934. break;
  935. } else {
  936. udelay(100);
  937. } // end of if( RTL8169_READ_GMII_REG(ioaddr, 1) & 0x20 )
  938. } // end for-loop to wait for auto-negotiation process
  939. } else {
  940. udelay(100);
  941. printf
  942. ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
  943. pci->driver_name,
  944. (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
  945. }
  946. r8169_reset(nic);
  947. /* point to NIC specific routines */
  948. nic->nic_op = &r8169_operations;
  949. nic->irqno = pci->irq;
  950. nic->ioaddr = ioaddr;
  951. return 1;
  952. }
  953. //======================================================================================================
  954. /*
  955. static void rtl8169_hw_PHY_reset(struct nic *nic __unused)
  956. {
  957. int val, phy_reset_expiretime = 50;
  958. struct rtl8169_private *priv = dev->priv;
  959. unsigned long ioaddr = priv->ioaddr;
  960. DBG("%s: Reset RTL8169s PHY\n", dev->name);
  961. val = ( RTL8169_READ_GMII_REG( ioaddr, 0 ) | 0x8000 ) & 0xffff;
  962. RTL8169_WRITE_GMII_REG( ioaddr, 0, val );
  963. do //waiting for phy reset
  964. {
  965. if( RTL8169_READ_GMII_REG( ioaddr, 0 ) & 0x8000 ){
  966. phy_reset_expiretime --;
  967. udelay(100);
  968. }
  969. else{
  970. break;
  971. }
  972. }while( phy_reset_expiretime >= 0 );
  973. assert( phy_reset_expiretime > 0 );
  974. }
  975. */
  976. struct phy_reg {
  977. u16 reg;
  978. u16 val;
  979. };
  980. static void rtl_phy_write(void *ioaddr, struct phy_reg *regs, int len)
  981. {
  982. while (len-- > 0) {
  983. RTL8169_WRITE_GMII_REG((u32)ioaddr, regs->reg, regs->val);
  984. regs++;
  985. }
  986. }
  987. static void rtl8169s_hw_phy_config(void *ioaddr)
  988. {
  989. struct {
  990. u16 regs[5]; /* Beware of bit-sign propagation */
  991. } phy_magic[5] = { {
  992. { 0x0000, //w 4 15 12 0
  993. 0x00a1, //w 3 15 0 00a1
  994. 0x0008, //w 2 15 0 0008
  995. 0x1020, //w 1 15 0 1020
  996. 0x1000 } },{ //w 0 15 0 1000
  997. { 0x7000, //w 4 15 12 7
  998. 0xff41, //w 3 15 0 ff41
  999. 0xde60, //w 2 15 0 de60
  1000. 0x0140, //w 1 15 0 0140
  1001. 0x0077 } },{ //w 0 15 0 0077
  1002. { 0xa000, //w 4 15 12 a
  1003. 0xdf01, //w 3 15 0 df01
  1004. 0xdf20, //w 2 15 0 df20
  1005. 0xff95, //w 1 15 0 ff95
  1006. 0xfa00 } },{ //w 0 15 0 fa00
  1007. { 0xb000, //w 4 15 12 b
  1008. 0xff41, //w 3 15 0 ff41
  1009. 0xde20, //w 2 15 0 de20
  1010. 0x0140, //w 1 15 0 0140
  1011. 0x00bb } },{ //w 0 15 0 00bb
  1012. { 0xf000, //w 4 15 12 f
  1013. 0xdf01, //w 3 15 0 df01
  1014. 0xdf20, //w 2 15 0 df20
  1015. 0xff95, //w 1 15 0 ff95
  1016. 0xbf00 } //w 0 15 0 bf00
  1017. }
  1018. }, *p = phy_magic;
  1019. unsigned int i;
  1020. RTL8169_WRITE_GMII_REG((u32)ioaddr, 0x1f, 0x0001); //w 31 2 0 1
  1021. RTL8169_WRITE_GMII_REG((u32)ioaddr, 0x15, 0x1000); //w 21 15 0 1000
  1022. RTL8169_WRITE_GMII_REG((u32)ioaddr, 0x18, 0x65c7); //w 24 15 0 65c7
  1023. RTL8169_WRITE_GMII_REG_BIT((u32)ioaddr, 4, 11, 0); //w 4 11 11 0
  1024. for (i = 0; i < ARRAY_SIZE(phy_magic); i++, p++) {
  1025. int val, pos = 4;
  1026. val = (RTL8169_READ_GMII_REG((u32)ioaddr, pos) & 0x0fff) | (p->regs[0] & 0xffff);
  1027. RTL8169_WRITE_GMII_REG((u32)ioaddr, pos, val);
  1028. while (--pos >= 0)
  1029. RTL8169_WRITE_GMII_REG((u32)ioaddr, pos, p->regs[4 - pos] & 0xffff);
  1030. RTL8169_WRITE_GMII_REG_BIT((u32)ioaddr, 4, 11, 1); //w 4 11 11 1
  1031. RTL8169_WRITE_GMII_REG_BIT((u32)ioaddr, 4, 11, 0); //w 4 11 11 0
  1032. }
  1033. RTL8169_WRITE_GMII_REG((u32)ioaddr, 0x1f, 0x0000); //w 31 2 0 0
  1034. }
  1035. static void rtl8169sb_hw_phy_config(void *ioaddr)
  1036. {
  1037. struct phy_reg phy_reg_init[] = {
  1038. { 0x1f, 0x0002 },
  1039. { 0x01, 0x90d0 },
  1040. { 0x1f, 0x0000 }
  1041. };
  1042. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1043. }
  1044. static void rtl8168cp_hw_phy_config(void *ioaddr)
  1045. {
  1046. struct phy_reg phy_reg_init[] = {
  1047. { 0x1f, 0x0000 },
  1048. { 0x1d, 0x0f00 },
  1049. { 0x1f, 0x0002 },
  1050. { 0x0c, 0x1ec8 },
  1051. { 0x1f, 0x0000 }
  1052. };
  1053. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1054. }
  1055. static void rtl8168c_hw_phy_config(void *ioaddr)
  1056. {
  1057. struct phy_reg phy_reg_init[] = {
  1058. { 0x1f, 0x0001 },
  1059. { 0x12, 0x2300 },
  1060. { 0x1f, 0x0002 },
  1061. { 0x00, 0x88d4 },
  1062. { 0x01, 0x82b1 },
  1063. { 0x03, 0x7002 },
  1064. { 0x08, 0x9e30 },
  1065. { 0x09, 0x01f0 },
  1066. { 0x0a, 0x5500 },
  1067. { 0x0c, 0x00c8 },
  1068. { 0x1f, 0x0003 },
  1069. { 0x12, 0xc096 },
  1070. { 0x16, 0x000a },
  1071. { 0x1f, 0x0000 }
  1072. };
  1073. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1074. }
  1075. static void rtl8168cx_hw_phy_config(void *ioaddr)
  1076. {
  1077. struct phy_reg phy_reg_init[] = {
  1078. { 0x1f, 0x0000 },
  1079. { 0x12, 0x2300 },
  1080. { 0x1f, 0x0003 },
  1081. { 0x16, 0x0f0a },
  1082. { 0x1f, 0x0000 },
  1083. { 0x1f, 0x0002 },
  1084. { 0x0c, 0x7eb8 },
  1085. { 0x1f, 0x0000 }
  1086. };
  1087. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1088. }
  1089. static void rtl8169_hw_phy_config(struct nic *nic __unused)
  1090. {
  1091. struct rtl8169_private *tp = &tpx;
  1092. void *ioaddr = tp->mmio_addr;
  1093. DBG("rtl8169_hw_phy_config(): card at addr=0x%lx: priv->mac_version=%d, priv->pcfg=%d\n", (unsigned long) ioaddr, tp->mac_version, tp->pcfg);
  1094. switch (tp->mac_version) {
  1095. case RTL_GIGA_MAC_VER_01:
  1096. break;
  1097. case RTL_GIGA_MAC_VER_02:
  1098. case RTL_GIGA_MAC_VER_03:
  1099. rtl8169s_hw_phy_config(ioaddr);
  1100. break;
  1101. case RTL_GIGA_MAC_VER_04:
  1102. rtl8169sb_hw_phy_config(ioaddr);
  1103. break;
  1104. case RTL_GIGA_MAC_VER_18:
  1105. rtl8168cp_hw_phy_config(ioaddr);
  1106. break;
  1107. case RTL_GIGA_MAC_VER_19:
  1108. rtl8168c_hw_phy_config(ioaddr);
  1109. break;
  1110. case RTL_GIGA_MAC_VER_20:
  1111. rtl8168cx_hw_phy_config(ioaddr);
  1112. break;
  1113. default:
  1114. break;
  1115. }
  1116. }
  1117. DRIVER ( "r8169/PCI", nic_driver, pci_driver, r8169_driver,
  1118. r8169_probe, r8169_disable );
  1119. /*
  1120. * Local variables:
  1121. * c-basic-offset: 8
  1122. * c-indent-level: 8
  1123. * tab-width: 8
  1124. * End:
  1125. */