rtl8169.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * rtl8169.c : U-Boot driver for the RealTek RTL8169
  3. *
  4. * Masami Komiya (mkomiya@sonare.it)
  5. *
  6. * Most part is taken from r8169.c of etherboot
  7. *
  8. */
  9. /**************************************************************************
  10. * r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
  11. * Written 2003 by Timothy Legge <tlegge@rogers.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. *
  27. * Portions of this code based on:
  28. * r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
  29. * for Linux kernel 2.4.x.
  30. *
  31. * Written 2002 ShuChen <shuchen@realtek.com.tw>
  32. * See Linux Driver for full information
  33. *
  34. * Linux Driver Version 1.27a, 10.02.2002
  35. *
  36. * Thanks to:
  37. * Jean Chen of RealTek Semiconductor Corp. for
  38. * providing the evaluation NIC used to develop
  39. * this driver. RealTek's support for Etherboot
  40. * is appreciated.
  41. *
  42. * REVISION HISTORY:
  43. * ================
  44. *
  45. * v1.0 11-26-2003 timlegge Initial port of Linux driver
  46. * v1.5 01-17-2004 timlegge Initial driver output cleanup
  47. *
  48. * Indent Options: indent -kr -i8
  49. ***************************************************************************/
  50. /*
  51. * 26 August 2006 Mihai Georgian <u-boot@linuxnotincluded.org.uk>
  52. * Modified to use le32_to_cpu and cpu_to_le32 properly
  53. */
  54. #include <common.h>
  55. #include <malloc.h>
  56. #include <net.h>
  57. #include <asm/io.h>
  58. #include <pci.h>
  59. #undef DEBUG_RTL8169
  60. #undef DEBUG_RTL8169_TX
  61. #undef DEBUG_RTL8169_RX
  62. #define drv_version "v1.5"
  63. #define drv_date "01-17-2004"
  64. static u32 ioaddr;
  65. /* Condensed operations for readability. */
  66. #define currticks() get_timer(0)
  67. /* media options */
  68. #define MAX_UNITS 8
  69. static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  70. /* MAC address length*/
  71. #define MAC_ADDR_LEN 6
  72. /* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
  73. #define MAX_ETH_FRAME_SIZE 1536
  74. #define TX_FIFO_THRESH 256 /* In bytes */
  75. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  76. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  77. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  78. #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
  79. #define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
  80. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  81. #define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
  82. #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
  83. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  84. #define RX_BUF_LEN 8192
  85. #define RTL_MIN_IO_SIZE 0x80
  86. #define TX_TIMEOUT (6*HZ)
  87. /* write/read MMIO register. Notice: {read,write}[wl] do the necessary swapping */
  88. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  89. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  90. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  91. #define RTL_R8(reg) readb (ioaddr + (reg))
  92. #define RTL_R16(reg) readw (ioaddr + (reg))
  93. #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
  94. #define ETH_FRAME_LEN MAX_ETH_FRAME_SIZE
  95. #define ETH_ALEN MAC_ADDR_LEN
  96. #define ETH_ZLEN 60
  97. enum RTL8169_registers {
  98. MAC0 = 0, /* Ethernet hardware address. */
  99. MAR0 = 8, /* Multicast filter. */
  100. TxDescStartAddr = 0x20,
  101. TxHDescStartAddr = 0x28,
  102. FLASH = 0x30,
  103. ERSR = 0x36,
  104. ChipCmd = 0x37,
  105. TxPoll = 0x38,
  106. IntrMask = 0x3C,
  107. IntrStatus = 0x3E,
  108. TxConfig = 0x40,
  109. RxConfig = 0x44,
  110. RxMissed = 0x4C,
  111. Cfg9346 = 0x50,
  112. Config0 = 0x51,
  113. Config1 = 0x52,
  114. Config2 = 0x53,
  115. Config3 = 0x54,
  116. Config4 = 0x55,
  117. Config5 = 0x56,
  118. MultiIntr = 0x5C,
  119. PHYAR = 0x60,
  120. TBICSR = 0x64,
  121. TBI_ANAR = 0x68,
  122. TBI_LPAR = 0x6A,
  123. PHYstatus = 0x6C,
  124. RxMaxSize = 0xDA,
  125. CPlusCmd = 0xE0,
  126. RxDescStartAddr = 0xE4,
  127. EarlyTxThres = 0xEC,
  128. FuncEvent = 0xF0,
  129. FuncEventMask = 0xF4,
  130. FuncPresetState = 0xF8,
  131. FuncForceEvent = 0xFC,
  132. };
  133. enum RTL8169_register_content {
  134. /*InterruptStatusBits */
  135. SYSErr = 0x8000,
  136. PCSTimeout = 0x4000,
  137. SWInt = 0x0100,
  138. TxDescUnavail = 0x80,
  139. RxFIFOOver = 0x40,
  140. RxUnderrun = 0x20,
  141. RxOverflow = 0x10,
  142. TxErr = 0x08,
  143. TxOK = 0x04,
  144. RxErr = 0x02,
  145. RxOK = 0x01,
  146. /*RxStatusDesc */
  147. RxRES = 0x00200000,
  148. RxCRC = 0x00080000,
  149. RxRUNT = 0x00100000,
  150. RxRWT = 0x00400000,
  151. /*ChipCmdBits */
  152. CmdReset = 0x10,
  153. CmdRxEnb = 0x08,
  154. CmdTxEnb = 0x04,
  155. RxBufEmpty = 0x01,
  156. /*Cfg9346Bits */
  157. Cfg9346_Lock = 0x00,
  158. Cfg9346_Unlock = 0xC0,
  159. /*rx_mode_bits */
  160. AcceptErr = 0x20,
  161. AcceptRunt = 0x10,
  162. AcceptBroadcast = 0x08,
  163. AcceptMulticast = 0x04,
  164. AcceptMyPhys = 0x02,
  165. AcceptAllPhys = 0x01,
  166. /*RxConfigBits */
  167. RxCfgFIFOShift = 13,
  168. RxCfgDMAShift = 8,
  169. /*TxConfigBits */
  170. TxInterFrameGapShift = 24,
  171. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  172. /*rtl8169_PHYstatus */
  173. TBI_Enable = 0x80,
  174. TxFlowCtrl = 0x40,
  175. RxFlowCtrl = 0x20,
  176. _1000bpsF = 0x10,
  177. _100bps = 0x08,
  178. _10bps = 0x04,
  179. LinkStatus = 0x02,
  180. FullDup = 0x01,
  181. /*GIGABIT_PHY_registers */
  182. PHY_CTRL_REG = 0,
  183. PHY_STAT_REG = 1,
  184. PHY_AUTO_NEGO_REG = 4,
  185. PHY_1000_CTRL_REG = 9,
  186. /*GIGABIT_PHY_REG_BIT */
  187. PHY_Restart_Auto_Nego = 0x0200,
  188. PHY_Enable_Auto_Nego = 0x1000,
  189. /* PHY_STAT_REG = 1; */
  190. PHY_Auto_Nego_Comp = 0x0020,
  191. /* PHY_AUTO_NEGO_REG = 4; */
  192. PHY_Cap_10_Half = 0x0020,
  193. PHY_Cap_10_Full = 0x0040,
  194. PHY_Cap_100_Half = 0x0080,
  195. PHY_Cap_100_Full = 0x0100,
  196. /* PHY_1000_CTRL_REG = 9; */
  197. PHY_Cap_1000_Full = 0x0200,
  198. PHY_Cap_Null = 0x0,
  199. /*_MediaType*/
  200. _10_Half = 0x01,
  201. _10_Full = 0x02,
  202. _100_Half = 0x04,
  203. _100_Full = 0x08,
  204. _1000_Full = 0x10,
  205. /*_TBICSRBit*/
  206. TBILinkOK = 0x02000000,
  207. };
  208. static struct {
  209. const char *name;
  210. u8 version; /* depend on RTL8169 docs */
  211. u32 RxConfigMask; /* should clear the bits supported by this chip */
  212. } rtl_chip_info[] = {
  213. {"RTL-8169", 0x00, 0xff7e1880,},
  214. {"RTL-8169", 0x04, 0xff7e1880,},
  215. {"RTL-8169", 0x00, 0xff7e1880,},
  216. {"RTL-8169s/8110s", 0x02, 0xff7e1880,},
  217. {"RTL-8169s/8110s", 0x04, 0xff7e1880,},
  218. {"RTL-8169sb/8110sb", 0x10, 0xff7e1880,},
  219. {"RTL-8169sc/8110sc", 0x18, 0xff7e1880,},
  220. {"RTL-8168b/8111sb", 0x30, 0xff7e1880,},
  221. {"RTL-8168b/8111sb", 0x38, 0xff7e1880,},
  222. {"RTL-8101e", 0x34, 0xff7e1880,},
  223. {"RTL-8100e", 0x32, 0xff7e1880,},
  224. };
  225. enum _DescStatusBit {
  226. OWNbit = 0x80000000,
  227. EORbit = 0x40000000,
  228. FSbit = 0x20000000,
  229. LSbit = 0x10000000,
  230. };
  231. struct TxDesc {
  232. u32 status;
  233. u32 vlan_tag;
  234. u32 buf_addr;
  235. u32 buf_Haddr;
  236. };
  237. struct RxDesc {
  238. u32 status;
  239. u32 vlan_tag;
  240. u32 buf_addr;
  241. u32 buf_Haddr;
  242. };
  243. /* Define the TX Descriptor */
  244. static u8 tx_ring[NUM_TX_DESC * sizeof(struct TxDesc) + 256];
  245. /* __attribute__ ((aligned(256))); */
  246. /* Create a static buffer of size RX_BUF_SZ for each
  247. TX Descriptor. All descriptors point to a
  248. part of this buffer */
  249. static unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];
  250. /* Define the RX Descriptor */
  251. static u8 rx_ring[NUM_RX_DESC * sizeof(struct TxDesc) + 256];
  252. /* __attribute__ ((aligned(256))); */
  253. /* Create a static buffer of size RX_BUF_SZ for each
  254. RX Descriptor All descriptors point to a
  255. part of this buffer */
  256. static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
  257. struct rtl8169_private {
  258. void *mmio_addr; /* memory map physical address */
  259. int chipset;
  260. unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  261. unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  262. unsigned long dirty_tx;
  263. unsigned char *TxDescArrays; /* Index of Tx Descriptor buffer */
  264. unsigned char *RxDescArrays; /* Index of Rx Descriptor buffer */
  265. struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
  266. struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
  267. unsigned char *RxBufferRings; /* Index of Rx Buffer */
  268. unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
  269. unsigned char *Tx_skbuff[NUM_TX_DESC];
  270. } tpx;
  271. static struct rtl8169_private *tpc;
  272. static const u16 rtl8169_intr_mask =
  273. SYSErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver | TxErr |
  274. TxOK | RxErr | RxOK;
  275. static const unsigned int rtl8169_rx_config =
  276. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  277. static struct pci_device_id supported[] = {
  278. {PCI_VENDOR_ID_REALTEK, 0x8167},
  279. {PCI_VENDOR_ID_REALTEK, 0x8169},
  280. {}
  281. };
  282. void mdio_write(int RegAddr, int value)
  283. {
  284. int i;
  285. RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
  286. udelay(1000);
  287. for (i = 2000; i > 0; i--) {
  288. /* Check if the RTL8169 has completed writing to the specified MII register */
  289. if (!(RTL_R32(PHYAR) & 0x80000000)) {
  290. break;
  291. } else {
  292. udelay(100);
  293. }
  294. }
  295. }
  296. int mdio_read(int RegAddr)
  297. {
  298. int i, value = -1;
  299. RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
  300. udelay(1000);
  301. for (i = 2000; i > 0; i--) {
  302. /* Check if the RTL8169 has completed retrieving data from the specified MII register */
  303. if (RTL_R32(PHYAR) & 0x80000000) {
  304. value = (int) (RTL_R32(PHYAR) & 0xFFFF);
  305. break;
  306. } else {
  307. udelay(100);
  308. }
  309. }
  310. return value;
  311. }
  312. static int rtl8169_init_board(struct eth_device *dev)
  313. {
  314. int i;
  315. u32 tmp;
  316. #ifdef DEBUG_RTL8169
  317. printf ("%s\n", __FUNCTION__);
  318. #endif
  319. ioaddr = dev->iobase;
  320. /* Soft reset the chip. */
  321. RTL_W8(ChipCmd, CmdReset);
  322. /* Check that the chip has finished the reset. */
  323. for (i = 1000; i > 0; i--)
  324. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  325. break;
  326. else
  327. udelay(10);
  328. /* identify chip attached to board */
  329. tmp = RTL_R32(TxConfig);
  330. tmp = ((tmp & 0x7c000000) + ((tmp & 0x00800000) << 2)) >> 24;
  331. for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--){
  332. if (tmp == rtl_chip_info[i].version) {
  333. tpc->chipset = i;
  334. goto match;
  335. }
  336. }
  337. /* if unknown chip, assume array element #0, original RTL-8169 in this case */
  338. printf("PCI device %s: unknown chip version, assuming RTL-8169\n", dev->name);
  339. printf("PCI device: TxConfig = 0x%hX\n", (unsigned long) RTL_R32(TxConfig));
  340. tpc->chipset = 0;
  341. match:
  342. return 0;
  343. }
  344. /**************************************************************************
  345. RECV - Receive a frame
  346. ***************************************************************************/
  347. static int rtl_recv(struct eth_device *dev)
  348. {
  349. /* return true if there's an ethernet packet ready to read */
  350. /* nic->packet should contain data on return */
  351. /* nic->packetlen should contain length of data */
  352. int cur_rx;
  353. int length = 0;
  354. #ifdef DEBUG_RTL8169_RX
  355. printf ("%s\n", __FUNCTION__);
  356. #endif
  357. ioaddr = dev->iobase;
  358. cur_rx = tpc->cur_rx;
  359. if ((le32_to_cpu(tpc->RxDescArray[cur_rx].status) & OWNbit) == 0) {
  360. if (!(le32_to_cpu(tpc->RxDescArray[cur_rx].status) & RxRES)) {
  361. unsigned char rxdata[RX_BUF_LEN];
  362. length = (int) (le32_to_cpu(tpc->RxDescArray[cur_rx].
  363. status) & 0x00001FFF) - 4;
  364. memcpy(rxdata, tpc->RxBufferRing[cur_rx], length);
  365. NetReceive(rxdata, length);
  366. if (cur_rx == NUM_RX_DESC - 1)
  367. tpc->RxDescArray[cur_rx].status =
  368. cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
  369. else
  370. tpc->RxDescArray[cur_rx].status =
  371. cpu_to_le32(OWNbit + RX_BUF_SIZE);
  372. tpc->RxDescArray[cur_rx].buf_addr =
  373. cpu_to_le32((unsigned long)tpc->RxBufferRing[cur_rx]);
  374. } else {
  375. puts("Error Rx");
  376. }
  377. cur_rx = (cur_rx + 1) % NUM_RX_DESC;
  378. tpc->cur_rx = cur_rx;
  379. return 1;
  380. } else {
  381. ushort sts = RTL_R8(IntrStatus);
  382. RTL_W8(IntrStatus, sts & ~(TxErr | RxErr | SYSErr));
  383. udelay(100); /* wait */
  384. }
  385. tpc->cur_rx = cur_rx;
  386. return (0); /* initially as this is called to flush the input */
  387. }
  388. #define HZ 1000
  389. /**************************************************************************
  390. SEND - Transmit a frame
  391. ***************************************************************************/
  392. static int rtl_send(struct eth_device *dev, volatile void *packet, int length)
  393. {
  394. /* send the packet to destination */
  395. u32 to;
  396. u8 *ptxb;
  397. int entry = tpc->cur_tx % NUM_TX_DESC;
  398. u32 len = length;
  399. int ret;
  400. #ifdef DEBUG_RTL8169_TX
  401. int stime = currticks();
  402. printf ("%s\n", __FUNCTION__);
  403. printf("sending %d bytes\n", len);
  404. #endif
  405. ioaddr = dev->iobase;
  406. /* point to the current txb incase multiple tx_rings are used */
  407. ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
  408. memcpy(ptxb, (char *)packet, (int)length);
  409. while (len < ETH_ZLEN)
  410. ptxb[len++] = '\0';
  411. tpc->TxDescArray[entry].buf_addr = cpu_to_le32((unsigned long)ptxb);
  412. if (entry != (NUM_TX_DESC - 1)) {
  413. tpc->TxDescArray[entry].status =
  414. cpu_to_le32((OWNbit | FSbit | LSbit) |
  415. ((len > ETH_ZLEN) ? len : ETH_ZLEN));
  416. } else {
  417. tpc->TxDescArray[entry].status =
  418. cpu_to_le32((OWNbit | EORbit | FSbit | LSbit) |
  419. ((len > ETH_ZLEN) ? len : ETH_ZLEN));
  420. }
  421. RTL_W8(TxPoll, 0x40); /* set polling bit */
  422. tpc->cur_tx++;
  423. to = currticks() + TX_TIMEOUT;
  424. while ((le32_to_cpu(tpc->TxDescArray[entry].status) & OWNbit)
  425. && (currticks() < to)); /* wait */
  426. if (currticks() >= to) {
  427. #ifdef DEBUG_RTL8169_TX
  428. puts ("tx timeout/error\n");
  429. printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
  430. #endif
  431. ret = 0;
  432. } else {
  433. #ifdef DEBUG_RTL8169_TX
  434. puts("tx done\n");
  435. #endif
  436. ret = length;
  437. }
  438. /* Delay to make net console (nc) work properly */
  439. udelay(20);
  440. return ret;
  441. }
  442. static void rtl8169_set_rx_mode(struct eth_device *dev)
  443. {
  444. u32 mc_filter[2]; /* Multicast hash filter */
  445. int rx_mode;
  446. u32 tmp = 0;
  447. #ifdef DEBUG_RTL8169
  448. printf ("%s\n", __FUNCTION__);
  449. #endif
  450. /* IFF_ALLMULTI */
  451. /* Too many to filter perfectly -- accept all multicasts. */
  452. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  453. mc_filter[1] = mc_filter[0] = 0xffffffff;
  454. tmp = rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
  455. rtl_chip_info[tpc->chipset].RxConfigMask);
  456. RTL_W32(RxConfig, tmp);
  457. RTL_W32(MAR0 + 0, mc_filter[0]);
  458. RTL_W32(MAR0 + 4, mc_filter[1]);
  459. }
  460. static void rtl8169_hw_start(struct eth_device *dev)
  461. {
  462. u32 i;
  463. #ifdef DEBUG_RTL8169
  464. int stime = currticks();
  465. printf ("%s\n", __FUNCTION__);
  466. #endif
  467. #if 0
  468. /* Soft reset the chip. */
  469. RTL_W8(ChipCmd, CmdReset);
  470. /* Check that the chip has finished the reset. */
  471. for (i = 1000; i > 0; i--) {
  472. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  473. break;
  474. else
  475. udelay(10);
  476. }
  477. #endif
  478. RTL_W8(Cfg9346, Cfg9346_Unlock);
  479. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  480. RTL_W8(EarlyTxThres, EarlyTxThld);
  481. /* For gigabit rtl8169 */
  482. RTL_W16(RxMaxSize, RxPacketMaxSize);
  483. /* Set Rx Config register */
  484. i = rtl8169_rx_config | (RTL_R32(RxConfig) &
  485. rtl_chip_info[tpc->chipset].RxConfigMask);
  486. RTL_W32(RxConfig, i);
  487. /* Set DMA burst size and Interframe Gap Time */
  488. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  489. (InterFrameGap << TxInterFrameGapShift));
  490. tpc->cur_rx = 0;
  491. RTL_W32(TxDescStartAddr, (unsigned long)tpc->TxDescArray);
  492. RTL_W32(RxDescStartAddr, (unsigned long)tpc->RxDescArray);
  493. RTL_W8(Cfg9346, Cfg9346_Lock);
  494. udelay(10);
  495. RTL_W32(RxMissed, 0);
  496. rtl8169_set_rx_mode(dev);
  497. /* no early-rx interrupts */
  498. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  499. #ifdef DEBUG_RTL8169
  500. printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
  501. #endif
  502. }
  503. static void rtl8169_init_ring(struct eth_device *dev)
  504. {
  505. int i;
  506. #ifdef DEBUG_RTL8169
  507. int stime = currticks();
  508. printf ("%s\n", __FUNCTION__);
  509. #endif
  510. tpc->cur_rx = 0;
  511. tpc->cur_tx = 0;
  512. tpc->dirty_tx = 0;
  513. memset(tpc->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
  514. memset(tpc->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
  515. for (i = 0; i < NUM_TX_DESC; i++) {
  516. tpc->Tx_skbuff[i] = &txb[i];
  517. }
  518. for (i = 0; i < NUM_RX_DESC; i++) {
  519. if (i == (NUM_RX_DESC - 1))
  520. tpc->RxDescArray[i].status =
  521. cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
  522. else
  523. tpc->RxDescArray[i].status =
  524. cpu_to_le32(OWNbit + RX_BUF_SIZE);
  525. tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
  526. tpc->RxDescArray[i].buf_addr =
  527. cpu_to_le32((unsigned long)tpc->RxBufferRing[i]);
  528. }
  529. #ifdef DEBUG_RTL8169
  530. printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
  531. #endif
  532. }
  533. /**************************************************************************
  534. RESET - Finish setting up the ethernet interface
  535. ***************************************************************************/
  536. static int rtl_reset(struct eth_device *dev, bd_t *bis)
  537. {
  538. int i;
  539. #ifdef DEBUG_RTL8169
  540. int stime = currticks();
  541. printf ("%s\n", __FUNCTION__);
  542. #endif
  543. tpc->TxDescArrays = tx_ring;
  544. /* Tx Desscriptor needs 256 bytes alignment; */
  545. tpc->TxDescArray = (struct TxDesc *) ((unsigned long)(tpc->TxDescArrays +
  546. 255) & ~255);
  547. tpc->RxDescArrays = rx_ring;
  548. /* Rx Desscriptor needs 256 bytes alignment; */
  549. tpc->RxDescArray = (struct RxDesc *) ((unsigned long)(tpc->RxDescArrays +
  550. 255) & ~255);
  551. rtl8169_init_ring(dev);
  552. rtl8169_hw_start(dev);
  553. /* Construct a perfect filter frame with the mac address as first match
  554. * and broadcast for all others */
  555. for (i = 0; i < 192; i++)
  556. txb[i] = 0xFF;
  557. txb[0] = dev->enetaddr[0];
  558. txb[1] = dev->enetaddr[1];
  559. txb[2] = dev->enetaddr[2];
  560. txb[3] = dev->enetaddr[3];
  561. txb[4] = dev->enetaddr[4];
  562. txb[5] = dev->enetaddr[5];
  563. #ifdef DEBUG_RTL8169
  564. printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
  565. #endif
  566. return 0;
  567. }
  568. /**************************************************************************
  569. HALT - Turn off ethernet interface
  570. ***************************************************************************/
  571. static void rtl_halt(struct eth_device *dev)
  572. {
  573. int i;
  574. #ifdef DEBUG_RTL8169
  575. printf ("%s\n", __FUNCTION__);
  576. #endif
  577. ioaddr = dev->iobase;
  578. /* Stop the chip's Tx and Rx DMA processes. */
  579. RTL_W8(ChipCmd, 0x00);
  580. /* Disable interrupts by clearing the interrupt mask. */
  581. RTL_W16(IntrMask, 0x0000);
  582. RTL_W32(RxMissed, 0);
  583. tpc->TxDescArrays = NULL;
  584. tpc->RxDescArrays = NULL;
  585. tpc->TxDescArray = NULL;
  586. tpc->RxDescArray = NULL;
  587. for (i = 0; i < NUM_RX_DESC; i++) {
  588. tpc->RxBufferRing[i] = NULL;
  589. }
  590. }
  591. /**************************************************************************
  592. INIT - Look for an adapter, this routine's visible to the outside
  593. ***************************************************************************/
  594. #define board_found 1
  595. #define valid_link 0
  596. static int rtl_init(struct eth_device *dev, bd_t *bis)
  597. {
  598. static int board_idx = -1;
  599. static int printed_version = 0;
  600. int i, rc;
  601. int option = -1, Cap10_100 = 0, Cap1000 = 0;
  602. #ifdef DEBUG_RTL8169
  603. printf ("%s\n", __FUNCTION__);
  604. #endif
  605. ioaddr = dev->iobase;
  606. board_idx++;
  607. printed_version = 1;
  608. /* point to private storage */
  609. tpc = &tpx;
  610. rc = rtl8169_init_board(dev);
  611. if (rc)
  612. return rc;
  613. /* Get MAC address. FIXME: read EEPROM */
  614. for (i = 0; i < MAC_ADDR_LEN; i++)
  615. bis->bi_enetaddr[i] = dev->enetaddr[i] = RTL_R8(MAC0 + i);
  616. #ifdef DEBUG_RTL8169
  617. printf("MAC Address");
  618. for (i = 0; i < MAC_ADDR_LEN; i++)
  619. printf(":%02x", dev->enetaddr[i]);
  620. putc('\n');
  621. #endif
  622. #ifdef DEBUG_RTL8169
  623. /* Print out some hardware info */
  624. printf("%s: at ioaddr 0x%x\n", dev->name, ioaddr);
  625. #endif
  626. /* if TBI is not endbled */
  627. if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
  628. int val = mdio_read(PHY_AUTO_NEGO_REG);
  629. option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
  630. /* Force RTL8169 in 10/100/1000 Full/Half mode. */
  631. if (option > 0) {
  632. #ifdef DEBUG_RTL8169
  633. printf("%s: Force-mode Enabled.\n", dev->name);
  634. #endif
  635. Cap10_100 = 0, Cap1000 = 0;
  636. switch (option) {
  637. case _10_Half:
  638. Cap10_100 = PHY_Cap_10_Half;
  639. Cap1000 = PHY_Cap_Null;
  640. break;
  641. case _10_Full:
  642. Cap10_100 = PHY_Cap_10_Full;
  643. Cap1000 = PHY_Cap_Null;
  644. break;
  645. case _100_Half:
  646. Cap10_100 = PHY_Cap_100_Half;
  647. Cap1000 = PHY_Cap_Null;
  648. break;
  649. case _100_Full:
  650. Cap10_100 = PHY_Cap_100_Full;
  651. Cap1000 = PHY_Cap_Null;
  652. break;
  653. case _1000_Full:
  654. Cap10_100 = PHY_Cap_Null;
  655. Cap1000 = PHY_Cap_1000_Full;
  656. break;
  657. default:
  658. break;
  659. }
  660. mdio_write(PHY_AUTO_NEGO_REG, Cap10_100 | (val & 0x1F)); /* leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
  661. mdio_write(PHY_1000_CTRL_REG, Cap1000);
  662. } else {
  663. #ifdef DEBUG_RTL8169
  664. printf("%s: Auto-negotiation Enabled.\n",
  665. dev->name);
  666. #endif
  667. /* enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
  668. mdio_write(PHY_AUTO_NEGO_REG,
  669. PHY_Cap_10_Half | PHY_Cap_10_Full |
  670. PHY_Cap_100_Half | PHY_Cap_100_Full |
  671. (val & 0x1F));
  672. /* enable 1000 Full Mode */
  673. mdio_write(PHY_1000_CTRL_REG, PHY_Cap_1000_Full);
  674. }
  675. /* Enable auto-negotiation and restart auto-nigotiation */
  676. mdio_write(PHY_CTRL_REG,
  677. PHY_Enable_Auto_Nego | PHY_Restart_Auto_Nego);
  678. udelay(100);
  679. /* wait for auto-negotiation process */
  680. for (i = 10000; i > 0; i--) {
  681. /* check if auto-negotiation complete */
  682. if (mdio_read(PHY_STAT_REG) & PHY_Auto_Nego_Comp) {
  683. udelay(100);
  684. option = RTL_R8(PHYstatus);
  685. if (option & _1000bpsF) {
  686. #ifdef DEBUG_RTL8169
  687. printf("%s: 1000Mbps Full-duplex operation.\n",
  688. dev->name);
  689. #endif
  690. } else {
  691. #ifdef DEBUG_RTL8169
  692. printf("%s: %sMbps %s-duplex operation.\n",
  693. dev->name,
  694. (option & _100bps) ? "100" :
  695. "10",
  696. (option & FullDup) ? "Full" :
  697. "Half");
  698. #endif
  699. }
  700. break;
  701. } else {
  702. udelay(100);
  703. }
  704. } /* end for-loop to wait for auto-negotiation process */
  705. } else {
  706. udelay(100);
  707. #ifdef DEBUG_RTL8169
  708. printf
  709. ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
  710. dev->name,
  711. (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
  712. #endif
  713. }
  714. return 1;
  715. }
  716. int rtl8169_initialize(bd_t *bis)
  717. {
  718. pci_dev_t devno;
  719. int card_number = 0;
  720. struct eth_device *dev;
  721. u32 iobase;
  722. int idx=0;
  723. while(1){
  724. /* Find RTL8169 */
  725. if ((devno = pci_find_devices(supported, idx++)) < 0)
  726. break;
  727. pci_read_config_dword(devno, PCI_BASE_ADDRESS_1, &iobase);
  728. iobase &= ~0xf;
  729. debug ("rtl8169: REALTEK RTL8169 @0x%x\n", iobase);
  730. dev = (struct eth_device *)malloc(sizeof *dev);
  731. sprintf (dev->name, "RTL8169#%d", card_number);
  732. dev->priv = (void *) devno;
  733. dev->iobase = (int)pci_mem_to_phys(devno, iobase);
  734. dev->init = rtl_reset;
  735. dev->halt = rtl_halt;
  736. dev->send = rtl_send;
  737. dev->recv = rtl_recv;
  738. eth_register (dev);
  739. rtl_init(dev, bis);
  740. card_number++;
  741. }
  742. return card_number;
  743. }