Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Realtek Semiconductor Corp. All rights reserved. |
| 4 | * |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 5 | */ |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 6 | |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 7 | #include <dm.h> |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 8 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 10 | #include <malloc.h> |
Stefan Roese | 0109440 | 2016-11-22 16:14:23 +0100 | [diff] [blame] | 11 | #include <memalign.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <net.h> |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 13 | #include <usb.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 15 | #include <linux/mii.h> |
| 16 | #include <linux/bitops.h> |
| 17 | #include "usb_ether.h" |
| 18 | #include "r8152.h" |
| 19 | |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 20 | struct r8152_version { |
| 21 | unsigned short tcr; |
| 22 | unsigned short version; |
| 23 | bool gmii; |
| 24 | }; |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 25 | |
Philipp Tomsich | 657bc53 | 2017-06-25 22:47:15 +0200 | [diff] [blame] | 26 | static const struct r8152_version r8152_versions[] = { |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 27 | { 0x4c00, RTL_VER_01, 0 }, |
| 28 | { 0x4c10, RTL_VER_02, 0 }, |
| 29 | { 0x5c00, RTL_VER_03, 1 }, |
| 30 | { 0x5c10, RTL_VER_04, 1 }, |
| 31 | { 0x5c20, RTL_VER_05, 1 }, |
| 32 | { 0x5c30, RTL_VER_06, 1 }, |
| 33 | { 0x4800, RTL_VER_07, 0 }, |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 34 | { 0x6000, RTL_VER_08, 1 }, |
| 35 | { 0x6010, RTL_VER_09, 1 }, |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static |
| 39 | int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 40 | { |
Stefan Roese | 0109440 | 2016-11-22 16:14:23 +0100 | [diff] [blame] | 41 | ALLOC_CACHE_ALIGN_BUFFER(void *, tmp, size); |
| 42 | int ret; |
| 43 | |
| 44 | ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), |
| 45 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 46 | value, index, tmp, size, 500); |
| 47 | memcpy(data, tmp, size); |
| 48 | return ret; |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static |
| 52 | int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 53 | { |
Stefan Roese | 0109440 | 2016-11-22 16:14:23 +0100 | [diff] [blame] | 54 | ALLOC_CACHE_ALIGN_BUFFER(void *, tmp, size); |
| 55 | |
| 56 | memcpy(tmp, data, size); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 57 | return usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), |
| 58 | RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, |
Stefan Roese | 0109440 | 2016-11-22 16:14:23 +0100 | [diff] [blame] | 59 | value, index, tmp, size, 500); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, |
| 63 | void *data, u16 type) |
| 64 | { |
| 65 | u16 burst_size = 64; |
| 66 | int ret; |
| 67 | int txsize; |
| 68 | |
| 69 | /* both size and index must be 4 bytes align */ |
| 70 | if ((size & 3) || !size || (index & 3) || !data) |
| 71 | return -EINVAL; |
| 72 | |
| 73 | if (index + size > 0xffff) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | while (size) { |
| 77 | txsize = min(size, burst_size); |
| 78 | ret = get_registers(tp, index, type, txsize, data); |
| 79 | if (ret < 0) |
| 80 | break; |
| 81 | |
| 82 | index += txsize; |
| 83 | data += txsize; |
| 84 | size -= txsize; |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, |
| 91 | u16 size, void *data, u16 type) |
| 92 | { |
| 93 | int ret; |
| 94 | u16 byteen_start, byteen_end, byte_en_to_hw; |
| 95 | u16 burst_size = 512; |
| 96 | int txsize; |
| 97 | |
| 98 | /* both size and index must be 4 bytes align */ |
| 99 | if ((size & 3) || !size || (index & 3) || !data) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | if (index + size > 0xffff) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | byteen_start = byteen & BYTE_EN_START_MASK; |
| 106 | byteen_end = byteen & BYTE_EN_END_MASK; |
| 107 | |
| 108 | byte_en_to_hw = byteen_start | (byteen_start << 4); |
| 109 | ret = set_registers(tp, index, type | byte_en_to_hw, 4, data); |
| 110 | if (ret < 0) |
| 111 | return ret; |
| 112 | |
| 113 | index += 4; |
| 114 | data += 4; |
| 115 | size -= 4; |
| 116 | |
| 117 | if (size) { |
| 118 | size -= 4; |
| 119 | |
| 120 | while (size) { |
| 121 | txsize = min(size, burst_size); |
| 122 | |
| 123 | ret = set_registers(tp, index, |
| 124 | type | BYTE_EN_DWORD, |
| 125 | txsize, data); |
| 126 | if (ret < 0) |
| 127 | return ret; |
| 128 | |
| 129 | index += txsize; |
| 130 | data += txsize; |
| 131 | size -= txsize; |
| 132 | } |
| 133 | |
| 134 | byte_en_to_hw = byteen_end | (byteen_end >> 4); |
| 135 | ret = set_registers(tp, index, type | byte_en_to_hw, 4, data); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 144 | { |
| 145 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); |
| 146 | } |
| 147 | |
| 148 | int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 149 | { |
| 150 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); |
| 151 | } |
| 152 | |
| 153 | int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 154 | { |
| 155 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB); |
| 156 | } |
| 157 | |
| 158 | int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 159 | { |
| 160 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); |
| 161 | } |
| 162 | |
| 163 | u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) |
| 164 | { |
| 165 | __le32 data; |
| 166 | |
| 167 | generic_ocp_read(tp, index, sizeof(data), &data, type); |
| 168 | |
| 169 | return __le32_to_cpu(data); |
| 170 | } |
| 171 | |
| 172 | void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 173 | { |
| 174 | __le32 tmp = __cpu_to_le32(data); |
| 175 | |
| 176 | generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); |
| 177 | } |
| 178 | |
| 179 | u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) |
| 180 | { |
| 181 | u32 data; |
| 182 | __le32 tmp; |
| 183 | u8 shift = index & 2; |
| 184 | |
| 185 | index &= ~3; |
| 186 | |
| 187 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
| 188 | |
| 189 | data = __le32_to_cpu(tmp); |
| 190 | data >>= (shift * 8); |
| 191 | data &= 0xffff; |
| 192 | |
| 193 | return data; |
| 194 | } |
| 195 | |
| 196 | void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 197 | { |
| 198 | u32 mask = 0xffff; |
| 199 | __le32 tmp; |
| 200 | u16 byen = BYTE_EN_WORD; |
| 201 | u8 shift = index & 2; |
| 202 | |
| 203 | data &= mask; |
| 204 | |
| 205 | if (index & 2) { |
| 206 | byen <<= shift; |
| 207 | mask <<= (shift * 8); |
| 208 | data <<= (shift * 8); |
| 209 | index &= ~3; |
| 210 | } |
| 211 | |
| 212 | tmp = __cpu_to_le32(data); |
| 213 | |
| 214 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
| 215 | } |
| 216 | |
| 217 | u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) |
| 218 | { |
| 219 | u32 data; |
| 220 | __le32 tmp; |
| 221 | u8 shift = index & 3; |
| 222 | |
| 223 | index &= ~3; |
| 224 | |
| 225 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
| 226 | |
| 227 | data = __le32_to_cpu(tmp); |
| 228 | data >>= (shift * 8); |
| 229 | data &= 0xff; |
| 230 | |
| 231 | return data; |
| 232 | } |
| 233 | |
| 234 | void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 235 | { |
| 236 | u32 mask = 0xff; |
| 237 | __le32 tmp; |
| 238 | u16 byen = BYTE_EN_BYTE; |
| 239 | u8 shift = index & 3; |
| 240 | |
| 241 | data &= mask; |
| 242 | |
| 243 | if (index & 3) { |
| 244 | byen <<= shift; |
| 245 | mask <<= (shift * 8); |
| 246 | data <<= (shift * 8); |
| 247 | index &= ~3; |
| 248 | } |
| 249 | |
| 250 | tmp = __cpu_to_le32(data); |
| 251 | |
| 252 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
| 253 | } |
| 254 | |
| 255 | u16 ocp_reg_read(struct r8152 *tp, u16 addr) |
| 256 | { |
| 257 | u16 ocp_base, ocp_index; |
| 258 | |
| 259 | ocp_base = addr & 0xf000; |
| 260 | if (ocp_base != tp->ocp_base) { |
| 261 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 262 | tp->ocp_base = ocp_base; |
| 263 | } |
| 264 | |
| 265 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 266 | return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); |
| 267 | } |
| 268 | |
| 269 | void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) |
| 270 | { |
| 271 | u16 ocp_base, ocp_index; |
| 272 | |
| 273 | ocp_base = addr & 0xf000; |
| 274 | if (ocp_base != tp->ocp_base) { |
| 275 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 276 | tp->ocp_base = ocp_base; |
| 277 | } |
| 278 | |
| 279 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 280 | ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); |
| 281 | } |
| 282 | |
| 283 | static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) |
| 284 | { |
| 285 | ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); |
| 286 | } |
| 287 | |
| 288 | static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) |
| 289 | { |
| 290 | return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); |
| 291 | } |
| 292 | |
| 293 | void sram_write(struct r8152 *tp, u16 addr, u16 data) |
| 294 | { |
| 295 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 296 | ocp_reg_write(tp, OCP_SRAM_DATA, data); |
| 297 | } |
| 298 | |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 299 | static u16 sram_read(struct r8152 *tp, u16 addr) |
| 300 | { |
| 301 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 302 | return ocp_reg_read(tp, OCP_SRAM_DATA); |
| 303 | } |
| 304 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 305 | int r8152_wait_for_bit(struct r8152 *tp, bool ocp_reg, u16 type, u16 index, |
| 306 | const u32 mask, bool set, unsigned int timeout) |
| 307 | { |
| 308 | u32 val; |
| 309 | |
| 310 | while (--timeout) { |
| 311 | if (ocp_reg) |
| 312 | val = ocp_reg_read(tp, index); |
| 313 | else |
| 314 | val = ocp_read_dword(tp, type, index); |
| 315 | |
| 316 | if (!set) |
| 317 | val = ~val; |
| 318 | |
| 319 | if ((val & mask) == mask) |
| 320 | return 0; |
| 321 | |
| 322 | mdelay(1); |
| 323 | } |
| 324 | |
| 325 | debug("%s: Timeout (index=%04x mask=%08x timeout=%d)\n", |
| 326 | __func__, index, mask, timeout); |
| 327 | |
| 328 | return -ETIMEDOUT; |
| 329 | } |
| 330 | |
| 331 | static void r8152b_reset_packet_filter(struct r8152 *tp) |
| 332 | { |
| 333 | u32 ocp_data; |
| 334 | |
| 335 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); |
| 336 | ocp_data &= ~FMC_FCR_MCU_EN; |
| 337 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 338 | ocp_data |= FMC_FCR_MCU_EN; |
| 339 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 340 | } |
| 341 | |
| 342 | static void rtl8152_wait_fifo_empty(struct r8152 *tp) |
| 343 | { |
| 344 | int ret; |
| 345 | |
| 346 | ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_PHY_PWR, |
| 347 | PLA_PHY_PWR_TXEMP, 1, R8152_WAIT_TIMEOUT); |
| 348 | if (ret) |
| 349 | debug("Timeout waiting for FIFO empty\n"); |
| 350 | |
| 351 | ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_TCR0, |
| 352 | TCR0_TX_EMPTY, 1, R8152_WAIT_TIMEOUT); |
| 353 | if (ret) |
| 354 | debug("Timeout waiting for TX empty\n"); |
| 355 | } |
| 356 | |
| 357 | static void rtl8152_nic_reset(struct r8152 *tp) |
| 358 | { |
| 359 | int ret; |
| 360 | u32 ocp_data; |
| 361 | |
| 362 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, BIST_CTRL); |
| 363 | ocp_data |= BIST_CTRL_SW_RESET; |
| 364 | ocp_write_dword(tp, MCU_TYPE_PLA, BIST_CTRL, ocp_data); |
| 365 | |
| 366 | ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, BIST_CTRL, |
| 367 | BIST_CTRL_SW_RESET, 0, R8152_WAIT_TIMEOUT); |
| 368 | if (ret) |
| 369 | debug("Timeout waiting for NIC reset\n"); |
| 370 | } |
| 371 | |
| 372 | static u8 rtl8152_get_speed(struct r8152 *tp) |
| 373 | { |
| 374 | return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); |
| 375 | } |
| 376 | |
| 377 | static void rtl_set_eee_plus(struct r8152 *tp) |
| 378 | { |
| 379 | u32 ocp_data; |
| 380 | |
| 381 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
| 382 | ocp_data &= ~EEEP_CR_EEEP_TX; |
| 383 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 384 | } |
| 385 | |
| 386 | static void rxdy_gated_en(struct r8152 *tp, bool enable) |
| 387 | { |
| 388 | u32 ocp_data; |
| 389 | |
| 390 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 391 | if (enable) |
| 392 | ocp_data |= RXDY_GATED_EN; |
| 393 | else |
| 394 | ocp_data &= ~RXDY_GATED_EN; |
| 395 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 396 | } |
| 397 | |
| 398 | static void rtl8152_set_rx_mode(struct r8152 *tp) |
| 399 | { |
| 400 | u32 ocp_data; |
| 401 | __le32 tmp[2]; |
| 402 | |
| 403 | tmp[0] = 0xffffffff; |
| 404 | tmp[1] = 0xffffffff; |
| 405 | |
| 406 | pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); |
| 407 | |
| 408 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 409 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 410 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 411 | } |
| 412 | |
Hayes Wang | b08cd54 | 2020-12-16 17:03:23 +0800 | [diff] [blame] | 413 | static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) |
| 414 | { |
| 415 | ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, |
| 416 | OWN_UPDATE | OWN_CLEAR); |
| 417 | } |
| 418 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 419 | static int rtl_enable(struct r8152 *tp) |
| 420 | { |
| 421 | u32 ocp_data; |
| 422 | |
| 423 | r8152b_reset_packet_filter(tp); |
| 424 | |
| 425 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); |
| 426 | ocp_data |= PLA_CR_RE | PLA_CR_TE; |
| 427 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); |
| 428 | |
Hayes Wang | b08cd54 | 2020-12-16 17:03:23 +0800 | [diff] [blame] | 429 | switch (tp->version) { |
| 430 | case RTL_VER_08: |
| 431 | case RTL_VER_09: |
| 432 | r8153b_rx_agg_chg_indicate(tp); |
| 433 | break; |
| 434 | default: |
| 435 | break; |
| 436 | } |
| 437 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 438 | rxdy_gated_en(tp, false); |
| 439 | |
| 440 | rtl8152_set_rx_mode(tp); |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int rtl8152_enable(struct r8152 *tp) |
| 446 | { |
| 447 | rtl_set_eee_plus(tp); |
| 448 | |
| 449 | return rtl_enable(tp); |
| 450 | } |
| 451 | |
| 452 | static void r8153_set_rx_early_timeout(struct r8152 *tp) |
| 453 | { |
| 454 | u32 ocp_data = tp->coalesce / 8; |
| 455 | |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 456 | switch (tp->version) { |
| 457 | case RTL_VER_03: |
| 458 | case RTL_VER_04: |
| 459 | case RTL_VER_05: |
| 460 | case RTL_VER_06: |
| 461 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 462 | ocp_data); |
| 463 | break; |
| 464 | |
| 465 | case RTL_VER_08: |
| 466 | case RTL_VER_09: |
| 467 | /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout |
| 468 | * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 1264ns. |
| 469 | */ |
| 470 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 471 | RX_AUXILIARY_TIMER / 8); |
| 472 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, |
| 473 | ocp_data); |
| 474 | break; |
| 475 | |
| 476 | default: |
| 477 | debug("** %s Invalid Device\n", __func__); |
| 478 | break; |
| 479 | } |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static void r8153_set_rx_early_size(struct r8152 *tp) |
| 483 | { |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 484 | u32 ocp_data = (RTL8152_AGG_BUF_SZ - RTL8153_RMS - |
| 485 | sizeof(struct rx_desc)); |
| 486 | |
| 487 | switch (tp->version) { |
| 488 | case RTL_VER_03: |
| 489 | case RTL_VER_04: |
| 490 | case RTL_VER_05: |
| 491 | case RTL_VER_06: |
| 492 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 493 | ocp_data / 4); |
| 494 | break; |
| 495 | |
| 496 | case RTL_VER_08: |
| 497 | case RTL_VER_09: |
| 498 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 499 | ocp_data / 8); |
| 500 | break; |
| 501 | |
| 502 | default: |
| 503 | debug("** %s Invalid Device\n", __func__); |
| 504 | break; |
| 505 | } |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static int rtl8153_enable(struct r8152 *tp) |
| 509 | { |
| 510 | rtl_set_eee_plus(tp); |
| 511 | r8153_set_rx_early_timeout(tp); |
| 512 | r8153_set_rx_early_size(tp); |
| 513 | |
| 514 | return rtl_enable(tp); |
| 515 | } |
| 516 | |
| 517 | static void rtl_disable(struct r8152 *tp) |
| 518 | { |
| 519 | u32 ocp_data; |
| 520 | |
| 521 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 522 | ocp_data &= ~RCR_ACPT_ALL; |
| 523 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 524 | |
| 525 | rxdy_gated_en(tp, true); |
| 526 | |
| 527 | rtl8152_wait_fifo_empty(tp); |
| 528 | rtl8152_nic_reset(tp); |
| 529 | } |
| 530 | |
| 531 | static void r8152_power_cut_en(struct r8152 *tp, bool enable) |
| 532 | { |
| 533 | u32 ocp_data; |
| 534 | |
| 535 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 536 | if (enable) |
| 537 | ocp_data |= POWER_CUT; |
| 538 | else |
| 539 | ocp_data &= ~POWER_CUT; |
| 540 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 541 | |
| 542 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 543 | ocp_data &= ~RESUME_INDICATE; |
| 544 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
| 545 | } |
| 546 | |
| 547 | static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) |
| 548 | { |
| 549 | u32 ocp_data; |
| 550 | |
| 551 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 552 | if (enable) |
| 553 | ocp_data |= CPCR_RX_VLAN; |
| 554 | else |
| 555 | ocp_data &= ~CPCR_RX_VLAN; |
| 556 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 557 | } |
| 558 | |
| 559 | static void r8153_u1u2en(struct r8152 *tp, bool enable) |
| 560 | { |
| 561 | u8 u1u2[8]; |
| 562 | |
| 563 | if (enable) |
| 564 | memset(u1u2, 0xff, sizeof(u1u2)); |
| 565 | else |
| 566 | memset(u1u2, 0x00, sizeof(u1u2)); |
| 567 | |
| 568 | usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); |
| 569 | } |
| 570 | |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 571 | static void r8153b_u1u2en(struct r8152 *tp, bool enable) |
| 572 | { |
| 573 | u16 ocp_data; |
| 574 | |
| 575 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); |
| 576 | if (enable) |
| 577 | ocp_data |= LPM_U1U2_EN; |
| 578 | else |
| 579 | ocp_data &= ~LPM_U1U2_EN; |
| 580 | |
| 581 | ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); |
| 582 | } |
| 583 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 584 | static void r8153_u2p3en(struct r8152 *tp, bool enable) |
| 585 | { |
| 586 | u32 ocp_data; |
| 587 | |
| 588 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); |
| 589 | if (enable && tp->version != RTL_VER_03 && tp->version != RTL_VER_04) |
| 590 | ocp_data |= U2P3_ENABLE; |
| 591 | else |
| 592 | ocp_data &= ~U2P3_ENABLE; |
| 593 | ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); |
| 594 | } |
| 595 | |
| 596 | static void r8153_power_cut_en(struct r8152 *tp, bool enable) |
| 597 | { |
| 598 | u32 ocp_data; |
| 599 | |
| 600 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 601 | if (enable) |
| 602 | ocp_data |= PWR_EN | PHASE2_EN; |
| 603 | else |
| 604 | ocp_data &= ~(PWR_EN | PHASE2_EN); |
| 605 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 606 | |
| 607 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 608 | ocp_data &= ~PCUT_STATUS; |
| 609 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 610 | } |
| 611 | |
Hayes Wang | 653d2e7 | 2020-06-16 17:09:44 +0800 | [diff] [blame] | 612 | static void rtl_reset_bmu(struct r8152 *tp) |
| 613 | { |
| 614 | u8 ocp_data; |
| 615 | |
| 616 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); |
| 617 | ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); |
| 618 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 619 | ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; |
| 620 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 621 | } |
| 622 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 623 | static int r8152_read_mac(struct r8152 *tp, unsigned char *macaddr) |
| 624 | { |
| 625 | int ret; |
| 626 | unsigned char enetaddr[8] = {0}; |
| 627 | |
| 628 | ret = pla_ocp_read(tp, PLA_IDR, 8, enetaddr); |
| 629 | if (ret < 0) |
| 630 | return ret; |
| 631 | |
| 632 | memcpy(macaddr, enetaddr, ETH_ALEN); |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static void r8152b_disable_aldps(struct r8152 *tp) |
| 637 | { |
| 638 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE); |
| 639 | mdelay(20); |
| 640 | } |
| 641 | |
| 642 | static void r8152b_enable_aldps(struct r8152 *tp) |
| 643 | { |
| 644 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 645 | LINKENA | DIS_SDSAVE); |
| 646 | } |
| 647 | |
| 648 | static void rtl8152_disable(struct r8152 *tp) |
| 649 | { |
| 650 | r8152b_disable_aldps(tp); |
| 651 | rtl_disable(tp); |
| 652 | r8152b_enable_aldps(tp); |
| 653 | } |
| 654 | |
| 655 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 656 | { |
| 657 | u16 data; |
| 658 | |
| 659 | data = r8152_mdio_read(tp, MII_BMCR); |
| 660 | if (data & BMCR_PDOWN) { |
| 661 | data &= ~BMCR_PDOWN; |
| 662 | r8152_mdio_write(tp, MII_BMCR, data); |
| 663 | } |
| 664 | |
| 665 | r8152b_firmware(tp); |
| 666 | } |
| 667 | |
| 668 | static void rtl8152_reinit_ll(struct r8152 *tp) |
| 669 | { |
| 670 | u32 ocp_data; |
| 671 | int ret; |
| 672 | |
| 673 | ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_PHY_PWR, |
| 674 | PLA_PHY_PWR_LLR, 1, R8152_WAIT_TIMEOUT); |
| 675 | if (ret) |
| 676 | debug("Timeout waiting for link list ready\n"); |
| 677 | |
| 678 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 679 | ocp_data |= RE_INIT_LL; |
| 680 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 681 | |
| 682 | ret = r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_PHY_PWR, |
| 683 | PLA_PHY_PWR_LLR, 1, R8152_WAIT_TIMEOUT); |
| 684 | if (ret) |
| 685 | debug("Timeout waiting for link list ready\n"); |
| 686 | } |
| 687 | |
| 688 | static void r8152b_exit_oob(struct r8152 *tp) |
| 689 | { |
| 690 | u32 ocp_data; |
| 691 | |
| 692 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 693 | ocp_data &= ~RCR_ACPT_ALL; |
| 694 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 695 | |
| 696 | rxdy_gated_en(tp, true); |
| 697 | r8152b_hw_phy_cfg(tp); |
| 698 | |
| 699 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 700 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 701 | |
| 702 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 703 | ocp_data &= ~NOW_IS_OOB; |
| 704 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 705 | |
| 706 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 707 | ocp_data &= ~MCU_BORW_EN; |
| 708 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 709 | |
| 710 | rtl8152_reinit_ll(tp); |
| 711 | rtl8152_nic_reset(tp); |
| 712 | |
| 713 | /* rx share fifo credit full threshold */ |
| 714 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 715 | |
| 716 | if (tp->udev->speed == USB_SPEED_FULL || |
| 717 | tp->udev->speed == USB_SPEED_LOW) { |
| 718 | /* rx share fifo credit near full threshold */ |
| 719 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 720 | RXFIFO_THR2_FULL); |
| 721 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 722 | RXFIFO_THR3_FULL); |
| 723 | } else { |
| 724 | /* rx share fifo credit near full threshold */ |
| 725 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 726 | RXFIFO_THR2_HIGH); |
| 727 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 728 | RXFIFO_THR3_HIGH); |
| 729 | } |
| 730 | |
| 731 | /* TX share fifo free credit full threshold */ |
| 732 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 733 | |
| 734 | ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); |
| 735 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); |
| 736 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 737 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 738 | |
| 739 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 740 | |
| 741 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 742 | ocp_data |= TCR0_AUTO_FIFO; |
| 743 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 744 | } |
| 745 | |
| 746 | static void r8152b_enter_oob(struct r8152 *tp) |
| 747 | { |
| 748 | u32 ocp_data; |
| 749 | |
| 750 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 751 | ocp_data &= ~NOW_IS_OOB; |
| 752 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 753 | |
| 754 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 755 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 756 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 757 | |
| 758 | rtl_disable(tp); |
| 759 | |
| 760 | rtl8152_reinit_ll(tp); |
| 761 | |
| 762 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 763 | |
| 764 | rtl_rx_vlan_en(tp, false); |
| 765 | |
Hayes Wang | 1bb3ee4 | 2020-05-22 16:54:11 +0800 | [diff] [blame] | 766 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 767 | ocp_data |= ALDPS_PROXY_MODE; |
Hayes Wang | 1bb3ee4 | 2020-05-22 16:54:11 +0800 | [diff] [blame] | 768 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 769 | |
| 770 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 771 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 772 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 773 | |
| 774 | rxdy_gated_en(tp, false); |
| 775 | |
| 776 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 777 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 778 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 779 | } |
| 780 | |
| 781 | static void r8153_hw_phy_cfg(struct r8152 *tp) |
| 782 | { |
| 783 | u32 ocp_data; |
| 784 | u16 data; |
| 785 | |
| 786 | if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || |
| 787 | tp->version == RTL_VER_05) |
| 788 | ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); |
| 789 | |
| 790 | data = r8152_mdio_read(tp, MII_BMCR); |
| 791 | if (data & BMCR_PDOWN) { |
| 792 | data &= ~BMCR_PDOWN; |
| 793 | r8152_mdio_write(tp, MII_BMCR, data); |
| 794 | } |
| 795 | |
| 796 | r8153_firmware(tp); |
| 797 | |
| 798 | if (tp->version == RTL_VER_03) { |
| 799 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 800 | data &= ~CTAP_SHORT_EN; |
| 801 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 802 | } |
| 803 | |
| 804 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 805 | data |= EEE_CLKDIV_EN; |
| 806 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 807 | |
| 808 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 809 | data |= EN_10M_BGOFF; |
| 810 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 811 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 812 | data |= EN_10M_PLLOFF; |
| 813 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 814 | sram_write(tp, SRAM_IMPEDANCE, 0x0b13); |
| 815 | |
| 816 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 817 | ocp_data |= PFM_PWM_SWITCH; |
| 818 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 819 | |
| 820 | /* Enable LPF corner auto tune */ |
| 821 | sram_write(tp, SRAM_LPF_CFG, 0xf70f); |
| 822 | |
| 823 | /* Adjust 10M Amplitude */ |
| 824 | sram_write(tp, SRAM_10M_AMP1, 0x00af); |
| 825 | sram_write(tp, SRAM_10M_AMP2, 0x0208); |
| 826 | } |
| 827 | |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 828 | static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) |
| 829 | { |
| 830 | u32 ocp_data; |
| 831 | |
| 832 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); |
| 833 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); |
| 834 | ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ |
| 835 | ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); |
| 836 | |
| 837 | return ocp_data; |
| 838 | } |
| 839 | |
| 840 | static void r8153b_hw_phy_cfg(struct r8152 *tp) |
| 841 | { |
| 842 | u32 ocp_data; |
| 843 | u16 data; |
| 844 | |
| 845 | data = r8152_mdio_read(tp, MII_BMCR); |
| 846 | if (data & BMCR_PDOWN) { |
| 847 | data &= ~BMCR_PDOWN; |
| 848 | r8152_mdio_write(tp, MII_BMCR, data); |
| 849 | } |
| 850 | |
| 851 | /* U1/U2/L1 idle timer. 500 us */ |
| 852 | ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); |
| 853 | |
| 854 | r8153b_firmware(tp); |
| 855 | |
| 856 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 857 | data |= R_TUNE_EN; |
| 858 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 859 | data = ocp_reg_read(tp, OCP_NCTL_CFG); |
| 860 | data |= PGA_RETURN_EN; |
| 861 | ocp_reg_write(tp, OCP_NCTL_CFG, data); |
| 862 | |
| 863 | /* ADC Bias Calibration: |
| 864 | * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake |
| 865 | * bit (bit3) to rebuild the real 16-bit data. Write the data to the |
| 866 | * ADC ioffset. |
| 867 | */ |
| 868 | ocp_data = r8152_efuse_read(tp, 0x7d); |
| 869 | ocp_data = ((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7); |
| 870 | if (ocp_data != 0xffff) |
| 871 | ocp_reg_write(tp, OCP_ADC_IOFFSET, ocp_data); |
| 872 | |
| 873 | /* ups mode tx-link-pulse timing adjustment: |
| 874 | * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] |
| 875 | * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt |
| 876 | */ |
| 877 | ocp_data = ocp_reg_read(tp, 0xc426); |
| 878 | ocp_data &= 0x3fff; |
| 879 | if (ocp_data) { |
| 880 | u32 swr_cnt_1ms_ini; |
| 881 | |
| 882 | swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; |
| 883 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); |
| 884 | ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; |
| 885 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); |
| 886 | } |
| 887 | |
| 888 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 889 | ocp_data |= PFM_PWM_SWITCH; |
| 890 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 891 | } |
| 892 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 893 | static void r8153_first_init(struct r8152 *tp) |
| 894 | { |
| 895 | u32 ocp_data; |
| 896 | |
| 897 | rxdy_gated_en(tp, true); |
| 898 | |
| 899 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 900 | ocp_data &= ~RCR_ACPT_ALL; |
| 901 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 902 | |
| 903 | r8153_hw_phy_cfg(tp); |
| 904 | |
| 905 | rtl8152_nic_reset(tp); |
Hayes Wang | 653d2e7 | 2020-06-16 17:09:44 +0800 | [diff] [blame] | 906 | rtl_reset_bmu(tp); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 907 | |
| 908 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 909 | ocp_data &= ~NOW_IS_OOB; |
| 910 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 911 | |
| 912 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 913 | ocp_data &= ~MCU_BORW_EN; |
| 914 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 915 | |
| 916 | rtl8152_reinit_ll(tp); |
| 917 | |
| 918 | rtl_rx_vlan_en(tp, false); |
| 919 | |
| 920 | ocp_data = RTL8153_RMS; |
| 921 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
| 922 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); |
| 923 | |
| 924 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 925 | ocp_data |= TCR0_AUTO_FIFO; |
| 926 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 927 | |
| 928 | rtl8152_nic_reset(tp); |
| 929 | |
| 930 | /* rx share fifo credit full threshold */ |
| 931 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 932 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); |
| 933 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); |
| 934 | /* TX share fifo free credit full threshold */ |
| 935 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); |
| 936 | |
| 937 | /* rx aggregation */ |
| 938 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 939 | |
| 940 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
| 941 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 942 | } |
| 943 | |
| 944 | static void r8153_enter_oob(struct r8152 *tp) |
| 945 | { |
| 946 | u32 ocp_data; |
| 947 | |
| 948 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 949 | ocp_data &= ~NOW_IS_OOB; |
| 950 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 951 | |
| 952 | rtl_disable(tp); |
Hayes Wang | 653d2e7 | 2020-06-16 17:09:44 +0800 | [diff] [blame] | 953 | rtl_reset_bmu(tp); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 954 | |
| 955 | rtl8152_reinit_ll(tp); |
| 956 | |
| 957 | ocp_data = RTL8153_RMS; |
| 958 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
| 959 | |
| 960 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 961 | ocp_data &= ~TEREDO_WAKE_MASK; |
| 962 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 963 | |
| 964 | rtl_rx_vlan_en(tp, false); |
| 965 | |
Hayes Wang | 1bb3ee4 | 2020-05-22 16:54:11 +0800 | [diff] [blame] | 966 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 967 | ocp_data |= ALDPS_PROXY_MODE; |
Hayes Wang | 1bb3ee4 | 2020-05-22 16:54:11 +0800 | [diff] [blame] | 968 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 969 | |
| 970 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 971 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 972 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 973 | |
| 974 | rxdy_gated_en(tp, false); |
| 975 | |
| 976 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 977 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 978 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 979 | } |
| 980 | |
| 981 | static void r8153_disable_aldps(struct r8152 *tp) |
| 982 | { |
| 983 | u16 data; |
| 984 | |
| 985 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 986 | data &= ~EN_ALDPS; |
| 987 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 988 | mdelay(20); |
| 989 | } |
| 990 | |
| 991 | static void rtl8153_disable(struct r8152 *tp) |
| 992 | { |
| 993 | r8153_disable_aldps(tp); |
| 994 | rtl_disable(tp); |
Hayes Wang | 653d2e7 | 2020-06-16 17:09:44 +0800 | [diff] [blame] | 995 | rtl_reset_bmu(tp); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex) |
| 999 | { |
| 1000 | u16 bmcr, anar, gbcr; |
| 1001 | |
| 1002 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 1003 | anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 1004 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
| 1005 | if (tp->supports_gmii) { |
| 1006 | gbcr = r8152_mdio_read(tp, MII_CTRL1000); |
| 1007 | gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 1008 | } else { |
| 1009 | gbcr = 0; |
| 1010 | } |
| 1011 | |
| 1012 | if (autoneg == AUTONEG_DISABLE) { |
| 1013 | if (speed == SPEED_10) { |
| 1014 | bmcr = 0; |
| 1015 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 1016 | } else if (speed == SPEED_100) { |
| 1017 | bmcr = BMCR_SPEED100; |
| 1018 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 1019 | } else if (speed == SPEED_1000 && tp->supports_gmii) { |
| 1020 | bmcr = BMCR_SPEED1000; |
| 1021 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
| 1022 | } else { |
| 1023 | return -EINVAL; |
| 1024 | } |
| 1025 | |
| 1026 | if (duplex == DUPLEX_FULL) |
| 1027 | bmcr |= BMCR_FULLDPLX; |
| 1028 | } else { |
| 1029 | if (speed == SPEED_10) { |
| 1030 | if (duplex == DUPLEX_FULL) |
| 1031 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 1032 | else |
| 1033 | anar |= ADVERTISE_10HALF; |
| 1034 | } else if (speed == SPEED_100) { |
| 1035 | if (duplex == DUPLEX_FULL) { |
| 1036 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 1037 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 1038 | } else { |
| 1039 | anar |= ADVERTISE_10HALF; |
| 1040 | anar |= ADVERTISE_100HALF; |
| 1041 | } |
| 1042 | } else if (speed == SPEED_1000 && tp->supports_gmii) { |
| 1043 | if (duplex == DUPLEX_FULL) { |
| 1044 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 1045 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 1046 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
| 1047 | } else { |
| 1048 | anar |= ADVERTISE_10HALF; |
| 1049 | anar |= ADVERTISE_100HALF; |
| 1050 | gbcr |= ADVERTISE_1000HALF; |
| 1051 | } |
| 1052 | } else { |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | |
Hayes Wang | 27217bc | 2020-06-16 17:09:45 +0800 | [diff] [blame] | 1056 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART | BMCR_RESET; |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | if (tp->supports_gmii) |
| 1060 | r8152_mdio_write(tp, MII_CTRL1000, gbcr); |
| 1061 | |
| 1062 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 1063 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | static void rtl8152_up(struct r8152 *tp) |
| 1069 | { |
| 1070 | r8152b_disable_aldps(tp); |
| 1071 | r8152b_exit_oob(tp); |
| 1072 | r8152b_enable_aldps(tp); |
| 1073 | } |
| 1074 | |
| 1075 | static void rtl8152_down(struct r8152 *tp) |
| 1076 | { |
| 1077 | r8152_power_cut_en(tp, false); |
| 1078 | r8152b_disable_aldps(tp); |
| 1079 | r8152b_enter_oob(tp); |
| 1080 | r8152b_enable_aldps(tp); |
| 1081 | } |
| 1082 | |
| 1083 | static void rtl8153_up(struct r8152 *tp) |
| 1084 | { |
| 1085 | r8153_u1u2en(tp, false); |
| 1086 | r8153_disable_aldps(tp); |
| 1087 | r8153_first_init(tp); |
| 1088 | r8153_u2p3en(tp, false); |
| 1089 | } |
| 1090 | |
| 1091 | static void rtl8153_down(struct r8152 *tp) |
| 1092 | { |
| 1093 | r8153_u1u2en(tp, false); |
| 1094 | r8153_u2p3en(tp, false); |
| 1095 | r8153_power_cut_en(tp, false); |
| 1096 | r8153_disable_aldps(tp); |
| 1097 | r8153_enter_oob(tp); |
| 1098 | } |
| 1099 | |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 1100 | static void rtl8153b_up(struct r8152 *tp) |
| 1101 | { |
| 1102 | r8153_first_init(tp); |
| 1103 | } |
| 1104 | |
| 1105 | static void rtl8153b_down(struct r8152 *tp) |
| 1106 | { |
| 1107 | r8153_enter_oob(tp); |
| 1108 | } |
| 1109 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1110 | static void r8152b_get_version(struct r8152 *tp) |
| 1111 | { |
| 1112 | u32 ocp_data; |
| 1113 | u16 tcr; |
| 1114 | int i; |
| 1115 | |
| 1116 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1); |
| 1117 | tcr = (u16)(ocp_data & VERSION_MASK); |
| 1118 | |
| 1119 | for (i = 0; i < ARRAY_SIZE(r8152_versions); i++) { |
| 1120 | if (tcr == r8152_versions[i].tcr) { |
| 1121 | /* Found a supported version */ |
| 1122 | tp->version = r8152_versions[i].version; |
| 1123 | tp->supports_gmii = r8152_versions[i].gmii; |
| 1124 | break; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | if (tp->version == RTL_VER_UNKNOWN) |
| 1129 | debug("r8152 Unknown tcr version 0x%04x\n", tcr); |
| 1130 | } |
| 1131 | |
| 1132 | static void r8152b_enable_fc(struct r8152 *tp) |
| 1133 | { |
| 1134 | u16 anar; |
| 1135 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 1136 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 1137 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 1138 | } |
| 1139 | |
| 1140 | static void rtl_tally_reset(struct r8152 *tp) |
| 1141 | { |
| 1142 | u32 ocp_data; |
| 1143 | |
| 1144 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); |
| 1145 | ocp_data |= TALLY_RESET; |
| 1146 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); |
| 1147 | } |
| 1148 | |
| 1149 | static void r8152b_init(struct r8152 *tp) |
| 1150 | { |
| 1151 | u32 ocp_data; |
| 1152 | |
| 1153 | r8152b_disable_aldps(tp); |
| 1154 | |
| 1155 | if (tp->version == RTL_VER_01) { |
| 1156 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 1157 | ocp_data &= ~LED_MODE_MASK; |
| 1158 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 1159 | } |
| 1160 | |
| 1161 | r8152_power_cut_en(tp, false); |
| 1162 | |
| 1163 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 1164 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 1165 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 1166 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 1167 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 1168 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 1169 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 1170 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 1171 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 1172 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 1173 | |
| 1174 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_TIMER); |
| 1175 | ocp_data |= BIT(15); |
| 1176 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_TIMER, ocp_data); |
| 1177 | ocp_write_word(tp, MCU_TYPE_USB, 0xcbfc, 0x03e8); |
| 1178 | ocp_data &= ~BIT(15); |
| 1179 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_TIMER, ocp_data); |
| 1180 | |
| 1181 | r8152b_enable_fc(tp); |
| 1182 | rtl_tally_reset(tp); |
| 1183 | |
| 1184 | /* enable rx aggregation */ |
| 1185 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 1186 | |
| 1187 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
| 1188 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 1189 | } |
| 1190 | |
| 1191 | static void r8153_init(struct r8152 *tp) |
| 1192 | { |
| 1193 | int i; |
| 1194 | u32 ocp_data; |
| 1195 | |
| 1196 | r8153_disable_aldps(tp); |
| 1197 | r8153_u1u2en(tp, false); |
| 1198 | |
| 1199 | r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_BOOT_CTRL, |
| 1200 | AUTOLOAD_DONE, 1, R8152_WAIT_TIMEOUT); |
| 1201 | |
| 1202 | for (i = 0; i < R8152_WAIT_TIMEOUT; i++) { |
| 1203 | ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK; |
| 1204 | if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN) |
| 1205 | break; |
| 1206 | |
| 1207 | mdelay(1); |
| 1208 | } |
| 1209 | |
| 1210 | r8153_u2p3en(tp, false); |
| 1211 | |
| 1212 | if (tp->version == RTL_VER_04) { |
| 1213 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); |
| 1214 | ocp_data &= ~pwd_dn_scale_mask; |
| 1215 | ocp_data |= pwd_dn_scale(96); |
| 1216 | ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); |
| 1217 | |
| 1218 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 1219 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 1220 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 1221 | } else if (tp->version == RTL_VER_05) { |
| 1222 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); |
| 1223 | ocp_data &= ~ECM_ALDPS; |
| 1224 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); |
| 1225 | |
| 1226 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 1227 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 1228 | ocp_data &= ~DYNAMIC_BURST; |
| 1229 | else |
| 1230 | ocp_data |= DYNAMIC_BURST; |
| 1231 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
| 1232 | } else if (tp->version == RTL_VER_06) { |
| 1233 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 1234 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 1235 | ocp_data &= ~DYNAMIC_BURST; |
| 1236 | else |
| 1237 | ocp_data |= DYNAMIC_BURST; |
| 1238 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
| 1239 | } |
| 1240 | |
| 1241 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); |
| 1242 | ocp_data |= EP4_FULL_FC; |
| 1243 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); |
| 1244 | |
| 1245 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); |
| 1246 | ocp_data &= ~TIMER11_EN; |
| 1247 | ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); |
| 1248 | |
| 1249 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 1250 | ocp_data &= ~LED_MODE_MASK; |
| 1251 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 1252 | |
| 1253 | ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; |
| 1254 | if (tp->version == RTL_VER_04 && tp->udev->speed != USB_SPEED_SUPER) |
| 1255 | ocp_data |= LPM_TIMER_500MS; |
| 1256 | else |
| 1257 | ocp_data |= LPM_TIMER_500US; |
| 1258 | ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); |
| 1259 | |
| 1260 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); |
| 1261 | ocp_data &= ~SEN_VAL_MASK; |
| 1262 | ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; |
| 1263 | ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); |
| 1264 | |
| 1265 | ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); |
| 1266 | |
| 1267 | r8153_power_cut_en(tp, false); |
| 1268 | |
| 1269 | r8152b_enable_fc(tp); |
| 1270 | rtl_tally_reset(tp); |
| 1271 | } |
| 1272 | |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 1273 | static void r8153b_init(struct r8152 *tp) |
| 1274 | { |
| 1275 | u32 ocp_data; |
| 1276 | int i; |
| 1277 | |
| 1278 | r8153_disable_aldps(tp); |
| 1279 | r8153b_u1u2en(tp, false); |
| 1280 | |
| 1281 | r8152_wait_for_bit(tp, 0, MCU_TYPE_PLA, PLA_BOOT_CTRL, |
| 1282 | AUTOLOAD_DONE, 1, R8152_WAIT_TIMEOUT); |
| 1283 | |
| 1284 | for (i = 0; i < R8152_WAIT_TIMEOUT; i++) { |
| 1285 | ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK; |
| 1286 | if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN) |
| 1287 | break; |
| 1288 | |
| 1289 | mdelay(1); |
| 1290 | } |
| 1291 | |
| 1292 | r8153_u2p3en(tp, false); |
| 1293 | |
| 1294 | /* MSC timer = 0xfff * 8ms = 32760 ms */ |
| 1295 | ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); |
| 1296 | |
| 1297 | r8153_power_cut_en(tp, false); |
| 1298 | |
| 1299 | /* MAC clock speed down */ |
| 1300 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); |
| 1301 | ocp_data |= MAC_CLK_SPDWN_EN; |
| 1302 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); |
| 1303 | |
| 1304 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); |
| 1305 | ocp_data &= ~PLA_MCU_SPDWN_EN; |
| 1306 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); |
| 1307 | |
| 1308 | if (tp->version == RTL_VER_09) { |
| 1309 | /* Disable Test IO for 32QFN */ |
| 1310 | if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) { |
| 1311 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 1312 | ocp_data |= TEST_IO_OFF; |
| 1313 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | /* rx aggregation */ |
| 1318 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 1319 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
| 1320 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 1321 | |
| 1322 | rtl_tally_reset(tp); |
| 1323 | r8153b_hw_phy_cfg(tp); |
| 1324 | r8152b_enable_fc(tp); |
| 1325 | } |
| 1326 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1327 | static void rtl8152_unload(struct r8152 *tp) |
| 1328 | { |
| 1329 | if (tp->version != RTL_VER_01) |
| 1330 | r8152_power_cut_en(tp, true); |
| 1331 | } |
| 1332 | |
| 1333 | static void rtl8153_unload(struct r8152 *tp) |
| 1334 | { |
| 1335 | r8153_power_cut_en(tp, false); |
| 1336 | } |
| 1337 | |
| 1338 | static int rtl_ops_init(struct r8152 *tp) |
| 1339 | { |
| 1340 | struct rtl_ops *ops = &tp->rtl_ops; |
| 1341 | int ret = 0; |
| 1342 | |
| 1343 | switch (tp->version) { |
| 1344 | case RTL_VER_01: |
| 1345 | case RTL_VER_02: |
| 1346 | case RTL_VER_07: |
| 1347 | ops->init = r8152b_init; |
| 1348 | ops->enable = rtl8152_enable; |
| 1349 | ops->disable = rtl8152_disable; |
| 1350 | ops->up = rtl8152_up; |
| 1351 | ops->down = rtl8152_down; |
| 1352 | ops->unload = rtl8152_unload; |
| 1353 | break; |
| 1354 | |
| 1355 | case RTL_VER_03: |
| 1356 | case RTL_VER_04: |
| 1357 | case RTL_VER_05: |
| 1358 | case RTL_VER_06: |
| 1359 | ops->init = r8153_init; |
| 1360 | ops->enable = rtl8153_enable; |
| 1361 | ops->disable = rtl8153_disable; |
| 1362 | ops->up = rtl8153_up; |
| 1363 | ops->down = rtl8153_down; |
| 1364 | ops->unload = rtl8153_unload; |
| 1365 | break; |
| 1366 | |
Hayes Wang | d215ca2 | 2020-06-16 17:09:47 +0800 | [diff] [blame] | 1367 | case RTL_VER_08: |
| 1368 | case RTL_VER_09: |
| 1369 | ops->init = r8153b_init; |
| 1370 | ops->enable = rtl8153_enable; |
| 1371 | ops->disable = rtl8153_disable; |
| 1372 | ops->up = rtl8153b_up; |
| 1373 | ops->down = rtl8153b_down; |
| 1374 | break; |
| 1375 | |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1376 | default: |
| 1377 | ret = -ENODEV; |
| 1378 | printf("r8152 Unknown Device\n"); |
| 1379 | break; |
| 1380 | } |
| 1381 | |
| 1382 | return ret; |
| 1383 | } |
| 1384 | |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1385 | static int r8152_init_common(struct r8152 *tp) |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1386 | { |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1387 | u8 speed; |
| 1388 | int timeout = 0; |
| 1389 | int link_detected; |
| 1390 | |
| 1391 | debug("** %s()\n", __func__); |
| 1392 | |
| 1393 | do { |
| 1394 | speed = rtl8152_get_speed(tp); |
| 1395 | |
| 1396 | link_detected = speed & LINK_STATUS; |
| 1397 | if (!link_detected) { |
| 1398 | if (timeout == 0) |
| 1399 | printf("Waiting for Ethernet connection... "); |
| 1400 | mdelay(TIMEOUT_RESOLUTION); |
| 1401 | timeout += TIMEOUT_RESOLUTION; |
| 1402 | } |
| 1403 | } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT); |
| 1404 | if (link_detected) { |
| 1405 | tp->rtl_ops.enable(tp); |
| 1406 | |
| 1407 | if (timeout != 0) |
| 1408 | printf("done.\n"); |
| 1409 | } else { |
| 1410 | printf("unable to connect.\n"); |
| 1411 | } |
| 1412 | |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1416 | static int r8152_send_common(struct ueth_data *ueth, void *packet, int length) |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1417 | { |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1418 | struct usb_device *udev = ueth->pusb_dev; |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1419 | u32 opts1, opts2 = 0; |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1420 | int err; |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1421 | int actual_len; |
Stefan Roese | 0109440 | 2016-11-22 16:14:23 +0100 | [diff] [blame] | 1422 | ALLOC_CACHE_ALIGN_BUFFER(uint8_t, msg, |
| 1423 | PKTSIZE + sizeof(struct tx_desc)); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1424 | struct tx_desc *tx_desc = (struct tx_desc *)msg; |
| 1425 | |
| 1426 | debug("** %s(), len %d\n", __func__, length); |
| 1427 | |
| 1428 | opts1 = length | TX_FS | TX_LS; |
| 1429 | |
| 1430 | tx_desc->opts2 = cpu_to_le32(opts2); |
| 1431 | tx_desc->opts1 = cpu_to_le32(opts1); |
| 1432 | |
| 1433 | memcpy(msg + sizeof(struct tx_desc), (void *)packet, length); |
| 1434 | |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1435 | err = usb_bulk_msg(udev, usb_sndbulkpipe(udev, ueth->ep_out), |
| 1436 | (void *)msg, length + sizeof(struct tx_desc), |
| 1437 | &actual_len, USB_BULK_SEND_TIMEOUT); |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 1438 | debug("Tx: len = %zu, actual = %u, err = %d\n", |
| 1439 | length + sizeof(struct tx_desc), actual_len, err); |
| 1440 | |
| 1441 | return err; |
| 1442 | } |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1443 | |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1444 | static int r8152_eth_start(struct udevice *dev) |
| 1445 | { |
| 1446 | struct r8152 *tp = dev_get_priv(dev); |
| 1447 | |
| 1448 | debug("** %s (%d)\n", __func__, __LINE__); |
| 1449 | |
| 1450 | return r8152_init_common(tp); |
| 1451 | } |
| 1452 | |
| 1453 | void r8152_eth_stop(struct udevice *dev) |
| 1454 | { |
| 1455 | struct r8152 *tp = dev_get_priv(dev); |
| 1456 | |
| 1457 | debug("** %s (%d)\n", __func__, __LINE__); |
| 1458 | |
| 1459 | tp->rtl_ops.disable(tp); |
| 1460 | } |
| 1461 | |
| 1462 | int r8152_eth_send(struct udevice *dev, void *packet, int length) |
| 1463 | { |
| 1464 | struct r8152 *tp = dev_get_priv(dev); |
| 1465 | |
| 1466 | return r8152_send_common(&tp->ueth, packet, length); |
| 1467 | } |
| 1468 | |
| 1469 | int r8152_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 1470 | { |
| 1471 | struct r8152 *tp = dev_get_priv(dev); |
| 1472 | struct ueth_data *ueth = &tp->ueth; |
| 1473 | uint8_t *ptr; |
| 1474 | int ret, len; |
| 1475 | struct rx_desc *rx_desc; |
| 1476 | u16 packet_len; |
| 1477 | |
| 1478 | len = usb_ether_get_rx_bytes(ueth, &ptr); |
| 1479 | debug("%s: first try, len=%d\n", __func__, len); |
| 1480 | if (!len) { |
| 1481 | if (!(flags & ETH_RECV_CHECK_DEVICE)) |
| 1482 | return -EAGAIN; |
| 1483 | ret = usb_ether_receive(ueth, RTL8152_AGG_BUF_SZ); |
| 1484 | if (ret) |
| 1485 | return ret; |
| 1486 | |
| 1487 | len = usb_ether_get_rx_bytes(ueth, &ptr); |
| 1488 | debug("%s: second try, len=%d\n", __func__, len); |
| 1489 | } |
| 1490 | |
| 1491 | rx_desc = (struct rx_desc *)ptr; |
| 1492 | packet_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; |
| 1493 | packet_len -= CRC_SIZE; |
| 1494 | |
| 1495 | if (packet_len > len - (sizeof(struct rx_desc) + CRC_SIZE)) { |
| 1496 | debug("Rx: too large packet: %d\n", packet_len); |
| 1497 | goto err; |
| 1498 | } |
| 1499 | |
| 1500 | *packetp = ptr + sizeof(struct rx_desc); |
| 1501 | return packet_len; |
| 1502 | |
| 1503 | err: |
| 1504 | usb_ether_advance_rxbuf(ueth, -1); |
| 1505 | return -ENOSPC; |
| 1506 | } |
| 1507 | |
| 1508 | static int r8152_free_pkt(struct udevice *dev, uchar *packet, int packet_len) |
| 1509 | { |
| 1510 | struct r8152 *tp = dev_get_priv(dev); |
| 1511 | |
| 1512 | packet_len += sizeof(struct rx_desc) + CRC_SIZE; |
| 1513 | packet_len = ALIGN(packet_len, 8); |
| 1514 | usb_ether_advance_rxbuf(&tp->ueth, packet_len); |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | static int r8152_write_hwaddr(struct udevice *dev) |
| 1520 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1521 | struct eth_pdata *pdata = dev_get_plat(dev); |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1522 | struct r8152 *tp = dev_get_priv(dev); |
| 1523 | |
| 1524 | unsigned char enetaddr[8] = { 0 }; |
| 1525 | |
| 1526 | debug("** %s (%d)\n", __func__, __LINE__); |
| 1527 | memcpy(enetaddr, pdata->enetaddr, ETH_ALEN); |
| 1528 | |
| 1529 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 1530 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, enetaddr); |
| 1531 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 1532 | |
| 1533 | debug("MAC %pM\n", pdata->enetaddr); |
| 1534 | return 0; |
| 1535 | } |
| 1536 | |
| 1537 | int r8152_read_rom_hwaddr(struct udevice *dev) |
| 1538 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1539 | struct eth_pdata *pdata = dev_get_plat(dev); |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1540 | struct r8152 *tp = dev_get_priv(dev); |
| 1541 | |
| 1542 | debug("** %s (%d)\n", __func__, __LINE__); |
| 1543 | r8152_read_mac(tp, pdata->enetaddr); |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
| 1547 | static int r8152_eth_probe(struct udevice *dev) |
| 1548 | { |
| 1549 | struct usb_device *udev = dev_get_parent_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1550 | struct eth_pdata *pdata = dev_get_plat(dev); |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1551 | struct r8152 *tp = dev_get_priv(dev); |
| 1552 | struct ueth_data *ueth = &tp->ueth; |
| 1553 | int ret; |
| 1554 | |
| 1555 | tp->udev = udev; |
| 1556 | r8152_read_mac(tp, pdata->enetaddr); |
| 1557 | |
| 1558 | r8152b_get_version(tp); |
| 1559 | |
| 1560 | ret = rtl_ops_init(tp); |
| 1561 | if (ret) |
| 1562 | return ret; |
| 1563 | |
| 1564 | tp->rtl_ops.init(tp); |
| 1565 | tp->rtl_ops.up(tp); |
| 1566 | |
| 1567 | rtl8152_set_speed(tp, AUTONEG_ENABLE, |
| 1568 | tp->supports_gmii ? SPEED_1000 : SPEED_100, |
| 1569 | DUPLEX_FULL); |
| 1570 | |
| 1571 | return usb_ether_register(dev, ueth, RTL8152_AGG_BUF_SZ); |
| 1572 | } |
| 1573 | |
| 1574 | static const struct eth_ops r8152_eth_ops = { |
| 1575 | .start = r8152_eth_start, |
| 1576 | .send = r8152_eth_send, |
| 1577 | .recv = r8152_eth_recv, |
| 1578 | .free_pkt = r8152_free_pkt, |
| 1579 | .stop = r8152_eth_stop, |
| 1580 | .write_hwaddr = r8152_write_hwaddr, |
| 1581 | .read_rom_hwaddr = r8152_read_rom_hwaddr, |
| 1582 | }; |
| 1583 | |
| 1584 | U_BOOT_DRIVER(r8152_eth) = { |
| 1585 | .name = "r8152_eth", |
| 1586 | .id = UCLASS_ETH, |
| 1587 | .probe = r8152_eth_probe, |
| 1588 | .ops = &r8152_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1589 | .priv_auto = sizeof(struct r8152), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1590 | .plat_auto = sizeof(struct eth_pdata), |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1591 | }; |
| 1592 | |
| 1593 | static const struct usb_device_id r8152_eth_id_table[] = { |
| 1594 | /* Realtek */ |
| 1595 | { USB_DEVICE(0x0bda, 0x8050) }, |
| 1596 | { USB_DEVICE(0x0bda, 0x8152) }, |
| 1597 | { USB_DEVICE(0x0bda, 0x8153) }, |
| 1598 | |
| 1599 | /* Samsung */ |
| 1600 | { USB_DEVICE(0x04e8, 0xa101) }, |
| 1601 | |
| 1602 | /* Lenovo */ |
| 1603 | { USB_DEVICE(0x17ef, 0x304f) }, |
| 1604 | { USB_DEVICE(0x17ef, 0x3052) }, |
| 1605 | { USB_DEVICE(0x17ef, 0x3054) }, |
| 1606 | { USB_DEVICE(0x17ef, 0x3057) }, |
| 1607 | { USB_DEVICE(0x17ef, 0x7205) }, |
| 1608 | { USB_DEVICE(0x17ef, 0x720a) }, |
| 1609 | { USB_DEVICE(0x17ef, 0x720b) }, |
| 1610 | { USB_DEVICE(0x17ef, 0x720c) }, |
| 1611 | |
| 1612 | /* TP-LINK */ |
| 1613 | { USB_DEVICE(0x2357, 0x0601) }, |
Oleksii Titov | 0c92935 | 2022-04-20 11:23:25 +0300 | [diff] [blame] | 1614 | { USB_DEVICE(0x2357, 0x0602) }, |
Stefan Roese | 47c5097 | 2016-06-29 07:58:05 +0200 | [diff] [blame] | 1615 | |
| 1616 | /* Nvidia */ |
| 1617 | { USB_DEVICE(0x0955, 0x09ff) }, |
| 1618 | |
| 1619 | { } /* Terminating entry */ |
| 1620 | }; |
| 1621 | |
| 1622 | U_BOOT_USB_DEVICE(r8152_eth, r8152_eth_id_table); |