1 /**************************************************************************
2 * r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
3 * Written 2003 by Timothy Legge <tlegge@rogers.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * Portions of this code based on:
20 * r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
21 * for Linux kernel 2.4.x.
23 * Written 2002 ShuChen <shuchen@realtek.com.tw>
24 * See Linux Driver for full information
26 * Linux Driver Versions:
28 * RTL8169_VERSION "2.2" <2004/08/09>
31 * Jean Chen of RealTek Semiconductor Corp. for
32 * providing the evaluation NIC used to develop
33 * this driver. RealTek's support for Etherboot
39 * v1.0 11-26-2003 timlegge Initial port of Linux driver
40 * v1.5 01-17-2004 timlegge Initial driver output cleanup
41 * v1.6 03-27-2004 timlegge Additional Cleanup
42 * v1.7 11-22-2005 timlegge Update to RealTek Driver Version 2.2
44 * Indent Options: indent -kr -i8
45 ***************************************************************************/
47 /* to get some global routines like printf */
48 #include "etherboot.h"
49 /* to get the interface to the body of the program */
51 /* to get the PCI support functions, if this is a PCI NIC */
55 #define drv_version "v1.6"
56 #define drv_date "03-27-2004"
63 #define dprintf(x) printf x
68 /* Condensed operations for readability. */
69 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
70 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
72 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
75 #undef RTL8169_JUMBO_FRAME_SUPPORT
76 #undef RTL8169_HW_FLOW_CONTROL_SUPPORT
79 #undef RTL8169_IOCTL_SUPPORT
80 #undef RTL8169_DYNAMIC_CONTROL
81 #define RTL8169_USE_IO
85 #define assert(expr) \
86 if(!(expr)) { printk( "Assertion failed! %s,%s,%s,line=%d\n", #expr,__FILE__,__FUNCTION__,__LINE__); }
87 #define DBG_PRINT( fmt, args...) printk("r8169: " fmt, ## args);
89 #define assert(expr) do {} while (0)
90 #define DBG_PRINT( fmt, args...) ;
91 #endif // end of #ifdef RTL8169_DEBUG
100 static int media = -1;
103 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
104 static int max_interrupt_work = 20;
108 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
109 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
110 static int multicast_filter_limit = 32;
113 /* MAC address length*/
114 #define MAC_ADDR_LEN 6
116 /* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
117 #define MAX_ETH_FRAME_SIZE 1536
119 #define TX_FIFO_THRESH 256 /* In bytes */
121 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
122 #define RX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
123 #define TX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
124 #define ETTh 0x3F /* 0x3F means NO threshold */
126 #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
127 #define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
128 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
130 #define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
131 #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
132 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
134 #define RTL_MIN_IO_SIZE 0x80
135 #define TX_TIMEOUT (6*HZ)
137 #define RTL8169_TIMER_EXPIRE_TIME 100 //100
139 #define ETH_HDR_LEN 14
140 #define DEFAULT_MTU 1500
141 #define DEFAULT_RX_BUF_LEN 1536
144 #ifdef RTL8169_JUMBO_FRAME_SUPPORT
145 #define MAX_JUMBO_FRAME_MTU ( 10000 )
146 #define MAX_RX_SKBDATA_SIZE ( MAX_JUMBO_FRAME_MTU + ETH_HDR_LEN )
148 #define MAX_RX_SKBDATA_SIZE 1600
149 #endif //end #ifdef RTL8169_JUMBO_FRAME_SUPPORT
151 #ifdef RTL8169_USE_IO
152 #define RTL_W8(reg, val8) outb ((val8), ioaddr + (reg))
153 #define RTL_W16(reg, val16) outw ((val16), ioaddr + (reg))
154 #define RTL_W32(reg, val32) outl ((val32), ioaddr + (reg))
155 #define RTL_R8(reg) inb (ioaddr + (reg))
156 #define RTL_R16(reg) inw (ioaddr + (reg))
157 #define RTL_R32(reg) ((unsigned long) inl (ioaddr + (reg)))
159 /* write/read MMIO register */
160 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
161 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
162 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
163 #define RTL_R8(reg) readb (ioaddr + (reg))
164 #define RTL_R16(reg) readw (ioaddr + (reg))
165 #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
168 #define MCFG_METHOD_1 0x01
169 #define MCFG_METHOD_2 0x02
170 #define MCFG_METHOD_3 0x03
171 #define MCFG_METHOD_4 0x04
173 #define PCFG_METHOD_1 0x01 //PHY Reg 0x03 bit0-3 == 0x0000
174 #define PCFG_METHOD_2 0x02 //PHY Reg 0x03 bit0-3 == 0x0001
175 #define PCFG_METHOD_3 0x03 //PHY Reg 0x03 bit0-3 == 0x0002
179 u8 mcfg; /* depend on RTL8169 docs */
180 u32 RxConfigMask; /* should clear the bits supported by this chip */
181 } rtl_chip_info[] = {
183 "RTL-8169", MCFG_METHOD_1, 0xff7e1880,}, {
184 "RTL8169s/8110s", MCFG_METHOD_2, 0xff7e1880}, {
185 "RTL8169s/8110s", MCFG_METHOD_3, 0xff7e1880},};
187 enum RTL8169_registers {
188 MAC0 = 0x0, /* Ethernet hardware address. */
189 MAR0 = 0x8, /* Multicast filter. */
190 TxDescStartAddr = 0x20,
191 TxHDescStartAddr = 0x28,
216 RxDescStartAddr = 0xE4,
219 FuncEventMask = 0xF4,
220 FuncPresetState = 0xF8,
221 FuncForceEvent = 0xFC,
224 enum RTL8169_register_content {
225 /*InterruptStatusBits */
229 TxDescUnavail = 0x80,
252 Cfg9346_Unlock = 0xC0,
257 AcceptBroadcast = 0x08,
258 AcceptMulticast = 0x04,
260 AcceptAllPhys = 0x01,
267 TxInterFrameGapShift = 24,
268 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
270 /*rtl8169_PHYstatus */
280 /*GIGABIT_PHY_registers */
283 PHY_AUTO_NEGO_REG = 4,
284 PHY_1000_CTRL_REG = 9,
286 /*GIGABIT_PHY_REG_BIT */
287 PHY_Restart_Auto_Nego = 0x0200,
288 PHY_Enable_Auto_Nego = 0x1000,
290 /* PHY_STAT_REG = 1; */
291 PHY_Auto_Neco_Comp = 0x0020,
293 /* PHY_AUTO_NEGO_REG = 4; */
294 PHY_Cap_10_Half = 0x0020,
295 PHY_Cap_10_Full = 0x0040,
296 PHY_Cap_100_Half = 0x0080,
297 PHY_Cap_100_Full = 0x0100,
299 /* PHY_1000_CTRL_REG = 9; */
300 PHY_Cap_1000_Full = 0x0200,
301 PHY_Cap_1000_Half = 0x0100,
303 PHY_Cap_PAUSE = 0x0400,
304 PHY_Cap_ASYM_PAUSE = 0x0800,
316 TBILinkOK = 0x02000000,
319 enum _DescStatusBit {
340 /* The descriptors for this card are required to be aligned on 256
341 * byte boundaries. As the align attribute does not do more than 16
342 * bytes of alignment it requires some extra steps. Add 256 to the
343 * size of the array and the init_ring adjusts the alignment.
345 * UPDATE: This is no longer true; we can request arbitrary alignment.
348 /* Define the TX and RX Descriptors and Buffers */
349 #define __align_256 __attribute__ (( aligned ( 256 ) ))
351 struct TxDesc tx_ring[NUM_TX_DESC] __align_256;
352 unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];
353 struct RxDesc rx_ring[NUM_RX_DESC] __align_256;
354 unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
355 } r8169_bufs __shared;
356 #define tx_ring r8169_bufs.tx_ring
357 #define rx_ring r8169_bufs.rx_ring
358 #define txb r8169_bufs.txb
359 #define rxb r8169_bufs.rxb
361 static struct rtl8169_private {
362 void *mmio_addr; /* memory map physical address */
366 unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
367 unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
368 struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
369 struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
370 unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
371 unsigned char *Tx_skbuff[NUM_TX_DESC];
374 static struct rtl8169_private *tpc;
376 static const u16 rtl8169_intr_mask =
377 LinkChg | RxOverflow | RxFIFOOver | TxErr | TxOK | RxErr | RxOK;
378 static const unsigned int rtl8169_rx_config =
379 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift) |
382 static void rtl8169_hw_PHY_config(struct nic *nic __unused);
383 //static void rtl8169_hw_PHY_reset(struct net_device *dev);
385 #define RTL8169_WRITE_GMII_REG_BIT( ioaddr, reg, bitnum, bitval )\
388 if( bitval == 1 ){ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) | (bitval<<bitnum) ) & 0xffff ; } \
389 else{ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) & (~(0x0001<<bitnum)) ) & 0xffff ; } \
390 RTL8169_WRITE_GMII_REG( ioaddr, reg, val ); \
393 //=================================================================
398 // 20-16 5-bit GMII/MII register address
399 // 15-0 16-bit GMII/MII register data
400 //=================================================================
401 void RTL8169_WRITE_GMII_REG(unsigned long ioaddr, int RegAddr, int value)
405 RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
408 for (i = 2000; i > 0; i--) {
409 // Check if the RTL8169 has completed writing to the specified MII register
410 if (!(RTL_R32(PHYAR) & 0x80000000)) {
414 } // end of if( ! (RTL_R32(PHYAR)&0x80000000) )
415 } // end of for() loop
418 //=================================================================
419 int RTL8169_READ_GMII_REG(unsigned long ioaddr, int RegAddr)
423 RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
426 for (i = 2000; i > 0; i--) {
427 // Check if the RTL8169 has completed retrieving data from the specified MII register
428 if (RTL_R32(PHYAR) & 0x80000000) {
429 value = (int) (RTL_R32(PHYAR) & 0xFFFF);
433 } // end of if( RTL_R32(PHYAR) & 0x80000000 )
434 } // end of for() loop
439 static void mdio_write(int RegAddr, int value)
443 RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
446 for (i = 2000; i > 0; i--) {
447 /* Check if the RTL8169 has completed writing to the specified MII register */
448 if (!(RTL_R32(PHYAR) & 0x80000000)) {
456 static int mdio_read(int RegAddr)
460 RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
463 for (i = 2000; i > 0; i--) {
464 /* Check if the RTL8169 has completed retrieving data from the specified MII register */
465 if (RTL_R32(PHYAR) & 0x80000000) {
466 value = (int) (RTL_R32(PHYAR) & 0xFFFF);
475 #define IORESOURCE_MEM 0x00000200
477 static int rtl8169_init_board(struct pci_device *pdev)
480 unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
482 adjust_pci_device(pdev);
484 mmio_start = pci_bar_start(pdev, PCI_BASE_ADDRESS_1);
485 // mmio_end = pci_resource_end (pdev, 1);
486 // mmio_flags = pci_resource_flags (pdev, PCI_BASE_ADDRESS_1);
487 mmio_len = pci_bar_size(pdev, PCI_BASE_ADDRESS_1);
489 // make sure PCI base addr 1 is MMIO
490 // if (!(mmio_flags & IORESOURCE_MEM)) {
491 // printf ("region #1 not an MMIO resource, aborting\n");
495 // check for weird/broken PCI region reporting
496 if (mmio_len < RTL_MIN_IO_SIZE) {
497 printf("Invalid PCI region size(s), aborting\n");
500 #ifdef RTL8169_USE_IO
501 ioaddr = pci_bar_start(pdev, PCI_BASE_ADDRESS_0);
503 // ioremap MMIO region
504 ioaddr = (unsigned long) ioremap(mmio_start, mmio_len);
506 printk("cannot remap MMIO, aborting\n");
511 tpc->mmio_addr = &ioaddr;
512 /* Soft reset the chip. */
513 RTL_W8(ChipCmd, CmdReset);
515 /* Check that the chip has finished the reset. */
516 for (i = 1000; i > 0; i--)
517 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
521 // identify config method
523 unsigned long val32 = (RTL_R32(TxConfig) & 0x7c800000);
524 if (val32 == (0x1 << 28)) {
525 tpc->mcfg = MCFG_METHOD_4;
526 } else if (val32 == (0x1 << 26)) {
527 tpc->mcfg = MCFG_METHOD_3;
528 } else if (val32 == (0x1 << 23)) {
529 tpc->mcfg = MCFG_METHOD_2;
530 } else if (val32 == 0x00000000) {
531 tpc->mcfg = MCFG_METHOD_1;
533 tpc->mcfg = MCFG_METHOD_1;
538 (unsigned char) (RTL8169_READ_GMII_REG(ioaddr, 3) &
541 tpc->pcfg = PCFG_METHOD_1;
542 } else if (val8 == 0x01) {
543 tpc->pcfg = PCFG_METHOD_2;
544 } else if (val8 == 0x02) {
545 tpc->pcfg = PCFG_METHOD_3;
547 tpc->pcfg = PCFG_METHOD_3;
551 /* identify chip attached to board */
553 for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--)
554 if (tpc->mcfg == rtl_chip_info[i].mcfg) {
558 /* if unknown chip, assume array element #0, original RTL-8169 in this case */
559 dprintf(("PCI device: unknown chip version, assuming RTL-8169\n"));
560 dprintf(("PCI device: TxConfig = 0x%hX\n",
561 (unsigned long) RTL_R32(TxConfig)));
571 /**************************************************************************
572 IRQ - Wait for a frame
573 ***************************************************************************/
574 void r8169_irq(struct nic *nic __unused, irq_action_t action)
577 int interested = RxOverflow | RxFIFOOver | RxErr | RxOK;
582 intr_status = RTL_R16(IntrStatus);
583 /* h/w no longer present (hotplug?) or major error,
585 if (intr_status == 0xFFFF)
588 intr_status = intr_status & ~interested;
589 if (action == ENABLE)
590 intr_status = intr_status | interested;
591 RTL_W16(IntrMask, intr_status);
594 RTL_W8(TxPoll, (RTL_R8(TxPoll) | 0x01));
599 static void r8169_irq ( struct nic *nic __unused, irq_action_t action ) {
601 int interested = RxUnderrun | RxOverflow | RxFIFOOver | RxErr | RxOK;
606 intr_status = RTL_R16(IntrStatus);
607 /* h/w no longer present (hotplug?) or major error,
609 if (intr_status == 0xFFFF)
612 intr_status = intr_status & ~interested;
613 if ( action == ENABLE )
614 intr_status = intr_status | interested;
615 RTL_W16(IntrMask, intr_status);
618 RTL_W8(TxPoll, (RTL_R8(TxPoll) | 0x01));
623 /**************************************************************************
624 POLL - Wait for a frame
625 ***************************************************************************/
626 static int r8169_poll(struct nic *nic, int retreive)
628 /* return true if there's an ethernet packet ready to read */
629 /* nic->packet should contain data on return */
630 /* nic->packetlen should contain length of data */
632 unsigned int intr_status = 0;
633 cur_rx = tpc->cur_rx;
634 if ((tpc->RxDescArray[cur_rx].status & OWNbit) == 0) {
635 /* There is a packet ready */
638 intr_status = RTL_R16(IntrStatus);
639 /* h/w no longer present (hotplug?) or major error,
641 if (intr_status == 0xFFFF)
643 RTL_W16(IntrStatus, intr_status &
644 ~(RxFIFOOver | RxOverflow | RxOK));
646 if (!(tpc->RxDescArray[cur_rx].status & RxRES)) {
647 nic->packetlen = (int) (tpc->RxDescArray[cur_rx].
648 status & 0x00001FFF) - 4;
649 memcpy(nic->packet, tpc->RxBufferRing[cur_rx],
651 if (cur_rx == NUM_RX_DESC - 1)
652 tpc->RxDescArray[cur_rx].status =
653 (OWNbit | EORbit) + RX_BUF_SIZE;
655 tpc->RxDescArray[cur_rx].status =
656 OWNbit + RX_BUF_SIZE;
657 tpc->RxDescArray[cur_rx].buf_addr =
658 virt_to_bus(tpc->RxBufferRing[cur_rx]);
661 /* FIXME: shouldn't I reset the status on an error */
662 cur_rx = (cur_rx + 1) % NUM_RX_DESC;
663 tpc->cur_rx = cur_rx;
664 RTL_W16(IntrStatus, intr_status &
665 (RxFIFOOver | RxOverflow | RxOK));
670 tpc->cur_rx = cur_rx;
671 /* FIXME: There is no reason to do this as cur_rx did not change */
673 return (0); /* initially as this is called to flush the input */
677 /**************************************************************************
678 TRANSMIT - Transmit a frame
679 ***************************************************************************/
680 static void r8169_transmit(struct nic *nic, const char *d, /* Destination */
681 unsigned int t, /* Type */
682 unsigned int s, /* size */
685 /* send the packet to destination */
690 int entry = tpc->cur_tx % NUM_TX_DESC;
692 /* point to the current txb incase multiple tx_rings are used */
693 ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
694 memcpy(ptxb, d, ETH_ALEN);
695 memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN);
696 nstype = htons((u16) t);
697 memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
698 memcpy(ptxb + ETH_HLEN, p, s);
704 tpc->TxDescArray[entry].buf_addr = virt_to_bus(ptxb);
705 if (entry != (NUM_TX_DESC - 1))
706 tpc->TxDescArray[entry].status =
707 (OWNbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s :
710 tpc->TxDescArray[entry].status =
711 (OWNbit | EORbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s
713 RTL_W8(TxPoll, 0x40); /* set polling bit */
716 to = currticks() + TX_TIMEOUT;
717 while ((tpc->TxDescArray[entry].status & OWNbit) && (currticks() < to)); /* wait */
719 if (currticks() >= to) {
720 printf("TX Time Out");
724 static void rtl8169_set_rx_mode(struct nic *nic __unused)
726 u32 mc_filter[2]; /* Multicast hash filter */
731 /* Too many to filter perfectly -- accept all multicasts. */
732 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
733 mc_filter[1] = mc_filter[0] = 0xffffffff;
736 rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
737 rtl_chip_info[tpc->chipset].
740 RTL_W32(RxConfig, tmp);
741 RTL_W32(MAR0 + 0, mc_filter[0]);
742 RTL_W32(MAR0 + 4, mc_filter[1]);
744 static void rtl8169_hw_start(struct nic *nic)
748 /* Soft reset the chip. */
749 RTL_W8(ChipCmd, CmdReset);
751 /* Check that the chip has finished the reset. */
752 for (i = 1000; i > 0; i--) {
753 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
759 RTL_W8(Cfg9346, Cfg9346_Unlock);
760 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
761 RTL_W8(ETThReg, ETTh);
763 /* For gigabit rtl8169 */
764 RTL_W16(RxMaxSize, RxPacketMaxSize);
766 /* Set Rx Config register */
767 i = rtl8169_rx_config | (RTL_R32(RxConfig) &
768 rtl_chip_info[tpc->chipset].RxConfigMask);
769 RTL_W32(RxConfig, i);
771 /* Set DMA burst size and Interframe Gap Time */
773 (TX_DMA_BURST << TxDMAShift) | (InterFrameGap <<
774 TxInterFrameGapShift));
777 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd));
779 if (tpc->mcfg == MCFG_METHOD_2 || tpc->mcfg == MCFG_METHOD_3) {
781 (RTL_R16(CPlusCmd) | (1 << 14) | (1 << 3)));
783 ("Set MAC Reg C+CR Offset 0xE0: bit-3 and bit-14\n");
785 RTL_W16(CPlusCmd, (RTL_R16(CPlusCmd) | (1 << 3)));
786 DBG_PRINT("Set MAC Reg C+CR Offset 0xE0: bit-3.\n");
790 //RTL_W16(0xE2, 0x1517);
791 //RTL_W16(0xE2, 0x152a);
792 //RTL_W16(0xE2, 0x282a);
793 RTL_W16(0xE2, 0x0000);
800 RTL_W32(TxDescStartAddr, virt_to_le32desc(tpc->TxDescArray));
801 RTL_W32(RxDescStartAddr, virt_to_le32desc(tpc->RxDescArray));
802 RTL_W8(Cfg9346, Cfg9346_Lock);
805 RTL_W32(RxMissed, 0);
807 rtl8169_set_rx_mode(nic);
809 /* no early-rx interrupts */
810 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
812 RTL_W16(IntrMask, rtl8169_intr_mask);
816 static void rtl8169_init_ring(struct nic *nic __unused)
822 memset(tpc->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
823 memset(tpc->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
825 for (i = 0; i < NUM_TX_DESC; i++) {
826 tpc->Tx_skbuff[i] = &txb[i];
829 for (i = 0; i < NUM_RX_DESC; i++) {
830 if (i == (NUM_RX_DESC - 1))
831 tpc->RxDescArray[i].status =
832 (OWNbit | EORbit) | RX_BUF_SIZE;
834 tpc->RxDescArray[i].status = OWNbit | RX_BUF_SIZE;
836 tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
837 tpc->RxDescArray[i].buf_addr =
838 virt_to_bus(tpc->RxBufferRing[i]);
842 /**************************************************************************
843 RESET - Finish setting up the ethernet interface
844 ***************************************************************************/
845 static void r8169_reset(struct nic *nic)
849 tpc->TxDescArray = tx_ring;
850 tpc->RxDescArray = rx_ring;
852 rtl8169_init_ring(nic);
853 rtl8169_hw_start(nic);
854 /* Construct a perfect filter frame with the mac address as first match
855 * and broadcast for all others */
856 for (i = 0; i < 192; i++)
859 txb[0] = nic->node_addr[0];
860 txb[1] = nic->node_addr[1];
861 txb[2] = nic->node_addr[2];
862 txb[3] = nic->node_addr[3];
863 txb[4] = nic->node_addr[4];
864 txb[5] = nic->node_addr[5];
867 /**************************************************************************
868 DISABLE - Turn off ethernet interface
869 ***************************************************************************/
870 static void r8169_disable ( struct nic *nic __unused ) {
872 /* Stop the chip's Tx and Rx DMA processes. */
873 RTL_W8(ChipCmd, 0x00);
875 /* Disable interrupts by clearing the interrupt mask. */
876 RTL_W16(IntrMask, 0x0000);
878 RTL_W32(RxMissed, 0);
880 tpc->TxDescArray = NULL;
881 tpc->RxDescArray = NULL;
882 for (i = 0; i < NUM_RX_DESC; i++) {
883 tpc->RxBufferRing[i] = NULL;
887 static struct nic_operations r8169_operations = {
888 .connect = dummy_connect,
890 .transmit = r8169_transmit,
895 static struct pci_id r8169_nics[] = {
896 PCI_ROM(0x10ec, 0x8169, "r8169", "RealTek RTL8169 Gigabit Ethernet"),
897 PCI_ROM(0x16ec, 0x0116, "usr-r8169", "US Robotics RTL8169 Gigabit Ethernet"),
898 PCI_ROM(0x1186, 0x4300, "dlink-r8169", "D-Link RTL8169 Gigabit Ethernet"),
901 PCI_DRIVER ( r8169_driver, r8169_nics, PCI_NO_CLASS );
903 /**************************************************************************
904 PROBE - Look for an adapter, this routine's visible to the outside
905 ***************************************************************************/
907 #define board_found 1
909 static int r8169_probe ( struct nic *nic, struct pci_device *pci ) {
911 static int board_idx = -1;
912 static int printed_version = 0;
914 int option = -1, Cap10_100 = 0, Cap1000 = 0;
916 printf("r8169.c: Found %s, Vendor=%hX Device=%hX\n",
917 pci->name, pci->vendor_id, pci->device_id);
923 /* point to private storage */
926 rc = rtl8169_init_board(pci); /* Return code is meaningless */
928 /* Get MAC address. FIXME: read EEPROM */
929 for (i = 0; i < MAC_ADDR_LEN; i++)
930 nic->node_addr[i] = RTL_R8(MAC0 + i);
932 dprintf(("%s: Identified chip type is '%s'.\n", pci->name,
933 rtl_chip_info[tpc->chipset].name));
934 /* Print out some hardware info */
935 printf("%s: %! at ioaddr %hX, ", pci->name, nic->node_addr,
939 rtl8169_hw_PHY_config(nic);
941 DBG_PRINT("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
944 if (tpc->mcfg < MCFG_METHOD_3) {
945 DBG_PRINT("Set PCI Latency=0x40\n");
946 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0x40);
949 if (tpc->mcfg == MCFG_METHOD_2) {
950 DBG_PRINT("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
952 DBG_PRINT("Set PHY Reg 0x0bh = 0x00h\n");
953 RTL8169_WRITE_GMII_REG(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
956 /* if TBI is not endbled */
957 if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
958 int val = RTL8169_READ_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG);
960 #ifdef RTL8169_HW_FLOW_CONTROL_SUPPORT
961 val |= PHY_Cap_PAUSE | PHY_Cap_ASYM_PAUSE;
962 #endif //end #define RTL8169_HW_FLOW_CONTROL_SUPPORT
965 /* Force RTL8169 in 10/100/1000 Full/Half mode. */
967 printf(" Force-mode Enabled.\n");
968 Cap10_100 = 0, Cap1000 = 0;
971 Cap10_100 = PHY_Cap_10_Half;
972 Cap1000 = PHY_Cap_Null;
975 Cap10_100 = PHY_Cap_10_Full;
976 Cap1000 = PHY_Cap_Null;
979 Cap10_100 = PHY_Cap_100_Half;
980 Cap1000 = PHY_Cap_Null;
983 Cap10_100 = PHY_Cap_100_Full;
984 Cap1000 = PHY_Cap_Null;
987 Cap10_100 = PHY_Cap_Null;
988 Cap1000 = PHY_Cap_1000_Full;
993 RTL8169_WRITE_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG, Cap10_100 | (val & 0xC1F)); //leave PHY_AUTO_NEGO_REG bit4:0 unchanged
994 RTL8169_WRITE_GMII_REG(ioaddr, PHY_1000_CTRL_REG,
997 dprintf(("Auto-negotiation Enabled.\n",
1000 // enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged
1001 RTL8169_WRITE_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG,
1005 PHY_Cap_100_Full | (val &
1008 // enable 1000 Full Mode
1009 // RTL8169_WRITE_GMII_REG( ioaddr, PHY_1000_CTRL_REG, PHY_Cap_1000_Full );
1010 RTL8169_WRITE_GMII_REG(ioaddr, PHY_1000_CTRL_REG, PHY_Cap_1000_Full | PHY_Cap_1000_Half); //rtl8168
1012 } // end of if( option > 0 )
1014 // Enable auto-negotiation and restart auto-nigotiation
1015 RTL8169_WRITE_GMII_REG(ioaddr, PHY_CTRL_REG,
1016 PHY_Enable_Auto_Nego |
1017 PHY_Restart_Auto_Nego);
1020 // wait for auto-negotiation process
1021 for (i = 10000; i > 0; i--) {
1022 //check if auto-negotiation complete
1023 if (RTL8169_READ_GMII_REG(ioaddr, PHY_STAT_REG) &
1024 PHY_Auto_Neco_Comp) {
1026 option = RTL_R8(PHYstatus);
1027 if (option & _1000bpsF) {
1029 ("1000Mbps Full-duplex operation.\n");
1032 ("%sMbps %s-duplex operation.\n",
1033 (option & _100bps) ? "100" :
1035 (option & FullDup) ? "Full" :
1041 } // end of if( RTL8169_READ_GMII_REG(ioaddr, 1) & 0x20 )
1042 } // end for-loop to wait for auto-negotiation process
1048 ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
1050 (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
1055 /* point to NIC specific routines */
1056 nic->nic_op = &r8169_operations;
1057 pci_fill_nic ( nic, pci );
1058 nic->irqno = pci->irq;
1059 nic->ioaddr = ioaddr;
1064 //======================================================================================================
1066 static void rtl8169_hw_PHY_reset(struct nic *nic __unused)
1068 int val, phy_reset_expiretime = 50;
1069 struct rtl8169_private *priv = dev->priv;
1070 unsigned long ioaddr = priv->ioaddr;
1072 DBG_PRINT("%s: Reset RTL8169s PHY\n", dev->name);
1074 val = ( RTL8169_READ_GMII_REG( ioaddr, 0 ) | 0x8000 ) & 0xffff;
1075 RTL8169_WRITE_GMII_REG( ioaddr, 0, val );
1077 do //waiting for phy reset
1079 if( RTL8169_READ_GMII_REG( ioaddr, 0 ) & 0x8000 ){
1080 phy_reset_expiretime --;
1086 }while( phy_reset_expiretime >= 0 );
1088 assert( phy_reset_expiretime > 0 );
1093 //======================================================================================================
1094 static void rtl8169_hw_PHY_config(struct nic *nic __unused)
1097 DBG_PRINT("priv->mcfg=%d, priv->pcfg=%d\n", tpc->mcfg, tpc->pcfg);
1099 if (tpc->mcfg == MCFG_METHOD_4) {
1101 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x1F, 0x0001 );
1102 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x1b, 0x841e );
1103 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x0e, 0x7bfb );
1104 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x09, 0x273a );
1107 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x1F,
1109 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1111 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x1F,
1113 } else if ((tpc->mcfg == MCFG_METHOD_2)
1114 || (tpc->mcfg == MCFG_METHOD_3)) {
1115 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x1F,
1117 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x15,
1119 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x18,
1121 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1123 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1125 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1127 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1129 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1131 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1133 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1135 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1137 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1139 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1141 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1143 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1145 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1147 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1149 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1151 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1153 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1155 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1157 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1159 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1161 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1163 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1165 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1167 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1169 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1171 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1173 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1175 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1177 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1179 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1181 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1183 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1185 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1187 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1189 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1191 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1193 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x1F,
1195 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x0B,
1198 DBG_PRINT("tpc->mcfg=%d. Discard hw PHY config.\n",
1203 DRIVER ( "r8169/PCI", nic_driver, pci_driver, r8169_driver,
1204 r8169_probe, r8169_disable );