blob: 83a05b45b9e5b19b47b1a4dd9acf33287a728fec [file] [log] [blame]
wdenka8bd82d2004-04-18 22:03:42 +00001/*
2 * rtl8169.c : U-Boot driver for the RealTek RTL8169
3 *
4 * Masami Komiya (mkomiya@sonare.it)
5 *
6 * Most part is taken from r8169.c of etherboot
7 *
8 */
9
10/**************************************************************************
11* r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
12* Written 2003 by Timothy Legge <tlegge@rogers.com>
13*
14* This program is free software; you can redistribute it and/or modify
15* it under the terms of the GNU General Public License as published by
16* the Free Software Foundation; either version 2 of the License, or
17* (at your option) any later version.
18*
19* This program is distributed in the hope that it will be useful,
20* but WITHOUT ANY WARRANTY; without even the implied warranty of
21* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22* GNU General Public License for more details.
23*
24* You should have received a copy of the GNU General Public License
25* along with this program; if not, write to the Free Software
26* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*
28* Portions of this code based on:
29* r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
30* for Linux kernel 2.4.x.
31*
32* Written 2002 ShuChen <shuchen@realtek.com.tw>
33* See Linux Driver for full information
34*
35* Linux Driver Version 1.27a, 10.02.2002
36*
37* Thanks to:
38* Jean Chen of RealTek Semiconductor Corp. for
39* providing the evaluation NIC used to develop
40* this driver. RealTek's support for Etherboot
41* is appreciated.
42*
43* REVISION HISTORY:
44* ================
45*
46* v1.0 11-26-2003 timlegge Initial port of Linux driver
47* v1.5 01-17-2004 timlegge Initial driver output cleanup
48*
49* Indent Options: indent -kr -i8
50***************************************************************************/
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +010051/*
52 * 26 August 2006 Mihai Georgian <u-boot@linuxnotincluded.org.uk>
53 * Modified to use le32_to_cpu and cpu_to_le32 properly
54 */
wdenka8bd82d2004-04-18 22:03:42 +000055#include <common.h>
56#include <malloc.h>
57#include <net.h>
Ben Warren02d69892008-08-31 09:49:42 -070058#include <netdev.h>
wdenka8bd82d2004-04-18 22:03:42 +000059#include <asm/io.h>
60#include <pci.h>
61
wdenka8bd82d2004-04-18 22:03:42 +000062#undef DEBUG_RTL8169
63#undef DEBUG_RTL8169_TX
64#undef DEBUG_RTL8169_RX
65
66#define drv_version "v1.5"
67#define drv_date "01-17-2004"
68
69static u32 ioaddr;
70
71/* Condensed operations for readability. */
wdenka8bd82d2004-04-18 22:03:42 +000072#define currticks() get_timer(0)
wdenka8bd82d2004-04-18 22:03:42 +000073
74/* media options */
75#define MAX_UNITS 8
76static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
77
78/* MAC address length*/
79#define MAC_ADDR_LEN 6
80
81/* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
82#define MAX_ETH_FRAME_SIZE 1536
83
84#define TX_FIFO_THRESH 256 /* In bytes */
85
86#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
87#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
88#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
89#define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
90#define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
91#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
92
93#define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
94#define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
95#define RX_BUF_SIZE 1536 /* Rx Buffer size */
96#define RX_BUF_LEN 8192
97
98#define RTL_MIN_IO_SIZE 0x80
99#define TX_TIMEOUT (6*HZ)
100
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100101/* write/read MMIO register. Notice: {read,write}[wl] do the necessary swapping */
wdenka8bd82d2004-04-18 22:03:42 +0000102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
107#define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
108
109#define ETH_FRAME_LEN MAX_ETH_FRAME_SIZE
110#define ETH_ALEN MAC_ADDR_LEN
111#define ETH_ZLEN 60
112
113enum RTL8169_registers {
114 MAC0 = 0, /* Ethernet hardware address. */
115 MAR0 = 8, /* Multicast filter. */
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900116 TxDescStartAddrLow = 0x20,
117 TxDescStartAddrHigh = 0x24,
118 TxHDescStartAddrLow = 0x28,
119 TxHDescStartAddrHigh = 0x2c,
wdenka8bd82d2004-04-18 22:03:42 +0000120 FLASH = 0x30,
121 ERSR = 0x36,
122 ChipCmd = 0x37,
123 TxPoll = 0x38,
124 IntrMask = 0x3C,
125 IntrStatus = 0x3E,
126 TxConfig = 0x40,
127 RxConfig = 0x44,
128 RxMissed = 0x4C,
129 Cfg9346 = 0x50,
130 Config0 = 0x51,
131 Config1 = 0x52,
132 Config2 = 0x53,
133 Config3 = 0x54,
134 Config4 = 0x55,
135 Config5 = 0x56,
136 MultiIntr = 0x5C,
137 PHYAR = 0x60,
138 TBICSR = 0x64,
139 TBI_ANAR = 0x68,
140 TBI_LPAR = 0x6A,
141 PHYstatus = 0x6C,
142 RxMaxSize = 0xDA,
143 CPlusCmd = 0xE0,
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900144 RxDescStartAddrLow = 0xE4,
145 RxDescStartAddrHigh = 0xE8,
wdenka8bd82d2004-04-18 22:03:42 +0000146 EarlyTxThres = 0xEC,
147 FuncEvent = 0xF0,
148 FuncEventMask = 0xF4,
149 FuncPresetState = 0xF8,
150 FuncForceEvent = 0xFC,
151};
152
153enum RTL8169_register_content {
154 /*InterruptStatusBits */
155 SYSErr = 0x8000,
156 PCSTimeout = 0x4000,
157 SWInt = 0x0100,
158 TxDescUnavail = 0x80,
159 RxFIFOOver = 0x40,
160 RxUnderrun = 0x20,
161 RxOverflow = 0x10,
162 TxErr = 0x08,
163 TxOK = 0x04,
164 RxErr = 0x02,
165 RxOK = 0x01,
166
167 /*RxStatusDesc */
168 RxRES = 0x00200000,
169 RxCRC = 0x00080000,
170 RxRUNT = 0x00100000,
171 RxRWT = 0x00400000,
172
173 /*ChipCmdBits */
174 CmdReset = 0x10,
175 CmdRxEnb = 0x08,
176 CmdTxEnb = 0x04,
177 RxBufEmpty = 0x01,
178
179 /*Cfg9346Bits */
180 Cfg9346_Lock = 0x00,
181 Cfg9346_Unlock = 0xC0,
182
183 /*rx_mode_bits */
184 AcceptErr = 0x20,
185 AcceptRunt = 0x10,
186 AcceptBroadcast = 0x08,
187 AcceptMulticast = 0x04,
188 AcceptMyPhys = 0x02,
189 AcceptAllPhys = 0x01,
190
191 /*RxConfigBits */
192 RxCfgFIFOShift = 13,
193 RxCfgDMAShift = 8,
194
195 /*TxConfigBits */
196 TxInterFrameGapShift = 24,
197 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
198
199 /*rtl8169_PHYstatus */
200 TBI_Enable = 0x80,
201 TxFlowCtrl = 0x40,
202 RxFlowCtrl = 0x20,
203 _1000bpsF = 0x10,
204 _100bps = 0x08,
205 _10bps = 0x04,
206 LinkStatus = 0x02,
207 FullDup = 0x01,
208
209 /*GIGABIT_PHY_registers */
210 PHY_CTRL_REG = 0,
211 PHY_STAT_REG = 1,
212 PHY_AUTO_NEGO_REG = 4,
213 PHY_1000_CTRL_REG = 9,
214
215 /*GIGABIT_PHY_REG_BIT */
216 PHY_Restart_Auto_Nego = 0x0200,
217 PHY_Enable_Auto_Nego = 0x1000,
218
219 /* PHY_STAT_REG = 1; */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100220 PHY_Auto_Nego_Comp = 0x0020,
wdenka8bd82d2004-04-18 22:03:42 +0000221
222 /* PHY_AUTO_NEGO_REG = 4; */
223 PHY_Cap_10_Half = 0x0020,
224 PHY_Cap_10_Full = 0x0040,
225 PHY_Cap_100_Half = 0x0080,
226 PHY_Cap_100_Full = 0x0100,
227
228 /* PHY_1000_CTRL_REG = 9; */
229 PHY_Cap_1000_Full = 0x0200,
230
231 PHY_Cap_Null = 0x0,
232
233 /*_MediaType*/
234 _10_Half = 0x01,
235 _10_Full = 0x02,
236 _100_Half = 0x04,
237 _100_Full = 0x08,
238 _1000_Full = 0x10,
239
240 /*_TBICSRBit*/
241 TBILinkOK = 0x02000000,
242};
243
244static struct {
245 const char *name;
246 u8 version; /* depend on RTL8169 docs */
247 u32 RxConfigMask; /* should clear the bits supported by this chip */
248} rtl_chip_info[] = {
249 {"RTL-8169", 0x00, 0xff7e1880,},
250 {"RTL-8169", 0x04, 0xff7e1880,},
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900251 {"RTL-8169", 0x00, 0xff7e1880,},
252 {"RTL-8169s/8110s", 0x02, 0xff7e1880,},
253 {"RTL-8169s/8110s", 0x04, 0xff7e1880,},
254 {"RTL-8169sb/8110sb", 0x10, 0xff7e1880,},
255 {"RTL-8169sc/8110sc", 0x18, 0xff7e1880,},
256 {"RTL-8168b/8111sb", 0x30, 0xff7e1880,},
257 {"RTL-8168b/8111sb", 0x38, 0xff7e1880,},
258 {"RTL-8101e", 0x34, 0xff7e1880,},
259 {"RTL-8100e", 0x32, 0xff7e1880,},
wdenka8bd82d2004-04-18 22:03:42 +0000260};
261
262enum _DescStatusBit {
263 OWNbit = 0x80000000,
264 EORbit = 0x40000000,
265 FSbit = 0x20000000,
266 LSbit = 0x10000000,
267};
268
269struct TxDesc {
270 u32 status;
271 u32 vlan_tag;
272 u32 buf_addr;
273 u32 buf_Haddr;
274};
275
276struct RxDesc {
277 u32 status;
278 u32 vlan_tag;
279 u32 buf_addr;
280 u32 buf_Haddr;
281};
282
283/* Define the TX Descriptor */
284static u8 tx_ring[NUM_TX_DESC * sizeof(struct TxDesc) + 256];
285/* __attribute__ ((aligned(256))); */
286
287/* Create a static buffer of size RX_BUF_SZ for each
288TX Descriptor. All descriptors point to a
289part of this buffer */
290static unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];
291
292/* Define the RX Descriptor */
293static u8 rx_ring[NUM_RX_DESC * sizeof(struct TxDesc) + 256];
294 /* __attribute__ ((aligned(256))); */
295
296/* Create a static buffer of size RX_BUF_SZ for each
297RX Descriptor All descriptors point to a
298part of this buffer */
299static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
300
301struct rtl8169_private {
302 void *mmio_addr; /* memory map physical address */
303 int chipset;
304 unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
305 unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
306 unsigned long dirty_tx;
307 unsigned char *TxDescArrays; /* Index of Tx Descriptor buffer */
308 unsigned char *RxDescArrays; /* Index of Rx Descriptor buffer */
309 struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
310 struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
311 unsigned char *RxBufferRings; /* Index of Rx Buffer */
312 unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
313 unsigned char *Tx_skbuff[NUM_TX_DESC];
314} tpx;
315
316static struct rtl8169_private *tpc;
317
318static const u16 rtl8169_intr_mask =
319 SYSErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver | TxErr |
320 TxOK | RxErr | RxOK;
321static const unsigned int rtl8169_rx_config =
322 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
323
324static struct pci_device_id supported[] = {
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900325 {PCI_VENDOR_ID_REALTEK, 0x8167},
wdenka8bd82d2004-04-18 22:03:42 +0000326 {PCI_VENDOR_ID_REALTEK, 0x8169},
327 {}
328};
329
330void mdio_write(int RegAddr, int value)
331{
332 int i;
333
334 RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
335 udelay(1000);
336
337 for (i = 2000; i > 0; i--) {
338 /* Check if the RTL8169 has completed writing to the specified MII register */
339 if (!(RTL_R32(PHYAR) & 0x80000000)) {
340 break;
341 } else {
342 udelay(100);
343 }
344 }
345}
346
347int mdio_read(int RegAddr)
348{
349 int i, value = -1;
350
351 RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
352 udelay(1000);
353
354 for (i = 2000; i > 0; i--) {
355 /* Check if the RTL8169 has completed retrieving data from the specified MII register */
356 if (RTL_R32(PHYAR) & 0x80000000) {
357 value = (int) (RTL_R32(PHYAR) & 0xFFFF);
358 break;
359 } else {
360 udelay(100);
361 }
362 }
363 return value;
364}
365
wdenka8bd82d2004-04-18 22:03:42 +0000366static int rtl8169_init_board(struct eth_device *dev)
367{
368 int i;
369 u32 tmp;
370
371#ifdef DEBUG_RTL8169
372 printf ("%s\n", __FUNCTION__);
373#endif
374 ioaddr = dev->iobase;
375
376 /* Soft reset the chip. */
377 RTL_W8(ChipCmd, CmdReset);
378
379 /* Check that the chip has finished the reset. */
380 for (i = 1000; i > 0; i--)
381 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
382 break;
383 else
384 udelay(10);
385
386 /* identify chip attached to board */
387 tmp = RTL_R32(TxConfig);
388 tmp = ((tmp & 0x7c000000) + ((tmp & 0x00800000) << 2)) >> 24;
389
390 for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--){
391 if (tmp == rtl_chip_info[i].version) {
392 tpc->chipset = i;
393 goto match;
394 }
395 }
396
397 /* if unknown chip, assume array element #0, original RTL-8169 in this case */
398 printf("PCI device %s: unknown chip version, assuming RTL-8169\n", dev->name);
Wolfgang Denk06c53be2008-07-10 13:16:09 +0200399 printf("PCI device: TxConfig = 0x%lX\n", (unsigned long) RTL_R32(TxConfig));
wdenka8bd82d2004-04-18 22:03:42 +0000400 tpc->chipset = 0;
401
402match:
403 return 0;
404}
405
406/**************************************************************************
407RECV - Receive a frame
408***************************************************************************/
409static int rtl_recv(struct eth_device *dev)
410{
411 /* return true if there's an ethernet packet ready to read */
412 /* nic->packet should contain data on return */
413 /* nic->packetlen should contain length of data */
414 int cur_rx;
415 int length = 0;
416
417#ifdef DEBUG_RTL8169_RX
418 printf ("%s\n", __FUNCTION__);
419#endif
420 ioaddr = dev->iobase;
421
422 cur_rx = tpc->cur_rx;
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900423 flush_cache((unsigned long)&tpc->RxDescArray[cur_rx],
424 sizeof(struct RxDesc));
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100425 if ((le32_to_cpu(tpc->RxDescArray[cur_rx].status) & OWNbit) == 0) {
426 if (!(le32_to_cpu(tpc->RxDescArray[cur_rx].status) & RxRES)) {
wdenka8bd82d2004-04-18 22:03:42 +0000427 unsigned char rxdata[RX_BUF_LEN];
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100428 length = (int) (le32_to_cpu(tpc->RxDescArray[cur_rx].
429 status) & 0x00001FFF) - 4;
wdenka8bd82d2004-04-18 22:03:42 +0000430
431 memcpy(rxdata, tpc->RxBufferRing[cur_rx], length);
432 NetReceive(rxdata, length);
433
434 if (cur_rx == NUM_RX_DESC - 1)
435 tpc->RxDescArray[cur_rx].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100436 cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000437 else
438 tpc->RxDescArray[cur_rx].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100439 cpu_to_le32(OWNbit + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000440 tpc->RxDescArray[cur_rx].buf_addr =
Guennadi Liakhovetskidd5748b2008-04-28 14:37:14 +0200441 cpu_to_le32((unsigned long)tpc->RxBufferRing[cur_rx]);
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900442 flush_cache((unsigned long)tpc->RxBufferRing[cur_rx],
443 RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000444 } else {
445 puts("Error Rx");
446 }
447 cur_rx = (cur_rx + 1) % NUM_RX_DESC;
448 tpc->cur_rx = cur_rx;
449 return 1;
450
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900451 } else {
452 ushort sts = RTL_R8(IntrStatus);
453 RTL_W8(IntrStatus, sts & ~(TxErr | RxErr | SYSErr));
454 udelay(100); /* wait */
wdenka8bd82d2004-04-18 22:03:42 +0000455 }
456 tpc->cur_rx = cur_rx;
457 return (0); /* initially as this is called to flush the input */
458}
459
460#define HZ 1000
461/**************************************************************************
462SEND - Transmit a frame
463***************************************************************************/
464static int rtl_send(struct eth_device *dev, volatile void *packet, int length)
465{
466 /* send the packet to destination */
467
468 u32 to;
469 u8 *ptxb;
470 int entry = tpc->cur_tx % NUM_TX_DESC;
471 u32 len = length;
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100472 int ret;
wdenka8bd82d2004-04-18 22:03:42 +0000473
474#ifdef DEBUG_RTL8169_TX
475 int stime = currticks();
476 printf ("%s\n", __FUNCTION__);
477 printf("sending %d bytes\n", len);
478#endif
479
480 ioaddr = dev->iobase;
481
482 /* point to the current txb incase multiple tx_rings are used */
483 ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
484 memcpy(ptxb, (char *)packet, (int)length);
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900485 flush_cache((unsigned long)ptxb, length);
wdenka8bd82d2004-04-18 22:03:42 +0000486
487 while (len < ETH_ZLEN)
488 ptxb[len++] = '\0';
489
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900490 tpc->TxDescArray[entry].buf_Haddr = 0;
Guennadi Liakhovetskidd5748b2008-04-28 14:37:14 +0200491 tpc->TxDescArray[entry].buf_addr = cpu_to_le32((unsigned long)ptxb);
wdenka8bd82d2004-04-18 22:03:42 +0000492 if (entry != (NUM_TX_DESC - 1)) {
493 tpc->TxDescArray[entry].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100494 cpu_to_le32((OWNbit | FSbit | LSbit) |
495 ((len > ETH_ZLEN) ? len : ETH_ZLEN));
wdenka8bd82d2004-04-18 22:03:42 +0000496 } else {
497 tpc->TxDescArray[entry].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100498 cpu_to_le32((OWNbit | EORbit | FSbit | LSbit) |
499 ((len > ETH_ZLEN) ? len : ETH_ZLEN));
wdenka8bd82d2004-04-18 22:03:42 +0000500 }
501 RTL_W8(TxPoll, 0x40); /* set polling bit */
502
503 tpc->cur_tx++;
504 to = currticks() + TX_TIMEOUT;
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900505 do {
506 flush_cache((unsigned long)&tpc->TxDescArray[entry],
507 sizeof(struct TxDesc));
508 } while ((le32_to_cpu(tpc->TxDescArray[entry].status) & OWNbit)
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100509 && (currticks() < to)); /* wait */
wdenka8bd82d2004-04-18 22:03:42 +0000510
511 if (currticks() >= to) {
512#ifdef DEBUG_RTL8169_TX
513 puts ("tx timeout/error\n");
514 printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
515#endif
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100516 ret = 0;
wdenka8bd82d2004-04-18 22:03:42 +0000517 } else {
518#ifdef DEBUG_RTL8169_TX
519 puts("tx done\n");
520#endif
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100521 ret = length;
wdenka8bd82d2004-04-18 22:03:42 +0000522 }
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100523 /* Delay to make net console (nc) work properly */
524 udelay(20);
525 return ret;
wdenka8bd82d2004-04-18 22:03:42 +0000526}
527
528static void rtl8169_set_rx_mode(struct eth_device *dev)
529{
530 u32 mc_filter[2]; /* Multicast hash filter */
531 int rx_mode;
532 u32 tmp = 0;
533
534#ifdef DEBUG_RTL8169
535 printf ("%s\n", __FUNCTION__);
536#endif
537
538 /* IFF_ALLMULTI */
539 /* Too many to filter perfectly -- accept all multicasts. */
540 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
541 mc_filter[1] = mc_filter[0] = 0xffffffff;
542
543 tmp = rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
544 rtl_chip_info[tpc->chipset].RxConfigMask);
545
546 RTL_W32(RxConfig, tmp);
547 RTL_W32(MAR0 + 0, mc_filter[0]);
548 RTL_W32(MAR0 + 4, mc_filter[1]);
549}
550
551static void rtl8169_hw_start(struct eth_device *dev)
552{
553 u32 i;
554
555#ifdef DEBUG_RTL8169
556 int stime = currticks();
557 printf ("%s\n", __FUNCTION__);
558#endif
559
560#if 0
561 /* Soft reset the chip. */
562 RTL_W8(ChipCmd, CmdReset);
563
564 /* Check that the chip has finished the reset. */
565 for (i = 1000; i > 0; i--) {
566 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
567 break;
568 else
569 udelay(10);
570 }
571#endif
572
573 RTL_W8(Cfg9346, Cfg9346_Unlock);
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900574
575 /* RTL-8169sb/8110sb or previous version */
576 if (tpc->chipset <= 5)
577 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
578
wdenka8bd82d2004-04-18 22:03:42 +0000579 RTL_W8(EarlyTxThres, EarlyTxThld);
580
581 /* For gigabit rtl8169 */
582 RTL_W16(RxMaxSize, RxPacketMaxSize);
583
584 /* Set Rx Config register */
585 i = rtl8169_rx_config | (RTL_R32(RxConfig) &
586 rtl_chip_info[tpc->chipset].RxConfigMask);
587 RTL_W32(RxConfig, i);
588
589 /* Set DMA burst size and Interframe Gap Time */
590 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
591 (InterFrameGap << TxInterFrameGapShift));
592
593
594 tpc->cur_rx = 0;
595
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900596 RTL_W32(TxDescStartAddrLow, (unsigned long)tpc->TxDescArray);
597 RTL_W32(TxDescStartAddrHigh, (unsigned long)0);
598 RTL_W32(RxDescStartAddrLow, (unsigned long)tpc->RxDescArray);
599 RTL_W32(RxDescStartAddrHigh, (unsigned long)0);
600
601 /* RTL-8169sc/8110sc or later version */
602 if (tpc->chipset > 5)
603 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
604
wdenka8bd82d2004-04-18 22:03:42 +0000605 RTL_W8(Cfg9346, Cfg9346_Lock);
606 udelay(10);
607
608 RTL_W32(RxMissed, 0);
609
610 rtl8169_set_rx_mode(dev);
611
612 /* no early-rx interrupts */
613 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
614
615#ifdef DEBUG_RTL8169
616 printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
617#endif
618}
619
620static void rtl8169_init_ring(struct eth_device *dev)
621{
622 int i;
623
624#ifdef DEBUG_RTL8169
625 int stime = currticks();
626 printf ("%s\n", __FUNCTION__);
627#endif
628
629 tpc->cur_rx = 0;
630 tpc->cur_tx = 0;
631 tpc->dirty_tx = 0;
632 memset(tpc->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
633 memset(tpc->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
634
635 for (i = 0; i < NUM_TX_DESC; i++) {
636 tpc->Tx_skbuff[i] = &txb[i];
637 }
638
639 for (i = 0; i < NUM_RX_DESC; i++) {
640 if (i == (NUM_RX_DESC - 1))
641 tpc->RxDescArray[i].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100642 cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000643 else
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100644 tpc->RxDescArray[i].status =
645 cpu_to_le32(OWNbit + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000646
647 tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
648 tpc->RxDescArray[i].buf_addr =
Guennadi Liakhovetskidd5748b2008-04-28 14:37:14 +0200649 cpu_to_le32((unsigned long)tpc->RxBufferRing[i]);
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900650 flush_cache((unsigned long)tpc->RxBufferRing[i], RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000651 }
652
653#ifdef DEBUG_RTL8169
654 printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
655#endif
656}
657
658/**************************************************************************
659RESET - Finish setting up the ethernet interface
660***************************************************************************/
Ben Warren422b1a02008-01-09 18:15:53 -0500661static int rtl_reset(struct eth_device *dev, bd_t *bis)
wdenka8bd82d2004-04-18 22:03:42 +0000662{
663 int i;
wdenka8bd82d2004-04-18 22:03:42 +0000664
665#ifdef DEBUG_RTL8169
666 int stime = currticks();
667 printf ("%s\n", __FUNCTION__);
668#endif
669
670 tpc->TxDescArrays = tx_ring;
wdenka8bd82d2004-04-18 22:03:42 +0000671 /* Tx Desscriptor needs 256 bytes alignment; */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100672 tpc->TxDescArray = (struct TxDesc *) ((unsigned long)(tpc->TxDescArrays +
673 255) & ~255);
wdenka8bd82d2004-04-18 22:03:42 +0000674
675 tpc->RxDescArrays = rx_ring;
676 /* Rx Desscriptor needs 256 bytes alignment; */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100677 tpc->RxDescArray = (struct RxDesc *) ((unsigned long)(tpc->RxDescArrays +
678 255) & ~255);
wdenka8bd82d2004-04-18 22:03:42 +0000679
680 rtl8169_init_ring(dev);
681 rtl8169_hw_start(dev);
682 /* Construct a perfect filter frame with the mac address as first match
683 * and broadcast for all others */
684 for (i = 0; i < 192; i++)
685 txb[i] = 0xFF;
686
687 txb[0] = dev->enetaddr[0];
688 txb[1] = dev->enetaddr[1];
689 txb[2] = dev->enetaddr[2];
690 txb[3] = dev->enetaddr[3];
691 txb[4] = dev->enetaddr[4];
692 txb[5] = dev->enetaddr[5];
693
694#ifdef DEBUG_RTL8169
695 printf ("%s elapsed time : %d\n", __FUNCTION__, currticks()-stime);
696#endif
Ben Warren422b1a02008-01-09 18:15:53 -0500697 return 0;
wdenka8bd82d2004-04-18 22:03:42 +0000698}
699
700/**************************************************************************
701HALT - Turn off ethernet interface
702***************************************************************************/
703static void rtl_halt(struct eth_device *dev)
704{
705 int i;
706
707#ifdef DEBUG_RTL8169
708 printf ("%s\n", __FUNCTION__);
709#endif
710
711 ioaddr = dev->iobase;
712
713 /* Stop the chip's Tx and Rx DMA processes. */
714 RTL_W8(ChipCmd, 0x00);
715
716 /* Disable interrupts by clearing the interrupt mask. */
717 RTL_W16(IntrMask, 0x0000);
718
719 RTL_W32(RxMissed, 0);
720
721 tpc->TxDescArrays = NULL;
722 tpc->RxDescArrays = NULL;
723 tpc->TxDescArray = NULL;
724 tpc->RxDescArray = NULL;
725 for (i = 0; i < NUM_RX_DESC; i++) {
726 tpc->RxBufferRing[i] = NULL;
727 }
728}
729
730/**************************************************************************
731INIT - Look for an adapter, this routine's visible to the outside
732***************************************************************************/
733
734#define board_found 1
735#define valid_link 0
736static int rtl_init(struct eth_device *dev, bd_t *bis)
737{
738 static int board_idx = -1;
739 static int printed_version = 0;
740 int i, rc;
741 int option = -1, Cap10_100 = 0, Cap1000 = 0;
742
743#ifdef DEBUG_RTL8169
744 printf ("%s\n", __FUNCTION__);
745#endif
746
747 ioaddr = dev->iobase;
748
749 board_idx++;
750
751 printed_version = 1;
752
753 /* point to private storage */
754 tpc = &tpx;
755
756 rc = rtl8169_init_board(dev);
757 if (rc)
758 return rc;
759
760 /* Get MAC address. FIXME: read EEPROM */
761 for (i = 0; i < MAC_ADDR_LEN; i++)
Mike Frysingerd3f87142009-02-11 19:01:26 -0500762 dev->enetaddr[i] = RTL_R8(MAC0 + i);
wdenka8bd82d2004-04-18 22:03:42 +0000763
764#ifdef DEBUG_RTL8169
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900765 printf("chipset = %d\n", tpc->chipset);
wdenka8bd82d2004-04-18 22:03:42 +0000766 printf("MAC Address");
767 for (i = 0; i < MAC_ADDR_LEN; i++)
768 printf(":%02x", dev->enetaddr[i]);
769 putc('\n');
770#endif
771
772#ifdef DEBUG_RTL8169
773 /* Print out some hardware info */
774 printf("%s: at ioaddr 0x%x\n", dev->name, ioaddr);
775#endif
776
777 /* if TBI is not endbled */
778 if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
779 int val = mdio_read(PHY_AUTO_NEGO_REG);
780
781 option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
782 /* Force RTL8169 in 10/100/1000 Full/Half mode. */
783 if (option > 0) {
784#ifdef DEBUG_RTL8169
785 printf("%s: Force-mode Enabled.\n", dev->name);
786#endif
787 Cap10_100 = 0, Cap1000 = 0;
788 switch (option) {
789 case _10_Half:
790 Cap10_100 = PHY_Cap_10_Half;
791 Cap1000 = PHY_Cap_Null;
792 break;
793 case _10_Full:
794 Cap10_100 = PHY_Cap_10_Full;
795 Cap1000 = PHY_Cap_Null;
796 break;
797 case _100_Half:
798 Cap10_100 = PHY_Cap_100_Half;
799 Cap1000 = PHY_Cap_Null;
800 break;
801 case _100_Full:
802 Cap10_100 = PHY_Cap_100_Full;
803 Cap1000 = PHY_Cap_Null;
804 break;
805 case _1000_Full:
806 Cap10_100 = PHY_Cap_Null;
807 Cap1000 = PHY_Cap_1000_Full;
808 break;
809 default:
810 break;
811 }
812 mdio_write(PHY_AUTO_NEGO_REG, Cap10_100 | (val & 0x1F)); /* leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
813 mdio_write(PHY_1000_CTRL_REG, Cap1000);
814 } else {
815#ifdef DEBUG_RTL8169
816 printf("%s: Auto-negotiation Enabled.\n",
817 dev->name);
818#endif
819 /* enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
820 mdio_write(PHY_AUTO_NEGO_REG,
821 PHY_Cap_10_Half | PHY_Cap_10_Full |
822 PHY_Cap_100_Half | PHY_Cap_100_Full |
823 (val & 0x1F));
824
825 /* enable 1000 Full Mode */
826 mdio_write(PHY_1000_CTRL_REG, PHY_Cap_1000_Full);
827
828 }
829
830 /* Enable auto-negotiation and restart auto-nigotiation */
831 mdio_write(PHY_CTRL_REG,
832 PHY_Enable_Auto_Nego | PHY_Restart_Auto_Nego);
833 udelay(100);
834
835 /* wait for auto-negotiation process */
836 for (i = 10000; i > 0; i--) {
837 /* check if auto-negotiation complete */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100838 if (mdio_read(PHY_STAT_REG) & PHY_Auto_Nego_Comp) {
wdenka8bd82d2004-04-18 22:03:42 +0000839 udelay(100);
840 option = RTL_R8(PHYstatus);
841 if (option & _1000bpsF) {
842#ifdef DEBUG_RTL8169
843 printf("%s: 1000Mbps Full-duplex operation.\n",
844 dev->name);
845#endif
846 } else {
847#ifdef DEBUG_RTL8169
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100848 printf("%s: %sMbps %s-duplex operation.\n",
849 dev->name,
850 (option & _100bps) ? "100" :
851 "10",
852 (option & FullDup) ? "Full" :
853 "Half");
wdenka8bd82d2004-04-18 22:03:42 +0000854#endif
855 }
856 break;
857 } else {
858 udelay(100);
859 }
860 } /* end for-loop to wait for auto-negotiation process */
861
862 } else {
863 udelay(100);
864#ifdef DEBUG_RTL8169
865 printf
866 ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
867 dev->name,
868 (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
869#endif
870 }
871
872 return 1;
873}
874
875int rtl8169_initialize(bd_t *bis)
876{
877 pci_dev_t devno;
878 int card_number = 0;
879 struct eth_device *dev;
880 u32 iobase;
881 int idx=0;
882
883 while(1){
884 /* Find RTL8169 */
885 if ((devno = pci_find_devices(supported, idx++)) < 0)
886 break;
887
888 pci_read_config_dword(devno, PCI_BASE_ADDRESS_1, &iobase);
889 iobase &= ~0xf;
890
891 debug ("rtl8169: REALTEK RTL8169 @0x%x\n", iobase);
892
893 dev = (struct eth_device *)malloc(sizeof *dev);
894
895 sprintf (dev->name, "RTL8169#%d", card_number);
896
897 dev->priv = (void *) devno;
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100898 dev->iobase = (int)pci_mem_to_phys(devno, iobase);
wdenka8bd82d2004-04-18 22:03:42 +0000899
900 dev->init = rtl_reset;
901 dev->halt = rtl_halt;
902 dev->send = rtl_send;
903 dev->recv = rtl_recv;
904
905 eth_register (dev);
906
907 rtl_init(dev, bis);
908
909 card_number++;
910 }
911 return card_number;
912}