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