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_INACCESSIBLE = 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, USB_CTRL_GET_TIMEOUT); 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, USB_CTRL_SET_TIMEOUT); 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_INACCESSIBLE, &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_INACCESSIBLE, &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_INACCESSIBLE, &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_INACCESSIBLE, &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_INACCESSIBLE, &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_INACCESSIBLE, &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_INACCESSIBLE, &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_INACCESSIBLE, &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)) 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 list_del_init(cursor); 2469 2470 agg = list_entry(cursor, struct rx_agg, list); 2471 urb = agg->urb; 2472 if (urb->status != 0 || urb->actual_length < ETH_ZLEN) 2473 goto submit; 2474 2475 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC); 2476 2477 rx_desc = agg->buffer; 2478 rx_data = agg->buffer; 2479 len_used += sizeof(struct rx_desc); 2480 2481 while (urb->actual_length > len_used) { 2482 struct net_device *netdev = tp->netdev; 2483 struct net_device_stats *stats = &netdev->stats; 2484 unsigned int pkt_len, rx_frag_head_sz; 2485 struct sk_buff *skb; 2486 2487 /* limit the skb numbers for rx_queue */ 2488 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000)) 2489 break; 2490 2491 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; 2492 if (pkt_len < ETH_ZLEN) 2493 break; 2494 2495 len_used += pkt_len; 2496 if (urb->actual_length < len_used) 2497 break; 2498 2499 pkt_len -= ETH_FCS_LEN; 2500 rx_data += sizeof(struct rx_desc); 2501 2502 if (!agg_free || tp->rx_copybreak > pkt_len) 2503 rx_frag_head_sz = pkt_len; 2504 else 2505 rx_frag_head_sz = tp->rx_copybreak; 2506 2507 skb = napi_alloc_skb(napi, rx_frag_head_sz); 2508 if (!skb) { 2509 stats->rx_dropped++; 2510 goto find_next_rx; 2511 } 2512 2513 skb->ip_summed = r8152_rx_csum(tp, rx_desc); 2514 memcpy(skb->data, rx_data, rx_frag_head_sz); 2515 skb_put(skb, rx_frag_head_sz); 2516 pkt_len -= rx_frag_head_sz; 2517 rx_data += rx_frag_head_sz; 2518 if (pkt_len) { 2519 skb_add_rx_frag(skb, 0, agg->page, 2520 agg_offset(agg, rx_data), 2521 pkt_len, 2522 SKB_DATA_ALIGN(pkt_len)); 2523 get_page(agg->page); 2524 } 2525 2526 skb->protocol = eth_type_trans(skb, netdev); 2527 rtl_rx_vlan_tag(rx_desc, skb); 2528 if (work_done < budget) { 2529 work_done++; 2530 stats->rx_packets++; 2531 stats->rx_bytes += skb->len; 2532 napi_gro_receive(napi, skb); 2533 } else { 2534 __skb_queue_tail(&tp->rx_queue, skb); 2535 } 2536 2537 find_next_rx: 2538 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN); 2539 rx_desc = (struct rx_desc *)rx_data; 2540 len_used = agg_offset(agg, rx_data); 2541 len_used += sizeof(struct rx_desc); 2542 } 2543 2544 WARN_ON(!agg_free && page_count(agg->page) > 1); 2545 2546 if (agg_free) { 2547 spin_lock_irqsave(&tp->rx_lock, flags); 2548 if (page_count(agg->page) == 1) { 2549 list_add(&agg_free->list, &tp->rx_used); 2550 } else { 2551 list_add_tail(&agg->list, &tp->rx_used); 2552 agg = agg_free; 2553 urb = agg->urb; 2554 } 2555 spin_unlock_irqrestore(&tp->rx_lock, flags); 2556 } 2557 2558 submit: 2559 if (!ret) { 2560 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); 2561 } else { 2562 urb->actual_length = 0; 2563 list_add_tail(&agg->list, next); 2564 } 2565 } 2566 2567 if (!list_empty(&rx_queue)) { 2568 spin_lock_irqsave(&tp->rx_lock, flags); 2569 list_splice_tail(&rx_queue, &tp->rx_done); 2570 spin_unlock_irqrestore(&tp->rx_lock, flags); 2571 } 2572 2573 out1: 2574 return work_done; 2575 } 2576 2577 static void tx_bottom(struct r8152 *tp) 2578 { 2579 int res; 2580 2581 do { 2582 struct net_device *netdev = tp->netdev; 2583 struct tx_agg *agg; 2584 2585 if (skb_queue_empty(&tp->tx_queue)) 2586 break; 2587 2588 agg = r8152_get_tx_agg(tp); 2589 if (!agg) 2590 break; 2591 2592 res = r8152_tx_agg_fill(tp, agg); 2593 if (!res) 2594 continue; 2595 2596 if (res == -ENODEV) { 2597 rtl_set_unplug(tp); 2598 netif_device_detach(netdev); 2599 } else { 2600 struct net_device_stats *stats = &netdev->stats; 2601 unsigned long flags; 2602 2603 netif_warn(tp, tx_err, netdev, 2604 "failed tx_urb %d\n", res); 2605 stats->tx_dropped += agg->skb_num; 2606 2607 spin_lock_irqsave(&tp->tx_lock, flags); 2608 list_add_tail(&agg->list, &tp->tx_free); 2609 spin_unlock_irqrestore(&tp->tx_lock, flags); 2610 } 2611 } while (res == 0); 2612 } 2613 2614 static void bottom_half(struct tasklet_struct *t) 2615 { 2616 struct r8152 *tp = from_tasklet(tp, t, tx_tl); 2617 2618 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 2619 return; 2620 2621 if (!test_bit(WORK_ENABLE, &tp->flags)) 2622 return; 2623 2624 /* When link down, the driver would cancel all bulks. */ 2625 /* This avoid the re-submitting bulk */ 2626 if (!netif_carrier_ok(tp->netdev)) 2627 return; 2628 2629 clear_bit(SCHEDULE_TASKLET, &tp->flags); 2630 2631 tx_bottom(tp); 2632 } 2633 2634 static int r8152_poll(struct napi_struct *napi, int budget) 2635 { 2636 struct r8152 *tp = container_of(napi, struct r8152, napi); 2637 int work_done; 2638 2639 if (!budget) 2640 return 0; 2641 2642 work_done = rx_bottom(tp, budget); 2643 2644 if (work_done < budget) { 2645 if (!napi_complete_done(napi, work_done)) 2646 goto out; 2647 if (!list_empty(&tp->rx_done)) 2648 napi_schedule(napi); 2649 } 2650 2651 out: 2652 return work_done; 2653 } 2654 2655 static 2656 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) 2657 { 2658 int ret; 2659 2660 /* The rx would be stopped, so skip submitting */ 2661 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) || 2662 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) 2663 return 0; 2664 2665 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in, 2666 agg->buffer, tp->rx_buf_sz, 2667 (usb_complete_t)read_bulk_callback, agg); 2668 2669 ret = usb_submit_urb(agg->urb, mem_flags); 2670 if (ret == -ENODEV) { 2671 rtl_set_unplug(tp); 2672 netif_device_detach(tp->netdev); 2673 } else if (ret) { 2674 struct urb *urb = agg->urb; 2675 unsigned long flags; 2676 2677 urb->actual_length = 0; 2678 spin_lock_irqsave(&tp->rx_lock, flags); 2679 list_add_tail(&agg->list, &tp->rx_done); 2680 spin_unlock_irqrestore(&tp->rx_lock, flags); 2681 2682 netif_err(tp, rx_err, tp->netdev, 2683 "Couldn't submit rx[%p], ret = %d\n", agg, ret); 2684 2685 napi_schedule(&tp->napi); 2686 } 2687 2688 return ret; 2689 } 2690 2691 static void rtl_drop_queued_tx(struct r8152 *tp) 2692 { 2693 struct net_device_stats *stats = &tp->netdev->stats; 2694 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; 2695 struct sk_buff *skb; 2696 2697 if (skb_queue_empty(tx_queue)) 2698 return; 2699 2700 __skb_queue_head_init(&skb_head); 2701 spin_lock_bh(&tx_queue->lock); 2702 skb_queue_splice_init(tx_queue, &skb_head); 2703 spin_unlock_bh(&tx_queue->lock); 2704 2705 while ((skb = __skb_dequeue(&skb_head))) { 2706 dev_kfree_skb(skb); 2707 stats->tx_dropped++; 2708 } 2709 } 2710 2711 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue) 2712 { 2713 struct r8152 *tp = netdev_priv(netdev); 2714 2715 netif_warn(tp, tx_err, netdev, "Tx timeout\n"); 2716 2717 usb_queue_reset_device(tp->intf); 2718 } 2719 2720 static void rtl8152_set_rx_mode(struct net_device *netdev) 2721 { 2722 struct r8152 *tp = netdev_priv(netdev); 2723 2724 if (netif_carrier_ok(netdev)) { 2725 set_bit(RTL8152_SET_RX_MODE, &tp->flags); 2726 schedule_delayed_work(&tp->schedule, 0); 2727 } 2728 } 2729 2730 static void _rtl8152_set_rx_mode(struct net_device *netdev) 2731 { 2732 struct r8152 *tp = netdev_priv(netdev); 2733 u32 mc_filter[2]; /* Multicast hash filter */ 2734 __le32 tmp[2]; 2735 u32 ocp_data; 2736 2737 netif_stop_queue(netdev); 2738 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 2739 ocp_data &= ~RCR_ACPT_ALL; 2740 ocp_data |= RCR_AB | RCR_APM; 2741 2742 if (netdev->flags & IFF_PROMISC) { 2743 /* Unconditionally log net taps. */ 2744 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); 2745 ocp_data |= RCR_AM | RCR_AAP; 2746 mc_filter[1] = 0xffffffff; 2747 mc_filter[0] = 0xffffffff; 2748 } else if ((netdev->flags & IFF_MULTICAST && 2749 netdev_mc_count(netdev) > multicast_filter_limit) || 2750 (netdev->flags & IFF_ALLMULTI)) { 2751 /* Too many to filter perfectly -- accept all multicasts. */ 2752 ocp_data |= RCR_AM; 2753 mc_filter[1] = 0xffffffff; 2754 mc_filter[0] = 0xffffffff; 2755 } else { 2756 mc_filter[1] = 0; 2757 mc_filter[0] = 0; 2758 2759 if (netdev->flags & IFF_MULTICAST) { 2760 struct netdev_hw_addr *ha; 2761 2762 netdev_for_each_mc_addr(ha, netdev) { 2763 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; 2764 2765 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); 2766 ocp_data |= RCR_AM; 2767 } 2768 } 2769 } 2770 2771 tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); 2772 tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); 2773 2774 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); 2775 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 2776 netif_wake_queue(netdev); 2777 } 2778 2779 static netdev_features_t 2780 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, 2781 netdev_features_t features) 2782 { 2783 u32 mss = skb_shinfo(skb)->gso_size; 2784 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; 2785 2786 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && 2787 skb_transport_offset(skb) > max_offset) 2788 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 2789 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz) 2790 features &= ~NETIF_F_GSO_MASK; 2791 2792 return features; 2793 } 2794 2795 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, 2796 struct net_device *netdev) 2797 { 2798 struct r8152 *tp = netdev_priv(netdev); 2799 2800 skb_tx_timestamp(skb); 2801 2802 skb_queue_tail(&tp->tx_queue, skb); 2803 2804 if (!list_empty(&tp->tx_free)) { 2805 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { 2806 set_bit(SCHEDULE_TASKLET, &tp->flags); 2807 schedule_delayed_work(&tp->schedule, 0); 2808 } else { 2809 usb_mark_last_busy(tp->udev); 2810 tasklet_schedule(&tp->tx_tl); 2811 } 2812 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { 2813 netif_stop_queue(netdev); 2814 } 2815 2816 return NETDEV_TX_OK; 2817 } 2818 2819 static void r8152b_reset_packet_filter(struct r8152 *tp) 2820 { 2821 u32 ocp_data; 2822 2823 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); 2824 ocp_data &= ~FMC_FCR_MCU_EN; 2825 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 2826 ocp_data |= FMC_FCR_MCU_EN; 2827 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 2828 } 2829 2830 static void rtl8152_nic_reset(struct r8152 *tp) 2831 { 2832 u32 ocp_data; 2833 int i; 2834 2835 switch (tp->version) { 2836 case RTL_TEST_01: 2837 case RTL_VER_10: 2838 case RTL_VER_11: 2839 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 2840 ocp_data &= ~CR_TE; 2841 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 2842 2843 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET); 2844 ocp_data &= ~BMU_RESET_EP_IN; 2845 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 2846 2847 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 2848 ocp_data |= CDC_ECM_EN; 2849 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 2850 2851 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 2852 ocp_data &= ~CR_RE; 2853 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 2854 2855 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET); 2856 ocp_data |= BMU_RESET_EP_IN; 2857 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 2858 2859 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 2860 ocp_data &= ~CDC_ECM_EN; 2861 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 2862 break; 2863 2864 default: 2865 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); 2866 2867 for (i = 0; i < 1000; i++) { 2868 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) 2869 break; 2870 usleep_range(100, 400); 2871 } 2872 break; 2873 } 2874 } 2875 2876 static void set_tx_qlen(struct r8152 *tp) 2877 { 2878 tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc)); 2879 } 2880 2881 static inline u16 rtl8152_get_speed(struct r8152 *tp) 2882 { 2883 return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); 2884 } 2885 2886 static void rtl_eee_plus_en(struct r8152 *tp, bool enable) 2887 { 2888 u32 ocp_data; 2889 2890 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); 2891 if (enable) 2892 ocp_data |= EEEP_CR_EEEP_TX; 2893 else 2894 ocp_data &= ~EEEP_CR_EEEP_TX; 2895 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); 2896 } 2897 2898 static void rtl_set_eee_plus(struct r8152 *tp) 2899 { 2900 if (rtl8152_get_speed(tp) & _10bps) 2901 rtl_eee_plus_en(tp, true); 2902 else 2903 rtl_eee_plus_en(tp, false); 2904 } 2905 2906 static void rxdy_gated_en(struct r8152 *tp, bool enable) 2907 { 2908 u32 ocp_data; 2909 2910 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); 2911 if (enable) 2912 ocp_data |= RXDY_GATED_EN; 2913 else 2914 ocp_data &= ~RXDY_GATED_EN; 2915 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); 2916 } 2917 2918 static int rtl_start_rx(struct r8152 *tp) 2919 { 2920 struct rx_agg *agg, *agg_next; 2921 struct list_head tmp_list; 2922 unsigned long flags; 2923 int ret = 0, i = 0; 2924 2925 INIT_LIST_HEAD(&tmp_list); 2926 2927 spin_lock_irqsave(&tp->rx_lock, flags); 2928 2929 INIT_LIST_HEAD(&tp->rx_done); 2930 INIT_LIST_HEAD(&tp->rx_used); 2931 2932 list_splice_init(&tp->rx_info, &tmp_list); 2933 2934 spin_unlock_irqrestore(&tp->rx_lock, flags); 2935 2936 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 2937 INIT_LIST_HEAD(&agg->list); 2938 2939 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */ 2940 if (++i > RTL8152_MAX_RX) { 2941 spin_lock_irqsave(&tp->rx_lock, flags); 2942 list_add_tail(&agg->list, &tp->rx_used); 2943 spin_unlock_irqrestore(&tp->rx_lock, flags); 2944 } else if (unlikely(ret < 0)) { 2945 spin_lock_irqsave(&tp->rx_lock, flags); 2946 list_add_tail(&agg->list, &tp->rx_done); 2947 spin_unlock_irqrestore(&tp->rx_lock, flags); 2948 } else { 2949 ret = r8152_submit_rx(tp, agg, GFP_KERNEL); 2950 } 2951 } 2952 2953 spin_lock_irqsave(&tp->rx_lock, flags); 2954 WARN_ON(!list_empty(&tp->rx_info)); 2955 list_splice(&tmp_list, &tp->rx_info); 2956 spin_unlock_irqrestore(&tp->rx_lock, flags); 2957 2958 return ret; 2959 } 2960 2961 static int rtl_stop_rx(struct r8152 *tp) 2962 { 2963 struct rx_agg *agg, *agg_next; 2964 struct list_head tmp_list; 2965 unsigned long flags; 2966 2967 INIT_LIST_HEAD(&tmp_list); 2968 2969 /* The usb_kill_urb() couldn't be used in atomic. 2970 * Therefore, move the list of rx_info to a tmp one. 2971 * Then, list_for_each_entry_safe could be used without 2972 * spin lock. 2973 */ 2974 2975 spin_lock_irqsave(&tp->rx_lock, flags); 2976 list_splice_init(&tp->rx_info, &tmp_list); 2977 spin_unlock_irqrestore(&tp->rx_lock, flags); 2978 2979 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { 2980 /* At least RTL8152_MAX_RX rx_agg have the page_count being 2981 * equal to 1, so the other ones could be freed safely. 2982 */ 2983 if (page_count(agg->page) > 1) 2984 free_rx_agg(tp, agg); 2985 else 2986 usb_kill_urb(agg->urb); 2987 } 2988 2989 /* Move back the list of temp to the rx_info */ 2990 spin_lock_irqsave(&tp->rx_lock, flags); 2991 WARN_ON(!list_empty(&tp->rx_info)); 2992 list_splice(&tmp_list, &tp->rx_info); 2993 spin_unlock_irqrestore(&tp->rx_lock, flags); 2994 2995 while (!skb_queue_empty(&tp->rx_queue)) 2996 dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); 2997 2998 return 0; 2999 } 3000 3001 static void rtl_set_ifg(struct r8152 *tp, u16 speed) 3002 { 3003 u32 ocp_data; 3004 3005 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1); 3006 ocp_data &= ~IFG_MASK; 3007 if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) { 3008 ocp_data |= IFG_144NS; 3009 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data); 3010 3011 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 3012 ocp_data &= ~TX10MIDLE_EN; 3013 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 3014 } else { 3015 ocp_data |= IFG_96NS; 3016 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data); 3017 3018 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 3019 ocp_data |= TX10MIDLE_EN; 3020 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 3021 } 3022 } 3023 3024 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) 3025 { 3026 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, 3027 OWN_UPDATE | OWN_CLEAR); 3028 } 3029 3030 static int rtl_enable(struct r8152 *tp) 3031 { 3032 u32 ocp_data; 3033 3034 r8152b_reset_packet_filter(tp); 3035 3036 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 3037 ocp_data |= CR_RE | CR_TE; 3038 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 3039 3040 switch (tp->version) { 3041 case RTL_VER_01: 3042 case RTL_VER_02: 3043 case RTL_VER_03: 3044 case RTL_VER_04: 3045 case RTL_VER_05: 3046 case RTL_VER_06: 3047 case RTL_VER_07: 3048 break; 3049 default: 3050 r8153b_rx_agg_chg_indicate(tp); 3051 break; 3052 } 3053 3054 rxdy_gated_en(tp, false); 3055 3056 return 0; 3057 } 3058 3059 static int rtl8152_enable(struct r8152 *tp) 3060 { 3061 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3062 return -ENODEV; 3063 3064 set_tx_qlen(tp); 3065 rtl_set_eee_plus(tp); 3066 3067 return rtl_enable(tp); 3068 } 3069 3070 static void r8153_set_rx_early_timeout(struct r8152 *tp) 3071 { 3072 u32 ocp_data = tp->coalesce / 8; 3073 3074 switch (tp->version) { 3075 case RTL_VER_03: 3076 case RTL_VER_04: 3077 case RTL_VER_05: 3078 case RTL_VER_06: 3079 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3080 ocp_data); 3081 break; 3082 3083 case RTL_VER_08: 3084 case RTL_VER_09: 3085 case RTL_VER_14: 3086 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout 3087 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns. 3088 */ 3089 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3090 128 / 8); 3091 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, 3092 ocp_data); 3093 break; 3094 3095 case RTL_VER_10: 3096 case RTL_VER_11: 3097 case RTL_VER_12: 3098 case RTL_VER_13: 3099 case RTL_VER_15: 3100 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 3101 640 / 8); 3102 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, 3103 ocp_data); 3104 break; 3105 3106 default: 3107 break; 3108 } 3109 } 3110 3111 static void r8153_set_rx_early_size(struct r8152 *tp) 3112 { 3113 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu); 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_SIZE, 3121 ocp_data / 4); 3122 break; 3123 case RTL_VER_08: 3124 case RTL_VER_09: 3125 case RTL_VER_14: 3126 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3127 ocp_data / 8); 3128 break; 3129 case RTL_TEST_01: 3130 case RTL_VER_10: 3131 case RTL_VER_11: 3132 case RTL_VER_12: 3133 case RTL_VER_13: 3134 case RTL_VER_15: 3135 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, 3136 ocp_data / 8); 3137 break; 3138 default: 3139 WARN_ON_ONCE(1); 3140 break; 3141 } 3142 } 3143 3144 static int rtl8153_enable(struct r8152 *tp) 3145 { 3146 u32 ocp_data; 3147 3148 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3149 return -ENODEV; 3150 3151 set_tx_qlen(tp); 3152 rtl_set_eee_plus(tp); 3153 r8153_set_rx_early_timeout(tp); 3154 r8153_set_rx_early_size(tp); 3155 3156 rtl_set_ifg(tp, rtl8152_get_speed(tp)); 3157 3158 switch (tp->version) { 3159 case RTL_VER_09: 3160 case RTL_VER_14: 3161 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 3162 ocp_data &= ~FC_PATCH_TASK; 3163 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 3164 usleep_range(1000, 2000); 3165 ocp_data |= FC_PATCH_TASK; 3166 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 3167 break; 3168 default: 3169 break; 3170 } 3171 3172 return rtl_enable(tp); 3173 } 3174 3175 static void rtl_disable(struct r8152 *tp) 3176 { 3177 u32 ocp_data; 3178 int i; 3179 3180 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 3181 rtl_drop_queued_tx(tp); 3182 return; 3183 } 3184 3185 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 3186 ocp_data &= ~RCR_ACPT_ALL; 3187 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 3188 3189 rtl_drop_queued_tx(tp); 3190 3191 for (i = 0; i < RTL8152_MAX_TX; i++) 3192 usb_kill_urb(tp->tx_info[i].urb); 3193 3194 rxdy_gated_en(tp, true); 3195 3196 for (i = 0; i < 1000; i++) { 3197 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 3198 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) 3199 break; 3200 usleep_range(1000, 2000); 3201 } 3202 3203 for (i = 0; i < 1000; i++) { 3204 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) 3205 break; 3206 usleep_range(1000, 2000); 3207 } 3208 3209 rtl_stop_rx(tp); 3210 3211 rtl8152_nic_reset(tp); 3212 } 3213 3214 static void r8152_power_cut_en(struct r8152 *tp, bool enable) 3215 { 3216 u32 ocp_data; 3217 3218 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); 3219 if (enable) 3220 ocp_data |= POWER_CUT; 3221 else 3222 ocp_data &= ~POWER_CUT; 3223 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); 3224 3225 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); 3226 ocp_data &= ~RESUME_INDICATE; 3227 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); 3228 } 3229 3230 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) 3231 { 3232 u32 ocp_data; 3233 3234 switch (tp->version) { 3235 case RTL_VER_01: 3236 case RTL_VER_02: 3237 case RTL_VER_03: 3238 case RTL_VER_04: 3239 case RTL_VER_05: 3240 case RTL_VER_06: 3241 case RTL_VER_07: 3242 case RTL_VER_08: 3243 case RTL_VER_09: 3244 case RTL_VER_14: 3245 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); 3246 if (enable) 3247 ocp_data |= CPCR_RX_VLAN; 3248 else 3249 ocp_data &= ~CPCR_RX_VLAN; 3250 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); 3251 break; 3252 3253 case RTL_TEST_01: 3254 case RTL_VER_10: 3255 case RTL_VER_11: 3256 case RTL_VER_12: 3257 case RTL_VER_13: 3258 case RTL_VER_15: 3259 default: 3260 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1); 3261 if (enable) 3262 ocp_data |= OUTER_VLAN | INNER_VLAN; 3263 else 3264 ocp_data &= ~(OUTER_VLAN | INNER_VLAN); 3265 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data); 3266 break; 3267 } 3268 } 3269 3270 static int rtl8152_set_features(struct net_device *dev, 3271 netdev_features_t features) 3272 { 3273 netdev_features_t changed = features ^ dev->features; 3274 struct r8152 *tp = netdev_priv(dev); 3275 int ret; 3276 3277 ret = usb_autopm_get_interface(tp->intf); 3278 if (ret < 0) 3279 goto out; 3280 3281 mutex_lock(&tp->control); 3282 3283 if (changed & NETIF_F_HW_VLAN_CTAG_RX) { 3284 if (features & NETIF_F_HW_VLAN_CTAG_RX) 3285 rtl_rx_vlan_en(tp, true); 3286 else 3287 rtl_rx_vlan_en(tp, false); 3288 } 3289 3290 mutex_unlock(&tp->control); 3291 3292 usb_autopm_put_interface(tp->intf); 3293 3294 out: 3295 return ret; 3296 } 3297 3298 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) 3299 3300 static u32 __rtl_get_wol(struct r8152 *tp) 3301 { 3302 u32 ocp_data; 3303 u32 wolopts = 0; 3304 3305 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3306 if (ocp_data & LINK_ON_WAKE_EN) 3307 wolopts |= WAKE_PHY; 3308 3309 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 3310 if (ocp_data & UWF_EN) 3311 wolopts |= WAKE_UCAST; 3312 if (ocp_data & BWF_EN) 3313 wolopts |= WAKE_BCAST; 3314 if (ocp_data & MWF_EN) 3315 wolopts |= WAKE_MCAST; 3316 3317 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 3318 if (ocp_data & MAGIC_EN) 3319 wolopts |= WAKE_MAGIC; 3320 3321 return wolopts; 3322 } 3323 3324 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) 3325 { 3326 u32 ocp_data; 3327 3328 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3329 3330 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3331 ocp_data &= ~LINK_ON_WAKE_EN; 3332 if (wolopts & WAKE_PHY) 3333 ocp_data |= LINK_ON_WAKE_EN; 3334 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3335 3336 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 3337 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN); 3338 if (wolopts & WAKE_UCAST) 3339 ocp_data |= UWF_EN; 3340 if (wolopts & WAKE_BCAST) 3341 ocp_data |= BWF_EN; 3342 if (wolopts & WAKE_MCAST) 3343 ocp_data |= MWF_EN; 3344 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); 3345 3346 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3347 3348 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 3349 ocp_data &= ~MAGIC_EN; 3350 if (wolopts & WAKE_MAGIC) 3351 ocp_data |= MAGIC_EN; 3352 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); 3353 3354 if (wolopts & WAKE_ANY) 3355 device_set_wakeup_enable(&tp->udev->dev, true); 3356 else 3357 device_set_wakeup_enable(&tp->udev->dev, false); 3358 } 3359 3360 static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable) 3361 { 3362 u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); 3363 3364 /* MAC clock speed down */ 3365 if (enable) 3366 ocp_data |= MAC_CLK_SPDWN_EN; 3367 else 3368 ocp_data &= ~MAC_CLK_SPDWN_EN; 3369 3370 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); 3371 } 3372 3373 static void r8156_mac_clk_spd(struct r8152 *tp, bool enable) 3374 { 3375 u32 ocp_data; 3376 3377 /* MAC clock speed down */ 3378 if (enable) { 3379 /* aldps_spdwn_ratio, tp10_spdwn_ratio */ 3380 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 3381 0x0403); 3382 3383 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); 3384 ocp_data &= ~EEE_SPDWN_RATIO_MASK; 3385 ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */ 3386 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); 3387 } else { 3388 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); 3389 ocp_data &= ~MAC_CLK_SPDWN_EN; 3390 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); 3391 } 3392 } 3393 3394 static void r8153_u1u2en(struct r8152 *tp, bool enable) 3395 { 3396 u8 u1u2[8]; 3397 3398 if (enable) 3399 memset(u1u2, 0xff, sizeof(u1u2)); 3400 else 3401 memset(u1u2, 0x00, sizeof(u1u2)); 3402 3403 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); 3404 } 3405 3406 static void r8153b_u1u2en(struct r8152 *tp, bool enable) 3407 { 3408 u32 ocp_data; 3409 3410 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); 3411 if (enable) 3412 ocp_data |= LPM_U1U2_EN; 3413 else 3414 ocp_data &= ~LPM_U1U2_EN; 3415 3416 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); 3417 } 3418 3419 static void r8153_u2p3en(struct r8152 *tp, bool enable) 3420 { 3421 u32 ocp_data; 3422 3423 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); 3424 if (enable) 3425 ocp_data |= U2P3_ENABLE; 3426 else 3427 ocp_data &= ~U2P3_ENABLE; 3428 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); 3429 } 3430 3431 static void r8153b_ups_flags(struct r8152 *tp) 3432 { 3433 u32 ups_flags = 0; 3434 3435 if (tp->ups_info.green) 3436 ups_flags |= UPS_FLAGS_EN_GREEN; 3437 3438 if (tp->ups_info.aldps) 3439 ups_flags |= UPS_FLAGS_EN_ALDPS; 3440 3441 if (tp->ups_info.eee) 3442 ups_flags |= UPS_FLAGS_EN_EEE; 3443 3444 if (tp->ups_info.flow_control) 3445 ups_flags |= UPS_FLAGS_EN_FLOW_CTR; 3446 3447 if (tp->ups_info.eee_ckdiv) 3448 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; 3449 3450 if (tp->ups_info.eee_cmod_lv) 3451 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN; 3452 3453 if (tp->ups_info.r_tune) 3454 ups_flags |= UPS_FLAGS_R_TUNE; 3455 3456 if (tp->ups_info._10m_ckdiv) 3457 ups_flags |= UPS_FLAGS_EN_10M_CKDIV; 3458 3459 if (tp->ups_info.eee_plloff_100) 3460 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; 3461 3462 if (tp->ups_info.eee_plloff_giga) 3463 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; 3464 3465 if (tp->ups_info._250m_ckdiv) 3466 ups_flags |= UPS_FLAGS_250M_CKDIV; 3467 3468 if (tp->ups_info.ctap_short_off) 3469 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS; 3470 3471 switch (tp->ups_info.speed_duplex) { 3472 case NWAY_10M_HALF: 3473 ups_flags |= ups_flags_speed(1); 3474 break; 3475 case NWAY_10M_FULL: 3476 ups_flags |= ups_flags_speed(2); 3477 break; 3478 case NWAY_100M_HALF: 3479 ups_flags |= ups_flags_speed(3); 3480 break; 3481 case NWAY_100M_FULL: 3482 ups_flags |= ups_flags_speed(4); 3483 break; 3484 case NWAY_1000M_FULL: 3485 ups_flags |= ups_flags_speed(5); 3486 break; 3487 case FORCE_10M_HALF: 3488 ups_flags |= ups_flags_speed(6); 3489 break; 3490 case FORCE_10M_FULL: 3491 ups_flags |= ups_flags_speed(7); 3492 break; 3493 case FORCE_100M_HALF: 3494 ups_flags |= ups_flags_speed(8); 3495 break; 3496 case FORCE_100M_FULL: 3497 ups_flags |= ups_flags_speed(9); 3498 break; 3499 default: 3500 break; 3501 } 3502 3503 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); 3504 } 3505 3506 static void r8156_ups_flags(struct r8152 *tp) 3507 { 3508 u32 ups_flags = 0; 3509 3510 if (tp->ups_info.green) 3511 ups_flags |= UPS_FLAGS_EN_GREEN; 3512 3513 if (tp->ups_info.aldps) 3514 ups_flags |= UPS_FLAGS_EN_ALDPS; 3515 3516 if (tp->ups_info.eee) 3517 ups_flags |= UPS_FLAGS_EN_EEE; 3518 3519 if (tp->ups_info.flow_control) 3520 ups_flags |= UPS_FLAGS_EN_FLOW_CTR; 3521 3522 if (tp->ups_info.eee_ckdiv) 3523 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; 3524 3525 if (tp->ups_info._10m_ckdiv) 3526 ups_flags |= UPS_FLAGS_EN_10M_CKDIV; 3527 3528 if (tp->ups_info.eee_plloff_100) 3529 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; 3530 3531 if (tp->ups_info.eee_plloff_giga) 3532 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; 3533 3534 if (tp->ups_info._250m_ckdiv) 3535 ups_flags |= UPS_FLAGS_250M_CKDIV; 3536 3537 switch (tp->ups_info.speed_duplex) { 3538 case FORCE_10M_HALF: 3539 ups_flags |= ups_flags_speed(0); 3540 break; 3541 case FORCE_10M_FULL: 3542 ups_flags |= ups_flags_speed(1); 3543 break; 3544 case FORCE_100M_HALF: 3545 ups_flags |= ups_flags_speed(2); 3546 break; 3547 case FORCE_100M_FULL: 3548 ups_flags |= ups_flags_speed(3); 3549 break; 3550 case NWAY_10M_HALF: 3551 ups_flags |= ups_flags_speed(4); 3552 break; 3553 case NWAY_10M_FULL: 3554 ups_flags |= ups_flags_speed(5); 3555 break; 3556 case NWAY_100M_HALF: 3557 ups_flags |= ups_flags_speed(6); 3558 break; 3559 case NWAY_100M_FULL: 3560 ups_flags |= ups_flags_speed(7); 3561 break; 3562 case NWAY_1000M_FULL: 3563 ups_flags |= ups_flags_speed(8); 3564 break; 3565 case NWAY_2500M_FULL: 3566 ups_flags |= ups_flags_speed(9); 3567 break; 3568 default: 3569 break; 3570 } 3571 3572 switch (tp->ups_info.lite_mode) { 3573 case 1: 3574 ups_flags |= 0 << 5; 3575 break; 3576 case 2: 3577 ups_flags |= 2 << 5; 3578 break; 3579 case 0: 3580 default: 3581 ups_flags |= 1 << 5; 3582 break; 3583 } 3584 3585 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); 3586 } 3587 3588 static void rtl_green_en(struct r8152 *tp, bool enable) 3589 { 3590 u16 data; 3591 3592 data = sram_read(tp, SRAM_GREEN_CFG); 3593 if (enable) 3594 data |= GREEN_ETH_EN; 3595 else 3596 data &= ~GREEN_ETH_EN; 3597 sram_write(tp, SRAM_GREEN_CFG, data); 3598 3599 tp->ups_info.green = enable; 3600 } 3601 3602 static void r8153b_green_en(struct r8152 *tp, bool enable) 3603 { 3604 if (enable) { 3605 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ 3606 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ 3607 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ 3608 } else { 3609 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ 3610 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ 3611 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ 3612 } 3613 3614 rtl_green_en(tp, true); 3615 } 3616 3617 static u16 r8153_phy_status(struct r8152 *tp, u16 desired) 3618 { 3619 u16 data; 3620 int i; 3621 3622 for (i = 0; i < 500; i++) { 3623 data = ocp_reg_read(tp, OCP_PHY_STATUS); 3624 data &= PHY_STAT_MASK; 3625 if (desired) { 3626 if (data == desired) 3627 break; 3628 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || 3629 data == PHY_STAT_EXT_INIT) { 3630 break; 3631 } 3632 3633 msleep(20); 3634 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3635 break; 3636 } 3637 3638 return data; 3639 } 3640 3641 static void r8153b_ups_en(struct r8152 *tp, bool enable) 3642 { 3643 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); 3644 3645 if (enable) { 3646 r8153b_ups_flags(tp); 3647 3648 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; 3649 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3650 3651 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3652 ocp_data |= UPS_FORCE_PWR_DOWN; 3653 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3654 } else { 3655 ocp_data &= ~(UPS_EN | USP_PREWAKE); 3656 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3657 3658 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3659 ocp_data &= ~UPS_FORCE_PWR_DOWN; 3660 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3661 3662 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 3663 int i; 3664 3665 for (i = 0; i < 500; i++) { 3666 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3667 return; 3668 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 3669 AUTOLOAD_DONE) 3670 break; 3671 msleep(20); 3672 } 3673 3674 tp->rtl_ops.hw_phy_cfg(tp); 3675 3676 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 3677 tp->duplex, tp->advertising); 3678 } 3679 } 3680 } 3681 3682 static void r8153c_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_data &= ~BIT(7); 3695 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3696 } else { 3697 ocp_data &= ~(UPS_EN | USP_PREWAKE); 3698 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3699 3700 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3701 ocp_data &= ~UPS_FORCE_PWR_DOWN; 3702 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3703 3704 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 3705 int i; 3706 3707 for (i = 0; i < 500; i++) { 3708 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 3709 return; 3710 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 3711 AUTOLOAD_DONE) 3712 break; 3713 msleep(20); 3714 } 3715 3716 tp->rtl_ops.hw_phy_cfg(tp); 3717 3718 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 3719 tp->duplex, tp->advertising); 3720 } 3721 3722 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3723 3724 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3725 ocp_data |= BIT(8); 3726 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3727 3728 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3729 } 3730 } 3731 3732 static void r8156_ups_en(struct r8152 *tp, bool enable) 3733 { 3734 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); 3735 3736 if (enable) { 3737 r8156_ups_flags(tp); 3738 3739 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; 3740 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3741 3742 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3743 ocp_data |= UPS_FORCE_PWR_DOWN; 3744 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3745 3746 switch (tp->version) { 3747 case RTL_VER_13: 3748 case RTL_VER_15: 3749 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL); 3750 ocp_data &= ~OOBS_POLLING; 3751 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data); 3752 break; 3753 default: 3754 break; 3755 } 3756 } else { 3757 ocp_data &= ~(UPS_EN | USP_PREWAKE); 3758 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3759 3760 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 3761 ocp_data &= ~UPS_FORCE_PWR_DOWN; 3762 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 3763 3764 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) { 3765 tp->rtl_ops.hw_phy_cfg(tp); 3766 3767 rtl8152_set_speed(tp, tp->autoneg, tp->speed, 3768 tp->duplex, tp->advertising); 3769 } 3770 } 3771 } 3772 3773 static void r8153_power_cut_en(struct r8152 *tp, bool enable) 3774 { 3775 u32 ocp_data; 3776 3777 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); 3778 if (enable) 3779 ocp_data |= PWR_EN | PHASE2_EN; 3780 else 3781 ocp_data &= ~(PWR_EN | PHASE2_EN); 3782 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3783 3784 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 3785 ocp_data &= ~PCUT_STATUS; 3786 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 3787 } 3788 3789 static void r8153b_power_cut_en(struct r8152 *tp, bool enable) 3790 { 3791 u32 ocp_data; 3792 3793 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); 3794 if (enable) 3795 ocp_data |= PWR_EN | PHASE2_EN; 3796 else 3797 ocp_data &= ~PWR_EN; 3798 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); 3799 3800 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 3801 ocp_data &= ~PCUT_STATUS; 3802 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 3803 } 3804 3805 static void r8153_queue_wake(struct r8152 *tp, bool enable) 3806 { 3807 u32 ocp_data; 3808 3809 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG); 3810 if (enable) 3811 ocp_data |= UPCOMING_RUNTIME_D3; 3812 else 3813 ocp_data &= ~UPCOMING_RUNTIME_D3; 3814 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data); 3815 3816 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG); 3817 ocp_data &= ~LINK_CHG_EVENT; 3818 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data); 3819 3820 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 3821 ocp_data &= ~LINK_CHANGE_FLAG; 3822 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 3823 } 3824 3825 static bool rtl_can_wakeup(struct r8152 *tp) 3826 { 3827 struct usb_device *udev = tp->udev; 3828 3829 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); 3830 } 3831 3832 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) 3833 { 3834 if (enable) { 3835 u32 ocp_data; 3836 3837 __rtl_set_wol(tp, WAKE_ANY); 3838 3839 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3840 3841 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3842 ocp_data |= LINK_OFF_WAKE_EN; 3843 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3844 3845 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3846 } else { 3847 u32 ocp_data; 3848 3849 __rtl_set_wol(tp, tp->saved_wolopts); 3850 3851 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 3852 3853 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 3854 ocp_data &= ~LINK_OFF_WAKE_EN; 3855 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 3856 3857 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 3858 } 3859 } 3860 3861 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) 3862 { 3863 if (enable) { 3864 r8153_u1u2en(tp, false); 3865 r8153_u2p3en(tp, false); 3866 rtl_runtime_suspend_enable(tp, true); 3867 } else { 3868 rtl_runtime_suspend_enable(tp, false); 3869 3870 switch (tp->version) { 3871 case RTL_VER_03: 3872 case RTL_VER_04: 3873 break; 3874 case RTL_VER_05: 3875 case RTL_VER_06: 3876 default: 3877 r8153_u2p3en(tp, true); 3878 break; 3879 } 3880 3881 r8153_u1u2en(tp, true); 3882 } 3883 } 3884 3885 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) 3886 { 3887 if (enable) { 3888 r8153_queue_wake(tp, true); 3889 r8153b_u1u2en(tp, false); 3890 r8153_u2p3en(tp, false); 3891 rtl_runtime_suspend_enable(tp, true); 3892 r8153b_ups_en(tp, true); 3893 } else { 3894 r8153b_ups_en(tp, false); 3895 r8153_queue_wake(tp, false); 3896 rtl_runtime_suspend_enable(tp, false); 3897 if (tp->udev->speed >= USB_SPEED_SUPER) 3898 r8153b_u1u2en(tp, true); 3899 } 3900 } 3901 3902 static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable) 3903 { 3904 if (enable) { 3905 r8153_queue_wake(tp, true); 3906 r8153b_u1u2en(tp, false); 3907 r8153_u2p3en(tp, false); 3908 rtl_runtime_suspend_enable(tp, true); 3909 r8153c_ups_en(tp, true); 3910 } else { 3911 r8153c_ups_en(tp, false); 3912 r8153_queue_wake(tp, false); 3913 rtl_runtime_suspend_enable(tp, false); 3914 r8153b_u1u2en(tp, true); 3915 } 3916 } 3917 3918 static void rtl8156_runtime_enable(struct r8152 *tp, bool enable) 3919 { 3920 if (enable) { 3921 r8153_queue_wake(tp, true); 3922 r8153b_u1u2en(tp, false); 3923 r8153_u2p3en(tp, false); 3924 rtl_runtime_suspend_enable(tp, true); 3925 } else { 3926 r8153_queue_wake(tp, false); 3927 rtl_runtime_suspend_enable(tp, false); 3928 r8153_u2p3en(tp, true); 3929 if (tp->udev->speed >= USB_SPEED_SUPER) 3930 r8153b_u1u2en(tp, true); 3931 } 3932 } 3933 3934 static void r8153_teredo_off(struct r8152 *tp) 3935 { 3936 u32 ocp_data; 3937 3938 switch (tp->version) { 3939 case RTL_VER_01: 3940 case RTL_VER_02: 3941 case RTL_VER_03: 3942 case RTL_VER_04: 3943 case RTL_VER_05: 3944 case RTL_VER_06: 3945 case RTL_VER_07: 3946 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); 3947 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | 3948 OOB_TEREDO_EN); 3949 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); 3950 break; 3951 3952 case RTL_VER_08: 3953 case RTL_VER_09: 3954 case RTL_TEST_01: 3955 case RTL_VER_10: 3956 case RTL_VER_11: 3957 case RTL_VER_12: 3958 case RTL_VER_13: 3959 case RTL_VER_14: 3960 case RTL_VER_15: 3961 default: 3962 /* The bit 0 ~ 7 are relative with teredo settings. They are 3963 * W1C (write 1 to clear), so set all 1 to disable it. 3964 */ 3965 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); 3966 break; 3967 } 3968 3969 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); 3970 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); 3971 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); 3972 } 3973 3974 static void rtl_reset_bmu(struct r8152 *tp) 3975 { 3976 u32 ocp_data; 3977 3978 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); 3979 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); 3980 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 3981 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; 3982 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); 3983 } 3984 3985 /* Clear the bp to stop the firmware before loading a new one */ 3986 static void rtl_clear_bp(struct r8152 *tp, u16 type) 3987 { 3988 u16 bp[16] = {0}; 3989 u16 bp_num; 3990 3991 switch (tp->version) { 3992 case RTL_VER_08: 3993 case RTL_VER_09: 3994 case RTL_VER_10: 3995 case RTL_VER_11: 3996 case RTL_VER_12: 3997 case RTL_VER_13: 3998 case RTL_VER_15: 3999 if (type == MCU_TYPE_USB) { 4000 ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0); 4001 bp_num = 16; 4002 break; 4003 } 4004 fallthrough; 4005 case RTL_VER_03: 4006 case RTL_VER_04: 4007 case RTL_VER_05: 4008 case RTL_VER_06: 4009 ocp_write_byte(tp, type, PLA_BP_EN, 0); 4010 fallthrough; 4011 case RTL_VER_01: 4012 case RTL_VER_02: 4013 case RTL_VER_07: 4014 bp_num = 8; 4015 break; 4016 case RTL_VER_14: 4017 default: 4018 ocp_write_word(tp, type, USB_BP2_EN, 0); 4019 bp_num = 16; 4020 break; 4021 } 4022 4023 generic_ocp_write(tp, PLA_BP_0, BYTE_EN_DWORD, bp_num << 1, bp, type); 4024 4025 /* wait 3 ms to make sure the firmware is stopped */ 4026 usleep_range(3000, 6000); 4027 ocp_write_word(tp, type, PLA_BP_BA, 0); 4028 } 4029 4030 static inline void rtl_reset_ocp_base(struct r8152 *tp) 4031 { 4032 tp->ocp_base = -1; 4033 } 4034 4035 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait) 4036 { 4037 u16 data, check; 4038 int i; 4039 4040 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD); 4041 if (request) { 4042 data |= PATCH_REQUEST; 4043 check = 0; 4044 } else { 4045 data &= ~PATCH_REQUEST; 4046 check = PATCH_READY; 4047 } 4048 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data); 4049 4050 for (i = 0; wait && i < 5000; i++) { 4051 u32 ocp_data; 4052 4053 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 4054 return -ENODEV; 4055 4056 usleep_range(1000, 2000); 4057 ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT); 4058 if ((ocp_data & PATCH_READY) ^ check) 4059 break; 4060 } 4061 4062 if (request && wait && 4063 !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { 4064 dev_err(&tp->intf->dev, "PHY patch request fail\n"); 4065 rtl_phy_patch_request(tp, false, false); 4066 return -ETIME; 4067 } else { 4068 return 0; 4069 } 4070 } 4071 4072 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key) 4073 { 4074 if (patch_key && key_addr) { 4075 sram_write(tp, key_addr, patch_key); 4076 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK); 4077 } else if (key_addr) { 4078 u16 data; 4079 4080 sram_write(tp, 0x0000, 0x0000); 4081 4082 data = ocp_reg_read(tp, OCP_PHY_LOCK); 4083 data &= ~PATCH_LOCK; 4084 ocp_reg_write(tp, OCP_PHY_LOCK, data); 4085 4086 sram_write(tp, key_addr, 0x0000); 4087 } else { 4088 WARN_ON_ONCE(1); 4089 } 4090 } 4091 4092 static int 4093 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait) 4094 { 4095 if (rtl_phy_patch_request(tp, true, wait)) 4096 return -ETIME; 4097 4098 rtl_patch_key_set(tp, key_addr, patch_key); 4099 4100 return 0; 4101 } 4102 4103 static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait) 4104 { 4105 rtl_patch_key_set(tp, key_addr, 0); 4106 4107 rtl_phy_patch_request(tp, false, wait); 4108 4109 return 0; 4110 } 4111 4112 static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy) 4113 { 4114 u16 fw_offset; 4115 u32 length; 4116 bool rc = false; 4117 4118 switch (tp->version) { 4119 case RTL_VER_01: 4120 case RTL_VER_02: 4121 case RTL_VER_03: 4122 case RTL_VER_04: 4123 case RTL_VER_05: 4124 case RTL_VER_06: 4125 case RTL_VER_07: 4126 case RTL_VER_08: 4127 case RTL_VER_09: 4128 case RTL_VER_10: 4129 case RTL_VER_11: 4130 case RTL_VER_12: 4131 case RTL_VER_14: 4132 goto out; 4133 case RTL_VER_13: 4134 case RTL_VER_15: 4135 default: 4136 break; 4137 } 4138 4139 fw_offset = __le16_to_cpu(phy->fw_offset); 4140 length = __le32_to_cpu(phy->blk_hdr.length); 4141 if (fw_offset < sizeof(*phy) || length <= fw_offset) { 4142 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4143 goto out; 4144 } 4145 4146 length -= fw_offset; 4147 if (length & 3) { 4148 dev_err(&tp->intf->dev, "invalid block length\n"); 4149 goto out; 4150 } 4151 4152 if (__le16_to_cpu(phy->fw_reg) != 0x9A00) { 4153 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4154 goto out; 4155 } 4156 4157 rc = true; 4158 out: 4159 return rc; 4160 } 4161 4162 static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver) 4163 { 4164 bool rc = false; 4165 4166 switch (tp->version) { 4167 case RTL_VER_10: 4168 case RTL_VER_11: 4169 case RTL_VER_12: 4170 case RTL_VER_13: 4171 case RTL_VER_15: 4172 break; 4173 default: 4174 goto out; 4175 } 4176 4177 if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) { 4178 dev_err(&tp->intf->dev, "invalid block length\n"); 4179 goto out; 4180 } 4181 4182 if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) { 4183 dev_err(&tp->intf->dev, "invalid phy ver addr\n"); 4184 goto out; 4185 } 4186 4187 rc = true; 4188 out: 4189 return rc; 4190 } 4191 4192 static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix) 4193 { 4194 bool rc = false; 4195 4196 switch (tp->version) { 4197 case RTL_VER_10: 4198 case RTL_VER_11: 4199 case RTL_VER_12: 4200 case RTL_VER_13: 4201 case RTL_VER_15: 4202 break; 4203 default: 4204 goto out; 4205 } 4206 4207 if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) { 4208 dev_err(&tp->intf->dev, "invalid block length\n"); 4209 goto out; 4210 } 4211 4212 if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD || 4213 __le16_to_cpu(fix->setting.data) != BIT(7)) { 4214 dev_err(&tp->intf->dev, "invalid phy fixup\n"); 4215 goto out; 4216 } 4217 4218 rc = true; 4219 out: 4220 return rc; 4221 } 4222 4223 static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy) 4224 { 4225 u16 fw_offset; 4226 u32 length; 4227 bool rc = false; 4228 4229 switch (tp->version) { 4230 case RTL_VER_10: 4231 case RTL_VER_11: 4232 case RTL_VER_12: 4233 case RTL_VER_13: 4234 case RTL_VER_15: 4235 break; 4236 default: 4237 goto out; 4238 } 4239 4240 fw_offset = __le16_to_cpu(phy->fw_offset); 4241 length = __le32_to_cpu(phy->blk_hdr.length); 4242 if (fw_offset < sizeof(*phy) || length <= fw_offset) { 4243 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4244 goto out; 4245 } 4246 4247 length -= fw_offset; 4248 if (length & 1) { 4249 dev_err(&tp->intf->dev, "invalid block length\n"); 4250 goto out; 4251 } 4252 4253 if (phy->pre_num > 2) { 4254 dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num); 4255 goto out; 4256 } 4257 4258 if (phy->bp_num > 8) { 4259 dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num); 4260 goto out; 4261 } 4262 4263 rc = true; 4264 out: 4265 return rc; 4266 } 4267 4268 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy) 4269 { 4270 u32 length; 4271 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start; 4272 bool rc = false; 4273 4274 switch (tp->version) { 4275 case RTL_VER_04: 4276 case RTL_VER_05: 4277 case RTL_VER_06: 4278 fw_reg = 0xa014; 4279 ba_reg = 0xa012; 4280 patch_en_addr = 0xa01a; 4281 mode_reg = 0xb820; 4282 bp_start = 0xa000; 4283 break; 4284 default: 4285 goto out; 4286 } 4287 4288 fw_offset = __le16_to_cpu(phy->fw_offset); 4289 if (fw_offset < sizeof(*phy)) { 4290 dev_err(&tp->intf->dev, "fw_offset too small\n"); 4291 goto out; 4292 } 4293 4294 length = __le32_to_cpu(phy->blk_hdr.length); 4295 if (length < fw_offset) { 4296 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4297 goto out; 4298 } 4299 4300 length -= __le16_to_cpu(phy->fw_offset); 4301 if (!length || (length & 1)) { 4302 dev_err(&tp->intf->dev, "invalid block length\n"); 4303 goto out; 4304 } 4305 4306 if (__le16_to_cpu(phy->fw_reg) != fw_reg) { 4307 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4308 goto out; 4309 } 4310 4311 if (__le16_to_cpu(phy->ba_reg) != ba_reg) { 4312 dev_err(&tp->intf->dev, "invalid base address register\n"); 4313 goto out; 4314 } 4315 4316 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) { 4317 dev_err(&tp->intf->dev, 4318 "invalid patch mode enabled register\n"); 4319 goto out; 4320 } 4321 4322 if (__le16_to_cpu(phy->mode_reg) != mode_reg) { 4323 dev_err(&tp->intf->dev, 4324 "invalid register to switch the mode\n"); 4325 goto out; 4326 } 4327 4328 if (__le16_to_cpu(phy->bp_start) != bp_start) { 4329 dev_err(&tp->intf->dev, 4330 "invalid start register of break point\n"); 4331 goto out; 4332 } 4333 4334 if (__le16_to_cpu(phy->bp_num) > 4) { 4335 dev_err(&tp->intf->dev, "invalid break point number\n"); 4336 goto out; 4337 } 4338 4339 rc = true; 4340 out: 4341 return rc; 4342 } 4343 4344 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac) 4345 { 4346 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset; 4347 bool rc = false; 4348 u32 length, type; 4349 int i, max_bp; 4350 4351 type = __le32_to_cpu(mac->blk_hdr.type); 4352 if (type == RTL_FW_PLA) { 4353 switch (tp->version) { 4354 case RTL_VER_01: 4355 case RTL_VER_02: 4356 case RTL_VER_07: 4357 fw_reg = 0xf800; 4358 bp_ba_addr = PLA_BP_BA; 4359 bp_en_addr = 0; 4360 bp_start = PLA_BP_0; 4361 max_bp = 8; 4362 break; 4363 case RTL_VER_03: 4364 case RTL_VER_04: 4365 case RTL_VER_05: 4366 case RTL_VER_06: 4367 case RTL_VER_08: 4368 case RTL_VER_09: 4369 case RTL_VER_11: 4370 case RTL_VER_12: 4371 case RTL_VER_13: 4372 case RTL_VER_15: 4373 fw_reg = 0xf800; 4374 bp_ba_addr = PLA_BP_BA; 4375 bp_en_addr = PLA_BP_EN; 4376 bp_start = PLA_BP_0; 4377 max_bp = 8; 4378 break; 4379 case RTL_VER_14: 4380 fw_reg = 0xf800; 4381 bp_ba_addr = PLA_BP_BA; 4382 bp_en_addr = USB_BP2_EN; 4383 bp_start = PLA_BP_0; 4384 max_bp = 16; 4385 break; 4386 default: 4387 goto out; 4388 } 4389 } else if (type == RTL_FW_USB) { 4390 switch (tp->version) { 4391 case RTL_VER_03: 4392 case RTL_VER_04: 4393 case RTL_VER_05: 4394 case RTL_VER_06: 4395 fw_reg = 0xf800; 4396 bp_ba_addr = USB_BP_BA; 4397 bp_en_addr = USB_BP_EN; 4398 bp_start = USB_BP_0; 4399 max_bp = 8; 4400 break; 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_14: 4407 case RTL_VER_15: 4408 fw_reg = 0xe600; 4409 bp_ba_addr = USB_BP_BA; 4410 bp_en_addr = USB_BP2_EN; 4411 bp_start = USB_BP_0; 4412 max_bp = 16; 4413 break; 4414 case RTL_VER_01: 4415 case RTL_VER_02: 4416 case RTL_VER_07: 4417 default: 4418 goto out; 4419 } 4420 } else { 4421 goto out; 4422 } 4423 4424 fw_offset = __le16_to_cpu(mac->fw_offset); 4425 if (fw_offset < sizeof(*mac)) { 4426 dev_err(&tp->intf->dev, "fw_offset too small\n"); 4427 goto out; 4428 } 4429 4430 length = __le32_to_cpu(mac->blk_hdr.length); 4431 if (length < fw_offset) { 4432 dev_err(&tp->intf->dev, "invalid fw_offset\n"); 4433 goto out; 4434 } 4435 4436 length -= fw_offset; 4437 if (length < 4 || (length & 3)) { 4438 dev_err(&tp->intf->dev, "invalid block length\n"); 4439 goto out; 4440 } 4441 4442 if (__le16_to_cpu(mac->fw_reg) != fw_reg) { 4443 dev_err(&tp->intf->dev, "invalid register to load firmware\n"); 4444 goto out; 4445 } 4446 4447 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) { 4448 dev_err(&tp->intf->dev, "invalid base address register\n"); 4449 goto out; 4450 } 4451 4452 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) { 4453 dev_err(&tp->intf->dev, "invalid enabled mask register\n"); 4454 goto out; 4455 } 4456 4457 if (__le16_to_cpu(mac->bp_start) != bp_start) { 4458 dev_err(&tp->intf->dev, 4459 "invalid start register of break point\n"); 4460 goto out; 4461 } 4462 4463 if (__le16_to_cpu(mac->bp_num) > max_bp) { 4464 dev_err(&tp->intf->dev, "invalid break point number\n"); 4465 goto out; 4466 } 4467 4468 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) { 4469 if (mac->bp[i]) { 4470 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i); 4471 goto out; 4472 } 4473 } 4474 4475 rc = true; 4476 out: 4477 return rc; 4478 } 4479 4480 /* Verify the checksum for the firmware file. It is calculated from the version 4481 * field to the end of the file. Compare the result with the checksum field to 4482 * make sure the file is correct. 4483 */ 4484 static long rtl8152_fw_verify_checksum(struct r8152 *tp, 4485 struct fw_header *fw_hdr, size_t size) 4486 { 4487 unsigned char checksum[sizeof(fw_hdr->checksum)]; 4488 struct crypto_shash *alg; 4489 struct shash_desc *sdesc; 4490 size_t len; 4491 long rc; 4492 4493 alg = crypto_alloc_shash("sha256", 0, 0); 4494 if (IS_ERR(alg)) { 4495 rc = PTR_ERR(alg); 4496 goto out; 4497 } 4498 4499 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) { 4500 rc = -EFAULT; 4501 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n", 4502 crypto_shash_digestsize(alg)); 4503 goto free_shash; 4504 } 4505 4506 len = sizeof(*sdesc) + crypto_shash_descsize(alg); 4507 sdesc = kmalloc(len, GFP_KERNEL); 4508 if (!sdesc) { 4509 rc = -ENOMEM; 4510 goto free_shash; 4511 } 4512 sdesc->tfm = alg; 4513 4514 len = size - sizeof(fw_hdr->checksum); 4515 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum); 4516 kfree(sdesc); 4517 if (rc) 4518 goto free_shash; 4519 4520 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) { 4521 dev_err(&tp->intf->dev, "checksum fail\n"); 4522 rc = -EFAULT; 4523 } 4524 4525 free_shash: 4526 crypto_free_shash(alg); 4527 out: 4528 return rc; 4529 } 4530 4531 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw) 4532 { 4533 const struct firmware *fw = rtl_fw->fw; 4534 struct fw_header *fw_hdr = (struct fw_header *)fw->data; 4535 unsigned long fw_flags = 0; 4536 long ret = -EFAULT; 4537 int i; 4538 4539 if (fw->size < sizeof(*fw_hdr)) { 4540 dev_err(&tp->intf->dev, "file too small\n"); 4541 goto fail; 4542 } 4543 4544 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size); 4545 if (ret) 4546 goto fail; 4547 4548 ret = -EFAULT; 4549 4550 for (i = sizeof(*fw_hdr); i < fw->size;) { 4551 struct fw_block *block = (struct fw_block *)&fw->data[i]; 4552 u32 type; 4553 4554 if ((i + sizeof(*block)) > fw->size) 4555 goto fail; 4556 4557 type = __le32_to_cpu(block->type); 4558 switch (type) { 4559 case RTL_FW_END: 4560 if (__le32_to_cpu(block->length) != sizeof(*block)) 4561 goto fail; 4562 goto fw_end; 4563 case RTL_FW_PLA: 4564 if (test_bit(FW_FLAGS_PLA, &fw_flags)) { 4565 dev_err(&tp->intf->dev, 4566 "multiple PLA firmware encountered"); 4567 goto fail; 4568 } 4569 4570 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) { 4571 dev_err(&tp->intf->dev, 4572 "check PLA firmware failed\n"); 4573 goto fail; 4574 } 4575 __set_bit(FW_FLAGS_PLA, &fw_flags); 4576 break; 4577 case RTL_FW_USB: 4578 if (test_bit(FW_FLAGS_USB, &fw_flags)) { 4579 dev_err(&tp->intf->dev, 4580 "multiple USB firmware encountered"); 4581 goto fail; 4582 } 4583 4584 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) { 4585 dev_err(&tp->intf->dev, 4586 "check USB firmware failed\n"); 4587 goto fail; 4588 } 4589 __set_bit(FW_FLAGS_USB, &fw_flags); 4590 break; 4591 case RTL_FW_PHY_START: 4592 if (test_bit(FW_FLAGS_START, &fw_flags) || 4593 test_bit(FW_FLAGS_NC, &fw_flags) || 4594 test_bit(FW_FLAGS_NC1, &fw_flags) || 4595 test_bit(FW_FLAGS_NC2, &fw_flags) || 4596 test_bit(FW_FLAGS_UC2, &fw_flags) || 4597 test_bit(FW_FLAGS_UC, &fw_flags) || 4598 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4599 dev_err(&tp->intf->dev, 4600 "check PHY_START fail\n"); 4601 goto fail; 4602 } 4603 4604 if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) { 4605 dev_err(&tp->intf->dev, 4606 "Invalid length for PHY_START\n"); 4607 goto fail; 4608 } 4609 __set_bit(FW_FLAGS_START, &fw_flags); 4610 break; 4611 case RTL_FW_PHY_STOP: 4612 if (test_bit(FW_FLAGS_STOP, &fw_flags) || 4613 !test_bit(FW_FLAGS_START, &fw_flags)) { 4614 dev_err(&tp->intf->dev, 4615 "Check PHY_STOP fail\n"); 4616 goto fail; 4617 } 4618 4619 if (__le32_to_cpu(block->length) != sizeof(*block)) { 4620 dev_err(&tp->intf->dev, 4621 "Invalid length for PHY_STOP\n"); 4622 goto fail; 4623 } 4624 __set_bit(FW_FLAGS_STOP, &fw_flags); 4625 break; 4626 case RTL_FW_PHY_NC: 4627 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4628 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4629 dev_err(&tp->intf->dev, 4630 "check PHY_NC fail\n"); 4631 goto fail; 4632 } 4633 4634 if (test_bit(FW_FLAGS_NC, &fw_flags)) { 4635 dev_err(&tp->intf->dev, 4636 "multiple PHY NC encountered\n"); 4637 goto fail; 4638 } 4639 4640 if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) { 4641 dev_err(&tp->intf->dev, 4642 "check PHY NC firmware failed\n"); 4643 goto fail; 4644 } 4645 __set_bit(FW_FLAGS_NC, &fw_flags); 4646 break; 4647 case RTL_FW_PHY_UNION_NC: 4648 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4649 test_bit(FW_FLAGS_NC1, &fw_flags) || 4650 test_bit(FW_FLAGS_NC2, &fw_flags) || 4651 test_bit(FW_FLAGS_UC2, &fw_flags) || 4652 test_bit(FW_FLAGS_UC, &fw_flags) || 4653 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4654 dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n"); 4655 goto fail; 4656 } 4657 4658 if (test_bit(FW_FLAGS_NC, &fw_flags)) { 4659 dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n"); 4660 goto fail; 4661 } 4662 4663 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4664 dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n"); 4665 goto fail; 4666 } 4667 __set_bit(FW_FLAGS_NC, &fw_flags); 4668 break; 4669 case RTL_FW_PHY_UNION_NC1: 4670 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4671 test_bit(FW_FLAGS_NC2, &fw_flags) || 4672 test_bit(FW_FLAGS_UC2, &fw_flags) || 4673 test_bit(FW_FLAGS_UC, &fw_flags) || 4674 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4675 dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n"); 4676 goto fail; 4677 } 4678 4679 if (test_bit(FW_FLAGS_NC1, &fw_flags)) { 4680 dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n"); 4681 goto fail; 4682 } 4683 4684 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4685 dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n"); 4686 goto fail; 4687 } 4688 __set_bit(FW_FLAGS_NC1, &fw_flags); 4689 break; 4690 case RTL_FW_PHY_UNION_NC2: 4691 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4692 test_bit(FW_FLAGS_UC2, &fw_flags) || 4693 test_bit(FW_FLAGS_UC, &fw_flags) || 4694 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4695 dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n"); 4696 goto fail; 4697 } 4698 4699 if (test_bit(FW_FLAGS_NC2, &fw_flags)) { 4700 dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n"); 4701 goto fail; 4702 } 4703 4704 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4705 dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n"); 4706 goto fail; 4707 } 4708 __set_bit(FW_FLAGS_NC2, &fw_flags); 4709 break; 4710 case RTL_FW_PHY_UNION_UC2: 4711 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4712 test_bit(FW_FLAGS_UC, &fw_flags) || 4713 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4714 dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n"); 4715 goto fail; 4716 } 4717 4718 if (test_bit(FW_FLAGS_UC2, &fw_flags)) { 4719 dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n"); 4720 goto fail; 4721 } 4722 4723 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4724 dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n"); 4725 goto fail; 4726 } 4727 __set_bit(FW_FLAGS_UC2, &fw_flags); 4728 break; 4729 case RTL_FW_PHY_UNION_UC: 4730 if (!test_bit(FW_FLAGS_START, &fw_flags) || 4731 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4732 dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n"); 4733 goto fail; 4734 } 4735 4736 if (test_bit(FW_FLAGS_UC, &fw_flags)) { 4737 dev_err(&tp->intf->dev, "multiple PHY UC encountered\n"); 4738 goto fail; 4739 } 4740 4741 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4742 dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n"); 4743 goto fail; 4744 } 4745 __set_bit(FW_FLAGS_UC, &fw_flags); 4746 break; 4747 case RTL_FW_PHY_UNION_MISC: 4748 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) { 4749 dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n"); 4750 goto fail; 4751 } 4752 break; 4753 case RTL_FW_PHY_FIXUP: 4754 if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) { 4755 dev_err(&tp->intf->dev, "check PHY fixup failed\n"); 4756 goto fail; 4757 } 4758 break; 4759 case RTL_FW_PHY_SPEED_UP: 4760 if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) { 4761 dev_err(&tp->intf->dev, "multiple PHY firmware encountered"); 4762 goto fail; 4763 } 4764 4765 if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) { 4766 dev_err(&tp->intf->dev, "check PHY speed up failed\n"); 4767 goto fail; 4768 } 4769 __set_bit(FW_FLAGS_SPEED_UP, &fw_flags); 4770 break; 4771 case RTL_FW_PHY_VER: 4772 if (test_bit(FW_FLAGS_START, &fw_flags) || 4773 test_bit(FW_FLAGS_NC, &fw_flags) || 4774 test_bit(FW_FLAGS_NC1, &fw_flags) || 4775 test_bit(FW_FLAGS_NC2, &fw_flags) || 4776 test_bit(FW_FLAGS_UC2, &fw_flags) || 4777 test_bit(FW_FLAGS_UC, &fw_flags) || 4778 test_bit(FW_FLAGS_STOP, &fw_flags)) { 4779 dev_err(&tp->intf->dev, "Invalid order to set PHY version\n"); 4780 goto fail; 4781 } 4782 4783 if (test_bit(FW_FLAGS_VER, &fw_flags)) { 4784 dev_err(&tp->intf->dev, "multiple PHY version encountered"); 4785 goto fail; 4786 } 4787 4788 if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) { 4789 dev_err(&tp->intf->dev, "check PHY version failed\n"); 4790 goto fail; 4791 } 4792 __set_bit(FW_FLAGS_VER, &fw_flags); 4793 break; 4794 default: 4795 dev_warn(&tp->intf->dev, "Unknown type %u is found\n", 4796 type); 4797 break; 4798 } 4799 4800 /* next block */ 4801 i += ALIGN(__le32_to_cpu(block->length), 8); 4802 } 4803 4804 fw_end: 4805 if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) { 4806 dev_err(&tp->intf->dev, "without PHY_STOP\n"); 4807 goto fail; 4808 } 4809 4810 return 0; 4811 fail: 4812 return ret; 4813 } 4814 4815 static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait) 4816 { 4817 u32 len; 4818 u8 *data; 4819 4820 rtl_reset_ocp_base(tp); 4821 4822 if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) { 4823 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n"); 4824 return; 4825 } 4826 4827 len = __le32_to_cpu(phy->blk_hdr.length); 4828 len -= __le16_to_cpu(phy->fw_offset); 4829 data = (u8 *)phy + __le16_to_cpu(phy->fw_offset); 4830 4831 if (rtl_phy_patch_request(tp, true, wait)) 4832 return; 4833 4834 while (len) { 4835 u32 ocp_data, size; 4836 int i; 4837 4838 if (len < 2048) 4839 size = len; 4840 else 4841 size = 2048; 4842 4843 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL); 4844 ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE; 4845 ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data); 4846 4847 generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB); 4848 4849 data += size; 4850 len -= size; 4851 4852 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL); 4853 ocp_data |= POL_GPHY_PATCH; 4854 ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data); 4855 4856 for (i = 0; i < 1000; i++) { 4857 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH)) 4858 break; 4859 } 4860 4861 if (i == 1000) { 4862 dev_err(&tp->intf->dev, "ram code speedup mode timeout\n"); 4863 break; 4864 } 4865 } 4866 4867 rtl_reset_ocp_base(tp); 4868 4869 rtl_phy_patch_request(tp, false, wait); 4870 4871 if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version)) 4872 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 4873 else 4874 dev_err(&tp->intf->dev, "ram code speedup mode fail\n"); 4875 } 4876 4877 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver) 4878 { 4879 u16 ver_addr, ver; 4880 4881 ver_addr = __le16_to_cpu(phy_ver->ver.addr); 4882 ver = __le16_to_cpu(phy_ver->ver.data); 4883 4884 rtl_reset_ocp_base(tp); 4885 4886 if (sram_read(tp, ver_addr) >= ver) { 4887 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n"); 4888 return 0; 4889 } 4890 4891 sram_write(tp, ver_addr, ver); 4892 4893 dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver); 4894 4895 return ver; 4896 } 4897 4898 static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix) 4899 { 4900 u16 addr, data; 4901 4902 rtl_reset_ocp_base(tp); 4903 4904 addr = __le16_to_cpu(fix->setting.addr); 4905 data = ocp_reg_read(tp, addr); 4906 4907 switch (__le16_to_cpu(fix->bit_cmd)) { 4908 case FW_FIXUP_AND: 4909 data &= __le16_to_cpu(fix->setting.data); 4910 break; 4911 case FW_FIXUP_OR: 4912 data |= __le16_to_cpu(fix->setting.data); 4913 break; 4914 case FW_FIXUP_NOT: 4915 data &= ~__le16_to_cpu(fix->setting.data); 4916 break; 4917 case FW_FIXUP_XOR: 4918 data ^= __le16_to_cpu(fix->setting.data); 4919 break; 4920 default: 4921 return; 4922 } 4923 4924 ocp_reg_write(tp, addr, data); 4925 4926 dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data); 4927 } 4928 4929 static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy) 4930 { 4931 __le16 *data; 4932 u32 length; 4933 int i, num; 4934 4935 rtl_reset_ocp_base(tp); 4936 4937 num = phy->pre_num; 4938 for (i = 0; i < num; i++) 4939 sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr), 4940 __le16_to_cpu(phy->pre_set[i].data)); 4941 4942 length = __le32_to_cpu(phy->blk_hdr.length); 4943 length -= __le16_to_cpu(phy->fw_offset); 4944 num = length / 2; 4945 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); 4946 4947 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); 4948 for (i = 0; i < num; i++) 4949 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); 4950 4951 num = phy->bp_num; 4952 for (i = 0; i < num; i++) 4953 sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data)); 4954 4955 if (phy->bp_num && phy->bp_en.addr) 4956 sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data)); 4957 4958 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 4959 } 4960 4961 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy) 4962 { 4963 u16 mode_reg, bp_index; 4964 u32 length, i, num; 4965 __le16 *data; 4966 4967 rtl_reset_ocp_base(tp); 4968 4969 mode_reg = __le16_to_cpu(phy->mode_reg); 4970 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre)); 4971 sram_write(tp, __le16_to_cpu(phy->ba_reg), 4972 __le16_to_cpu(phy->ba_data)); 4973 4974 length = __le32_to_cpu(phy->blk_hdr.length); 4975 length -= __le16_to_cpu(phy->fw_offset); 4976 num = length / 2; 4977 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); 4978 4979 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); 4980 for (i = 0; i < num; i++) 4981 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); 4982 4983 sram_write(tp, __le16_to_cpu(phy->patch_en_addr), 4984 __le16_to_cpu(phy->patch_en_value)); 4985 4986 bp_index = __le16_to_cpu(phy->bp_start); 4987 num = __le16_to_cpu(phy->bp_num); 4988 for (i = 0; i < num; i++) { 4989 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i])); 4990 bp_index += 2; 4991 } 4992 4993 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post)); 4994 4995 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); 4996 } 4997 4998 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac) 4999 { 5000 u16 bp_en_addr, type, fw_ver_reg; 5001 u32 length; 5002 u8 *data; 5003 5004 switch (__le32_to_cpu(mac->blk_hdr.type)) { 5005 case RTL_FW_PLA: 5006 type = MCU_TYPE_PLA; 5007 break; 5008 case RTL_FW_USB: 5009 type = MCU_TYPE_USB; 5010 break; 5011 default: 5012 return; 5013 } 5014 5015 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg); 5016 if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) { 5017 dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB"); 5018 return; 5019 } 5020 5021 rtl_clear_bp(tp, type); 5022 5023 /* Enable backup/restore of MACDBG. This is required after clearing PLA 5024 * break points and before applying the PLA firmware. 5025 */ 5026 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA && 5027 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) { 5028 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM); 5029 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM); 5030 } 5031 5032 length = __le32_to_cpu(mac->blk_hdr.length); 5033 length -= __le16_to_cpu(mac->fw_offset); 5034 5035 data = (u8 *)mac; 5036 data += __le16_to_cpu(mac->fw_offset); 5037 5038 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data, 5039 type); 5040 5041 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr), 5042 __le16_to_cpu(mac->bp_ba_value)); 5043 5044 generic_ocp_write(tp, __le16_to_cpu(mac->bp_start), BYTE_EN_DWORD, 5045 __le16_to_cpu(mac->bp_num) << 1, mac->bp, type); 5046 5047 bp_en_addr = __le16_to_cpu(mac->bp_en_addr); 5048 if (bp_en_addr) 5049 ocp_write_word(tp, type, bp_en_addr, 5050 __le16_to_cpu(mac->bp_en_value)); 5051 5052 if (fw_ver_reg) 5053 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg, 5054 mac->fw_ver_data); 5055 5056 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info); 5057 } 5058 5059 static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut) 5060 { 5061 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5062 const struct firmware *fw; 5063 struct fw_header *fw_hdr; 5064 struct fw_phy_patch_key *key; 5065 u16 key_addr = 0; 5066 int i, patch_phy = 1; 5067 5068 if (IS_ERR_OR_NULL(rtl_fw->fw)) 5069 return; 5070 5071 fw = rtl_fw->fw; 5072 fw_hdr = (struct fw_header *)fw->data; 5073 5074 if (rtl_fw->pre_fw) 5075 rtl_fw->pre_fw(tp); 5076 5077 for (i = offsetof(struct fw_header, blocks); i < fw->size;) { 5078 struct fw_block *block = (struct fw_block *)&fw->data[i]; 5079 5080 switch (__le32_to_cpu(block->type)) { 5081 case RTL_FW_END: 5082 goto post_fw; 5083 case RTL_FW_PLA: 5084 case RTL_FW_USB: 5085 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block); 5086 break; 5087 case RTL_FW_PHY_START: 5088 if (!patch_phy) 5089 break; 5090 key = (struct fw_phy_patch_key *)block; 5091 key_addr = __le16_to_cpu(key->key_reg); 5092 rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut); 5093 break; 5094 case RTL_FW_PHY_STOP: 5095 if (!patch_phy) 5096 break; 5097 WARN_ON(!key_addr); 5098 rtl_post_ram_code(tp, key_addr, !power_cut); 5099 break; 5100 case RTL_FW_PHY_NC: 5101 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block); 5102 break; 5103 case RTL_FW_PHY_VER: 5104 patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block); 5105 break; 5106 case RTL_FW_PHY_UNION_NC: 5107 case RTL_FW_PHY_UNION_NC1: 5108 case RTL_FW_PHY_UNION_NC2: 5109 case RTL_FW_PHY_UNION_UC2: 5110 case RTL_FW_PHY_UNION_UC: 5111 case RTL_FW_PHY_UNION_MISC: 5112 if (patch_phy) 5113 rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block); 5114 break; 5115 case RTL_FW_PHY_FIXUP: 5116 if (patch_phy) 5117 rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block); 5118 break; 5119 case RTL_FW_PHY_SPEED_UP: 5120 rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut); 5121 break; 5122 default: 5123 break; 5124 } 5125 5126 i += ALIGN(__le32_to_cpu(block->length), 8); 5127 } 5128 5129 post_fw: 5130 if (rtl_fw->post_fw) 5131 rtl_fw->post_fw(tp); 5132 5133 rtl_reset_ocp_base(tp); 5134 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE); 5135 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version); 5136 } 5137 5138 static void rtl8152_release_firmware(struct r8152 *tp) 5139 { 5140 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5141 5142 if (!IS_ERR_OR_NULL(rtl_fw->fw)) { 5143 release_firmware(rtl_fw->fw); 5144 rtl_fw->fw = NULL; 5145 } 5146 } 5147 5148 static int rtl8152_request_firmware(struct r8152 *tp) 5149 { 5150 struct rtl_fw *rtl_fw = &tp->rtl_fw; 5151 long rc; 5152 5153 if (rtl_fw->fw || !rtl_fw->fw_name) { 5154 dev_info(&tp->intf->dev, "skip request firmware\n"); 5155 rc = 0; 5156 goto result; 5157 } 5158 5159 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev); 5160 if (rc < 0) 5161 goto result; 5162 5163 rc = rtl8152_check_firmware(tp, rtl_fw); 5164 if (rc < 0) 5165 release_firmware(rtl_fw->fw); 5166 5167 result: 5168 if (rc) { 5169 rtl_fw->fw = ERR_PTR(rc); 5170 5171 dev_warn(&tp->intf->dev, 5172 "unable to load firmware patch %s (%ld)\n", 5173 rtl_fw->fw_name, rc); 5174 } 5175 5176 return rc; 5177 } 5178 5179 static void r8152_aldps_en(struct r8152 *tp, bool enable) 5180 { 5181 if (enable) { 5182 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | 5183 LINKENA | DIS_SDSAVE); 5184 } else { 5185 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | 5186 DIS_SDSAVE); 5187 msleep(20); 5188 } 5189 } 5190 5191 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) 5192 { 5193 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); 5194 ocp_reg_write(tp, OCP_EEE_DATA, reg); 5195 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); 5196 } 5197 5198 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) 5199 { 5200 u16 data; 5201 5202 r8152_mmd_indirect(tp, dev, reg); 5203 data = ocp_reg_read(tp, OCP_EEE_DATA); 5204 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 5205 5206 return data; 5207 } 5208 5209 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) 5210 { 5211 r8152_mmd_indirect(tp, dev, reg); 5212 ocp_reg_write(tp, OCP_EEE_DATA, data); 5213 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 5214 } 5215 5216 static void r8152_eee_en(struct r8152 *tp, bool enable) 5217 { 5218 u16 config1, config2, config3; 5219 u32 ocp_data; 5220 5221 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); 5222 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; 5223 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); 5224 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; 5225 5226 if (enable) { 5227 ocp_data |= EEE_RX_EN | EEE_TX_EN; 5228 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; 5229 config1 |= sd_rise_time(1); 5230 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; 5231 config3 |= fast_snr(42); 5232 } else { 5233 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); 5234 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | 5235 RX_QUIET_EN); 5236 config1 |= sd_rise_time(7); 5237 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); 5238 config3 |= fast_snr(511); 5239 } 5240 5241 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); 5242 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); 5243 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); 5244 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); 5245 } 5246 5247 static void r8153_eee_en(struct r8152 *tp, bool enable) 5248 { 5249 u32 ocp_data; 5250 u16 config; 5251 5252 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); 5253 config = ocp_reg_read(tp, OCP_EEE_CFG); 5254 5255 if (enable) { 5256 ocp_data |= EEE_RX_EN | EEE_TX_EN; 5257 config |= EEE10_EN; 5258 } else { 5259 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); 5260 config &= ~EEE10_EN; 5261 } 5262 5263 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); 5264 ocp_reg_write(tp, OCP_EEE_CFG, config); 5265 5266 tp->ups_info.eee = enable; 5267 } 5268 5269 static void r8156_eee_en(struct r8152 *tp, bool enable) 5270 { 5271 u16 config; 5272 5273 r8153_eee_en(tp, enable); 5274 5275 config = ocp_reg_read(tp, OCP_EEE_ADV2); 5276 5277 if (enable) 5278 config |= MDIO_EEE_2_5GT; 5279 else 5280 config &= ~MDIO_EEE_2_5GT; 5281 5282 ocp_reg_write(tp, OCP_EEE_ADV2, config); 5283 } 5284 5285 static void rtl_eee_enable(struct r8152 *tp, bool enable) 5286 { 5287 switch (tp->version) { 5288 case RTL_VER_01: 5289 case RTL_VER_02: 5290 case RTL_VER_07: 5291 if (enable) { 5292 r8152_eee_en(tp, true); 5293 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 5294 tp->eee_adv); 5295 } else { 5296 r8152_eee_en(tp, false); 5297 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); 5298 } 5299 break; 5300 case RTL_VER_03: 5301 case RTL_VER_04: 5302 case RTL_VER_05: 5303 case RTL_VER_06: 5304 case RTL_VER_08: 5305 case RTL_VER_09: 5306 case RTL_VER_14: 5307 if (enable) { 5308 r8153_eee_en(tp, true); 5309 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); 5310 } else { 5311 r8153_eee_en(tp, false); 5312 ocp_reg_write(tp, OCP_EEE_ADV, 0); 5313 } 5314 break; 5315 case RTL_VER_10: 5316 case RTL_VER_11: 5317 case RTL_VER_12: 5318 case RTL_VER_13: 5319 case RTL_VER_15: 5320 if (enable) { 5321 r8156_eee_en(tp, true); 5322 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); 5323 } else { 5324 r8156_eee_en(tp, false); 5325 ocp_reg_write(tp, OCP_EEE_ADV, 0); 5326 } 5327 break; 5328 default: 5329 break; 5330 } 5331 } 5332 5333 static void r8152b_enable_fc(struct r8152 *tp) 5334 { 5335 u16 anar; 5336 5337 anar = r8152_mdio_read(tp, MII_ADVERTISE); 5338 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; 5339 r8152_mdio_write(tp, MII_ADVERTISE, anar); 5340 5341 tp->ups_info.flow_control = true; 5342 } 5343 5344 static void rtl8152_disable(struct r8152 *tp) 5345 { 5346 r8152_aldps_en(tp, false); 5347 rtl_disable(tp); 5348 r8152_aldps_en(tp, true); 5349 } 5350 5351 static void r8152b_hw_phy_cfg(struct r8152 *tp) 5352 { 5353 rtl8152_apply_firmware(tp, false); 5354 rtl_eee_enable(tp, tp->eee_en); 5355 r8152_aldps_en(tp, true); 5356 r8152b_enable_fc(tp); 5357 5358 set_bit(PHY_RESET, &tp->flags); 5359 } 5360 5361 static void wait_oob_link_list_ready(struct r8152 *tp) 5362 { 5363 u32 ocp_data; 5364 int i; 5365 5366 for (i = 0; i < 1000; i++) { 5367 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5368 if (ocp_data & LINK_LIST_READY) 5369 break; 5370 usleep_range(1000, 2000); 5371 } 5372 } 5373 5374 static void r8156b_wait_loading_flash(struct r8152 *tp) 5375 { 5376 if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) && 5377 !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) { 5378 int i; 5379 5380 for (i = 0; i < 100; i++) { 5381 if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE) 5382 break; 5383 usleep_range(1000, 2000); 5384 } 5385 } 5386 } 5387 5388 static void r8152b_exit_oob(struct r8152 *tp) 5389 { 5390 u32 ocp_data; 5391 5392 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5393 ocp_data &= ~RCR_ACPT_ALL; 5394 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5395 5396 rxdy_gated_en(tp, true); 5397 r8153_teredo_off(tp); 5398 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 5399 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); 5400 5401 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5402 ocp_data &= ~NOW_IS_OOB; 5403 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5404 5405 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5406 ocp_data &= ~MCU_BORW_EN; 5407 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5408 5409 wait_oob_link_list_ready(tp); 5410 5411 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5412 ocp_data |= RE_INIT_LL; 5413 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5414 5415 wait_oob_link_list_ready(tp); 5416 5417 rtl8152_nic_reset(tp); 5418 5419 /* rx share fifo credit full threshold */ 5420 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 5421 5422 if (tp->udev->speed == USB_SPEED_FULL || 5423 tp->udev->speed == USB_SPEED_LOW) { 5424 /* rx share fifo credit near full threshold */ 5425 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 5426 RXFIFO_THR2_FULL); 5427 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 5428 RXFIFO_THR3_FULL); 5429 } else { 5430 /* rx share fifo credit near full threshold */ 5431 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 5432 RXFIFO_THR2_HIGH); 5433 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 5434 RXFIFO_THR3_HIGH); 5435 } 5436 5437 /* TX share fifo free credit full threshold */ 5438 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); 5439 5440 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); 5441 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); 5442 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, 5443 TEST_MODE_DISABLE | TX_SIZE_ADJUST1); 5444 5445 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 5446 5447 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 5448 5449 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); 5450 ocp_data |= TCR0_AUTO_FIFO; 5451 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); 5452 } 5453 5454 static void r8152b_enter_oob(struct r8152 *tp) 5455 { 5456 u32 ocp_data; 5457 5458 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5459 ocp_data &= ~NOW_IS_OOB; 5460 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5461 5462 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 5463 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 5464 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 5465 5466 rtl_disable(tp); 5467 5468 wait_oob_link_list_ready(tp); 5469 5470 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5471 ocp_data |= RE_INIT_LL; 5472 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5473 5474 wait_oob_link_list_ready(tp); 5475 5476 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 5477 5478 rtl_rx_vlan_en(tp, true); 5479 5480 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); 5481 ocp_data |= ALDPS_PROXY_MODE; 5482 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); 5483 5484 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5485 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; 5486 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5487 5488 rxdy_gated_en(tp, false); 5489 5490 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5491 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 5492 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5493 } 5494 5495 static int r8153_pre_firmware_1(struct r8152 *tp) 5496 { 5497 int i; 5498 5499 /* Wait till the WTD timer is ready. It would take at most 104 ms. */ 5500 for (i = 0; i < 104; i++) { 5501 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL); 5502 5503 if (!(ocp_data & WTD1_EN)) 5504 break; 5505 usleep_range(1000, 2000); 5506 } 5507 5508 return 0; 5509 } 5510 5511 static int r8153_post_firmware_1(struct r8152 *tp) 5512 { 5513 /* set USB_BP_4 to support USB_SPEED_SUPER only */ 5514 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) 5515 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY); 5516 5517 /* reset UPHY timer to 36 ms */ 5518 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 5519 5520 return 0; 5521 } 5522 5523 static int r8153_pre_firmware_2(struct r8152 *tp) 5524 { 5525 u32 ocp_data; 5526 5527 r8153_pre_firmware_1(tp); 5528 5529 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); 5530 ocp_data &= ~FW_FIX_SUSPEND; 5531 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); 5532 5533 return 0; 5534 } 5535 5536 static int r8153_post_firmware_2(struct r8152 *tp) 5537 { 5538 u32 ocp_data; 5539 5540 /* enable bp0 if support USB_SPEED_SUPER only */ 5541 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) { 5542 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); 5543 ocp_data |= BIT(0); 5544 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); 5545 } 5546 5547 /* reset UPHY timer to 36 ms */ 5548 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); 5549 5550 /* enable U3P3 check, set the counter to 4 */ 5551 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4); 5552 5553 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); 5554 ocp_data |= FW_FIX_SUSPEND; 5555 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); 5556 5557 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 5558 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 5559 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 5560 5561 return 0; 5562 } 5563 5564 static int r8153_post_firmware_3(struct r8152 *tp) 5565 { 5566 u32 ocp_data; 5567 5568 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 5569 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 5570 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 5571 5572 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 5573 ocp_data |= FW_IP_RESET_EN; 5574 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 5575 5576 return 0; 5577 } 5578 5579 static int r8153b_pre_firmware_1(struct r8152 *tp) 5580 { 5581 /* enable fc timer and set timer to 1 second. */ 5582 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, 5583 CTRL_TIMER_EN | (1000 / 8)); 5584 5585 return 0; 5586 } 5587 5588 static int r8153b_post_firmware_1(struct r8152 *tp) 5589 { 5590 u32 ocp_data; 5591 5592 /* enable bp0 for RTL8153-BND */ 5593 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); 5594 if (ocp_data & BND_MASK) { 5595 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); 5596 ocp_data |= BIT(0); 5597 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); 5598 } 5599 5600 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 5601 ocp_data |= FLOW_CTRL_PATCH_OPT; 5602 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 5603 5604 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 5605 ocp_data |= FC_PATCH_TASK; 5606 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 5607 5608 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 5609 ocp_data |= FW_IP_RESET_EN; 5610 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 5611 5612 return 0; 5613 } 5614 5615 static int r8153c_post_firmware_1(struct r8152 *tp) 5616 { 5617 u32 ocp_data; 5618 5619 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 5620 ocp_data |= FLOW_CTRL_PATCH_2; 5621 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 5622 5623 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 5624 ocp_data |= FC_PATCH_TASK; 5625 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 5626 5627 return 0; 5628 } 5629 5630 static int r8156a_post_firmware_1(struct r8152 *tp) 5631 { 5632 u32 ocp_data; 5633 5634 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); 5635 ocp_data |= FW_IP_RESET_EN; 5636 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); 5637 5638 /* Modify U3PHY parameter for compatibility issue */ 5639 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e); 5640 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9); 5641 5642 return 0; 5643 } 5644 5645 static void r8153_aldps_en(struct r8152 *tp, bool enable) 5646 { 5647 u16 data; 5648 5649 data = ocp_reg_read(tp, OCP_POWER_CFG); 5650 if (enable) { 5651 data |= EN_ALDPS; 5652 ocp_reg_write(tp, OCP_POWER_CFG, data); 5653 } else { 5654 int i; 5655 5656 data &= ~EN_ALDPS; 5657 ocp_reg_write(tp, OCP_POWER_CFG, data); 5658 for (i = 0; i < 20; i++) { 5659 usleep_range(1000, 2000); 5660 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) 5661 break; 5662 } 5663 } 5664 5665 tp->ups_info.aldps = enable; 5666 } 5667 5668 static void r8153_hw_phy_cfg(struct r8152 *tp) 5669 { 5670 u32 ocp_data; 5671 u16 data; 5672 5673 /* disable ALDPS before updating the PHY parameters */ 5674 r8153_aldps_en(tp, false); 5675 5676 /* disable EEE before updating the PHY parameters */ 5677 rtl_eee_enable(tp, false); 5678 5679 rtl8152_apply_firmware(tp, false); 5680 5681 if (tp->version == RTL_VER_03) { 5682 data = ocp_reg_read(tp, OCP_EEE_CFG); 5683 data &= ~CTAP_SHORT_EN; 5684 ocp_reg_write(tp, OCP_EEE_CFG, data); 5685 } 5686 5687 data = ocp_reg_read(tp, OCP_POWER_CFG); 5688 data |= EEE_CLKDIV_EN; 5689 ocp_reg_write(tp, OCP_POWER_CFG, data); 5690 5691 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 5692 data |= EN_10M_BGOFF; 5693 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 5694 data = ocp_reg_read(tp, OCP_POWER_CFG); 5695 data |= EN_10M_PLLOFF; 5696 ocp_reg_write(tp, OCP_POWER_CFG, data); 5697 sram_write(tp, SRAM_IMPEDANCE, 0x0b13); 5698 5699 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 5700 ocp_data |= PFM_PWM_SWITCH; 5701 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 5702 5703 /* Enable LPF corner auto tune */ 5704 sram_write(tp, SRAM_LPF_CFG, 0xf70f); 5705 5706 /* Adjust 10M Amplitude */ 5707 sram_write(tp, SRAM_10M_AMP1, 0x00af); 5708 sram_write(tp, SRAM_10M_AMP2, 0x0208); 5709 5710 if (tp->eee_en) 5711 rtl_eee_enable(tp, true); 5712 5713 r8153_aldps_en(tp, true); 5714 r8152b_enable_fc(tp); 5715 5716 switch (tp->version) { 5717 case RTL_VER_03: 5718 case RTL_VER_04: 5719 break; 5720 case RTL_VER_05: 5721 case RTL_VER_06: 5722 default: 5723 r8153_u2p3en(tp, true); 5724 break; 5725 } 5726 5727 set_bit(PHY_RESET, &tp->flags); 5728 } 5729 5730 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) 5731 { 5732 u32 ocp_data; 5733 5734 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); 5735 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); 5736 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ 5737 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); 5738 5739 return ocp_data; 5740 } 5741 5742 static void r8153b_hw_phy_cfg(struct r8152 *tp) 5743 { 5744 u32 ocp_data; 5745 u16 data; 5746 5747 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 5748 if (ocp_data & PCUT_STATUS) { 5749 ocp_data &= ~PCUT_STATUS; 5750 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 5751 } 5752 5753 /* disable ALDPS before updating the PHY parameters */ 5754 r8153_aldps_en(tp, false); 5755 5756 /* disable EEE before updating the PHY parameters */ 5757 rtl_eee_enable(tp, false); 5758 5759 /* U1/U2/L1 idle timer. 500 us */ 5760 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 5761 5762 data = r8153_phy_status(tp, 0); 5763 5764 switch (data) { 5765 case PHY_STAT_PWRDN: 5766 case PHY_STAT_EXT_INIT: 5767 rtl8152_apply_firmware(tp, true); 5768 5769 data = r8152_mdio_read(tp, MII_BMCR); 5770 data &= ~BMCR_PDOWN; 5771 r8152_mdio_write(tp, MII_BMCR, data); 5772 break; 5773 case PHY_STAT_LAN_ON: 5774 default: 5775 rtl8152_apply_firmware(tp, false); 5776 break; 5777 } 5778 5779 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 5780 5781 data = sram_read(tp, SRAM_GREEN_CFG); 5782 data |= R_TUNE_EN; 5783 sram_write(tp, SRAM_GREEN_CFG, data); 5784 data = ocp_reg_read(tp, OCP_NCTL_CFG); 5785 data |= PGA_RETURN_EN; 5786 ocp_reg_write(tp, OCP_NCTL_CFG, data); 5787 5788 /* ADC Bias Calibration: 5789 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake 5790 * bit (bit3) to rebuild the real 16-bit data. Write the data to the 5791 * ADC ioffset. 5792 */ 5793 ocp_data = r8152_efuse_read(tp, 0x7d); 5794 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); 5795 if (data != 0xffff) 5796 ocp_reg_write(tp, OCP_ADC_IOFFSET, data); 5797 5798 /* ups mode tx-link-pulse timing adjustment: 5799 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] 5800 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt 5801 */ 5802 ocp_data = ocp_reg_read(tp, 0xc426); 5803 ocp_data &= 0x3fff; 5804 if (ocp_data) { 5805 u32 swr_cnt_1ms_ini; 5806 5807 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; 5808 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); 5809 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; 5810 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); 5811 } 5812 5813 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 5814 ocp_data |= PFM_PWM_SWITCH; 5815 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 5816 5817 /* Advnace EEE */ 5818 if (!rtl_phy_patch_request(tp, true, true)) { 5819 data = ocp_reg_read(tp, OCP_POWER_CFG); 5820 data |= EEE_CLKDIV_EN; 5821 ocp_reg_write(tp, OCP_POWER_CFG, data); 5822 tp->ups_info.eee_ckdiv = true; 5823 5824 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 5825 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV; 5826 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 5827 tp->ups_info.eee_cmod_lv = true; 5828 tp->ups_info._10m_ckdiv = true; 5829 tp->ups_info.eee_plloff_giga = true; 5830 5831 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); 5832 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); 5833 tp->ups_info._250m_ckdiv = true; 5834 5835 rtl_phy_patch_request(tp, false, true); 5836 } 5837 5838 if (tp->eee_en) 5839 rtl_eee_enable(tp, true); 5840 5841 r8153_aldps_en(tp, true); 5842 r8152b_enable_fc(tp); 5843 5844 set_bit(PHY_RESET, &tp->flags); 5845 } 5846 5847 static void r8153c_hw_phy_cfg(struct r8152 *tp) 5848 { 5849 r8153b_hw_phy_cfg(tp); 5850 5851 tp->ups_info.r_tune = true; 5852 } 5853 5854 static void rtl8153_change_mtu(struct r8152 *tp) 5855 { 5856 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu)); 5857 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); 5858 } 5859 5860 static void r8153_first_init(struct r8152 *tp) 5861 { 5862 u32 ocp_data; 5863 5864 rxdy_gated_en(tp, true); 5865 r8153_teredo_off(tp); 5866 5867 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5868 ocp_data &= ~RCR_ACPT_ALL; 5869 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5870 5871 rtl8152_nic_reset(tp); 5872 rtl_reset_bmu(tp); 5873 5874 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5875 ocp_data &= ~NOW_IS_OOB; 5876 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5877 5878 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5879 ocp_data &= ~MCU_BORW_EN; 5880 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5881 5882 wait_oob_link_list_ready(tp); 5883 5884 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5885 ocp_data |= RE_INIT_LL; 5886 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5887 5888 wait_oob_link_list_ready(tp); 5889 5890 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 5891 5892 rtl8153_change_mtu(tp); 5893 5894 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); 5895 ocp_data |= TCR0_AUTO_FIFO; 5896 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); 5897 5898 rtl8152_nic_reset(tp); 5899 5900 /* rx share fifo credit full threshold */ 5901 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 5902 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); 5903 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); 5904 /* TX share fifo free credit full threshold */ 5905 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); 5906 } 5907 5908 static void r8153_enter_oob(struct r8152 *tp) 5909 { 5910 u32 ocp_data; 5911 5912 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5913 ocp_data &= ~NOW_IS_OOB; 5914 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5915 5916 /* RX FIFO settings for OOB */ 5917 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 5918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 5919 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 5920 5921 rtl_disable(tp); 5922 rtl_reset_bmu(tp); 5923 5924 wait_oob_link_list_ready(tp); 5925 5926 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5927 ocp_data |= RE_INIT_LL; 5928 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5929 5930 wait_oob_link_list_ready(tp); 5931 5932 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522); 5933 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT); 5934 5935 switch (tp->version) { 5936 case RTL_VER_03: 5937 case RTL_VER_04: 5938 case RTL_VER_05: 5939 case RTL_VER_06: 5940 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); 5941 ocp_data &= ~TEREDO_WAKE_MASK; 5942 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); 5943 break; 5944 5945 case RTL_VER_08: 5946 case RTL_VER_09: 5947 case RTL_VER_14: 5948 /* Clear teredo wake event. bit[15:8] is the teredo wakeup 5949 * type. Set it to zero. bits[7:0] are the W1C bits about 5950 * the events. Set them to all 1 to clear them. 5951 */ 5952 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); 5953 break; 5954 5955 default: 5956 break; 5957 } 5958 5959 rtl_rx_vlan_en(tp, true); 5960 5961 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); 5962 ocp_data |= ALDPS_PROXY_MODE; 5963 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); 5964 5965 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 5966 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; 5967 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 5968 5969 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 5970 ocp_data |= MCU_BORW_EN; 5971 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 5972 5973 rxdy_gated_en(tp, false); 5974 5975 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 5976 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 5977 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 5978 } 5979 5980 static void rtl8153_disable(struct r8152 *tp) 5981 { 5982 r8153_aldps_en(tp, false); 5983 rtl_disable(tp); 5984 rtl_reset_bmu(tp); 5985 r8153_aldps_en(tp, true); 5986 } 5987 5988 static u32 fc_pause_on_auto(struct r8152 *tp) 5989 { 5990 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024); 5991 } 5992 5993 static u32 fc_pause_off_auto(struct r8152 *tp) 5994 { 5995 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024); 5996 } 5997 5998 static void r8156_fc_parameter(struct r8152 *tp) 5999 { 6000 u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp); 6001 u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp); 6002 6003 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16); 6004 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16); 6005 } 6006 6007 static int rtl8156_enable(struct r8152 *tp) 6008 { 6009 u32 ocp_data; 6010 u16 speed; 6011 6012 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6013 return -ENODEV; 6014 6015 r8156_fc_parameter(tp); 6016 set_tx_qlen(tp); 6017 rtl_set_eee_plus(tp); 6018 r8153_set_rx_early_timeout(tp); 6019 r8153_set_rx_early_size(tp); 6020 6021 speed = rtl8152_get_speed(tp); 6022 rtl_set_ifg(tp, speed); 6023 6024 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 6025 if (speed & _2500bps) 6026 ocp_data &= ~IDLE_SPDWN_EN; 6027 else 6028 ocp_data |= IDLE_SPDWN_EN; 6029 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 6030 6031 if (speed & _1000bps) 6032 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11); 6033 else if (speed & _500bps) 6034 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d); 6035 6036 if (tp->udev->speed == USB_SPEED_HIGH) { 6037 /* USB 0xb45e[3:0] l1_nyet_hird */ 6038 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL); 6039 ocp_data &= ~0xf; 6040 if (is_flow_control(speed)) 6041 ocp_data |= 0xf; 6042 else 6043 ocp_data |= 0x1; 6044 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data); 6045 } 6046 6047 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 6048 ocp_data &= ~FC_PATCH_TASK; 6049 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 6050 usleep_range(1000, 2000); 6051 ocp_data |= FC_PATCH_TASK; 6052 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 6053 6054 return rtl_enable(tp); 6055 } 6056 6057 static void rtl8156_disable(struct r8152 *tp) 6058 { 6059 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 0); 6060 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 0); 6061 6062 rtl8153_disable(tp); 6063 } 6064 6065 static int rtl8156b_enable(struct r8152 *tp) 6066 { 6067 u32 ocp_data; 6068 u16 speed; 6069 6070 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6071 return -ENODEV; 6072 6073 set_tx_qlen(tp); 6074 rtl_set_eee_plus(tp); 6075 6076 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM); 6077 ocp_data &= ~RX_AGGR_NUM_MASK; 6078 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data); 6079 6080 r8153_set_rx_early_timeout(tp); 6081 r8153_set_rx_early_size(tp); 6082 6083 speed = rtl8152_get_speed(tp); 6084 rtl_set_ifg(tp, speed); 6085 6086 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 6087 if (speed & _2500bps) 6088 ocp_data &= ~IDLE_SPDWN_EN; 6089 else 6090 ocp_data |= IDLE_SPDWN_EN; 6091 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 6092 6093 if (tp->udev->speed == USB_SPEED_HIGH) { 6094 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL); 6095 ocp_data &= ~0xf; 6096 if (is_flow_control(speed)) 6097 ocp_data |= 0xf; 6098 else 6099 ocp_data |= 0x1; 6100 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data); 6101 } 6102 6103 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 6104 ocp_data &= ~FC_PATCH_TASK; 6105 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 6106 usleep_range(1000, 2000); 6107 ocp_data |= FC_PATCH_TASK; 6108 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 6109 6110 return rtl_enable(tp); 6111 } 6112 6113 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, 6114 u32 advertising) 6115 { 6116 u16 bmcr; 6117 int ret = 0; 6118 6119 if (autoneg == AUTONEG_DISABLE) { 6120 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL) 6121 return -EINVAL; 6122 6123 switch (speed) { 6124 case SPEED_10: 6125 bmcr = BMCR_SPEED10; 6126 if (duplex == DUPLEX_FULL) { 6127 bmcr |= BMCR_FULLDPLX; 6128 tp->ups_info.speed_duplex = FORCE_10M_FULL; 6129 } else { 6130 tp->ups_info.speed_duplex = FORCE_10M_HALF; 6131 } 6132 break; 6133 case SPEED_100: 6134 bmcr = BMCR_SPEED100; 6135 if (duplex == DUPLEX_FULL) { 6136 bmcr |= BMCR_FULLDPLX; 6137 tp->ups_info.speed_duplex = FORCE_100M_FULL; 6138 } else { 6139 tp->ups_info.speed_duplex = FORCE_100M_HALF; 6140 } 6141 break; 6142 case SPEED_1000: 6143 if (tp->mii.supports_gmii) { 6144 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX; 6145 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 6146 break; 6147 } 6148 fallthrough; 6149 default: 6150 ret = -EINVAL; 6151 goto out; 6152 } 6153 6154 if (duplex == DUPLEX_FULL) 6155 tp->mii.full_duplex = 1; 6156 else 6157 tp->mii.full_duplex = 0; 6158 6159 tp->mii.force_media = 1; 6160 } else { 6161 u16 orig, new1; 6162 u32 support; 6163 6164 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 6165 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 6166 6167 if (tp->mii.supports_gmii) { 6168 support |= RTL_ADVERTISED_1000_FULL; 6169 6170 if (tp->support_2500full) 6171 support |= RTL_ADVERTISED_2500_FULL; 6172 } 6173 6174 if (!(advertising & support)) 6175 return -EINVAL; 6176 6177 orig = r8152_mdio_read(tp, MII_ADVERTISE); 6178 new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL | 6179 ADVERTISE_100HALF | ADVERTISE_100FULL); 6180 if (advertising & RTL_ADVERTISED_10_HALF) { 6181 new1 |= ADVERTISE_10HALF; 6182 tp->ups_info.speed_duplex = NWAY_10M_HALF; 6183 } 6184 if (advertising & RTL_ADVERTISED_10_FULL) { 6185 new1 |= ADVERTISE_10FULL; 6186 tp->ups_info.speed_duplex = NWAY_10M_FULL; 6187 } 6188 6189 if (advertising & RTL_ADVERTISED_100_HALF) { 6190 new1 |= ADVERTISE_100HALF; 6191 tp->ups_info.speed_duplex = NWAY_100M_HALF; 6192 } 6193 if (advertising & RTL_ADVERTISED_100_FULL) { 6194 new1 |= ADVERTISE_100FULL; 6195 tp->ups_info.speed_duplex = NWAY_100M_FULL; 6196 } 6197 6198 if (orig != new1) { 6199 r8152_mdio_write(tp, MII_ADVERTISE, new1); 6200 tp->mii.advertising = new1; 6201 } 6202 6203 if (tp->mii.supports_gmii) { 6204 orig = r8152_mdio_read(tp, MII_CTRL1000); 6205 new1 = orig & ~(ADVERTISE_1000FULL | 6206 ADVERTISE_1000HALF); 6207 6208 if (advertising & RTL_ADVERTISED_1000_FULL) { 6209 new1 |= ADVERTISE_1000FULL; 6210 tp->ups_info.speed_duplex = NWAY_1000M_FULL; 6211 } 6212 6213 if (orig != new1) 6214 r8152_mdio_write(tp, MII_CTRL1000, new1); 6215 } 6216 6217 if (tp->support_2500full) { 6218 orig = ocp_reg_read(tp, OCP_10GBT_CTRL); 6219 new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G; 6220 6221 if (advertising & RTL_ADVERTISED_2500_FULL) { 6222 new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G; 6223 tp->ups_info.speed_duplex = NWAY_2500M_FULL; 6224 } 6225 6226 if (orig != new1) 6227 ocp_reg_write(tp, OCP_10GBT_CTRL, new1); 6228 } 6229 6230 bmcr = BMCR_ANENABLE | BMCR_ANRESTART; 6231 6232 tp->mii.force_media = 0; 6233 } 6234 6235 if (test_and_clear_bit(PHY_RESET, &tp->flags)) 6236 bmcr |= BMCR_RESET; 6237 6238 r8152_mdio_write(tp, MII_BMCR, bmcr); 6239 6240 if (bmcr & BMCR_RESET) { 6241 int i; 6242 6243 for (i = 0; i < 50; i++) { 6244 msleep(20); 6245 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) 6246 break; 6247 } 6248 } 6249 6250 out: 6251 return ret; 6252 } 6253 6254 static void rtl8152_up(struct r8152 *tp) 6255 { 6256 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6257 return; 6258 6259 r8152_aldps_en(tp, false); 6260 r8152b_exit_oob(tp); 6261 r8152_aldps_en(tp, true); 6262 } 6263 6264 static void rtl8152_down(struct r8152 *tp) 6265 { 6266 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6267 rtl_drop_queued_tx(tp); 6268 return; 6269 } 6270 6271 r8152_power_cut_en(tp, false); 6272 r8152_aldps_en(tp, false); 6273 r8152b_enter_oob(tp); 6274 r8152_aldps_en(tp, true); 6275 } 6276 6277 static void rtl8153_up(struct r8152 *tp) 6278 { 6279 u32 ocp_data; 6280 6281 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6282 return; 6283 6284 r8153_u1u2en(tp, false); 6285 r8153_u2p3en(tp, false); 6286 r8153_aldps_en(tp, false); 6287 r8153_first_init(tp); 6288 6289 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 6290 ocp_data |= LANWAKE_CLR_EN; 6291 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 6292 6293 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG); 6294 ocp_data &= ~LANWAKE_PIN; 6295 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data); 6296 6297 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1); 6298 ocp_data &= ~DELAY_PHY_PWR_CHG; 6299 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data); 6300 6301 r8153_aldps_en(tp, true); 6302 6303 switch (tp->version) { 6304 case RTL_VER_03: 6305 case RTL_VER_04: 6306 break; 6307 case RTL_VER_05: 6308 case RTL_VER_06: 6309 default: 6310 r8153_u2p3en(tp, true); 6311 break; 6312 } 6313 6314 r8153_u1u2en(tp, true); 6315 } 6316 6317 static void rtl8153_down(struct r8152 *tp) 6318 { 6319 u32 ocp_data; 6320 6321 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6322 rtl_drop_queued_tx(tp); 6323 return; 6324 } 6325 6326 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 6327 ocp_data &= ~LANWAKE_CLR_EN; 6328 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 6329 6330 r8153_u1u2en(tp, false); 6331 r8153_u2p3en(tp, false); 6332 r8153_power_cut_en(tp, false); 6333 r8153_aldps_en(tp, false); 6334 r8153_enter_oob(tp); 6335 r8153_aldps_en(tp, true); 6336 } 6337 6338 static void rtl8153b_up(struct r8152 *tp) 6339 { 6340 u32 ocp_data; 6341 6342 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6343 return; 6344 6345 r8153b_u1u2en(tp, false); 6346 r8153_u2p3en(tp, false); 6347 r8153_aldps_en(tp, false); 6348 6349 r8153_first_init(tp); 6350 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); 6351 6352 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6353 ocp_data &= ~PLA_MCU_SPDWN_EN; 6354 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6355 6356 r8153_aldps_en(tp, true); 6357 6358 if (tp->udev->speed >= USB_SPEED_SUPER) 6359 r8153b_u1u2en(tp, true); 6360 } 6361 6362 static void rtl8153b_down(struct r8152 *tp) 6363 { 6364 u32 ocp_data; 6365 6366 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6367 rtl_drop_queued_tx(tp); 6368 return; 6369 } 6370 6371 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6372 ocp_data |= PLA_MCU_SPDWN_EN; 6373 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6374 6375 r8153b_u1u2en(tp, false); 6376 r8153_u2p3en(tp, false); 6377 r8153b_power_cut_en(tp, false); 6378 r8153_aldps_en(tp, false); 6379 r8153_enter_oob(tp); 6380 r8153_aldps_en(tp, true); 6381 } 6382 6383 static void rtl8153c_change_mtu(struct r8152 *tp) 6384 { 6385 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu)); 6386 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64); 6387 6388 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64); 6389 6390 /* Adjust the tx fifo free credit full threshold, otherwise 6391 * the fifo would be too small to send a jumbo frame packet. 6392 */ 6393 if (tp->netdev->mtu < 8000) 6394 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8); 6395 else 6396 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8); 6397 } 6398 6399 static void rtl8153c_up(struct r8152 *tp) 6400 { 6401 u32 ocp_data; 6402 6403 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6404 return; 6405 6406 r8153b_u1u2en(tp, false); 6407 r8153_u2p3en(tp, false); 6408 r8153_aldps_en(tp, false); 6409 6410 rxdy_gated_en(tp, true); 6411 r8153_teredo_off(tp); 6412 6413 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 6414 ocp_data &= ~RCR_ACPT_ALL; 6415 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 6416 6417 rtl8152_nic_reset(tp); 6418 rtl_reset_bmu(tp); 6419 6420 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6421 ocp_data &= ~NOW_IS_OOB; 6422 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6423 6424 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6425 ocp_data &= ~MCU_BORW_EN; 6426 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6427 6428 wait_oob_link_list_ready(tp); 6429 6430 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6431 ocp_data |= RE_INIT_LL; 6432 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6433 6434 wait_oob_link_list_ready(tp); 6435 6436 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 6437 6438 rtl8153c_change_mtu(tp); 6439 6440 rtl8152_nic_reset(tp); 6441 6442 /* rx share fifo credit full threshold */ 6443 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02); 6444 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08); 6445 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); 6446 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); 6447 6448 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); 6449 6450 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 6451 6452 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); 6453 ocp_data |= BIT(8); 6454 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); 6455 6456 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 6457 6458 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6459 ocp_data &= ~PLA_MCU_SPDWN_EN; 6460 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6461 6462 r8153_aldps_en(tp, true); 6463 r8153b_u1u2en(tp, true); 6464 } 6465 6466 static void rtl8156_change_mtu(struct r8152 *tp) 6467 { 6468 u32 rx_max_size = mtu_to_size(tp->netdev->mtu); 6469 6470 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size); 6471 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); 6472 r8156_fc_parameter(tp); 6473 6474 /* TX share fifo free credit full threshold */ 6475 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64); 6476 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 6477 ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16); 6478 } 6479 6480 static void rtl8156_up(struct r8152 *tp) 6481 { 6482 u32 ocp_data; 6483 6484 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6485 return; 6486 6487 r8153b_u1u2en(tp, false); 6488 r8153_u2p3en(tp, false); 6489 r8153_aldps_en(tp, false); 6490 6491 rxdy_gated_en(tp, true); 6492 r8153_teredo_off(tp); 6493 6494 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 6495 ocp_data &= ~RCR_ACPT_ALL; 6496 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 6497 6498 rtl8152_nic_reset(tp); 6499 rtl_reset_bmu(tp); 6500 6501 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6502 ocp_data &= ~NOW_IS_OOB; 6503 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6504 6505 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6506 ocp_data &= ~MCU_BORW_EN; 6507 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6508 6509 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); 6510 6511 rtl8156_change_mtu(tp); 6512 6513 switch (tp->version) { 6514 case RTL_TEST_01: 6515 case RTL_VER_10: 6516 case RTL_VER_11: 6517 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG); 6518 ocp_data |= ACT_ODMA; 6519 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data); 6520 break; 6521 default: 6522 break; 6523 } 6524 6525 /* share FIFO settings */ 6526 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL); 6527 ocp_data &= ~RXFIFO_FULL_MASK; 6528 ocp_data |= 0x08; 6529 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data); 6530 6531 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6532 ocp_data &= ~PLA_MCU_SPDWN_EN; 6533 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6534 6535 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION); 6536 ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF); 6537 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data); 6538 6539 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400); 6540 6541 if (tp->saved_wolopts != __rtl_get_wol(tp)) { 6542 netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n"); 6543 __rtl_set_wol(tp, tp->saved_wolopts); 6544 } 6545 6546 r8153_aldps_en(tp, true); 6547 r8153_u2p3en(tp, true); 6548 6549 if (tp->udev->speed >= USB_SPEED_SUPER) 6550 r8153b_u1u2en(tp, true); 6551 } 6552 6553 static void rtl8156_down(struct r8152 *tp) 6554 { 6555 u32 ocp_data; 6556 6557 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6558 rtl_drop_queued_tx(tp); 6559 return; 6560 } 6561 6562 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 6563 ocp_data |= PLA_MCU_SPDWN_EN; 6564 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 6565 6566 r8153b_u1u2en(tp, false); 6567 r8153_u2p3en(tp, false); 6568 r8153b_power_cut_en(tp, false); 6569 r8153_aldps_en(tp, false); 6570 6571 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6572 ocp_data &= ~NOW_IS_OOB; 6573 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6574 6575 /* RX FIFO settings for OOB */ 6576 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 64 / 16); 6577 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 1024 / 16); 6578 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 4096 / 16); 6579 6580 rtl_disable(tp); 6581 rtl_reset_bmu(tp); 6582 6583 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522); 6584 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT); 6585 6586 /* Clear teredo wake event. bit[15:8] is the teredo wakeup 6587 * type. Set it to zero. bits[7:0] are the W1C bits about 6588 * the events. Set them to all 1 to clear them. 6589 */ 6590 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); 6591 6592 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 6593 ocp_data |= NOW_IS_OOB; 6594 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 6595 6596 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 6597 ocp_data |= MCU_BORW_EN; 6598 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 6599 6600 rtl_rx_vlan_en(tp, true); 6601 rxdy_gated_en(tp, false); 6602 6603 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 6604 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 6605 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 6606 6607 r8153_aldps_en(tp, true); 6608 } 6609 6610 static bool rtl8152_in_nway(struct r8152 *tp) 6611 { 6612 u16 nway_state; 6613 6614 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); 6615 tp->ocp_base = 0x2000; 6616 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ 6617 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); 6618 6619 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ 6620 if (nway_state & 0xc000) 6621 return false; 6622 else 6623 return true; 6624 } 6625 6626 static bool rtl8153_in_nway(struct r8152 *tp) 6627 { 6628 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; 6629 6630 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) 6631 return false; 6632 else 6633 return true; 6634 } 6635 6636 static void r8156_mdio_force_mode(struct r8152 *tp) 6637 { 6638 u16 data; 6639 6640 /* Select force mode through 0xa5b4 bit 15 6641 * 0: MDIO force mode 6642 * 1: MMD force mode 6643 */ 6644 data = ocp_reg_read(tp, 0xa5b4); 6645 if (data & BIT(15)) { 6646 data &= ~BIT(15); 6647 ocp_reg_write(tp, 0xa5b4, data); 6648 } 6649 } 6650 6651 static void set_carrier(struct r8152 *tp) 6652 { 6653 struct net_device *netdev = tp->netdev; 6654 struct napi_struct *napi = &tp->napi; 6655 u16 speed; 6656 6657 speed = rtl8152_get_speed(tp); 6658 6659 if (speed & LINK_STATUS) { 6660 if (!netif_carrier_ok(netdev)) { 6661 tp->rtl_ops.enable(tp); 6662 netif_stop_queue(netdev); 6663 napi_disable(napi); 6664 netif_carrier_on(netdev); 6665 rtl_start_rx(tp); 6666 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 6667 _rtl8152_set_rx_mode(netdev); 6668 napi_enable(napi); 6669 netif_wake_queue(netdev); 6670 netif_info(tp, link, netdev, "carrier on\n"); 6671 } else if (netif_queue_stopped(netdev) && 6672 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { 6673 netif_wake_queue(netdev); 6674 } 6675 } else { 6676 if (netif_carrier_ok(netdev)) { 6677 netif_carrier_off(netdev); 6678 tasklet_disable(&tp->tx_tl); 6679 napi_disable(napi); 6680 tp->rtl_ops.disable(tp); 6681 napi_enable(napi); 6682 tasklet_enable(&tp->tx_tl); 6683 netif_info(tp, link, netdev, "carrier off\n"); 6684 } 6685 } 6686 } 6687 6688 static void rtl_work_func_t(struct work_struct *work) 6689 { 6690 struct r8152 *tp = container_of(work, struct r8152, schedule.work); 6691 6692 /* If the device is unplugged or !netif_running(), the workqueue 6693 * doesn't need to wake the device, and could return directly. 6694 */ 6695 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) || !netif_running(tp->netdev)) 6696 return; 6697 6698 if (usb_autopm_get_interface(tp->intf) < 0) 6699 return; 6700 6701 if (!test_bit(WORK_ENABLE, &tp->flags)) 6702 goto out1; 6703 6704 if (!mutex_trylock(&tp->control)) { 6705 schedule_delayed_work(&tp->schedule, 0); 6706 goto out1; 6707 } 6708 6709 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) 6710 set_carrier(tp); 6711 6712 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) 6713 _rtl8152_set_rx_mode(tp->netdev); 6714 6715 /* don't schedule tasket before linking */ 6716 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) && 6717 netif_carrier_ok(tp->netdev)) 6718 tasklet_schedule(&tp->tx_tl); 6719 6720 if (test_and_clear_bit(RX_EPROTO, &tp->flags) && 6721 !list_empty(&tp->rx_done)) 6722 napi_schedule(&tp->napi); 6723 6724 mutex_unlock(&tp->control); 6725 6726 out1: 6727 usb_autopm_put_interface(tp->intf); 6728 } 6729 6730 static void rtl_hw_phy_work_func_t(struct work_struct *work) 6731 { 6732 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); 6733 6734 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6735 return; 6736 6737 if (usb_autopm_get_interface(tp->intf) < 0) 6738 return; 6739 6740 mutex_lock(&tp->control); 6741 6742 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) { 6743 tp->rtl_fw.retry = false; 6744 tp->rtl_fw.fw = NULL; 6745 6746 /* Delay execution in case request_firmware() is not ready yet. 6747 */ 6748 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10); 6749 goto ignore_once; 6750 } 6751 6752 tp->rtl_ops.hw_phy_cfg(tp); 6753 6754 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex, 6755 tp->advertising); 6756 6757 ignore_once: 6758 mutex_unlock(&tp->control); 6759 6760 usb_autopm_put_interface(tp->intf); 6761 } 6762 6763 #ifdef CONFIG_PM_SLEEP 6764 static int rtl_notifier(struct notifier_block *nb, unsigned long action, 6765 void *data) 6766 { 6767 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); 6768 6769 switch (action) { 6770 case PM_HIBERNATION_PREPARE: 6771 case PM_SUSPEND_PREPARE: 6772 usb_autopm_get_interface(tp->intf); 6773 break; 6774 6775 case PM_POST_HIBERNATION: 6776 case PM_POST_SUSPEND: 6777 usb_autopm_put_interface(tp->intf); 6778 break; 6779 6780 case PM_POST_RESTORE: 6781 case PM_RESTORE_PREPARE: 6782 default: 6783 break; 6784 } 6785 6786 return NOTIFY_DONE; 6787 } 6788 #endif 6789 6790 static int rtl8152_open(struct net_device *netdev) 6791 { 6792 struct r8152 *tp = netdev_priv(netdev); 6793 int res = 0; 6794 6795 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) { 6796 cancel_delayed_work_sync(&tp->hw_phy_work); 6797 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work); 6798 } 6799 6800 res = alloc_all_mem(tp); 6801 if (res) 6802 goto out; 6803 6804 res = usb_autopm_get_interface(tp->intf); 6805 if (res < 0) 6806 goto out_free; 6807 6808 mutex_lock(&tp->control); 6809 6810 tp->rtl_ops.up(tp); 6811 6812 netif_carrier_off(netdev); 6813 netif_start_queue(netdev); 6814 set_bit(WORK_ENABLE, &tp->flags); 6815 6816 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); 6817 if (res) { 6818 if (res == -ENODEV) 6819 netif_device_detach(tp->netdev); 6820 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", 6821 res); 6822 goto out_unlock; 6823 } 6824 napi_enable(&tp->napi); 6825 tasklet_enable(&tp->tx_tl); 6826 6827 mutex_unlock(&tp->control); 6828 6829 usb_autopm_put_interface(tp->intf); 6830 #ifdef CONFIG_PM_SLEEP 6831 tp->pm_notifier.notifier_call = rtl_notifier; 6832 register_pm_notifier(&tp->pm_notifier); 6833 #endif 6834 return 0; 6835 6836 out_unlock: 6837 mutex_unlock(&tp->control); 6838 usb_autopm_put_interface(tp->intf); 6839 out_free: 6840 free_all_mem(tp); 6841 out: 6842 return res; 6843 } 6844 6845 static int rtl8152_close(struct net_device *netdev) 6846 { 6847 struct r8152 *tp = netdev_priv(netdev); 6848 int res = 0; 6849 6850 #ifdef CONFIG_PM_SLEEP 6851 unregister_pm_notifier(&tp->pm_notifier); 6852 #endif 6853 tasklet_disable(&tp->tx_tl); 6854 clear_bit(WORK_ENABLE, &tp->flags); 6855 usb_kill_urb(tp->intr_urb); 6856 cancel_delayed_work_sync(&tp->schedule); 6857 napi_disable(&tp->napi); 6858 netif_stop_queue(netdev); 6859 6860 res = usb_autopm_get_interface(tp->intf); 6861 if (res < 0 || test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { 6862 rtl_drop_queued_tx(tp); 6863 rtl_stop_rx(tp); 6864 } else { 6865 mutex_lock(&tp->control); 6866 6867 tp->rtl_ops.down(tp); 6868 6869 mutex_unlock(&tp->control); 6870 } 6871 6872 if (!res) 6873 usb_autopm_put_interface(tp->intf); 6874 6875 free_all_mem(tp); 6876 6877 return res; 6878 } 6879 6880 static void rtl_tally_reset(struct r8152 *tp) 6881 { 6882 u32 ocp_data; 6883 6884 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); 6885 ocp_data |= TALLY_RESET; 6886 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); 6887 } 6888 6889 static void r8152b_init(struct r8152 *tp) 6890 { 6891 u32 ocp_data; 6892 u16 data; 6893 6894 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6895 return; 6896 6897 data = r8152_mdio_read(tp, MII_BMCR); 6898 if (data & BMCR_PDOWN) { 6899 data &= ~BMCR_PDOWN; 6900 r8152_mdio_write(tp, MII_BMCR, data); 6901 } 6902 6903 r8152_aldps_en(tp, false); 6904 6905 if (tp->version == RTL_VER_01) { 6906 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); 6907 ocp_data &= ~LED_MODE_MASK; 6908 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); 6909 } 6910 6911 r8152_power_cut_en(tp, false); 6912 6913 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 6914 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; 6915 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 6916 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); 6917 ocp_data &= ~MCU_CLK_RATIO_MASK; 6918 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; 6919 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); 6920 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | 6921 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; 6922 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); 6923 6924 rtl_tally_reset(tp); 6925 6926 /* enable rx aggregation */ 6927 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 6928 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 6929 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 6930 } 6931 6932 static void r8153_init(struct r8152 *tp) 6933 { 6934 u32 ocp_data; 6935 u16 data; 6936 int i; 6937 6938 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6939 return; 6940 6941 r8153_u1u2en(tp, false); 6942 6943 for (i = 0; i < 500; i++) { 6944 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 6945 AUTOLOAD_DONE) 6946 break; 6947 6948 msleep(20); 6949 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 6950 break; 6951 } 6952 6953 data = r8153_phy_status(tp, 0); 6954 6955 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || 6956 tp->version == RTL_VER_05) 6957 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); 6958 6959 data = r8152_mdio_read(tp, MII_BMCR); 6960 if (data & BMCR_PDOWN) { 6961 data &= ~BMCR_PDOWN; 6962 r8152_mdio_write(tp, MII_BMCR, data); 6963 } 6964 6965 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 6966 6967 r8153_u2p3en(tp, false); 6968 6969 if (tp->version == RTL_VER_04) { 6970 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); 6971 ocp_data &= ~pwd_dn_scale_mask; 6972 ocp_data |= pwd_dn_scale(96); 6973 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); 6974 6975 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); 6976 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; 6977 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); 6978 } else if (tp->version == RTL_VER_05) { 6979 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); 6980 ocp_data &= ~ECM_ALDPS; 6981 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); 6982 6983 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); 6984 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 6985 ocp_data &= ~DYNAMIC_BURST; 6986 else 6987 ocp_data |= DYNAMIC_BURST; 6988 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); 6989 } else if (tp->version == RTL_VER_06) { 6990 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); 6991 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) 6992 ocp_data &= ~DYNAMIC_BURST; 6993 else 6994 ocp_data |= DYNAMIC_BURST; 6995 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); 6996 6997 r8153_queue_wake(tp, false); 6998 6999 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 7000 if (rtl8152_get_speed(tp) & LINK_STATUS) 7001 ocp_data |= CUR_LINK_OK; 7002 else 7003 ocp_data &= ~CUR_LINK_OK; 7004 ocp_data |= POLL_LINK_CHG; 7005 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 7006 } 7007 7008 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); 7009 ocp_data |= EP4_FULL_FC; 7010 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); 7011 7012 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); 7013 ocp_data &= ~TIMER11_EN; 7014 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); 7015 7016 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); 7017 ocp_data &= ~LED_MODE_MASK; 7018 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); 7019 7020 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; 7021 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) 7022 ocp_data |= LPM_TIMER_500MS; 7023 else 7024 ocp_data |= LPM_TIMER_500US; 7025 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); 7026 7027 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); 7028 ocp_data &= ~SEN_VAL_MASK; 7029 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; 7030 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); 7031 7032 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); 7033 7034 r8153_power_cut_en(tp, false); 7035 rtl_runtime_suspend_enable(tp, false); 7036 r8153_mac_clk_speed_down(tp, false); 7037 r8153_u1u2en(tp, true); 7038 usb_enable_lpm(tp->udev); 7039 7040 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6); 7041 ocp_data |= LANWAKE_CLR_EN; 7042 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data); 7043 7044 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG); 7045 ocp_data &= ~LANWAKE_PIN; 7046 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data); 7047 7048 /* rx aggregation */ 7049 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 7050 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 7051 if (tp->dell_tb_rx_agg_bug) 7052 ocp_data |= RX_AGG_DISABLE; 7053 7054 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 7055 7056 rtl_tally_reset(tp); 7057 7058 switch (tp->udev->speed) { 7059 case USB_SPEED_SUPER: 7060 case USB_SPEED_SUPER_PLUS: 7061 tp->coalesce = COALESCE_SUPER; 7062 break; 7063 case USB_SPEED_HIGH: 7064 tp->coalesce = COALESCE_HIGH; 7065 break; 7066 default: 7067 tp->coalesce = COALESCE_SLOW; 7068 break; 7069 } 7070 } 7071 7072 static void r8153b_init(struct r8152 *tp) 7073 { 7074 u32 ocp_data; 7075 u16 data; 7076 int i; 7077 7078 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7079 return; 7080 7081 r8153b_u1u2en(tp, false); 7082 7083 for (i = 0; i < 500; i++) { 7084 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7085 AUTOLOAD_DONE) 7086 break; 7087 7088 msleep(20); 7089 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7090 break; 7091 } 7092 7093 data = r8153_phy_status(tp, 0); 7094 7095 data = r8152_mdio_read(tp, MII_BMCR); 7096 if (data & BMCR_PDOWN) { 7097 data &= ~BMCR_PDOWN; 7098 r8152_mdio_write(tp, MII_BMCR, data); 7099 } 7100 7101 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7102 7103 r8153_u2p3en(tp, false); 7104 7105 /* MSC timer = 0xfff * 8ms = 32760 ms */ 7106 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 7107 7108 r8153b_power_cut_en(tp, false); 7109 r8153b_ups_en(tp, false); 7110 r8153_queue_wake(tp, false); 7111 rtl_runtime_suspend_enable(tp, false); 7112 7113 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 7114 if (rtl8152_get_speed(tp) & LINK_STATUS) 7115 ocp_data |= CUR_LINK_OK; 7116 else 7117 ocp_data &= ~CUR_LINK_OK; 7118 ocp_data |= POLL_LINK_CHG; 7119 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 7120 7121 if (tp->udev->speed >= USB_SPEED_SUPER) 7122 r8153b_u1u2en(tp, true); 7123 7124 usb_enable_lpm(tp->udev); 7125 7126 /* MAC clock speed down */ 7127 r8153_mac_clk_speed_down(tp, true); 7128 7129 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 7130 ocp_data &= ~PLA_MCU_SPDWN_EN; 7131 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 7132 7133 if (tp->version == RTL_VER_09) { 7134 /* Disable Test IO for 32QFN */ 7135 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) { 7136 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 7137 ocp_data |= TEST_IO_OFF; 7138 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 7139 } 7140 } 7141 7142 set_bit(GREEN_ETHERNET, &tp->flags); 7143 7144 /* rx aggregation */ 7145 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 7146 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 7147 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 7148 7149 rtl_tally_reset(tp); 7150 7151 tp->coalesce = 15000; /* 15 us */ 7152 } 7153 7154 static void r8153c_init(struct r8152 *tp) 7155 { 7156 u32 ocp_data; 7157 u16 data; 7158 int i; 7159 7160 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7161 return; 7162 7163 r8153b_u1u2en(tp, false); 7164 7165 /* Disable spi_en */ 7166 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 7167 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); 7168 ocp_data &= ~BIT(3); 7169 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); 7170 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0); 7171 ocp_data |= BIT(1); 7172 ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data); 7173 7174 for (i = 0; i < 500; i++) { 7175 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 7176 AUTOLOAD_DONE) 7177 break; 7178 7179 msleep(20); 7180 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 7181 return; 7182 } 7183 7184 data = r8153_phy_status(tp, 0); 7185 7186 data = r8152_mdio_read(tp, MII_BMCR); 7187 if (data & BMCR_PDOWN) { 7188 data &= ~BMCR_PDOWN; 7189 r8152_mdio_write(tp, MII_BMCR, data); 7190 } 7191 7192 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7193 7194 r8153_u2p3en(tp, false); 7195 7196 /* MSC timer = 0xfff * 8ms = 32760 ms */ 7197 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 7198 7199 r8153b_power_cut_en(tp, false); 7200 r8153c_ups_en(tp, false); 7201 r8153_queue_wake(tp, false); 7202 rtl_runtime_suspend_enable(tp, false); 7203 7204 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 7205 if (rtl8152_get_speed(tp) & LINK_STATUS) 7206 ocp_data |= CUR_LINK_OK; 7207 else 7208 ocp_data &= ~CUR_LINK_OK; 7209 7210 ocp_data |= POLL_LINK_CHG; 7211 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 7212 7213 r8153b_u1u2en(tp, true); 7214 7215 usb_enable_lpm(tp->udev); 7216 7217 /* MAC clock speed down */ 7218 r8153_mac_clk_speed_down(tp, true); 7219 7220 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2); 7221 ocp_data &= ~BIT(7); 7222 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data); 7223 7224 set_bit(GREEN_ETHERNET, &tp->flags); 7225 7226 /* rx aggregation */ 7227 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 7228 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 7229 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 7230 7231 rtl_tally_reset(tp); 7232 7233 tp->coalesce = 15000; /* 15 us */ 7234 } 7235 7236 static void r8156_hw_phy_cfg(struct r8152 *tp) 7237 { 7238 u32 ocp_data; 7239 u16 data; 7240 7241 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 7242 if (ocp_data & PCUT_STATUS) { 7243 ocp_data &= ~PCUT_STATUS; 7244 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 7245 } 7246 7247 data = r8153_phy_status(tp, 0); 7248 switch (data) { 7249 case PHY_STAT_EXT_INIT: 7250 rtl8152_apply_firmware(tp, true); 7251 7252 data = ocp_reg_read(tp, 0xa468); 7253 data &= ~(BIT(3) | BIT(1)); 7254 ocp_reg_write(tp, 0xa468, data); 7255 break; 7256 case PHY_STAT_LAN_ON: 7257 case PHY_STAT_PWRDN: 7258 default: 7259 rtl8152_apply_firmware(tp, false); 7260 break; 7261 } 7262 7263 /* disable ALDPS before updating the PHY parameters */ 7264 r8153_aldps_en(tp, false); 7265 7266 /* disable EEE before updating the PHY parameters */ 7267 rtl_eee_enable(tp, false); 7268 7269 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7270 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 7271 7272 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 7273 ocp_data |= PFM_PWM_SWITCH; 7274 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 7275 7276 switch (tp->version) { 7277 case RTL_VER_10: 7278 data = ocp_reg_read(tp, 0xad40); 7279 data &= ~0x3ff; 7280 data |= BIT(7) | BIT(2); 7281 ocp_reg_write(tp, 0xad40, data); 7282 7283 data = ocp_reg_read(tp, 0xad4e); 7284 data |= BIT(4); 7285 ocp_reg_write(tp, 0xad4e, data); 7286 data = ocp_reg_read(tp, 0xad16); 7287 data &= ~0x3ff; 7288 data |= 0x6; 7289 ocp_reg_write(tp, 0xad16, data); 7290 data = ocp_reg_read(tp, 0xad32); 7291 data &= ~0x3f; 7292 data |= 6; 7293 ocp_reg_write(tp, 0xad32, data); 7294 data = ocp_reg_read(tp, 0xac08); 7295 data &= ~(BIT(12) | BIT(8)); 7296 ocp_reg_write(tp, 0xac08, data); 7297 data = ocp_reg_read(tp, 0xac8a); 7298 data |= BIT(12) | BIT(13) | BIT(14); 7299 data &= ~BIT(15); 7300 ocp_reg_write(tp, 0xac8a, data); 7301 data = ocp_reg_read(tp, 0xad18); 7302 data |= BIT(10); 7303 ocp_reg_write(tp, 0xad18, data); 7304 data = ocp_reg_read(tp, 0xad1a); 7305 data |= 0x3ff; 7306 ocp_reg_write(tp, 0xad1a, data); 7307 data = ocp_reg_read(tp, 0xad1c); 7308 data |= 0x3ff; 7309 ocp_reg_write(tp, 0xad1c, data); 7310 7311 data = sram_read(tp, 0x80ea); 7312 data &= ~0xff00; 7313 data |= 0xc400; 7314 sram_write(tp, 0x80ea, data); 7315 data = sram_read(tp, 0x80eb); 7316 data &= ~0x0700; 7317 data |= 0x0300; 7318 sram_write(tp, 0x80eb, data); 7319 data = sram_read(tp, 0x80f8); 7320 data &= ~0xff00; 7321 data |= 0x1c00; 7322 sram_write(tp, 0x80f8, data); 7323 data = sram_read(tp, 0x80f1); 7324 data &= ~0xff00; 7325 data |= 0x3000; 7326 sram_write(tp, 0x80f1, data); 7327 7328 data = sram_read(tp, 0x80fe); 7329 data &= ~0xff00; 7330 data |= 0xa500; 7331 sram_write(tp, 0x80fe, data); 7332 data = sram_read(tp, 0x8102); 7333 data &= ~0xff00; 7334 data |= 0x5000; 7335 sram_write(tp, 0x8102, data); 7336 data = sram_read(tp, 0x8015); 7337 data &= ~0xff00; 7338 data |= 0x3300; 7339 sram_write(tp, 0x8015, data); 7340 data = sram_read(tp, 0x8100); 7341 data &= ~0xff00; 7342 data |= 0x7000; 7343 sram_write(tp, 0x8100, data); 7344 data = sram_read(tp, 0x8014); 7345 data &= ~0xff00; 7346 data |= 0xf000; 7347 sram_write(tp, 0x8014, data); 7348 data = sram_read(tp, 0x8016); 7349 data &= ~0xff00; 7350 data |= 0x6500; 7351 sram_write(tp, 0x8016, data); 7352 data = sram_read(tp, 0x80dc); 7353 data &= ~0xff00; 7354 data |= 0xed00; 7355 sram_write(tp, 0x80dc, data); 7356 data = sram_read(tp, 0x80df); 7357 data |= BIT(8); 7358 sram_write(tp, 0x80df, data); 7359 data = sram_read(tp, 0x80e1); 7360 data &= ~BIT(8); 7361 sram_write(tp, 0x80e1, data); 7362 7363 data = ocp_reg_read(tp, 0xbf06); 7364 data &= ~0x003f; 7365 data |= 0x0038; 7366 ocp_reg_write(tp, 0xbf06, data); 7367 7368 sram_write(tp, 0x819f, 0xddb6); 7369 7370 ocp_reg_write(tp, 0xbc34, 0x5555); 7371 data = ocp_reg_read(tp, 0xbf0a); 7372 data &= ~0x0e00; 7373 data |= 0x0a00; 7374 ocp_reg_write(tp, 0xbf0a, data); 7375 7376 data = ocp_reg_read(tp, 0xbd2c); 7377 data &= ~BIT(13); 7378 ocp_reg_write(tp, 0xbd2c, data); 7379 break; 7380 case RTL_VER_11: 7381 data = ocp_reg_read(tp, 0xad16); 7382 data |= 0x3ff; 7383 ocp_reg_write(tp, 0xad16, data); 7384 data = ocp_reg_read(tp, 0xad32); 7385 data &= ~0x3f; 7386 data |= 6; 7387 ocp_reg_write(tp, 0xad32, data); 7388 data = ocp_reg_read(tp, 0xac08); 7389 data &= ~(BIT(12) | BIT(8)); 7390 ocp_reg_write(tp, 0xac08, data); 7391 data = ocp_reg_read(tp, 0xacc0); 7392 data &= ~0x3; 7393 data |= BIT(1); 7394 ocp_reg_write(tp, 0xacc0, data); 7395 data = ocp_reg_read(tp, 0xad40); 7396 data &= ~0xe7; 7397 data |= BIT(6) | BIT(2); 7398 ocp_reg_write(tp, 0xad40, data); 7399 data = ocp_reg_read(tp, 0xac14); 7400 data &= ~BIT(7); 7401 ocp_reg_write(tp, 0xac14, data); 7402 data = ocp_reg_read(tp, 0xac80); 7403 data &= ~(BIT(8) | BIT(9)); 7404 ocp_reg_write(tp, 0xac80, data); 7405 data = ocp_reg_read(tp, 0xac5e); 7406 data &= ~0x7; 7407 data |= BIT(1); 7408 ocp_reg_write(tp, 0xac5e, data); 7409 ocp_reg_write(tp, 0xad4c, 0x00a8); 7410 ocp_reg_write(tp, 0xac5c, 0x01ff); 7411 data = ocp_reg_read(tp, 0xac8a); 7412 data &= ~0xf0; 7413 data |= BIT(4) | BIT(5); 7414 ocp_reg_write(tp, 0xac8a, data); 7415 ocp_reg_write(tp, 0xb87c, 0x8157); 7416 data = ocp_reg_read(tp, 0xb87e); 7417 data &= ~0xff00; 7418 data |= 0x0500; 7419 ocp_reg_write(tp, 0xb87e, data); 7420 ocp_reg_write(tp, 0xb87c, 0x8159); 7421 data = ocp_reg_read(tp, 0xb87e); 7422 data &= ~0xff00; 7423 data |= 0x0700; 7424 ocp_reg_write(tp, 0xb87e, data); 7425 7426 /* AAGC */ 7427 ocp_reg_write(tp, 0xb87c, 0x80a2); 7428 ocp_reg_write(tp, 0xb87e, 0x0153); 7429 ocp_reg_write(tp, 0xb87c, 0x809c); 7430 ocp_reg_write(tp, 0xb87e, 0x0153); 7431 7432 /* EEE parameter */ 7433 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056); 7434 7435 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG); 7436 ocp_data |= EN_XG_LIP | EN_G_LIP; 7437 ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data); 7438 7439 sram_write(tp, 0x8257, 0x020f); /* XG PLL */ 7440 sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */ 7441 7442 if (rtl_phy_patch_request(tp, true, true)) 7443 return; 7444 7445 /* Advance EEE */ 7446 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 7447 ocp_data |= EEE_SPDWN_EN; 7448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 7449 7450 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 7451 data &= ~(EN_EEE_100 | EN_EEE_1000); 7452 data |= EN_10M_CLKDIV; 7453 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 7454 tp->ups_info._10m_ckdiv = true; 7455 tp->ups_info.eee_plloff_100 = false; 7456 tp->ups_info.eee_plloff_giga = false; 7457 7458 data = ocp_reg_read(tp, OCP_POWER_CFG); 7459 data &= ~EEE_CLKDIV_EN; 7460 ocp_reg_write(tp, OCP_POWER_CFG, data); 7461 tp->ups_info.eee_ckdiv = false; 7462 7463 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); 7464 ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5)); 7465 tp->ups_info._250m_ckdiv = false; 7466 7467 rtl_phy_patch_request(tp, false, true); 7468 7469 /* enable ADC Ibias Cal */ 7470 data = ocp_reg_read(tp, 0xd068); 7471 data |= BIT(13); 7472 ocp_reg_write(tp, 0xd068, data); 7473 7474 /* enable Thermal Sensor */ 7475 data = sram_read(tp, 0x81a2); 7476 data &= ~BIT(8); 7477 sram_write(tp, 0x81a2, data); 7478 data = ocp_reg_read(tp, 0xb54c); 7479 data &= ~0xff00; 7480 data |= 0xdb00; 7481 ocp_reg_write(tp, 0xb54c, data); 7482 7483 /* Nway 2.5G Lite */ 7484 data = ocp_reg_read(tp, 0xa454); 7485 data &= ~BIT(0); 7486 ocp_reg_write(tp, 0xa454, data); 7487 7488 /* CS DSP solution */ 7489 data = ocp_reg_read(tp, OCP_10GBT_CTRL); 7490 data |= RTL_ADV2_5G_F_R; 7491 ocp_reg_write(tp, OCP_10GBT_CTRL, data); 7492 data = ocp_reg_read(tp, 0xad4e); 7493 data &= ~BIT(4); 7494 ocp_reg_write(tp, 0xad4e, data); 7495 data = ocp_reg_read(tp, 0xa86a); 7496 data &= ~BIT(0); 7497 ocp_reg_write(tp, 0xa86a, data); 7498 7499 /* MDI SWAP */ 7500 if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) && 7501 (ocp_reg_read(tp, 0xd068) & BIT(1))) { 7502 u16 swap_a, swap_b; 7503 7504 data = ocp_reg_read(tp, 0xd068); 7505 data &= ~0x1f; 7506 data |= 0x1; /* p0 */ 7507 ocp_reg_write(tp, 0xd068, data); 7508 swap_a = ocp_reg_read(tp, 0xd06a); 7509 data &= ~0x18; 7510 data |= 0x18; /* p3 */ 7511 ocp_reg_write(tp, 0xd068, data); 7512 swap_b = ocp_reg_read(tp, 0xd06a); 7513 data &= ~0x18; /* p0 */ 7514 ocp_reg_write(tp, 0xd068, data); 7515 ocp_reg_write(tp, 0xd06a, 7516 (swap_a & ~0x7ff) | (swap_b & 0x7ff)); 7517 data |= 0x18; /* p3 */ 7518 ocp_reg_write(tp, 0xd068, data); 7519 ocp_reg_write(tp, 0xd06a, 7520 (swap_b & ~0x7ff) | (swap_a & 0x7ff)); 7521 data &= ~0x18; 7522 data |= 0x08; /* p1 */ 7523 ocp_reg_write(tp, 0xd068, data); 7524 swap_a = ocp_reg_read(tp, 0xd06a); 7525 data &= ~0x18; 7526 data |= 0x10; /* p2 */ 7527 ocp_reg_write(tp, 0xd068, data); 7528 swap_b = ocp_reg_read(tp, 0xd06a); 7529 data &= ~0x18; 7530 data |= 0x08; /* p1 */ 7531 ocp_reg_write(tp, 0xd068, data); 7532 ocp_reg_write(tp, 0xd06a, 7533 (swap_a & ~0x7ff) | (swap_b & 0x7ff)); 7534 data &= ~0x18; 7535 data |= 0x10; /* p2 */ 7536 ocp_reg_write(tp, 0xd068, data); 7537 ocp_reg_write(tp, 0xd06a, 7538 (swap_b & ~0x7ff) | (swap_a & 0x7ff)); 7539 swap_a = ocp_reg_read(tp, 0xbd5a); 7540 swap_b = ocp_reg_read(tp, 0xbd5c); 7541 ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) | 7542 ((swap_b & 0x1f) << 8) | 7543 ((swap_b >> 8) & 0x1f)); 7544 ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) | 7545 ((swap_a & 0x1f) << 8) | 7546 ((swap_a >> 8) & 0x1f)); 7547 swap_a = ocp_reg_read(tp, 0xbc18); 7548 swap_b = ocp_reg_read(tp, 0xbc1a); 7549 ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) | 7550 ((swap_b & 0x1f) << 8) | 7551 ((swap_b >> 8) & 0x1f)); 7552 ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) | 7553 ((swap_a & 0x1f) << 8) | 7554 ((swap_a >> 8) & 0x1f)); 7555 } 7556 7557 /* Notify the MAC when the speed is changed to force mode. */ 7558 data = ocp_reg_read(tp, OCP_INTR_EN); 7559 data |= INTR_SPEED_FORCE; 7560 ocp_reg_write(tp, OCP_INTR_EN, data); 7561 break; 7562 default: 7563 break; 7564 } 7565 7566 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 7567 7568 data = ocp_reg_read(tp, 0xa428); 7569 data &= ~BIT(9); 7570 ocp_reg_write(tp, 0xa428, data); 7571 data = ocp_reg_read(tp, 0xa5ea); 7572 data &= ~BIT(0); 7573 ocp_reg_write(tp, 0xa5ea, data); 7574 tp->ups_info.lite_mode = 0; 7575 7576 if (tp->eee_en) 7577 rtl_eee_enable(tp, true); 7578 7579 r8153_aldps_en(tp, true); 7580 r8152b_enable_fc(tp); 7581 r8153_u2p3en(tp, true); 7582 7583 set_bit(PHY_RESET, &tp->flags); 7584 } 7585 7586 static void r8156b_hw_phy_cfg(struct r8152 *tp) 7587 { 7588 u32 ocp_data; 7589 u16 data; 7590 7591 switch (tp->version) { 7592 case RTL_VER_12: 7593 ocp_reg_write(tp, 0xbf86, 0x9000); 7594 data = ocp_reg_read(tp, 0xc402); 7595 data |= BIT(10); 7596 ocp_reg_write(tp, 0xc402, data); 7597 data &= ~BIT(10); 7598 ocp_reg_write(tp, 0xc402, data); 7599 ocp_reg_write(tp, 0xbd86, 0x1010); 7600 ocp_reg_write(tp, 0xbd88, 0x1010); 7601 data = ocp_reg_read(tp, 0xbd4e); 7602 data &= ~(BIT(10) | BIT(11)); 7603 data |= BIT(11); 7604 ocp_reg_write(tp, 0xbd4e, data); 7605 data = ocp_reg_read(tp, 0xbf46); 7606 data &= ~0xf00; 7607 data |= 0x700; 7608 ocp_reg_write(tp, 0xbf46, data); 7609 break; 7610 case RTL_VER_13: 7611 case RTL_VER_15: 7612 r8156b_wait_loading_flash(tp); 7613 break; 7614 default: 7615 break; 7616 } 7617 7618 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); 7619 if (ocp_data & PCUT_STATUS) { 7620 ocp_data &= ~PCUT_STATUS; 7621 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); 7622 } 7623 7624 data = r8153_phy_status(tp, 0); 7625 switch (data) { 7626 case PHY_STAT_EXT_INIT: 7627 rtl8152_apply_firmware(tp, true); 7628 7629 data = ocp_reg_read(tp, 0xa466); 7630 data &= ~BIT(0); 7631 ocp_reg_write(tp, 0xa466, data); 7632 7633 data = ocp_reg_read(tp, 0xa468); 7634 data &= ~(BIT(3) | BIT(1)); 7635 ocp_reg_write(tp, 0xa468, data); 7636 break; 7637 case PHY_STAT_LAN_ON: 7638 case PHY_STAT_PWRDN: 7639 default: 7640 rtl8152_apply_firmware(tp, false); 7641 break; 7642 } 7643 7644 data = r8152_mdio_read(tp, MII_BMCR); 7645 if (data & BMCR_PDOWN) { 7646 data &= ~BMCR_PDOWN; 7647 r8152_mdio_write(tp, MII_BMCR, data); 7648 } 7649 7650 /* disable ALDPS before updating the PHY parameters */ 7651 r8153_aldps_en(tp, false); 7652 7653 /* disable EEE before updating the PHY parameters */ 7654 rtl_eee_enable(tp, false); 7655 7656 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 7657 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 7658 7659 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 7660 ocp_data |= PFM_PWM_SWITCH; 7661 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 7662 7663 switch (tp->version) { 7664 case RTL_VER_12: 7665 data = ocp_reg_read(tp, 0xbc08); 7666 data |= BIT(3) | BIT(2); 7667 ocp_reg_write(tp, 0xbc08, data); 7668 7669 data = sram_read(tp, 0x8fff); 7670 data &= ~0xff00; 7671 data |= 0x0400; 7672 sram_write(tp, 0x8fff, data); 7673 7674 data = ocp_reg_read(tp, 0xacda); 7675 data |= 0xff00; 7676 ocp_reg_write(tp, 0xacda, data); 7677 data = ocp_reg_read(tp, 0xacde); 7678 data |= 0xf000; 7679 ocp_reg_write(tp, 0xacde, data); 7680 ocp_reg_write(tp, 0xac8c, 0x0ffc); 7681 ocp_reg_write(tp, 0xac46, 0xb7b4); 7682 ocp_reg_write(tp, 0xac50, 0x0fbc); 7683 ocp_reg_write(tp, 0xac3c, 0x9240); 7684 ocp_reg_write(tp, 0xac4e, 0x0db4); 7685 ocp_reg_write(tp, 0xacc6, 0x0707); 7686 ocp_reg_write(tp, 0xacc8, 0xa0d3); 7687 ocp_reg_write(tp, 0xad08, 0x0007); 7688 7689 ocp_reg_write(tp, 0xb87c, 0x8560); 7690 ocp_reg_write(tp, 0xb87e, 0x19cc); 7691 ocp_reg_write(tp, 0xb87c, 0x8562); 7692 ocp_reg_write(tp, 0xb87e, 0x19cc); 7693 ocp_reg_write(tp, 0xb87c, 0x8564); 7694 ocp_reg_write(tp, 0xb87e, 0x19cc); 7695 ocp_reg_write(tp, 0xb87c, 0x8566); 7696 ocp_reg_write(tp, 0xb87e, 0x147d); 7697 ocp_reg_write(tp, 0xb87c, 0x8568); 7698 ocp_reg_write(tp, 0xb87e, 0x147d); 7699 ocp_reg_write(tp, 0xb87c, 0x856a); 7700 ocp_reg_write(tp, 0xb87e, 0x147d); 7701 ocp_reg_write(tp, 0xb87c, 0x8ffe); 7702 ocp_reg_write(tp, 0xb87e, 0x0907); 7703 ocp_reg_write(tp, 0xb87c, 0x80d6); 7704 ocp_reg_write(tp, 0xb87e, 0x2801); 7705 ocp_reg_write(tp, 0xb87c, 0x80f2); 7706 ocp_reg_write(tp, 0xb87e, 0x2801); 7707 ocp_reg_write(tp, 0xb87c, 0x80f4); 7708 ocp_reg_write(tp, 0xb87e, 0x6077); 7709 ocp_reg_write(tp, 0xb506, 0x01e7); 7710 7711 ocp_reg_write(tp, 0xb87c, 0x8013); 7712 ocp_reg_write(tp, 0xb87e, 0x0700); 7713 ocp_reg_write(tp, 0xb87c, 0x8fb9); 7714 ocp_reg_write(tp, 0xb87e, 0x2801); 7715 ocp_reg_write(tp, 0xb87c, 0x8fba); 7716 ocp_reg_write(tp, 0xb87e, 0x0100); 7717 ocp_reg_write(tp, 0xb87c, 0x8fbc); 7718 ocp_reg_write(tp, 0xb87e, 0x1900); 7719 ocp_reg_write(tp, 0xb87c, 0x8fbe); 7720 ocp_reg_write(tp, 0xb87e, 0xe100); 7721 ocp_reg_write(tp, 0xb87c, 0x8fc0); 7722 ocp_reg_write(tp, 0xb87e, 0x0800); 7723 ocp_reg_write(tp, 0xb87c, 0x8fc2); 7724 ocp_reg_write(tp, 0xb87e, 0xe500); 7725 ocp_reg_write(tp, 0xb87c, 0x8fc4); 7726 ocp_reg_write(tp, 0xb87e, 0x0f00); 7727 ocp_reg_write(tp, 0xb87c, 0x8fc6); 7728 ocp_reg_write(tp, 0xb87e, 0xf100); 7729 ocp_reg_write(tp, 0xb87c, 0x8fc8); 7730 ocp_reg_write(tp, 0xb87e, 0x0400); 7731 ocp_reg_write(tp, 0xb87c, 0x8fca); 7732 ocp_reg_write(tp, 0xb87e, 0xf300); 7733 ocp_reg_write(tp, 0xb87c, 0x8fcc); 7734 ocp_reg_write(tp, 0xb87e, 0xfd00); 7735 ocp_reg_write(tp, 0xb87c, 0x8fce); 7736 ocp_reg_write(tp, 0xb87e, 0xff00); 7737 ocp_reg_write(tp, 0xb87c, 0x8fd0); 7738 ocp_reg_write(tp, 0xb87e, 0xfb00); 7739 ocp_reg_write(tp, 0xb87c, 0x8fd2); 7740 ocp_reg_write(tp, 0xb87e, 0x0100); 7741 ocp_reg_write(tp, 0xb87c, 0x8fd4); 7742 ocp_reg_write(tp, 0xb87e, 0xf400); 7743 ocp_reg_write(tp, 0xb87c, 0x8fd6); 7744 ocp_reg_write(tp, 0xb87e, 0xff00); 7745 ocp_reg_write(tp, 0xb87c, 0x8fd8); 7746 ocp_reg_write(tp, 0xb87e, 0xf600); 7747 7748 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG); 7749 ocp_data |= EN_XG_LIP | EN_G_LIP; 7750 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data); 7751 ocp_reg_write(tp, 0xb87c, 0x813d); 7752 ocp_reg_write(tp, 0xb87e, 0x390e); 7753 ocp_reg_write(tp, 0xb87c, 0x814f); 7754 ocp_reg_write(tp, 0xb87e, 0x790e); 7755 ocp_reg_write(tp, 0xb87c, 0x80b0); 7756 ocp_reg_write(tp, 0xb87e, 0x0f31); 7757 data = ocp_reg_read(tp, 0xbf4c); 7758 data |= BIT(1); 7759 ocp_reg_write(tp, 0xbf4c, data); 7760 data = ocp_reg_read(tp, 0xbcca); 7761 data |= BIT(9) | BIT(8); 7762 ocp_reg_write(tp, 0xbcca, data); 7763 ocp_reg_write(tp, 0xb87c, 0x8141); 7764 ocp_reg_write(tp, 0xb87e, 0x320e); 7765 ocp_reg_write(tp, 0xb87c, 0x8153); 7766 ocp_reg_write(tp, 0xb87e, 0x720e); 7767 ocp_reg_write(tp, 0xb87c, 0x8529); 7768 ocp_reg_write(tp, 0xb87e, 0x050e); 7769 data = ocp_reg_read(tp, OCP_EEE_CFG); 7770 data &= ~CTAP_SHORT_EN; 7771 ocp_reg_write(tp, OCP_EEE_CFG, data); 7772 7773 sram_write(tp, 0x816c, 0xc4a0); 7774 sram_write(tp, 0x8170, 0xc4a0); 7775 sram_write(tp, 0x8174, 0x04a0); 7776 sram_write(tp, 0x8178, 0x04a0); 7777 sram_write(tp, 0x817c, 0x0719); 7778 sram_write(tp, 0x8ff4, 0x0400); 7779 sram_write(tp, 0x8ff1, 0x0404); 7780 7781 ocp_reg_write(tp, 0xbf4a, 0x001b); 7782 ocp_reg_write(tp, 0xb87c, 0x8033); 7783 ocp_reg_write(tp, 0xb87e, 0x7c13); 7784 ocp_reg_write(tp, 0xb87c, 0x8037); 7785 ocp_reg_write(tp, 0xb87e, 0x7c13); 7786 ocp_reg_write(tp, 0xb87c, 0x803b); 7787 ocp_reg_write(tp, 0xb87e, 0xfc32); 7788 ocp_reg_write(tp, 0xb87c, 0x803f); 7789 ocp_reg_write(tp, 0xb87e, 0x7c13); 7790 ocp_reg_write(tp, 0xb87c, 0x8043); 7791 ocp_reg_write(tp, 0xb87e, 0x7c13); 7792 ocp_reg_write(tp, 0xb87c, 0x8047); 7793 ocp_reg_write(tp, 0xb87e, 0x7c13); 7794 7795 ocp_reg_write(tp, 0xb87c, 0x8145); 7796 ocp_reg_write(tp, 0xb87e, 0x370e); 7797 ocp_reg_write(tp, 0xb87c, 0x8157); 7798 ocp_reg_write(tp, 0xb87e, 0x770e); 7799 ocp_reg_write(tp, 0xb87c, 0x8169); 7800 ocp_reg_write(tp, 0xb87e, 0x0d0a); 7801 ocp_reg_write(tp, 0xb87c, 0x817b); 7802 ocp_reg_write(tp, 0xb87e, 0x1d0a); 7803 7804 data = sram_read(tp, 0x8217); 7805 data &= ~0xff00; 7806 data |= 0x5000; 7807 sram_write(tp, 0x8217, data); 7808 data = sram_read(tp, 0x821a); 7809 data &= ~0xff00; 7810 data |= 0x5000; 7811 sram_write(tp, 0x821a, data); 7812 sram_write(tp, 0x80da, 0x0403); 7813 data = sram_read(tp, 0x80dc); 7814 data &= ~0xff00; 7815 data |= 0x1000; 7816 sram_write(tp, 0x80dc, data); 7817 sram_write(tp, 0x80b3, 0x0384); 7818 sram_write(tp, 0x80b7, 0x2007); 7819 data = sram_read(tp, 0x80ba); 7820 data &= ~0xff00; 7821 data |= 0x6c00; 7822 sram_write(tp, 0x80ba, data); 7823 sram_write(tp, 0x80b5, 0xf009); 7824 data = sram_read(tp, 0x80bd); 7825 data &= ~0xff00; 7826 data |= 0x9f00; 7827 sram_write(tp, 0x80bd, data); 7828 sram_write(tp, 0x80c7, 0xf083); 7829 sram_write(tp, 0x80dd, 0x03f0); 7830 data = sram_read(tp, 0x80df); 7831 data &= ~0xff00; 7832 data |= 0x1000; 7833 sram_write(tp, 0x80df, data); 7834 sram_write(tp, 0x80cb, 0x2007); 7835 data = sram_read(tp, 0x80ce); 7836 data &= ~0xff00; 7837 data |= 0x6c00; 7838 sram_write(tp, 0x80ce, data); 7839 sram_write(tp, 0x80c9, 0x8009); 7840 data = sram_read(tp, 0x80d1); 7841 data &= ~0xff00; 7842 data |= 0x8000; 7843 sram_write(tp, 0x80d1, data); 7844 sram_write(tp, 0x80a3, 0x200a); 7845 sram_write(tp, 0x80a5, 0xf0ad); 7846 sram_write(tp, 0x809f, 0x6073); 7847 sram_write(tp, 0x80a1, 0x000b); 7848 data = sram_read(tp, 0x80a9); 7849 data &= ~0xff00; 7850 data |= 0xc000; 7851 sram_write(tp, 0x80a9, data); 7852 7853 if (rtl_phy_patch_request(tp, true, true)) 7854 return; 7855 7856 data = ocp_reg_read(tp, 0xb896); 7857 data &= ~BIT(0); 7858 ocp_reg_write(tp, 0xb896, data); 7859 data = ocp_reg_read(tp, 0xb892); 7860 data &= ~0xff00; 7861 ocp_reg_write(tp, 0xb892, data); 7862 ocp_reg_write(tp, 0xb88e, 0xc23e); 7863 ocp_reg_write(tp, 0xb890, 0x0000); 7864 ocp_reg_write(tp, 0xb88e, 0xc240); 7865 ocp_reg_write(tp, 0xb890, 0x0103); 7866 ocp_reg_write(tp, 0xb88e, 0xc242); 7867 ocp_reg_write(tp, 0xb890, 0x0507); 7868 ocp_reg_write(tp, 0xb88e, 0xc244); 7869 ocp_reg_write(tp, 0xb890, 0x090b); 7870 ocp_reg_write(tp, 0xb88e, 0xc246); 7871 ocp_reg_write(tp, 0xb890, 0x0c0e); 7872 ocp_reg_write(tp, 0xb88e, 0xc248); 7873 ocp_reg_write(tp, 0xb890, 0x1012); 7874 ocp_reg_write(tp, 0xb88e, 0xc24a); 7875 ocp_reg_write(tp, 0xb890, 0x1416); 7876 data = ocp_reg_read(tp, 0xb896); 7877 data |= BIT(0); 7878 ocp_reg_write(tp, 0xb896, data); 7879 7880 rtl_phy_patch_request(tp, false, true); 7881 7882 data = ocp_reg_read(tp, 0xa86a); 7883 data |= BIT(0); 7884 ocp_reg_write(tp, 0xa86a, data); 7885 data = ocp_reg_read(tp, 0xa6f0); 7886 data |= BIT(0); 7887 ocp_reg_write(tp, 0xa6f0, data); 7888 7889 ocp_reg_write(tp, 0xbfa0, 0xd70d); 7890 ocp_reg_write(tp, 0xbfa2, 0x4100); 7891 ocp_reg_write(tp, 0xbfa4, 0xe868); 7892 ocp_reg_write(tp, 0xbfa6, 0xdc59); 7893 ocp_reg_write(tp, 0xb54c, 0x3c18); 7894 data = ocp_reg_read(tp, 0xbfa4); 7895 data &= ~BIT(5); 7896 ocp_reg_write(tp, 0xbfa4, data); 7897 data = sram_read(tp, 0x817d); 7898 data |= BIT(12); 7899 sram_write(tp, 0x817d, data); 7900 break; 7901 case RTL_VER_13: 7902 /* 2.5G INRX */ 7903 data = ocp_reg_read(tp, 0xac46); 7904 data &= ~0x00f0; 7905 data |= 0x0090; 7906 ocp_reg_write(tp, 0xac46, data); 7907 data = ocp_reg_read(tp, 0xad30); 7908 data &= ~0x0003; 7909 data |= 0x0001; 7910 ocp_reg_write(tp, 0xad30, data); 7911 fallthrough; 7912 case RTL_VER_15: 7913 /* EEE parameter */ 7914 ocp_reg_write(tp, 0xb87c, 0x80f5); 7915 ocp_reg_write(tp, 0xb87e, 0x760e); 7916 ocp_reg_write(tp, 0xb87c, 0x8107); 7917 ocp_reg_write(tp, 0xb87e, 0x360e); 7918 ocp_reg_write(tp, 0xb87c, 0x8551); 7919 data = ocp_reg_read(tp, 0xb87e); 7920 data &= ~0xff00; 7921 data |= 0x0800; 7922 ocp_reg_write(tp, 0xb87e, data); 7923 7924 /* ADC_PGA parameter */ 7925 data = ocp_reg_read(tp, 0xbf00); 7926 data &= ~0xe000; 7927 data |= 0xa000; 7928 ocp_reg_write(tp, 0xbf00, data); 7929 data = ocp_reg_read(tp, 0xbf46); 7930 data &= ~0x0f00; 7931 data |= 0x0300; 7932 ocp_reg_write(tp, 0xbf46, data); 7933 7934 /* Green Table-PGA, 1G full viterbi */ 7935 sram_write(tp, 0x8044, 0x2417); 7936 sram_write(tp, 0x804a, 0x2417); 7937 sram_write(tp, 0x8050, 0x2417); 7938 sram_write(tp, 0x8056, 0x2417); 7939 sram_write(tp, 0x805c, 0x2417); 7940 sram_write(tp, 0x8062, 0x2417); 7941 sram_write(tp, 0x8068, 0x2417); 7942 sram_write(tp, 0x806e, 0x2417); 7943 sram_write(tp, 0x8074, 0x2417); 7944 sram_write(tp, 0x807a, 0x2417); 7945 7946 /* XG PLL */ 7947 data = ocp_reg_read(tp, 0xbf84); 7948 data &= ~0xe000; 7949 data |= 0xa000; 7950 ocp_reg_write(tp, 0xbf84, data); 7951 break; 7952 default: 7953 break; 7954 } 7955 7956 /* Notify the MAC when the speed is changed to force mode. */ 7957 data = ocp_reg_read(tp, OCP_INTR_EN); 7958 data |= INTR_SPEED_FORCE; 7959 ocp_reg_write(tp, OCP_INTR_EN, data); 7960 7961 if (rtl_phy_patch_request(tp, true, true)) 7962 return; 7963 7964 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4); 7965 ocp_data |= EEE_SPDWN_EN; 7966 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data); 7967 7968 data = ocp_reg_read(tp, OCP_DOWN_SPEED); 7969 data &= ~(EN_EEE_100 | EN_EEE_1000); 7970 data |= EN_10M_CLKDIV; 7971 ocp_reg_write(tp, OCP_DOWN_SPEED, data); 7972 tp->ups_info._10m_ckdiv = true; 7973 tp->ups_info.eee_plloff_100 = false; 7974 tp->ups_info.eee_plloff_giga = false; 7975 7976 data = ocp_reg_read(tp, OCP_POWER_CFG); 7977 data &= ~EEE_CLKDIV_EN; 7978 ocp_reg_write(tp, OCP_POWER_CFG, data); 7979 tp->ups_info.eee_ckdiv = false; 7980 7981 rtl_phy_patch_request(tp, false, true); 7982 7983 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); 7984 7985 data = ocp_reg_read(tp, 0xa428); 7986 data &= ~BIT(9); 7987 ocp_reg_write(tp, 0xa428, data); 7988 data = ocp_reg_read(tp, 0xa5ea); 7989 data &= ~BIT(0); 7990 ocp_reg_write(tp, 0xa5ea, data); 7991 tp->ups_info.lite_mode = 0; 7992 7993 if (tp->eee_en) 7994 rtl_eee_enable(tp, true); 7995 7996 r8153_aldps_en(tp, true); 7997 r8152b_enable_fc(tp); 7998 r8153_u2p3en(tp, true); 7999 8000 set_bit(PHY_RESET, &tp->flags); 8001 } 8002 8003 static void r8156_init(struct r8152 *tp) 8004 { 8005 u32 ocp_data; 8006 u16 data; 8007 int i; 8008 8009 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8010 return; 8011 8012 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP); 8013 ocp_data &= ~EN_ALL_SPEED; 8014 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data); 8015 8016 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0); 8017 8018 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION); 8019 ocp_data |= BYPASS_MAC_RESET; 8020 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data); 8021 8022 r8153b_u1u2en(tp, false); 8023 8024 for (i = 0; i < 500; i++) { 8025 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 8026 AUTOLOAD_DONE) 8027 break; 8028 8029 msleep(20); 8030 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8031 return; 8032 } 8033 8034 data = r8153_phy_status(tp, 0); 8035 if (data == PHY_STAT_EXT_INIT) { 8036 data = ocp_reg_read(tp, 0xa468); 8037 data &= ~(BIT(3) | BIT(1)); 8038 ocp_reg_write(tp, 0xa468, data); 8039 } 8040 8041 data = r8152_mdio_read(tp, MII_BMCR); 8042 if (data & BMCR_PDOWN) { 8043 data &= ~BMCR_PDOWN; 8044 r8152_mdio_write(tp, MII_BMCR, data); 8045 } 8046 8047 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 8048 WARN_ON_ONCE(data != PHY_STAT_LAN_ON); 8049 8050 r8153_u2p3en(tp, false); 8051 8052 /* MSC timer = 0xfff * 8ms = 32760 ms */ 8053 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 8054 8055 /* U1/U2/L1 idle timer. 500 us */ 8056 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 8057 8058 r8153b_power_cut_en(tp, false); 8059 r8156_ups_en(tp, false); 8060 r8153_queue_wake(tp, false); 8061 rtl_runtime_suspend_enable(tp, false); 8062 8063 if (tp->udev->speed >= USB_SPEED_SUPER) 8064 r8153b_u1u2en(tp, true); 8065 8066 usb_enable_lpm(tp->udev); 8067 8068 r8156_mac_clk_spd(tp, true); 8069 8070 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 8071 ocp_data &= ~PLA_MCU_SPDWN_EN; 8072 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 8073 8074 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 8075 if (rtl8152_get_speed(tp) & LINK_STATUS) 8076 ocp_data |= CUR_LINK_OK; 8077 else 8078 ocp_data &= ~CUR_LINK_OK; 8079 ocp_data |= POLL_LINK_CHG; 8080 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 8081 8082 set_bit(GREEN_ETHERNET, &tp->flags); 8083 8084 /* rx aggregation */ 8085 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 8086 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 8087 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 8088 8089 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG); 8090 ocp_data |= ACT_ODMA; 8091 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data); 8092 8093 r8156_mdio_force_mode(tp); 8094 rtl_tally_reset(tp); 8095 8096 tp->coalesce = 15000; /* 15 us */ 8097 } 8098 8099 static void r8156b_init(struct r8152 *tp) 8100 { 8101 u32 ocp_data; 8102 u16 data; 8103 int i; 8104 8105 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8106 return; 8107 8108 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP); 8109 ocp_data &= ~EN_ALL_SPEED; 8110 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data); 8111 8112 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0); 8113 8114 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION); 8115 ocp_data |= BYPASS_MAC_RESET; 8116 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data); 8117 8118 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); 8119 ocp_data |= RX_DETECT8; 8120 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); 8121 8122 r8153b_u1u2en(tp, false); 8123 8124 switch (tp->version) { 8125 case RTL_VER_13: 8126 case RTL_VER_15: 8127 r8156b_wait_loading_flash(tp); 8128 break; 8129 default: 8130 break; 8131 } 8132 8133 for (i = 0; i < 500; i++) { 8134 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & 8135 AUTOLOAD_DONE) 8136 break; 8137 8138 msleep(20); 8139 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 8140 return; 8141 } 8142 8143 data = r8153_phy_status(tp, 0); 8144 if (data == PHY_STAT_EXT_INIT) { 8145 data = ocp_reg_read(tp, 0xa468); 8146 data &= ~(BIT(3) | BIT(1)); 8147 ocp_reg_write(tp, 0xa468, data); 8148 8149 data = ocp_reg_read(tp, 0xa466); 8150 data &= ~BIT(0); 8151 ocp_reg_write(tp, 0xa466, data); 8152 } 8153 8154 data = r8152_mdio_read(tp, MII_BMCR); 8155 if (data & BMCR_PDOWN) { 8156 data &= ~BMCR_PDOWN; 8157 r8152_mdio_write(tp, MII_BMCR, data); 8158 } 8159 8160 data = r8153_phy_status(tp, PHY_STAT_LAN_ON); 8161 8162 r8153_u2p3en(tp, false); 8163 8164 /* MSC timer = 0xfff * 8ms = 32760 ms */ 8165 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); 8166 8167 /* U1/U2/L1 idle timer. 500 us */ 8168 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); 8169 8170 r8153b_power_cut_en(tp, false); 8171 r8156_ups_en(tp, false); 8172 r8153_queue_wake(tp, false); 8173 rtl_runtime_suspend_enable(tp, false); 8174 8175 if (tp->udev->speed >= USB_SPEED_SUPER) 8176 r8153b_u1u2en(tp, true); 8177 8178 usb_enable_lpm(tp->udev); 8179 8180 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR); 8181 ocp_data &= ~SLOT_EN; 8182 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 8183 8184 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); 8185 ocp_data |= FLOW_CTRL_EN; 8186 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); 8187 8188 /* enable fc timer and set timer to 600 ms. */ 8189 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, 8190 CTRL_TIMER_EN | (600 / 8)); 8191 8192 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); 8193 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN)) 8194 ocp_data |= FLOW_CTRL_PATCH_2; 8195 ocp_data &= ~AUTO_SPEEDUP; 8196 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); 8197 8198 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); 8199 ocp_data |= FC_PATCH_TASK; 8200 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); 8201 8202 r8156_mac_clk_spd(tp, true); 8203 8204 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3); 8205 ocp_data &= ~PLA_MCU_SPDWN_EN; 8206 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data); 8207 8208 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); 8209 if (rtl8152_get_speed(tp) & LINK_STATUS) 8210 ocp_data |= CUR_LINK_OK; 8211 else 8212 ocp_data &= ~CUR_LINK_OK; 8213 ocp_data |= POLL_LINK_CHG; 8214 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); 8215 8216 set_bit(GREEN_ETHERNET, &tp->flags); 8217 8218 /* rx aggregation */ 8219 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 8220 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); 8221 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 8222 8223 r8156_mdio_force_mode(tp); 8224 rtl_tally_reset(tp); 8225 8226 tp->coalesce = 15000; /* 15 us */ 8227 } 8228 8229 static bool rtl_check_vendor_ok(struct usb_interface *intf) 8230 { 8231 struct usb_host_interface *alt = intf->cur_altsetting; 8232 struct usb_endpoint_descriptor *in, *out, *intr; 8233 8234 if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) { 8235 dev_err(&intf->dev, "Expected endpoints are not found\n"); 8236 return false; 8237 } 8238 8239 /* Check Rx endpoint address */ 8240 if (usb_endpoint_num(in) != 1) { 8241 dev_err(&intf->dev, "Invalid Rx endpoint address\n"); 8242 return false; 8243 } 8244 8245 /* Check Tx endpoint address */ 8246 if (usb_endpoint_num(out) != 2) { 8247 dev_err(&intf->dev, "Invalid Tx endpoint address\n"); 8248 return false; 8249 } 8250 8251 /* Check interrupt endpoint address */ 8252 if (usb_endpoint_num(intr) != 3) { 8253 dev_err(&intf->dev, "Invalid interrupt endpoint address\n"); 8254 return false; 8255 } 8256 8257 return true; 8258 } 8259 8260 static int rtl8152_pre_reset(struct usb_interface *intf) 8261 { 8262 struct r8152 *tp = usb_get_intfdata(intf); 8263 struct net_device *netdev; 8264 8265 if (!tp) 8266 return 0; 8267 8268 netdev = tp->netdev; 8269 if (!netif_running(netdev)) 8270 return 0; 8271 8272 netif_stop_queue(netdev); 8273 tasklet_disable(&tp->tx_tl); 8274 clear_bit(WORK_ENABLE, &tp->flags); 8275 usb_kill_urb(tp->intr_urb); 8276 cancel_delayed_work_sync(&tp->schedule); 8277 napi_disable(&tp->napi); 8278 if (netif_carrier_ok(netdev)) { 8279 mutex_lock(&tp->control); 8280 tp->rtl_ops.disable(tp); 8281 mutex_unlock(&tp->control); 8282 } 8283 8284 return 0; 8285 } 8286 8287 static int rtl8152_post_reset(struct usb_interface *intf) 8288 { 8289 struct r8152 *tp = usb_get_intfdata(intf); 8290 struct net_device *netdev; 8291 struct sockaddr sa; 8292 8293 if (!tp) 8294 return 0; 8295 8296 /* reset the MAC address in case of policy change */ 8297 if (determine_ethernet_addr(tp, &sa) >= 0) { 8298 rtnl_lock(); 8299 dev_set_mac_address (tp->netdev, &sa, NULL); 8300 rtnl_unlock(); 8301 } 8302 8303 netdev = tp->netdev; 8304 if (!netif_running(netdev)) 8305 return 0; 8306 8307 set_bit(WORK_ENABLE, &tp->flags); 8308 if (netif_carrier_ok(netdev)) { 8309 mutex_lock(&tp->control); 8310 tp->rtl_ops.enable(tp); 8311 rtl_start_rx(tp); 8312 _rtl8152_set_rx_mode(netdev); 8313 mutex_unlock(&tp->control); 8314 } 8315 8316 napi_enable(&tp->napi); 8317 tasklet_enable(&tp->tx_tl); 8318 netif_wake_queue(netdev); 8319 usb_submit_urb(tp->intr_urb, GFP_KERNEL); 8320 8321 if (!list_empty(&tp->rx_done)) 8322 napi_schedule(&tp->napi); 8323 8324 return 0; 8325 } 8326 8327 static bool delay_autosuspend(struct r8152 *tp) 8328 { 8329 bool sw_linking = !!netif_carrier_ok(tp->netdev); 8330 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); 8331 8332 /* This means a linking change occurs and the driver doesn't detect it, 8333 * yet. If the driver has disabled tx/rx and hw is linking on, the 8334 * device wouldn't wake up by receiving any packet. 8335 */ 8336 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) 8337 return true; 8338 8339 /* If the linking down is occurred by nway, the device may miss the 8340 * linking change event. And it wouldn't wake when linking on. 8341 */ 8342 if (!sw_linking && tp->rtl_ops.in_nway(tp)) 8343 return true; 8344 else if (!skb_queue_empty(&tp->tx_queue)) 8345 return true; 8346 else 8347 return false; 8348 } 8349 8350 static int rtl8152_runtime_resume(struct r8152 *tp) 8351 { 8352 struct net_device *netdev = tp->netdev; 8353 8354 if (netif_running(netdev) && netdev->flags & IFF_UP) { 8355 struct napi_struct *napi = &tp->napi; 8356 8357 tp->rtl_ops.autosuspend_en(tp, false); 8358 napi_disable(napi); 8359 set_bit(WORK_ENABLE, &tp->flags); 8360 8361 if (netif_carrier_ok(netdev)) { 8362 if (rtl8152_get_speed(tp) & LINK_STATUS) { 8363 rtl_start_rx(tp); 8364 } else { 8365 netif_carrier_off(netdev); 8366 tp->rtl_ops.disable(tp); 8367 netif_info(tp, link, netdev, "linking down\n"); 8368 } 8369 } 8370 8371 napi_enable(napi); 8372 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8373 smp_mb__after_atomic(); 8374 8375 if (!list_empty(&tp->rx_done)) 8376 napi_schedule(&tp->napi); 8377 8378 usb_submit_urb(tp->intr_urb, GFP_NOIO); 8379 } else { 8380 if (netdev->flags & IFF_UP) 8381 tp->rtl_ops.autosuspend_en(tp, false); 8382 8383 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8384 } 8385 8386 return 0; 8387 } 8388 8389 static int rtl8152_system_resume(struct r8152 *tp) 8390 { 8391 struct net_device *netdev = tp->netdev; 8392 8393 netif_device_attach(netdev); 8394 8395 if (netif_running(netdev) && (netdev->flags & IFF_UP)) { 8396 tp->rtl_ops.up(tp); 8397 netif_carrier_off(netdev); 8398 set_bit(WORK_ENABLE, &tp->flags); 8399 usb_submit_urb(tp->intr_urb, GFP_NOIO); 8400 } 8401 8402 return 0; 8403 } 8404 8405 static int rtl8152_runtime_suspend(struct r8152 *tp) 8406 { 8407 struct net_device *netdev = tp->netdev; 8408 int ret = 0; 8409 8410 if (!tp->rtl_ops.autosuspend_en) 8411 return -EBUSY; 8412 8413 set_bit(SELECTIVE_SUSPEND, &tp->flags); 8414 smp_mb__after_atomic(); 8415 8416 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 8417 u32 rcr = 0; 8418 8419 if (netif_carrier_ok(netdev)) { 8420 u32 ocp_data; 8421 8422 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 8423 ocp_data = rcr & ~RCR_ACPT_ALL; 8424 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 8425 rxdy_gated_en(tp, true); 8426 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 8427 PLA_OOB_CTRL); 8428 if (!(ocp_data & RXFIFO_EMPTY)) { 8429 rxdy_gated_en(tp, false); 8430 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 8431 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8432 smp_mb__after_atomic(); 8433 ret = -EBUSY; 8434 goto out1; 8435 } 8436 } 8437 8438 clear_bit(WORK_ENABLE, &tp->flags); 8439 usb_kill_urb(tp->intr_urb); 8440 8441 tp->rtl_ops.autosuspend_en(tp, true); 8442 8443 if (netif_carrier_ok(netdev)) { 8444 struct napi_struct *napi = &tp->napi; 8445 8446 napi_disable(napi); 8447 rtl_stop_rx(tp); 8448 rxdy_gated_en(tp, false); 8449 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); 8450 napi_enable(napi); 8451 } 8452 8453 if (delay_autosuspend(tp)) { 8454 rtl8152_runtime_resume(tp); 8455 ret = -EBUSY; 8456 } 8457 } 8458 8459 out1: 8460 return ret; 8461 } 8462 8463 static int rtl8152_system_suspend(struct r8152 *tp) 8464 { 8465 struct net_device *netdev = tp->netdev; 8466 8467 netif_device_detach(netdev); 8468 8469 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { 8470 struct napi_struct *napi = &tp->napi; 8471 8472 clear_bit(WORK_ENABLE, &tp->flags); 8473 usb_kill_urb(tp->intr_urb); 8474 tasklet_disable(&tp->tx_tl); 8475 napi_disable(napi); 8476 cancel_delayed_work_sync(&tp->schedule); 8477 tp->rtl_ops.down(tp); 8478 napi_enable(napi); 8479 tasklet_enable(&tp->tx_tl); 8480 } 8481 8482 return 0; 8483 } 8484 8485 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) 8486 { 8487 struct r8152 *tp = usb_get_intfdata(intf); 8488 int ret; 8489 8490 mutex_lock(&tp->control); 8491 8492 if (PMSG_IS_AUTO(message)) 8493 ret = rtl8152_runtime_suspend(tp); 8494 else 8495 ret = rtl8152_system_suspend(tp); 8496 8497 mutex_unlock(&tp->control); 8498 8499 return ret; 8500 } 8501 8502 static int rtl8152_resume(struct usb_interface *intf) 8503 { 8504 struct r8152 *tp = usb_get_intfdata(intf); 8505 int ret; 8506 8507 mutex_lock(&tp->control); 8508 8509 rtl_reset_ocp_base(tp); 8510 8511 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) 8512 ret = rtl8152_runtime_resume(tp); 8513 else 8514 ret = rtl8152_system_resume(tp); 8515 8516 mutex_unlock(&tp->control); 8517 8518 return ret; 8519 } 8520 8521 static int rtl8152_reset_resume(struct usb_interface *intf) 8522 { 8523 struct r8152 *tp = usb_get_intfdata(intf); 8524 8525 clear_bit(SELECTIVE_SUSPEND, &tp->flags); 8526 rtl_reset_ocp_base(tp); 8527 tp->rtl_ops.init(tp); 8528 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 8529 set_ethernet_addr(tp, true); 8530 return rtl8152_resume(intf); 8531 } 8532 8533 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 8534 { 8535 struct r8152 *tp = netdev_priv(dev); 8536 8537 if (usb_autopm_get_interface(tp->intf) < 0) 8538 return; 8539 8540 if (!rtl_can_wakeup(tp)) { 8541 wol->supported = 0; 8542 wol->wolopts = 0; 8543 } else { 8544 mutex_lock(&tp->control); 8545 wol->supported = WAKE_ANY; 8546 wol->wolopts = __rtl_get_wol(tp); 8547 mutex_unlock(&tp->control); 8548 } 8549 8550 usb_autopm_put_interface(tp->intf); 8551 } 8552 8553 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 8554 { 8555 struct r8152 *tp = netdev_priv(dev); 8556 int ret; 8557 8558 if (!rtl_can_wakeup(tp)) 8559 return -EOPNOTSUPP; 8560 8561 if (wol->wolopts & ~WAKE_ANY) 8562 return -EINVAL; 8563 8564 ret = usb_autopm_get_interface(tp->intf); 8565 if (ret < 0) 8566 goto out_set_wol; 8567 8568 mutex_lock(&tp->control); 8569 8570 __rtl_set_wol(tp, wol->wolopts); 8571 tp->saved_wolopts = wol->wolopts & WAKE_ANY; 8572 8573 mutex_unlock(&tp->control); 8574 8575 usb_autopm_put_interface(tp->intf); 8576 8577 out_set_wol: 8578 return ret; 8579 } 8580 8581 static u32 rtl8152_get_msglevel(struct net_device *dev) 8582 { 8583 struct r8152 *tp = netdev_priv(dev); 8584 8585 return tp->msg_enable; 8586 } 8587 8588 static void rtl8152_set_msglevel(struct net_device *dev, u32 value) 8589 { 8590 struct r8152 *tp = netdev_priv(dev); 8591 8592 tp->msg_enable = value; 8593 } 8594 8595 static void rtl8152_get_drvinfo(struct net_device *netdev, 8596 struct ethtool_drvinfo *info) 8597 { 8598 struct r8152 *tp = netdev_priv(netdev); 8599 8600 strscpy(info->driver, MODULENAME, sizeof(info->driver)); 8601 strscpy(info->version, DRIVER_VERSION, sizeof(info->version)); 8602 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); 8603 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw)) 8604 strscpy(info->fw_version, tp->rtl_fw.version, 8605 sizeof(info->fw_version)); 8606 } 8607 8608 static 8609 int rtl8152_get_link_ksettings(struct net_device *netdev, 8610 struct ethtool_link_ksettings *cmd) 8611 { 8612 struct r8152 *tp = netdev_priv(netdev); 8613 int ret; 8614 8615 if (!tp->mii.mdio_read) 8616 return -EOPNOTSUPP; 8617 8618 ret = usb_autopm_get_interface(tp->intf); 8619 if (ret < 0) 8620 goto out; 8621 8622 mutex_lock(&tp->control); 8623 8624 mii_ethtool_get_link_ksettings(&tp->mii, cmd); 8625 8626 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8627 cmd->link_modes.supported, tp->support_2500full); 8628 8629 if (tp->support_2500full) { 8630 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8631 cmd->link_modes.advertising, 8632 ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G); 8633 8634 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8635 cmd->link_modes.lp_advertising, 8636 ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G); 8637 8638 if (is_speed_2500(rtl8152_get_speed(tp))) 8639 cmd->base.speed = SPEED_2500; 8640 } 8641 8642 mutex_unlock(&tp->control); 8643 8644 usb_autopm_put_interface(tp->intf); 8645 8646 out: 8647 return ret; 8648 } 8649 8650 static int rtl8152_set_link_ksettings(struct net_device *dev, 8651 const struct ethtool_link_ksettings *cmd) 8652 { 8653 struct r8152 *tp = netdev_priv(dev); 8654 u32 advertising = 0; 8655 int ret; 8656 8657 ret = usb_autopm_get_interface(tp->intf); 8658 if (ret < 0) 8659 goto out; 8660 8661 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, 8662 cmd->link_modes.advertising)) 8663 advertising |= RTL_ADVERTISED_10_HALF; 8664 8665 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, 8666 cmd->link_modes.advertising)) 8667 advertising |= RTL_ADVERTISED_10_FULL; 8668 8669 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 8670 cmd->link_modes.advertising)) 8671 advertising |= RTL_ADVERTISED_100_HALF; 8672 8673 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 8674 cmd->link_modes.advertising)) 8675 advertising |= RTL_ADVERTISED_100_FULL; 8676 8677 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 8678 cmd->link_modes.advertising)) 8679 advertising |= RTL_ADVERTISED_1000_HALF; 8680 8681 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 8682 cmd->link_modes.advertising)) 8683 advertising |= RTL_ADVERTISED_1000_FULL; 8684 8685 if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 8686 cmd->link_modes.advertising)) 8687 advertising |= RTL_ADVERTISED_2500_FULL; 8688 8689 mutex_lock(&tp->control); 8690 8691 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, 8692 cmd->base.duplex, advertising); 8693 if (!ret) { 8694 tp->autoneg = cmd->base.autoneg; 8695 tp->speed = cmd->base.speed; 8696 tp->duplex = cmd->base.duplex; 8697 tp->advertising = advertising; 8698 } 8699 8700 mutex_unlock(&tp->control); 8701 8702 usb_autopm_put_interface(tp->intf); 8703 8704 out: 8705 return ret; 8706 } 8707 8708 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { 8709 "tx_packets", 8710 "rx_packets", 8711 "tx_errors", 8712 "rx_errors", 8713 "rx_missed", 8714 "align_errors", 8715 "tx_single_collisions", 8716 "tx_multi_collisions", 8717 "rx_unicast", 8718 "rx_broadcast", 8719 "rx_multicast", 8720 "tx_aborted", 8721 "tx_underrun", 8722 }; 8723 8724 static int rtl8152_get_sset_count(struct net_device *dev, int sset) 8725 { 8726 switch (sset) { 8727 case ETH_SS_STATS: 8728 return ARRAY_SIZE(rtl8152_gstrings); 8729 default: 8730 return -EOPNOTSUPP; 8731 } 8732 } 8733 8734 static void rtl8152_get_ethtool_stats(struct net_device *dev, 8735 struct ethtool_stats *stats, u64 *data) 8736 { 8737 struct r8152 *tp = netdev_priv(dev); 8738 struct tally_counter tally; 8739 8740 if (usb_autopm_get_interface(tp->intf) < 0) 8741 return; 8742 8743 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); 8744 8745 usb_autopm_put_interface(tp->intf); 8746 8747 data[0] = le64_to_cpu(tally.tx_packets); 8748 data[1] = le64_to_cpu(tally.rx_packets); 8749 data[2] = le64_to_cpu(tally.tx_errors); 8750 data[3] = le32_to_cpu(tally.rx_errors); 8751 data[4] = le16_to_cpu(tally.rx_missed); 8752 data[5] = le16_to_cpu(tally.align_errors); 8753 data[6] = le32_to_cpu(tally.tx_one_collision); 8754 data[7] = le32_to_cpu(tally.tx_multi_collision); 8755 data[8] = le64_to_cpu(tally.rx_unicast); 8756 data[9] = le64_to_cpu(tally.rx_broadcast); 8757 data[10] = le32_to_cpu(tally.rx_multicast); 8758 data[11] = le16_to_cpu(tally.tx_aborted); 8759 data[12] = le16_to_cpu(tally.tx_underrun); 8760 } 8761 8762 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) 8763 { 8764 switch (stringset) { 8765 case ETH_SS_STATS: 8766 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings)); 8767 break; 8768 } 8769 } 8770 8771 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) 8772 { 8773 u32 lp, adv, supported = 0; 8774 u16 val; 8775 8776 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 8777 supported = mmd_eee_cap_to_ethtool_sup_t(val); 8778 8779 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 8780 adv = mmd_eee_adv_to_ethtool_adv_t(val); 8781 8782 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 8783 lp = mmd_eee_adv_to_ethtool_adv_t(val); 8784 8785 eee->eee_enabled = tp->eee_en; 8786 eee->eee_active = !!(supported & adv & lp); 8787 eee->supported = supported; 8788 eee->advertised = tp->eee_adv; 8789 eee->lp_advertised = lp; 8790 8791 return 0; 8792 } 8793 8794 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) 8795 { 8796 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); 8797 8798 tp->eee_en = eee->eee_enabled; 8799 tp->eee_adv = val; 8800 8801 rtl_eee_enable(tp, tp->eee_en); 8802 8803 return 0; 8804 } 8805 8806 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) 8807 { 8808 u32 lp, adv, supported = 0; 8809 u16 val; 8810 8811 val = ocp_reg_read(tp, OCP_EEE_ABLE); 8812 supported = mmd_eee_cap_to_ethtool_sup_t(val); 8813 8814 val = ocp_reg_read(tp, OCP_EEE_ADV); 8815 adv = mmd_eee_adv_to_ethtool_adv_t(val); 8816 8817 val = ocp_reg_read(tp, OCP_EEE_LPABLE); 8818 lp = mmd_eee_adv_to_ethtool_adv_t(val); 8819 8820 eee->eee_enabled = tp->eee_en; 8821 eee->eee_active = !!(supported & adv & lp); 8822 eee->supported = supported; 8823 eee->advertised = tp->eee_adv; 8824 eee->lp_advertised = lp; 8825 8826 return 0; 8827 } 8828 8829 static int 8830 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) 8831 { 8832 struct r8152 *tp = netdev_priv(net); 8833 int ret; 8834 8835 if (!tp->rtl_ops.eee_get) { 8836 ret = -EOPNOTSUPP; 8837 goto out; 8838 } 8839 8840 ret = usb_autopm_get_interface(tp->intf); 8841 if (ret < 0) 8842 goto out; 8843 8844 mutex_lock(&tp->control); 8845 8846 ret = tp->rtl_ops.eee_get(tp, edata); 8847 8848 mutex_unlock(&tp->control); 8849 8850 usb_autopm_put_interface(tp->intf); 8851 8852 out: 8853 return ret; 8854 } 8855 8856 static int 8857 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) 8858 { 8859 struct r8152 *tp = netdev_priv(net); 8860 int ret; 8861 8862 if (!tp->rtl_ops.eee_set) { 8863 ret = -EOPNOTSUPP; 8864 goto out; 8865 } 8866 8867 ret = usb_autopm_get_interface(tp->intf); 8868 if (ret < 0) 8869 goto out; 8870 8871 mutex_lock(&tp->control); 8872 8873 ret = tp->rtl_ops.eee_set(tp, edata); 8874 if (!ret) 8875 ret = mii_nway_restart(&tp->mii); 8876 8877 mutex_unlock(&tp->control); 8878 8879 usb_autopm_put_interface(tp->intf); 8880 8881 out: 8882 return ret; 8883 } 8884 8885 static int rtl8152_nway_reset(struct net_device *dev) 8886 { 8887 struct r8152 *tp = netdev_priv(dev); 8888 int ret; 8889 8890 ret = usb_autopm_get_interface(tp->intf); 8891 if (ret < 0) 8892 goto out; 8893 8894 mutex_lock(&tp->control); 8895 8896 ret = mii_nway_restart(&tp->mii); 8897 8898 mutex_unlock(&tp->control); 8899 8900 usb_autopm_put_interface(tp->intf); 8901 8902 out: 8903 return ret; 8904 } 8905 8906 static int rtl8152_get_coalesce(struct net_device *netdev, 8907 struct ethtool_coalesce *coalesce, 8908 struct kernel_ethtool_coalesce *kernel_coal, 8909 struct netlink_ext_ack *extack) 8910 { 8911 struct r8152 *tp = netdev_priv(netdev); 8912 8913 switch (tp->version) { 8914 case RTL_VER_01: 8915 case RTL_VER_02: 8916 case RTL_VER_07: 8917 return -EOPNOTSUPP; 8918 default: 8919 break; 8920 } 8921 8922 coalesce->rx_coalesce_usecs = tp->coalesce; 8923 8924 return 0; 8925 } 8926 8927 static int rtl8152_set_coalesce(struct net_device *netdev, 8928 struct ethtool_coalesce *coalesce, 8929 struct kernel_ethtool_coalesce *kernel_coal, 8930 struct netlink_ext_ack *extack) 8931 { 8932 struct r8152 *tp = netdev_priv(netdev); 8933 int ret; 8934 8935 switch (tp->version) { 8936 case RTL_VER_01: 8937 case RTL_VER_02: 8938 case RTL_VER_07: 8939 return -EOPNOTSUPP; 8940 default: 8941 break; 8942 } 8943 8944 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) 8945 return -EINVAL; 8946 8947 ret = usb_autopm_get_interface(tp->intf); 8948 if (ret < 0) 8949 return ret; 8950 8951 mutex_lock(&tp->control); 8952 8953 if (tp->coalesce != coalesce->rx_coalesce_usecs) { 8954 tp->coalesce = coalesce->rx_coalesce_usecs; 8955 8956 if (netif_running(netdev) && netif_carrier_ok(netdev)) { 8957 netif_stop_queue(netdev); 8958 napi_disable(&tp->napi); 8959 tp->rtl_ops.disable(tp); 8960 tp->rtl_ops.enable(tp); 8961 rtl_start_rx(tp); 8962 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 8963 _rtl8152_set_rx_mode(netdev); 8964 napi_enable(&tp->napi); 8965 netif_wake_queue(netdev); 8966 } 8967 } 8968 8969 mutex_unlock(&tp->control); 8970 8971 usb_autopm_put_interface(tp->intf); 8972 8973 return ret; 8974 } 8975 8976 static int rtl8152_get_tunable(struct net_device *netdev, 8977 const struct ethtool_tunable *tunable, void *d) 8978 { 8979 struct r8152 *tp = netdev_priv(netdev); 8980 8981 switch (tunable->id) { 8982 case ETHTOOL_RX_COPYBREAK: 8983 *(u32 *)d = tp->rx_copybreak; 8984 break; 8985 default: 8986 return -EOPNOTSUPP; 8987 } 8988 8989 return 0; 8990 } 8991 8992 static int rtl8152_set_tunable(struct net_device *netdev, 8993 const struct ethtool_tunable *tunable, 8994 const void *d) 8995 { 8996 struct r8152 *tp = netdev_priv(netdev); 8997 u32 val; 8998 8999 switch (tunable->id) { 9000 case ETHTOOL_RX_COPYBREAK: 9001 val = *(u32 *)d; 9002 if (val < ETH_ZLEN) { 9003 netif_err(tp, rx_err, netdev, 9004 "Invalid rx copy break value\n"); 9005 return -EINVAL; 9006 } 9007 9008 if (tp->rx_copybreak != val) { 9009 if (netdev->flags & IFF_UP) { 9010 mutex_lock(&tp->control); 9011 napi_disable(&tp->napi); 9012 tp->rx_copybreak = val; 9013 napi_enable(&tp->napi); 9014 mutex_unlock(&tp->control); 9015 } else { 9016 tp->rx_copybreak = val; 9017 } 9018 } 9019 break; 9020 default: 9021 return -EOPNOTSUPP; 9022 } 9023 9024 return 0; 9025 } 9026 9027 static void rtl8152_get_ringparam(struct net_device *netdev, 9028 struct ethtool_ringparam *ring, 9029 struct kernel_ethtool_ringparam *kernel_ring, 9030 struct netlink_ext_ack *extack) 9031 { 9032 struct r8152 *tp = netdev_priv(netdev); 9033 9034 ring->rx_max_pending = RTL8152_RX_MAX_PENDING; 9035 ring->rx_pending = tp->rx_pending; 9036 } 9037 9038 static int rtl8152_set_ringparam(struct net_device *netdev, 9039 struct ethtool_ringparam *ring, 9040 struct kernel_ethtool_ringparam *kernel_ring, 9041 struct netlink_ext_ack *extack) 9042 { 9043 struct r8152 *tp = netdev_priv(netdev); 9044 9045 if (ring->rx_pending < (RTL8152_MAX_RX * 2)) 9046 return -EINVAL; 9047 9048 if (tp->rx_pending != ring->rx_pending) { 9049 if (netdev->flags & IFF_UP) { 9050 mutex_lock(&tp->control); 9051 napi_disable(&tp->napi); 9052 tp->rx_pending = ring->rx_pending; 9053 napi_enable(&tp->napi); 9054 mutex_unlock(&tp->control); 9055 } else { 9056 tp->rx_pending = ring->rx_pending; 9057 } 9058 } 9059 9060 return 0; 9061 } 9062 9063 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 9064 { 9065 struct r8152 *tp = netdev_priv(netdev); 9066 u16 bmcr, lcladv, rmtadv; 9067 u8 cap; 9068 9069 if (usb_autopm_get_interface(tp->intf) < 0) 9070 return; 9071 9072 mutex_lock(&tp->control); 9073 9074 bmcr = r8152_mdio_read(tp, MII_BMCR); 9075 lcladv = r8152_mdio_read(tp, MII_ADVERTISE); 9076 rmtadv = r8152_mdio_read(tp, MII_LPA); 9077 9078 mutex_unlock(&tp->control); 9079 9080 usb_autopm_put_interface(tp->intf); 9081 9082 if (!(bmcr & BMCR_ANENABLE)) { 9083 pause->autoneg = 0; 9084 pause->rx_pause = 0; 9085 pause->tx_pause = 0; 9086 return; 9087 } 9088 9089 pause->autoneg = 1; 9090 9091 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv); 9092 9093 if (cap & FLOW_CTRL_RX) 9094 pause->rx_pause = 1; 9095 9096 if (cap & FLOW_CTRL_TX) 9097 pause->tx_pause = 1; 9098 } 9099 9100 static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 9101 { 9102 struct r8152 *tp = netdev_priv(netdev); 9103 u16 old, new1; 9104 u8 cap = 0; 9105 int ret; 9106 9107 ret = usb_autopm_get_interface(tp->intf); 9108 if (ret < 0) 9109 return ret; 9110 9111 mutex_lock(&tp->control); 9112 9113 if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) { 9114 ret = -EINVAL; 9115 goto out; 9116 } 9117 9118 if (pause->rx_pause) 9119 cap |= FLOW_CTRL_RX; 9120 9121 if (pause->tx_pause) 9122 cap |= FLOW_CTRL_TX; 9123 9124 old = r8152_mdio_read(tp, MII_ADVERTISE); 9125 new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap); 9126 if (old != new1) 9127 r8152_mdio_write(tp, MII_ADVERTISE, new1); 9128 9129 out: 9130 mutex_unlock(&tp->control); 9131 usb_autopm_put_interface(tp->intf); 9132 9133 return ret; 9134 } 9135 9136 static const struct ethtool_ops ops = { 9137 .supported_coalesce_params = ETHTOOL_COALESCE_USECS, 9138 .get_drvinfo = rtl8152_get_drvinfo, 9139 .get_link = ethtool_op_get_link, 9140 .nway_reset = rtl8152_nway_reset, 9141 .get_msglevel = rtl8152_get_msglevel, 9142 .set_msglevel = rtl8152_set_msglevel, 9143 .get_wol = rtl8152_get_wol, 9144 .set_wol = rtl8152_set_wol, 9145 .get_strings = rtl8152_get_strings, 9146 .get_sset_count = rtl8152_get_sset_count, 9147 .get_ethtool_stats = rtl8152_get_ethtool_stats, 9148 .get_coalesce = rtl8152_get_coalesce, 9149 .set_coalesce = rtl8152_set_coalesce, 9150 .get_eee = rtl_ethtool_get_eee, 9151 .set_eee = rtl_ethtool_set_eee, 9152 .get_link_ksettings = rtl8152_get_link_ksettings, 9153 .set_link_ksettings = rtl8152_set_link_ksettings, 9154 .get_tunable = rtl8152_get_tunable, 9155 .set_tunable = rtl8152_set_tunable, 9156 .get_ringparam = rtl8152_get_ringparam, 9157 .set_ringparam = rtl8152_set_ringparam, 9158 .get_pauseparam = rtl8152_get_pauseparam, 9159 .set_pauseparam = rtl8152_set_pauseparam, 9160 }; 9161 9162 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) 9163 { 9164 struct r8152 *tp = netdev_priv(netdev); 9165 struct mii_ioctl_data *data = if_mii(rq); 9166 int res; 9167 9168 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9169 return -ENODEV; 9170 9171 res = usb_autopm_get_interface(tp->intf); 9172 if (res < 0) 9173 goto out; 9174 9175 switch (cmd) { 9176 case SIOCGMIIPHY: 9177 data->phy_id = R8152_PHY_ID; /* Internal PHY */ 9178 break; 9179 9180 case SIOCGMIIREG: 9181 mutex_lock(&tp->control); 9182 data->val_out = r8152_mdio_read(tp, data->reg_num); 9183 mutex_unlock(&tp->control); 9184 break; 9185 9186 case SIOCSMIIREG: 9187 if (!capable(CAP_NET_ADMIN)) { 9188 res = -EPERM; 9189 break; 9190 } 9191 mutex_lock(&tp->control); 9192 r8152_mdio_write(tp, data->reg_num, data->val_in); 9193 mutex_unlock(&tp->control); 9194 break; 9195 9196 default: 9197 res = -EOPNOTSUPP; 9198 } 9199 9200 usb_autopm_put_interface(tp->intf); 9201 9202 out: 9203 return res; 9204 } 9205 9206 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) 9207 { 9208 struct r8152 *tp = netdev_priv(dev); 9209 int ret; 9210 9211 switch (tp->version) { 9212 case RTL_VER_01: 9213 case RTL_VER_02: 9214 case RTL_VER_07: 9215 dev->mtu = new_mtu; 9216 return 0; 9217 default: 9218 break; 9219 } 9220 9221 ret = usb_autopm_get_interface(tp->intf); 9222 if (ret < 0) 9223 return ret; 9224 9225 mutex_lock(&tp->control); 9226 9227 dev->mtu = new_mtu; 9228 9229 if (netif_running(dev)) { 9230 if (tp->rtl_ops.change_mtu) 9231 tp->rtl_ops.change_mtu(tp); 9232 9233 if (netif_carrier_ok(dev)) { 9234 netif_stop_queue(dev); 9235 napi_disable(&tp->napi); 9236 tasklet_disable(&tp->tx_tl); 9237 tp->rtl_ops.disable(tp); 9238 tp->rtl_ops.enable(tp); 9239 rtl_start_rx(tp); 9240 tasklet_enable(&tp->tx_tl); 9241 napi_enable(&tp->napi); 9242 rtl8152_set_rx_mode(dev); 9243 netif_wake_queue(dev); 9244 } 9245 } 9246 9247 mutex_unlock(&tp->control); 9248 9249 usb_autopm_put_interface(tp->intf); 9250 9251 return ret; 9252 } 9253 9254 static const struct net_device_ops rtl8152_netdev_ops = { 9255 .ndo_open = rtl8152_open, 9256 .ndo_stop = rtl8152_close, 9257 .ndo_eth_ioctl = rtl8152_ioctl, 9258 .ndo_start_xmit = rtl8152_start_xmit, 9259 .ndo_tx_timeout = rtl8152_tx_timeout, 9260 .ndo_set_features = rtl8152_set_features, 9261 .ndo_set_rx_mode = rtl8152_set_rx_mode, 9262 .ndo_set_mac_address = rtl8152_set_mac_address, 9263 .ndo_change_mtu = rtl8152_change_mtu, 9264 .ndo_validate_addr = eth_validate_addr, 9265 .ndo_features_check = rtl8152_features_check, 9266 }; 9267 9268 static void rtl8152_unload(struct r8152 *tp) 9269 { 9270 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9271 return; 9272 9273 if (tp->version != RTL_VER_01) 9274 r8152_power_cut_en(tp, true); 9275 } 9276 9277 static void rtl8153_unload(struct r8152 *tp) 9278 { 9279 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9280 return; 9281 9282 r8153_power_cut_en(tp, false); 9283 } 9284 9285 static void rtl8153b_unload(struct r8152 *tp) 9286 { 9287 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) 9288 return; 9289 9290 r8153b_power_cut_en(tp, false); 9291 } 9292 9293 static int rtl_ops_init(struct r8152 *tp) 9294 { 9295 struct rtl_ops *ops = &tp->rtl_ops; 9296 int ret = 0; 9297 9298 switch (tp->version) { 9299 case RTL_VER_01: 9300 case RTL_VER_02: 9301 case RTL_VER_07: 9302 ops->init = r8152b_init; 9303 ops->enable = rtl8152_enable; 9304 ops->disable = rtl8152_disable; 9305 ops->up = rtl8152_up; 9306 ops->down = rtl8152_down; 9307 ops->unload = rtl8152_unload; 9308 ops->eee_get = r8152_get_eee; 9309 ops->eee_set = r8152_set_eee; 9310 ops->in_nway = rtl8152_in_nway; 9311 ops->hw_phy_cfg = r8152b_hw_phy_cfg; 9312 ops->autosuspend_en = rtl_runtime_suspend_enable; 9313 tp->rx_buf_sz = 16 * 1024; 9314 tp->eee_en = true; 9315 tp->eee_adv = MDIO_EEE_100TX; 9316 break; 9317 9318 case RTL_VER_03: 9319 case RTL_VER_04: 9320 case RTL_VER_05: 9321 case RTL_VER_06: 9322 ops->init = r8153_init; 9323 ops->enable = rtl8153_enable; 9324 ops->disable = rtl8153_disable; 9325 ops->up = rtl8153_up; 9326 ops->down = rtl8153_down; 9327 ops->unload = rtl8153_unload; 9328 ops->eee_get = r8153_get_eee; 9329 ops->eee_set = r8152_set_eee; 9330 ops->in_nway = rtl8153_in_nway; 9331 ops->hw_phy_cfg = r8153_hw_phy_cfg; 9332 ops->autosuspend_en = rtl8153_runtime_enable; 9333 ops->change_mtu = rtl8153_change_mtu; 9334 if (tp->udev->speed < USB_SPEED_SUPER) 9335 tp->rx_buf_sz = 16 * 1024; 9336 else 9337 tp->rx_buf_sz = 32 * 1024; 9338 tp->eee_en = true; 9339 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9340 break; 9341 9342 case RTL_VER_08: 9343 case RTL_VER_09: 9344 ops->init = r8153b_init; 9345 ops->enable = rtl8153_enable; 9346 ops->disable = rtl8153_disable; 9347 ops->up = rtl8153b_up; 9348 ops->down = rtl8153b_down; 9349 ops->unload = rtl8153b_unload; 9350 ops->eee_get = r8153_get_eee; 9351 ops->eee_set = r8152_set_eee; 9352 ops->in_nway = rtl8153_in_nway; 9353 ops->hw_phy_cfg = r8153b_hw_phy_cfg; 9354 ops->autosuspend_en = rtl8153b_runtime_enable; 9355 ops->change_mtu = rtl8153_change_mtu; 9356 tp->rx_buf_sz = 32 * 1024; 9357 tp->eee_en = true; 9358 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9359 break; 9360 9361 case RTL_VER_11: 9362 tp->eee_en = true; 9363 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9364 fallthrough; 9365 case RTL_VER_10: 9366 ops->init = r8156_init; 9367 ops->enable = rtl8156_enable; 9368 ops->disable = rtl8156_disable; 9369 ops->up = rtl8156_up; 9370 ops->down = rtl8156_down; 9371 ops->unload = rtl8153_unload; 9372 ops->eee_get = r8153_get_eee; 9373 ops->eee_set = r8152_set_eee; 9374 ops->in_nway = rtl8153_in_nway; 9375 ops->hw_phy_cfg = r8156_hw_phy_cfg; 9376 ops->autosuspend_en = rtl8156_runtime_enable; 9377 ops->change_mtu = rtl8156_change_mtu; 9378 tp->rx_buf_sz = 48 * 1024; 9379 tp->support_2500full = 1; 9380 break; 9381 9382 case RTL_VER_12: 9383 case RTL_VER_13: 9384 tp->support_2500full = 1; 9385 fallthrough; 9386 case RTL_VER_15: 9387 tp->eee_en = true; 9388 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9389 ops->init = r8156b_init; 9390 ops->enable = rtl8156b_enable; 9391 ops->disable = rtl8153_disable; 9392 ops->up = rtl8156_up; 9393 ops->down = rtl8156_down; 9394 ops->unload = rtl8153_unload; 9395 ops->eee_get = r8153_get_eee; 9396 ops->eee_set = r8152_set_eee; 9397 ops->in_nway = rtl8153_in_nway; 9398 ops->hw_phy_cfg = r8156b_hw_phy_cfg; 9399 ops->autosuspend_en = rtl8156_runtime_enable; 9400 ops->change_mtu = rtl8156_change_mtu; 9401 tp->rx_buf_sz = 48 * 1024; 9402 break; 9403 9404 case RTL_VER_14: 9405 ops->init = r8153c_init; 9406 ops->enable = rtl8153_enable; 9407 ops->disable = rtl8153_disable; 9408 ops->up = rtl8153c_up; 9409 ops->down = rtl8153b_down; 9410 ops->unload = rtl8153_unload; 9411 ops->eee_get = r8153_get_eee; 9412 ops->eee_set = r8152_set_eee; 9413 ops->in_nway = rtl8153_in_nway; 9414 ops->hw_phy_cfg = r8153c_hw_phy_cfg; 9415 ops->autosuspend_en = rtl8153c_runtime_enable; 9416 ops->change_mtu = rtl8153c_change_mtu; 9417 tp->rx_buf_sz = 32 * 1024; 9418 tp->eee_en = true; 9419 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; 9420 break; 9421 9422 default: 9423 ret = -ENODEV; 9424 dev_err(&tp->intf->dev, "Unknown Device\n"); 9425 break; 9426 } 9427 9428 return ret; 9429 } 9430 9431 #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw" 9432 #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw" 9433 #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw" 9434 #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw" 9435 #define FIRMWARE_8153C_1 "rtl_nic/rtl8153c-1.fw" 9436 #define FIRMWARE_8156A_2 "rtl_nic/rtl8156a-2.fw" 9437 #define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw" 9438 9439 MODULE_FIRMWARE(FIRMWARE_8153A_2); 9440 MODULE_FIRMWARE(FIRMWARE_8153A_3); 9441 MODULE_FIRMWARE(FIRMWARE_8153A_4); 9442 MODULE_FIRMWARE(FIRMWARE_8153B_2); 9443 MODULE_FIRMWARE(FIRMWARE_8153C_1); 9444 MODULE_FIRMWARE(FIRMWARE_8156A_2); 9445 MODULE_FIRMWARE(FIRMWARE_8156B_2); 9446 9447 static int rtl_fw_init(struct r8152 *tp) 9448 { 9449 struct rtl_fw *rtl_fw = &tp->rtl_fw; 9450 9451 switch (tp->version) { 9452 case RTL_VER_04: 9453 rtl_fw->fw_name = FIRMWARE_8153A_2; 9454 rtl_fw->pre_fw = r8153_pre_firmware_1; 9455 rtl_fw->post_fw = r8153_post_firmware_1; 9456 break; 9457 case RTL_VER_05: 9458 rtl_fw->fw_name = FIRMWARE_8153A_3; 9459 rtl_fw->pre_fw = r8153_pre_firmware_2; 9460 rtl_fw->post_fw = r8153_post_firmware_2; 9461 break; 9462 case RTL_VER_06: 9463 rtl_fw->fw_name = FIRMWARE_8153A_4; 9464 rtl_fw->post_fw = r8153_post_firmware_3; 9465 break; 9466 case RTL_VER_09: 9467 rtl_fw->fw_name = FIRMWARE_8153B_2; 9468 rtl_fw->pre_fw = r8153b_pre_firmware_1; 9469 rtl_fw->post_fw = r8153b_post_firmware_1; 9470 break; 9471 case RTL_VER_11: 9472 rtl_fw->fw_name = FIRMWARE_8156A_2; 9473 rtl_fw->post_fw = r8156a_post_firmware_1; 9474 break; 9475 case RTL_VER_13: 9476 case RTL_VER_15: 9477 rtl_fw->fw_name = FIRMWARE_8156B_2; 9478 break; 9479 case RTL_VER_14: 9480 rtl_fw->fw_name = FIRMWARE_8153C_1; 9481 rtl_fw->pre_fw = r8153b_pre_firmware_1; 9482 rtl_fw->post_fw = r8153c_post_firmware_1; 9483 break; 9484 default: 9485 break; 9486 } 9487 9488 return 0; 9489 } 9490 9491 static u8 __rtl_get_hw_ver(struct usb_device *udev) 9492 { 9493 u32 ocp_data = 0; 9494 __le32 *tmp; 9495 u8 version; 9496 int ret; 9497 9498 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); 9499 if (!tmp) 9500 return 0; 9501 9502 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 9503 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 9504 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 9505 USB_CTRL_GET_TIMEOUT); 9506 if (ret > 0) 9507 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; 9508 9509 kfree(tmp); 9510 9511 switch (ocp_data) { 9512 case 0x4c00: 9513 version = RTL_VER_01; 9514 break; 9515 case 0x4c10: 9516 version = RTL_VER_02; 9517 break; 9518 case 0x5c00: 9519 version = RTL_VER_03; 9520 break; 9521 case 0x5c10: 9522 version = RTL_VER_04; 9523 break; 9524 case 0x5c20: 9525 version = RTL_VER_05; 9526 break; 9527 case 0x5c30: 9528 version = RTL_VER_06; 9529 break; 9530 case 0x4800: 9531 version = RTL_VER_07; 9532 break; 9533 case 0x6000: 9534 version = RTL_VER_08; 9535 break; 9536 case 0x6010: 9537 version = RTL_VER_09; 9538 break; 9539 case 0x7010: 9540 version = RTL_TEST_01; 9541 break; 9542 case 0x7020: 9543 version = RTL_VER_10; 9544 break; 9545 case 0x7030: 9546 version = RTL_VER_11; 9547 break; 9548 case 0x7400: 9549 version = RTL_VER_12; 9550 break; 9551 case 0x7410: 9552 version = RTL_VER_13; 9553 break; 9554 case 0x6400: 9555 version = RTL_VER_14; 9556 break; 9557 case 0x7420: 9558 version = RTL_VER_15; 9559 break; 9560 default: 9561 version = RTL_VER_UNKNOWN; 9562 dev_info(&udev->dev, "Unknown version 0x%04x\n", ocp_data); 9563 break; 9564 } 9565 9566 return version; 9567 } 9568 9569 u8 rtl8152_get_version(struct usb_interface *intf) 9570 { 9571 u8 version; 9572 9573 version = __rtl_get_hw_ver(interface_to_usbdev(intf)); 9574 9575 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); 9576 9577 return version; 9578 } 9579 EXPORT_SYMBOL_GPL(rtl8152_get_version); 9580 9581 static bool rtl8152_supports_lenovo_macpassthru(struct usb_device *udev) 9582 { 9583 int parent_vendor_id = le16_to_cpu(udev->parent->descriptor.idVendor); 9584 int product_id = le16_to_cpu(udev->descriptor.idProduct); 9585 int vendor_id = le16_to_cpu(udev->descriptor.idVendor); 9586 9587 if (vendor_id == VENDOR_ID_LENOVO) { 9588 switch (product_id) { 9589 case DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB: 9590 case DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK: 9591 case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2: 9592 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2: 9593 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3: 9594 case DEVICE_ID_THINKPAD_USB_C_DONGLE: 9595 return 1; 9596 } 9597 } else if (vendor_id == VENDOR_ID_REALTEK && parent_vendor_id == VENDOR_ID_LENOVO) { 9598 switch (product_id) { 9599 case 0x8153: 9600 return 1; 9601 } 9602 } 9603 return 0; 9604 } 9605 9606 static int rtl8152_probe(struct usb_interface *intf, 9607 const struct usb_device_id *id) 9608 { 9609 struct usb_device *udev = interface_to_usbdev(intf); 9610 struct r8152 *tp; 9611 struct net_device *netdev; 9612 u8 version; 9613 int ret; 9614 9615 if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC) 9616 return -ENODEV; 9617 9618 if (!rtl_check_vendor_ok(intf)) 9619 return -ENODEV; 9620 9621 version = rtl8152_get_version(intf); 9622 if (version == RTL_VER_UNKNOWN) 9623 return -ENODEV; 9624 9625 usb_reset_device(udev); 9626 netdev = alloc_etherdev(sizeof(struct r8152)); 9627 if (!netdev) { 9628 dev_err(&intf->dev, "Out of memory\n"); 9629 return -ENOMEM; 9630 } 9631 9632 SET_NETDEV_DEV(netdev, &intf->dev); 9633 tp = netdev_priv(netdev); 9634 tp->msg_enable = 0x7FFF; 9635 9636 tp->udev = udev; 9637 tp->netdev = netdev; 9638 tp->intf = intf; 9639 tp->version = version; 9640 9641 tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0); 9642 tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0); 9643 tp->pipe_in = usb_rcvbulkpipe(udev, 1); 9644 tp->pipe_out = usb_sndbulkpipe(udev, 2); 9645 tp->pipe_intr = usb_rcvintpipe(udev, 3); 9646 9647 switch (version) { 9648 case RTL_VER_01: 9649 case RTL_VER_02: 9650 case RTL_VER_07: 9651 tp->mii.supports_gmii = 0; 9652 break; 9653 default: 9654 tp->mii.supports_gmii = 1; 9655 break; 9656 } 9657 9658 ret = rtl_ops_init(tp); 9659 if (ret) 9660 goto out; 9661 9662 rtl_fw_init(tp); 9663 9664 mutex_init(&tp->control); 9665 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); 9666 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t); 9667 tasklet_setup(&tp->tx_tl, bottom_half); 9668 tasklet_disable(&tp->tx_tl); 9669 9670 netdev->netdev_ops = &rtl8152_netdev_ops; 9671 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; 9672 9673 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 9674 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | 9675 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | 9676 NETIF_F_HW_VLAN_CTAG_TX; 9677 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | 9678 NETIF_F_TSO | NETIF_F_FRAGLIST | 9679 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | 9680 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; 9681 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | 9682 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | 9683 NETIF_F_IPV6_CSUM | NETIF_F_TSO6; 9684 9685 if (tp->version == RTL_VER_01) { 9686 netdev->features &= ~NETIF_F_RXCSUM; 9687 netdev->hw_features &= ~NETIF_F_RXCSUM; 9688 } 9689 9690 tp->lenovo_macpassthru = rtl8152_supports_lenovo_macpassthru(udev); 9691 9692 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && 9693 (!strcmp(udev->serial, "000001000000") || 9694 !strcmp(udev->serial, "000002000000"))) { 9695 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); 9696 tp->dell_tb_rx_agg_bug = 1; 9697 } 9698 9699 netdev->ethtool_ops = &ops; 9700 netif_set_tso_max_size(netdev, RTL_LIMITED_TSO_SIZE); 9701 9702 /* MTU range: 68 - 1500 or 9194 */ 9703 netdev->min_mtu = ETH_MIN_MTU; 9704 switch (tp->version) { 9705 case RTL_VER_03: 9706 case RTL_VER_04: 9707 case RTL_VER_05: 9708 case RTL_VER_06: 9709 case RTL_VER_08: 9710 case RTL_VER_09: 9711 case RTL_VER_14: 9712 netdev->max_mtu = size_to_mtu(9 * 1024); 9713 break; 9714 case RTL_VER_10: 9715 case RTL_VER_11: 9716 netdev->max_mtu = size_to_mtu(15 * 1024); 9717 break; 9718 case RTL_VER_12: 9719 case RTL_VER_13: 9720 case RTL_VER_15: 9721 netdev->max_mtu = size_to_mtu(16 * 1024); 9722 break; 9723 case RTL_VER_01: 9724 case RTL_VER_02: 9725 case RTL_VER_07: 9726 default: 9727 netdev->max_mtu = ETH_DATA_LEN; 9728 break; 9729 } 9730 9731 tp->mii.dev = netdev; 9732 tp->mii.mdio_read = read_mii_word; 9733 tp->mii.mdio_write = write_mii_word; 9734 tp->mii.phy_id_mask = 0x3f; 9735 tp->mii.reg_num_mask = 0x1f; 9736 tp->mii.phy_id = R8152_PHY_ID; 9737 9738 tp->autoneg = AUTONEG_ENABLE; 9739 tp->speed = SPEED_100; 9740 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | 9741 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; 9742 if (tp->mii.supports_gmii) { 9743 if (tp->support_2500full && 9744 tp->udev->speed >= USB_SPEED_SUPER) { 9745 tp->speed = SPEED_2500; 9746 tp->advertising |= RTL_ADVERTISED_2500_FULL; 9747 } else { 9748 tp->speed = SPEED_1000; 9749 } 9750 tp->advertising |= RTL_ADVERTISED_1000_FULL; 9751 } 9752 tp->duplex = DUPLEX_FULL; 9753 9754 tp->rx_copybreak = RTL8152_RXFG_HEADSZ; 9755 tp->rx_pending = 10 * RTL8152_MAX_RX; 9756 9757 intf->needs_remote_wakeup = 1; 9758 9759 if (!rtl_can_wakeup(tp)) 9760 __rtl_set_wol(tp, 0); 9761 else 9762 tp->saved_wolopts = __rtl_get_wol(tp); 9763 9764 tp->rtl_ops.init(tp); 9765 #if IS_BUILTIN(CONFIG_USB_RTL8152) 9766 /* Retry in case request_firmware() is not ready yet. */ 9767 tp->rtl_fw.retry = true; 9768 #endif 9769 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); 9770 set_ethernet_addr(tp, false); 9771 9772 usb_set_intfdata(intf, tp); 9773 9774 netif_napi_add(netdev, &tp->napi, r8152_poll); 9775 9776 ret = register_netdev(netdev); 9777 if (ret != 0) { 9778 dev_err(&intf->dev, "couldn't register the device\n"); 9779 goto out1; 9780 } 9781 9782 if (tp->saved_wolopts) 9783 device_set_wakeup_enable(&udev->dev, true); 9784 else 9785 device_set_wakeup_enable(&udev->dev, false); 9786 9787 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); 9788 9789 return 0; 9790 9791 out1: 9792 tasklet_kill(&tp->tx_tl); 9793 cancel_delayed_work_sync(&tp->hw_phy_work); 9794 if (tp->rtl_ops.unload) 9795 tp->rtl_ops.unload(tp); 9796 rtl8152_release_firmware(tp); 9797 usb_set_intfdata(intf, NULL); 9798 out: 9799 free_netdev(netdev); 9800 return ret; 9801 } 9802 9803 static void rtl8152_disconnect(struct usb_interface *intf) 9804 { 9805 struct r8152 *tp = usb_get_intfdata(intf); 9806 9807 usb_set_intfdata(intf, NULL); 9808 if (tp) { 9809 rtl_set_unplug(tp); 9810 9811 unregister_netdev(tp->netdev); 9812 tasklet_kill(&tp->tx_tl); 9813 cancel_delayed_work_sync(&tp->hw_phy_work); 9814 if (tp->rtl_ops.unload) 9815 tp->rtl_ops.unload(tp); 9816 rtl8152_release_firmware(tp); 9817 free_netdev(tp->netdev); 9818 } 9819 } 9820 9821 /* table of devices that work with this driver */ 9822 static const struct usb_device_id rtl8152_table[] = { 9823 /* Realtek */ 9824 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8050) }, 9825 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8053) }, 9826 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8152) }, 9827 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8153) }, 9828 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8155) }, 9829 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8156) }, 9830 9831 /* Microsoft */ 9832 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab) }, 9833 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6) }, 9834 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927) }, 9835 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0c5e) }, 9836 { USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101) }, 9837 { USB_DEVICE(VENDOR_ID_LENOVO, 0x304f) }, 9838 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3054) }, 9839 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3062) }, 9840 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3069) }, 9841 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3082) }, 9842 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7205) }, 9843 { USB_DEVICE(VENDOR_ID_LENOVO, 0x720c) }, 9844 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7214) }, 9845 { USB_DEVICE(VENDOR_ID_LENOVO, 0x721e) }, 9846 { USB_DEVICE(VENDOR_ID_LENOVO, 0xa387) }, 9847 { USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041) }, 9848 { USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff) }, 9849 { USB_DEVICE(VENDOR_ID_TPLINK, 0x0601) }, 9850 { USB_DEVICE(VENDOR_ID_DLINK, 0xb301) }, 9851 {} 9852 }; 9853 9854 MODULE_DEVICE_TABLE(usb, rtl8152_table); 9855 9856 static struct usb_driver rtl8152_driver = { 9857 .name = MODULENAME, 9858 .id_table = rtl8152_table, 9859 .probe = rtl8152_probe, 9860 .disconnect = rtl8152_disconnect, 9861 .suspend = rtl8152_suspend, 9862 .resume = rtl8152_resume, 9863 .reset_resume = rtl8152_reset_resume, 9864 .pre_reset = rtl8152_pre_reset, 9865 .post_reset = rtl8152_post_reset, 9866 .supports_autosuspend = 1, 9867 .disable_hub_initiated_lpm = 1, 9868 }; 9869 9870 static int rtl8152_cfgselector_probe(struct usb_device *udev) 9871 { 9872 struct usb_host_config *c; 9873 int i, num_configs; 9874 9875 /* Switch the device to vendor mode, if and only if the vendor mode 9876 * driver supports it. 9877 */ 9878 if (__rtl_get_hw_ver(udev) == RTL_VER_UNKNOWN) 9879 return 0; 9880 9881 /* The vendor mode is not always config #1, so to find it out. */ 9882 c = udev->config; 9883 num_configs = udev->descriptor.bNumConfigurations; 9884 for (i = 0; i < num_configs; (i++, c++)) { 9885 struct usb_interface_descriptor *desc = NULL; 9886 9887 if (!c->desc.bNumInterfaces) 9888 continue; 9889 desc = &c->intf_cache[0]->altsetting->desc; 9890 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) 9891 break; 9892 } 9893 9894 if (i == num_configs) 9895 return -ENODEV; 9896 9897 if (usb_set_configuration(udev, c->desc.bConfigurationValue)) { 9898 dev_err(&udev->dev, "Failed to set configuration %d\n", 9899 c->desc.bConfigurationValue); 9900 return -ENODEV; 9901 } 9902 9903 return 0; 9904 } 9905 9906 static struct usb_device_driver rtl8152_cfgselector_driver = { 9907 .name = MODULENAME "-cfgselector", 9908 .probe = rtl8152_cfgselector_probe, 9909 .id_table = rtl8152_table, 9910 .generic_subclass = 1, 9911 .supports_autosuspend = 1, 9912 }; 9913 9914 static int __init rtl8152_driver_init(void) 9915 { 9916 int ret; 9917 9918 ret = usb_register_device_driver(&rtl8152_cfgselector_driver, THIS_MODULE); 9919 if (ret) 9920 return ret; 9921 return usb_register(&rtl8152_driver); 9922 } 9923 9924 static void __exit rtl8152_driver_exit(void) 9925 { 9926 usb_deregister(&rtl8152_driver); 9927 usb_deregister_device_driver(&rtl8152_cfgselector_driver); 9928 } 9929 9930 module_init(rtl8152_driver_init); 9931 module_exit(rtl8152_driver_exit); 9932 9933 MODULE_AUTHOR(DRIVER_AUTHOR); 9934 MODULE_DESCRIPTION(DRIVER_DESC); 9935 MODULE_LICENSE("GPL"); 9936 MODULE_VERSION(DRIVER_VERSION); 9937