rtl8169.c 28 KB

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