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
79 #define assert(expr) \
80 if(!(expr)) { printk( "Assertion failed! %s,%s,%s,line=%d\n", #expr,__FILE__,__FUNCTION__,__LINE__); }
83 #define DBG_PRINTF( fmt, args...) printk("r8169: " fmt, ## args);
88 #define assert(expr) do {} while (0)
91 #define DBG_PRINTF( fmt, args...) ;
93 #endif // end of #ifdef RTL8169_DEBUG
102 static int media
= -1;
105 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
106 static int max_interrupt_work
= 20;
110 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
111 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
112 static int multicast_filter_limit
= 32;
115 /* MAC address length*/
116 #define MAC_ADDR_LEN 6
118 /* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
119 #define MAX_ETH_FRAME_SIZE 1536
121 #define TX_FIFO_THRESH 256 /* In bytes */
123 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
124 #define RX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
125 #define TX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
126 #define ETTh 0x3F /* 0x3F means NO threshold */
128 #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
129 #define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
130 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
132 #define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
133 #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
134 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
136 #define RTL_MIN_IO_SIZE 0x80
137 #define TX_TIMEOUT (6*HZ)
139 #define RTL8169_TIMER_EXPIRE_TIME 100 //100
141 #define ETH_HDR_LEN 14
142 #define DEFAULT_MTU 1500
143 #define DEFAULT_RX_BUF_LEN 1536
146 #ifdef RTL8169_JUMBO_FRAME_SUPPORT
147 #define MAX_JUMBO_FRAME_MTU ( 10000 )
148 #define MAX_RX_SKBDATA_SIZE ( MAX_JUMBO_FRAME_MTU + ETH_HDR_LEN )
150 #define MAX_RX_SKBDATA_SIZE 1600
151 #endif //end #ifdef RTL8169_JUMBO_FRAME_SUPPORT
153 #ifdef RTL8169_USE_IO
154 #define RTL_W8(reg, val8) outb ((val8), ioaddr + (reg))
155 #define RTL_W16(reg, val16) outw ((val16), ioaddr + (reg))
156 #define RTL_W32(reg, val32) outl ((val32), ioaddr + (reg))
157 #define RTL_R8(reg) inb (ioaddr + (reg))
158 #define RTL_R16(reg) inw (ioaddr + (reg))
159 #define RTL_R32(reg) ((unsigned long) inl (ioaddr + (reg)))
161 /* write/read MMIO register */
162 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
163 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
164 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
165 #define RTL_R8(reg) readb (ioaddr + (reg))
166 #define RTL_R16(reg) readw (ioaddr + (reg))
167 #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
170 #define MCFG_METHOD_1 0x01
171 #define MCFG_METHOD_2 0x02
172 #define MCFG_METHOD_3 0x03
173 #define MCFG_METHOD_4 0x04
175 #define PCFG_METHOD_1 0x01 //PHY Reg 0x03 bit0-3 == 0x0000
176 #define PCFG_METHOD_2 0x02 //PHY Reg 0x03 bit0-3 == 0x0001
177 #define PCFG_METHOD_3 0x03 //PHY Reg 0x03 bit0-3 == 0x0002
181 u8 mcfg
; /* depend on RTL8169 docs */
182 u32 RxConfigMask
; /* should clear the bits supported by this chip */
183 } rtl_chip_info
[] = {
185 "RTL-8169", MCFG_METHOD_1
, 0xff7e1880,}, {
186 "RTL8169s/8110s", MCFG_METHOD_2
, 0xff7e1880}, {
187 "RTL8169s/8110s", MCFG_METHOD_3
, 0xff7e1880},};
189 enum RTL8169_registers
{
190 MAC0
= 0x0, /* Ethernet hardware address. */
191 MAR0
= 0x8, /* Multicast filter. */
192 TxDescStartAddr
= 0x20,
193 TxHDescStartAddr
= 0x28,
218 RxDescStartAddr
= 0xE4,
221 FuncEventMask
= 0xF4,
222 FuncPresetState
= 0xF8,
223 FuncForceEvent
= 0xFC,
226 enum RTL8169_register_content
{
227 /*InterruptStatusBits */
231 TxDescUnavail
= 0x80,
254 Cfg9346_Unlock
= 0xC0,
259 AcceptBroadcast
= 0x08,
260 AcceptMulticast
= 0x04,
262 AcceptAllPhys
= 0x01,
269 TxInterFrameGapShift
= 24,
270 TxDMAShift
= 8, /* DMA burst value (0-7) is shift this many bits */
272 /*rtl8169_PHYstatus */
282 /*GIGABIT_PHY_registers */
285 PHY_AUTO_NEGO_REG
= 4,
286 PHY_1000_CTRL_REG
= 9,
288 /*GIGABIT_PHY_REG_BIT */
289 PHY_Restart_Auto_Nego
= 0x0200,
290 PHY_Enable_Auto_Nego
= 0x1000,
292 /* PHY_STAT_REG = 1; */
293 PHY_Auto_Neco_Comp
= 0x0020,
295 /* PHY_AUTO_NEGO_REG = 4; */
296 PHY_Cap_10_Half
= 0x0020,
297 PHY_Cap_10_Full
= 0x0040,
298 PHY_Cap_100_Half
= 0x0080,
299 PHY_Cap_100_Full
= 0x0100,
301 /* PHY_1000_CTRL_REG = 9; */
302 PHY_Cap_1000_Full
= 0x0200,
303 PHY_Cap_1000_Half
= 0x0100,
305 PHY_Cap_PAUSE
= 0x0400,
306 PHY_Cap_ASYM_PAUSE
= 0x0800,
318 TBILinkOK
= 0x02000000,
321 enum _DescStatusBit
{
342 /* The descriptors for this card are required to be aligned on 256
343 * byte boundaries. As the align attribute does not do more than 16
344 * bytes of alignment it requires some extra steps. Add 256 to the
345 * size of the array and the init_ring adjusts the alignment.
347 * UPDATE: This is no longer true; we can request arbitrary alignment.
350 /* Define the TX and RX Descriptors and Buffers */
351 #define __align_256 __attribute__ (( aligned ( 256 ) ))
353 struct TxDesc tx_ring
[NUM_TX_DESC
] __align_256
;
354 unsigned char txb
[NUM_TX_DESC
* RX_BUF_SIZE
];
355 struct RxDesc rx_ring
[NUM_RX_DESC
] __align_256
;
356 unsigned char rxb
[NUM_RX_DESC
* RX_BUF_SIZE
];
357 } r8169_bufs __shared
;
358 #define tx_ring r8169_bufs.tx_ring
359 #define rx_ring r8169_bufs.rx_ring
360 #define txb r8169_bufs.txb
361 #define rxb r8169_bufs.rxb
363 static struct rtl8169_private
{
364 void *mmio_addr
; /* memory map physical address */
368 unsigned long cur_rx
; /* Index into the Rx descriptor buffer of next Rx pkt. */
369 unsigned long cur_tx
; /* Index into the Tx descriptor buffer of next Rx pkt. */
370 struct TxDesc
*TxDescArray
; /* Index of 256-alignment Tx Descriptor buffer */
371 struct RxDesc
*RxDescArray
; /* Index of 256-alignment Rx Descriptor buffer */
372 unsigned char *RxBufferRing
[NUM_RX_DESC
]; /* Index of Rx Buffer array */
373 unsigned char *Tx_skbuff
[NUM_TX_DESC
];
376 static struct rtl8169_private
*tpc
;
378 static const u16 rtl8169_intr_mask
=
379 LinkChg
| RxOverflow
| RxFIFOOver
| TxErr
| TxOK
| RxErr
| RxOK
;
380 static const unsigned int rtl8169_rx_config
=
381 (RX_FIFO_THRESH
<< RxCfgFIFOShift
) | (RX_DMA_BURST
<< RxCfgDMAShift
) |
384 static void rtl8169_hw_PHY_config(struct nic
*nic __unused
);
385 //static void rtl8169_hw_PHY_reset(struct net_device *dev);
387 #define RTL8169_WRITE_GMII_REG_BIT( ioaddr, reg, bitnum, bitval )\
390 if( bitval == 1 ){ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) | (bitval<<bitnum) ) & 0xffff ; } \
391 else{ val = ( RTL8169_READ_GMII_REG( ioaddr, reg ) & (~(0x0001<<bitnum)) ) & 0xffff ; } \
392 RTL8169_WRITE_GMII_REG( ioaddr, reg, val ); \
395 //=================================================================
400 // 20-16 5-bit GMII/MII register address
401 // 15-0 16-bit GMII/MII register data
402 //=================================================================
403 void RTL8169_WRITE_GMII_REG(unsigned long ioaddr
, int RegAddr
, int value
)
407 RTL_W32(PHYAR
, 0x80000000 | (RegAddr
& 0xFF) << 16 | value
);
410 for (i
= 2000; i
> 0; i
--) {
411 // Check if the RTL8169 has completed writing to the specified MII register
412 if (!(RTL_R32(PHYAR
) & 0x80000000)) {
416 } // end of if( ! (RTL_R32(PHYAR)&0x80000000) )
417 } // end of for() loop
420 //=================================================================
421 int RTL8169_READ_GMII_REG(unsigned long ioaddr
, int RegAddr
)
425 RTL_W32(PHYAR
, 0x0 | (RegAddr
& 0xFF) << 16);
428 for (i
= 2000; i
> 0; i
--) {
429 // Check if the RTL8169 has completed retrieving data from the specified MII register
430 if (RTL_R32(PHYAR
) & 0x80000000) {
431 value
= (int) (RTL_R32(PHYAR
) & 0xFFFF);
435 } // end of if( RTL_R32(PHYAR) & 0x80000000 )
436 } // end of for() loop
442 static void mdio_write(int RegAddr
, int value
)
446 RTL_W32(PHYAR
, 0x80000000 | (RegAddr
& 0xFF) << 16 | value
);
449 for (i
= 2000; i
> 0; i
--) {
450 /* Check if the RTL8169 has completed writing to the specified MII register */
451 if (!(RTL_R32(PHYAR
) & 0x80000000)) {
459 static int mdio_read(int RegAddr
)
463 RTL_W32(PHYAR
, 0x0 | (RegAddr
& 0xFF) << 16);
466 for (i
= 2000; i
> 0; i
--) {
467 /* Check if the RTL8169 has completed retrieving data from the specified MII register */
468 if (RTL_R32(PHYAR
) & 0x80000000) {
469 value
= (int) (RTL_R32(PHYAR
) & 0xFFFF);
479 #define IORESOURCE_MEM 0x00000200
481 static int rtl8169_init_board(struct pci_device
*pdev
)
484 // unsigned long mmio_end, mmio_flags
485 unsigned long mmio_start
, mmio_len
;
487 adjust_pci_device(pdev
);
489 mmio_start
= pci_bar_start(pdev
, PCI_BASE_ADDRESS_1
);
490 // mmio_end = pci_resource_end (pdev, 1);
491 // mmio_flags = pci_resource_flags (pdev, PCI_BASE_ADDRESS_1);
492 mmio_len
= pci_bar_size(pdev
, PCI_BASE_ADDRESS_1
);
494 // make sure PCI base addr 1 is MMIO
495 // if (!(mmio_flags & IORESOURCE_MEM)) {
496 // printf ("region #1 not an MMIO resource, aborting\n");
500 // check for weird/broken PCI region reporting
501 if (mmio_len
< RTL_MIN_IO_SIZE
) {
502 printf("Invalid PCI region size(s), aborting\n");
505 #ifdef RTL8169_USE_IO
506 ioaddr
= pci_bar_start(pdev
, PCI_BASE_ADDRESS_0
);
508 // ioremap MMIO region
509 ioaddr
= (unsigned long) ioremap(mmio_start
, mmio_len
);
511 printk("cannot remap MMIO, aborting\n");
516 tpc
->mmio_addr
= &ioaddr
;
517 /* Soft reset the chip. */
518 RTL_W8(ChipCmd
, CmdReset
);
520 /* Check that the chip has finished the reset. */
521 for (i
= 1000; i
> 0; i
--)
522 if ((RTL_R8(ChipCmd
) & CmdReset
) == 0)
526 // identify config method
528 unsigned long val32
= (RTL_R32(TxConfig
) & 0x7c800000);
529 if (val32
== (0x1 << 28)) {
530 tpc
->mcfg
= MCFG_METHOD_4
;
531 } else if (val32
== (0x1 << 26)) {
532 tpc
->mcfg
= MCFG_METHOD_3
;
533 } else if (val32
== (0x1 << 23)) {
534 tpc
->mcfg
= MCFG_METHOD_2
;
535 } else if (val32
== 0x00000000) {
536 tpc
->mcfg
= MCFG_METHOD_1
;
538 tpc
->mcfg
= MCFG_METHOD_1
;
543 (unsigned char) (RTL8169_READ_GMII_REG(ioaddr
, 3) &
546 tpc
->pcfg
= PCFG_METHOD_1
;
547 } else if (val8
== 0x01) {
548 tpc
->pcfg
= PCFG_METHOD_2
;
549 } else if (val8
== 0x02) {
550 tpc
->pcfg
= PCFG_METHOD_3
;
552 tpc
->pcfg
= PCFG_METHOD_3
;
556 /* identify chip attached to board */
558 for (i
= ARRAY_SIZE(rtl_chip_info
) - 1; i
>= 0; i
--)
559 if (tpc
->mcfg
== rtl_chip_info
[i
].mcfg
) {
563 /* if unknown chip, assume array element #0, original RTL-8169 in this case */
564 DBG ( "PCI device: unknown chip version, assuming RTL-8169\n" );
565 DBG ( "PCI device: TxConfig = %#lX\n", ( unsigned long ) RTL_R32 ( TxConfig
) );
575 /**************************************************************************
576 IRQ - Wait for a frame
577 ***************************************************************************/
578 static void r8169_irq(struct nic
*nic __unused
, irq_action_t action
)
581 int interested
= RxOverflow
| RxFIFOOver
| RxErr
| RxOK
;
586 intr_status
= RTL_R16(IntrStatus
);
587 /* h/w no longer present (hotplug?) or major error,
589 if (intr_status
== 0xFFFF)
592 intr_status
= intr_status
& ~interested
;
593 if (action
== ENABLE
)
594 intr_status
= intr_status
| interested
;
595 RTL_W16(IntrMask
, intr_status
);
598 RTL_W8(TxPoll
, (RTL_R8(TxPoll
) | 0x01));
603 /**************************************************************************
604 POLL - Wait for a frame
605 ***************************************************************************/
606 static int r8169_poll(struct nic
*nic
, int retreive
)
608 /* return true if there's an ethernet packet ready to read */
609 /* nic->packet should contain data on return */
610 /* nic->packetlen should contain length of data */
612 unsigned int intr_status
= 0;
613 cur_rx
= tpc
->cur_rx
;
614 if ((tpc
->RxDescArray
[cur_rx
].status
& OWNbit
) == 0) {
615 /* There is a packet ready */
618 intr_status
= RTL_R16(IntrStatus
);
619 /* h/w no longer present (hotplug?) or major error,
621 if (intr_status
== 0xFFFF)
623 RTL_W16(IntrStatus
, intr_status
&
624 ~(RxFIFOOver
| RxOverflow
| RxOK
));
626 if (!(tpc
->RxDescArray
[cur_rx
].status
& RxRES
)) {
627 nic
->packetlen
= (int) (tpc
->RxDescArray
[cur_rx
].
628 status
& 0x00001FFF) - 4;
629 memcpy(nic
->packet
, tpc
->RxBufferRing
[cur_rx
],
631 if (cur_rx
== NUM_RX_DESC
- 1)
632 tpc
->RxDescArray
[cur_rx
].status
=
633 (OWNbit
| EORbit
) + RX_BUF_SIZE
;
635 tpc
->RxDescArray
[cur_rx
].status
=
636 OWNbit
+ RX_BUF_SIZE
;
637 tpc
->RxDescArray
[cur_rx
].buf_addr
=
638 virt_to_bus(tpc
->RxBufferRing
[cur_rx
]);
641 /* FIXME: shouldn't I reset the status on an error */
642 cur_rx
= (cur_rx
+ 1) % NUM_RX_DESC
;
643 tpc
->cur_rx
= cur_rx
;
644 RTL_W16(IntrStatus
, intr_status
&
645 (RxFIFOOver
| RxOverflow
| RxOK
));
650 tpc
->cur_rx
= cur_rx
;
651 /* FIXME: There is no reason to do this as cur_rx did not change */
653 return (0); /* initially as this is called to flush the input */
657 /**************************************************************************
658 TRANSMIT - Transmit a frame
659 ***************************************************************************/
660 static void r8169_transmit(struct nic
*nic
, const char *d
, /* Destination */
661 unsigned int t
, /* Type */
662 unsigned int s
, /* size */
665 /* send the packet to destination */
670 int entry
= tpc
->cur_tx
% NUM_TX_DESC
;
672 /* point to the current txb incase multiple tx_rings are used */
673 ptxb
= tpc
->Tx_skbuff
[entry
* MAX_ETH_FRAME_SIZE
];
674 memcpy(ptxb
, d
, ETH_ALEN
);
675 memcpy(ptxb
+ ETH_ALEN
, nic
->node_addr
, ETH_ALEN
);
676 nstype
= htons((u16
) t
);
677 memcpy(ptxb
+ 2 * ETH_ALEN
, (u8
*) & nstype
, 2);
678 memcpy(ptxb
+ ETH_HLEN
, p
, s
);
684 tpc
->TxDescArray
[entry
].buf_addr
= virt_to_bus(ptxb
);
685 if (entry
!= (NUM_TX_DESC
- 1))
686 tpc
->TxDescArray
[entry
].status
=
687 (OWNbit
| FSbit
| LSbit
) | ((s
> ETH_ZLEN
) ? s
:
690 tpc
->TxDescArray
[entry
].status
=
691 (OWNbit
| EORbit
| FSbit
| LSbit
) | ((s
> ETH_ZLEN
) ? s
693 RTL_W8(TxPoll
, 0x40); /* set polling bit */
696 to
= currticks() + TX_TIMEOUT
;
697 while ((tpc
->TxDescArray
[entry
].status
& OWNbit
) && (currticks() < to
)); /* wait */
699 if (currticks() >= to
) {
700 printf("TX Time Out");
704 static void rtl8169_set_rx_mode(struct nic
*nic __unused
)
706 u32 mc_filter
[2]; /* Multicast hash filter */
711 /* Too many to filter perfectly -- accept all multicasts. */
712 rx_mode
= AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
;
713 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
716 rtl8169_rx_config
| rx_mode
| (RTL_R32(RxConfig
) &
717 rtl_chip_info
[tpc
->chipset
].
720 RTL_W32(RxConfig
, tmp
);
721 RTL_W32(MAR0
+ 0, mc_filter
[0]);
722 RTL_W32(MAR0
+ 4, mc_filter
[1]);
724 static void rtl8169_hw_start(struct nic
*nic
)
728 /* Soft reset the chip. */
729 RTL_W8(ChipCmd
, CmdReset
);
731 /* Check that the chip has finished the reset. */
732 for (i
= 1000; i
> 0; i
--) {
733 if ((RTL_R8(ChipCmd
) & CmdReset
) == 0)
739 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
740 RTL_W8(ChipCmd
, CmdTxEnb
| CmdRxEnb
);
741 RTL_W8(ETThReg
, ETTh
);
743 /* For gigabit rtl8169 */
744 RTL_W16(RxMaxSize
, RxPacketMaxSize
);
746 /* Set Rx Config register */
747 i
= rtl8169_rx_config
| (RTL_R32(RxConfig
) &
748 rtl_chip_info
[tpc
->chipset
].RxConfigMask
);
749 RTL_W32(RxConfig
, i
);
751 /* Set DMA burst size and Interframe Gap Time */
753 (TX_DMA_BURST
<< TxDMAShift
) | (InterFrameGap
<<
754 TxInterFrameGapShift
));
757 RTL_W16(CPlusCmd
, RTL_R16(CPlusCmd
));
759 if (tpc
->mcfg
== MCFG_METHOD_2
|| tpc
->mcfg
== MCFG_METHOD_3
) {
761 (RTL_R16(CPlusCmd
) | (1 << 14) | (1 << 3)));
763 ("Set MAC Reg C+CR Offset 0xE0: bit-3 and bit-14\n");
765 RTL_W16(CPlusCmd
, (RTL_R16(CPlusCmd
) | (1 << 3)));
766 DBG_PRINTF("Set MAC Reg C+CR Offset 0xE0: bit-3.\n");
770 //RTL_W16(0xE2, 0x1517);
771 //RTL_W16(0xE2, 0x152a);
772 //RTL_W16(0xE2, 0x282a);
773 RTL_W16(0xE2, 0x0000);
780 RTL_W32(TxDescStartAddr
, virt_to_le32desc(tpc
->TxDescArray
));
781 RTL_W32(RxDescStartAddr
, virt_to_le32desc(tpc
->RxDescArray
));
782 RTL_W8(Cfg9346
, Cfg9346_Lock
);
785 RTL_W32(RxMissed
, 0);
787 rtl8169_set_rx_mode(nic
);
789 /* no early-rx interrupts */
790 RTL_W16(MultiIntr
, RTL_R16(MultiIntr
) & 0xF000);
792 RTL_W16(IntrMask
, rtl8169_intr_mask
);
796 static void rtl8169_init_ring(struct nic
*nic __unused
)
802 memset(tpc
->TxDescArray
, 0x0, NUM_TX_DESC
* sizeof(struct TxDesc
));
803 memset(tpc
->RxDescArray
, 0x0, NUM_RX_DESC
* sizeof(struct RxDesc
));
805 for (i
= 0; i
< NUM_TX_DESC
; i
++) {
806 tpc
->Tx_skbuff
[i
] = &txb
[i
];
809 for (i
= 0; i
< NUM_RX_DESC
; i
++) {
810 if (i
== (NUM_RX_DESC
- 1))
811 tpc
->RxDescArray
[i
].status
=
812 (OWNbit
| EORbit
) | RX_BUF_SIZE
;
814 tpc
->RxDescArray
[i
].status
= OWNbit
| RX_BUF_SIZE
;
816 tpc
->RxBufferRing
[i
] = &rxb
[i
* RX_BUF_SIZE
];
817 tpc
->RxDescArray
[i
].buf_addr
=
818 virt_to_bus(tpc
->RxBufferRing
[i
]);
822 /**************************************************************************
823 RESET - Finish setting up the ethernet interface
824 ***************************************************************************/
825 static void r8169_reset(struct nic
*nic
)
829 tpc
->TxDescArray
= tx_ring
;
830 tpc
->RxDescArray
= rx_ring
;
832 rtl8169_init_ring(nic
);
833 rtl8169_hw_start(nic
);
834 /* Construct a perfect filter frame with the mac address as first match
835 * and broadcast for all others */
836 for (i
= 0; i
< 192; i
++)
839 txb
[0] = nic
->node_addr
[0];
840 txb
[1] = nic
->node_addr
[1];
841 txb
[2] = nic
->node_addr
[2];
842 txb
[3] = nic
->node_addr
[3];
843 txb
[4] = nic
->node_addr
[4];
844 txb
[5] = nic
->node_addr
[5];
847 /**************************************************************************
848 DISABLE - Turn off ethernet interface
849 ***************************************************************************/
850 static void r8169_disable ( struct nic
*nic __unused
) {
852 /* Stop the chip's Tx and Rx DMA processes. */
853 RTL_W8(ChipCmd
, 0x00);
855 /* Disable interrupts by clearing the interrupt mask. */
856 RTL_W16(IntrMask
, 0x0000);
858 RTL_W32(RxMissed
, 0);
860 tpc
->TxDescArray
= NULL
;
861 tpc
->RxDescArray
= NULL
;
862 for (i
= 0; i
< NUM_RX_DESC
; i
++) {
863 tpc
->RxBufferRing
[i
] = NULL
;
867 static struct nic_operations r8169_operations
= {
868 .connect
= dummy_connect
,
870 .transmit
= r8169_transmit
,
875 static struct pci_device_id r8169_nics
[] = {
876 PCI_ROM(0x10ec, 0x8169, "r8169", "RealTek RTL8169 Gigabit Ethernet"),
877 PCI_ROM(0x16ec, 0x0116, "usr-r8169", "US Robotics RTL8169 Gigabit Ethernet"),
878 PCI_ROM(0x1186, 0x4300, "dlink-r8169", "D-Link RTL8169 Gigabit Ethernet"),
881 PCI_DRIVER ( r8169_driver
, r8169_nics
, PCI_NO_CLASS
);
883 /**************************************************************************
884 PROBE - Look for an adapter, this routine's visible to the outside
885 ***************************************************************************/
887 #define board_found 1
889 static int r8169_probe ( struct nic
*nic
, struct pci_device
*pci
) {
891 static int board_idx
= -1;
892 static int printed_version
= 0;
894 int option
= -1, Cap10_100
= 0, Cap1000
= 0;
896 printf ( "r8169.c: Found %s, Vendor=%hX Device=%hX\n",
897 pci
->driver_name
, pci
->vendor
, pci
->device
);
903 /* point to private storage */
906 rc
= rtl8169_init_board(pci
); /* Return code is meaningless */
908 /* Get MAC address. FIXME: read EEPROM */
909 for (i
= 0; i
< MAC_ADDR_LEN
; i
++)
910 nic
->node_addr
[i
] = RTL_R8(MAC0
+ i
);
912 DBG ( "%s: Identified chip type is '%s'.\n", pci
->driver_name
,
913 rtl_chip_info
[tpc
->chipset
].name
);
915 /* Print out some hardware info */
916 DBG ( "%s: %s at IOAddr %#hX, ", pci
->driver_name
, eth_ntoa ( nic
->node_addr
),
917 (unsigned int) ioaddr
);
920 rtl8169_hw_PHY_config(nic
);
922 DBG_PRINTF("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
925 if (tpc
->mcfg
< MCFG_METHOD_3
) {
926 DBG_PRINTF("Set PCI Latency=0x40\n");
927 pci_write_config_byte(pci
, PCI_LATENCY_TIMER
, 0x40);
930 if (tpc
->mcfg
== MCFG_METHOD_2
) {
931 DBG_PRINTF("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
933 DBG_PRINTF("Set PHY Reg 0x0bh = 0x00h\n");
934 RTL8169_WRITE_GMII_REG(ioaddr
, 0x0b, 0x0000); //w 0x0b 15 0 0
937 /* if TBI is not endbled */
938 if (!(RTL_R8(PHYstatus
) & TBI_Enable
)) {
939 int val
= RTL8169_READ_GMII_REG(ioaddr
, PHY_AUTO_NEGO_REG
);
941 #ifdef RTL8169_HW_FLOW_CONTROL_SUPPORT
942 val
|= PHY_Cap_PAUSE
| PHY_Cap_ASYM_PAUSE
;
943 #endif //end #define RTL8169_HW_FLOW_CONTROL_SUPPORT
946 /* Force RTL8169 in 10/100/1000 Full/Half mode. */
948 printf(" Force-mode Enabled.\n");
949 Cap10_100
= 0, Cap1000
= 0;
952 Cap10_100
= PHY_Cap_10_Half
;
953 Cap1000
= PHY_Cap_Null
;
956 Cap10_100
= PHY_Cap_10_Full
;
957 Cap1000
= PHY_Cap_Null
;
960 Cap10_100
= PHY_Cap_100_Half
;
961 Cap1000
= PHY_Cap_Null
;
964 Cap10_100
= PHY_Cap_100_Full
;
965 Cap1000
= PHY_Cap_Null
;
968 Cap10_100
= PHY_Cap_Null
;
969 Cap1000
= PHY_Cap_1000_Full
;
974 RTL8169_WRITE_GMII_REG(ioaddr
, PHY_AUTO_NEGO_REG
, Cap10_100
| (val
& 0xC1F)); //leave PHY_AUTO_NEGO_REG bit4:0 unchanged
975 RTL8169_WRITE_GMII_REG(ioaddr
, PHY_1000_CTRL_REG
,
978 DBG ( "%s: Auto-negotiation Enabled.\n", pci
->driver_name
);
980 // enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged
981 RTL8169_WRITE_GMII_REG(ioaddr
, PHY_AUTO_NEGO_REG
,
985 PHY_Cap_100_Full
| (val
&
988 // enable 1000 Full Mode
989 // RTL8169_WRITE_GMII_REG( ioaddr, PHY_1000_CTRL_REG, PHY_Cap_1000_Full );
990 RTL8169_WRITE_GMII_REG(ioaddr
, PHY_1000_CTRL_REG
, PHY_Cap_1000_Full
| PHY_Cap_1000_Half
); //rtl8168
992 } // end of if( option > 0 )
994 // Enable auto-negotiation and restart auto-nigotiation
995 RTL8169_WRITE_GMII_REG(ioaddr
, PHY_CTRL_REG
,
996 PHY_Enable_Auto_Nego
|
997 PHY_Restart_Auto_Nego
);
1000 // wait for auto-negotiation process
1001 for (i
= 10000; i
> 0; i
--) {
1002 //check if auto-negotiation complete
1003 if (RTL8169_READ_GMII_REG(ioaddr
, PHY_STAT_REG
) &
1004 PHY_Auto_Neco_Comp
) {
1006 option
= RTL_R8(PHYstatus
);
1007 if (option
& _1000bpsF
) {
1009 ("1000Mbps Full-duplex operation.\n");
1012 ("%sMbps %s-duplex operation.\n",
1013 (option
& _100bps
) ? "100" :
1015 (option
& FullDup
) ? "Full" :
1021 } // end of if( RTL8169_READ_GMII_REG(ioaddr, 1) & 0x20 )
1022 } // end for-loop to wait for auto-negotiation process
1028 ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
1030 (RTL_R32(TBICSR
) & TBILinkOK
) ? "OK" : "Failed");
1035 /* point to NIC specific routines */
1036 nic
->nic_op
= &r8169_operations
;
1037 pci_fill_nic ( nic
, pci
);
1038 nic
->irqno
= pci
->irq
;
1039 nic
->ioaddr
= ioaddr
;
1044 //======================================================================================================
1046 static void rtl8169_hw_PHY_reset(struct nic *nic __unused)
1048 int val, phy_reset_expiretime = 50;
1049 struct rtl8169_private *priv = dev->priv;
1050 unsigned long ioaddr = priv->ioaddr;
1052 DBG_PRINTF("%s: Reset RTL8169s PHY\n", dev->name);
1054 val = ( RTL8169_READ_GMII_REG( ioaddr, 0 ) | 0x8000 ) & 0xffff;
1055 RTL8169_WRITE_GMII_REG( ioaddr, 0, val );
1057 do //waiting for phy reset
1059 if( RTL8169_READ_GMII_REG( ioaddr, 0 ) & 0x8000 ){
1060 phy_reset_expiretime --;
1066 }while( phy_reset_expiretime >= 0 );
1068 assert( phy_reset_expiretime > 0 );
1073 //======================================================================================================
1074 static void rtl8169_hw_PHY_config(struct nic
*nic __unused
)
1077 DBG_PRINTF("priv->mcfg=%d, priv->pcfg=%d\n", tpc
->mcfg
, tpc
->pcfg
);
1079 if (tpc
->mcfg
== MCFG_METHOD_4
) {
1081 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x1F, 0x0001 );
1082 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x1b, 0x841e );
1083 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x0e, 0x7bfb );
1084 RTL8169_WRITE_GMII_REG( (unsigned long)ioaddr, 0x09, 0x273a );
1087 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x1F,
1089 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x01,
1091 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x1F,
1093 } else if ((tpc
->mcfg
== MCFG_METHOD_2
)
1094 || (tpc
->mcfg
== MCFG_METHOD_3
)) {
1095 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x1F,
1097 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x15,
1099 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x18,
1101 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1103 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x03,
1105 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x02,
1107 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x01,
1109 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x00,
1111 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1113 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1115 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1117 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x03,
1119 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x02,
1121 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x01,
1123 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x00,
1125 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1127 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1129 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1131 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x03,
1133 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x02,
1135 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x01,
1137 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x00,
1139 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1141 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1143 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1145 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x03,
1147 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x02,
1149 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x01,
1151 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x00,
1153 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1155 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1157 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1159 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x03,
1161 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x02,
1163 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x01,
1165 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x00,
1167 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1169 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1171 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x04,
1173 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x1F,
1175 RTL8169_WRITE_GMII_REG((unsigned long) ioaddr
, 0x0B,
1178 DBG_PRINTF("tpc->mcfg=%d. Discard hw PHY config.\n",
1183 DRIVER ( "r8169/PCI", nic_driver
, pci_driver
, r8169_driver
,
1184 r8169_probe
, r8169_disable
);