rtl8169.c 25 KB

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