Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 2 | /* |
| 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 | * |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 15 | * 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 Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 38 | /* |
| 39 | * 26 August 2006 Mihai Georgian <u-boot@linuxnotincluded.org.uk> |
| 40 | * Modified to use le32_to_cpu and cpu_to_le32 properly |
| 41 | */ |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 42 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 43 | #include <cpu_func.h> |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 44 | #include <dm.h> |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 45 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 46 | #include <log.h> |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 47 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 48 | #include <memalign.h> |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 49 | #include <net.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 50 | #include <asm/cache.h> |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 51 | #include <asm/io.h> |
| 52 | #include <pci.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 53 | #include <linux/delay.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 54 | #include <linux/printk.h> |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 55 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 56 | #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 Reding | 744152f | 2015-03-20 12:41:21 +0100 | [diff] [blame] | 63 | static unsigned long ioaddr; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 64 | |
| 65 | /* Condensed operations for readability. */ |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 66 | #define currticks() get_timer(0) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 67 | |
| 68 | /* media options */ |
| 69 | #define MAX_UNITS 8 |
| 70 | static 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 Reding | c94bbfd | 2014-12-09 22:25:24 -0700 | [diff] [blame] | 88 | #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 |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 93 | #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 Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 99 | /* write/read MMIO register. Notice: {read,write}[wl] do the necessary swapping */ |
Minda Chen | a6a0d6a | 2023-07-20 19:37:26 +0800 | [diff] [blame] | 100 | #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))) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 106 | |
Thierry Reding | 744152f | 2015-03-20 12:41:21 +0100 | [diff] [blame] | 107 | #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 Shimoda | d65e34d | 2009-02-25 14:27:29 +0900 | [diff] [blame] | 111 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 112 | enum RTL8169_registers { |
| 113 | MAC0 = 0, /* Ethernet hardware address. */ |
| 114 | MAR0 = 8, /* Multicast filter. */ |
Yoshihiro Shimoda | db70b84 | 2008-07-09 21:07:34 +0900 | [diff] [blame] | 115 | TxDescStartAddrLow = 0x20, |
| 116 | TxDescStartAddrHigh = 0x24, |
| 117 | TxHDescStartAddrLow = 0x28, |
| 118 | TxHDescStartAddrHigh = 0x2c, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 119 | FLASH = 0x30, |
| 120 | ERSR = 0x36, |
| 121 | ChipCmd = 0x37, |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 122 | TxPoll_8169 = 0x38, |
| 123 | IntrMask_8169 = 0x3C, |
| 124 | IntrStatus_8169 = 0x3E, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 125 | 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 Shimoda | db70b84 | 2008-07-09 21:07:34 +0900 | [diff] [blame] | 143 | RxDescStartAddrLow = 0xE4, |
| 144 | RxDescStartAddrHigh = 0xE8, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 145 | EarlyTxThres = 0xEC, |
| 146 | FuncEvent = 0xF0, |
| 147 | FuncEventMask = 0xF4, |
| 148 | FuncPresetState = 0xF8, |
| 149 | FuncForceEvent = 0xFC, |
| 150 | }; |
| 151 | |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 152 | enum RTL8125_registers { |
| 153 | IntrMask_8125 = 0x38, |
| 154 | IntrStatus_8125 = 0x3C, |
| 155 | TxPoll_8125 = 0x90, |
| 156 | }; |
| 157 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 158 | enum 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 Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 225 | PHY_Auto_Nego_Comp = 0x0020, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 226 | |
| 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 Warren | a7a435e | 2020-03-26 15:59:13 -0700 | [diff] [blame] | 247 | |
| 248 | /* FuncEvent/Misc */ |
| 249 | RxDv_Gated_En = 0x80000, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | static 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 Iwamatsu | d75469d | 2008-03-08 09:25:49 +0900 | [diff] [blame] | 259 | {"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 Reding | 945dd96 | 2019-09-11 19:19:06 +0200 | [diff] [blame] | 266 | {"RTL-8168c/8111c", 0x3c, 0xff7e1880,}, |
Thierry Reding | 2287286 | 2013-09-20 16:03:43 +0200 | [diff] [blame] | 267 | {"RTL-8168d/8111d", 0x28, 0xff7e1880,}, |
Thierry Reding | 65a6691 | 2013-09-20 16:03:44 +0200 | [diff] [blame] | 268 | {"RTL-8168evl/8111evl", 0x2e, 0xff7e1880,}, |
Thierry Reding | cc0856c | 2014-12-09 22:25:27 -0700 | [diff] [blame] | 269 | {"RTL-8168/8111g", 0x4c, 0xff7e1880,}, |
Nobuhiro Iwamatsu | d75469d | 2008-03-08 09:25:49 +0900 | [diff] [blame] | 270 | {"RTL-8101e", 0x34, 0xff7e1880,}, |
| 271 | {"RTL-8100e", 0x32, 0xff7e1880,}, |
Thierry Reding | cdd69ac | 2019-04-16 18:20:30 +0200 | [diff] [blame] | 272 | {"RTL-8168h/8111h", 0x54, 0xff7e1880,}, |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 273 | {"RTL-8125B", 0x64, 0xff7e1880,}, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | enum _DescStatusBit { |
| 277 | OWNbit = 0x80000000, |
| 278 | EORbit = 0x40000000, |
| 279 | FSbit = 0x20000000, |
| 280 | LSbit = 0x10000000, |
| 281 | }; |
| 282 | |
| 283 | struct TxDesc { |
| 284 | u32 status; |
| 285 | u32 vlan_tag; |
| 286 | u32 buf_addr; |
| 287 | u32 buf_Haddr; |
| 288 | }; |
| 289 | |
| 290 | struct RxDesc { |
| 291 | u32 status; |
| 292 | u32 vlan_tag; |
| 293 | u32 buf_addr; |
| 294 | u32 buf_Haddr; |
| 295 | }; |
| 296 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 297 | static unsigned char rxdata[RX_BUF_LEN]; |
| 298 | |
Thierry Reding | dad3ba0 | 2014-12-09 22:25:25 -0700 | [diff] [blame] | 299 | #define RTL8169_DESC_SIZE 16 |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 300 | |
Thierry Reding | dad3ba0 | 2014-12-09 22:25:25 -0700 | [diff] [blame] | 301 | #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 Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 312 | * |
| 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 Chen | 3094845 | 2023-07-20 19:37:27 +0800 | [diff] [blame] | 315 | * |
| 316 | * Hardware maintain D-cache coherency in RISC-V architecture. |
Thierry Reding | dad3ba0 | 2014-12-09 22:25:25 -0700 | [diff] [blame] | 317 | */ |
| 318 | #if RTL8169_DESC_SIZE < ARCH_DMA_MINALIGN |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 319 | #if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \ |
Minda Chen | 3094845 | 2023-07-20 19:37:27 +0800 | [diff] [blame] | 320 | !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86) && !defined(CONFIG_RISCV) |
Thierry Reding | dad3ba0 | 2014-12-09 22:25:25 -0700 | [diff] [blame] | 321 | #warning cache-line size is larger than descriptor size |
| 322 | #endif |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 323 | #endif |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 324 | |
Thierry Reding | dad3ba0 | 2014-12-09 22:25:25 -0700 | [diff] [blame] | 325 | /* |
| 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 | */ |
| 329 | DEFINE_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 | */ |
| 335 | DEFINE_ALIGN_BUFFER(u8, rxb, NUM_RX_DESC * RX_BUF_SIZE, RTL8169_ALIGN); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 336 | |
| 337 | struct rtl8169_private { |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 338 | ulong iobase; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 339 | 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; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 344 | 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 | |
| 351 | static struct rtl8169_private *tpc; |
| 352 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 353 | static const unsigned int rtl8169_rx_config = |
| 354 | (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift); |
| 355 | |
| 356 | static struct pci_device_id supported[] = { |
Minda Chen | ff8590a | 2023-07-20 19:37:28 +0800 | [diff] [blame] | 357 | { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8125) }, |
| 358 | { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161) }, |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 359 | { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167) }, |
| 360 | { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168) }, |
| 361 | { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169) }, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 362 | {} |
| 363 | }; |
| 364 | |
| 365 | void 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 | |
| 382 | int 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 Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 401 | static int rtl8169_init_board(unsigned long dev_iobase, const char *name) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 402 | { |
| 403 | int i; |
| 404 | u32 tmp; |
| 405 | |
| 406 | #ifdef DEBUG_RTL8169 |
| 407 | printf ("%s\n", __FUNCTION__); |
| 408 | #endif |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 409 | ioaddr = dev_iobase; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 410 | |
| 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 Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 433 | printf("PCI device %s: unknown chip version, assuming RTL-8169\n", |
| 434 | name); |
Wolfgang Denk | 06c53be | 2008-07-10 13:16:09 +0200 | [diff] [blame] | 435 | printf("PCI device: TxConfig = 0x%lX\n", (unsigned long) RTL_R32(TxConfig)); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 436 | tpc->chipset = 0; |
| 437 | |
| 438 | match: |
| 439 | return 0; |
| 440 | } |
| 441 | |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 442 | /* |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 443 | * 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 | */ |
| 460 | static 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 Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 472 | * Cache maintenance functions. These are simple wrappers around the more |
| 473 | * general purpose flush_cache() and invalidate_dcache_range() functions. |
| 474 | */ |
| 475 | |
| 476 | static void rtl_inval_rx_desc(struct RxDesc *desc) |
| 477 | { |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 478 | #ifndef CONFIG_SYS_NONCACHED_MEMORY |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 479 | 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 Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 483 | #endif |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static void rtl_flush_rx_desc(struct RxDesc *desc) |
| 487 | { |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 488 | #ifndef CONFIG_SYS_NONCACHED_MEMORY |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 489 | flush_cache((unsigned long)desc, sizeof(*desc)); |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 490 | #endif |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static void rtl_inval_tx_desc(struct TxDesc *desc) |
| 494 | { |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 495 | #ifndef CONFIG_SYS_NONCACHED_MEMORY |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 496 | 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 Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 500 | #endif |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static void rtl_flush_tx_desc(struct TxDesc *desc) |
| 504 | { |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 505 | #ifndef CONFIG_SYS_NONCACHED_MEMORY |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 506 | flush_cache((unsigned long)desc, sizeof(*desc)); |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 507 | #endif |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static 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 | |
| 518 | static void rtl_flush_buffer(void *buf, size_t size) |
| 519 | { |
| 520 | flush_cache((unsigned long)buf, size); |
| 521 | } |
| 522 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 523 | /************************************************************************** |
| 524 | RECV - Receive a frame |
| 525 | ***************************************************************************/ |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 526 | static int rtl_recv_common(struct udevice *dev, unsigned long dev_iobase, |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 527 | uchar **packetp) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 528 | { |
| 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 Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 532 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 533 | int cur_rx; |
| 534 | int length = 0; |
| 535 | |
| 536 | #ifdef DEBUG_RTL8169_RX |
| 537 | printf ("%s\n", __FUNCTION__); |
| 538 | #endif |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 539 | ioaddr = dev_iobase; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 540 | |
| 541 | cur_rx = tpc->cur_rx; |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 542 | |
| 543 | rtl_inval_rx_desc(&tpc->RxDescArray[cur_rx]); |
| 544 | |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 545 | if ((le32_to_cpu(tpc->RxDescArray[cur_rx].status) & OWNbit) == 0) { |
| 546 | if (!(le32_to_cpu(tpc->RxDescArray[cur_rx].status) & RxRES)) { |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 547 | length = (int) (le32_to_cpu(tpc->RxDescArray[cur_rx]. |
| 548 | status) & 0x00001FFF) - 4; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 549 | |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 550 | rtl_inval_buffer(tpc->RxBufferRing[cur_rx], length); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 551 | memcpy(rxdata, tpc->RxBufferRing[cur_rx], length); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 552 | |
| 553 | if (cur_rx == NUM_RX_DESC - 1) |
| 554 | tpc->RxDescArray[cur_rx].status = |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 555 | cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 556 | else |
| 557 | tpc->RxDescArray[cur_rx].status = |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 558 | cpu_to_le32(OWNbit + RX_BUF_SIZE); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 559 | tpc->RxDescArray[cur_rx].buf_addr = cpu_to_le32( |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 560 | dm_pci_mem_to_phys(dev, |
| 561 | (pci_addr_t)(unsigned long) |
| 562 | tpc->RxBufferRing[cur_rx])); |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 563 | rtl_flush_rx_desc(&tpc->RxDescArray[cur_rx]); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 564 | *packetp = rxdata; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 565 | } else { |
| 566 | puts("Error Rx"); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 567 | length = -EIO; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 568 | } |
| 569 | cur_rx = (cur_rx + 1) % NUM_RX_DESC; |
| 570 | tpc->cur_rx = cur_rx; |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 571 | return length; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 572 | |
Nobuhiro Iwamatsu | d75469d | 2008-03-08 09:25:49 +0900 | [diff] [blame] | 573 | } else { |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 574 | u32 IntrStatus = IntrStatus_8169; |
| 575 | |
| 576 | if (pplat->device == 0x8125) |
| 577 | IntrStatus = IntrStatus_8125; |
Nobuhiro Iwamatsu | d75469d | 2008-03-08 09:25:49 +0900 | [diff] [blame] | 578 | ushort sts = RTL_R8(IntrStatus); |
| 579 | RTL_W8(IntrStatus, sts & ~(TxErr | RxErr | SYSErr)); |
| 580 | udelay(100); /* wait */ |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 581 | } |
| 582 | tpc->cur_rx = cur_rx; |
| 583 | return (0); /* initially as this is called to flush the input */ |
| 584 | } |
| 585 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 586 | int rtl8169_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 587 | { |
| 588 | struct rtl8169_private *priv = dev_get_priv(dev); |
| 589 | |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 590 | return rtl_recv_common(dev, priv->iobase, packetp); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 591 | } |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 592 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 593 | #define HZ 1000 |
| 594 | /************************************************************************** |
| 595 | SEND - Transmit a frame |
| 596 | ***************************************************************************/ |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 597 | static int rtl_send_common(struct udevice *dev, unsigned long dev_iobase, |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 598 | void *packet, int length) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 599 | { |
| 600 | /* send the packet to destination */ |
| 601 | |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 602 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 603 | u32 to; |
| 604 | u8 *ptxb; |
| 605 | int entry = tpc->cur_tx % NUM_TX_DESC; |
| 606 | u32 len = length; |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 607 | int ret; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 608 | |
| 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 Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 615 | ioaddr = dev_iobase; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 616 | |
| 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 Chubb | 7377647 | 2016-09-14 01:29:03 +0000 | [diff] [blame] | 624 | rtl_flush_buffer(ptxb, ALIGN(len, RTL8169_ALIGN)); |
| 625 | |
Yoshihiro Shimoda | db70b84 | 2008-07-09 21:07:34 +0900 | [diff] [blame] | 626 | tpc->TxDescArray[entry].buf_Haddr = 0; |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 627 | tpc->TxDescArray[entry].buf_addr = cpu_to_le32( |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 628 | dm_pci_mem_to_phys(dev, (pci_addr_t)(unsigned long)ptxb)); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 629 | if (entry != (NUM_TX_DESC - 1)) { |
| 630 | tpc->TxDescArray[entry].status = |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 631 | cpu_to_le32((OWNbit | FSbit | LSbit) | |
| 632 | ((len > ETH_ZLEN) ? len : ETH_ZLEN)); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 633 | } else { |
| 634 | tpc->TxDescArray[entry].status = |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 635 | cpu_to_le32((OWNbit | EORbit | FSbit | LSbit) | |
| 636 | ((len > ETH_ZLEN) ? len : ETH_ZLEN)); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 637 | } |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 638 | rtl_flush_tx_desc(&tpc->TxDescArray[entry]); |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 639 | if (pplat->device == 0x8125) |
| 640 | RTL_W8(TxPoll_8125, 0x1); /* set polling bit */ |
| 641 | else |
| 642 | RTL_W8(TxPoll_8169, 0x40); /* set polling bit */ |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 643 | |
| 644 | tpc->cur_tx++; |
| 645 | to = currticks() + TX_TIMEOUT; |
Yoshihiro Shimoda | d4c02e6 | 2009-02-25 14:27:24 +0900 | [diff] [blame] | 646 | do { |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 647 | rtl_inval_tx_desc(&tpc->TxDescArray[entry]); |
Yoshihiro Shimoda | d4c02e6 | 2009-02-25 14:27:24 +0900 | [diff] [blame] | 648 | } while ((le32_to_cpu(tpc->TxDescArray[entry].status) & OWNbit) |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 649 | && (currticks() < to)); /* wait */ |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 650 | |
| 651 | if (currticks() >= to) { |
| 652 | #ifdef DEBUG_RTL8169_TX |
Thierry Reding | 7a36b9c | 2013-09-20 16:03:41 +0200 | [diff] [blame] | 653 | puts("tx timeout/error\n"); |
| 654 | printf("%s elapsed time : %lu\n", __func__, currticks()-stime); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 655 | #endif |
Oleksandr Tymoshenko | 4c64c4d | 2016-07-01 13:22:00 -0700 | [diff] [blame] | 656 | ret = -ETIMEDOUT; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 657 | } else { |
| 658 | #ifdef DEBUG_RTL8169_TX |
| 659 | puts("tx done\n"); |
| 660 | #endif |
Oleksandr Tymoshenko | 4c64c4d | 2016-07-01 13:22:00 -0700 | [diff] [blame] | 661 | ret = 0; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 662 | } |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 663 | /* Delay to make net console (nc) work properly */ |
| 664 | udelay(20); |
| 665 | return ret; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 668 | int rtl8169_eth_send(struct udevice *dev, void *packet, int length) |
| 669 | { |
| 670 | struct rtl8169_private *priv = dev_get_priv(dev); |
| 671 | |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 672 | return rtl_send_common(dev, priv->iobase, packet, length); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 673 | } |
| 674 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 675 | static void rtl8169_set_rx_mode(void) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 676 | { |
| 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 Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 698 | static void rtl8169_hw_start(struct udevice *dev) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 699 | { |
| 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 Shimoda | db70b84 | 2008-07-09 21:07:34 +0900 | [diff] [blame] | 721 | |
| 722 | /* RTL-8169sb/8110sb or previous version */ |
| 723 | if (tpc->chipset <= 5) |
| 724 | RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb); |
| 725 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 726 | 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 Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 743 | RTL_W32(TxDescStartAddrLow, dm_pci_mem_to_phys(dev, |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 744 | (pci_addr_t)(unsigned long)tpc->TxDescArray)); |
Yoshihiro Shimoda | db70b84 | 2008-07-09 21:07:34 +0900 | [diff] [blame] | 745 | RTL_W32(TxDescStartAddrHigh, (unsigned long)0); |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 746 | RTL_W32(RxDescStartAddrLow, dm_pci_mem_to_phys( |
| 747 | dev, (pci_addr_t)(unsigned long)tpc->RxDescArray)); |
Yoshihiro Shimoda | db70b84 | 2008-07-09 21:07:34 +0900 | [diff] [blame] | 748 | 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 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 754 | RTL_W8(Cfg9346, Cfg9346_Lock); |
| 755 | udelay(10); |
| 756 | |
| 757 | RTL_W32(RxMissed, 0); |
| 758 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 759 | rtl8169_set_rx_mode(); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 760 | |
| 761 | /* no early-rx interrupts */ |
| 762 | RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000); |
| 763 | |
| 764 | #ifdef DEBUG_RTL8169 |
Thierry Reding | 7a36b9c | 2013-09-20 16:03:41 +0200 | [diff] [blame] | 765 | printf("%s elapsed time : %lu\n", __func__, currticks()-stime); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 766 | #endif |
| 767 | } |
| 768 | |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 769 | static void rtl8169_init_ring(struct udevice *dev) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 770 | { |
| 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 Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 791 | cpu_to_le32((OWNbit | EORbit) + RX_BUF_SIZE); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 792 | else |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 793 | tpc->RxDescArray[i].status = |
| 794 | cpu_to_le32(OWNbit + RX_BUF_SIZE); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 795 | |
| 796 | tpc->RxBufferRing[i] = &rxb[i * RX_BUF_SIZE]; |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 797 | tpc->RxDescArray[i].buf_addr = cpu_to_le32(dm_pci_mem_to_phys( |
| 798 | dev, (pci_addr_t)(unsigned long)tpc->RxBufferRing[i])); |
Thierry Reding | 22ece0e | 2013-09-20 16:03:42 +0200 | [diff] [blame] | 799 | rtl_flush_rx_desc(&tpc->RxDescArray[i]); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | #ifdef DEBUG_RTL8169 |
Thierry Reding | 7a36b9c | 2013-09-20 16:03:41 +0200 | [diff] [blame] | 803 | printf("%s elapsed time : %lu\n", __func__, currticks()-stime); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 804 | #endif |
| 805 | } |
| 806 | |
Stephen Warren | dad7b74 | 2016-04-26 15:29:00 -0600 | [diff] [blame] | 807 | static void rtl8169_common_start(struct udevice *dev, unsigned char *enetaddr, |
| 808 | unsigned long dev_iobase) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 809 | { |
| 810 | int i; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 811 | |
| 812 | #ifdef DEBUG_RTL8169 |
| 813 | int stime = currticks(); |
| 814 | printf ("%s\n", __FUNCTION__); |
| 815 | #endif |
| 816 | |
Stephen Warren | dad7b74 | 2016-04-26 15:29:00 -0600 | [diff] [blame] | 817 | ioaddr = dev_iobase; |
| 818 | |
Simon Glass | 552ddbe | 2015-11-29 13:18:04 -0700 | [diff] [blame] | 819 | rtl8169_init_ring(dev); |
| 820 | rtl8169_hw_start(dev); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 821 | /* 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 Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 826 | 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]; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 832 | |
| 833 | #ifdef DEBUG_RTL8169 |
Thierry Reding | 7a36b9c | 2013-09-20 16:03:41 +0200 | [diff] [blame] | 834 | printf("%s elapsed time : %lu\n", __func__, currticks()-stime); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 835 | #endif |
| 836 | } |
| 837 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 838 | static int rtl8169_eth_start(struct udevice *dev) |
| 839 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 840 | struct eth_pdata *plat = dev_get_plat(dev); |
Stephen Warren | dad7b74 | 2016-04-26 15:29:00 -0600 | [diff] [blame] | 841 | struct rtl8169_private *priv = dev_get_priv(dev); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 842 | |
Stephen Warren | dad7b74 | 2016-04-26 15:29:00 -0600 | [diff] [blame] | 843 | rtl8169_common_start(dev, plat->enetaddr, priv->iobase); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 844 | |
| 845 | return 0; |
| 846 | } |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 847 | |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 848 | static void rtl_halt_common(struct udevice *dev) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 849 | { |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 850 | struct rtl8169_private *priv = dev_get_priv(dev); |
| 851 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 852 | int i; |
| 853 | |
| 854 | #ifdef DEBUG_RTL8169 |
| 855 | printf ("%s\n", __FUNCTION__); |
| 856 | #endif |
| 857 | |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 858 | ioaddr = priv->iobase; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 859 | |
| 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 Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 864 | if (pplat->device == 0x8125) |
| 865 | RTL_W16(IntrMask_8125, 0x0000); |
| 866 | else |
| 867 | RTL_W16(IntrMask_8169, 0x0000); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 868 | |
| 869 | RTL_W32(RxMissed, 0); |
| 870 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 871 | for (i = 0; i < NUM_RX_DESC; i++) { |
| 872 | tpc->RxBufferRing[i] = NULL; |
| 873 | } |
| 874 | } |
| 875 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 876 | void rtl8169_eth_stop(struct udevice *dev) |
| 877 | { |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 878 | rtl_halt_common(dev); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 879 | } |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 880 | |
Thierry Reding | b6054b5 | 2019-04-16 18:20:29 +0200 | [diff] [blame] | 881 | static int rtl8169_write_hwaddr(struct udevice *dev) |
| 882 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 883 | struct eth_pdata *plat = dev_get_plat(dev); |
Thierry Reding | b6054b5 | 2019-04-16 18:20:29 +0200 | [diff] [blame] | 884 | 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 Reding | b6054b5 | 2019-04-16 18:20:29 +0200 | [diff] [blame] | 895 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 896 | /************************************************************************** |
| 897 | INIT - Look for an adapter, this routine's visible to the outside |
| 898 | ***************************************************************************/ |
| 899 | |
| 900 | #define board_found 1 |
| 901 | #define valid_link 0 |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 902 | static int rtl_init(unsigned long dev_ioaddr, const char *name, |
| 903 | unsigned char *enetaddr) |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 904 | { |
| 905 | static int board_idx = -1; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 906 | 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 Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 912 | ioaddr = dev_ioaddr; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 913 | |
| 914 | board_idx++; |
| 915 | |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 916 | /* point to private storage */ |
| 917 | tpc = &tpx; |
| 918 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 919 | rc = rtl8169_init_board(ioaddr, name); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 920 | if (rc) |
| 921 | return rc; |
| 922 | |
| 923 | /* Get MAC address. FIXME: read EEPROM */ |
| 924 | for (i = 0; i < MAC_ADDR_LEN; i++) |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 925 | enetaddr[i] = RTL_R8(MAC0 + i); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 926 | |
| 927 | #ifdef DEBUG_RTL8169 |
Yoshihiro Shimoda | db70b84 | 2008-07-09 21:07:34 +0900 | [diff] [blame] | 928 | printf("chipset = %d\n", tpc->chipset); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 929 | printf("MAC Address"); |
| 930 | for (i = 0; i < MAC_ADDR_LEN; i++) |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 931 | printf(":%02x", enetaddr[i]); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 932 | putc('\n'); |
| 933 | #endif |
| 934 | |
| 935 | #ifdef DEBUG_RTL8169 |
| 936 | /* Print out some hardware info */ |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 937 | printf("%s: at ioaddr 0x%lx\n", name, ioaddr); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 938 | #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 Meng | dbe2538 | 2016-03-17 23:27:44 -0700 | [diff] [blame] | 948 | printf("%s: Force-mode Enabled.\n", name); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 949 | #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 Meng | dbe2538 | 2016-03-17 23:27:44 -0700 | [diff] [blame] | 980 | name); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 981 | #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 Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 1001 | if (mdio_read(PHY_STAT_REG) & PHY_Auto_Nego_Comp) { |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 1002 | udelay(100); |
| 1003 | option = RTL_R8(PHYstatus); |
| 1004 | if (option & _1000bpsF) { |
| 1005 | #ifdef DEBUG_RTL8169 |
| 1006 | printf("%s: 1000Mbps Full-duplex operation.\n", |
Bin Meng | dbe2538 | 2016-03-17 23:27:44 -0700 | [diff] [blame] | 1007 | name); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 1008 | #endif |
| 1009 | } else { |
| 1010 | #ifdef DEBUG_RTL8169 |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 1011 | printf("%s: %sMbps %s-duplex operation.\n", |
Bin Meng | dbe2538 | 2016-03-17 23:27:44 -0700 | [diff] [blame] | 1012 | name, |
Guennadi Liakhovetski | 6a5e1d7 | 2007-11-20 13:14:20 +0100 | [diff] [blame] | 1013 | (option & _100bps) ? "100" : |
| 1014 | "10", |
| 1015 | (option & FullDup) ? "Full" : |
| 1016 | "Half"); |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 1017 | #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 Meng | dbe2538 | 2016-03-17 23:27:44 -0700 | [diff] [blame] | 1030 | name, |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 1031 | (RTL_R32(TBICSR) & TBILinkOK) ? "OK" : "Failed"); |
| 1032 | #endif |
| 1033 | } |
| 1034 | |
Thierry Reding | dad3ba0 | 2014-12-09 22:25:25 -0700 | [diff] [blame] | 1035 | |
Thierry Reding | d58acdc | 2014-12-09 22:25:26 -0700 | [diff] [blame] | 1036 | 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; |
wdenk | a8bd82d | 2004-04-18 22:03:42 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1047 | static int rtl8169_eth_probe(struct udevice *dev) |
| 1048 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1049 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1050 | struct rtl8169_private *priv = dev_get_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1051 | struct eth_pdata *plat = dev_get_plat(dev); |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1052 | int region; |
| 1053 | int ret; |
| 1054 | |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1055 | switch (pplat->device) { |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 1056 | case 0x8125: |
Minda Chen | ff8590a | 2023-07-20 19:37:28 +0800 | [diff] [blame] | 1057 | case 0x8161: |
| 1058 | case 0x8168: |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1059 | region = 2; |
| 1060 | break; |
| 1061 | default: |
| 1062 | region = 1; |
| 1063 | break; |
| 1064 | } |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1065 | |
Eugen Hristev | bcbb64b | 2023-04-25 16:06:58 +0300 | [diff] [blame] | 1066 | 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 Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1072 | 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 Warren | a7a435e | 2020-03-26 15:59:13 -0700 | [diff] [blame] | 1078 | /* |
| 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 Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | static 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 Reding | b6054b5 | 2019-04-16 18:20:29 +0200 | [diff] [blame] | 1099 | .write_hwaddr = rtl8169_write_hwaddr, |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1100 | }; |
| 1101 | |
| 1102 | static const struct udevice_id rtl8169_eth_ids[] = { |
| 1103 | { .compatible = "realtek,rtl8169" }, |
| 1104 | { } |
| 1105 | }; |
| 1106 | |
| 1107 | U_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 Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1113 | .priv_auto = sizeof(struct rtl8169_private), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1114 | .plat_auto = sizeof(struct eth_pdata), |
Simon Glass | d0a5a0b | 2015-07-06 16:47:45 -0600 | [diff] [blame] | 1115 | }; |
| 1116 | |
| 1117 | U_BOOT_PCI_DEVICE(eth_rtl8169, supported); |