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