rtl8169.c 25 KB

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