rtl8169.c 23 KB

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