blob: 93e83661cec4ed5629f1d00948f11ccc1789049f [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 Glass90526e92020-05-10 11:39:56 -060050#include <asm/cache.h>
wdenka8bd82d2004-04-18 22:03:42 +000051#include <asm/io.h>
52#include <pci.h>
Simon Glassc05ed002020-05-10 11:40:11 -060053#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060054#include <linux/printk.h>
wdenka8bd82d2004-04-18 22:03:42 +000055
wdenka8bd82d2004-04-18 22:03:42 +000056#undef DEBUG_RTL8169
57#undef DEBUG_RTL8169_TX
58#undef DEBUG_RTL8169_RX
59
60#define drv_version "v1.5"
61#define drv_date "01-17-2004"
62
Thierry Reding744152f2015-03-20 12:41:21 +010063static unsigned long ioaddr;
wdenka8bd82d2004-04-18 22:03:42 +000064
65/* Condensed operations for readability. */
wdenka8bd82d2004-04-18 22:03:42 +000066#define currticks() get_timer(0)
wdenka8bd82d2004-04-18 22:03:42 +000067
68/* media options */
69#define MAX_UNITS 8
70static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
71
72/* MAC address length*/
73#define MAC_ADDR_LEN 6
74
75/* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/
76#define MAX_ETH_FRAME_SIZE 1536
77
78#define TX_FIFO_THRESH 256 /* In bytes */
79
80#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
81#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
82#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
83#define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
84#define RxPacketMaxSize 0x0800 /* Maximum size supported is 16K-1 */
85#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
86
87#define NUM_TX_DESC 1 /* Number of Tx descriptor registers */
Thierry Redingc94bbfd2014-12-09 22:25:24 -070088#ifdef CONFIG_SYS_RX_ETH_BUFFER
89 #define NUM_RX_DESC CONFIG_SYS_RX_ETH_BUFFER
90#else
91 #define NUM_RX_DESC 4 /* Number of Rx descriptor registers */
92#endif
wdenka8bd82d2004-04-18 22:03:42 +000093#define RX_BUF_SIZE 1536 /* Rx Buffer size */
94#define RX_BUF_LEN 8192
95
96#define RTL_MIN_IO_SIZE 0x80
97#define TX_TIMEOUT (6*HZ)
98
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +010099/* write/read MMIO register. Notice: {read,write}[wl] do the necessary swapping */
Minda Chena6a0d6a2023-07-20 19:37:26 +0800100#define RTL_W8(reg, val8) writeb((val8), (void *)(ioaddr + (reg)))
101#define RTL_W16(reg, val16) writew((val16), (void *)(ioaddr + (reg)))
102#define RTL_W32(reg, val32) writel((val32), (void *)(ioaddr + (reg)))
103#define RTL_R8(reg) readb((void *)(ioaddr + (reg)))
104#define RTL_R16(reg) readw((void *)(ioaddr + (reg)))
105#define RTL_R32(reg) readl((void *)(ioaddr + (reg)))
wdenka8bd82d2004-04-18 22:03:42 +0000106
Thierry Reding744152f2015-03-20 12:41:21 +0100107#define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)(unsigned long)dev->priv, \
108 (pci_addr_t)(unsigned long)a)
109#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)(unsigned long)dev->priv, \
110 (phys_addr_t)a)
Yoshihiro Shimodad65e34d2009-02-25 14:27:29 +0900111
wdenka8bd82d2004-04-18 22:03:42 +0000112enum RTL8169_registers {
113 MAC0 = 0, /* Ethernet hardware address. */
114 MAR0 = 8, /* Multicast filter. */
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900115 TxDescStartAddrLow = 0x20,
116 TxDescStartAddrHigh = 0x24,
117 TxHDescStartAddrLow = 0x28,
118 TxHDescStartAddrHigh = 0x2c,
wdenka8bd82d2004-04-18 22:03:42 +0000119 FLASH = 0x30,
120 ERSR = 0x36,
121 ChipCmd = 0x37,
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300122 TxPoll_8169 = 0x38,
123 IntrMask_8169 = 0x3C,
124 IntrStatus_8169 = 0x3E,
wdenka8bd82d2004-04-18 22:03:42 +0000125 TxConfig = 0x40,
126 RxConfig = 0x44,
127 RxMissed = 0x4C,
128 Cfg9346 = 0x50,
129 Config0 = 0x51,
130 Config1 = 0x52,
131 Config2 = 0x53,
132 Config3 = 0x54,
133 Config4 = 0x55,
134 Config5 = 0x56,
135 MultiIntr = 0x5C,
136 PHYAR = 0x60,
137 TBICSR = 0x64,
138 TBI_ANAR = 0x68,
139 TBI_LPAR = 0x6A,
140 PHYstatus = 0x6C,
141 RxMaxSize = 0xDA,
142 CPlusCmd = 0xE0,
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900143 RxDescStartAddrLow = 0xE4,
144 RxDescStartAddrHigh = 0xE8,
wdenka8bd82d2004-04-18 22:03:42 +0000145 EarlyTxThres = 0xEC,
146 FuncEvent = 0xF0,
147 FuncEventMask = 0xF4,
148 FuncPresetState = 0xF8,
149 FuncForceEvent = 0xFC,
150};
151
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300152enum RTL8125_registers {
153 IntrMask_8125 = 0x38,
154 IntrStatus_8125 = 0x3C,
155 TxPoll_8125 = 0x90,
156};
157
wdenka8bd82d2004-04-18 22:03:42 +0000158enum RTL8169_register_content {
159 /*InterruptStatusBits */
160 SYSErr = 0x8000,
161 PCSTimeout = 0x4000,
162 SWInt = 0x0100,
163 TxDescUnavail = 0x80,
164 RxFIFOOver = 0x40,
165 RxUnderrun = 0x20,
166 RxOverflow = 0x10,
167 TxErr = 0x08,
168 TxOK = 0x04,
169 RxErr = 0x02,
170 RxOK = 0x01,
171
172 /*RxStatusDesc */
173 RxRES = 0x00200000,
174 RxCRC = 0x00080000,
175 RxRUNT = 0x00100000,
176 RxRWT = 0x00400000,
177
178 /*ChipCmdBits */
179 CmdReset = 0x10,
180 CmdRxEnb = 0x08,
181 CmdTxEnb = 0x04,
182 RxBufEmpty = 0x01,
183
184 /*Cfg9346Bits */
185 Cfg9346_Lock = 0x00,
186 Cfg9346_Unlock = 0xC0,
187
188 /*rx_mode_bits */
189 AcceptErr = 0x20,
190 AcceptRunt = 0x10,
191 AcceptBroadcast = 0x08,
192 AcceptMulticast = 0x04,
193 AcceptMyPhys = 0x02,
194 AcceptAllPhys = 0x01,
195
196 /*RxConfigBits */
197 RxCfgFIFOShift = 13,
198 RxCfgDMAShift = 8,
199
200 /*TxConfigBits */
201 TxInterFrameGapShift = 24,
202 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
203
204 /*rtl8169_PHYstatus */
205 TBI_Enable = 0x80,
206 TxFlowCtrl = 0x40,
207 RxFlowCtrl = 0x20,
208 _1000bpsF = 0x10,
209 _100bps = 0x08,
210 _10bps = 0x04,
211 LinkStatus = 0x02,
212 FullDup = 0x01,
213
214 /*GIGABIT_PHY_registers */
215 PHY_CTRL_REG = 0,
216 PHY_STAT_REG = 1,
217 PHY_AUTO_NEGO_REG = 4,
218 PHY_1000_CTRL_REG = 9,
219
220 /*GIGABIT_PHY_REG_BIT */
221 PHY_Restart_Auto_Nego = 0x0200,
222 PHY_Enable_Auto_Nego = 0x1000,
223
224 /* PHY_STAT_REG = 1; */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100225 PHY_Auto_Nego_Comp = 0x0020,
wdenka8bd82d2004-04-18 22:03:42 +0000226
227 /* PHY_AUTO_NEGO_REG = 4; */
228 PHY_Cap_10_Half = 0x0020,
229 PHY_Cap_10_Full = 0x0040,
230 PHY_Cap_100_Half = 0x0080,
231 PHY_Cap_100_Full = 0x0100,
232
233 /* PHY_1000_CTRL_REG = 9; */
234 PHY_Cap_1000_Full = 0x0200,
235
236 PHY_Cap_Null = 0x0,
237
238 /*_MediaType*/
239 _10_Half = 0x01,
240 _10_Full = 0x02,
241 _100_Half = 0x04,
242 _100_Full = 0x08,
243 _1000_Full = 0x10,
244
245 /*_TBICSRBit*/
246 TBILinkOK = 0x02000000,
Tom Warrena7a435e2020-03-26 15:59:13 -0700247
248 /* FuncEvent/Misc */
249 RxDv_Gated_En = 0x80000,
wdenka8bd82d2004-04-18 22:03:42 +0000250};
251
252static struct {
253 const char *name;
254 u8 version; /* depend on RTL8169 docs */
255 u32 RxConfigMask; /* should clear the bits supported by this chip */
256} rtl_chip_info[] = {
257 {"RTL-8169", 0x00, 0xff7e1880,},
258 {"RTL-8169", 0x04, 0xff7e1880,},
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900259 {"RTL-8169", 0x00, 0xff7e1880,},
260 {"RTL-8169s/8110s", 0x02, 0xff7e1880,},
261 {"RTL-8169s/8110s", 0x04, 0xff7e1880,},
262 {"RTL-8169sb/8110sb", 0x10, 0xff7e1880,},
263 {"RTL-8169sc/8110sc", 0x18, 0xff7e1880,},
264 {"RTL-8168b/8111sb", 0x30, 0xff7e1880,},
265 {"RTL-8168b/8111sb", 0x38, 0xff7e1880,},
Thierry Reding945dd962019-09-11 19:19:06 +0200266 {"RTL-8168c/8111c", 0x3c, 0xff7e1880,},
Thierry Reding22872862013-09-20 16:03:43 +0200267 {"RTL-8168d/8111d", 0x28, 0xff7e1880,},
Thierry Reding65a66912013-09-20 16:03:44 +0200268 {"RTL-8168evl/8111evl", 0x2e, 0xff7e1880,},
Thierry Redingcc0856c2014-12-09 22:25:27 -0700269 {"RTL-8168/8111g", 0x4c, 0xff7e1880,},
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900270 {"RTL-8101e", 0x34, 0xff7e1880,},
271 {"RTL-8100e", 0x32, 0xff7e1880,},
Thierry Redingcdd69ac2019-04-16 18:20:30 +0200272 {"RTL-8168h/8111h", 0x54, 0xff7e1880,},
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300273 {"RTL-8125B", 0x64, 0xff7e1880,},
wdenka8bd82d2004-04-18 22:03:42 +0000274};
275
276enum _DescStatusBit {
277 OWNbit = 0x80000000,
278 EORbit = 0x40000000,
279 FSbit = 0x20000000,
280 LSbit = 0x10000000,
281};
282
283struct TxDesc {
284 u32 status;
285 u32 vlan_tag;
286 u32 buf_addr;
287 u32 buf_Haddr;
288};
289
290struct RxDesc {
291 u32 status;
292 u32 vlan_tag;
293 u32 buf_addr;
294 u32 buf_Haddr;
295};
296
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600297static unsigned char rxdata[RX_BUF_LEN];
298
Thierry Redingdad3ba02014-12-09 22:25:25 -0700299#define RTL8169_DESC_SIZE 16
wdenka8bd82d2004-04-18 22:03:42 +0000300
Thierry Redingdad3ba02014-12-09 22:25:25 -0700301#if ARCH_DMA_MINALIGN > 256
302# define RTL8169_ALIGN ARCH_DMA_MINALIGN
303#else
304# define RTL8169_ALIGN 256
305#endif
306
307/*
308 * Warn if the cache-line size is larger than the descriptor size. In such
309 * cases the driver will likely fail because the CPU needs to flush the cache
310 * when requeuing RX buffers, therefore descriptors written by the hardware
311 * may be discarded.
Thierry Redingd58acdc2014-12-09 22:25:26 -0700312 *
313 * This can be fixed by defining CONFIG_SYS_NONCACHED_MEMORY which will cause
314 * the driver to allocate descriptors from a pool of non-cached memory.
Minda Chen30948452023-07-20 19:37:27 +0800315 *
316 * Hardware maintain D-cache coherency in RISC-V architecture.
Thierry Redingdad3ba02014-12-09 22:25:25 -0700317 */
318#if RTL8169_DESC_SIZE < ARCH_DMA_MINALIGN
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600319#if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
Minda Chen30948452023-07-20 19:37:27 +0800320 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86) && !defined(CONFIG_RISCV)
Thierry Redingdad3ba02014-12-09 22:25:25 -0700321#warning cache-line size is larger than descriptor size
322#endif
Thierry Redingd58acdc2014-12-09 22:25:26 -0700323#endif
wdenka8bd82d2004-04-18 22:03:42 +0000324
Thierry Redingdad3ba02014-12-09 22:25:25 -0700325/*
326 * Create a static buffer of size RX_BUF_SZ for each TX Descriptor. All
327 * descriptors point to a part of this buffer.
328 */
329DEFINE_ALIGN_BUFFER(u8, txb, NUM_TX_DESC * RX_BUF_SIZE, RTL8169_ALIGN);
330
331/*
332 * Create a static buffer of size RX_BUF_SZ for each RX Descriptor. All
333 * descriptors point to a part of this buffer.
334 */
335DEFINE_ALIGN_BUFFER(u8, rxb, NUM_RX_DESC * RX_BUF_SIZE, RTL8169_ALIGN);
wdenka8bd82d2004-04-18 22:03:42 +0000336
337struct rtl8169_private {
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600338 ulong iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000339 void *mmio_addr; /* memory map physical address */
340 int chipset;
341 unsigned long cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
342 unsigned long cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
343 unsigned long dirty_tx;
wdenka8bd82d2004-04-18 22:03:42 +0000344 struct TxDesc *TxDescArray; /* Index of 256-alignment Tx Descriptor buffer */
345 struct RxDesc *RxDescArray; /* Index of 256-alignment Rx Descriptor buffer */
346 unsigned char *RxBufferRings; /* Index of Rx Buffer */
347 unsigned char *RxBufferRing[NUM_RX_DESC]; /* Index of Rx Buffer array */
348 unsigned char *Tx_skbuff[NUM_TX_DESC];
349} tpx;
350
351static struct rtl8169_private *tpc;
352
wdenka8bd82d2004-04-18 22:03:42 +0000353static const unsigned int rtl8169_rx_config =
354 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
355
356static struct pci_device_id supported[] = {
Minda Chenff8590a2023-07-20 19:37:28 +0800357 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8125) },
358 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161) },
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600359 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167) },
360 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168) },
361 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169) },
wdenka8bd82d2004-04-18 22:03:42 +0000362 {}
363};
364
365void mdio_write(int RegAddr, int value)
366{
367 int i;
368
369 RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
370 udelay(1000);
371
372 for (i = 2000; i > 0; i--) {
373 /* Check if the RTL8169 has completed writing to the specified MII register */
374 if (!(RTL_R32(PHYAR) & 0x80000000)) {
375 break;
376 } else {
377 udelay(100);
378 }
379 }
380}
381
382int mdio_read(int RegAddr)
383{
384 int i, value = -1;
385
386 RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
387 udelay(1000);
388
389 for (i = 2000; i > 0; i--) {
390 /* Check if the RTL8169 has completed retrieving data from the specified MII register */
391 if (RTL_R32(PHYAR) & 0x80000000) {
392 value = (int) (RTL_R32(PHYAR) & 0xFFFF);
393 break;
394 } else {
395 udelay(100);
396 }
397 }
398 return value;
399}
400
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600401static int rtl8169_init_board(unsigned long dev_iobase, const char *name)
wdenka8bd82d2004-04-18 22:03:42 +0000402{
403 int i;
404 u32 tmp;
405
406#ifdef DEBUG_RTL8169
407 printf ("%s\n", __FUNCTION__);
408#endif
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600409 ioaddr = dev_iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000410
411 /* Soft reset the chip. */
412 RTL_W8(ChipCmd, CmdReset);
413
414 /* Check that the chip has finished the reset. */
415 for (i = 1000; i > 0; i--)
416 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
417 break;
418 else
419 udelay(10);
420
421 /* identify chip attached to board */
422 tmp = RTL_R32(TxConfig);
423 tmp = ((tmp & 0x7c000000) + ((tmp & 0x00800000) << 2)) >> 24;
424
425 for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--){
426 if (tmp == rtl_chip_info[i].version) {
427 tpc->chipset = i;
428 goto match;
429 }
430 }
431
432 /* if unknown chip, assume array element #0, original RTL-8169 in this case */
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600433 printf("PCI device %s: unknown chip version, assuming RTL-8169\n",
434 name);
Wolfgang Denk06c53be2008-07-10 13:16:09 +0200435 printf("PCI device: TxConfig = 0x%lX\n", (unsigned long) RTL_R32(TxConfig));
wdenka8bd82d2004-04-18 22:03:42 +0000436 tpc->chipset = 0;
437
438match:
439 return 0;
440}
441
Thierry Reding22ece0e2013-09-20 16:03:42 +0200442/*
Thierry Redingd58acdc2014-12-09 22:25:26 -0700443 * TX and RX descriptors are 16 bytes. This causes problems with the cache
444 * maintenance on CPUs where the cache-line size exceeds the size of these
445 * descriptors. What will happen is that when the driver receives a packet
446 * it will be immediately requeued for the hardware to reuse. The CPU will
447 * therefore need to flush the cache-line containing the descriptor, which
448 * will cause all other descriptors in the same cache-line to be flushed
449 * along with it. If one of those descriptors had been written to by the
450 * device those changes (and the associated packet) will be lost.
451 *
452 * To work around this, we make use of non-cached memory if available. If
453 * descriptors are mapped uncached there's no need to manually flush them
454 * or invalidate them.
455 *
456 * Note that this only applies to descriptors. The packet data buffers do
457 * not have the same constraints since they are 1536 bytes large, so they
458 * are unlikely to share cache-lines.
459 */
460static void *rtl_alloc_descs(unsigned int num)
461{
462 size_t size = num * RTL8169_DESC_SIZE;
463
464#ifdef CONFIG_SYS_NONCACHED_MEMORY
465 return (void *)noncached_alloc(size, RTL8169_ALIGN);
466#else
467 return memalign(RTL8169_ALIGN, size);
468#endif
469}
470
471/*
Thierry Reding22ece0e2013-09-20 16:03:42 +0200472 * Cache maintenance functions. These are simple wrappers around the more
473 * general purpose flush_cache() and invalidate_dcache_range() functions.
474 */
475
476static void rtl_inval_rx_desc(struct RxDesc *desc)
477{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700478#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200479 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
480 unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
481
482 invalidate_dcache_range(start, end);
Thierry Redingd58acdc2014-12-09 22:25:26 -0700483#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200484}
485
486static void rtl_flush_rx_desc(struct RxDesc *desc)
487{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700488#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200489 flush_cache((unsigned long)desc, sizeof(*desc));
Thierry Redingd58acdc2014-12-09 22:25:26 -0700490#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200491}
492
493static void rtl_inval_tx_desc(struct TxDesc *desc)
494{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700495#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200496 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
497 unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
498
499 invalidate_dcache_range(start, end);
Thierry Redingd58acdc2014-12-09 22:25:26 -0700500#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200501}
502
503static void rtl_flush_tx_desc(struct TxDesc *desc)
504{
Thierry Redingd58acdc2014-12-09 22:25:26 -0700505#ifndef CONFIG_SYS_NONCACHED_MEMORY
Thierry Reding22ece0e2013-09-20 16:03:42 +0200506 flush_cache((unsigned long)desc, sizeof(*desc));
Thierry Redingd58acdc2014-12-09 22:25:26 -0700507#endif
Thierry Reding22ece0e2013-09-20 16:03:42 +0200508}
509
510static void rtl_inval_buffer(void *buf, size_t size)
511{
512 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
513 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
514
515 invalidate_dcache_range(start, end);
516}
517
518static void rtl_flush_buffer(void *buf, size_t size)
519{
520 flush_cache((unsigned long)buf, size);
521}
522
wdenka8bd82d2004-04-18 22:03:42 +0000523/**************************************************************************
524RECV - Receive a frame
525***************************************************************************/
Simon Glass552ddbe2015-11-29 13:18:04 -0700526static int rtl_recv_common(struct udevice *dev, unsigned long dev_iobase,
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600527 uchar **packetp)
wdenka8bd82d2004-04-18 22:03:42 +0000528{
529 /* return true if there's an ethernet packet ready to read */
530 /* nic->packet should contain data on return */
531 /* nic->packetlen should contain length of data */
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300532 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
wdenka8bd82d2004-04-18 22:03:42 +0000533 int cur_rx;
534 int length = 0;
535
536#ifdef DEBUG_RTL8169_RX
537 printf ("%s\n", __FUNCTION__);
538#endif
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600539 ioaddr = dev_iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000540
541 cur_rx = tpc->cur_rx;
Thierry Reding22ece0e2013-09-20 16:03:42 +0200542
543 rtl_inval_rx_desc(&tpc->RxDescArray[cur_rx]);
544
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100545 if ((le32_to_cpu(tpc->RxDescArray[cur_rx].status) & OWNbit) == 0) {
546 if (!(le32_to_cpu(tpc->RxDescArray[cur_rx].status) & RxRES)) {
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100547 length = (int) (le32_to_cpu(tpc->RxDescArray[cur_rx].
548 status) & 0x00001FFF) - 4;
wdenka8bd82d2004-04-18 22:03:42 +0000549
Thierry Reding22ece0e2013-09-20 16:03:42 +0200550 rtl_inval_buffer(tpc->RxBufferRing[cur_rx], length);
wdenka8bd82d2004-04-18 22:03:42 +0000551 memcpy(rxdata, tpc->RxBufferRing[cur_rx], length);
wdenka8bd82d2004-04-18 22:03:42 +0000552
553 if (cur_rx == NUM_RX_DESC - 1)
554 tpc->RxDescArray[cur_rx].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100555 cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000556 else
557 tpc->RxDescArray[cur_rx].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100558 cpu_to_le32(OWNbit + RX_BUF_SIZE);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600559 tpc->RxDescArray[cur_rx].buf_addr = cpu_to_le32(
Simon Glass552ddbe2015-11-29 13:18:04 -0700560 dm_pci_mem_to_phys(dev,
561 (pci_addr_t)(unsigned long)
562 tpc->RxBufferRing[cur_rx]));
Thierry Reding22ece0e2013-09-20 16:03:42 +0200563 rtl_flush_rx_desc(&tpc->RxDescArray[cur_rx]);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600564 *packetp = rxdata;
wdenka8bd82d2004-04-18 22:03:42 +0000565 } else {
566 puts("Error Rx");
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600567 length = -EIO;
wdenka8bd82d2004-04-18 22:03:42 +0000568 }
569 cur_rx = (cur_rx + 1) % NUM_RX_DESC;
570 tpc->cur_rx = cur_rx;
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600571 return length;
wdenka8bd82d2004-04-18 22:03:42 +0000572
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900573 } else {
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300574 u32 IntrStatus = IntrStatus_8169;
575
576 if (pplat->device == 0x8125)
577 IntrStatus = IntrStatus_8125;
Nobuhiro Iwamatsud75469d2008-03-08 09:25:49 +0900578 ushort sts = RTL_R8(IntrStatus);
579 RTL_W8(IntrStatus, sts & ~(TxErr | RxErr | SYSErr));
580 udelay(100); /* wait */
wdenka8bd82d2004-04-18 22:03:42 +0000581 }
582 tpc->cur_rx = cur_rx;
583 return (0); /* initially as this is called to flush the input */
584}
585
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600586int rtl8169_eth_recv(struct udevice *dev, int flags, uchar **packetp)
587{
588 struct rtl8169_private *priv = dev_get_priv(dev);
589
Simon Glass552ddbe2015-11-29 13:18:04 -0700590 return rtl_recv_common(dev, priv->iobase, packetp);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600591}
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600592
wdenka8bd82d2004-04-18 22:03:42 +0000593#define HZ 1000
594/**************************************************************************
595SEND - Transmit a frame
596***************************************************************************/
Simon Glass552ddbe2015-11-29 13:18:04 -0700597static int rtl_send_common(struct udevice *dev, unsigned long dev_iobase,
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600598 void *packet, int length)
wdenka8bd82d2004-04-18 22:03:42 +0000599{
600 /* send the packet to destination */
601
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300602 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
wdenka8bd82d2004-04-18 22:03:42 +0000603 u32 to;
604 u8 *ptxb;
605 int entry = tpc->cur_tx % NUM_TX_DESC;
606 u32 len = length;
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100607 int ret;
wdenka8bd82d2004-04-18 22:03:42 +0000608
609#ifdef DEBUG_RTL8169_TX
610 int stime = currticks();
611 printf ("%s\n", __FUNCTION__);
612 printf("sending %d bytes\n", len);
613#endif
614
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600615 ioaddr = dev_iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000616
617 /* point to the current txb incase multiple tx_rings are used */
618 ptxb = tpc->Tx_skbuff[entry * MAX_ETH_FRAME_SIZE];
619 memcpy(ptxb, (char *)packet, (int)length);
620
621 while (len < ETH_ZLEN)
622 ptxb[len++] = '\0';
623
Peter Chubb73776472016-09-14 01:29:03 +0000624 rtl_flush_buffer(ptxb, ALIGN(len, RTL8169_ALIGN));
625
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900626 tpc->TxDescArray[entry].buf_Haddr = 0;
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600627 tpc->TxDescArray[entry].buf_addr = cpu_to_le32(
Simon Glass552ddbe2015-11-29 13:18:04 -0700628 dm_pci_mem_to_phys(dev, (pci_addr_t)(unsigned long)ptxb));
wdenka8bd82d2004-04-18 22:03:42 +0000629 if (entry != (NUM_TX_DESC - 1)) {
630 tpc->TxDescArray[entry].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100631 cpu_to_le32((OWNbit | FSbit | LSbit) |
632 ((len > ETH_ZLEN) ? len : ETH_ZLEN));
wdenka8bd82d2004-04-18 22:03:42 +0000633 } else {
634 tpc->TxDescArray[entry].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100635 cpu_to_le32((OWNbit | EORbit | FSbit | LSbit) |
636 ((len > ETH_ZLEN) ? len : ETH_ZLEN));
wdenka8bd82d2004-04-18 22:03:42 +0000637 }
Thierry Reding22ece0e2013-09-20 16:03:42 +0200638 rtl_flush_tx_desc(&tpc->TxDescArray[entry]);
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300639 if (pplat->device == 0x8125)
640 RTL_W8(TxPoll_8125, 0x1); /* set polling bit */
641 else
642 RTL_W8(TxPoll_8169, 0x40); /* set polling bit */
wdenka8bd82d2004-04-18 22:03:42 +0000643
644 tpc->cur_tx++;
645 to = currticks() + TX_TIMEOUT;
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900646 do {
Thierry Reding22ece0e2013-09-20 16:03:42 +0200647 rtl_inval_tx_desc(&tpc->TxDescArray[entry]);
Yoshihiro Shimodad4c02e62009-02-25 14:27:24 +0900648 } while ((le32_to_cpu(tpc->TxDescArray[entry].status) & OWNbit)
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100649 && (currticks() < to)); /* wait */
wdenka8bd82d2004-04-18 22:03:42 +0000650
651 if (currticks() >= to) {
652#ifdef DEBUG_RTL8169_TX
Thierry Reding7a36b9c2013-09-20 16:03:41 +0200653 puts("tx timeout/error\n");
654 printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
wdenka8bd82d2004-04-18 22:03:42 +0000655#endif
Oleksandr Tymoshenko4c64c4d2016-07-01 13:22:00 -0700656 ret = -ETIMEDOUT;
wdenka8bd82d2004-04-18 22:03:42 +0000657 } else {
658#ifdef DEBUG_RTL8169_TX
659 puts("tx done\n");
660#endif
Oleksandr Tymoshenko4c64c4d2016-07-01 13:22:00 -0700661 ret = 0;
wdenka8bd82d2004-04-18 22:03:42 +0000662 }
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100663 /* Delay to make net console (nc) work properly */
664 udelay(20);
665 return ret;
wdenka8bd82d2004-04-18 22:03:42 +0000666}
667
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600668int rtl8169_eth_send(struct udevice *dev, void *packet, int length)
669{
670 struct rtl8169_private *priv = dev_get_priv(dev);
671
Simon Glass552ddbe2015-11-29 13:18:04 -0700672 return rtl_send_common(dev, priv->iobase, packet, length);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600673}
674
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600675static void rtl8169_set_rx_mode(void)
wdenka8bd82d2004-04-18 22:03:42 +0000676{
677 u32 mc_filter[2]; /* Multicast hash filter */
678 int rx_mode;
679 u32 tmp = 0;
680
681#ifdef DEBUG_RTL8169
682 printf ("%s\n", __FUNCTION__);
683#endif
684
685 /* IFF_ALLMULTI */
686 /* Too many to filter perfectly -- accept all multicasts. */
687 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
688 mc_filter[1] = mc_filter[0] = 0xffffffff;
689
690 tmp = rtl8169_rx_config | rx_mode | (RTL_R32(RxConfig) &
691 rtl_chip_info[tpc->chipset].RxConfigMask);
692
693 RTL_W32(RxConfig, tmp);
694 RTL_W32(MAR0 + 0, mc_filter[0]);
695 RTL_W32(MAR0 + 4, mc_filter[1]);
696}
697
Simon Glass552ddbe2015-11-29 13:18:04 -0700698static void rtl8169_hw_start(struct udevice *dev)
wdenka8bd82d2004-04-18 22:03:42 +0000699{
700 u32 i;
701
702#ifdef DEBUG_RTL8169
703 int stime = currticks();
704 printf ("%s\n", __FUNCTION__);
705#endif
706
707#if 0
708 /* Soft reset the chip. */
709 RTL_W8(ChipCmd, CmdReset);
710
711 /* Check that the chip has finished the reset. */
712 for (i = 1000; i > 0; i--) {
713 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
714 break;
715 else
716 udelay(10);
717 }
718#endif
719
720 RTL_W8(Cfg9346, Cfg9346_Unlock);
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900721
722 /* RTL-8169sb/8110sb or previous version */
723 if (tpc->chipset <= 5)
724 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
725
wdenka8bd82d2004-04-18 22:03:42 +0000726 RTL_W8(EarlyTxThres, EarlyTxThld);
727
728 /* For gigabit rtl8169 */
729 RTL_W16(RxMaxSize, RxPacketMaxSize);
730
731 /* Set Rx Config register */
732 i = rtl8169_rx_config | (RTL_R32(RxConfig) &
733 rtl_chip_info[tpc->chipset].RxConfigMask);
734 RTL_W32(RxConfig, i);
735
736 /* Set DMA burst size and Interframe Gap Time */
737 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
738 (InterFrameGap << TxInterFrameGapShift));
739
740
741 tpc->cur_rx = 0;
742
Simon Glass552ddbe2015-11-29 13:18:04 -0700743 RTL_W32(TxDescStartAddrLow, dm_pci_mem_to_phys(dev,
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600744 (pci_addr_t)(unsigned long)tpc->TxDescArray));
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900745 RTL_W32(TxDescStartAddrHigh, (unsigned long)0);
Simon Glass552ddbe2015-11-29 13:18:04 -0700746 RTL_W32(RxDescStartAddrLow, dm_pci_mem_to_phys(
747 dev, (pci_addr_t)(unsigned long)tpc->RxDescArray));
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900748 RTL_W32(RxDescStartAddrHigh, (unsigned long)0);
749
750 /* RTL-8169sc/8110sc or later version */
751 if (tpc->chipset > 5)
752 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
753
wdenka8bd82d2004-04-18 22:03:42 +0000754 RTL_W8(Cfg9346, Cfg9346_Lock);
755 udelay(10);
756
757 RTL_W32(RxMissed, 0);
758
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600759 rtl8169_set_rx_mode();
wdenka8bd82d2004-04-18 22:03:42 +0000760
761 /* no early-rx interrupts */
762 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
763
764#ifdef DEBUG_RTL8169
Thierry Reding7a36b9c2013-09-20 16:03:41 +0200765 printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
wdenka8bd82d2004-04-18 22:03:42 +0000766#endif
767}
768
Simon Glass552ddbe2015-11-29 13:18:04 -0700769static void rtl8169_init_ring(struct udevice *dev)
wdenka8bd82d2004-04-18 22:03:42 +0000770{
771 int i;
772
773#ifdef DEBUG_RTL8169
774 int stime = currticks();
775 printf ("%s\n", __FUNCTION__);
776#endif
777
778 tpc->cur_rx = 0;
779 tpc->cur_tx = 0;
780 tpc->dirty_tx = 0;
781 memset(tpc->TxDescArray, 0x0, NUM_TX_DESC * sizeof(struct TxDesc));
782 memset(tpc->RxDescArray, 0x0, NUM_RX_DESC * sizeof(struct RxDesc));
783
784 for (i = 0; i < NUM_TX_DESC; i++) {
785 tpc->Tx_skbuff[i] = &txb[i];
786 }
787
788 for (i = 0; i < NUM_RX_DESC; i++) {
789 if (i == (NUM_RX_DESC - 1))
790 tpc->RxDescArray[i].status =
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100791 cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000792 else
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +0100793 tpc->RxDescArray[i].status =
794 cpu_to_le32(OWNbit + RX_BUF_SIZE);
wdenka8bd82d2004-04-18 22:03:42 +0000795
796 tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE];
Simon Glass552ddbe2015-11-29 13:18:04 -0700797 tpc->RxDescArray[i].buf_addr = cpu_to_le32(dm_pci_mem_to_phys(
798 dev, (pci_addr_t)(unsigned long)tpc->RxBufferRing[i]));
Thierry Reding22ece0e2013-09-20 16:03:42 +0200799 rtl_flush_rx_desc(&tpc->RxDescArray[i]);
wdenka8bd82d2004-04-18 22:03:42 +0000800 }
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
Stephen Warrendad7b742016-04-26 15:29:00 -0600807static void rtl8169_common_start(struct udevice *dev, unsigned char *enetaddr,
808 unsigned long dev_iobase)
wdenka8bd82d2004-04-18 22:03:42 +0000809{
810 int i;
wdenka8bd82d2004-04-18 22:03:42 +0000811
812#ifdef DEBUG_RTL8169
813 int stime = currticks();
814 printf ("%s\n", __FUNCTION__);
815#endif
816
Stephen Warrendad7b742016-04-26 15:29:00 -0600817 ioaddr = dev_iobase;
818
Simon Glass552ddbe2015-11-29 13:18:04 -0700819 rtl8169_init_ring(dev);
820 rtl8169_hw_start(dev);
wdenka8bd82d2004-04-18 22:03:42 +0000821 /* Construct a perfect filter frame with the mac address as first match
822 * and broadcast for all others */
823 for (i = 0; i < 192; i++)
824 txb[i] = 0xFF;
825
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600826 txb[0] = enetaddr[0];
827 txb[1] = enetaddr[1];
828 txb[2] = enetaddr[2];
829 txb[3] = enetaddr[3];
830 txb[4] = enetaddr[4];
831 txb[5] = enetaddr[5];
wdenka8bd82d2004-04-18 22:03:42 +0000832
833#ifdef DEBUG_RTL8169
Thierry Reding7a36b9c2013-09-20 16:03:41 +0200834 printf("%s elapsed time : %lu\n", __func__, currticks()-stime);
wdenka8bd82d2004-04-18 22:03:42 +0000835#endif
836}
837
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600838static int rtl8169_eth_start(struct udevice *dev)
839{
Simon Glassc69cda22020-12-03 16:55:20 -0700840 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warrendad7b742016-04-26 15:29:00 -0600841 struct rtl8169_private *priv = dev_get_priv(dev);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600842
Stephen Warrendad7b742016-04-26 15:29:00 -0600843 rtl8169_common_start(dev, plat->enetaddr, priv->iobase);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600844
845 return 0;
846}
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600847
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300848static void rtl_halt_common(struct udevice *dev)
wdenka8bd82d2004-04-18 22:03:42 +0000849{
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300850 struct rtl8169_private *priv = dev_get_priv(dev);
851 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
wdenka8bd82d2004-04-18 22:03:42 +0000852 int i;
853
854#ifdef DEBUG_RTL8169
855 printf ("%s\n", __FUNCTION__);
856#endif
857
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300858 ioaddr = priv->iobase;
wdenka8bd82d2004-04-18 22:03:42 +0000859
860 /* Stop the chip's Tx and Rx DMA processes. */
861 RTL_W8(ChipCmd, 0x00);
862
863 /* Disable interrupts by clearing the interrupt mask. */
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300864 if (pplat->device == 0x8125)
865 RTL_W16(IntrMask_8125, 0x0000);
866 else
867 RTL_W16(IntrMask_8169, 0x0000);
wdenka8bd82d2004-04-18 22:03:42 +0000868
869 RTL_W32(RxMissed, 0);
870
wdenka8bd82d2004-04-18 22:03:42 +0000871 for (i = 0; i < NUM_RX_DESC; i++) {
872 tpc->RxBufferRing[i] = NULL;
873 }
874}
875
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600876void rtl8169_eth_stop(struct udevice *dev)
877{
Eugen Hristevbcbb64b2023-04-25 16:06:58 +0300878 rtl_halt_common(dev);
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600879}
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600880
Thierry Redingb6054b52019-04-16 18:20:29 +0200881static int rtl8169_write_hwaddr(struct udevice *dev)
882{
Simon Glassc69cda22020-12-03 16:55:20 -0700883 struct eth_pdata *plat = dev_get_plat(dev);
Thierry Redingb6054b52019-04-16 18:20:29 +0200884 unsigned int i;
885
886 RTL_W8(Cfg9346, Cfg9346_Unlock);
887
888 for (i = 0; i < MAC_ADDR_LEN; i++)
889 RTL_W8(MAC0 + i, plat->enetaddr[i]);
890
891 RTL_W8(Cfg9346, Cfg9346_Lock);
892
893 return 0;
894}
Thierry Redingb6054b52019-04-16 18:20:29 +0200895
wdenka8bd82d2004-04-18 22:03:42 +0000896/**************************************************************************
897INIT - Look for an adapter, this routine's visible to the outside
898***************************************************************************/
899
900#define board_found 1
901#define valid_link 0
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600902static int rtl_init(unsigned long dev_ioaddr, const char *name,
903 unsigned char *enetaddr)
wdenka8bd82d2004-04-18 22:03:42 +0000904{
905 static int board_idx = -1;
wdenka8bd82d2004-04-18 22:03:42 +0000906 int i, rc;
907 int option = -1, Cap10_100 = 0, Cap1000 = 0;
908
909#ifdef DEBUG_RTL8169
910 printf ("%s\n", __FUNCTION__);
911#endif
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600912 ioaddr = dev_ioaddr;
wdenka8bd82d2004-04-18 22:03:42 +0000913
914 board_idx++;
915
wdenka8bd82d2004-04-18 22:03:42 +0000916 /* point to private storage */
917 tpc = &tpx;
918
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600919 rc = rtl8169_init_board(ioaddr, name);
wdenka8bd82d2004-04-18 22:03:42 +0000920 if (rc)
921 return rc;
922
923 /* Get MAC address. FIXME: read EEPROM */
924 for (i = 0; i < MAC_ADDR_LEN; i++)
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600925 enetaddr[i] = RTL_R8(MAC0 + i);
wdenka8bd82d2004-04-18 22:03:42 +0000926
927#ifdef DEBUG_RTL8169
Yoshihiro Shimodadb70b842008-07-09 21:07:34 +0900928 printf("chipset = %d\n", tpc->chipset);
wdenka8bd82d2004-04-18 22:03:42 +0000929 printf("MAC Address");
930 for (i = 0; i < MAC_ADDR_LEN; i++)
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600931 printf(":%02x", enetaddr[i]);
wdenka8bd82d2004-04-18 22:03:42 +0000932 putc('\n');
933#endif
934
935#ifdef DEBUG_RTL8169
936 /* Print out some hardware info */
Simon Glassd0a5a0b2015-07-06 16:47:45 -0600937 printf("%s: at ioaddr 0x%lx\n", name, ioaddr);
wdenka8bd82d2004-04-18 22:03:42 +0000938#endif
939
940 /* if TBI is not endbled */
941 if (!(RTL_R8(PHYstatus) & TBI_Enable)) {
942 int val = mdio_read(PHY_AUTO_NEGO_REG);
943
944 option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
945 /* Force RTL8169 in 10/100/1000 Full/Half mode. */
946 if (option > 0) {
947#ifdef DEBUG_RTL8169
Bin Mengdbe25382016-03-17 23:27:44 -0700948 printf("%s: Force-mode Enabled.\n", name);
wdenka8bd82d2004-04-18 22:03:42 +0000949#endif
950 Cap10_100 = 0, Cap1000 = 0;
951 switch (option) {
952 case _10_Half:
953 Cap10_100 = PHY_Cap_10_Half;
954 Cap1000 = PHY_Cap_Null;
955 break;
956 case _10_Full:
957 Cap10_100 = PHY_Cap_10_Full;
958 Cap1000 = PHY_Cap_Null;
959 break;
960 case _100_Half:
961 Cap10_100 = PHY_Cap_100_Half;
962 Cap1000 = PHY_Cap_Null;
963 break;
964 case _100_Full:
965 Cap10_100 = PHY_Cap_100_Full;
966 Cap1000 = PHY_Cap_Null;
967 break;
968 case _1000_Full:
969 Cap10_100 = PHY_Cap_Null;
970 Cap1000 = PHY_Cap_1000_Full;
971 break;
972 default:
973 break;
974 }
975 mdio_write(PHY_AUTO_NEGO_REG, Cap10_100 | (val & 0x1F)); /* leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
976 mdio_write(PHY_1000_CTRL_REG, Cap1000);
977 } else {
978#ifdef DEBUG_RTL8169
979 printf("%s: Auto-negotiation Enabled.\n",
Bin Mengdbe25382016-03-17 23:27:44 -0700980 name);
wdenka8bd82d2004-04-18 22:03:42 +0000981#endif
982 /* enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged */
983 mdio_write(PHY_AUTO_NEGO_REG,
984 PHY_Cap_10_Half | PHY_Cap_10_Full |
985 PHY_Cap_100_Half | PHY_Cap_100_Full |
986 (val & 0x1F));
987
988 /* enable 1000 Full Mode */
989 mdio_write(PHY_1000_CTRL_REG, PHY_Cap_1000_Full);
990
991 }
992
993 /* Enable auto-negotiation and restart auto-nigotiation */
994 mdio_write(PHY_CTRL_REG,
995 PHY_Enable_Auto_Nego | PHY_Restart_Auto_Nego);
996 udelay(100);
997
998 /* wait for auto-negotiation process */
999 for (i = 10000; i > 0; i--) {
1000 /* check if auto-negotiation complete */
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +01001001 if (mdio_read(PHY_STAT_REG) & PHY_Auto_Nego_Comp) {
wdenka8bd82d2004-04-18 22:03:42 +00001002 udelay(100);
1003 option = RTL_R8(PHYstatus);
1004 if (option & _1000bpsF) {
1005#ifdef DEBUG_RTL8169
1006 printf("%s: 1000Mbps Full-duplex operation.\n",
Bin Mengdbe25382016-03-17 23:27:44 -07001007 name);
wdenka8bd82d2004-04-18 22:03:42 +00001008#endif
1009 } else {
1010#ifdef DEBUG_RTL8169
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +01001011 printf("%s: %sMbps %s-duplex operation.\n",
Bin Mengdbe25382016-03-17 23:27:44 -07001012 name,
Guennadi Liakhovetski6a5e1d72007-11-20 13:14:20 +01001013 (option & _100bps) ? "100" :
1014 "10",
1015 (option & FullDup) ? "Full" :
1016 "Half");
wdenka8bd82d2004-04-18 22:03:42 +00001017#endif
1018 }
1019 break;
1020 } else {
1021 udelay(100);
1022 }
1023 } /* end for-loop to wait for auto-negotiation process */
1024
1025 } else {
1026 udelay(100);
1027#ifdef DEBUG_RTL8169
1028 printf
1029 ("%s: 1000Mbps Full-duplex operation, TBI Link %s!\n",
Bin Mengdbe25382016-03-17 23:27:44 -07001030 name,
wdenka8bd82d2004-04-18 22:03:42 +00001031 (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed");
1032#endif
1033 }
1034
Thierry Redingdad3ba02014-12-09 22:25:25 -07001035
Thierry Redingd58acdc2014-12-09 22:25:26 -07001036 tpc->RxDescArray = rtl_alloc_descs(NUM_RX_DESC);
1037 if (!tpc->RxDescArray)
1038 return -ENOMEM;
1039
1040 tpc->TxDescArray = rtl_alloc_descs(NUM_TX_DESC);
1041 if (!tpc->TxDescArray)
1042 return -ENOMEM;
1043
1044 return 0;
wdenka8bd82d2004-04-18 22:03:42 +00001045}
1046
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001047static int rtl8169_eth_probe(struct udevice *dev)
1048{
Simon Glass8a8d24b2020-12-03 16:55:23 -07001049 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001050 struct rtl8169_private *priv = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -07001051 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001052 int region;
1053 int ret;
1054
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001055 switch (pplat->device) {
Eugen Hristevbcbb64b2023-04-25 16:06:58 +03001056 case 0x8125:
Minda Chenff8590a2023-07-20 19:37:28 +08001057 case 0x8161:
1058 case 0x8168:
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001059 region = 2;
1060 break;
1061 default:
1062 region = 1;
1063 break;
1064 }
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001065
Eugen Hristevbcbb64b2023-04-25 16:06:58 +03001066 priv->iobase = (ulong)dm_pci_map_bar(dev,
1067 PCI_BASE_ADDRESS_0 + region * 4,
1068 0, 0,
1069 PCI_REGION_TYPE, PCI_REGION_MEM);
1070
1071 debug("rtl8169: REALTEK RTL8169 @0x%lx\n", priv->iobase);
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001072 ret = rtl_init(priv->iobase, dev->name, plat->enetaddr);
1073 if (ret < 0) {
1074 printf(pr_fmt("failed to initialize card: %d\n"), ret);
1075 return ret;
1076 }
1077
Tom Warrena7a435e2020-03-26 15:59:13 -07001078 /*
1079 * WAR for DHCP failure after rebooting from kernel.
1080 * Clear RxDv_Gated_En bit which was set by kernel driver.
1081 * Without this, U-Boot can't get an IP via DHCP.
1082 * Register (FuncEvent, aka MISC) and RXDV_GATED_EN bit are from
1083 * the r8169.c kernel driver.
1084 */
1085
1086 u32 val = RTL_R32(FuncEvent);
1087 debug("%s: FuncEvent/Misc (0xF0) = 0x%08X\n", __func__, val);
1088 val &= ~RxDv_Gated_En;
1089 RTL_W32(FuncEvent, val);
1090
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001091 return 0;
1092}
1093
1094static const struct eth_ops rtl8169_eth_ops = {
1095 .start = rtl8169_eth_start,
1096 .send = rtl8169_eth_send,
1097 .recv = rtl8169_eth_recv,
1098 .stop = rtl8169_eth_stop,
Thierry Redingb6054b52019-04-16 18:20:29 +02001099 .write_hwaddr = rtl8169_write_hwaddr,
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001100};
1101
1102static const struct udevice_id rtl8169_eth_ids[] = {
1103 { .compatible = "realtek,rtl8169" },
1104 { }
1105};
1106
1107U_BOOT_DRIVER(eth_rtl8169) = {
1108 .name = "eth_rtl8169",
1109 .id = UCLASS_ETH,
1110 .of_match = rtl8169_eth_ids,
1111 .probe = rtl8169_eth_probe,
1112 .ops = &rtl8169_eth_ops,
Simon Glass41575d82020-12-03 16:55:17 -07001113 .priv_auto = sizeof(struct rtl8169_private),
Simon Glasscaa4daa2020-12-03 16:55:18 -07001114 .plat_auto = sizeof(struct eth_pdata),
Simon Glassd0a5a0b2015-07-06 16:47:45 -06001115};
1116
1117U_BOOT_PCI_DEVICE(eth_rtl8169, supported);