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 #include "etherboot.h"
50 #include <gpxe/ethernet.h>
53 #define drv_version "v1.6"
54 #define drv_date "03-27-2004"
60 /* Condensed operations for readability. */
61 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
62 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
64 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
67 #undef RTL8169_JUMBO_FRAME_SUPPORT
68 #undef RTL8169_HW_FLOW_CONTROL_SUPPORT
71 #undef RTL8169_IOCTL_SUPPORT
72 #undef RTL8169_DYNAMIC_CONTROL
73 #define RTL8169_USE_IO
77 #define assert(expr) \
78 if(!(expr)) { printk( "Assertion failed! %s,%s,%s,line=%d\n", #expr,__FILE__,__FUNCTION__,__LINE__); }
79 #define DBG_PRINTF( fmt, args...) printk("r8169: " fmt, ## args);
81 #define assert(expr) do {} while (0)
82 #define DBG_PRINTF( fmt, args...) ;
83 #endif // end of #ifdef RTL8169_DEBUG
92 static int media = -1;
95 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
96 static int max_interrupt_work = 20;
100 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
101 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
102 static int multicast_filter_limit = 32;
105 /* MAC address length*/
106 #define MAC_ADDR_LEN 6
108 /* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
109 #define MAX_ETH_FRAME_SIZE 1536
111 #define TX_FIFO_THRESH 256 /* In bytes */
113 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
114 #define RX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
115 #define TX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
116 #define ETTh 0x3F /* 0x3F means NO threshold */
118 #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
119 #define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
120 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
122 #define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
123 #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
124 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
126 #define RTL_MIN_IO_SIZE 0x80
127 #define TX_TIMEOUT (6*HZ)
129 #define RTL8169_TIMER_EXPIRE_TIME 100 //100
131 #define ETH_HDR_LEN 14
132 #define DEFAULT_MTU 1500
133 #define DEFAULT_RX_BUF_LEN 1536
136 #ifdef RTL8169_JUMBO_FRAME_SUPPORT
137 #define MAX_JUMBO_FRAME_MTU ( 10000 )
138 #define MAX_RX_SKBDATA_SIZE ( MAX_JUMBO_FRAME_MTU + ETH_HDR_LEN )
140 #define MAX_RX_SKBDATA_SIZE 1600
141 #endif //end #ifdef RTL8169_JUMBO_FRAME_SUPPORT
143 #ifdef RTL8169_USE_IO
144 #define RTL_W8(reg, val8) outb ((val8), ioaddr + (reg))
145 #define RTL_W16(reg, val16) outw ((val16), ioaddr + (reg))
146 #define RTL_W32(reg, val32) outl ((val32), ioaddr + (reg))
147 #define RTL_R8(reg) inb (ioaddr + (reg))
148 #define RTL_R16(reg) inw (ioaddr + (reg))
149 #define RTL_R32(reg) ((unsigned long) inl (ioaddr + (reg)))
151 /* write/read MMIO register */
152 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
153 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
154 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
155 #define RTL_R8(reg) readb (ioaddr + (reg))
156 #define RTL_R16(reg) readw (ioaddr + (reg))
157 #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
160 #define MCFG_METHOD_1 0x01
161 #define MCFG_METHOD_2 0x02
162 #define MCFG_METHOD_3 0x03
163 #define MCFG_METHOD_4 0x04
165 #define PCFG_METHOD_1 0x01 //PHY Reg 0x03 bit0-3 == 0x0000
166 #define PCFG_METHOD_2 0x02 //PHY Reg 0x03 bit0-3 == 0x0001
167 #define PCFG_METHOD_3 0x03 //PHY Reg 0x03 bit0-3 == 0x0002
171 u8 mcfg; /* depend on RTL8169 docs */
172 u32 RxConfigMask; /* should clear the bits supported by this chip */
173 } rtl_chip_info[] = {
175 "RTL-8169", MCFG_METHOD_1, 0xff7e1880,}, {
176 "RTL8169s/8110s", MCFG_METHOD_2, 0xff7e1880}, {
177 "RTL8169s/8110s", MCFG_METHOD_3, 0xff7e1880},};
179 enum RTL8169_registers {
180 MAC0 = 0x0, /* Ethernet hardware address. */
181 MAR0 = 0x8, /* Multicast filter. */
182 TxDescStartAddr = 0x20,
183 TxHDescStartAddr = 0x28,
208 RxDescStartAddr = 0xE4,
211 FuncEventMask = 0xF4,
212 FuncPresetState = 0xF8,
213 FuncForceEvent = 0xFC,
216 enum RTL8169_register_content {
217 /*InterruptStatusBits */
221 TxDescUnavail = 0x80,
244 Cfg9346_Unlock = 0xC0,
249 AcceptBroadcast = 0x08,
250 AcceptMulticast = 0x04,
252 AcceptAllPhys = 0x01,
259 TxInterFrameGapShift = 24,
260 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
262 /*rtl8169_PHYstatus */
272 /*GIGABIT_PHY_registers */
275 PHY_AUTO_NEGO_REG = 4,
276 PHY_1000_CTRL_REG = 9,
278 /*GIGABIT_PHY_REG_BIT */
279 PHY_Restart_Auto_Nego = 0x0200,
280 PHY_Enable_Auto_Nego = 0x1000,
282 /* PHY_STAT_REG = 1; */
283 PHY_Auto_Neco_Comp = 0x0020,
285 /* PHY_AUTO_NEGO_REG = 4; */
286 PHY_Cap_10_Half = 0x0020,
287 PHY_Cap_10_Full = 0x0040,
288 PHY_Cap_100_Half = 0x0080,
289 PHY_Cap_100_Full = 0x0100,
291 /* PHY_1000_CTRL_REG = 9; */
292 PHY_Cap_1000_Full = 0x0200,
293 PHY_Cap_1000_Half = 0x0100,
295 PHY_Cap_PAUSE = 0x0400,
296 PHY_Cap_ASYM_PAUSE = 0x0800,
308 TBILinkOK = 0x02000000,
311 enum _DescStatusBit {
332 /* The descriptors for this card are required to be aligned on 256
333 * byte boundaries. As the align attribute does not do more than 16
334 * bytes of alignment it requires some extra steps. Add 256 to the
335 * size of the array and the init_ring adjusts the alignment.
337 * UPDATE: This is no longer true; we can request arbitrary alignment.
340 /* Define the TX and RX Descriptors and Buffers */
341 #define __align_256 __attribute__ (( aligned ( 256 ) ))
343 struct TxDesc tx_ring[NUM_TX_DESC] __align_256;
344 unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];
345 struct RxDesc rx_ring[NUM_RX_DESC] __align_256;
346 unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
347 } r8169_bufs __shared;
348 #define tx_ring r8169_bufs.tx_ring
349 #define rx_ring r8169_bufs.rx_ring
350 #define txb r8169_bufs.txb
351 #define rxb r8169_bufs.rxb
353 static struct rtl8169_private {
354 void *mmio_addr; /* memory map physical address */
358 unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
359 unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
360 struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
361 struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
362 unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
363 unsigned char *Tx_skbuff[NUM_TX_DESC];
366 static struct rtl8169_private *tpc;
368 static const u16 rtl8169_intr_mask =
369 LinkChg | RxOverflow | RxFIFOOver | TxErr | TxOK | RxErr | RxOK;
370 static const unsigned int rtl8169_rx_config =
371 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift) |
374 static void rtl8169_hw_PHY_config(struct nic *nic __unused);
375 //static void rtl8169_hw_PHY_reset(struct net_device *dev);
377 #define RTL8169_WRITE_GMII_REG_BIT( ioaddr, reg, bitnum, bitval )\
380 if( bitval == 1 ){ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) | (bitval<<bitnum) ) & 0xffff ; } \
381 else{ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) & (~(0x0001<<bitnum)) ) & 0xffff ; } \
382 RTL8169_WRITE_GMII_REG( ioaddr, reg, val ); \
385 //=================================================================
390 // 20-16 5-bit GMII/MII register address
391 // 15-0 16-bit GMII/MII register data
392 //=================================================================
393 void RTL8169_WRITE_GMII_REG(unsigned long ioaddr, int RegAddr, int value)
397 RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
400 for (i = 2000; i > 0; i--) {
401 // Check if the RTL8169 has completed writing to the specified MII register
402 if (!(RTL_R32(PHYAR) & 0x80000000)) {
406 } // end of if( ! (RTL_R32(PHYAR)&0x80000000) )
407 } // end of for() loop
410 //=================================================================
411 int RTL8169_READ_GMII_REG(unsigned long ioaddr, int RegAddr)
415 RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
418 for (i = 2000; i > 0; i--) {
419 // Check if the RTL8169 has completed retrieving data from the specified MII register
420 if (RTL_R32(PHYAR) & 0x80000000) {
421 value = (int) (RTL_R32(PHYAR) & 0xFFFF);
425 } // end of if( RTL_R32(PHYAR) & 0x80000000 )
426 } // end of for() loop
432 static void mdio_write(int RegAddr, int value)
436 RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
439 for (i = 2000; i > 0; i--) {
440 /* Check if the RTL8169 has completed writing to the specified MII register */
441 if (!(RTL_R32(PHYAR) & 0x80000000)) {
449 static int mdio_read(int RegAddr)
453 RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
456 for (i = 2000; i > 0; i--) {
457 /* Check if the RTL8169 has completed retrieving data from the specified MII register */
458 if (RTL_R32(PHYAR) & 0x80000000) {
459 value = (int) (RTL_R32(PHYAR) & 0xFFFF);
469 #define IORESOURCE_MEM 0x00000200
471 static int rtl8169_init_board(struct pci_device *pdev)
474 // unsigned long mmio_end, mmio_flags
475 unsigned long mmio_start, mmio_len;
477 adjust_pci_device(pdev);
479 mmio_start = pci_bar_start(pdev, PCI_BASE_ADDRESS_1);
480 // mmio_end = pci_resource_end (pdev, 1);
481 // mmio_flags = pci_resource_flags (pdev, PCI_BASE_ADDRESS_1);
482 mmio_len = pci_bar_size(pdev, PCI_BASE_ADDRESS_1);
484 // make sure PCI base addr 1 is MMIO
485 // if (!(mmio_flags & IORESOURCE_MEM)) {
486 // printf ("region #1 not an MMIO resource, aborting\n");
490 // check for weird/broken PCI region reporting
491 if (mmio_len < RTL_MIN_IO_SIZE) {
492 printf("Invalid PCI region size(s), aborting\n");
495 #ifdef RTL8169_USE_IO
496 ioaddr = pci_bar_start(pdev, PCI_BASE_ADDRESS_0);
498 // ioremap MMIO region
499 ioaddr = (unsigned long) ioremap(mmio_start, mmio_len);
501 printk("cannot remap MMIO, aborting\n");
506 tpc->mmio_addr = &ioaddr;
507 /* Soft reset the chip. */
508 RTL_W8(ChipCmd, CmdReset);
510 /* Check that the chip has finished the reset. */
511 for (i = 1000; i > 0; i--)
512 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
516 // identify config method
518 unsigned long val32 = (RTL_R32(TxConfig) & 0x7c800000);
519 if (val32 == (0x1 << 28)) {
520 tpc->mcfg = MCFG_METHOD_4;
521 } else if (val32 == (0x1 << 26)) {
522 tpc->mcfg = MCFG_METHOD_3;
523 } else if (val32 == (0x1 << 23)) {
524 tpc->mcfg = MCFG_METHOD_2;
525 } else if (val32 == 0x00000000) {
526 tpc->mcfg = MCFG_METHOD_1;
528 tpc->mcfg = MCFG_METHOD_1;
533 (unsigned char) (RTL8169_READ_GMII_REG(ioaddr, 3) &
536 tpc->pcfg = PCFG_METHOD_1;
537 } else if (val8 == 0x01) {
538 tpc->pcfg = PCFG_METHOD_2;
539 } else if (val8 == 0x02) {
540 tpc->pcfg = PCFG_METHOD_3;
542 tpc->pcfg = PCFG_METHOD_3;
546 /* identify chip attached to board */
548 for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--)
549 if (tpc->mcfg == rtl_chip_info[i].mcfg) {
553 /* if unknown chip, assume array element #0, original RTL-8169 in this case */
554 DBG ( "PCI device: unknown chip version, assuming RTL-8169\n" );
555 DBG ( "PCI device: TxConfig = %#lX\n", ( unsigned long ) RTL_R32 ( TxConfig ) );
565 /**************************************************************************
566 IRQ - Wait for a frame
567 ***************************************************************************/
568 static void r8169_irq(struct nic *nic __unused, irq_action_t action)
571 int interested = RxOverflow | RxFIFOOver | RxErr | RxOK;
576 intr_status = RTL_R16(IntrStatus);
577 /* h/w no longer present (hotplug?) or major error,
579 if (intr_status == 0xFFFF)
582 intr_status = intr_status & ~interested;
583 if (action == ENABLE)
584 intr_status = intr_status | interested;
585 RTL_W16(IntrMask, intr_status);
588 RTL_W8(TxPoll, (RTL_R8(TxPoll) | 0x01));
593 /**************************************************************************
594 POLL - Wait for a frame
595 ***************************************************************************/
596 static int r8169_poll(struct nic *nic, int retreive)
598 /* return true if there's an ethernet packet ready to read */
599 /* nic->packet should contain data on return */
600 /* nic->packetlen should contain length of data */
602 unsigned int intr_status = 0;
603 cur_rx = tpc->cur_rx;
604 if ((tpc->RxDescArray[cur_rx].status & OWNbit) == 0) {
605 /* There is a packet ready */
608 intr_status = RTL_R16(IntrStatus);
609 /* h/w no longer present (hotplug?) or major error,
611 if (intr_status == 0xFFFF)
613 RTL_W16(IntrStatus, intr_status &
614 ~(RxFIFOOver | RxOverflow | RxOK));
616 if (!(tpc->RxDescArray[cur_rx].status & RxRES)) {
617 nic->packetlen = (int) (tpc->RxDescArray[cur_rx].
618 status & 0x00001FFF) - 4;
619 memcpy(nic->packet, tpc->RxBufferRing[cur_rx],
621 if (cur_rx == NUM_RX_DESC - 1)
622 tpc->RxDescArray[cur_rx].status =
623 (OWNbit | EORbit) + RX_BUF_SIZE;
625 tpc->RxDescArray[cur_rx].status =
626 OWNbit + RX_BUF_SIZE;
627 tpc->RxDescArray[cur_rx].buf_addr =
628 virt_to_bus(tpc->RxBufferRing[cur_rx]);
631 /* FIXME: shouldn't I reset the status on an error */
632 cur_rx = (cur_rx + 1) % NUM_RX_DESC;
633 tpc->cur_rx = cur_rx;
634 RTL_W16(IntrStatus, intr_status &
635 (RxFIFOOver | RxOverflow | RxOK));
640 tpc->cur_rx = cur_rx;
641 /* FIXME: There is no reason to do this as cur_rx did not change */
643 return (0); /* initially as this is called to flush the input */
647 /**************************************************************************
648 TRANSMIT - Transmit a frame
649 ***************************************************************************/
650 static void r8169_transmit(struct nic *nic, const char *d, /* Destination */
651 unsigned int t, /* Type */
652 unsigned int s, /* size */
655 /* send the packet to destination */
660 int entry = tpc->cur_tx % NUM_TX_DESC;
662 /* point to the current txb incase multiple tx_rings are used */
663 ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
664 memcpy(ptxb, d, ETH_ALEN);
665 memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN);
666 nstype = htons((u16) t);
667 memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
668 memcpy(ptxb + ETH_HLEN, p, s);
674 tpc->TxDescArray[entry].buf_addr = virt_to_bus(ptxb);
675 if (entry != (NUM_TX_DESC - 1))
676 tpc->TxDescArray[entry].status =
677 (OWNbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s :
680 tpc->TxDescArray[entry].status =
681 (OWNbit | EORbit | FSbit | LSbit) | ((s > ETH_ZLEN) ? s
683 RTL_W8(TxPoll, 0x40); /* set polling bit */
686 to = currticks() + TX_TIMEOUT;
687 while ((tpc->TxDescArray[entry].status & OWNbit) && (currticks() < to)); /* wait */
689 if (currticks() >= to) {
690 printf("TX Time Out");
694 static void rtl8169_set_rx_mode(struct nic *nic __unused)
696 u32 mc_filter[2]; /* Multicast hash filter */
701 /* Too many to filter perfectly -- accept all multicasts. */
702 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
703 mc_filter[1] = mc_filter[0] = 0xffffffff;
706 rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
707 rtl_chip_info[tpc->chipset].
710 RTL_W32(RxConfig, tmp);
711 RTL_W32(MAR0 + 0, mc_filter[0]);
712 RTL_W32(MAR0 + 4, mc_filter[1]);
714 static void rtl8169_hw_start(struct nic *nic)
718 /* Soft reset the chip. */
719 RTL_W8(ChipCmd, CmdReset);
721 /* Check that the chip has finished the reset. */
722 for (i = 1000; i > 0; i--) {
723 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
729 RTL_W8(Cfg9346, Cfg9346_Unlock);
730 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
731 RTL_W8(ETThReg, ETTh);
733 /* For gigabit rtl8169 */
734 RTL_W16(RxMaxSize, RxPacketMaxSize);
736 /* Set Rx Config register */
737 i = rtl8169_rx_config | (RTL_R32(RxConfig) &
738 rtl_chip_info[tpc->chipset].RxConfigMask);
739 RTL_W32(RxConfig, i);
741 /* Set DMA burst size and Interframe Gap Time */
743 (TX_DMA_BURST << TxDMAShift) | (InterFrameGap <<
744 TxInterFrameGapShift));
747 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd));
749 if (tpc->mcfg == MCFG_METHOD_2 || tpc->mcfg == MCFG_METHOD_3) {
751 (RTL_R16(CPlusCmd) | (1 << 14) | (1 << 3)));
753 ("Set MAC Reg C+CR Offset 0xE0: bit-3 and bit-14\n");
755 RTL_W16(CPlusCmd, (RTL_R16(CPlusCmd) | (1 << 3)));
756 DBG_PRINTF("Set MAC Reg C+CR Offset 0xE0: bit-3.\n");
760 //RTL_W16(0xE2, 0x1517);
761 //RTL_W16(0xE2, 0x152a);
762 //RTL_W16(0xE2, 0x282a);
763 RTL_W16(0xE2, 0x0000);
770 RTL_W32(TxDescStartAddr, virt_to_le32desc(tpc->TxDescArray));
771 RTL_W32(RxDescStartAddr, virt_to_le32desc(tpc->RxDescArray));
772 RTL_W8(Cfg9346, Cfg9346_Lock);
775 RTL_W32(RxMissed, 0);
777 rtl8169_set_rx_mode(nic);
779 /* no early-rx interrupts */
780 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
782 RTL_W16(IntrMask, rtl8169_intr_mask);
786 static void rtl8169_init_ring(struct nic *nic __unused)
792 memset(tpc->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
793 memset(tpc->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
795 for (i = 0; i < NUM_TX_DESC; i++) {
796 tpc->Tx_skbuff[i] = &txb[i];
799 for (i = 0; i < NUM_RX_DESC; i++) {
800 if (i == (NUM_RX_DESC - 1))
801 tpc->RxDescArray[i].status =
802 (OWNbit | EORbit) | RX_BUF_SIZE;
804 tpc->RxDescArray[i].status = OWNbit | RX_BUF_SIZE;
806 tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
807 tpc->RxDescArray[i].buf_addr =
808 virt_to_bus(tpc->RxBufferRing[i]);
812 /**************************************************************************
813 RESET - Finish setting up the ethernet interface
814 ***************************************************************************/
815 static void r8169_reset(struct nic *nic)
819 tpc->TxDescArray = tx_ring;
820 tpc->RxDescArray = rx_ring;
822 rtl8169_init_ring(nic);
823 rtl8169_hw_start(nic);
824 /* Construct a perfect filter frame with the mac address as first match
825 * and broadcast for all others */
826 for (i = 0; i < 192; i++)
829 txb[0] = nic->node_addr[0];
830 txb[1] = nic->node_addr[1];
831 txb[2] = nic->node_addr[2];
832 txb[3] = nic->node_addr[3];
833 txb[4] = nic->node_addr[4];
834 txb[5] = nic->node_addr[5];
837 /**************************************************************************
838 DISABLE - Turn off ethernet interface
839 ***************************************************************************/
840 static void r8169_disable ( struct nic *nic __unused ) {
842 /* Stop the chip's Tx and Rx DMA processes. */
843 RTL_W8(ChipCmd, 0x00);
845 /* Disable interrupts by clearing the interrupt mask. */
846 RTL_W16(IntrMask, 0x0000);
848 RTL_W32(RxMissed, 0);
850 tpc->TxDescArray = NULL;
851 tpc->RxDescArray = NULL;
852 for (i = 0; i < NUM_RX_DESC; i++) {
853 tpc->RxBufferRing[i] = NULL;
857 static struct nic_operations r8169_operations = {
858 .connect = dummy_connect,
860 .transmit = r8169_transmit,
865 static struct pci_device_id r8169_nics[] = {
866 PCI_ROM(0x10ec, 0x8169, "r8169", "RealTek RTL8169 Gigabit Ethernet"),
867 PCI_ROM(0x16ec, 0x0116, "usr-r8169", "US Robotics RTL8169 Gigabit Ethernet"),
868 PCI_ROM(0x1186, 0x4300, "dlink-r8169", "D-Link RTL8169 Gigabit Ethernet"),
871 PCI_DRIVER ( r8169_driver, r8169_nics, PCI_NO_CLASS );
873 /**************************************************************************
874 PROBE - Look for an adapter, this routine's visible to the outside
875 ***************************************************************************/
877 #define board_found 1
879 static int r8169_probe ( struct nic *nic, struct pci_device *pci ) {
881 static int board_idx = -1;
882 static int printed_version = 0;
884 int option = -1, Cap10_100 = 0, Cap1000 = 0;
886 printf ( "r8169.c: Found %s, Vendor=%hX Device=%hX\n",
887 pci->name, pci->vendor, pci->device );
893 /* point to private storage */
896 rc = rtl8169_init_board(pci); /* Return code is meaningless */
898 /* Get MAC address. FIXME: read EEPROM */
899 for (i = 0; i < MAC_ADDR_LEN; i++)
900 nic->node_addr[i] = RTL_R8(MAC0 + i);
902 DBG ( "%s: Identified chip type is '%s'.\n", pci->name,
903 rtl_chip_info[tpc->chipset].name );
905 /* Print out some hardware info */
906 DBG ( "%s: %s at IOAddr %#hX, ", pci->name, eth_ntoa ( nic->node_addr ),
910 rtl8169_hw_PHY_config(nic);
912 DBG_PRINTF("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
915 if (tpc->mcfg < MCFG_METHOD_3) {
916 DBG_PRINTF("Set PCI Latency=0x40\n");
917 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0x40);
920 if (tpc->mcfg == MCFG_METHOD_2) {
921 DBG_PRINTF("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
923 DBG_PRINTF("Set PHY Reg 0x0bh = 0x00h\n");
924 RTL8169_WRITE_GMII_REG(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
927 /* if TBI is not endbled */
928 if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
929 int val = RTL8169_READ_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG);
931 #ifdef RTL8169_HW_FLOW_CONTROL_SUPPORT
932 val |= PHY_Cap_PAUSE | PHY_Cap_ASYM_PAUSE;
933 #endif //end #define RTL8169_HW_FLOW_CONTROL_SUPPORT
936 /* Force RTL8169 in 10/100/1000 Full/Half mode. */
938 printf(" Force-mode Enabled.\n");
939 Cap10_100 = 0, Cap1000 = 0;
942 Cap10_100 = PHY_Cap_10_Half;
943 Cap1000 = PHY_Cap_Null;
946 Cap10_100 = PHY_Cap_10_Full;
947 Cap1000 = PHY_Cap_Null;
950 Cap10_100 = PHY_Cap_100_Half;
951 Cap1000 = PHY_Cap_Null;
954 Cap10_100 = PHY_Cap_100_Full;
955 Cap1000 = PHY_Cap_Null;
958 Cap10_100 = PHY_Cap_Null;
959 Cap1000 = PHY_Cap_1000_Full;
964 RTL8169_WRITE_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG, Cap10_100 | (val & 0xC1F)); //leave PHY_AUTO_NEGO_REG bit4:0 unchanged
965 RTL8169_WRITE_GMII_REG(ioaddr, PHY_1000_CTRL_REG,
968 DBG ( "%s: Auto-negotiation Enabled.\n", pci->name );
970 // enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged
971 RTL8169_WRITE_GMII_REG(ioaddr, PHY_AUTO_NEGO_REG,
975 PHY_Cap_100_Full | (val &
978 // enable 1000 Full Mode
979 // RTL8169_WRITE_GMII_REG( ioaddr, PHY_1000_CTRL_REG, PHY_Cap_1000_Full );
980 RTL8169_WRITE_GMII_REG(ioaddr, PHY_1000_CTRL_REG, PHY_Cap_1000_Full | PHY_Cap_1000_Half); //rtl8168
982 } // end of if( option > 0 )
984 // Enable auto-negotiation and restart auto-nigotiation
985 RTL8169_WRITE_GMII_REG(ioaddr, PHY_CTRL_REG,
986 PHY_Enable_Auto_Nego |
987 PHY_Restart_Auto_Nego);
990 // wait for auto-negotiation process
991 for (i = 10000; i > 0; i--) {
992 //check if auto-negotiation complete
993 if (RTL8169_READ_GMII_REG(ioaddr, PHY_STAT_REG) &
994 PHY_Auto_Neco_Comp) {
996 option = RTL_R8(PHYstatus);
997 if (option & _1000bpsF) {
999 ("1000Mbps Full-duplex operation.\n");
1002 ("%sMbps %s-duplex operation.\n",
1003 (option & _100bps) ? "100" :
1005 (option & FullDup) ? "Full" :
1011 } // end of if( RTL8169_READ_GMII_REG(ioaddr, 1) & 0x20 )
1012 } // end for-loop to wait for auto-negotiation process
1018 ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
1020 (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
1025 /* point to NIC specific routines */
1026 nic->nic_op = &r8169_operations;
1027 pci_fill_nic ( nic, pci );
1028 nic->irqno = pci->irq;
1029 nic->ioaddr = ioaddr;
1034 //======================================================================================================
1036 static void rtl8169_hw_PHY_reset(struct nic *nic __unused)
1038 int val, phy_reset_expiretime = 50;
1039 struct rtl8169_private *priv = dev->priv;
1040 unsigned long ioaddr = priv->ioaddr;
1042 DBG_PRINTF("%s: Reset RTL8169s PHY\n", dev->name);
1044 val = ( RTL8169_READ_GMII_REG( ioaddr, 0 ) | 0x8000 ) & 0xffff;
1045 RTL8169_WRITE_GMII_REG( ioaddr, 0, val );
1047 do //waiting for phy reset
1049 if( RTL8169_READ_GMII_REG( ioaddr, 0 ) & 0x8000 ){
1050 phy_reset_expiretime --;
1056 }while( phy_reset_expiretime >= 0 );
1058 assert( phy_reset_expiretime > 0 );
1063 //======================================================================================================
1064 static void rtl8169_hw_PHY_config(struct nic *nic __unused)
1067 DBG_PRINTF("priv->mcfg=%d, priv->pcfg=%d\n", tpc->mcfg, tpc->pcfg);
1069 if (tpc->mcfg == MCFG_METHOD_4) {
1071 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x1F, 0x0001 );
1072 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x1b, 0x841e );
1073 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x0e, 0x7bfb );
1074 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x09, 0x273a );
1077 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x1F,
1079 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1081 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x1F,
1083 } else if ((tpc->mcfg == MCFG_METHOD_2)
1084 || (tpc->mcfg == MCFG_METHOD_3)) {
1085 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x1F,
1087 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x15,
1089 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x18,
1091 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1093 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1095 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1097 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1099 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1101 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1103 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1105 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1107 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1109 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1111 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1113 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1115 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1117 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1119 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
1121 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x03,
1123 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1125 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1127 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1129 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
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, 0x03,
1137 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1139 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1141 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1143 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
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, 0x03,
1151 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x02,
1153 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x01,
1155 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x00,
1157 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x04,
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, 0x1F,
1165 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr, 0x0B,
1168 DBG_PRINTF("tpc->mcfg=%d. Discard hw PHY config.\n",
1173 DRIVER ( "r8169/PCI", nic_driver, pci_driver, r8169_driver,
1174 r8169_probe, r8169_disable );