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