1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
6 #include <linux/signal.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <linux/mii.h>
12 #include <linux/ethtool.h>
13 #include <linux/usb.h>
14 #include <linux/crc32.h>
15 #include <linux/if_vlan.h>
16 #include <linux/uaccess.h>
17 #include <linux/list.h>
19 #include <linux/ipv6.h>
20 #include <net/ip6_checksum.h>
21 #include <uapi/linux/mdio.h>
22 #include <linux/mdio.h>
23 #include <linux/usb/cdc.h>
24 #include <linux/suspend.h>
25 #include <linux/atomic.h>
26 #include <linux/acpi.h>
27 #include <linux/firmware.h>
28 #include <crypto/hash.h>
29 #include <linux/usb/r8152.h>
31 /* Information for net-next */
32 #define NETNEXT_VERSION "12"
34 /* Information for net */
35 #define NET_VERSION "13"
37 #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
39 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
40 #define MODULENAME "r8152"
42 #define R8152_PHY_ID 32
44 #define PLA_IDR 0xc000
45 #define PLA_RCR 0xc010
46 #define PLA_RCR1 0xc012
47 #define PLA_RMS 0xc016
48 #define PLA_RXFIFO_CTRL0 0xc0a0
49 #define PLA_RXFIFO_FULL 0xc0a2
50 #define PLA_RXFIFO_CTRL1 0xc0a4
51 #define PLA_RX_FIFO_FULL 0xc0a6
52 #define PLA_RXFIFO_CTRL2 0xc0a8
53 #define PLA_RX_FIFO_EMPTY 0xc0aa
54 #define PLA_DMY_REG0 0xc0b0
55 #define PLA_FMC 0xc0b4
56 #define PLA_CFG_WOL 0xc0b6
57 #define PLA_TEREDO_CFG 0xc0bc
58 #define PLA_TEREDO_WAKE_BASE 0xc0c4
59 #define PLA_MAR 0xcd00
60 #define PLA_BACKUP 0xd000
61 #define PLA_BDC_CR 0xd1a0
62 #define PLA_TEREDO_TIMER 0xd2cc
63 #define PLA_REALWOW_TIMER 0xd2e8
64 #define PLA_UPHY_TIMER 0xd388
65 #define PLA_SUSPEND_FLAG 0xd38a
66 #define PLA_INDICATE_FALG 0xd38c
67 #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */
68 #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */
69 #define PLA_EXTRA_STATUS 0xd398
70 #define PLA_GPHY_CTRL 0xd3ae
71 #define PLA_POL_GPIO_CTRL 0xdc6a
72 #define PLA_EFUSE_DATA 0xdd00
73 #define PLA_EFUSE_CMD 0xdd02
74 #define PLA_LEDSEL 0xdd90
75 #define PLA_LED_FEATURE 0xdd92
76 #define PLA_PHYAR 0xde00
77 #define PLA_BOOT_CTRL 0xe004
78 #define PLA_LWAKE_CTRL_REG 0xe007
79 #define PLA_GPHY_INTR_IMR 0xe022
80 #define PLA_EEE_CR 0xe040
81 #define PLA_EEE_TXTWSYS 0xe04c
82 #define PLA_EEE_TXTWSYS_2P5G 0xe058
83 #define PLA_EEEP_CR 0xe080
84 #define PLA_MAC_PWR_CTRL 0xe0c0
85 #define PLA_MAC_PWR_CTRL2 0xe0ca
86 #define PLA_MAC_PWR_CTRL3 0xe0cc
87 #define PLA_MAC_PWR_CTRL4 0xe0ce
88 #define PLA_WDT6_CTRL 0xe428
89 #define PLA_TCR0 0xe610
90 #define PLA_TCR1 0xe612
91 #define PLA_MTPS 0xe615
92 #define PLA_TXFIFO_CTRL 0xe618
93 #define PLA_TXFIFO_FULL 0xe61a
94 #define PLA_RSTTALLY 0xe800
96 #define PLA_CRWECR 0xe81c
97 #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
98 #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
99 #define PLA_CONFIG5 0xe822
100 #define PLA_PHY_PWR 0xe84c
101 #define PLA_OOB_CTRL 0xe84f
102 #define PLA_CPCR 0xe854
103 #define PLA_MISC_0 0xe858
104 #define PLA_MISC_1 0xe85a
105 #define PLA_OCP_GPHY_BASE 0xe86c
106 #define PLA_TALLYCNT 0xe890
107 #define PLA_SFF_STS_7 0xe8de
108 #define PLA_PHYSTATUS 0xe908
109 #define PLA_CONFIG6 0xe90a /* CONFIG6 */
110 #define PLA_USB_CFG 0xe952
111 #define PLA_BP_BA 0xfc26
112 #define PLA_BP_0 0xfc28
113 #define PLA_BP_1 0xfc2a
114 #define PLA_BP_2 0xfc2c
115 #define PLA_BP_3 0xfc2e
116 #define PLA_BP_4 0xfc30
117 #define PLA_BP_5 0xfc32
118 #define PLA_BP_6 0xfc34
119 #define PLA_BP_7 0xfc36
120 #define PLA_BP_EN 0xfc38
122 #define USB_USB2PHY 0xb41e
123 #define USB_SSPHYLINK1 0xb426
124 #define USB_SSPHYLINK2 0xb428
125 #define USB_L1_CTRL 0xb45e
126 #define USB_U2P3_CTRL 0xb460
127 #define USB_CSR_DUMMY1 0xb464
128 #define USB_CSR_DUMMY2 0xb466
129 #define USB_DEV_STAT 0xb808
130 #define USB_CONNECT_TIMER 0xcbf8
131 #define USB_MSC_TIMER 0xcbfc
132 #define USB_BURST_SIZE 0xcfc0
133 #define USB_FW_FIX_EN0 0xcfca
134 #define USB_FW_FIX_EN1 0xcfcc
135 #define USB_LPM_CONFIG 0xcfd8
136 #define USB_ECM_OPTION 0xcfee
137 #define USB_CSTMR 0xcfef /* RTL8153A */
138 #define USB_MISC_2 0xcfff
139 #define USB_ECM_OP 0xd26b
140 #define USB_GPHY_CTRL 0xd284
141 #define USB_SPEED_OPTION 0xd32a
142 #define USB_FW_CTRL 0xd334 /* RTL8153B */
143 #define USB_FC_TIMER 0xd340
144 #define USB_USB_CTRL 0xd406
145 #define USB_PHY_CTRL 0xd408
146 #define USB_TX_AGG 0xd40a
147 #define USB_RX_BUF_TH 0xd40c
148 #define USB_USB_TIMER 0xd428
149 #define USB_RX_EARLY_TIMEOUT 0xd42c
150 #define USB_RX_EARLY_SIZE 0xd42e
151 #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
152 #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
153 #define USB_TX_DMA 0xd434
154 #define USB_UPT_RXDMA_OWN 0xd437
155 #define USB_UPHY3_MDCMDIO 0xd480
156 #define USB_TOLERANCE 0xd490
157 #define USB_LPM_CTRL 0xd41a
158 #define USB_BMU_RESET 0xd4b0
159 #define USB_BMU_CONFIG 0xd4b4
160 #define USB_U1U2_TIMER 0xd4da
161 #define USB_FW_TASK 0xd4e8 /* RTL8153B */
162 #define USB_RX_AGGR_NUM 0xd4ee
163 #define USB_UPS_CTRL 0xd800
164 #define USB_POWER_CUT 0xd80a
165 #define USB_MISC_0 0xd81a
166 #define USB_MISC_1 0xd81f
167 #define USB_AFE_CTRL2 0xd824
168 #define USB_UPHY_XTAL 0xd826
169 #define USB_UPS_CFG 0xd842
170 #define USB_UPS_FLAGS 0xd848
171 #define USB_WDT1_CTRL 0xe404
172 #define USB_WDT11_CTRL 0xe43c
173 #define USB_BP_BA PLA_BP_BA
174 #define USB_BP_0 PLA_BP_0
175 #define USB_BP_1 PLA_BP_1
176 #define USB_BP_2 PLA_BP_2
177 #define USB_BP_3 PLA_BP_3
178 #define USB_BP_4 PLA_BP_4
179 #define USB_BP_5 PLA_BP_5
180 #define USB_BP_6 PLA_BP_6
181 #define USB_BP_7 PLA_BP_7
182 #define USB_BP_EN PLA_BP_EN /* RTL8153A */
183 #define USB_BP_8 0xfc38 /* RTL8153B */
184 #define USB_BP_9 0xfc3a
185 #define USB_BP_10 0xfc3c
186 #define USB_BP_11 0xfc3e
187 #define USB_BP_12 0xfc40
188 #define USB_BP_13 0xfc42
189 #define USB_BP_14 0xfc44
190 #define USB_BP_15 0xfc46
191 #define USB_BP2_EN 0xfc48
194 #define OCP_ALDPS_CONFIG 0x2010
195 #define OCP_EEE_CONFIG1 0x2080
196 #define OCP_EEE_CONFIG2 0x2092
197 #define OCP_EEE_CONFIG3 0x2094
198 #define OCP_BASE_MII 0xa400
199 #define OCP_EEE_AR 0xa41a
200 #define OCP_EEE_DATA 0xa41c
201 #define OCP_PHY_STATUS 0xa420
202 #define OCP_NCTL_CFG 0xa42c
203 #define OCP_POWER_CFG 0xa430
204 #define OCP_EEE_CFG 0xa432
205 #define OCP_SRAM_ADDR 0xa436
206 #define OCP_SRAM_DATA 0xa438
207 #define OCP_DOWN_SPEED 0xa442
208 #define OCP_EEE_ABLE 0xa5c4
209 #define OCP_EEE_ADV 0xa5d0
210 #define OCP_EEE_LPABLE 0xa5d2
211 #define OCP_10GBT_CTRL 0xa5d4
212 #define OCP_10GBT_STAT 0xa5d6
213 #define OCP_EEE_ADV2 0xa6d4
214 #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
215 #define OCP_PHY_PATCH_STAT 0xb800
216 #define OCP_PHY_PATCH_CMD 0xb820
217 #define OCP_PHY_LOCK 0xb82e
218 #define OCP_ADC_IOFFSET 0xbcfc
219 #define OCP_ADC_CFG 0xbc06
220 #define OCP_SYSCLK_CFG 0xc416
223 #define SRAM_GREEN_CFG 0x8011
224 #define SRAM_LPF_CFG 0x8012
225 #define SRAM_GPHY_FW_VER 0x801e
226 #define SRAM_10M_AMP1 0x8080
227 #define SRAM_10M_AMP2 0x8082
228 #define SRAM_IMPEDANCE 0x8084
229 #define SRAM_PHY_LOCK 0xb82e
232 #define RCR_AAP 0x00000001
233 #define RCR_APM 0x00000002
234 #define RCR_AM 0x00000004
235 #define RCR_AB 0x00000008
236 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
237 #define SLOT_EN BIT(11)
240 #define OUTER_VLAN BIT(7)
241 #define INNER_VLAN BIT(6)
243 /* PLA_RXFIFO_CTRL0 */
244 #define RXFIFO_THR1_NORMAL 0x00080002
245 #define RXFIFO_THR1_OOB 0x01800003
247 /* PLA_RXFIFO_FULL */
248 #define RXFIFO_FULL_MASK 0xfff
250 /* PLA_RXFIFO_CTRL1 */
251 #define RXFIFO_THR2_FULL 0x00000060
252 #define RXFIFO_THR2_HIGH 0x00000038
253 #define RXFIFO_THR2_OOB 0x0000004a
254 #define RXFIFO_THR2_NORMAL 0x00a0
256 /* PLA_RXFIFO_CTRL2 */
257 #define RXFIFO_THR3_FULL 0x00000078
258 #define RXFIFO_THR3_HIGH 0x00000048
259 #define RXFIFO_THR3_OOB 0x0000005a
260 #define RXFIFO_THR3_NORMAL 0x0110
262 /* PLA_TXFIFO_CTRL */
263 #define TXFIFO_THR_NORMAL 0x00400008
264 #define TXFIFO_THR_NORMAL2 0x01000008
267 #define ECM_ALDPS 0x0002
270 #define FMC_FCR_MCU_EN 0x0001
273 #define EEEP_CR_EEEP_TX 0x0002
276 #define WDT6_SET_MODE 0x0010
279 #define TCR0_TX_EMPTY 0x0800
280 #define TCR0_AUTO_FIFO 0x0080
283 #define VERSION_MASK 0x7cf0
284 #define IFG_MASK (BIT(3) | BIT(9) | BIT(8))
285 #define IFG_144NS BIT(9)
286 #define IFG_96NS (BIT(9) | BIT(8))
289 #define MTPS_JUMBO (12 * 1024 / 64)
290 #define MTPS_DEFAULT (6 * 1024 / 64)
293 #define TALLY_RESET 0x0001
301 #define CRWECR_NORAML 0x00
302 #define CRWECR_CONFIG 0xc0
305 #define NOW_IS_OOB 0x80
306 #define TXFIFO_EMPTY 0x20
307 #define RXFIFO_EMPTY 0x10
308 #define LINK_LIST_READY 0x02
309 #define DIS_MCU_CLROOB 0x01
310 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
313 #define RXDY_GATED_EN 0x0008
316 #define RE_INIT_LL 0x8000
317 #define MCU_BORW_EN 0x4000
320 #define FLOW_CTRL_EN BIT(0)
321 #define CPCR_RX_VLAN 0x0040
324 #define MAGIC_EN 0x0001
327 #define TEREDO_SEL 0x8000
328 #define TEREDO_WAKE_MASK 0x7f00
329 #define TEREDO_RS_EVENT_MASK 0x00fe
330 #define OOB_TEREDO_EN 0x0001
333 #define ALDPS_PROXY_MODE 0x0001
336 #define EFUSE_READ_CMD BIT(15)
337 #define EFUSE_DATA_BIT16 BIT(7)
340 #define LINK_ON_WAKE_EN 0x0010
341 #define LINK_OFF_WAKE_EN 0x0008
344 #define LANWAKE_CLR_EN BIT(0)
347 #define EN_XG_LIP BIT(1)
348 #define EN_G_LIP BIT(2)
351 #define BWF_EN 0x0040
352 #define MWF_EN 0x0020
353 #define UWF_EN 0x0010
354 #define LAN_WAKE_EN 0x0002
356 /* PLA_LED_FEATURE */
357 #define LED_MODE_MASK 0x0700
360 #define TX_10M_IDLE_EN 0x0080
361 #define PFM_PWM_SWITCH 0x0040
362 #define TEST_IO_OFF BIT(4)
364 /* PLA_MAC_PWR_CTRL */
365 #define D3_CLK_GATED_EN 0x00004000
366 #define MCU_CLK_RATIO 0x07010f07
367 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
368 #define ALDPS_SPDWN_RATIO 0x0f87
370 /* PLA_MAC_PWR_CTRL2 */
371 #define EEE_SPDWN_RATIO 0x8007
372 #define MAC_CLK_SPDWN_EN BIT(15)
373 #define EEE_SPDWN_RATIO_MASK 0xff
375 /* PLA_MAC_PWR_CTRL3 */
376 #define PLA_MCU_SPDWN_EN BIT(14)
377 #define PKT_AVAIL_SPDWN_EN 0x0100
378 #define SUSPEND_SPDWN_EN 0x0004
379 #define U1U2_SPDWN_EN 0x0002
380 #define L1_SPDWN_EN 0x0001
382 /* PLA_MAC_PWR_CTRL4 */
383 #define PWRSAVE_SPDWN_EN 0x1000
384 #define RXDV_SPDWN_EN 0x0800
385 #define TX10MIDLE_EN 0x0100
386 #define IDLE_SPDWN_EN BIT(6)
387 #define TP100_SPDWN_EN 0x0020
388 #define TP500_SPDWN_EN 0x0010
389 #define TP1000_SPDWN_EN 0x0008
390 #define EEE_SPDWN_EN 0x0001
392 /* PLA_GPHY_INTR_IMR */
393 #define GPHY_STS_MSK 0x0001
394 #define SPEED_DOWN_MSK 0x0002
395 #define SPDWN_RXDV_MSK 0x0004
396 #define SPDWN_LINKCHG_MSK 0x0008
399 #define PHYAR_FLAG 0x80000000
402 #define EEE_RX_EN 0x0001
403 #define EEE_TX_EN 0x0002
406 #define AUTOLOAD_DONE 0x0002
408 /* PLA_LWAKE_CTRL_REG */
409 #define LANWAKE_PIN BIT(7)
411 /* PLA_SUSPEND_FLAG */
412 #define LINK_CHG_EVENT BIT(0)
414 /* PLA_INDICATE_FALG */
415 #define UPCOMING_RUNTIME_D3 BIT(0)
417 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */
418 #define DEBUG_OE BIT(0)
419 #define DEBUG_LTSSM 0x0082
421 /* PLA_EXTRA_STATUS */
422 #define CUR_LINK_OK BIT(15)
423 #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */
424 #define LINK_CHANGE_FLAG BIT(8)
425 #define POLL_LINK_CHG BIT(0)
428 #define GPHY_FLASH BIT(1)
430 /* PLA_POL_GPIO_CTRL */
431 #define DACK_DET_EN BIT(15)
432 #define POL_GPHY_PATCH BIT(4)
435 #define USB2PHY_SUSPEND 0x0001
436 #define USB2PHY_L1 0x0002
439 #define DELAY_PHY_PWR_CHG BIT(1)
442 #define pwd_dn_scale_mask 0x3ffe
443 #define pwd_dn_scale(x) ((x) << 1)
446 #define DYNAMIC_BURST 0x0001
449 #define EP4_FULL_FC 0x0001
452 #define STAT_SPEED_MASK 0x0006
453 #define STAT_SPEED_HIGH 0x0000
454 #define STAT_SPEED_FULL 0x0002
457 #define FW_FIX_SUSPEND BIT(14)
460 #define FW_IP_RESET_EN BIT(9)
463 #define LPM_U1U2_EN BIT(0)
466 #define TX_AGG_MAX_THRESHOLD 0x03
469 #define RX_THR_SUPPER 0x0c350180
470 #define RX_THR_HIGH 0x7a120180
471 #define RX_THR_SLOW 0xffff0180
472 #define RX_THR_B 0x00010001
475 #define TEST_MODE_DISABLE 0x00000001
476 #define TX_SIZE_ADJUST1 0x00000100
479 #define BMU_RESET_EP_IN 0x01
480 #define BMU_RESET_EP_OUT 0x02
483 #define ACT_ODMA BIT(1)
485 /* USB_UPT_RXDMA_OWN */
486 #define OWN_UPDATE BIT(0)
487 #define OWN_CLEAR BIT(1)
490 #define FC_PATCH_TASK BIT(1)
492 /* USB_RX_AGGR_NUM */
493 #define RX_AGGR_NUM_MASK 0x1ff
496 #define POWER_CUT 0x0100
498 /* USB_PM_CTRL_STATUS */
499 #define RESUME_INDICATE 0x0001
502 #define BYPASS_MAC_RESET BIT(5)
505 #define FORCE_SUPER BIT(0)
508 #define UPS_FORCE_PWR_DOWN BIT(0)
511 #define EN_ALL_SPEED BIT(0)
514 #define GPHY_PATCH_DONE BIT(2)
515 #define BYPASS_FLASH BIT(5)
516 #define BACKUP_RESTRORE BIT(6)
518 /* USB_SPEED_OPTION */
519 #define RG_PWRDN_EN BIT(8)
520 #define ALL_SPEED_OFF BIT(9)
523 #define FLOW_CTRL_PATCH_OPT BIT(1)
524 #define AUTO_SPEEDUP BIT(3)
525 #define FLOW_CTRL_PATCH_2 BIT(8)
528 #define CTRL_TIMER_EN BIT(15)
531 #define CDC_ECM_EN BIT(3)
532 #define RX_AGG_DISABLE 0x0010
533 #define RX_ZERO_EN 0x0080
536 #define U2P3_ENABLE 0x0001
537 #define RX_DETECT8 BIT(3)
540 #define PWR_EN 0x0001
541 #define PHASE2_EN 0x0008
542 #define UPS_EN BIT(4)
543 #define USP_PREWAKE BIT(5)
546 #define PCUT_STATUS 0x0001
548 /* USB_RX_EARLY_TIMEOUT */
549 #define COALESCE_SUPER 85000U
550 #define COALESCE_HIGH 250000U
551 #define COALESCE_SLOW 524280U
554 #define WTD1_EN BIT(0)
557 #define TIMER11_EN 0x0001
560 /* bit 4 ~ 5: fifo empty boundary */
561 #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
562 /* bit 2 ~ 3: LMP timer */
563 #define LPM_TIMER_MASK 0x0c
564 #define LPM_TIMER_500MS 0x04 /* 500 ms */
565 #define LPM_TIMER_500US 0x0c /* 500 us */
566 #define ROK_EXIT_LPM 0x02
569 #define SEN_VAL_MASK 0xf800
570 #define SEN_VAL_NORMAL 0xa000
571 #define SEL_RXIDLE 0x0100
574 #define OOBS_POLLING BIT(8)
577 #define SAW_CNT_1MS_MASK 0x0fff
578 #define MID_REVERSE BIT(5) /* RTL8156A */
581 #define UPS_FLAGS_R_TUNE BIT(0)
582 #define UPS_FLAGS_EN_10M_CKDIV BIT(1)
583 #define UPS_FLAGS_250M_CKDIV BIT(2)
584 #define UPS_FLAGS_EN_ALDPS BIT(3)
585 #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
586 #define UPS_FLAGS_SPEED_MASK (0xf << 16)
587 #define ups_flags_speed(x) ((x) << 16)
588 #define UPS_FLAGS_EN_EEE BIT(20)
589 #define UPS_FLAGS_EN_500M_EEE BIT(21)
590 #define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
591 #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23)
592 #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
593 #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
594 #define UPS_FLAGS_EN_GREEN BIT(26)
595 #define UPS_FLAGS_EN_FLOW_CTR BIT(27)
611 /* OCP_ALDPS_CONFIG */
612 #define ENPWRSAVE 0x8000
613 #define ENPDNPS 0x0200
614 #define LINKENA 0x0100
615 #define DIS_SDSAVE 0x0010
618 #define PHY_STAT_MASK 0x0007
619 #define PHY_STAT_EXT_INIT 2
620 #define PHY_STAT_LAN_ON 3
621 #define PHY_STAT_PWRDN 5
624 #define PGA_RETURN_EN BIT(1)
627 #define EEE_CLKDIV_EN 0x8000
628 #define EN_ALDPS 0x0004
629 #define EN_10M_PLLOFF 0x0001
631 /* OCP_EEE_CONFIG1 */
632 #define RG_TXLPI_MSK_HFDUP 0x8000
633 #define RG_MATCLR_EN 0x4000
634 #define EEE_10_CAP 0x2000
635 #define EEE_NWAY_EN 0x1000
636 #define TX_QUIET_EN 0x0200
637 #define RX_QUIET_EN 0x0100
638 #define sd_rise_time_mask 0x0070
639 #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
640 #define RG_RXLPI_MSK_HFDUP 0x0008
641 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
643 /* OCP_EEE_CONFIG2 */
644 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
645 #define RG_DACQUIET_EN 0x0400
646 #define RG_LDVQUIET_EN 0x0200
647 #define RG_CKRSEL 0x0020
648 #define RG_EEEPRG_EN 0x0010
650 /* OCP_EEE_CONFIG3 */
651 #define fast_snr_mask 0xff80
652 #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
653 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
654 #define MSK_PH 0x0006 /* bit 0 ~ 3 */
657 /* bit[15:14] function */
658 #define FUN_ADDR 0x0000
659 #define FUN_DATA 0x4000
660 /* bit[4:0] device addr */
663 #define CTAP_SHORT_EN 0x0040
664 #define EEE10_EN 0x0010
667 #define EN_EEE_CMODE BIT(14)
668 #define EN_EEE_1000 BIT(13)
669 #define EN_EEE_100 BIT(12)
670 #define EN_10M_CLKDIV BIT(11)
671 #define EN_10M_BGOFF 0x0080
674 #define RTL_ADV2_5G_F_R BIT(5) /* Advertise 2.5GBASE-T fast-retrain */
677 #define TXDIS_STATE 0x01
678 #define ABD_STATE 0x02
680 /* OCP_PHY_PATCH_STAT */
681 #define PATCH_READY BIT(6)
683 /* OCP_PHY_PATCH_CMD */
684 #define PATCH_REQUEST BIT(4)
687 #define PATCH_LOCK BIT(0)
690 #define CKADSEL_L 0x0100
691 #define ADC_EN 0x0080
692 #define EN_EMI_L 0x0040
695 #define sysclk_div_expo(x) (min(x, 5) << 8)
696 #define clk_div_expo(x) (min(x, 5) << 4)
699 #define GREEN_ETH_EN BIT(15)
700 #define R_TUNE_EN BIT(11)
703 #define LPF_AUTO_TUNE 0x8000
706 #define GDAC_IB_UPALL 0x0008
709 #define AMP_DN 0x0200
712 #define RX_DRIVING_MASK 0x6000
715 #define PHY_PATCH_LOCK 0x0001
718 #define AD_MASK 0xfee0
719 #define BND_MASK 0x0004
720 #define BD_MASK 0x0001
722 #define PASS_THRU_MASK 0x1
724 #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */
726 enum rtl_register_content {
739 #define is_speed_2500(_speed) (((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS))
740 #define is_flow_control(_speed) (((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow))
742 #define RTL8152_MAX_TX 4
743 #define RTL8152_MAX_RX 10
748 #define RTL8152_RX_MAX_PENDING 4096
749 #define RTL8152_RXFG_HEADSZ 256
751 #define INTR_LINK 0x0004
753 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
754 #define RTL8153_RMS RTL8153_MAX_PACKET
755 #define RTL8152_TX_TIMEOUT (5 * HZ)
756 #define mtu_to_size(m) ((m) + VLAN_ETH_HLEN + ETH_FCS_LEN)
757 #define size_to_mtu(s) ((s) - VLAN_ETH_HLEN - ETH_FCS_LEN)
758 #define rx_reserved_size(x) (mtu_to_size(x) + sizeof(struct rx_desc) + RX_ALIGN)
773 #define DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB 0x721e
774 #define DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK 0x3054
775 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
776 #define DEVICE_ID_THINKPAD_USB_C_DONGLE 0x720c
777 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
778 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3 0x3062
780 struct tally_counter {
787 __le32 tx_one_collision;
788 __le32 tx_multi_collision;
798 #define RX_LEN_MASK 0x7fff
801 #define RD_UDP_CS BIT(23)
802 #define RD_TCP_CS BIT(22)
803 #define RD_IPV6_CS BIT(20)
804 #define RD_IPV4_CS BIT(19)
807 #define IPF BIT(23) /* IP checksum fail */
808 #define UDPF BIT(22) /* UDP checksum fail */
809 #define TCPF BIT(21) /* TCP checksum fail */
810 #define RX_VLAN_TAG BIT(16)
819 #define TX_FS BIT(31) /* First segment of a packet */
820 #define TX_LS BIT(30) /* Final segment of a packet */
821 #define GTSENDV4 BIT(28)
822 #define GTSENDV6 BIT(27)
823 #define GTTCPHO_SHIFT 18
824 #define GTTCPHO_MAX 0x7fU
825 #define TX_LEN_MAX 0x3ffffU
828 #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
829 #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
830 #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
831 #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
833 #define MSS_MAX 0x7ffU
834 #define TCPHO_SHIFT 17
835 #define TCPHO_MAX 0x7ffU
836 #define TX_VLAN_TAG BIT(16)
842 struct list_head list, info_list;
844 struct r8152 *context;
850 struct list_head list;
852 struct r8152 *context;
861 struct usb_device *udev;
862 struct napi_struct napi;
863 struct usb_interface *intf;
864 struct net_device *netdev;
865 struct urb *intr_urb;
866 struct tx_agg tx_info[RTL8152_MAX_TX];
867 struct list_head rx_info, rx_used;
868 struct list_head rx_done, tx_free;
869 struct sk_buff_head tx_queue, rx_queue;
870 spinlock_t rx_lock, tx_lock;
871 struct delayed_work schedule, hw_phy_work;
872 struct mii_if_info mii;
873 struct mutex control; /* use for hw setting */
874 #ifdef CONFIG_PM_SLEEP
875 struct notifier_block pm_notifier;
877 struct tasklet_struct tx_tl;
880 void (*init)(struct r8152 *tp);
881 int (*enable)(struct r8152 *tp);
882 void (*disable)(struct r8152 *tp);
883 void (*up)(struct r8152 *tp);
884 void (*down)(struct r8152 *tp);
885 void (*unload)(struct r8152 *tp);
886 int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee);
887 int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee);
888 bool (*in_nway)(struct r8152 *tp);
889 void (*hw_phy_cfg)(struct r8152 *tp);
890 void (*autosuspend_en)(struct r8152 *tp, bool enable);
891 void (*change_mtu)(struct r8152 *tp);
904 u32 eee_plloff_100:1;
905 u32 eee_plloff_giga:1;
909 u32 ctap_short_off:1;
912 #define RTL_VER_SIZE 32
916 const struct firmware *fw;
918 char version[RTL_VER_SIZE];
919 int (*pre_fw)(struct r8152 *tp);
920 int (*post_fw)(struct r8152 *tp);
937 u32 fc_pause_on, fc_pause_off;
939 unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out;
941 u32 support_2500full:1;
942 u32 lenovo_macpassthru:1;
943 u32 dell_tb_rx_agg_bug:1;
954 * struct fw_block - block type and total length
955 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
956 * RTL_FW_USB and so on.
957 * @length: total length of the current block.
965 * struct fw_header - header of the firmware file
966 * @checksum: checksum of sha256 which is calculated from the whole file
967 * except the checksum field of the file. That is, calculate sha256
968 * from the version field to the end of the file.
969 * @version: version of this firmware.
970 * @blocks: the first firmware block of the file
974 char version[RTL_VER_SIZE];
975 struct fw_block blocks[];
978 enum rtl8152_fw_flags {
992 enum rtl8152_fw_fixup_cmd {
1004 struct fw_phy_speed_up {
1005 struct fw_block blk_hdr;
1014 struct fw_block blk_hdr;
1015 struct fw_phy_set ver;
1019 struct fw_phy_fixup {
1020 struct fw_block blk_hdr;
1021 struct fw_phy_set setting;
1026 struct fw_phy_union {
1027 struct fw_block blk_hdr;
1030 struct fw_phy_set pre_set[2];
1031 struct fw_phy_set bp[8];
1032 struct fw_phy_set bp_en;
1039 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
1040 * The layout of the firmware block is:
1041 * <struct fw_mac> + <info> + <firmware data>.
1042 * @blk_hdr: firmware descriptor (type, length)
1043 * @fw_offset: offset of the firmware binary data. The start address of
1044 * the data would be the address of struct fw_mac + @fw_offset.
1045 * @fw_reg: the register to load the firmware. Depends on chip.
1046 * @bp_ba_addr: the register to write break point base address. Depends on
1048 * @bp_ba_value: break point base address. Depends on chip.
1049 * @bp_en_addr: the register to write break point enabled mask. Depends
1051 * @bp_en_value: break point enabled mask. Depends on the firmware.
1052 * @bp_start: the start register of break points. Depends on chip.
1053 * @bp_num: the break point number which needs to be set for this firmware.
1054 * Depends on the firmware.
1055 * @bp: break points. Depends on firmware.
1056 * @reserved: reserved space (unused)
1057 * @fw_ver_reg: the register to store the fw version.
1058 * @fw_ver_data: the firmware version of the current type.
1059 * @info: additional information for debugging, and is followed by the
1060 * binary data of firmware.
1063 struct fw_block blk_hdr;
1072 __le16 bp[16]; /* any value determined by firmware */
1080 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
1081 * This is used to set patch key when loading the firmware of PHY.
1082 * @blk_hdr: firmware descriptor (type, length)
1083 * @key_reg: the register to write the patch key.
1084 * @key_data: patch key.
1085 * @reserved: reserved space (unused)
1087 struct fw_phy_patch_key {
1088 struct fw_block blk_hdr;
1095 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
1096 * The layout of the firmware block is:
1097 * <struct fw_phy_nc> + <info> + <firmware data>.
1098 * @blk_hdr: firmware descriptor (type, length)
1099 * @fw_offset: offset of the firmware binary data. The start address of
1100 * the data would be the address of struct fw_phy_nc + @fw_offset.
1101 * @fw_reg: the register to load the firmware. Depends on chip.
1102 * @ba_reg: the register to write the base address. Depends on chip.
1103 * @ba_data: base address. Depends on chip.
1104 * @patch_en_addr: the register of enabling patch mode. Depends on chip.
1105 * @patch_en_value: patch mode enabled mask. Depends on the firmware.
1106 * @mode_reg: the regitster of switching the mode.
1107 * @mode_pre: the mode needing to be set before loading the firmware.
1108 * @mode_post: the mode to be set when finishing to load the firmware.
1109 * @reserved: reserved space (unused)
1110 * @bp_start: the start register of break points. Depends on chip.
1111 * @bp_num: the break point number which needs to be set for this firmware.
1112 * Depends on the firmware.
1113 * @bp: break points. Depends on firmware.
1114 * @info: additional information for debugging, and is followed by the
1115 * binary data of firmware.
1118 struct fw_block blk_hdr;
1123 __le16 patch_en_addr;
1124 __le16 patch_en_value;
1143 RTL_FW_PHY_UNION_NC,
1144 RTL_FW_PHY_UNION_NC1,
1145 RTL_FW_PHY_UNION_NC2,
1146 RTL_FW_PHY_UNION_UC2,
1147 RTL_FW_PHY_UNION_UC,
1148 RTL_FW_PHY_UNION_MISC,
1149 RTL_FW_PHY_SPEED_UP,
1154 RTL_VER_UNKNOWN = 0,
1177 TX_CSUM_SUCCESS = 0,
1182 #define RTL_ADVERTISED_10_HALF BIT(0)
1183 #define RTL_ADVERTISED_10_FULL BIT(1)
1184 #define RTL_ADVERTISED_100_HALF BIT(2)
1185 #define RTL_ADVERTISED_100_FULL BIT(3)
1186 #define RTL_ADVERTISED_1000_HALF BIT(4)
1187 #define RTL_ADVERTISED_1000_FULL BIT(5)
1188 #define RTL_ADVERTISED_2500_FULL BIT(6)
1190 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
1191 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
1193 static const int multicast_filter_limit = 32;
1194 static unsigned int agg_buf_sz = 16384;
1196 #define RTL_LIMITED_TSO_SIZE (size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc))
1199 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1204 tmp = kmalloc(size, GFP_KERNEL);
1208 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_in,
1209 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
1210 value, index, tmp, size, 500);
1212 memset(data, 0xff, size);
1214 memcpy(data, tmp, size);
1222 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1227 tmp = kmemdup(data, size, GFP_KERNEL);
1231 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_out,
1232 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
1233 value, index, tmp, size, 500);
1240 static void rtl_set_unplug(struct r8152 *tp)
1242 if (tp->udev->state == USB_STATE_NOTATTACHED) {
1243 set_bit(RTL8152_UNPLUG, &tp->flags);
1244 smp_mb__after_atomic();
1248 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
1249 void *data, u16 type)
1254 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1257 /* both size and indix must be 4 bytes align */
1258 if ((size & 3) || !size || (index & 3) || !data)
1261 if ((u32)index + (u32)size > 0xffff)
1266 ret = get_registers(tp, index, type, limit, data);
1274 ret = get_registers(tp, index, type, size, data);
1291 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
1292 u16 size, void *data, u16 type)
1295 u16 byteen_start, byteen_end, byen;
1298 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1301 /* both size and indix must be 4 bytes align */
1302 if ((size & 3) || !size || (index & 3) || !data)
1305 if ((u32)index + (u32)size > 0xffff)
1308 byteen_start = byteen & BYTE_EN_START_MASK;
1309 byteen_end = byteen & BYTE_EN_END_MASK;
1311 byen = byteen_start | (byteen_start << 4);
1312 ret = set_registers(tp, index, type | byen, 4, data);
1325 ret = set_registers(tp, index,
1326 type | BYTE_EN_DWORD,
1335 ret = set_registers(tp, index,
1336 type | BYTE_EN_DWORD,
1348 byen = byteen_end | (byteen_end >> 4);
1349 ret = set_registers(tp, index, type | byen, 4, data);
1362 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1364 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1368 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1370 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1374 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1376 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1379 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1383 generic_ocp_read(tp, index, sizeof(data), &data, type);
1385 return __le32_to_cpu(data);
1388 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1390 __le32 tmp = __cpu_to_le32(data);
1392 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
1395 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1399 u16 byen = BYTE_EN_WORD;
1400 u8 shift = index & 2;
1405 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
1407 data = __le32_to_cpu(tmp);
1408 data >>= (shift * 8);
1414 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1418 u16 byen = BYTE_EN_WORD;
1419 u8 shift = index & 2;
1425 mask <<= (shift * 8);
1426 data <<= (shift * 8);
1430 tmp = __cpu_to_le32(data);
1432 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1435 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1439 u8 shift = index & 3;
1443 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
1445 data = __le32_to_cpu(tmp);
1446 data >>= (shift * 8);
1452 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1456 u16 byen = BYTE_EN_BYTE;
1457 u8 shift = index & 3;
1463 mask <<= (shift * 8);
1464 data <<= (shift * 8);
1468 tmp = __cpu_to_le32(data);
1470 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1473 static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1475 u16 ocp_base, ocp_index;
1477 ocp_base = addr & 0xf000;
1478 if (ocp_base != tp->ocp_base) {
1479 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1480 tp->ocp_base = ocp_base;
1483 ocp_index = (addr & 0x0fff) | 0xb000;
1484 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1487 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1489 u16 ocp_base, ocp_index;
1491 ocp_base = addr & 0xf000;
1492 if (ocp_base != tp->ocp_base) {
1493 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1494 tp->ocp_base = ocp_base;
1497 ocp_index = (addr & 0x0fff) | 0xb000;
1498 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1501 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
1503 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
1506 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
1508 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
1511 static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1513 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1514 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1517 static u16 sram_read(struct r8152 *tp, u16 addr)
1519 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1520 return ocp_reg_read(tp, OCP_SRAM_DATA);
1523 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1525 struct r8152 *tp = netdev_priv(netdev);
1528 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1531 if (phy_id != R8152_PHY_ID)
1534 ret = r8152_mdio_read(tp, reg);
1540 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1542 struct r8152 *tp = netdev_priv(netdev);
1544 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1547 if (phy_id != R8152_PHY_ID)
1550 r8152_mdio_write(tp, reg, val);
1554 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
1557 rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
1560 static int __rtl8152_set_mac_address(struct net_device *netdev, void *p,
1563 struct r8152 *tp = netdev_priv(netdev);
1564 struct sockaddr *addr = p;
1565 int ret = -EADDRNOTAVAIL;
1567 if (!is_valid_ether_addr(addr->sa_data))
1571 ret = usb_autopm_get_interface(tp->intf);
1576 mutex_lock(&tp->control);
1578 eth_hw_addr_set(netdev, addr->sa_data);
1580 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1581 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1582 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1584 mutex_unlock(&tp->control);
1587 usb_autopm_put_interface(tp->intf);
1592 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1594 return __rtl8152_set_mac_address(netdev, p, false);
1597 /* Devices containing proper chips can support a persistent
1598 * host system provided MAC address.
1599 * Examples of this are Dell TB15 and Dell WD15 docks
1601 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1604 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1605 union acpi_object *obj;
1608 unsigned char buf[6];
1610 acpi_object_type mac_obj_type;
1613 if (tp->lenovo_macpassthru) {
1614 mac_obj_name = "\\MACA";
1615 mac_obj_type = ACPI_TYPE_STRING;
1618 /* test for -AD variant of RTL8153 */
1619 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1620 if ((ocp_data & AD_MASK) == 0x1000) {
1621 /* test for MAC address pass-through bit */
1622 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1623 if ((ocp_data & PASS_THRU_MASK) != 1) {
1624 netif_dbg(tp, probe, tp->netdev,
1625 "No efuse for RTL8153-AD MAC pass through\n");
1629 /* test for RTL8153-BND and RTL8153-BD */
1630 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1631 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1632 netif_dbg(tp, probe, tp->netdev,
1633 "Invalid variant for MAC pass through\n");
1638 mac_obj_name = "\\_SB.AMAC";
1639 mac_obj_type = ACPI_TYPE_BUFFER;
1643 /* returns _AUXMAC_#AABBCCDDEEFF# */
1644 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
1645 obj = (union acpi_object *)buffer.pointer;
1646 if (!ACPI_SUCCESS(status))
1648 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
1649 netif_warn(tp, probe, tp->netdev,
1650 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
1651 obj->type, obj->string.length);
1655 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1656 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1657 netif_warn(tp, probe, tp->netdev,
1658 "Invalid header when reading pass-thru MAC addr\n");
1661 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1662 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1663 netif_warn(tp, probe, tp->netdev,
1664 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1669 memcpy(sa->sa_data, buf, 6);
1670 netif_info(tp, probe, tp->netdev,
1671 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1678 static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1680 struct net_device *dev = tp->netdev;
1683 sa->sa_family = dev->type;
1685 ret = eth_platform_get_mac_address(&tp->udev->dev, sa->sa_data);
1687 if (tp->version == RTL_VER_01) {
1688 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1690 /* if device doesn't support MAC pass through this will
1691 * be expected to be non-zero
1693 ret = vendor_mac_passthru_addr_read(tp, sa);
1695 ret = pla_ocp_read(tp, PLA_BACKUP, 8,
1701 netif_err(tp, probe, dev, "Get ether addr fail\n");
1702 } else if (!is_valid_ether_addr(sa->sa_data)) {
1703 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1705 eth_hw_addr_random(dev);
1706 ether_addr_copy(sa->sa_data, dev->dev_addr);
1707 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1715 static int set_ethernet_addr(struct r8152 *tp, bool in_resume)
1717 struct net_device *dev = tp->netdev;
1721 ret = determine_ethernet_addr(tp, &sa);
1725 if (tp->version == RTL_VER_01)
1726 eth_hw_addr_set(dev, sa.sa_data);
1728 ret = __rtl8152_set_mac_address(dev, &sa, in_resume);
1733 static void read_bulk_callback(struct urb *urb)
1735 struct net_device *netdev;
1736 int status = urb->status;
1739 unsigned long flags;
1749 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1752 if (!test_bit(WORK_ENABLE, &tp->flags))
1755 netdev = tp->netdev;
1757 /* When link down, the driver would cancel all bulks. */
1758 /* This avoid the re-submitting bulk */
1759 if (!netif_carrier_ok(netdev))
1762 usb_mark_last_busy(tp->udev);
1766 if (urb->actual_length < ETH_ZLEN)
1769 spin_lock_irqsave(&tp->rx_lock, flags);
1770 list_add_tail(&agg->list, &tp->rx_done);
1771 spin_unlock_irqrestore(&tp->rx_lock, flags);
1772 napi_schedule(&tp->napi);
1776 netif_device_detach(tp->netdev);
1779 urb->actual_length = 0;
1780 spin_lock_irqsave(&tp->rx_lock, flags);
1781 list_add_tail(&agg->list, &tp->rx_done);
1782 spin_unlock_irqrestore(&tp->rx_lock, flags);
1783 set_bit(RX_EPROTO, &tp->flags);
1784 schedule_delayed_work(&tp->schedule, 1);
1787 return; /* the urb is in unlink state */
1789 if (net_ratelimit())
1790 netdev_warn(netdev, "maybe reset is needed?\n");
1793 if (net_ratelimit())
1794 netdev_warn(netdev, "Rx status %d\n", status);
1798 r8152_submit_rx(tp, agg, GFP_ATOMIC);
1801 static void write_bulk_callback(struct urb *urb)
1803 struct net_device_stats *stats;
1804 struct net_device *netdev;
1807 unsigned long flags;
1808 int status = urb->status;
1818 netdev = tp->netdev;
1819 stats = &netdev->stats;
1821 if (net_ratelimit())
1822 netdev_warn(netdev, "Tx status %d\n", status);
1823 stats->tx_errors += agg->skb_num;
1825 stats->tx_packets += agg->skb_num;
1826 stats->tx_bytes += agg->skb_len;
1829 spin_lock_irqsave(&tp->tx_lock, flags);
1830 list_add_tail(&agg->list, &tp->tx_free);
1831 spin_unlock_irqrestore(&tp->tx_lock, flags);
1833 usb_autopm_put_interface_async(tp->intf);
1835 if (!netif_carrier_ok(netdev))
1838 if (!test_bit(WORK_ENABLE, &tp->flags))
1841 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1844 if (!skb_queue_empty(&tp->tx_queue))
1845 tasklet_schedule(&tp->tx_tl);
1848 static void intr_callback(struct urb *urb)
1852 int status = urb->status;
1859 if (!test_bit(WORK_ENABLE, &tp->flags))
1862 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1866 case 0: /* success */
1868 case -ECONNRESET: /* unlink */
1870 netif_device_detach(tp->netdev);
1874 netif_info(tp, intr, tp->netdev,
1875 "Stop submitting intr, status %d\n", status);
1878 if (net_ratelimit())
1879 netif_info(tp, intr, tp->netdev,
1880 "intr status -EOVERFLOW\n");
1882 /* -EPIPE: should clear the halt */
1884 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1888 d = urb->transfer_buffer;
1889 if (INTR_LINK & __le16_to_cpu(d[0])) {
1890 if (!netif_carrier_ok(tp->netdev)) {
1891 set_bit(RTL8152_LINK_CHG, &tp->flags);
1892 schedule_delayed_work(&tp->schedule, 0);
1895 if (netif_carrier_ok(tp->netdev)) {
1896 netif_stop_queue(tp->netdev);
1897 set_bit(RTL8152_LINK_CHG, &tp->flags);
1898 schedule_delayed_work(&tp->schedule, 0);
1903 res = usb_submit_urb(urb, GFP_ATOMIC);
1904 if (res == -ENODEV) {
1906 netif_device_detach(tp->netdev);
1908 netif_err(tp, intr, tp->netdev,
1909 "can't resubmit intr, status %d\n", res);
1913 static inline void *rx_agg_align(void *data)
1915 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
1918 static inline void *tx_agg_align(void *data)
1920 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
1923 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1925 list_del(&agg->info_list);
1927 usb_free_urb(agg->urb);
1928 put_page(agg->page);
1931 atomic_dec(&tp->rx_count);
1934 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1936 struct net_device *netdev = tp->netdev;
1937 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1938 unsigned int order = get_order(tp->rx_buf_sz);
1939 struct rx_agg *rx_agg;
1940 unsigned long flags;
1942 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1946 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
1950 rx_agg->buffer = page_address(rx_agg->page);
1952 rx_agg->urb = usb_alloc_urb(0, mflags);
1956 rx_agg->context = tp;
1958 INIT_LIST_HEAD(&rx_agg->list);
1959 INIT_LIST_HEAD(&rx_agg->info_list);
1960 spin_lock_irqsave(&tp->rx_lock, flags);
1961 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1962 spin_unlock_irqrestore(&tp->rx_lock, flags);
1964 atomic_inc(&tp->rx_count);
1969 __free_pages(rx_agg->page, order);
1975 static void free_all_mem(struct r8152 *tp)
1977 struct rx_agg *agg, *agg_next;
1978 unsigned long flags;
1981 spin_lock_irqsave(&tp->rx_lock, flags);
1983 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1984 free_rx_agg(tp, agg);
1986 spin_unlock_irqrestore(&tp->rx_lock, flags);
1988 WARN_ON(atomic_read(&tp->rx_count));
1990 for (i = 0; i < RTL8152_MAX_TX; i++) {
1991 usb_free_urb(tp->tx_info[i].urb);
1992 tp->tx_info[i].urb = NULL;
1994 kfree(tp->tx_info[i].buffer);
1995 tp->tx_info[i].buffer = NULL;
1996 tp->tx_info[i].head = NULL;
1999 usb_free_urb(tp->intr_urb);
2000 tp->intr_urb = NULL;
2002 kfree(tp->intr_buff);
2003 tp->intr_buff = NULL;
2006 static int alloc_all_mem(struct r8152 *tp)
2008 struct net_device *netdev = tp->netdev;
2009 struct usb_interface *intf = tp->intf;
2010 struct usb_host_interface *alt = intf->cur_altsetting;
2011 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
2014 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
2016 spin_lock_init(&tp->rx_lock);
2017 spin_lock_init(&tp->tx_lock);
2018 INIT_LIST_HEAD(&tp->rx_info);
2019 INIT_LIST_HEAD(&tp->tx_free);
2020 INIT_LIST_HEAD(&tp->rx_done);
2021 skb_queue_head_init(&tp->tx_queue);
2022 skb_queue_head_init(&tp->rx_queue);
2023 atomic_set(&tp->rx_count, 0);
2025 for (i = 0; i < RTL8152_MAX_RX; i++) {
2026 if (!alloc_rx_agg(tp, GFP_KERNEL))
2030 for (i = 0; i < RTL8152_MAX_TX; i++) {
2034 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
2038 if (buf != tx_agg_align(buf)) {
2040 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
2046 urb = usb_alloc_urb(0, GFP_KERNEL);
2052 INIT_LIST_HEAD(&tp->tx_info[i].list);
2053 tp->tx_info[i].context = tp;
2054 tp->tx_info[i].urb = urb;
2055 tp->tx_info[i].buffer = buf;
2056 tp->tx_info[i].head = tx_agg_align(buf);
2058 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
2061 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2065 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
2069 tp->intr_interval = (int)ep_intr->desc.bInterval;
2070 usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr,
2071 tp->intr_buff, INTBUFSIZE, intr_callback,
2072 tp, tp->intr_interval);
2081 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
2083 struct tx_agg *agg = NULL;
2084 unsigned long flags;
2086 if (list_empty(&tp->tx_free))
2089 spin_lock_irqsave(&tp->tx_lock, flags);
2090 if (!list_empty(&tp->tx_free)) {
2091 struct list_head *cursor;
2093 cursor = tp->tx_free.next;
2094 list_del_init(cursor);
2095 agg = list_entry(cursor, struct tx_agg, list);
2097 spin_unlock_irqrestore(&tp->tx_lock, flags);
2102 /* r8152_csum_workaround()
2103 * The hw limits the value of the transport offset. When the offset is out of
2104 * range, calculate the checksum by sw.
2106 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
2107 struct sk_buff_head *list)
2109 if (skb_shinfo(skb)->gso_size) {
2110 netdev_features_t features = tp->netdev->features;
2111 struct sk_buff *segs, *seg, *next;
2112 struct sk_buff_head seg_list;
2114 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
2115 segs = skb_gso_segment(skb, features);
2116 if (IS_ERR(segs) || !segs)
2119 __skb_queue_head_init(&seg_list);
2121 skb_list_walk_safe(segs, seg, next) {
2122 skb_mark_not_on_list(seg);
2123 __skb_queue_tail(&seg_list, seg);
2126 skb_queue_splice(&seg_list, list);
2128 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2129 if (skb_checksum_help(skb) < 0)
2132 __skb_queue_head(list, skb);
2134 struct net_device_stats *stats;
2137 stats = &tp->netdev->stats;
2138 stats->tx_dropped++;
2143 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
2145 if (skb_vlan_tag_present(skb)) {
2148 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
2149 desc->opts2 |= cpu_to_le32(opts2);
2153 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
2155 u32 opts2 = le32_to_cpu(desc->opts2);
2157 if (opts2 & RX_VLAN_TAG)
2158 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2159 swab16(opts2 & 0xffff));
2162 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
2163 struct sk_buff *skb, u32 len)
2165 u32 mss = skb_shinfo(skb)->gso_size;
2166 u32 opts1, opts2 = 0;
2167 int ret = TX_CSUM_SUCCESS;
2169 WARN_ON_ONCE(len > TX_LEN_MAX);
2171 opts1 = len | TX_FS | TX_LS;
2174 u32 transport_offset = (u32)skb_transport_offset(skb);
2176 if (transport_offset > GTTCPHO_MAX) {
2177 netif_warn(tp, tx_err, tp->netdev,
2178 "Invalid transport offset 0x%x for TSO\n",
2184 switch (vlan_get_protocol(skb)) {
2185 case htons(ETH_P_IP):
2189 case htons(ETH_P_IPV6):
2190 if (skb_cow_head(skb, 0)) {
2194 tcp_v6_gso_csum_prep(skb);
2203 opts1 |= transport_offset << GTTCPHO_SHIFT;
2204 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
2205 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2206 u32 transport_offset = (u32)skb_transport_offset(skb);
2209 if (transport_offset > TCPHO_MAX) {
2210 netif_warn(tp, tx_err, tp->netdev,
2211 "Invalid transport offset 0x%x\n",
2217 switch (vlan_get_protocol(skb)) {
2218 case htons(ETH_P_IP):
2220 ip_protocol = ip_hdr(skb)->protocol;
2223 case htons(ETH_P_IPV6):
2225 ip_protocol = ipv6_hdr(skb)->nexthdr;
2229 ip_protocol = IPPROTO_RAW;
2233 if (ip_protocol == IPPROTO_TCP)
2235 else if (ip_protocol == IPPROTO_UDP)
2240 opts2 |= transport_offset << TCPHO_SHIFT;
2243 desc->opts2 = cpu_to_le32(opts2);
2244 desc->opts1 = cpu_to_le32(opts1);
2250 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
2252 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2256 __skb_queue_head_init(&skb_head);
2257 spin_lock(&tx_queue->lock);
2258 skb_queue_splice_init(tx_queue, &skb_head);
2259 spin_unlock(&tx_queue->lock);
2261 tx_data = agg->head;
2264 remain = agg_buf_sz;
2266 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
2267 struct tx_desc *tx_desc;
2268 struct sk_buff *skb;
2271 skb = __skb_dequeue(&skb_head);
2275 len = skb->len + sizeof(*tx_desc);
2278 __skb_queue_head(&skb_head, skb);
2282 tx_data = tx_agg_align(tx_data);
2283 tx_desc = (struct tx_desc *)tx_data;
2285 if (r8152_tx_csum(tp, tx_desc, skb, skb->len)) {
2286 r8152_csum_workaround(tp, skb, &skb_head);
2290 rtl_tx_vlan_tag(tx_desc, skb);
2292 tx_data += sizeof(*tx_desc);
2295 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
2296 struct net_device_stats *stats = &tp->netdev->stats;
2298 stats->tx_dropped++;
2299 dev_kfree_skb_any(skb);
2300 tx_data -= sizeof(*tx_desc);
2305 agg->skb_len += len;
2306 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
2308 dev_kfree_skb_any(skb);
2310 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
2312 if (tp->dell_tb_rx_agg_bug)
2316 if (!skb_queue_empty(&skb_head)) {
2317 spin_lock(&tx_queue->lock);
2318 skb_queue_splice(&skb_head, tx_queue);
2319 spin_unlock(&tx_queue->lock);
2322 netif_tx_lock(tp->netdev);
2324 if (netif_queue_stopped(tp->netdev) &&
2325 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
2326 netif_wake_queue(tp->netdev);
2328 netif_tx_unlock(tp->netdev);
2330 ret = usb_autopm_get_interface_async(tp->intf);
2334 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out,
2335 agg->head, (int)(tx_data - (u8 *)agg->head),
2336 (usb_complete_t)write_bulk_callback, agg);
2338 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
2340 usb_autopm_put_interface_async(tp->intf);
2346 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
2348 u8 checksum = CHECKSUM_NONE;
2351 if (!(tp->netdev->features & NETIF_F_RXCSUM))
2354 opts2 = le32_to_cpu(rx_desc->opts2);
2355 opts3 = le32_to_cpu(rx_desc->opts3);
2357 if (opts2 & RD_IPV4_CS) {
2359 checksum = CHECKSUM_NONE;
2360 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2361 checksum = CHECKSUM_UNNECESSARY;
2362 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2363 checksum = CHECKSUM_UNNECESSARY;
2364 } else if (opts2 & RD_IPV6_CS) {
2365 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2366 checksum = CHECKSUM_UNNECESSARY;
2367 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2368 checksum = CHECKSUM_UNNECESSARY;
2375 static inline bool rx_count_exceed(struct r8152 *tp)
2377 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2380 static inline int agg_offset(struct rx_agg *agg, void *addr)
2382 return (int)(addr - agg->buffer);
2385 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2387 struct rx_agg *agg, *agg_next, *agg_free = NULL;
2388 unsigned long flags;
2390 spin_lock_irqsave(&tp->rx_lock, flags);
2392 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2393 if (page_count(agg->page) == 1) {
2395 list_del_init(&agg->list);
2399 if (rx_count_exceed(tp)) {
2400 list_del_init(&agg->list);
2401 free_rx_agg(tp, agg);
2407 spin_unlock_irqrestore(&tp->rx_lock, flags);
2409 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
2410 agg_free = alloc_rx_agg(tp, mflags);
2415 static int rx_bottom(struct r8152 *tp, int budget)
2417 unsigned long flags;
2418 struct list_head *cursor, *next, rx_queue;
2419 int ret = 0, work_done = 0;
2420 struct napi_struct *napi = &tp->napi;
2422 if (!skb_queue_empty(&tp->rx_queue)) {
2423 while (work_done < budget) {
2424 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2425 struct net_device *netdev = tp->netdev;
2426 struct net_device_stats *stats = &netdev->stats;
2427 unsigned int pkt_len;
2433 napi_gro_receive(napi, skb);
2435 stats->rx_packets++;
2436 stats->rx_bytes += pkt_len;
2440 if (list_empty(&tp->rx_done))
2443 clear_bit(RX_EPROTO, &tp->flags);
2444 INIT_LIST_HEAD(&rx_queue);
2445 spin_lock_irqsave(&tp->rx_lock, flags);
2446 list_splice_init(&tp->rx_done, &rx_queue);
2447 spin_unlock_irqrestore(&tp->rx_lock, flags);
2449 list_for_each_safe(cursor, next, &rx_queue) {
2450 struct rx_desc *rx_desc;
2451 struct rx_agg *agg, *agg_free;
2456 list_del_init(cursor);
2458 agg = list_entry(cursor, struct rx_agg, list);
2460 if (urb->status != 0 || urb->actual_length < ETH_ZLEN)
2463 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2465 rx_desc = agg->buffer;
2466 rx_data = agg->buffer;
2467 len_used += sizeof(struct rx_desc);
2469 while (urb->actual_length > len_used) {
2470 struct net_device *netdev = tp->netdev;
2471 struct net_device_stats *stats = &netdev->stats;
2472 unsigned int pkt_len, rx_frag_head_sz;
2473 struct sk_buff *skb;
2475 /* limit the skb numbers for rx_queue */
2476 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2479 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
2480 if (pkt_len < ETH_ZLEN)
2483 len_used += pkt_len;
2484 if (urb->actual_length < len_used)
2487 pkt_len -= ETH_FCS_LEN;
2488 rx_data += sizeof(struct rx_desc);
2490 if (!agg_free || tp->rx_copybreak > pkt_len)
2491 rx_frag_head_sz = pkt_len;
2493 rx_frag_head_sz = tp->rx_copybreak;
2495 skb = napi_alloc_skb(napi, rx_frag_head_sz);
2497 stats->rx_dropped++;
2501 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
2502 memcpy(skb->data, rx_data, rx_frag_head_sz);
2503 skb_put(skb, rx_frag_head_sz);
2504 pkt_len -= rx_frag_head_sz;
2505 rx_data += rx_frag_head_sz;
2507 skb_add_rx_frag(skb, 0, agg->page,
2508 agg_offset(agg, rx_data),
2510 SKB_DATA_ALIGN(pkt_len));
2511 get_page(agg->page);
2514 skb->protocol = eth_type_trans(skb, netdev);
2515 rtl_rx_vlan_tag(rx_desc, skb);
2516 if (work_done < budget) {
2518 stats->rx_packets++;
2519 stats->rx_bytes += skb->len;
2520 napi_gro_receive(napi, skb);
2522 __skb_queue_tail(&tp->rx_queue, skb);
2526 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
2527 rx_desc = (struct rx_desc *)rx_data;
2528 len_used = agg_offset(agg, rx_data);
2529 len_used += sizeof(struct rx_desc);
2532 WARN_ON(!agg_free && page_count(agg->page) > 1);
2535 spin_lock_irqsave(&tp->rx_lock, flags);
2536 if (page_count(agg->page) == 1) {
2537 list_add(&agg_free->list, &tp->rx_used);
2539 list_add_tail(&agg->list, &tp->rx_used);
2543 spin_unlock_irqrestore(&tp->rx_lock, flags);
2548 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2550 urb->actual_length = 0;
2551 list_add_tail(&agg->list, next);
2555 if (!list_empty(&rx_queue)) {
2556 spin_lock_irqsave(&tp->rx_lock, flags);
2557 list_splice_tail(&rx_queue, &tp->rx_done);
2558 spin_unlock_irqrestore(&tp->rx_lock, flags);
2565 static void tx_bottom(struct r8152 *tp)
2570 struct net_device *netdev = tp->netdev;
2573 if (skb_queue_empty(&tp->tx_queue))
2576 agg = r8152_get_tx_agg(tp);
2580 res = r8152_tx_agg_fill(tp, agg);
2584 if (res == -ENODEV) {
2586 netif_device_detach(netdev);
2588 struct net_device_stats *stats = &netdev->stats;
2589 unsigned long flags;
2591 netif_warn(tp, tx_err, netdev,
2592 "failed tx_urb %d\n", res);
2593 stats->tx_dropped += agg->skb_num;
2595 spin_lock_irqsave(&tp->tx_lock, flags);
2596 list_add_tail(&agg->list, &tp->tx_free);
2597 spin_unlock_irqrestore(&tp->tx_lock, flags);
2602 static void bottom_half(struct tasklet_struct *t)
2604 struct r8152 *tp = from_tasklet(tp, t, tx_tl);
2606 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2609 if (!test_bit(WORK_ENABLE, &tp->flags))
2612 /* When link down, the driver would cancel all bulks. */
2613 /* This avoid the re-submitting bulk */
2614 if (!netif_carrier_ok(tp->netdev))
2617 clear_bit(SCHEDULE_TASKLET, &tp->flags);
2622 static int r8152_poll(struct napi_struct *napi, int budget)
2624 struct r8152 *tp = container_of(napi, struct r8152, napi);
2627 work_done = rx_bottom(tp, budget);
2629 if (work_done < budget) {
2630 if (!napi_complete_done(napi, work_done))
2632 if (!list_empty(&tp->rx_done))
2633 napi_schedule(napi);
2641 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2645 /* The rx would be stopped, so skip submitting */
2646 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2647 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2650 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in,
2651 agg->buffer, tp->rx_buf_sz,
2652 (usb_complete_t)read_bulk_callback, agg);
2654 ret = usb_submit_urb(agg->urb, mem_flags);
2655 if (ret == -ENODEV) {
2657 netif_device_detach(tp->netdev);
2659 struct urb *urb = agg->urb;
2660 unsigned long flags;
2662 urb->actual_length = 0;
2663 spin_lock_irqsave(&tp->rx_lock, flags);
2664 list_add_tail(&agg->list, &tp->rx_done);
2665 spin_unlock_irqrestore(&tp->rx_lock, flags);
2667 netif_err(tp, rx_err, tp->netdev,
2668 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2670 napi_schedule(&tp->napi);
2676 static void rtl_drop_queued_tx(struct r8152 *tp)
2678 struct net_device_stats *stats = &tp->netdev->stats;
2679 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2680 struct sk_buff *skb;
2682 if (skb_queue_empty(tx_queue))
2685 __skb_queue_head_init(&skb_head);
2686 spin_lock_bh(&tx_queue->lock);
2687 skb_queue_splice_init(tx_queue, &skb_head);
2688 spin_unlock_bh(&tx_queue->lock);
2690 while ((skb = __skb_dequeue(&skb_head))) {
2692 stats->tx_dropped++;
2696 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2698 struct r8152 *tp = netdev_priv(netdev);
2700 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
2702 usb_queue_reset_device(tp->intf);
2705 static void rtl8152_set_rx_mode(struct net_device *netdev)
2707 struct r8152 *tp = netdev_priv(netdev);
2709 if (netif_carrier_ok(netdev)) {
2710 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2711 schedule_delayed_work(&tp->schedule, 0);
2715 static void _rtl8152_set_rx_mode(struct net_device *netdev)
2717 struct r8152 *tp = netdev_priv(netdev);
2718 u32 mc_filter[2]; /* Multicast hash filter */
2722 netif_stop_queue(netdev);
2723 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2724 ocp_data &= ~RCR_ACPT_ALL;
2725 ocp_data |= RCR_AB | RCR_APM;
2727 if (netdev->flags & IFF_PROMISC) {
2728 /* Unconditionally log net taps. */
2729 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2730 ocp_data |= RCR_AM | RCR_AAP;
2731 mc_filter[1] = 0xffffffff;
2732 mc_filter[0] = 0xffffffff;
2733 } else if ((netdev->flags & IFF_MULTICAST &&
2734 netdev_mc_count(netdev) > multicast_filter_limit) ||
2735 (netdev->flags & IFF_ALLMULTI)) {
2736 /* Too many to filter perfectly -- accept all multicasts. */
2738 mc_filter[1] = 0xffffffff;
2739 mc_filter[0] = 0xffffffff;
2744 if (netdev->flags & IFF_MULTICAST) {
2745 struct netdev_hw_addr *ha;
2747 netdev_for_each_mc_addr(ha, netdev) {
2748 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2750 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2756 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2757 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
2759 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
2760 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2761 netif_wake_queue(netdev);
2764 static netdev_features_t
2765 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2766 netdev_features_t features)
2768 u32 mss = skb_shinfo(skb)->gso_size;
2769 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2771 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) &&
2772 skb_transport_offset(skb) > max_offset)
2773 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2774 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2775 features &= ~NETIF_F_GSO_MASK;
2780 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
2781 struct net_device *netdev)
2783 struct r8152 *tp = netdev_priv(netdev);
2785 skb_tx_timestamp(skb);
2787 skb_queue_tail(&tp->tx_queue, skb);
2789 if (!list_empty(&tp->tx_free)) {
2790 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2791 set_bit(SCHEDULE_TASKLET, &tp->flags);
2792 schedule_delayed_work(&tp->schedule, 0);
2794 usb_mark_last_busy(tp->udev);
2795 tasklet_schedule(&tp->tx_tl);
2797 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
2798 netif_stop_queue(netdev);
2801 return NETDEV_TX_OK;
2804 static void r8152b_reset_packet_filter(struct r8152 *tp)
2808 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2809 ocp_data &= ~FMC_FCR_MCU_EN;
2810 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2811 ocp_data |= FMC_FCR_MCU_EN;
2812 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2815 static void rtl8152_nic_reset(struct r8152 *tp)
2820 switch (tp->version) {
2824 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2826 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2828 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2829 ocp_data &= ~BMU_RESET_EP_IN;
2830 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2832 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2833 ocp_data |= CDC_ECM_EN;
2834 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2836 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2838 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2840 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2841 ocp_data |= BMU_RESET_EP_IN;
2842 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2844 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2845 ocp_data &= ~CDC_ECM_EN;
2846 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2850 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2852 for (i = 0; i < 1000; i++) {
2853 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2855 usleep_range(100, 400);
2861 static void set_tx_qlen(struct r8152 *tp)
2863 tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc));
2866 static inline u16 rtl8152_get_speed(struct r8152 *tp)
2868 return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2871 static void rtl_eee_plus_en(struct r8152 *tp, bool enable)
2875 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2877 ocp_data |= EEEP_CR_EEEP_TX;
2879 ocp_data &= ~EEEP_CR_EEEP_TX;
2880 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2883 static void rtl_set_eee_plus(struct r8152 *tp)
2885 if (rtl8152_get_speed(tp) & _10bps)
2886 rtl_eee_plus_en(tp, true);
2888 rtl_eee_plus_en(tp, false);
2891 static void rxdy_gated_en(struct r8152 *tp, bool enable)
2895 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2897 ocp_data |= RXDY_GATED_EN;
2899 ocp_data &= ~RXDY_GATED_EN;
2900 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2903 static int rtl_start_rx(struct r8152 *tp)
2905 struct rx_agg *agg, *agg_next;
2906 struct list_head tmp_list;
2907 unsigned long flags;
2910 INIT_LIST_HEAD(&tmp_list);
2912 spin_lock_irqsave(&tp->rx_lock, flags);
2914 INIT_LIST_HEAD(&tp->rx_done);
2915 INIT_LIST_HEAD(&tp->rx_used);
2917 list_splice_init(&tp->rx_info, &tmp_list);
2919 spin_unlock_irqrestore(&tp->rx_lock, flags);
2921 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2922 INIT_LIST_HEAD(&agg->list);
2924 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2925 if (++i > RTL8152_MAX_RX) {
2926 spin_lock_irqsave(&tp->rx_lock, flags);
2927 list_add_tail(&agg->list, &tp->rx_used);
2928 spin_unlock_irqrestore(&tp->rx_lock, flags);
2929 } else if (unlikely(ret < 0)) {
2930 spin_lock_irqsave(&tp->rx_lock, flags);
2931 list_add_tail(&agg->list, &tp->rx_done);
2932 spin_unlock_irqrestore(&tp->rx_lock, flags);
2934 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
2938 spin_lock_irqsave(&tp->rx_lock, flags);
2939 WARN_ON(!list_empty(&tp->rx_info));
2940 list_splice(&tmp_list, &tp->rx_info);
2941 spin_unlock_irqrestore(&tp->rx_lock, flags);
2946 static int rtl_stop_rx(struct r8152 *tp)
2948 struct rx_agg *agg, *agg_next;
2949 struct list_head tmp_list;
2950 unsigned long flags;
2952 INIT_LIST_HEAD(&tmp_list);
2954 /* The usb_kill_urb() couldn't be used in atomic.
2955 * Therefore, move the list of rx_info to a tmp one.
2956 * Then, list_for_each_entry_safe could be used without
2960 spin_lock_irqsave(&tp->rx_lock, flags);
2961 list_splice_init(&tp->rx_info, &tmp_list);
2962 spin_unlock_irqrestore(&tp->rx_lock, flags);
2964 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2965 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2966 * equal to 1, so the other ones could be freed safely.
2968 if (page_count(agg->page) > 1)
2969 free_rx_agg(tp, agg);
2971 usb_kill_urb(agg->urb);
2974 /* Move back the list of temp to the rx_info */
2975 spin_lock_irqsave(&tp->rx_lock, flags);
2976 WARN_ON(!list_empty(&tp->rx_info));
2977 list_splice(&tmp_list, &tp->rx_info);
2978 spin_unlock_irqrestore(&tp->rx_lock, flags);
2980 while (!skb_queue_empty(&tp->rx_queue))
2981 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2986 static void rtl_set_ifg(struct r8152 *tp, u16 speed)
2990 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2991 ocp_data &= ~IFG_MASK;
2992 if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) {
2993 ocp_data |= IFG_144NS;
2994 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
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);
3000 ocp_data |= IFG_96NS;
3001 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
3003 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
3004 ocp_data |= TX10MIDLE_EN;
3005 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
3009 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
3011 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
3012 OWN_UPDATE | OWN_CLEAR);
3015 static int rtl_enable(struct r8152 *tp)
3019 r8152b_reset_packet_filter(tp);
3021 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
3022 ocp_data |= CR_RE | CR_TE;
3023 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
3025 switch (tp->version) {
3029 r8153b_rx_agg_chg_indicate(tp);
3035 rxdy_gated_en(tp, false);
3040 static int rtl8152_enable(struct r8152 *tp)
3042 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3046 rtl_set_eee_plus(tp);
3048 return rtl_enable(tp);
3051 static void r8153_set_rx_early_timeout(struct r8152 *tp)
3053 u32 ocp_data = tp->coalesce / 8;
3055 switch (tp->version) {
3060 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3067 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
3068 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
3070 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3072 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3081 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3083 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3085 r8153b_rx_agg_chg_indicate(tp);
3093 static void r8153_set_rx_early_size(struct r8152 *tp)
3095 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
3097 switch (tp->version) {
3102 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3108 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3117 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3119 r8153b_rx_agg_chg_indicate(tp);
3127 static int rtl8153_enable(struct r8152 *tp)
3131 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3135 rtl_set_eee_plus(tp);
3136 r8153_set_rx_early_timeout(tp);
3137 r8153_set_rx_early_size(tp);
3139 rtl_set_ifg(tp, rtl8152_get_speed(tp));
3141 switch (tp->version) {
3144 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
3145 ocp_data &= ~FC_PATCH_TASK;
3146 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3147 usleep_range(1000, 2000);
3148 ocp_data |= FC_PATCH_TASK;
3149 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3155 return rtl_enable(tp);
3158 static void rtl_disable(struct r8152 *tp)
3163 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3164 rtl_drop_queued_tx(tp);
3168 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3169 ocp_data &= ~RCR_ACPT_ALL;
3170 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3172 rtl_drop_queued_tx(tp);
3174 for (i = 0; i < RTL8152_MAX_TX; i++)
3175 usb_kill_urb(tp->tx_info[i].urb);
3177 rxdy_gated_en(tp, true);
3179 for (i = 0; i < 1000; i++) {
3180 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3181 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
3183 usleep_range(1000, 2000);
3186 for (i = 0; i < 1000; i++) {
3187 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
3189 usleep_range(1000, 2000);
3194 rtl8152_nic_reset(tp);
3197 static void r8152_power_cut_en(struct r8152 *tp, bool enable)
3201 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
3203 ocp_data |= POWER_CUT;
3205 ocp_data &= ~POWER_CUT;
3206 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
3208 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
3209 ocp_data &= ~RESUME_INDICATE;
3210 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
3213 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
3217 switch (tp->version) {
3228 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
3230 ocp_data |= CPCR_RX_VLAN;
3232 ocp_data &= ~CPCR_RX_VLAN;
3233 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
3243 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1);
3245 ocp_data |= OUTER_VLAN | INNER_VLAN;
3247 ocp_data &= ~(OUTER_VLAN | INNER_VLAN);
3248 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data);
3253 static int rtl8152_set_features(struct net_device *dev,
3254 netdev_features_t features)
3256 netdev_features_t changed = features ^ dev->features;
3257 struct r8152 *tp = netdev_priv(dev);
3260 ret = usb_autopm_get_interface(tp->intf);
3264 mutex_lock(&tp->control);
3266 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3267 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3268 rtl_rx_vlan_en(tp, true);
3270 rtl_rx_vlan_en(tp, false);
3273 mutex_unlock(&tp->control);
3275 usb_autopm_put_interface(tp->intf);
3281 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
3283 static u32 __rtl_get_wol(struct r8152 *tp)
3288 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3289 if (ocp_data & LINK_ON_WAKE_EN)
3290 wolopts |= WAKE_PHY;
3292 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3293 if (ocp_data & UWF_EN)
3294 wolopts |= WAKE_UCAST;
3295 if (ocp_data & BWF_EN)
3296 wolopts |= WAKE_BCAST;
3297 if (ocp_data & MWF_EN)
3298 wolopts |= WAKE_MCAST;
3300 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3301 if (ocp_data & MAGIC_EN)
3302 wolopts |= WAKE_MAGIC;
3307 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
3311 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3313 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3314 ocp_data &= ~LINK_ON_WAKE_EN;
3315 if (wolopts & WAKE_PHY)
3316 ocp_data |= LINK_ON_WAKE_EN;
3317 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3319 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3320 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
3321 if (wolopts & WAKE_UCAST)
3323 if (wolopts & WAKE_BCAST)
3325 if (wolopts & WAKE_MCAST)
3327 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3329 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3331 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3332 ocp_data &= ~MAGIC_EN;
3333 if (wolopts & WAKE_MAGIC)
3334 ocp_data |= MAGIC_EN;
3335 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3337 if (wolopts & WAKE_ANY)
3338 device_set_wakeup_enable(&tp->udev->dev, true);
3340 device_set_wakeup_enable(&tp->udev->dev, false);
3343 static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable)
3345 u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3347 /* MAC clock speed down */
3349 ocp_data |= MAC_CLK_SPDWN_EN;
3351 ocp_data &= ~MAC_CLK_SPDWN_EN;
3353 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3356 static void r8156_mac_clk_spd(struct r8152 *tp, bool enable)
3360 /* MAC clock speed down */
3362 /* aldps_spdwn_ratio, tp10_spdwn_ratio */
3363 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3366 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3367 ocp_data &= ~EEE_SPDWN_RATIO_MASK;
3368 ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */
3369 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3371 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3372 ocp_data &= ~MAC_CLK_SPDWN_EN;
3373 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3377 static void r8153_u1u2en(struct r8152 *tp, bool enable)
3382 memset(u1u2, 0xff, sizeof(u1u2));
3384 memset(u1u2, 0x00, sizeof(u1u2));
3386 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3389 static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3393 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3395 ocp_data |= LPM_U1U2_EN;
3397 ocp_data &= ~LPM_U1U2_EN;
3399 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3402 static void r8153_u2p3en(struct r8152 *tp, bool enable)
3406 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
3408 ocp_data |= U2P3_ENABLE;
3410 ocp_data &= ~U2P3_ENABLE;
3411 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3414 static void r8153b_ups_flags(struct r8152 *tp)
3418 if (tp->ups_info.green)
3419 ups_flags |= UPS_FLAGS_EN_GREEN;
3421 if (tp->ups_info.aldps)
3422 ups_flags |= UPS_FLAGS_EN_ALDPS;
3424 if (tp->ups_info.eee)
3425 ups_flags |= UPS_FLAGS_EN_EEE;
3427 if (tp->ups_info.flow_control)
3428 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3430 if (tp->ups_info.eee_ckdiv)
3431 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3433 if (tp->ups_info.eee_cmod_lv)
3434 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3436 if (tp->ups_info.r_tune)
3437 ups_flags |= UPS_FLAGS_R_TUNE;
3439 if (tp->ups_info._10m_ckdiv)
3440 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3442 if (tp->ups_info.eee_plloff_100)
3443 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3445 if (tp->ups_info.eee_plloff_giga)
3446 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3448 if (tp->ups_info._250m_ckdiv)
3449 ups_flags |= UPS_FLAGS_250M_CKDIV;
3451 if (tp->ups_info.ctap_short_off)
3452 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3454 switch (tp->ups_info.speed_duplex) {
3456 ups_flags |= ups_flags_speed(1);
3459 ups_flags |= ups_flags_speed(2);
3461 case NWAY_100M_HALF:
3462 ups_flags |= ups_flags_speed(3);
3464 case NWAY_100M_FULL:
3465 ups_flags |= ups_flags_speed(4);
3467 case NWAY_1000M_FULL:
3468 ups_flags |= ups_flags_speed(5);
3470 case FORCE_10M_HALF:
3471 ups_flags |= ups_flags_speed(6);
3473 case FORCE_10M_FULL:
3474 ups_flags |= ups_flags_speed(7);
3476 case FORCE_100M_HALF:
3477 ups_flags |= ups_flags_speed(8);
3479 case FORCE_100M_FULL:
3480 ups_flags |= ups_flags_speed(9);
3486 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3489 static void r8156_ups_flags(struct r8152 *tp)
3493 if (tp->ups_info.green)
3494 ups_flags |= UPS_FLAGS_EN_GREEN;
3496 if (tp->ups_info.aldps)
3497 ups_flags |= UPS_FLAGS_EN_ALDPS;
3499 if (tp->ups_info.eee)
3500 ups_flags |= UPS_FLAGS_EN_EEE;
3502 if (tp->ups_info.flow_control)
3503 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3505 if (tp->ups_info.eee_ckdiv)
3506 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3508 if (tp->ups_info._10m_ckdiv)
3509 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3511 if (tp->ups_info.eee_plloff_100)
3512 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3514 if (tp->ups_info.eee_plloff_giga)
3515 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3517 if (tp->ups_info._250m_ckdiv)
3518 ups_flags |= UPS_FLAGS_250M_CKDIV;
3520 switch (tp->ups_info.speed_duplex) {
3521 case FORCE_10M_HALF:
3522 ups_flags |= ups_flags_speed(0);
3524 case FORCE_10M_FULL:
3525 ups_flags |= ups_flags_speed(1);
3527 case FORCE_100M_HALF:
3528 ups_flags |= ups_flags_speed(2);
3530 case FORCE_100M_FULL:
3531 ups_flags |= ups_flags_speed(3);
3534 ups_flags |= ups_flags_speed(4);
3537 ups_flags |= ups_flags_speed(5);
3539 case NWAY_100M_HALF:
3540 ups_flags |= ups_flags_speed(6);
3542 case NWAY_100M_FULL:
3543 ups_flags |= ups_flags_speed(7);
3545 case NWAY_1000M_FULL:
3546 ups_flags |= ups_flags_speed(8);
3548 case NWAY_2500M_FULL:
3549 ups_flags |= ups_flags_speed(9);
3555 switch (tp->ups_info.lite_mode) {
3557 ups_flags |= 0 << 5;
3560 ups_flags |= 2 << 5;
3564 ups_flags |= 1 << 5;
3568 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3571 static void rtl_green_en(struct r8152 *tp, bool enable)
3575 data = sram_read(tp, SRAM_GREEN_CFG);
3577 data |= GREEN_ETH_EN;
3579 data &= ~GREEN_ETH_EN;
3580 sram_write(tp, SRAM_GREEN_CFG, data);
3582 tp->ups_info.green = enable;
3585 static void r8153b_green_en(struct r8152 *tp, bool enable)
3588 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
3589 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
3590 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
3592 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
3593 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
3594 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
3597 rtl_green_en(tp, true);
3600 static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3605 for (i = 0; i < 500; i++) {
3606 data = ocp_reg_read(tp, OCP_PHY_STATUS);
3607 data &= PHY_STAT_MASK;
3609 if (data == desired)
3611 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3612 data == PHY_STAT_EXT_INIT) {
3617 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3624 static void r8153b_ups_en(struct r8152 *tp, bool enable)
3626 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3629 r8153b_ups_flags(tp);
3631 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3632 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
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);
3638 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3639 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3641 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3642 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3643 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3645 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3648 for (i = 0; i < 500; i++) {
3649 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3655 tp->rtl_ops.hw_phy_cfg(tp);
3657 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3658 tp->duplex, tp->advertising);
3663 static void r8153c_ups_en(struct r8152 *tp, bool enable)
3665 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3668 r8153b_ups_flags(tp);
3670 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3671 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3673 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3674 ocp_data |= UPS_FORCE_PWR_DOWN;
3675 ocp_data &= ~BIT(7);
3676 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3678 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3679 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3681 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3682 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3683 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3685 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3688 for (i = 0; i < 500; i++) {
3689 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3695 tp->rtl_ops.hw_phy_cfg(tp);
3697 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3698 tp->duplex, tp->advertising);
3701 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3703 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3705 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3707 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3711 static void r8156_ups_en(struct r8152 *tp, bool enable)
3713 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3716 r8156_ups_flags(tp);
3718 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3719 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3721 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3722 ocp_data |= UPS_FORCE_PWR_DOWN;
3723 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3725 switch (tp->version) {
3728 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL);
3729 ocp_data &= ~OOBS_POLLING;
3730 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data);
3736 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3737 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3739 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3740 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3741 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3743 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3744 tp->rtl_ops.hw_phy_cfg(tp);
3746 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3747 tp->duplex, tp->advertising);
3752 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3756 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3758 ocp_data |= PWR_EN | PHASE2_EN;
3760 ocp_data &= ~(PWR_EN | PHASE2_EN);
3761 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3763 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3764 ocp_data &= ~PCUT_STATUS;
3765 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3768 static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3772 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3774 ocp_data |= PWR_EN | PHASE2_EN;
3776 ocp_data &= ~PWR_EN;
3777 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3779 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3780 ocp_data &= ~PCUT_STATUS;
3781 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3784 static void r8153_queue_wake(struct r8152 *tp, bool enable)
3788 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
3790 ocp_data |= UPCOMING_RUNTIME_D3;
3792 ocp_data &= ~UPCOMING_RUNTIME_D3;
3793 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
3795 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3796 ocp_data &= ~LINK_CHG_EVENT;
3797 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3799 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3800 ocp_data &= ~LINK_CHANGE_FLAG;
3801 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
3804 static bool rtl_can_wakeup(struct r8152 *tp)
3806 struct usb_device *udev = tp->udev;
3808 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3811 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3816 __rtl_set_wol(tp, WAKE_ANY);
3818 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3820 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3821 ocp_data |= LINK_OFF_WAKE_EN;
3822 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3824 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3828 __rtl_set_wol(tp, tp->saved_wolopts);
3830 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3832 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3833 ocp_data &= ~LINK_OFF_WAKE_EN;
3834 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3836 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3840 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3843 r8153_u1u2en(tp, false);
3844 r8153_u2p3en(tp, false);
3845 rtl_runtime_suspend_enable(tp, true);
3847 rtl_runtime_suspend_enable(tp, false);
3849 switch (tp->version) {
3856 r8153_u2p3en(tp, true);
3860 r8153_u1u2en(tp, true);
3864 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3867 r8153_queue_wake(tp, true);
3868 r8153b_u1u2en(tp, false);
3869 r8153_u2p3en(tp, false);
3870 rtl_runtime_suspend_enable(tp, true);
3871 r8153b_ups_en(tp, true);
3873 r8153b_ups_en(tp, false);
3874 r8153_queue_wake(tp, false);
3875 rtl_runtime_suspend_enable(tp, false);
3876 if (tp->udev->speed >= USB_SPEED_SUPER)
3877 r8153b_u1u2en(tp, true);
3881 static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable)
3884 r8153_queue_wake(tp, true);
3885 r8153b_u1u2en(tp, false);
3886 r8153_u2p3en(tp, false);
3887 rtl_runtime_suspend_enable(tp, true);
3888 r8153c_ups_en(tp, true);
3890 r8153c_ups_en(tp, false);
3891 r8153_queue_wake(tp, false);
3892 rtl_runtime_suspend_enable(tp, false);
3893 r8153b_u1u2en(tp, true);
3897 static void rtl8156_runtime_enable(struct r8152 *tp, bool enable)
3900 r8153_queue_wake(tp, true);
3901 r8153b_u1u2en(tp, false);
3902 r8153_u2p3en(tp, false);
3903 rtl_runtime_suspend_enable(tp, true);
3905 r8153_queue_wake(tp, false);
3906 rtl_runtime_suspend_enable(tp, false);
3907 r8153_u2p3en(tp, true);
3908 if (tp->udev->speed >= USB_SPEED_SUPER)
3909 r8153b_u1u2en(tp, true);
3913 static void r8153_teredo_off(struct r8152 *tp)
3917 switch (tp->version) {
3925 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3926 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3928 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3941 /* The bit 0 ~ 7 are relative with teredo settings. They are
3942 * W1C (write 1 to clear), so set all 1 to disable it.
3944 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3948 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3949 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3950 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3953 static void rtl_reset_bmu(struct r8152 *tp)
3957 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3958 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3959 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3960 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3961 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3964 /* Clear the bp to stop the firmware before loading a new one */
3965 static void rtl_clear_bp(struct r8152 *tp, u16 type)
3967 switch (tp->version) {
3976 ocp_write_byte(tp, type, PLA_BP_EN, 0);
3979 ocp_write_word(tp, type, USB_BP2_EN, 0);
3981 ocp_write_word(tp, type, USB_BP_8, 0);
3982 ocp_write_word(tp, type, USB_BP_9, 0);
3983 ocp_write_word(tp, type, USB_BP_10, 0);
3984 ocp_write_word(tp, type, USB_BP_11, 0);
3985 ocp_write_word(tp, type, USB_BP_12, 0);
3986 ocp_write_word(tp, type, USB_BP_13, 0);
3987 ocp_write_word(tp, type, USB_BP_14, 0);
3988 ocp_write_word(tp, type, USB_BP_15, 0);
3998 if (type == MCU_TYPE_USB) {
3999 ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
4001 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0);
4002 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0);
4003 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0);
4004 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0);
4005 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0);
4006 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0);
4007 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0);
4008 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0);
4010 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
4015 ocp_write_word(tp, type, PLA_BP_0, 0);
4016 ocp_write_word(tp, type, PLA_BP_1, 0);
4017 ocp_write_word(tp, type, PLA_BP_2, 0);
4018 ocp_write_word(tp, type, PLA_BP_3, 0);
4019 ocp_write_word(tp, type, PLA_BP_4, 0);
4020 ocp_write_word(tp, type, PLA_BP_5, 0);
4021 ocp_write_word(tp, type, PLA_BP_6, 0);
4022 ocp_write_word(tp, type, PLA_BP_7, 0);
4024 /* wait 3 ms to make sure the firmware is stopped */
4025 usleep_range(3000, 6000);
4026 ocp_write_word(tp, type, PLA_BP_BA, 0);
4029 static inline void rtl_reset_ocp_base(struct r8152 *tp)
4034 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
4039 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
4041 data |= PATCH_REQUEST;
4044 data &= ~PATCH_REQUEST;
4045 check = PATCH_READY;
4047 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
4049 for (i = 0; wait && i < 5000; i++) {
4052 usleep_range(1000, 2000);
4053 ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
4054 if ((ocp_data & PATCH_READY) ^ check)
4058 if (request && wait &&
4059 !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
4060 dev_err(&tp->intf->dev, "PHY patch request fail\n");
4061 rtl_phy_patch_request(tp, false, false);
4068 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
4070 if (patch_key && key_addr) {
4071 sram_write(tp, key_addr, patch_key);
4072 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
4073 } else if (key_addr) {
4076 sram_write(tp, 0x0000, 0x0000);
4078 data = ocp_reg_read(tp, OCP_PHY_LOCK);
4079 data &= ~PATCH_LOCK;
4080 ocp_reg_write(tp, OCP_PHY_LOCK, data);
4082 sram_write(tp, key_addr, 0x0000);
4089 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
4091 if (rtl_phy_patch_request(tp, true, wait))
4094 rtl_patch_key_set(tp, key_addr, patch_key);
4099 static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
4101 rtl_patch_key_set(tp, key_addr, 0);
4103 rtl_phy_patch_request(tp, false, wait);
4108 static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy)
4114 switch (tp->version) {
4135 fw_offset = __le16_to_cpu(phy->fw_offset);
4136 length = __le32_to_cpu(phy->blk_hdr.length);
4137 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4138 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4142 length -= fw_offset;
4144 dev_err(&tp->intf->dev, "invalid block length\n");
4148 if (__le16_to_cpu(phy->fw_reg) != 0x9A00) {
4149 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4158 static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver)
4162 switch (tp->version) {
4173 if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) {
4174 dev_err(&tp->intf->dev, "invalid block length\n");
4178 if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) {
4179 dev_err(&tp->intf->dev, "invalid phy ver addr\n");
4188 static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix)
4192 switch (tp->version) {
4203 if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) {
4204 dev_err(&tp->intf->dev, "invalid block length\n");
4208 if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD ||
4209 __le16_to_cpu(fix->setting.data) != BIT(7)) {
4210 dev_err(&tp->intf->dev, "invalid phy fixup\n");
4219 static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy)
4225 switch (tp->version) {
4236 fw_offset = __le16_to_cpu(phy->fw_offset);
4237 length = __le32_to_cpu(phy->blk_hdr.length);
4238 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4239 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4243 length -= fw_offset;
4245 dev_err(&tp->intf->dev, "invalid block length\n");
4249 if (phy->pre_num > 2) {
4250 dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num);
4254 if (phy->bp_num > 8) {
4255 dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num);
4264 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
4267 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
4270 switch (tp->version) {
4276 patch_en_addr = 0xa01a;
4284 fw_offset = __le16_to_cpu(phy->fw_offset);
4285 if (fw_offset < sizeof(*phy)) {
4286 dev_err(&tp->intf->dev, "fw_offset too small\n");
4290 length = __le32_to_cpu(phy->blk_hdr.length);
4291 if (length < fw_offset) {
4292 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4296 length -= __le16_to_cpu(phy->fw_offset);
4297 if (!length || (length & 1)) {
4298 dev_err(&tp->intf->dev, "invalid block length\n");
4302 if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
4303 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4307 if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
4308 dev_err(&tp->intf->dev, "invalid base address register\n");
4312 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
4313 dev_err(&tp->intf->dev,
4314 "invalid patch mode enabled register\n");
4318 if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
4319 dev_err(&tp->intf->dev,
4320 "invalid register to switch the mode\n");
4324 if (__le16_to_cpu(phy->bp_start) != bp_start) {
4325 dev_err(&tp->intf->dev,
4326 "invalid start register of break point\n");
4330 if (__le16_to_cpu(phy->bp_num) > 4) {
4331 dev_err(&tp->intf->dev, "invalid break point number\n");
4340 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
4342 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
4347 type = __le32_to_cpu(mac->blk_hdr.type);
4348 if (type == RTL_FW_PLA) {
4349 switch (tp->version) {
4354 bp_ba_addr = PLA_BP_BA;
4356 bp_start = PLA_BP_0;
4370 bp_ba_addr = PLA_BP_BA;
4371 bp_en_addr = PLA_BP_EN;
4372 bp_start = PLA_BP_0;
4377 bp_ba_addr = PLA_BP_BA;
4378 bp_en_addr = USB_BP2_EN;
4379 bp_start = PLA_BP_0;
4385 } else if (type == RTL_FW_USB) {
4386 switch (tp->version) {
4392 bp_ba_addr = USB_BP_BA;
4393 bp_en_addr = USB_BP_EN;
4394 bp_start = USB_BP_0;
4405 bp_ba_addr = USB_BP_BA;
4406 bp_en_addr = USB_BP2_EN;
4407 bp_start = USB_BP_0;
4420 fw_offset = __le16_to_cpu(mac->fw_offset);
4421 if (fw_offset < sizeof(*mac)) {
4422 dev_err(&tp->intf->dev, "fw_offset too small\n");
4426 length = __le32_to_cpu(mac->blk_hdr.length);
4427 if (length < fw_offset) {
4428 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4432 length -= fw_offset;
4433 if (length < 4 || (length & 3)) {
4434 dev_err(&tp->intf->dev, "invalid block length\n");
4438 if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
4439 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4443 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
4444 dev_err(&tp->intf->dev, "invalid base address register\n");
4448 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
4449 dev_err(&tp->intf->dev, "invalid enabled mask register\n");
4453 if (__le16_to_cpu(mac->bp_start) != bp_start) {
4454 dev_err(&tp->intf->dev,
4455 "invalid start register of break point\n");
4459 if (__le16_to_cpu(mac->bp_num) > max_bp) {
4460 dev_err(&tp->intf->dev, "invalid break point number\n");
4464 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
4466 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
4476 /* Verify the checksum for the firmware file. It is calculated from the version
4477 * field to the end of the file. Compare the result with the checksum field to
4478 * make sure the file is correct.
4480 static long rtl8152_fw_verify_checksum(struct r8152 *tp,
4481 struct fw_header *fw_hdr, size_t size)
4483 unsigned char checksum[sizeof(fw_hdr->checksum)];
4484 struct crypto_shash *alg;
4485 struct shash_desc *sdesc;
4489 alg = crypto_alloc_shash("sha256", 0, 0);
4495 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
4497 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
4498 crypto_shash_digestsize(alg));
4502 len = sizeof(*sdesc) + crypto_shash_descsize(alg);
4503 sdesc = kmalloc(len, GFP_KERNEL);
4510 len = size - sizeof(fw_hdr->checksum);
4511 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
4516 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
4517 dev_err(&tp->intf->dev, "checksum fail\n");
4522 crypto_free_shash(alg);
4527 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
4529 const struct firmware *fw = rtl_fw->fw;
4530 struct fw_header *fw_hdr = (struct fw_header *)fw->data;
4531 unsigned long fw_flags = 0;
4535 if (fw->size < sizeof(*fw_hdr)) {
4536 dev_err(&tp->intf->dev, "file too small\n");
4540 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
4546 for (i = sizeof(*fw_hdr); i < fw->size;) {
4547 struct fw_block *block = (struct fw_block *)&fw->data[i];
4550 if ((i + sizeof(*block)) > fw->size)
4553 type = __le32_to_cpu(block->type);
4556 if (__le32_to_cpu(block->length) != sizeof(*block))
4560 if (test_bit(FW_FLAGS_PLA, &fw_flags)) {
4561 dev_err(&tp->intf->dev,
4562 "multiple PLA firmware encountered");
4566 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4567 dev_err(&tp->intf->dev,
4568 "check PLA firmware failed\n");
4571 __set_bit(FW_FLAGS_PLA, &fw_flags);
4574 if (test_bit(FW_FLAGS_USB, &fw_flags)) {
4575 dev_err(&tp->intf->dev,
4576 "multiple USB firmware encountered");
4580 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4581 dev_err(&tp->intf->dev,
4582 "check USB firmware failed\n");
4585 __set_bit(FW_FLAGS_USB, &fw_flags);
4587 case RTL_FW_PHY_START:
4588 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4589 test_bit(FW_FLAGS_NC, &fw_flags) ||
4590 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4591 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4592 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4593 test_bit(FW_FLAGS_UC, &fw_flags) ||
4594 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4595 dev_err(&tp->intf->dev,
4596 "check PHY_START fail\n");
4600 if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) {
4601 dev_err(&tp->intf->dev,
4602 "Invalid length for PHY_START\n");
4605 __set_bit(FW_FLAGS_START, &fw_flags);
4607 case RTL_FW_PHY_STOP:
4608 if (test_bit(FW_FLAGS_STOP, &fw_flags) ||
4609 !test_bit(FW_FLAGS_START, &fw_flags)) {
4610 dev_err(&tp->intf->dev,
4611 "Check PHY_STOP fail\n");
4615 if (__le32_to_cpu(block->length) != sizeof(*block)) {
4616 dev_err(&tp->intf->dev,
4617 "Invalid length for PHY_STOP\n");
4620 __set_bit(FW_FLAGS_STOP, &fw_flags);
4623 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4624 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4625 dev_err(&tp->intf->dev,
4626 "check PHY_NC fail\n");
4630 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4631 dev_err(&tp->intf->dev,
4632 "multiple PHY NC encountered\n");
4636 if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) {
4637 dev_err(&tp->intf->dev,
4638 "check PHY NC firmware failed\n");
4641 __set_bit(FW_FLAGS_NC, &fw_flags);
4643 case RTL_FW_PHY_UNION_NC:
4644 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4645 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4646 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4647 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4648 test_bit(FW_FLAGS_UC, &fw_flags) ||
4649 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4650 dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n");
4654 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4655 dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n");
4659 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4660 dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n");
4663 __set_bit(FW_FLAGS_NC, &fw_flags);
4665 case RTL_FW_PHY_UNION_NC1:
4666 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4667 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4668 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4669 test_bit(FW_FLAGS_UC, &fw_flags) ||
4670 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4671 dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n");
4675 if (test_bit(FW_FLAGS_NC1, &fw_flags)) {
4676 dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n");
4680 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4681 dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n");
4684 __set_bit(FW_FLAGS_NC1, &fw_flags);
4686 case RTL_FW_PHY_UNION_NC2:
4687 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4688 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4689 test_bit(FW_FLAGS_UC, &fw_flags) ||
4690 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4691 dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n");
4695 if (test_bit(FW_FLAGS_NC2, &fw_flags)) {
4696 dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n");
4700 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4701 dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n");
4704 __set_bit(FW_FLAGS_NC2, &fw_flags);
4706 case RTL_FW_PHY_UNION_UC2:
4707 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4708 test_bit(FW_FLAGS_UC, &fw_flags) ||
4709 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4710 dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n");
4714 if (test_bit(FW_FLAGS_UC2, &fw_flags)) {
4715 dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n");
4719 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4720 dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n");
4723 __set_bit(FW_FLAGS_UC2, &fw_flags);
4725 case RTL_FW_PHY_UNION_UC:
4726 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4727 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4728 dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n");
4732 if (test_bit(FW_FLAGS_UC, &fw_flags)) {
4733 dev_err(&tp->intf->dev, "multiple PHY UC encountered\n");
4737 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4738 dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n");
4741 __set_bit(FW_FLAGS_UC, &fw_flags);
4743 case RTL_FW_PHY_UNION_MISC:
4744 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4745 dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n");
4749 case RTL_FW_PHY_FIXUP:
4750 if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) {
4751 dev_err(&tp->intf->dev, "check PHY fixup failed\n");
4755 case RTL_FW_PHY_SPEED_UP:
4756 if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) {
4757 dev_err(&tp->intf->dev, "multiple PHY firmware encountered");
4761 if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) {
4762 dev_err(&tp->intf->dev, "check PHY speed up failed\n");
4765 __set_bit(FW_FLAGS_SPEED_UP, &fw_flags);
4767 case RTL_FW_PHY_VER:
4768 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4769 test_bit(FW_FLAGS_NC, &fw_flags) ||
4770 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4771 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4772 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4773 test_bit(FW_FLAGS_UC, &fw_flags) ||
4774 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4775 dev_err(&tp->intf->dev, "Invalid order to set PHY version\n");
4779 if (test_bit(FW_FLAGS_VER, &fw_flags)) {
4780 dev_err(&tp->intf->dev, "multiple PHY version encountered");
4784 if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) {
4785 dev_err(&tp->intf->dev, "check PHY version failed\n");
4788 __set_bit(FW_FLAGS_VER, &fw_flags);
4791 dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
4797 i += ALIGN(__le32_to_cpu(block->length), 8);
4801 if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) {
4802 dev_err(&tp->intf->dev, "without PHY_STOP\n");
4811 static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait)
4816 rtl_reset_ocp_base(tp);
4818 if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) {
4819 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4823 len = __le32_to_cpu(phy->blk_hdr.length);
4824 len -= __le16_to_cpu(phy->fw_offset);
4825 data = (u8 *)phy + __le16_to_cpu(phy->fw_offset);
4827 if (rtl_phy_patch_request(tp, true, wait))
4839 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL);
4840 ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE;
4841 ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data);
4843 generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB);
4848 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL);
4849 ocp_data |= POL_GPHY_PATCH;
4850 ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data);
4852 for (i = 0; i < 1000; i++) {
4853 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH))
4858 dev_err(&tp->intf->dev, "ram code speedup mode timeout\n");
4863 rtl_reset_ocp_base(tp);
4865 rtl_phy_patch_request(tp, false, wait);
4867 if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version))
4868 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4870 dev_err(&tp->intf->dev, "ram code speedup mode fail\n");
4873 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver)
4877 ver_addr = __le16_to_cpu(phy_ver->ver.addr);
4878 ver = __le16_to_cpu(phy_ver->ver.data);
4880 rtl_reset_ocp_base(tp);
4882 if (sram_read(tp, ver_addr) >= ver) {
4883 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4887 sram_write(tp, ver_addr, ver);
4889 dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver);
4894 static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix)
4898 rtl_reset_ocp_base(tp);
4900 addr = __le16_to_cpu(fix->setting.addr);
4901 data = ocp_reg_read(tp, addr);
4903 switch (__le16_to_cpu(fix->bit_cmd)) {
4905 data &= __le16_to_cpu(fix->setting.data);
4908 data |= __le16_to_cpu(fix->setting.data);
4911 data &= ~__le16_to_cpu(fix->setting.data);
4914 data ^= __le16_to_cpu(fix->setting.data);
4920 ocp_reg_write(tp, addr, data);
4922 dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data);
4925 static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy)
4931 rtl_reset_ocp_base(tp);
4934 for (i = 0; i < num; i++)
4935 sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr),
4936 __le16_to_cpu(phy->pre_set[i].data));
4938 length = __le32_to_cpu(phy->blk_hdr.length);
4939 length -= __le16_to_cpu(phy->fw_offset);
4941 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4943 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4944 for (i = 0; i < num; i++)
4945 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4948 for (i = 0; i < num; i++)
4949 sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data));
4951 if (phy->bp_num && phy->bp_en.addr)
4952 sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data));
4954 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4957 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
4959 u16 mode_reg, bp_index;
4963 rtl_reset_ocp_base(tp);
4965 mode_reg = __le16_to_cpu(phy->mode_reg);
4966 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
4967 sram_write(tp, __le16_to_cpu(phy->ba_reg),
4968 __le16_to_cpu(phy->ba_data));
4970 length = __le32_to_cpu(phy->blk_hdr.length);
4971 length -= __le16_to_cpu(phy->fw_offset);
4973 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4975 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4976 for (i = 0; i < num; i++)
4977 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4979 sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
4980 __le16_to_cpu(phy->patch_en_value));
4982 bp_index = __le16_to_cpu(phy->bp_start);
4983 num = __le16_to_cpu(phy->bp_num);
4984 for (i = 0; i < num; i++) {
4985 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
4989 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
4991 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4994 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
4996 u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg;
5001 switch (__le32_to_cpu(mac->blk_hdr.type)) {
5003 type = MCU_TYPE_PLA;
5006 type = MCU_TYPE_USB;
5012 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
5013 if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) {
5014 dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB");
5018 rtl_clear_bp(tp, type);
5020 /* Enable backup/restore of MACDBG. This is required after clearing PLA
5021 * break points and before applying the PLA firmware.
5023 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
5024 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
5025 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
5026 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
5029 length = __le32_to_cpu(mac->blk_hdr.length);
5030 length -= __le16_to_cpu(mac->fw_offset);
5033 data += __le16_to_cpu(mac->fw_offset);
5035 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
5038 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
5039 __le16_to_cpu(mac->bp_ba_value));
5041 bp_index = __le16_to_cpu(mac->bp_start);
5042 bp_num = __le16_to_cpu(mac->bp_num);
5043 for (i = 0; i < bp_num; i++) {
5044 ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i]));
5048 bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
5050 ocp_write_word(tp, type, bp_en_addr,
5051 __le16_to_cpu(mac->bp_en_value));
5054 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
5057 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
5060 static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
5062 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5063 const struct firmware *fw;
5064 struct fw_header *fw_hdr;
5065 struct fw_phy_patch_key *key;
5067 int i, patch_phy = 1;
5069 if (IS_ERR_OR_NULL(rtl_fw->fw))
5073 fw_hdr = (struct fw_header *)fw->data;
5078 for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
5079 struct fw_block *block = (struct fw_block *)&fw->data[i];
5081 switch (__le32_to_cpu(block->type)) {
5086 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
5088 case RTL_FW_PHY_START:
5091 key = (struct fw_phy_patch_key *)block;
5092 key_addr = __le16_to_cpu(key->key_reg);
5093 rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut);
5095 case RTL_FW_PHY_STOP:
5099 rtl_post_ram_code(tp, key_addr, !power_cut);
5102 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
5104 case RTL_FW_PHY_VER:
5105 patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block);
5107 case RTL_FW_PHY_UNION_NC:
5108 case RTL_FW_PHY_UNION_NC1:
5109 case RTL_FW_PHY_UNION_NC2:
5110 case RTL_FW_PHY_UNION_UC2:
5111 case RTL_FW_PHY_UNION_UC:
5112 case RTL_FW_PHY_UNION_MISC:
5114 rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block);
5116 case RTL_FW_PHY_FIXUP:
5118 rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block);
5120 case RTL_FW_PHY_SPEED_UP:
5121 rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut);
5127 i += ALIGN(__le32_to_cpu(block->length), 8);
5131 if (rtl_fw->post_fw)
5132 rtl_fw->post_fw(tp);
5134 rtl_reset_ocp_base(tp);
5135 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
5136 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
5139 static void rtl8152_release_firmware(struct r8152 *tp)
5141 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5143 if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
5144 release_firmware(rtl_fw->fw);
5149 static int rtl8152_request_firmware(struct r8152 *tp)
5151 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5154 if (rtl_fw->fw || !rtl_fw->fw_name) {
5155 dev_info(&tp->intf->dev, "skip request firmware\n");
5160 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
5164 rc = rtl8152_check_firmware(tp, rtl_fw);
5166 release_firmware(rtl_fw->fw);
5170 rtl_fw->fw = ERR_PTR(rc);
5172 dev_warn(&tp->intf->dev,
5173 "unable to load firmware patch %s (%ld)\n",
5174 rtl_fw->fw_name, rc);
5180 static void r8152_aldps_en(struct r8152 *tp, bool enable)
5183 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
5184 LINKENA | DIS_SDSAVE);
5186 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
5192 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
5194 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
5195 ocp_reg_write(tp, OCP_EEE_DATA, reg);
5196 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
5199 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
5203 r8152_mmd_indirect(tp, dev, reg);
5204 data = ocp_reg_read(tp, OCP_EEE_DATA);
5205 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5210 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
5212 r8152_mmd_indirect(tp, dev, reg);
5213 ocp_reg_write(tp, OCP_EEE_DATA, data);
5214 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5217 static void r8152_eee_en(struct r8152 *tp, bool enable)
5219 u16 config1, config2, config3;
5222 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5223 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
5224 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
5225 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
5228 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5229 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
5230 config1 |= sd_rise_time(1);
5231 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
5232 config3 |= fast_snr(42);
5234 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5235 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
5237 config1 |= sd_rise_time(7);
5238 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
5239 config3 |= fast_snr(511);
5242 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5243 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
5244 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
5245 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
5248 static void r8153_eee_en(struct r8152 *tp, bool enable)
5253 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5254 config = ocp_reg_read(tp, OCP_EEE_CFG);
5257 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5260 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5261 config &= ~EEE10_EN;
5264 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5265 ocp_reg_write(tp, OCP_EEE_CFG, config);
5267 tp->ups_info.eee = enable;
5270 static void r8156_eee_en(struct r8152 *tp, bool enable)
5274 r8153_eee_en(tp, enable);
5276 config = ocp_reg_read(tp, OCP_EEE_ADV2);
5279 config |= MDIO_EEE_2_5GT;
5281 config &= ~MDIO_EEE_2_5GT;
5283 ocp_reg_write(tp, OCP_EEE_ADV2, config);
5286 static void rtl_eee_enable(struct r8152 *tp, bool enable)
5288 switch (tp->version) {
5293 r8152_eee_en(tp, true);
5294 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
5297 r8152_eee_en(tp, false);
5298 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
5309 r8153_eee_en(tp, true);
5310 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5312 r8153_eee_en(tp, false);
5313 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5322 r8156_eee_en(tp, true);
5323 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5325 r8156_eee_en(tp, false);
5326 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5334 static void r8152b_enable_fc(struct r8152 *tp)
5338 anar = r8152_mdio_read(tp, MII_ADVERTISE);
5339 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
5340 r8152_mdio_write(tp, MII_ADVERTISE, anar);
5342 tp->ups_info.flow_control = true;
5345 static void rtl8152_disable(struct r8152 *tp)
5347 r8152_aldps_en(tp, false);
5349 r8152_aldps_en(tp, true);
5352 static void r8152b_hw_phy_cfg(struct r8152 *tp)
5354 rtl8152_apply_firmware(tp, false);
5355 rtl_eee_enable(tp, tp->eee_en);
5356 r8152_aldps_en(tp, true);
5357 r8152b_enable_fc(tp);
5359 set_bit(PHY_RESET, &tp->flags);
5362 static void wait_oob_link_list_ready(struct r8152 *tp)
5367 for (i = 0; i < 1000; i++) {
5368 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5369 if (ocp_data & LINK_LIST_READY)
5371 usleep_range(1000, 2000);
5375 static void r8156b_wait_loading_flash(struct r8152 *tp)
5377 if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) &&
5378 !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) {
5381 for (i = 0; i < 100; i++) {
5382 if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE)
5384 usleep_range(1000, 2000);
5389 static void r8152b_exit_oob(struct r8152 *tp)
5393 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5394 ocp_data &= ~RCR_ACPT_ALL;
5395 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5397 rxdy_gated_en(tp, true);
5398 r8153_teredo_off(tp);
5399 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
5400 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
5402 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5403 ocp_data &= ~NOW_IS_OOB;
5404 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5406 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5407 ocp_data &= ~MCU_BORW_EN;
5408 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5410 wait_oob_link_list_ready(tp);
5412 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5413 ocp_data |= RE_INIT_LL;
5414 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5416 wait_oob_link_list_ready(tp);
5418 rtl8152_nic_reset(tp);
5420 /* rx share fifo credit full threshold */
5421 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5423 if (tp->udev->speed == USB_SPEED_FULL ||
5424 tp->udev->speed == USB_SPEED_LOW) {
5425 /* rx share fifo credit near full threshold */
5426 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5428 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5431 /* rx share fifo credit near full threshold */
5432 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5434 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5438 /* TX share fifo free credit full threshold */
5439 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
5441 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
5442 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
5443 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
5444 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
5446 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
5448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5450 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5451 ocp_data |= TCR0_AUTO_FIFO;
5452 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5455 static void r8152b_enter_oob(struct r8152 *tp)
5459 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5460 ocp_data &= ~NOW_IS_OOB;
5461 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5463 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
5464 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
5465 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
5469 wait_oob_link_list_ready(tp);
5471 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5472 ocp_data |= RE_INIT_LL;
5473 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5475 wait_oob_link_list_ready(tp);
5477 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5479 rtl_rx_vlan_en(tp, true);
5481 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
5482 ocp_data |= ALDPS_PROXY_MODE;
5483 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
5485 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5486 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5487 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5489 rxdy_gated_en(tp, false);
5491 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5492 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5493 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5496 static int r8153_pre_firmware_1(struct r8152 *tp)
5500 /* Wait till the WTD timer is ready. It would take at most 104 ms. */
5501 for (i = 0; i < 104; i++) {
5502 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
5504 if (!(ocp_data & WTD1_EN))
5506 usleep_range(1000, 2000);
5512 static int r8153_post_firmware_1(struct r8152 *tp)
5514 /* set USB_BP_4 to support USB_SPEED_SUPER only */
5515 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
5516 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
5518 /* reset UPHY timer to 36 ms */
5519 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5524 static int r8153_pre_firmware_2(struct r8152 *tp)
5528 r8153_pre_firmware_1(tp);
5530 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5531 ocp_data &= ~FW_FIX_SUSPEND;
5532 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5537 static int r8153_post_firmware_2(struct r8152 *tp)
5541 /* enable bp0 if support USB_SPEED_SUPER only */
5542 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
5543 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5545 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5548 /* reset UPHY timer to 36 ms */
5549 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5551 /* enable U3P3 check, set the counter to 4 */
5552 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
5554 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5555 ocp_data |= FW_FIX_SUSPEND;
5556 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5558 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5559 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5560 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5565 static int r8153_post_firmware_3(struct r8152 *tp)
5569 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5570 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5571 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5573 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5574 ocp_data |= FW_IP_RESET_EN;
5575 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5580 static int r8153b_pre_firmware_1(struct r8152 *tp)
5582 /* enable fc timer and set timer to 1 second. */
5583 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
5584 CTRL_TIMER_EN | (1000 / 8));
5589 static int r8153b_post_firmware_1(struct r8152 *tp)
5593 /* enable bp0 for RTL8153-BND */
5594 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
5595 if (ocp_data & BND_MASK) {
5596 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5598 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5601 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5602 ocp_data |= FLOW_CTRL_PATCH_OPT;
5603 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5605 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5606 ocp_data |= FC_PATCH_TASK;
5607 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5609 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5610 ocp_data |= FW_IP_RESET_EN;
5611 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5616 static int r8153c_post_firmware_1(struct r8152 *tp)
5620 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5621 ocp_data |= FLOW_CTRL_PATCH_2;
5622 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5624 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5625 ocp_data |= FC_PATCH_TASK;
5626 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5631 static int r8156a_post_firmware_1(struct r8152 *tp)
5635 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5636 ocp_data |= FW_IP_RESET_EN;
5637 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5639 /* Modify U3PHY parameter for compatibility issue */
5640 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e);
5641 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9);
5646 static void r8153_aldps_en(struct r8152 *tp, bool enable)
5650 data = ocp_reg_read(tp, OCP_POWER_CFG);
5653 ocp_reg_write(tp, OCP_POWER_CFG, data);
5658 ocp_reg_write(tp, OCP_POWER_CFG, data);
5659 for (i = 0; i < 20; i++) {
5660 usleep_range(1000, 2000);
5661 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
5666 tp->ups_info.aldps = enable;
5669 static void r8153_hw_phy_cfg(struct r8152 *tp)
5674 /* disable ALDPS before updating the PHY parameters */
5675 r8153_aldps_en(tp, false);
5677 /* disable EEE before updating the PHY parameters */
5678 rtl_eee_enable(tp, false);
5680 rtl8152_apply_firmware(tp, false);
5682 if (tp->version == RTL_VER_03) {
5683 data = ocp_reg_read(tp, OCP_EEE_CFG);
5684 data &= ~CTAP_SHORT_EN;
5685 ocp_reg_write(tp, OCP_EEE_CFG, data);
5688 data = ocp_reg_read(tp, OCP_POWER_CFG);
5689 data |= EEE_CLKDIV_EN;
5690 ocp_reg_write(tp, OCP_POWER_CFG, data);
5692 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5693 data |= EN_10M_BGOFF;
5694 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5695 data = ocp_reg_read(tp, OCP_POWER_CFG);
5696 data |= EN_10M_PLLOFF;
5697 ocp_reg_write(tp, OCP_POWER_CFG, data);
5698 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
5700 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5701 ocp_data |= PFM_PWM_SWITCH;
5702 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5704 /* Enable LPF corner auto tune */
5705 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
5707 /* Adjust 10M Amplitude */
5708 sram_write(tp, SRAM_10M_AMP1, 0x00af);
5709 sram_write(tp, SRAM_10M_AMP2, 0x0208);
5712 rtl_eee_enable(tp, true);
5714 r8153_aldps_en(tp, true);
5715 r8152b_enable_fc(tp);
5717 switch (tp->version) {
5724 r8153_u2p3en(tp, true);
5728 set_bit(PHY_RESET, &tp->flags);
5731 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
5735 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
5736 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
5737 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
5738 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
5743 static void r8153b_hw_phy_cfg(struct r8152 *tp)
5748 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
5749 if (ocp_data & PCUT_STATUS) {
5750 ocp_data &= ~PCUT_STATUS;
5751 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
5754 /* disable ALDPS before updating the PHY parameters */
5755 r8153_aldps_en(tp, false);
5757 /* disable EEE before updating the PHY parameters */
5758 rtl_eee_enable(tp, false);
5760 /* U1/U2/L1 idle timer. 500 us */
5761 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5763 data = r8153_phy_status(tp, 0);
5766 case PHY_STAT_PWRDN:
5767 case PHY_STAT_EXT_INIT:
5768 rtl8152_apply_firmware(tp, true);
5770 data = r8152_mdio_read(tp, MII_BMCR);
5771 data &= ~BMCR_PDOWN;
5772 r8152_mdio_write(tp, MII_BMCR, data);
5774 case PHY_STAT_LAN_ON:
5776 rtl8152_apply_firmware(tp, false);
5780 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
5782 data = sram_read(tp, SRAM_GREEN_CFG);
5784 sram_write(tp, SRAM_GREEN_CFG, data);
5785 data = ocp_reg_read(tp, OCP_NCTL_CFG);
5786 data |= PGA_RETURN_EN;
5787 ocp_reg_write(tp, OCP_NCTL_CFG, data);
5789 /* ADC Bias Calibration:
5790 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
5791 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
5794 ocp_data = r8152_efuse_read(tp, 0x7d);
5795 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
5797 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
5799 /* ups mode tx-link-pulse timing adjustment:
5800 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
5801 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
5803 ocp_data = ocp_reg_read(tp, 0xc426);
5806 u32 swr_cnt_1ms_ini;
5808 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
5809 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
5810 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
5811 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
5814 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5815 ocp_data |= PFM_PWM_SWITCH;
5816 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5819 if (!rtl_phy_patch_request(tp, true, true)) {
5820 data = ocp_reg_read(tp, OCP_POWER_CFG);
5821 data |= EEE_CLKDIV_EN;
5822 ocp_reg_write(tp, OCP_POWER_CFG, data);
5823 tp->ups_info.eee_ckdiv = true;
5825 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5826 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
5827 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5828 tp->ups_info.eee_cmod_lv = true;
5829 tp->ups_info._10m_ckdiv = true;
5830 tp->ups_info.eee_plloff_giga = true;
5832 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
5833 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
5834 tp->ups_info._250m_ckdiv = true;
5836 rtl_phy_patch_request(tp, false, true);
5840 rtl_eee_enable(tp, true);
5842 r8153_aldps_en(tp, true);
5843 r8152b_enable_fc(tp);
5845 set_bit(PHY_RESET, &tp->flags);
5848 static void r8153c_hw_phy_cfg(struct r8152 *tp)
5850 r8153b_hw_phy_cfg(tp);
5852 tp->ups_info.r_tune = true;
5855 static void rtl8153_change_mtu(struct r8152 *tp)
5857 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
5858 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
5861 static void r8153_first_init(struct r8152 *tp)
5865 rxdy_gated_en(tp, true);
5866 r8153_teredo_off(tp);
5868 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5869 ocp_data &= ~RCR_ACPT_ALL;
5870 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5872 rtl8152_nic_reset(tp);
5875 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5876 ocp_data &= ~NOW_IS_OOB;
5877 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5879 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5880 ocp_data &= ~MCU_BORW_EN;
5881 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5883 wait_oob_link_list_ready(tp);
5885 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5886 ocp_data |= RE_INIT_LL;
5887 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5889 wait_oob_link_list_ready(tp);
5891 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
5893 rtl8153_change_mtu(tp);
5895 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5896 ocp_data |= TCR0_AUTO_FIFO;
5897 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5899 rtl8152_nic_reset(tp);
5901 /* rx share fifo credit full threshold */
5902 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5903 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
5904 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
5905 /* TX share fifo free credit full threshold */
5906 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
5909 static void r8153_enter_oob(struct r8152 *tp)
5913 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5914 ocp_data &= ~NOW_IS_OOB;
5915 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5917 /* RX FIFO settings for OOB */
5918 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
5919 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
5920 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
5925 wait_oob_link_list_ready(tp);
5927 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5928 ocp_data |= RE_INIT_LL;
5929 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5931 wait_oob_link_list_ready(tp);
5933 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
5934 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
5936 switch (tp->version) {
5941 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
5942 ocp_data &= ~TEREDO_WAKE_MASK;
5943 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
5949 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
5950 * type. Set it to zero. bits[7:0] are the W1C bits about
5951 * the events. Set them to all 1 to clear them.
5953 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
5960 rtl_rx_vlan_en(tp, true);
5962 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
5963 ocp_data |= ALDPS_PROXY_MODE;
5964 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
5966 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5967 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5968 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5970 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5971 ocp_data |= MCU_BORW_EN;
5972 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5974 rxdy_gated_en(tp, false);
5976 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5977 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5978 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5981 static void rtl8153_disable(struct r8152 *tp)
5983 r8153_aldps_en(tp, false);
5986 r8153_aldps_en(tp, true);
5989 static int rtl8156_enable(struct r8152 *tp)
5994 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5998 rtl_set_eee_plus(tp);
5999 r8153_set_rx_early_timeout(tp);
6000 r8153_set_rx_early_size(tp);
6002 speed = rtl8152_get_speed(tp);
6003 rtl_set_ifg(tp, speed);
6005 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
6006 if (speed & _2500bps)
6007 ocp_data &= ~IDLE_SPDWN_EN;
6009 ocp_data |= IDLE_SPDWN_EN;
6010 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
6012 if (speed & _1000bps)
6013 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
6014 else if (speed & _500bps)
6015 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
6017 if (tp->udev->speed == USB_SPEED_HIGH) {
6018 /* USB 0xb45e[3:0] l1_nyet_hird */
6019 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6021 if (is_flow_control(speed))
6025 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6028 return rtl_enable(tp);
6031 static int rtl8156b_enable(struct r8152 *tp)
6036 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6040 rtl_set_eee_plus(tp);
6042 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM);
6043 ocp_data &= ~RX_AGGR_NUM_MASK;
6044 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data);
6046 r8153_set_rx_early_timeout(tp);
6047 r8153_set_rx_early_size(tp);
6049 speed = rtl8152_get_speed(tp);
6050 rtl_set_ifg(tp, speed);
6052 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
6053 if (speed & _2500bps)
6054 ocp_data &= ~IDLE_SPDWN_EN;
6056 ocp_data |= IDLE_SPDWN_EN;
6057 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
6059 if (tp->udev->speed == USB_SPEED_HIGH) {
6060 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6062 if (is_flow_control(speed))
6066 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6069 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
6070 ocp_data &= ~FC_PATCH_TASK;
6071 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6072 usleep_range(1000, 2000);
6073 ocp_data |= FC_PATCH_TASK;
6074 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6076 return rtl_enable(tp);
6079 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
6085 if (autoneg == AUTONEG_DISABLE) {
6086 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
6091 bmcr = BMCR_SPEED10;
6092 if (duplex == DUPLEX_FULL) {
6093 bmcr |= BMCR_FULLDPLX;
6094 tp->ups_info.speed_duplex = FORCE_10M_FULL;
6096 tp->ups_info.speed_duplex = FORCE_10M_HALF;
6100 bmcr = BMCR_SPEED100;
6101 if (duplex == DUPLEX_FULL) {
6102 bmcr |= BMCR_FULLDPLX;
6103 tp->ups_info.speed_duplex = FORCE_100M_FULL;
6105 tp->ups_info.speed_duplex = FORCE_100M_HALF;
6109 if (tp->mii.supports_gmii) {
6110 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
6111 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6120 if (duplex == DUPLEX_FULL)
6121 tp->mii.full_duplex = 1;
6123 tp->mii.full_duplex = 0;
6125 tp->mii.force_media = 1;
6130 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6131 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6133 if (tp->mii.supports_gmii) {
6134 support |= RTL_ADVERTISED_1000_FULL;
6136 if (tp->support_2500full)
6137 support |= RTL_ADVERTISED_2500_FULL;
6140 if (!(advertising & support))
6143 orig = r8152_mdio_read(tp, MII_ADVERTISE);
6144 new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
6145 ADVERTISE_100HALF | ADVERTISE_100FULL);
6146 if (advertising & RTL_ADVERTISED_10_HALF) {
6147 new1 |= ADVERTISE_10HALF;
6148 tp->ups_info.speed_duplex = NWAY_10M_HALF;
6150 if (advertising & RTL_ADVERTISED_10_FULL) {
6151 new1 |= ADVERTISE_10FULL;
6152 tp->ups_info.speed_duplex = NWAY_10M_FULL;
6155 if (advertising & RTL_ADVERTISED_100_HALF) {
6156 new1 |= ADVERTISE_100HALF;
6157 tp->ups_info.speed_duplex = NWAY_100M_HALF;
6159 if (advertising & RTL_ADVERTISED_100_FULL) {
6160 new1 |= ADVERTISE_100FULL;
6161 tp->ups_info.speed_duplex = NWAY_100M_FULL;
6165 r8152_mdio_write(tp, MII_ADVERTISE, new1);
6166 tp->mii.advertising = new1;
6169 if (tp->mii.supports_gmii) {
6170 orig = r8152_mdio_read(tp, MII_CTRL1000);
6171 new1 = orig & ~(ADVERTISE_1000FULL |
6172 ADVERTISE_1000HALF);
6174 if (advertising & RTL_ADVERTISED_1000_FULL) {
6175 new1 |= ADVERTISE_1000FULL;
6176 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6180 r8152_mdio_write(tp, MII_CTRL1000, new1);
6183 if (tp->support_2500full) {
6184 orig = ocp_reg_read(tp, OCP_10GBT_CTRL);
6185 new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G;
6187 if (advertising & RTL_ADVERTISED_2500_FULL) {
6188 new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G;
6189 tp->ups_info.speed_duplex = NWAY_2500M_FULL;
6193 ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
6196 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
6198 tp->mii.force_media = 0;
6201 if (test_and_clear_bit(PHY_RESET, &tp->flags))
6204 r8152_mdio_write(tp, MII_BMCR, bmcr);
6206 if (bmcr & BMCR_RESET) {
6209 for (i = 0; i < 50; i++) {
6211 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
6220 static void rtl8152_up(struct r8152 *tp)
6222 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6225 r8152_aldps_en(tp, false);
6226 r8152b_exit_oob(tp);
6227 r8152_aldps_en(tp, true);
6230 static void rtl8152_down(struct r8152 *tp)
6232 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6233 rtl_drop_queued_tx(tp);
6237 r8152_power_cut_en(tp, false);
6238 r8152_aldps_en(tp, false);
6239 r8152b_enter_oob(tp);
6240 r8152_aldps_en(tp, true);
6243 static void rtl8153_up(struct r8152 *tp)
6247 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6250 r8153_u1u2en(tp, false);
6251 r8153_u2p3en(tp, false);
6252 r8153_aldps_en(tp, false);
6253 r8153_first_init(tp);
6255 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6256 ocp_data |= LANWAKE_CLR_EN;
6257 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6259 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
6260 ocp_data &= ~LANWAKE_PIN;
6261 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
6263 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
6264 ocp_data &= ~DELAY_PHY_PWR_CHG;
6265 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
6267 r8153_aldps_en(tp, true);
6269 switch (tp->version) {
6276 r8153_u2p3en(tp, true);
6280 r8153_u1u2en(tp, true);
6283 static void rtl8153_down(struct r8152 *tp)
6287 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6288 rtl_drop_queued_tx(tp);
6292 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6293 ocp_data &= ~LANWAKE_CLR_EN;
6294 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6296 r8153_u1u2en(tp, false);
6297 r8153_u2p3en(tp, false);
6298 r8153_power_cut_en(tp, false);
6299 r8153_aldps_en(tp, false);
6300 r8153_enter_oob(tp);
6301 r8153_aldps_en(tp, true);
6304 static void rtl8153b_up(struct r8152 *tp)
6308 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6311 r8153b_u1u2en(tp, false);
6312 r8153_u2p3en(tp, false);
6313 r8153_aldps_en(tp, false);
6315 r8153_first_init(tp);
6316 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6318 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6319 ocp_data &= ~PLA_MCU_SPDWN_EN;
6320 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6322 r8153_aldps_en(tp, true);
6324 if (tp->udev->speed >= USB_SPEED_SUPER)
6325 r8153b_u1u2en(tp, true);
6328 static void rtl8153b_down(struct r8152 *tp)
6332 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6333 rtl_drop_queued_tx(tp);
6337 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6338 ocp_data |= PLA_MCU_SPDWN_EN;
6339 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6341 r8153b_u1u2en(tp, false);
6342 r8153_u2p3en(tp, false);
6343 r8153b_power_cut_en(tp, false);
6344 r8153_aldps_en(tp, false);
6345 r8153_enter_oob(tp);
6346 r8153_aldps_en(tp, true);
6349 static void rtl8153c_change_mtu(struct r8152 *tp)
6351 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
6352 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
6354 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6356 /* Adjust the tx fifo free credit full threshold, otherwise
6357 * the fifo would be too small to send a jumbo frame packet.
6359 if (tp->netdev->mtu < 8000)
6360 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8);
6362 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8);
6365 static void rtl8153c_up(struct r8152 *tp)
6369 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6372 r8153b_u1u2en(tp, false);
6373 r8153_u2p3en(tp, false);
6374 r8153_aldps_en(tp, false);
6376 rxdy_gated_en(tp, true);
6377 r8153_teredo_off(tp);
6379 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6380 ocp_data &= ~RCR_ACPT_ALL;
6381 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6383 rtl8152_nic_reset(tp);
6386 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6387 ocp_data &= ~NOW_IS_OOB;
6388 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6390 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6391 ocp_data &= ~MCU_BORW_EN;
6392 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6394 wait_oob_link_list_ready(tp);
6396 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6397 ocp_data |= RE_INIT_LL;
6398 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6400 wait_oob_link_list_ready(tp);
6402 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6404 rtl8153c_change_mtu(tp);
6406 rtl8152_nic_reset(tp);
6408 /* rx share fifo credit full threshold */
6409 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02);
6410 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08);
6411 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
6412 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
6414 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6416 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
6418 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
6420 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
6422 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
6424 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6425 ocp_data &= ~PLA_MCU_SPDWN_EN;
6426 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6428 r8153_aldps_en(tp, true);
6429 r8153b_u1u2en(tp, true);
6432 static inline u32 fc_pause_on_auto(struct r8152 *tp)
6434 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024);
6437 static inline u32 fc_pause_off_auto(struct r8152 *tp)
6439 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024);
6442 static void r8156_fc_parameter(struct r8152 *tp)
6444 u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp);
6445 u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp);
6447 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16);
6448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16);
6451 static void rtl8156_change_mtu(struct r8152 *tp)
6453 u32 rx_max_size = mtu_to_size(tp->netdev->mtu);
6455 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size);
6456 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
6457 r8156_fc_parameter(tp);
6459 /* TX share fifo free credit full threshold */
6460 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6461 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
6462 ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16);
6465 static void rtl8156_up(struct r8152 *tp)
6469 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6472 r8153b_u1u2en(tp, false);
6473 r8153_u2p3en(tp, false);
6474 r8153_aldps_en(tp, false);
6476 rxdy_gated_en(tp, true);
6477 r8153_teredo_off(tp);
6479 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6480 ocp_data &= ~RCR_ACPT_ALL;
6481 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6483 rtl8152_nic_reset(tp);
6486 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6487 ocp_data &= ~NOW_IS_OOB;
6488 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6490 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6491 ocp_data &= ~MCU_BORW_EN;
6492 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6494 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6496 rtl8156_change_mtu(tp);
6498 switch (tp->version) {
6502 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
6503 ocp_data |= ACT_ODMA;
6504 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
6510 /* share FIFO settings */
6511 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL);
6512 ocp_data &= ~RXFIFO_FULL_MASK;
6514 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data);
6516 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6517 ocp_data &= ~PLA_MCU_SPDWN_EN;
6518 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6520 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION);
6521 ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF);
6522 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data);
6524 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
6526 if (tp->saved_wolopts != __rtl_get_wol(tp)) {
6527 netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n");
6528 __rtl_set_wol(tp, tp->saved_wolopts);
6531 r8153_aldps_en(tp, true);
6532 r8153_u2p3en(tp, true);
6534 if (tp->udev->speed >= USB_SPEED_SUPER)
6535 r8153b_u1u2en(tp, true);
6538 static void rtl8156_down(struct r8152 *tp)
6542 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6543 rtl_drop_queued_tx(tp);
6547 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6548 ocp_data |= PLA_MCU_SPDWN_EN;
6549 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6551 r8153b_u1u2en(tp, false);
6552 r8153_u2p3en(tp, false);
6553 r8153b_power_cut_en(tp, false);
6554 r8153_aldps_en(tp, false);
6556 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6557 ocp_data &= ~NOW_IS_OOB;
6558 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6560 /* RX FIFO settings for OOB */
6561 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 64 / 16);
6562 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 1024 / 16);
6563 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 4096 / 16);
6568 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
6569 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
6571 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
6572 * type. Set it to zero. bits[7:0] are the W1C bits about
6573 * the events. Set them to all 1 to clear them.
6575 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
6577 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6578 ocp_data |= NOW_IS_OOB;
6579 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6581 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6582 ocp_data |= MCU_BORW_EN;
6583 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6585 rtl_rx_vlan_en(tp, true);
6586 rxdy_gated_en(tp, false);
6588 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6589 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
6590 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6592 r8153_aldps_en(tp, true);
6595 static bool rtl8152_in_nway(struct r8152 *tp)
6599 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
6600 tp->ocp_base = 0x2000;
6601 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
6602 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
6604 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
6605 if (nway_state & 0xc000)
6611 static bool rtl8153_in_nway(struct r8152 *tp)
6613 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
6615 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
6621 static void r8156_mdio_force_mode(struct r8152 *tp)
6625 /* Select force mode through 0xa5b4 bit 15
6626 * 0: MDIO force mode
6629 data = ocp_reg_read(tp, 0xa5b4);
6630 if (data & BIT(15)) {
6632 ocp_reg_write(tp, 0xa5b4, data);
6636 static void set_carrier(struct r8152 *tp)
6638 struct net_device *netdev = tp->netdev;
6639 struct napi_struct *napi = &tp->napi;
6642 speed = rtl8152_get_speed(tp);
6644 if (speed & LINK_STATUS) {
6645 if (!netif_carrier_ok(netdev)) {
6646 tp->rtl_ops.enable(tp);
6647 netif_stop_queue(netdev);
6649 netif_carrier_on(netdev);
6651 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6652 _rtl8152_set_rx_mode(netdev);
6654 netif_wake_queue(netdev);
6655 netif_info(tp, link, netdev, "carrier on\n");
6656 } else if (netif_queue_stopped(netdev) &&
6657 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
6658 netif_wake_queue(netdev);
6661 if (netif_carrier_ok(netdev)) {
6662 netif_carrier_off(netdev);
6663 tasklet_disable(&tp->tx_tl);
6665 tp->rtl_ops.disable(tp);
6667 tasklet_enable(&tp->tx_tl);
6668 netif_info(tp, link, netdev, "carrier off\n");
6673 static void rtl_work_func_t(struct work_struct *work)
6675 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
6677 /* If the device is unplugged or !netif_running(), the workqueue
6678 * doesn't need to wake the device, and could return directly.
6680 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
6683 if (usb_autopm_get_interface(tp->intf) < 0)
6686 if (!test_bit(WORK_ENABLE, &tp->flags))
6689 if (!mutex_trylock(&tp->control)) {
6690 schedule_delayed_work(&tp->schedule, 0);
6694 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
6697 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
6698 _rtl8152_set_rx_mode(tp->netdev);
6700 /* don't schedule tasket before linking */
6701 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
6702 netif_carrier_ok(tp->netdev))
6703 tasklet_schedule(&tp->tx_tl);
6705 if (test_and_clear_bit(RX_EPROTO, &tp->flags) &&
6706 !list_empty(&tp->rx_done))
6707 napi_schedule(&tp->napi);
6709 mutex_unlock(&tp->control);
6712 usb_autopm_put_interface(tp->intf);
6715 static void rtl_hw_phy_work_func_t(struct work_struct *work)
6717 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
6719 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6722 if (usb_autopm_get_interface(tp->intf) < 0)
6725 mutex_lock(&tp->control);
6727 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
6728 tp->rtl_fw.retry = false;
6729 tp->rtl_fw.fw = NULL;
6731 /* Delay execution in case request_firmware() is not ready yet.
6733 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
6737 tp->rtl_ops.hw_phy_cfg(tp);
6739 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
6743 mutex_unlock(&tp->control);
6745 usb_autopm_put_interface(tp->intf);
6748 #ifdef CONFIG_PM_SLEEP
6749 static int rtl_notifier(struct notifier_block *nb, unsigned long action,
6752 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
6755 case PM_HIBERNATION_PREPARE:
6756 case PM_SUSPEND_PREPARE:
6757 usb_autopm_get_interface(tp->intf);
6760 case PM_POST_HIBERNATION:
6761 case PM_POST_SUSPEND:
6762 usb_autopm_put_interface(tp->intf);
6765 case PM_POST_RESTORE:
6766 case PM_RESTORE_PREPARE:
6775 static int rtl8152_open(struct net_device *netdev)
6777 struct r8152 *tp = netdev_priv(netdev);
6780 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
6781 cancel_delayed_work_sync(&tp->hw_phy_work);
6782 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
6785 res = alloc_all_mem(tp);
6789 res = usb_autopm_get_interface(tp->intf);
6793 mutex_lock(&tp->control);
6797 netif_carrier_off(netdev);
6798 netif_start_queue(netdev);
6799 set_bit(WORK_ENABLE, &tp->flags);
6801 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
6804 netif_device_detach(tp->netdev);
6805 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
6809 napi_enable(&tp->napi);
6810 tasklet_enable(&tp->tx_tl);
6812 mutex_unlock(&tp->control);
6814 usb_autopm_put_interface(tp->intf);
6815 #ifdef CONFIG_PM_SLEEP
6816 tp->pm_notifier.notifier_call = rtl_notifier;
6817 register_pm_notifier(&tp->pm_notifier);
6822 mutex_unlock(&tp->control);
6823 usb_autopm_put_interface(tp->intf);
6830 static int rtl8152_close(struct net_device *netdev)
6832 struct r8152 *tp = netdev_priv(netdev);
6835 #ifdef CONFIG_PM_SLEEP
6836 unregister_pm_notifier(&tp->pm_notifier);
6838 tasklet_disable(&tp->tx_tl);
6839 clear_bit(WORK_ENABLE, &tp->flags);
6840 usb_kill_urb(tp->intr_urb);
6841 cancel_delayed_work_sync(&tp->schedule);
6842 napi_disable(&tp->napi);
6843 netif_stop_queue(netdev);
6845 res = usb_autopm_get_interface(tp->intf);
6846 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
6847 rtl_drop_queued_tx(tp);
6850 mutex_lock(&tp->control);
6852 tp->rtl_ops.down(tp);
6854 mutex_unlock(&tp->control);
6858 usb_autopm_put_interface(tp->intf);
6865 static void rtl_tally_reset(struct r8152 *tp)
6869 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
6870 ocp_data |= TALLY_RESET;
6871 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
6874 static void r8152b_init(struct r8152 *tp)
6879 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6882 data = r8152_mdio_read(tp, MII_BMCR);
6883 if (data & BMCR_PDOWN) {
6884 data &= ~BMCR_PDOWN;
6885 r8152_mdio_write(tp, MII_BMCR, data);
6888 r8152_aldps_en(tp, false);
6890 if (tp->version == RTL_VER_01) {
6891 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
6892 ocp_data &= ~LED_MODE_MASK;
6893 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
6896 r8152_power_cut_en(tp, false);
6898 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
6899 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
6900 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
6901 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
6902 ocp_data &= ~MCU_CLK_RATIO_MASK;
6903 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
6904 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
6905 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
6906 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
6907 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
6909 rtl_tally_reset(tp);
6911 /* enable rx aggregation */
6912 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
6913 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
6914 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
6917 static void r8153_init(struct r8152 *tp)
6923 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6926 r8153_u1u2en(tp, false);
6928 for (i = 0; i < 500; i++) {
6929 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
6934 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6938 data = r8153_phy_status(tp, 0);
6940 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
6941 tp->version == RTL_VER_05)
6942 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
6944 data = r8152_mdio_read(tp, MII_BMCR);
6945 if (data & BMCR_PDOWN) {
6946 data &= ~BMCR_PDOWN;
6947 r8152_mdio_write(tp, MII_BMCR, data);
6950 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
6952 r8153_u2p3en(tp, false);
6954 if (tp->version == RTL_VER_04) {
6955 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
6956 ocp_data &= ~pwd_dn_scale_mask;
6957 ocp_data |= pwd_dn_scale(96);
6958 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
6960 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
6961 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
6962 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
6963 } else if (tp->version == RTL_VER_05) {
6964 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
6965 ocp_data &= ~ECM_ALDPS;
6966 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
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;
6972 ocp_data |= DYNAMIC_BURST;
6973 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
6974 } else if (tp->version == RTL_VER_06) {
6975 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
6976 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
6977 ocp_data &= ~DYNAMIC_BURST;
6979 ocp_data |= DYNAMIC_BURST;
6980 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
6982 r8153_queue_wake(tp, false);
6984 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
6985 if (rtl8152_get_speed(tp) & LINK_STATUS)
6986 ocp_data |= CUR_LINK_OK;
6988 ocp_data &= ~CUR_LINK_OK;
6989 ocp_data |= POLL_LINK_CHG;
6990 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
6993 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
6994 ocp_data |= EP4_FULL_FC;
6995 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
6997 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
6998 ocp_data &= ~TIMER11_EN;
6999 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
7001 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
7002 ocp_data &= ~LED_MODE_MASK;
7003 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
7005 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
7006 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
7007 ocp_data |= LPM_TIMER_500MS;
7009 ocp_data |= LPM_TIMER_500US;
7010 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
7012 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
7013 ocp_data &= ~SEN_VAL_MASK;
7014 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
7015 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
7017 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
7019 r8153_power_cut_en(tp, false);
7020 rtl_runtime_suspend_enable(tp, false);
7021 r8153_mac_clk_speed_down(tp, false);
7022 r8153_u1u2en(tp, true);
7023 usb_enable_lpm(tp->udev);
7025 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
7026 ocp_data |= LANWAKE_CLR_EN;
7027 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
7029 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
7030 ocp_data &= ~LANWAKE_PIN;
7031 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
7033 /* rx aggregation */
7034 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7035 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7036 if (tp->dell_tb_rx_agg_bug)
7037 ocp_data |= RX_AGG_DISABLE;
7039 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7041 rtl_tally_reset(tp);
7043 switch (tp->udev->speed) {
7044 case USB_SPEED_SUPER:
7045 case USB_SPEED_SUPER_PLUS:
7046 tp->coalesce = COALESCE_SUPER;
7048 case USB_SPEED_HIGH:
7049 tp->coalesce = COALESCE_HIGH;
7052 tp->coalesce = COALESCE_SLOW;
7057 static void r8153b_init(struct r8152 *tp)
7063 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7066 r8153b_u1u2en(tp, false);
7068 for (i = 0; i < 500; i++) {
7069 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7074 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7078 data = r8153_phy_status(tp, 0);
7080 data = r8152_mdio_read(tp, MII_BMCR);
7081 if (data & BMCR_PDOWN) {
7082 data &= ~BMCR_PDOWN;
7083 r8152_mdio_write(tp, MII_BMCR, data);
7086 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7088 r8153_u2p3en(tp, false);
7090 /* MSC timer = 0xfff * 8ms = 32760 ms */
7091 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7093 r8153b_power_cut_en(tp, false);
7094 r8153b_ups_en(tp, false);
7095 r8153_queue_wake(tp, false);
7096 rtl_runtime_suspend_enable(tp, false);
7098 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7099 if (rtl8152_get_speed(tp) & LINK_STATUS)
7100 ocp_data |= CUR_LINK_OK;
7102 ocp_data &= ~CUR_LINK_OK;
7103 ocp_data |= POLL_LINK_CHG;
7104 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7106 if (tp->udev->speed >= USB_SPEED_SUPER)
7107 r8153b_u1u2en(tp, true);
7109 usb_enable_lpm(tp->udev);
7111 /* MAC clock speed down */
7112 r8153_mac_clk_speed_down(tp, true);
7114 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
7115 ocp_data &= ~PLA_MCU_SPDWN_EN;
7116 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
7118 if (tp->version == RTL_VER_09) {
7119 /* Disable Test IO for 32QFN */
7120 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
7121 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7122 ocp_data |= TEST_IO_OFF;
7123 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7127 set_bit(GREEN_ETHERNET, &tp->flags);
7129 /* rx aggregation */
7130 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7131 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7132 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7134 rtl_tally_reset(tp);
7136 tp->coalesce = 15000; /* 15 us */
7139 static void r8153c_init(struct r8152 *tp)
7145 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7148 r8153b_u1u2en(tp, false);
7150 /* Disable spi_en */
7151 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
7152 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
7153 ocp_data &= ~BIT(3);
7154 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
7155 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0);
7157 ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data);
7159 for (i = 0; i < 500; i++) {
7160 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7165 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7169 data = r8153_phy_status(tp, 0);
7171 data = r8152_mdio_read(tp, MII_BMCR);
7172 if (data & BMCR_PDOWN) {
7173 data &= ~BMCR_PDOWN;
7174 r8152_mdio_write(tp, MII_BMCR, data);
7177 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7179 r8153_u2p3en(tp, false);
7181 /* MSC timer = 0xfff * 8ms = 32760 ms */
7182 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7184 r8153b_power_cut_en(tp, false);
7185 r8153c_ups_en(tp, false);
7186 r8153_queue_wake(tp, false);
7187 rtl_runtime_suspend_enable(tp, false);
7189 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7190 if (rtl8152_get_speed(tp) & LINK_STATUS)
7191 ocp_data |= CUR_LINK_OK;
7193 ocp_data &= ~CUR_LINK_OK;
7195 ocp_data |= POLL_LINK_CHG;
7196 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7198 r8153b_u1u2en(tp, true);
7200 usb_enable_lpm(tp->udev);
7202 /* MAC clock speed down */
7203 r8153_mac_clk_speed_down(tp, true);
7205 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
7206 ocp_data &= ~BIT(7);
7207 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
7209 set_bit(GREEN_ETHERNET, &tp->flags);
7211 /* rx aggregation */
7212 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7213 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7214 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7216 rtl_tally_reset(tp);
7218 tp->coalesce = 15000; /* 15 us */
7221 static void r8156_hw_phy_cfg(struct r8152 *tp)
7226 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7227 if (ocp_data & PCUT_STATUS) {
7228 ocp_data &= ~PCUT_STATUS;
7229 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7232 data = r8153_phy_status(tp, 0);
7234 case PHY_STAT_EXT_INIT:
7235 rtl8152_apply_firmware(tp, true);
7237 data = ocp_reg_read(tp, 0xa468);
7238 data &= ~(BIT(3) | BIT(1));
7239 ocp_reg_write(tp, 0xa468, data);
7241 case PHY_STAT_LAN_ON:
7242 case PHY_STAT_PWRDN:
7244 rtl8152_apply_firmware(tp, false);
7248 /* disable ALDPS before updating the PHY parameters */
7249 r8153_aldps_en(tp, false);
7251 /* disable EEE before updating the PHY parameters */
7252 rtl_eee_enable(tp, false);
7254 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7255 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7257 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7258 ocp_data |= PFM_PWM_SWITCH;
7259 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7261 switch (tp->version) {
7263 data = ocp_reg_read(tp, 0xad40);
7265 data |= BIT(7) | BIT(2);
7266 ocp_reg_write(tp, 0xad40, data);
7268 data = ocp_reg_read(tp, 0xad4e);
7270 ocp_reg_write(tp, 0xad4e, data);
7271 data = ocp_reg_read(tp, 0xad16);
7274 ocp_reg_write(tp, 0xad16, data);
7275 data = ocp_reg_read(tp, 0xad32);
7278 ocp_reg_write(tp, 0xad32, data);
7279 data = ocp_reg_read(tp, 0xac08);
7280 data &= ~(BIT(12) | BIT(8));
7281 ocp_reg_write(tp, 0xac08, data);
7282 data = ocp_reg_read(tp, 0xac8a);
7283 data |= BIT(12) | BIT(13) | BIT(14);
7285 ocp_reg_write(tp, 0xac8a, data);
7286 data = ocp_reg_read(tp, 0xad18);
7288 ocp_reg_write(tp, 0xad18, data);
7289 data = ocp_reg_read(tp, 0xad1a);
7291 ocp_reg_write(tp, 0xad1a, data);
7292 data = ocp_reg_read(tp, 0xad1c);
7294 ocp_reg_write(tp, 0xad1c, data);
7296 data = sram_read(tp, 0x80ea);
7299 sram_write(tp, 0x80ea, data);
7300 data = sram_read(tp, 0x80eb);
7303 sram_write(tp, 0x80eb, data);
7304 data = sram_read(tp, 0x80f8);
7307 sram_write(tp, 0x80f8, data);
7308 data = sram_read(tp, 0x80f1);
7311 sram_write(tp, 0x80f1, data);
7313 data = sram_read(tp, 0x80fe);
7316 sram_write(tp, 0x80fe, data);
7317 data = sram_read(tp, 0x8102);
7320 sram_write(tp, 0x8102, data);
7321 data = sram_read(tp, 0x8015);
7324 sram_write(tp, 0x8015, data);
7325 data = sram_read(tp, 0x8100);
7328 sram_write(tp, 0x8100, data);
7329 data = sram_read(tp, 0x8014);
7332 sram_write(tp, 0x8014, data);
7333 data = sram_read(tp, 0x8016);
7336 sram_write(tp, 0x8016, data);
7337 data = sram_read(tp, 0x80dc);
7340 sram_write(tp, 0x80dc, data);
7341 data = sram_read(tp, 0x80df);
7343 sram_write(tp, 0x80df, data);
7344 data = sram_read(tp, 0x80e1);
7346 sram_write(tp, 0x80e1, data);
7348 data = ocp_reg_read(tp, 0xbf06);
7351 ocp_reg_write(tp, 0xbf06, data);
7353 sram_write(tp, 0x819f, 0xddb6);
7355 ocp_reg_write(tp, 0xbc34, 0x5555);
7356 data = ocp_reg_read(tp, 0xbf0a);
7359 ocp_reg_write(tp, 0xbf0a, data);
7361 data = ocp_reg_read(tp, 0xbd2c);
7363 ocp_reg_write(tp, 0xbd2c, data);
7366 data = ocp_reg_read(tp, 0xad16);
7368 ocp_reg_write(tp, 0xad16, data);
7369 data = ocp_reg_read(tp, 0xad32);
7372 ocp_reg_write(tp, 0xad32, data);
7373 data = ocp_reg_read(tp, 0xac08);
7374 data &= ~(BIT(12) | BIT(8));
7375 ocp_reg_write(tp, 0xac08, data);
7376 data = ocp_reg_read(tp, 0xacc0);
7379 ocp_reg_write(tp, 0xacc0, data);
7380 data = ocp_reg_read(tp, 0xad40);
7382 data |= BIT(6) | BIT(2);
7383 ocp_reg_write(tp, 0xad40, data);
7384 data = ocp_reg_read(tp, 0xac14);
7386 ocp_reg_write(tp, 0xac14, data);
7387 data = ocp_reg_read(tp, 0xac80);
7388 data &= ~(BIT(8) | BIT(9));
7389 ocp_reg_write(tp, 0xac80, data);
7390 data = ocp_reg_read(tp, 0xac5e);
7393 ocp_reg_write(tp, 0xac5e, data);
7394 ocp_reg_write(tp, 0xad4c, 0x00a8);
7395 ocp_reg_write(tp, 0xac5c, 0x01ff);
7396 data = ocp_reg_read(tp, 0xac8a);
7398 data |= BIT(4) | BIT(5);
7399 ocp_reg_write(tp, 0xac8a, data);
7400 ocp_reg_write(tp, 0xb87c, 0x8157);
7401 data = ocp_reg_read(tp, 0xb87e);
7404 ocp_reg_write(tp, 0xb87e, data);
7405 ocp_reg_write(tp, 0xb87c, 0x8159);
7406 data = ocp_reg_read(tp, 0xb87e);
7409 ocp_reg_write(tp, 0xb87e, data);
7412 ocp_reg_write(tp, 0xb87c, 0x80a2);
7413 ocp_reg_write(tp, 0xb87e, 0x0153);
7414 ocp_reg_write(tp, 0xb87c, 0x809c);
7415 ocp_reg_write(tp, 0xb87e, 0x0153);
7418 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056);
7420 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7421 ocp_data |= EN_XG_LIP | EN_G_LIP;
7422 ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7424 sram_write(tp, 0x8257, 0x020f); /* XG PLL */
7425 sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */
7427 if (rtl_phy_patch_request(tp, true, true))
7431 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7432 ocp_data |= EEE_SPDWN_EN;
7433 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7435 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7436 data &= ~(EN_EEE_100 | EN_EEE_1000);
7437 data |= EN_10M_CLKDIV;
7438 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7439 tp->ups_info._10m_ckdiv = true;
7440 tp->ups_info.eee_plloff_100 = false;
7441 tp->ups_info.eee_plloff_giga = false;
7443 data = ocp_reg_read(tp, OCP_POWER_CFG);
7444 data &= ~EEE_CLKDIV_EN;
7445 ocp_reg_write(tp, OCP_POWER_CFG, data);
7446 tp->ups_info.eee_ckdiv = false;
7448 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
7449 ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5));
7450 tp->ups_info._250m_ckdiv = false;
7452 rtl_phy_patch_request(tp, false, true);
7454 /* enable ADC Ibias Cal */
7455 data = ocp_reg_read(tp, 0xd068);
7457 ocp_reg_write(tp, 0xd068, data);
7459 /* enable Thermal Sensor */
7460 data = sram_read(tp, 0x81a2);
7462 sram_write(tp, 0x81a2, data);
7463 data = ocp_reg_read(tp, 0xb54c);
7466 ocp_reg_write(tp, 0xb54c, data);
7468 /* Nway 2.5G Lite */
7469 data = ocp_reg_read(tp, 0xa454);
7471 ocp_reg_write(tp, 0xa454, data);
7473 /* CS DSP solution */
7474 data = ocp_reg_read(tp, OCP_10GBT_CTRL);
7475 data |= RTL_ADV2_5G_F_R;
7476 ocp_reg_write(tp, OCP_10GBT_CTRL, data);
7477 data = ocp_reg_read(tp, 0xad4e);
7479 ocp_reg_write(tp, 0xad4e, data);
7480 data = ocp_reg_read(tp, 0xa86a);
7482 ocp_reg_write(tp, 0xa86a, data);
7485 if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) &&
7486 (ocp_reg_read(tp, 0xd068) & BIT(1))) {
7489 data = ocp_reg_read(tp, 0xd068);
7491 data |= 0x1; /* p0 */
7492 ocp_reg_write(tp, 0xd068, data);
7493 swap_a = ocp_reg_read(tp, 0xd06a);
7495 data |= 0x18; /* p3 */
7496 ocp_reg_write(tp, 0xd068, data);
7497 swap_b = ocp_reg_read(tp, 0xd06a);
7498 data &= ~0x18; /* p0 */
7499 ocp_reg_write(tp, 0xd068, data);
7500 ocp_reg_write(tp, 0xd06a,
7501 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7502 data |= 0x18; /* p3 */
7503 ocp_reg_write(tp, 0xd068, data);
7504 ocp_reg_write(tp, 0xd06a,
7505 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7507 data |= 0x08; /* p1 */
7508 ocp_reg_write(tp, 0xd068, data);
7509 swap_a = ocp_reg_read(tp, 0xd06a);
7511 data |= 0x10; /* p2 */
7512 ocp_reg_write(tp, 0xd068, data);
7513 swap_b = ocp_reg_read(tp, 0xd06a);
7515 data |= 0x08; /* p1 */
7516 ocp_reg_write(tp, 0xd068, data);
7517 ocp_reg_write(tp, 0xd06a,
7518 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7520 data |= 0x10; /* p2 */
7521 ocp_reg_write(tp, 0xd068, data);
7522 ocp_reg_write(tp, 0xd06a,
7523 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7524 swap_a = ocp_reg_read(tp, 0xbd5a);
7525 swap_b = ocp_reg_read(tp, 0xbd5c);
7526 ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) |
7527 ((swap_b & 0x1f) << 8) |
7528 ((swap_b >> 8) & 0x1f));
7529 ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) |
7530 ((swap_a & 0x1f) << 8) |
7531 ((swap_a >> 8) & 0x1f));
7532 swap_a = ocp_reg_read(tp, 0xbc18);
7533 swap_b = ocp_reg_read(tp, 0xbc1a);
7534 ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) |
7535 ((swap_b & 0x1f) << 8) |
7536 ((swap_b >> 8) & 0x1f));
7537 ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) |
7538 ((swap_a & 0x1f) << 8) |
7539 ((swap_a >> 8) & 0x1f));
7546 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7548 data = ocp_reg_read(tp, 0xa428);
7550 ocp_reg_write(tp, 0xa428, data);
7551 data = ocp_reg_read(tp, 0xa5ea);
7553 ocp_reg_write(tp, 0xa5ea, data);
7554 tp->ups_info.lite_mode = 0;
7557 rtl_eee_enable(tp, true);
7559 r8153_aldps_en(tp, true);
7560 r8152b_enable_fc(tp);
7561 r8153_u2p3en(tp, true);
7563 set_bit(PHY_RESET, &tp->flags);
7566 static void r8156b_hw_phy_cfg(struct r8152 *tp)
7571 switch (tp->version) {
7573 ocp_reg_write(tp, 0xbf86, 0x9000);
7574 data = ocp_reg_read(tp, 0xc402);
7576 ocp_reg_write(tp, 0xc402, data);
7578 ocp_reg_write(tp, 0xc402, data);
7579 ocp_reg_write(tp, 0xbd86, 0x1010);
7580 ocp_reg_write(tp, 0xbd88, 0x1010);
7581 data = ocp_reg_read(tp, 0xbd4e);
7582 data &= ~(BIT(10) | BIT(11));
7584 ocp_reg_write(tp, 0xbd4e, data);
7585 data = ocp_reg_read(tp, 0xbf46);
7588 ocp_reg_write(tp, 0xbf46, data);
7592 r8156b_wait_loading_flash(tp);
7598 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7599 if (ocp_data & PCUT_STATUS) {
7600 ocp_data &= ~PCUT_STATUS;
7601 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7604 data = r8153_phy_status(tp, 0);
7606 case PHY_STAT_EXT_INIT:
7607 rtl8152_apply_firmware(tp, true);
7609 data = ocp_reg_read(tp, 0xa466);
7611 ocp_reg_write(tp, 0xa466, data);
7613 data = ocp_reg_read(tp, 0xa468);
7614 data &= ~(BIT(3) | BIT(1));
7615 ocp_reg_write(tp, 0xa468, data);
7617 case PHY_STAT_LAN_ON:
7618 case PHY_STAT_PWRDN:
7620 rtl8152_apply_firmware(tp, false);
7624 data = r8152_mdio_read(tp, MII_BMCR);
7625 if (data & BMCR_PDOWN) {
7626 data &= ~BMCR_PDOWN;
7627 r8152_mdio_write(tp, MII_BMCR, data);
7630 /* disable ALDPS before updating the PHY parameters */
7631 r8153_aldps_en(tp, false);
7633 /* disable EEE before updating the PHY parameters */
7634 rtl_eee_enable(tp, false);
7636 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7637 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7639 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7640 ocp_data |= PFM_PWM_SWITCH;
7641 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7643 switch (tp->version) {
7645 data = ocp_reg_read(tp, 0xbc08);
7646 data |= BIT(3) | BIT(2);
7647 ocp_reg_write(tp, 0xbc08, data);
7649 data = sram_read(tp, 0x8fff);
7652 sram_write(tp, 0x8fff, data);
7654 data = ocp_reg_read(tp, 0xacda);
7656 ocp_reg_write(tp, 0xacda, data);
7657 data = ocp_reg_read(tp, 0xacde);
7659 ocp_reg_write(tp, 0xacde, data);
7660 ocp_reg_write(tp, 0xac8c, 0x0ffc);
7661 ocp_reg_write(tp, 0xac46, 0xb7b4);
7662 ocp_reg_write(tp, 0xac50, 0x0fbc);
7663 ocp_reg_write(tp, 0xac3c, 0x9240);
7664 ocp_reg_write(tp, 0xac4e, 0x0db4);
7665 ocp_reg_write(tp, 0xacc6, 0x0707);
7666 ocp_reg_write(tp, 0xacc8, 0xa0d3);
7667 ocp_reg_write(tp, 0xad08, 0x0007);
7669 ocp_reg_write(tp, 0xb87c, 0x8560);
7670 ocp_reg_write(tp, 0xb87e, 0x19cc);
7671 ocp_reg_write(tp, 0xb87c, 0x8562);
7672 ocp_reg_write(tp, 0xb87e, 0x19cc);
7673 ocp_reg_write(tp, 0xb87c, 0x8564);
7674 ocp_reg_write(tp, 0xb87e, 0x19cc);
7675 ocp_reg_write(tp, 0xb87c, 0x8566);
7676 ocp_reg_write(tp, 0xb87e, 0x147d);
7677 ocp_reg_write(tp, 0xb87c, 0x8568);
7678 ocp_reg_write(tp, 0xb87e, 0x147d);
7679 ocp_reg_write(tp, 0xb87c, 0x856a);
7680 ocp_reg_write(tp, 0xb87e, 0x147d);
7681 ocp_reg_write(tp, 0xb87c, 0x8ffe);
7682 ocp_reg_write(tp, 0xb87e, 0x0907);
7683 ocp_reg_write(tp, 0xb87c, 0x80d6);
7684 ocp_reg_write(tp, 0xb87e, 0x2801);
7685 ocp_reg_write(tp, 0xb87c, 0x80f2);
7686 ocp_reg_write(tp, 0xb87e, 0x2801);
7687 ocp_reg_write(tp, 0xb87c, 0x80f4);
7688 ocp_reg_write(tp, 0xb87e, 0x6077);
7689 ocp_reg_write(tp, 0xb506, 0x01e7);
7691 ocp_reg_write(tp, 0xb87c, 0x8013);
7692 ocp_reg_write(tp, 0xb87e, 0x0700);
7693 ocp_reg_write(tp, 0xb87c, 0x8fb9);
7694 ocp_reg_write(tp, 0xb87e, 0x2801);
7695 ocp_reg_write(tp, 0xb87c, 0x8fba);
7696 ocp_reg_write(tp, 0xb87e, 0x0100);
7697 ocp_reg_write(tp, 0xb87c, 0x8fbc);
7698 ocp_reg_write(tp, 0xb87e, 0x1900);
7699 ocp_reg_write(tp, 0xb87c, 0x8fbe);
7700 ocp_reg_write(tp, 0xb87e, 0xe100);
7701 ocp_reg_write(tp, 0xb87c, 0x8fc0);
7702 ocp_reg_write(tp, 0xb87e, 0x0800);
7703 ocp_reg_write(tp, 0xb87c, 0x8fc2);
7704 ocp_reg_write(tp, 0xb87e, 0xe500);
7705 ocp_reg_write(tp, 0xb87c, 0x8fc4);
7706 ocp_reg_write(tp, 0xb87e, 0x0f00);
7707 ocp_reg_write(tp, 0xb87c, 0x8fc6);
7708 ocp_reg_write(tp, 0xb87e, 0xf100);
7709 ocp_reg_write(tp, 0xb87c, 0x8fc8);
7710 ocp_reg_write(tp, 0xb87e, 0x0400);
7711 ocp_reg_write(tp, 0xb87c, 0x8fca);
7712 ocp_reg_write(tp, 0xb87e, 0xf300);
7713 ocp_reg_write(tp, 0xb87c, 0x8fcc);
7714 ocp_reg_write(tp, 0xb87e, 0xfd00);
7715 ocp_reg_write(tp, 0xb87c, 0x8fce);
7716 ocp_reg_write(tp, 0xb87e, 0xff00);
7717 ocp_reg_write(tp, 0xb87c, 0x8fd0);
7718 ocp_reg_write(tp, 0xb87e, 0xfb00);
7719 ocp_reg_write(tp, 0xb87c, 0x8fd2);
7720 ocp_reg_write(tp, 0xb87e, 0x0100);
7721 ocp_reg_write(tp, 0xb87c, 0x8fd4);
7722 ocp_reg_write(tp, 0xb87e, 0xf400);
7723 ocp_reg_write(tp, 0xb87c, 0x8fd6);
7724 ocp_reg_write(tp, 0xb87e, 0xff00);
7725 ocp_reg_write(tp, 0xb87c, 0x8fd8);
7726 ocp_reg_write(tp, 0xb87e, 0xf600);
7728 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7729 ocp_data |= EN_XG_LIP | EN_G_LIP;
7730 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7731 ocp_reg_write(tp, 0xb87c, 0x813d);
7732 ocp_reg_write(tp, 0xb87e, 0x390e);
7733 ocp_reg_write(tp, 0xb87c, 0x814f);
7734 ocp_reg_write(tp, 0xb87e, 0x790e);
7735 ocp_reg_write(tp, 0xb87c, 0x80b0);
7736 ocp_reg_write(tp, 0xb87e, 0x0f31);
7737 data = ocp_reg_read(tp, 0xbf4c);
7739 ocp_reg_write(tp, 0xbf4c, data);
7740 data = ocp_reg_read(tp, 0xbcca);
7741 data |= BIT(9) | BIT(8);
7742 ocp_reg_write(tp, 0xbcca, data);
7743 ocp_reg_write(tp, 0xb87c, 0x8141);
7744 ocp_reg_write(tp, 0xb87e, 0x320e);
7745 ocp_reg_write(tp, 0xb87c, 0x8153);
7746 ocp_reg_write(tp, 0xb87e, 0x720e);
7747 ocp_reg_write(tp, 0xb87c, 0x8529);
7748 ocp_reg_write(tp, 0xb87e, 0x050e);
7749 data = ocp_reg_read(tp, OCP_EEE_CFG);
7750 data &= ~CTAP_SHORT_EN;
7751 ocp_reg_write(tp, OCP_EEE_CFG, data);
7753 sram_write(tp, 0x816c, 0xc4a0);
7754 sram_write(tp, 0x8170, 0xc4a0);
7755 sram_write(tp, 0x8174, 0x04a0);
7756 sram_write(tp, 0x8178, 0x04a0);
7757 sram_write(tp, 0x817c, 0x0719);
7758 sram_write(tp, 0x8ff4, 0x0400);
7759 sram_write(tp, 0x8ff1, 0x0404);
7761 ocp_reg_write(tp, 0xbf4a, 0x001b);
7762 ocp_reg_write(tp, 0xb87c, 0x8033);
7763 ocp_reg_write(tp, 0xb87e, 0x7c13);
7764 ocp_reg_write(tp, 0xb87c, 0x8037);
7765 ocp_reg_write(tp, 0xb87e, 0x7c13);
7766 ocp_reg_write(tp, 0xb87c, 0x803b);
7767 ocp_reg_write(tp, 0xb87e, 0xfc32);
7768 ocp_reg_write(tp, 0xb87c, 0x803f);
7769 ocp_reg_write(tp, 0xb87e, 0x7c13);
7770 ocp_reg_write(tp, 0xb87c, 0x8043);
7771 ocp_reg_write(tp, 0xb87e, 0x7c13);
7772 ocp_reg_write(tp, 0xb87c, 0x8047);
7773 ocp_reg_write(tp, 0xb87e, 0x7c13);
7775 ocp_reg_write(tp, 0xb87c, 0x8145);
7776 ocp_reg_write(tp, 0xb87e, 0x370e);
7777 ocp_reg_write(tp, 0xb87c, 0x8157);
7778 ocp_reg_write(tp, 0xb87e, 0x770e);
7779 ocp_reg_write(tp, 0xb87c, 0x8169);
7780 ocp_reg_write(tp, 0xb87e, 0x0d0a);
7781 ocp_reg_write(tp, 0xb87c, 0x817b);
7782 ocp_reg_write(tp, 0xb87e, 0x1d0a);
7784 data = sram_read(tp, 0x8217);
7787 sram_write(tp, 0x8217, data);
7788 data = sram_read(tp, 0x821a);
7791 sram_write(tp, 0x821a, data);
7792 sram_write(tp, 0x80da, 0x0403);
7793 data = sram_read(tp, 0x80dc);
7796 sram_write(tp, 0x80dc, data);
7797 sram_write(tp, 0x80b3, 0x0384);
7798 sram_write(tp, 0x80b7, 0x2007);
7799 data = sram_read(tp, 0x80ba);
7802 sram_write(tp, 0x80ba, data);
7803 sram_write(tp, 0x80b5, 0xf009);
7804 data = sram_read(tp, 0x80bd);
7807 sram_write(tp, 0x80bd, data);
7808 sram_write(tp, 0x80c7, 0xf083);
7809 sram_write(tp, 0x80dd, 0x03f0);
7810 data = sram_read(tp, 0x80df);
7813 sram_write(tp, 0x80df, data);
7814 sram_write(tp, 0x80cb, 0x2007);
7815 data = sram_read(tp, 0x80ce);
7818 sram_write(tp, 0x80ce, data);
7819 sram_write(tp, 0x80c9, 0x8009);
7820 data = sram_read(tp, 0x80d1);
7823 sram_write(tp, 0x80d1, data);
7824 sram_write(tp, 0x80a3, 0x200a);
7825 sram_write(tp, 0x80a5, 0xf0ad);
7826 sram_write(tp, 0x809f, 0x6073);
7827 sram_write(tp, 0x80a1, 0x000b);
7828 data = sram_read(tp, 0x80a9);
7831 sram_write(tp, 0x80a9, data);
7833 if (rtl_phy_patch_request(tp, true, true))
7836 data = ocp_reg_read(tp, 0xb896);
7838 ocp_reg_write(tp, 0xb896, data);
7839 data = ocp_reg_read(tp, 0xb892);
7841 ocp_reg_write(tp, 0xb892, data);
7842 ocp_reg_write(tp, 0xb88e, 0xc23e);
7843 ocp_reg_write(tp, 0xb890, 0x0000);
7844 ocp_reg_write(tp, 0xb88e, 0xc240);
7845 ocp_reg_write(tp, 0xb890, 0x0103);
7846 ocp_reg_write(tp, 0xb88e, 0xc242);
7847 ocp_reg_write(tp, 0xb890, 0x0507);
7848 ocp_reg_write(tp, 0xb88e, 0xc244);
7849 ocp_reg_write(tp, 0xb890, 0x090b);
7850 ocp_reg_write(tp, 0xb88e, 0xc246);
7851 ocp_reg_write(tp, 0xb890, 0x0c0e);
7852 ocp_reg_write(tp, 0xb88e, 0xc248);
7853 ocp_reg_write(tp, 0xb890, 0x1012);
7854 ocp_reg_write(tp, 0xb88e, 0xc24a);
7855 ocp_reg_write(tp, 0xb890, 0x1416);
7856 data = ocp_reg_read(tp, 0xb896);
7858 ocp_reg_write(tp, 0xb896, data);
7860 rtl_phy_patch_request(tp, false, true);
7862 data = ocp_reg_read(tp, 0xa86a);
7864 ocp_reg_write(tp, 0xa86a, data);
7865 data = ocp_reg_read(tp, 0xa6f0);
7867 ocp_reg_write(tp, 0xa6f0, data);
7869 ocp_reg_write(tp, 0xbfa0, 0xd70d);
7870 ocp_reg_write(tp, 0xbfa2, 0x4100);
7871 ocp_reg_write(tp, 0xbfa4, 0xe868);
7872 ocp_reg_write(tp, 0xbfa6, 0xdc59);
7873 ocp_reg_write(tp, 0xb54c, 0x3c18);
7874 data = ocp_reg_read(tp, 0xbfa4);
7876 ocp_reg_write(tp, 0xbfa4, data);
7877 data = sram_read(tp, 0x817d);
7879 sram_write(tp, 0x817d, data);
7883 data = ocp_reg_read(tp, 0xac46);
7886 ocp_reg_write(tp, 0xac46, data);
7887 data = ocp_reg_read(tp, 0xad30);
7890 ocp_reg_write(tp, 0xad30, data);
7894 ocp_reg_write(tp, 0xb87c, 0x80f5);
7895 ocp_reg_write(tp, 0xb87e, 0x760e);
7896 ocp_reg_write(tp, 0xb87c, 0x8107);
7897 ocp_reg_write(tp, 0xb87e, 0x360e);
7898 ocp_reg_write(tp, 0xb87c, 0x8551);
7899 data = ocp_reg_read(tp, 0xb87e);
7902 ocp_reg_write(tp, 0xb87e, data);
7904 /* ADC_PGA parameter */
7905 data = ocp_reg_read(tp, 0xbf00);
7908 ocp_reg_write(tp, 0xbf00, data);
7909 data = ocp_reg_read(tp, 0xbf46);
7912 ocp_reg_write(tp, 0xbf46, data);
7914 /* Green Table-PGA, 1G full viterbi */
7915 sram_write(tp, 0x8044, 0x2417);
7916 sram_write(tp, 0x804a, 0x2417);
7917 sram_write(tp, 0x8050, 0x2417);
7918 sram_write(tp, 0x8056, 0x2417);
7919 sram_write(tp, 0x805c, 0x2417);
7920 sram_write(tp, 0x8062, 0x2417);
7921 sram_write(tp, 0x8068, 0x2417);
7922 sram_write(tp, 0x806e, 0x2417);
7923 sram_write(tp, 0x8074, 0x2417);
7924 sram_write(tp, 0x807a, 0x2417);
7927 data = ocp_reg_read(tp, 0xbf84);
7930 ocp_reg_write(tp, 0xbf84, data);
7936 if (rtl_phy_patch_request(tp, true, true))
7939 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7940 ocp_data |= EEE_SPDWN_EN;
7941 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7943 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7944 data &= ~(EN_EEE_100 | EN_EEE_1000);
7945 data |= EN_10M_CLKDIV;
7946 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7947 tp->ups_info._10m_ckdiv = true;
7948 tp->ups_info.eee_plloff_100 = false;
7949 tp->ups_info.eee_plloff_giga = false;
7951 data = ocp_reg_read(tp, OCP_POWER_CFG);
7952 data &= ~EEE_CLKDIV_EN;
7953 ocp_reg_write(tp, OCP_POWER_CFG, data);
7954 tp->ups_info.eee_ckdiv = false;
7956 rtl_phy_patch_request(tp, false, true);
7958 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7960 data = ocp_reg_read(tp, 0xa428);
7962 ocp_reg_write(tp, 0xa428, data);
7963 data = ocp_reg_read(tp, 0xa5ea);
7965 ocp_reg_write(tp, 0xa5ea, data);
7966 tp->ups_info.lite_mode = 0;
7969 rtl_eee_enable(tp, true);
7971 r8153_aldps_en(tp, true);
7972 r8152b_enable_fc(tp);
7973 r8153_u2p3en(tp, true);
7975 set_bit(PHY_RESET, &tp->flags);
7978 static void r8156_init(struct r8152 *tp)
7984 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7987 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
7988 ocp_data &= ~EN_ALL_SPEED;
7989 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
7991 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
7993 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
7994 ocp_data |= BYPASS_MAC_RESET;
7995 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
7997 r8153b_u1u2en(tp, false);
7999 for (i = 0; i < 500; i++) {
8000 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
8005 if (test_bit(RTL8152_UNPLUG, &tp->flags))
8009 data = r8153_phy_status(tp, 0);
8010 if (data == PHY_STAT_EXT_INIT) {
8011 data = ocp_reg_read(tp, 0xa468);
8012 data &= ~(BIT(3) | BIT(1));
8013 ocp_reg_write(tp, 0xa468, data);
8016 data = r8152_mdio_read(tp, MII_BMCR);
8017 if (data & BMCR_PDOWN) {
8018 data &= ~BMCR_PDOWN;
8019 r8152_mdio_write(tp, MII_BMCR, data);
8022 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8023 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
8025 r8153_u2p3en(tp, false);
8027 /* MSC timer = 0xfff * 8ms = 32760 ms */
8028 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8030 /* U1/U2/L1 idle timer. 500 us */
8031 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8033 r8153b_power_cut_en(tp, false);
8034 r8156_ups_en(tp, false);
8035 r8153_queue_wake(tp, false);
8036 rtl_runtime_suspend_enable(tp, false);
8038 if (tp->udev->speed >= USB_SPEED_SUPER)
8039 r8153b_u1u2en(tp, true);
8041 usb_enable_lpm(tp->udev);
8043 r8156_mac_clk_spd(tp, true);
8045 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8046 ocp_data &= ~PLA_MCU_SPDWN_EN;
8047 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8049 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8050 if (rtl8152_get_speed(tp) & LINK_STATUS)
8051 ocp_data |= CUR_LINK_OK;
8053 ocp_data &= ~CUR_LINK_OK;
8054 ocp_data |= POLL_LINK_CHG;
8055 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8057 set_bit(GREEN_ETHERNET, &tp->flags);
8059 /* rx aggregation */
8060 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8061 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8062 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8064 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
8065 ocp_data |= ACT_ODMA;
8066 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
8068 r8156_mdio_force_mode(tp);
8069 rtl_tally_reset(tp);
8071 tp->coalesce = 15000; /* 15 us */
8074 static void r8156b_init(struct r8152 *tp)
8080 if (test_bit(RTL8152_UNPLUG, &tp->flags))
8083 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
8084 ocp_data &= ~EN_ALL_SPEED;
8085 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
8087 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
8089 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
8090 ocp_data |= BYPASS_MAC_RESET;
8091 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
8093 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
8094 ocp_data |= RX_DETECT8;
8095 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
8097 r8153b_u1u2en(tp, false);
8099 switch (tp->version) {
8102 r8156b_wait_loading_flash(tp);
8108 for (i = 0; i < 500; i++) {
8109 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
8114 if (test_bit(RTL8152_UNPLUG, &tp->flags))
8118 data = r8153_phy_status(tp, 0);
8119 if (data == PHY_STAT_EXT_INIT) {
8120 data = ocp_reg_read(tp, 0xa468);
8121 data &= ~(BIT(3) | BIT(1));
8122 ocp_reg_write(tp, 0xa468, data);
8124 data = ocp_reg_read(tp, 0xa466);
8126 ocp_reg_write(tp, 0xa466, data);
8129 data = r8152_mdio_read(tp, MII_BMCR);
8130 if (data & BMCR_PDOWN) {
8131 data &= ~BMCR_PDOWN;
8132 r8152_mdio_write(tp, MII_BMCR, data);
8135 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8137 r8153_u2p3en(tp, false);
8139 /* MSC timer = 0xfff * 8ms = 32760 ms */
8140 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8142 /* U1/U2/L1 idle timer. 500 us */
8143 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8145 r8153b_power_cut_en(tp, false);
8146 r8156_ups_en(tp, false);
8147 r8153_queue_wake(tp, false);
8148 rtl_runtime_suspend_enable(tp, false);
8150 if (tp->udev->speed >= USB_SPEED_SUPER)
8151 r8153b_u1u2en(tp, true);
8153 usb_enable_lpm(tp->udev);
8155 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR);
8156 ocp_data &= ~SLOT_EN;
8157 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8159 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
8160 ocp_data |= FLOW_CTRL_EN;
8161 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
8163 /* enable fc timer and set timer to 600 ms. */
8164 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
8165 CTRL_TIMER_EN | (600 / 8));
8167 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
8168 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN))
8169 ocp_data |= FLOW_CTRL_PATCH_2;
8170 ocp_data &= ~AUTO_SPEEDUP;
8171 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
8173 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
8174 ocp_data |= FC_PATCH_TASK;
8175 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
8177 r8156_mac_clk_spd(tp, true);
8179 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8180 ocp_data &= ~PLA_MCU_SPDWN_EN;
8181 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8183 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8184 if (rtl8152_get_speed(tp) & LINK_STATUS)
8185 ocp_data |= CUR_LINK_OK;
8187 ocp_data &= ~CUR_LINK_OK;
8188 ocp_data |= POLL_LINK_CHG;
8189 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8191 set_bit(GREEN_ETHERNET, &tp->flags);
8193 /* rx aggregation */
8194 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8195 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8196 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8198 r8156_mdio_force_mode(tp);
8199 rtl_tally_reset(tp);
8201 tp->coalesce = 15000; /* 15 us */
8204 static bool rtl_check_vendor_ok(struct usb_interface *intf)
8206 struct usb_host_interface *alt = intf->cur_altsetting;
8207 struct usb_endpoint_descriptor *in, *out, *intr;
8209 if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) {
8210 dev_err(&intf->dev, "Expected endpoints are not found\n");
8214 /* Check Rx endpoint address */
8215 if (usb_endpoint_num(in) != 1) {
8216 dev_err(&intf->dev, "Invalid Rx endpoint address\n");
8220 /* Check Tx endpoint address */
8221 if (usb_endpoint_num(out) != 2) {
8222 dev_err(&intf->dev, "Invalid Tx endpoint address\n");
8226 /* Check interrupt endpoint address */
8227 if (usb_endpoint_num(intr) != 3) {
8228 dev_err(&intf->dev, "Invalid interrupt endpoint address\n");
8235 static int rtl8152_pre_reset(struct usb_interface *intf)
8237 struct r8152 *tp = usb_get_intfdata(intf);
8238 struct net_device *netdev;
8243 netdev = tp->netdev;
8244 if (!netif_running(netdev))
8247 netif_stop_queue(netdev);
8248 tasklet_disable(&tp->tx_tl);
8249 clear_bit(WORK_ENABLE, &tp->flags);
8250 usb_kill_urb(tp->intr_urb);
8251 cancel_delayed_work_sync(&tp->schedule);
8252 napi_disable(&tp->napi);
8253 if (netif_carrier_ok(netdev)) {
8254 mutex_lock(&tp->control);
8255 tp->rtl_ops.disable(tp);
8256 mutex_unlock(&tp->control);
8262 static int rtl8152_post_reset(struct usb_interface *intf)
8264 struct r8152 *tp = usb_get_intfdata(intf);
8265 struct net_device *netdev;
8271 /* reset the MAC address in case of policy change */
8272 if (determine_ethernet_addr(tp, &sa) >= 0) {
8274 dev_set_mac_address (tp->netdev, &sa, NULL);
8278 netdev = tp->netdev;
8279 if (!netif_running(netdev))
8282 set_bit(WORK_ENABLE, &tp->flags);
8283 if (netif_carrier_ok(netdev)) {
8284 mutex_lock(&tp->control);
8285 tp->rtl_ops.enable(tp);
8287 _rtl8152_set_rx_mode(netdev);
8288 mutex_unlock(&tp->control);
8291 napi_enable(&tp->napi);
8292 tasklet_enable(&tp->tx_tl);
8293 netif_wake_queue(netdev);
8294 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
8296 if (!list_empty(&tp->rx_done))
8297 napi_schedule(&tp->napi);
8302 static bool delay_autosuspend(struct r8152 *tp)
8304 bool sw_linking = !!netif_carrier_ok(tp->netdev);
8305 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
8307 /* This means a linking change occurs and the driver doesn't detect it,
8308 * yet. If the driver has disabled tx/rx and hw is linking on, the
8309 * device wouldn't wake up by receiving any packet.
8311 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
8314 /* If the linking down is occurred by nway, the device may miss the
8315 * linking change event. And it wouldn't wake when linking on.
8317 if (!sw_linking && tp->rtl_ops.in_nway(tp))
8319 else if (!skb_queue_empty(&tp->tx_queue))
8325 static int rtl8152_runtime_resume(struct r8152 *tp)
8327 struct net_device *netdev = tp->netdev;
8329 if (netif_running(netdev) && netdev->flags & IFF_UP) {
8330 struct napi_struct *napi = &tp->napi;
8332 tp->rtl_ops.autosuspend_en(tp, false);
8334 set_bit(WORK_ENABLE, &tp->flags);
8336 if (netif_carrier_ok(netdev)) {
8337 if (rtl8152_get_speed(tp) & LINK_STATUS) {
8340 netif_carrier_off(netdev);
8341 tp->rtl_ops.disable(tp);
8342 netif_info(tp, link, netdev, "linking down\n");
8347 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8348 smp_mb__after_atomic();
8350 if (!list_empty(&tp->rx_done))
8351 napi_schedule(&tp->napi);
8353 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8355 if (netdev->flags & IFF_UP)
8356 tp->rtl_ops.autosuspend_en(tp, false);
8358 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8364 static int rtl8152_system_resume(struct r8152 *tp)
8366 struct net_device *netdev = tp->netdev;
8368 netif_device_attach(netdev);
8370 if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
8372 netif_carrier_off(netdev);
8373 set_bit(WORK_ENABLE, &tp->flags);
8374 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8380 static int rtl8152_runtime_suspend(struct r8152 *tp)
8382 struct net_device *netdev = tp->netdev;
8385 if (!tp->rtl_ops.autosuspend_en)
8388 set_bit(SELECTIVE_SUSPEND, &tp->flags);
8389 smp_mb__after_atomic();
8391 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8394 if (netif_carrier_ok(netdev)) {
8397 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
8398 ocp_data = rcr & ~RCR_ACPT_ALL;
8399 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8400 rxdy_gated_en(tp, true);
8401 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
8403 if (!(ocp_data & RXFIFO_EMPTY)) {
8404 rxdy_gated_en(tp, false);
8405 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8406 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8407 smp_mb__after_atomic();
8413 clear_bit(WORK_ENABLE, &tp->flags);
8414 usb_kill_urb(tp->intr_urb);
8416 tp->rtl_ops.autosuspend_en(tp, true);
8418 if (netif_carrier_ok(netdev)) {
8419 struct napi_struct *napi = &tp->napi;
8423 rxdy_gated_en(tp, false);
8424 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8428 if (delay_autosuspend(tp)) {
8429 rtl8152_runtime_resume(tp);
8438 static int rtl8152_system_suspend(struct r8152 *tp)
8440 struct net_device *netdev = tp->netdev;
8442 netif_device_detach(netdev);
8444 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8445 struct napi_struct *napi = &tp->napi;
8447 clear_bit(WORK_ENABLE, &tp->flags);
8448 usb_kill_urb(tp->intr_urb);
8449 tasklet_disable(&tp->tx_tl);
8451 cancel_delayed_work_sync(&tp->schedule);
8452 tp->rtl_ops.down(tp);
8454 tasklet_enable(&tp->tx_tl);
8460 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
8462 struct r8152 *tp = usb_get_intfdata(intf);
8465 mutex_lock(&tp->control);
8467 if (PMSG_IS_AUTO(message))
8468 ret = rtl8152_runtime_suspend(tp);
8470 ret = rtl8152_system_suspend(tp);
8472 mutex_unlock(&tp->control);
8477 static int rtl8152_resume(struct usb_interface *intf)
8479 struct r8152 *tp = usb_get_intfdata(intf);
8482 mutex_lock(&tp->control);
8484 rtl_reset_ocp_base(tp);
8486 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
8487 ret = rtl8152_runtime_resume(tp);
8489 ret = rtl8152_system_resume(tp);
8491 mutex_unlock(&tp->control);
8496 static int rtl8152_reset_resume(struct usb_interface *intf)
8498 struct r8152 *tp = usb_get_intfdata(intf);
8500 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8501 rtl_reset_ocp_base(tp);
8502 tp->rtl_ops.init(tp);
8503 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
8504 set_ethernet_addr(tp, true);
8505 return rtl8152_resume(intf);
8508 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8510 struct r8152 *tp = netdev_priv(dev);
8512 if (usb_autopm_get_interface(tp->intf) < 0)
8515 if (!rtl_can_wakeup(tp)) {
8519 mutex_lock(&tp->control);
8520 wol->supported = WAKE_ANY;
8521 wol->wolopts = __rtl_get_wol(tp);
8522 mutex_unlock(&tp->control);
8525 usb_autopm_put_interface(tp->intf);
8528 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8530 struct r8152 *tp = netdev_priv(dev);
8533 if (!rtl_can_wakeup(tp))
8536 if (wol->wolopts & ~WAKE_ANY)
8539 ret = usb_autopm_get_interface(tp->intf);
8543 mutex_lock(&tp->control);
8545 __rtl_set_wol(tp, wol->wolopts);
8546 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
8548 mutex_unlock(&tp->control);
8550 usb_autopm_put_interface(tp->intf);
8556 static u32 rtl8152_get_msglevel(struct net_device *dev)
8558 struct r8152 *tp = netdev_priv(dev);
8560 return tp->msg_enable;
8563 static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
8565 struct r8152 *tp = netdev_priv(dev);
8567 tp->msg_enable = value;
8570 static void rtl8152_get_drvinfo(struct net_device *netdev,
8571 struct ethtool_drvinfo *info)
8573 struct r8152 *tp = netdev_priv(netdev);
8575 strscpy(info->driver, MODULENAME, sizeof(info->driver));
8576 strscpy(info->version, DRIVER_VERSION, sizeof(info->version));
8577 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
8578 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
8579 strscpy(info->fw_version, tp->rtl_fw.version,
8580 sizeof(info->fw_version));
8584 int rtl8152_get_link_ksettings(struct net_device *netdev,
8585 struct ethtool_link_ksettings *cmd)
8587 struct r8152 *tp = netdev_priv(netdev);
8590 if (!tp->mii.mdio_read)
8593 ret = usb_autopm_get_interface(tp->intf);
8597 mutex_lock(&tp->control);
8599 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
8601 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8602 cmd->link_modes.supported, tp->support_2500full);
8604 if (tp->support_2500full) {
8605 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8606 cmd->link_modes.advertising,
8607 ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G);
8609 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8610 cmd->link_modes.lp_advertising,
8611 ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G);
8613 if (is_speed_2500(rtl8152_get_speed(tp)))
8614 cmd->base.speed = SPEED_2500;
8617 mutex_unlock(&tp->control);
8619 usb_autopm_put_interface(tp->intf);
8625 static int rtl8152_set_link_ksettings(struct net_device *dev,
8626 const struct ethtool_link_ksettings *cmd)
8628 struct r8152 *tp = netdev_priv(dev);
8629 u32 advertising = 0;
8632 ret = usb_autopm_get_interface(tp->intf);
8636 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
8637 cmd->link_modes.advertising))
8638 advertising |= RTL_ADVERTISED_10_HALF;
8640 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
8641 cmd->link_modes.advertising))
8642 advertising |= RTL_ADVERTISED_10_FULL;
8644 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
8645 cmd->link_modes.advertising))
8646 advertising |= RTL_ADVERTISED_100_HALF;
8648 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
8649 cmd->link_modes.advertising))
8650 advertising |= RTL_ADVERTISED_100_FULL;
8652 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
8653 cmd->link_modes.advertising))
8654 advertising |= RTL_ADVERTISED_1000_HALF;
8656 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
8657 cmd->link_modes.advertising))
8658 advertising |= RTL_ADVERTISED_1000_FULL;
8660 if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8661 cmd->link_modes.advertising))
8662 advertising |= RTL_ADVERTISED_2500_FULL;
8664 mutex_lock(&tp->control);
8666 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
8667 cmd->base.duplex, advertising);
8669 tp->autoneg = cmd->base.autoneg;
8670 tp->speed = cmd->base.speed;
8671 tp->duplex = cmd->base.duplex;
8672 tp->advertising = advertising;
8675 mutex_unlock(&tp->control);
8677 usb_autopm_put_interface(tp->intf);
8683 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
8690 "tx_single_collisions",
8691 "tx_multi_collisions",
8699 static int rtl8152_get_sset_count(struct net_device *dev, int sset)
8703 return ARRAY_SIZE(rtl8152_gstrings);
8709 static void rtl8152_get_ethtool_stats(struct net_device *dev,
8710 struct ethtool_stats *stats, u64 *data)
8712 struct r8152 *tp = netdev_priv(dev);
8713 struct tally_counter tally;
8715 if (usb_autopm_get_interface(tp->intf) < 0)
8718 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
8720 usb_autopm_put_interface(tp->intf);
8722 data[0] = le64_to_cpu(tally.tx_packets);
8723 data[1] = le64_to_cpu(tally.rx_packets);
8724 data[2] = le64_to_cpu(tally.tx_errors);
8725 data[3] = le32_to_cpu(tally.rx_errors);
8726 data[4] = le16_to_cpu(tally.rx_missed);
8727 data[5] = le16_to_cpu(tally.align_errors);
8728 data[6] = le32_to_cpu(tally.tx_one_collision);
8729 data[7] = le32_to_cpu(tally.tx_multi_collision);
8730 data[8] = le64_to_cpu(tally.rx_unicast);
8731 data[9] = le64_to_cpu(tally.rx_broadcast);
8732 data[10] = le32_to_cpu(tally.rx_multicast);
8733 data[11] = le16_to_cpu(tally.tx_aborted);
8734 data[12] = le16_to_cpu(tally.tx_underrun);
8737 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
8739 switch (stringset) {
8741 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings));
8746 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8748 u32 lp, adv, supported = 0;
8751 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
8752 supported = mmd_eee_cap_to_ethtool_sup_t(val);
8754 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
8755 adv = mmd_eee_adv_to_ethtool_adv_t(val);
8757 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
8758 lp = mmd_eee_adv_to_ethtool_adv_t(val);
8760 eee->eee_enabled = tp->eee_en;
8761 eee->eee_active = !!(supported & adv & lp);
8762 eee->supported = supported;
8763 eee->advertised = tp->eee_adv;
8764 eee->lp_advertised = lp;
8769 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
8771 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
8773 tp->eee_en = eee->eee_enabled;
8776 rtl_eee_enable(tp, tp->eee_en);
8781 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8783 u32 lp, adv, supported = 0;
8786 val = ocp_reg_read(tp, OCP_EEE_ABLE);
8787 supported = mmd_eee_cap_to_ethtool_sup_t(val);
8789 val = ocp_reg_read(tp, OCP_EEE_ADV);
8790 adv = mmd_eee_adv_to_ethtool_adv_t(val);
8792 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
8793 lp = mmd_eee_adv_to_ethtool_adv_t(val);
8795 eee->eee_enabled = tp->eee_en;
8796 eee->eee_active = !!(supported & adv & lp);
8797 eee->supported = supported;
8798 eee->advertised = tp->eee_adv;
8799 eee->lp_advertised = lp;
8805 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
8807 struct r8152 *tp = netdev_priv(net);
8810 if (!tp->rtl_ops.eee_get) {
8815 ret = usb_autopm_get_interface(tp->intf);
8819 mutex_lock(&tp->control);
8821 ret = tp->rtl_ops.eee_get(tp, edata);
8823 mutex_unlock(&tp->control);
8825 usb_autopm_put_interface(tp->intf);
8832 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
8834 struct r8152 *tp = netdev_priv(net);
8837 if (!tp->rtl_ops.eee_set) {
8842 ret = usb_autopm_get_interface(tp->intf);
8846 mutex_lock(&tp->control);
8848 ret = tp->rtl_ops.eee_set(tp, edata);
8850 ret = mii_nway_restart(&tp->mii);
8852 mutex_unlock(&tp->control);
8854 usb_autopm_put_interface(tp->intf);
8860 static int rtl8152_nway_reset(struct net_device *dev)
8862 struct r8152 *tp = netdev_priv(dev);
8865 ret = usb_autopm_get_interface(tp->intf);
8869 mutex_lock(&tp->control);
8871 ret = mii_nway_restart(&tp->mii);
8873 mutex_unlock(&tp->control);
8875 usb_autopm_put_interface(tp->intf);
8881 static int rtl8152_get_coalesce(struct net_device *netdev,
8882 struct ethtool_coalesce *coalesce,
8883 struct kernel_ethtool_coalesce *kernel_coal,
8884 struct netlink_ext_ack *extack)
8886 struct r8152 *tp = netdev_priv(netdev);
8888 switch (tp->version) {
8897 coalesce->rx_coalesce_usecs = tp->coalesce;
8902 static int rtl8152_set_coalesce(struct net_device *netdev,
8903 struct ethtool_coalesce *coalesce,
8904 struct kernel_ethtool_coalesce *kernel_coal,
8905 struct netlink_ext_ack *extack)
8907 struct r8152 *tp = netdev_priv(netdev);
8910 switch (tp->version) {
8919 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
8922 ret = usb_autopm_get_interface(tp->intf);
8926 mutex_lock(&tp->control);
8928 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
8929 tp->coalesce = coalesce->rx_coalesce_usecs;
8931 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
8932 netif_stop_queue(netdev);
8933 napi_disable(&tp->napi);
8934 tp->rtl_ops.disable(tp);
8935 tp->rtl_ops.enable(tp);
8937 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
8938 _rtl8152_set_rx_mode(netdev);
8939 napi_enable(&tp->napi);
8940 netif_wake_queue(netdev);
8944 mutex_unlock(&tp->control);
8946 usb_autopm_put_interface(tp->intf);
8951 static int rtl8152_get_tunable(struct net_device *netdev,
8952 const struct ethtool_tunable *tunable, void *d)
8954 struct r8152 *tp = netdev_priv(netdev);
8956 switch (tunable->id) {
8957 case ETHTOOL_RX_COPYBREAK:
8958 *(u32 *)d = tp->rx_copybreak;
8967 static int rtl8152_set_tunable(struct net_device *netdev,
8968 const struct ethtool_tunable *tunable,
8971 struct r8152 *tp = netdev_priv(netdev);
8974 switch (tunable->id) {
8975 case ETHTOOL_RX_COPYBREAK:
8977 if (val < ETH_ZLEN) {
8978 netif_err(tp, rx_err, netdev,
8979 "Invalid rx copy break value\n");
8983 if (tp->rx_copybreak != val) {
8984 if (netdev->flags & IFF_UP) {
8985 mutex_lock(&tp->control);
8986 napi_disable(&tp->napi);
8987 tp->rx_copybreak = val;
8988 napi_enable(&tp->napi);
8989 mutex_unlock(&tp->control);
8991 tp->rx_copybreak = val;
9002 static void rtl8152_get_ringparam(struct net_device *netdev,
9003 struct ethtool_ringparam *ring,
9004 struct kernel_ethtool_ringparam *kernel_ring,
9005 struct netlink_ext_ack *extack)
9007 struct r8152 *tp = netdev_priv(netdev);
9009 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
9010 ring->rx_pending = tp->rx_pending;
9013 static int rtl8152_set_ringparam(struct net_device *netdev,
9014 struct ethtool_ringparam *ring,
9015 struct kernel_ethtool_ringparam *kernel_ring,
9016 struct netlink_ext_ack *extack)
9018 struct r8152 *tp = netdev_priv(netdev);
9020 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
9023 if (tp->rx_pending != ring->rx_pending) {
9024 if (netdev->flags & IFF_UP) {
9025 mutex_lock(&tp->control);
9026 napi_disable(&tp->napi);
9027 tp->rx_pending = ring->rx_pending;
9028 napi_enable(&tp->napi);
9029 mutex_unlock(&tp->control);
9031 tp->rx_pending = ring->rx_pending;
9038 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9040 struct r8152 *tp = netdev_priv(netdev);
9041 u16 bmcr, lcladv, rmtadv;
9044 if (usb_autopm_get_interface(tp->intf) < 0)
9047 mutex_lock(&tp->control);
9049 bmcr = r8152_mdio_read(tp, MII_BMCR);
9050 lcladv = r8152_mdio_read(tp, MII_ADVERTISE);
9051 rmtadv = r8152_mdio_read(tp, MII_LPA);
9053 mutex_unlock(&tp->control);
9055 usb_autopm_put_interface(tp->intf);
9057 if (!(bmcr & BMCR_ANENABLE)) {
9059 pause->rx_pause = 0;
9060 pause->tx_pause = 0;
9066 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
9068 if (cap & FLOW_CTRL_RX)
9069 pause->rx_pause = 1;
9071 if (cap & FLOW_CTRL_TX)
9072 pause->tx_pause = 1;
9075 static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9077 struct r8152 *tp = netdev_priv(netdev);
9082 ret = usb_autopm_get_interface(tp->intf);
9086 mutex_lock(&tp->control);
9088 if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) {
9093 if (pause->rx_pause)
9094 cap |= FLOW_CTRL_RX;
9096 if (pause->tx_pause)
9097 cap |= FLOW_CTRL_TX;
9099 old = r8152_mdio_read(tp, MII_ADVERTISE);
9100 new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap);
9102 r8152_mdio_write(tp, MII_ADVERTISE, new1);
9105 mutex_unlock(&tp->control);
9106 usb_autopm_put_interface(tp->intf);
9111 static const struct ethtool_ops ops = {
9112 .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
9113 .get_drvinfo = rtl8152_get_drvinfo,
9114 .get_link = ethtool_op_get_link,
9115 .nway_reset = rtl8152_nway_reset,
9116 .get_msglevel = rtl8152_get_msglevel,
9117 .set_msglevel = rtl8152_set_msglevel,
9118 .get_wol = rtl8152_get_wol,
9119 .set_wol = rtl8152_set_wol,
9120 .get_strings = rtl8152_get_strings,
9121 .get_sset_count = rtl8152_get_sset_count,
9122 .get_ethtool_stats = rtl8152_get_ethtool_stats,
9123 .get_coalesce = rtl8152_get_coalesce,
9124 .set_coalesce = rtl8152_set_coalesce,
9125 .get_eee = rtl_ethtool_get_eee,
9126 .set_eee = rtl_ethtool_set_eee,
9127 .get_link_ksettings = rtl8152_get_link_ksettings,
9128 .set_link_ksettings = rtl8152_set_link_ksettings,
9129 .get_tunable = rtl8152_get_tunable,
9130 .set_tunable = rtl8152_set_tunable,
9131 .get_ringparam = rtl8152_get_ringparam,
9132 .set_ringparam = rtl8152_set_ringparam,
9133 .get_pauseparam = rtl8152_get_pauseparam,
9134 .set_pauseparam = rtl8152_set_pauseparam,
9137 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
9139 struct r8152 *tp = netdev_priv(netdev);
9140 struct mii_ioctl_data *data = if_mii(rq);
9143 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9146 res = usb_autopm_get_interface(tp->intf);
9152 data->phy_id = R8152_PHY_ID; /* Internal PHY */
9156 mutex_lock(&tp->control);
9157 data->val_out = r8152_mdio_read(tp, data->reg_num);
9158 mutex_unlock(&tp->control);
9162 if (!capable(CAP_NET_ADMIN)) {
9166 mutex_lock(&tp->control);
9167 r8152_mdio_write(tp, data->reg_num, data->val_in);
9168 mutex_unlock(&tp->control);
9175 usb_autopm_put_interface(tp->intf);
9181 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
9183 struct r8152 *tp = netdev_priv(dev);
9186 switch (tp->version) {
9196 ret = usb_autopm_get_interface(tp->intf);
9200 mutex_lock(&tp->control);
9204 if (netif_running(dev)) {
9205 if (tp->rtl_ops.change_mtu)
9206 tp->rtl_ops.change_mtu(tp);
9208 if (netif_carrier_ok(dev)) {
9209 netif_stop_queue(dev);
9210 napi_disable(&tp->napi);
9211 tasklet_disable(&tp->tx_tl);
9212 tp->rtl_ops.disable(tp);
9213 tp->rtl_ops.enable(tp);
9215 tasklet_enable(&tp->tx_tl);
9216 napi_enable(&tp->napi);
9217 rtl8152_set_rx_mode(dev);
9218 netif_wake_queue(dev);
9222 mutex_unlock(&tp->control);
9224 usb_autopm_put_interface(tp->intf);
9229 static const struct net_device_ops rtl8152_netdev_ops = {
9230 .ndo_open = rtl8152_open,
9231 .ndo_stop = rtl8152_close,
9232 .ndo_eth_ioctl = rtl8152_ioctl,
9233 .ndo_start_xmit = rtl8152_start_xmit,
9234 .ndo_tx_timeout = rtl8152_tx_timeout,
9235 .ndo_set_features = rtl8152_set_features,
9236 .ndo_set_rx_mode = rtl8152_set_rx_mode,
9237 .ndo_set_mac_address = rtl8152_set_mac_address,
9238 .ndo_change_mtu = rtl8152_change_mtu,
9239 .ndo_validate_addr = eth_validate_addr,
9240 .ndo_features_check = rtl8152_features_check,
9243 static void rtl8152_unload(struct r8152 *tp)
9245 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9248 if (tp->version != RTL_VER_01)
9249 r8152_power_cut_en(tp, true);
9252 static void rtl8153_unload(struct r8152 *tp)
9254 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9257 r8153_power_cut_en(tp, false);
9260 static void rtl8153b_unload(struct r8152 *tp)
9262 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9265 r8153b_power_cut_en(tp, false);
9268 static int rtl_ops_init(struct r8152 *tp)
9270 struct rtl_ops *ops = &tp->rtl_ops;
9273 switch (tp->version) {
9277 ops->init = r8152b_init;
9278 ops->enable = rtl8152_enable;
9279 ops->disable = rtl8152_disable;
9280 ops->up = rtl8152_up;
9281 ops->down = rtl8152_down;
9282 ops->unload = rtl8152_unload;
9283 ops->eee_get = r8152_get_eee;
9284 ops->eee_set = r8152_set_eee;
9285 ops->in_nway = rtl8152_in_nway;
9286 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
9287 ops->autosuspend_en = rtl_runtime_suspend_enable;
9288 tp->rx_buf_sz = 16 * 1024;
9290 tp->eee_adv = MDIO_EEE_100TX;
9297 ops->init = r8153_init;
9298 ops->enable = rtl8153_enable;
9299 ops->disable = rtl8153_disable;
9300 ops->up = rtl8153_up;
9301 ops->down = rtl8153_down;
9302 ops->unload = rtl8153_unload;
9303 ops->eee_get = r8153_get_eee;
9304 ops->eee_set = r8152_set_eee;
9305 ops->in_nway = rtl8153_in_nway;
9306 ops->hw_phy_cfg = r8153_hw_phy_cfg;
9307 ops->autosuspend_en = rtl8153_runtime_enable;
9308 ops->change_mtu = rtl8153_change_mtu;
9309 if (tp->udev->speed < USB_SPEED_SUPER)
9310 tp->rx_buf_sz = 16 * 1024;
9312 tp->rx_buf_sz = 32 * 1024;
9314 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9319 ops->init = r8153b_init;
9320 ops->enable = rtl8153_enable;
9321 ops->disable = rtl8153_disable;
9322 ops->up = rtl8153b_up;
9323 ops->down = rtl8153b_down;
9324 ops->unload = rtl8153b_unload;
9325 ops->eee_get = r8153_get_eee;
9326 ops->eee_set = r8152_set_eee;
9327 ops->in_nway = rtl8153_in_nway;
9328 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
9329 ops->autosuspend_en = rtl8153b_runtime_enable;
9330 ops->change_mtu = rtl8153_change_mtu;
9331 tp->rx_buf_sz = 32 * 1024;
9333 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9338 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9341 ops->init = r8156_init;
9342 ops->enable = rtl8156_enable;
9343 ops->disable = rtl8153_disable;
9344 ops->up = rtl8156_up;
9345 ops->down = rtl8156_down;
9346 ops->unload = rtl8153_unload;
9347 ops->eee_get = r8153_get_eee;
9348 ops->eee_set = r8152_set_eee;
9349 ops->in_nway = rtl8153_in_nway;
9350 ops->hw_phy_cfg = r8156_hw_phy_cfg;
9351 ops->autosuspend_en = rtl8156_runtime_enable;
9352 ops->change_mtu = rtl8156_change_mtu;
9353 tp->rx_buf_sz = 48 * 1024;
9354 tp->support_2500full = 1;
9359 tp->support_2500full = 1;
9363 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9364 ops->init = r8156b_init;
9365 ops->enable = rtl8156b_enable;
9366 ops->disable = rtl8153_disable;
9367 ops->up = rtl8156_up;
9368 ops->down = rtl8156_down;
9369 ops->unload = rtl8153_unload;
9370 ops->eee_get = r8153_get_eee;
9371 ops->eee_set = r8152_set_eee;
9372 ops->in_nway = rtl8153_in_nway;
9373 ops->hw_phy_cfg = r8156b_hw_phy_cfg;
9374 ops->autosuspend_en = rtl8156_runtime_enable;
9375 ops->change_mtu = rtl8156_change_mtu;
9376 tp->rx_buf_sz = 48 * 1024;
9380 ops->init = r8153c_init;
9381 ops->enable = rtl8153_enable;
9382 ops->disable = rtl8153_disable;
9383 ops->up = rtl8153c_up;
9384 ops->down = rtl8153b_down;
9385 ops->unload = rtl8153_unload;
9386 ops->eee_get = r8153_get_eee;
9387 ops->eee_set = r8152_set_eee;
9388 ops->in_nway = rtl8153_in_nway;
9389 ops->hw_phy_cfg = r8153c_hw_phy_cfg;
9390 ops->autosuspend_en = rtl8153c_runtime_enable;
9391 ops->change_mtu = rtl8153c_change_mtu;
9392 tp->rx_buf_sz = 32 * 1024;
9394 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9399 dev_err(&tp->intf->dev, "Unknown Device\n");
9406 #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw"
9407 #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw"
9408 #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw"
9409 #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw"
9410 #define FIRMWARE_8153C_1 "rtl_nic/rtl8153c-1.fw"
9411 #define FIRMWARE_8156A_2 "rtl_nic/rtl8156a-2.fw"
9412 #define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw"
9414 MODULE_FIRMWARE(FIRMWARE_8153A_2);
9415 MODULE_FIRMWARE(FIRMWARE_8153A_3);
9416 MODULE_FIRMWARE(FIRMWARE_8153A_4);
9417 MODULE_FIRMWARE(FIRMWARE_8153B_2);
9418 MODULE_FIRMWARE(FIRMWARE_8153C_1);
9419 MODULE_FIRMWARE(FIRMWARE_8156A_2);
9420 MODULE_FIRMWARE(FIRMWARE_8156B_2);
9422 static int rtl_fw_init(struct r8152 *tp)
9424 struct rtl_fw *rtl_fw = &tp->rtl_fw;
9426 switch (tp->version) {
9428 rtl_fw->fw_name = FIRMWARE_8153A_2;
9429 rtl_fw->pre_fw = r8153_pre_firmware_1;
9430 rtl_fw->post_fw = r8153_post_firmware_1;
9433 rtl_fw->fw_name = FIRMWARE_8153A_3;
9434 rtl_fw->pre_fw = r8153_pre_firmware_2;
9435 rtl_fw->post_fw = r8153_post_firmware_2;
9438 rtl_fw->fw_name = FIRMWARE_8153A_4;
9439 rtl_fw->post_fw = r8153_post_firmware_3;
9442 rtl_fw->fw_name = FIRMWARE_8153B_2;
9443 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9444 rtl_fw->post_fw = r8153b_post_firmware_1;
9447 rtl_fw->fw_name = FIRMWARE_8156A_2;
9448 rtl_fw->post_fw = r8156a_post_firmware_1;
9452 rtl_fw->fw_name = FIRMWARE_8156B_2;
9455 rtl_fw->fw_name = FIRMWARE_8153C_1;
9456 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9457 rtl_fw->post_fw = r8153c_post_firmware_1;
9466 static u8 __rtl_get_hw_ver(struct usb_device *udev)
9473 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
9477 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
9478 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
9479 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
9481 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
9487 version = RTL_VER_01;
9490 version = RTL_VER_02;
9493 version = RTL_VER_03;
9496 version = RTL_VER_04;
9499 version = RTL_VER_05;
9502 version = RTL_VER_06;
9505 version = RTL_VER_07;
9508 version = RTL_VER_08;
9511 version = RTL_VER_09;
9514 version = RTL_TEST_01;
9517 version = RTL_VER_10;
9520 version = RTL_VER_11;
9523 version = RTL_VER_12;
9526 version = RTL_VER_13;
9529 version = RTL_VER_14;
9532 version = RTL_VER_15;
9535 version = RTL_VER_UNKNOWN;
9536 dev_info(&udev->dev, "Unknown version 0x%04x\n", ocp_data);
9543 u8 rtl8152_get_version(struct usb_interface *intf)
9547 version = __rtl_get_hw_ver(interface_to_usbdev(intf));
9549 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
9553 EXPORT_SYMBOL_GPL(rtl8152_get_version);
9555 static bool rtl8152_supports_lenovo_macpassthru(struct usb_device *udev)
9557 int parent_vendor_id = le16_to_cpu(udev->parent->descriptor.idVendor);
9558 int product_id = le16_to_cpu(udev->descriptor.idProduct);
9559 int vendor_id = le16_to_cpu(udev->descriptor.idVendor);
9561 if (vendor_id == VENDOR_ID_LENOVO) {
9562 switch (product_id) {
9563 case DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB:
9564 case DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK:
9565 case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2:
9566 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2:
9567 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3:
9568 case DEVICE_ID_THINKPAD_USB_C_DONGLE:
9571 } else if (vendor_id == VENDOR_ID_REALTEK && parent_vendor_id == VENDOR_ID_LENOVO) {
9572 switch (product_id) {
9580 static int rtl8152_probe(struct usb_interface *intf,
9581 const struct usb_device_id *id)
9583 struct usb_device *udev = interface_to_usbdev(intf);
9585 struct net_device *netdev;
9589 if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
9592 if (!rtl_check_vendor_ok(intf))
9595 version = rtl8152_get_version(intf);
9596 if (version == RTL_VER_UNKNOWN)
9599 usb_reset_device(udev);
9600 netdev = alloc_etherdev(sizeof(struct r8152));
9602 dev_err(&intf->dev, "Out of memory\n");
9606 SET_NETDEV_DEV(netdev, &intf->dev);
9607 tp = netdev_priv(netdev);
9608 tp->msg_enable = 0x7FFF;
9611 tp->netdev = netdev;
9613 tp->version = version;
9615 tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0);
9616 tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0);
9617 tp->pipe_in = usb_rcvbulkpipe(udev, 1);
9618 tp->pipe_out = usb_sndbulkpipe(udev, 2);
9619 tp->pipe_intr = usb_rcvintpipe(udev, 3);
9625 tp->mii.supports_gmii = 0;
9628 tp->mii.supports_gmii = 1;
9632 ret = rtl_ops_init(tp);
9638 mutex_init(&tp->control);
9639 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
9640 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
9641 tasklet_setup(&tp->tx_tl, bottom_half);
9642 tasklet_disable(&tp->tx_tl);
9644 netdev->netdev_ops = &rtl8152_netdev_ops;
9645 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
9647 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9648 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
9649 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
9650 NETIF_F_HW_VLAN_CTAG_TX;
9651 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9652 NETIF_F_TSO | NETIF_F_FRAGLIST |
9653 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
9654 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
9655 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
9656 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
9657 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
9659 if (tp->version == RTL_VER_01) {
9660 netdev->features &= ~NETIF_F_RXCSUM;
9661 netdev->hw_features &= ~NETIF_F_RXCSUM;
9664 tp->lenovo_macpassthru = rtl8152_supports_lenovo_macpassthru(udev);
9666 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
9667 (!strcmp(udev->serial, "000001000000") ||
9668 !strcmp(udev->serial, "000002000000"))) {
9669 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
9670 tp->dell_tb_rx_agg_bug = 1;
9673 netdev->ethtool_ops = &ops;
9674 netif_set_tso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
9676 /* MTU range: 68 - 1500 or 9194 */
9677 netdev->min_mtu = ETH_MIN_MTU;
9678 switch (tp->version) {
9686 netdev->max_mtu = size_to_mtu(9 * 1024);
9690 netdev->max_mtu = size_to_mtu(15 * 1024);
9695 netdev->max_mtu = size_to_mtu(16 * 1024);
9701 netdev->max_mtu = ETH_DATA_LEN;
9705 tp->mii.dev = netdev;
9706 tp->mii.mdio_read = read_mii_word;
9707 tp->mii.mdio_write = write_mii_word;
9708 tp->mii.phy_id_mask = 0x3f;
9709 tp->mii.reg_num_mask = 0x1f;
9710 tp->mii.phy_id = R8152_PHY_ID;
9712 tp->autoneg = AUTONEG_ENABLE;
9713 tp->speed = SPEED_100;
9714 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
9715 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
9716 if (tp->mii.supports_gmii) {
9717 if (tp->support_2500full &&
9718 tp->udev->speed >= USB_SPEED_SUPER) {
9719 tp->speed = SPEED_2500;
9720 tp->advertising |= RTL_ADVERTISED_2500_FULL;
9722 tp->speed = SPEED_1000;
9724 tp->advertising |= RTL_ADVERTISED_1000_FULL;
9726 tp->duplex = DUPLEX_FULL;
9728 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
9729 tp->rx_pending = 10 * RTL8152_MAX_RX;
9731 intf->needs_remote_wakeup = 1;
9733 if (!rtl_can_wakeup(tp))
9734 __rtl_set_wol(tp, 0);
9736 tp->saved_wolopts = __rtl_get_wol(tp);
9738 tp->rtl_ops.init(tp);
9739 #if IS_BUILTIN(CONFIG_USB_RTL8152)
9740 /* Retry in case request_firmware() is not ready yet. */
9741 tp->rtl_fw.retry = true;
9743 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
9744 set_ethernet_addr(tp, false);
9746 usb_set_intfdata(intf, tp);
9748 netif_napi_add_weight(netdev, &tp->napi, r8152_poll,
9749 tp->support_2500full ? 256 : 64);
9751 ret = register_netdev(netdev);
9753 dev_err(&intf->dev, "couldn't register the device\n");
9757 if (tp->saved_wolopts)
9758 device_set_wakeup_enable(&udev->dev, true);
9760 device_set_wakeup_enable(&udev->dev, false);
9762 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
9767 tasklet_kill(&tp->tx_tl);
9768 usb_set_intfdata(intf, NULL);
9770 free_netdev(netdev);
9774 static void rtl8152_disconnect(struct usb_interface *intf)
9776 struct r8152 *tp = usb_get_intfdata(intf);
9778 usb_set_intfdata(intf, NULL);
9782 unregister_netdev(tp->netdev);
9783 tasklet_kill(&tp->tx_tl);
9784 cancel_delayed_work_sync(&tp->hw_phy_work);
9785 if (tp->rtl_ops.unload)
9786 tp->rtl_ops.unload(tp);
9787 rtl8152_release_firmware(tp);
9788 free_netdev(tp->netdev);
9792 /* table of devices that work with this driver */
9793 static const struct usb_device_id rtl8152_table[] = {
9795 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8050) },
9796 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8053) },
9797 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8152) },
9798 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8153) },
9799 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8155) },
9800 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8156) },
9803 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab) },
9804 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6) },
9805 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927) },
9806 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0c5e) },
9807 { USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101) },
9808 { USB_DEVICE(VENDOR_ID_LENOVO, 0x304f) },
9809 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3054) },
9810 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3062) },
9811 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3069) },
9812 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3082) },
9813 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7205) },
9814 { USB_DEVICE(VENDOR_ID_LENOVO, 0x720c) },
9815 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7214) },
9816 { USB_DEVICE(VENDOR_ID_LENOVO, 0x721e) },
9817 { USB_DEVICE(VENDOR_ID_LENOVO, 0xa387) },
9818 { USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041) },
9819 { USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff) },
9820 { USB_DEVICE(VENDOR_ID_TPLINK, 0x0601) },
9824 MODULE_DEVICE_TABLE(usb, rtl8152_table);
9826 static struct usb_driver rtl8152_driver = {
9828 .id_table = rtl8152_table,
9829 .probe = rtl8152_probe,
9830 .disconnect = rtl8152_disconnect,
9831 .suspend = rtl8152_suspend,
9832 .resume = rtl8152_resume,
9833 .reset_resume = rtl8152_reset_resume,
9834 .pre_reset = rtl8152_pre_reset,
9835 .post_reset = rtl8152_post_reset,
9836 .supports_autosuspend = 1,
9837 .disable_hub_initiated_lpm = 1,
9840 static int rtl8152_cfgselector_probe(struct usb_device *udev)
9842 struct usb_host_config *c;
9845 /* Switch the device to vendor mode, if and only if the vendor mode
9846 * driver supports it.
9848 if (__rtl_get_hw_ver(udev) == RTL_VER_UNKNOWN)
9851 /* The vendor mode is not always config #1, so to find it out. */
9853 num_configs = udev->descriptor.bNumConfigurations;
9854 for (i = 0; i < num_configs; (i++, c++)) {
9855 struct usb_interface_descriptor *desc = NULL;
9857 if (!c->desc.bNumInterfaces)
9859 desc = &c->intf_cache[0]->altsetting->desc;
9860 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
9864 if (i == num_configs)
9867 if (usb_set_configuration(udev, c->desc.bConfigurationValue)) {
9868 dev_err(&udev->dev, "Failed to set configuration %d\n",
9869 c->desc.bConfigurationValue);
9876 static struct usb_device_driver rtl8152_cfgselector_driver = {
9877 .name = MODULENAME "-cfgselector",
9878 .probe = rtl8152_cfgselector_probe,
9879 .id_table = rtl8152_table,
9880 .generic_subclass = 1,
9883 static int __init rtl8152_driver_init(void)
9887 ret = usb_register_device_driver(&rtl8152_cfgselector_driver, THIS_MODULE);
9890 return usb_register(&rtl8152_driver);
9893 static void __exit rtl8152_driver_exit(void)
9895 usb_deregister(&rtl8152_driver);
9896 usb_deregister_device_driver(&rtl8152_cfgselector_driver);
9899 module_init(rtl8152_driver_init);
9900 module_exit(rtl8152_driver_exit);
9902 MODULE_AUTHOR(DRIVER_AUTHOR);
9903 MODULE_DESCRIPTION(DRIVER_DESC);
9904 MODULE_LICENSE("GPL");
9905 MODULE_VERSION(DRIVER_VERSION);