rtl8169.c 24 KB

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