1 /* rtl8139.c - etherboot driver for the Realtek 8139 chipset
3 ported from the linux driver written by Donald Becker
4 by Rainer Bawidamann (Rainer.Bawidamann@informatik.uni-ulm.de) 1999
6 This software may be used and distributed according to the terms
7 of the GNU Public License, incorporated herein by reference.
9 changes to the original driver:
10 - removed support for interrupts, switching to polling mode (yuck!)
11 - removed support for the 8129 chip (external MII)
15 /*********************************************************************/
16 /* Revision History */
17 /*********************************************************************/
20 27 May 2006 mcb30@users.sourceforge.net (Michael Brown)
21 Rewrote to use the new net driver API, the updated PCI API, and
22 the generic three-wire serial device support for EEPROM access.
24 28 Dec 2002 ken_yap@users.sourceforge.net (Ken Yap)
25 Put in virt_to_bus calls to allow Etherboot relocation.
27 06 Apr 2001 ken_yap@users.sourceforge.net (Ken Yap)
28 Following email from Hyun-Joon Cha, added a disable routine, otherwise
29 NIC remains live and can crash the kernel later.
31 4 Feb 2000 espenlaub@informatik.uni-ulm.de (Klaus Espenlaub)
32 Shuffled things around, removed the leftovers from the 8129 support
33 that was in the Linux driver and added a bit more 8139 definitions.
34 Moved the 8K receive buffer to a fixed, available address outside the
35 0x98000-0x9ffff range. This is a bit of a hack, but currently the only
36 way to make room for the Etherboot features that need substantial amounts
37 of code like the ANSI console support. Currently the buffer is just below
38 0x10000, so this even conforms to the tagged boot image specification,
39 which reserves the ranges 0x00000-0x10000 and 0x98000-0xA0000. My
40 interpretation of this "reserved" is that Etherboot may do whatever it
41 likes, as long as its environment is kept intact (like the BIOS
42 variables). Hopefully fixed rtl_poll() once and for all. The symptoms
43 were that if Etherboot was left at the boot menu for several minutes, the
44 first eth_poll failed. Seems like I am the only person who does this.
45 First of all I fixed the debugging code and then set out for a long bug
46 hunting session. It took me about a week full time work - poking around
47 various places in the driver, reading Don Becker's and Jeff Garzik's Linux
48 driver and even the FreeBSD driver (what a piece of crap!) - and
49 eventually spotted the nasty thing: the transmit routine was acknowledging
50 each and every interrupt pending, including the RxOverrun and RxFIFIOver
51 interrupts. This confused the RTL8139 thoroughly. It destroyed the
52 Rx ring contents by dumping the 2K FIFO contents right where we wanted to
53 get the next packet. Oh well, what fun.
55 18 Jan 2000 mdc@thinguin.org (Marty Connor)
56 Drastically simplified error handling. Basically, if any error
57 in transmission or reception occurs, the card is reset.
58 Also, pointed all transmit descriptors to the same buffer to
59 save buffer space. This should decrease driver size and avoid
60 corruption because of exceeding 32K during runtime.
62 28 Jul 1999 (Matthias Meixner - meixner@rbg.informatik.tu-darmstadt.de)
63 rtl_poll was quite broken: it used the RxOK interrupt flag instead
64 of the RxBufferEmpty flag which often resulted in very bad
65 transmission performace - below 1kBytes/s.
76 #include <gpxe/if_ether.h>
77 #include <gpxe/ethernet.h>
78 #include <gpxe/pkbuff.h>
79 #include <gpxe/netdevice.h>
80 #include <gpxe/spi_bit.h>
81 #include <gpxe/threewire.h>
84 #define TX_RING_SIZE 4
88 struct pk_buff *pkb[TX_RING_SIZE];
97 unsigned short ioaddr;
100 struct spi_bit_basher spibit;
101 struct spi_device eeprom;
102 struct nvo_block nvo;
105 /* Tuning Parameters */
106 #define TX_FIFO_THRESH 256 /* In bytes, rounded down to 32 byte units. */
107 #define RX_FIFO_THRESH 4 /* Rx buffer level before first PCI xfer. */
108 #define RX_DMA_BURST 4 /* Maximum PCI burst, '4' is 256 bytes */
109 #define TX_DMA_BURST 4 /* Calculate as 16<<val. */
110 #define TX_IPG 3 /* This is the only valid value */
111 #define RX_BUF_LEN_IDX 0 /* 0, 1, 2 is allowed - 8,16,32K rx buffer */
112 #define RX_BUF_LEN ( (8192 << RX_BUF_LEN_IDX) )
115 /* Symbolic offsets to registers. */
116 enum RTL8139_registers {
117 MAC0=0, /* Ethernet hardware address. */
118 MAR0=8, /* Multicast filter. */
119 TxStatus0=0x10, /* Transmit status (four 32bit registers). */
120 TxAddr0=0x20, /* Tx descriptors (also four 32bit). */
121 RxBuf=0x30, RxEarlyCnt=0x34, RxEarlyStatus=0x36,
122 ChipCmd=0x37, RxBufPtr=0x38, RxBufAddr=0x3A,
123 IntrMask=0x3C, IntrStatus=0x3E,
124 TxConfig=0x40, RxConfig=0x44,
125 Timer=0x48, /* general-purpose counter. */
126 RxMissed=0x4C, /* 24 bits valid, write clears. */
127 Cfg9346=0x50, Config0=0x51, Config1=0x52,
128 TimerIntrReg=0x54, /* intr if gp counter reaches this value */
132 RevisionID=0x5E, /* revision of the RTL8139 chip */
134 MII_BMCR=0x62, MII_BMSR=0x64, NWayAdvert=0x66, NWayLPAR=0x68,
136 DisconnectCnt=0x6C, FalseCarrierCnt=0x6E,
138 RxCnt=0x72, /* packet received counter */
139 CSCR=0x74, /* chip status and configuration register */
140 PhyParm1=0x78,TwisterParm=0x7c,PhyParm2=0x80, /* undocumented */
141 /* from 0x84 onwards are a number of power management/wakeup frame
142 * definitions we will probably never need to know about. */
145 enum RxEarlyStatusBits {
146 ERGood=0x08, ERBad=0x04, EROVW=0x02, EROK=0x01
150 CmdReset=0x10, CmdRxEnb=0x08, CmdTxEnb=0x04, RxBufEmpty=0x01, };
153 SERR=0x8000, TimeOut=0x4000, LenChg=0x2000,
154 FOVW=0x40, PUN_LinkChg=0x20, RXOVW=0x10,
155 TER=0x08, TOK=0x04, RER=0x02, ROK=0x01
158 /* Interrupt register bits, using my own meaningful names. */
159 enum IntrStatusBits {
160 PCIErr=0x8000, PCSTimeout=0x4000, CableLenChange= 0x2000,
161 RxFIFOOver=0x40, RxUnderrun=0x20, RxOverflow=0x10,
162 TxErr=0x08, TxOK=0x04, RxErr=0x02, RxOK=0x01,
165 TxHostOwns=0x2000, TxUnderrun=0x4000, TxStatOK=0x8000,
166 TxOutOfWindow=0x20000000, TxAborted=0x40000000,
167 TxCarrierLost=0x80000000,
170 RxMulticast=0x8000, RxPhysical=0x4000, RxBroadcast=0x2000,
171 RxBadSymbol=0x0020, RxRunt=0x0010, RxTooLong=0x0008, RxCRCErr=0x0004,
172 RxBadAlign=0x0002, RxStatusOK=0x0001,
175 enum MediaStatusBits {
176 MSRTxFlowEnable=0x80, MSRRxFlowEnable=0x40, MSRSpeed10=0x08,
177 MSRLinkFail=0x04, MSRRxPauseFlag=0x02, MSRTxPauseFlag=0x01,
181 BMCRReset=0x8000, BMCRSpeed100=0x2000, BMCRNWayEnable=0x1000,
182 BMCRRestartNWay=0x0200, BMCRDuplex=0x0100,
186 CSCR_LinkOKBit=0x0400, CSCR_LinkChangeBit=0x0800,
187 CSCR_LinkStatusBits=0x0f000, CSCR_LinkDownOffCmd=0x003c0,
188 CSCR_LinkDownCmd=0x0f3c0,
194 AcceptErr=0x20, AcceptRunt=0x10, AcceptBroadcast=0x08,
195 AcceptMulticast=0x04, AcceptMyPhys=0x02, AcceptAllPhys=0x01,
203 #define EE_M1 0x80 /* Mode select bit 1 */
204 #define EE_M0 0x40 /* Mode select bit 0 */
205 #define EE_CS 0x08 /* EEPROM chip select */
206 #define EE_SK 0x04 /* EEPROM shift clock */
207 #define EE_DI 0x02 /* Data in */
208 #define EE_DO 0x01 /* Data out */
210 /* Offsets within EEPROM (these are word offsets) */
213 static const uint8_t rtl_ee_bits[] = {
214 [SPI_BIT_SCLK] = EE_SK,
215 [SPI_BIT_MOSI] = EE_DI,
216 [SPI_BIT_MISO] = EE_DO,
217 [SPI_BIT_SS(0)] = ( EE_CS | EE_M1 ),
220 static int rtl_spi_read_bit ( struct bit_basher *basher,
221 unsigned int bit_id ) {
222 struct rtl8139_nic *rtl = container_of ( basher, struct rtl8139_nic,
224 uint8_t mask = rtl_ee_bits[bit_id];
227 eereg = inb ( rtl->ioaddr + Cfg9346 );
228 return ( eereg & mask );
231 static void rtl_spi_write_bit ( struct bit_basher *basher,
232 unsigned int bit_id, unsigned long data ) {
233 struct rtl8139_nic *rtl = container_of ( basher, struct rtl8139_nic,
235 uint8_t mask = rtl_ee_bits[bit_id];
238 eereg = inb ( rtl->ioaddr + Cfg9346 );
240 eereg |= ( data & mask );
241 outb ( eereg, rtl->ioaddr + Cfg9346 );
244 static struct bit_basher_operations rtl_basher_ops = {
245 .read = rtl_spi_read_bit,
246 .write = rtl_spi_write_bit,
249 /** Portion of EEPROM available for non-volatile stored options
251 * We use offset 0x40 (i.e. address 0x20), length 0x40. This block is
252 * marked as VPD in the rtl8139 datasheets, so we use it only if we
253 * detect that the card is not supporting VPD.
255 static struct nvo_fragment rtl_nvo_fragments[] = {
261 * Set up for EEPROM access
265 void rtl_init_eeprom ( struct rtl8139_nic *rtl ) {
269 /* Initialise three-wire bus */
270 rtl->spibit.basher.op = &rtl_basher_ops;
271 rtl->spibit.bus.mode = SPI_MODE_THREEWIRE;
272 init_spi_bit_basher ( &rtl->spibit );
274 /* Detect EEPROM type and initialise three-wire device */
275 ee9356 = ( inw ( rtl->ioaddr + RxConfig ) & Eeprom9356 );
277 DBG ( "EEPROM is an AT93C56\n" );
278 init_at93c56 ( &rtl->eeprom, 16 );
280 DBG ( "EEPROM is an AT93C46\n" );
281 init_at93c46 ( &rtl->eeprom, 16 );
283 rtl->eeprom.bus = &rtl->spibit.bus;
285 /* Initialise space for non-volatile options, if available */
286 vpd = ( inw ( rtl->ioaddr + Config1 ) & VPDEnable );
288 DBG ( "EEPROM in use for VPD; cannot use for options\n" );
290 rtl->nvo.nvs = &rtl->eeprom.nvs;
291 rtl->nvo.fragments = rtl_nvo_fragments;
300 * Issues a hardware reset and waits for the reset to complete.
302 static void rtl_reset ( struct rtl8139_nic *rtl ) {
305 outb ( CmdReset, rtl->ioaddr + ChipCmd );
314 * @v netdev Net device
315 * @ret rc Return status code
317 static int rtl_open ( struct net_device *netdev ) {
318 struct rtl8139_nic *rtl = netdev->priv;
321 /* Program the MAC address */
322 for ( i = 0 ; i < ETH_ALEN ; i++ )
323 outb ( netdev->ll_addr[i], rtl->ioaddr + MAC0 + i );
326 rtl->rx.ring = malloc ( RX_BUF_LEN + RX_BUF_PAD );
327 if ( ! rtl->rx.ring )
329 outl ( virt_to_bus ( rtl->rx.ring ), rtl->ioaddr + RxBuf );
330 DBG ( "RX ring at %lx\n", virt_to_bus ( rtl->rx.ring ) );
332 /* Enable TX and RX */
333 outb ( ( CmdRxEnb | CmdTxEnb ), rtl->ioaddr + ChipCmd );
334 outl ( ( ( RX_FIFO_THRESH << 13 ) | ( RX_BUF_LEN_IDX << 11 ) |
335 ( RX_DMA_BURST << 8 ) | AcceptBroadcast | AcceptMulticast |
336 AcceptMyPhys ), rtl->ioaddr + RxConfig );
337 outl ( 0xffffffffUL, rtl->ioaddr + MAR0 + 0 );
338 outl ( 0xffffffffUL, rtl->ioaddr + MAR0 + 4 );
339 outl ( ( ( TX_DMA_BURST << 8 ) | ( TX_IPG << 24 ) ),
340 rtl->ioaddr + TxConfig );
348 * @v netdev Net device
350 static void rtl_close ( struct net_device *netdev ) {
351 struct rtl8139_nic *rtl = netdev->priv;
354 /* Reset the hardware to disable everything in one go */
358 free ( rtl->rx.ring );
361 /* Free any old TX buffers that hadn't yet completed */
362 for ( i = 0 ; i < TX_RING_SIZE ; i++ ) {
363 if ( rtl->tx.pkb[i] ) {
364 free_pkb ( rtl->tx.pkb[i] );
365 rtl->tx.pkb[i] = NULL;
366 DBG ( "TX id %d discarded\n", i );
374 * @v netdev Network device
375 * @v pkb Packet buffer
376 * @ret rc Return status code
378 static int rtl_transmit ( struct net_device *netdev, struct pk_buff *pkb ) {
379 struct rtl8139_nic *rtl = netdev->priv;
383 /* Check for space in TX ring */
384 if ( rtl->tx.pkb[rtl->tx.next] != NULL ) {
385 printf ( "TX overflow\n" );
390 /* Align packet data */
391 align = ( virt_to_bus ( pkb->data ) & 0x3 );
392 pkb_push ( pkb, align );
393 memmove ( pkb->data, pkb->data + align, pkb_len ( pkb ) - align );
394 pkb_unput ( pkb, align );
396 /* Pad to minimum packet length */
397 pad_len = ( ETH_ZLEN - pkb_len ( pkb ) );
399 memset ( pkb_put ( pkb, pad_len ), 0, pad_len );
402 DBG ( "TX id %d at %lx+%x\n", rtl->tx.next,
403 virt_to_bus ( pkb->data ), pkb_len ( pkb ) );
404 rtl->tx.pkb[rtl->tx.next] = pkb;
405 outl ( virt_to_bus ( pkb->data ),
406 rtl->ioaddr + TxAddr0 + 4 * rtl->tx.next );
407 outl ( ( ( ( TX_FIFO_THRESH & 0x7e0 ) << 11 ) | pkb_len ( pkb ) ),
408 rtl->ioaddr + TxStatus0 + 4 * rtl->tx.next );
409 rtl->tx.next = ( rtl->tx.next + 1 ) % TX_RING_SIZE;
415 * Poll for received packets
417 * @v netdev Network device
419 static void rtl_poll ( struct net_device *netdev ) {
420 struct rtl8139_nic *rtl = netdev->priv;
423 unsigned int rx_status;
425 struct pk_buff *rx_pkb;
429 /* Acknowledge interrupts */
430 status = inw ( rtl->ioaddr + IntrStatus );
433 outw ( status, rtl->ioaddr + IntrStatus );
435 /* Handle TX completions */
436 tsad = inw ( rtl->ioaddr + TxSummary );
437 for ( i = 0 ; i < TX_RING_SIZE ; i++ ) {
438 if ( ( rtl->tx.pkb[i] != NULL ) && ( tsad & ( 1 << i ) ) ) {
439 DBG ( "TX id %d complete\n", i );
440 free_pkb ( rtl->tx.pkb[i] );
441 rtl->tx.pkb[i] = NULL;
445 /* Handle received packets */
446 while ( ! ( inw ( rtl->ioaddr + ChipCmd ) & RxBufEmpty ) ) {
447 rx_status = * ( ( uint16_t * )
448 ( rtl->rx.ring + rtl->rx.offset ) );
449 rx_len = * ( ( uint16_t * )
450 ( rtl->rx.ring + rtl->rx.offset + 2 ) );
451 if ( rx_status & RxOK ) {
452 DBG ( "RX packet at offset %x+%x\n", rtl->rx.offset,
455 rx_pkb = alloc_pkb ( rx_len );
457 /* Leave packet for next call to poll() */
461 wrapped_len = ( ( rtl->rx.offset + 4 + rx_len )
463 if ( wrapped_len < 0 )
466 memcpy ( pkb_put ( rx_pkb, rx_len - wrapped_len ),
467 rtl->rx.ring + rtl->rx.offset + 4,
468 rx_len - wrapped_len );
469 memcpy ( pkb_put ( rx_pkb, wrapped_len ),
470 rtl->rx.ring, wrapped_len );
472 netdev_rx ( netdev, rx_pkb );
474 DBG ( "RX bad packet (status %#04x len %d)\n",
477 rtl->rx.offset = ( ( ( rtl->rx.offset + 4 + rx_len + 3 ) & ~3 )
479 outw ( rtl->rx.offset - 16, rtl->ioaddr + RxBufPtr );
484 static void rtl_irq(struct nic *nic, irq_action_t action)
487 /* Bit of a guess as to which interrupts we should allow */
488 unsigned int interested = ROK | RER | RXOVW | FOVW | SERR;
493 mask = inw(rtl->ioaddr + IntrMask);
494 mask = mask & ~interested;
495 if ( action == ENABLE ) mask = mask | interested;
496 outw(mask, rtl->ioaddr + IntrMask);
499 /* Apparently writing a 1 to this read-only bit of a
500 * read-only and otherwise unrelated register will
501 * force an interrupt. If you ever want to see how
502 * not to write a datasheet, read the one for the
505 outb(EROK, rtl->ioaddr + RxEarlyStatus);
516 * @ret rc Return status code
518 static int rtl_probe ( struct pci_device *pci,
519 const struct pci_device_id *id __unused ) {
520 struct net_device *netdev;
521 struct rtl8139_nic *rtl = NULL;
522 int registered_netdev = 0;
525 /* Fix up PCI device */
526 adjust_pci_device ( pci );
528 /* Allocate net device */
529 netdev = alloc_etherdev ( sizeof ( *rtl ) );
535 pci_set_drvdata ( pci, netdev );
536 memset ( rtl, 0, sizeof ( *rtl ) );
537 rtl->ioaddr = pci->ioaddr;
539 /* Reset the NIC, set up EEPROM access and read MAC address */
541 rtl_init_eeprom ( rtl );
542 nvs_read ( &rtl->eeprom.nvs, EE_MAC, netdev->ll_addr, ETH_ALEN );
544 /* Point to NIC specific routines */
545 // netdev->open = rtl_open;
546 // netdev->close = rtl_close;
547 netdev->transmit = rtl_transmit;
548 netdev->poll = rtl_poll;
550 /* Register network device */
551 if ( ( rc = register_netdev ( netdev ) ) != 0 )
553 registered_netdev = 1;
555 /* Register non-volatile storage */
556 if ( rtl->nvo.nvs ) {
557 if ( ( rc = nvo_register ( &rtl->nvo ) ) != 0 )
561 #warning "Hack alert"
571 if ( registered_netdev )
572 unregister_netdev ( netdev );
573 /* Free net device */
574 free_netdev ( netdev );
583 static void rtl_remove ( struct pci_device *pci ) {
584 struct net_device *netdev = pci_get_drvdata ( pci );
585 struct rtl8139_nic *rtl = netdev->priv;
588 #warning "Hack alert"
589 rtl_close ( netdev );
592 nvo_unregister ( &rtl->nvo );
593 unregister_netdev ( netdev );
595 free_netdev ( netdev );
598 static struct pci_device_id rtl8139_nics[] = {
599 PCI_ROM(0x10ec, 0x8129, "rtl8129", "Realtek 8129"),
600 PCI_ROM(0x10ec, 0x8139, "rtl8139", "Realtek 8139"),
601 PCI_ROM(0x10ec, 0x8138, "rtl8139b", "Realtek 8139B"),
602 PCI_ROM(0x1186, 0x1300, "dfe538", "DFE530TX+/DFE538TX"),
603 PCI_ROM(0x1113, 0x1211, "smc1211-1", "SMC EZ10/100"),
604 PCI_ROM(0x1112, 0x1211, "smc1211", "SMC EZ10/100"),
605 PCI_ROM(0x1500, 0x1360, "delta8139", "Delta Electronics 8139"),
606 PCI_ROM(0x4033, 0x1360, "addtron8139", "Addtron Technology 8139"),
607 PCI_ROM(0x1186, 0x1340, "dfe690txd", "D-Link DFE690TXD"),
608 PCI_ROM(0x13d1, 0xab06, "fe2000vx", "AboCom FE2000VX"),
609 PCI_ROM(0x1259, 0xa117, "allied8139", "Allied Telesyn 8139"),
610 PCI_ROM(0x14ea, 0xab06, "fnw3603tx", "Planex FNW-3603-TX"),
611 PCI_ROM(0x14ea, 0xab07, "fnw3800tx", "Planex FNW-3800-TX"),
612 PCI_ROM(0xffff, 0x8139, "clone-rtl8139", "Cloned 8139"),
615 struct pci_driver rtl8139_driver __pci_driver = {
617 .id_count = ( sizeof ( rtl8139_nics ) / sizeof ( rtl8139_nics[0] ) ),
619 .remove = rtl_remove,