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