rtl8139.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * rtl8139.c : U-Boot driver for the RealTek RTL8139
  4. *
  5. * Masami Komiya (mkomiya@sonare.it)
  6. *
  7. * Most part is taken from rtl8139.c of etherboot
  8. *
  9. */
  10. /* rtl8139.c - etherboot driver for the Realtek 8139 chipset
  11. *
  12. * ported from the linux driver written by Donald Becker
  13. * by Rainer Bawidamann (Rainer.Bawidamann@informatik.uni-ulm.de) 1999
  14. *
  15. * changes to the original driver:
  16. * - removed support for interrupts, switching to polling mode (yuck!)
  17. * - removed support for the 8129 chip (external MII)
  18. */
  19. /*********************************************************************/
  20. /* Revision History */
  21. /*********************************************************************/
  22. /*
  23. * 28 Dec 2002 ken_yap@users.sourceforge.net (Ken Yap)
  24. * Put in virt_to_bus calls to allow Etherboot relocation.
  25. *
  26. * 06 Apr 2001 ken_yap@users.sourceforge.net (Ken Yap)
  27. * Following email from Hyun-Joon Cha, added a disable routine, otherwise
  28. * NIC remains live and can crash the kernel later.
  29. *
  30. * 4 Feb 2000 espenlaub@informatik.uni-ulm.de (Klaus Espenlaub)
  31. * Shuffled things around, removed the leftovers from the 8129 support
  32. * that was in the Linux driver and added a bit more 8139 definitions.
  33. * Moved the 8K receive buffer to a fixed, available address outside the
  34. * 0x98000-0x9ffff range. This is a bit of a hack, but currently the only
  35. * way to make room for the Etherboot features that need substantial amounts
  36. * of code like the ANSI console support. Currently the buffer is just below
  37. * 0x10000, so this even conforms to the tagged boot image specification,
  38. * which reserves the ranges 0x00000-0x10000 and 0x98000-0xA0000. My
  39. * interpretation of this "reserved" is that Etherboot may do whatever it
  40. * likes, as long as its environment is kept intact (like the BIOS
  41. * variables). Hopefully fixed rtl8139_recv() once and for all. The symptoms
  42. * were that if Etherboot was left at the boot menu for several minutes, the
  43. * first eth_poll failed. Seems like I am the only person who does this.
  44. * First of all I fixed the debugging code and then set out for a long bug
  45. * hunting session. It took me about a week full time work - poking around
  46. * various places in the driver, reading Don Becker's and Jeff Garzik's Linux
  47. * driver and even the FreeBSD driver (what a piece of crap!) - and
  48. * eventually spotted the nasty thing: the transmit routine was acknowledging
  49. * each and every interrupt pending, including the RxOverrun and RxFIFIOver
  50. * interrupts. This confused the RTL8139 thoroughly. It destroyed the
  51. * Rx ring contents by dumping the 2K FIFO contents right where we wanted to
  52. * get the next packet. Oh well, what fun.
  53. *
  54. * 18 Jan 2000 mdc@thinguin.org (Marty Connor)
  55. * Drastically simplified error handling. Basically, if any error
  56. * in transmission or reception occurs, the card is reset.
  57. * Also, pointed all transmit descriptors to the same buffer to
  58. * save buffer space. This should decrease driver size and avoid
  59. * corruption because of exceeding 32K during runtime.
  60. *
  61. * 28 Jul 1999 (Matthias Meixner - meixner@rbg.informatik.tu-darmstadt.de)
  62. * rtl8139_recv was quite broken: it used the RxOK interrupt flag instead
  63. * of the RxBufferEmpty flag which often resulted in very bad
  64. * transmission performace - below 1kBytes/s.
  65. *
  66. */
  67. #include <common.h>
  68. #include <cpu_func.h>
  69. #include <dm.h>
  70. #include <log.h>
  71. #include <malloc.h>
  72. #include <net.h>
  73. #include <netdev.h>
  74. #include <asm/io.h>
  75. #include <pci.h>
  76. #include <linux/bitops.h>
  77. #include <linux/delay.h>
  78. #include <linux/types.h>
  79. #define RTL_TIMEOUT 100000
  80. /* PCI Tuning Parameters */
  81. /* Threshold is bytes transferred to chip before transmission starts. */
  82. #define TX_FIFO_THRESH 256 /* In bytes, rounded down to 32 byte units. */
  83. #define RX_FIFO_THRESH 4 /* Rx buffer level before first PCI xfer. */
  84. #define RX_DMA_BURST 4 /* Maximum PCI burst, '4' is 256 bytes */
  85. #define TX_DMA_BURST 4 /* Calculate as 16<<val. */
  86. #define NUM_TX_DESC 4 /* Number of Tx descriptor registers. */
  87. #define TX_BUF_SIZE ETH_FRAME_LEN /* FCS is added by the chip */
  88. #define RX_BUF_LEN_IDX 0 /* 0, 1, 2 is allowed - 8,16,32K rx buffer */
  89. #define RX_BUF_LEN (8192 << RX_BUF_LEN_IDX)
  90. #define DEBUG_TX 0 /* set to 1 to enable debug code */
  91. #define DEBUG_RX 0 /* set to 1 to enable debug code */
  92. #define bus_to_phys(devno, a) dm_pci_mem_to_phys((devno), (a))
  93. #define phys_to_bus(devno, a) dm_pci_phys_to_mem((devno), (a))
  94. /* Symbolic offsets to registers. */
  95. /* Ethernet hardware address. */
  96. #define RTL_REG_MAC0 0x00
  97. /* Multicast filter. */
  98. #define RTL_REG_MAR0 0x08
  99. /* Transmit status (four 32bit registers). */
  100. #define RTL_REG_TXSTATUS0 0x10
  101. /* Tx descriptors (also four 32bit). */
  102. #define RTL_REG_TXADDR0 0x20
  103. #define RTL_REG_RXBUF 0x30
  104. #define RTL_REG_RXEARLYCNT 0x34
  105. #define RTL_REG_RXEARLYSTATUS 0x36
  106. #define RTL_REG_CHIPCMD 0x37
  107. #define RTL_REG_CHIPCMD_CMDRESET BIT(4)
  108. #define RTL_REG_CHIPCMD_CMDRXENB BIT(3)
  109. #define RTL_REG_CHIPCMD_CMDTXENB BIT(2)
  110. #define RTL_REG_CHIPCMD_RXBUFEMPTY BIT(0)
  111. #define RTL_REG_RXBUFPTR 0x38
  112. #define RTL_REG_RXBUFADDR 0x3A
  113. #define RTL_REG_INTRMASK 0x3C
  114. #define RTL_REG_INTRSTATUS 0x3E
  115. #define RTL_REG_INTRSTATUS_PCIERR BIT(15)
  116. #define RTL_REG_INTRSTATUS_PCSTIMEOUT BIT(14)
  117. #define RTL_REG_INTRSTATUS_CABLELENCHANGE BIT(13)
  118. #define RTL_REG_INTRSTATUS_RXFIFOOVER BIT(6)
  119. #define RTL_REG_INTRSTATUS_RXUNDERRUN BIT(5)
  120. #define RTL_REG_INTRSTATUS_RXOVERFLOW BIT(4)
  121. #define RTL_REG_INTRSTATUS_TXERR BIT(3)
  122. #define RTL_REG_INTRSTATUS_TXOK BIT(2)
  123. #define RTL_REG_INTRSTATUS_RXERR BIT(1)
  124. #define RTL_REG_INTRSTATUS_RXOK BIT(0)
  125. #define RTL_REG_TXCONFIG 0x40
  126. #define RTL_REG_RXCONFIG 0x44
  127. #define RTL_REG_RXCONFIG_RXCFGWRAP BIT(7)
  128. #define RTL_REG_RXCONFIG_ACCEPTERR BIT(5)
  129. #define RTL_REG_RXCONFIG_ACCEPTRUNT BIT(4)
  130. #define RTL_REG_RXCONFIG_ACCEPTBROADCAST BIT(3)
  131. #define RTL_REG_RXCONFIG_ACCEPTMULTICAST BIT(2)
  132. #define RTL_REG_RXCONFIG_ACCEPTMYPHYS BIT(1)
  133. #define RTL_REG_RXCONFIG_ACCEPTALLPHYS BIT(0)
  134. /* general-purpose counter. */
  135. #define RTL_REG_TIMER 0x48
  136. /* 24 bits valid, write clears. */
  137. #define RTL_REG_RXMISSED 0x4C
  138. #define RTL_REG_CFG9346 0x50
  139. #define RTL_REG_CONFIG0 0x51
  140. #define RTL_REG_CONFIG1 0x52
  141. /* intr if gp counter reaches this value */
  142. #define RTL_REG_TIMERINTRREG 0x54
  143. #define RTL_REG_MEDIASTATUS 0x58
  144. #define RTL_REG_MEDIASTATUS_MSRTXFLOWENABLE BIT(7)
  145. #define RTL_REG_MEDIASTATUS_MSRRXFLOWENABLE BIT(6)
  146. #define RTL_REG_MEDIASTATUS_MSRSPEED10 BIT(3)
  147. #define RTL_REG_MEDIASTATUS_MSRLINKFAIL BIT(2)
  148. #define RTL_REG_MEDIASTATUS_MSRRXPAUSEFLAG BIT(1)
  149. #define RTL_REG_MEDIASTATUS_MSRTXPAUSEFLAG BIT(0)
  150. #define RTL_REG_CONFIG3 0x59
  151. #define RTL_REG_MULTIINTR 0x5C
  152. /* revision of the RTL8139 chip */
  153. #define RTL_REG_REVISIONID 0x5E
  154. #define RTL_REG_TXSUMMARY 0x60
  155. #define RTL_REG_MII_BMCR 0x62
  156. #define RTL_REG_MII_BMSR 0x64
  157. #define RTL_REG_NWAYADVERT 0x66
  158. #define RTL_REG_NWAYLPAR 0x68
  159. #define RTL_REG_NWAYEXPANSION 0x6A
  160. #define RTL_REG_DISCONNECTCNT 0x6C
  161. #define RTL_REG_FALSECARRIERCNT 0x6E
  162. #define RTL_REG_NWAYTESTREG 0x70
  163. /* packet received counter */
  164. #define RTL_REG_RXCNT 0x72
  165. /* chip status and configuration register */
  166. #define RTL_REG_CSCR 0x74
  167. #define RTL_REG_PHYPARM1 0x78
  168. #define RTL_REG_TWISTERPARM 0x7c
  169. /* undocumented */
  170. #define RTL_REG_PHYPARM2 0x80
  171. /*
  172. * from 0x84 onwards are a number of power management/wakeup frame
  173. * definitions we will probably never need to know about.
  174. */
  175. #define RTL_STS_RXMULTICAST BIT(15)
  176. #define RTL_STS_RXPHYSICAL BIT(14)
  177. #define RTL_STS_RXBROADCAST BIT(13)
  178. #define RTL_STS_RXBADSYMBOL BIT(5)
  179. #define RTL_STS_RXRUNT BIT(4)
  180. #define RTL_STS_RXTOOLONG BIT(3)
  181. #define RTL_STS_RXCRCERR BIT(2)
  182. #define RTL_STS_RXBADALIGN BIT(1)
  183. #define RTL_STS_RXSTATUSOK BIT(0)
  184. struct rtl8139_priv {
  185. struct udevice *devno;
  186. unsigned int rxstatus;
  187. unsigned int cur_rx;
  188. unsigned int cur_tx;
  189. unsigned long ioaddr;
  190. unsigned char enetaddr[6];
  191. };
  192. /* The RTL8139 can only transmit from a contiguous, aligned memory block. */
  193. static unsigned char tx_buffer[TX_BUF_SIZE] __aligned(4);
  194. static unsigned char rx_ring[RX_BUF_LEN + 16] __aligned(4);
  195. /* Serial EEPROM section. */
  196. /* EEPROM_Ctrl bits. */
  197. #define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
  198. #define EE_CS 0x08 /* EEPROM chip select. */
  199. #define EE_DATA_WRITE 0x02 /* EEPROM chip data in. */
  200. #define EE_WRITE_0 0x00
  201. #define EE_WRITE_1 0x02
  202. #define EE_DATA_READ 0x01 /* EEPROM chip data out. */
  203. #define EE_ENB (0x80 | EE_CS)
  204. /* The EEPROM commands include the alway-set leading bit. */
  205. #define EE_WRITE_CMD 5
  206. #define EE_READ_CMD 6
  207. #define EE_ERASE_CMD 7
  208. static void rtl8139_eeprom_delay(struct rtl8139_priv *priv)
  209. {
  210. /*
  211. * Delay between EEPROM clock transitions.
  212. * No extra delay is needed with 33MHz PCI, but 66MHz may change this.
  213. */
  214. inl(priv->ioaddr + RTL_REG_CFG9346);
  215. }
  216. static int rtl8139_read_eeprom(struct rtl8139_priv *priv,
  217. unsigned int location, unsigned int addr_len)
  218. {
  219. unsigned int read_cmd = location | (EE_READ_CMD << addr_len);
  220. uintptr_t ee_addr = priv->ioaddr + RTL_REG_CFG9346;
  221. unsigned int retval = 0;
  222. u8 dataval;
  223. int i;
  224. outb(EE_ENB & ~EE_CS, ee_addr);
  225. outb(EE_ENB, ee_addr);
  226. rtl8139_eeprom_delay(priv);
  227. /* Shift the read command bits out. */
  228. for (i = 4 + addr_len; i >= 0; i--) {
  229. dataval = (read_cmd & BIT(i)) ? EE_DATA_WRITE : 0;
  230. outb(EE_ENB | dataval, ee_addr);
  231. rtl8139_eeprom_delay(priv);
  232. outb(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
  233. rtl8139_eeprom_delay(priv);
  234. }
  235. outb(EE_ENB, ee_addr);
  236. rtl8139_eeprom_delay(priv);
  237. for (i = 16; i > 0; i--) {
  238. outb(EE_ENB | EE_SHIFT_CLK, ee_addr);
  239. rtl8139_eeprom_delay(priv);
  240. retval <<= 1;
  241. retval |= inb(ee_addr) & EE_DATA_READ;
  242. outb(EE_ENB, ee_addr);
  243. rtl8139_eeprom_delay(priv);
  244. }
  245. /* Terminate the EEPROM access. */
  246. outb(~EE_CS, ee_addr);
  247. rtl8139_eeprom_delay(priv);
  248. return retval;
  249. }
  250. static const unsigned int rtl8139_rx_config =
  251. (RX_BUF_LEN_IDX << 11) |
  252. (RX_FIFO_THRESH << 13) |
  253. (RX_DMA_BURST << 8);
  254. static void rtl8139_set_rx_mode(struct rtl8139_priv *priv)
  255. {
  256. /* !IFF_PROMISC */
  257. unsigned int rx_mode = RTL_REG_RXCONFIG_ACCEPTBROADCAST |
  258. RTL_REG_RXCONFIG_ACCEPTMULTICAST |
  259. RTL_REG_RXCONFIG_ACCEPTMYPHYS;
  260. outl(rtl8139_rx_config | rx_mode, priv->ioaddr + RTL_REG_RXCONFIG);
  261. outl(0xffffffff, priv->ioaddr + RTL_REG_MAR0 + 0);
  262. outl(0xffffffff, priv->ioaddr + RTL_REG_MAR0 + 4);
  263. }
  264. static void rtl8139_hw_reset(struct rtl8139_priv *priv)
  265. {
  266. u8 reg;
  267. int i;
  268. outb(RTL_REG_CHIPCMD_CMDRESET, priv->ioaddr + RTL_REG_CHIPCMD);
  269. /* Give the chip 10ms to finish the reset. */
  270. for (i = 0; i < 100; i++) {
  271. reg = inb(priv->ioaddr + RTL_REG_CHIPCMD);
  272. if (!(reg & RTL_REG_CHIPCMD_CMDRESET))
  273. break;
  274. udelay(100);
  275. }
  276. }
  277. static void rtl8139_reset(struct rtl8139_priv *priv)
  278. {
  279. int i;
  280. priv->cur_rx = 0;
  281. priv->cur_tx = 0;
  282. rtl8139_hw_reset(priv);
  283. for (i = 0; i < ETH_ALEN; i++)
  284. outb(priv->enetaddr[i], priv->ioaddr + RTL_REG_MAC0 + i);
  285. /* Must enable Tx/Rx before setting transfer thresholds! */
  286. outb(RTL_REG_CHIPCMD_CMDRXENB | RTL_REG_CHIPCMD_CMDTXENB,
  287. priv->ioaddr + RTL_REG_CHIPCMD);
  288. /* accept no frames yet! */
  289. outl(rtl8139_rx_config, priv->ioaddr + RTL_REG_RXCONFIG);
  290. outl((TX_DMA_BURST << 8) | 0x03000000, priv->ioaddr + RTL_REG_TXCONFIG);
  291. /*
  292. * The Linux driver changes RTL_REG_CONFIG1 here to use a different
  293. * LED pattern for half duplex or full/autodetect duplex (for
  294. * full/autodetect, the outputs are TX/RX, Link10/100, FULL, while
  295. * for half duplex it uses TX/RX, Link100, Link10). This is messy,
  296. * because it doesn't match the inscription on the mounting bracket.
  297. * It should not be changed from the configuration EEPROM default,
  298. * because the card manufacturer should have set that to match the
  299. * card.
  300. */
  301. debug_cond(DEBUG_RX, "rx ring address is %p\n", rx_ring);
  302. flush_cache((unsigned long)rx_ring, RX_BUF_LEN);
  303. outl(phys_to_bus(priv->devno, (int)rx_ring), priv->ioaddr + RTL_REG_RXBUF);
  304. /*
  305. * If we add multicast support, the RTL_REG_MAR0 register would have
  306. * to be initialized to 0xffffffffffffffff (two 32 bit accesses).
  307. * Etherboot only needs broadcast (for ARP/RARP/BOOTP/DHCP) and
  308. * unicast.
  309. */
  310. outb(RTL_REG_CHIPCMD_CMDRXENB | RTL_REG_CHIPCMD_CMDTXENB,
  311. priv->ioaddr + RTL_REG_CHIPCMD);
  312. outl(rtl8139_rx_config, priv->ioaddr + RTL_REG_RXCONFIG);
  313. /* Start the chip's Tx and Rx process. */
  314. outl(0, priv->ioaddr + RTL_REG_RXMISSED);
  315. rtl8139_set_rx_mode(priv);
  316. /* Disable all known interrupts by setting the interrupt mask. */
  317. outw(0, priv->ioaddr + RTL_REG_INTRMASK);
  318. }
  319. static int rtl8139_send_common(struct rtl8139_priv *priv,
  320. void *packet, int length)
  321. {
  322. unsigned int len = length;
  323. unsigned long txstatus;
  324. unsigned int status;
  325. int i = 0;
  326. memcpy(tx_buffer, packet, length);
  327. debug_cond(DEBUG_TX, "sending %d bytes\n", len);
  328. /*
  329. * Note: RTL8139 doesn't auto-pad, send minimum payload (another 4
  330. * bytes are sent automatically for the FCS, totalling to 64 bytes).
  331. */
  332. while (len < ETH_ZLEN)
  333. tx_buffer[len++] = '\0';
  334. flush_cache((unsigned long)tx_buffer, length);
  335. outl(phys_to_bus(priv->devno, (unsigned long)tx_buffer),
  336. priv->ioaddr + RTL_REG_TXADDR0 + priv->cur_tx * 4);
  337. outl(((TX_FIFO_THRESH << 11) & 0x003f0000) | len,
  338. priv->ioaddr + RTL_REG_TXSTATUS0 + priv->cur_tx * 4);
  339. do {
  340. status = inw(priv->ioaddr + RTL_REG_INTRSTATUS);
  341. /*
  342. * Only acknlowledge interrupt sources we can properly
  343. * handle here - the RTL_REG_INTRSTATUS_RXOVERFLOW/
  344. * RTL_REG_INTRSTATUS_RXFIFOOVER MUST be handled in the
  345. * rtl8139_recv() function.
  346. */
  347. status &= RTL_REG_INTRSTATUS_TXOK | RTL_REG_INTRSTATUS_TXERR |
  348. RTL_REG_INTRSTATUS_PCIERR;
  349. outw(status, priv->ioaddr + RTL_REG_INTRSTATUS);
  350. if (status)
  351. break;
  352. udelay(10);
  353. } while (i++ < RTL_TIMEOUT);
  354. txstatus = inl(priv->ioaddr + RTL_REG_TXSTATUS0 + priv->cur_tx * 4);
  355. if (!(status & RTL_REG_INTRSTATUS_TXOK)) {
  356. debug_cond(DEBUG_TX,
  357. "tx timeout/error (%d usecs), status %hX txstatus %lX\n",
  358. 10 * i, status, txstatus);
  359. rtl8139_reset(priv);
  360. return 0;
  361. }
  362. priv->cur_tx = (priv->cur_tx + 1) % NUM_TX_DESC;
  363. debug_cond(DEBUG_TX, "tx done, status %hX txstatus %lX\n",
  364. status, txstatus);
  365. return length;
  366. }
  367. static int rtl8139_recv_common(struct rtl8139_priv *priv, unsigned char *rxdata,
  368. uchar **packetp)
  369. {
  370. const unsigned int rxstat = RTL_REG_INTRSTATUS_RXFIFOOVER |
  371. RTL_REG_INTRSTATUS_RXOVERFLOW |
  372. RTL_REG_INTRSTATUS_RXOK;
  373. unsigned int rx_size, rx_status;
  374. unsigned int ring_offs;
  375. int length = 0;
  376. if (inb(priv->ioaddr + RTL_REG_CHIPCMD) & RTL_REG_CHIPCMD_RXBUFEMPTY)
  377. return 0;
  378. priv->rxstatus = inw(priv->ioaddr + RTL_REG_INTRSTATUS);
  379. /* See below for the rest of the interrupt acknowledges. */
  380. outw(priv->rxstatus & ~rxstat, priv->ioaddr + RTL_REG_INTRSTATUS);
  381. debug_cond(DEBUG_RX, "%s: int %hX ", __func__, priv->rxstatus);
  382. ring_offs = priv->cur_rx % RX_BUF_LEN;
  383. /* ring_offs is guaranteed being 4-byte aligned */
  384. rx_status = le32_to_cpu(*(unsigned int *)(rx_ring + ring_offs));
  385. rx_size = rx_status >> 16;
  386. rx_status &= 0xffff;
  387. if ((rx_status & (RTL_STS_RXBADSYMBOL | RTL_STS_RXRUNT |
  388. RTL_STS_RXTOOLONG | RTL_STS_RXCRCERR |
  389. RTL_STS_RXBADALIGN)) ||
  390. (rx_size < ETH_ZLEN) ||
  391. (rx_size > ETH_FRAME_LEN + 4)) {
  392. printf("rx error %hX\n", rx_status);
  393. /* this clears all interrupts still pending */
  394. rtl8139_reset(priv);
  395. return 0;
  396. }
  397. /* Received a good packet */
  398. length = rx_size - 4; /* no one cares about the FCS */
  399. if (ring_offs + 4 + rx_size - 4 > RX_BUF_LEN) {
  400. int semi_count = RX_BUF_LEN - ring_offs - 4;
  401. memcpy(rxdata, rx_ring + ring_offs + 4, semi_count);
  402. memcpy(&rxdata[semi_count], rx_ring,
  403. rx_size - 4 - semi_count);
  404. *packetp = rxdata;
  405. debug_cond(DEBUG_RX, "rx packet %d+%d bytes",
  406. semi_count, rx_size - 4 - semi_count);
  407. } else {
  408. *packetp = rx_ring + ring_offs + 4;
  409. debug_cond(DEBUG_RX, "rx packet %d bytes", rx_size - 4);
  410. }
  411. return length;
  412. }
  413. static int rtl8139_free_pkt_common(struct rtl8139_priv *priv, unsigned int len)
  414. {
  415. const unsigned int rxstat = RTL_REG_INTRSTATUS_RXFIFOOVER |
  416. RTL_REG_INTRSTATUS_RXOVERFLOW |
  417. RTL_REG_INTRSTATUS_RXOK;
  418. unsigned int rx_size = len + 4;
  419. flush_cache((unsigned long)rx_ring, RX_BUF_LEN);
  420. priv->cur_rx = ROUND(priv->cur_rx + rx_size + 4, 4);
  421. outw(priv->cur_rx - 16, priv->ioaddr + RTL_REG_RXBUFPTR);
  422. /*
  423. * See RTL8139 Programming Guide V0.1 for the official handling of
  424. * Rx overflow situations. The document itself contains basically
  425. * no usable information, except for a few exception handling rules.
  426. */
  427. outw(priv->rxstatus & rxstat, priv->ioaddr + RTL_REG_INTRSTATUS);
  428. return 0;
  429. }
  430. static int rtl8139_init_common(struct rtl8139_priv *priv)
  431. {
  432. u8 reg;
  433. /* Bring the chip out of low-power mode. */
  434. outb(0x00, priv->ioaddr + RTL_REG_CONFIG1);
  435. rtl8139_reset(priv);
  436. reg = inb(priv->ioaddr + RTL_REG_MEDIASTATUS);
  437. if (reg & RTL_REG_MEDIASTATUS_MSRLINKFAIL) {
  438. printf("Cable not connected or other link failure\n");
  439. return -1;
  440. }
  441. return 0;
  442. }
  443. static void rtl8139_stop_common(struct rtl8139_priv *priv)
  444. {
  445. rtl8139_hw_reset(priv);
  446. }
  447. static void rtl8139_get_hwaddr(struct rtl8139_priv *priv)
  448. {
  449. unsigned short *ap = (unsigned short *)priv->enetaddr;
  450. int i, addr_len;
  451. /* Bring the chip out of low-power mode. */
  452. outb(0x00, priv->ioaddr + RTL_REG_CONFIG1);
  453. addr_len = rtl8139_read_eeprom(priv, 0, 8) == 0x8129 ? 8 : 6;
  454. for (i = 0; i < 3; i++)
  455. *ap++ = le16_to_cpu(rtl8139_read_eeprom(priv, i + 7, addr_len));
  456. }
  457. static void rtl8139_name(char *str, int card_number)
  458. {
  459. sprintf(str, "RTL8139#%u", card_number);
  460. }
  461. static struct pci_device_id supported[] = {
  462. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, PCI_DEVICE_ID_REALTEK_8139) },
  463. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, PCI_DEVICE_ID_DLINK_8139) },
  464. { }
  465. };
  466. static int rtl8139_start(struct udevice *dev)
  467. {
  468. struct eth_pdata *plat = dev_get_plat(dev);
  469. struct rtl8139_priv *priv = dev_get_priv(dev);
  470. memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
  471. return rtl8139_init_common(priv);
  472. }
  473. static void rtl8139_stop(struct udevice *dev)
  474. {
  475. struct rtl8139_priv *priv = dev_get_priv(dev);
  476. rtl8139_stop_common(priv);
  477. }
  478. static int rtl8139_send(struct udevice *dev, void *packet, int length)
  479. {
  480. struct rtl8139_priv *priv = dev_get_priv(dev);
  481. int ret;
  482. ret = rtl8139_send_common(priv, packet, length);
  483. return ret ? 0 : -ETIMEDOUT;
  484. }
  485. static int rtl8139_recv(struct udevice *dev, int flags, uchar **packetp)
  486. {
  487. struct rtl8139_priv *priv = dev_get_priv(dev);
  488. static unsigned char rxdata[RX_BUF_LEN];
  489. return rtl8139_recv_common(priv, rxdata, packetp);
  490. }
  491. static int rtl8139_free_pkt(struct udevice *dev, uchar *packet, int length)
  492. {
  493. struct rtl8139_priv *priv = dev_get_priv(dev);
  494. rtl8139_free_pkt_common(priv, length);
  495. return 0;
  496. }
  497. static int rtl8139_write_hwaddr(struct udevice *dev)
  498. {
  499. struct eth_pdata *plat = dev_get_plat(dev);
  500. struct rtl8139_priv *priv = dev_get_priv(dev);
  501. memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
  502. rtl8139_reset(priv);
  503. return 0;
  504. }
  505. static int rtl8139_read_rom_hwaddr(struct udevice *dev)
  506. {
  507. struct rtl8139_priv *priv = dev_get_priv(dev);
  508. rtl8139_get_hwaddr(priv);
  509. return 0;
  510. }
  511. static int rtl8139_bind(struct udevice *dev)
  512. {
  513. static int card_number;
  514. char name[16];
  515. rtl8139_name(name, card_number++);
  516. return device_set_name(dev, name);
  517. }
  518. static int rtl8139_probe(struct udevice *dev)
  519. {
  520. struct eth_pdata *plat = dev_get_plat(dev);
  521. struct rtl8139_priv *priv = dev_get_priv(dev);
  522. u32 iobase;
  523. dm_pci_read_config32(dev, PCI_BASE_ADDRESS_1, &iobase);
  524. iobase &= ~0xf;
  525. debug("rtl8139: REALTEK RTL8139 @0x%x\n", iobase);
  526. priv->devno = dev;
  527. priv->ioaddr = (unsigned long)bus_to_phys(dev, iobase);
  528. rtl8139_get_hwaddr(priv);
  529. memcpy(plat->enetaddr, priv->enetaddr, sizeof(priv->enetaddr));
  530. dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x20);
  531. return 0;
  532. }
  533. static const struct eth_ops rtl8139_ops = {
  534. .start = rtl8139_start,
  535. .send = rtl8139_send,
  536. .recv = rtl8139_recv,
  537. .stop = rtl8139_stop,
  538. .free_pkt = rtl8139_free_pkt,
  539. .write_hwaddr = rtl8139_write_hwaddr,
  540. .read_rom_hwaddr = rtl8139_read_rom_hwaddr,
  541. };
  542. U_BOOT_DRIVER(eth_rtl8139) = {
  543. .name = "eth_rtl8139",
  544. .id = UCLASS_ETH,
  545. .bind = rtl8139_bind,
  546. .probe = rtl8139_probe,
  547. .ops = &rtl8139_ops,
  548. .priv_auto = sizeof(struct rtl8139_priv),
  549. .plat_auto = sizeof(struct eth_pdata),
  550. };
  551. U_BOOT_PCI_DEVICE(eth_rtl8139, supported);