blob: da2cfb7f550fbb31b71838b4783a1ef3308e6760 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenka8bd82d2004-04-18 22:03:42 +00002/*
3 * rtl8169.c : U-Boot driver for the RealTek RTL8169
4 *
5 * Masami Komiya (mkomiya@sonare.it)
6 *
7 * Most part is taken from r8169.c of etherboot
8 *
9 */
10
11/**************************************************************************
12* r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit
13* Written 2003 by Timothy Legge <tlegge@rogers.com>
14*
wdenka8bd82d2004-04-18 22:03:42 +000015* Portions of this code based on:
16* r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver
17* for Linux kernel 2.4.x.
18*
19* Written 2002 ShuChen <shuchen@realtek.com.tw>
20* See Linux Driver for full information
21*
22* Linux Driver Version 1.27a, 10.02.2002
23*
24* Thanks to:
25* Jean Chen of RealTek Semiconductor Corp. for
26* providing the evaluation NIC used to develop
27* this driver. RealTek's support for Etherboot
28* is appreciated.
29*
30* REVISION HISTORY:
31* ================
32*
33* v1.0 11-26-2003 timlegge Initial port of Linux driver
34* v1.5 01-17-2004 timlegge Initial driver output cleanup
35*
36* Indent Options: indent -kr -i8
37***************************************************************************/
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +010038/*
39 * 26 August 2006 Mihai Georgian <u-boot@linuxnotincluded.org.uk>
40 * Modified to use le32_to_cpu and cpu_to_le32 properly
41 */
wdenka8bd82d2004-04-18 22:03:42 +000042#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070043#include <cpu_func.h>
Simon Glassd0a5a0b2015-07-06 16:47:45 -060044#include <dm.h>
Thierry Redingd58acdc2014-12-09 22:25:26 -070045#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060046#include <log.h>
wdenka8bd82d2004-04-18 22:03:42 +000047#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060048#include <memalign.h>
wdenka8bd82d2004-04-18 22:03:42 +000049#include <net.h>
Simon Glassd0a5a0b2015-07-06 16:47:45 -060050#ifndef CONFIG_DM_ETH
Ben Warren02d69892008-08-31 09:49:42 -070051#include <netdev.h>
Simon Glassd0a5a0b2015-07-06 16:47:45 -060052#endif
Simon Glass90526e92020-05-10 11:39:56 -060053#include <asm/cache.h>
wdenka8bd82d2004-04-18 22:03:42 +000054#include <asm/io.h>
55#include <pci.h>
Simon Glassc05ed002020-05-10 11:40:11 -060056#include <linux/delay.h>
wdenka8bd82d2004-04-18 22:03:42 +000057
wdenka8bd82d2004-04-18 22:03:42 +000058#undef DEBUG_RTL8169
59#undef DEBUG_RTL8169_TX
60#undef DEBUG_RTL8169_RX
61
62#define drv_version "v1.5"
63#define drv_date "01-17-2004"
64
Thierry Reding744152f2015-03-20 12:41:21 +010065static unsigned long ioaddr;
wdenka8bd82d2004-04-18 22:03:42 +000066
67/* Condensed operations for readability. */
wdenka8bd82d2004-04-18 22:03:42 +000068#define currticks() get_timer(0)
wdenka8bd82d2004-04-18 22:03:42 +000069
70/* media options */
71#define MAX_UNITS 8
72static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
73
74/* MAC address length*/
75#define MAC_ADDR_LEN 6
76
77/* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
78#define MAX_ETH_FRAME_SIZE 1536
79
80#define TX_FIFO_THRESH 256 /* In bytes */
81
82#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
83#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
84#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
85#define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
86#define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
87#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
88
89#define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
Thierry Redingc94bbfd2014-12-09 22:25:24 -070090#ifdef CONFIG_SYS_RX_ETH_BUFFER
91 #define NUM_RX_DESC CONFIG_SYS_RX_ETH_BUFFER
92#else
93 #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
94#endif
wdenka8bd82d2004-04-18 22:03:42 +000095#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 */
Thierry Reding744152f2015-03-20 12:41:21 +0100102#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) readl(ioaddr + (reg))
wdenka8bd82d2004-04-18 22:03:42 +0000108
Thierry Reding744152f2015-03-20 12:41:21 +0100109#define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)(unsigned long)dev->priv, \
110 (pci_addr_t)(unsigned long)a)
111#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)(unsigned long)dev->priv, \
112 (phys_addr_t)a)
Yoshihiro Shimodad65e34d2009-02-25 14:27:29 +0900113
wdenka8bd82d2004-04-18 22:03:42 +0000114enum RTL8169_registers {
115 MAC0 = 0, /* Ethernet hardware address. */
116 MAR0 = 8, /* Multicast filter. */
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900117 TxDescStartAddrLow = 0x20,
118 TxDescStartAddrHigh = 0x24,
119 TxHDescStartAddrLow = 0x28,
120 TxHDescStartAddrHigh = 0x2c,
wdenka8bd82d2004-04-18 22:03:42 +0000121 FLASH = 0x30,
122 ERSR = 0x36,
123 ChipCmd = 0x37,
124 TxPoll = 0x38,
125 IntrMask = 0x3C,
126 IntrStatus = 0x3E,
127 TxConfig = 0x40,
128 RxConfig = 0x44,
129 RxMissed = 0x4C,
130 Cfg9346 = 0x50,
131 Config0 = 0x51,
132 Config1 = 0x52,
133 Config2 = 0x53,
134 Config3 = 0x54,
135 Config4 = 0x55,
136 Config5 = 0x56,
137 MultiIntr = 0x5C,
138 PHYAR = 0x60,
139 TBICSR = 0x64,
140 TBI_ANAR = 0x68,
141 TBI_LPAR = 0x6A,
142 PHYstatus = 0x6C,
143 RxMaxSize = 0xDA,
144 CPlusCmd = 0xE0,
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900145 RxDescStartAddrLow = 0xE4,
146 RxDescStartAddrHigh = 0xE8,
wdenka8bd82d2004-04-18 22:03:42 +0000147 EarlyTxThres = 0xEC,
148 FuncEvent = 0xF0,
149 FuncEventMask = 0xF4,
150 FuncPresetState = 0xF8,
151 FuncForceEvent = 0xFC,
152};
153
154enum RTL8169_register_content {
155 /*InterruptStatusBits */
156 SYSErr = 0x8000,
157 PCSTimeout = 0x4000,
158 SWInt = 0x0100,
159 TxDescUnavail = 0x80,
160 RxFIFOOver = 0x40,
161 RxUnderrun = 0x20,
162 RxOverflow = 0x10,
163 TxErr = 0x08,
164 TxOK = 0x04,
165 RxErr = 0x02,
166 RxOK = 0x01,
167
168 /*RxStatusDesc */
169 RxRES = 0x00200000,
170 RxCRC = 0x00080000,
171 RxRUNT = 0x00100000,
172 RxRWT = 0x00400000,
173
174 /*ChipCmdBits */
175 CmdReset = 0x10,
176 CmdRxEnb = 0x08,
177 CmdTxEnb = 0x04,
178 RxBufEmpty = 0x01,
179
180 /*Cfg9346Bits */
181 Cfg9346_Lock = 0x00,
182 Cfg9346_Unlock = 0xC0,
183
184 /*rx_mode_bits */
185 AcceptErr = 0x20,
186 AcceptRunt = 0x10,
187 AcceptBroadcast = 0x08,
188 AcceptMulticast = 0x04,
189 AcceptMyPhys = 0x02,
190 AcceptAllPhys = 0x01,
191
192 /*RxConfigBits */
193 RxCfgFIFOShift = 13,
194 RxCfgDMAShift = 8,
195
196 /*TxConfigBits */
197 TxInterFrameGapShift = 24,
198 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
199
200 /*rtl8169_PHYstatus */
201 TBI_Enable = 0x80,
202 TxFlowCtrl = 0x40,
203 RxFlowCtrl = 0x20,
204 _1000bpsF = 0x10,
205 _100bps = 0x08,
206 _10bps = 0x04,
207 LinkStatus = 0x02,
208 FullDup = 0x01,
209
210 /*GIGABIT_PHY_registers */
211 PHY_CTRL_REG = 0,
212 PHY_STAT_REG = 1,
213 PHY_AUTO_NEGO_REG = 4,
214 PHY_1000_CTRL_REG = 9,
215
216 /*GIGABIT_PHY_REG_BIT */
217 PHY_Restart_Auto_Nego = 0x0200,
218 PHY_Enable_Auto_Nego = 0x1000,
219
220 /* PHY_STAT_REG = 1; */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100221 PHY_Auto_Nego_Comp = 0x0020,
wdenka8bd82d2004-04-18 22:03:42 +0000222
223 /* PHY_AUTO_NEGO_REG = 4; */
224 PHY_Cap_10_Half = 0x0020,
225 PHY_Cap_10_Full = 0x0040,
226 PHY_Cap_100_Half = 0x0080,
227 PHY_Cap_100_Full = 0x0100,
228
229 /* PHY_1000_CTRL_REG = 9; */
230 PHY_Cap_1000_Full = 0x0200,
231
232 PHY_Cap_Null = 0x0,
233
234 /*_MediaType*/
235 _10_Half = 0x01,
236 _10_Full = 0x02,
237 _100_Half = 0x04,
238 _100_Full = 0x08,
239 _1000_Full = 0x10,
240
241 /*_TBICSRBit*/
242 TBILinkOK = 0x02000000,
Tom Warrena7a435e2020-03-26 15:59:13 -0700243
244 /* FuncEvent/Misc */
245 RxDv_Gated_En = 0x80000,
wdenka8bd82d2004-04-18 22:03:42 +0000246};
247
248static struct {
249 const char *name;
250 u8 version; /* depend on RTL8169 docs */
251 u32 RxConfigMask; /* should clear the bits supported by this chip */
252} rtl_chip_info[] = {
253 {"RTL-8169", 0x00, 0xff7e1880,},
254 {"RTL-8169", 0x04, 0xff7e1880,},
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900255 {"RTL-8169", 0x00, 0xff7e1880,},
256 {"RTL-8169s/8110s", 0x02, 0xff7e1880,},
257 {"RTL-8169s/8110s", 0x04, 0xff7e1880,},
258 {"RTL-8169sb/8110sb", 0x10, 0xff7e1880,},
259 {"RTL-8169sc/8110sc", 0x18, 0xff7e1880,},
260 {"RTL-8168b/8111sb", 0x30, 0xff7e1880,},
261 {"RTL-8168b/8111sb", 0x38, 0xff7e1880,},
Thierry Reding945dd962019-09-11 19:19:06 +0200262 {"RTL-8168c/8111c", 0x3c, 0xff7e1880,},
Thierry Reding22872862013-09-20 16:03:43 +0200263 {"RTL-8168d/8111d", 0x28, 0xff7e1880,},
Thierry Reding65a66912013-09-20 16:03:44 +0200264 {"RTL-8168evl/8111evl", 0x2e, 0xff7e1880,},
Thierry Redingcc0856c2014-12-09 22:25:27 -0700265 {"RTL-8168/8111g", 0x4c, 0xff7e1880,},
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900266 {"RTL-8101e", 0x34, 0xff7e1880,},
267 {"RTL-8100e", 0x32, 0xff7e1880,},
Thierry Redingcdd69ac2019-04-16 18:20:30 +0200268 {"RTL-8168h/8111h", 0x54, 0xff7e1880,},
wdenka8bd82d2004-04-18 22:03:42 +0000269};
270
271enum _DescStatusBit {
272 OWNbit = 0x80000000,
273 EORbit = 0x40000000,
274 FSbit = 0x20000000,
275 LSbit = 0x10000000,
276};
277
278struct TxDesc {
279 u32 status;
280 u32 vlan_tag;
281 u32 buf_addr;
282 u32 buf_Haddr;
283};
284
285struct RxDesc {
286 u32 status;
287 u32 vlan_tag;
288 u32 buf_addr;
289 u32 buf_Haddr;
290};
291
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600292static unsigned char rxdata[RX_BUF_LEN];
293
Thierry Redingdad3ba02014-12-09 22:25:25 -0700294#define RTL8169_DESC_SIZE 16
wdenka8bd82d2004-04-18 22:03:42 +0000295
Thierry Redingdad3ba02014-12-09 22:25:25 -0700296#if ARCH_DMA_MINALIGN > 256
297# define RTL8169_ALIGN ARCH_DMA_MINALIGN
298#else
299# define RTL8169_ALIGN 256
300#endif
301
302/*
303 * Warn if the cache-line size is larger than the descriptor size. In such
304 * cases the driver will likely fail because the CPU needs to flush the cache
305 * when requeuing RX buffers, therefore descriptors written by the hardware
306 * may be discarded.
Thierry Redingd58acdc2014-12-09 22:25:26 -0700307 *
308 * This can be fixed by defining CONFIG_SYS_NONCACHED_MEMORY which will cause
309 * the driver to allocate descriptors from a pool of non-cached memory.
Thierry Redingdad3ba02014-12-09 22:25:25 -0700310 */
311#if RTL8169_DESC_SIZE < ARCH_DMA_MINALIGN
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600312#if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
Trevor Woerner10015022019-05-03 09:41:00 -0400313 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86)
Thierry Redingdad3ba02014-12-09 22:25:25 -0700314#warning cache-line size is larger than descriptor size
315#endif
Thierry Redingd58acdc2014-12-09 22:25:26 -0700316#endif
wdenka8bd82d2004-04-18 22:03:42 +0000317
Thierry Redingdad3ba02014-12-09 22:25:25 -0700318/*
319 * Create a static buffer of size RX_BUF_SZ for each TX Descriptor. All
320 * descriptors point to a part of this buffer.
321 */
322DEFINE_ALIGN_BUFFER(u8, txb, NUM_TX_DESC * RX_BUF_SIZE, RTL8169_ALIGN);
323
324/*
325 * Create a static buffer of size RX_BUF_SZ for each RX Descriptor. All
326 * descriptors point to a part of this buffer.
327 */
328DEFINE_ALIGN_BUFFER(u8, rxb, NUM_RX_DESC * RX_BUF_SIZE, RTL8169_ALIGN);
wdenka8bd82d2004-04-18 22:03:42 +0000329
330struct rtl8169_private {
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600331 ulong iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000332 void *mmio_addr; /* memory map physical address */
333 int chipset;
334 unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
335 unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
336 unsigned long dirty_tx;
wdenka8bd82d2004-04-18 22:03:42 +0000337 struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
338 struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
339 unsigned char *RxBufferRings; /* Index of Rx Buffer */
340 unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
341 unsigned char *Tx_skbuff[NUM_TX_DESC];
342} tpx;
343
344static struct rtl8169_private *tpc;
345
wdenka8bd82d2004-04-18 22:03:42 +0000346static const unsigned int rtl8169_rx_config =
347 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
348
349static struct pci_device_id supported[] = {
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600350 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167) },
351 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168) },
352 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169) },
wdenka8bd82d2004-04-18 22:03:42 +0000353 {}
354};
355
356void mdio_write(int RegAddr, int value)
357{
358 int i;
359
360 RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
361 udelay(1000);
362
363 for (i = 2000; i > 0; i--) {
364 /* Check if the RTL8169 has completed writing to the specified MII register */
365 if (!(RTL_R32(PHYAR) & 0x80000000)) {
366 break;
367 } else {
368 udelay(100);
369 }
370 }
371}
372
373int mdio_read(int RegAddr)
374{
375 int i, value = -1;
376
377 RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
378 udelay(1000);
379
380 for (i = 2000; i > 0; i--) {
381 /* Check if the RTL8169 has completed retrieving data from the specified MII register */
382 if (RTL_R32(PHYAR) & 0x80000000) {
383 value = (int) (RTL_R32(PHYAR) & 0xFFFF);
384 break;
385 } else {
386 udelay(100);
387 }
388 }
389 return value;
390}
391
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600392static int rtl8169_init_board(unsigned long dev_iobase, const char *name)
wdenka8bd82d2004-04-18 22:03:42 +0000393{
394 int i;
395 u32 tmp;
396
397#ifdef DEBUG_RTL8169
398 printf ("%s\n", __FUNCTION__);
399#endif
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600400 ioaddr = dev_iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000401
402 /* Soft reset the chip. */
403 RTL_W8(ChipCmd, CmdReset);
404
405 /* Check that the chip has finished the reset. */
406 for (i = 1000; i > 0; i--)
407 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
408 break;
409 else
410 udelay(10);
411
412 /* identify chip attached to board */
413 tmp = RTL_R32(TxConfig);
414 tmp = ((tmp & 0x7c000000) + ((tmp & 0x00800000) << 2)) >> 24;
415
416 for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--){
417 if (tmp == rtl_chip_info[i].version) {
418 tpc->chipset = i;
419 goto match;
420 }
421 }
422
423 /* if unknown chip, assume array element #0, original RTL-8169 in this case */
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600424 printf("PCI device %s: unknown chip version, assuming RTL-8169\n",
425 name);
Wolfgang Denk06c53be2008-07-10 13:16:09 +0200426 printf("PCI device: TxConfig = 0x%lX\n", (unsigned long) RTL_R32(TxConfig));
wdenka8bd82d2004-04-18 22:03:42 +0000427 tpc->chipset = 0;
428
429match:
430 return 0;
431}
432
Thierry Reding22ece0e2013-09-20 16:03:42 +0200433/*
Thierry Redingd58acdc2014-12-09 22:25:26 -0700434 * TX and RX descriptors are 16 bytes. This causes problems with the cache
435 * maintenance on CPUs where the cache-line size exceeds the size of these
436 * descriptors. What will happen is that when the driver receives a packet
437 * it will be immediately requeued for the hardware to reuse. The CPU will
438 * therefore need to flush the cache-line containing the descriptor, which
439 * will cause all other descriptors in the same cache-line to be flushed
440 * along with it. If one of those descriptors had been written to by the
441 * device those changes (and the associated packet) will be lost.
442 *
443 * To work around this, we make use of non-cached memory if available. If
444 * descriptors are mapped uncached there's no need to manually flush them
445 * or invalidate them.
446 *
447 * Note that this only applies to descriptors. The packet data buffers do
448 * not have the same constraints since they are 1536 bytes large, so they
449 * are unlikely to share cache-lines.
450 */
451static void *rtl_alloc_descs(unsigned int num)
452{
453 size_t size = num * RTL8169_DESC_SIZE;
454
455#ifdef CONFIG_SYS_NONCACHED_MEMORY
456 return (void *)noncached_alloc(size, RTL8169_ALIGN);
457#else
458 return memalign(RTL8169_ALIGN, size);
459#endif
460}
461
462/*
Thierry Reding22ece0e2013-09-20 16:03:42 +0200463 * Cache maintenance functions. These are simple wrappers around the more
464 * general purpose flush_cache() and invalidate_dcache_range() functions.
465 */
466
467static void rtl_inval_rx_desc(struct RxDesc *desc)
468{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700469#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200470 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
471 unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
472
473 invalidate_dcache_range(start, end);
Thierry Redingd58acdc2014-12-09 22:25:26 -0700474#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200475}
476
477static void rtl_flush_rx_desc(struct RxDesc *desc)
478{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700479#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200480 flush_cache((unsigned long)desc, sizeof(*desc));
Thierry Redingd58acdc2014-12-09 22:25:26 -0700481#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200482}
483
484static void rtl_inval_tx_desc(struct TxDesc *desc)
485{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700486#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200487 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
488 unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
489
490 invalidate_dcache_range(start, end);
Thierry Redingd58acdc2014-12-09 22:25:26 -0700491#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200492}
493
494static void rtl_flush_tx_desc(struct TxDesc *desc)
495{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700496#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200497 flush_cache((unsigned long)desc, sizeof(*desc));
Thierry Redingd58acdc2014-12-09 22:25:26 -0700498#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200499}
500
501static void rtl_inval_buffer(void *buf, size_t size)
502{
503 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
504 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
505
506 invalidate_dcache_range(start, end);
507}
508
509static void rtl_flush_buffer(void *buf, size_t size)
510{
511 flush_cache((unsigned long)buf, size);
512}
513
wdenka8bd82d2004-04-18 22:03:42 +0000514/**************************************************************************
515RECV - Receive a frame
516***************************************************************************/
Simon Glass552ddbe2015-11-29 13:18:04 -0700517#ifdef CONFIG_DM_ETH
518static int rtl_recv_common(struct udevice *dev, unsigned long dev_iobase,
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600519 uchar **packetp)
Simon Glass552ddbe2015-11-29 13:18:04 -0700520#else
521static int rtl_recv_common(pci_dev_t dev, unsigned long dev_iobase,
522 uchar **packetp)
523#endif
wdenka8bd82d2004-04-18 22:03:42 +0000524{
525 /* return true if there's an ethernet packet ready to read */
526 /* nic->packet should contain data on return */
527 /* nic->packetlen should contain length of data */
528 int cur_rx;
529 int length = 0;
530
531#ifdef DEBUG_RTL8169_RX
532 printf ("%s\n", __FUNCTION__);
533#endif
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600534 ioaddr = dev_iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000535
536 cur_rx = tpc->cur_rx;
Thierry Reding22ece0e2013-09-20 16:03:42 +0200537
538 rtl_inval_rx_desc(&tpc->RxDescArray[cur_rx]);
539
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100540 if ((le32_to_cpu(tpc->RxDescArray[cur_rx].status) & OWNbit) == 0) {
541 if (!(le32_to_cpu(tpc->RxDescArray[cur_rx].status) & RxRES)) {
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100542 length = (int) (le32_to_cpu(tpc->RxDescArray[cur_rx].
543 status) & 0x00001FFF) - 4;
wdenka8bd82d2004-04-18 22:03:42 +0000544
Thierry Reding22ece0e2013-09-20 16:03:42 +0200545 rtl_inval_buffer(tpc->RxBufferRing[cur_rx], length);
wdenka8bd82d2004-04-18 22:03:42 +0000546 memcpy(rxdata, tpc->RxBufferRing[cur_rx], length);
wdenka8bd82d2004-04-18 22:03:42 +0000547
548 if (cur_rx == NUM_RX_DESC - 1)
549 tpc->RxDescArray[cur_rx].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100550 cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000551 else
552 tpc->RxDescArray[cur_rx].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100553 cpu_to_le32(OWNbit + RX_BUF_SIZE);
Simon Glass552ddbe2015-11-29 13:18:04 -0700554#ifdef CONFIG_DM_ETH
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600555 tpc->RxDescArray[cur_rx].buf_addr = cpu_to_le32(
Simon Glass552ddbe2015-11-29 13:18:04 -0700556 dm_pci_mem_to_phys(dev,
557 (pci_addr_t)(unsigned long)
558 tpc->RxBufferRing[cur_rx]));
559#else
560 tpc->RxDescArray[cur_rx].buf_addr = cpu_to_le32(
561 pci_mem_to_phys(dev, (pci_addr_t)(unsigned long)
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600562 tpc->RxBufferRing[cur_rx]));
Simon Glass552ddbe2015-11-29 13:18:04 -0700563#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200564 rtl_flush_rx_desc(&tpc->RxDescArray[cur_rx]);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600565#ifdef CONFIG_DM_ETH
566 *packetp = rxdata;
567#else
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500568 net_process_received_packet(rxdata, length);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600569#endif
wdenka8bd82d2004-04-18 22:03:42 +0000570 } else {
571 puts("Error Rx");
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600572 length = -EIO;
wdenka8bd82d2004-04-18 22:03:42 +0000573 }
574 cur_rx = (cur_rx + 1) % NUM_RX_DESC;
575 tpc->cur_rx = cur_rx;
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600576 return length;
wdenka8bd82d2004-04-18 22:03:42 +0000577
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900578 } else {
579 ushort sts = RTL_R8(IntrStatus);
580 RTL_W8(IntrStatus, sts & ~(TxErr | RxErr | SYSErr));
581 udelay(100); /* wait */
wdenka8bd82d2004-04-18 22:03:42 +0000582 }
583 tpc->cur_rx = cur_rx;
584 return (0); /* initially as this is called to flush the input */
585}
586
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600587#ifdef CONFIG_DM_ETH
588int rtl8169_eth_recv(struct udevice *dev, int flags, uchar **packetp)
589{
590 struct rtl8169_private *priv = dev_get_priv(dev);
591
Simon Glass552ddbe2015-11-29 13:18:04 -0700592 return rtl_recv_common(dev, priv->iobase, packetp);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600593}
594#else
595static int rtl_recv(struct eth_device *dev)
596{
Stephen Warrenf3ba5522015-10-02 17:44:34 -0600597 return rtl_recv_common((pci_dev_t)(unsigned long)dev->priv,
598 dev->iobase, NULL);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600599}
600#endif /* nCONFIG_DM_ETH */
601
wdenka8bd82d2004-04-18 22:03:42 +0000602#define HZ 1000
603/**************************************************************************
604SEND - Transmit a frame
605***************************************************************************/
Simon Glass552ddbe2015-11-29 13:18:04 -0700606#ifdef CONFIG_DM_ETH
607static int rtl_send_common(struct udevice *dev, unsigned long dev_iobase,
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600608 void *packet, int length)
Simon Glass552ddbe2015-11-29 13:18:04 -0700609#else
610static int rtl_send_common(pci_dev_t dev, unsigned long dev_iobase,
611 void *packet, int length)
612#endif
wdenka8bd82d2004-04-18 22:03:42 +0000613{
614 /* send the packet to destination */
615
616 u32 to;
617 u8 *ptxb;
618 int entry = tpc->cur_tx % NUM_TX_DESC;
619 u32 len = length;
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100620 int ret;
wdenka8bd82d2004-04-18 22:03:42 +0000621
622#ifdef DEBUG_RTL8169_TX
623 int stime = currticks();
624 printf ("%s\n", __FUNCTION__);
625 printf("sending %d bytes\n", len);
626#endif
627
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600628 ioaddr = dev_iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000629
630 /* point to the current txb incase multiple tx_rings are used */
631 ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
632 memcpy(ptxb, (char *)packet, (int)length);
633
634 while (len < ETH_ZLEN)
635 ptxb[len++] = '\0';
636
Peter Chubb73776472016-09-14 01:29:03 +0000637 rtl_flush_buffer(ptxb, ALIGN(len, RTL8169_ALIGN));
638
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900639 tpc->TxDescArray[entry].buf_Haddr = 0;
Simon Glass552ddbe2015-11-29 13:18:04 -0700640#ifdef CONFIG_DM_ETH
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600641 tpc->TxDescArray[entry].buf_addr = cpu_to_le32(
Simon Glass552ddbe2015-11-29 13:18:04 -0700642 dm_pci_mem_to_phys(dev, (pci_addr_t)(unsigned long)ptxb));
643#else
644 tpc->TxDescArray[entry].buf_addr = cpu_to_le32(
645 pci_mem_to_phys(dev, (pci_addr_t)(unsigned long)ptxb));
646#endif
wdenka8bd82d2004-04-18 22:03:42 +0000647 if (entry != (NUM_TX_DESC - 1)) {
648 tpc->TxDescArray[entry].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100649 cpu_to_le32((OWNbit | FSbit | LSbit) |
650 ((len > ETH_ZLEN) ? len : ETH_ZLEN));
wdenka8bd82d2004-04-18 22:03:42 +0000651 } else {
652 tpc->TxDescArray[entry].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100653 cpu_to_le32((OWNbit | EORbit | FSbit | LSbit) |
654 ((len > ETH_ZLEN) ? len : ETH_ZLEN));
wdenka8bd82d2004-04-18 22:03:42 +0000655 }
Thierry Reding22ece0e2013-09-20 16:03:42 +0200656 rtl_flush_tx_desc(&tpc->TxDescArray[entry]);
wdenka8bd82d2004-04-18 22:03:42 +0000657 RTL_W8(TxPoll, 0x40); /* set polling bit */
658
659 tpc->cur_tx++;
660 to = currticks() + TX_TIMEOUT;
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900661 do {
Thierry Reding22ece0e2013-09-20 16:03:42 +0200662 rtl_inval_tx_desc(&tpc->TxDescArray[entry]);
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900663 } while ((le32_to_cpu(tpc->TxDescArray[entry].status) & OWNbit)
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100664 && (currticks() < to)); /* wait */
wdenka8bd82d2004-04-18 22:03:42 +0000665
666 if (currticks() >= to) {
667#ifdef DEBUG_RTL8169_TX
Thierry Reding7a36b9c2013-09-20 16:03:41 +0200668 puts("tx timeout/error\n");
669 printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
wdenka8bd82d2004-04-18 22:03:42 +0000670#endif
Oleksandr Tymoshenko4c64c4d2016-07-01 13:22:00 -0700671 ret = -ETIMEDOUT;
wdenka8bd82d2004-04-18 22:03:42 +0000672 } else {
673#ifdef DEBUG_RTL8169_TX
674 puts("tx done\n");
675#endif
Oleksandr Tymoshenko4c64c4d2016-07-01 13:22:00 -0700676 ret = 0;
wdenka8bd82d2004-04-18 22:03:42 +0000677 }
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100678 /* Delay to make net console (nc) work properly */
679 udelay(20);
680 return ret;
wdenka8bd82d2004-04-18 22:03:42 +0000681}
682
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600683#ifdef CONFIG_DM_ETH
684int rtl8169_eth_send(struct udevice *dev, void *packet, int length)
685{
686 struct rtl8169_private *priv = dev_get_priv(dev);
687
Simon Glass552ddbe2015-11-29 13:18:04 -0700688 return rtl_send_common(dev, priv->iobase, packet, length);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600689}
690
691#else
692static int rtl_send(struct eth_device *dev, void *packet, int length)
693{
Stephen Warrenf3ba5522015-10-02 17:44:34 -0600694 return rtl_send_common((pci_dev_t)(unsigned long)dev->priv,
695 dev->iobase, packet, length);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600696}
697#endif
698
699static void rtl8169_set_rx_mode(void)
wdenka8bd82d2004-04-18 22:03:42 +0000700{
701 u32 mc_filter[2]; /* Multicast hash filter */
702 int rx_mode;
703 u32 tmp = 0;
704
705#ifdef DEBUG_RTL8169
706 printf ("%s\n", __FUNCTION__);
707#endif
708
709 /* IFF_ALLMULTI */
710 /* Too many to filter perfectly -- accept all multicasts. */
711 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
712 mc_filter[1] = mc_filter[0] = 0xffffffff;
713
714 tmp = rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
715 rtl_chip_info[tpc->chipset].RxConfigMask);
716
717 RTL_W32(RxConfig, tmp);
718 RTL_W32(MAR0 + 0, mc_filter[0]);
719 RTL_W32(MAR0 + 4, mc_filter[1]);
720}
721
Simon Glass552ddbe2015-11-29 13:18:04 -0700722#ifdef CONFIG_DM_ETH
723static void rtl8169_hw_start(struct udevice *dev)
724#else
725static void rtl8169_hw_start(pci_dev_t dev)
726#endif
wdenka8bd82d2004-04-18 22:03:42 +0000727{
728 u32 i;
729
730#ifdef DEBUG_RTL8169
731 int stime = currticks();
732 printf ("%s\n", __FUNCTION__);
733#endif
734
735#if 0
736 /* Soft reset the chip. */
737 RTL_W8(ChipCmd, CmdReset);
738
739 /* Check that the chip has finished the reset. */
740 for (i = 1000; i > 0; i--) {
741 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
742 break;
743 else
744 udelay(10);
745 }
746#endif
747
748 RTL_W8(Cfg9346, Cfg9346_Unlock);
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900749
750 /* RTL-8169sb/8110sb or previous version */
751 if (tpc->chipset <= 5)
752 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
753
wdenka8bd82d2004-04-18 22:03:42 +0000754 RTL_W8(EarlyTxThres, EarlyTxThld);
755
756 /* For gigabit rtl8169 */
757 RTL_W16(RxMaxSize, RxPacketMaxSize);
758
759 /* Set Rx Config register */
760 i = rtl8169_rx_config | (RTL_R32(RxConfig) &
761 rtl_chip_info[tpc->chipset].RxConfigMask);
762 RTL_W32(RxConfig, i);
763
764 /* Set DMA burst size and Interframe Gap Time */
765 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
766 (InterFrameGap << TxInterFrameGapShift));
767
768
769 tpc->cur_rx = 0;
770
Simon Glass552ddbe2015-11-29 13:18:04 -0700771#ifdef CONFIG_DM_ETH
772 RTL_W32(TxDescStartAddrLow, dm_pci_mem_to_phys(dev,
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600773 (pci_addr_t)(unsigned long)tpc->TxDescArray));
Simon Glass552ddbe2015-11-29 13:18:04 -0700774#else
775 RTL_W32(TxDescStartAddrLow, pci_mem_to_phys(dev,
776 (pci_addr_t)(unsigned long)tpc->TxDescArray));
777#endif
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900778 RTL_W32(TxDescStartAddrHigh, (unsigned long)0);
Simon Glass552ddbe2015-11-29 13:18:04 -0700779#ifdef CONFIG_DM_ETH
780 RTL_W32(RxDescStartAddrLow, dm_pci_mem_to_phys(
781 dev, (pci_addr_t)(unsigned long)tpc->RxDescArray));
782#else
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600783 RTL_W32(RxDescStartAddrLow, pci_mem_to_phys(
Simon Glass552ddbe2015-11-29 13:18:04 -0700784 dev, (pci_addr_t)(unsigned long)tpc->RxDescArray));
785#endif
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900786 RTL_W32(RxDescStartAddrHigh, (unsigned long)0);
787
788 /* RTL-8169sc/8110sc or later version */
789 if (tpc->chipset > 5)
790 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
791
wdenka8bd82d2004-04-18 22:03:42 +0000792 RTL_W8(Cfg9346, Cfg9346_Lock);
793 udelay(10);
794
795 RTL_W32(RxMissed, 0);
796
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600797 rtl8169_set_rx_mode();
wdenka8bd82d2004-04-18 22:03:42 +0000798
799 /* no early-rx interrupts */
800 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
801
802#ifdef DEBUG_RTL8169
Thierry Reding7a36b9c2013-09-20 16:03:41 +0200803 printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
wdenka8bd82d2004-04-18 22:03:42 +0000804#endif
805}
806
Simon Glass552ddbe2015-11-29 13:18:04 -0700807#ifdef CONFIG_DM_ETH
808static void rtl8169_init_ring(struct udevice *dev)
809#else
810static void rtl8169_init_ring(pci_dev_t dev)
811#endif
wdenka8bd82d2004-04-18 22:03:42 +0000812{
813 int i;
814
815#ifdef DEBUG_RTL8169
816 int stime = currticks();
817 printf ("%s\n", __FUNCTION__);
818#endif
819
820 tpc->cur_rx = 0;
821 tpc->cur_tx = 0;
822 tpc->dirty_tx = 0;
823 memset(tpc->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
824 memset(tpc->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
825
826 for (i = 0; i < NUM_TX_DESC; i++) {
827 tpc->Tx_skbuff[i] = &txb[i];
828 }
829
830 for (i = 0; i < NUM_RX_DESC; i++) {
831 if (i == (NUM_RX_DESC - 1))
832 tpc->RxDescArray[i].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100833 cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000834 else
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100835 tpc->RxDescArray[i].status =
836 cpu_to_le32(OWNbit + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000837
838 tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
Simon Glass552ddbe2015-11-29 13:18:04 -0700839#ifdef CONFIG_DM_ETH
840 tpc->RxDescArray[i].buf_addr = cpu_to_le32(dm_pci_mem_to_phys(
841 dev, (pci_addr_t)(unsigned long)tpc->RxBufferRing[i]));
842#else
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600843 tpc->RxDescArray[i].buf_addr = cpu_to_le32(pci_mem_to_phys(
Simon Glass552ddbe2015-11-29 13:18:04 -0700844 dev, (pci_addr_t)(unsigned long)tpc->RxBufferRing[i]));
845#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200846 rtl_flush_rx_desc(&tpc->RxDescArray[i]);
wdenka8bd82d2004-04-18 22:03:42 +0000847 }
848
849#ifdef DEBUG_RTL8169
Thierry Reding7a36b9c2013-09-20 16:03:41 +0200850 printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
wdenka8bd82d2004-04-18 22:03:42 +0000851#endif
852}
853
Simon Glass552ddbe2015-11-29 13:18:04 -0700854#ifdef CONFIG_DM_ETH
Stephen Warrendad7b742016-04-26 15:29:00 -0600855static void rtl8169_common_start(struct udevice *dev, unsigned char *enetaddr,
856 unsigned long dev_iobase)
Simon Glass552ddbe2015-11-29 13:18:04 -0700857#else
Stephen Warrendad7b742016-04-26 15:29:00 -0600858static void rtl8169_common_start(pci_dev_t dev, unsigned char *enetaddr,
859 unsigned long dev_iobase)
Simon Glass552ddbe2015-11-29 13:18:04 -0700860#endif
wdenka8bd82d2004-04-18 22:03:42 +0000861{
862 int i;
wdenka8bd82d2004-04-18 22:03:42 +0000863
864#ifdef DEBUG_RTL8169
865 int stime = currticks();
866 printf ("%s\n", __FUNCTION__);
867#endif
868
Stephen Warrendad7b742016-04-26 15:29:00 -0600869 ioaddr = dev_iobase;
870
Simon Glass552ddbe2015-11-29 13:18:04 -0700871 rtl8169_init_ring(dev);
872 rtl8169_hw_start(dev);
wdenka8bd82d2004-04-18 22:03:42 +0000873 /* Construct a perfect filter frame with the mac address as first match
874 * and broadcast for all others */
875 for (i = 0; i < 192; i++)
876 txb[i] = 0xFF;
877
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600878 txb[0] = enetaddr[0];
879 txb[1] = enetaddr[1];
880 txb[2] = enetaddr[2];
881 txb[3] = enetaddr[3];
882 txb[4] = enetaddr[4];
883 txb[5] = enetaddr[5];
wdenka8bd82d2004-04-18 22:03:42 +0000884
885#ifdef DEBUG_RTL8169
Thierry Reding7a36b9c2013-09-20 16:03:41 +0200886 printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
wdenka8bd82d2004-04-18 22:03:42 +0000887#endif
888}
889
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600890#ifdef CONFIG_DM_ETH
891static int rtl8169_eth_start(struct udevice *dev)
892{
Simon Glassc69cda22020-12-03 16:55:20 -0700893 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warrendad7b742016-04-26 15:29:00 -0600894 struct rtl8169_private *priv = dev_get_priv(dev);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600895
Stephen Warrendad7b742016-04-26 15:29:00 -0600896 rtl8169_common_start(dev, plat->enetaddr, priv->iobase);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600897
898 return 0;
899}
900#else
wdenka8bd82d2004-04-18 22:03:42 +0000901/**************************************************************************
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600902RESET - Finish setting up the ethernet interface
wdenka8bd82d2004-04-18 22:03:42 +0000903***************************************************************************/
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900904static int rtl_reset(struct eth_device *dev, struct bd_info *bis)
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600905{
Stephen Warrenf3ba5522015-10-02 17:44:34 -0600906 rtl8169_common_start((pci_dev_t)(unsigned long)dev->priv,
Stephen Warrendad7b742016-04-26 15:29:00 -0600907 dev->enetaddr, dev->iobase);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600908
909 return 0;
910}
911#endif /* nCONFIG_DM_ETH */
912
913static void rtl_halt_common(unsigned long dev_iobase)
wdenka8bd82d2004-04-18 22:03:42 +0000914{
915 int i;
916
917#ifdef DEBUG_RTL8169
918 printf ("%s\n", __FUNCTION__);
919#endif
920
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600921 ioaddr = dev_iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000922
923 /* Stop the chip's Tx and Rx DMA processes. */
924 RTL_W8(ChipCmd, 0x00);
925
926 /* Disable interrupts by clearing the interrupt mask. */
927 RTL_W16(IntrMask, 0x0000);
928
929 RTL_W32(RxMissed, 0);
930
wdenka8bd82d2004-04-18 22:03:42 +0000931 for (i = 0; i < NUM_RX_DESC; i++) {
932 tpc->RxBufferRing[i] = NULL;
933 }
934}
935
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600936#ifdef CONFIG_DM_ETH
937void rtl8169_eth_stop(struct udevice *dev)
938{
939 struct rtl8169_private *priv = dev_get_priv(dev);
940
941 rtl_halt_common(priv->iobase);
942}
943#else
944/**************************************************************************
945HALT - Turn off ethernet interface
946***************************************************************************/
947static void rtl_halt(struct eth_device *dev)
948{
949 rtl_halt_common(dev->iobase);
950}
951#endif
952
Thierry Redingb6054b52019-04-16 18:20:29 +0200953#ifdef CONFIG_DM_ETH
954static int rtl8169_write_hwaddr(struct udevice *dev)
955{
Simon Glassc69cda22020-12-03 16:55:20 -0700956 struct eth_pdata *plat = dev_get_plat(dev);
Thierry Redingb6054b52019-04-16 18:20:29 +0200957 unsigned int i;
958
959 RTL_W8(Cfg9346, Cfg9346_Unlock);
960
961 for (i = 0; i < MAC_ADDR_LEN; i++)
962 RTL_W8(MAC0 + i, plat->enetaddr[i]);
963
964 RTL_W8(Cfg9346, Cfg9346_Lock);
965
966 return 0;
967}
968#endif
969
wdenka8bd82d2004-04-18 22:03:42 +0000970/**************************************************************************
971INIT - Look for an adapter, this routine's visible to the outside
972***************************************************************************/
973
974#define board_found 1
975#define valid_link 0
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600976static int rtl_init(unsigned long dev_ioaddr, const char *name,
977 unsigned char *enetaddr)
wdenka8bd82d2004-04-18 22:03:42 +0000978{
979 static int board_idx = -1;
wdenka8bd82d2004-04-18 22:03:42 +0000980 int i, rc;
981 int option = -1, Cap10_100 = 0, Cap1000 = 0;
982
983#ifdef DEBUG_RTL8169
984 printf ("%s\n", __FUNCTION__);
985#endif
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600986 ioaddr = dev_ioaddr;
wdenka8bd82d2004-04-18 22:03:42 +0000987
988 board_idx++;
989
wdenka8bd82d2004-04-18 22:03:42 +0000990 /* point to private storage */
991 tpc = &tpx;
992
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600993 rc = rtl8169_init_board(ioaddr, name);
wdenka8bd82d2004-04-18 22:03:42 +0000994 if (rc)
995 return rc;
996
997 /* Get MAC address. FIXME: read EEPROM */
998 for (i = 0; i < MAC_ADDR_LEN; i++)
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600999 enetaddr[i] = RTL_R8(MAC0 + i);
wdenka8bd82d2004-04-18 22:03:42 +00001000
1001#ifdef DEBUG_RTL8169
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +09001002 printf("chipset = %d\n", tpc->chipset);
wdenka8bd82d2004-04-18 22:03:42 +00001003 printf("MAC Address");
1004 for (i = 0; i < MAC_ADDR_LEN; i++)
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001005 printf(":%02x", enetaddr[i]);
wdenka8bd82d2004-04-18 22:03:42 +00001006 putc('\n');
1007#endif
1008
1009#ifdef DEBUG_RTL8169
1010 /* Print out some hardware info */
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001011 printf("%s: at ioaddr 0x%lx\n", name, ioaddr);
wdenka8bd82d2004-04-18 22:03:42 +00001012#endif
1013
1014 /* if TBI is not endbled */
1015 if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
1016 int val = mdio_read(PHY_AUTO_NEGO_REG);
1017
1018 option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
1019 /* Force RTL8169 in 10/100/1000 Full/Half mode. */
1020 if (option > 0) {
1021#ifdef DEBUG_RTL8169
Bin Mengdbe25382016-03-17 23:27:44 -07001022 printf("%s: Force-mode Enabled.\n", name);
wdenka8bd82d2004-04-18 22:03:42 +00001023#endif
1024 Cap10_100 = 0, Cap1000 = 0;
1025 switch (option) {
1026 case _10_Half:
1027 Cap10_100 = PHY_Cap_10_Half;
1028 Cap1000 = PHY_Cap_Null;
1029 break;
1030 case _10_Full:
1031 Cap10_100 = PHY_Cap_10_Full;
1032 Cap1000 = PHY_Cap_Null;
1033 break;
1034 case _100_Half:
1035 Cap10_100 = PHY_Cap_100_Half;
1036 Cap1000 = PHY_Cap_Null;
1037 break;
1038 case _100_Full:
1039 Cap10_100 = PHY_Cap_100_Full;
1040 Cap1000 = PHY_Cap_Null;
1041 break;
1042 case _1000_Full:
1043 Cap10_100 = PHY_Cap_Null;
1044 Cap1000 = PHY_Cap_1000_Full;
1045 break;
1046 default:
1047 break;
1048 }
1049 mdio_write(PHY_AUTO_NEGO_REG, Cap10_100 | (val & 0x1F)); /* leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
1050 mdio_write(PHY_1000_CTRL_REG, Cap1000);
1051 } else {
1052#ifdef DEBUG_RTL8169
1053 printf("%s: Auto-negotiation Enabled.\n",
Bin Mengdbe25382016-03-17 23:27:44 -07001054 name);
wdenka8bd82d2004-04-18 22:03:42 +00001055#endif
1056 /* enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
1057 mdio_write(PHY_AUTO_NEGO_REG,
1058 PHY_Cap_10_Half | PHY_Cap_10_Full |
1059 PHY_Cap_100_Half | PHY_Cap_100_Full |
1060 (val & 0x1F));
1061
1062 /* enable 1000 Full Mode */
1063 mdio_write(PHY_1000_CTRL_REG, PHY_Cap_1000_Full);
1064
1065 }
1066
1067 /* Enable auto-negotiation and restart auto-nigotiation */
1068 mdio_write(PHY_CTRL_REG,
1069 PHY_Enable_Auto_Nego | PHY_Restart_Auto_Nego);
1070 udelay(100);
1071
1072 /* wait for auto-negotiation process */
1073 for (i = 10000; i > 0; i--) {
1074 /* check if auto-negotiation complete */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +01001075 if (mdio_read(PHY_STAT_REG) & PHY_Auto_Nego_Comp) {
wdenka8bd82d2004-04-18 22:03:42 +00001076 udelay(100);
1077 option = RTL_R8(PHYstatus);
1078 if (option & _1000bpsF) {
1079#ifdef DEBUG_RTL8169
1080 printf("%s: 1000Mbps Full-duplex operation.\n",
Bin Mengdbe25382016-03-17 23:27:44 -07001081 name);
wdenka8bd82d2004-04-18 22:03:42 +00001082#endif
1083 } else {
1084#ifdef DEBUG_RTL8169
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +01001085 printf("%s: %sMbps %s-duplex operation.\n",
Bin Mengdbe25382016-03-17 23:27:44 -07001086 name,
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +01001087 (option & _100bps) ? "100" :
1088 "10",
1089 (option & FullDup) ? "Full" :
1090 "Half");
wdenka8bd82d2004-04-18 22:03:42 +00001091#endif
1092 }
1093 break;
1094 } else {
1095 udelay(100);
1096 }
1097 } /* end for-loop to wait for auto-negotiation process */
1098
1099 } else {
1100 udelay(100);
1101#ifdef DEBUG_RTL8169
1102 printf
1103 ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
Bin Mengdbe25382016-03-17 23:27:44 -07001104 name,
wdenka8bd82d2004-04-18 22:03:42 +00001105 (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
1106#endif
1107 }
1108
Thierry Redingdad3ba02014-12-09 22:25:25 -07001109
Thierry Redingd58acdc2014-12-09 22:25:26 -07001110 tpc->RxDescArray = rtl_alloc_descs(NUM_RX_DESC);
1111 if (!tpc->RxDescArray)
1112 return -ENOMEM;
1113
1114 tpc->TxDescArray = rtl_alloc_descs(NUM_TX_DESC);
1115 if (!tpc->TxDescArray)
1116 return -ENOMEM;
1117
1118 return 0;
wdenka8bd82d2004-04-18 22:03:42 +00001119}
1120
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001121#ifndef CONFIG_DM_ETH
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001122int rtl8169_initialize(struct bd_info *bis)
wdenka8bd82d2004-04-18 22:03:42 +00001123{
1124 pci_dev_t devno;
1125 int card_number = 0;
1126 struct eth_device *dev;
1127 u32 iobase;
1128 int idx=0;
1129
1130 while(1){
Thierry Reding22872862013-09-20 16:03:43 +02001131 unsigned int region;
1132 u16 device;
Thierry Redingd58acdc2014-12-09 22:25:26 -07001133 int err;
Thierry Reding22872862013-09-20 16:03:43 +02001134
wdenka8bd82d2004-04-18 22:03:42 +00001135 /* Find RTL8169 */
1136 if ((devno = pci_find_devices(supported, idx++)) < 0)
1137 break;
1138
Thierry Reding22872862013-09-20 16:03:43 +02001139 pci_read_config_word(devno, PCI_DEVICE_ID, &device);
1140 switch (device) {
1141 case 0x8168:
1142 region = 2;
1143 break;
1144
1145 default:
1146 region = 1;
1147 break;
1148 }
1149
1150 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0 + (region * 4), &iobase);
wdenka8bd82d2004-04-18 22:03:42 +00001151 iobase &= ~0xf;
1152
1153 debug ("rtl8169: REALTEK RTL8169 @0x%x\n", iobase);
1154
1155 dev = (struct eth_device *)malloc(sizeof *dev);
Nobuhiro Iwamatsuf4eaef72010-10-19 14:03:38 +09001156 if (!dev) {
1157 printf("Can not allocate memory of rtl8169\n");
1158 break;
1159 }
wdenka8bd82d2004-04-18 22:03:42 +00001160
Nobuhiro Iwamatsuf4eaef72010-10-19 14:03:38 +09001161 memset(dev, 0, sizeof(*dev));
wdenka8bd82d2004-04-18 22:03:42 +00001162 sprintf (dev->name, "RTL8169#%d", card_number);
1163
Thierry Reding744152f2015-03-20 12:41:21 +01001164 dev->priv = (void *)(unsigned long)devno;
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +01001165 dev->iobase = (int)pci_mem_to_phys(devno, iobase);
wdenka8bd82d2004-04-18 22:03:42 +00001166
1167 dev->init = rtl_reset;
1168 dev->halt = rtl_halt;
1169 dev->send = rtl_send;
1170 dev->recv = rtl_recv;
1171
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001172 err = rtl_init(dev->iobase, dev->name, dev->enetaddr);
Thierry Redingd58acdc2014-12-09 22:25:26 -07001173 if (err < 0) {
1174 printf(pr_fmt("failed to initialize card: %d\n"), err);
1175 free(dev);
1176 continue;
1177 }
wdenka8bd82d2004-04-18 22:03:42 +00001178
Thierry Redingd58acdc2014-12-09 22:25:26 -07001179 eth_register (dev);
wdenka8bd82d2004-04-18 22:03:42 +00001180
1181 card_number++;
1182 }
1183 return card_number;
1184}
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001185#endif
1186
1187#ifdef CONFIG_DM_ETH
1188static int rtl8169_eth_probe(struct udevice *dev)
1189{
Simon Glass8a8d24b2020-12-03 16:55:23 -07001190 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001191 struct rtl8169_private *priv = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -07001192 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001193 u32 iobase;
1194 int region;
1195 int ret;
1196
1197 debug("rtl8169: REALTEK RTL8169 @0x%x\n", iobase);
1198 switch (pplat->device) {
1199 case 0x8168:
1200 region = 2;
1201 break;
1202 default:
1203 region = 1;
1204 break;
1205 }
Simon Glass552ddbe2015-11-29 13:18:04 -07001206 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0 + region * 4, &iobase);
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001207 iobase &= ~0xf;
Simon Glass552ddbe2015-11-29 13:18:04 -07001208 priv->iobase = (int)dm_pci_mem_to_phys(dev, iobase);
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001209
1210 ret = rtl_init(priv->iobase, dev->name, plat->enetaddr);
1211 if (ret < 0) {
1212 printf(pr_fmt("failed to initialize card: %d\n"), ret);
1213 return ret;
1214 }
1215
Tom Warrena7a435e2020-03-26 15:59:13 -07001216 /*
1217 * WAR for DHCP failure after rebooting from kernel.
1218 * Clear RxDv_Gated_En bit which was set by kernel driver.
1219 * Without this, U-Boot can't get an IP via DHCP.
1220 * Register (FuncEvent, aka MISC) and RXDV_GATED_EN bit are from
1221 * the r8169.c kernel driver.
1222 */
1223
1224 u32 val = RTL_R32(FuncEvent);
1225 debug("%s: FuncEvent/Misc (0xF0) = 0x%08X\n", __func__, val);
1226 val &= ~RxDv_Gated_En;
1227 RTL_W32(FuncEvent, val);
1228
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001229 return 0;
1230}
1231
1232static const struct eth_ops rtl8169_eth_ops = {
1233 .start = rtl8169_eth_start,
1234 .send = rtl8169_eth_send,
1235 .recv = rtl8169_eth_recv,
1236 .stop = rtl8169_eth_stop,
Thierry Redingb6054b52019-04-16 18:20:29 +02001237 .write_hwaddr = rtl8169_write_hwaddr,
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001238};
1239
1240static const struct udevice_id rtl8169_eth_ids[] = {
1241 { .compatible = "realtek,rtl8169" },
1242 { }
1243};
1244
1245U_BOOT_DRIVER(eth_rtl8169) = {
1246 .name = "eth_rtl8169",
1247 .id = UCLASS_ETH,
1248 .of_match = rtl8169_eth_ids,
1249 .probe = rtl8169_eth_probe,
1250 .ops = &rtl8169_eth_ops,
Simon Glass41575d82020-12-03 16:55:17 -07001251 .priv_auto = sizeof(struct rtl8169_private),
Simon Glasscaa4daa2020-12-03 16:55:18 -07001252 .plat_auto = sizeof(struct eth_pdata),
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001253};
1254
1255U_BOOT_PCI_DEVICE(eth_rtl8169, supported);
1256#endif