1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved. 4 */ 5 6 #include <linux/signal.h> 7 #include <linux/slab.h> 8 #include <linux/module.h> 9 #include <linux/netdevice.h> 10 #include <linux/etherdevice.h> 11 #include <linux/mii.h> 12 #include <linux/ethtool.h> 13 #include <linux/usb.h> 14 #include <linux/crc32.h> 15 #include <linux/if_vlan.h> 16 #include <linux/uaccess.h> 17 #include <linux/list.h> 18 #include <linux/ip.h> 19 #include <linux/ipv6.h> 20 #include <net/ip6_checksum.h> 21 #include <uapi/linux/mdio.h> 22 #include <linux/mdio.h> 23 #include <linux/usb/cdc.h> 24 #include <linux/suspend.h> 25 #include <linux/atomic.h> 26 #include <linux/acpi.h> 27 #include <linux/firmware.h> 28 #include <crypto/hash.h> 29 #include <linux/usb/r8152.h> 30 31 /* Information for net-next */ 32 #define NETNEXT_VERSION "12" 33 34 /* Information for net */ 35 #define NET_VERSION "13" 36 37 #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION 38 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" 39 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" 40 #define MODULENAME "r8152" 41 42 #define R8152_PHY_ID 32 43 44 #define PLA_IDR 0xc000 45 #define PLA_RCR 0xc010 46 #define PLA_RCR1 0xc012 47 #define PLA_RMS 0xc016 48 #define PLA_RXFIFO_CTRL0 0xc0a0 49 #define PLA_RXFIFO_FULL 0xc0a2 50 #define PLA_RXFIFO_CTRL1 0xc0a4 51 #define PLA_RX_FIFO_FULL 0xc0a6 52 #define PLA_RXFIFO_CTRL2 0xc0a8 53 #define PLA_RX_FIFO_EMPTY 0xc0aa 54 #define PLA_DMY_REG0 0xc0b0 55 #define PLA_FMC 0xc0b4 56 #define PLA_CFG_WOL 0xc0b6 57 #define PLA_TEREDO_CFG 0xc0bc 58 #define PLA_TEREDO_WAKE_BASE 0xc0c4 59 #define PLA_MAR 0xcd00 60 #define PLA_BACKUP 0xd000 61 #define PLA_BDC_CR 0xd1a0 62 #define PLA_TEREDO_TIMER 0xd2cc 63 #define PLA_REALWOW_TIMER 0xd2e8 64 #define PLA_UPHY_TIMER 0xd388 65 #define PLA_SUSPEND_FLAG 0xd38a 66 #define PLA_INDICATE_FALG 0xd38c 67 #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */ 68 #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */ 69 #define PLA_EXTRA_STATUS 0xd398 70 #define PLA_GPHY_CTRL 0xd3ae 71 #define PLA_POL_GPIO_CTRL 0xdc6a 72 #define PLA_EFUSE_DATA 0xdd00 73 #define PLA_EFUSE_CMD 0xdd02 74 #define PLA_LEDSEL 0xdd90 75 #define PLA_LED_FEATURE 0xdd92 76 #define PLA_PHYAR 0xde00 77 #define PLA_BOOT_CTRL 0xe004 78 #define PLA_LWAKE_CTRL_REG 0xe007 79 #define PLA_GPHY_INTR_IMR 0xe022 80 #define PLA_EEE_CR 0xe040 81 #define PLA_EEE_TXTWSYS 0xe04c 82 #define PLA_EEE_TXTWSYS_2P5G 0xe058 83 #define PLA_EEEP_CR 0xe080 84 #define PLA_MAC_PWR_CTRL 0xe0c0 85 #define PLA_MAC_PWR_CTRL2 0xe0ca 86 #define PLA_MAC_PWR_CTRL3 0xe0cc 87 #define PLA_MAC_PWR_CTRL4 0xe0ce 88 #define PLA_WDT6_CTRL 0xe428 89 #define PLA_TCR0 0xe610 90 #define PLA_TCR1 0xe612 91 #define PLA_MTPS 0xe615 92 #define PLA_TXFIFO_CTRL 0xe618 93 #define PLA_TXFIFO_FULL 0xe61a 94 #define PLA_RSTTALLY 0xe800 95 #define PLA_CR 0xe813 96 #define PLA_CRWECR 0xe81c 97 #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */ 98 #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */ 99 #define PLA_CONFIG5 0xe822 100 #define PLA_PHY_PWR 0xe84c 101 #define PLA_OOB_CTRL 0xe84f 102 #define PLA_CPCR 0xe854 103 #define PLA_MISC_0 0xe858 104 #define PLA_MISC_1 0xe85a 105 #define PLA_OCP_GPHY_BASE 0xe86c 106 #define PLA_TALLYCNT 0xe890 107 #define PLA_SFF_STS_7 0xe8de 108 #define PLA_PHYSTATUS 0xe908 109 #define PLA_CONFIG6 0xe90a /* CONFIG6 */ 110 #define PLA_USB_CFG 0xe952 111 #define PLA_BP_BA 0xfc26 112 #define PLA_BP_0 0xfc28 113 #define PLA_BP_1 0xfc2a 114 #define PLA_BP_2 0xfc2c 115 #define PLA_BP_3 0xfc2e 116 #define PLA_BP_4 0xfc30 117 #define PLA_BP_5 0xfc32 118 #define PLA_BP_6 0xfc34 119 #define PLA_BP_7 0xfc36 120 #define PLA_BP_EN 0xfc38 121 122 #define USB_USB2PHY 0xb41e 123 #define USB_SSPHYLINK1 0xb426 124 #define USB_SSPHYLINK2 0xb428 125 #define USB_L1_CTRL 0xb45e 126 #define USB_U2P3_CTRL 0xb460 127 #define USB_CSR_DUMMY1 0xb464 128 #define USB_CSR_DUMMY2 0xb466 129 #define USB_DEV_STAT 0xb808 130 #define USB_CONNECT_TIMER 0xcbf8 131 #define USB_MSC_TIMER 0xcbfc 132 #define USB_BURST_SIZE 0xcfc0 133 #define USB_FW_FIX_EN0 0xcfca 134 #define USB_FW_FIX_EN1 0xcfcc 135 #define USB_LPM_CONFIG 0xcfd8 136 #define USB_ECM_OPTION 0xcfee 137 #define USB_CSTMR 0xcfef /* RTL8153A */ 138 #define USB_MISC_2 0xcfff 139 #define USB_ECM_OP 0xd26b 140 #define USB_GPHY_CTRL 0xd284 141 #define USB_SPEED_OPTION 0xd32a 142 #define USB_FW_CTRL 0xd334 /* RTL8153B */ 143 #define USB_FC_TIMER 0xd340 144 #define USB_USB_CTRL 0xd406 145 #define USB_PHY_CTRL 0xd408 146 #define USB_TX_AGG 0xd40a 147 #define USB_RX_BUF_TH 0xd40c 148 #define USB_USB_TIMER 0xd428 149 #define USB_RX_EARLY_TIMEOUT 0xd42c 150 #define USB_RX_EARLY_SIZE 0xd42e 151 #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */ 152 #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */ 153 #define USB_TX_DMA 0xd434 154 #define USB_UPT_RXDMA_OWN 0xd437 155 #define USB_UPHY3_MDCMDIO 0xd480 156 #define USB_TOLERANCE 0xd490 157 #define USB_LPM_CTRL 0xd41a 158 #define USB_BMU_RESET 0xd4b0 159 #define USB_BMU_CONFIG 0xd4b4 160 #define USB_U1U2_TIMER 0xd4da 161 #define USB_FW_TASK 0xd4e8 /* RTL8153B */ 162 #define USB_RX_AGGR_NUM 0xd4ee 163 #define USB_UPS_CTRL 0xd800 164 #define USB_POWER_CUT 0xd80a 165 #define USB_MISC_0 0xd81a 166 #define USB_MISC_1 0xd81f 167 #define USB_AFE_CTRL2 0xd824 168 #define USB_UPHY_XTAL 0xd826 169 #define USB_UPS_CFG 0xd842 170 #define USB_UPS_FLAGS 0xd848 171 #define USB_WDT1_CTRL 0xe404 172 #define USB_WDT11_CTRL 0xe43c 173 #define USB_BP_BA PLA_BP_BA 174 #define USB_BP_0 PLA_BP_0 175 #define USB_BP_1 PLA_BP_1 176 #define USB_BP_2 PLA_BP_2 177 #define USB_BP_3 PLA_BP_3 178 #define USB_BP_4 PLA_BP_4 179 #define USB_BP_5 PLA_BP_5 180 #define USB_BP_6 PLA_BP_6 181 #define USB_BP_7 PLA_BP_7 182 #define USB_BP_EN PLA_BP_EN /* RTL8153A */ 183 #define USB_BP_8 0xfc38 /* RTL8153B */ 184 #define USB_BP_9 0xfc3a 185 #define USB_BP_10 0xfc3c 186 #define USB_BP_11 0xfc3e 187 #define USB_BP_12 0xfc40 188 #define USB_BP_13 0xfc42 189 #define USB_BP_14 0xfc44 190 #define USB_BP_15 0xfc46 191 #define USB_BP2_EN 0xfc48 192 193 /* OCP Registers */ 194 #define OCP_ALDPS_CONFIG 0x2010 195 #define OCP_EEE_CONFIG1 0x2080 196 #define OCP_EEE_CONFIG2 0x2092 197 #define OCP_EEE_CONFIG3 0x2094 198 #define OCP_BASE_MII 0xa400 199 #define OCP_EEE_AR 0xa41a 200 #define OCP_EEE_DATA 0xa41c 201 #define OCP_PHY_STATUS 0xa420 202 #define OCP_NCTL_CFG 0xa42c 203 #define OCP_POWER_CFG 0xa430 204 #define OCP_EEE_CFG 0xa432 205 #define OCP_SRAM_ADDR 0xa436 206 #define OCP_SRAM_DATA 0xa438 207 #define OCP_DOWN_SPEED 0xa442 208 #define OCP_EEE_ABLE 0xa5c4 209 #define OCP_EEE_ADV 0xa5d0 210 #define OCP_EEE_LPABLE 0xa5d2 211 #define OCP_10GBT_CTRL 0xa5d4 212 #define OCP_10GBT_STAT 0xa5d6 213 #define OCP_EEE_ADV2 0xa6d4 214 #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */ 215 #define OCP_PHY_PATCH_STAT 0xb800 216 #define OCP_PHY_PATCH_CMD 0xb820 217 #define OCP_PHY_LOCK 0xb82e 218 #define OCP_ADC_IOFFSET 0xbcfc 219 #define OCP_ADC_CFG 0xbc06 220 #define OCP_SYSCLK_CFG 0xc416 221 222 /* SRAM Register */ 223 #define SRAM_GREEN_CFG 0x8011 224 #define SRAM_LPF_CFG 0x8012 225 #define SRAM_GPHY_FW_VER 0x801e 226 #define SRAM_10M_AMP1 0x8080 227 #define SRAM_10M_AMP2 0x8082 228 #define SRAM_IMPEDANCE 0x8084 229 #define SRAM_PHY_LOCK 0xb82e 230 231 /* PLA_RCR */ 232 #define RCR_AAP 0x00000001 233 #define RCR_APM 0x00000002 234 #define RCR_AM 0x00000004 235 #define RCR_AB 0x00000008 236 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) 237 #define SLOT_EN BIT(11) 238 239 /* PLA_RCR1 */ 240 #define OUTER_VLAN BIT(7) 241 #define INNER_VLAN BIT(6) 242 243 /* PLA_RXFIFO_CTRL0 */ 244 #define RXFIFO_THR1_NORMAL 0x00080002 245 #define RXFIFO_THR1_OOB 0x01800003 246 247 /* PLA_RXFIFO_FULL */ 248 #define RXFIFO_FULL_MASK 0xfff 249 250 /* PLA_RXFIFO_CTRL1 */ 251 #define RXFIFO_THR2_FULL 0x00000060 252 #define RXFIFO_THR2_HIGH 0x00000038 253 #define RXFIFO_THR2_OOB 0x0000004a 254 #define RXFIFO_THR2_NORMAL 0x00a0 255 256 /* PLA_RXFIFO_CTRL2 */ 257 #define RXFIFO_THR3_FULL 0x00000078 258 #define RXFIFO_THR3_HIGH 0x00000048 259 #define RXFIFO_THR3_OOB 0x0000005a 260 #define RXFIFO_THR3_NORMAL 0x0110 261 262 /* PLA_TXFIFO_CTRL */ 263 #define TXFIFO_THR_NORMAL 0x00400008 264 #define TXFIFO_THR_NORMAL2 0x01000008 265 266 /* PLA_DMY_REG0 */ 267 #define ECM_ALDPS 0x0002 268 269 /* PLA_FMC */ 270 #define FMC_FCR_MCU_EN 0x0001 271 272 /* PLA_EEEP_CR */ 273 #define EEEP_CR_EEEP_TX 0x0002 274 275 /* PLA_WDT6_CTRL */ 276 #define WDT6_SET_MODE 0x0010 277 278 /* PLA_TCR0 */ 279 #define TCR0_TX_EMPTY 0x0800 280 #define TCR0_AUTO_FIFO 0x0080 281 282 /* PLA_TCR1 */ 283 #define VERSION_MASK 0x7cf0 284 #define IFG_MASK (BIT(3) | BIT(9) | BIT(8)) 285 #define IFG_144NS BIT(9) 286 #define IFG_96NS (BIT(9) | BIT(8)) 287 288 /* PLA_MTPS */ 289 #define MTPS_JUMBO (12 * 1024 / 64) 290 #define MTPS_DEFAULT (6 * 1024 / 64) 291 292 /* PLA_RSTTALLY */ 293 #define TALLY_RESET 0x0001 294 295 /* PLA_CR */ 296 #define CR_RST 0x10 297 #define CR_RE 0x08 298 #define CR_TE 0x04 299 300 /* PLA_CRWECR */ 301 #define CRWECR_NORAML 0x00 302 #define CRWECR_CONFIG 0xc0 303 304 /* PLA_OOB_CTRL */ 305 #define NOW_IS_OOB 0x80 306 #define TXFIFO_EMPTY 0x20 307 #define RXFIFO_EMPTY 0x10 308 #define LINK_LIST_READY 0x02 309 #define DIS_MCU_CLROOB 0x01 310 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) 311 312 /* PLA_MISC_1 */ 313 #define RXDY_GATED_EN 0x0008 314 315 /* PLA_SFF_STS_7 */ 316 #define RE_INIT_LL 0x8000 317 #define MCU_BORW_EN 0x4000 318 319 /* PLA_CPCR */ 320 #define FLOW_CTRL_EN BIT(0) 321 #define CPCR_RX_VLAN 0x0040 322 323 /* PLA_CFG_WOL */ 324 #define MAGIC_EN 0x0001 325 326 /* PLA_TEREDO_CFG */ 327 #define TEREDO_SEL 0x8000 328 #define TEREDO_WAKE_MASK 0x7f00 329 #define TEREDO_RS_EVENT_MASK 0x00fe 330 #define OOB_TEREDO_EN 0x0001 331 332 /* PLA_BDC_CR */ 333 #define ALDPS_PROXY_MODE 0x0001 334 335 /* PLA_EFUSE_CMD */ 336 #define EFUSE_READ_CMD BIT(15) 337 #define EFUSE_DATA_BIT16 BIT(7) 338 339 /* PLA_CONFIG34 */ 340 #define LINK_ON_WAKE_EN 0x0010 341 #define LINK_OFF_WAKE_EN 0x0008 342 343 /* PLA_CONFIG6 */ 344 #define LANWAKE_CLR_EN BIT(0) 345 346 /* PLA_USB_CFG */ 347 #define EN_XG_LIP BIT(1) 348 #define EN_G_LIP BIT(2) 349 350 /* PLA_CONFIG5 */ 351 #define BWF_EN 0x0040 352 #define MWF_EN 0x0020 353 #define UWF_EN 0x0010 354 #define LAN_WAKE_EN 0x0002 355 356 /* PLA_LED_FEATURE */ 357 #define LED_MODE_MASK 0x0700 358 359 /* PLA_PHY_PWR */ 360 #define TX_10M_IDLE_EN 0x0080 361 #define PFM_PWM_SWITCH 0x0040 362 #define TEST_IO_OFF BIT(4) 363 364 /* PLA_MAC_PWR_CTRL */ 365 #define D3_CLK_GATED_EN 0x00004000 366 #define MCU_CLK_RATIO 0x07010f07 367 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f 368 #define ALDPS_SPDWN_RATIO 0x0f87 369 370 /* PLA_MAC_PWR_CTRL2 */ 371 #define EEE_SPDWN_RATIO 0x8007 372 #define MAC_CLK_SPDWN_EN BIT(15) 373 #define EEE_SPDWN_RATIO_MASK 0xff 374 375 /* PLA_MAC_PWR_CTRL3 */ 376 #define PLA_MCU_SPDWN_EN BIT(14) 377 #define PKT_AVAIL_SPDWN_EN 0x0100 378 #define SUSPEND_SPDWN_EN 0x0004 379 #define U1U2_SPDWN_EN 0x0002 380 #define L1_SPDWN_EN 0x0001 381 382 /* PLA_MAC_PWR_CTRL4 */ 383 #define PWRSAVE_SPDWN_EN 0x1000 384 #define RXDV_SPDWN_EN 0x0800 385 #define TX10MIDLE_EN 0x0100 386 #define IDLE_SPDWN_EN BIT(6) 387 #define TP100_SPDWN_EN 0x0020 388 #define TP500_SPDWN_EN 0x0010 389 #define TP1000_SPDWN_EN 0x0008 390 #define EEE_SPDWN_EN 0x0001 391 392 /* PLA_GPHY_INTR_IMR */ 393 #define GPHY_STS_MSK 0x0001 394 #define SPEED_DOWN_MSK 0x0002 395 #define SPDWN_RXDV_MSK 0x0004 396 #define SPDWN_LINKCHG_MSK 0x0008 397 398 /* PLA_PHYAR */ 399 #define PHYAR_FLAG 0x80000000 400 401 /* PLA_EEE_CR */ 402 #define EEE_RX_EN 0x0001 403 #define EEE_TX_EN 0x0002 404 405 /* PLA_BOOT_CTRL */ 406 #define AUTOLOAD_DONE 0x0002 407 408 /* PLA_LWAKE_CTRL_REG */ 409 #define LANWAKE_PIN BIT(7) 410 411 /* PLA_SUSPEND_FLAG */ 412 #define LINK_CHG_EVENT BIT(0) 413 414 /* PLA_INDICATE_FALG */ 415 #define UPCOMING_RUNTIME_D3 BIT(0) 416 417 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */ 418 #define DEBUG_OE BIT(0) 419 #define DEBUG_LTSSM 0x0082 420 421 /* PLA_EXTRA_STATUS */ 422 #define CUR_LINK_OK BIT(15) 423 #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */ 424 #define LINK_CHANGE_FLAG BIT(8) 425 #define POLL_LINK_CHG BIT(0) 426 427 /* PLA_GPHY_CTRL */ 428 #define GPHY_FLASH BIT(1) 429 430 /* PLA_POL_GPIO_CTRL */ 431 #define DACK_DET_EN BIT(15) 432 #define POL_GPHY_PATCH BIT(4) 433 434 /* USB_USB2PHY */ 435 #define USB2PHY_SUSPEND 0x0001 436 #define USB2PHY_L1 0x0002 437 438 /* USB_SSPHYLINK1 */ 439 #define DELAY_PHY_PWR_CHG BIT(1) 440 441 /* USB_SSPHYLINK2 */ 442 #define pwd_dn_scale_mask 0x3ffe 443 #define pwd_dn_scale(x) ((x) << 1) 444 445 /* USB_CSR_DUMMY1 */ 446 #define DYNAMIC_BURST 0x0001 447 448 /* USB_CSR_DUMMY2 */ 449 #define EP4_FULL_FC 0x0001 450 451 /* USB_DEV_STAT */ 452 #define STAT_SPEED_MASK 0x0006 453 #define STAT_SPEED_HIGH 0x0000 454 #define STAT_SPEED_FULL 0x0002 455 456 /* USB_FW_FIX_EN0 */ 457 #define FW_FIX_SUSPEND BIT(14) 458 459 /* USB_FW_FIX_EN1 */ 460 #define FW_IP_RESET_EN BIT(9) 461 462 /* USB_LPM_CONFIG */ 463 #define LPM_U1U2_EN BIT(0) 464 465 /* USB_TX_AGG */ 466 #define TX_AGG_MAX_THRESHOLD 0x03 467 468 /* USB_RX_BUF_TH */ 469 #define RX_THR_SUPPER 0x0c350180 470 #define RX_THR_HIGH 0x7a120180 471 #define RX_THR_SLOW 0xffff0180 472 #define RX_THR_B 0x00010001 473 474 /* USB_TX_DMA */ 475 #define TEST_MODE_DISABLE 0x00000001 476 #define TX_SIZE_ADJUST1 0x00000100 477 478 /* USB_BMU_RESET */ 479 #define BMU_RESET_EP_IN 0x01 480 #define BMU_RESET_EP_OUT 0x02 481 482 /* USB_BMU_CONFIG */ 483 #define ACT_ODMA BIT(1) 484 485 /* USB_UPT_RXDMA_OWN */ 486 #define OWN_UPDATE BIT(0) 487 #define OWN_CLEAR BIT(1) 488 489 /* USB_FW_TASK */ 490 #define FC_PATCH_TASK BIT(1) 491 492 /* USB_RX_AGGR_NUM */ 493 #define RX_AGGR_NUM_MASK 0x1ff 494 495 /* USB_UPS_CTRL */ 496 #define POWER_CUT 0x0100 497 498 /* USB_PM_CTRL_STATUS */ 499 #define RESUME_INDICATE 0x0001 500 501 /* USB_ECM_OPTION */ 502 #define BYPASS_MAC_RESET BIT(5) 503 504 /* USB_CSTMR */ 505 #define FORCE_SUPER BIT(0) 506 507 /* USB_MISC_2 */ 508 #define UPS_FORCE_PWR_DOWN BIT(0) 509 510 /* USB_ECM_OP */ 511 #define EN_ALL_SPEED BIT(0) 512 513 /* USB_GPHY_CTRL */ 514 #define GPHY_PATCH_DONE BIT(2) 515 #define BYPASS_FLASH BIT(5) 516 #define BACKUP_RESTRORE BIT(6) 517 518 /* USB_SPEED_OPTION */ 519 #define RG_PWRDN_EN BIT(8) 520 #define ALL_SPEED_OFF BIT(9) 521 522 /* USB_FW_CTRL */ 523 #define FLOW_CTRL_PATCH_OPT BIT(1) 524 #define AUTO_SPEEDUP BIT(3) 525 #define FLOW_CTRL_PATCH_2 BIT(8) 526 527 /* USB_FC_TIMER */ 528 #define CTRL_TIMER_EN BIT(15) 529 530 /* USB_USB_CTRL */ 531 #define CDC_ECM_EN BIT(3) 532 #define RX_AGG_DISABLE 0x0010 533 #define RX_ZERO_EN 0x0080 534 535 /* USB_U2P3_CTRL */ 536 #define U2P3_ENABLE 0x0001 537 #define RX_DETECT8 BIT(3) 538 539 /* USB_POWER_CUT */ 540 #define PWR_EN 0x0001 541 #define PHASE2_EN 0x0008 542 #define UPS_EN BIT(4) 543 #define USP_PREWAKE BIT(5) 544 545 /* USB_MISC_0 */ 546 #define PCUT_STATUS 0x0001 547 548 /* USB_RX_EARLY_TIMEOUT */ 549 #define COALESCE_SUPER 85000U 550 #define COALESCE_HIGH 250000U 551 #define COALESCE_SLOW 524280U 552 553 /* USB_WDT1_CTRL */ 554 #define WTD1_EN BIT(0) 555 556 /* USB_WDT11_CTRL */ 557 #define TIMER11_EN 0x0001 558 559 /* USB_LPM_CTRL */ 560 /* bit 4 ~ 5: fifo empty boundary */ 561 #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */ 562 /* bit 2 ~ 3: LMP timer */ 563 #define LPM_TIMER_MASK 0x0c 564 #define LPM_TIMER_500MS 0x04 /* 500 ms */ 565 #define LPM_TIMER_500US 0x0c /* 500 us */ 566 #define ROK_EXIT_LPM 0x02 567 568 /* USB_AFE_CTRL2 */ 569 #define SEN_VAL_MASK 0xf800 570 #define SEN_VAL_NORMAL 0xa000 571 #define SEL_RXIDLE 0x0100 572 573 /* USB_UPHY_XTAL */ 574 #define OOBS_POLLING BIT(8) 575 576 /* USB_UPS_CFG */ 577 #define SAW_CNT_1MS_MASK 0x0fff 578 #define MID_REVERSE BIT(5) /* RTL8156A */ 579 580 /* USB_UPS_FLAGS */ 581 #define UPS_FLAGS_R_TUNE BIT(0) 582 #define UPS_FLAGS_EN_10M_CKDIV BIT(1) 583 #define UPS_FLAGS_250M_CKDIV BIT(2) 584 #define UPS_FLAGS_EN_ALDPS BIT(3) 585 #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4) 586 #define UPS_FLAGS_SPEED_MASK (0xf << 16) 587 #define ups_flags_speed(x) ((x) << 16) 588 #define UPS_FLAGS_EN_EEE BIT(20) 589 #define UPS_FLAGS_EN_500M_EEE BIT(21) 590 #define UPS_FLAGS_EN_EEE_CKDIV BIT(22) 591 #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23) 592 #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24) 593 #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25) 594 #define UPS_FLAGS_EN_GREEN BIT(26) 595 #define UPS_FLAGS_EN_FLOW_CTR BIT(27) 596 597 enum spd_duplex { 598 NWAY_10M_HALF, 599 NWAY_10M_FULL, 600 NWAY_100M_HALF, 601 NWAY_100M_FULL, 602 NWAY_1000M_FULL, 603 FORCE_10M_HALF, 604 FORCE_10M_FULL, 605 FORCE_100M_HALF, 606 FORCE_100M_FULL, 607 FORCE_1000M_FULL, 608 NWAY_2500M_FULL, 609 }; 610 611 /* OCP_ALDPS_CONFIG */ 612 #define ENPWRSAVE 0x8000 613 #define ENPDNPS 0x0200 614 #define LINKENA 0x0100 615 #define DIS_SDSAVE 0x0010 616 617 /* OCP_PHY_STATUS */ 618 #define PHY_STAT_MASK 0x0007 619 #define PHY_STAT_EXT_INIT 2 620 #define PHY_STAT_LAN_ON 3 621 #define PHY_STAT_PWRDN 5 622 623 /* OCP_NCTL_CFG */ 624 #define PGA_RETURN_EN BIT(1) 625 626 /* OCP_POWER_CFG */ 627 #define EEE_CLKDIV_EN 0x8000 628 #define EN_ALDPS 0x0004 629 #define EN_10M_PLLOFF 0x0001 630 631 /* OCP_EEE_CONFIG1 */ 632 #define RG_TXLPI_MSK_HFDUP 0x8000 633 #define RG_MATCLR_EN 0x4000 634 #define EEE_10_CAP 0x2000 635 #define EEE_NWAY_EN 0x1000 636 #define TX_QUIET_EN 0x0200 637 #define RX_QUIET_EN 0x0100 638 #define sd_rise_time_mask 0x0070 639 #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */ 640 #define RG_RXLPI_MSK_HFDUP 0x0008 641 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ 642 643 /* OCP_EEE_CONFIG2 */ 644 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ 645 #define RG_DACQUIET_EN 0x0400 646 #define RG_LDVQUIET_EN 0x0200 647 #define RG_CKRSEL 0x0020 648 #define RG_EEEPRG_EN 0x0010 649 650 /* OCP_EEE_CONFIG3 */ 651 #define fast_snr_mask 0xff80 652 #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */ 653 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ 654 #define MSK_PH 0x0006 /* bit 0 ~ 3 */ 655 656 /* OCP_EEE_AR */ 657 /* bit[15:14] function */ 658 #define FUN_ADDR 0x0000 659 #define FUN_DATA 0x4000 660 /* bit[4:0] device addr */ 661 662 /* OCP_EEE_CFG */ 663 #define CTAP_SHORT_EN 0x0040 664 #define EEE10_EN 0x0010 665 666 /* OCP_DOWN_SPEED */ 667 #define EN_EEE_CMODE BIT(14) 668 #define EN_EEE_1000 BIT(13) 669 #define EN_EEE_100 BIT(12) 670 #define EN_10M_CLKDIV BIT(11) 671 #define EN_10M_BGOFF 0x0080 672 673 /* OCP_10GBT_CTRL */ 674 #define RTL_ADV2_5G_F_R BIT(5) /* Advertise 2.5GBASE-T fast-retrain */ 675 676 /* OCP_PHY_STATE */ 677 #define TXDIS_STATE 0x01 678 #define ABD_STATE 0x02 679 680 /* OCP_PHY_PATCH_STAT */ 681 #define PATCH_READY BIT(6) 682 683 /* OCP_PHY_PATCH_CMD */ 684 #define PATCH_REQUEST BIT(4) 685 686 /* OCP_PHY_LOCK */ 687 #define PATCH_LOCK BIT(0) 688 689 /* OCP_ADC_CFG */ 690 #define CKADSEL_L 0x0100 691 #define ADC_EN 0x0080 692 #define EN_EMI_L 0x0040 693 694 /* OCP_SYSCLK_CFG */ 695 #define sysclk_div_expo(x) (min(x, 5) << 8) 696 #define clk_div_expo(x) (min(x, 5) << 4) 697 698 /* SRAM_GREEN_CFG */ 699 #define GREEN_ETH_EN BIT(15) 700 #define R_TUNE_EN BIT(11) 701 702 /* SRAM_LPF_CFG */ 703 #define LPF_AUTO_TUNE 0x8000 704 705 /* SRAM_10M_AMP1 */ 706 #define GDAC_IB_UPALL 0x0008 707 708 /* SRAM_10M_AMP2 */ 709 #define AMP_DN 0x0200 710 711 /* SRAM_IMPEDANCE */ 712 #define RX_DRIVING_MASK 0x6000 713 714 /* SRAM_PHY_LOCK */ 715 #define PHY_PATCH_LOCK 0x0001 716 717 /* MAC PASSTHRU */ 718 #define AD_MASK 0xfee0 719 #define BND_MASK 0x0004 720 #define BD_MASK 0x0001 721 #define EFUSE 0xcfdb 722 #define PASS_THRU_MASK 0x1 723 724 #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */ 725 726 enum rtl_register_content { 727 _2500bps = BIT(10), 728 _1250bps = BIT(9), 729 _500bps = BIT(8), 730 _tx_flow = BIT(6), 731 _rx_flow = BIT(5), 732 _1000bps = 0x10, 733 _100bps = 0x08, 734 _10bps = 0x04, 735 LINK_STATUS = 0x02, 736 FULL_DUP = 0x01, 737 }; 738 739 #define is_speed_2500(_speed) (((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS)) 740 #define is_flow_control(_speed) (((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow)) 741 742 #define RTL8152_MAX_TX 4 743 #define RTL8152_MAX_RX 10 744 #define INTBUFSIZE 2 745 #define TX_ALIGN 4 746 #define RX_ALIGN 8 747 748 #define RTL8152_RX_MAX_PENDING 4096 749 #define RTL8152_RXFG_HEADSZ 256 750 751 #define INTR_LINK 0x0004 752 753 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) 754 #define RTL8153_RMS RTL8153_MAX_PACKET 755 #define RTL8152_TX_TIMEOUT (5 * HZ) 756 #define mtu_to_size(m) ((m) + VLAN_ETH_HLEN + ETH_FCS_LEN) 757 #define size_to_mtu(s) ((s) - VLAN_ETH_HLEN - ETH_FCS_LEN) 758 #define rx_reserved_size(x) (mtu_to_size(x) + sizeof(struct rx_desc) + RX_ALIGN) 759 760 /* rtl8152 flags */ 761 enum rtl8152_flags { 762 RTL8152_UNPLUG = 0, 763 RTL8152_SET_RX_MODE, 764 WORK_ENABLE, 765 RTL8152_LINK_CHG, 766 SELECTIVE_SUSPEND, 767 PHY_RESET, 768 SCHEDULE_TASKLET, 769 GREEN_ETHERNET, 770 RX_EPROTO, 771 }; 772 773 #define DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB 0x721e 774 #define DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK 0x3054 775 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082 776 #define DEVICE_ID_THINKPAD_USB_C_DONGLE 0x720c 777 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387 778 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3 0x3062 779 780 struct tally_counter { 781 __le64 tx_packets; 782 __le64 rx_packets; 783 __le64 tx_errors; 784 __le32 rx_errors; 785 __le16 rx_missed; 786 __le16 align_errors; 787 __le32 tx_one_collision; 788 __le32 tx_multi_collision; 789 __le64 rx_unicast; 790 __le64 rx_broadcast; 791 __le32 rx_multicast; 792 __le16 tx_aborted; 793 __le16 tx_underrun; 794 }; 795 796 struct rx_desc { 797 __le32 opts1; 798 #define RX_LEN_MASK 0x7fff 799 800 __le32 opts2; 801 #define RD_UDP_CS BIT(23) 802 #define RD_TCP_CS BIT(22) 803 #define RD_IPV6_CS BIT(20) 804 #define RD_IPV4_CS BIT(19) 805 806 __le32 opts3; 807 #define IPF BIT(23) /* IP checksum fail */ 808 #define UDPF BIT(22) /* UDP checksum fail */ 809 #define TCPF BIT(21) /* TCP checksum fail */ 810 #define RX_VLAN_TAG BIT(16) 811 812 __le32 opts4; 813 __le32 opts5; 814 __le32 opts6; 815 }; 816 817 struct tx_desc { 818 __le32 opts1; 819 #define TX_FS BIT(31) /* First segment of a packet */ 820 #define TX_LS BIT(30) /* Final segment of a packet */ 821 #define GTSENDV4 BIT(28) 822 #define GTSENDV6 BIT(27) 823 #define GTTCPHO_SHIFT 18 824 #define GTTCPHO_MAX 0x7fU 825 #define TX_LEN_MAX 0x3ffffU 826 827 __le32 opts2; 828 #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */ 829 #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */ 830 #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */ 831 #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */ 832 #define MSS_SHIFT 17 833 #define MSS_MAX 0x7ffU 834 #define TCPHO_SHIFT 17 835 #define TCPHO_MAX 0x7ffU 836 #define TX_VLAN_TAG BIT(16) 837 }; 838 839 struct r8152; 840 841 struct rx_agg { 842 struct list_head list, info_list; 843 struct urb *urb; 844 struct r8152 *context; 845 struct page *page; 846 void *buffer; 847 }; 848 849 struct tx_agg { 850 struct list_head list; 851 struct urb *urb; 852 struct r8152 *context; 853 void *buffer; 854 void *head; 855 u32 skb_num; 856 u32 skb_len; 857 }; 858 859 struct r8152 { 860 unsigned long flags; 861 struct usb_device *udev; 862 struct napi_struct napi; 863 struct usb_interface *intf; 864 struct net_device *netdev; 865 struct urb *intr_urb; 866 struct tx_agg tx_info[RTL8152_MAX_TX]; 867 struct list_head rx_info, rx_used; 868 struct list_head rx_done, tx_free; 869 struct sk_buff_head tx_queue, rx_queue; 870 spinlock_t rx_lock, tx_lock; 871 struct delayed_work schedule, hw_phy_work; 872 struct mii_if_info mii; 873 struct mutex control; /* use for hw setting */ 874 #ifdef CONFIG_PM_SLEEP 875 struct notifier_block pm_notifier; 876 #endif 877 struct tasklet_struct tx_tl; 878 879 struct rtl_ops { 880 void (*init)(struct r8152 *tp); 881 int (*enable)(struct r8152 *tp); 882 void (*disable)(struct r8152 *tp); 883 void (*up)(struct r8152 *tp); 884 void (*down)(struct r8152 *tp); 885 void (*unload)(struct r8152 *tp); 886 int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee); 887 int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee); 888 bool (*in_nway)(struct r8152 *tp); 889 void (*hw_phy_cfg)(struct r8152 *tp); 890 void (*autosuspend_en)(struct r8152 *tp, bool enable); 891 void (*change_mtu)(struct r8152 *tp); 892 } rtl_ops; 893 894 struct ups_info { 895 u32 r_tune:1; 896 u32 _10m_ckdiv:1; 897 u32 _250m_ckdiv:1; 898 u32 aldps:1; 899 u32 lite_mode:2; 900 u32 speed_duplex:4; 901 u32 eee:1; 902 u32 eee_lite:1; 903 u32 eee_ckdiv:1; 904 u32 eee_plloff_100:1; 905 u32 eee_plloff_giga:1; 906 u32 eee_cmod_lv:1; 907 u32 green:1; 908 u32 flow_control:1; 909 u32 ctap_short_off:1; 910 } ups_info; 911 912 #define RTL_VER_SIZE 32 913 914 struct rtl_fw { 915 const char *fw_name; 916 const struct firmware *fw; 917 918 char version[RTL_VER_SIZE]; 919 int (*pre_fw)(struct r8152 *tp); 920 int (*post_fw)(struct r8152 *tp); 921 922 bool retry; 923 } rtl_fw; 924 925 atomic_t rx_count; 926 927 bool eee_en; 928 int intr_interval; 929 u32 saved_wolopts; 930 u32 msg_enable; 931 u32 tx_qlen; 932 u32 coalesce; 933 u32 advertising; 934 u32 rx_buf_sz; 935 u32 rx_copybreak; 936 u32 rx_pending; 937 u32 fc_pause_on, fc_pause_off; 938 939 unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out; 940 941 u32 support_2500full:1; 942 u32 lenovo_macpassthru:1; 943 u32 dell_tb_rx_agg_bug:1; 944 u16 ocp_base; 945 u16 speed; 946 u16 eee_adv; 947 u8 *intr_buff; 948 u8 version; 949 u8 duplex; 950 u8 autoneg; 951 }; 952 953 /** 954 * struct fw_block - block type and total length 955 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA, 956 * RTL_FW_USB and so on. 957 * @length: total length of the current block. 958 */ 959 struct fw_block { 960 __le32 type; 961 __le32 length; 962 } __packed; 963 964 /** 965 * struct fw_header - header of the firmware file 966 * @checksum: checksum of sha256 which is calculated from the whole file 967 * except the checksum field of the file. That is, calculate sha256 968 * from the version field to the end of the file. 969 * @version: version of this firmware. 970 * @blocks: the first firmware block of the file 971 */ 972 struct fw_header { 973 u8 checksum[32]; 974 char version[RTL_VER_SIZE]; 975 struct fw_block blocks[]; 976 } __packed; 977 978 enum rtl8152_fw_flags { 979 FW_FLAGS_USB = 0, 980 FW_FLAGS_PLA, 981 FW_FLAGS_START, 982 FW_FLAGS_STOP, 983 FW_FLAGS_NC, 984 FW_FLAGS_NC1, 985 FW_FLAGS_NC2, 986 FW_FLAGS_UC2, 987 FW_FLAGS_UC, 988 FW_FLAGS_SPEED_UP, 989 FW_FLAGS_VER, 990 }; 991 992 enum rtl8152_fw_fixup_cmd { 993 FW_FIXUP_AND = 0, 994 FW_FIXUP_OR, 995 FW_FIXUP_NOT, 996 FW_FIXUP_XOR, 997 }; 998 999 struct fw_phy_set { 1000 __le16 addr; 1001 __le16 data; 1002 } __packed; 1003 1004 struct fw_phy_speed_up { 1005 struct fw_block blk_hdr; 1006 __le16 fw_offset; 1007 __le16 version; 1008 __le16 fw_reg; 1009 __le16 reserved; 1010 char info[]; 1011 } __packed; 1012 1013 struct fw_phy_ver { 1014 struct fw_block blk_hdr; 1015 struct fw_phy_set ver; 1016 __le32 reserved; 1017 } __packed; 1018 1019 struct fw_phy_fixup { 1020 struct fw_block blk_hdr; 1021 struct fw_phy_set setting; 1022 __le16 bit_cmd; 1023 __le16 reserved; 1024 } __packed; 1025 1026 struct fw_phy_union { 1027 struct fw_block blk_hdr; 1028 __le16 fw_offset; 1029 __le16 fw_reg; 1030 struct fw_phy_set pre_set[2]; 1031 struct fw_phy_set bp[8]; 1032 struct fw_phy_set bp_en; 1033 u8 pre_num; 1034 u8 bp_num; 1035 char info[]; 1036 } __packed; 1037 1038 /** 1039 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB. 1040 * The layout of the firmware block is: 1041 * <struct fw_mac> + <info> + <firmware data>. 1042 * @blk_hdr: firmware descriptor (type, length) 1043 * @fw_offset: offset of the firmware binary data. The start address of 1044 * the data would be the address of struct fw_mac + @fw_offset. 1045 * @fw_reg: the register to load the firmware. Depends on chip. 1046 * @bp_ba_addr: the register to write break point base address. Depends on 1047 * chip. 1048 * @bp_ba_value: break point base address. Depends on chip. 1049 * @bp_en_addr: the register to write break point enabled mask. Depends 1050 * on chip. 1051 * @bp_en_value: break point enabled mask. Depends on the firmware. 1052 * @bp_start: the start register of break points. Depends on chip. 1053 * @bp_num: the break point number which needs to be set for this firmware. 1054 * Depends on the firmware. 1055 * @bp: break points. Depends on firmware. 1056 * @reserved: reserved space (unused) 1057 * @fw_ver_reg: the register to store the fw version. 1058 * @fw_ver_data: the firmware version of the current type. 1059 * @info: additional information for debugging, and is followed by the 1060 * binary data of firmware. 1061 */ 1062 struct fw_mac { 1063 struct fw_block blk_hdr; 1064 __le16 fw_offset; 1065 __le16 fw_reg; 1066 __le16 bp_ba_addr; 1067 __le16 bp_ba_value; 1068 __le16 bp_en_addr; 1069 __le16 bp_en_value; 1070 __le16 bp_start; 1071 __le16 bp_num; 1072 __le16 bp[16]; /* any value determined by firmware */ 1073 __le32 reserved; 1074 __le16 fw_ver_reg; 1075 u8 fw_ver_data; 1076 char info[]; 1077 } __packed; 1078 1079 /** 1080 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START. 1081 * This is used to set patch key when loading the firmware of PHY. 1082 * @blk_hdr: firmware descriptor (type, length) 1083 * @key_reg: the register to write the patch key. 1084 * @key_data: patch key. 1085 * @reserved: reserved space (unused) 1086 */ 1087 struct fw_phy_patch_key { 1088 struct fw_block blk_hdr; 1089 __le16 key_reg; 1090 __le16 key_data; 1091 __le32 reserved; 1092 } __packed; 1093 1094 /** 1095 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC. 1096 * The layout of the firmware block is: 1097 * <struct fw_phy_nc> + <info> + <firmware data>. 1098 * @blk_hdr: firmware descriptor (type, length) 1099 * @fw_offset: offset of the firmware binary data. The start address of 1100 * the data would be the address of struct fw_phy_nc + @fw_offset. 1101 * @fw_reg: the register to load the firmware. Depends on chip. 1102 * @ba_reg: the register to write the base address. Depends on chip. 1103 * @ba_data: base address. Depends on chip. 1104 * @patch_en_addr: the register of enabling patch mode. Depends on chip. 1105 * @patch_en_value: patch mode enabled mask. Depends on the firmware. 1106 * @mode_reg: the regitster of switching the mode. 1107 * @mode_pre: the mode needing to be set before loading the firmware. 1108 * @mode_post: the mode to be set when finishing to load the firmware. 1109 * @reserved: reserved space (unused) 1110 * @bp_start: the start register of break points. Depends on chip. 1111 * @bp_num: the break point number which needs to be set for this firmware. 1112 * Depends on the firmware. 1113 * @bp: break points. Depends on firmware. 1114 * @info: additional information for debugging, and is followed by the 1115 * binary data of firmware. 1116 */ 1117 struct fw_phy_nc { 1118 struct fw_block blk_hdr; 1119 __le16 fw_offset; 1120 __le16 fw_reg; 1121 __le16 ba_reg; 1122 __le16 ba_data; 1123 __le16 patch_en_addr; 1124 __le16 patch_en_value; 1125 __le16 mode_reg; 1126 __le16 mode_pre; 1127 __le16 mode_post; 1128 __le16 reserved; 1129 __le16 bp_start; 1130 __le16 bp_num; 1131 __le16 bp[4]; 1132 char info[]; 1133 } __packed; 1134 1135 enum rtl_fw_type { 1136 RTL_FW_END = 0, 1137 RTL_FW_PLA, 1138 RTL_FW_USB, 1139 RTL_FW_PHY_START, 1140 RTL_FW_PHY_STOP, 1141 RTL_FW_PHY_NC, 1142 RTL_FW_PHY_FIXUP, 1143 RTL_FW_PHY_UNION_NC, 1144 RTL_FW_PHY_UNION_NC1, 1145 RTL_FW_PHY_UNION_NC2, 1146 RTL_FW_PHY_UNION_UC2, 1147 RTL_FW_PHY_UNION_UC, 1148 RTL_FW_PHY_UNION_MISC, 1149 RTL_FW_PHY_SPEED_UP, 1150 RTL_FW_PHY_VER, 1151 }; 1152 1153 enum rtl_version { 1154 RTL_VER_UNKNOWN = 0, 1155 RTL_VER_01, 1156 RTL_VER_02, 1157 RTL_VER_03, 1158 RTL_VER_04, 1159 RTL_VER_05, 1160 RTL_VER_06, 1161 RTL_VER_07, 1162 RTL_VER_08, 1163 RTL_VER_09, 1164 1165 RTL_TEST_01, 1166 RTL_VER_10, 1167 RTL_VER_11, 1168 RTL_VER_12, 1169 RTL_VER_13, 1170 RTL_VER_14, 1171 RTL_VER_15, 1172 1173 RTL_VER_MAX 1174 }; 1175 1176 enum tx_csum_stat { 1177 TX_CSUM_SUCCESS = 0, 1178 TX_CSUM_TSO, 1179 TX_CSUM_NONE 1180 }; 1181 1182 #define RTL_ADVERTISED_10_HALF BIT(0) 1183 #define RTL_ADVERTISED_10_FULL BIT(1) 1184 #define RTL_ADVERTISED_100_HALF BIT(2) 1185 #define RTL_ADVERTISED_100_FULL BIT(3) 1186 #define RTL_ADVERTISED_1000_HALF BIT(4) 1187 #define RTL_ADVERTISED_1000_FULL BIT(5) 1188 #define RTL_ADVERTISED_2500_FULL BIT(6) 1189 1190 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). 1191 * The RTL chips use a 64 element hash table based on the Ethernet CRC. 1192 */ 1193 static const int multicast_filter_limit = 32; 1194 static unsigned int agg_buf_sz = 16384; 1195 1196 #define RTL_LIMITED_TSO_SIZE (size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc)) 1197 1198 static 1199 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 1200 { 1201 int ret; 1202 void *tmp; 1203 1204 tmp = kmalloc(size, GFP_KERNEL); 1205 if (!tmp) 1206 return -ENOMEM; 1207 1208 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_in, 1209 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 1210 value, index, tmp, size, 500); 1211 if (ret < 0) 1212 memset(data, 0xff, size); 1213 else 1214 memcpy(data, tmp, size); 1215 1216 kfree(tmp); 1217 1218 return ret; 1219 } 1220 1221 static 1222 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 1223 { 1224 int ret; 1225 void *tmp; 1226 1227 tmp = kmemdup(data, size, GFP_KERNEL); 1228 if (!tmp) 1229 return -ENOMEM; 1230 1231 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_out, 1232 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, 1233 value, index, tmp, size, 500); 1234 1235 kfree(tmp); 1236 1237 return ret; 1238 } 1239 1240 static void rtl_set_unplug(struct r8152 *tp) 1241 { 1242 if (tp->udev->state == USB_STATE_NOTATTACHED) { 1243 set_bit(RTL8152_UNPLUG, &tp->flags); 1244 smp_mb__after_atomic(); 1245 } 1246 } 1247 1248 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, 1249 void *data, u16 type) 1250 { 1251 u16 limit = 64; 1252 int ret = 0; 1253 1254 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1255 return -ENODEV; 1256 1257 /* both size and indix must be 4 bytes align */ 1258 if ((size & 3) || !size || (index & 3) || !data) 1259 return -EPERM; 1260 1261 if ((u32)index + (u32)size > 0xffff) 1262 return -EPERM; 1263 1264 while (size) { 1265 if (size > limit) { 1266 ret = get_registers(tp, index, type, limit, data); 1267 if (ret < 0) 1268 break; 1269 1270 index += limit; 1271 data += limit; 1272 size -= limit; 1273 } else { 1274 ret = get_registers(tp, index, type, size, data); 1275 if (ret < 0) 1276 break; 1277 1278 index += size; 1279 data += size; 1280 size = 0; 1281 break; 1282 } 1283 } 1284 1285 if (ret == -ENODEV) 1286 rtl_set_unplug(tp); 1287 1288 return ret; 1289 } 1290 1291 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, 1292 u16 size, void *data, u16 type) 1293 { 1294 int ret; 1295 u16 byteen_start, byteen_end, byen; 1296 u16 limit = 512; 1297 1298 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1299 return -ENODEV; 1300 1301 /* both size and indix must be 4 bytes align */ 1302 if ((size & 3) || !size || (index & 3) || !data) 1303 return -EPERM; 1304 1305 if ((u32)index + (u32)size > 0xffff) 1306 return -EPERM; 1307 1308 byteen_start = byteen & BYTE_EN_START_MASK; 1309 byteen_end = byteen & BYTE_EN_END_MASK; 1310 1311 byen = byteen_start | (byteen_start << 4); 1312 ret = set_registers(tp, index, type | byen, 4, data); 1313 if (ret < 0) 1314 goto error1; 1315 1316 index += 4; 1317 data += 4; 1318 size -= 4; 1319 1320 if (size) { 1321 size -= 4; 1322 1323 while (size) { 1324 if (size > limit) { 1325 ret = set_registers(tp, index, 1326 type | BYTE_EN_DWORD, 1327 limit, data); 1328 if (ret < 0) 1329 goto error1; 1330 1331 index += limit; 1332 data += limit; 1333 size -= limit; 1334 } else { 1335 ret = set_registers(tp, index, 1336 type | BYTE_EN_DWORD, 1337 size, data); 1338 if (ret < 0) 1339 goto error1; 1340 1341 index += size; 1342 data += size; 1343 size = 0; 1344 break; 1345 } 1346 } 1347 1348 byen = byteen_end | (byteen_end >> 4); 1349 ret = set_registers(tp, index, type | byen, 4, data); 1350 if (ret < 0) 1351 goto error1; 1352 } 1353 1354 error1: 1355 if (ret == -ENODEV) 1356 rtl_set_unplug(tp); 1357 1358 return ret; 1359 } 1360 1361 static inline 1362 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) 1363 { 1364 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); 1365 } 1366 1367 static inline 1368 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 1369 { 1370 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); 1371 } 1372 1373 static inline 1374 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 1375 { 1376 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); 1377 } 1378 1379 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) 1380 { 1381 __le32 data; 1382 1383 generic_ocp_read(tp, index, sizeof(data), &data, type); 1384 1385 return __le32_to_cpu(data); 1386 } 1387 1388 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) 1389 { 1390 __le32 tmp = __cpu_to_le32(data); 1391 1392 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); 1393 } 1394 1395 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) 1396 { 1397 u32 data; 1398 __le32 tmp; 1399 u16 byen = BYTE_EN_WORD; 1400 u8 shift = index & 2; 1401 1402 index &= ~3; 1403 byen <<= shift; 1404 1405 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen); 1406 1407 data = __le32_to_cpu(tmp); 1408 data >>= (shift * 8); 1409 data &= 0xffff; 1410 1411 return (u16)data; 1412 } 1413 1414 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) 1415 { 1416 u32 mask = 0xffff; 1417 __le32 tmp; 1418 u16 byen = BYTE_EN_WORD; 1419 u8 shift = index & 2; 1420 1421 data &= mask; 1422 1423 if (index & 2) { 1424 byen <<= shift; 1425 mask <<= (shift * 8); 1426 data <<= (shift * 8); 1427 index &= ~3; 1428 } 1429 1430 tmp = __cpu_to_le32(data); 1431 1432 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); 1433 } 1434 1435 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) 1436 { 1437 u32 data; 1438 __le32 tmp; 1439 u8 shift = index & 3; 1440 1441 index &= ~3; 1442 1443 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); 1444 1445 data = __le32_to_cpu(tmp); 1446 data >>= (shift * 8); 1447 data &= 0xff; 1448 1449 return (u8)data; 1450 } 1451 1452 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) 1453 { 1454 u32 mask = 0xff; 1455 __le32 tmp; 1456 u16 byen = BYTE_EN_BYTE; 1457 u8 shift = index & 3; 1458 1459 data &= mask; 1460 1461 if (index & 3) { 1462 byen <<= shift; 1463 mask <<= (shift * 8); 1464 data <<= (shift * 8); 1465 index &= ~3; 1466 } 1467 1468 tmp = __cpu_to_le32(data); 1469 1470 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); 1471 } 1472 1473 static u16 ocp_reg_read(struct r8152 *tp, u16 addr) 1474 { 1475 u16 ocp_base, ocp_index; 1476 1477 ocp_base = addr & 0xf000; 1478 if (ocp_base != tp->ocp_base) { 1479 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); 1480 tp->ocp_base = ocp_base; 1481 } 1482 1483 ocp_index = (addr & 0x0fff) | 0xb000; 1484 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); 1485 } 1486 1487 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) 1488 { 1489 u16 ocp_base, ocp_index; 1490 1491 ocp_base = addr & 0xf000; 1492 if (ocp_base != tp->ocp_base) { 1493 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); 1494 tp->ocp_base = ocp_base; 1495 } 1496 1497 ocp_index = (addr & 0x0fff) | 0xb000; 1498 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); 1499 } 1500 1501 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) 1502 { 1503 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); 1504 } 1505 1506 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) 1507 { 1508 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); 1509 } 1510 1511 static void sram_write(struct r8152 *tp, u16 addr, u16 data) 1512 { 1513 ocp_reg_write(tp, OCP_SRAM_ADDR, addr); 1514 ocp_reg_write(tp, OCP_SRAM_DATA, data); 1515 } 1516 1517 static u16 sram_read(struct r8152 *tp, u16 addr) 1518 { 1519 ocp_reg_write(tp, OCP_SRAM_ADDR, addr); 1520 return ocp_reg_read(tp, OCP_SRAM_DATA); 1521 } 1522 1523 static int read_mii_word(struct net_device *netdev, int phy_id, int reg) 1524 { 1525 struct r8152 *tp = netdev_priv(netdev); 1526 int ret; 1527 1528 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1529 return -ENODEV; 1530 1531 if (phy_id != R8152_PHY_ID) 1532 return -EINVAL; 1533 1534 ret = r8152_mdio_read(tp, reg); 1535 1536 return ret; 1537 } 1538 1539 static 1540 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) 1541 { 1542 struct r8152 *tp = netdev_priv(netdev); 1543 1544 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1545 return; 1546 1547 if (phy_id != R8152_PHY_ID) 1548 return; 1549 1550 r8152_mdio_write(tp, reg, val); 1551 } 1552 1553 static int 1554 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags); 1555 1556 static int 1557 rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, 1558 u32 advertising); 1559 1560 static int __rtl8152_set_mac_address(struct net_device *netdev, void *p, 1561 bool in_resume) 1562 { 1563 struct r8152 *tp = netdev_priv(netdev); 1564 struct sockaddr *addr = p; 1565 int ret = -EADDRNOTAVAIL; 1566 1567 if (!is_valid_ether_addr(addr->sa_data)) 1568 goto out1; 1569 1570 if (!in_resume) { 1571 ret = usb_autopm_get_interface(tp->intf); 1572 if (ret < 0) 1573 goto out1; 1574 } 1575 1576 mutex_lock(&tp->control); 1577 1578 eth_hw_addr_set(netdev, addr->sa_data); 1579 1580 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 1581 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); 1582 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 1583 1584 mutex_unlock(&tp->control); 1585 1586 if (!in_resume) 1587 usb_autopm_put_interface(tp->intf); 1588 out1: 1589 return ret; 1590 } 1591 1592 static int rtl8152_set_mac_address(struct net_device *netdev, void *p) 1593 { 1594 return __rtl8152_set_mac_address(netdev, p, false); 1595 } 1596 1597 /* Devices containing proper chips can support a persistent 1598 * host system provided MAC address. 1599 * Examples of this are Dell TB15 and Dell WD15 docks 1600 */ 1601 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa) 1602 { 1603 acpi_status status; 1604 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 1605 union acpi_object *obj; 1606 int ret = -EINVAL; 1607 u32 ocp_data; 1608 unsigned char buf[6]; 1609 char *mac_obj_name; 1610 acpi_object_type mac_obj_type; 1611 int mac_strlen; 1612 1613 if (tp->lenovo_macpassthru) { 1614 mac_obj_name = "\\MACA"; 1615 mac_obj_type = ACPI_TYPE_STRING; 1616 mac_strlen = 0x16; 1617 } else { 1618 /* test for -AD variant of RTL8153 */ 1619 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 1620 if ((ocp_data & AD_MASK) == 0x1000) { 1621 /* test for MAC address pass-through bit */ 1622 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE); 1623 if ((ocp_data & PASS_THRU_MASK) != 1) { 1624 netif_dbg(tp, probe, tp->netdev, 1625 "No efuse for RTL8153-AD MAC pass through\n"); 1626 return -ENODEV; 1627 } 1628 } else { 1629 /* test for RTL8153-BND and RTL8153-BD */ 1630 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); 1631 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) { 1632 netif_dbg(tp, probe, tp->netdev, 1633 "Invalid variant for MAC pass through\n"); 1634 return -ENODEV; 1635 } 1636 } 1637 1638 mac_obj_name = "\\_SB.AMAC"; 1639 mac_obj_type = ACPI_TYPE_BUFFER; 1640 mac_strlen = 0x17; 1641 } 1642 1643 /* returns _AUXMAC_#AABBCCDDEEFF# */ 1644 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer); 1645 obj = (union acpi_object *)buffer.pointer; 1646 if (!ACPI_SUCCESS(status)) 1647 return -ENODEV; 1648 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) { 1649 netif_warn(tp, probe, tp->netdev, 1650 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n", 1651 obj->type, obj->string.length); 1652 goto amacout; 1653 } 1654 1655 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 || 1656 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) { 1657 netif_warn(tp, probe, tp->netdev, 1658 "Invalid header when reading pass-thru MAC addr\n"); 1659 goto amacout; 1660 } 1661 ret = hex2bin(buf, obj->string.pointer + 9, 6); 1662 if (!(ret == 0 && is_valid_ether_addr(buf))) { 1663 netif_warn(tp, probe, tp->netdev, 1664 "Invalid MAC for pass-thru MAC addr: %d, %pM\n", 1665 ret, buf); 1666 ret = -EINVAL; 1667 goto amacout; 1668 } 1669 memcpy(sa->sa_data, buf, 6); 1670 netif_info(tp, probe, tp->netdev, 1671 "Using pass-thru MAC addr %pM\n", sa->sa_data); 1672 1673 amacout: 1674 kfree(obj); 1675 return ret; 1676 } 1677 1678 static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa) 1679 { 1680 struct net_device *dev = tp->netdev; 1681 int ret; 1682 1683 sa->sa_family = dev->type; 1684 1685 ret = eth_platform_get_mac_address(&tp->udev->dev, sa->sa_data); 1686 if (ret < 0) { 1687 if (tp->version == RTL_VER_01) { 1688 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data); 1689 } else { 1690 /* if device doesn't support MAC pass through this will 1691 * be expected to be non-zero 1692 */ 1693 ret = vendor_mac_passthru_addr_read(tp, sa); 1694 if (ret < 0) 1695 ret = pla_ocp_read(tp, PLA_BACKUP, 8, 1696 sa->sa_data); 1697 } 1698 } 1699 1700 if (ret < 0) { 1701 netif_err(tp, probe, dev, "Get ether addr fail\n"); 1702 } else if (!is_valid_ether_addr(sa->sa_data)) { 1703 netif_err(tp, probe, dev, "Invalid ether addr %pM\n", 1704 sa->sa_data); 1705 eth_hw_addr_random(dev); 1706 ether_addr_copy(sa->sa_data, dev->dev_addr); 1707 netif_info(tp, probe, dev, "Random ether addr %pM\n", 1708 sa->sa_data); 1709 return 0; 1710 } 1711 1712 return ret; 1713 } 1714 1715 static int set_ethernet_addr(struct r8152 *tp, bool in_resume) 1716 { 1717 struct net_device *dev = tp->netdev; 1718 struct sockaddr sa; 1719 int ret; 1720 1721 ret = determine_ethernet_addr(tp, &sa); 1722 if (ret < 0) 1723 return ret; 1724 1725 if (tp->version == RTL_VER_01) 1726 eth_hw_addr_set(dev, sa.sa_data); 1727 else 1728 ret = __rtl8152_set_mac_address(dev, &sa, in_resume); 1729 1730 return ret; 1731 } 1732 1733 static void read_bulk_callback(struct urb *urb) 1734 { 1735 struct net_device *netdev; 1736 int status = urb->status; 1737 struct rx_agg *agg; 1738 struct r8152 *tp; 1739 unsigned long flags; 1740 1741 agg = urb->context; 1742 if (!agg) 1743 return; 1744 1745 tp = agg->context; 1746 if (!tp) 1747 return; 1748 1749 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1750 return; 1751 1752 if (!test_bit(WORK_ENABLE, &tp->flags)) 1753 return; 1754 1755 netdev = tp->netdev; 1756 1757 /* When link down, the driver would cancel all bulks. */ 1758 /* This avoid the re-submitting bulk */ 1759 if (!netif_carrier_ok(netdev)) 1760 return; 1761 1762 usb_mark_last_busy(tp->udev); 1763 1764 switch (status) { 1765 case 0: 1766 if (urb->actual_length < ETH_ZLEN) 1767 break; 1768 1769 spin_lock_irqsave(&tp->rx_lock, flags); 1770 list_add_tail(&agg->list, &tp->rx_done); 1771 spin_unlock_irqrestore(&tp->rx_lock, flags); 1772 napi_schedule(&tp->napi); 1773 return; 1774 case -ESHUTDOWN: 1775 rtl_set_unplug(tp); 1776 netif_device_detach(tp->netdev); 1777 return; 1778 case -EPROTO: 1779 urb->actual_length = 0; 1780 spin_lock_irqsave(&tp->rx_lock, flags); 1781 list_add_tail(&agg->list, &tp->rx_done); 1782 spin_unlock_irqrestore(&tp->rx_lock, flags); 1783 set_bit(RX_EPROTO, &tp->flags); 1784 schedule_delayed_work(&tp->schedule, 1); 1785 return; 1786 case -ENOENT: 1787 return; /* the urb is in unlink state */ 1788 case -ETIME: 1789 if (net_ratelimit()) 1790 netdev_warn(netdev, "maybe reset is needed?\n"); 1791 break; 1792 default: 1793 if (net_ratelimit()) 1794 netdev_warn(netdev, "Rx status %d\n", status); 1795 break; 1796 } 1797 1798 r8152_submit_rx(tp, agg, GFP_ATOMIC); 1799 } 1800 1801 static void write_bulk_callback(struct urb *urb) 1802 { 1803 struct net_device_stats *stats; 1804 struct net_device *netdev; 1805 struct tx_agg *agg; 1806 struct r8152 *tp; 1807 unsigned long flags; 1808 int status = urb->status; 1809 1810 agg = urb->context; 1811 if (!agg) 1812 return; 1813 1814 tp = agg->context; 1815 if (!tp) 1816 return; 1817 1818 netdev = tp->netdev; 1819 stats = &netdev->stats; 1820 if (status) { 1821 if (net_ratelimit()) 1822 netdev_warn(netdev, "Tx status %d\n", status); 1823 stats->tx_errors += agg->skb_num; 1824 } else { 1825 stats->tx_packets += agg->skb_num; 1826 stats->tx_bytes += agg->skb_len; 1827 } 1828 1829 spin_lock_irqsave(&tp->tx_lock, flags); 1830 list_add_tail(&agg->list, &tp->tx_free); 1831 spin_unlock_irqrestore(&tp->tx_lock, flags); 1832 1833 usb_autopm_put_interface_async(tp->intf); 1834 1835 if (!netif_carrier_ok(netdev)) 1836 return; 1837 1838 if (!test_bit(WORK_ENABLE, &tp->flags)) 1839 return; 1840 1841 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1842 return; 1843 1844 if (!skb_queue_empty(&tp->tx_queue)) 1845 tasklet_schedule(&tp->tx_tl); 1846 } 1847 1848 static void intr_callback(struct urb *urb) 1849 { 1850 struct r8152 *tp; 1851 __le16 *d; 1852 int status = urb->status; 1853 int res; 1854 1855 tp = urb->context; 1856 if (!tp) 1857 return; 1858 1859 if (!test_bit(WORK_ENABLE, &tp->flags)) 1860 return; 1861 1862 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1863 return; 1864 1865 switch (status) { 1866 case 0: /* success */ 1867 break; 1868 case -ECONNRESET: /* unlink */ 1869 case -ESHUTDOWN: 1870 netif_device_detach(tp->netdev); 1871 fallthrough; 1872 case -ENOENT: 1873 case -EPROTO: 1874 netif_info(tp, intr, tp->netdev, 1875 "Stop submitting intr, status %d\n", status); 1876 return; 1877 case -EOVERFLOW: 1878 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); 1879 goto resubmit; 1880 /* -EPIPE: should clear the halt */ 1881 default: 1882 netif_info(tp, intr, tp->netdev, "intr status %d\n", status); 1883 goto resubmit; 1884 } 1885 1886 d = urb->transfer_buffer; 1887 if (INTR_LINK & __le16_to_cpu(d[0])) { 1888 if (!netif_carrier_ok(tp->netdev)) { 1889 set_bit(RTL8152_LINK_CHG, &tp->flags); 1890 schedule_delayed_work(&tp->schedule, 0); 1891 } 1892 } else { 1893 if (netif_carrier_ok(tp->netdev)) { 1894 netif_stop_queue(tp->netdev); 1895 set_bit(RTL8152_LINK_CHG, &tp->flags); 1896 schedule_delayed_work(&tp->schedule, 0); 1897 } 1898 } 1899 1900 resubmit: 1901 res = usb_submit_urb(urb, GFP_ATOMIC); 1902 if (res == -ENODEV) { 1903 rtl_set_unplug(tp); 1904 netif_device_detach(tp->netdev); 1905 } else if (res) { 1906 netif_err(tp, intr, tp->netdev, 1907 "can't resubmit intr, status %d\n", res); 1908 } 1909 } 1910 1911 static inline void *rx_agg_align(void *data) 1912 { 1913 return (void *)ALIGN((uintptr_t)data, RX_ALIGN); 1914 } 1915 1916 static inline void *tx_agg_align(void *data) 1917 { 1918 return (void *)ALIGN((uintptr_t)data, TX_ALIGN); 1919 } 1920 1921 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg) 1922 { 1923 list_del(&agg->info_list); 1924 1925 usb_free_urb(agg->urb); 1926 put_page(agg->page); 1927 kfree(agg); 1928 1929 atomic_dec(&tp->rx_count); 1930 } 1931 1932 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags) 1933 { 1934 struct net_device *netdev = tp->netdev; 1935 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; 1936 unsigned int order = get_order(tp->rx_buf_sz); 1937 struct rx_agg *rx_agg; 1938 unsigned long flags; 1939 1940 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node); 1941 if (!rx_agg) 1942 return NULL; 1943 1944 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order); 1945 if (!rx_agg->page) 1946 goto free_rx; 1947 1948 rx_agg->buffer = page_address(rx_agg->page); 1949 1950 rx_agg->urb = usb_alloc_urb(0, mflags); 1951 if (!rx_agg->urb) 1952 goto free_buf; 1953 1954 rx_agg->context = tp; 1955 1956 INIT_LIST_HEAD(&rx_agg->list); 1957 INIT_LIST_HEAD(&rx_agg->info_list); 1958 spin_lock_irqsave(&tp->rx_lock, flags); 1959 list_add_tail(&rx_agg->info_list, &tp->rx_info); 1960 spin_unlock_irqrestore(&tp->rx_lock, flags); 1961 1962 atomic_inc(&tp->rx_count); 1963 1964 return rx_agg; 1965 1966 free_buf: 1967 __free_pages(rx_agg->page, order); 1968 free_rx: 1969 kfree(rx_agg); 1970 return NULL; 1971 } 1972 1973 static void free_all_mem(struct r8152 *tp) 1974 { 1975 struct rx_agg *agg, *agg_next; 1976 unsigned long flags; 1977 int i; 1978 1979 spin_lock_irqsave(&tp->rx_lock, flags); 1980 1981 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list) 1982 free_rx_agg(tp, agg); 1983 1984 spin_unlock_irqrestore(&tp->rx_lock, flags); 1985 1986 WARN_ON(atomic_read(&tp->rx_count)); 1987 1988 for (i = 0; i < RTL8152_MAX_TX; i++) { 1989 usb_free_urb(tp->tx_info[i].urb); 1990 tp->tx_info[i].urb = NULL; 1991 1992 kfree(tp->tx_info[i].buffer); 1993 tp->tx_info[i].buffer = NULL; 1994 tp->tx_info[i].head = NULL; 1995 } 1996 1997 usb_free_urb(tp->intr_urb); 1998 tp->intr_urb = NULL; 1999 2000 kfree(tp->intr_buff); 2001 tp->intr_buff = NULL; 2002 } 2003 2004 static int alloc_all_mem(struct r8152 *tp) 2005 { 2006 struct net_device *netdev = tp->netdev; 2007 struct usb_interface *intf = tp->intf; 2008 struct usb_host_interface *alt = intf->cur_altsetting; 2009 struct usb_host_endpoint *ep_intr = alt->endpoint + 2; 2010 int node, i; 2011 2012 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; 2013 2014 spin_lock_init(&tp->rx_lock); 2015 spin_lock_init(&tp->tx_lock); 2016 INIT_LIST_HEAD(&tp->rx_info); 2017 INIT_LIST_HEAD(&tp->tx_free); 2018 INIT_LIST_HEAD(&tp->rx_done); 2019 skb_queue_head_init(&tp->tx_queue); 2020 skb_queue_head_init(&tp->rx_queue); 2021 atomic_set(&tp->rx_count, 0); 2022 2023 for (i = 0; i < RTL8152_MAX_RX; i++) { 2024 if (!alloc_rx_agg(tp, GFP_KERNEL)) 2025 goto err1; 2026 } 2027 2028 for (i = 0; i < RTL8152_MAX_TX; i++) { 2029 struct urb *urb; 2030 u8 *buf; 2031 2032 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); 2033 if (!buf) 2034 goto err1; 2035 2036 if (buf != tx_agg_align(buf)) { 2037 kfree(buf); 2038 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL, 2039 node); 2040 if (!buf) 2041 goto err1; 2042 } 2043 2044 urb = usb_alloc_urb(0, GFP_KERNEL); 2045 if (!urb) { 2046 kfree(buf); 2047 goto err1; 2048 } 2049 2050 INIT_LIST_HEAD(&tp->tx_info[i].list); 2051 tp->tx_info[i].context = tp; 2052 tp->tx_info[i].urb = urb; 2053 tp->tx_info[i].buffer = buf; 2054 tp->tx_info[i].head = tx_agg_align(buf); 2055 2056 list_add_tail(&tp->tx_info[i].list, &tp->tx_free); 2057 } 2058 2059 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); 2060 if (!tp->intr_urb) 2061 goto err1; 2062 2063 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); 2064 if (!tp->intr_buff) 2065 goto err1; 2066 2067 tp->intr_interval = (int)ep_intr->desc.bInterval; 2068 usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr, 2069 tp->intr_buff, INTBUFSIZE, intr_callback, 2070 tp, tp->intr_interval); 2071 2072 return 0; 2073 2074 err1: 2075 free_all_mem(tp); 2076 return -ENOMEM; 2077 } 2078 2079 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) 2080 { 2081 struct tx_agg *agg = NULL; 2082 unsigned long flags; 2083 2084 if (list_empty(&tp->tx_free)) 2085 return NULL; 2086 2087 spin_lock_irqsave(&tp->tx_lock, flags); 2088 if (!list_empty(&tp->tx_free)) { 2089 struct list_head *cursor; 2090 2091 cursor = tp->tx_free.next; 2092 list_del_init(cursor); 2093 agg = list_entry(cursor, struct tx_agg, list); 2094 } 2095 spin_unlock_irqrestore(&tp->tx_lock, flags); 2096 2097 return agg; 2098 } 2099 2100 /* r8152_csum_workaround() 2101 * The hw limits the value of the transport offset. When the offset is out of 2102 * range, calculate the checksum by sw. 2103 */ 2104 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb, 2105 struct sk_buff_head *list) 2106 { 2107 if (skb_shinfo(skb)->gso_size) { 2108 netdev_features_t features = tp->netdev->features; 2109 struct sk_buff *segs, *seg, *next; 2110 struct sk_buff_head seg_list; 2111 2112 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6); 2113 segs = skb_gso_segment(skb, features); 2114 if (IS_ERR(segs) || !segs) 2115 goto drop; 2116 2117 __skb_queue_head_init(&seg_list); 2118 2119 skb_list_walk_safe(segs, seg, next) { 2120 skb_mark_not_on_list(seg); 2121 __skb_queue_tail(&seg_list, seg); 2122 } 2123 2124 skb_queue_splice(&seg_list, list); 2125 dev_kfree_skb(skb); 2126 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 2127 if (skb_checksum_help(skb) < 0) 2128 goto drop; 2129 2130 __skb_queue_head(list, skb); 2131 } else { 2132 struct net_device_stats *stats; 2133 2134 drop: 2135 stats = &tp->netdev->stats; 2136 stats->tx_dropped++; 2137 dev_kfree_skb(skb); 2138 } 2139 } 2140 2141 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb) 2142 { 2143 if (skb_vlan_tag_present(skb)) { 2144 u32 opts2; 2145 2146 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)); 2147 desc->opts2 |= cpu_to_le32(opts2); 2148 } 2149 } 2150 2151 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) 2152 { 2153 u32 opts2 = le32_to_cpu(desc->opts2); 2154 2155 if (opts2 & RX_VLAN_TAG) 2156 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 2157 swab16(opts2 & 0xffff)); 2158 } 2159 2160 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, 2161 struct sk_buff *skb, u32 len) 2162 { 2163 u32 mss = skb_shinfo(skb)->gso_size; 2164 u32 opts1, opts2 = 0; 2165 int ret = TX_CSUM_SUCCESS; 2166 2167 WARN_ON_ONCE(len > TX_LEN_MAX); 2168 2169 opts1 = len | TX_FS | TX_LS; 2170 2171 if (mss) { 2172 u32 transport_offset = (u32)skb_transport_offset(skb); 2173 2174 if (transport_offset > GTTCPHO_MAX) { 2175 netif_warn(tp, tx_err, tp->netdev, 2176 "Invalid transport offset 0x%x for TSO\n", 2177 transport_offset); 2178 ret = TX_CSUM_TSO; 2179 goto unavailable; 2180 } 2181 2182 switch (vlan_get_protocol(skb)) { 2183 case htons(ETH_P_IP): 2184 opts1 |= GTSENDV4; 2185 break; 2186 2187 case htons(ETH_P_IPV6): 2188 if (skb_cow_head(skb, 0)) { 2189 ret = TX_CSUM_TSO; 2190 goto unavailable; 2191 } 2192 tcp_v6_gso_csum_prep(skb); 2193 opts1 |= GTSENDV6; 2194 break; 2195 2196 default: 2197 WARN_ON_ONCE(1); 2198 break; 2199 } 2200 2201 opts1 |= transport_offset << GTTCPHO_SHIFT; 2202 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT; 2203 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 2204 u32 transport_offset = (u32)skb_transport_offset(skb); 2205 u8 ip_protocol; 2206 2207 if (transport_offset > TCPHO_MAX) { 2208 netif_warn(tp, tx_err, tp->netdev, 2209 "Invalid transport offset 0x%x\n", 2210 transport_offset); 2211 ret = TX_CSUM_NONE; 2212 goto unavailable; 2213 } 2214 2215 switch (vlan_get_protocol(skb)) { 2216 case htons(ETH_P_IP): 2217 opts2 |= IPV4_CS; 2218 ip_protocol = ip_hdr(skb)->protocol; 2219 break; 2220 2221 case htons(ETH_P_IPV6): 2222 opts2 |= IPV6_CS; 2223 ip_protocol = ipv6_hdr(skb)->nexthdr; 2224 break; 2225 2226 default: 2227 ip_protocol = IPPROTO_RAW; 2228 break; 2229 } 2230 2231 if (ip_protocol == IPPROTO_TCP) 2232 opts2 |= TCP_CS; 2233 else if (ip_protocol == IPPROTO_UDP) 2234 opts2 |= UDP_CS; 2235 else 2236 WARN_ON_ONCE(1); 2237 2238 opts2 |= transport_offset << TCPHO_SHIFT; 2239 } 2240 2241 desc->opts2 = cpu_to_le32(opts2); 2242 desc->opts1 = cpu_to_le32(opts1); 2243 2244 unavailable: 2245 return ret; 2246 } 2247 2248 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) 2249 { 2250 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; 2251 int remain, ret; 2252 u8 *tx_data; 2253 2254 __skb_queue_head_init(&skb_head); 2255 spin_lock(&tx_queue->lock); 2256 skb_queue_splice_init(tx_queue, &skb_head); 2257 spin_unlock(&tx_queue->lock); 2258 2259 tx_data = agg->head; 2260 agg->skb_num = 0; 2261 agg->skb_len = 0; 2262 remain = agg_buf_sz; 2263 2264 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { 2265 struct tx_desc *tx_desc; 2266 struct sk_buff *skb; 2267 unsigned int len; 2268 2269 skb = __skb_dequeue(&skb_head); 2270 if (!skb) 2271 break; 2272 2273 len = skb->len + sizeof(*tx_desc); 2274 2275 if (len > remain) { 2276 __skb_queue_head(&skb_head, skb); 2277 break; 2278 } 2279 2280 tx_data = tx_agg_align(tx_data); 2281 tx_desc = (struct tx_desc *)tx_data; 2282 2283 if (r8152_tx_csum(tp, tx_desc, skb, skb->len)) { 2284 r8152_csum_workaround(tp, skb, &skb_head); 2285 continue; 2286 } 2287 2288 rtl_tx_vlan_tag(tx_desc, skb); 2289 2290 tx_data += sizeof(*tx_desc); 2291 2292 len = skb->len; 2293 if (skb_copy_bits(skb, 0, tx_data, len) < 0) { 2294 struct net_device_stats *stats = &tp->netdev->stats; 2295 2296 stats->tx_dropped++; 2297 dev_kfree_skb_any(skb); 2298 tx_data -= sizeof(*tx_desc); 2299 continue; 2300 } 2301 2302 tx_data += len; 2303 agg->skb_len += len; 2304 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1; 2305 2306 dev_kfree_skb_any(skb); 2307 2308 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head); 2309 2310 if (tp->dell_tb_rx_agg_bug) 2311 break; 2312 } 2313 2314 if (!skb_queue_empty(&skb_head)) { 2315 spin_lock(&tx_queue->lock); 2316 skb_queue_splice(&skb_head, tx_queue); 2317 spin_unlock(&tx_queue->lock); 2318 } 2319 2320 netif_tx_lock(tp->netdev); 2321 2322 if (netif_queue_stopped(tp->netdev) && 2323 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) 2324 netif_wake_queue(tp->netdev); 2325 2326 netif_tx_unlock(tp->netdev); 2327 2328 ret = usb_autopm_get_interface_async(tp->intf); 2329 if (ret < 0) 2330 goto out_tx_fill; 2331 2332 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out, 2333 agg->head, (int)(tx_data - (u8 *)agg->head), 2334 (usb_complete_t)write_bulk_callback, agg); 2335 2336 ret = usb_submit_urb(agg->urb, GFP_ATOMIC); 2337 if (ret < 0) 2338 usb_autopm_put_interface_async(tp->intf); 2339 2340 out_tx_fill: 2341 return ret; 2342 } 2343 2344 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc) 2345 { 2346 u8 checksum = CHECKSUM_NONE; 2347 u32 opts2, opts3; 2348 2349 if (!(tp->netdev->features & NETIF_F_RXCSUM)) 2350 goto return_result; 2351 2352 opts2 = le32_to_cpu(rx_desc->opts2); 2353 opts3 = le32_to_cpu(rx_desc->opts3); 2354 2355 if (opts2 & RD_IPV4_CS) { 2356 if (opts3 & IPF) 2357 checksum = CHECKSUM_NONE; 2358 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) 2359 checksum = CHECKSUM_UNNECESSARY; 2360 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) 2361 checksum = CHECKSUM_UNNECESSARY; 2362 } else if (opts2 & RD_IPV6_CS) { 2363 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) 2364 checksum = CHECKSUM_UNNECESSARY; 2365 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) 2366 checksum = CHECKSUM_UNNECESSARY; 2367 } 2368 2369 return_result: 2370 return checksum; 2371 } 2372 2373 static inline bool rx_count_exceed(struct r8152 *tp) 2374 { 2375 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX; 2376 } 2377 2378 static inline int agg_offset(struct rx_agg *agg, void *addr) 2379 { 2380 return (int)(addr - agg->buffer); 2381 } 2382 2383 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags) 2384 { 2385 struct rx_agg *agg, *agg_next, *agg_free = NULL; 2386 unsigned long flags; 2387 2388 spin_lock_irqsave(&tp->rx_lock, flags); 2389 2390 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) { 2391 if (page_count(agg->page) == 1) { 2392 if (!agg_free) { 2393 list_del_init(&agg->list); 2394 agg_free = agg; 2395 continue; 2396 } 2397 if (rx_count_exceed(tp)) { 2398 list_del_init(&agg->list); 2399 free_rx_agg(tp, agg); 2400 } 2401 break; 2402 } 2403 } 2404 2405 spin_unlock_irqrestore(&tp->rx_lock, flags); 2406 2407 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending) 2408 agg_free = alloc_rx_agg(tp, mflags); 2409 2410 return agg_free; 2411 } 2412 2413 static int rx_bottom(struct r8152 *tp, int budget) 2414 { 2415 unsigned long flags; 2416 struct list_head *cursor, *next, rx_queue; 2417 int ret = 0, work_done = 0; 2418 struct napi_struct *napi = &tp->napi; 2419 2420 if (!skb_queue_empty(&tp->rx_queue)) { 2421 while (work_done < budget) { 2422 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue); 2423 struct net_device *netdev = tp->netdev; 2424 struct net_device_stats *stats = &netdev->stats; 2425 unsigned int pkt_len; 2426 2427 if (!skb) 2428 break; 2429 2430 pkt_len = skb->len; 2431 napi_gro_receive(napi, skb); 2432 work_done++; 2433 stats->rx_packets++; 2434 stats->rx_bytes += pkt_len; 2435 } 2436 } 2437 2438 if (list_empty(&tp->rx_done)) 2439 goto out1; 2440 2441 clear_bit(RX_EPROTO, &tp->flags); 2442 INIT_LIST_HEAD(&rx_queue); 2443 spin_lock_irqsave(&tp->rx_lock, flags); 2444 list_splice_init(&tp->rx_done, &rx_queue); 2445 spin_unlock_irqrestore(&tp->rx_lock, flags); 2446 2447 list_for_each_safe(cursor, next, &rx_queue) { 2448 struct rx_desc *rx_desc; 2449 struct rx_agg *agg, *agg_free; 2450 int len_used = 0; 2451 struct urb *urb; 2452 u8 *rx_data; 2453 2454 list_del_init(cursor); 2455 2456 agg = list_entry(cursor, struct rx_agg, list); 2457 urb = agg->urb; 2458 if (urb->status != 0 || urb->actual_length < ETH_ZLEN) 2459 goto submit; 2460 2461 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC); 2462 2463 rx_desc = agg->buffer; 2464 rx_data = agg->buffer; 2465 len_used += sizeof(struct rx_desc); 2466 2467 while (urb->actual_length > len_used) { 2468 struct net_device *netdev = tp->netdev; 2469 struct net_device_stats *stats = &netdev->stats; 2470 unsigned int pkt_len, rx_frag_head_sz; 2471 struct sk_buff *skb; 2472 2473 /* limit the skb numbers for rx_queue */ 2474 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000)) 2475 break; 2476 2477 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; 2478 if (pkt_len < ETH_ZLEN) 2479 break; 2480 2481 len_used += pkt_len; 2482 if (urb->actual_length < len_used) 2483 break; 2484 2485 pkt_len -= ETH_FCS_LEN; 2486 rx_data += sizeof(struct rx_desc); 2487 2488 if (!agg_free || tp->rx_copybreak > pkt_len) 2489 rx_frag_head_sz = pkt_len; 2490 else 2491 rx_frag_head_sz = tp->rx_copybreak; 2492 2493 skb = napi_alloc_skb(napi, rx_frag_head_sz); 2494 if (!skb) { 2495 stats->rx_dropped++; 2496 goto find_next_rx; 2497 } 2498 2499 skb->ip_summed = r8152_rx_csum(tp, rx_desc); 2500 memcpy(skb->data, rx_data, rx_frag_head_sz); 2501 skb_put(skb, rx_frag_head_sz); 2502 pkt_len -= rx_frag_head_sz; 2503 rx_data += rx_frag_head_sz; 2504 if (pkt_len) { 2505 skb_add_rx_frag(skb, 0, agg->page, 2506 agg_offset(agg, rx_data), 2507 pkt_len, 2508 SKB_DATA_ALIGN(pkt_len)); 2509 get_page(agg->page); 2510 } 2511 2512 skb->protocol = eth_type_trans(skb, netdev); 2513 rtl_rx_vlan_tag(rx_desc, skb); 2514 if (work_done < budget) { 2515 work_done++; 2516 stats->rx_packets++; 2517 stats->rx_bytes += skb->len; 2518 napi_gro_receive(napi, skb); 2519 } else { 2520 __skb_queue_tail(&tp->rx_queue, skb); 2521 } 2522 2523 find_next_rx: 2524 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN); 2525 rx_desc = (struct rx_desc *)rx_data; 2526 len_used = agg_offset(agg, rx_data); 2527 len_used += sizeof(struct rx_desc); 2528 } 2529 2530 WARN_ON(!agg_free && page_count(agg->page) > 1); 2531 2532 if (agg_free) { 2533 spin_lock_irqsave(&tp->rx_lock, flags); 2534 if (page_count(agg->page) == 1) { 2535 list_add(&agg_free->list, &tp->rx_used); 2536 } else { 2537 list_add_tail(&agg->list, &tp->rx_used); 2538 agg = agg_free; 2539 urb = agg->urb; 2540 } 2541 spin_unlock_irqrestore(&tp->rx_lock, flags); 2542 } 2543 2544 submit: 2545 if (!ret) { 2546 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); 2547 } else { 2548 urb->actual_length = 0; 2549 list_add_tail(&agg->list, next); 2550 } 2551 } 2552 2553 if (!list_empty(&rx_queue)) { 2554 spin_lock_irqsave(&tp->rx_lock, flags); 2555 list_splice_tail(&rx_queue, &tp->rx_done); 2556 spin_unlock_irqrestore(&tp->rx_lock, flags); 2557 } 2558 2559 out1: 2560 return work_done; 2561 } 2562 2563 static void tx_bottom(struct r8152 *tp) 2564 { 2565 int res; 2566 2567 do { 2568 struct net_device *netdev = tp->netdev; 2569 struct tx_agg *agg; 2570 2571 if (skb_queue_empty(&tp->tx_queue)) 2572 break; 2573 2574 agg = r8152_get_tx_agg(tp); 2575 if (!agg) 2576 break; 2577 2578 res = r8152_tx_agg_fill(tp, agg); 2579 if (!res) 2580 continue; 2581 2582 if (res == -ENODEV) { 2583 rtl_set_unplug(tp); 2584 netif_device_detach(netdev); 2585 } else { 2586 struct net_device_stats *stats = &netdev->stats; 2587 unsigned long flags; 2588 2589 netif_warn(tp, tx_err, netdev, 2590 "failed tx_urb %d\n", res); 2591 stats->tx_dropped += agg->skb_num; 2592 2593 spin_lock_irqsave(&tp->tx_lock, flags); 2594 list_add_tail(&agg->list, &tp->tx_free); 2595 spin_unlock_irqrestore(&tp->tx_lock, flags); 2596 } 2597 } while (res == 0); 2598 } 2599 2600 static void bottom_half(struct tasklet_struct *t) 2601 { 2602 struct r8152 *tp = from_tasklet(tp, t, tx_tl); 2603 2604 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 2605 return; 2606 2607 if (!test_bit(WORK_ENABLE, &tp->flags)) 2608 return; 2609 2610 /* When link down, the driver would cancel all bulks. */ 2611 /* This avoid the re-submitting bulk */ 2612 if (!netif_carrier_ok(tp->netdev)) 2613 return; 2614 2615 clear_bit(SCHEDULE_TASKLET, &tp->flags); 2616 2617 tx_bottom(tp); 2618 } 2619 2620 static int r8152_poll(struct napi_struct *napi, int budget) 2621 { 2622 struct r8152 *tp = container_of(napi, struct r8152, napi); 2623 int work_done; 2624 2625 work_done = rx_bottom(tp, budget); 2626 2627 if (work_done < budget) { 2628 if (!napi_complete_done(napi, work_done)) 2629 goto out; 2630 if (!list_empty(&tp->rx_done)) 2631 napi_schedule(napi); 2632 } 2633 2634 out: 2635 return work_done; 2636 } 2637 2638 static 2639 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) 2640 { 2641 int ret; 2642 2643 /* The rx would be stopped, so skip submitting */ 2644 if (test_bit(RTL8152_UNPLUG, &tp->flags) || 2645 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) 2646 return 0; 2647 2648 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in, 2649 agg->buffer, tp->rx_buf_sz, 2650 (usb_complete_t)read_bulk_callback, agg); 2651 2652 ret = usb_submit_urb(agg->urb, mem_flags); 2653 if (ret == -ENODEV) { 2654 rtl_set_unplug(tp); 2655 netif_device_detach(tp->netdev); 2656 } else if (ret) { 2657 struct urb *urb = agg->urb; 2658 unsigned long flags; 2659 2660 urb->actual_length = 0; 2661 spin_lock_irqsave(&tp->rx_lock, flags); 2662 list_add_tail(&agg->list, &tp->rx_done); 2663 spin_unlock_irqrestore(&tp->rx_lock, flags); 2664 2665 netif_err(tp, rx_err, tp->netdev, 2666 "Couldn't submit rx[%p], ret = %d\n", agg, ret); 2667 2668 napi_schedule(&tp->napi); 2669 } 2670 2671 return ret; 2672 } 2673 2674 static void rtl_drop_queued_tx(struct r8152 *tp) 2675 { 2676 struct net_device_stats *stats = &tp->netdev->stats; 2677 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; 2678 struct sk_buff *skb; 2679 2680 if (skb_queue_empty(tx_queue)) 2681 return; 2682 2683 __skb_queue_head_init(&skb_head); 2684 spin_lock_bh(&tx_queue->lock); 2685 skb_queue_splice_init(tx_queue, &skb_head); 2686 spin_unlock_bh(&tx_queue->lock); 2687 2688 while ((skb = __skb_dequeue(&skb_head))) { 2689 dev_kfree_skb(skb); 2690 stats->tx_dropped++; 2691 } 2692 } 2693 2694 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue) 2695 { 2696 struct r8152 *tp = netdev_priv(netdev); 2697 2698 netif_warn(tp, tx_err, netdev, "Tx timeout\n"); 2699 2700 usb_queue_reset_device(tp->intf); 2701 } 2702 2703 static void rtl8152_set_rx_mode(struct net_device *netdev) 2704 { 2705 struct r8152 *tp = netdev_priv(netdev); 2706 2707 if (netif_carrier_ok(netdev)) { 2708 set_bit(RTL8152_SET_RX_MODE, &tp->flags); 2709 schedule_delayed_work(&tp->schedule, 0); 2710 } 2711 } 2712 2713 static void _rtl8152_set_rx_mode(struct net_device *netdev) 2714 { 2715 struct r8152 *tp = netdev_priv(netdev); 2716 u32 mc_filter[2]; /* Multicast hash filter */ 2717 __le32 tmp[2]; 2718 u32 ocp_data; 2719 2720 netif_stop_queue(netdev); 2721 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 2722 ocp_data &= ~RCR_ACPT_ALL; 2723 ocp_data |= RCR_AB | RCR_APM; 2724 2725 if (netdev->flags & IFF_PROMISC) { 2726 /* Unconditionally log net taps. */ 2727 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); 2728 ocp_data |= RCR_AM | RCR_AAP; 2729 mc_filter[1] = 0xffffffff; 2730 mc_filter[0] = 0xffffffff; 2731 } else if ((netdev->flags & IFF_MULTICAST && 2732 netdev_mc_count(netdev) > multicast_filter_limit) || 2733 (netdev->flags & IFF_ALLMULTI)) { 2734 /* Too many to filter perfectly -- accept all multicasts. */ 2735 ocp_data |= RCR_AM; 2736 mc_filter[1] = 0xffffffff; 2737 mc_filter[0] = 0xffffffff; 2738 } else { 2739 mc_filter[1] = 0; 2740 mc_filter[0] = 0; 2741 2742 if (netdev->flags & IFF_MULTICAST) { 2743 struct netdev_hw_addr *ha; 2744 2745 netdev_for_each_mc_addr(ha, netdev) { 2746 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; 2747 2748 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); 2749 ocp_data |= RCR_AM; 2750 } 2751 } 2752 } 2753 2754 tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); 2755 tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); 2756 2757 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); 2758 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 2759 netif_wake_queue(netdev); 2760 } 2761 2762 static netdev_features_t 2763 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, 2764 netdev_features_t features) 2765 { 2766 u32 mss = skb_shinfo(skb)->gso_size; 2767 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; 2768 2769 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && 2770 skb_transport_offset(skb) > max_offset) 2771 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 2772 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz) 2773 features &= ~NETIF_F_GSO_MASK; 2774 2775 return features; 2776 } 2777 2778 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, 2779 struct net_device *netdev) 2780 { 2781 struct r8152 *tp = netdev_priv(netdev); 2782 2783 skb_tx_timestamp(skb); 2784 2785 skb_queue_tail(&tp->tx_queue, skb); 2786 2787 if (!list_empty(&tp->tx_free)) { 2788 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { 2789 set_bit(SCHEDULE_TASKLET, &tp->flags); 2790 schedule_delayed_work(&tp->schedule, 0); 2791 } else { 2792 usb_mark_last_busy(tp->udev); 2793 tasklet_schedule(&tp->tx_tl); 2794 } 2795 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { 2796 netif_stop_queue(netdev); 2797 } 2798 2799 return NETDEV_TX_OK; 2800 } 2801 2802 static void r8152b_reset_packet_filter(struct r8152 *tp) 2803 { 2804 u32 ocp_data; 2805 2806 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); 2807 ocp_data &= ~FMC_FCR_MCU_EN; 2808 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 2809 ocp_data |= FMC_FCR_MCU_EN; 2810 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 2811 } 2812 2813 static void rtl8152_nic_reset(struct r8152 *tp) 2814 { 2815 u32 ocp_data; 2816 int i; 2817 2818 switch (tp->version) { 2819 case RTL_TEST_01: 2820 case RTL_VER_10: 2821 case RTL_VER_11: 2822 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 2823 ocp_data &= ~CR_TE; 2824 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 2825 2826 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET); 2827 ocp_data &= ~BMU_RESET_EP_IN; 2828 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 2829 2830 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 2831 ocp_data |= CDC_ECM_EN; 2832 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 2833 2834 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 2835 ocp_data &= ~CR_RE; 2836 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 2837 2838 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET); 2839 ocp_data |= BMU_RESET_EP_IN; 2840 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 2841 2842 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 2843 ocp_data &= ~CDC_ECM_EN; 2844 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 2845 break; 2846 2847 default: 2848 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); 2849 2850 for (i = 0; i < 1000; i++) { 2851 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) 2852 break; 2853 usleep_range(100, 400); 2854 } 2855 break; 2856 } 2857 } 2858 2859 static void set_tx_qlen(struct r8152 *tp) 2860 { 2861 tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc)); 2862 } 2863 2864 static inline u16 rtl8152_get_speed(struct r8152 *tp) 2865 { 2866 return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); 2867 } 2868 2869 static void rtl_eee_plus_en(struct r8152 *tp, bool enable) 2870 { 2871 u32 ocp_data; 2872 2873 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); 2874 if (enable) 2875 ocp_data |= EEEP_CR_EEEP_TX; 2876 else 2877 ocp_data &= ~EEEP_CR_EEEP_TX; 2878 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); 2879 } 2880 2881 static void rtl_set_eee_plus(struct r8152 *tp) 2882 { 2883 if (rtl8152_get_speed(tp) & _10bps) 2884 rtl_eee_plus_en(tp, true); 2885 else 2886 rtl_eee_plus_en(tp, false); 2887 } 2888 2889 static void rxdy_gated_en(struct r8152 *tp, bool enable) 2890 { 2891 u32 ocp_data; 2892 2893 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); 2894 if (enable) 2895 ocp_data |= RXDY_GATED_EN; 2896 else 2897 ocp_data &= ~RXDY_GATED_EN; 2898 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); 2899 } 2900 2901 static int rtl_start_rx(struct r8152 *tp) 2902 { 2903 struct rx_agg *agg, *agg_next; 2904 struct list_head tmp_list; 2905 unsigned long flags; 2906 int ret = 0, i = 0; 2907 2908 INIT_LIST_HEAD(&tmp_list); 2909 2910 spin_lock_irqsave(&tp->rx_lock, flags); 2911 2912 INIT_LIST_HEAD(&tp->rx_done); 2913 INIT_LIST_HEAD(&tp->rx_used); 2914 2915 list_splice_init(&tp->rx_info, &tmp_list); 2916 2917 spin_unlock_irqrestore(&tp->rx_lock, flags); 2918 2919 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 2920 INIT_LIST_HEAD(&agg->list); 2921 2922 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */ 2923 if (++i > RTL8152_MAX_RX) { 2924 spin_lock_irqsave(&tp->rx_lock, flags); 2925 list_add_tail(&agg->list, &tp->rx_used); 2926 spin_unlock_irqrestore(&tp->rx_lock, flags); 2927 } else if (unlikely(ret < 0)) { 2928 spin_lock_irqsave(&tp->rx_lock, flags); 2929 list_add_tail(&agg->list, &tp->rx_done); 2930 spin_unlock_irqrestore(&tp->rx_lock, flags); 2931 } else { 2932 ret = r8152_submit_rx(tp, agg, GFP_KERNEL); 2933 } 2934 } 2935 2936 spin_lock_irqsave(&tp->rx_lock, flags); 2937 WARN_ON(!list_empty(&tp->rx_info)); 2938 list_splice(&tmp_list, &tp->rx_info); 2939 spin_unlock_irqrestore(&tp->rx_lock, flags); 2940 2941 return ret; 2942 } 2943 2944 static int rtl_stop_rx(struct r8152 *tp) 2945 { 2946 struct rx_agg *agg, *agg_next; 2947 struct list_head tmp_list; 2948 unsigned long flags; 2949 2950 INIT_LIST_HEAD(&tmp_list); 2951 2952 /* The usb_kill_urb() couldn't be used in atomic. 2953 * Therefore, move the list of rx_info to a tmp one. 2954 * Then, list_for_each_entry_safe could be used without 2955 * spin lock. 2956 */ 2957 2958 spin_lock_irqsave(&tp->rx_lock, flags); 2959 list_splice_init(&tp->rx_info, &tmp_list); 2960 spin_unlock_irqrestore(&tp->rx_lock, flags); 2961 2962 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 2963 /* At least RTL8152_MAX_RX rx_agg have the page_count being 2964 * equal to 1, so the other ones could be freed safely. 2965 */ 2966 if (page_count(agg->page) > 1) 2967 free_rx_agg(tp, agg); 2968 else 2969 usb_kill_urb(agg->urb); 2970 } 2971 2972 /* Move back the list of temp to the rx_info */ 2973 spin_lock_irqsave(&tp->rx_lock, flags); 2974 WARN_ON(!list_empty(&tp->rx_info)); 2975 list_splice(&tmp_list, &tp->rx_info); 2976 spin_unlock_irqrestore(&tp->rx_lock, flags); 2977 2978 while (!skb_queue_empty(&tp->rx_queue)) 2979 dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); 2980 2981 return 0; 2982 } 2983 2984 static void rtl_set_ifg(struct r8152 *tp, u16 speed) 2985 { 2986 u32 ocp_data; 2987 2988 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1); 2989 ocp_data &= ~IFG_MASK; 2990 if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) { 2991 ocp_data |= IFG_144NS; 2992 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data); 2993 2994 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 2995 ocp_data &= ~TX10MIDLE_EN; 2996 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 2997 } else { 2998 ocp_data |= IFG_96NS; 2999 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data); 3000 3001 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 3002 ocp_data |= TX10MIDLE_EN; 3003 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 3004 } 3005 } 3006 3007 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) 3008 { 3009 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, 3010 OWN_UPDATE | OWN_CLEAR); 3011 } 3012 3013 static int rtl_enable(struct r8152 *tp) 3014 { 3015 u32 ocp_data; 3016 3017 r8152b_reset_packet_filter(tp); 3018 3019 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 3020 ocp_data |= CR_RE | CR_TE; 3021 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 3022 3023 switch (tp->version) { 3024 case RTL_VER_08: 3025 case RTL_VER_09: 3026 case RTL_VER_14: 3027 r8153b_rx_agg_chg_indicate(tp); 3028 break; 3029 default: 3030 break; 3031 } 3032 3033 rxdy_gated_en(tp, false); 3034 3035 return 0; 3036 } 3037 3038 static int rtl8152_enable(struct r8152 *tp) 3039 { 3040 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 3041 return -ENODEV; 3042 3043 set_tx_qlen(tp); 3044 rtl_set_eee_plus(tp); 3045 3046 return rtl_enable(tp); 3047 } 3048 3049 static void r8153_set_rx_early_timeout(struct r8152 *tp) 3050 { 3051 u32 ocp_data = tp->coalesce / 8; 3052 3053 switch (tp->version) { 3054 case RTL_VER_03: 3055 case RTL_VER_04: 3056 case RTL_VER_05: 3057 case RTL_VER_06: 3058 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3059 ocp_data); 3060 break; 3061 3062 case RTL_VER_08: 3063 case RTL_VER_09: 3064 case RTL_VER_14: 3065 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout 3066 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns. 3067 */ 3068 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3069 128 / 8); 3070 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, 3071 ocp_data); 3072 break; 3073 3074 case RTL_VER_10: 3075 case RTL_VER_11: 3076 case RTL_VER_12: 3077 case RTL_VER_13: 3078 case RTL_VER_15: 3079 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3080 640 / 8); 3081 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, 3082 ocp_data); 3083 r8153b_rx_agg_chg_indicate(tp); 3084 break; 3085 3086 default: 3087 break; 3088 } 3089 } 3090 3091 static void r8153_set_rx_early_size(struct r8152 *tp) 3092 { 3093 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu); 3094 3095 switch (tp->version) { 3096 case RTL_VER_03: 3097 case RTL_VER_04: 3098 case RTL_VER_05: 3099 case RTL_VER_06: 3100 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3101 ocp_data / 4); 3102 break; 3103 case RTL_VER_08: 3104 case RTL_VER_09: 3105 case RTL_VER_14: 3106 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3107 ocp_data / 8); 3108 break; 3109 case RTL_TEST_01: 3110 case RTL_VER_10: 3111 case RTL_VER_11: 3112 case RTL_VER_12: 3113 case RTL_VER_13: 3114 case RTL_VER_15: 3115 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3116 ocp_data / 8); 3117 r8153b_rx_agg_chg_indicate(tp); 3118 break; 3119 default: 3120 WARN_ON_ONCE(1); 3121 break; 3122 } 3123 } 3124 3125 static int rtl8153_enable(struct r8152 *tp) 3126 { 3127 u32 ocp_data; 3128 3129 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 3130 return -ENODEV; 3131 3132 set_tx_qlen(tp); 3133 rtl_set_eee_plus(tp); 3134 r8153_set_rx_early_timeout(tp); 3135 r8153_set_rx_early_size(tp); 3136 3137 rtl_set_ifg(tp, rtl8152_get_speed(tp)); 3138 3139 switch (tp->version) { 3140 case RTL_VER_09: 3141 case RTL_VER_14: 3142 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 3143 ocp_data &= ~FC_PATCH_TASK; 3144 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 3145 usleep_range(1000, 2000); 3146 ocp_data |= FC_PATCH_TASK; 3147 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 3148 break; 3149 default: 3150 break; 3151 } 3152 3153 return rtl_enable(tp); 3154 } 3155 3156 static void rtl_disable(struct r8152 *tp) 3157 { 3158 u32 ocp_data; 3159 int i; 3160 3161 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 3162 rtl_drop_queued_tx(tp); 3163 return; 3164 } 3165 3166 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 3167 ocp_data &= ~RCR_ACPT_ALL; 3168 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 3169 3170 rtl_drop_queued_tx(tp); 3171 3172 for (i = 0; i < RTL8152_MAX_TX; i++) 3173 usb_kill_urb(tp->tx_info[i].urb); 3174 3175 rxdy_gated_en(tp, true); 3176 3177 for (i = 0; i < 1000; i++) { 3178 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 3179 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) 3180 break; 3181 usleep_range(1000, 2000); 3182 } 3183 3184 for (i = 0; i < 1000; i++) { 3185 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) 3186 break; 3187 usleep_range(1000, 2000); 3188 } 3189 3190 rtl_stop_rx(tp); 3191 3192 rtl8152_nic_reset(tp); 3193 } 3194 3195 static void r8152_power_cut_en(struct r8152 *tp, bool enable) 3196 { 3197 u32 ocp_data; 3198 3199 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); 3200 if (enable) 3201 ocp_data |= POWER_CUT; 3202 else 3203 ocp_data &= ~POWER_CUT; 3204 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); 3205 3206 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); 3207 ocp_data &= ~RESUME_INDICATE; 3208 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); 3209 } 3210 3211 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) 3212 { 3213 u32 ocp_data; 3214 3215 switch (tp->version) { 3216 case RTL_VER_01: 3217 case RTL_VER_02: 3218 case RTL_VER_03: 3219 case RTL_VER_04: 3220 case RTL_VER_05: 3221 case RTL_VER_06: 3222 case RTL_VER_07: 3223 case RTL_VER_08: 3224 case RTL_VER_09: 3225 case RTL_VER_14: 3226 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); 3227 if (enable) 3228 ocp_data |= CPCR_RX_VLAN; 3229 else 3230 ocp_data &= ~CPCR_RX_VLAN; 3231 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); 3232 break; 3233 3234 case RTL_TEST_01: 3235 case RTL_VER_10: 3236 case RTL_VER_11: 3237 case RTL_VER_12: 3238 case RTL_VER_13: 3239 case RTL_VER_15: 3240 default: 3241 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1); 3242 if (enable) 3243 ocp_data |= OUTER_VLAN | INNER_VLAN; 3244 else 3245 ocp_data &= ~(OUTER_VLAN | INNER_VLAN); 3246 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data); 3247 break; 3248 } 3249 } 3250 3251 static int rtl8152_set_features(struct net_device *dev, 3252 netdev_features_t features) 3253 { 3254 netdev_features_t changed = features ^ dev->features; 3255 struct r8152 *tp = netdev_priv(dev); 3256 int ret; 3257 3258 ret = usb_autopm_get_interface(tp->intf); 3259 if (ret < 0) 3260 goto out; 3261 3262 mutex_lock(&tp->control); 3263 3264 if (changed & NETIF_F_HW_VLAN_CTAG_RX) { 3265 if (features & NETIF_F_HW_VLAN_CTAG_RX) 3266 rtl_rx_vlan_en(tp, true); 3267 else 3268 rtl_rx_vlan_en(tp, false); 3269 } 3270 3271 mutex_unlock(&tp->control); 3272 3273 usb_autopm_put_interface(tp->intf); 3274 3275 out: 3276 return ret; 3277 } 3278 3279 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) 3280 3281 static u32 __rtl_get_wol(struct r8152 *tp) 3282 { 3283 u32 ocp_data; 3284 u32 wolopts = 0; 3285 3286 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3287 if (ocp_data & LINK_ON_WAKE_EN) 3288 wolopts |= WAKE_PHY; 3289 3290 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 3291 if (ocp_data & UWF_EN) 3292 wolopts |= WAKE_UCAST; 3293 if (ocp_data & BWF_EN) 3294 wolopts |= WAKE_BCAST; 3295 if (ocp_data & MWF_EN) 3296 wolopts |= WAKE_MCAST; 3297 3298 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 3299 if (ocp_data & MAGIC_EN) 3300 wolopts |= WAKE_MAGIC; 3301 3302 return wolopts; 3303 } 3304 3305 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) 3306 { 3307 u32 ocp_data; 3308 3309 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3310 3311 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3312 ocp_data &= ~LINK_ON_WAKE_EN; 3313 if (wolopts & WAKE_PHY) 3314 ocp_data |= LINK_ON_WAKE_EN; 3315 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3316 3317 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 3318 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN); 3319 if (wolopts & WAKE_UCAST) 3320 ocp_data |= UWF_EN; 3321 if (wolopts & WAKE_BCAST) 3322 ocp_data |= BWF_EN; 3323 if (wolopts & WAKE_MCAST) 3324 ocp_data |= MWF_EN; 3325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); 3326 3327 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3328 3329 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 3330 ocp_data &= ~MAGIC_EN; 3331 if (wolopts & WAKE_MAGIC) 3332 ocp_data |= MAGIC_EN; 3333 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); 3334 3335 if (wolopts & WAKE_ANY) 3336 device_set_wakeup_enable(&tp->udev->dev, true); 3337 else 3338 device_set_wakeup_enable(&tp->udev->dev, false); 3339 } 3340 3341 static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable) 3342 { 3343 u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); 3344 3345 /* MAC clock speed down */ 3346 if (enable) 3347 ocp_data |= MAC_CLK_SPDWN_EN; 3348 else 3349 ocp_data &= ~MAC_CLK_SPDWN_EN; 3350 3351 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); 3352 } 3353 3354 static void r8156_mac_clk_spd(struct r8152 *tp, bool enable) 3355 { 3356 u32 ocp_data; 3357 3358 /* MAC clock speed down */ 3359 if (enable) { 3360 /* aldps_spdwn_ratio, tp10_spdwn_ratio */ 3361 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 3362 0x0403); 3363 3364 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); 3365 ocp_data &= ~EEE_SPDWN_RATIO_MASK; 3366 ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */ 3367 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); 3368 } else { 3369 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); 3370 ocp_data &= ~MAC_CLK_SPDWN_EN; 3371 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); 3372 } 3373 } 3374 3375 static void r8153_u1u2en(struct r8152 *tp, bool enable) 3376 { 3377 u8 u1u2[8]; 3378 3379 if (enable) 3380 memset(u1u2, 0xff, sizeof(u1u2)); 3381 else 3382 memset(u1u2, 0x00, sizeof(u1u2)); 3383 3384 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); 3385 } 3386 3387 static void r8153b_u1u2en(struct r8152 *tp, bool enable) 3388 { 3389 u32 ocp_data; 3390 3391 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); 3392 if (enable) 3393 ocp_data |= LPM_U1U2_EN; 3394 else 3395 ocp_data &= ~LPM_U1U2_EN; 3396 3397 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); 3398 } 3399 3400 static void r8153_u2p3en(struct r8152 *tp, bool enable) 3401 { 3402 u32 ocp_data; 3403 3404 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); 3405 if (enable) 3406 ocp_data |= U2P3_ENABLE; 3407 else 3408 ocp_data &= ~U2P3_ENABLE; 3409 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); 3410 } 3411 3412 static void r8153b_ups_flags(struct r8152 *tp) 3413 { 3414 u32 ups_flags = 0; 3415 3416 if (tp->ups_info.green) 3417 ups_flags |= UPS_FLAGS_EN_GREEN; 3418 3419 if (tp->ups_info.aldps) 3420 ups_flags |= UPS_FLAGS_EN_ALDPS; 3421 3422 if (tp->ups_info.eee) 3423 ups_flags |= UPS_FLAGS_EN_EEE; 3424 3425 if (tp->ups_info.flow_control) 3426 ups_flags |= UPS_FLAGS_EN_FLOW_CTR; 3427 3428 if (tp->ups_info.eee_ckdiv) 3429 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; 3430 3431 if (tp->ups_info.eee_cmod_lv) 3432 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN; 3433 3434 if (tp->ups_info.r_tune) 3435 ups_flags |= UPS_FLAGS_R_TUNE; 3436 3437 if (tp->ups_info._10m_ckdiv) 3438 ups_flags |= UPS_FLAGS_EN_10M_CKDIV; 3439 3440 if (tp->ups_info.eee_plloff_100) 3441 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; 3442 3443 if (tp->ups_info.eee_plloff_giga) 3444 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; 3445 3446 if (tp->ups_info._250m_ckdiv) 3447 ups_flags |= UPS_FLAGS_250M_CKDIV; 3448 3449 if (tp->ups_info.ctap_short_off) 3450 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS; 3451 3452 switch (tp->ups_info.speed_duplex) { 3453 case NWAY_10M_HALF: 3454 ups_flags |= ups_flags_speed(1); 3455 break; 3456 case NWAY_10M_FULL: 3457 ups_flags |= ups_flags_speed(2); 3458 break; 3459 case NWAY_100M_HALF: 3460 ups_flags |= ups_flags_speed(3); 3461 break; 3462 case NWAY_100M_FULL: 3463 ups_flags |= ups_flags_speed(4); 3464 break; 3465 case NWAY_1000M_FULL: 3466 ups_flags |= ups_flags_speed(5); 3467 break; 3468 case FORCE_10M_HALF: 3469 ups_flags |= ups_flags_speed(6); 3470 break; 3471 case FORCE_10M_FULL: 3472 ups_flags |= ups_flags_speed(7); 3473 break; 3474 case FORCE_100M_HALF: 3475 ups_flags |= ups_flags_speed(8); 3476 break; 3477 case FORCE_100M_FULL: 3478 ups_flags |= ups_flags_speed(9); 3479 break; 3480 default: 3481 break; 3482 } 3483 3484 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); 3485 } 3486 3487 static void r8156_ups_flags(struct r8152 *tp) 3488 { 3489 u32 ups_flags = 0; 3490 3491 if (tp->ups_info.green) 3492 ups_flags |= UPS_FLAGS_EN_GREEN; 3493 3494 if (tp->ups_info.aldps) 3495 ups_flags |= UPS_FLAGS_EN_ALDPS; 3496 3497 if (tp->ups_info.eee) 3498 ups_flags |= UPS_FLAGS_EN_EEE; 3499 3500 if (tp->ups_info.flow_control) 3501 ups_flags |= UPS_FLAGS_EN_FLOW_CTR; 3502 3503 if (tp->ups_info.eee_ckdiv) 3504 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; 3505 3506 if (tp->ups_info._10m_ckdiv) 3507 ups_flags |= UPS_FLAGS_EN_10M_CKDIV; 3508 3509 if (tp->ups_info.eee_plloff_100) 3510 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; 3511 3512 if (tp->ups_info.eee_plloff_giga) 3513 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; 3514 3515 if (tp->ups_info._250m_ckdiv) 3516 ups_flags |= UPS_FLAGS_250M_CKDIV; 3517 3518 switch (tp->ups_info.speed_duplex) { 3519 case FORCE_10M_HALF: 3520 ups_flags |= ups_flags_speed(0); 3521 break; 3522 case FORCE_10M_FULL: 3523 ups_flags |= ups_flags_speed(1); 3524 break; 3525 case FORCE_100M_HALF: 3526 ups_flags |= ups_flags_speed(2); 3527 break; 3528 case FORCE_100M_FULL: 3529 ups_flags |= ups_flags_speed(3); 3530 break; 3531 case NWAY_10M_HALF: 3532 ups_flags |= ups_flags_speed(4); 3533 break; 3534 case NWAY_10M_FULL: 3535 ups_flags |= ups_flags_speed(5); 3536 break; 3537 case NWAY_100M_HALF: 3538 ups_flags |= ups_flags_speed(6); 3539 break; 3540 case NWAY_100M_FULL: 3541 ups_flags |= ups_flags_speed(7); 3542 break; 3543 case NWAY_1000M_FULL: 3544 ups_flags |= ups_flags_speed(8); 3545 break; 3546 case NWAY_2500M_FULL: 3547 ups_flags |= ups_flags_speed(9); 3548 break; 3549 default: 3550 break; 3551 } 3552 3553 switch (tp->ups_info.lite_mode) { 3554 case 1: 3555 ups_flags |= 0 << 5; 3556 break; 3557 case 2: 3558 ups_flags |= 2 << 5; 3559 break; 3560 case 0: 3561 default: 3562 ups_flags |= 1 << 5; 3563 break; 3564 } 3565 3566 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); 3567 } 3568 3569 static void rtl_green_en(struct r8152 *tp, bool enable) 3570 { 3571 u16 data; 3572 3573 data = sram_read(tp, SRAM_GREEN_CFG); 3574 if (enable) 3575 data |= GREEN_ETH_EN; 3576 else 3577 data &= ~GREEN_ETH_EN; 3578 sram_write(tp, SRAM_GREEN_CFG, data); 3579 3580 tp->ups_info.green = enable; 3581 } 3582 3583 static void r8153b_green_en(struct r8152 *tp, bool enable) 3584 { 3585 if (enable) { 3586 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ 3587 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ 3588 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ 3589 } else { 3590 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ 3591 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ 3592 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ 3593 } 3594 3595 rtl_green_en(tp, true); 3596 } 3597 3598 static u16 r8153_phy_status(struct r8152 *tp, u16 desired) 3599 { 3600 u16 data; 3601 int i; 3602 3603 for (i = 0; i < 500; i++) { 3604 data = ocp_reg_read(tp, OCP_PHY_STATUS); 3605 data &= PHY_STAT_MASK; 3606 if (desired) { 3607 if (data == desired) 3608 break; 3609 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || 3610 data == PHY_STAT_EXT_INIT) { 3611 break; 3612 } 3613 3614 msleep(20); 3615 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 3616 break; 3617 } 3618 3619 return data; 3620 } 3621 3622 static void r8153b_ups_en(struct r8152 *tp, bool enable) 3623 { 3624 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); 3625 3626 if (enable) { 3627 r8153b_ups_flags(tp); 3628 3629 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; 3630 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3631 3632 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3633 ocp_data |= UPS_FORCE_PWR_DOWN; 3634 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3635 } else { 3636 ocp_data &= ~(UPS_EN | USP_PREWAKE); 3637 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3638 3639 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3640 ocp_data &= ~UPS_FORCE_PWR_DOWN; 3641 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3642 3643 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 3644 int i; 3645 3646 for (i = 0; i < 500; i++) { 3647 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 3648 AUTOLOAD_DONE) 3649 break; 3650 msleep(20); 3651 } 3652 3653 tp->rtl_ops.hw_phy_cfg(tp); 3654 3655 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 3656 tp->duplex, tp->advertising); 3657 } 3658 } 3659 } 3660 3661 static void r8153c_ups_en(struct r8152 *tp, bool enable) 3662 { 3663 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); 3664 3665 if (enable) { 3666 r8153b_ups_flags(tp); 3667 3668 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; 3669 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3670 3671 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3672 ocp_data |= UPS_FORCE_PWR_DOWN; 3673 ocp_data &= ~BIT(7); 3674 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3675 } else { 3676 ocp_data &= ~(UPS_EN | USP_PREWAKE); 3677 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3678 3679 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3680 ocp_data &= ~UPS_FORCE_PWR_DOWN; 3681 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3682 3683 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 3684 int i; 3685 3686 for (i = 0; i < 500; i++) { 3687 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 3688 AUTOLOAD_DONE) 3689 break; 3690 msleep(20); 3691 } 3692 3693 tp->rtl_ops.hw_phy_cfg(tp); 3694 3695 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 3696 tp->duplex, tp->advertising); 3697 } 3698 3699 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3700 3701 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3702 ocp_data |= BIT(8); 3703 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3704 3705 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3706 } 3707 } 3708 3709 static void r8156_ups_en(struct r8152 *tp, bool enable) 3710 { 3711 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); 3712 3713 if (enable) { 3714 r8156_ups_flags(tp); 3715 3716 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; 3717 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3718 3719 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3720 ocp_data |= UPS_FORCE_PWR_DOWN; 3721 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3722 3723 switch (tp->version) { 3724 case RTL_VER_13: 3725 case RTL_VER_15: 3726 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL); 3727 ocp_data &= ~OOBS_POLLING; 3728 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data); 3729 break; 3730 default: 3731 break; 3732 } 3733 } else { 3734 ocp_data &= ~(UPS_EN | USP_PREWAKE); 3735 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3736 3737 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3738 ocp_data &= ~UPS_FORCE_PWR_DOWN; 3739 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3740 3741 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 3742 tp->rtl_ops.hw_phy_cfg(tp); 3743 3744 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 3745 tp->duplex, tp->advertising); 3746 } 3747 } 3748 } 3749 3750 static void r8153_power_cut_en(struct r8152 *tp, bool enable) 3751 { 3752 u32 ocp_data; 3753 3754 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); 3755 if (enable) 3756 ocp_data |= PWR_EN | PHASE2_EN; 3757 else 3758 ocp_data &= ~(PWR_EN | PHASE2_EN); 3759 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3760 3761 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 3762 ocp_data &= ~PCUT_STATUS; 3763 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 3764 } 3765 3766 static void r8153b_power_cut_en(struct r8152 *tp, bool enable) 3767 { 3768 u32 ocp_data; 3769 3770 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); 3771 if (enable) 3772 ocp_data |= PWR_EN | PHASE2_EN; 3773 else 3774 ocp_data &= ~PWR_EN; 3775 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3776 3777 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 3778 ocp_data &= ~PCUT_STATUS; 3779 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 3780 } 3781 3782 static void r8153_queue_wake(struct r8152 *tp, bool enable) 3783 { 3784 u32 ocp_data; 3785 3786 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG); 3787 if (enable) 3788 ocp_data |= UPCOMING_RUNTIME_D3; 3789 else 3790 ocp_data &= ~UPCOMING_RUNTIME_D3; 3791 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data); 3792 3793 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG); 3794 ocp_data &= ~LINK_CHG_EVENT; 3795 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data); 3796 3797 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 3798 ocp_data &= ~LINK_CHANGE_FLAG; 3799 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 3800 } 3801 3802 static bool rtl_can_wakeup(struct r8152 *tp) 3803 { 3804 struct usb_device *udev = tp->udev; 3805 3806 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); 3807 } 3808 3809 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) 3810 { 3811 if (enable) { 3812 u32 ocp_data; 3813 3814 __rtl_set_wol(tp, WAKE_ANY); 3815 3816 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3817 3818 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3819 ocp_data |= LINK_OFF_WAKE_EN; 3820 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3821 3822 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3823 } else { 3824 u32 ocp_data; 3825 3826 __rtl_set_wol(tp, tp->saved_wolopts); 3827 3828 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3829 3830 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3831 ocp_data &= ~LINK_OFF_WAKE_EN; 3832 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3833 3834 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3835 } 3836 } 3837 3838 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) 3839 { 3840 if (enable) { 3841 r8153_u1u2en(tp, false); 3842 r8153_u2p3en(tp, false); 3843 rtl_runtime_suspend_enable(tp, true); 3844 } else { 3845 rtl_runtime_suspend_enable(tp, false); 3846 3847 switch (tp->version) { 3848 case RTL_VER_03: 3849 case RTL_VER_04: 3850 break; 3851 case RTL_VER_05: 3852 case RTL_VER_06: 3853 default: 3854 r8153_u2p3en(tp, true); 3855 break; 3856 } 3857 3858 r8153_u1u2en(tp, true); 3859 } 3860 } 3861 3862 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) 3863 { 3864 if (enable) { 3865 r8153_queue_wake(tp, true); 3866 r8153b_u1u2en(tp, false); 3867 r8153_u2p3en(tp, false); 3868 rtl_runtime_suspend_enable(tp, true); 3869 r8153b_ups_en(tp, true); 3870 } else { 3871 r8153b_ups_en(tp, false); 3872 r8153_queue_wake(tp, false); 3873 rtl_runtime_suspend_enable(tp, false); 3874 if (tp->udev->speed >= USB_SPEED_SUPER) 3875 r8153b_u1u2en(tp, true); 3876 } 3877 } 3878 3879 static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable) 3880 { 3881 if (enable) { 3882 r8153_queue_wake(tp, true); 3883 r8153b_u1u2en(tp, false); 3884 r8153_u2p3en(tp, false); 3885 rtl_runtime_suspend_enable(tp, true); 3886 r8153c_ups_en(tp, true); 3887 } else { 3888 r8153c_ups_en(tp, false); 3889 r8153_queue_wake(tp, false); 3890 rtl_runtime_suspend_enable(tp, false); 3891 r8153b_u1u2en(tp, true); 3892 } 3893 } 3894 3895 static void rtl8156_runtime_enable(struct r8152 *tp, bool enable) 3896 { 3897 if (enable) { 3898 r8153_queue_wake(tp, true); 3899 r8153b_u1u2en(tp, false); 3900 r8153_u2p3en(tp, false); 3901 rtl_runtime_suspend_enable(tp, true); 3902 } else { 3903 r8153_queue_wake(tp, false); 3904 rtl_runtime_suspend_enable(tp, false); 3905 r8153_u2p3en(tp, true); 3906 if (tp->udev->speed >= USB_SPEED_SUPER) 3907 r8153b_u1u2en(tp, true); 3908 } 3909 } 3910 3911 static void r8153_teredo_off(struct r8152 *tp) 3912 { 3913 u32 ocp_data; 3914 3915 switch (tp->version) { 3916 case RTL_VER_01: 3917 case RTL_VER_02: 3918 case RTL_VER_03: 3919 case RTL_VER_04: 3920 case RTL_VER_05: 3921 case RTL_VER_06: 3922 case RTL_VER_07: 3923 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); 3924 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | 3925 OOB_TEREDO_EN); 3926 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); 3927 break; 3928 3929 case RTL_VER_08: 3930 case RTL_VER_09: 3931 case RTL_TEST_01: 3932 case RTL_VER_10: 3933 case RTL_VER_11: 3934 case RTL_VER_12: 3935 case RTL_VER_13: 3936 case RTL_VER_14: 3937 case RTL_VER_15: 3938 default: 3939 /* The bit 0 ~ 7 are relative with teredo settings. They are 3940 * W1C (write 1 to clear), so set all 1 to disable it. 3941 */ 3942 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); 3943 break; 3944 } 3945 3946 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); 3947 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); 3948 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); 3949 } 3950 3951 static void rtl_reset_bmu(struct r8152 *tp) 3952 { 3953 u32 ocp_data; 3954 3955 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); 3956 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); 3957 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 3958 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; 3959 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 3960 } 3961 3962 /* Clear the bp to stop the firmware before loading a new one */ 3963 static void rtl_clear_bp(struct r8152 *tp, u16 type) 3964 { 3965 switch (tp->version) { 3966 case RTL_VER_01: 3967 case RTL_VER_02: 3968 case RTL_VER_07: 3969 break; 3970 case RTL_VER_03: 3971 case RTL_VER_04: 3972 case RTL_VER_05: 3973 case RTL_VER_06: 3974 ocp_write_byte(tp, type, PLA_BP_EN, 0); 3975 break; 3976 case RTL_VER_14: 3977 ocp_write_word(tp, type, USB_BP2_EN, 0); 3978 3979 ocp_write_word(tp, type, USB_BP_8, 0); 3980 ocp_write_word(tp, type, USB_BP_9, 0); 3981 ocp_write_word(tp, type, USB_BP_10, 0); 3982 ocp_write_word(tp, type, USB_BP_11, 0); 3983 ocp_write_word(tp, type, USB_BP_12, 0); 3984 ocp_write_word(tp, type, USB_BP_13, 0); 3985 ocp_write_word(tp, type, USB_BP_14, 0); 3986 ocp_write_word(tp, type, USB_BP_15, 0); 3987 break; 3988 case RTL_VER_08: 3989 case RTL_VER_09: 3990 case RTL_VER_10: 3991 case RTL_VER_11: 3992 case RTL_VER_12: 3993 case RTL_VER_13: 3994 case RTL_VER_15: 3995 default: 3996 if (type == MCU_TYPE_USB) { 3997 ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0); 3998 3999 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0); 4000 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0); 4001 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0); 4002 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0); 4003 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0); 4004 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0); 4005 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0); 4006 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0); 4007 } else { 4008 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0); 4009 } 4010 break; 4011 } 4012 4013 ocp_write_word(tp, type, PLA_BP_0, 0); 4014 ocp_write_word(tp, type, PLA_BP_1, 0); 4015 ocp_write_word(tp, type, PLA_BP_2, 0); 4016 ocp_write_word(tp, type, PLA_BP_3, 0); 4017 ocp_write_word(tp, type, PLA_BP_4, 0); 4018 ocp_write_word(tp, type, PLA_BP_5, 0); 4019 ocp_write_word(tp, type, PLA_BP_6, 0); 4020 ocp_write_word(tp, type, PLA_BP_7, 0); 4021 4022 /* wait 3 ms to make sure the firmware is stopped */ 4023 usleep_range(3000, 6000); 4024 ocp_write_word(tp, type, PLA_BP_BA, 0); 4025 } 4026 4027 static inline void rtl_reset_ocp_base(struct r8152 *tp) 4028 { 4029 tp->ocp_base = -1; 4030 } 4031 4032 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait) 4033 { 4034 u16 data, check; 4035 int i; 4036 4037 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD); 4038 if (request) { 4039 data |= PATCH_REQUEST; 4040 check = 0; 4041 } else { 4042 data &= ~PATCH_REQUEST; 4043 check = PATCH_READY; 4044 } 4045 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data); 4046 4047 for (i = 0; wait && i < 5000; i++) { 4048 u32 ocp_data; 4049 4050 usleep_range(1000, 2000); 4051 ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT); 4052 if ((ocp_data & PATCH_READY) ^ check) 4053 break; 4054 } 4055 4056 if (request && wait && 4057 !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { 4058 dev_err(&tp->intf->dev, "PHY patch request fail\n"); 4059 rtl_phy_patch_request(tp, false, false); 4060 return -ETIME; 4061 } else { 4062 return 0; 4063 } 4064 } 4065 4066 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key) 4067 { 4068 if (patch_key && key_addr) { 4069 sram_write(tp, key_addr, patch_key); 4070 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK); 4071 } else if (key_addr) { 4072 u16 data; 4073 4074 sram_write(tp, 0x0000, 0x0000); 4075 4076 data = ocp_reg_read(tp, OCP_PHY_LOCK); 4077 data &= ~PATCH_LOCK; 4078 ocp_reg_write(tp, OCP_PHY_LOCK, data); 4079 4080 sram_write(tp, key_addr, 0x0000); 4081 } else { 4082 WARN_ON_ONCE(1); 4083 } 4084 } 4085 4086 static int 4087 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait) 4088 { 4089 if (rtl_phy_patch_request(tp, true, wait)) 4090 return -ETIME; 4091 4092 rtl_patch_key_set(tp, key_addr, patch_key); 4093 4094 return 0; 4095 } 4096 4097 static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait) 4098 { 4099 rtl_patch_key_set(tp, key_addr, 0); 4100 4101 rtl_phy_patch_request(tp, false, wait); 4102 4103 return 0; 4104 } 4105 4106 static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy) 4107 { 4108 u16 fw_offset; 4109 u32 length; 4110 bool rc = false; 4111 4112 switch (tp->version) { 4113 case RTL_VER_01: 4114 case RTL_VER_02: 4115 case RTL_VER_03: 4116 case RTL_VER_04: 4117 case RTL_VER_05: 4118 case RTL_VER_06: 4119 case RTL_VER_07: 4120 case RTL_VER_08: 4121 case RTL_VER_09: 4122 case RTL_VER_10: 4123 case RTL_VER_11: 4124 case RTL_VER_12: 4125 case RTL_VER_14: 4126 goto out; 4127 case RTL_VER_13: 4128 case RTL_VER_15: 4129 default: 4130 break; 4131 } 4132 4133 fw_offset = __le16_to_cpu(phy->fw_offset); 4134 length = __le32_to_cpu(phy->blk_hdr.length); 4135 if (fw_offset < sizeof(*phy) || length <= fw_offset) { 4136 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4137 goto out; 4138 } 4139 4140 length -= fw_offset; 4141 if (length & 3) { 4142 dev_err(&tp->intf->dev, "invalid block length\n"); 4143 goto out; 4144 } 4145 4146 if (__le16_to_cpu(phy->fw_reg) != 0x9A00) { 4147 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4148 goto out; 4149 } 4150 4151 rc = true; 4152 out: 4153 return rc; 4154 } 4155 4156 static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver) 4157 { 4158 bool rc = false; 4159 4160 switch (tp->version) { 4161 case RTL_VER_10: 4162 case RTL_VER_11: 4163 case RTL_VER_12: 4164 case RTL_VER_13: 4165 case RTL_VER_15: 4166 break; 4167 default: 4168 goto out; 4169 } 4170 4171 if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) { 4172 dev_err(&tp->intf->dev, "invalid block length\n"); 4173 goto out; 4174 } 4175 4176 if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) { 4177 dev_err(&tp->intf->dev, "invalid phy ver addr\n"); 4178 goto out; 4179 } 4180 4181 rc = true; 4182 out: 4183 return rc; 4184 } 4185 4186 static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix) 4187 { 4188 bool rc = false; 4189 4190 switch (tp->version) { 4191 case RTL_VER_10: 4192 case RTL_VER_11: 4193 case RTL_VER_12: 4194 case RTL_VER_13: 4195 case RTL_VER_15: 4196 break; 4197 default: 4198 goto out; 4199 } 4200 4201 if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) { 4202 dev_err(&tp->intf->dev, "invalid block length\n"); 4203 goto out; 4204 } 4205 4206 if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD || 4207 __le16_to_cpu(fix->setting.data) != BIT(7)) { 4208 dev_err(&tp->intf->dev, "invalid phy fixup\n"); 4209 goto out; 4210 } 4211 4212 rc = true; 4213 out: 4214 return rc; 4215 } 4216 4217 static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy) 4218 { 4219 u16 fw_offset; 4220 u32 length; 4221 bool rc = false; 4222 4223 switch (tp->version) { 4224 case RTL_VER_10: 4225 case RTL_VER_11: 4226 case RTL_VER_12: 4227 case RTL_VER_13: 4228 case RTL_VER_15: 4229 break; 4230 default: 4231 goto out; 4232 } 4233 4234 fw_offset = __le16_to_cpu(phy->fw_offset); 4235 length = __le32_to_cpu(phy->blk_hdr.length); 4236 if (fw_offset < sizeof(*phy) || length <= fw_offset) { 4237 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4238 goto out; 4239 } 4240 4241 length -= fw_offset; 4242 if (length & 1) { 4243 dev_err(&tp->intf->dev, "invalid block length\n"); 4244 goto out; 4245 } 4246 4247 if (phy->pre_num > 2) { 4248 dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num); 4249 goto out; 4250 } 4251 4252 if (phy->bp_num > 8) { 4253 dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num); 4254 goto out; 4255 } 4256 4257 rc = true; 4258 out: 4259 return rc; 4260 } 4261 4262 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy) 4263 { 4264 u32 length; 4265 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start; 4266 bool rc = false; 4267 4268 switch (tp->version) { 4269 case RTL_VER_04: 4270 case RTL_VER_05: 4271 case RTL_VER_06: 4272 fw_reg = 0xa014; 4273 ba_reg = 0xa012; 4274 patch_en_addr = 0xa01a; 4275 mode_reg = 0xb820; 4276 bp_start = 0xa000; 4277 break; 4278 default: 4279 goto out; 4280 } 4281 4282 fw_offset = __le16_to_cpu(phy->fw_offset); 4283 if (fw_offset < sizeof(*phy)) { 4284 dev_err(&tp->intf->dev, "fw_offset too small\n"); 4285 goto out; 4286 } 4287 4288 length = __le32_to_cpu(phy->blk_hdr.length); 4289 if (length < fw_offset) { 4290 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4291 goto out; 4292 } 4293 4294 length -= __le16_to_cpu(phy->fw_offset); 4295 if (!length || (length & 1)) { 4296 dev_err(&tp->intf->dev, "invalid block length\n"); 4297 goto out; 4298 } 4299 4300 if (__le16_to_cpu(phy->fw_reg) != fw_reg) { 4301 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4302 goto out; 4303 } 4304 4305 if (__le16_to_cpu(phy->ba_reg) != ba_reg) { 4306 dev_err(&tp->intf->dev, "invalid base address register\n"); 4307 goto out; 4308 } 4309 4310 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) { 4311 dev_err(&tp->intf->dev, 4312 "invalid patch mode enabled register\n"); 4313 goto out; 4314 } 4315 4316 if (__le16_to_cpu(phy->mode_reg) != mode_reg) { 4317 dev_err(&tp->intf->dev, 4318 "invalid register to switch the mode\n"); 4319 goto out; 4320 } 4321 4322 if (__le16_to_cpu(phy->bp_start) != bp_start) { 4323 dev_err(&tp->intf->dev, 4324 "invalid start register of break point\n"); 4325 goto out; 4326 } 4327 4328 if (__le16_to_cpu(phy->bp_num) > 4) { 4329 dev_err(&tp->intf->dev, "invalid break point number\n"); 4330 goto out; 4331 } 4332 4333 rc = true; 4334 out: 4335 return rc; 4336 } 4337 4338 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac) 4339 { 4340 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset; 4341 bool rc = false; 4342 u32 length, type; 4343 int i, max_bp; 4344 4345 type = __le32_to_cpu(mac->blk_hdr.type); 4346 if (type == RTL_FW_PLA) { 4347 switch (tp->version) { 4348 case RTL_VER_01: 4349 case RTL_VER_02: 4350 case RTL_VER_07: 4351 fw_reg = 0xf800; 4352 bp_ba_addr = PLA_BP_BA; 4353 bp_en_addr = 0; 4354 bp_start = PLA_BP_0; 4355 max_bp = 8; 4356 break; 4357 case RTL_VER_03: 4358 case RTL_VER_04: 4359 case RTL_VER_05: 4360 case RTL_VER_06: 4361 case RTL_VER_08: 4362 case RTL_VER_09: 4363 case RTL_VER_11: 4364 case RTL_VER_12: 4365 case RTL_VER_13: 4366 case RTL_VER_15: 4367 fw_reg = 0xf800; 4368 bp_ba_addr = PLA_BP_BA; 4369 bp_en_addr = PLA_BP_EN; 4370 bp_start = PLA_BP_0; 4371 max_bp = 8; 4372 break; 4373 case RTL_VER_14: 4374 fw_reg = 0xf800; 4375 bp_ba_addr = PLA_BP_BA; 4376 bp_en_addr = USB_BP2_EN; 4377 bp_start = PLA_BP_0; 4378 max_bp = 16; 4379 break; 4380 default: 4381 goto out; 4382 } 4383 } else if (type == RTL_FW_USB) { 4384 switch (tp->version) { 4385 case RTL_VER_03: 4386 case RTL_VER_04: 4387 case RTL_VER_05: 4388 case RTL_VER_06: 4389 fw_reg = 0xf800; 4390 bp_ba_addr = USB_BP_BA; 4391 bp_en_addr = USB_BP_EN; 4392 bp_start = USB_BP_0; 4393 max_bp = 8; 4394 break; 4395 case RTL_VER_08: 4396 case RTL_VER_09: 4397 case RTL_VER_11: 4398 case RTL_VER_12: 4399 case RTL_VER_13: 4400 case RTL_VER_14: 4401 case RTL_VER_15: 4402 fw_reg = 0xe600; 4403 bp_ba_addr = USB_BP_BA; 4404 bp_en_addr = USB_BP2_EN; 4405 bp_start = USB_BP_0; 4406 max_bp = 16; 4407 break; 4408 case RTL_VER_01: 4409 case RTL_VER_02: 4410 case RTL_VER_07: 4411 default: 4412 goto out; 4413 } 4414 } else { 4415 goto out; 4416 } 4417 4418 fw_offset = __le16_to_cpu(mac->fw_offset); 4419 if (fw_offset < sizeof(*mac)) { 4420 dev_err(&tp->intf->dev, "fw_offset too small\n"); 4421 goto out; 4422 } 4423 4424 length = __le32_to_cpu(mac->blk_hdr.length); 4425 if (length < fw_offset) { 4426 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4427 goto out; 4428 } 4429 4430 length -= fw_offset; 4431 if (length < 4 || (length & 3)) { 4432 dev_err(&tp->intf->dev, "invalid block length\n"); 4433 goto out; 4434 } 4435 4436 if (__le16_to_cpu(mac->fw_reg) != fw_reg) { 4437 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4438 goto out; 4439 } 4440 4441 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) { 4442 dev_err(&tp->intf->dev, "invalid base address register\n"); 4443 goto out; 4444 } 4445 4446 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) { 4447 dev_err(&tp->intf->dev, "invalid enabled mask register\n"); 4448 goto out; 4449 } 4450 4451 if (__le16_to_cpu(mac->bp_start) != bp_start) { 4452 dev_err(&tp->intf->dev, 4453 "invalid start register of break point\n"); 4454 goto out; 4455 } 4456 4457 if (__le16_to_cpu(mac->bp_num) > max_bp) { 4458 dev_err(&tp->intf->dev, "invalid break point number\n"); 4459 goto out; 4460 } 4461 4462 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) { 4463 if (mac->bp[i]) { 4464 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i); 4465 goto out; 4466 } 4467 } 4468 4469 rc = true; 4470 out: 4471 return rc; 4472 } 4473 4474 /* Verify the checksum for the firmware file. It is calculated from the version 4475 * field to the end of the file. Compare the result with the checksum field to 4476 * make sure the file is correct. 4477 */ 4478 static long rtl8152_fw_verify_checksum(struct r8152 *tp, 4479 struct fw_header *fw_hdr, size_t size) 4480 { 4481 unsigned char checksum[sizeof(fw_hdr->checksum)]; 4482 struct crypto_shash *alg; 4483 struct shash_desc *sdesc; 4484 size_t len; 4485 long rc; 4486 4487 alg = crypto_alloc_shash("sha256", 0, 0); 4488 if (IS_ERR(alg)) { 4489 rc = PTR_ERR(alg); 4490 goto out; 4491 } 4492 4493 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) { 4494 rc = -EFAULT; 4495 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n", 4496 crypto_shash_digestsize(alg)); 4497 goto free_shash; 4498 } 4499 4500 len = sizeof(*sdesc) + crypto_shash_descsize(alg); 4501 sdesc = kmalloc(len, GFP_KERNEL); 4502 if (!sdesc) { 4503 rc = -ENOMEM; 4504 goto free_shash; 4505 } 4506 sdesc->tfm = alg; 4507 4508 len = size - sizeof(fw_hdr->checksum); 4509 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum); 4510 kfree(sdesc); 4511 if (rc) 4512 goto free_shash; 4513 4514 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) { 4515 dev_err(&tp->intf->dev, "checksum fail\n"); 4516 rc = -EFAULT; 4517 } 4518 4519 free_shash: 4520 crypto_free_shash(alg); 4521 out: 4522 return rc; 4523 } 4524 4525 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw) 4526 { 4527 const struct firmware *fw = rtl_fw->fw; 4528 struct fw_header *fw_hdr = (struct fw_header *)fw->data; 4529 unsigned long fw_flags = 0; 4530 long ret = -EFAULT; 4531 int i; 4532 4533 if (fw->size < sizeof(*fw_hdr)) { 4534 dev_err(&tp->intf->dev, "file too small\n"); 4535 goto fail; 4536 } 4537 4538 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size); 4539 if (ret) 4540 goto fail; 4541 4542 ret = -EFAULT; 4543 4544 for (i = sizeof(*fw_hdr); i < fw->size;) { 4545 struct fw_block *block = (struct fw_block *)&fw->data[i]; 4546 u32 type; 4547 4548 if ((i + sizeof(*block)) > fw->size) 4549 goto fail; 4550 4551 type = __le32_to_cpu(block->type); 4552 switch (type) { 4553 case RTL_FW_END: 4554 if (__le32_to_cpu(block->length) != sizeof(*block)) 4555 goto fail; 4556 goto fw_end; 4557 case RTL_FW_PLA: 4558 if (test_bit(FW_FLAGS_PLA, &fw_flags)) { 4559 dev_err(&tp->intf->dev, 4560 "multiple PLA firmware encountered"); 4561 goto fail; 4562 } 4563 4564 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) { 4565 dev_err(&tp->intf->dev, 4566 "check PLA firmware failed\n"); 4567 goto fail; 4568 } 4569 __set_bit(FW_FLAGS_PLA, &fw_flags); 4570 break; 4571 case RTL_FW_USB: 4572 if (test_bit(FW_FLAGS_USB, &fw_flags)) { 4573 dev_err(&tp->intf->dev, 4574 "multiple USB firmware encountered"); 4575 goto fail; 4576 } 4577 4578 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) { 4579 dev_err(&tp->intf->dev, 4580 "check USB firmware failed\n"); 4581 goto fail; 4582 } 4583 __set_bit(FW_FLAGS_USB, &fw_flags); 4584 break; 4585 case RTL_FW_PHY_START: 4586 if (test_bit(FW_FLAGS_START, &fw_flags) || 4587 test_bit(FW_FLAGS_NC, &fw_flags) || 4588 test_bit(FW_FLAGS_NC1, &fw_flags) || 4589 test_bit(FW_FLAGS_NC2, &fw_flags) || 4590 test_bit(FW_FLAGS_UC2, &fw_flags) || 4591 test_bit(FW_FLAGS_UC, &fw_flags) || 4592 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4593 dev_err(&tp->intf->dev, 4594 "check PHY_START fail\n"); 4595 goto fail; 4596 } 4597 4598 if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) { 4599 dev_err(&tp->intf->dev, 4600 "Invalid length for PHY_START\n"); 4601 goto fail; 4602 } 4603 __set_bit(FW_FLAGS_START, &fw_flags); 4604 break; 4605 case RTL_FW_PHY_STOP: 4606 if (test_bit(FW_FLAGS_STOP, &fw_flags) || 4607 !test_bit(FW_FLAGS_START, &fw_flags)) { 4608 dev_err(&tp->intf->dev, 4609 "Check PHY_STOP fail\n"); 4610 goto fail; 4611 } 4612 4613 if (__le32_to_cpu(block->length) != sizeof(*block)) { 4614 dev_err(&tp->intf->dev, 4615 "Invalid length for PHY_STOP\n"); 4616 goto fail; 4617 } 4618 __set_bit(FW_FLAGS_STOP, &fw_flags); 4619 break; 4620 case RTL_FW_PHY_NC: 4621 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4622 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4623 dev_err(&tp->intf->dev, 4624 "check PHY_NC fail\n"); 4625 goto fail; 4626 } 4627 4628 if (test_bit(FW_FLAGS_NC, &fw_flags)) { 4629 dev_err(&tp->intf->dev, 4630 "multiple PHY NC encountered\n"); 4631 goto fail; 4632 } 4633 4634 if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) { 4635 dev_err(&tp->intf->dev, 4636 "check PHY NC firmware failed\n"); 4637 goto fail; 4638 } 4639 __set_bit(FW_FLAGS_NC, &fw_flags); 4640 break; 4641 case RTL_FW_PHY_UNION_NC: 4642 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4643 test_bit(FW_FLAGS_NC1, &fw_flags) || 4644 test_bit(FW_FLAGS_NC2, &fw_flags) || 4645 test_bit(FW_FLAGS_UC2, &fw_flags) || 4646 test_bit(FW_FLAGS_UC, &fw_flags) || 4647 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4648 dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n"); 4649 goto fail; 4650 } 4651 4652 if (test_bit(FW_FLAGS_NC, &fw_flags)) { 4653 dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n"); 4654 goto fail; 4655 } 4656 4657 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4658 dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n"); 4659 goto fail; 4660 } 4661 __set_bit(FW_FLAGS_NC, &fw_flags); 4662 break; 4663 case RTL_FW_PHY_UNION_NC1: 4664 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4665 test_bit(FW_FLAGS_NC2, &fw_flags) || 4666 test_bit(FW_FLAGS_UC2, &fw_flags) || 4667 test_bit(FW_FLAGS_UC, &fw_flags) || 4668 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4669 dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n"); 4670 goto fail; 4671 } 4672 4673 if (test_bit(FW_FLAGS_NC1, &fw_flags)) { 4674 dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n"); 4675 goto fail; 4676 } 4677 4678 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4679 dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n"); 4680 goto fail; 4681 } 4682 __set_bit(FW_FLAGS_NC1, &fw_flags); 4683 break; 4684 case RTL_FW_PHY_UNION_NC2: 4685 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4686 test_bit(FW_FLAGS_UC2, &fw_flags) || 4687 test_bit(FW_FLAGS_UC, &fw_flags) || 4688 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4689 dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n"); 4690 goto fail; 4691 } 4692 4693 if (test_bit(FW_FLAGS_NC2, &fw_flags)) { 4694 dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n"); 4695 goto fail; 4696 } 4697 4698 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4699 dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n"); 4700 goto fail; 4701 } 4702 __set_bit(FW_FLAGS_NC2, &fw_flags); 4703 break; 4704 case RTL_FW_PHY_UNION_UC2: 4705 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4706 test_bit(FW_FLAGS_UC, &fw_flags) || 4707 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4708 dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n"); 4709 goto fail; 4710 } 4711 4712 if (test_bit(FW_FLAGS_UC2, &fw_flags)) { 4713 dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n"); 4714 goto fail; 4715 } 4716 4717 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4718 dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n"); 4719 goto fail; 4720 } 4721 __set_bit(FW_FLAGS_UC2, &fw_flags); 4722 break; 4723 case RTL_FW_PHY_UNION_UC: 4724 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4725 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4726 dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n"); 4727 goto fail; 4728 } 4729 4730 if (test_bit(FW_FLAGS_UC, &fw_flags)) { 4731 dev_err(&tp->intf->dev, "multiple PHY UC encountered\n"); 4732 goto fail; 4733 } 4734 4735 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4736 dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n"); 4737 goto fail; 4738 } 4739 __set_bit(FW_FLAGS_UC, &fw_flags); 4740 break; 4741 case RTL_FW_PHY_UNION_MISC: 4742 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4743 dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n"); 4744 goto fail; 4745 } 4746 break; 4747 case RTL_FW_PHY_FIXUP: 4748 if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) { 4749 dev_err(&tp->intf->dev, "check PHY fixup failed\n"); 4750 goto fail; 4751 } 4752 break; 4753 case RTL_FW_PHY_SPEED_UP: 4754 if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) { 4755 dev_err(&tp->intf->dev, "multiple PHY firmware encountered"); 4756 goto fail; 4757 } 4758 4759 if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) { 4760 dev_err(&tp->intf->dev, "check PHY speed up failed\n"); 4761 goto fail; 4762 } 4763 __set_bit(FW_FLAGS_SPEED_UP, &fw_flags); 4764 break; 4765 case RTL_FW_PHY_VER: 4766 if (test_bit(FW_FLAGS_START, &fw_flags) || 4767 test_bit(FW_FLAGS_NC, &fw_flags) || 4768 test_bit(FW_FLAGS_NC1, &fw_flags) || 4769 test_bit(FW_FLAGS_NC2, &fw_flags) || 4770 test_bit(FW_FLAGS_UC2, &fw_flags) || 4771 test_bit(FW_FLAGS_UC, &fw_flags) || 4772 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4773 dev_err(&tp->intf->dev, "Invalid order to set PHY version\n"); 4774 goto fail; 4775 } 4776 4777 if (test_bit(FW_FLAGS_VER, &fw_flags)) { 4778 dev_err(&tp->intf->dev, "multiple PHY version encountered"); 4779 goto fail; 4780 } 4781 4782 if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) { 4783 dev_err(&tp->intf->dev, "check PHY version failed\n"); 4784 goto fail; 4785 } 4786 __set_bit(FW_FLAGS_VER, &fw_flags); 4787 break; 4788 default: 4789 dev_warn(&tp->intf->dev, "Unknown type %u is found\n", 4790 type); 4791 break; 4792 } 4793 4794 /* next block */ 4795 i += ALIGN(__le32_to_cpu(block->length), 8); 4796 } 4797 4798 fw_end: 4799 if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) { 4800 dev_err(&tp->intf->dev, "without PHY_STOP\n"); 4801 goto fail; 4802 } 4803 4804 return 0; 4805 fail: 4806 return ret; 4807 } 4808 4809 static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait) 4810 { 4811 u32 len; 4812 u8 *data; 4813 4814 rtl_reset_ocp_base(tp); 4815 4816 if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) { 4817 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n"); 4818 return; 4819 } 4820 4821 len = __le32_to_cpu(phy->blk_hdr.length); 4822 len -= __le16_to_cpu(phy->fw_offset); 4823 data = (u8 *)phy + __le16_to_cpu(phy->fw_offset); 4824 4825 if (rtl_phy_patch_request(tp, true, wait)) 4826 return; 4827 4828 while (len) { 4829 u32 ocp_data, size; 4830 int i; 4831 4832 if (len < 2048) 4833 size = len; 4834 else 4835 size = 2048; 4836 4837 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL); 4838 ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE; 4839 ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data); 4840 4841 generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB); 4842 4843 data += size; 4844 len -= size; 4845 4846 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL); 4847 ocp_data |= POL_GPHY_PATCH; 4848 ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data); 4849 4850 for (i = 0; i < 1000; i++) { 4851 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH)) 4852 break; 4853 } 4854 4855 if (i == 1000) { 4856 dev_err(&tp->intf->dev, "ram code speedup mode timeout\n"); 4857 break; 4858 } 4859 } 4860 4861 rtl_reset_ocp_base(tp); 4862 4863 rtl_phy_patch_request(tp, false, wait); 4864 4865 if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version)) 4866 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 4867 else 4868 dev_err(&tp->intf->dev, "ram code speedup mode fail\n"); 4869 } 4870 4871 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver) 4872 { 4873 u16 ver_addr, ver; 4874 4875 ver_addr = __le16_to_cpu(phy_ver->ver.addr); 4876 ver = __le16_to_cpu(phy_ver->ver.data); 4877 4878 rtl_reset_ocp_base(tp); 4879 4880 if (sram_read(tp, ver_addr) >= ver) { 4881 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n"); 4882 return 0; 4883 } 4884 4885 sram_write(tp, ver_addr, ver); 4886 4887 dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver); 4888 4889 return ver; 4890 } 4891 4892 static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix) 4893 { 4894 u16 addr, data; 4895 4896 rtl_reset_ocp_base(tp); 4897 4898 addr = __le16_to_cpu(fix->setting.addr); 4899 data = ocp_reg_read(tp, addr); 4900 4901 switch (__le16_to_cpu(fix->bit_cmd)) { 4902 case FW_FIXUP_AND: 4903 data &= __le16_to_cpu(fix->setting.data); 4904 break; 4905 case FW_FIXUP_OR: 4906 data |= __le16_to_cpu(fix->setting.data); 4907 break; 4908 case FW_FIXUP_NOT: 4909 data &= ~__le16_to_cpu(fix->setting.data); 4910 break; 4911 case FW_FIXUP_XOR: 4912 data ^= __le16_to_cpu(fix->setting.data); 4913 break; 4914 default: 4915 return; 4916 } 4917 4918 ocp_reg_write(tp, addr, data); 4919 4920 dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data); 4921 } 4922 4923 static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy) 4924 { 4925 __le16 *data; 4926 u32 length; 4927 int i, num; 4928 4929 rtl_reset_ocp_base(tp); 4930 4931 num = phy->pre_num; 4932 for (i = 0; i < num; i++) 4933 sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr), 4934 __le16_to_cpu(phy->pre_set[i].data)); 4935 4936 length = __le32_to_cpu(phy->blk_hdr.length); 4937 length -= __le16_to_cpu(phy->fw_offset); 4938 num = length / 2; 4939 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); 4940 4941 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); 4942 for (i = 0; i < num; i++) 4943 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); 4944 4945 num = phy->bp_num; 4946 for (i = 0; i < num; i++) 4947 sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data)); 4948 4949 if (phy->bp_num && phy->bp_en.addr) 4950 sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data)); 4951 4952 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 4953 } 4954 4955 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy) 4956 { 4957 u16 mode_reg, bp_index; 4958 u32 length, i, num; 4959 __le16 *data; 4960 4961 rtl_reset_ocp_base(tp); 4962 4963 mode_reg = __le16_to_cpu(phy->mode_reg); 4964 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre)); 4965 sram_write(tp, __le16_to_cpu(phy->ba_reg), 4966 __le16_to_cpu(phy->ba_data)); 4967 4968 length = __le32_to_cpu(phy->blk_hdr.length); 4969 length -= __le16_to_cpu(phy->fw_offset); 4970 num = length / 2; 4971 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); 4972 4973 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); 4974 for (i = 0; i < num; i++) 4975 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); 4976 4977 sram_write(tp, __le16_to_cpu(phy->patch_en_addr), 4978 __le16_to_cpu(phy->patch_en_value)); 4979 4980 bp_index = __le16_to_cpu(phy->bp_start); 4981 num = __le16_to_cpu(phy->bp_num); 4982 for (i = 0; i < num; i++) { 4983 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i])); 4984 bp_index += 2; 4985 } 4986 4987 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post)); 4988 4989 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 4990 } 4991 4992 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac) 4993 { 4994 u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg; 4995 u32 length; 4996 u8 *data; 4997 int i; 4998 4999 switch (__le32_to_cpu(mac->blk_hdr.type)) { 5000 case RTL_FW_PLA: 5001 type = MCU_TYPE_PLA; 5002 break; 5003 case RTL_FW_USB: 5004 type = MCU_TYPE_USB; 5005 break; 5006 default: 5007 return; 5008 } 5009 5010 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg); 5011 if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) { 5012 dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB"); 5013 return; 5014 } 5015 5016 rtl_clear_bp(tp, type); 5017 5018 /* Enable backup/restore of MACDBG. This is required after clearing PLA 5019 * break points and before applying the PLA firmware. 5020 */ 5021 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA && 5022 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) { 5023 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM); 5024 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM); 5025 } 5026 5027 length = __le32_to_cpu(mac->blk_hdr.length); 5028 length -= __le16_to_cpu(mac->fw_offset); 5029 5030 data = (u8 *)mac; 5031 data += __le16_to_cpu(mac->fw_offset); 5032 5033 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data, 5034 type); 5035 5036 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr), 5037 __le16_to_cpu(mac->bp_ba_value)); 5038 5039 bp_index = __le16_to_cpu(mac->bp_start); 5040 bp_num = __le16_to_cpu(mac->bp_num); 5041 for (i = 0; i < bp_num; i++) { 5042 ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i])); 5043 bp_index += 2; 5044 } 5045 5046 bp_en_addr = __le16_to_cpu(mac->bp_en_addr); 5047 if (bp_en_addr) 5048 ocp_write_word(tp, type, bp_en_addr, 5049 __le16_to_cpu(mac->bp_en_value)); 5050 5051 if (fw_ver_reg) 5052 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg, 5053 mac->fw_ver_data); 5054 5055 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info); 5056 } 5057 5058 static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut) 5059 { 5060 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5061 const struct firmware *fw; 5062 struct fw_header *fw_hdr; 5063 struct fw_phy_patch_key *key; 5064 u16 key_addr = 0; 5065 int i, patch_phy = 1; 5066 5067 if (IS_ERR_OR_NULL(rtl_fw->fw)) 5068 return; 5069 5070 fw = rtl_fw->fw; 5071 fw_hdr = (struct fw_header *)fw->data; 5072 5073 if (rtl_fw->pre_fw) 5074 rtl_fw->pre_fw(tp); 5075 5076 for (i = offsetof(struct fw_header, blocks); i < fw->size;) { 5077 struct fw_block *block = (struct fw_block *)&fw->data[i]; 5078 5079 switch (__le32_to_cpu(block->type)) { 5080 case RTL_FW_END: 5081 goto post_fw; 5082 case RTL_FW_PLA: 5083 case RTL_FW_USB: 5084 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block); 5085 break; 5086 case RTL_FW_PHY_START: 5087 if (!patch_phy) 5088 break; 5089 key = (struct fw_phy_patch_key *)block; 5090 key_addr = __le16_to_cpu(key->key_reg); 5091 rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut); 5092 break; 5093 case RTL_FW_PHY_STOP: 5094 if (!patch_phy) 5095 break; 5096 WARN_ON(!key_addr); 5097 rtl_post_ram_code(tp, key_addr, !power_cut); 5098 break; 5099 case RTL_FW_PHY_NC: 5100 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block); 5101 break; 5102 case RTL_FW_PHY_VER: 5103 patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block); 5104 break; 5105 case RTL_FW_PHY_UNION_NC: 5106 case RTL_FW_PHY_UNION_NC1: 5107 case RTL_FW_PHY_UNION_NC2: 5108 case RTL_FW_PHY_UNION_UC2: 5109 case RTL_FW_PHY_UNION_UC: 5110 case RTL_FW_PHY_UNION_MISC: 5111 if (patch_phy) 5112 rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block); 5113 break; 5114 case RTL_FW_PHY_FIXUP: 5115 if (patch_phy) 5116 rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block); 5117 break; 5118 case RTL_FW_PHY_SPEED_UP: 5119 rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut); 5120 break; 5121 default: 5122 break; 5123 } 5124 5125 i += ALIGN(__le32_to_cpu(block->length), 8); 5126 } 5127 5128 post_fw: 5129 if (rtl_fw->post_fw) 5130 rtl_fw->post_fw(tp); 5131 5132 rtl_reset_ocp_base(tp); 5133 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE); 5134 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version); 5135 } 5136 5137 static void rtl8152_release_firmware(struct r8152 *tp) 5138 { 5139 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5140 5141 if (!IS_ERR_OR_NULL(rtl_fw->fw)) { 5142 release_firmware(rtl_fw->fw); 5143 rtl_fw->fw = NULL; 5144 } 5145 } 5146 5147 static int rtl8152_request_firmware(struct r8152 *tp) 5148 { 5149 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5150 long rc; 5151 5152 if (rtl_fw->fw || !rtl_fw->fw_name) { 5153 dev_info(&tp->intf->dev, "skip request firmware\n"); 5154 rc = 0; 5155 goto result; 5156 } 5157 5158 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev); 5159 if (rc < 0) 5160 goto result; 5161 5162 rc = rtl8152_check_firmware(tp, rtl_fw); 5163 if (rc < 0) 5164 release_firmware(rtl_fw->fw); 5165 5166 result: 5167 if (rc) { 5168 rtl_fw->fw = ERR_PTR(rc); 5169 5170 dev_warn(&tp->intf->dev, 5171 "unable to load firmware patch %s (%ld)\n", 5172 rtl_fw->fw_name, rc); 5173 } 5174 5175 return rc; 5176 } 5177 5178 static void r8152_aldps_en(struct r8152 *tp, bool enable) 5179 { 5180 if (enable) { 5181 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | 5182 LINKENA | DIS_SDSAVE); 5183 } else { 5184 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | 5185 DIS_SDSAVE); 5186 msleep(20); 5187 } 5188 } 5189 5190 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) 5191 { 5192 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); 5193 ocp_reg_write(tp, OCP_EEE_DATA, reg); 5194 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); 5195 } 5196 5197 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) 5198 { 5199 u16 data; 5200 5201 r8152_mmd_indirect(tp, dev, reg); 5202 data = ocp_reg_read(tp, OCP_EEE_DATA); 5203 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 5204 5205 return data; 5206 } 5207 5208 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) 5209 { 5210 r8152_mmd_indirect(tp, dev, reg); 5211 ocp_reg_write(tp, OCP_EEE_DATA, data); 5212 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 5213 } 5214 5215 static void r8152_eee_en(struct r8152 *tp, bool enable) 5216 { 5217 u16 config1, config2, config3; 5218 u32 ocp_data; 5219 5220 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); 5221 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; 5222 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); 5223 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; 5224 5225 if (enable) { 5226 ocp_data |= EEE_RX_EN | EEE_TX_EN; 5227 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; 5228 config1 |= sd_rise_time(1); 5229 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; 5230 config3 |= fast_snr(42); 5231 } else { 5232 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); 5233 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | 5234 RX_QUIET_EN); 5235 config1 |= sd_rise_time(7); 5236 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); 5237 config3 |= fast_snr(511); 5238 } 5239 5240 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); 5241 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); 5242 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); 5243 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); 5244 } 5245 5246 static void r8153_eee_en(struct r8152 *tp, bool enable) 5247 { 5248 u32 ocp_data; 5249 u16 config; 5250 5251 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); 5252 config = ocp_reg_read(tp, OCP_EEE_CFG); 5253 5254 if (enable) { 5255 ocp_data |= EEE_RX_EN | EEE_TX_EN; 5256 config |= EEE10_EN; 5257 } else { 5258 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); 5259 config &= ~EEE10_EN; 5260 } 5261 5262 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); 5263 ocp_reg_write(tp, OCP_EEE_CFG, config); 5264 5265 tp->ups_info.eee = enable; 5266 } 5267 5268 static void r8156_eee_en(struct r8152 *tp, bool enable) 5269 { 5270 u16 config; 5271 5272 r8153_eee_en(tp, enable); 5273 5274 config = ocp_reg_read(tp, OCP_EEE_ADV2); 5275 5276 if (enable) 5277 config |= MDIO_EEE_2_5GT; 5278 else 5279 config &= ~MDIO_EEE_2_5GT; 5280 5281 ocp_reg_write(tp, OCP_EEE_ADV2, config); 5282 } 5283 5284 static void rtl_eee_enable(struct r8152 *tp, bool enable) 5285 { 5286 switch (tp->version) { 5287 case RTL_VER_01: 5288 case RTL_VER_02: 5289 case RTL_VER_07: 5290 if (enable) { 5291 r8152_eee_en(tp, true); 5292 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 5293 tp->eee_adv); 5294 } else { 5295 r8152_eee_en(tp, false); 5296 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); 5297 } 5298 break; 5299 case RTL_VER_03: 5300 case RTL_VER_04: 5301 case RTL_VER_05: 5302 case RTL_VER_06: 5303 case RTL_VER_08: 5304 case RTL_VER_09: 5305 case RTL_VER_14: 5306 if (enable) { 5307 r8153_eee_en(tp, true); 5308 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); 5309 } else { 5310 r8153_eee_en(tp, false); 5311 ocp_reg_write(tp, OCP_EEE_ADV, 0); 5312 } 5313 break; 5314 case RTL_VER_10: 5315 case RTL_VER_11: 5316 case RTL_VER_12: 5317 case RTL_VER_13: 5318 case RTL_VER_15: 5319 if (enable) { 5320 r8156_eee_en(tp, true); 5321 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); 5322 } else { 5323 r8156_eee_en(tp, false); 5324 ocp_reg_write(tp, OCP_EEE_ADV, 0); 5325 } 5326 break; 5327 default: 5328 break; 5329 } 5330 } 5331 5332 static void r8152b_enable_fc(struct r8152 *tp) 5333 { 5334 u16 anar; 5335 5336 anar = r8152_mdio_read(tp, MII_ADVERTISE); 5337 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; 5338 r8152_mdio_write(tp, MII_ADVERTISE, anar); 5339 5340 tp->ups_info.flow_control = true; 5341 } 5342 5343 static void rtl8152_disable(struct r8152 *tp) 5344 { 5345 r8152_aldps_en(tp, false); 5346 rtl_disable(tp); 5347 r8152_aldps_en(tp, true); 5348 } 5349 5350 static void r8152b_hw_phy_cfg(struct r8152 *tp) 5351 { 5352 rtl8152_apply_firmware(tp, false); 5353 rtl_eee_enable(tp, tp->eee_en); 5354 r8152_aldps_en(tp, true); 5355 r8152b_enable_fc(tp); 5356 5357 set_bit(PHY_RESET, &tp->flags); 5358 } 5359 5360 static void wait_oob_link_list_ready(struct r8152 *tp) 5361 { 5362 u32 ocp_data; 5363 int i; 5364 5365 for (i = 0; i < 1000; i++) { 5366 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5367 if (ocp_data & LINK_LIST_READY) 5368 break; 5369 usleep_range(1000, 2000); 5370 } 5371 } 5372 5373 static void r8156b_wait_loading_flash(struct r8152 *tp) 5374 { 5375 if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) && 5376 !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) { 5377 int i; 5378 5379 for (i = 0; i < 100; i++) { 5380 if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE) 5381 break; 5382 usleep_range(1000, 2000); 5383 } 5384 } 5385 } 5386 5387 static void r8152b_exit_oob(struct r8152 *tp) 5388 { 5389 u32 ocp_data; 5390 5391 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5392 ocp_data &= ~RCR_ACPT_ALL; 5393 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5394 5395 rxdy_gated_en(tp, true); 5396 r8153_teredo_off(tp); 5397 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 5398 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); 5399 5400 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5401 ocp_data &= ~NOW_IS_OOB; 5402 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5403 5404 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5405 ocp_data &= ~MCU_BORW_EN; 5406 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5407 5408 wait_oob_link_list_ready(tp); 5409 5410 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5411 ocp_data |= RE_INIT_LL; 5412 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5413 5414 wait_oob_link_list_ready(tp); 5415 5416 rtl8152_nic_reset(tp); 5417 5418 /* rx share fifo credit full threshold */ 5419 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 5420 5421 if (tp->udev->speed == USB_SPEED_FULL || 5422 tp->udev->speed == USB_SPEED_LOW) { 5423 /* rx share fifo credit near full threshold */ 5424 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 5425 RXFIFO_THR2_FULL); 5426 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 5427 RXFIFO_THR3_FULL); 5428 } else { 5429 /* rx share fifo credit near full threshold */ 5430 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 5431 RXFIFO_THR2_HIGH); 5432 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 5433 RXFIFO_THR3_HIGH); 5434 } 5435 5436 /* TX share fifo free credit full threshold */ 5437 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); 5438 5439 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); 5440 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); 5441 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, 5442 TEST_MODE_DISABLE | TX_SIZE_ADJUST1); 5443 5444 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 5445 5446 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 5447 5448 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); 5449 ocp_data |= TCR0_AUTO_FIFO; 5450 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); 5451 } 5452 5453 static void r8152b_enter_oob(struct r8152 *tp) 5454 { 5455 u32 ocp_data; 5456 5457 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5458 ocp_data &= ~NOW_IS_OOB; 5459 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5460 5461 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 5462 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 5463 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 5464 5465 rtl_disable(tp); 5466 5467 wait_oob_link_list_ready(tp); 5468 5469 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5470 ocp_data |= RE_INIT_LL; 5471 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5472 5473 wait_oob_link_list_ready(tp); 5474 5475 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 5476 5477 rtl_rx_vlan_en(tp, true); 5478 5479 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); 5480 ocp_data |= ALDPS_PROXY_MODE; 5481 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); 5482 5483 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5484 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; 5485 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5486 5487 rxdy_gated_en(tp, false); 5488 5489 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5490 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 5491 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5492 } 5493 5494 static int r8153_pre_firmware_1(struct r8152 *tp) 5495 { 5496 int i; 5497 5498 /* Wait till the WTD timer is ready. It would take at most 104 ms. */ 5499 for (i = 0; i < 104; i++) { 5500 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL); 5501 5502 if (!(ocp_data & WTD1_EN)) 5503 break; 5504 usleep_range(1000, 2000); 5505 } 5506 5507 return 0; 5508 } 5509 5510 static int r8153_post_firmware_1(struct r8152 *tp) 5511 { 5512 /* set USB_BP_4 to support USB_SPEED_SUPER only */ 5513 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) 5514 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY); 5515 5516 /* reset UPHY timer to 36 ms */ 5517 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 5518 5519 return 0; 5520 } 5521 5522 static int r8153_pre_firmware_2(struct r8152 *tp) 5523 { 5524 u32 ocp_data; 5525 5526 r8153_pre_firmware_1(tp); 5527 5528 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); 5529 ocp_data &= ~FW_FIX_SUSPEND; 5530 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); 5531 5532 return 0; 5533 } 5534 5535 static int r8153_post_firmware_2(struct r8152 *tp) 5536 { 5537 u32 ocp_data; 5538 5539 /* enable bp0 if support USB_SPEED_SUPER only */ 5540 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) { 5541 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); 5542 ocp_data |= BIT(0); 5543 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); 5544 } 5545 5546 /* reset UPHY timer to 36 ms */ 5547 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 5548 5549 /* enable U3P3 check, set the counter to 4 */ 5550 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4); 5551 5552 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); 5553 ocp_data |= FW_FIX_SUSPEND; 5554 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); 5555 5556 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 5557 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 5558 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 5559 5560 return 0; 5561 } 5562 5563 static int r8153_post_firmware_3(struct r8152 *tp) 5564 { 5565 u32 ocp_data; 5566 5567 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 5568 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 5569 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 5570 5571 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 5572 ocp_data |= FW_IP_RESET_EN; 5573 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 5574 5575 return 0; 5576 } 5577 5578 static int r8153b_pre_firmware_1(struct r8152 *tp) 5579 { 5580 /* enable fc timer and set timer to 1 second. */ 5581 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, 5582 CTRL_TIMER_EN | (1000 / 8)); 5583 5584 return 0; 5585 } 5586 5587 static int r8153b_post_firmware_1(struct r8152 *tp) 5588 { 5589 u32 ocp_data; 5590 5591 /* enable bp0 for RTL8153-BND */ 5592 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); 5593 if (ocp_data & BND_MASK) { 5594 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); 5595 ocp_data |= BIT(0); 5596 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); 5597 } 5598 5599 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 5600 ocp_data |= FLOW_CTRL_PATCH_OPT; 5601 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 5602 5603 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 5604 ocp_data |= FC_PATCH_TASK; 5605 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 5606 5607 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 5608 ocp_data |= FW_IP_RESET_EN; 5609 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 5610 5611 return 0; 5612 } 5613 5614 static int r8153c_post_firmware_1(struct r8152 *tp) 5615 { 5616 u32 ocp_data; 5617 5618 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 5619 ocp_data |= FLOW_CTRL_PATCH_2; 5620 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 5621 5622 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 5623 ocp_data |= FC_PATCH_TASK; 5624 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 5625 5626 return 0; 5627 } 5628 5629 static int r8156a_post_firmware_1(struct r8152 *tp) 5630 { 5631 u32 ocp_data; 5632 5633 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 5634 ocp_data |= FW_IP_RESET_EN; 5635 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 5636 5637 /* Modify U3PHY parameter for compatibility issue */ 5638 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e); 5639 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9); 5640 5641 return 0; 5642 } 5643 5644 static void r8153_aldps_en(struct r8152 *tp, bool enable) 5645 { 5646 u16 data; 5647 5648 data = ocp_reg_read(tp, OCP_POWER_CFG); 5649 if (enable) { 5650 data |= EN_ALDPS; 5651 ocp_reg_write(tp, OCP_POWER_CFG, data); 5652 } else { 5653 int i; 5654 5655 data &= ~EN_ALDPS; 5656 ocp_reg_write(tp, OCP_POWER_CFG, data); 5657 for (i = 0; i < 20; i++) { 5658 usleep_range(1000, 2000); 5659 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) 5660 break; 5661 } 5662 } 5663 5664 tp->ups_info.aldps = enable; 5665 } 5666 5667 static void r8153_hw_phy_cfg(struct r8152 *tp) 5668 { 5669 u32 ocp_data; 5670 u16 data; 5671 5672 /* disable ALDPS before updating the PHY parameters */ 5673 r8153_aldps_en(tp, false); 5674 5675 /* disable EEE before updating the PHY parameters */ 5676 rtl_eee_enable(tp, false); 5677 5678 rtl8152_apply_firmware(tp, false); 5679 5680 if (tp->version == RTL_VER_03) { 5681 data = ocp_reg_read(tp, OCP_EEE_CFG); 5682 data &= ~CTAP_SHORT_EN; 5683 ocp_reg_write(tp, OCP_EEE_CFG, data); 5684 } 5685 5686 data = ocp_reg_read(tp, OCP_POWER_CFG); 5687 data |= EEE_CLKDIV_EN; 5688 ocp_reg_write(tp, OCP_POWER_CFG, data); 5689 5690 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 5691 data |= EN_10M_BGOFF; 5692 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 5693 data = ocp_reg_read(tp, OCP_POWER_CFG); 5694 data |= EN_10M_PLLOFF; 5695 ocp_reg_write(tp, OCP_POWER_CFG, data); 5696 sram_write(tp, SRAM_IMPEDANCE, 0x0b13); 5697 5698 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 5699 ocp_data |= PFM_PWM_SWITCH; 5700 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 5701 5702 /* Enable LPF corner auto tune */ 5703 sram_write(tp, SRAM_LPF_CFG, 0xf70f); 5704 5705 /* Adjust 10M Amplitude */ 5706 sram_write(tp, SRAM_10M_AMP1, 0x00af); 5707 sram_write(tp, SRAM_10M_AMP2, 0x0208); 5708 5709 if (tp->eee_en) 5710 rtl_eee_enable(tp, true); 5711 5712 r8153_aldps_en(tp, true); 5713 r8152b_enable_fc(tp); 5714 5715 switch (tp->version) { 5716 case RTL_VER_03: 5717 case RTL_VER_04: 5718 break; 5719 case RTL_VER_05: 5720 case RTL_VER_06: 5721 default: 5722 r8153_u2p3en(tp, true); 5723 break; 5724 } 5725 5726 set_bit(PHY_RESET, &tp->flags); 5727 } 5728 5729 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) 5730 { 5731 u32 ocp_data; 5732 5733 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); 5734 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); 5735 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ 5736 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); 5737 5738 return ocp_data; 5739 } 5740 5741 static void r8153b_hw_phy_cfg(struct r8152 *tp) 5742 { 5743 u32 ocp_data; 5744 u16 data; 5745 5746 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 5747 if (ocp_data & PCUT_STATUS) { 5748 ocp_data &= ~PCUT_STATUS; 5749 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 5750 } 5751 5752 /* disable ALDPS before updating the PHY parameters */ 5753 r8153_aldps_en(tp, false); 5754 5755 /* disable EEE before updating the PHY parameters */ 5756 rtl_eee_enable(tp, false); 5757 5758 /* U1/U2/L1 idle timer. 500 us */ 5759 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 5760 5761 data = r8153_phy_status(tp, 0); 5762 5763 switch (data) { 5764 case PHY_STAT_PWRDN: 5765 case PHY_STAT_EXT_INIT: 5766 rtl8152_apply_firmware(tp, true); 5767 5768 data = r8152_mdio_read(tp, MII_BMCR); 5769 data &= ~BMCR_PDOWN; 5770 r8152_mdio_write(tp, MII_BMCR, data); 5771 break; 5772 case PHY_STAT_LAN_ON: 5773 default: 5774 rtl8152_apply_firmware(tp, false); 5775 break; 5776 } 5777 5778 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 5779 5780 data = sram_read(tp, SRAM_GREEN_CFG); 5781 data |= R_TUNE_EN; 5782 sram_write(tp, SRAM_GREEN_CFG, data); 5783 data = ocp_reg_read(tp, OCP_NCTL_CFG); 5784 data |= PGA_RETURN_EN; 5785 ocp_reg_write(tp, OCP_NCTL_CFG, data); 5786 5787 /* ADC Bias Calibration: 5788 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake 5789 * bit (bit3) to rebuild the real 16-bit data. Write the data to the 5790 * ADC ioffset. 5791 */ 5792 ocp_data = r8152_efuse_read(tp, 0x7d); 5793 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); 5794 if (data != 0xffff) 5795 ocp_reg_write(tp, OCP_ADC_IOFFSET, data); 5796 5797 /* ups mode tx-link-pulse timing adjustment: 5798 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] 5799 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt 5800 */ 5801 ocp_data = ocp_reg_read(tp, 0xc426); 5802 ocp_data &= 0x3fff; 5803 if (ocp_data) { 5804 u32 swr_cnt_1ms_ini; 5805 5806 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; 5807 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); 5808 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; 5809 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); 5810 } 5811 5812 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 5813 ocp_data |= PFM_PWM_SWITCH; 5814 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 5815 5816 /* Advnace EEE */ 5817 if (!rtl_phy_patch_request(tp, true, true)) { 5818 data = ocp_reg_read(tp, OCP_POWER_CFG); 5819 data |= EEE_CLKDIV_EN; 5820 ocp_reg_write(tp, OCP_POWER_CFG, data); 5821 tp->ups_info.eee_ckdiv = true; 5822 5823 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 5824 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV; 5825 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 5826 tp->ups_info.eee_cmod_lv = true; 5827 tp->ups_info._10m_ckdiv = true; 5828 tp->ups_info.eee_plloff_giga = true; 5829 5830 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); 5831 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); 5832 tp->ups_info._250m_ckdiv = true; 5833 5834 rtl_phy_patch_request(tp, false, true); 5835 } 5836 5837 if (tp->eee_en) 5838 rtl_eee_enable(tp, true); 5839 5840 r8153_aldps_en(tp, true); 5841 r8152b_enable_fc(tp); 5842 5843 set_bit(PHY_RESET, &tp->flags); 5844 } 5845 5846 static void r8153c_hw_phy_cfg(struct r8152 *tp) 5847 { 5848 r8153b_hw_phy_cfg(tp); 5849 5850 tp->ups_info.r_tune = true; 5851 } 5852 5853 static void rtl8153_change_mtu(struct r8152 *tp) 5854 { 5855 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu)); 5856 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); 5857 } 5858 5859 static void r8153_first_init(struct r8152 *tp) 5860 { 5861 u32 ocp_data; 5862 5863 rxdy_gated_en(tp, true); 5864 r8153_teredo_off(tp); 5865 5866 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5867 ocp_data &= ~RCR_ACPT_ALL; 5868 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5869 5870 rtl8152_nic_reset(tp); 5871 rtl_reset_bmu(tp); 5872 5873 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5874 ocp_data &= ~NOW_IS_OOB; 5875 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5876 5877 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5878 ocp_data &= ~MCU_BORW_EN; 5879 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5880 5881 wait_oob_link_list_ready(tp); 5882 5883 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5884 ocp_data |= RE_INIT_LL; 5885 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5886 5887 wait_oob_link_list_ready(tp); 5888 5889 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 5890 5891 rtl8153_change_mtu(tp); 5892 5893 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); 5894 ocp_data |= TCR0_AUTO_FIFO; 5895 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); 5896 5897 rtl8152_nic_reset(tp); 5898 5899 /* rx share fifo credit full threshold */ 5900 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 5901 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); 5902 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); 5903 /* TX share fifo free credit full threshold */ 5904 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); 5905 } 5906 5907 static void r8153_enter_oob(struct r8152 *tp) 5908 { 5909 u32 ocp_data; 5910 5911 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5912 ocp_data &= ~NOW_IS_OOB; 5913 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5914 5915 /* RX FIFO settings for OOB */ 5916 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 5917 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 5918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 5919 5920 rtl_disable(tp); 5921 rtl_reset_bmu(tp); 5922 5923 wait_oob_link_list_ready(tp); 5924 5925 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5926 ocp_data |= RE_INIT_LL; 5927 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5928 5929 wait_oob_link_list_ready(tp); 5930 5931 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522); 5932 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT); 5933 5934 switch (tp->version) { 5935 case RTL_VER_03: 5936 case RTL_VER_04: 5937 case RTL_VER_05: 5938 case RTL_VER_06: 5939 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); 5940 ocp_data &= ~TEREDO_WAKE_MASK; 5941 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); 5942 break; 5943 5944 case RTL_VER_08: 5945 case RTL_VER_09: 5946 case RTL_VER_14: 5947 /* Clear teredo wake event. bit[15:8] is the teredo wakeup 5948 * type. Set it to zero. bits[7:0] are the W1C bits about 5949 * the events. Set them to all 1 to clear them. 5950 */ 5951 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); 5952 break; 5953 5954 default: 5955 break; 5956 } 5957 5958 rtl_rx_vlan_en(tp, true); 5959 5960 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); 5961 ocp_data |= ALDPS_PROXY_MODE; 5962 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); 5963 5964 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5965 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; 5966 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5967 5968 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5969 ocp_data |= MCU_BORW_EN; 5970 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5971 5972 rxdy_gated_en(tp, false); 5973 5974 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5975 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 5976 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5977 } 5978 5979 static void rtl8153_disable(struct r8152 *tp) 5980 { 5981 r8153_aldps_en(tp, false); 5982 rtl_disable(tp); 5983 rtl_reset_bmu(tp); 5984 r8153_aldps_en(tp, true); 5985 } 5986 5987 static int rtl8156_enable(struct r8152 *tp) 5988 { 5989 u32 ocp_data; 5990 u16 speed; 5991 5992 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 5993 return -ENODEV; 5994 5995 set_tx_qlen(tp); 5996 rtl_set_eee_plus(tp); 5997 r8153_set_rx_early_timeout(tp); 5998 r8153_set_rx_early_size(tp); 5999 6000 speed = rtl8152_get_speed(tp); 6001 rtl_set_ifg(tp, speed); 6002 6003 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 6004 if (speed & _2500bps) 6005 ocp_data &= ~IDLE_SPDWN_EN; 6006 else 6007 ocp_data |= IDLE_SPDWN_EN; 6008 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 6009 6010 if (speed & _1000bps) 6011 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11); 6012 else if (speed & _500bps) 6013 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d); 6014 6015 if (tp->udev->speed == USB_SPEED_HIGH) { 6016 /* USB 0xb45e[3:0] l1_nyet_hird */ 6017 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL); 6018 ocp_data &= ~0xf; 6019 if (is_flow_control(speed)) 6020 ocp_data |= 0xf; 6021 else 6022 ocp_data |= 0x1; 6023 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data); 6024 } 6025 6026 return rtl_enable(tp); 6027 } 6028 6029 static int rtl8156b_enable(struct r8152 *tp) 6030 { 6031 u32 ocp_data; 6032 u16 speed; 6033 6034 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6035 return -ENODEV; 6036 6037 set_tx_qlen(tp); 6038 rtl_set_eee_plus(tp); 6039 6040 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM); 6041 ocp_data &= ~RX_AGGR_NUM_MASK; 6042 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data); 6043 6044 r8153_set_rx_early_timeout(tp); 6045 r8153_set_rx_early_size(tp); 6046 6047 speed = rtl8152_get_speed(tp); 6048 rtl_set_ifg(tp, speed); 6049 6050 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 6051 if (speed & _2500bps) 6052 ocp_data &= ~IDLE_SPDWN_EN; 6053 else 6054 ocp_data |= IDLE_SPDWN_EN; 6055 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 6056 6057 if (tp->udev->speed == USB_SPEED_HIGH) { 6058 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL); 6059 ocp_data &= ~0xf; 6060 if (is_flow_control(speed)) 6061 ocp_data |= 0xf; 6062 else 6063 ocp_data |= 0x1; 6064 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data); 6065 } 6066 6067 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 6068 ocp_data &= ~FC_PATCH_TASK; 6069 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 6070 usleep_range(1000, 2000); 6071 ocp_data |= FC_PATCH_TASK; 6072 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 6073 6074 return rtl_enable(tp); 6075 } 6076 6077 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, 6078 u32 advertising) 6079 { 6080 u16 bmcr; 6081 int ret = 0; 6082 6083 if (autoneg == AUTONEG_DISABLE) { 6084 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL) 6085 return -EINVAL; 6086 6087 switch (speed) { 6088 case SPEED_10: 6089 bmcr = BMCR_SPEED10; 6090 if (duplex == DUPLEX_FULL) { 6091 bmcr |= BMCR_FULLDPLX; 6092 tp->ups_info.speed_duplex = FORCE_10M_FULL; 6093 } else { 6094 tp->ups_info.speed_duplex = FORCE_10M_HALF; 6095 } 6096 break; 6097 case SPEED_100: 6098 bmcr = BMCR_SPEED100; 6099 if (duplex == DUPLEX_FULL) { 6100 bmcr |= BMCR_FULLDPLX; 6101 tp->ups_info.speed_duplex = FORCE_100M_FULL; 6102 } else { 6103 tp->ups_info.speed_duplex = FORCE_100M_HALF; 6104 } 6105 break; 6106 case SPEED_1000: 6107 if (tp->mii.supports_gmii) { 6108 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX; 6109 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 6110 break; 6111 } 6112 fallthrough; 6113 default: 6114 ret = -EINVAL; 6115 goto out; 6116 } 6117 6118 if (duplex == DUPLEX_FULL) 6119 tp->mii.full_duplex = 1; 6120 else 6121 tp->mii.full_duplex = 0; 6122 6123 tp->mii.force_media = 1; 6124 } else { 6125 u16 orig, new1; 6126 u32 support; 6127 6128 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 6129 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 6130 6131 if (tp->mii.supports_gmii) { 6132 support |= RTL_ADVERTISED_1000_FULL; 6133 6134 if (tp->support_2500full) 6135 support |= RTL_ADVERTISED_2500_FULL; 6136 } 6137 6138 if (!(advertising & support)) 6139 return -EINVAL; 6140 6141 orig = r8152_mdio_read(tp, MII_ADVERTISE); 6142 new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL | 6143 ADVERTISE_100HALF | ADVERTISE_100FULL); 6144 if (advertising & RTL_ADVERTISED_10_HALF) { 6145 new1 |= ADVERTISE_10HALF; 6146 tp->ups_info.speed_duplex = NWAY_10M_HALF; 6147 } 6148 if (advertising & RTL_ADVERTISED_10_FULL) { 6149 new1 |= ADVERTISE_10FULL; 6150 tp->ups_info.speed_duplex = NWAY_10M_FULL; 6151 } 6152 6153 if (advertising & RTL_ADVERTISED_100_HALF) { 6154 new1 |= ADVERTISE_100HALF; 6155 tp->ups_info.speed_duplex = NWAY_100M_HALF; 6156 } 6157 if (advertising & RTL_ADVERTISED_100_FULL) { 6158 new1 |= ADVERTISE_100FULL; 6159 tp->ups_info.speed_duplex = NWAY_100M_FULL; 6160 } 6161 6162 if (orig != new1) { 6163 r8152_mdio_write(tp, MII_ADVERTISE, new1); 6164 tp->mii.advertising = new1; 6165 } 6166 6167 if (tp->mii.supports_gmii) { 6168 orig = r8152_mdio_read(tp, MII_CTRL1000); 6169 new1 = orig & ~(ADVERTISE_1000FULL | 6170 ADVERTISE_1000HALF); 6171 6172 if (advertising & RTL_ADVERTISED_1000_FULL) { 6173 new1 |= ADVERTISE_1000FULL; 6174 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 6175 } 6176 6177 if (orig != new1) 6178 r8152_mdio_write(tp, MII_CTRL1000, new1); 6179 } 6180 6181 if (tp->support_2500full) { 6182 orig = ocp_reg_read(tp, OCP_10GBT_CTRL); 6183 new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G; 6184 6185 if (advertising & RTL_ADVERTISED_2500_FULL) { 6186 new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G; 6187 tp->ups_info.speed_duplex = NWAY_2500M_FULL; 6188 } 6189 6190 if (orig != new1) 6191 ocp_reg_write(tp, OCP_10GBT_CTRL, new1); 6192 } 6193 6194 bmcr = BMCR_ANENABLE | BMCR_ANRESTART; 6195 6196 tp->mii.force_media = 0; 6197 } 6198 6199 if (test_and_clear_bit(PHY_RESET, &tp->flags)) 6200 bmcr |= BMCR_RESET; 6201 6202 r8152_mdio_write(tp, MII_BMCR, bmcr); 6203 6204 if (bmcr & BMCR_RESET) { 6205 int i; 6206 6207 for (i = 0; i < 50; i++) { 6208 msleep(20); 6209 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) 6210 break; 6211 } 6212 } 6213 6214 out: 6215 return ret; 6216 } 6217 6218 static void rtl8152_up(struct r8152 *tp) 6219 { 6220 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6221 return; 6222 6223 r8152_aldps_en(tp, false); 6224 r8152b_exit_oob(tp); 6225 r8152_aldps_en(tp, true); 6226 } 6227 6228 static void rtl8152_down(struct r8152 *tp) 6229 { 6230 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 6231 rtl_drop_queued_tx(tp); 6232 return; 6233 } 6234 6235 r8152_power_cut_en(tp, false); 6236 r8152_aldps_en(tp, false); 6237 r8152b_enter_oob(tp); 6238 r8152_aldps_en(tp, true); 6239 } 6240 6241 static void rtl8153_up(struct r8152 *tp) 6242 { 6243 u32 ocp_data; 6244 6245 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6246 return; 6247 6248 r8153_u1u2en(tp, false); 6249 r8153_u2p3en(tp, false); 6250 r8153_aldps_en(tp, false); 6251 r8153_first_init(tp); 6252 6253 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 6254 ocp_data |= LANWAKE_CLR_EN; 6255 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 6256 6257 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG); 6258 ocp_data &= ~LANWAKE_PIN; 6259 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data); 6260 6261 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1); 6262 ocp_data &= ~DELAY_PHY_PWR_CHG; 6263 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data); 6264 6265 r8153_aldps_en(tp, true); 6266 6267 switch (tp->version) { 6268 case RTL_VER_03: 6269 case RTL_VER_04: 6270 break; 6271 case RTL_VER_05: 6272 case RTL_VER_06: 6273 default: 6274 r8153_u2p3en(tp, true); 6275 break; 6276 } 6277 6278 r8153_u1u2en(tp, true); 6279 } 6280 6281 static void rtl8153_down(struct r8152 *tp) 6282 { 6283 u32 ocp_data; 6284 6285 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 6286 rtl_drop_queued_tx(tp); 6287 return; 6288 } 6289 6290 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 6291 ocp_data &= ~LANWAKE_CLR_EN; 6292 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 6293 6294 r8153_u1u2en(tp, false); 6295 r8153_u2p3en(tp, false); 6296 r8153_power_cut_en(tp, false); 6297 r8153_aldps_en(tp, false); 6298 r8153_enter_oob(tp); 6299 r8153_aldps_en(tp, true); 6300 } 6301 6302 static void rtl8153b_up(struct r8152 *tp) 6303 { 6304 u32 ocp_data; 6305 6306 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6307 return; 6308 6309 r8153b_u1u2en(tp, false); 6310 r8153_u2p3en(tp, false); 6311 r8153_aldps_en(tp, false); 6312 6313 r8153_first_init(tp); 6314 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); 6315 6316 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6317 ocp_data &= ~PLA_MCU_SPDWN_EN; 6318 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6319 6320 r8153_aldps_en(tp, true); 6321 6322 if (tp->udev->speed >= USB_SPEED_SUPER) 6323 r8153b_u1u2en(tp, true); 6324 } 6325 6326 static void rtl8153b_down(struct r8152 *tp) 6327 { 6328 u32 ocp_data; 6329 6330 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 6331 rtl_drop_queued_tx(tp); 6332 return; 6333 } 6334 6335 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6336 ocp_data |= PLA_MCU_SPDWN_EN; 6337 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6338 6339 r8153b_u1u2en(tp, false); 6340 r8153_u2p3en(tp, false); 6341 r8153b_power_cut_en(tp, false); 6342 r8153_aldps_en(tp, false); 6343 r8153_enter_oob(tp); 6344 r8153_aldps_en(tp, true); 6345 } 6346 6347 static void rtl8153c_change_mtu(struct r8152 *tp) 6348 { 6349 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu)); 6350 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64); 6351 6352 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64); 6353 6354 /* Adjust the tx fifo free credit full threshold, otherwise 6355 * the fifo would be too small to send a jumbo frame packet. 6356 */ 6357 if (tp->netdev->mtu < 8000) 6358 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8); 6359 else 6360 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8); 6361 } 6362 6363 static void rtl8153c_up(struct r8152 *tp) 6364 { 6365 u32 ocp_data; 6366 6367 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6368 return; 6369 6370 r8153b_u1u2en(tp, false); 6371 r8153_u2p3en(tp, false); 6372 r8153_aldps_en(tp, false); 6373 6374 rxdy_gated_en(tp, true); 6375 r8153_teredo_off(tp); 6376 6377 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 6378 ocp_data &= ~RCR_ACPT_ALL; 6379 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 6380 6381 rtl8152_nic_reset(tp); 6382 rtl_reset_bmu(tp); 6383 6384 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6385 ocp_data &= ~NOW_IS_OOB; 6386 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6387 6388 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6389 ocp_data &= ~MCU_BORW_EN; 6390 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6391 6392 wait_oob_link_list_ready(tp); 6393 6394 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6395 ocp_data |= RE_INIT_LL; 6396 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6397 6398 wait_oob_link_list_ready(tp); 6399 6400 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 6401 6402 rtl8153c_change_mtu(tp); 6403 6404 rtl8152_nic_reset(tp); 6405 6406 /* rx share fifo credit full threshold */ 6407 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02); 6408 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08); 6409 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); 6410 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); 6411 6412 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); 6413 6414 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 6415 6416 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 6417 ocp_data |= BIT(8); 6418 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 6419 6420 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 6421 6422 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6423 ocp_data &= ~PLA_MCU_SPDWN_EN; 6424 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6425 6426 r8153_aldps_en(tp, true); 6427 r8153b_u1u2en(tp, true); 6428 } 6429 6430 static inline u32 fc_pause_on_auto(struct r8152 *tp) 6431 { 6432 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024); 6433 } 6434 6435 static inline u32 fc_pause_off_auto(struct r8152 *tp) 6436 { 6437 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024); 6438 } 6439 6440 static void r8156_fc_parameter(struct r8152 *tp) 6441 { 6442 u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp); 6443 u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp); 6444 6445 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16); 6446 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16); 6447 } 6448 6449 static void rtl8156_change_mtu(struct r8152 *tp) 6450 { 6451 u32 rx_max_size = mtu_to_size(tp->netdev->mtu); 6452 6453 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size); 6454 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); 6455 r8156_fc_parameter(tp); 6456 6457 /* TX share fifo free credit full threshold */ 6458 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64); 6459 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 6460 ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16); 6461 } 6462 6463 static void rtl8156_up(struct r8152 *tp) 6464 { 6465 u32 ocp_data; 6466 6467 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6468 return; 6469 6470 r8153b_u1u2en(tp, false); 6471 r8153_u2p3en(tp, false); 6472 r8153_aldps_en(tp, false); 6473 6474 rxdy_gated_en(tp, true); 6475 r8153_teredo_off(tp); 6476 6477 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 6478 ocp_data &= ~RCR_ACPT_ALL; 6479 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 6480 6481 rtl8152_nic_reset(tp); 6482 rtl_reset_bmu(tp); 6483 6484 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6485 ocp_data &= ~NOW_IS_OOB; 6486 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6487 6488 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6489 ocp_data &= ~MCU_BORW_EN; 6490 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6491 6492 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 6493 6494 rtl8156_change_mtu(tp); 6495 6496 switch (tp->version) { 6497 case RTL_TEST_01: 6498 case RTL_VER_10: 6499 case RTL_VER_11: 6500 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG); 6501 ocp_data |= ACT_ODMA; 6502 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data); 6503 break; 6504 default: 6505 break; 6506 } 6507 6508 /* share FIFO settings */ 6509 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL); 6510 ocp_data &= ~RXFIFO_FULL_MASK; 6511 ocp_data |= 0x08; 6512 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data); 6513 6514 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6515 ocp_data &= ~PLA_MCU_SPDWN_EN; 6516 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6517 6518 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION); 6519 ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF); 6520 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data); 6521 6522 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400); 6523 6524 if (tp->saved_wolopts != __rtl_get_wol(tp)) { 6525 netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n"); 6526 __rtl_set_wol(tp, tp->saved_wolopts); 6527 } 6528 6529 r8153_aldps_en(tp, true); 6530 r8153_u2p3en(tp, true); 6531 6532 if (tp->udev->speed >= USB_SPEED_SUPER) 6533 r8153b_u1u2en(tp, true); 6534 } 6535 6536 static void rtl8156_down(struct r8152 *tp) 6537 { 6538 u32 ocp_data; 6539 6540 if (test_bit(RTL8152_UNPLUG, &tp->flags)) { 6541 rtl_drop_queued_tx(tp); 6542 return; 6543 } 6544 6545 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6546 ocp_data |= PLA_MCU_SPDWN_EN; 6547 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6548 6549 r8153b_u1u2en(tp, false); 6550 r8153_u2p3en(tp, false); 6551 r8153b_power_cut_en(tp, false); 6552 r8153_aldps_en(tp, false); 6553 6554 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6555 ocp_data &= ~NOW_IS_OOB; 6556 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6557 6558 /* RX FIFO settings for OOB */ 6559 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 64 / 16); 6560 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 1024 / 16); 6561 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 4096 / 16); 6562 6563 rtl_disable(tp); 6564 rtl_reset_bmu(tp); 6565 6566 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522); 6567 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT); 6568 6569 /* Clear teredo wake event. bit[15:8] is the teredo wakeup 6570 * type. Set it to zero. bits[7:0] are the W1C bits about 6571 * the events. Set them to all 1 to clear them. 6572 */ 6573 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); 6574 6575 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6576 ocp_data |= NOW_IS_OOB; 6577 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6578 6579 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6580 ocp_data |= MCU_BORW_EN; 6581 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6582 6583 rtl_rx_vlan_en(tp, true); 6584 rxdy_gated_en(tp, false); 6585 6586 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 6587 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 6588 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 6589 6590 r8153_aldps_en(tp, true); 6591 } 6592 6593 static bool rtl8152_in_nway(struct r8152 *tp) 6594 { 6595 u16 nway_state; 6596 6597 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); 6598 tp->ocp_base = 0x2000; 6599 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ 6600 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); 6601 6602 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ 6603 if (nway_state & 0xc000) 6604 return false; 6605 else 6606 return true; 6607 } 6608 6609 static bool rtl8153_in_nway(struct r8152 *tp) 6610 { 6611 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; 6612 6613 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) 6614 return false; 6615 else 6616 return true; 6617 } 6618 6619 static void r8156_mdio_force_mode(struct r8152 *tp) 6620 { 6621 u16 data; 6622 6623 /* Select force mode through 0xa5b4 bit 15 6624 * 0: MDIO force mode 6625 * 1: MMD force mode 6626 */ 6627 data = ocp_reg_read(tp, 0xa5b4); 6628 if (data & BIT(15)) { 6629 data &= ~BIT(15); 6630 ocp_reg_write(tp, 0xa5b4, data); 6631 } 6632 } 6633 6634 static void set_carrier(struct r8152 *tp) 6635 { 6636 struct net_device *netdev = tp->netdev; 6637 struct napi_struct *napi = &tp->napi; 6638 u16 speed; 6639 6640 speed = rtl8152_get_speed(tp); 6641 6642 if (speed & LINK_STATUS) { 6643 if (!netif_carrier_ok(netdev)) { 6644 tp->rtl_ops.enable(tp); 6645 netif_stop_queue(netdev); 6646 napi_disable(napi); 6647 netif_carrier_on(netdev); 6648 rtl_start_rx(tp); 6649 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 6650 _rtl8152_set_rx_mode(netdev); 6651 napi_enable(napi); 6652 netif_wake_queue(netdev); 6653 netif_info(tp, link, netdev, "carrier on\n"); 6654 } else if (netif_queue_stopped(netdev) && 6655 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { 6656 netif_wake_queue(netdev); 6657 } 6658 } else { 6659 if (netif_carrier_ok(netdev)) { 6660 netif_carrier_off(netdev); 6661 tasklet_disable(&tp->tx_tl); 6662 napi_disable(napi); 6663 tp->rtl_ops.disable(tp); 6664 napi_enable(napi); 6665 tasklet_enable(&tp->tx_tl); 6666 netif_info(tp, link, netdev, "carrier off\n"); 6667 } 6668 } 6669 } 6670 6671 static void rtl_work_func_t(struct work_struct *work) 6672 { 6673 struct r8152 *tp = container_of(work, struct r8152, schedule.work); 6674 6675 /* If the device is unplugged or !netif_running(), the workqueue 6676 * doesn't need to wake the device, and could return directly. 6677 */ 6678 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev)) 6679 return; 6680 6681 if (usb_autopm_get_interface(tp->intf) < 0) 6682 return; 6683 6684 if (!test_bit(WORK_ENABLE, &tp->flags)) 6685 goto out1; 6686 6687 if (!mutex_trylock(&tp->control)) { 6688 schedule_delayed_work(&tp->schedule, 0); 6689 goto out1; 6690 } 6691 6692 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) 6693 set_carrier(tp); 6694 6695 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) 6696 _rtl8152_set_rx_mode(tp->netdev); 6697 6698 /* don't schedule tasket before linking */ 6699 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) && 6700 netif_carrier_ok(tp->netdev)) 6701 tasklet_schedule(&tp->tx_tl); 6702 6703 if (test_and_clear_bit(RX_EPROTO, &tp->flags) && 6704 !list_empty(&tp->rx_done)) 6705 napi_schedule(&tp->napi); 6706 6707 mutex_unlock(&tp->control); 6708 6709 out1: 6710 usb_autopm_put_interface(tp->intf); 6711 } 6712 6713 static void rtl_hw_phy_work_func_t(struct work_struct *work) 6714 { 6715 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); 6716 6717 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6718 return; 6719 6720 if (usb_autopm_get_interface(tp->intf) < 0) 6721 return; 6722 6723 mutex_lock(&tp->control); 6724 6725 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) { 6726 tp->rtl_fw.retry = false; 6727 tp->rtl_fw.fw = NULL; 6728 6729 /* Delay execution in case request_firmware() is not ready yet. 6730 */ 6731 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10); 6732 goto ignore_once; 6733 } 6734 6735 tp->rtl_ops.hw_phy_cfg(tp); 6736 6737 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex, 6738 tp->advertising); 6739 6740 ignore_once: 6741 mutex_unlock(&tp->control); 6742 6743 usb_autopm_put_interface(tp->intf); 6744 } 6745 6746 #ifdef CONFIG_PM_SLEEP 6747 static int rtl_notifier(struct notifier_block *nb, unsigned long action, 6748 void *data) 6749 { 6750 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); 6751 6752 switch (action) { 6753 case PM_HIBERNATION_PREPARE: 6754 case PM_SUSPEND_PREPARE: 6755 usb_autopm_get_interface(tp->intf); 6756 break; 6757 6758 case PM_POST_HIBERNATION: 6759 case PM_POST_SUSPEND: 6760 usb_autopm_put_interface(tp->intf); 6761 break; 6762 6763 case PM_POST_RESTORE: 6764 case PM_RESTORE_PREPARE: 6765 default: 6766 break; 6767 } 6768 6769 return NOTIFY_DONE; 6770 } 6771 #endif 6772 6773 static int rtl8152_open(struct net_device *netdev) 6774 { 6775 struct r8152 *tp = netdev_priv(netdev); 6776 int res = 0; 6777 6778 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) { 6779 cancel_delayed_work_sync(&tp->hw_phy_work); 6780 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work); 6781 } 6782 6783 res = alloc_all_mem(tp); 6784 if (res) 6785 goto out; 6786 6787 res = usb_autopm_get_interface(tp->intf); 6788 if (res < 0) 6789 goto out_free; 6790 6791 mutex_lock(&tp->control); 6792 6793 tp->rtl_ops.up(tp); 6794 6795 netif_carrier_off(netdev); 6796 netif_start_queue(netdev); 6797 set_bit(WORK_ENABLE, &tp->flags); 6798 6799 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); 6800 if (res) { 6801 if (res == -ENODEV) 6802 netif_device_detach(tp->netdev); 6803 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", 6804 res); 6805 goto out_unlock; 6806 } 6807 napi_enable(&tp->napi); 6808 tasklet_enable(&tp->tx_tl); 6809 6810 mutex_unlock(&tp->control); 6811 6812 usb_autopm_put_interface(tp->intf); 6813 #ifdef CONFIG_PM_SLEEP 6814 tp->pm_notifier.notifier_call = rtl_notifier; 6815 register_pm_notifier(&tp->pm_notifier); 6816 #endif 6817 return 0; 6818 6819 out_unlock: 6820 mutex_unlock(&tp->control); 6821 usb_autopm_put_interface(tp->intf); 6822 out_free: 6823 free_all_mem(tp); 6824 out: 6825 return res; 6826 } 6827 6828 static int rtl8152_close(struct net_device *netdev) 6829 { 6830 struct r8152 *tp = netdev_priv(netdev); 6831 int res = 0; 6832 6833 #ifdef CONFIG_PM_SLEEP 6834 unregister_pm_notifier(&tp->pm_notifier); 6835 #endif 6836 tasklet_disable(&tp->tx_tl); 6837 clear_bit(WORK_ENABLE, &tp->flags); 6838 usb_kill_urb(tp->intr_urb); 6839 cancel_delayed_work_sync(&tp->schedule); 6840 napi_disable(&tp->napi); 6841 netif_stop_queue(netdev); 6842 6843 res = usb_autopm_get_interface(tp->intf); 6844 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) { 6845 rtl_drop_queued_tx(tp); 6846 rtl_stop_rx(tp); 6847 } else { 6848 mutex_lock(&tp->control); 6849 6850 tp->rtl_ops.down(tp); 6851 6852 mutex_unlock(&tp->control); 6853 } 6854 6855 if (!res) 6856 usb_autopm_put_interface(tp->intf); 6857 6858 free_all_mem(tp); 6859 6860 return res; 6861 } 6862 6863 static void rtl_tally_reset(struct r8152 *tp) 6864 { 6865 u32 ocp_data; 6866 6867 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); 6868 ocp_data |= TALLY_RESET; 6869 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); 6870 } 6871 6872 static void r8152b_init(struct r8152 *tp) 6873 { 6874 u32 ocp_data; 6875 u16 data; 6876 6877 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6878 return; 6879 6880 data = r8152_mdio_read(tp, MII_BMCR); 6881 if (data & BMCR_PDOWN) { 6882 data &= ~BMCR_PDOWN; 6883 r8152_mdio_write(tp, MII_BMCR, data); 6884 } 6885 6886 r8152_aldps_en(tp, false); 6887 6888 if (tp->version == RTL_VER_01) { 6889 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); 6890 ocp_data &= ~LED_MODE_MASK; 6891 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); 6892 } 6893 6894 r8152_power_cut_en(tp, false); 6895 6896 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 6897 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; 6898 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 6899 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); 6900 ocp_data &= ~MCU_CLK_RATIO_MASK; 6901 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; 6902 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); 6903 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | 6904 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; 6905 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); 6906 6907 rtl_tally_reset(tp); 6908 6909 /* enable rx aggregation */ 6910 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 6911 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 6912 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 6913 } 6914 6915 static void r8153_init(struct r8152 *tp) 6916 { 6917 u32 ocp_data; 6918 u16 data; 6919 int i; 6920 6921 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6922 return; 6923 6924 r8153_u1u2en(tp, false); 6925 6926 for (i = 0; i < 500; i++) { 6927 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 6928 AUTOLOAD_DONE) 6929 break; 6930 6931 msleep(20); 6932 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 6933 break; 6934 } 6935 6936 data = r8153_phy_status(tp, 0); 6937 6938 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || 6939 tp->version == RTL_VER_05) 6940 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); 6941 6942 data = r8152_mdio_read(tp, MII_BMCR); 6943 if (data & BMCR_PDOWN) { 6944 data &= ~BMCR_PDOWN; 6945 r8152_mdio_write(tp, MII_BMCR, data); 6946 } 6947 6948 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 6949 6950 r8153_u2p3en(tp, false); 6951 6952 if (tp->version == RTL_VER_04) { 6953 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); 6954 ocp_data &= ~pwd_dn_scale_mask; 6955 ocp_data |= pwd_dn_scale(96); 6956 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); 6957 6958 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 6959 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 6960 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 6961 } else if (tp->version == RTL_VER_05) { 6962 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); 6963 ocp_data &= ~ECM_ALDPS; 6964 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); 6965 6966 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); 6967 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 6968 ocp_data &= ~DYNAMIC_BURST; 6969 else 6970 ocp_data |= DYNAMIC_BURST; 6971 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); 6972 } else if (tp->version == RTL_VER_06) { 6973 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); 6974 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 6975 ocp_data &= ~DYNAMIC_BURST; 6976 else 6977 ocp_data |= DYNAMIC_BURST; 6978 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); 6979 6980 r8153_queue_wake(tp, false); 6981 6982 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 6983 if (rtl8152_get_speed(tp) & LINK_STATUS) 6984 ocp_data |= CUR_LINK_OK; 6985 else 6986 ocp_data &= ~CUR_LINK_OK; 6987 ocp_data |= POLL_LINK_CHG; 6988 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 6989 } 6990 6991 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); 6992 ocp_data |= EP4_FULL_FC; 6993 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); 6994 6995 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); 6996 ocp_data &= ~TIMER11_EN; 6997 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); 6998 6999 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); 7000 ocp_data &= ~LED_MODE_MASK; 7001 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); 7002 7003 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; 7004 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) 7005 ocp_data |= LPM_TIMER_500MS; 7006 else 7007 ocp_data |= LPM_TIMER_500US; 7008 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); 7009 7010 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); 7011 ocp_data &= ~SEN_VAL_MASK; 7012 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; 7013 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); 7014 7015 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); 7016 7017 r8153_power_cut_en(tp, false); 7018 rtl_runtime_suspend_enable(tp, false); 7019 r8153_mac_clk_speed_down(tp, false); 7020 r8153_u1u2en(tp, true); 7021 usb_enable_lpm(tp->udev); 7022 7023 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 7024 ocp_data |= LANWAKE_CLR_EN; 7025 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 7026 7027 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG); 7028 ocp_data &= ~LANWAKE_PIN; 7029 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data); 7030 7031 /* rx aggregation */ 7032 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 7033 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 7034 if (tp->dell_tb_rx_agg_bug) 7035 ocp_data |= RX_AGG_DISABLE; 7036 7037 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 7038 7039 rtl_tally_reset(tp); 7040 7041 switch (tp->udev->speed) { 7042 case USB_SPEED_SUPER: 7043 case USB_SPEED_SUPER_PLUS: 7044 tp->coalesce = COALESCE_SUPER; 7045 break; 7046 case USB_SPEED_HIGH: 7047 tp->coalesce = COALESCE_HIGH; 7048 break; 7049 default: 7050 tp->coalesce = COALESCE_SLOW; 7051 break; 7052 } 7053 } 7054 7055 static void r8153b_init(struct r8152 *tp) 7056 { 7057 u32 ocp_data; 7058 u16 data; 7059 int i; 7060 7061 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 7062 return; 7063 7064 r8153b_u1u2en(tp, false); 7065 7066 for (i = 0; i < 500; i++) { 7067 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7068 AUTOLOAD_DONE) 7069 break; 7070 7071 msleep(20); 7072 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 7073 break; 7074 } 7075 7076 data = r8153_phy_status(tp, 0); 7077 7078 data = r8152_mdio_read(tp, MII_BMCR); 7079 if (data & BMCR_PDOWN) { 7080 data &= ~BMCR_PDOWN; 7081 r8152_mdio_write(tp, MII_BMCR, data); 7082 } 7083 7084 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7085 7086 r8153_u2p3en(tp, false); 7087 7088 /* MSC timer = 0xfff * 8ms = 32760 ms */ 7089 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 7090 7091 r8153b_power_cut_en(tp, false); 7092 r8153b_ups_en(tp, false); 7093 r8153_queue_wake(tp, false); 7094 rtl_runtime_suspend_enable(tp, false); 7095 7096 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 7097 if (rtl8152_get_speed(tp) & LINK_STATUS) 7098 ocp_data |= CUR_LINK_OK; 7099 else 7100 ocp_data &= ~CUR_LINK_OK; 7101 ocp_data |= POLL_LINK_CHG; 7102 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 7103 7104 if (tp->udev->speed >= USB_SPEED_SUPER) 7105 r8153b_u1u2en(tp, true); 7106 7107 usb_enable_lpm(tp->udev); 7108 7109 /* MAC clock speed down */ 7110 r8153_mac_clk_speed_down(tp, true); 7111 7112 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 7113 ocp_data &= ~PLA_MCU_SPDWN_EN; 7114 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 7115 7116 if (tp->version == RTL_VER_09) { 7117 /* Disable Test IO for 32QFN */ 7118 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) { 7119 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 7120 ocp_data |= TEST_IO_OFF; 7121 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 7122 } 7123 } 7124 7125 set_bit(GREEN_ETHERNET, &tp->flags); 7126 7127 /* rx aggregation */ 7128 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 7129 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 7130 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 7131 7132 rtl_tally_reset(tp); 7133 7134 tp->coalesce = 15000; /* 15 us */ 7135 } 7136 7137 static void r8153c_init(struct r8152 *tp) 7138 { 7139 u32 ocp_data; 7140 u16 data; 7141 int i; 7142 7143 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 7144 return; 7145 7146 r8153b_u1u2en(tp, false); 7147 7148 /* Disable spi_en */ 7149 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 7150 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 7151 ocp_data &= ~BIT(3); 7152 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); 7153 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0); 7154 ocp_data |= BIT(1); 7155 ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data); 7156 7157 for (i = 0; i < 500; i++) { 7158 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7159 AUTOLOAD_DONE) 7160 break; 7161 7162 msleep(20); 7163 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 7164 return; 7165 } 7166 7167 data = r8153_phy_status(tp, 0); 7168 7169 data = r8152_mdio_read(tp, MII_BMCR); 7170 if (data & BMCR_PDOWN) { 7171 data &= ~BMCR_PDOWN; 7172 r8152_mdio_write(tp, MII_BMCR, data); 7173 } 7174 7175 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7176 7177 r8153_u2p3en(tp, false); 7178 7179 /* MSC timer = 0xfff * 8ms = 32760 ms */ 7180 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 7181 7182 r8153b_power_cut_en(tp, false); 7183 r8153c_ups_en(tp, false); 7184 r8153_queue_wake(tp, false); 7185 rtl_runtime_suspend_enable(tp, false); 7186 7187 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 7188 if (rtl8152_get_speed(tp) & LINK_STATUS) 7189 ocp_data |= CUR_LINK_OK; 7190 else 7191 ocp_data &= ~CUR_LINK_OK; 7192 7193 ocp_data |= POLL_LINK_CHG; 7194 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 7195 7196 r8153b_u1u2en(tp, true); 7197 7198 usb_enable_lpm(tp->udev); 7199 7200 /* MAC clock speed down */ 7201 r8153_mac_clk_speed_down(tp, true); 7202 7203 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 7204 ocp_data &= ~BIT(7); 7205 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 7206 7207 set_bit(GREEN_ETHERNET, &tp->flags); 7208 7209 /* rx aggregation */ 7210 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 7211 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 7212 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 7213 7214 rtl_tally_reset(tp); 7215 7216 tp->coalesce = 15000; /* 15 us */ 7217 } 7218 7219 static void r8156_hw_phy_cfg(struct r8152 *tp) 7220 { 7221 u32 ocp_data; 7222 u16 data; 7223 7224 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 7225 if (ocp_data & PCUT_STATUS) { 7226 ocp_data &= ~PCUT_STATUS; 7227 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 7228 } 7229 7230 data = r8153_phy_status(tp, 0); 7231 switch (data) { 7232 case PHY_STAT_EXT_INIT: 7233 rtl8152_apply_firmware(tp, true); 7234 7235 data = ocp_reg_read(tp, 0xa468); 7236 data &= ~(BIT(3) | BIT(1)); 7237 ocp_reg_write(tp, 0xa468, data); 7238 break; 7239 case PHY_STAT_LAN_ON: 7240 case PHY_STAT_PWRDN: 7241 default: 7242 rtl8152_apply_firmware(tp, false); 7243 break; 7244 } 7245 7246 /* disable ALDPS before updating the PHY parameters */ 7247 r8153_aldps_en(tp, false); 7248 7249 /* disable EEE before updating the PHY parameters */ 7250 rtl_eee_enable(tp, false); 7251 7252 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7253 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 7254 7255 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 7256 ocp_data |= PFM_PWM_SWITCH; 7257 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 7258 7259 switch (tp->version) { 7260 case RTL_VER_10: 7261 data = ocp_reg_read(tp, 0xad40); 7262 data &= ~0x3ff; 7263 data |= BIT(7) | BIT(2); 7264 ocp_reg_write(tp, 0xad40, data); 7265 7266 data = ocp_reg_read(tp, 0xad4e); 7267 data |= BIT(4); 7268 ocp_reg_write(tp, 0xad4e, data); 7269 data = ocp_reg_read(tp, 0xad16); 7270 data &= ~0x3ff; 7271 data |= 0x6; 7272 ocp_reg_write(tp, 0xad16, data); 7273 data = ocp_reg_read(tp, 0xad32); 7274 data &= ~0x3f; 7275 data |= 6; 7276 ocp_reg_write(tp, 0xad32, data); 7277 data = ocp_reg_read(tp, 0xac08); 7278 data &= ~(BIT(12) | BIT(8)); 7279 ocp_reg_write(tp, 0xac08, data); 7280 data = ocp_reg_read(tp, 0xac8a); 7281 data |= BIT(12) | BIT(13) | BIT(14); 7282 data &= ~BIT(15); 7283 ocp_reg_write(tp, 0xac8a, data); 7284 data = ocp_reg_read(tp, 0xad18); 7285 data |= BIT(10); 7286 ocp_reg_write(tp, 0xad18, data); 7287 data = ocp_reg_read(tp, 0xad1a); 7288 data |= 0x3ff; 7289 ocp_reg_write(tp, 0xad1a, data); 7290 data = ocp_reg_read(tp, 0xad1c); 7291 data |= 0x3ff; 7292 ocp_reg_write(tp, 0xad1c, data); 7293 7294 data = sram_read(tp, 0x80ea); 7295 data &= ~0xff00; 7296 data |= 0xc400; 7297 sram_write(tp, 0x80ea, data); 7298 data = sram_read(tp, 0x80eb); 7299 data &= ~0x0700; 7300 data |= 0x0300; 7301 sram_write(tp, 0x80eb, data); 7302 data = sram_read(tp, 0x80f8); 7303 data &= ~0xff00; 7304 data |= 0x1c00; 7305 sram_write(tp, 0x80f8, data); 7306 data = sram_read(tp, 0x80f1); 7307 data &= ~0xff00; 7308 data |= 0x3000; 7309 sram_write(tp, 0x80f1, data); 7310 7311 data = sram_read(tp, 0x80fe); 7312 data &= ~0xff00; 7313 data |= 0xa500; 7314 sram_write(tp, 0x80fe, data); 7315 data = sram_read(tp, 0x8102); 7316 data &= ~0xff00; 7317 data |= 0x5000; 7318 sram_write(tp, 0x8102, data); 7319 data = sram_read(tp, 0x8015); 7320 data &= ~0xff00; 7321 data |= 0x3300; 7322 sram_write(tp, 0x8015, data); 7323 data = sram_read(tp, 0x8100); 7324 data &= ~0xff00; 7325 data |= 0x7000; 7326 sram_write(tp, 0x8100, data); 7327 data = sram_read(tp, 0x8014); 7328 data &= ~0xff00; 7329 data |= 0xf000; 7330 sram_write(tp, 0x8014, data); 7331 data = sram_read(tp, 0x8016); 7332 data &= ~0xff00; 7333 data |= 0x6500; 7334 sram_write(tp, 0x8016, data); 7335 data = sram_read(tp, 0x80dc); 7336 data &= ~0xff00; 7337 data |= 0xed00; 7338 sram_write(tp, 0x80dc, data); 7339 data = sram_read(tp, 0x80df); 7340 data |= BIT(8); 7341 sram_write(tp, 0x80df, data); 7342 data = sram_read(tp, 0x80e1); 7343 data &= ~BIT(8); 7344 sram_write(tp, 0x80e1, data); 7345 7346 data = ocp_reg_read(tp, 0xbf06); 7347 data &= ~0x003f; 7348 data |= 0x0038; 7349 ocp_reg_write(tp, 0xbf06, data); 7350 7351 sram_write(tp, 0x819f, 0xddb6); 7352 7353 ocp_reg_write(tp, 0xbc34, 0x5555); 7354 data = ocp_reg_read(tp, 0xbf0a); 7355 data &= ~0x0e00; 7356 data |= 0x0a00; 7357 ocp_reg_write(tp, 0xbf0a, data); 7358 7359 data = ocp_reg_read(tp, 0xbd2c); 7360 data &= ~BIT(13); 7361 ocp_reg_write(tp, 0xbd2c, data); 7362 break; 7363 case RTL_VER_11: 7364 data = ocp_reg_read(tp, 0xad16); 7365 data |= 0x3ff; 7366 ocp_reg_write(tp, 0xad16, data); 7367 data = ocp_reg_read(tp, 0xad32); 7368 data &= ~0x3f; 7369 data |= 6; 7370 ocp_reg_write(tp, 0xad32, data); 7371 data = ocp_reg_read(tp, 0xac08); 7372 data &= ~(BIT(12) | BIT(8)); 7373 ocp_reg_write(tp, 0xac08, data); 7374 data = ocp_reg_read(tp, 0xacc0); 7375 data &= ~0x3; 7376 data |= BIT(1); 7377 ocp_reg_write(tp, 0xacc0, data); 7378 data = ocp_reg_read(tp, 0xad40); 7379 data &= ~0xe7; 7380 data |= BIT(6) | BIT(2); 7381 ocp_reg_write(tp, 0xad40, data); 7382 data = ocp_reg_read(tp, 0xac14); 7383 data &= ~BIT(7); 7384 ocp_reg_write(tp, 0xac14, data); 7385 data = ocp_reg_read(tp, 0xac80); 7386 data &= ~(BIT(8) | BIT(9)); 7387 ocp_reg_write(tp, 0xac80, data); 7388 data = ocp_reg_read(tp, 0xac5e); 7389 data &= ~0x7; 7390 data |= BIT(1); 7391 ocp_reg_write(tp, 0xac5e, data); 7392 ocp_reg_write(tp, 0xad4c, 0x00a8); 7393 ocp_reg_write(tp, 0xac5c, 0x01ff); 7394 data = ocp_reg_read(tp, 0xac8a); 7395 data &= ~0xf0; 7396 data |= BIT(4) | BIT(5); 7397 ocp_reg_write(tp, 0xac8a, data); 7398 ocp_reg_write(tp, 0xb87c, 0x8157); 7399 data = ocp_reg_read(tp, 0xb87e); 7400 data &= ~0xff00; 7401 data |= 0x0500; 7402 ocp_reg_write(tp, 0xb87e, data); 7403 ocp_reg_write(tp, 0xb87c, 0x8159); 7404 data = ocp_reg_read(tp, 0xb87e); 7405 data &= ~0xff00; 7406 data |= 0x0700; 7407 ocp_reg_write(tp, 0xb87e, data); 7408 7409 /* AAGC */ 7410 ocp_reg_write(tp, 0xb87c, 0x80a2); 7411 ocp_reg_write(tp, 0xb87e, 0x0153); 7412 ocp_reg_write(tp, 0xb87c, 0x809c); 7413 ocp_reg_write(tp, 0xb87e, 0x0153); 7414 7415 /* EEE parameter */ 7416 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056); 7417 7418 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG); 7419 ocp_data |= EN_XG_LIP | EN_G_LIP; 7420 ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data); 7421 7422 sram_write(tp, 0x8257, 0x020f); /* XG PLL */ 7423 sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */ 7424 7425 if (rtl_phy_patch_request(tp, true, true)) 7426 return; 7427 7428 /* Advance EEE */ 7429 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 7430 ocp_data |= EEE_SPDWN_EN; 7431 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 7432 7433 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 7434 data &= ~(EN_EEE_100 | EN_EEE_1000); 7435 data |= EN_10M_CLKDIV; 7436 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 7437 tp->ups_info._10m_ckdiv = true; 7438 tp->ups_info.eee_plloff_100 = false; 7439 tp->ups_info.eee_plloff_giga = false; 7440 7441 data = ocp_reg_read(tp, OCP_POWER_CFG); 7442 data &= ~EEE_CLKDIV_EN; 7443 ocp_reg_write(tp, OCP_POWER_CFG, data); 7444 tp->ups_info.eee_ckdiv = false; 7445 7446 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); 7447 ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5)); 7448 tp->ups_info._250m_ckdiv = false; 7449 7450 rtl_phy_patch_request(tp, false, true); 7451 7452 /* enable ADC Ibias Cal */ 7453 data = ocp_reg_read(tp, 0xd068); 7454 data |= BIT(13); 7455 ocp_reg_write(tp, 0xd068, data); 7456 7457 /* enable Thermal Sensor */ 7458 data = sram_read(tp, 0x81a2); 7459 data &= ~BIT(8); 7460 sram_write(tp, 0x81a2, data); 7461 data = ocp_reg_read(tp, 0xb54c); 7462 data &= ~0xff00; 7463 data |= 0xdb00; 7464 ocp_reg_write(tp, 0xb54c, data); 7465 7466 /* Nway 2.5G Lite */ 7467 data = ocp_reg_read(tp, 0xa454); 7468 data &= ~BIT(0); 7469 ocp_reg_write(tp, 0xa454, data); 7470 7471 /* CS DSP solution */ 7472 data = ocp_reg_read(tp, OCP_10GBT_CTRL); 7473 data |= RTL_ADV2_5G_F_R; 7474 ocp_reg_write(tp, OCP_10GBT_CTRL, data); 7475 data = ocp_reg_read(tp, 0xad4e); 7476 data &= ~BIT(4); 7477 ocp_reg_write(tp, 0xad4e, data); 7478 data = ocp_reg_read(tp, 0xa86a); 7479 data &= ~BIT(0); 7480 ocp_reg_write(tp, 0xa86a, data); 7481 7482 /* MDI SWAP */ 7483 if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) && 7484 (ocp_reg_read(tp, 0xd068) & BIT(1))) { 7485 u16 swap_a, swap_b; 7486 7487 data = ocp_reg_read(tp, 0xd068); 7488 data &= ~0x1f; 7489 data |= 0x1; /* p0 */ 7490 ocp_reg_write(tp, 0xd068, data); 7491 swap_a = ocp_reg_read(tp, 0xd06a); 7492 data &= ~0x18; 7493 data |= 0x18; /* p3 */ 7494 ocp_reg_write(tp, 0xd068, data); 7495 swap_b = ocp_reg_read(tp, 0xd06a); 7496 data &= ~0x18; /* p0 */ 7497 ocp_reg_write(tp, 0xd068, data); 7498 ocp_reg_write(tp, 0xd06a, 7499 (swap_a & ~0x7ff) | (swap_b & 0x7ff)); 7500 data |= 0x18; /* p3 */ 7501 ocp_reg_write(tp, 0xd068, data); 7502 ocp_reg_write(tp, 0xd06a, 7503 (swap_b & ~0x7ff) | (swap_a & 0x7ff)); 7504 data &= ~0x18; 7505 data |= 0x08; /* p1 */ 7506 ocp_reg_write(tp, 0xd068, data); 7507 swap_a = ocp_reg_read(tp, 0xd06a); 7508 data &= ~0x18; 7509 data |= 0x10; /* p2 */ 7510 ocp_reg_write(tp, 0xd068, data); 7511 swap_b = ocp_reg_read(tp, 0xd06a); 7512 data &= ~0x18; 7513 data |= 0x08; /* p1 */ 7514 ocp_reg_write(tp, 0xd068, data); 7515 ocp_reg_write(tp, 0xd06a, 7516 (swap_a & ~0x7ff) | (swap_b & 0x7ff)); 7517 data &= ~0x18; 7518 data |= 0x10; /* p2 */ 7519 ocp_reg_write(tp, 0xd068, data); 7520 ocp_reg_write(tp, 0xd06a, 7521 (swap_b & ~0x7ff) | (swap_a & 0x7ff)); 7522 swap_a = ocp_reg_read(tp, 0xbd5a); 7523 swap_b = ocp_reg_read(tp, 0xbd5c); 7524 ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) | 7525 ((swap_b & 0x1f) << 8) | 7526 ((swap_b >> 8) & 0x1f)); 7527 ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) | 7528 ((swap_a & 0x1f) << 8) | 7529 ((swap_a >> 8) & 0x1f)); 7530 swap_a = ocp_reg_read(tp, 0xbc18); 7531 swap_b = ocp_reg_read(tp, 0xbc1a); 7532 ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) | 7533 ((swap_b & 0x1f) << 8) | 7534 ((swap_b >> 8) & 0x1f)); 7535 ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) | 7536 ((swap_a & 0x1f) << 8) | 7537 ((swap_a >> 8) & 0x1f)); 7538 } 7539 break; 7540 default: 7541 break; 7542 } 7543 7544 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 7545 7546 data = ocp_reg_read(tp, 0xa428); 7547 data &= ~BIT(9); 7548 ocp_reg_write(tp, 0xa428, data); 7549 data = ocp_reg_read(tp, 0xa5ea); 7550 data &= ~BIT(0); 7551 ocp_reg_write(tp, 0xa5ea, data); 7552 tp->ups_info.lite_mode = 0; 7553 7554 if (tp->eee_en) 7555 rtl_eee_enable(tp, true); 7556 7557 r8153_aldps_en(tp, true); 7558 r8152b_enable_fc(tp); 7559 r8153_u2p3en(tp, true); 7560 7561 set_bit(PHY_RESET, &tp->flags); 7562 } 7563 7564 static void r8156b_hw_phy_cfg(struct r8152 *tp) 7565 { 7566 u32 ocp_data; 7567 u16 data; 7568 7569 switch (tp->version) { 7570 case RTL_VER_12: 7571 ocp_reg_write(tp, 0xbf86, 0x9000); 7572 data = ocp_reg_read(tp, 0xc402); 7573 data |= BIT(10); 7574 ocp_reg_write(tp, 0xc402, data); 7575 data &= ~BIT(10); 7576 ocp_reg_write(tp, 0xc402, data); 7577 ocp_reg_write(tp, 0xbd86, 0x1010); 7578 ocp_reg_write(tp, 0xbd88, 0x1010); 7579 data = ocp_reg_read(tp, 0xbd4e); 7580 data &= ~(BIT(10) | BIT(11)); 7581 data |= BIT(11); 7582 ocp_reg_write(tp, 0xbd4e, data); 7583 data = ocp_reg_read(tp, 0xbf46); 7584 data &= ~0xf00; 7585 data |= 0x700; 7586 ocp_reg_write(tp, 0xbf46, data); 7587 break; 7588 case RTL_VER_13: 7589 case RTL_VER_15: 7590 r8156b_wait_loading_flash(tp); 7591 break; 7592 default: 7593 break; 7594 } 7595 7596 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 7597 if (ocp_data & PCUT_STATUS) { 7598 ocp_data &= ~PCUT_STATUS; 7599 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 7600 } 7601 7602 data = r8153_phy_status(tp, 0); 7603 switch (data) { 7604 case PHY_STAT_EXT_INIT: 7605 rtl8152_apply_firmware(tp, true); 7606 7607 data = ocp_reg_read(tp, 0xa466); 7608 data &= ~BIT(0); 7609 ocp_reg_write(tp, 0xa466, data); 7610 7611 data = ocp_reg_read(tp, 0xa468); 7612 data &= ~(BIT(3) | BIT(1)); 7613 ocp_reg_write(tp, 0xa468, data); 7614 break; 7615 case PHY_STAT_LAN_ON: 7616 case PHY_STAT_PWRDN: 7617 default: 7618 rtl8152_apply_firmware(tp, false); 7619 break; 7620 } 7621 7622 data = r8152_mdio_read(tp, MII_BMCR); 7623 if (data & BMCR_PDOWN) { 7624 data &= ~BMCR_PDOWN; 7625 r8152_mdio_write(tp, MII_BMCR, data); 7626 } 7627 7628 /* disable ALDPS before updating the PHY parameters */ 7629 r8153_aldps_en(tp, false); 7630 7631 /* disable EEE before updating the PHY parameters */ 7632 rtl_eee_enable(tp, false); 7633 7634 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7635 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 7636 7637 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 7638 ocp_data |= PFM_PWM_SWITCH; 7639 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 7640 7641 switch (tp->version) { 7642 case RTL_VER_12: 7643 data = ocp_reg_read(tp, 0xbc08); 7644 data |= BIT(3) | BIT(2); 7645 ocp_reg_write(tp, 0xbc08, data); 7646 7647 data = sram_read(tp, 0x8fff); 7648 data &= ~0xff00; 7649 data |= 0x0400; 7650 sram_write(tp, 0x8fff, data); 7651 7652 data = ocp_reg_read(tp, 0xacda); 7653 data |= 0xff00; 7654 ocp_reg_write(tp, 0xacda, data); 7655 data = ocp_reg_read(tp, 0xacde); 7656 data |= 0xf000; 7657 ocp_reg_write(tp, 0xacde, data); 7658 ocp_reg_write(tp, 0xac8c, 0x0ffc); 7659 ocp_reg_write(tp, 0xac46, 0xb7b4); 7660 ocp_reg_write(tp, 0xac50, 0x0fbc); 7661 ocp_reg_write(tp, 0xac3c, 0x9240); 7662 ocp_reg_write(tp, 0xac4e, 0x0db4); 7663 ocp_reg_write(tp, 0xacc6, 0x0707); 7664 ocp_reg_write(tp, 0xacc8, 0xa0d3); 7665 ocp_reg_write(tp, 0xad08, 0x0007); 7666 7667 ocp_reg_write(tp, 0xb87c, 0x8560); 7668 ocp_reg_write(tp, 0xb87e, 0x19cc); 7669 ocp_reg_write(tp, 0xb87c, 0x8562); 7670 ocp_reg_write(tp, 0xb87e, 0x19cc); 7671 ocp_reg_write(tp, 0xb87c, 0x8564); 7672 ocp_reg_write(tp, 0xb87e, 0x19cc); 7673 ocp_reg_write(tp, 0xb87c, 0x8566); 7674 ocp_reg_write(tp, 0xb87e, 0x147d); 7675 ocp_reg_write(tp, 0xb87c, 0x8568); 7676 ocp_reg_write(tp, 0xb87e, 0x147d); 7677 ocp_reg_write(tp, 0xb87c, 0x856a); 7678 ocp_reg_write(tp, 0xb87e, 0x147d); 7679 ocp_reg_write(tp, 0xb87c, 0x8ffe); 7680 ocp_reg_write(tp, 0xb87e, 0x0907); 7681 ocp_reg_write(tp, 0xb87c, 0x80d6); 7682 ocp_reg_write(tp, 0xb87e, 0x2801); 7683 ocp_reg_write(tp, 0xb87c, 0x80f2); 7684 ocp_reg_write(tp, 0xb87e, 0x2801); 7685 ocp_reg_write(tp, 0xb87c, 0x80f4); 7686 ocp_reg_write(tp, 0xb87e, 0x6077); 7687 ocp_reg_write(tp, 0xb506, 0x01e7); 7688 7689 ocp_reg_write(tp, 0xb87c, 0x8013); 7690 ocp_reg_write(tp, 0xb87e, 0x0700); 7691 ocp_reg_write(tp, 0xb87c, 0x8fb9); 7692 ocp_reg_write(tp, 0xb87e, 0x2801); 7693 ocp_reg_write(tp, 0xb87c, 0x8fba); 7694 ocp_reg_write(tp, 0xb87e, 0x0100); 7695 ocp_reg_write(tp, 0xb87c, 0x8fbc); 7696 ocp_reg_write(tp, 0xb87e, 0x1900); 7697 ocp_reg_write(tp, 0xb87c, 0x8fbe); 7698 ocp_reg_write(tp, 0xb87e, 0xe100); 7699 ocp_reg_write(tp, 0xb87c, 0x8fc0); 7700 ocp_reg_write(tp, 0xb87e, 0x0800); 7701 ocp_reg_write(tp, 0xb87c, 0x8fc2); 7702 ocp_reg_write(tp, 0xb87e, 0xe500); 7703 ocp_reg_write(tp, 0xb87c, 0x8fc4); 7704 ocp_reg_write(tp, 0xb87e, 0x0f00); 7705 ocp_reg_write(tp, 0xb87c, 0x8fc6); 7706 ocp_reg_write(tp, 0xb87e, 0xf100); 7707 ocp_reg_write(tp, 0xb87c, 0x8fc8); 7708 ocp_reg_write(tp, 0xb87e, 0x0400); 7709 ocp_reg_write(tp, 0xb87c, 0x8fca); 7710 ocp_reg_write(tp, 0xb87e, 0xf300); 7711 ocp_reg_write(tp, 0xb87c, 0x8fcc); 7712 ocp_reg_write(tp, 0xb87e, 0xfd00); 7713 ocp_reg_write(tp, 0xb87c, 0x8fce); 7714 ocp_reg_write(tp, 0xb87e, 0xff00); 7715 ocp_reg_write(tp, 0xb87c, 0x8fd0); 7716 ocp_reg_write(tp, 0xb87e, 0xfb00); 7717 ocp_reg_write(tp, 0xb87c, 0x8fd2); 7718 ocp_reg_write(tp, 0xb87e, 0x0100); 7719 ocp_reg_write(tp, 0xb87c, 0x8fd4); 7720 ocp_reg_write(tp, 0xb87e, 0xf400); 7721 ocp_reg_write(tp, 0xb87c, 0x8fd6); 7722 ocp_reg_write(tp, 0xb87e, 0xff00); 7723 ocp_reg_write(tp, 0xb87c, 0x8fd8); 7724 ocp_reg_write(tp, 0xb87e, 0xf600); 7725 7726 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG); 7727 ocp_data |= EN_XG_LIP | EN_G_LIP; 7728 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data); 7729 ocp_reg_write(tp, 0xb87c, 0x813d); 7730 ocp_reg_write(tp, 0xb87e, 0x390e); 7731 ocp_reg_write(tp, 0xb87c, 0x814f); 7732 ocp_reg_write(tp, 0xb87e, 0x790e); 7733 ocp_reg_write(tp, 0xb87c, 0x80b0); 7734 ocp_reg_write(tp, 0xb87e, 0x0f31); 7735 data = ocp_reg_read(tp, 0xbf4c); 7736 data |= BIT(1); 7737 ocp_reg_write(tp, 0xbf4c, data); 7738 data = ocp_reg_read(tp, 0xbcca); 7739 data |= BIT(9) | BIT(8); 7740 ocp_reg_write(tp, 0xbcca, data); 7741 ocp_reg_write(tp, 0xb87c, 0x8141); 7742 ocp_reg_write(tp, 0xb87e, 0x320e); 7743 ocp_reg_write(tp, 0xb87c, 0x8153); 7744 ocp_reg_write(tp, 0xb87e, 0x720e); 7745 ocp_reg_write(tp, 0xb87c, 0x8529); 7746 ocp_reg_write(tp, 0xb87e, 0x050e); 7747 data = ocp_reg_read(tp, OCP_EEE_CFG); 7748 data &= ~CTAP_SHORT_EN; 7749 ocp_reg_write(tp, OCP_EEE_CFG, data); 7750 7751 sram_write(tp, 0x816c, 0xc4a0); 7752 sram_write(tp, 0x8170, 0xc4a0); 7753 sram_write(tp, 0x8174, 0x04a0); 7754 sram_write(tp, 0x8178, 0x04a0); 7755 sram_write(tp, 0x817c, 0x0719); 7756 sram_write(tp, 0x8ff4, 0x0400); 7757 sram_write(tp, 0x8ff1, 0x0404); 7758 7759 ocp_reg_write(tp, 0xbf4a, 0x001b); 7760 ocp_reg_write(tp, 0xb87c, 0x8033); 7761 ocp_reg_write(tp, 0xb87e, 0x7c13); 7762 ocp_reg_write(tp, 0xb87c, 0x8037); 7763 ocp_reg_write(tp, 0xb87e, 0x7c13); 7764 ocp_reg_write(tp, 0xb87c, 0x803b); 7765 ocp_reg_write(tp, 0xb87e, 0xfc32); 7766 ocp_reg_write(tp, 0xb87c, 0x803f); 7767 ocp_reg_write(tp, 0xb87e, 0x7c13); 7768 ocp_reg_write(tp, 0xb87c, 0x8043); 7769 ocp_reg_write(tp, 0xb87e, 0x7c13); 7770 ocp_reg_write(tp, 0xb87c, 0x8047); 7771 ocp_reg_write(tp, 0xb87e, 0x7c13); 7772 7773 ocp_reg_write(tp, 0xb87c, 0x8145); 7774 ocp_reg_write(tp, 0xb87e, 0x370e); 7775 ocp_reg_write(tp, 0xb87c, 0x8157); 7776 ocp_reg_write(tp, 0xb87e, 0x770e); 7777 ocp_reg_write(tp, 0xb87c, 0x8169); 7778 ocp_reg_write(tp, 0xb87e, 0x0d0a); 7779 ocp_reg_write(tp, 0xb87c, 0x817b); 7780 ocp_reg_write(tp, 0xb87e, 0x1d0a); 7781 7782 data = sram_read(tp, 0x8217); 7783 data &= ~0xff00; 7784 data |= 0x5000; 7785 sram_write(tp, 0x8217, data); 7786 data = sram_read(tp, 0x821a); 7787 data &= ~0xff00; 7788 data |= 0x5000; 7789 sram_write(tp, 0x821a, data); 7790 sram_write(tp, 0x80da, 0x0403); 7791 data = sram_read(tp, 0x80dc); 7792 data &= ~0xff00; 7793 data |= 0x1000; 7794 sram_write(tp, 0x80dc, data); 7795 sram_write(tp, 0x80b3, 0x0384); 7796 sram_write(tp, 0x80b7, 0x2007); 7797 data = sram_read(tp, 0x80ba); 7798 data &= ~0xff00; 7799 data |= 0x6c00; 7800 sram_write(tp, 0x80ba, data); 7801 sram_write(tp, 0x80b5, 0xf009); 7802 data = sram_read(tp, 0x80bd); 7803 data &= ~0xff00; 7804 data |= 0x9f00; 7805 sram_write(tp, 0x80bd, data); 7806 sram_write(tp, 0x80c7, 0xf083); 7807 sram_write(tp, 0x80dd, 0x03f0); 7808 data = sram_read(tp, 0x80df); 7809 data &= ~0xff00; 7810 data |= 0x1000; 7811 sram_write(tp, 0x80df, data); 7812 sram_write(tp, 0x80cb, 0x2007); 7813 data = sram_read(tp, 0x80ce); 7814 data &= ~0xff00; 7815 data |= 0x6c00; 7816 sram_write(tp, 0x80ce, data); 7817 sram_write(tp, 0x80c9, 0x8009); 7818 data = sram_read(tp, 0x80d1); 7819 data &= ~0xff00; 7820 data |= 0x8000; 7821 sram_write(tp, 0x80d1, data); 7822 sram_write(tp, 0x80a3, 0x200a); 7823 sram_write(tp, 0x80a5, 0xf0ad); 7824 sram_write(tp, 0x809f, 0x6073); 7825 sram_write(tp, 0x80a1, 0x000b); 7826 data = sram_read(tp, 0x80a9); 7827 data &= ~0xff00; 7828 data |= 0xc000; 7829 sram_write(tp, 0x80a9, data); 7830 7831 if (rtl_phy_patch_request(tp, true, true)) 7832 return; 7833 7834 data = ocp_reg_read(tp, 0xb896); 7835 data &= ~BIT(0); 7836 ocp_reg_write(tp, 0xb896, data); 7837 data = ocp_reg_read(tp, 0xb892); 7838 data &= ~0xff00; 7839 ocp_reg_write(tp, 0xb892, data); 7840 ocp_reg_write(tp, 0xb88e, 0xc23e); 7841 ocp_reg_write(tp, 0xb890, 0x0000); 7842 ocp_reg_write(tp, 0xb88e, 0xc240); 7843 ocp_reg_write(tp, 0xb890, 0x0103); 7844 ocp_reg_write(tp, 0xb88e, 0xc242); 7845 ocp_reg_write(tp, 0xb890, 0x0507); 7846 ocp_reg_write(tp, 0xb88e, 0xc244); 7847 ocp_reg_write(tp, 0xb890, 0x090b); 7848 ocp_reg_write(tp, 0xb88e, 0xc246); 7849 ocp_reg_write(tp, 0xb890, 0x0c0e); 7850 ocp_reg_write(tp, 0xb88e, 0xc248); 7851 ocp_reg_write(tp, 0xb890, 0x1012); 7852 ocp_reg_write(tp, 0xb88e, 0xc24a); 7853 ocp_reg_write(tp, 0xb890, 0x1416); 7854 data = ocp_reg_read(tp, 0xb896); 7855 data |= BIT(0); 7856 ocp_reg_write(tp, 0xb896, data); 7857 7858 rtl_phy_patch_request(tp, false, true); 7859 7860 data = ocp_reg_read(tp, 0xa86a); 7861 data |= BIT(0); 7862 ocp_reg_write(tp, 0xa86a, data); 7863 data = ocp_reg_read(tp, 0xa6f0); 7864 data |= BIT(0); 7865 ocp_reg_write(tp, 0xa6f0, data); 7866 7867 ocp_reg_write(tp, 0xbfa0, 0xd70d); 7868 ocp_reg_write(tp, 0xbfa2, 0x4100); 7869 ocp_reg_write(tp, 0xbfa4, 0xe868); 7870 ocp_reg_write(tp, 0xbfa6, 0xdc59); 7871 ocp_reg_write(tp, 0xb54c, 0x3c18); 7872 data = ocp_reg_read(tp, 0xbfa4); 7873 data &= ~BIT(5); 7874 ocp_reg_write(tp, 0xbfa4, data); 7875 data = sram_read(tp, 0x817d); 7876 data |= BIT(12); 7877 sram_write(tp, 0x817d, data); 7878 break; 7879 case RTL_VER_13: 7880 /* 2.5G INRX */ 7881 data = ocp_reg_read(tp, 0xac46); 7882 data &= ~0x00f0; 7883 data |= 0x0090; 7884 ocp_reg_write(tp, 0xac46, data); 7885 data = ocp_reg_read(tp, 0xad30); 7886 data &= ~0x0003; 7887 data |= 0x0001; 7888 ocp_reg_write(tp, 0xad30, data); 7889 fallthrough; 7890 case RTL_VER_15: 7891 /* EEE parameter */ 7892 ocp_reg_write(tp, 0xb87c, 0x80f5); 7893 ocp_reg_write(tp, 0xb87e, 0x760e); 7894 ocp_reg_write(tp, 0xb87c, 0x8107); 7895 ocp_reg_write(tp, 0xb87e, 0x360e); 7896 ocp_reg_write(tp, 0xb87c, 0x8551); 7897 data = ocp_reg_read(tp, 0xb87e); 7898 data &= ~0xff00; 7899 data |= 0x0800; 7900 ocp_reg_write(tp, 0xb87e, data); 7901 7902 /* ADC_PGA parameter */ 7903 data = ocp_reg_read(tp, 0xbf00); 7904 data &= ~0xe000; 7905 data |= 0xa000; 7906 ocp_reg_write(tp, 0xbf00, data); 7907 data = ocp_reg_read(tp, 0xbf46); 7908 data &= ~0x0f00; 7909 data |= 0x0300; 7910 ocp_reg_write(tp, 0xbf46, data); 7911 7912 /* Green Table-PGA, 1G full viterbi */ 7913 sram_write(tp, 0x8044, 0x2417); 7914 sram_write(tp, 0x804a, 0x2417); 7915 sram_write(tp, 0x8050, 0x2417); 7916 sram_write(tp, 0x8056, 0x2417); 7917 sram_write(tp, 0x805c, 0x2417); 7918 sram_write(tp, 0x8062, 0x2417); 7919 sram_write(tp, 0x8068, 0x2417); 7920 sram_write(tp, 0x806e, 0x2417); 7921 sram_write(tp, 0x8074, 0x2417); 7922 sram_write(tp, 0x807a, 0x2417); 7923 7924 /* XG PLL */ 7925 data = ocp_reg_read(tp, 0xbf84); 7926 data &= ~0xe000; 7927 data |= 0xa000; 7928 ocp_reg_write(tp, 0xbf84, data); 7929 break; 7930 default: 7931 break; 7932 } 7933 7934 if (rtl_phy_patch_request(tp, true, true)) 7935 return; 7936 7937 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 7938 ocp_data |= EEE_SPDWN_EN; 7939 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 7940 7941 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 7942 data &= ~(EN_EEE_100 | EN_EEE_1000); 7943 data |= EN_10M_CLKDIV; 7944 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 7945 tp->ups_info._10m_ckdiv = true; 7946 tp->ups_info.eee_plloff_100 = false; 7947 tp->ups_info.eee_plloff_giga = false; 7948 7949 data = ocp_reg_read(tp, OCP_POWER_CFG); 7950 data &= ~EEE_CLKDIV_EN; 7951 ocp_reg_write(tp, OCP_POWER_CFG, data); 7952 tp->ups_info.eee_ckdiv = false; 7953 7954 rtl_phy_patch_request(tp, false, true); 7955 7956 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 7957 7958 data = ocp_reg_read(tp, 0xa428); 7959 data &= ~BIT(9); 7960 ocp_reg_write(tp, 0xa428, data); 7961 data = ocp_reg_read(tp, 0xa5ea); 7962 data &= ~BIT(0); 7963 ocp_reg_write(tp, 0xa5ea, data); 7964 tp->ups_info.lite_mode = 0; 7965 7966 if (tp->eee_en) 7967 rtl_eee_enable(tp, true); 7968 7969 r8153_aldps_en(tp, true); 7970 r8152b_enable_fc(tp); 7971 r8153_u2p3en(tp, true); 7972 7973 set_bit(PHY_RESET, &tp->flags); 7974 } 7975 7976 static void r8156_init(struct r8152 *tp) 7977 { 7978 u32 ocp_data; 7979 u16 data; 7980 int i; 7981 7982 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 7983 return; 7984 7985 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP); 7986 ocp_data &= ~EN_ALL_SPEED; 7987 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data); 7988 7989 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0); 7990 7991 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION); 7992 ocp_data |= BYPASS_MAC_RESET; 7993 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data); 7994 7995 r8153b_u1u2en(tp, false); 7996 7997 for (i = 0; i < 500; i++) { 7998 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7999 AUTOLOAD_DONE) 8000 break; 8001 8002 msleep(20); 8003 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 8004 return; 8005 } 8006 8007 data = r8153_phy_status(tp, 0); 8008 if (data == PHY_STAT_EXT_INIT) { 8009 data = ocp_reg_read(tp, 0xa468); 8010 data &= ~(BIT(3) | BIT(1)); 8011 ocp_reg_write(tp, 0xa468, data); 8012 } 8013 8014 data = r8152_mdio_read(tp, MII_BMCR); 8015 if (data & BMCR_PDOWN) { 8016 data &= ~BMCR_PDOWN; 8017 r8152_mdio_write(tp, MII_BMCR, data); 8018 } 8019 8020 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 8021 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 8022 8023 r8153_u2p3en(tp, false); 8024 8025 /* MSC timer = 0xfff * 8ms = 32760 ms */ 8026 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 8027 8028 /* U1/U2/L1 idle timer. 500 us */ 8029 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 8030 8031 r8153b_power_cut_en(tp, false); 8032 r8156_ups_en(tp, false); 8033 r8153_queue_wake(tp, false); 8034 rtl_runtime_suspend_enable(tp, false); 8035 8036 if (tp->udev->speed >= USB_SPEED_SUPER) 8037 r8153b_u1u2en(tp, true); 8038 8039 usb_enable_lpm(tp->udev); 8040 8041 r8156_mac_clk_spd(tp, true); 8042 8043 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 8044 ocp_data &= ~PLA_MCU_SPDWN_EN; 8045 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 8046 8047 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 8048 if (rtl8152_get_speed(tp) & LINK_STATUS) 8049 ocp_data |= CUR_LINK_OK; 8050 else 8051 ocp_data &= ~CUR_LINK_OK; 8052 ocp_data |= POLL_LINK_CHG; 8053 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 8054 8055 set_bit(GREEN_ETHERNET, &tp->flags); 8056 8057 /* rx aggregation */ 8058 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 8059 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 8060 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 8061 8062 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG); 8063 ocp_data |= ACT_ODMA; 8064 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data); 8065 8066 r8156_mdio_force_mode(tp); 8067 rtl_tally_reset(tp); 8068 8069 tp->coalesce = 15000; /* 15 us */ 8070 } 8071 8072 static void r8156b_init(struct r8152 *tp) 8073 { 8074 u32 ocp_data; 8075 u16 data; 8076 int i; 8077 8078 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 8079 return; 8080 8081 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP); 8082 ocp_data &= ~EN_ALL_SPEED; 8083 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data); 8084 8085 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0); 8086 8087 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION); 8088 ocp_data |= BYPASS_MAC_RESET; 8089 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data); 8090 8091 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); 8092 ocp_data |= RX_DETECT8; 8093 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); 8094 8095 r8153b_u1u2en(tp, false); 8096 8097 switch (tp->version) { 8098 case RTL_VER_13: 8099 case RTL_VER_15: 8100 r8156b_wait_loading_flash(tp); 8101 break; 8102 default: 8103 break; 8104 } 8105 8106 for (i = 0; i < 500; i++) { 8107 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 8108 AUTOLOAD_DONE) 8109 break; 8110 8111 msleep(20); 8112 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 8113 return; 8114 } 8115 8116 data = r8153_phy_status(tp, 0); 8117 if (data == PHY_STAT_EXT_INIT) { 8118 data = ocp_reg_read(tp, 0xa468); 8119 data &= ~(BIT(3) | BIT(1)); 8120 ocp_reg_write(tp, 0xa468, data); 8121 8122 data = ocp_reg_read(tp, 0xa466); 8123 data &= ~BIT(0); 8124 ocp_reg_write(tp, 0xa466, data); 8125 } 8126 8127 data = r8152_mdio_read(tp, MII_BMCR); 8128 if (data & BMCR_PDOWN) { 8129 data &= ~BMCR_PDOWN; 8130 r8152_mdio_write(tp, MII_BMCR, data); 8131 } 8132 8133 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 8134 8135 r8153_u2p3en(tp, false); 8136 8137 /* MSC timer = 0xfff * 8ms = 32760 ms */ 8138 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 8139 8140 /* U1/U2/L1 idle timer. 500 us */ 8141 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 8142 8143 r8153b_power_cut_en(tp, false); 8144 r8156_ups_en(tp, false); 8145 r8153_queue_wake(tp, false); 8146 rtl_runtime_suspend_enable(tp, false); 8147 8148 if (tp->udev->speed >= USB_SPEED_SUPER) 8149 r8153b_u1u2en(tp, true); 8150 8151 usb_enable_lpm(tp->udev); 8152 8153 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR); 8154 ocp_data &= ~SLOT_EN; 8155 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 8156 8157 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); 8158 ocp_data |= FLOW_CTRL_EN; 8159 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); 8160 8161 /* enable fc timer and set timer to 600 ms. */ 8162 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, 8163 CTRL_TIMER_EN | (600 / 8)); 8164 8165 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 8166 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN)) 8167 ocp_data |= FLOW_CTRL_PATCH_2; 8168 ocp_data &= ~AUTO_SPEEDUP; 8169 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 8170 8171 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 8172 ocp_data |= FC_PATCH_TASK; 8173 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 8174 8175 r8156_mac_clk_spd(tp, true); 8176 8177 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 8178 ocp_data &= ~PLA_MCU_SPDWN_EN; 8179 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 8180 8181 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 8182 if (rtl8152_get_speed(tp) & LINK_STATUS) 8183 ocp_data |= CUR_LINK_OK; 8184 else 8185 ocp_data &= ~CUR_LINK_OK; 8186 ocp_data |= POLL_LINK_CHG; 8187 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 8188 8189 set_bit(GREEN_ETHERNET, &tp->flags); 8190 8191 /* rx aggregation */ 8192 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 8193 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 8194 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 8195 8196 r8156_mdio_force_mode(tp); 8197 rtl_tally_reset(tp); 8198 8199 tp->coalesce = 15000; /* 15 us */ 8200 } 8201 8202 static bool rtl_check_vendor_ok(struct usb_interface *intf) 8203 { 8204 struct usb_host_interface *alt = intf->cur_altsetting; 8205 struct usb_endpoint_descriptor *in, *out, *intr; 8206 8207 if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) { 8208 dev_err(&intf->dev, "Expected endpoints are not found\n"); 8209 return false; 8210 } 8211 8212 /* Check Rx endpoint address */ 8213 if (usb_endpoint_num(in) != 1) { 8214 dev_err(&intf->dev, "Invalid Rx endpoint address\n"); 8215 return false; 8216 } 8217 8218 /* Check Tx endpoint address */ 8219 if (usb_endpoint_num(out) != 2) { 8220 dev_err(&intf->dev, "Invalid Tx endpoint address\n"); 8221 return false; 8222 } 8223 8224 /* Check interrupt endpoint address */ 8225 if (usb_endpoint_num(intr) != 3) { 8226 dev_err(&intf->dev, "Invalid interrupt endpoint address\n"); 8227 return false; 8228 } 8229 8230 return true; 8231 } 8232 8233 static bool rtl_vendor_mode(struct usb_interface *intf) 8234 { 8235 struct usb_host_interface *alt = intf->cur_altsetting; 8236 struct usb_device *udev; 8237 struct usb_host_config *c; 8238 int i, num_configs; 8239 8240 if (alt->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) 8241 return rtl_check_vendor_ok(intf); 8242 8243 /* The vendor mode is not always config #1, so to find it out. */ 8244 udev = interface_to_usbdev(intf); 8245 c = udev->config; 8246 num_configs = udev->descriptor.bNumConfigurations; 8247 if (num_configs < 2) 8248 return false; 8249 8250 for (i = 0; i < num_configs; (i++, c++)) { 8251 struct usb_interface_descriptor *desc = NULL; 8252 8253 if (c->desc.bNumInterfaces > 0) 8254 desc = &c->intf_cache[0]->altsetting->desc; 8255 else 8256 continue; 8257 8258 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) { 8259 usb_driver_set_configuration(udev, c->desc.bConfigurationValue); 8260 break; 8261 } 8262 } 8263 8264 if (i == num_configs) 8265 dev_err(&intf->dev, "Unexpected Device\n"); 8266 8267 return false; 8268 } 8269 8270 static int rtl8152_pre_reset(struct usb_interface *intf) 8271 { 8272 struct r8152 *tp = usb_get_intfdata(intf); 8273 struct net_device *netdev; 8274 8275 if (!tp) 8276 return 0; 8277 8278 netdev = tp->netdev; 8279 if (!netif_running(netdev)) 8280 return 0; 8281 8282 netif_stop_queue(netdev); 8283 tasklet_disable(&tp->tx_tl); 8284 clear_bit(WORK_ENABLE, &tp->flags); 8285 usb_kill_urb(tp->intr_urb); 8286 cancel_delayed_work_sync(&tp->schedule); 8287 napi_disable(&tp->napi); 8288 if (netif_carrier_ok(netdev)) { 8289 mutex_lock(&tp->control); 8290 tp->rtl_ops.disable(tp); 8291 mutex_unlock(&tp->control); 8292 } 8293 8294 return 0; 8295 } 8296 8297 static int rtl8152_post_reset(struct usb_interface *intf) 8298 { 8299 struct r8152 *tp = usb_get_intfdata(intf); 8300 struct net_device *netdev; 8301 struct sockaddr sa; 8302 8303 if (!tp) 8304 return 0; 8305 8306 /* reset the MAC address in case of policy change */ 8307 if (determine_ethernet_addr(tp, &sa) >= 0) { 8308 rtnl_lock(); 8309 dev_set_mac_address (tp->netdev, &sa, NULL); 8310 rtnl_unlock(); 8311 } 8312 8313 netdev = tp->netdev; 8314 if (!netif_running(netdev)) 8315 return 0; 8316 8317 set_bit(WORK_ENABLE, &tp->flags); 8318 if (netif_carrier_ok(netdev)) { 8319 mutex_lock(&tp->control); 8320 tp->rtl_ops.enable(tp); 8321 rtl_start_rx(tp); 8322 _rtl8152_set_rx_mode(netdev); 8323 mutex_unlock(&tp->control); 8324 } 8325 8326 napi_enable(&tp->napi); 8327 tasklet_enable(&tp->tx_tl); 8328 netif_wake_queue(netdev); 8329 usb_submit_urb(tp->intr_urb, GFP_KERNEL); 8330 8331 if (!list_empty(&tp->rx_done)) 8332 napi_schedule(&tp->napi); 8333 8334 return 0; 8335 } 8336 8337 static bool delay_autosuspend(struct r8152 *tp) 8338 { 8339 bool sw_linking = !!netif_carrier_ok(tp->netdev); 8340 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); 8341 8342 /* This means a linking change occurs and the driver doesn't detect it, 8343 * yet. If the driver has disabled tx/rx and hw is linking on, the 8344 * device wouldn't wake up by receiving any packet. 8345 */ 8346 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) 8347 return true; 8348 8349 /* If the linking down is occurred by nway, the device may miss the 8350 * linking change event. And it wouldn't wake when linking on. 8351 */ 8352 if (!sw_linking && tp->rtl_ops.in_nway(tp)) 8353 return true; 8354 else if (!skb_queue_empty(&tp->tx_queue)) 8355 return true; 8356 else 8357 return false; 8358 } 8359 8360 static int rtl8152_runtime_resume(struct r8152 *tp) 8361 { 8362 struct net_device *netdev = tp->netdev; 8363 8364 if (netif_running(netdev) && netdev->flags & IFF_UP) { 8365 struct napi_struct *napi = &tp->napi; 8366 8367 tp->rtl_ops.autosuspend_en(tp, false); 8368 napi_disable(napi); 8369 set_bit(WORK_ENABLE, &tp->flags); 8370 8371 if (netif_carrier_ok(netdev)) { 8372 if (rtl8152_get_speed(tp) & LINK_STATUS) { 8373 rtl_start_rx(tp); 8374 } else { 8375 netif_carrier_off(netdev); 8376 tp->rtl_ops.disable(tp); 8377 netif_info(tp, link, netdev, "linking down\n"); 8378 } 8379 } 8380 8381 napi_enable(napi); 8382 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8383 smp_mb__after_atomic(); 8384 8385 if (!list_empty(&tp->rx_done)) 8386 napi_schedule(&tp->napi); 8387 8388 usb_submit_urb(tp->intr_urb, GFP_NOIO); 8389 } else { 8390 if (netdev->flags & IFF_UP) 8391 tp->rtl_ops.autosuspend_en(tp, false); 8392 8393 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8394 } 8395 8396 return 0; 8397 } 8398 8399 static int rtl8152_system_resume(struct r8152 *tp) 8400 { 8401 struct net_device *netdev = tp->netdev; 8402 8403 netif_device_attach(netdev); 8404 8405 if (netif_running(netdev) && (netdev->flags & IFF_UP)) { 8406 tp->rtl_ops.up(tp); 8407 netif_carrier_off(netdev); 8408 set_bit(WORK_ENABLE, &tp->flags); 8409 usb_submit_urb(tp->intr_urb, GFP_NOIO); 8410 } 8411 8412 return 0; 8413 } 8414 8415 static int rtl8152_runtime_suspend(struct r8152 *tp) 8416 { 8417 struct net_device *netdev = tp->netdev; 8418 int ret = 0; 8419 8420 if (!tp->rtl_ops.autosuspend_en) 8421 return -EBUSY; 8422 8423 set_bit(SELECTIVE_SUSPEND, &tp->flags); 8424 smp_mb__after_atomic(); 8425 8426 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 8427 u32 rcr = 0; 8428 8429 if (netif_carrier_ok(netdev)) { 8430 u32 ocp_data; 8431 8432 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 8433 ocp_data = rcr & ~RCR_ACPT_ALL; 8434 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 8435 rxdy_gated_en(tp, true); 8436 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 8437 PLA_OOB_CTRL); 8438 if (!(ocp_data & RXFIFO_EMPTY)) { 8439 rxdy_gated_en(tp, false); 8440 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 8441 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8442 smp_mb__after_atomic(); 8443 ret = -EBUSY; 8444 goto out1; 8445 } 8446 } 8447 8448 clear_bit(WORK_ENABLE, &tp->flags); 8449 usb_kill_urb(tp->intr_urb); 8450 8451 tp->rtl_ops.autosuspend_en(tp, true); 8452 8453 if (netif_carrier_ok(netdev)) { 8454 struct napi_struct *napi = &tp->napi; 8455 8456 napi_disable(napi); 8457 rtl_stop_rx(tp); 8458 rxdy_gated_en(tp, false); 8459 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 8460 napi_enable(napi); 8461 } 8462 8463 if (delay_autosuspend(tp)) { 8464 rtl8152_runtime_resume(tp); 8465 ret = -EBUSY; 8466 } 8467 } 8468 8469 out1: 8470 return ret; 8471 } 8472 8473 static int rtl8152_system_suspend(struct r8152 *tp) 8474 { 8475 struct net_device *netdev = tp->netdev; 8476 8477 netif_device_detach(netdev); 8478 8479 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 8480 struct napi_struct *napi = &tp->napi; 8481 8482 clear_bit(WORK_ENABLE, &tp->flags); 8483 usb_kill_urb(tp->intr_urb); 8484 tasklet_disable(&tp->tx_tl); 8485 napi_disable(napi); 8486 cancel_delayed_work_sync(&tp->schedule); 8487 tp->rtl_ops.down(tp); 8488 napi_enable(napi); 8489 tasklet_enable(&tp->tx_tl); 8490 } 8491 8492 return 0; 8493 } 8494 8495 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) 8496 { 8497 struct r8152 *tp = usb_get_intfdata(intf); 8498 int ret; 8499 8500 mutex_lock(&tp->control); 8501 8502 if (PMSG_IS_AUTO(message)) 8503 ret = rtl8152_runtime_suspend(tp); 8504 else 8505 ret = rtl8152_system_suspend(tp); 8506 8507 mutex_unlock(&tp->control); 8508 8509 return ret; 8510 } 8511 8512 static int rtl8152_resume(struct usb_interface *intf) 8513 { 8514 struct r8152 *tp = usb_get_intfdata(intf); 8515 int ret; 8516 8517 mutex_lock(&tp->control); 8518 8519 rtl_reset_ocp_base(tp); 8520 8521 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) 8522 ret = rtl8152_runtime_resume(tp); 8523 else 8524 ret = rtl8152_system_resume(tp); 8525 8526 mutex_unlock(&tp->control); 8527 8528 return ret; 8529 } 8530 8531 static int rtl8152_reset_resume(struct usb_interface *intf) 8532 { 8533 struct r8152 *tp = usb_get_intfdata(intf); 8534 8535 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8536 rtl_reset_ocp_base(tp); 8537 tp->rtl_ops.init(tp); 8538 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 8539 set_ethernet_addr(tp, true); 8540 return rtl8152_resume(intf); 8541 } 8542 8543 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 8544 { 8545 struct r8152 *tp = netdev_priv(dev); 8546 8547 if (usb_autopm_get_interface(tp->intf) < 0) 8548 return; 8549 8550 if (!rtl_can_wakeup(tp)) { 8551 wol->supported = 0; 8552 wol->wolopts = 0; 8553 } else { 8554 mutex_lock(&tp->control); 8555 wol->supported = WAKE_ANY; 8556 wol->wolopts = __rtl_get_wol(tp); 8557 mutex_unlock(&tp->control); 8558 } 8559 8560 usb_autopm_put_interface(tp->intf); 8561 } 8562 8563 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 8564 { 8565 struct r8152 *tp = netdev_priv(dev); 8566 int ret; 8567 8568 if (!rtl_can_wakeup(tp)) 8569 return -EOPNOTSUPP; 8570 8571 if (wol->wolopts & ~WAKE_ANY) 8572 return -EINVAL; 8573 8574 ret = usb_autopm_get_interface(tp->intf); 8575 if (ret < 0) 8576 goto out_set_wol; 8577 8578 mutex_lock(&tp->control); 8579 8580 __rtl_set_wol(tp, wol->wolopts); 8581 tp->saved_wolopts = wol->wolopts & WAKE_ANY; 8582 8583 mutex_unlock(&tp->control); 8584 8585 usb_autopm_put_interface(tp->intf); 8586 8587 out_set_wol: 8588 return ret; 8589 } 8590 8591 static u32 rtl8152_get_msglevel(struct net_device *dev) 8592 { 8593 struct r8152 *tp = netdev_priv(dev); 8594 8595 return tp->msg_enable; 8596 } 8597 8598 static void rtl8152_set_msglevel(struct net_device *dev, u32 value) 8599 { 8600 struct r8152 *tp = netdev_priv(dev); 8601 8602 tp->msg_enable = value; 8603 } 8604 8605 static void rtl8152_get_drvinfo(struct net_device *netdev, 8606 struct ethtool_drvinfo *info) 8607 { 8608 struct r8152 *tp = netdev_priv(netdev); 8609 8610 strscpy(info->driver, MODULENAME, sizeof(info->driver)); 8611 strscpy(info->version, DRIVER_VERSION, sizeof(info->version)); 8612 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); 8613 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw)) 8614 strscpy(info->fw_version, tp->rtl_fw.version, 8615 sizeof(info->fw_version)); 8616 } 8617 8618 static 8619 int rtl8152_get_link_ksettings(struct net_device *netdev, 8620 struct ethtool_link_ksettings *cmd) 8621 { 8622 struct r8152 *tp = netdev_priv(netdev); 8623 int ret; 8624 8625 if (!tp->mii.mdio_read) 8626 return -EOPNOTSUPP; 8627 8628 ret = usb_autopm_get_interface(tp->intf); 8629 if (ret < 0) 8630 goto out; 8631 8632 mutex_lock(&tp->control); 8633 8634 mii_ethtool_get_link_ksettings(&tp->mii, cmd); 8635 8636 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8637 cmd->link_modes.supported, tp->support_2500full); 8638 8639 if (tp->support_2500full) { 8640 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8641 cmd->link_modes.advertising, 8642 ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G); 8643 8644 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8645 cmd->link_modes.lp_advertising, 8646 ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G); 8647 8648 if (is_speed_2500(rtl8152_get_speed(tp))) 8649 cmd->base.speed = SPEED_2500; 8650 } 8651 8652 mutex_unlock(&tp->control); 8653 8654 usb_autopm_put_interface(tp->intf); 8655 8656 out: 8657 return ret; 8658 } 8659 8660 static int rtl8152_set_link_ksettings(struct net_device *dev, 8661 const struct ethtool_link_ksettings *cmd) 8662 { 8663 struct r8152 *tp = netdev_priv(dev); 8664 u32 advertising = 0; 8665 int ret; 8666 8667 ret = usb_autopm_get_interface(tp->intf); 8668 if (ret < 0) 8669 goto out; 8670 8671 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, 8672 cmd->link_modes.advertising)) 8673 advertising |= RTL_ADVERTISED_10_HALF; 8674 8675 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, 8676 cmd->link_modes.advertising)) 8677 advertising |= RTL_ADVERTISED_10_FULL; 8678 8679 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 8680 cmd->link_modes.advertising)) 8681 advertising |= RTL_ADVERTISED_100_HALF; 8682 8683 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 8684 cmd->link_modes.advertising)) 8685 advertising |= RTL_ADVERTISED_100_FULL; 8686 8687 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 8688 cmd->link_modes.advertising)) 8689 advertising |= RTL_ADVERTISED_1000_HALF; 8690 8691 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 8692 cmd->link_modes.advertising)) 8693 advertising |= RTL_ADVERTISED_1000_FULL; 8694 8695 if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8696 cmd->link_modes.advertising)) 8697 advertising |= RTL_ADVERTISED_2500_FULL; 8698 8699 mutex_lock(&tp->control); 8700 8701 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, 8702 cmd->base.duplex, advertising); 8703 if (!ret) { 8704 tp->autoneg = cmd->base.autoneg; 8705 tp->speed = cmd->base.speed; 8706 tp->duplex = cmd->base.duplex; 8707 tp->advertising = advertising; 8708 } 8709 8710 mutex_unlock(&tp->control); 8711 8712 usb_autopm_put_interface(tp->intf); 8713 8714 out: 8715 return ret; 8716 } 8717 8718 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { 8719 "tx_packets", 8720 "rx_packets", 8721 "tx_errors", 8722 "rx_errors", 8723 "rx_missed", 8724 "align_errors", 8725 "tx_single_collisions", 8726 "tx_multi_collisions", 8727 "rx_unicast", 8728 "rx_broadcast", 8729 "rx_multicast", 8730 "tx_aborted", 8731 "tx_underrun", 8732 }; 8733 8734 static int rtl8152_get_sset_count(struct net_device *dev, int sset) 8735 { 8736 switch (sset) { 8737 case ETH_SS_STATS: 8738 return ARRAY_SIZE(rtl8152_gstrings); 8739 default: 8740 return -EOPNOTSUPP; 8741 } 8742 } 8743 8744 static void rtl8152_get_ethtool_stats(struct net_device *dev, 8745 struct ethtool_stats *stats, u64 *data) 8746 { 8747 struct r8152 *tp = netdev_priv(dev); 8748 struct tally_counter tally; 8749 8750 if (usb_autopm_get_interface(tp->intf) < 0) 8751 return; 8752 8753 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); 8754 8755 usb_autopm_put_interface(tp->intf); 8756 8757 data[0] = le64_to_cpu(tally.tx_packets); 8758 data[1] = le64_to_cpu(tally.rx_packets); 8759 data[2] = le64_to_cpu(tally.tx_errors); 8760 data[3] = le32_to_cpu(tally.rx_errors); 8761 data[4] = le16_to_cpu(tally.rx_missed); 8762 data[5] = le16_to_cpu(tally.align_errors); 8763 data[6] = le32_to_cpu(tally.tx_one_collision); 8764 data[7] = le32_to_cpu(tally.tx_multi_collision); 8765 data[8] = le64_to_cpu(tally.rx_unicast); 8766 data[9] = le64_to_cpu(tally.rx_broadcast); 8767 data[10] = le32_to_cpu(tally.rx_multicast); 8768 data[11] = le16_to_cpu(tally.tx_aborted); 8769 data[12] = le16_to_cpu(tally.tx_underrun); 8770 } 8771 8772 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) 8773 { 8774 switch (stringset) { 8775 case ETH_SS_STATS: 8776 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings)); 8777 break; 8778 } 8779 } 8780 8781 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) 8782 { 8783 u32 lp, adv, supported = 0; 8784 u16 val; 8785 8786 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 8787 supported = mmd_eee_cap_to_ethtool_sup_t(val); 8788 8789 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 8790 adv = mmd_eee_adv_to_ethtool_adv_t(val); 8791 8792 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 8793 lp = mmd_eee_adv_to_ethtool_adv_t(val); 8794 8795 eee->eee_enabled = tp->eee_en; 8796 eee->eee_active = !!(supported & adv & lp); 8797 eee->supported = supported; 8798 eee->advertised = tp->eee_adv; 8799 eee->lp_advertised = lp; 8800 8801 return 0; 8802 } 8803 8804 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) 8805 { 8806 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); 8807 8808 tp->eee_en = eee->eee_enabled; 8809 tp->eee_adv = val; 8810 8811 rtl_eee_enable(tp, tp->eee_en); 8812 8813 return 0; 8814 } 8815 8816 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) 8817 { 8818 u32 lp, adv, supported = 0; 8819 u16 val; 8820 8821 val = ocp_reg_read(tp, OCP_EEE_ABLE); 8822 supported = mmd_eee_cap_to_ethtool_sup_t(val); 8823 8824 val = ocp_reg_read(tp, OCP_EEE_ADV); 8825 adv = mmd_eee_adv_to_ethtool_adv_t(val); 8826 8827 val = ocp_reg_read(tp, OCP_EEE_LPABLE); 8828 lp = mmd_eee_adv_to_ethtool_adv_t(val); 8829 8830 eee->eee_enabled = tp->eee_en; 8831 eee->eee_active = !!(supported & adv & lp); 8832 eee->supported = supported; 8833 eee->advertised = tp->eee_adv; 8834 eee->lp_advertised = lp; 8835 8836 return 0; 8837 } 8838 8839 static int 8840 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) 8841 { 8842 struct r8152 *tp = netdev_priv(net); 8843 int ret; 8844 8845 if (!tp->rtl_ops.eee_get) { 8846 ret = -EOPNOTSUPP; 8847 goto out; 8848 } 8849 8850 ret = usb_autopm_get_interface(tp->intf); 8851 if (ret < 0) 8852 goto out; 8853 8854 mutex_lock(&tp->control); 8855 8856 ret = tp->rtl_ops.eee_get(tp, edata); 8857 8858 mutex_unlock(&tp->control); 8859 8860 usb_autopm_put_interface(tp->intf); 8861 8862 out: 8863 return ret; 8864 } 8865 8866 static int 8867 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) 8868 { 8869 struct r8152 *tp = netdev_priv(net); 8870 int ret; 8871 8872 if (!tp->rtl_ops.eee_set) { 8873 ret = -EOPNOTSUPP; 8874 goto out; 8875 } 8876 8877 ret = usb_autopm_get_interface(tp->intf); 8878 if (ret < 0) 8879 goto out; 8880 8881 mutex_lock(&tp->control); 8882 8883 ret = tp->rtl_ops.eee_set(tp, edata); 8884 if (!ret) 8885 ret = mii_nway_restart(&tp->mii); 8886 8887 mutex_unlock(&tp->control); 8888 8889 usb_autopm_put_interface(tp->intf); 8890 8891 out: 8892 return ret; 8893 } 8894 8895 static int rtl8152_nway_reset(struct net_device *dev) 8896 { 8897 struct r8152 *tp = netdev_priv(dev); 8898 int ret; 8899 8900 ret = usb_autopm_get_interface(tp->intf); 8901 if (ret < 0) 8902 goto out; 8903 8904 mutex_lock(&tp->control); 8905 8906 ret = mii_nway_restart(&tp->mii); 8907 8908 mutex_unlock(&tp->control); 8909 8910 usb_autopm_put_interface(tp->intf); 8911 8912 out: 8913 return ret; 8914 } 8915 8916 static int rtl8152_get_coalesce(struct net_device *netdev, 8917 struct ethtool_coalesce *coalesce, 8918 struct kernel_ethtool_coalesce *kernel_coal, 8919 struct netlink_ext_ack *extack) 8920 { 8921 struct r8152 *tp = netdev_priv(netdev); 8922 8923 switch (tp->version) { 8924 case RTL_VER_01: 8925 case RTL_VER_02: 8926 case RTL_VER_07: 8927 return -EOPNOTSUPP; 8928 default: 8929 break; 8930 } 8931 8932 coalesce->rx_coalesce_usecs = tp->coalesce; 8933 8934 return 0; 8935 } 8936 8937 static int rtl8152_set_coalesce(struct net_device *netdev, 8938 struct ethtool_coalesce *coalesce, 8939 struct kernel_ethtool_coalesce *kernel_coal, 8940 struct netlink_ext_ack *extack) 8941 { 8942 struct r8152 *tp = netdev_priv(netdev); 8943 int ret; 8944 8945 switch (tp->version) { 8946 case RTL_VER_01: 8947 case RTL_VER_02: 8948 case RTL_VER_07: 8949 return -EOPNOTSUPP; 8950 default: 8951 break; 8952 } 8953 8954 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) 8955 return -EINVAL; 8956 8957 ret = usb_autopm_get_interface(tp->intf); 8958 if (ret < 0) 8959 return ret; 8960 8961 mutex_lock(&tp->control); 8962 8963 if (tp->coalesce != coalesce->rx_coalesce_usecs) { 8964 tp->coalesce = coalesce->rx_coalesce_usecs; 8965 8966 if (netif_running(netdev) && netif_carrier_ok(netdev)) { 8967 netif_stop_queue(netdev); 8968 napi_disable(&tp->napi); 8969 tp->rtl_ops.disable(tp); 8970 tp->rtl_ops.enable(tp); 8971 rtl_start_rx(tp); 8972 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 8973 _rtl8152_set_rx_mode(netdev); 8974 napi_enable(&tp->napi); 8975 netif_wake_queue(netdev); 8976 } 8977 } 8978 8979 mutex_unlock(&tp->control); 8980 8981 usb_autopm_put_interface(tp->intf); 8982 8983 return ret; 8984 } 8985 8986 static int rtl8152_get_tunable(struct net_device *netdev, 8987 const struct ethtool_tunable *tunable, void *d) 8988 { 8989 struct r8152 *tp = netdev_priv(netdev); 8990 8991 switch (tunable->id) { 8992 case ETHTOOL_RX_COPYBREAK: 8993 *(u32 *)d = tp->rx_copybreak; 8994 break; 8995 default: 8996 return -EOPNOTSUPP; 8997 } 8998 8999 return 0; 9000 } 9001 9002 static int rtl8152_set_tunable(struct net_device *netdev, 9003 const struct ethtool_tunable *tunable, 9004 const void *d) 9005 { 9006 struct r8152 *tp = netdev_priv(netdev); 9007 u32 val; 9008 9009 switch (tunable->id) { 9010 case ETHTOOL_RX_COPYBREAK: 9011 val = *(u32 *)d; 9012 if (val < ETH_ZLEN) { 9013 netif_err(tp, rx_err, netdev, 9014 "Invalid rx copy break value\n"); 9015 return -EINVAL; 9016 } 9017 9018 if (tp->rx_copybreak != val) { 9019 if (netdev->flags & IFF_UP) { 9020 mutex_lock(&tp->control); 9021 napi_disable(&tp->napi); 9022 tp->rx_copybreak = val; 9023 napi_enable(&tp->napi); 9024 mutex_unlock(&tp->control); 9025 } else { 9026 tp->rx_copybreak = val; 9027 } 9028 } 9029 break; 9030 default: 9031 return -EOPNOTSUPP; 9032 } 9033 9034 return 0; 9035 } 9036 9037 static void rtl8152_get_ringparam(struct net_device *netdev, 9038 struct ethtool_ringparam *ring, 9039 struct kernel_ethtool_ringparam *kernel_ring, 9040 struct netlink_ext_ack *extack) 9041 { 9042 struct r8152 *tp = netdev_priv(netdev); 9043 9044 ring->rx_max_pending = RTL8152_RX_MAX_PENDING; 9045 ring->rx_pending = tp->rx_pending; 9046 } 9047 9048 static int rtl8152_set_ringparam(struct net_device *netdev, 9049 struct ethtool_ringparam *ring, 9050 struct kernel_ethtool_ringparam *kernel_ring, 9051 struct netlink_ext_ack *extack) 9052 { 9053 struct r8152 *tp = netdev_priv(netdev); 9054 9055 if (ring->rx_pending < (RTL8152_MAX_RX * 2)) 9056 return -EINVAL; 9057 9058 if (tp->rx_pending != ring->rx_pending) { 9059 if (netdev->flags & IFF_UP) { 9060 mutex_lock(&tp->control); 9061 napi_disable(&tp->napi); 9062 tp->rx_pending = ring->rx_pending; 9063 napi_enable(&tp->napi); 9064 mutex_unlock(&tp->control); 9065 } else { 9066 tp->rx_pending = ring->rx_pending; 9067 } 9068 } 9069 9070 return 0; 9071 } 9072 9073 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 9074 { 9075 struct r8152 *tp = netdev_priv(netdev); 9076 u16 bmcr, lcladv, rmtadv; 9077 u8 cap; 9078 9079 if (usb_autopm_get_interface(tp->intf) < 0) 9080 return; 9081 9082 mutex_lock(&tp->control); 9083 9084 bmcr = r8152_mdio_read(tp, MII_BMCR); 9085 lcladv = r8152_mdio_read(tp, MII_ADVERTISE); 9086 rmtadv = r8152_mdio_read(tp, MII_LPA); 9087 9088 mutex_unlock(&tp->control); 9089 9090 usb_autopm_put_interface(tp->intf); 9091 9092 if (!(bmcr & BMCR_ANENABLE)) { 9093 pause->autoneg = 0; 9094 pause->rx_pause = 0; 9095 pause->tx_pause = 0; 9096 return; 9097 } 9098 9099 pause->autoneg = 1; 9100 9101 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv); 9102 9103 if (cap & FLOW_CTRL_RX) 9104 pause->rx_pause = 1; 9105 9106 if (cap & FLOW_CTRL_TX) 9107 pause->tx_pause = 1; 9108 } 9109 9110 static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 9111 { 9112 struct r8152 *tp = netdev_priv(netdev); 9113 u16 old, new1; 9114 u8 cap = 0; 9115 int ret; 9116 9117 ret = usb_autopm_get_interface(tp->intf); 9118 if (ret < 0) 9119 return ret; 9120 9121 mutex_lock(&tp->control); 9122 9123 if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) { 9124 ret = -EINVAL; 9125 goto out; 9126 } 9127 9128 if (pause->rx_pause) 9129 cap |= FLOW_CTRL_RX; 9130 9131 if (pause->tx_pause) 9132 cap |= FLOW_CTRL_TX; 9133 9134 old = r8152_mdio_read(tp, MII_ADVERTISE); 9135 new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap); 9136 if (old != new1) 9137 r8152_mdio_write(tp, MII_ADVERTISE, new1); 9138 9139 out: 9140 mutex_unlock(&tp->control); 9141 usb_autopm_put_interface(tp->intf); 9142 9143 return ret; 9144 } 9145 9146 static const struct ethtool_ops ops = { 9147 .supported_coalesce_params = ETHTOOL_COALESCE_USECS, 9148 .get_drvinfo = rtl8152_get_drvinfo, 9149 .get_link = ethtool_op_get_link, 9150 .nway_reset = rtl8152_nway_reset, 9151 .get_msglevel = rtl8152_get_msglevel, 9152 .set_msglevel = rtl8152_set_msglevel, 9153 .get_wol = rtl8152_get_wol, 9154 .set_wol = rtl8152_set_wol, 9155 .get_strings = rtl8152_get_strings, 9156 .get_sset_count = rtl8152_get_sset_count, 9157 .get_ethtool_stats = rtl8152_get_ethtool_stats, 9158 .get_coalesce = rtl8152_get_coalesce, 9159 .set_coalesce = rtl8152_set_coalesce, 9160 .get_eee = rtl_ethtool_get_eee, 9161 .set_eee = rtl_ethtool_set_eee, 9162 .get_link_ksettings = rtl8152_get_link_ksettings, 9163 .set_link_ksettings = rtl8152_set_link_ksettings, 9164 .get_tunable = rtl8152_get_tunable, 9165 .set_tunable = rtl8152_set_tunable, 9166 .get_ringparam = rtl8152_get_ringparam, 9167 .set_ringparam = rtl8152_set_ringparam, 9168 .get_pauseparam = rtl8152_get_pauseparam, 9169 .set_pauseparam = rtl8152_set_pauseparam, 9170 }; 9171 9172 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) 9173 { 9174 struct r8152 *tp = netdev_priv(netdev); 9175 struct mii_ioctl_data *data = if_mii(rq); 9176 int res; 9177 9178 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 9179 return -ENODEV; 9180 9181 res = usb_autopm_get_interface(tp->intf); 9182 if (res < 0) 9183 goto out; 9184 9185 switch (cmd) { 9186 case SIOCGMIIPHY: 9187 data->phy_id = R8152_PHY_ID; /* Internal PHY */ 9188 break; 9189 9190 case SIOCGMIIREG: 9191 mutex_lock(&tp->control); 9192 data->val_out = r8152_mdio_read(tp, data->reg_num); 9193 mutex_unlock(&tp->control); 9194 break; 9195 9196 case SIOCSMIIREG: 9197 if (!capable(CAP_NET_ADMIN)) { 9198 res = -EPERM; 9199 break; 9200 } 9201 mutex_lock(&tp->control); 9202 r8152_mdio_write(tp, data->reg_num, data->val_in); 9203 mutex_unlock(&tp->control); 9204 break; 9205 9206 default: 9207 res = -EOPNOTSUPP; 9208 } 9209 9210 usb_autopm_put_interface(tp->intf); 9211 9212 out: 9213 return res; 9214 } 9215 9216 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) 9217 { 9218 struct r8152 *tp = netdev_priv(dev); 9219 int ret; 9220 9221 switch (tp->version) { 9222 case RTL_VER_01: 9223 case RTL_VER_02: 9224 case RTL_VER_07: 9225 dev->mtu = new_mtu; 9226 return 0; 9227 default: 9228 break; 9229 } 9230 9231 ret = usb_autopm_get_interface(tp->intf); 9232 if (ret < 0) 9233 return ret; 9234 9235 mutex_lock(&tp->control); 9236 9237 dev->mtu = new_mtu; 9238 9239 if (netif_running(dev)) { 9240 if (tp->rtl_ops.change_mtu) 9241 tp->rtl_ops.change_mtu(tp); 9242 9243 if (netif_carrier_ok(dev)) { 9244 netif_stop_queue(dev); 9245 napi_disable(&tp->napi); 9246 tasklet_disable(&tp->tx_tl); 9247 tp->rtl_ops.disable(tp); 9248 tp->rtl_ops.enable(tp); 9249 rtl_start_rx(tp); 9250 tasklet_enable(&tp->tx_tl); 9251 napi_enable(&tp->napi); 9252 rtl8152_set_rx_mode(dev); 9253 netif_wake_queue(dev); 9254 } 9255 } 9256 9257 mutex_unlock(&tp->control); 9258 9259 usb_autopm_put_interface(tp->intf); 9260 9261 return ret; 9262 } 9263 9264 static const struct net_device_ops rtl8152_netdev_ops = { 9265 .ndo_open = rtl8152_open, 9266 .ndo_stop = rtl8152_close, 9267 .ndo_eth_ioctl = rtl8152_ioctl, 9268 .ndo_start_xmit = rtl8152_start_xmit, 9269 .ndo_tx_timeout = rtl8152_tx_timeout, 9270 .ndo_set_features = rtl8152_set_features, 9271 .ndo_set_rx_mode = rtl8152_set_rx_mode, 9272 .ndo_set_mac_address = rtl8152_set_mac_address, 9273 .ndo_change_mtu = rtl8152_change_mtu, 9274 .ndo_validate_addr = eth_validate_addr, 9275 .ndo_features_check = rtl8152_features_check, 9276 }; 9277 9278 static void rtl8152_unload(struct r8152 *tp) 9279 { 9280 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 9281 return; 9282 9283 if (tp->version != RTL_VER_01) 9284 r8152_power_cut_en(tp, true); 9285 } 9286 9287 static void rtl8153_unload(struct r8152 *tp) 9288 { 9289 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 9290 return; 9291 9292 r8153_power_cut_en(tp, false); 9293 } 9294 9295 static void rtl8153b_unload(struct r8152 *tp) 9296 { 9297 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 9298 return; 9299 9300 r8153b_power_cut_en(tp, false); 9301 } 9302 9303 static int rtl_ops_init(struct r8152 *tp) 9304 { 9305 struct rtl_ops *ops = &tp->rtl_ops; 9306 int ret = 0; 9307 9308 switch (tp->version) { 9309 case RTL_VER_01: 9310 case RTL_VER_02: 9311 case RTL_VER_07: 9312 ops->init = r8152b_init; 9313 ops->enable = rtl8152_enable; 9314 ops->disable = rtl8152_disable; 9315 ops->up = rtl8152_up; 9316 ops->down = rtl8152_down; 9317 ops->unload = rtl8152_unload; 9318 ops->eee_get = r8152_get_eee; 9319 ops->eee_set = r8152_set_eee; 9320 ops->in_nway = rtl8152_in_nway; 9321 ops->hw_phy_cfg = r8152b_hw_phy_cfg; 9322 ops->autosuspend_en = rtl_runtime_suspend_enable; 9323 tp->rx_buf_sz = 16 * 1024; 9324 tp->eee_en = true; 9325 tp->eee_adv = MDIO_EEE_100TX; 9326 break; 9327 9328 case RTL_VER_03: 9329 case RTL_VER_04: 9330 case RTL_VER_05: 9331 case RTL_VER_06: 9332 ops->init = r8153_init; 9333 ops->enable = rtl8153_enable; 9334 ops->disable = rtl8153_disable; 9335 ops->up = rtl8153_up; 9336 ops->down = rtl8153_down; 9337 ops->unload = rtl8153_unload; 9338 ops->eee_get = r8153_get_eee; 9339 ops->eee_set = r8152_set_eee; 9340 ops->in_nway = rtl8153_in_nway; 9341 ops->hw_phy_cfg = r8153_hw_phy_cfg; 9342 ops->autosuspend_en = rtl8153_runtime_enable; 9343 ops->change_mtu = rtl8153_change_mtu; 9344 if (tp->udev->speed < USB_SPEED_SUPER) 9345 tp->rx_buf_sz = 16 * 1024; 9346 else 9347 tp->rx_buf_sz = 32 * 1024; 9348 tp->eee_en = true; 9349 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9350 break; 9351 9352 case RTL_VER_08: 9353 case RTL_VER_09: 9354 ops->init = r8153b_init; 9355 ops->enable = rtl8153_enable; 9356 ops->disable = rtl8153_disable; 9357 ops->up = rtl8153b_up; 9358 ops->down = rtl8153b_down; 9359 ops->unload = rtl8153b_unload; 9360 ops->eee_get = r8153_get_eee; 9361 ops->eee_set = r8152_set_eee; 9362 ops->in_nway = rtl8153_in_nway; 9363 ops->hw_phy_cfg = r8153b_hw_phy_cfg; 9364 ops->autosuspend_en = rtl8153b_runtime_enable; 9365 ops->change_mtu = rtl8153_change_mtu; 9366 tp->rx_buf_sz = 32 * 1024; 9367 tp->eee_en = true; 9368 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9369 break; 9370 9371 case RTL_VER_11: 9372 tp->eee_en = true; 9373 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9374 fallthrough; 9375 case RTL_VER_10: 9376 ops->init = r8156_init; 9377 ops->enable = rtl8156_enable; 9378 ops->disable = rtl8153_disable; 9379 ops->up = rtl8156_up; 9380 ops->down = rtl8156_down; 9381 ops->unload = rtl8153_unload; 9382 ops->eee_get = r8153_get_eee; 9383 ops->eee_set = r8152_set_eee; 9384 ops->in_nway = rtl8153_in_nway; 9385 ops->hw_phy_cfg = r8156_hw_phy_cfg; 9386 ops->autosuspend_en = rtl8156_runtime_enable; 9387 ops->change_mtu = rtl8156_change_mtu; 9388 tp->rx_buf_sz = 48 * 1024; 9389 tp->support_2500full = 1; 9390 break; 9391 9392 case RTL_VER_12: 9393 case RTL_VER_13: 9394 tp->support_2500full = 1; 9395 fallthrough; 9396 case RTL_VER_15: 9397 tp->eee_en = true; 9398 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9399 ops->init = r8156b_init; 9400 ops->enable = rtl8156b_enable; 9401 ops->disable = rtl8153_disable; 9402 ops->up = rtl8156_up; 9403 ops->down = rtl8156_down; 9404 ops->unload = rtl8153_unload; 9405 ops->eee_get = r8153_get_eee; 9406 ops->eee_set = r8152_set_eee; 9407 ops->in_nway = rtl8153_in_nway; 9408 ops->hw_phy_cfg = r8156b_hw_phy_cfg; 9409 ops->autosuspend_en = rtl8156_runtime_enable; 9410 ops->change_mtu = rtl8156_change_mtu; 9411 tp->rx_buf_sz = 48 * 1024; 9412 break; 9413 9414 case RTL_VER_14: 9415 ops->init = r8153c_init; 9416 ops->enable = rtl8153_enable; 9417 ops->disable = rtl8153_disable; 9418 ops->up = rtl8153c_up; 9419 ops->down = rtl8153b_down; 9420 ops->unload = rtl8153_unload; 9421 ops->eee_get = r8153_get_eee; 9422 ops->eee_set = r8152_set_eee; 9423 ops->in_nway = rtl8153_in_nway; 9424 ops->hw_phy_cfg = r8153c_hw_phy_cfg; 9425 ops->autosuspend_en = rtl8153c_runtime_enable; 9426 ops->change_mtu = rtl8153c_change_mtu; 9427 tp->rx_buf_sz = 32 * 1024; 9428 tp->eee_en = true; 9429 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9430 break; 9431 9432 default: 9433 ret = -ENODEV; 9434 dev_err(&tp->intf->dev, "Unknown Device\n"); 9435 break; 9436 } 9437 9438 return ret; 9439 } 9440 9441 #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw" 9442 #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw" 9443 #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw" 9444 #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw" 9445 #define FIRMWARE_8153C_1 "rtl_nic/rtl8153c-1.fw" 9446 #define FIRMWARE_8156A_2 "rtl_nic/rtl8156a-2.fw" 9447 #define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw" 9448 9449 MODULE_FIRMWARE(FIRMWARE_8153A_2); 9450 MODULE_FIRMWARE(FIRMWARE_8153A_3); 9451 MODULE_FIRMWARE(FIRMWARE_8153A_4); 9452 MODULE_FIRMWARE(FIRMWARE_8153B_2); 9453 MODULE_FIRMWARE(FIRMWARE_8153C_1); 9454 MODULE_FIRMWARE(FIRMWARE_8156A_2); 9455 MODULE_FIRMWARE(FIRMWARE_8156B_2); 9456 9457 static int rtl_fw_init(struct r8152 *tp) 9458 { 9459 struct rtl_fw *rtl_fw = &tp->rtl_fw; 9460 9461 switch (tp->version) { 9462 case RTL_VER_04: 9463 rtl_fw->fw_name = FIRMWARE_8153A_2; 9464 rtl_fw->pre_fw = r8153_pre_firmware_1; 9465 rtl_fw->post_fw = r8153_post_firmware_1; 9466 break; 9467 case RTL_VER_05: 9468 rtl_fw->fw_name = FIRMWARE_8153A_3; 9469 rtl_fw->pre_fw = r8153_pre_firmware_2; 9470 rtl_fw->post_fw = r8153_post_firmware_2; 9471 break; 9472 case RTL_VER_06: 9473 rtl_fw->fw_name = FIRMWARE_8153A_4; 9474 rtl_fw->post_fw = r8153_post_firmware_3; 9475 break; 9476 case RTL_VER_09: 9477 rtl_fw->fw_name = FIRMWARE_8153B_2; 9478 rtl_fw->pre_fw = r8153b_pre_firmware_1; 9479 rtl_fw->post_fw = r8153b_post_firmware_1; 9480 break; 9481 case RTL_VER_11: 9482 rtl_fw->fw_name = FIRMWARE_8156A_2; 9483 rtl_fw->post_fw = r8156a_post_firmware_1; 9484 break; 9485 case RTL_VER_13: 9486 case RTL_VER_15: 9487 rtl_fw->fw_name = FIRMWARE_8156B_2; 9488 break; 9489 case RTL_VER_14: 9490 rtl_fw->fw_name = FIRMWARE_8153C_1; 9491 rtl_fw->pre_fw = r8153b_pre_firmware_1; 9492 rtl_fw->post_fw = r8153c_post_firmware_1; 9493 break; 9494 default: 9495 break; 9496 } 9497 9498 return 0; 9499 } 9500 9501 u8 rtl8152_get_version(struct usb_interface *intf) 9502 { 9503 struct usb_device *udev = interface_to_usbdev(intf); 9504 u32 ocp_data = 0; 9505 __le32 *tmp; 9506 u8 version; 9507 int ret; 9508 9509 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); 9510 if (!tmp) 9511 return 0; 9512 9513 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 9514 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 9515 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500); 9516 if (ret > 0) 9517 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; 9518 9519 kfree(tmp); 9520 9521 switch (ocp_data) { 9522 case 0x4c00: 9523 version = RTL_VER_01; 9524 break; 9525 case 0x4c10: 9526 version = RTL_VER_02; 9527 break; 9528 case 0x5c00: 9529 version = RTL_VER_03; 9530 break; 9531 case 0x5c10: 9532 version = RTL_VER_04; 9533 break; 9534 case 0x5c20: 9535 version = RTL_VER_05; 9536 break; 9537 case 0x5c30: 9538 version = RTL_VER_06; 9539 break; 9540 case 0x4800: 9541 version = RTL_VER_07; 9542 break; 9543 case 0x6000: 9544 version = RTL_VER_08; 9545 break; 9546 case 0x6010: 9547 version = RTL_VER_09; 9548 break; 9549 case 0x7010: 9550 version = RTL_TEST_01; 9551 break; 9552 case 0x7020: 9553 version = RTL_VER_10; 9554 break; 9555 case 0x7030: 9556 version = RTL_VER_11; 9557 break; 9558 case 0x7400: 9559 version = RTL_VER_12; 9560 break; 9561 case 0x7410: 9562 version = RTL_VER_13; 9563 break; 9564 case 0x6400: 9565 version = RTL_VER_14; 9566 break; 9567 case 0x7420: 9568 version = RTL_VER_15; 9569 break; 9570 default: 9571 version = RTL_VER_UNKNOWN; 9572 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data); 9573 break; 9574 } 9575 9576 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); 9577 9578 return version; 9579 } 9580 EXPORT_SYMBOL_GPL(rtl8152_get_version); 9581 9582 static bool rtl8152_supports_lenovo_macpassthru(struct usb_device *udev) 9583 { 9584 int parent_vendor_id = le16_to_cpu(udev->parent->descriptor.idVendor); 9585 int product_id = le16_to_cpu(udev->descriptor.idProduct); 9586 int vendor_id = le16_to_cpu(udev->descriptor.idVendor); 9587 9588 if (vendor_id == VENDOR_ID_LENOVO) { 9589 switch (product_id) { 9590 case DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB: 9591 case DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK: 9592 case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2: 9593 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2: 9594 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3: 9595 case DEVICE_ID_THINKPAD_USB_C_DONGLE: 9596 return 1; 9597 } 9598 } else if (vendor_id == VENDOR_ID_REALTEK && parent_vendor_id == VENDOR_ID_LENOVO) { 9599 switch (product_id) { 9600 case 0x8153: 9601 return 1; 9602 } 9603 } 9604 return 0; 9605 } 9606 9607 static int rtl8152_probe(struct usb_interface *intf, 9608 const struct usb_device_id *id) 9609 { 9610 struct usb_device *udev = interface_to_usbdev(intf); 9611 u8 version = rtl8152_get_version(intf); 9612 struct r8152 *tp; 9613 struct net_device *netdev; 9614 int ret; 9615 9616 if (version == RTL_VER_UNKNOWN) 9617 return -ENODEV; 9618 9619 if (!rtl_vendor_mode(intf)) 9620 return -ENODEV; 9621 9622 usb_reset_device(udev); 9623 netdev = alloc_etherdev(sizeof(struct r8152)); 9624 if (!netdev) { 9625 dev_err(&intf->dev, "Out of memory\n"); 9626 return -ENOMEM; 9627 } 9628 9629 SET_NETDEV_DEV(netdev, &intf->dev); 9630 tp = netdev_priv(netdev); 9631 tp->msg_enable = 0x7FFF; 9632 9633 tp->udev = udev; 9634 tp->netdev = netdev; 9635 tp->intf = intf; 9636 tp->version = version; 9637 9638 tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0); 9639 tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0); 9640 tp->pipe_in = usb_rcvbulkpipe(udev, 1); 9641 tp->pipe_out = usb_sndbulkpipe(udev, 2); 9642 tp->pipe_intr = usb_rcvintpipe(udev, 3); 9643 9644 switch (version) { 9645 case RTL_VER_01: 9646 case RTL_VER_02: 9647 case RTL_VER_07: 9648 tp->mii.supports_gmii = 0; 9649 break; 9650 default: 9651 tp->mii.supports_gmii = 1; 9652 break; 9653 } 9654 9655 ret = rtl_ops_init(tp); 9656 if (ret) 9657 goto out; 9658 9659 rtl_fw_init(tp); 9660 9661 mutex_init(&tp->control); 9662 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); 9663 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t); 9664 tasklet_setup(&tp->tx_tl, bottom_half); 9665 tasklet_disable(&tp->tx_tl); 9666 9667 netdev->netdev_ops = &rtl8152_netdev_ops; 9668 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; 9669 9670 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 9671 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | 9672 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | 9673 NETIF_F_HW_VLAN_CTAG_TX; 9674 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 9675 NETIF_F_TSO | NETIF_F_FRAGLIST | 9676 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | 9677 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; 9678 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | 9679 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | 9680 NETIF_F_IPV6_CSUM | NETIF_F_TSO6; 9681 9682 if (tp->version == RTL_VER_01) { 9683 netdev->features &= ~NETIF_F_RXCSUM; 9684 netdev->hw_features &= ~NETIF_F_RXCSUM; 9685 } 9686 9687 tp->lenovo_macpassthru = rtl8152_supports_lenovo_macpassthru(udev); 9688 9689 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && 9690 (!strcmp(udev->serial, "000001000000") || 9691 !strcmp(udev->serial, "000002000000"))) { 9692 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); 9693 tp->dell_tb_rx_agg_bug = 1; 9694 } 9695 9696 netdev->ethtool_ops = &ops; 9697 netif_set_tso_max_size(netdev, RTL_LIMITED_TSO_SIZE); 9698 9699 /* MTU range: 68 - 1500 or 9194 */ 9700 netdev->min_mtu = ETH_MIN_MTU; 9701 switch (tp->version) { 9702 case RTL_VER_03: 9703 case RTL_VER_04: 9704 case RTL_VER_05: 9705 case RTL_VER_06: 9706 case RTL_VER_08: 9707 case RTL_VER_09: 9708 case RTL_VER_14: 9709 netdev->max_mtu = size_to_mtu(9 * 1024); 9710 break; 9711 case RTL_VER_10: 9712 case RTL_VER_11: 9713 netdev->max_mtu = size_to_mtu(15 * 1024); 9714 break; 9715 case RTL_VER_12: 9716 case RTL_VER_13: 9717 case RTL_VER_15: 9718 netdev->max_mtu = size_to_mtu(16 * 1024); 9719 break; 9720 case RTL_VER_01: 9721 case RTL_VER_02: 9722 case RTL_VER_07: 9723 default: 9724 netdev->max_mtu = ETH_DATA_LEN; 9725 break; 9726 } 9727 9728 tp->mii.dev = netdev; 9729 tp->mii.mdio_read = read_mii_word; 9730 tp->mii.mdio_write = write_mii_word; 9731 tp->mii.phy_id_mask = 0x3f; 9732 tp->mii.reg_num_mask = 0x1f; 9733 tp->mii.phy_id = R8152_PHY_ID; 9734 9735 tp->autoneg = AUTONEG_ENABLE; 9736 tp->speed = SPEED_100; 9737 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 9738 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 9739 if (tp->mii.supports_gmii) { 9740 if (tp->support_2500full && 9741 tp->udev->speed >= USB_SPEED_SUPER) { 9742 tp->speed = SPEED_2500; 9743 tp->advertising |= RTL_ADVERTISED_2500_FULL; 9744 } else { 9745 tp->speed = SPEED_1000; 9746 } 9747 tp->advertising |= RTL_ADVERTISED_1000_FULL; 9748 } 9749 tp->duplex = DUPLEX_FULL; 9750 9751 tp->rx_copybreak = RTL8152_RXFG_HEADSZ; 9752 tp->rx_pending = 10 * RTL8152_MAX_RX; 9753 9754 intf->needs_remote_wakeup = 1; 9755 9756 if (!rtl_can_wakeup(tp)) 9757 __rtl_set_wol(tp, 0); 9758 else 9759 tp->saved_wolopts = __rtl_get_wol(tp); 9760 9761 tp->rtl_ops.init(tp); 9762 #if IS_BUILTIN(CONFIG_USB_RTL8152) 9763 /* Retry in case request_firmware() is not ready yet. */ 9764 tp->rtl_fw.retry = true; 9765 #endif 9766 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 9767 set_ethernet_addr(tp, false); 9768 9769 usb_set_intfdata(intf, tp); 9770 9771 netif_napi_add_weight(netdev, &tp->napi, r8152_poll, 9772 tp->support_2500full ? 256 : 64); 9773 9774 ret = register_netdev(netdev); 9775 if (ret != 0) { 9776 dev_err(&intf->dev, "couldn't register the device\n"); 9777 goto out1; 9778 } 9779 9780 if (tp->saved_wolopts) 9781 device_set_wakeup_enable(&udev->dev, true); 9782 else 9783 device_set_wakeup_enable(&udev->dev, false); 9784 9785 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); 9786 9787 return 0; 9788 9789 out1: 9790 tasklet_kill(&tp->tx_tl); 9791 usb_set_intfdata(intf, NULL); 9792 out: 9793 free_netdev(netdev); 9794 return ret; 9795 } 9796 9797 static void rtl8152_disconnect(struct usb_interface *intf) 9798 { 9799 struct r8152 *tp = usb_get_intfdata(intf); 9800 9801 usb_set_intfdata(intf, NULL); 9802 if (tp) { 9803 rtl_set_unplug(tp); 9804 9805 unregister_netdev(tp->netdev); 9806 tasklet_kill(&tp->tx_tl); 9807 cancel_delayed_work_sync(&tp->hw_phy_work); 9808 if (tp->rtl_ops.unload) 9809 tp->rtl_ops.unload(tp); 9810 rtl8152_release_firmware(tp); 9811 free_netdev(tp->netdev); 9812 } 9813 } 9814 9815 #define REALTEK_USB_DEVICE(vend, prod) { \ 9816 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC), \ 9817 }, \ 9818 { \ 9819 USB_DEVICE_AND_INTERFACE_INFO(vend, prod, USB_CLASS_COMM, \ 9820 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), \ 9821 } 9822 9823 /* table of devices that work with this driver */ 9824 static const struct usb_device_id rtl8152_table[] = { 9825 /* Realtek */ 9826 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050), 9827 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8053), 9828 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152), 9829 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153), 9830 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8155), 9831 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8156), 9832 9833 /* Microsoft */ 9834 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab), 9835 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6), 9836 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927), 9837 REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101), 9838 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f), 9839 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3054), 9840 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062), 9841 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069), 9842 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082), 9843 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205), 9844 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c), 9845 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214), 9846 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x721e), 9847 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387), 9848 REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041), 9849 REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff), 9850 REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601), 9851 {} 9852 }; 9853 9854 MODULE_DEVICE_TABLE(usb, rtl8152_table); 9855 9856 static struct usb_driver rtl8152_driver = { 9857 .name = MODULENAME, 9858 .id_table = rtl8152_table, 9859 .probe = rtl8152_probe, 9860 .disconnect = rtl8152_disconnect, 9861 .suspend = rtl8152_suspend, 9862 .resume = rtl8152_resume, 9863 .reset_resume = rtl8152_reset_resume, 9864 .pre_reset = rtl8152_pre_reset, 9865 .post_reset = rtl8152_post_reset, 9866 .supports_autosuspend = 1, 9867 .disable_hub_initiated_lpm = 1, 9868 }; 9869 9870 module_usb_driver(rtl8152_driver); 9871 9872 MODULE_AUTHOR(DRIVER_AUTHOR); 9873 MODULE_DESCRIPTION(DRIVER_DESC); 9874 MODULE_LICENSE("GPL"); 9875 MODULE_VERSION(DRIVER_VERSION); 9876