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/phy.h>
14 #include <linux/usb.h>
15 #include <linux/crc32.h>
16 #include <linux/if_vlan.h>
17 #include <linux/uaccess.h>
18 #include <linux/list.h>
20 #include <linux/ipv6.h>
21 #include <net/ip6_checksum.h>
22 #include <uapi/linux/mdio.h>
23 #include <linux/mdio.h>
24 #include <linux/usb/cdc.h>
25 #include <linux/suspend.h>
26 #include <linux/atomic.h>
27 #include <linux/acpi.h>
28 #include <linux/firmware.h>
29 #include <crypto/hash.h>
30 #include <linux/usb/r8152.h>
33 /* Information for net-next */
34 #define NETNEXT_VERSION "12"
36 /* Information for net */
37 #define NET_VERSION "13"
39 #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
41 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
42 #define MODULENAME "r8152"
44 #define R8152_PHY_ID 32
46 #define PLA_IDR 0xc000
47 #define PLA_RCR 0xc010
48 #define PLA_RCR1 0xc012
49 #define PLA_RMS 0xc016
50 #define PLA_RXFIFO_CTRL0 0xc0a0
51 #define PLA_RXFIFO_FULL 0xc0a2
52 #define PLA_RXFIFO_CTRL1 0xc0a4
53 #define PLA_RX_FIFO_FULL 0xc0a6
54 #define PLA_RXFIFO_CTRL2 0xc0a8
55 #define PLA_RX_FIFO_EMPTY 0xc0aa
56 #define PLA_DMY_REG0 0xc0b0
57 #define PLA_FMC 0xc0b4
58 #define PLA_CFG_WOL 0xc0b6
59 #define PLA_TEREDO_CFG 0xc0bc
60 #define PLA_TEREDO_WAKE_BASE 0xc0c4
61 #define PLA_MAR 0xcd00
62 #define PLA_BACKUP 0xd000
63 #define PLA_BDC_CR 0xd1a0
64 #define PLA_TEREDO_TIMER 0xd2cc
65 #define PLA_REALWOW_TIMER 0xd2e8
66 #define PLA_UPHY_TIMER 0xd388
67 #define PLA_SUSPEND_FLAG 0xd38a
68 #define PLA_INDICATE_FALG 0xd38c
69 #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */
70 #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */
71 #define PLA_EXTRA_STATUS 0xd398
72 #define PLA_GPHY_CTRL 0xd3ae
73 #define PLA_POL_GPIO_CTRL 0xdc6a
74 #define PLA_EFUSE_DATA 0xdd00
75 #define PLA_EFUSE_CMD 0xdd02
76 #define PLA_LEDSEL 0xdd90
77 #define PLA_LED_FEATURE 0xdd92
78 #define PLA_PHYAR 0xde00
79 #define PLA_BOOT_CTRL 0xe004
80 #define PLA_LWAKE_CTRL_REG 0xe007
81 #define PLA_GPHY_INTR_IMR 0xe022
82 #define PLA_EEE_CR 0xe040
83 #define PLA_EEE_TXTWSYS 0xe04c
84 #define PLA_EEE_TXTWSYS_2P5G 0xe058
85 #define PLA_EEEP_CR 0xe080
86 #define PLA_MAC_PWR_CTRL 0xe0c0
87 #define PLA_MAC_PWR_CTRL2 0xe0ca
88 #define PLA_MAC_PWR_CTRL3 0xe0cc
89 #define PLA_MAC_PWR_CTRL4 0xe0ce
90 #define PLA_WDT6_CTRL 0xe428
91 #define PLA_TCR0 0xe610
92 #define PLA_TCR1 0xe612
93 #define PLA_MTPS 0xe615
94 #define PLA_TXFIFO_CTRL 0xe618
95 #define PLA_TXFIFO_FULL 0xe61a
96 #define PLA_RSTTALLY 0xe800
98 #define PLA_CRWECR 0xe81c
99 #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
100 #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
101 #define PLA_CONFIG5 0xe822
102 #define PLA_PHY_PWR 0xe84c
103 #define PLA_OOB_CTRL 0xe84f
104 #define PLA_CPCR 0xe854
105 #define PLA_MISC_0 0xe858
106 #define PLA_MISC_1 0xe85a
107 #define PLA_OCP_GPHY_BASE 0xe86c
108 #define PLA_TALLYCNT 0xe890
109 #define PLA_SFF_STS_7 0xe8de
110 #define PLA_PHYSTATUS 0xe908
111 #define PLA_CONFIG6 0xe90a /* CONFIG6 */
112 #define PLA_USB_CFG 0xe952
113 #define PLA_BP_BA 0xfc26
114 #define PLA_BP_0 0xfc28
115 #define PLA_BP_1 0xfc2a
116 #define PLA_BP_2 0xfc2c
117 #define PLA_BP_3 0xfc2e
118 #define PLA_BP_4 0xfc30
119 #define PLA_BP_5 0xfc32
120 #define PLA_BP_6 0xfc34
121 #define PLA_BP_7 0xfc36
122 #define PLA_BP_EN 0xfc38
124 #define USB_USB2PHY 0xb41e
125 #define USB_SSPHYLINK1 0xb426
126 #define USB_SSPHYLINK2 0xb428
127 #define USB_L1_CTRL 0xb45e
128 #define USB_U2P3_CTRL 0xb460
129 #define USB_CSR_DUMMY1 0xb464
130 #define USB_CSR_DUMMY2 0xb466
131 #define USB_DEV_STAT 0xb808
132 #define USB_CONNECT_TIMER 0xcbf8
133 #define USB_MSC_TIMER 0xcbfc
134 #define USB_BURST_SIZE 0xcfc0
135 #define USB_FW_FIX_EN0 0xcfca
136 #define USB_FW_FIX_EN1 0xcfcc
137 #define USB_LPM_CONFIG 0xcfd8
138 #define USB_ECM_OPTION 0xcfee
139 #define USB_CSTMR 0xcfef /* RTL8153A */
140 #define USB_MISC_2 0xcfff
141 #define USB_ECM_OP 0xd26b
142 #define USB_GPHY_CTRL 0xd284
143 #define USB_SPEED_OPTION 0xd32a
144 #define USB_FW_CTRL 0xd334 /* RTL8153B */
145 #define USB_FC_TIMER 0xd340
146 #define USB_USB_CTRL 0xd406
147 #define USB_PHY_CTRL 0xd408
148 #define USB_TX_AGG 0xd40a
149 #define USB_RX_BUF_TH 0xd40c
150 #define USB_USB_TIMER 0xd428
151 #define USB_RX_EARLY_TIMEOUT 0xd42c
152 #define USB_RX_EARLY_SIZE 0xd42e
153 #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
154 #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
155 #define USB_TX_DMA 0xd434
156 #define USB_UPT_RXDMA_OWN 0xd437
157 #define USB_UPHY3_MDCMDIO 0xd480
158 #define USB_TOLERANCE 0xd490
159 #define USB_LPM_CTRL 0xd41a
160 #define USB_BMU_RESET 0xd4b0
161 #define USB_BMU_CONFIG 0xd4b4
162 #define USB_U1U2_TIMER 0xd4da
163 #define USB_FW_TASK 0xd4e8 /* RTL8153B */
164 #define USB_RX_AGGR_NUM 0xd4ee
165 #define USB_UPS_CTRL 0xd800
166 #define USB_POWER_CUT 0xd80a
167 #define USB_MISC_0 0xd81a
168 #define USB_MISC_1 0xd81f
169 #define USB_AFE_CTRL2 0xd824
170 #define USB_UPHY_XTAL 0xd826
171 #define USB_UPS_CFG 0xd842
172 #define USB_UPS_FLAGS 0xd848
173 #define USB_WDT1_CTRL 0xe404
174 #define USB_WDT11_CTRL 0xe43c
175 #define USB_BP_BA PLA_BP_BA
176 #define USB_BP_0 PLA_BP_0
177 #define USB_BP_1 PLA_BP_1
178 #define USB_BP_2 PLA_BP_2
179 #define USB_BP_3 PLA_BP_3
180 #define USB_BP_4 PLA_BP_4
181 #define USB_BP_5 PLA_BP_5
182 #define USB_BP_6 PLA_BP_6
183 #define USB_BP_7 PLA_BP_7
184 #define USB_BP_EN PLA_BP_EN /* RTL8153A */
185 #define USB_BP_8 0xfc38 /* RTL8153B */
186 #define USB_BP_9 0xfc3a
187 #define USB_BP_10 0xfc3c
188 #define USB_BP_11 0xfc3e
189 #define USB_BP_12 0xfc40
190 #define USB_BP_13 0xfc42
191 #define USB_BP_14 0xfc44
192 #define USB_BP_15 0xfc46
193 #define USB_BP2_EN 0xfc48
196 #define OCP_ALDPS_CONFIG 0x2010
197 #define OCP_EEE_CONFIG1 0x2080
198 #define OCP_EEE_CONFIG2 0x2092
199 #define OCP_EEE_CONFIG3 0x2094
200 #define OCP_BASE_MII 0xa400
201 #define OCP_EEE_AR 0xa41a
202 #define OCP_EEE_DATA 0xa41c
203 #define OCP_PHY_STATUS 0xa420
204 #define OCP_INTR_EN 0xa424
205 #define OCP_NCTL_CFG 0xa42c
206 #define OCP_POWER_CFG 0xa430
207 #define OCP_EEE_CFG 0xa432
208 #define OCP_SRAM_ADDR 0xa436
209 #define OCP_SRAM_DATA 0xa438
210 #define OCP_DOWN_SPEED 0xa442
211 #define OCP_EEE_ABLE 0xa5c4
212 #define OCP_EEE_ADV 0xa5d0
213 #define OCP_EEE_LPABLE 0xa5d2
214 #define OCP_10GBT_CTRL 0xa5d4
215 #define OCP_10GBT_STAT 0xa5d6
216 #define OCP_EEE_ADV2 0xa6d4
217 #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
218 #define OCP_PHY_PATCH_STAT 0xb800
219 #define OCP_PHY_PATCH_CMD 0xb820
220 #define OCP_PHY_LOCK 0xb82e
221 #define OCP_ADC_IOFFSET 0xbcfc
222 #define OCP_ADC_CFG 0xbc06
223 #define OCP_SYSCLK_CFG 0xc416
226 #define SRAM_GREEN_CFG 0x8011
227 #define SRAM_LPF_CFG 0x8012
228 #define SRAM_GPHY_FW_VER 0x801e
229 #define SRAM_10M_AMP1 0x8080
230 #define SRAM_10M_AMP2 0x8082
231 #define SRAM_IMPEDANCE 0x8084
232 #define SRAM_PHY_LOCK 0xb82e
235 #define RCR_AAP 0x00000001
236 #define RCR_APM 0x00000002
237 #define RCR_AM 0x00000004
238 #define RCR_AB 0x00000008
239 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
240 #define SLOT_EN BIT(11)
243 #define OUTER_VLAN BIT(7)
244 #define INNER_VLAN BIT(6)
246 /* PLA_RXFIFO_CTRL0 */
247 #define RXFIFO_THR1_NORMAL 0x00080002
248 #define RXFIFO_THR1_OOB 0x01800003
250 /* PLA_RXFIFO_FULL */
251 #define RXFIFO_FULL_MASK 0xfff
253 /* PLA_RXFIFO_CTRL1 */
254 #define RXFIFO_THR2_FULL 0x00000060
255 #define RXFIFO_THR2_HIGH 0x00000038
256 #define RXFIFO_THR2_OOB 0x0000004a
257 #define RXFIFO_THR2_NORMAL 0x00a0
259 /* PLA_RXFIFO_CTRL2 */
260 #define RXFIFO_THR3_FULL 0x00000078
261 #define RXFIFO_THR3_HIGH 0x00000048
262 #define RXFIFO_THR3_OOB 0x0000005a
263 #define RXFIFO_THR3_NORMAL 0x0110
265 /* PLA_TXFIFO_CTRL */
266 #define TXFIFO_THR_NORMAL 0x00400008
267 #define TXFIFO_THR_NORMAL2 0x01000008
270 #define ECM_ALDPS 0x0002
273 #define FMC_FCR_MCU_EN 0x0001
276 #define EEEP_CR_EEEP_TX 0x0002
279 #define WDT6_SET_MODE 0x0010
282 #define TCR0_TX_EMPTY 0x0800
283 #define TCR0_AUTO_FIFO 0x0080
286 #define VERSION_MASK 0x7cf0
287 #define IFG_MASK (BIT(3) | BIT(9) | BIT(8))
288 #define IFG_144NS BIT(9)
289 #define IFG_96NS (BIT(9) | BIT(8))
292 #define MTPS_JUMBO (12 * 1024 / 64)
293 #define MTPS_DEFAULT (6 * 1024 / 64)
296 #define TALLY_RESET 0x0001
304 #define CRWECR_NORAML 0x00
305 #define CRWECR_CONFIG 0xc0
308 #define NOW_IS_OOB 0x80
309 #define TXFIFO_EMPTY 0x20
310 #define RXFIFO_EMPTY 0x10
311 #define LINK_LIST_READY 0x02
312 #define DIS_MCU_CLROOB 0x01
313 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
316 #define RXDY_GATED_EN 0x0008
319 #define RE_INIT_LL 0x8000
320 #define MCU_BORW_EN 0x4000
323 #define FLOW_CTRL_EN BIT(0)
324 #define CPCR_RX_VLAN 0x0040
327 #define MAGIC_EN 0x0001
330 #define TEREDO_SEL 0x8000
331 #define TEREDO_WAKE_MASK 0x7f00
332 #define TEREDO_RS_EVENT_MASK 0x00fe
333 #define OOB_TEREDO_EN 0x0001
336 #define ALDPS_PROXY_MODE 0x0001
339 #define EFUSE_READ_CMD BIT(15)
340 #define EFUSE_DATA_BIT16 BIT(7)
343 #define LINK_ON_WAKE_EN 0x0010
344 #define LINK_OFF_WAKE_EN 0x0008
347 #define LANWAKE_CLR_EN BIT(0)
350 #define EN_XG_LIP BIT(1)
351 #define EN_G_LIP BIT(2)
354 #define BWF_EN 0x0040
355 #define MWF_EN 0x0020
356 #define UWF_EN 0x0010
357 #define LAN_WAKE_EN 0x0002
359 /* PLA_LED_FEATURE */
360 #define LED_MODE_MASK 0x0700
363 #define TX_10M_IDLE_EN 0x0080
364 #define PFM_PWM_SWITCH 0x0040
365 #define TEST_IO_OFF BIT(4)
367 /* PLA_MAC_PWR_CTRL */
368 #define D3_CLK_GATED_EN 0x00004000
369 #define MCU_CLK_RATIO 0x07010f07
370 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
371 #define ALDPS_SPDWN_RATIO 0x0f87
373 /* PLA_MAC_PWR_CTRL2 */
374 #define EEE_SPDWN_RATIO 0x8007
375 #define MAC_CLK_SPDWN_EN BIT(15)
376 #define EEE_SPDWN_RATIO_MASK 0xff
378 /* PLA_MAC_PWR_CTRL3 */
379 #define PLA_MCU_SPDWN_EN BIT(14)
380 #define PKT_AVAIL_SPDWN_EN 0x0100
381 #define SUSPEND_SPDWN_EN 0x0004
382 #define U1U2_SPDWN_EN 0x0002
383 #define L1_SPDWN_EN 0x0001
385 /* PLA_MAC_PWR_CTRL4 */
386 #define PWRSAVE_SPDWN_EN 0x1000
387 #define RXDV_SPDWN_EN 0x0800
388 #define TX10MIDLE_EN 0x0100
389 #define IDLE_SPDWN_EN BIT(6)
390 #define TP100_SPDWN_EN 0x0020
391 #define TP500_SPDWN_EN 0x0010
392 #define TP1000_SPDWN_EN 0x0008
393 #define EEE_SPDWN_EN 0x0001
395 /* PLA_GPHY_INTR_IMR */
396 #define GPHY_STS_MSK 0x0001
397 #define SPEED_DOWN_MSK 0x0002
398 #define SPDWN_RXDV_MSK 0x0004
399 #define SPDWN_LINKCHG_MSK 0x0008
402 #define PHYAR_FLAG 0x80000000
405 #define EEE_RX_EN 0x0001
406 #define EEE_TX_EN 0x0002
409 #define AUTOLOAD_DONE 0x0002
411 /* PLA_LWAKE_CTRL_REG */
412 #define LANWAKE_PIN BIT(7)
414 /* PLA_SUSPEND_FLAG */
415 #define LINK_CHG_EVENT BIT(0)
417 /* PLA_INDICATE_FALG */
418 #define UPCOMING_RUNTIME_D3 BIT(0)
420 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */
421 #define DEBUG_OE BIT(0)
422 #define DEBUG_LTSSM 0x0082
424 /* PLA_EXTRA_STATUS */
425 #define CUR_LINK_OK BIT(15)
426 #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */
427 #define LINK_CHANGE_FLAG BIT(8)
428 #define POLL_LINK_CHG BIT(0)
431 #define GPHY_FLASH BIT(1)
433 /* PLA_POL_GPIO_CTRL */
434 #define DACK_DET_EN BIT(15)
435 #define POL_GPHY_PATCH BIT(4)
438 #define USB2PHY_SUSPEND 0x0001
439 #define USB2PHY_L1 0x0002
442 #define DELAY_PHY_PWR_CHG BIT(1)
445 #define pwd_dn_scale_mask 0x3ffe
446 #define pwd_dn_scale(x) ((x) << 1)
449 #define DYNAMIC_BURST 0x0001
452 #define EP4_FULL_FC 0x0001
455 #define STAT_SPEED_MASK 0x0006
456 #define STAT_SPEED_HIGH 0x0000
457 #define STAT_SPEED_FULL 0x0002
460 #define FW_FIX_SUSPEND BIT(14)
463 #define FW_IP_RESET_EN BIT(9)
466 #define LPM_U1U2_EN BIT(0)
469 #define TX_AGG_MAX_THRESHOLD 0x03
472 #define RX_THR_SUPPER 0x0c350180
473 #define RX_THR_HIGH 0x7a120180
474 #define RX_THR_SLOW 0xffff0180
475 #define RX_THR_B 0x00010001
478 #define TEST_MODE_DISABLE 0x00000001
479 #define TX_SIZE_ADJUST1 0x00000100
482 #define BMU_RESET_EP_IN 0x01
483 #define BMU_RESET_EP_OUT 0x02
486 #define ACT_ODMA BIT(1)
488 /* USB_UPT_RXDMA_OWN */
489 #define OWN_UPDATE BIT(0)
490 #define OWN_CLEAR BIT(1)
493 #define FC_PATCH_TASK BIT(1)
495 /* USB_RX_AGGR_NUM */
496 #define RX_AGGR_NUM_MASK 0x1ff
499 #define POWER_CUT 0x0100
501 /* USB_PM_CTRL_STATUS */
502 #define RESUME_INDICATE 0x0001
505 #define BYPASS_MAC_RESET BIT(5)
508 #define FORCE_SUPER BIT(0)
511 #define UPS_FORCE_PWR_DOWN BIT(0)
514 #define EN_ALL_SPEED BIT(0)
517 #define GPHY_PATCH_DONE BIT(2)
518 #define BYPASS_FLASH BIT(5)
519 #define BACKUP_RESTRORE BIT(6)
521 /* USB_SPEED_OPTION */
522 #define RG_PWRDN_EN BIT(8)
523 #define ALL_SPEED_OFF BIT(9)
526 #define FLOW_CTRL_PATCH_OPT BIT(1)
527 #define AUTO_SPEEDUP BIT(3)
528 #define FLOW_CTRL_PATCH_2 BIT(8)
531 #define CTRL_TIMER_EN BIT(15)
534 #define CDC_ECM_EN BIT(3)
535 #define RX_AGG_DISABLE 0x0010
536 #define RX_ZERO_EN 0x0080
539 #define U2P3_ENABLE 0x0001
540 #define RX_DETECT8 BIT(3)
543 #define PWR_EN 0x0001
544 #define PHASE2_EN 0x0008
545 #define UPS_EN BIT(4)
546 #define USP_PREWAKE BIT(5)
549 #define PCUT_STATUS 0x0001
551 /* USB_RX_EARLY_TIMEOUT */
552 #define COALESCE_SUPER 85000U
553 #define COALESCE_HIGH 250000U
554 #define COALESCE_SLOW 524280U
557 #define WTD1_EN BIT(0)
560 #define TIMER11_EN 0x0001
563 /* bit 4 ~ 5: fifo empty boundary */
564 #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
565 /* bit 2 ~ 3: LMP timer */
566 #define LPM_TIMER_MASK 0x0c
567 #define LPM_TIMER_500MS 0x04 /* 500 ms */
568 #define LPM_TIMER_500US 0x0c /* 500 us */
569 #define ROK_EXIT_LPM 0x02
572 #define SEN_VAL_MASK 0xf800
573 #define SEN_VAL_NORMAL 0xa000
574 #define SEL_RXIDLE 0x0100
577 #define OOBS_POLLING BIT(8)
580 #define SAW_CNT_1MS_MASK 0x0fff
581 #define MID_REVERSE BIT(5) /* RTL8156A */
584 #define UPS_FLAGS_R_TUNE BIT(0)
585 #define UPS_FLAGS_EN_10M_CKDIV BIT(1)
586 #define UPS_FLAGS_250M_CKDIV BIT(2)
587 #define UPS_FLAGS_EN_ALDPS BIT(3)
588 #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
589 #define UPS_FLAGS_SPEED_MASK (0xf << 16)
590 #define ups_flags_speed(x) ((x) << 16)
591 #define UPS_FLAGS_EN_EEE BIT(20)
592 #define UPS_FLAGS_EN_500M_EEE BIT(21)
593 #define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
594 #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23)
595 #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
596 #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
597 #define UPS_FLAGS_EN_GREEN BIT(26)
598 #define UPS_FLAGS_EN_FLOW_CTR BIT(27)
614 /* OCP_ALDPS_CONFIG */
615 #define ENPWRSAVE 0x8000
616 #define ENPDNPS 0x0200
617 #define LINKENA 0x0100
618 #define DIS_SDSAVE 0x0010
621 #define PHY_STAT_MASK 0x0007
622 #define PHY_STAT_EXT_INIT 2
623 #define PHY_STAT_LAN_ON 3
624 #define PHY_STAT_PWRDN 5
627 #define INTR_SPEED_FORCE BIT(3)
630 #define PGA_RETURN_EN BIT(1)
633 #define EEE_CLKDIV_EN 0x8000
634 #define EN_ALDPS 0x0004
635 #define EN_10M_PLLOFF 0x0001
637 /* OCP_EEE_CONFIG1 */
638 #define RG_TXLPI_MSK_HFDUP 0x8000
639 #define RG_MATCLR_EN 0x4000
640 #define EEE_10_CAP 0x2000
641 #define EEE_NWAY_EN 0x1000
642 #define TX_QUIET_EN 0x0200
643 #define RX_QUIET_EN 0x0100
644 #define sd_rise_time_mask 0x0070
645 #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
646 #define RG_RXLPI_MSK_HFDUP 0x0008
647 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
649 /* OCP_EEE_CONFIG2 */
650 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
651 #define RG_DACQUIET_EN 0x0400
652 #define RG_LDVQUIET_EN 0x0200
653 #define RG_CKRSEL 0x0020
654 #define RG_EEEPRG_EN 0x0010
656 /* OCP_EEE_CONFIG3 */
657 #define fast_snr_mask 0xff80
658 #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
659 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
660 #define MSK_PH 0x0006 /* bit 0 ~ 3 */
663 /* bit[15:14] function */
664 #define FUN_ADDR 0x0000
665 #define FUN_DATA 0x4000
666 /* bit[4:0] device addr */
669 #define CTAP_SHORT_EN 0x0040
670 #define EEE10_EN 0x0010
673 #define EN_EEE_CMODE BIT(14)
674 #define EN_EEE_1000 BIT(13)
675 #define EN_EEE_100 BIT(12)
676 #define EN_10M_CLKDIV BIT(11)
677 #define EN_10M_BGOFF 0x0080
680 #define RTL_ADV2_5G_F_R BIT(5) /* Advertise 2.5GBASE-T fast-retrain */
683 #define TXDIS_STATE 0x01
684 #define ABD_STATE 0x02
686 /* OCP_PHY_PATCH_STAT */
687 #define PATCH_READY BIT(6)
689 /* OCP_PHY_PATCH_CMD */
690 #define PATCH_REQUEST BIT(4)
693 #define PATCH_LOCK BIT(0)
696 #define CKADSEL_L 0x0100
697 #define ADC_EN 0x0080
698 #define EN_EMI_L 0x0040
701 #define sysclk_div_expo(x) (min(x, 5) << 8)
702 #define clk_div_expo(x) (min(x, 5) << 4)
705 #define GREEN_ETH_EN BIT(15)
706 #define R_TUNE_EN BIT(11)
709 #define LPF_AUTO_TUNE 0x8000
712 #define GDAC_IB_UPALL 0x0008
715 #define AMP_DN 0x0200
718 #define RX_DRIVING_MASK 0x6000
721 #define PHY_PATCH_LOCK 0x0001
724 #define AD_MASK 0xfee0
725 #define BND_MASK 0x0004
726 #define BD_MASK 0x0001
728 #define PASS_THRU_MASK 0x1
730 #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */
732 enum rtl_register_content {
745 #define is_speed_2500(_speed) (((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS))
746 #define is_flow_control(_speed) (((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow))
748 #define RTL8152_MAX_TX 4
749 #define RTL8152_MAX_RX 10
754 #define RTL8152_RX_MAX_PENDING 4096
755 #define RTL8152_RXFG_HEADSZ 256
757 #define INTR_LINK 0x0004
759 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
760 #define RTL8153_RMS RTL8153_MAX_PACKET
761 #define RTL8152_TX_TIMEOUT (5 * HZ)
762 #define mtu_to_size(m) ((m) + VLAN_ETH_HLEN + ETH_FCS_LEN)
763 #define size_to_mtu(s) ((s) - VLAN_ETH_HLEN - ETH_FCS_LEN)
764 #define rx_reserved_size(x) (mtu_to_size(x) + sizeof(struct rx_desc) + RX_ALIGN)
768 RTL8152_INACCESSIBLE = 0,
778 PROBED_WITH_NO_ERRORS,
782 #define DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB 0x721e
783 #define DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK 0x3054
784 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
785 #define DEVICE_ID_THINKPAD_USB_C_DONGLE 0x720c
786 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
787 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3 0x3062
789 struct tally_counter {
796 __le32 tx_one_collision;
797 __le32 tx_multi_collision;
807 #define RX_LEN_MASK 0x7fff
810 #define RD_UDP_CS BIT(23)
811 #define RD_TCP_CS BIT(22)
812 #define RD_IPV6_CS BIT(20)
813 #define RD_IPV4_CS BIT(19)
816 #define IPF BIT(23) /* IP checksum fail */
817 #define UDPF BIT(22) /* UDP checksum fail */
818 #define TCPF BIT(21) /* TCP checksum fail */
819 #define RX_VLAN_TAG BIT(16)
828 #define TX_FS BIT(31) /* First segment of a packet */
829 #define TX_LS BIT(30) /* Final segment of a packet */
830 #define GTSENDV4 BIT(28)
831 #define GTSENDV6 BIT(27)
832 #define GTTCPHO_SHIFT 18
833 #define GTTCPHO_MAX 0x7fU
834 #define TX_LEN_MAX 0x3ffffU
837 #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
838 #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
839 #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
840 #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
842 #define MSS_MAX 0x7ffU
843 #define TCPHO_SHIFT 17
844 #define TCPHO_MAX 0x7ffU
845 #define TX_VLAN_TAG BIT(16)
851 struct list_head list, info_list;
853 struct r8152 *context;
859 struct list_head list;
861 struct r8152 *context;
870 struct usb_device *udev;
871 struct napi_struct napi;
872 struct usb_interface *intf;
873 struct net_device *netdev;
874 struct urb *intr_urb;
875 struct tx_agg tx_info[RTL8152_MAX_TX];
876 struct list_head rx_info, rx_used;
877 struct list_head rx_done, tx_free;
878 struct sk_buff_head tx_queue, rx_queue;
879 spinlock_t rx_lock, tx_lock;
880 struct delayed_work schedule, hw_phy_work;
881 struct mii_if_info mii;
882 struct mutex control; /* use for hw setting */
883 #ifdef CONFIG_PM_SLEEP
884 struct notifier_block pm_notifier;
886 struct tasklet_struct tx_tl;
889 void (*init)(struct r8152 *tp);
890 int (*enable)(struct r8152 *tp);
891 void (*disable)(struct r8152 *tp);
892 void (*up)(struct r8152 *tp);
893 void (*down)(struct r8152 *tp);
894 void (*unload)(struct r8152 *tp);
895 int (*eee_get)(struct r8152 *tp, struct ethtool_keee *eee);
896 int (*eee_set)(struct r8152 *tp, struct ethtool_keee *eee);
897 bool (*in_nway)(struct r8152 *tp);
898 void (*hw_phy_cfg)(struct r8152 *tp);
899 void (*autosuspend_en)(struct r8152 *tp, bool enable);
900 void (*change_mtu)(struct r8152 *tp);
913 u32 eee_plloff_100:1;
914 u32 eee_plloff_giga:1;
918 u32 ctap_short_off:1;
921 #define RTL_VER_SIZE 32
925 const struct firmware *fw;
927 char version[RTL_VER_SIZE];
928 int (*pre_fw)(struct r8152 *tp);
929 int (*post_fw)(struct r8152 *tp);
946 u32 fc_pause_on, fc_pause_off;
948 unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out;
950 u32 support_2500full:1;
951 u32 lenovo_macpassthru:1;
952 u32 dell_tb_rx_agg_bug:1;
961 unsigned int reg_access_reset_count;
965 * struct fw_block - block type and total length
966 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
967 * RTL_FW_USB and so on.
968 * @length: total length of the current block.
976 * struct fw_header - header of the firmware file
977 * @checksum: checksum of sha256 which is calculated from the whole file
978 * except the checksum field of the file. That is, calculate sha256
979 * from the version field to the end of the file.
980 * @version: version of this firmware.
981 * @blocks: the first firmware block of the file
985 char version[RTL_VER_SIZE];
986 struct fw_block blocks[];
989 enum rtl8152_fw_flags {
1003 enum rtl8152_fw_fixup_cmd {
1015 struct fw_phy_speed_up {
1016 struct fw_block blk_hdr;
1025 struct fw_block blk_hdr;
1026 struct fw_phy_set ver;
1030 struct fw_phy_fixup {
1031 struct fw_block blk_hdr;
1032 struct fw_phy_set setting;
1037 struct fw_phy_union {
1038 struct fw_block blk_hdr;
1041 struct fw_phy_set pre_set[2];
1042 struct fw_phy_set bp[8];
1043 struct fw_phy_set bp_en;
1050 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
1051 * The layout of the firmware block is:
1052 * <struct fw_mac> + <info> + <firmware data>.
1053 * @blk_hdr: firmware descriptor (type, length)
1054 * @fw_offset: offset of the firmware binary data. The start address of
1055 * the data would be the address of struct fw_mac + @fw_offset.
1056 * @fw_reg: the register to load the firmware. Depends on chip.
1057 * @bp_ba_addr: the register to write break point base address. Depends on
1059 * @bp_ba_value: break point base address. Depends on chip.
1060 * @bp_en_addr: the register to write break point enabled mask. Depends
1062 * @bp_en_value: break point enabled mask. Depends on the firmware.
1063 * @bp_start: the start register of break points. Depends on chip.
1064 * @bp_num: the break point number which needs to be set for this firmware.
1065 * Depends on the firmware.
1066 * @bp: break points. Depends on firmware.
1067 * @reserved: reserved space (unused)
1068 * @fw_ver_reg: the register to store the fw version.
1069 * @fw_ver_data: the firmware version of the current type.
1070 * @info: additional information for debugging, and is followed by the
1071 * binary data of firmware.
1074 struct fw_block blk_hdr;
1083 __le16 bp[16]; /* any value determined by firmware */
1091 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
1092 * This is used to set patch key when loading the firmware of PHY.
1093 * @blk_hdr: firmware descriptor (type, length)
1094 * @key_reg: the register to write the patch key.
1095 * @key_data: patch key.
1096 * @reserved: reserved space (unused)
1098 struct fw_phy_patch_key {
1099 struct fw_block blk_hdr;
1106 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
1107 * The layout of the firmware block is:
1108 * <struct fw_phy_nc> + <info> + <firmware data>.
1109 * @blk_hdr: firmware descriptor (type, length)
1110 * @fw_offset: offset of the firmware binary data. The start address of
1111 * the data would be the address of struct fw_phy_nc + @fw_offset.
1112 * @fw_reg: the register to load the firmware. Depends on chip.
1113 * @ba_reg: the register to write the base address. Depends on chip.
1114 * @ba_data: base address. Depends on chip.
1115 * @patch_en_addr: the register of enabling patch mode. Depends on chip.
1116 * @patch_en_value: patch mode enabled mask. Depends on the firmware.
1117 * @mode_reg: the regitster of switching the mode.
1118 * @mode_pre: the mode needing to be set before loading the firmware.
1119 * @mode_post: the mode to be set when finishing to load the firmware.
1120 * @reserved: reserved space (unused)
1121 * @bp_start: the start register of break points. Depends on chip.
1122 * @bp_num: the break point number which needs to be set for this firmware.
1123 * Depends on the firmware.
1124 * @bp: break points. Depends on firmware.
1125 * @info: additional information for debugging, and is followed by the
1126 * binary data of firmware.
1129 struct fw_block blk_hdr;
1134 __le16 patch_en_addr;
1135 __le16 patch_en_value;
1154 RTL_FW_PHY_UNION_NC,
1155 RTL_FW_PHY_UNION_NC1,
1156 RTL_FW_PHY_UNION_NC2,
1157 RTL_FW_PHY_UNION_UC2,
1158 RTL_FW_PHY_UNION_UC,
1159 RTL_FW_PHY_UNION_MISC,
1160 RTL_FW_PHY_SPEED_UP,
1165 RTL_VER_UNKNOWN = 0,
1188 TX_CSUM_SUCCESS = 0,
1193 #define RTL_ADVERTISED_10_HALF BIT(0)
1194 #define RTL_ADVERTISED_10_FULL BIT(1)
1195 #define RTL_ADVERTISED_100_HALF BIT(2)
1196 #define RTL_ADVERTISED_100_FULL BIT(3)
1197 #define RTL_ADVERTISED_1000_HALF BIT(4)
1198 #define RTL_ADVERTISED_1000_FULL BIT(5)
1199 #define RTL_ADVERTISED_2500_FULL BIT(6)
1201 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
1202 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
1204 static const int multicast_filter_limit = 32;
1205 static unsigned int agg_buf_sz = 16384;
1207 #define RTL_LIMITED_TSO_SIZE (size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc))
1209 /* If register access fails then we block access and issue a reset. If this
1210 * happens too many times in a row without a successful access then we stop
1211 * trying to reset and just leave access blocked.
1213 #define REGISTER_ACCESS_MAX_RESETS 3
1215 static void rtl_set_inaccessible(struct r8152 *tp)
1217 set_bit(RTL8152_INACCESSIBLE, &tp->flags);
1218 smp_mb__after_atomic();
1221 static void rtl_set_accessible(struct r8152 *tp)
1223 clear_bit(RTL8152_INACCESSIBLE, &tp->flags);
1224 smp_mb__after_atomic();
1228 int r8152_control_msg(struct r8152 *tp, unsigned int pipe, __u8 request,
1229 __u8 requesttype, __u16 value, __u16 index, void *data,
1230 __u16 size, const char *msg_tag)
1232 struct usb_device *udev = tp->udev;
1235 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1238 ret = usb_control_msg(udev, pipe, request, requesttype,
1239 value, index, data, size,
1240 USB_CTRL_GET_TIMEOUT);
1242 /* No need to issue a reset to report an error if the USB device got
1243 * unplugged; just return immediately.
1248 /* If the write was successful then we're done */
1250 tp->reg_access_reset_count = 0;
1255 "Failed to %s %d bytes at %#06x/%#06x (%d)\n",
1256 msg_tag, size, value, index, ret);
1258 /* Block all future register access until we reset. Much of the code
1259 * in the driver doesn't check for errors. Notably, many parts of the
1260 * driver do a read/modify/write of a register value without
1261 * confirming that the read succeeded. Writing back modified garbage
1262 * like this can fully wedge the adapter, requiring a power cycle.
1264 rtl_set_inaccessible(tp);
1266 /* If probe hasn't yet finished, then we'll request a retry of the
1267 * whole probe routine if we get any control transfer errors. We
1268 * never have to clear this bit since we free/reallocate the whole "tp"
1269 * structure if we retry probe.
1271 if (!test_bit(PROBED_WITH_NO_ERRORS, &tp->flags)) {
1272 set_bit(PROBE_SHOULD_RETRY, &tp->flags);
1276 /* Failing to access registers in pre-reset is not surprising since we
1277 * wouldn't be resetting if things were behaving normally. The register
1278 * access we do in pre-reset isn't truly mandatory--we're just reusing
1279 * the disable() function and trying to be nice by powering the
1280 * adapter down before resetting it. Thus, if we're in pre-reset,
1281 * we'll return right away and not try to queue up yet another reset.
1282 * We know the post-reset is already coming.
1284 if (test_bit(IN_PRE_RESET, &tp->flags))
1287 if (tp->reg_access_reset_count < REGISTER_ACCESS_MAX_RESETS) {
1288 usb_queue_reset_device(tp->intf);
1289 tp->reg_access_reset_count++;
1290 } else if (tp->reg_access_reset_count == REGISTER_ACCESS_MAX_RESETS) {
1292 "Tried to reset %d times; giving up.\n",
1293 REGISTER_ACCESS_MAX_RESETS);
1300 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1305 tmp = kmalloc(size, GFP_KERNEL);
1309 ret = r8152_control_msg(tp, tp->pipe_ctrl_in,
1310 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
1311 value, index, tmp, size, "read");
1314 memset(data, 0xff, size);
1316 memcpy(data, tmp, size);
1324 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1329 tmp = kmemdup(data, size, GFP_KERNEL);
1333 ret = r8152_control_msg(tp, tp->pipe_ctrl_out,
1334 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
1335 value, index, tmp, size, "write");
1342 static void rtl_set_unplug(struct r8152 *tp)
1344 if (tp->udev->state == USB_STATE_NOTATTACHED)
1345 rtl_set_inaccessible(tp);
1348 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
1349 void *data, u16 type)
1354 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1357 /* both size and indix must be 4 bytes align */
1358 if ((size & 3) || !size || (index & 3) || !data)
1361 if ((u32)index + (u32)size > 0xffff)
1366 ret = get_registers(tp, index, type, limit, data);
1374 ret = get_registers(tp, index, type, size, data);
1391 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
1392 u16 size, void *data, u16 type)
1395 u16 byteen_start, byteen_end, byen;
1398 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1401 /* both size and indix must be 4 bytes align */
1402 if ((size & 3) || !size || (index & 3) || !data)
1405 if ((u32)index + (u32)size > 0xffff)
1408 byteen_start = byteen & BYTE_EN_START_MASK;
1409 byteen_end = byteen & BYTE_EN_END_MASK;
1411 byen = byteen_start | (byteen_start << 4);
1413 /* Split the first DWORD if the byte_en is not 0xff */
1414 if (byen != BYTE_EN_DWORD) {
1415 ret = set_registers(tp, index, type | byen, 4, data);
1425 byen = byteen_end | (byteen_end >> 4);
1427 /* Split the last DWORD if the byte_en is not 0xff */
1428 if (byen != BYTE_EN_DWORD)
1433 ret = set_registers(tp, index,
1434 type | BYTE_EN_DWORD,
1443 ret = set_registers(tp, index,
1444 type | BYTE_EN_DWORD,
1456 /* Set the last DWORD */
1457 if (byen != BYTE_EN_DWORD)
1458 ret = set_registers(tp, index, type | byen, 4, data);
1469 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1471 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1475 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1477 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1481 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1483 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1486 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1490 generic_ocp_read(tp, index, sizeof(data), &data, type);
1492 return __le32_to_cpu(data);
1495 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1497 __le32 tmp = __cpu_to_le32(data);
1499 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
1502 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1506 u16 byen = BYTE_EN_WORD;
1507 u8 shift = index & 2;
1512 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
1514 data = __le32_to_cpu(tmp);
1515 data >>= (shift * 8);
1521 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1525 u16 byen = BYTE_EN_WORD;
1526 u8 shift = index & 2;
1532 mask <<= (shift * 8);
1533 data <<= (shift * 8);
1537 tmp = __cpu_to_le32(data);
1539 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1542 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1546 u8 shift = index & 3;
1550 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
1552 data = __le32_to_cpu(tmp);
1553 data >>= (shift * 8);
1559 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1563 u16 byen = BYTE_EN_BYTE;
1564 u8 shift = index & 3;
1570 mask <<= (shift * 8);
1571 data <<= (shift * 8);
1575 tmp = __cpu_to_le32(data);
1577 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1580 static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1582 u16 ocp_base, ocp_index;
1584 ocp_base = addr & 0xf000;
1585 if (ocp_base != tp->ocp_base) {
1586 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1587 tp->ocp_base = ocp_base;
1590 ocp_index = (addr & 0x0fff) | 0xb000;
1591 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1594 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1596 u16 ocp_base, ocp_index;
1598 ocp_base = addr & 0xf000;
1599 if (ocp_base != tp->ocp_base) {
1600 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1601 tp->ocp_base = ocp_base;
1604 ocp_index = (addr & 0x0fff) | 0xb000;
1605 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1608 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
1610 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
1613 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
1615 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
1618 static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1620 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1621 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1624 static u16 sram_read(struct r8152 *tp, u16 addr)
1626 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1627 return ocp_reg_read(tp, OCP_SRAM_DATA);
1630 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1632 struct r8152 *tp = netdev_priv(netdev);
1635 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1638 if (phy_id != R8152_PHY_ID)
1641 ret = r8152_mdio_read(tp, reg);
1647 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1649 struct r8152 *tp = netdev_priv(netdev);
1651 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1654 if (phy_id != R8152_PHY_ID)
1657 r8152_mdio_write(tp, reg, val);
1661 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
1664 rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
1667 static int __rtl8152_set_mac_address(struct net_device *netdev, void *p,
1670 struct r8152 *tp = netdev_priv(netdev);
1671 struct sockaddr *addr = p;
1672 int ret = -EADDRNOTAVAIL;
1674 if (!is_valid_ether_addr(addr->sa_data))
1678 ret = usb_autopm_get_interface(tp->intf);
1683 mutex_lock(&tp->control);
1685 eth_hw_addr_set(netdev, addr->sa_data);
1687 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1688 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1689 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1691 mutex_unlock(&tp->control);
1694 usb_autopm_put_interface(tp->intf);
1699 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1701 return __rtl8152_set_mac_address(netdev, p, false);
1704 /* Devices containing proper chips can support a persistent
1705 * host system provided MAC address.
1706 * Examples of this are Dell TB15 and Dell WD15 docks
1708 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1711 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1712 union acpi_object *obj;
1715 unsigned char buf[6];
1717 acpi_object_type mac_obj_type;
1720 if (tp->lenovo_macpassthru) {
1721 mac_obj_name = "\\MACA";
1722 mac_obj_type = ACPI_TYPE_STRING;
1725 /* test for -AD variant of RTL8153 */
1726 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1727 if ((ocp_data & AD_MASK) == 0x1000) {
1728 /* test for MAC address pass-through bit */
1729 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1730 if ((ocp_data & PASS_THRU_MASK) != 1) {
1731 netif_dbg(tp, probe, tp->netdev,
1732 "No efuse for RTL8153-AD MAC pass through\n");
1736 /* test for RTL8153-BND and RTL8153-BD */
1737 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1738 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1739 netif_dbg(tp, probe, tp->netdev,
1740 "Invalid variant for MAC pass through\n");
1745 mac_obj_name = "\\_SB.AMAC";
1746 mac_obj_type = ACPI_TYPE_BUFFER;
1750 /* returns _AUXMAC_#AABBCCDDEEFF# */
1751 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
1752 obj = (union acpi_object *)buffer.pointer;
1753 if (!ACPI_SUCCESS(status))
1755 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
1756 netif_warn(tp, probe, tp->netdev,
1757 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
1758 obj->type, obj->string.length);
1762 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1763 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1764 netif_warn(tp, probe, tp->netdev,
1765 "Invalid header when reading pass-thru MAC addr\n");
1768 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1769 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1770 netif_warn(tp, probe, tp->netdev,
1771 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1776 memcpy(sa->sa_data, buf, 6);
1777 tp->netdev->addr_assign_type = NET_ADDR_STOLEN;
1778 netif_info(tp, probe, tp->netdev,
1779 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1786 static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1788 struct net_device *dev = tp->netdev;
1791 sa->sa_family = dev->type;
1793 ret = eth_platform_get_mac_address(&tp->udev->dev, sa->sa_data);
1795 if (tp->version == RTL_VER_01) {
1796 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1798 /* if device doesn't support MAC pass through this will
1799 * be expected to be non-zero
1801 ret = vendor_mac_passthru_addr_read(tp, sa);
1803 ret = pla_ocp_read(tp, PLA_BACKUP, 8,
1809 netif_err(tp, probe, dev, "Get ether addr fail\n");
1810 } else if (!is_valid_ether_addr(sa->sa_data)) {
1811 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1813 eth_hw_addr_random(dev);
1814 ether_addr_copy(sa->sa_data, dev->dev_addr);
1815 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1823 static int set_ethernet_addr(struct r8152 *tp, bool in_resume)
1825 struct net_device *dev = tp->netdev;
1829 ret = determine_ethernet_addr(tp, &sa);
1833 if (tp->version == RTL_VER_01)
1834 eth_hw_addr_set(dev, sa.sa_data);
1836 ret = __rtl8152_set_mac_address(dev, &sa, in_resume);
1841 static void read_bulk_callback(struct urb *urb)
1843 struct net_device *netdev;
1844 int status = urb->status;
1847 unsigned long flags;
1857 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1860 if (!test_bit(WORK_ENABLE, &tp->flags))
1863 netdev = tp->netdev;
1865 /* When link down, the driver would cancel all bulks. */
1866 /* This avoid the re-submitting bulk */
1867 if (!netif_carrier_ok(netdev))
1870 usb_mark_last_busy(tp->udev);
1874 if (urb->actual_length < ETH_ZLEN)
1877 spin_lock_irqsave(&tp->rx_lock, flags);
1878 list_add_tail(&agg->list, &tp->rx_done);
1879 spin_unlock_irqrestore(&tp->rx_lock, flags);
1880 napi_schedule(&tp->napi);
1884 netif_device_detach(tp->netdev);
1887 urb->actual_length = 0;
1888 spin_lock_irqsave(&tp->rx_lock, flags);
1889 list_add_tail(&agg->list, &tp->rx_done);
1890 spin_unlock_irqrestore(&tp->rx_lock, flags);
1891 set_bit(RX_EPROTO, &tp->flags);
1892 schedule_delayed_work(&tp->schedule, 1);
1895 return; /* the urb is in unlink state */
1897 if (net_ratelimit())
1898 netdev_warn(netdev, "maybe reset is needed?\n");
1901 if (net_ratelimit())
1902 netdev_warn(netdev, "Rx status %d\n", status);
1906 r8152_submit_rx(tp, agg, GFP_ATOMIC);
1909 static void write_bulk_callback(struct urb *urb)
1911 struct net_device_stats *stats;
1912 struct net_device *netdev;
1915 unsigned long flags;
1916 int status = urb->status;
1926 netdev = tp->netdev;
1927 stats = &netdev->stats;
1929 if (net_ratelimit())
1930 netdev_warn(netdev, "Tx status %d\n", status);
1931 stats->tx_errors += agg->skb_num;
1933 stats->tx_packets += agg->skb_num;
1934 stats->tx_bytes += agg->skb_len;
1937 spin_lock_irqsave(&tp->tx_lock, flags);
1938 list_add_tail(&agg->list, &tp->tx_free);
1939 spin_unlock_irqrestore(&tp->tx_lock, flags);
1941 usb_autopm_put_interface_async(tp->intf);
1943 if (!netif_carrier_ok(netdev))
1946 if (!test_bit(WORK_ENABLE, &tp->flags))
1949 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1952 if (!skb_queue_empty(&tp->tx_queue))
1953 tasklet_schedule(&tp->tx_tl);
1956 static void intr_callback(struct urb *urb)
1960 int status = urb->status;
1967 if (!test_bit(WORK_ENABLE, &tp->flags))
1970 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
1974 case 0: /* success */
1976 case -ECONNRESET: /* unlink */
1978 netif_device_detach(tp->netdev);
1982 netif_info(tp, intr, tp->netdev,
1983 "Stop submitting intr, status %d\n", status);
1986 if (net_ratelimit())
1987 netif_info(tp, intr, tp->netdev,
1988 "intr status -EOVERFLOW\n");
1990 /* -EPIPE: should clear the halt */
1992 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1996 d = urb->transfer_buffer;
1997 if (INTR_LINK & __le16_to_cpu(d[0])) {
1998 if (!netif_carrier_ok(tp->netdev)) {
1999 set_bit(RTL8152_LINK_CHG, &tp->flags);
2000 schedule_delayed_work(&tp->schedule, 0);
2003 if (netif_carrier_ok(tp->netdev)) {
2004 netif_stop_queue(tp->netdev);
2005 set_bit(RTL8152_LINK_CHG, &tp->flags);
2006 schedule_delayed_work(&tp->schedule, 0);
2011 res = usb_submit_urb(urb, GFP_ATOMIC);
2012 if (res == -ENODEV) {
2014 netif_device_detach(tp->netdev);
2016 netif_err(tp, intr, tp->netdev,
2017 "can't resubmit intr, status %d\n", res);
2021 static inline void *rx_agg_align(void *data)
2023 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
2026 static inline void *tx_agg_align(void *data)
2028 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
2031 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
2033 list_del(&agg->info_list);
2035 usb_free_urb(agg->urb);
2036 put_page(agg->page);
2039 atomic_dec(&tp->rx_count);
2042 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
2044 struct net_device *netdev = tp->netdev;
2045 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
2046 unsigned int order = get_order(tp->rx_buf_sz);
2047 struct rx_agg *rx_agg;
2048 unsigned long flags;
2050 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
2054 rx_agg->page = alloc_pages(mflags | __GFP_COMP | __GFP_NOWARN, order);
2058 rx_agg->buffer = page_address(rx_agg->page);
2060 rx_agg->urb = usb_alloc_urb(0, mflags);
2064 rx_agg->context = tp;
2066 INIT_LIST_HEAD(&rx_agg->list);
2067 INIT_LIST_HEAD(&rx_agg->info_list);
2068 spin_lock_irqsave(&tp->rx_lock, flags);
2069 list_add_tail(&rx_agg->info_list, &tp->rx_info);
2070 spin_unlock_irqrestore(&tp->rx_lock, flags);
2072 atomic_inc(&tp->rx_count);
2077 __free_pages(rx_agg->page, order);
2083 static void free_all_mem(struct r8152 *tp)
2085 struct rx_agg *agg, *agg_next;
2086 unsigned long flags;
2089 spin_lock_irqsave(&tp->rx_lock, flags);
2091 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
2092 free_rx_agg(tp, agg);
2094 spin_unlock_irqrestore(&tp->rx_lock, flags);
2096 WARN_ON(atomic_read(&tp->rx_count));
2098 for (i = 0; i < RTL8152_MAX_TX; i++) {
2099 usb_free_urb(tp->tx_info[i].urb);
2100 tp->tx_info[i].urb = NULL;
2102 kfree(tp->tx_info[i].buffer);
2103 tp->tx_info[i].buffer = NULL;
2104 tp->tx_info[i].head = NULL;
2107 usb_free_urb(tp->intr_urb);
2108 tp->intr_urb = NULL;
2110 kfree(tp->intr_buff);
2111 tp->intr_buff = NULL;
2114 static int alloc_all_mem(struct r8152 *tp)
2116 struct net_device *netdev = tp->netdev;
2117 struct usb_interface *intf = tp->intf;
2118 struct usb_host_interface *alt = intf->cur_altsetting;
2119 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
2122 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
2124 spin_lock_init(&tp->rx_lock);
2125 spin_lock_init(&tp->tx_lock);
2126 INIT_LIST_HEAD(&tp->rx_info);
2127 INIT_LIST_HEAD(&tp->tx_free);
2128 INIT_LIST_HEAD(&tp->rx_done);
2129 skb_queue_head_init(&tp->tx_queue);
2130 skb_queue_head_init(&tp->rx_queue);
2131 atomic_set(&tp->rx_count, 0);
2133 for (i = 0; i < RTL8152_MAX_RX; i++) {
2134 if (!alloc_rx_agg(tp, GFP_KERNEL))
2138 for (i = 0; i < RTL8152_MAX_TX; i++) {
2142 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
2146 if (buf != tx_agg_align(buf)) {
2148 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
2154 urb = usb_alloc_urb(0, GFP_KERNEL);
2160 INIT_LIST_HEAD(&tp->tx_info[i].list);
2161 tp->tx_info[i].context = tp;
2162 tp->tx_info[i].urb = urb;
2163 tp->tx_info[i].buffer = buf;
2164 tp->tx_info[i].head = tx_agg_align(buf);
2166 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
2169 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2173 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
2177 tp->intr_interval = (int)ep_intr->desc.bInterval;
2178 usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr,
2179 tp->intr_buff, INTBUFSIZE, intr_callback,
2180 tp, tp->intr_interval);
2189 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
2191 struct tx_agg *agg = NULL;
2192 unsigned long flags;
2194 if (list_empty(&tp->tx_free))
2197 spin_lock_irqsave(&tp->tx_lock, flags);
2198 if (!list_empty(&tp->tx_free)) {
2199 struct list_head *cursor;
2201 cursor = tp->tx_free.next;
2202 list_del_init(cursor);
2203 agg = list_entry(cursor, struct tx_agg, list);
2205 spin_unlock_irqrestore(&tp->tx_lock, flags);
2210 /* r8152_csum_workaround()
2211 * The hw limits the value of the transport offset. When the offset is out of
2212 * range, calculate the checksum by sw.
2214 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
2215 struct sk_buff_head *list)
2217 if (skb_shinfo(skb)->gso_size) {
2218 netdev_features_t features = tp->netdev->features;
2219 struct sk_buff *segs, *seg, *next;
2220 struct sk_buff_head seg_list;
2222 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
2223 segs = skb_gso_segment(skb, features);
2224 if (IS_ERR(segs) || !segs)
2227 __skb_queue_head_init(&seg_list);
2229 skb_list_walk_safe(segs, seg, next) {
2230 skb_mark_not_on_list(seg);
2231 __skb_queue_tail(&seg_list, seg);
2234 skb_queue_splice(&seg_list, list);
2236 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2237 if (skb_checksum_help(skb) < 0)
2240 __skb_queue_head(list, skb);
2242 struct net_device_stats *stats;
2245 stats = &tp->netdev->stats;
2246 stats->tx_dropped++;
2251 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
2253 if (skb_vlan_tag_present(skb)) {
2256 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
2257 desc->opts2 |= cpu_to_le32(opts2);
2261 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
2263 u32 opts2 = le32_to_cpu(desc->opts2);
2265 if (opts2 & RX_VLAN_TAG)
2266 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2267 swab16(opts2 & 0xffff));
2270 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
2271 struct sk_buff *skb, u32 len)
2273 u32 mss = skb_shinfo(skb)->gso_size;
2274 u32 opts1, opts2 = 0;
2275 int ret = TX_CSUM_SUCCESS;
2277 WARN_ON_ONCE(len > TX_LEN_MAX);
2279 opts1 = len | TX_FS | TX_LS;
2282 u32 transport_offset = (u32)skb_transport_offset(skb);
2284 if (transport_offset > GTTCPHO_MAX) {
2285 netif_warn(tp, tx_err, tp->netdev,
2286 "Invalid transport offset 0x%x for TSO\n",
2292 switch (vlan_get_protocol(skb)) {
2293 case htons(ETH_P_IP):
2297 case htons(ETH_P_IPV6):
2298 if (skb_cow_head(skb, 0)) {
2302 tcp_v6_gso_csum_prep(skb);
2311 opts1 |= transport_offset << GTTCPHO_SHIFT;
2312 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
2313 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2314 u32 transport_offset = (u32)skb_transport_offset(skb);
2317 if (transport_offset > TCPHO_MAX) {
2318 netif_warn(tp, tx_err, tp->netdev,
2319 "Invalid transport offset 0x%x\n",
2325 switch (vlan_get_protocol(skb)) {
2326 case htons(ETH_P_IP):
2328 ip_protocol = ip_hdr(skb)->protocol;
2331 case htons(ETH_P_IPV6):
2333 ip_protocol = ipv6_hdr(skb)->nexthdr;
2337 ip_protocol = IPPROTO_RAW;
2341 if (ip_protocol == IPPROTO_TCP)
2343 else if (ip_protocol == IPPROTO_UDP)
2348 opts2 |= transport_offset << TCPHO_SHIFT;
2351 desc->opts2 = cpu_to_le32(opts2);
2352 desc->opts1 = cpu_to_le32(opts1);
2358 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
2360 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2364 __skb_queue_head_init(&skb_head);
2365 spin_lock(&tx_queue->lock);
2366 skb_queue_splice_init(tx_queue, &skb_head);
2367 spin_unlock(&tx_queue->lock);
2369 tx_data = agg->head;
2372 remain = agg_buf_sz;
2374 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
2375 struct tx_desc *tx_desc;
2376 struct sk_buff *skb;
2379 skb = __skb_dequeue(&skb_head);
2383 len = skb->len + sizeof(*tx_desc);
2386 __skb_queue_head(&skb_head, skb);
2390 tx_data = tx_agg_align(tx_data);
2391 tx_desc = (struct tx_desc *)tx_data;
2393 if (r8152_tx_csum(tp, tx_desc, skb, skb->len)) {
2394 r8152_csum_workaround(tp, skb, &skb_head);
2398 rtl_tx_vlan_tag(tx_desc, skb);
2400 tx_data += sizeof(*tx_desc);
2403 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
2404 struct net_device_stats *stats = &tp->netdev->stats;
2406 stats->tx_dropped++;
2407 dev_kfree_skb_any(skb);
2408 tx_data -= sizeof(*tx_desc);
2413 agg->skb_len += len;
2414 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
2416 dev_kfree_skb_any(skb);
2418 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
2420 if (tp->dell_tb_rx_agg_bug)
2424 if (!skb_queue_empty(&skb_head)) {
2425 spin_lock(&tx_queue->lock);
2426 skb_queue_splice(&skb_head, tx_queue);
2427 spin_unlock(&tx_queue->lock);
2430 netif_tx_lock(tp->netdev);
2432 if (netif_queue_stopped(tp->netdev) &&
2433 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
2434 netif_wake_queue(tp->netdev);
2436 netif_tx_unlock(tp->netdev);
2438 ret = usb_autopm_get_interface_async(tp->intf);
2442 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out,
2443 agg->head, (int)(tx_data - (u8 *)agg->head),
2444 (usb_complete_t)write_bulk_callback, agg);
2446 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
2448 usb_autopm_put_interface_async(tp->intf);
2454 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
2456 u8 checksum = CHECKSUM_NONE;
2459 if (!(tp->netdev->features & NETIF_F_RXCSUM))
2462 opts2 = le32_to_cpu(rx_desc->opts2);
2463 opts3 = le32_to_cpu(rx_desc->opts3);
2465 if (opts2 & RD_IPV4_CS) {
2467 checksum = CHECKSUM_NONE;
2468 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2469 checksum = CHECKSUM_UNNECESSARY;
2470 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2471 checksum = CHECKSUM_UNNECESSARY;
2472 } else if (opts2 & RD_IPV6_CS) {
2473 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2474 checksum = CHECKSUM_UNNECESSARY;
2475 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2476 checksum = CHECKSUM_UNNECESSARY;
2483 static inline bool rx_count_exceed(struct r8152 *tp)
2485 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2488 static inline int agg_offset(struct rx_agg *agg, void *addr)
2490 return (int)(addr - agg->buffer);
2493 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2495 struct rx_agg *agg, *agg_next, *agg_free = NULL;
2496 unsigned long flags;
2498 spin_lock_irqsave(&tp->rx_lock, flags);
2500 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2501 if (page_count(agg->page) == 1) {
2503 list_del_init(&agg->list);
2507 if (rx_count_exceed(tp)) {
2508 list_del_init(&agg->list);
2509 free_rx_agg(tp, agg);
2515 spin_unlock_irqrestore(&tp->rx_lock, flags);
2517 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
2518 agg_free = alloc_rx_agg(tp, mflags);
2523 static int rx_bottom(struct r8152 *tp, int budget)
2525 unsigned long flags;
2526 struct list_head *cursor, *next, rx_queue;
2527 int ret = 0, work_done = 0;
2528 struct napi_struct *napi = &tp->napi;
2530 if (!skb_queue_empty(&tp->rx_queue)) {
2531 while (work_done < budget) {
2532 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2533 struct net_device *netdev = tp->netdev;
2534 struct net_device_stats *stats = &netdev->stats;
2535 unsigned int pkt_len;
2541 napi_gro_receive(napi, skb);
2543 stats->rx_packets++;
2544 stats->rx_bytes += pkt_len;
2548 if (list_empty(&tp->rx_done) || work_done >= budget)
2551 clear_bit(RX_EPROTO, &tp->flags);
2552 INIT_LIST_HEAD(&rx_queue);
2553 spin_lock_irqsave(&tp->rx_lock, flags);
2554 list_splice_init(&tp->rx_done, &rx_queue);
2555 spin_unlock_irqrestore(&tp->rx_lock, flags);
2557 list_for_each_safe(cursor, next, &rx_queue) {
2558 struct rx_desc *rx_desc;
2559 struct rx_agg *agg, *agg_free;
2564 /* A bulk transfer of USB may contain may packets, so the
2565 * total packets may more than the budget. Deal with all
2566 * packets in current bulk transfer, and stop to handle the
2567 * next bulk transfer until next schedule, if budget is
2570 if (work_done >= budget)
2573 list_del_init(cursor);
2575 agg = list_entry(cursor, struct rx_agg, list);
2577 if (urb->status != 0 || urb->actual_length < ETH_ZLEN)
2580 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2582 rx_desc = agg->buffer;
2583 rx_data = agg->buffer;
2584 len_used += sizeof(struct rx_desc);
2586 while (urb->actual_length > len_used) {
2587 struct net_device *netdev = tp->netdev;
2588 struct net_device_stats *stats = &netdev->stats;
2589 unsigned int pkt_len, rx_frag_head_sz, len;
2590 struct sk_buff *skb;
2593 WARN_ON_ONCE(skb_queue_len(&tp->rx_queue) >= 1000);
2595 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
2596 if (pkt_len < ETH_ZLEN)
2599 len_used += pkt_len;
2600 if (urb->actual_length < len_used)
2603 pkt_len -= ETH_FCS_LEN;
2605 rx_data += sizeof(struct rx_desc);
2607 if (!agg_free || tp->rx_copybreak > len)
2613 /* If the budget is exhausted, the packet
2614 * would be queued in the driver. That is,
2615 * napi_gro_frags() wouldn't be called, so
2616 * we couldn't use napi_get_frags().
2618 if (work_done >= budget) {
2619 rx_frag_head_sz = tp->rx_copybreak;
2620 skb = napi_alloc_skb(napi,
2623 rx_frag_head_sz = 0;
2624 skb = napi_get_frags(napi);
2627 rx_frag_head_sz = 0;
2628 skb = napi_alloc_skb(napi, len);
2632 stats->rx_dropped++;
2636 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
2637 rtl_rx_vlan_tag(rx_desc, skb);
2640 if (rx_frag_head_sz) {
2641 memcpy(skb->data, rx_data,
2643 skb_put(skb, rx_frag_head_sz);
2644 len -= rx_frag_head_sz;
2645 rx_data += rx_frag_head_sz;
2646 skb->protocol = eth_type_trans(skb,
2650 skb_add_rx_frag(skb, 0, agg->page,
2651 agg_offset(agg, rx_data),
2652 len, SKB_DATA_ALIGN(len));
2653 get_page(agg->page);
2655 memcpy(skb->data, rx_data, len);
2657 skb->protocol = eth_type_trans(skb, netdev);
2660 if (work_done < budget) {
2662 napi_gro_frags(napi);
2664 napi_gro_receive(napi, skb);
2667 stats->rx_packets++;
2668 stats->rx_bytes += pkt_len;
2670 __skb_queue_tail(&tp->rx_queue, skb);
2674 rx_data = rx_agg_align(rx_data + len + ETH_FCS_LEN);
2675 rx_desc = (struct rx_desc *)rx_data;
2676 len_used = agg_offset(agg, rx_data);
2677 len_used += sizeof(struct rx_desc);
2680 WARN_ON(!agg_free && page_count(agg->page) > 1);
2683 spin_lock_irqsave(&tp->rx_lock, flags);
2684 if (page_count(agg->page) == 1) {
2685 list_add(&agg_free->list, &tp->rx_used);
2687 list_add_tail(&agg->list, &tp->rx_used);
2691 spin_unlock_irqrestore(&tp->rx_lock, flags);
2696 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2698 urb->actual_length = 0;
2699 list_add_tail(&agg->list, next);
2703 /* Splice the remained list back to rx_done for next schedule */
2704 if (!list_empty(&rx_queue)) {
2705 spin_lock_irqsave(&tp->rx_lock, flags);
2706 list_splice(&rx_queue, &tp->rx_done);
2707 spin_unlock_irqrestore(&tp->rx_lock, flags);
2714 static void tx_bottom(struct r8152 *tp)
2719 struct net_device *netdev = tp->netdev;
2722 if (skb_queue_empty(&tp->tx_queue))
2725 agg = r8152_get_tx_agg(tp);
2729 res = r8152_tx_agg_fill(tp, agg);
2733 if (res == -ENODEV) {
2735 netif_device_detach(netdev);
2737 struct net_device_stats *stats = &netdev->stats;
2738 unsigned long flags;
2740 netif_warn(tp, tx_err, netdev,
2741 "failed tx_urb %d\n", res);
2742 stats->tx_dropped += agg->skb_num;
2744 spin_lock_irqsave(&tp->tx_lock, flags);
2745 list_add_tail(&agg->list, &tp->tx_free);
2746 spin_unlock_irqrestore(&tp->tx_lock, flags);
2751 static void bottom_half(struct tasklet_struct *t)
2753 struct r8152 *tp = from_tasklet(tp, t, tx_tl);
2755 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
2758 if (!test_bit(WORK_ENABLE, &tp->flags))
2761 /* When link down, the driver would cancel all bulks. */
2762 /* This avoid the re-submitting bulk */
2763 if (!netif_carrier_ok(tp->netdev))
2766 clear_bit(SCHEDULE_TASKLET, &tp->flags);
2771 static int r8152_poll(struct napi_struct *napi, int budget)
2773 struct r8152 *tp = container_of(napi, struct r8152, napi);
2779 work_done = rx_bottom(tp, budget);
2781 if (work_done < budget) {
2782 if (!napi_complete_done(napi, work_done))
2784 if (!list_empty(&tp->rx_done))
2785 napi_schedule(napi);
2793 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2797 /* The rx would be stopped, so skip submitting */
2798 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) ||
2799 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2802 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in,
2803 agg->buffer, tp->rx_buf_sz,
2804 (usb_complete_t)read_bulk_callback, agg);
2806 ret = usb_submit_urb(agg->urb, mem_flags);
2807 if (ret == -ENODEV) {
2809 netif_device_detach(tp->netdev);
2811 struct urb *urb = agg->urb;
2812 unsigned long flags;
2814 urb->actual_length = 0;
2815 spin_lock_irqsave(&tp->rx_lock, flags);
2816 list_add_tail(&agg->list, &tp->rx_done);
2817 spin_unlock_irqrestore(&tp->rx_lock, flags);
2819 netif_err(tp, rx_err, tp->netdev,
2820 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2822 napi_schedule(&tp->napi);
2828 static void rtl_drop_queued_tx(struct r8152 *tp)
2830 struct net_device_stats *stats = &tp->netdev->stats;
2831 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2832 struct sk_buff *skb;
2834 if (skb_queue_empty(tx_queue))
2837 __skb_queue_head_init(&skb_head);
2838 spin_lock_bh(&tx_queue->lock);
2839 skb_queue_splice_init(tx_queue, &skb_head);
2840 spin_unlock_bh(&tx_queue->lock);
2842 while ((skb = __skb_dequeue(&skb_head))) {
2844 stats->tx_dropped++;
2848 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2850 struct r8152 *tp = netdev_priv(netdev);
2852 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
2854 usb_queue_reset_device(tp->intf);
2857 static void rtl8152_set_rx_mode(struct net_device *netdev)
2859 struct r8152 *tp = netdev_priv(netdev);
2861 if (netif_carrier_ok(netdev)) {
2862 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2863 schedule_delayed_work(&tp->schedule, 0);
2867 static void _rtl8152_set_rx_mode(struct net_device *netdev)
2869 struct r8152 *tp = netdev_priv(netdev);
2870 u32 mc_filter[2]; /* Multicast hash filter */
2874 netif_stop_queue(netdev);
2875 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2876 ocp_data &= ~RCR_ACPT_ALL;
2877 ocp_data |= RCR_AB | RCR_APM;
2879 if (netdev->flags & IFF_PROMISC) {
2880 /* Unconditionally log net taps. */
2881 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2882 ocp_data |= RCR_AM | RCR_AAP;
2883 mc_filter[1] = 0xffffffff;
2884 mc_filter[0] = 0xffffffff;
2885 } else if ((netdev->flags & IFF_MULTICAST &&
2886 netdev_mc_count(netdev) > multicast_filter_limit) ||
2887 (netdev->flags & IFF_ALLMULTI)) {
2888 /* Too many to filter perfectly -- accept all multicasts. */
2890 mc_filter[1] = 0xffffffff;
2891 mc_filter[0] = 0xffffffff;
2896 if (netdev->flags & IFF_MULTICAST) {
2897 struct netdev_hw_addr *ha;
2899 netdev_for_each_mc_addr(ha, netdev) {
2900 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2902 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2908 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2909 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
2911 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
2912 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2913 netif_wake_queue(netdev);
2916 static netdev_features_t
2917 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2918 netdev_features_t features)
2920 u32 mss = skb_shinfo(skb)->gso_size;
2921 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2923 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) &&
2924 skb_transport_offset(skb) > max_offset)
2925 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2926 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2927 features &= ~NETIF_F_GSO_MASK;
2932 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
2933 struct net_device *netdev)
2935 struct r8152 *tp = netdev_priv(netdev);
2937 skb_tx_timestamp(skb);
2939 skb_queue_tail(&tp->tx_queue, skb);
2941 if (!list_empty(&tp->tx_free)) {
2942 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2943 set_bit(SCHEDULE_TASKLET, &tp->flags);
2944 schedule_delayed_work(&tp->schedule, 0);
2946 usb_mark_last_busy(tp->udev);
2947 tasklet_schedule(&tp->tx_tl);
2949 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
2950 netif_stop_queue(netdev);
2953 return NETDEV_TX_OK;
2956 static void r8152b_reset_packet_filter(struct r8152 *tp)
2960 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2961 ocp_data &= ~FMC_FCR_MCU_EN;
2962 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2963 ocp_data |= FMC_FCR_MCU_EN;
2964 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2967 static void rtl8152_nic_reset(struct r8152 *tp)
2972 switch (tp->version) {
2976 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2978 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2980 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2981 ocp_data &= ~BMU_RESET_EP_IN;
2982 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2984 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2985 ocp_data |= CDC_ECM_EN;
2986 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2988 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2990 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2992 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2993 ocp_data |= BMU_RESET_EP_IN;
2994 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2996 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2997 ocp_data &= ~CDC_ECM_EN;
2998 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3002 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
3004 for (i = 0; i < 1000; i++) {
3005 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3007 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
3009 usleep_range(100, 400);
3015 static void set_tx_qlen(struct r8152 *tp)
3017 tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc));
3020 static inline u16 rtl8152_get_speed(struct r8152 *tp)
3022 return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
3025 static void rtl_eee_plus_en(struct r8152 *tp, bool enable)
3029 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
3031 ocp_data |= EEEP_CR_EEEP_TX;
3033 ocp_data &= ~EEEP_CR_EEEP_TX;
3034 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
3037 static void rtl_set_eee_plus(struct r8152 *tp)
3039 if (rtl8152_get_speed(tp) & _10bps)
3040 rtl_eee_plus_en(tp, true);
3042 rtl_eee_plus_en(tp, false);
3045 static void rxdy_gated_en(struct r8152 *tp, bool enable)
3049 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
3051 ocp_data |= RXDY_GATED_EN;
3053 ocp_data &= ~RXDY_GATED_EN;
3054 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
3057 static int rtl_start_rx(struct r8152 *tp)
3059 struct rx_agg *agg, *agg_next;
3060 struct list_head tmp_list;
3061 unsigned long flags;
3064 INIT_LIST_HEAD(&tmp_list);
3066 spin_lock_irqsave(&tp->rx_lock, flags);
3068 INIT_LIST_HEAD(&tp->rx_done);
3069 INIT_LIST_HEAD(&tp->rx_used);
3071 list_splice_init(&tp->rx_info, &tmp_list);
3073 spin_unlock_irqrestore(&tp->rx_lock, flags);
3075 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
3076 INIT_LIST_HEAD(&agg->list);
3078 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
3079 if (++i > RTL8152_MAX_RX) {
3080 spin_lock_irqsave(&tp->rx_lock, flags);
3081 list_add_tail(&agg->list, &tp->rx_used);
3082 spin_unlock_irqrestore(&tp->rx_lock, flags);
3083 } else if (unlikely(ret < 0)) {
3084 spin_lock_irqsave(&tp->rx_lock, flags);
3085 list_add_tail(&agg->list, &tp->rx_done);
3086 spin_unlock_irqrestore(&tp->rx_lock, flags);
3088 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
3092 spin_lock_irqsave(&tp->rx_lock, flags);
3093 WARN_ON(!list_empty(&tp->rx_info));
3094 list_splice(&tmp_list, &tp->rx_info);
3095 spin_unlock_irqrestore(&tp->rx_lock, flags);
3100 static int rtl_stop_rx(struct r8152 *tp)
3102 struct rx_agg *agg, *agg_next;
3103 struct list_head tmp_list;
3104 unsigned long flags;
3106 INIT_LIST_HEAD(&tmp_list);
3108 /* The usb_kill_urb() couldn't be used in atomic.
3109 * Therefore, move the list of rx_info to a tmp one.
3110 * Then, list_for_each_entry_safe could be used without
3114 spin_lock_irqsave(&tp->rx_lock, flags);
3115 list_splice_init(&tp->rx_info, &tmp_list);
3116 spin_unlock_irqrestore(&tp->rx_lock, flags);
3118 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
3119 /* At least RTL8152_MAX_RX rx_agg have the page_count being
3120 * equal to 1, so the other ones could be freed safely.
3122 if (page_count(agg->page) > 1)
3123 free_rx_agg(tp, agg);
3125 usb_kill_urb(agg->urb);
3128 /* Move back the list of temp to the rx_info */
3129 spin_lock_irqsave(&tp->rx_lock, flags);
3130 WARN_ON(!list_empty(&tp->rx_info));
3131 list_splice(&tmp_list, &tp->rx_info);
3132 spin_unlock_irqrestore(&tp->rx_lock, flags);
3134 while (!skb_queue_empty(&tp->rx_queue))
3135 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
3140 static void rtl_set_ifg(struct r8152 *tp, u16 speed)
3144 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3145 ocp_data &= ~IFG_MASK;
3146 if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) {
3147 ocp_data |= IFG_144NS;
3148 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
3150 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
3151 ocp_data &= ~TX10MIDLE_EN;
3152 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
3154 ocp_data |= IFG_96NS;
3155 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
3157 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
3158 ocp_data |= TX10MIDLE_EN;
3159 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
3163 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
3165 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
3166 OWN_UPDATE | OWN_CLEAR);
3169 static int rtl_enable(struct r8152 *tp)
3173 r8152b_reset_packet_filter(tp);
3175 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
3176 ocp_data |= CR_RE | CR_TE;
3177 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
3179 switch (tp->version) {
3189 r8153b_rx_agg_chg_indicate(tp);
3193 rxdy_gated_en(tp, false);
3198 static int rtl8152_enable(struct r8152 *tp)
3200 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3204 rtl_set_eee_plus(tp);
3206 return rtl_enable(tp);
3209 static void r8153_set_rx_early_timeout(struct r8152 *tp)
3211 u32 ocp_data = tp->coalesce / 8;
3213 switch (tp->version) {
3218 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3225 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
3226 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
3228 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3230 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3239 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3241 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3250 static void r8153_set_rx_early_size(struct r8152 *tp)
3252 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
3254 switch (tp->version) {
3259 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3265 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3274 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3283 static int rtl8153_enable(struct r8152 *tp)
3287 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3291 rtl_set_eee_plus(tp);
3292 r8153_set_rx_early_timeout(tp);
3293 r8153_set_rx_early_size(tp);
3295 rtl_set_ifg(tp, rtl8152_get_speed(tp));
3297 switch (tp->version) {
3300 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
3301 ocp_data &= ~FC_PATCH_TASK;
3302 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3303 usleep_range(1000, 2000);
3304 ocp_data |= FC_PATCH_TASK;
3305 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3311 return rtl_enable(tp);
3314 static void rtl_disable(struct r8152 *tp)
3319 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
3320 rtl_drop_queued_tx(tp);
3324 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3325 ocp_data &= ~RCR_ACPT_ALL;
3326 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3328 rtl_drop_queued_tx(tp);
3330 for (i = 0; i < RTL8152_MAX_TX; i++)
3331 usb_kill_urb(tp->tx_info[i].urb);
3333 rxdy_gated_en(tp, true);
3335 for (i = 0; i < 1000; i++) {
3336 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3338 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3339 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
3341 usleep_range(1000, 2000);
3344 for (i = 0; i < 1000; i++) {
3345 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3347 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
3349 usleep_range(1000, 2000);
3354 rtl8152_nic_reset(tp);
3357 static void r8152_power_cut_en(struct r8152 *tp, bool enable)
3361 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
3363 ocp_data |= POWER_CUT;
3365 ocp_data &= ~POWER_CUT;
3366 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
3368 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
3369 ocp_data &= ~RESUME_INDICATE;
3370 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
3373 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
3377 switch (tp->version) {
3388 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
3390 ocp_data |= CPCR_RX_VLAN;
3392 ocp_data &= ~CPCR_RX_VLAN;
3393 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
3403 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1);
3405 ocp_data |= OUTER_VLAN | INNER_VLAN;
3407 ocp_data &= ~(OUTER_VLAN | INNER_VLAN);
3408 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data);
3413 static int rtl8152_set_features(struct net_device *dev,
3414 netdev_features_t features)
3416 netdev_features_t changed = features ^ dev->features;
3417 struct r8152 *tp = netdev_priv(dev);
3420 ret = usb_autopm_get_interface(tp->intf);
3424 mutex_lock(&tp->control);
3426 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3427 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3428 rtl_rx_vlan_en(tp, true);
3430 rtl_rx_vlan_en(tp, false);
3433 mutex_unlock(&tp->control);
3435 usb_autopm_put_interface(tp->intf);
3441 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
3443 static u32 __rtl_get_wol(struct r8152 *tp)
3448 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3449 if (ocp_data & LINK_ON_WAKE_EN)
3450 wolopts |= WAKE_PHY;
3452 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3453 if (ocp_data & UWF_EN)
3454 wolopts |= WAKE_UCAST;
3455 if (ocp_data & BWF_EN)
3456 wolopts |= WAKE_BCAST;
3457 if (ocp_data & MWF_EN)
3458 wolopts |= WAKE_MCAST;
3460 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3461 if (ocp_data & MAGIC_EN)
3462 wolopts |= WAKE_MAGIC;
3467 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
3471 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3473 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3474 ocp_data &= ~LINK_ON_WAKE_EN;
3475 if (wolopts & WAKE_PHY)
3476 ocp_data |= LINK_ON_WAKE_EN;
3477 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3479 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3480 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
3481 if (wolopts & WAKE_UCAST)
3483 if (wolopts & WAKE_BCAST)
3485 if (wolopts & WAKE_MCAST)
3487 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3489 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3491 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3492 ocp_data &= ~MAGIC_EN;
3493 if (wolopts & WAKE_MAGIC)
3494 ocp_data |= MAGIC_EN;
3495 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3497 if (wolopts & WAKE_ANY)
3498 device_set_wakeup_enable(&tp->udev->dev, true);
3500 device_set_wakeup_enable(&tp->udev->dev, false);
3503 static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable)
3505 u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3507 /* MAC clock speed down */
3509 ocp_data |= MAC_CLK_SPDWN_EN;
3511 ocp_data &= ~MAC_CLK_SPDWN_EN;
3513 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3516 static void r8156_mac_clk_spd(struct r8152 *tp, bool enable)
3520 /* MAC clock speed down */
3522 /* aldps_spdwn_ratio, tp10_spdwn_ratio */
3523 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3526 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3527 ocp_data &= ~EEE_SPDWN_RATIO_MASK;
3528 ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */
3529 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3531 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3532 ocp_data &= ~MAC_CLK_SPDWN_EN;
3533 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3537 static void r8153_u1u2en(struct r8152 *tp, bool enable)
3542 memset(u1u2, 0xff, sizeof(u1u2));
3544 memset(u1u2, 0x00, sizeof(u1u2));
3546 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3549 static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3553 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3555 ocp_data |= LPM_U1U2_EN;
3557 ocp_data &= ~LPM_U1U2_EN;
3559 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3562 static void r8153_u2p3en(struct r8152 *tp, bool enable)
3566 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
3568 ocp_data |= U2P3_ENABLE;
3570 ocp_data &= ~U2P3_ENABLE;
3571 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3574 static void r8153b_ups_flags(struct r8152 *tp)
3578 if (tp->ups_info.green)
3579 ups_flags |= UPS_FLAGS_EN_GREEN;
3581 if (tp->ups_info.aldps)
3582 ups_flags |= UPS_FLAGS_EN_ALDPS;
3584 if (tp->ups_info.eee)
3585 ups_flags |= UPS_FLAGS_EN_EEE;
3587 if (tp->ups_info.flow_control)
3588 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3590 if (tp->ups_info.eee_ckdiv)
3591 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3593 if (tp->ups_info.eee_cmod_lv)
3594 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3596 if (tp->ups_info.r_tune)
3597 ups_flags |= UPS_FLAGS_R_TUNE;
3599 if (tp->ups_info._10m_ckdiv)
3600 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3602 if (tp->ups_info.eee_plloff_100)
3603 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3605 if (tp->ups_info.eee_plloff_giga)
3606 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3608 if (tp->ups_info._250m_ckdiv)
3609 ups_flags |= UPS_FLAGS_250M_CKDIV;
3611 if (tp->ups_info.ctap_short_off)
3612 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3614 switch (tp->ups_info.speed_duplex) {
3616 ups_flags |= ups_flags_speed(1);
3619 ups_flags |= ups_flags_speed(2);
3621 case NWAY_100M_HALF:
3622 ups_flags |= ups_flags_speed(3);
3624 case NWAY_100M_FULL:
3625 ups_flags |= ups_flags_speed(4);
3627 case NWAY_1000M_FULL:
3628 ups_flags |= ups_flags_speed(5);
3630 case FORCE_10M_HALF:
3631 ups_flags |= ups_flags_speed(6);
3633 case FORCE_10M_FULL:
3634 ups_flags |= ups_flags_speed(7);
3636 case FORCE_100M_HALF:
3637 ups_flags |= ups_flags_speed(8);
3639 case FORCE_100M_FULL:
3640 ups_flags |= ups_flags_speed(9);
3646 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3649 static void r8156_ups_flags(struct r8152 *tp)
3653 if (tp->ups_info.green)
3654 ups_flags |= UPS_FLAGS_EN_GREEN;
3656 if (tp->ups_info.aldps)
3657 ups_flags |= UPS_FLAGS_EN_ALDPS;
3659 if (tp->ups_info.eee)
3660 ups_flags |= UPS_FLAGS_EN_EEE;
3662 if (tp->ups_info.flow_control)
3663 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3665 if (tp->ups_info.eee_ckdiv)
3666 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3668 if (tp->ups_info._10m_ckdiv)
3669 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3671 if (tp->ups_info.eee_plloff_100)
3672 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3674 if (tp->ups_info.eee_plloff_giga)
3675 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3677 if (tp->ups_info._250m_ckdiv)
3678 ups_flags |= UPS_FLAGS_250M_CKDIV;
3680 switch (tp->ups_info.speed_duplex) {
3681 case FORCE_10M_HALF:
3682 ups_flags |= ups_flags_speed(0);
3684 case FORCE_10M_FULL:
3685 ups_flags |= ups_flags_speed(1);
3687 case FORCE_100M_HALF:
3688 ups_flags |= ups_flags_speed(2);
3690 case FORCE_100M_FULL:
3691 ups_flags |= ups_flags_speed(3);
3694 ups_flags |= ups_flags_speed(4);
3697 ups_flags |= ups_flags_speed(5);
3699 case NWAY_100M_HALF:
3700 ups_flags |= ups_flags_speed(6);
3702 case NWAY_100M_FULL:
3703 ups_flags |= ups_flags_speed(7);
3705 case NWAY_1000M_FULL:
3706 ups_flags |= ups_flags_speed(8);
3708 case NWAY_2500M_FULL:
3709 ups_flags |= ups_flags_speed(9);
3715 switch (tp->ups_info.lite_mode) {
3717 ups_flags |= 0 << 5;
3720 ups_flags |= 2 << 5;
3724 ups_flags |= 1 << 5;
3728 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3731 static void rtl_green_en(struct r8152 *tp, bool enable)
3735 data = sram_read(tp, SRAM_GREEN_CFG);
3737 data |= GREEN_ETH_EN;
3739 data &= ~GREEN_ETH_EN;
3740 sram_write(tp, SRAM_GREEN_CFG, data);
3742 tp->ups_info.green = enable;
3745 static void r8153b_green_en(struct r8152 *tp, bool enable)
3748 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
3749 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
3750 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
3752 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
3753 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
3754 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
3757 rtl_green_en(tp, true);
3760 static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3765 for (i = 0; i < 500; i++) {
3766 data = ocp_reg_read(tp, OCP_PHY_STATUS);
3767 data &= PHY_STAT_MASK;
3769 if (data == desired)
3771 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3772 data == PHY_STAT_EXT_INIT) {
3777 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3784 static void r8153b_ups_en(struct r8152 *tp, bool enable)
3786 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3789 r8153b_ups_flags(tp);
3791 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3792 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3794 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3795 ocp_data |= UPS_FORCE_PWR_DOWN;
3796 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3798 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3799 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3801 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3802 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3803 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3805 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3808 for (i = 0; i < 500; i++) {
3809 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3811 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3817 tp->rtl_ops.hw_phy_cfg(tp);
3819 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3820 tp->duplex, tp->advertising);
3825 static void r8153c_ups_en(struct r8152 *tp, bool enable)
3827 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3830 r8153b_ups_flags(tp);
3832 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3833 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3835 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3836 ocp_data |= UPS_FORCE_PWR_DOWN;
3837 ocp_data &= ~BIT(7);
3838 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3840 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3841 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3843 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3844 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3845 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3847 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3850 for (i = 0; i < 500; i++) {
3851 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
3853 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3859 tp->rtl_ops.hw_phy_cfg(tp);
3861 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3862 tp->duplex, tp->advertising);
3865 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3867 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3869 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3871 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3875 static void r8156_ups_en(struct r8152 *tp, bool enable)
3877 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3880 r8156_ups_flags(tp);
3882 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3883 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3885 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3886 ocp_data |= UPS_FORCE_PWR_DOWN;
3887 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3889 switch (tp->version) {
3892 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL);
3893 ocp_data &= ~OOBS_POLLING;
3894 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data);
3900 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3901 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3903 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3904 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3905 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3907 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3908 tp->rtl_ops.hw_phy_cfg(tp);
3910 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3911 tp->duplex, tp->advertising);
3916 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3920 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3922 ocp_data |= PWR_EN | PHASE2_EN;
3924 ocp_data &= ~(PWR_EN | PHASE2_EN);
3925 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3927 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3928 ocp_data &= ~PCUT_STATUS;
3929 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3932 static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3936 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3938 ocp_data |= PWR_EN | PHASE2_EN;
3940 ocp_data &= ~PWR_EN;
3941 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3943 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3944 ocp_data &= ~PCUT_STATUS;
3945 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3948 static void r8153_queue_wake(struct r8152 *tp, bool enable)
3952 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
3954 ocp_data |= UPCOMING_RUNTIME_D3;
3956 ocp_data &= ~UPCOMING_RUNTIME_D3;
3957 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
3959 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3960 ocp_data &= ~LINK_CHG_EVENT;
3961 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3963 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3964 ocp_data &= ~LINK_CHANGE_FLAG;
3965 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
3968 static bool rtl_can_wakeup(struct r8152 *tp)
3970 struct usb_device *udev = tp->udev;
3972 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3975 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3980 __rtl_set_wol(tp, WAKE_ANY);
3982 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3984 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3985 ocp_data |= LINK_OFF_WAKE_EN;
3986 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3988 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3992 __rtl_set_wol(tp, tp->saved_wolopts);
3994 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3996 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3997 ocp_data &= ~LINK_OFF_WAKE_EN;
3998 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
4000 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
4004 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
4007 r8153_u1u2en(tp, false);
4008 r8153_u2p3en(tp, false);
4009 rtl_runtime_suspend_enable(tp, true);
4011 rtl_runtime_suspend_enable(tp, false);
4013 switch (tp->version) {
4020 r8153_u2p3en(tp, true);
4024 r8153_u1u2en(tp, true);
4028 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
4031 r8153_queue_wake(tp, true);
4032 r8153b_u1u2en(tp, false);
4033 r8153_u2p3en(tp, false);
4034 rtl_runtime_suspend_enable(tp, true);
4035 r8153b_ups_en(tp, true);
4037 r8153b_ups_en(tp, false);
4038 r8153_queue_wake(tp, false);
4039 rtl_runtime_suspend_enable(tp, false);
4040 if (tp->udev->speed >= USB_SPEED_SUPER)
4041 r8153b_u1u2en(tp, true);
4045 static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable)
4048 r8153_queue_wake(tp, true);
4049 r8153b_u1u2en(tp, false);
4050 r8153_u2p3en(tp, false);
4051 rtl_runtime_suspend_enable(tp, true);
4052 r8153c_ups_en(tp, true);
4054 r8153c_ups_en(tp, false);
4055 r8153_queue_wake(tp, false);
4056 rtl_runtime_suspend_enable(tp, false);
4057 r8153b_u1u2en(tp, true);
4061 static void rtl8156_runtime_enable(struct r8152 *tp, bool enable)
4064 r8153_queue_wake(tp, true);
4065 r8153b_u1u2en(tp, false);
4066 r8153_u2p3en(tp, false);
4067 rtl_runtime_suspend_enable(tp, true);
4069 r8153_queue_wake(tp, false);
4070 rtl_runtime_suspend_enable(tp, false);
4071 r8153_u2p3en(tp, true);
4072 if (tp->udev->speed >= USB_SPEED_SUPER)
4073 r8153b_u1u2en(tp, true);
4077 static void r8153_teredo_off(struct r8152 *tp)
4081 switch (tp->version) {
4089 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
4090 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
4092 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
4105 /* The bit 0 ~ 7 are relative with teredo settings. They are
4106 * W1C (write 1 to clear), so set all 1 to disable it.
4108 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
4112 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
4113 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
4114 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
4117 static void rtl_reset_bmu(struct r8152 *tp)
4121 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
4122 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
4123 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
4124 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
4125 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
4128 /* Clear the bp to stop the firmware before loading a new one */
4129 static void rtl_clear_bp(struct r8152 *tp, u16 type)
4134 switch (tp->version) {
4142 if (type == MCU_TYPE_USB) {
4143 ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
4152 ocp_write_byte(tp, type, PLA_BP_EN, 0);
4161 ocp_write_word(tp, type, USB_BP2_EN, 0);
4166 generic_ocp_write(tp, PLA_BP_0, BYTE_EN_DWORD, bp_num << 1, bp, type);
4168 /* wait 3 ms to make sure the firmware is stopped */
4169 usleep_range(3000, 6000);
4170 ocp_write_word(tp, type, PLA_BP_BA, 0);
4173 static inline void rtl_reset_ocp_base(struct r8152 *tp)
4178 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
4183 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
4185 data |= PATCH_REQUEST;
4188 data &= ~PATCH_REQUEST;
4189 check = PATCH_READY;
4191 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
4193 for (i = 0; wait && i < 5000; i++) {
4196 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
4199 usleep_range(1000, 2000);
4200 ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
4201 if ((ocp_data & PATCH_READY) ^ check)
4205 if (request && wait &&
4206 !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
4207 dev_err(&tp->intf->dev, "PHY patch request fail\n");
4208 rtl_phy_patch_request(tp, false, false);
4215 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
4217 if (patch_key && key_addr) {
4218 sram_write(tp, key_addr, patch_key);
4219 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
4220 } else if (key_addr) {
4223 sram_write(tp, 0x0000, 0x0000);
4225 data = ocp_reg_read(tp, OCP_PHY_LOCK);
4226 data &= ~PATCH_LOCK;
4227 ocp_reg_write(tp, OCP_PHY_LOCK, data);
4229 sram_write(tp, key_addr, 0x0000);
4236 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
4238 if (rtl_phy_patch_request(tp, true, wait))
4241 rtl_patch_key_set(tp, key_addr, patch_key);
4246 static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
4248 rtl_patch_key_set(tp, key_addr, 0);
4250 rtl_phy_patch_request(tp, false, wait);
4255 static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy)
4261 switch (tp->version) {
4282 fw_offset = __le16_to_cpu(phy->fw_offset);
4283 length = __le32_to_cpu(phy->blk_hdr.length);
4284 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4285 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4289 length -= fw_offset;
4291 dev_err(&tp->intf->dev, "invalid block length\n");
4295 if (__le16_to_cpu(phy->fw_reg) != 0x9A00) {
4296 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4305 static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver)
4309 switch (tp->version) {
4320 if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) {
4321 dev_err(&tp->intf->dev, "invalid block length\n");
4325 if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) {
4326 dev_err(&tp->intf->dev, "invalid phy ver addr\n");
4335 static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix)
4339 switch (tp->version) {
4350 if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) {
4351 dev_err(&tp->intf->dev, "invalid block length\n");
4355 if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD ||
4356 __le16_to_cpu(fix->setting.data) != BIT(7)) {
4357 dev_err(&tp->intf->dev, "invalid phy fixup\n");
4366 static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy)
4372 switch (tp->version) {
4383 fw_offset = __le16_to_cpu(phy->fw_offset);
4384 length = __le32_to_cpu(phy->blk_hdr.length);
4385 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4386 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4390 length -= fw_offset;
4392 dev_err(&tp->intf->dev, "invalid block length\n");
4396 if (phy->pre_num > 2) {
4397 dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num);
4401 if (phy->bp_num > 8) {
4402 dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num);
4411 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
4414 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
4417 switch (tp->version) {
4423 patch_en_addr = 0xa01a;
4431 fw_offset = __le16_to_cpu(phy->fw_offset);
4432 if (fw_offset < sizeof(*phy)) {
4433 dev_err(&tp->intf->dev, "fw_offset too small\n");
4437 length = __le32_to_cpu(phy->blk_hdr.length);
4438 if (length < fw_offset) {
4439 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4443 length -= __le16_to_cpu(phy->fw_offset);
4444 if (!length || (length & 1)) {
4445 dev_err(&tp->intf->dev, "invalid block length\n");
4449 if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
4450 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4454 if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
4455 dev_err(&tp->intf->dev, "invalid base address register\n");
4459 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
4460 dev_err(&tp->intf->dev,
4461 "invalid patch mode enabled register\n");
4465 if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
4466 dev_err(&tp->intf->dev,
4467 "invalid register to switch the mode\n");
4471 if (__le16_to_cpu(phy->bp_start) != bp_start) {
4472 dev_err(&tp->intf->dev,
4473 "invalid start register of break point\n");
4477 if (__le16_to_cpu(phy->bp_num) > 4) {
4478 dev_err(&tp->intf->dev, "invalid break point number\n");
4487 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
4489 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
4494 type = __le32_to_cpu(mac->blk_hdr.type);
4495 if (type == RTL_FW_PLA) {
4496 switch (tp->version) {
4501 bp_ba_addr = PLA_BP_BA;
4503 bp_start = PLA_BP_0;
4517 bp_ba_addr = PLA_BP_BA;
4518 bp_en_addr = PLA_BP_EN;
4519 bp_start = PLA_BP_0;
4524 bp_ba_addr = PLA_BP_BA;
4525 bp_en_addr = USB_BP2_EN;
4526 bp_start = PLA_BP_0;
4532 } else if (type == RTL_FW_USB) {
4533 switch (tp->version) {
4539 bp_ba_addr = USB_BP_BA;
4540 bp_en_addr = USB_BP_EN;
4541 bp_start = USB_BP_0;
4552 bp_ba_addr = USB_BP_BA;
4553 bp_en_addr = USB_BP2_EN;
4554 bp_start = USB_BP_0;
4567 fw_offset = __le16_to_cpu(mac->fw_offset);
4568 if (fw_offset < sizeof(*mac)) {
4569 dev_err(&tp->intf->dev, "fw_offset too small\n");
4573 length = __le32_to_cpu(mac->blk_hdr.length);
4574 if (length < fw_offset) {
4575 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4579 length -= fw_offset;
4580 if (length < 4 || (length & 3)) {
4581 dev_err(&tp->intf->dev, "invalid block length\n");
4585 if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
4586 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4590 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
4591 dev_err(&tp->intf->dev, "invalid base address register\n");
4595 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
4596 dev_err(&tp->intf->dev, "invalid enabled mask register\n");
4600 if (__le16_to_cpu(mac->bp_start) != bp_start) {
4601 dev_err(&tp->intf->dev,
4602 "invalid start register of break point\n");
4606 if (__le16_to_cpu(mac->bp_num) > max_bp) {
4607 dev_err(&tp->intf->dev, "invalid break point number\n");
4611 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
4613 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
4623 /* Verify the checksum for the firmware file. It is calculated from the version
4624 * field to the end of the file. Compare the result with the checksum field to
4625 * make sure the file is correct.
4627 static long rtl8152_fw_verify_checksum(struct r8152 *tp,
4628 struct fw_header *fw_hdr, size_t size)
4630 unsigned char checksum[sizeof(fw_hdr->checksum)];
4631 struct crypto_shash *alg;
4632 struct shash_desc *sdesc;
4636 alg = crypto_alloc_shash("sha256", 0, 0);
4642 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
4644 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
4645 crypto_shash_digestsize(alg));
4649 len = sizeof(*sdesc) + crypto_shash_descsize(alg);
4650 sdesc = kmalloc(len, GFP_KERNEL);
4657 len = size - sizeof(fw_hdr->checksum);
4658 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
4663 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
4664 dev_err(&tp->intf->dev, "checksum fail\n");
4669 crypto_free_shash(alg);
4674 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
4676 const struct firmware *fw = rtl_fw->fw;
4677 struct fw_header *fw_hdr = (struct fw_header *)fw->data;
4678 unsigned long fw_flags = 0;
4682 if (fw->size < sizeof(*fw_hdr)) {
4683 dev_err(&tp->intf->dev, "file too small\n");
4687 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
4693 for (i = sizeof(*fw_hdr); i < fw->size;) {
4694 struct fw_block *block = (struct fw_block *)&fw->data[i];
4697 if ((i + sizeof(*block)) > fw->size)
4700 type = __le32_to_cpu(block->type);
4703 if (__le32_to_cpu(block->length) != sizeof(*block))
4707 if (test_bit(FW_FLAGS_PLA, &fw_flags)) {
4708 dev_err(&tp->intf->dev,
4709 "multiple PLA firmware encountered");
4713 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4714 dev_err(&tp->intf->dev,
4715 "check PLA firmware failed\n");
4718 __set_bit(FW_FLAGS_PLA, &fw_flags);
4721 if (test_bit(FW_FLAGS_USB, &fw_flags)) {
4722 dev_err(&tp->intf->dev,
4723 "multiple USB firmware encountered");
4727 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4728 dev_err(&tp->intf->dev,
4729 "check USB firmware failed\n");
4732 __set_bit(FW_FLAGS_USB, &fw_flags);
4734 case RTL_FW_PHY_START:
4735 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4736 test_bit(FW_FLAGS_NC, &fw_flags) ||
4737 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4738 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4739 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4740 test_bit(FW_FLAGS_UC, &fw_flags) ||
4741 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4742 dev_err(&tp->intf->dev,
4743 "check PHY_START fail\n");
4747 if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) {
4748 dev_err(&tp->intf->dev,
4749 "Invalid length for PHY_START\n");
4752 __set_bit(FW_FLAGS_START, &fw_flags);
4754 case RTL_FW_PHY_STOP:
4755 if (test_bit(FW_FLAGS_STOP, &fw_flags) ||
4756 !test_bit(FW_FLAGS_START, &fw_flags)) {
4757 dev_err(&tp->intf->dev,
4758 "Check PHY_STOP fail\n");
4762 if (__le32_to_cpu(block->length) != sizeof(*block)) {
4763 dev_err(&tp->intf->dev,
4764 "Invalid length for PHY_STOP\n");
4767 __set_bit(FW_FLAGS_STOP, &fw_flags);
4770 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4771 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4772 dev_err(&tp->intf->dev,
4773 "check PHY_NC fail\n");
4777 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4778 dev_err(&tp->intf->dev,
4779 "multiple PHY NC encountered\n");
4783 if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) {
4784 dev_err(&tp->intf->dev,
4785 "check PHY NC firmware failed\n");
4788 __set_bit(FW_FLAGS_NC, &fw_flags);
4790 case RTL_FW_PHY_UNION_NC:
4791 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4792 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4793 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4794 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4795 test_bit(FW_FLAGS_UC, &fw_flags) ||
4796 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4797 dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n");
4801 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4802 dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n");
4806 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4807 dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n");
4810 __set_bit(FW_FLAGS_NC, &fw_flags);
4812 case RTL_FW_PHY_UNION_NC1:
4813 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4814 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4815 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4816 test_bit(FW_FLAGS_UC, &fw_flags) ||
4817 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4818 dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n");
4822 if (test_bit(FW_FLAGS_NC1, &fw_flags)) {
4823 dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n");
4827 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4828 dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n");
4831 __set_bit(FW_FLAGS_NC1, &fw_flags);
4833 case RTL_FW_PHY_UNION_NC2:
4834 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4835 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4836 test_bit(FW_FLAGS_UC, &fw_flags) ||
4837 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4838 dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n");
4842 if (test_bit(FW_FLAGS_NC2, &fw_flags)) {
4843 dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n");
4847 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4848 dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n");
4851 __set_bit(FW_FLAGS_NC2, &fw_flags);
4853 case RTL_FW_PHY_UNION_UC2:
4854 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4855 test_bit(FW_FLAGS_UC, &fw_flags) ||
4856 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4857 dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n");
4861 if (test_bit(FW_FLAGS_UC2, &fw_flags)) {
4862 dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n");
4866 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4867 dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n");
4870 __set_bit(FW_FLAGS_UC2, &fw_flags);
4872 case RTL_FW_PHY_UNION_UC:
4873 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4874 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4875 dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n");
4879 if (test_bit(FW_FLAGS_UC, &fw_flags)) {
4880 dev_err(&tp->intf->dev, "multiple PHY UC encountered\n");
4884 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4885 dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n");
4888 __set_bit(FW_FLAGS_UC, &fw_flags);
4890 case RTL_FW_PHY_UNION_MISC:
4891 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4892 dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n");
4896 case RTL_FW_PHY_FIXUP:
4897 if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) {
4898 dev_err(&tp->intf->dev, "check PHY fixup failed\n");
4902 case RTL_FW_PHY_SPEED_UP:
4903 if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) {
4904 dev_err(&tp->intf->dev, "multiple PHY firmware encountered");
4908 if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) {
4909 dev_err(&tp->intf->dev, "check PHY speed up failed\n");
4912 __set_bit(FW_FLAGS_SPEED_UP, &fw_flags);
4914 case RTL_FW_PHY_VER:
4915 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4916 test_bit(FW_FLAGS_NC, &fw_flags) ||
4917 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4918 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4919 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4920 test_bit(FW_FLAGS_UC, &fw_flags) ||
4921 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4922 dev_err(&tp->intf->dev, "Invalid order to set PHY version\n");
4926 if (test_bit(FW_FLAGS_VER, &fw_flags)) {
4927 dev_err(&tp->intf->dev, "multiple PHY version encountered");
4931 if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) {
4932 dev_err(&tp->intf->dev, "check PHY version failed\n");
4935 __set_bit(FW_FLAGS_VER, &fw_flags);
4938 dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
4944 i += ALIGN(__le32_to_cpu(block->length), 8);
4948 if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) {
4949 dev_err(&tp->intf->dev, "without PHY_STOP\n");
4958 static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait)
4963 rtl_reset_ocp_base(tp);
4965 if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) {
4966 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4970 len = __le32_to_cpu(phy->blk_hdr.length);
4971 len -= __le16_to_cpu(phy->fw_offset);
4972 data = (u8 *)phy + __le16_to_cpu(phy->fw_offset);
4974 if (rtl_phy_patch_request(tp, true, wait))
4986 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL);
4987 ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE;
4988 ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data);
4990 generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB);
4995 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL);
4996 ocp_data |= POL_GPHY_PATCH;
4997 ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data);
4999 for (i = 0; i < 1000; i++) {
5000 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH))
5005 dev_err(&tp->intf->dev, "ram code speedup mode timeout\n");
5010 rtl_reset_ocp_base(tp);
5012 rtl_phy_patch_request(tp, false, wait);
5014 if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version))
5015 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
5017 dev_err(&tp->intf->dev, "ram code speedup mode fail\n");
5020 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver)
5024 ver_addr = __le16_to_cpu(phy_ver->ver.addr);
5025 ver = __le16_to_cpu(phy_ver->ver.data);
5027 rtl_reset_ocp_base(tp);
5029 if (sram_read(tp, ver_addr) >= ver) {
5030 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
5034 sram_write(tp, ver_addr, ver);
5036 dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver);
5041 static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix)
5045 rtl_reset_ocp_base(tp);
5047 addr = __le16_to_cpu(fix->setting.addr);
5048 data = ocp_reg_read(tp, addr);
5050 switch (__le16_to_cpu(fix->bit_cmd)) {
5052 data &= __le16_to_cpu(fix->setting.data);
5055 data |= __le16_to_cpu(fix->setting.data);
5058 data &= ~__le16_to_cpu(fix->setting.data);
5061 data ^= __le16_to_cpu(fix->setting.data);
5067 ocp_reg_write(tp, addr, data);
5069 dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data);
5072 static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy)
5078 rtl_reset_ocp_base(tp);
5081 for (i = 0; i < num; i++)
5082 sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr),
5083 __le16_to_cpu(phy->pre_set[i].data));
5085 length = __le32_to_cpu(phy->blk_hdr.length);
5086 length -= __le16_to_cpu(phy->fw_offset);
5088 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
5090 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
5091 for (i = 0; i < num; i++)
5092 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
5095 for (i = 0; i < num; i++)
5096 sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data));
5098 if (phy->bp_num && phy->bp_en.addr)
5099 sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data));
5101 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
5104 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
5106 u16 mode_reg, bp_index;
5110 rtl_reset_ocp_base(tp);
5112 mode_reg = __le16_to_cpu(phy->mode_reg);
5113 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
5114 sram_write(tp, __le16_to_cpu(phy->ba_reg),
5115 __le16_to_cpu(phy->ba_data));
5117 length = __le32_to_cpu(phy->blk_hdr.length);
5118 length -= __le16_to_cpu(phy->fw_offset);
5120 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
5122 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
5123 for (i = 0; i < num; i++)
5124 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
5126 sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
5127 __le16_to_cpu(phy->patch_en_value));
5129 bp_index = __le16_to_cpu(phy->bp_start);
5130 num = __le16_to_cpu(phy->bp_num);
5131 for (i = 0; i < num; i++) {
5132 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
5136 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
5138 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
5141 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
5143 u16 bp_en_addr, type, fw_ver_reg;
5147 switch (__le32_to_cpu(mac->blk_hdr.type)) {
5149 type = MCU_TYPE_PLA;
5152 type = MCU_TYPE_USB;
5158 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
5159 if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) {
5160 dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB");
5164 rtl_clear_bp(tp, type);
5166 /* Enable backup/restore of MACDBG. This is required after clearing PLA
5167 * break points and before applying the PLA firmware.
5169 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
5170 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
5171 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
5172 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
5175 length = __le32_to_cpu(mac->blk_hdr.length);
5176 length -= __le16_to_cpu(mac->fw_offset);
5179 data += __le16_to_cpu(mac->fw_offset);
5181 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
5184 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
5185 __le16_to_cpu(mac->bp_ba_value));
5187 generic_ocp_write(tp, __le16_to_cpu(mac->bp_start), BYTE_EN_DWORD,
5188 __le16_to_cpu(mac->bp_num) << 1, mac->bp, type);
5190 bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
5192 ocp_write_word(tp, type, bp_en_addr,
5193 __le16_to_cpu(mac->bp_en_value));
5196 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
5199 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
5202 static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
5204 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5205 const struct firmware *fw;
5206 struct fw_header *fw_hdr;
5207 struct fw_phy_patch_key *key;
5209 int i, patch_phy = 1;
5211 if (IS_ERR_OR_NULL(rtl_fw->fw))
5215 fw_hdr = (struct fw_header *)fw->data;
5220 for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
5221 struct fw_block *block = (struct fw_block *)&fw->data[i];
5223 switch (__le32_to_cpu(block->type)) {
5228 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
5230 case RTL_FW_PHY_START:
5233 key = (struct fw_phy_patch_key *)block;
5234 key_addr = __le16_to_cpu(key->key_reg);
5235 rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut);
5237 case RTL_FW_PHY_STOP:
5241 rtl_post_ram_code(tp, key_addr, !power_cut);
5244 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
5246 case RTL_FW_PHY_VER:
5247 patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block);
5249 case RTL_FW_PHY_UNION_NC:
5250 case RTL_FW_PHY_UNION_NC1:
5251 case RTL_FW_PHY_UNION_NC2:
5252 case RTL_FW_PHY_UNION_UC2:
5253 case RTL_FW_PHY_UNION_UC:
5254 case RTL_FW_PHY_UNION_MISC:
5256 rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block);
5258 case RTL_FW_PHY_FIXUP:
5260 rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block);
5262 case RTL_FW_PHY_SPEED_UP:
5263 rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut);
5269 i += ALIGN(__le32_to_cpu(block->length), 8);
5273 if (rtl_fw->post_fw)
5274 rtl_fw->post_fw(tp);
5276 rtl_reset_ocp_base(tp);
5277 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
5278 dev_dbg(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
5281 static void rtl8152_release_firmware(struct r8152 *tp)
5283 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5285 if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
5286 release_firmware(rtl_fw->fw);
5291 static int rtl8152_request_firmware(struct r8152 *tp)
5293 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5296 if (rtl_fw->fw || !rtl_fw->fw_name) {
5297 dev_info(&tp->intf->dev, "skip request firmware\n");
5302 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
5306 rc = rtl8152_check_firmware(tp, rtl_fw);
5308 release_firmware(rtl_fw->fw);
5312 rtl_fw->fw = ERR_PTR(rc);
5314 dev_warn(&tp->intf->dev,
5315 "unable to load firmware patch %s (%ld)\n",
5316 rtl_fw->fw_name, rc);
5322 static void r8152_aldps_en(struct r8152 *tp, bool enable)
5325 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
5326 LINKENA | DIS_SDSAVE);
5328 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
5334 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
5336 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
5337 ocp_reg_write(tp, OCP_EEE_DATA, reg);
5338 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
5341 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
5345 r8152_mmd_indirect(tp, dev, reg);
5346 data = ocp_reg_read(tp, OCP_EEE_DATA);
5347 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5352 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
5354 r8152_mmd_indirect(tp, dev, reg);
5355 ocp_reg_write(tp, OCP_EEE_DATA, data);
5356 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5359 static void r8152_eee_en(struct r8152 *tp, bool enable)
5361 u16 config1, config2, config3;
5364 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5365 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
5366 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
5367 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
5370 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5371 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
5372 config1 |= sd_rise_time(1);
5373 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
5374 config3 |= fast_snr(42);
5376 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5377 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
5379 config1 |= sd_rise_time(7);
5380 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
5381 config3 |= fast_snr(511);
5384 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5385 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
5386 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
5387 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
5390 static void r8153_eee_en(struct r8152 *tp, bool enable)
5395 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5396 config = ocp_reg_read(tp, OCP_EEE_CFG);
5399 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5402 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5403 config &= ~EEE10_EN;
5406 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5407 ocp_reg_write(tp, OCP_EEE_CFG, config);
5409 tp->ups_info.eee = enable;
5412 static void r8156_eee_en(struct r8152 *tp, bool enable)
5416 r8153_eee_en(tp, enable);
5418 config = ocp_reg_read(tp, OCP_EEE_ADV2);
5421 config |= MDIO_EEE_2_5GT;
5423 config &= ~MDIO_EEE_2_5GT;
5425 ocp_reg_write(tp, OCP_EEE_ADV2, config);
5428 static void rtl_eee_enable(struct r8152 *tp, bool enable)
5430 switch (tp->version) {
5435 r8152_eee_en(tp, true);
5436 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
5439 r8152_eee_en(tp, false);
5440 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
5451 r8153_eee_en(tp, true);
5452 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5454 r8153_eee_en(tp, false);
5455 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5464 r8156_eee_en(tp, true);
5465 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5467 r8156_eee_en(tp, false);
5468 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5476 static void r8152b_enable_fc(struct r8152 *tp)
5480 anar = r8152_mdio_read(tp, MII_ADVERTISE);
5481 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
5482 r8152_mdio_write(tp, MII_ADVERTISE, anar);
5484 tp->ups_info.flow_control = true;
5487 static void rtl8152_disable(struct r8152 *tp)
5489 r8152_aldps_en(tp, false);
5491 r8152_aldps_en(tp, true);
5494 static void r8152b_hw_phy_cfg(struct r8152 *tp)
5496 rtl8152_apply_firmware(tp, false);
5497 rtl_eee_enable(tp, tp->eee_en);
5498 r8152_aldps_en(tp, true);
5499 r8152b_enable_fc(tp);
5501 set_bit(PHY_RESET, &tp->flags);
5504 static void wait_oob_link_list_ready(struct r8152 *tp)
5509 for (i = 0; i < 1000; i++) {
5510 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
5512 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5513 if (ocp_data & LINK_LIST_READY)
5515 usleep_range(1000, 2000);
5519 static void r8156b_wait_loading_flash(struct r8152 *tp)
5521 if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) &&
5522 !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) {
5525 for (i = 0; i < 100; i++) {
5526 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
5528 if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE)
5530 usleep_range(1000, 2000);
5535 static void r8152b_exit_oob(struct r8152 *tp)
5539 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5540 ocp_data &= ~RCR_ACPT_ALL;
5541 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5543 rxdy_gated_en(tp, true);
5544 r8153_teredo_off(tp);
5545 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
5546 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
5548 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5549 ocp_data &= ~NOW_IS_OOB;
5550 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5552 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5553 ocp_data &= ~MCU_BORW_EN;
5554 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5556 wait_oob_link_list_ready(tp);
5558 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5559 ocp_data |= RE_INIT_LL;
5560 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5562 wait_oob_link_list_ready(tp);
5564 rtl8152_nic_reset(tp);
5566 /* rx share fifo credit full threshold */
5567 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5569 if (tp->udev->speed == USB_SPEED_FULL ||
5570 tp->udev->speed == USB_SPEED_LOW) {
5571 /* rx share fifo credit near full threshold */
5572 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5574 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5577 /* rx share fifo credit near full threshold */
5578 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5580 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5584 /* TX share fifo free credit full threshold */
5585 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
5587 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
5588 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
5589 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
5590 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
5592 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
5594 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5596 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5597 ocp_data |= TCR0_AUTO_FIFO;
5598 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5601 static void r8152b_enter_oob(struct r8152 *tp)
5605 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5606 ocp_data &= ~NOW_IS_OOB;
5607 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5609 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
5610 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
5611 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
5615 wait_oob_link_list_ready(tp);
5617 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5618 ocp_data |= RE_INIT_LL;
5619 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5621 wait_oob_link_list_ready(tp);
5623 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5625 rtl_rx_vlan_en(tp, true);
5627 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
5628 ocp_data |= ALDPS_PROXY_MODE;
5629 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
5631 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5632 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5633 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5635 rxdy_gated_en(tp, false);
5637 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5638 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5639 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5642 static int r8153_pre_firmware_1(struct r8152 *tp)
5646 /* Wait till the WTD timer is ready. It would take at most 104 ms. */
5647 for (i = 0; i < 104; i++) {
5648 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
5650 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
5652 if (!(ocp_data & WTD1_EN))
5654 usleep_range(1000, 2000);
5660 static int r8153_post_firmware_1(struct r8152 *tp)
5662 /* set USB_BP_4 to support USB_SPEED_SUPER only */
5663 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
5664 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
5666 /* reset UPHY timer to 36 ms */
5667 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5672 static int r8153_pre_firmware_2(struct r8152 *tp)
5676 r8153_pre_firmware_1(tp);
5678 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5679 ocp_data &= ~FW_FIX_SUSPEND;
5680 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5685 static int r8153_post_firmware_2(struct r8152 *tp)
5689 /* enable bp0 if support USB_SPEED_SUPER only */
5690 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
5691 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5693 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5696 /* reset UPHY timer to 36 ms */
5697 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5699 /* enable U3P3 check, set the counter to 4 */
5700 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
5702 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5703 ocp_data |= FW_FIX_SUSPEND;
5704 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5706 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5707 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5708 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5713 static int r8153_post_firmware_3(struct r8152 *tp)
5717 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5718 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5719 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5721 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5722 ocp_data |= FW_IP_RESET_EN;
5723 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5728 static int r8153b_pre_firmware_1(struct r8152 *tp)
5730 /* enable fc timer and set timer to 1 second. */
5731 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
5732 CTRL_TIMER_EN | (1000 / 8));
5737 static int r8153b_post_firmware_1(struct r8152 *tp)
5741 /* enable bp0 for RTL8153-BND */
5742 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
5743 if (ocp_data & BND_MASK) {
5744 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5746 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5749 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5750 ocp_data |= FLOW_CTRL_PATCH_OPT;
5751 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5753 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5754 ocp_data |= FC_PATCH_TASK;
5755 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5757 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5758 ocp_data |= FW_IP_RESET_EN;
5759 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5764 static int r8153c_post_firmware_1(struct r8152 *tp)
5768 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5769 ocp_data |= FLOW_CTRL_PATCH_2;
5770 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5772 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5773 ocp_data |= FC_PATCH_TASK;
5774 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5779 static int r8156a_post_firmware_1(struct r8152 *tp)
5783 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5784 ocp_data |= FW_IP_RESET_EN;
5785 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5787 /* Modify U3PHY parameter for compatibility issue */
5788 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e);
5789 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9);
5794 static void r8153_aldps_en(struct r8152 *tp, bool enable)
5798 data = ocp_reg_read(tp, OCP_POWER_CFG);
5801 ocp_reg_write(tp, OCP_POWER_CFG, data);
5806 ocp_reg_write(tp, OCP_POWER_CFG, data);
5807 for (i = 0; i < 20; i++) {
5808 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
5810 usleep_range(1000, 2000);
5811 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
5816 tp->ups_info.aldps = enable;
5819 static void r8153_hw_phy_cfg(struct r8152 *tp)
5824 /* disable ALDPS before updating the PHY parameters */
5825 r8153_aldps_en(tp, false);
5827 /* disable EEE before updating the PHY parameters */
5828 rtl_eee_enable(tp, false);
5830 rtl8152_apply_firmware(tp, false);
5832 if (tp->version == RTL_VER_03) {
5833 data = ocp_reg_read(tp, OCP_EEE_CFG);
5834 data &= ~CTAP_SHORT_EN;
5835 ocp_reg_write(tp, OCP_EEE_CFG, data);
5838 data = ocp_reg_read(tp, OCP_POWER_CFG);
5839 data |= EEE_CLKDIV_EN;
5840 ocp_reg_write(tp, OCP_POWER_CFG, data);
5842 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5843 data |= EN_10M_BGOFF;
5844 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5845 data = ocp_reg_read(tp, OCP_POWER_CFG);
5846 data |= EN_10M_PLLOFF;
5847 ocp_reg_write(tp, OCP_POWER_CFG, data);
5848 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
5850 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5851 ocp_data |= PFM_PWM_SWITCH;
5852 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5854 /* Enable LPF corner auto tune */
5855 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
5857 /* Adjust 10M Amplitude */
5858 sram_write(tp, SRAM_10M_AMP1, 0x00af);
5859 sram_write(tp, SRAM_10M_AMP2, 0x0208);
5862 rtl_eee_enable(tp, true);
5864 r8153_aldps_en(tp, true);
5865 r8152b_enable_fc(tp);
5867 switch (tp->version) {
5874 r8153_u2p3en(tp, true);
5878 set_bit(PHY_RESET, &tp->flags);
5881 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
5885 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
5886 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
5887 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
5888 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
5893 static void r8153b_hw_phy_cfg(struct r8152 *tp)
5898 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
5899 if (ocp_data & PCUT_STATUS) {
5900 ocp_data &= ~PCUT_STATUS;
5901 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
5904 /* disable ALDPS before updating the PHY parameters */
5905 r8153_aldps_en(tp, false);
5907 /* disable EEE before updating the PHY parameters */
5908 rtl_eee_enable(tp, false);
5910 /* U1/U2/L1 idle timer. 500 us */
5911 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5913 data = r8153_phy_status(tp, 0);
5916 case PHY_STAT_PWRDN:
5917 case PHY_STAT_EXT_INIT:
5918 rtl8152_apply_firmware(tp, true);
5920 data = r8152_mdio_read(tp, MII_BMCR);
5921 data &= ~BMCR_PDOWN;
5922 r8152_mdio_write(tp, MII_BMCR, data);
5924 case PHY_STAT_LAN_ON:
5926 rtl8152_apply_firmware(tp, false);
5930 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
5932 data = sram_read(tp, SRAM_GREEN_CFG);
5934 sram_write(tp, SRAM_GREEN_CFG, data);
5935 data = ocp_reg_read(tp, OCP_NCTL_CFG);
5936 data |= PGA_RETURN_EN;
5937 ocp_reg_write(tp, OCP_NCTL_CFG, data);
5939 /* ADC Bias Calibration:
5940 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
5941 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
5944 ocp_data = r8152_efuse_read(tp, 0x7d);
5945 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
5947 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
5949 /* ups mode tx-link-pulse timing adjustment:
5950 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
5951 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
5953 ocp_data = ocp_reg_read(tp, 0xc426);
5956 u32 swr_cnt_1ms_ini;
5958 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
5959 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
5960 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
5961 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
5964 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5965 ocp_data |= PFM_PWM_SWITCH;
5966 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5969 if (!rtl_phy_patch_request(tp, true, true)) {
5970 data = ocp_reg_read(tp, OCP_POWER_CFG);
5971 data |= EEE_CLKDIV_EN;
5972 ocp_reg_write(tp, OCP_POWER_CFG, data);
5973 tp->ups_info.eee_ckdiv = true;
5975 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5976 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
5977 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5978 tp->ups_info.eee_cmod_lv = true;
5979 tp->ups_info._10m_ckdiv = true;
5980 tp->ups_info.eee_plloff_giga = true;
5982 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
5983 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
5984 tp->ups_info._250m_ckdiv = true;
5986 rtl_phy_patch_request(tp, false, true);
5990 rtl_eee_enable(tp, true);
5992 r8153_aldps_en(tp, true);
5993 r8152b_enable_fc(tp);
5995 set_bit(PHY_RESET, &tp->flags);
5998 static void r8153c_hw_phy_cfg(struct r8152 *tp)
6000 r8153b_hw_phy_cfg(tp);
6002 tp->ups_info.r_tune = true;
6005 static void rtl8153_change_mtu(struct r8152 *tp)
6007 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
6008 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
6011 static void r8153_first_init(struct r8152 *tp)
6015 rxdy_gated_en(tp, true);
6016 r8153_teredo_off(tp);
6018 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6019 ocp_data &= ~RCR_ACPT_ALL;
6020 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6022 rtl8152_nic_reset(tp);
6025 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6026 ocp_data &= ~NOW_IS_OOB;
6027 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6029 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6030 ocp_data &= ~MCU_BORW_EN;
6031 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6033 wait_oob_link_list_ready(tp);
6035 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6036 ocp_data |= RE_INIT_LL;
6037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6039 wait_oob_link_list_ready(tp);
6041 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6043 rtl8153_change_mtu(tp);
6045 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
6046 ocp_data |= TCR0_AUTO_FIFO;
6047 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
6049 rtl8152_nic_reset(tp);
6051 /* rx share fifo credit full threshold */
6052 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
6053 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
6054 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
6055 /* TX share fifo free credit full threshold */
6056 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
6059 static void r8153_enter_oob(struct r8152 *tp)
6063 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6064 ocp_data &= ~NOW_IS_OOB;
6065 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6067 /* RX FIFO settings for OOB */
6068 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
6069 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
6070 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
6075 wait_oob_link_list_ready(tp);
6077 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6078 ocp_data |= RE_INIT_LL;
6079 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6081 wait_oob_link_list_ready(tp);
6083 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
6084 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
6086 switch (tp->version) {
6091 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
6092 ocp_data &= ~TEREDO_WAKE_MASK;
6093 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
6099 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
6100 * type. Set it to zero. bits[7:0] are the W1C bits about
6101 * the events. Set them to all 1 to clear them.
6103 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
6110 rtl_rx_vlan_en(tp, true);
6112 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
6113 ocp_data |= ALDPS_PROXY_MODE;
6114 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
6116 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6117 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
6118 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6120 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6121 ocp_data |= MCU_BORW_EN;
6122 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6124 rxdy_gated_en(tp, false);
6126 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6127 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
6128 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6131 static void rtl8153_disable(struct r8152 *tp)
6133 r8153_aldps_en(tp, false);
6136 r8153_aldps_en(tp, true);
6139 static u32 fc_pause_on_auto(struct r8152 *tp)
6141 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024);
6144 static u32 fc_pause_off_auto(struct r8152 *tp)
6146 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024);
6149 static void r8156_fc_parameter(struct r8152 *tp)
6151 u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp);
6152 u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp);
6154 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16);
6155 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16);
6158 static int rtl8156_enable(struct r8152 *tp)
6163 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6166 r8156_fc_parameter(tp);
6168 rtl_set_eee_plus(tp);
6169 r8153_set_rx_early_timeout(tp);
6170 r8153_set_rx_early_size(tp);
6172 speed = rtl8152_get_speed(tp);
6173 rtl_set_ifg(tp, speed);
6175 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
6176 if (speed & _2500bps)
6177 ocp_data &= ~IDLE_SPDWN_EN;
6179 ocp_data |= IDLE_SPDWN_EN;
6180 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
6182 if (speed & _1000bps)
6183 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
6184 else if (speed & _500bps)
6185 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
6187 if (tp->udev->speed == USB_SPEED_HIGH) {
6188 /* USB 0xb45e[3:0] l1_nyet_hird */
6189 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6191 if (is_flow_control(speed))
6195 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6198 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
6199 ocp_data &= ~FC_PATCH_TASK;
6200 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6201 usleep_range(1000, 2000);
6202 ocp_data |= FC_PATCH_TASK;
6203 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6205 return rtl_enable(tp);
6208 static void rtl8156_disable(struct r8152 *tp)
6210 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 0);
6211 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 0);
6213 rtl8153_disable(tp);
6216 static int rtl8156b_enable(struct r8152 *tp)
6221 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6225 rtl_set_eee_plus(tp);
6227 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM);
6228 ocp_data &= ~RX_AGGR_NUM_MASK;
6229 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data);
6231 r8153_set_rx_early_timeout(tp);
6232 r8153_set_rx_early_size(tp);
6234 speed = rtl8152_get_speed(tp);
6235 rtl_set_ifg(tp, speed);
6237 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
6238 if (speed & _2500bps)
6239 ocp_data &= ~IDLE_SPDWN_EN;
6241 ocp_data |= IDLE_SPDWN_EN;
6242 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
6244 if (tp->udev->speed == USB_SPEED_HIGH) {
6245 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6247 if (is_flow_control(speed))
6251 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6254 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
6255 ocp_data &= ~FC_PATCH_TASK;
6256 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6257 usleep_range(1000, 2000);
6258 ocp_data |= FC_PATCH_TASK;
6259 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6261 return rtl_enable(tp);
6264 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
6270 if (autoneg == AUTONEG_DISABLE) {
6271 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
6276 bmcr = BMCR_SPEED10;
6277 if (duplex == DUPLEX_FULL) {
6278 bmcr |= BMCR_FULLDPLX;
6279 tp->ups_info.speed_duplex = FORCE_10M_FULL;
6281 tp->ups_info.speed_duplex = FORCE_10M_HALF;
6285 bmcr = BMCR_SPEED100;
6286 if (duplex == DUPLEX_FULL) {
6287 bmcr |= BMCR_FULLDPLX;
6288 tp->ups_info.speed_duplex = FORCE_100M_FULL;
6290 tp->ups_info.speed_duplex = FORCE_100M_HALF;
6294 if (tp->mii.supports_gmii) {
6295 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
6296 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6305 if (duplex == DUPLEX_FULL)
6306 tp->mii.full_duplex = 1;
6308 tp->mii.full_duplex = 0;
6310 tp->mii.force_media = 1;
6315 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6316 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6318 if (tp->mii.supports_gmii) {
6319 support |= RTL_ADVERTISED_1000_FULL;
6321 if (tp->support_2500full)
6322 support |= RTL_ADVERTISED_2500_FULL;
6325 if (!(advertising & support))
6328 orig = r8152_mdio_read(tp, MII_ADVERTISE);
6329 new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
6330 ADVERTISE_100HALF | ADVERTISE_100FULL);
6331 if (advertising & RTL_ADVERTISED_10_HALF) {
6332 new1 |= ADVERTISE_10HALF;
6333 tp->ups_info.speed_duplex = NWAY_10M_HALF;
6335 if (advertising & RTL_ADVERTISED_10_FULL) {
6336 new1 |= ADVERTISE_10FULL;
6337 tp->ups_info.speed_duplex = NWAY_10M_FULL;
6340 if (advertising & RTL_ADVERTISED_100_HALF) {
6341 new1 |= ADVERTISE_100HALF;
6342 tp->ups_info.speed_duplex = NWAY_100M_HALF;
6344 if (advertising & RTL_ADVERTISED_100_FULL) {
6345 new1 |= ADVERTISE_100FULL;
6346 tp->ups_info.speed_duplex = NWAY_100M_FULL;
6350 r8152_mdio_write(tp, MII_ADVERTISE, new1);
6351 tp->mii.advertising = new1;
6354 if (tp->mii.supports_gmii) {
6355 orig = r8152_mdio_read(tp, MII_CTRL1000);
6356 new1 = orig & ~(ADVERTISE_1000FULL |
6357 ADVERTISE_1000HALF);
6359 if (advertising & RTL_ADVERTISED_1000_FULL) {
6360 new1 |= ADVERTISE_1000FULL;
6361 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6365 r8152_mdio_write(tp, MII_CTRL1000, new1);
6368 if (tp->support_2500full) {
6369 orig = ocp_reg_read(tp, OCP_10GBT_CTRL);
6370 new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G;
6372 if (advertising & RTL_ADVERTISED_2500_FULL) {
6373 new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G;
6374 tp->ups_info.speed_duplex = NWAY_2500M_FULL;
6378 ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
6381 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
6383 tp->mii.force_media = 0;
6386 if (test_and_clear_bit(PHY_RESET, &tp->flags))
6389 r8152_mdio_write(tp, MII_BMCR, bmcr);
6391 if (bmcr & BMCR_RESET) {
6394 for (i = 0; i < 50; i++) {
6396 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
6405 static void rtl8152_up(struct r8152 *tp)
6407 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6410 r8152_aldps_en(tp, false);
6411 r8152b_exit_oob(tp);
6412 r8152_aldps_en(tp, true);
6415 static void rtl8152_down(struct r8152 *tp)
6417 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
6418 rtl_drop_queued_tx(tp);
6422 r8152_power_cut_en(tp, false);
6423 r8152_aldps_en(tp, false);
6424 r8152b_enter_oob(tp);
6425 r8152_aldps_en(tp, true);
6428 static void rtl8153_up(struct r8152 *tp)
6432 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6435 r8153_u1u2en(tp, false);
6436 r8153_u2p3en(tp, false);
6437 r8153_aldps_en(tp, false);
6438 r8153_first_init(tp);
6440 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6441 ocp_data |= LANWAKE_CLR_EN;
6442 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6444 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
6445 ocp_data &= ~LANWAKE_PIN;
6446 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
6448 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
6449 ocp_data &= ~DELAY_PHY_PWR_CHG;
6450 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
6452 r8153_aldps_en(tp, true);
6454 switch (tp->version) {
6461 r8153_u2p3en(tp, true);
6465 r8153_u1u2en(tp, true);
6468 static void rtl8153_down(struct r8152 *tp)
6472 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
6473 rtl_drop_queued_tx(tp);
6477 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6478 ocp_data &= ~LANWAKE_CLR_EN;
6479 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6481 r8153_u1u2en(tp, false);
6482 r8153_u2p3en(tp, false);
6483 r8153_power_cut_en(tp, false);
6484 r8153_aldps_en(tp, false);
6485 r8153_enter_oob(tp);
6486 r8153_aldps_en(tp, true);
6489 static void rtl8153b_up(struct r8152 *tp)
6493 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6496 r8153b_u1u2en(tp, false);
6497 r8153_u2p3en(tp, false);
6498 r8153_aldps_en(tp, false);
6500 r8153_first_init(tp);
6501 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6503 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6504 ocp_data &= ~PLA_MCU_SPDWN_EN;
6505 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6507 r8153_aldps_en(tp, true);
6509 if (tp->udev->speed >= USB_SPEED_SUPER)
6510 r8153b_u1u2en(tp, true);
6513 static void rtl8153b_down(struct r8152 *tp)
6517 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
6518 rtl_drop_queued_tx(tp);
6522 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6523 ocp_data |= PLA_MCU_SPDWN_EN;
6524 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6526 r8153b_u1u2en(tp, false);
6527 r8153_u2p3en(tp, false);
6528 r8153b_power_cut_en(tp, false);
6529 r8153_aldps_en(tp, false);
6530 r8153_enter_oob(tp);
6531 r8153_aldps_en(tp, true);
6534 static void rtl8153c_change_mtu(struct r8152 *tp)
6536 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
6537 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
6539 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6541 /* Adjust the tx fifo free credit full threshold, otherwise
6542 * the fifo would be too small to send a jumbo frame packet.
6544 if (tp->netdev->mtu < 8000)
6545 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8);
6547 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8);
6550 static void rtl8153c_up(struct r8152 *tp)
6554 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6557 r8153b_u1u2en(tp, false);
6558 r8153_u2p3en(tp, false);
6559 r8153_aldps_en(tp, false);
6561 rxdy_gated_en(tp, true);
6562 r8153_teredo_off(tp);
6564 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6565 ocp_data &= ~RCR_ACPT_ALL;
6566 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6568 rtl8152_nic_reset(tp);
6571 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6572 ocp_data &= ~NOW_IS_OOB;
6573 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6575 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6576 ocp_data &= ~MCU_BORW_EN;
6577 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6579 wait_oob_link_list_ready(tp);
6581 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6582 ocp_data |= RE_INIT_LL;
6583 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6585 wait_oob_link_list_ready(tp);
6587 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6589 rtl8153c_change_mtu(tp);
6591 rtl8152_nic_reset(tp);
6593 /* rx share fifo credit full threshold */
6594 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02);
6595 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08);
6596 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
6597 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
6599 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6601 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
6603 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
6605 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
6607 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
6609 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6610 ocp_data &= ~PLA_MCU_SPDWN_EN;
6611 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6613 r8153_aldps_en(tp, true);
6614 r8153b_u1u2en(tp, true);
6617 static void rtl8156_change_mtu(struct r8152 *tp)
6619 u32 rx_max_size = mtu_to_size(tp->netdev->mtu);
6621 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size);
6622 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
6623 r8156_fc_parameter(tp);
6625 /* TX share fifo free credit full threshold */
6626 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6627 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
6628 ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16);
6631 static void rtl8156_up(struct r8152 *tp)
6635 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6638 r8153b_u1u2en(tp, false);
6639 r8153_u2p3en(tp, false);
6640 r8153_aldps_en(tp, false);
6642 rxdy_gated_en(tp, true);
6643 r8153_teredo_off(tp);
6645 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6646 ocp_data &= ~RCR_ACPT_ALL;
6647 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6649 rtl8152_nic_reset(tp);
6652 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6653 ocp_data &= ~NOW_IS_OOB;
6654 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6656 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6657 ocp_data &= ~MCU_BORW_EN;
6658 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6660 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6662 rtl8156_change_mtu(tp);
6664 switch (tp->version) {
6668 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
6669 ocp_data |= ACT_ODMA;
6670 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
6676 /* share FIFO settings */
6677 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL);
6678 ocp_data &= ~RXFIFO_FULL_MASK;
6680 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data);
6682 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6683 ocp_data &= ~PLA_MCU_SPDWN_EN;
6684 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6686 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION);
6687 ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF);
6688 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data);
6690 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
6692 if (tp->saved_wolopts != __rtl_get_wol(tp)) {
6693 netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n");
6694 __rtl_set_wol(tp, tp->saved_wolopts);
6697 r8153_aldps_en(tp, true);
6698 r8153_u2p3en(tp, true);
6700 if (tp->udev->speed >= USB_SPEED_SUPER)
6701 r8153b_u1u2en(tp, true);
6704 static void rtl8156_down(struct r8152 *tp)
6708 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
6709 rtl_drop_queued_tx(tp);
6713 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6714 ocp_data |= PLA_MCU_SPDWN_EN;
6715 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6717 r8153b_u1u2en(tp, false);
6718 r8153_u2p3en(tp, false);
6719 r8153b_power_cut_en(tp, false);
6720 r8153_aldps_en(tp, false);
6722 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6723 ocp_data &= ~NOW_IS_OOB;
6724 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6726 /* RX FIFO settings for OOB */
6727 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 64 / 16);
6728 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, 1024 / 16);
6729 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, 4096 / 16);
6734 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1522);
6735 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_DEFAULT);
6737 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
6738 * type. Set it to zero. bits[7:0] are the W1C bits about
6739 * the events. Set them to all 1 to clear them.
6741 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
6743 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6744 ocp_data |= NOW_IS_OOB;
6745 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6747 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6748 ocp_data |= MCU_BORW_EN;
6749 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6751 rtl_rx_vlan_en(tp, true);
6752 rxdy_gated_en(tp, false);
6754 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6755 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
6756 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6758 r8153_aldps_en(tp, true);
6761 static bool rtl8152_in_nway(struct r8152 *tp)
6765 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
6766 tp->ocp_base = 0x2000;
6767 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
6768 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
6770 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
6771 if (nway_state & 0xc000)
6777 static bool rtl8153_in_nway(struct r8152 *tp)
6779 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
6781 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
6787 static void r8156_mdio_force_mode(struct r8152 *tp)
6791 /* Select force mode through 0xa5b4 bit 15
6792 * 0: MDIO force mode
6795 data = ocp_reg_read(tp, 0xa5b4);
6796 if (data & BIT(15)) {
6798 ocp_reg_write(tp, 0xa5b4, data);
6802 static void set_carrier(struct r8152 *tp)
6804 struct net_device *netdev = tp->netdev;
6805 struct napi_struct *napi = &tp->napi;
6808 speed = rtl8152_get_speed(tp);
6810 if (speed & LINK_STATUS) {
6811 if (!netif_carrier_ok(netdev)) {
6812 tp->rtl_ops.enable(tp);
6813 netif_stop_queue(netdev);
6815 netif_carrier_on(netdev);
6817 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6818 _rtl8152_set_rx_mode(netdev);
6820 netif_wake_queue(netdev);
6821 netif_info(tp, link, netdev, "carrier on\n");
6822 } else if (netif_queue_stopped(netdev) &&
6823 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
6824 netif_wake_queue(netdev);
6827 if (netif_carrier_ok(netdev)) {
6828 netif_carrier_off(netdev);
6829 tasklet_disable(&tp->tx_tl);
6831 tp->rtl_ops.disable(tp);
6833 tasklet_enable(&tp->tx_tl);
6834 netif_info(tp, link, netdev, "carrier off\n");
6839 static void rtl_work_func_t(struct work_struct *work)
6841 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
6843 /* If the device is unplugged or !netif_running(), the workqueue
6844 * doesn't need to wake the device, and could return directly.
6846 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags) || !netif_running(tp->netdev))
6849 if (usb_autopm_get_interface(tp->intf) < 0)
6852 if (!test_bit(WORK_ENABLE, &tp->flags))
6855 if (!mutex_trylock(&tp->control)) {
6856 schedule_delayed_work(&tp->schedule, 0);
6860 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
6863 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
6864 _rtl8152_set_rx_mode(tp->netdev);
6866 /* don't schedule tasket before linking */
6867 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
6868 netif_carrier_ok(tp->netdev))
6869 tasklet_schedule(&tp->tx_tl);
6871 if (test_and_clear_bit(RX_EPROTO, &tp->flags) &&
6872 !list_empty(&tp->rx_done))
6873 napi_schedule(&tp->napi);
6875 mutex_unlock(&tp->control);
6878 usb_autopm_put_interface(tp->intf);
6881 static void rtl_hw_phy_work_func_t(struct work_struct *work)
6883 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
6885 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
6888 if (usb_autopm_get_interface(tp->intf) < 0)
6891 mutex_lock(&tp->control);
6893 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
6894 tp->rtl_fw.retry = false;
6895 tp->rtl_fw.fw = NULL;
6897 /* Delay execution in case request_firmware() is not ready yet.
6899 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
6903 tp->rtl_ops.hw_phy_cfg(tp);
6905 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
6909 mutex_unlock(&tp->control);
6911 usb_autopm_put_interface(tp->intf);
6914 #ifdef CONFIG_PM_SLEEP
6915 static int rtl_notifier(struct notifier_block *nb, unsigned long action,
6918 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
6921 case PM_HIBERNATION_PREPARE:
6922 case PM_SUSPEND_PREPARE:
6923 usb_autopm_get_interface(tp->intf);
6926 case PM_POST_HIBERNATION:
6927 case PM_POST_SUSPEND:
6928 usb_autopm_put_interface(tp->intf);
6931 case PM_POST_RESTORE:
6932 case PM_RESTORE_PREPARE:
6941 static int rtl8152_open(struct net_device *netdev)
6943 struct r8152 *tp = netdev_priv(netdev);
6946 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
6947 cancel_delayed_work_sync(&tp->hw_phy_work);
6948 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
6951 res = alloc_all_mem(tp);
6955 res = usb_autopm_get_interface(tp->intf);
6959 mutex_lock(&tp->control);
6963 netif_carrier_off(netdev);
6964 netif_start_queue(netdev);
6965 set_bit(WORK_ENABLE, &tp->flags);
6967 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
6970 netif_device_detach(tp->netdev);
6971 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
6975 napi_enable(&tp->napi);
6976 tasklet_enable(&tp->tx_tl);
6978 mutex_unlock(&tp->control);
6980 usb_autopm_put_interface(tp->intf);
6981 #ifdef CONFIG_PM_SLEEP
6982 tp->pm_notifier.notifier_call = rtl_notifier;
6983 register_pm_notifier(&tp->pm_notifier);
6988 mutex_unlock(&tp->control);
6989 usb_autopm_put_interface(tp->intf);
6996 static int rtl8152_close(struct net_device *netdev)
6998 struct r8152 *tp = netdev_priv(netdev);
7001 #ifdef CONFIG_PM_SLEEP
7002 unregister_pm_notifier(&tp->pm_notifier);
7004 tasklet_disable(&tp->tx_tl);
7005 clear_bit(WORK_ENABLE, &tp->flags);
7006 usb_kill_urb(tp->intr_urb);
7007 cancel_delayed_work_sync(&tp->schedule);
7008 napi_disable(&tp->napi);
7009 netif_stop_queue(netdev);
7011 res = usb_autopm_get_interface(tp->intf);
7012 if (res < 0 || test_bit(RTL8152_INACCESSIBLE, &tp->flags)) {
7013 rtl_drop_queued_tx(tp);
7016 mutex_lock(&tp->control);
7018 tp->rtl_ops.down(tp);
7020 mutex_unlock(&tp->control);
7024 usb_autopm_put_interface(tp->intf);
7031 static void rtl_tally_reset(struct r8152 *tp)
7035 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
7036 ocp_data |= TALLY_RESET;
7037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
7040 static void r8152b_init(struct r8152 *tp)
7045 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
7048 data = r8152_mdio_read(tp, MII_BMCR);
7049 if (data & BMCR_PDOWN) {
7050 data &= ~BMCR_PDOWN;
7051 r8152_mdio_write(tp, MII_BMCR, data);
7054 r8152_aldps_en(tp, false);
7056 if (tp->version == RTL_VER_01) {
7057 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
7058 ocp_data &= ~LED_MODE_MASK;
7059 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
7062 r8152_power_cut_en(tp, false);
7064 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7065 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
7066 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7067 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
7068 ocp_data &= ~MCU_CLK_RATIO_MASK;
7069 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
7070 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
7071 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
7072 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
7073 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
7075 rtl_tally_reset(tp);
7077 /* enable rx aggregation */
7078 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7079 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7080 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7083 static void r8153_init(struct r8152 *tp)
7089 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
7092 r8153_u1u2en(tp, false);
7094 for (i = 0; i < 500; i++) {
7095 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7100 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
7104 data = r8153_phy_status(tp, 0);
7106 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
7107 tp->version == RTL_VER_05)
7108 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
7110 data = r8152_mdio_read(tp, MII_BMCR);
7111 if (data & BMCR_PDOWN) {
7112 data &= ~BMCR_PDOWN;
7113 r8152_mdio_write(tp, MII_BMCR, data);
7116 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7118 r8153_u2p3en(tp, false);
7120 if (tp->version == RTL_VER_04) {
7121 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
7122 ocp_data &= ~pwd_dn_scale_mask;
7123 ocp_data |= pwd_dn_scale(96);
7124 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
7126 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
7127 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
7128 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
7129 } else if (tp->version == RTL_VER_05) {
7130 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
7131 ocp_data &= ~ECM_ALDPS;
7132 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
7134 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
7135 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
7136 ocp_data &= ~DYNAMIC_BURST;
7138 ocp_data |= DYNAMIC_BURST;
7139 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
7140 } else if (tp->version == RTL_VER_06) {
7141 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
7142 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
7143 ocp_data &= ~DYNAMIC_BURST;
7145 ocp_data |= DYNAMIC_BURST;
7146 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
7148 r8153_queue_wake(tp, false);
7150 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7151 if (rtl8152_get_speed(tp) & LINK_STATUS)
7152 ocp_data |= CUR_LINK_OK;
7154 ocp_data &= ~CUR_LINK_OK;
7155 ocp_data |= POLL_LINK_CHG;
7156 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7159 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
7160 ocp_data |= EP4_FULL_FC;
7161 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
7163 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
7164 ocp_data &= ~TIMER11_EN;
7165 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
7167 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
7168 ocp_data &= ~LED_MODE_MASK;
7169 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
7171 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
7172 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
7173 ocp_data |= LPM_TIMER_500MS;
7175 ocp_data |= LPM_TIMER_500US;
7176 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
7178 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
7179 ocp_data &= ~SEN_VAL_MASK;
7180 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
7181 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
7183 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
7185 r8153_power_cut_en(tp, false);
7186 rtl_runtime_suspend_enable(tp, false);
7187 r8153_mac_clk_speed_down(tp, false);
7188 r8153_u1u2en(tp, true);
7189 usb_enable_lpm(tp->udev);
7191 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
7192 ocp_data |= LANWAKE_CLR_EN;
7193 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
7195 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
7196 ocp_data &= ~LANWAKE_PIN;
7197 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
7199 /* rx aggregation */
7200 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7201 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7202 if (tp->dell_tb_rx_agg_bug)
7203 ocp_data |= RX_AGG_DISABLE;
7205 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7207 rtl_tally_reset(tp);
7209 switch (tp->udev->speed) {
7210 case USB_SPEED_SUPER:
7211 case USB_SPEED_SUPER_PLUS:
7212 tp->coalesce = COALESCE_SUPER;
7214 case USB_SPEED_HIGH:
7215 tp->coalesce = COALESCE_HIGH;
7218 tp->coalesce = COALESCE_SLOW;
7223 static void r8153b_init(struct r8152 *tp)
7229 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
7232 r8153b_u1u2en(tp, false);
7234 for (i = 0; i < 500; i++) {
7235 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7240 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
7244 data = r8153_phy_status(tp, 0);
7246 data = r8152_mdio_read(tp, MII_BMCR);
7247 if (data & BMCR_PDOWN) {
7248 data &= ~BMCR_PDOWN;
7249 r8152_mdio_write(tp, MII_BMCR, data);
7252 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7254 r8153_u2p3en(tp, false);
7256 /* MSC timer = 0xfff * 8ms = 32760 ms */
7257 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7259 r8153b_power_cut_en(tp, false);
7260 r8153b_ups_en(tp, false);
7261 r8153_queue_wake(tp, false);
7262 rtl_runtime_suspend_enable(tp, false);
7264 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7265 if (rtl8152_get_speed(tp) & LINK_STATUS)
7266 ocp_data |= CUR_LINK_OK;
7268 ocp_data &= ~CUR_LINK_OK;
7269 ocp_data |= POLL_LINK_CHG;
7270 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7272 if (tp->udev->speed >= USB_SPEED_SUPER)
7273 r8153b_u1u2en(tp, true);
7275 usb_enable_lpm(tp->udev);
7277 /* MAC clock speed down */
7278 r8153_mac_clk_speed_down(tp, true);
7280 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
7281 ocp_data &= ~PLA_MCU_SPDWN_EN;
7282 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
7284 if (tp->version == RTL_VER_09) {
7285 /* Disable Test IO for 32QFN */
7286 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
7287 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7288 ocp_data |= TEST_IO_OFF;
7289 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7293 set_bit(GREEN_ETHERNET, &tp->flags);
7295 /* rx aggregation */
7296 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7297 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7298 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7300 rtl_tally_reset(tp);
7302 tp->coalesce = 15000; /* 15 us */
7305 static void r8153c_init(struct r8152 *tp)
7311 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
7314 r8153b_u1u2en(tp, false);
7316 /* Disable spi_en */
7317 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
7318 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
7319 ocp_data &= ~BIT(3);
7320 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
7321 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0);
7323 ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data);
7325 for (i = 0; i < 500; i++) {
7326 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7331 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
7335 data = r8153_phy_status(tp, 0);
7337 data = r8152_mdio_read(tp, MII_BMCR);
7338 if (data & BMCR_PDOWN) {
7339 data &= ~BMCR_PDOWN;
7340 r8152_mdio_write(tp, MII_BMCR, data);
7343 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7345 r8153_u2p3en(tp, false);
7347 /* MSC timer = 0xfff * 8ms = 32760 ms */
7348 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7350 r8153b_power_cut_en(tp, false);
7351 r8153c_ups_en(tp, false);
7352 r8153_queue_wake(tp, false);
7353 rtl_runtime_suspend_enable(tp, false);
7355 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7356 if (rtl8152_get_speed(tp) & LINK_STATUS)
7357 ocp_data |= CUR_LINK_OK;
7359 ocp_data &= ~CUR_LINK_OK;
7361 ocp_data |= POLL_LINK_CHG;
7362 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7364 r8153b_u1u2en(tp, true);
7366 usb_enable_lpm(tp->udev);
7368 /* MAC clock speed down */
7369 r8153_mac_clk_speed_down(tp, true);
7371 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
7372 ocp_data &= ~BIT(7);
7373 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
7375 set_bit(GREEN_ETHERNET, &tp->flags);
7377 /* rx aggregation */
7378 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7379 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7380 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7382 rtl_tally_reset(tp);
7384 tp->coalesce = 15000; /* 15 us */
7387 static void r8156_hw_phy_cfg(struct r8152 *tp)
7392 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7393 if (ocp_data & PCUT_STATUS) {
7394 ocp_data &= ~PCUT_STATUS;
7395 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7398 data = r8153_phy_status(tp, 0);
7400 case PHY_STAT_EXT_INIT:
7401 rtl8152_apply_firmware(tp, true);
7403 data = ocp_reg_read(tp, 0xa468);
7404 data &= ~(BIT(3) | BIT(1));
7405 ocp_reg_write(tp, 0xa468, data);
7407 case PHY_STAT_LAN_ON:
7408 case PHY_STAT_PWRDN:
7410 rtl8152_apply_firmware(tp, false);
7414 /* disable ALDPS before updating the PHY parameters */
7415 r8153_aldps_en(tp, false);
7417 /* disable EEE before updating the PHY parameters */
7418 rtl_eee_enable(tp, false);
7420 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7421 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7423 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7424 ocp_data |= PFM_PWM_SWITCH;
7425 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7427 switch (tp->version) {
7429 data = ocp_reg_read(tp, 0xad40);
7431 data |= BIT(7) | BIT(2);
7432 ocp_reg_write(tp, 0xad40, data);
7434 data = ocp_reg_read(tp, 0xad4e);
7436 ocp_reg_write(tp, 0xad4e, data);
7437 data = ocp_reg_read(tp, 0xad16);
7440 ocp_reg_write(tp, 0xad16, data);
7441 data = ocp_reg_read(tp, 0xad32);
7444 ocp_reg_write(tp, 0xad32, data);
7445 data = ocp_reg_read(tp, 0xac08);
7446 data &= ~(BIT(12) | BIT(8));
7447 ocp_reg_write(tp, 0xac08, data);
7448 data = ocp_reg_read(tp, 0xac8a);
7449 data |= BIT(12) | BIT(13) | BIT(14);
7451 ocp_reg_write(tp, 0xac8a, data);
7452 data = ocp_reg_read(tp, 0xad18);
7454 ocp_reg_write(tp, 0xad18, data);
7455 data = ocp_reg_read(tp, 0xad1a);
7457 ocp_reg_write(tp, 0xad1a, data);
7458 data = ocp_reg_read(tp, 0xad1c);
7460 ocp_reg_write(tp, 0xad1c, data);
7462 data = sram_read(tp, 0x80ea);
7465 sram_write(tp, 0x80ea, data);
7466 data = sram_read(tp, 0x80eb);
7469 sram_write(tp, 0x80eb, data);
7470 data = sram_read(tp, 0x80f8);
7473 sram_write(tp, 0x80f8, data);
7474 data = sram_read(tp, 0x80f1);
7477 sram_write(tp, 0x80f1, data);
7479 data = sram_read(tp, 0x80fe);
7482 sram_write(tp, 0x80fe, data);
7483 data = sram_read(tp, 0x8102);
7486 sram_write(tp, 0x8102, data);
7487 data = sram_read(tp, 0x8015);
7490 sram_write(tp, 0x8015, data);
7491 data = sram_read(tp, 0x8100);
7494 sram_write(tp, 0x8100, data);
7495 data = sram_read(tp, 0x8014);
7498 sram_write(tp, 0x8014, data);
7499 data = sram_read(tp, 0x8016);
7502 sram_write(tp, 0x8016, data);
7503 data = sram_read(tp, 0x80dc);
7506 sram_write(tp, 0x80dc, data);
7507 data = sram_read(tp, 0x80df);
7509 sram_write(tp, 0x80df, data);
7510 data = sram_read(tp, 0x80e1);
7512 sram_write(tp, 0x80e1, data);
7514 data = ocp_reg_read(tp, 0xbf06);
7517 ocp_reg_write(tp, 0xbf06, data);
7519 sram_write(tp, 0x819f, 0xddb6);
7521 ocp_reg_write(tp, 0xbc34, 0x5555);
7522 data = ocp_reg_read(tp, 0xbf0a);
7525 ocp_reg_write(tp, 0xbf0a, data);
7527 data = ocp_reg_read(tp, 0xbd2c);
7529 ocp_reg_write(tp, 0xbd2c, data);
7532 data = ocp_reg_read(tp, 0xad16);
7534 ocp_reg_write(tp, 0xad16, data);
7535 data = ocp_reg_read(tp, 0xad32);
7538 ocp_reg_write(tp, 0xad32, data);
7539 data = ocp_reg_read(tp, 0xac08);
7540 data &= ~(BIT(12) | BIT(8));
7541 ocp_reg_write(tp, 0xac08, data);
7542 data = ocp_reg_read(tp, 0xacc0);
7545 ocp_reg_write(tp, 0xacc0, data);
7546 data = ocp_reg_read(tp, 0xad40);
7548 data |= BIT(6) | BIT(2);
7549 ocp_reg_write(tp, 0xad40, data);
7550 data = ocp_reg_read(tp, 0xac14);
7552 ocp_reg_write(tp, 0xac14, data);
7553 data = ocp_reg_read(tp, 0xac80);
7554 data &= ~(BIT(8) | BIT(9));
7555 ocp_reg_write(tp, 0xac80, data);
7556 data = ocp_reg_read(tp, 0xac5e);
7559 ocp_reg_write(tp, 0xac5e, data);
7560 ocp_reg_write(tp, 0xad4c, 0x00a8);
7561 ocp_reg_write(tp, 0xac5c, 0x01ff);
7562 data = ocp_reg_read(tp, 0xac8a);
7564 data |= BIT(4) | BIT(5);
7565 ocp_reg_write(tp, 0xac8a, data);
7566 ocp_reg_write(tp, 0xb87c, 0x8157);
7567 data = ocp_reg_read(tp, 0xb87e);
7570 ocp_reg_write(tp, 0xb87e, data);
7571 ocp_reg_write(tp, 0xb87c, 0x8159);
7572 data = ocp_reg_read(tp, 0xb87e);
7575 ocp_reg_write(tp, 0xb87e, data);
7578 ocp_reg_write(tp, 0xb87c, 0x80a2);
7579 ocp_reg_write(tp, 0xb87e, 0x0153);
7580 ocp_reg_write(tp, 0xb87c, 0x809c);
7581 ocp_reg_write(tp, 0xb87e, 0x0153);
7584 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056);
7586 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7587 ocp_data |= EN_XG_LIP | EN_G_LIP;
7588 ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7590 sram_write(tp, 0x8257, 0x020f); /* XG PLL */
7591 sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */
7593 if (rtl_phy_patch_request(tp, true, true))
7597 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7598 ocp_data |= EEE_SPDWN_EN;
7599 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7601 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7602 data &= ~(EN_EEE_100 | EN_EEE_1000);
7603 data |= EN_10M_CLKDIV;
7604 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7605 tp->ups_info._10m_ckdiv = true;
7606 tp->ups_info.eee_plloff_100 = false;
7607 tp->ups_info.eee_plloff_giga = false;
7609 data = ocp_reg_read(tp, OCP_POWER_CFG);
7610 data &= ~EEE_CLKDIV_EN;
7611 ocp_reg_write(tp, OCP_POWER_CFG, data);
7612 tp->ups_info.eee_ckdiv = false;
7614 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
7615 ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5));
7616 tp->ups_info._250m_ckdiv = false;
7618 rtl_phy_patch_request(tp, false, true);
7620 /* enable ADC Ibias Cal */
7621 data = ocp_reg_read(tp, 0xd068);
7623 ocp_reg_write(tp, 0xd068, data);
7625 /* enable Thermal Sensor */
7626 data = sram_read(tp, 0x81a2);
7628 sram_write(tp, 0x81a2, data);
7629 data = ocp_reg_read(tp, 0xb54c);
7632 ocp_reg_write(tp, 0xb54c, data);
7634 /* Nway 2.5G Lite */
7635 data = ocp_reg_read(tp, 0xa454);
7637 ocp_reg_write(tp, 0xa454, data);
7639 /* CS DSP solution */
7640 data = ocp_reg_read(tp, OCP_10GBT_CTRL);
7641 data |= RTL_ADV2_5G_F_R;
7642 ocp_reg_write(tp, OCP_10GBT_CTRL, data);
7643 data = ocp_reg_read(tp, 0xad4e);
7645 ocp_reg_write(tp, 0xad4e, data);
7646 data = ocp_reg_read(tp, 0xa86a);
7648 ocp_reg_write(tp, 0xa86a, data);
7651 if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) &&
7652 (ocp_reg_read(tp, 0xd068) & BIT(1))) {
7655 data = ocp_reg_read(tp, 0xd068);
7657 data |= 0x1; /* p0 */
7658 ocp_reg_write(tp, 0xd068, data);
7659 swap_a = ocp_reg_read(tp, 0xd06a);
7661 data |= 0x18; /* p3 */
7662 ocp_reg_write(tp, 0xd068, data);
7663 swap_b = ocp_reg_read(tp, 0xd06a);
7664 data &= ~0x18; /* p0 */
7665 ocp_reg_write(tp, 0xd068, data);
7666 ocp_reg_write(tp, 0xd06a,
7667 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7668 data |= 0x18; /* p3 */
7669 ocp_reg_write(tp, 0xd068, data);
7670 ocp_reg_write(tp, 0xd06a,
7671 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7673 data |= 0x08; /* p1 */
7674 ocp_reg_write(tp, 0xd068, data);
7675 swap_a = ocp_reg_read(tp, 0xd06a);
7677 data |= 0x10; /* p2 */
7678 ocp_reg_write(tp, 0xd068, data);
7679 swap_b = ocp_reg_read(tp, 0xd06a);
7681 data |= 0x08; /* p1 */
7682 ocp_reg_write(tp, 0xd068, data);
7683 ocp_reg_write(tp, 0xd06a,
7684 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7686 data |= 0x10; /* p2 */
7687 ocp_reg_write(tp, 0xd068, data);
7688 ocp_reg_write(tp, 0xd06a,
7689 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7690 swap_a = ocp_reg_read(tp, 0xbd5a);
7691 swap_b = ocp_reg_read(tp, 0xbd5c);
7692 ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) |
7693 ((swap_b & 0x1f) << 8) |
7694 ((swap_b >> 8) & 0x1f));
7695 ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) |
7696 ((swap_a & 0x1f) << 8) |
7697 ((swap_a >> 8) & 0x1f));
7698 swap_a = ocp_reg_read(tp, 0xbc18);
7699 swap_b = ocp_reg_read(tp, 0xbc1a);
7700 ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) |
7701 ((swap_b & 0x1f) << 8) |
7702 ((swap_b >> 8) & 0x1f));
7703 ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) |
7704 ((swap_a & 0x1f) << 8) |
7705 ((swap_a >> 8) & 0x1f));
7708 /* Notify the MAC when the speed is changed to force mode. */
7709 data = ocp_reg_read(tp, OCP_INTR_EN);
7710 data |= INTR_SPEED_FORCE;
7711 ocp_reg_write(tp, OCP_INTR_EN, data);
7717 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7719 data = ocp_reg_read(tp, 0xa428);
7721 ocp_reg_write(tp, 0xa428, data);
7722 data = ocp_reg_read(tp, 0xa5ea);
7724 ocp_reg_write(tp, 0xa5ea, data);
7725 tp->ups_info.lite_mode = 0;
7728 rtl_eee_enable(tp, true);
7730 r8153_aldps_en(tp, true);
7731 r8152b_enable_fc(tp);
7732 r8153_u2p3en(tp, true);
7734 set_bit(PHY_RESET, &tp->flags);
7737 static void r8156b_hw_phy_cfg(struct r8152 *tp)
7742 switch (tp->version) {
7744 ocp_reg_write(tp, 0xbf86, 0x9000);
7745 data = ocp_reg_read(tp, 0xc402);
7747 ocp_reg_write(tp, 0xc402, data);
7749 ocp_reg_write(tp, 0xc402, data);
7750 ocp_reg_write(tp, 0xbd86, 0x1010);
7751 ocp_reg_write(tp, 0xbd88, 0x1010);
7752 data = ocp_reg_read(tp, 0xbd4e);
7753 data &= ~(BIT(10) | BIT(11));
7755 ocp_reg_write(tp, 0xbd4e, data);
7756 data = ocp_reg_read(tp, 0xbf46);
7759 ocp_reg_write(tp, 0xbf46, data);
7763 r8156b_wait_loading_flash(tp);
7769 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7770 if (ocp_data & PCUT_STATUS) {
7771 ocp_data &= ~PCUT_STATUS;
7772 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7775 data = r8153_phy_status(tp, 0);
7777 case PHY_STAT_EXT_INIT:
7778 rtl8152_apply_firmware(tp, true);
7780 data = ocp_reg_read(tp, 0xa466);
7782 ocp_reg_write(tp, 0xa466, data);
7784 data = ocp_reg_read(tp, 0xa468);
7785 data &= ~(BIT(3) | BIT(1));
7786 ocp_reg_write(tp, 0xa468, data);
7788 case PHY_STAT_LAN_ON:
7789 case PHY_STAT_PWRDN:
7791 rtl8152_apply_firmware(tp, false);
7795 data = r8152_mdio_read(tp, MII_BMCR);
7796 if (data & BMCR_PDOWN) {
7797 data &= ~BMCR_PDOWN;
7798 r8152_mdio_write(tp, MII_BMCR, data);
7801 /* disable ALDPS before updating the PHY parameters */
7802 r8153_aldps_en(tp, false);
7804 /* disable EEE before updating the PHY parameters */
7805 rtl_eee_enable(tp, false);
7807 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7808 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7810 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7811 ocp_data |= PFM_PWM_SWITCH;
7812 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7814 switch (tp->version) {
7816 data = ocp_reg_read(tp, 0xbc08);
7817 data |= BIT(3) | BIT(2);
7818 ocp_reg_write(tp, 0xbc08, data);
7820 data = sram_read(tp, 0x8fff);
7823 sram_write(tp, 0x8fff, data);
7825 data = ocp_reg_read(tp, 0xacda);
7827 ocp_reg_write(tp, 0xacda, data);
7828 data = ocp_reg_read(tp, 0xacde);
7830 ocp_reg_write(tp, 0xacde, data);
7831 ocp_reg_write(tp, 0xac8c, 0x0ffc);
7832 ocp_reg_write(tp, 0xac46, 0xb7b4);
7833 ocp_reg_write(tp, 0xac50, 0x0fbc);
7834 ocp_reg_write(tp, 0xac3c, 0x9240);
7835 ocp_reg_write(tp, 0xac4e, 0x0db4);
7836 ocp_reg_write(tp, 0xacc6, 0x0707);
7837 ocp_reg_write(tp, 0xacc8, 0xa0d3);
7838 ocp_reg_write(tp, 0xad08, 0x0007);
7840 ocp_reg_write(tp, 0xb87c, 0x8560);
7841 ocp_reg_write(tp, 0xb87e, 0x19cc);
7842 ocp_reg_write(tp, 0xb87c, 0x8562);
7843 ocp_reg_write(tp, 0xb87e, 0x19cc);
7844 ocp_reg_write(tp, 0xb87c, 0x8564);
7845 ocp_reg_write(tp, 0xb87e, 0x19cc);
7846 ocp_reg_write(tp, 0xb87c, 0x8566);
7847 ocp_reg_write(tp, 0xb87e, 0x147d);
7848 ocp_reg_write(tp, 0xb87c, 0x8568);
7849 ocp_reg_write(tp, 0xb87e, 0x147d);
7850 ocp_reg_write(tp, 0xb87c, 0x856a);
7851 ocp_reg_write(tp, 0xb87e, 0x147d);
7852 ocp_reg_write(tp, 0xb87c, 0x8ffe);
7853 ocp_reg_write(tp, 0xb87e, 0x0907);
7854 ocp_reg_write(tp, 0xb87c, 0x80d6);
7855 ocp_reg_write(tp, 0xb87e, 0x2801);
7856 ocp_reg_write(tp, 0xb87c, 0x80f2);
7857 ocp_reg_write(tp, 0xb87e, 0x2801);
7858 ocp_reg_write(tp, 0xb87c, 0x80f4);
7859 ocp_reg_write(tp, 0xb87e, 0x6077);
7860 ocp_reg_write(tp, 0xb506, 0x01e7);
7862 ocp_reg_write(tp, 0xb87c, 0x8013);
7863 ocp_reg_write(tp, 0xb87e, 0x0700);
7864 ocp_reg_write(tp, 0xb87c, 0x8fb9);
7865 ocp_reg_write(tp, 0xb87e, 0x2801);
7866 ocp_reg_write(tp, 0xb87c, 0x8fba);
7867 ocp_reg_write(tp, 0xb87e, 0x0100);
7868 ocp_reg_write(tp, 0xb87c, 0x8fbc);
7869 ocp_reg_write(tp, 0xb87e, 0x1900);
7870 ocp_reg_write(tp, 0xb87c, 0x8fbe);
7871 ocp_reg_write(tp, 0xb87e, 0xe100);
7872 ocp_reg_write(tp, 0xb87c, 0x8fc0);
7873 ocp_reg_write(tp, 0xb87e, 0x0800);
7874 ocp_reg_write(tp, 0xb87c, 0x8fc2);
7875 ocp_reg_write(tp, 0xb87e, 0xe500);
7876 ocp_reg_write(tp, 0xb87c, 0x8fc4);
7877 ocp_reg_write(tp, 0xb87e, 0x0f00);
7878 ocp_reg_write(tp, 0xb87c, 0x8fc6);
7879 ocp_reg_write(tp, 0xb87e, 0xf100);
7880 ocp_reg_write(tp, 0xb87c, 0x8fc8);
7881 ocp_reg_write(tp, 0xb87e, 0x0400);
7882 ocp_reg_write(tp, 0xb87c, 0x8fca);
7883 ocp_reg_write(tp, 0xb87e, 0xf300);
7884 ocp_reg_write(tp, 0xb87c, 0x8fcc);
7885 ocp_reg_write(tp, 0xb87e, 0xfd00);
7886 ocp_reg_write(tp, 0xb87c, 0x8fce);
7887 ocp_reg_write(tp, 0xb87e, 0xff00);
7888 ocp_reg_write(tp, 0xb87c, 0x8fd0);
7889 ocp_reg_write(tp, 0xb87e, 0xfb00);
7890 ocp_reg_write(tp, 0xb87c, 0x8fd2);
7891 ocp_reg_write(tp, 0xb87e, 0x0100);
7892 ocp_reg_write(tp, 0xb87c, 0x8fd4);
7893 ocp_reg_write(tp, 0xb87e, 0xf400);
7894 ocp_reg_write(tp, 0xb87c, 0x8fd6);
7895 ocp_reg_write(tp, 0xb87e, 0xff00);
7896 ocp_reg_write(tp, 0xb87c, 0x8fd8);
7897 ocp_reg_write(tp, 0xb87e, 0xf600);
7899 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7900 ocp_data |= EN_XG_LIP | EN_G_LIP;
7901 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7902 ocp_reg_write(tp, 0xb87c, 0x813d);
7903 ocp_reg_write(tp, 0xb87e, 0x390e);
7904 ocp_reg_write(tp, 0xb87c, 0x814f);
7905 ocp_reg_write(tp, 0xb87e, 0x790e);
7906 ocp_reg_write(tp, 0xb87c, 0x80b0);
7907 ocp_reg_write(tp, 0xb87e, 0x0f31);
7908 data = ocp_reg_read(tp, 0xbf4c);
7910 ocp_reg_write(tp, 0xbf4c, data);
7911 data = ocp_reg_read(tp, 0xbcca);
7912 data |= BIT(9) | BIT(8);
7913 ocp_reg_write(tp, 0xbcca, data);
7914 ocp_reg_write(tp, 0xb87c, 0x8141);
7915 ocp_reg_write(tp, 0xb87e, 0x320e);
7916 ocp_reg_write(tp, 0xb87c, 0x8153);
7917 ocp_reg_write(tp, 0xb87e, 0x720e);
7918 ocp_reg_write(tp, 0xb87c, 0x8529);
7919 ocp_reg_write(tp, 0xb87e, 0x050e);
7920 data = ocp_reg_read(tp, OCP_EEE_CFG);
7921 data &= ~CTAP_SHORT_EN;
7922 ocp_reg_write(tp, OCP_EEE_CFG, data);
7924 sram_write(tp, 0x816c, 0xc4a0);
7925 sram_write(tp, 0x8170, 0xc4a0);
7926 sram_write(tp, 0x8174, 0x04a0);
7927 sram_write(tp, 0x8178, 0x04a0);
7928 sram_write(tp, 0x817c, 0x0719);
7929 sram_write(tp, 0x8ff4, 0x0400);
7930 sram_write(tp, 0x8ff1, 0x0404);
7932 ocp_reg_write(tp, 0xbf4a, 0x001b);
7933 ocp_reg_write(tp, 0xb87c, 0x8033);
7934 ocp_reg_write(tp, 0xb87e, 0x7c13);
7935 ocp_reg_write(tp, 0xb87c, 0x8037);
7936 ocp_reg_write(tp, 0xb87e, 0x7c13);
7937 ocp_reg_write(tp, 0xb87c, 0x803b);
7938 ocp_reg_write(tp, 0xb87e, 0xfc32);
7939 ocp_reg_write(tp, 0xb87c, 0x803f);
7940 ocp_reg_write(tp, 0xb87e, 0x7c13);
7941 ocp_reg_write(tp, 0xb87c, 0x8043);
7942 ocp_reg_write(tp, 0xb87e, 0x7c13);
7943 ocp_reg_write(tp, 0xb87c, 0x8047);
7944 ocp_reg_write(tp, 0xb87e, 0x7c13);
7946 ocp_reg_write(tp, 0xb87c, 0x8145);
7947 ocp_reg_write(tp, 0xb87e, 0x370e);
7948 ocp_reg_write(tp, 0xb87c, 0x8157);
7949 ocp_reg_write(tp, 0xb87e, 0x770e);
7950 ocp_reg_write(tp, 0xb87c, 0x8169);
7951 ocp_reg_write(tp, 0xb87e, 0x0d0a);
7952 ocp_reg_write(tp, 0xb87c, 0x817b);
7953 ocp_reg_write(tp, 0xb87e, 0x1d0a);
7955 data = sram_read(tp, 0x8217);
7958 sram_write(tp, 0x8217, data);
7959 data = sram_read(tp, 0x821a);
7962 sram_write(tp, 0x821a, data);
7963 sram_write(tp, 0x80da, 0x0403);
7964 data = sram_read(tp, 0x80dc);
7967 sram_write(tp, 0x80dc, data);
7968 sram_write(tp, 0x80b3, 0x0384);
7969 sram_write(tp, 0x80b7, 0x2007);
7970 data = sram_read(tp, 0x80ba);
7973 sram_write(tp, 0x80ba, data);
7974 sram_write(tp, 0x80b5, 0xf009);
7975 data = sram_read(tp, 0x80bd);
7978 sram_write(tp, 0x80bd, data);
7979 sram_write(tp, 0x80c7, 0xf083);
7980 sram_write(tp, 0x80dd, 0x03f0);
7981 data = sram_read(tp, 0x80df);
7984 sram_write(tp, 0x80df, data);
7985 sram_write(tp, 0x80cb, 0x2007);
7986 data = sram_read(tp, 0x80ce);
7989 sram_write(tp, 0x80ce, data);
7990 sram_write(tp, 0x80c9, 0x8009);
7991 data = sram_read(tp, 0x80d1);
7994 sram_write(tp, 0x80d1, data);
7995 sram_write(tp, 0x80a3, 0x200a);
7996 sram_write(tp, 0x80a5, 0xf0ad);
7997 sram_write(tp, 0x809f, 0x6073);
7998 sram_write(tp, 0x80a1, 0x000b);
7999 data = sram_read(tp, 0x80a9);
8002 sram_write(tp, 0x80a9, data);
8004 if (rtl_phy_patch_request(tp, true, true))
8007 data = ocp_reg_read(tp, 0xb896);
8009 ocp_reg_write(tp, 0xb896, data);
8010 data = ocp_reg_read(tp, 0xb892);
8012 ocp_reg_write(tp, 0xb892, data);
8013 ocp_reg_write(tp, 0xb88e, 0xc23e);
8014 ocp_reg_write(tp, 0xb890, 0x0000);
8015 ocp_reg_write(tp, 0xb88e, 0xc240);
8016 ocp_reg_write(tp, 0xb890, 0x0103);
8017 ocp_reg_write(tp, 0xb88e, 0xc242);
8018 ocp_reg_write(tp, 0xb890, 0x0507);
8019 ocp_reg_write(tp, 0xb88e, 0xc244);
8020 ocp_reg_write(tp, 0xb890, 0x090b);
8021 ocp_reg_write(tp, 0xb88e, 0xc246);
8022 ocp_reg_write(tp, 0xb890, 0x0c0e);
8023 ocp_reg_write(tp, 0xb88e, 0xc248);
8024 ocp_reg_write(tp, 0xb890, 0x1012);
8025 ocp_reg_write(tp, 0xb88e, 0xc24a);
8026 ocp_reg_write(tp, 0xb890, 0x1416);
8027 data = ocp_reg_read(tp, 0xb896);
8029 ocp_reg_write(tp, 0xb896, data);
8031 rtl_phy_patch_request(tp, false, true);
8033 data = ocp_reg_read(tp, 0xa86a);
8035 ocp_reg_write(tp, 0xa86a, data);
8036 data = ocp_reg_read(tp, 0xa6f0);
8038 ocp_reg_write(tp, 0xa6f0, data);
8040 ocp_reg_write(tp, 0xbfa0, 0xd70d);
8041 ocp_reg_write(tp, 0xbfa2, 0x4100);
8042 ocp_reg_write(tp, 0xbfa4, 0xe868);
8043 ocp_reg_write(tp, 0xbfa6, 0xdc59);
8044 ocp_reg_write(tp, 0xb54c, 0x3c18);
8045 data = ocp_reg_read(tp, 0xbfa4);
8047 ocp_reg_write(tp, 0xbfa4, data);
8048 data = sram_read(tp, 0x817d);
8050 sram_write(tp, 0x817d, data);
8054 data = ocp_reg_read(tp, 0xac46);
8057 ocp_reg_write(tp, 0xac46, data);
8058 data = ocp_reg_read(tp, 0xad30);
8061 ocp_reg_write(tp, 0xad30, data);
8065 ocp_reg_write(tp, 0xb87c, 0x80f5);
8066 ocp_reg_write(tp, 0xb87e, 0x760e);
8067 ocp_reg_write(tp, 0xb87c, 0x8107);
8068 ocp_reg_write(tp, 0xb87e, 0x360e);
8069 ocp_reg_write(tp, 0xb87c, 0x8551);
8070 data = ocp_reg_read(tp, 0xb87e);
8073 ocp_reg_write(tp, 0xb87e, data);
8075 /* ADC_PGA parameter */
8076 data = ocp_reg_read(tp, 0xbf00);
8079 ocp_reg_write(tp, 0xbf00, data);
8080 data = ocp_reg_read(tp, 0xbf46);
8083 ocp_reg_write(tp, 0xbf46, data);
8085 /* Green Table-PGA, 1G full viterbi */
8086 sram_write(tp, 0x8044, 0x2417);
8087 sram_write(tp, 0x804a, 0x2417);
8088 sram_write(tp, 0x8050, 0x2417);
8089 sram_write(tp, 0x8056, 0x2417);
8090 sram_write(tp, 0x805c, 0x2417);
8091 sram_write(tp, 0x8062, 0x2417);
8092 sram_write(tp, 0x8068, 0x2417);
8093 sram_write(tp, 0x806e, 0x2417);
8094 sram_write(tp, 0x8074, 0x2417);
8095 sram_write(tp, 0x807a, 0x2417);
8098 data = ocp_reg_read(tp, 0xbf84);
8101 ocp_reg_write(tp, 0xbf84, data);
8107 /* Notify the MAC when the speed is changed to force mode. */
8108 data = ocp_reg_read(tp, OCP_INTR_EN);
8109 data |= INTR_SPEED_FORCE;
8110 ocp_reg_write(tp, OCP_INTR_EN, data);
8112 if (rtl_phy_patch_request(tp, true, true))
8115 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
8116 ocp_data |= EEE_SPDWN_EN;
8117 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
8119 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
8120 data &= ~(EN_EEE_100 | EN_EEE_1000);
8121 data |= EN_10M_CLKDIV;
8122 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
8123 tp->ups_info._10m_ckdiv = true;
8124 tp->ups_info.eee_plloff_100 = false;
8125 tp->ups_info.eee_plloff_giga = false;
8127 data = ocp_reg_read(tp, OCP_POWER_CFG);
8128 data &= ~EEE_CLKDIV_EN;
8129 ocp_reg_write(tp, OCP_POWER_CFG, data);
8130 tp->ups_info.eee_ckdiv = false;
8132 rtl_phy_patch_request(tp, false, true);
8134 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
8136 data = ocp_reg_read(tp, 0xa428);
8138 ocp_reg_write(tp, 0xa428, data);
8139 data = ocp_reg_read(tp, 0xa5ea);
8141 ocp_reg_write(tp, 0xa5ea, data);
8142 tp->ups_info.lite_mode = 0;
8145 rtl_eee_enable(tp, true);
8147 r8153_aldps_en(tp, true);
8148 r8152b_enable_fc(tp);
8149 r8153_u2p3en(tp, true);
8151 set_bit(PHY_RESET, &tp->flags);
8154 static void r8156_init(struct r8152 *tp)
8160 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
8163 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
8164 ocp_data &= ~EN_ALL_SPEED;
8165 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
8167 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
8169 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
8170 ocp_data |= BYPASS_MAC_RESET;
8171 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
8173 r8153b_u1u2en(tp, false);
8175 for (i = 0; i < 500; i++) {
8176 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
8181 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
8185 data = r8153_phy_status(tp, 0);
8186 if (data == PHY_STAT_EXT_INIT) {
8187 data = ocp_reg_read(tp, 0xa468);
8188 data &= ~(BIT(3) | BIT(1));
8189 ocp_reg_write(tp, 0xa468, data);
8192 data = r8152_mdio_read(tp, MII_BMCR);
8193 if (data & BMCR_PDOWN) {
8194 data &= ~BMCR_PDOWN;
8195 r8152_mdio_write(tp, MII_BMCR, data);
8198 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8199 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
8201 r8153_u2p3en(tp, false);
8203 /* MSC timer = 0xfff * 8ms = 32760 ms */
8204 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8206 /* U1/U2/L1 idle timer. 500 us */
8207 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8209 r8153b_power_cut_en(tp, false);
8210 r8156_ups_en(tp, false);
8211 r8153_queue_wake(tp, false);
8212 rtl_runtime_suspend_enable(tp, false);
8214 if (tp->udev->speed >= USB_SPEED_SUPER)
8215 r8153b_u1u2en(tp, true);
8217 usb_enable_lpm(tp->udev);
8219 r8156_mac_clk_spd(tp, true);
8221 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8222 ocp_data &= ~PLA_MCU_SPDWN_EN;
8223 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8225 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8226 if (rtl8152_get_speed(tp) & LINK_STATUS)
8227 ocp_data |= CUR_LINK_OK;
8229 ocp_data &= ~CUR_LINK_OK;
8230 ocp_data |= POLL_LINK_CHG;
8231 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8233 set_bit(GREEN_ETHERNET, &tp->flags);
8235 /* rx aggregation */
8236 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8237 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8238 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8240 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
8241 ocp_data |= ACT_ODMA;
8242 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
8244 r8156_mdio_force_mode(tp);
8245 rtl_tally_reset(tp);
8247 tp->coalesce = 15000; /* 15 us */
8250 static void r8156b_init(struct r8152 *tp)
8256 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
8259 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
8260 ocp_data &= ~EN_ALL_SPEED;
8261 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
8263 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
8265 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
8266 ocp_data |= BYPASS_MAC_RESET;
8267 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
8269 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
8270 ocp_data |= RX_DETECT8;
8271 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
8273 r8153b_u1u2en(tp, false);
8275 switch (tp->version) {
8278 r8156b_wait_loading_flash(tp);
8284 for (i = 0; i < 500; i++) {
8285 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
8290 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
8294 data = r8153_phy_status(tp, 0);
8295 if (data == PHY_STAT_EXT_INIT) {
8296 data = ocp_reg_read(tp, 0xa468);
8297 data &= ~(BIT(3) | BIT(1));
8298 ocp_reg_write(tp, 0xa468, data);
8300 data = ocp_reg_read(tp, 0xa466);
8302 ocp_reg_write(tp, 0xa466, data);
8305 data = r8152_mdio_read(tp, MII_BMCR);
8306 if (data & BMCR_PDOWN) {
8307 data &= ~BMCR_PDOWN;
8308 r8152_mdio_write(tp, MII_BMCR, data);
8311 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8313 r8153_u2p3en(tp, false);
8315 /* MSC timer = 0xfff * 8ms = 32760 ms */
8316 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8318 /* U1/U2/L1 idle timer. 500 us */
8319 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8321 r8153b_power_cut_en(tp, false);
8322 r8156_ups_en(tp, false);
8323 r8153_queue_wake(tp, false);
8324 rtl_runtime_suspend_enable(tp, false);
8326 if (tp->udev->speed >= USB_SPEED_SUPER)
8327 r8153b_u1u2en(tp, true);
8329 usb_enable_lpm(tp->udev);
8331 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR);
8332 ocp_data &= ~SLOT_EN;
8333 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8335 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
8336 ocp_data |= FLOW_CTRL_EN;
8337 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
8339 /* enable fc timer and set timer to 600 ms. */
8340 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
8341 CTRL_TIMER_EN | (600 / 8));
8343 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
8344 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN))
8345 ocp_data |= FLOW_CTRL_PATCH_2;
8346 ocp_data &= ~AUTO_SPEEDUP;
8347 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
8349 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
8350 ocp_data |= FC_PATCH_TASK;
8351 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
8353 r8156_mac_clk_spd(tp, true);
8355 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8356 ocp_data &= ~PLA_MCU_SPDWN_EN;
8357 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8359 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8360 if (rtl8152_get_speed(tp) & LINK_STATUS)
8361 ocp_data |= CUR_LINK_OK;
8363 ocp_data &= ~CUR_LINK_OK;
8364 ocp_data |= POLL_LINK_CHG;
8365 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8367 set_bit(GREEN_ETHERNET, &tp->flags);
8369 /* rx aggregation */
8370 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8371 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8372 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8374 r8156_mdio_force_mode(tp);
8375 rtl_tally_reset(tp);
8377 tp->coalesce = 15000; /* 15 us */
8380 static bool rtl_check_vendor_ok(struct usb_interface *intf)
8382 struct usb_host_interface *alt = intf->cur_altsetting;
8383 struct usb_endpoint_descriptor *in, *out, *intr;
8385 if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) {
8386 dev_err(&intf->dev, "Expected endpoints are not found\n");
8390 /* Check Rx endpoint address */
8391 if (usb_endpoint_num(in) != 1) {
8392 dev_err(&intf->dev, "Invalid Rx endpoint address\n");
8396 /* Check Tx endpoint address */
8397 if (usb_endpoint_num(out) != 2) {
8398 dev_err(&intf->dev, "Invalid Tx endpoint address\n");
8402 /* Check interrupt endpoint address */
8403 if (usb_endpoint_num(intr) != 3) {
8404 dev_err(&intf->dev, "Invalid interrupt endpoint address\n");
8411 static int rtl8152_pre_reset(struct usb_interface *intf)
8413 struct r8152 *tp = usb_get_intfdata(intf);
8414 struct net_device *netdev;
8418 if (!tp || !test_bit(PROBED_WITH_NO_ERRORS, &tp->flags))
8421 netdev = tp->netdev;
8422 if (!netif_running(netdev))
8425 netif_stop_queue(netdev);
8426 tasklet_disable(&tp->tx_tl);
8427 clear_bit(WORK_ENABLE, &tp->flags);
8428 usb_kill_urb(tp->intr_urb);
8429 cancel_delayed_work_sync(&tp->schedule);
8430 napi_disable(&tp->napi);
8431 if (netif_carrier_ok(netdev)) {
8432 mutex_lock(&tp->control);
8433 set_bit(IN_PRE_RESET, &tp->flags);
8434 tp->rtl_ops.disable(tp);
8435 clear_bit(IN_PRE_RESET, &tp->flags);
8436 mutex_unlock(&tp->control);
8442 static int rtl8152_post_reset(struct usb_interface *intf)
8444 struct r8152 *tp = usb_get_intfdata(intf);
8445 struct net_device *netdev;
8448 if (!tp || !test_bit(PROBED_WITH_NO_ERRORS, &tp->flags))
8451 rtl_set_accessible(tp);
8453 /* reset the MAC address in case of policy change */
8454 if (determine_ethernet_addr(tp, &sa) >= 0)
8455 dev_set_mac_address (tp->netdev, &sa, NULL);
8457 netdev = tp->netdev;
8458 if (!netif_running(netdev))
8461 set_bit(WORK_ENABLE, &tp->flags);
8462 if (netif_carrier_ok(netdev)) {
8463 mutex_lock(&tp->control);
8464 tp->rtl_ops.enable(tp);
8466 _rtl8152_set_rx_mode(netdev);
8467 mutex_unlock(&tp->control);
8470 napi_enable(&tp->napi);
8471 tasklet_enable(&tp->tx_tl);
8472 netif_wake_queue(netdev);
8473 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
8475 if (!list_empty(&tp->rx_done))
8476 napi_schedule(&tp->napi);
8483 static bool delay_autosuspend(struct r8152 *tp)
8485 bool sw_linking = !!netif_carrier_ok(tp->netdev);
8486 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
8488 /* This means a linking change occurs and the driver doesn't detect it,
8489 * yet. If the driver has disabled tx/rx and hw is linking on, the
8490 * device wouldn't wake up by receiving any packet.
8492 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
8495 /* If the linking down is occurred by nway, the device may miss the
8496 * linking change event. And it wouldn't wake when linking on.
8498 if (!sw_linking && tp->rtl_ops.in_nway(tp))
8500 else if (!skb_queue_empty(&tp->tx_queue))
8506 static int rtl8152_runtime_resume(struct r8152 *tp)
8508 struct net_device *netdev = tp->netdev;
8510 if (netif_running(netdev) && netdev->flags & IFF_UP) {
8511 struct napi_struct *napi = &tp->napi;
8513 tp->rtl_ops.autosuspend_en(tp, false);
8515 set_bit(WORK_ENABLE, &tp->flags);
8517 if (netif_carrier_ok(netdev)) {
8518 if (rtl8152_get_speed(tp) & LINK_STATUS) {
8521 netif_carrier_off(netdev);
8522 tp->rtl_ops.disable(tp);
8523 netif_info(tp, link, netdev, "linking down\n");
8528 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8529 smp_mb__after_atomic();
8531 if (!list_empty(&tp->rx_done))
8532 napi_schedule(&tp->napi);
8534 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8536 if (netdev->flags & IFF_UP)
8537 tp->rtl_ops.autosuspend_en(tp, false);
8539 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8545 static int rtl8152_system_resume(struct r8152 *tp)
8547 struct net_device *netdev = tp->netdev;
8549 netif_device_attach(netdev);
8551 if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
8553 netif_carrier_off(netdev);
8554 set_bit(WORK_ENABLE, &tp->flags);
8555 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8558 /* If the device is RTL8152_INACCESSIBLE here then we should do a
8559 * reset. This is important because the usb_lock_device_for_reset()
8560 * that happens as a result of usb_queue_reset_device() will silently
8561 * fail if the device was suspended or if too much time passed.
8563 * NOTE: The device is locked here so we can directly do the reset.
8564 * We don't need usb_lock_device_for_reset() because that's just a
8565 * wrapper over device_lock() and device_resume() (which calls us)
8568 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
8569 usb_reset_device(tp->udev);
8574 static int rtl8152_runtime_suspend(struct r8152 *tp)
8576 struct net_device *netdev = tp->netdev;
8579 if (!tp->rtl_ops.autosuspend_en)
8582 set_bit(SELECTIVE_SUSPEND, &tp->flags);
8583 smp_mb__after_atomic();
8585 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8588 if (netif_carrier_ok(netdev)) {
8591 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
8592 ocp_data = rcr & ~RCR_ACPT_ALL;
8593 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8594 rxdy_gated_en(tp, true);
8595 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
8597 if (!(ocp_data & RXFIFO_EMPTY)) {
8598 rxdy_gated_en(tp, false);
8599 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8600 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8601 smp_mb__after_atomic();
8607 clear_bit(WORK_ENABLE, &tp->flags);
8608 usb_kill_urb(tp->intr_urb);
8610 tp->rtl_ops.autosuspend_en(tp, true);
8612 if (netif_carrier_ok(netdev)) {
8613 struct napi_struct *napi = &tp->napi;
8617 rxdy_gated_en(tp, false);
8618 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8622 if (delay_autosuspend(tp)) {
8623 rtl8152_runtime_resume(tp);
8632 static int rtl8152_system_suspend(struct r8152 *tp)
8634 struct net_device *netdev = tp->netdev;
8636 netif_device_detach(netdev);
8638 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8639 struct napi_struct *napi = &tp->napi;
8641 clear_bit(WORK_ENABLE, &tp->flags);
8642 usb_kill_urb(tp->intr_urb);
8643 tasklet_disable(&tp->tx_tl);
8645 cancel_delayed_work_sync(&tp->schedule);
8646 tp->rtl_ops.down(tp);
8648 tasklet_enable(&tp->tx_tl);
8651 /* If we're inaccessible here then some of the work that we did to
8652 * get the adapter ready for suspend didn't work. Queue up a wakeup
8653 * event so we can try again.
8655 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
8656 pm_wakeup_event(&tp->udev->dev, 0);
8661 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
8663 struct r8152 *tp = usb_get_intfdata(intf);
8666 mutex_lock(&tp->control);
8668 if (PMSG_IS_AUTO(message))
8669 ret = rtl8152_runtime_suspend(tp);
8671 ret = rtl8152_system_suspend(tp);
8673 mutex_unlock(&tp->control);
8678 static int rtl8152_resume(struct usb_interface *intf)
8680 struct r8152 *tp = usb_get_intfdata(intf);
8683 mutex_lock(&tp->control);
8685 rtl_reset_ocp_base(tp);
8687 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
8688 ret = rtl8152_runtime_resume(tp);
8690 ret = rtl8152_system_resume(tp);
8692 mutex_unlock(&tp->control);
8697 static int rtl8152_reset_resume(struct usb_interface *intf)
8699 struct r8152 *tp = usb_get_intfdata(intf);
8701 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8702 rtl_reset_ocp_base(tp);
8703 tp->rtl_ops.init(tp);
8704 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
8705 set_ethernet_addr(tp, true);
8706 return rtl8152_resume(intf);
8709 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8711 struct r8152 *tp = netdev_priv(dev);
8713 if (usb_autopm_get_interface(tp->intf) < 0)
8716 if (!rtl_can_wakeup(tp)) {
8720 mutex_lock(&tp->control);
8721 wol->supported = WAKE_ANY;
8722 wol->wolopts = __rtl_get_wol(tp);
8723 mutex_unlock(&tp->control);
8726 usb_autopm_put_interface(tp->intf);
8729 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8731 struct r8152 *tp = netdev_priv(dev);
8734 if (!rtl_can_wakeup(tp))
8737 if (wol->wolopts & ~WAKE_ANY)
8740 ret = usb_autopm_get_interface(tp->intf);
8744 mutex_lock(&tp->control);
8746 __rtl_set_wol(tp, wol->wolopts);
8747 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
8749 mutex_unlock(&tp->control);
8751 usb_autopm_put_interface(tp->intf);
8757 static u32 rtl8152_get_msglevel(struct net_device *dev)
8759 struct r8152 *tp = netdev_priv(dev);
8761 return tp->msg_enable;
8764 static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
8766 struct r8152 *tp = netdev_priv(dev);
8768 tp->msg_enable = value;
8771 static void rtl8152_get_drvinfo(struct net_device *netdev,
8772 struct ethtool_drvinfo *info)
8774 struct r8152 *tp = netdev_priv(netdev);
8776 strscpy(info->driver, MODULENAME, sizeof(info->driver));
8777 strscpy(info->version, DRIVER_VERSION, sizeof(info->version));
8778 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
8779 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
8780 strscpy(info->fw_version, tp->rtl_fw.version,
8781 sizeof(info->fw_version));
8785 int rtl8152_get_link_ksettings(struct net_device *netdev,
8786 struct ethtool_link_ksettings *cmd)
8788 struct r8152 *tp = netdev_priv(netdev);
8791 if (!tp->mii.mdio_read)
8794 ret = usb_autopm_get_interface(tp->intf);
8798 mutex_lock(&tp->control);
8800 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
8802 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8803 cmd->link_modes.supported, tp->support_2500full);
8805 if (tp->support_2500full) {
8806 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8807 cmd->link_modes.advertising,
8808 ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G);
8810 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8811 cmd->link_modes.lp_advertising,
8812 ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G);
8814 if (is_speed_2500(rtl8152_get_speed(tp)))
8815 cmd->base.speed = SPEED_2500;
8818 mutex_unlock(&tp->control);
8820 usb_autopm_put_interface(tp->intf);
8826 static int rtl8152_set_link_ksettings(struct net_device *dev,
8827 const struct ethtool_link_ksettings *cmd)
8829 struct r8152 *tp = netdev_priv(dev);
8830 u32 advertising = 0;
8833 ret = usb_autopm_get_interface(tp->intf);
8837 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
8838 cmd->link_modes.advertising))
8839 advertising |= RTL_ADVERTISED_10_HALF;
8841 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
8842 cmd->link_modes.advertising))
8843 advertising |= RTL_ADVERTISED_10_FULL;
8845 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
8846 cmd->link_modes.advertising))
8847 advertising |= RTL_ADVERTISED_100_HALF;
8849 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
8850 cmd->link_modes.advertising))
8851 advertising |= RTL_ADVERTISED_100_FULL;
8853 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
8854 cmd->link_modes.advertising))
8855 advertising |= RTL_ADVERTISED_1000_HALF;
8857 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
8858 cmd->link_modes.advertising))
8859 advertising |= RTL_ADVERTISED_1000_FULL;
8861 if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8862 cmd->link_modes.advertising))
8863 advertising |= RTL_ADVERTISED_2500_FULL;
8865 mutex_lock(&tp->control);
8867 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
8868 cmd->base.duplex, advertising);
8870 tp->autoneg = cmd->base.autoneg;
8871 tp->speed = cmd->base.speed;
8872 tp->duplex = cmd->base.duplex;
8873 tp->advertising = advertising;
8876 mutex_unlock(&tp->control);
8878 usb_autopm_put_interface(tp->intf);
8884 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
8891 "tx_single_collisions",
8892 "tx_multi_collisions",
8900 static int rtl8152_get_sset_count(struct net_device *dev, int sset)
8904 return ARRAY_SIZE(rtl8152_gstrings);
8910 static void rtl8152_get_ethtool_stats(struct net_device *dev,
8911 struct ethtool_stats *stats, u64 *data)
8913 struct r8152 *tp = netdev_priv(dev);
8914 struct tally_counter tally;
8916 if (usb_autopm_get_interface(tp->intf) < 0)
8919 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
8921 usb_autopm_put_interface(tp->intf);
8923 data[0] = le64_to_cpu(tally.tx_packets);
8924 data[1] = le64_to_cpu(tally.rx_packets);
8925 data[2] = le64_to_cpu(tally.tx_errors);
8926 data[3] = le32_to_cpu(tally.rx_errors);
8927 data[4] = le16_to_cpu(tally.rx_missed);
8928 data[5] = le16_to_cpu(tally.align_errors);
8929 data[6] = le32_to_cpu(tally.tx_one_collision);
8930 data[7] = le32_to_cpu(tally.tx_multi_collision);
8931 data[8] = le64_to_cpu(tally.rx_unicast);
8932 data[9] = le64_to_cpu(tally.rx_broadcast);
8933 data[10] = le32_to_cpu(tally.rx_multicast);
8934 data[11] = le16_to_cpu(tally.tx_aborted);
8935 data[12] = le16_to_cpu(tally.tx_underrun);
8938 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
8940 switch (stringset) {
8942 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings));
8947 static int r8152_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
8949 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
8952 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
8953 mii_eee_cap1_mod_linkmode_t(eee->supported, val);
8955 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
8956 mii_eee_cap1_mod_linkmode_t(eee->advertised, val);
8958 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
8959 mii_eee_cap1_mod_linkmode_t(eee->lp_advertised, val);
8961 eee->eee_enabled = tp->eee_en;
8963 linkmode_and(common, eee->advertised, eee->lp_advertised);
8964 eee->eee_active = phy_check_valid(tp->speed, tp->duplex, common);
8969 static int r8152_set_eee(struct r8152 *tp, struct ethtool_keee *eee)
8971 u16 val = linkmode_to_mii_eee_cap1_t(eee->advertised);
8973 tp->eee_en = eee->eee_enabled;
8976 rtl_eee_enable(tp, tp->eee_en);
8981 static int r8153_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
8983 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
8986 val = ocp_reg_read(tp, OCP_EEE_ABLE);
8987 mii_eee_cap1_mod_linkmode_t(eee->supported, val);
8989 val = ocp_reg_read(tp, OCP_EEE_ADV);
8990 mii_eee_cap1_mod_linkmode_t(eee->advertised, val);
8992 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
8993 mii_eee_cap1_mod_linkmode_t(eee->lp_advertised, val);
8995 eee->eee_enabled = tp->eee_en;
8997 linkmode_and(common, eee->advertised, eee->lp_advertised);
8998 eee->eee_active = phy_check_valid(tp->speed, tp->duplex, common);
9004 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_keee *edata)
9006 struct r8152 *tp = netdev_priv(net);
9009 if (!tp->rtl_ops.eee_get) {
9014 ret = usb_autopm_get_interface(tp->intf);
9018 mutex_lock(&tp->control);
9020 ret = tp->rtl_ops.eee_get(tp, edata);
9022 mutex_unlock(&tp->control);
9024 usb_autopm_put_interface(tp->intf);
9031 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_keee *edata)
9033 struct r8152 *tp = netdev_priv(net);
9036 if (!tp->rtl_ops.eee_set) {
9041 ret = usb_autopm_get_interface(tp->intf);
9045 mutex_lock(&tp->control);
9047 ret = tp->rtl_ops.eee_set(tp, edata);
9049 ret = mii_nway_restart(&tp->mii);
9051 mutex_unlock(&tp->control);
9053 usb_autopm_put_interface(tp->intf);
9059 static int rtl8152_nway_reset(struct net_device *dev)
9061 struct r8152 *tp = netdev_priv(dev);
9064 ret = usb_autopm_get_interface(tp->intf);
9068 mutex_lock(&tp->control);
9070 ret = mii_nway_restart(&tp->mii);
9072 mutex_unlock(&tp->control);
9074 usb_autopm_put_interface(tp->intf);
9080 static int rtl8152_get_coalesce(struct net_device *netdev,
9081 struct ethtool_coalesce *coalesce,
9082 struct kernel_ethtool_coalesce *kernel_coal,
9083 struct netlink_ext_ack *extack)
9085 struct r8152 *tp = netdev_priv(netdev);
9087 switch (tp->version) {
9096 coalesce->rx_coalesce_usecs = tp->coalesce;
9101 static int rtl8152_set_coalesce(struct net_device *netdev,
9102 struct ethtool_coalesce *coalesce,
9103 struct kernel_ethtool_coalesce *kernel_coal,
9104 struct netlink_ext_ack *extack)
9106 struct r8152 *tp = netdev_priv(netdev);
9109 switch (tp->version) {
9118 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
9121 ret = usb_autopm_get_interface(tp->intf);
9125 mutex_lock(&tp->control);
9127 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
9128 tp->coalesce = coalesce->rx_coalesce_usecs;
9130 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
9131 netif_stop_queue(netdev);
9132 napi_disable(&tp->napi);
9133 tp->rtl_ops.disable(tp);
9134 tp->rtl_ops.enable(tp);
9136 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
9137 _rtl8152_set_rx_mode(netdev);
9138 napi_enable(&tp->napi);
9139 netif_wake_queue(netdev);
9143 mutex_unlock(&tp->control);
9145 usb_autopm_put_interface(tp->intf);
9150 static int rtl8152_get_tunable(struct net_device *netdev,
9151 const struct ethtool_tunable *tunable, void *d)
9153 struct r8152 *tp = netdev_priv(netdev);
9155 switch (tunable->id) {
9156 case ETHTOOL_RX_COPYBREAK:
9157 *(u32 *)d = tp->rx_copybreak;
9166 static int rtl8152_set_tunable(struct net_device *netdev,
9167 const struct ethtool_tunable *tunable,
9170 struct r8152 *tp = netdev_priv(netdev);
9173 switch (tunable->id) {
9174 case ETHTOOL_RX_COPYBREAK:
9176 if (val < ETH_ZLEN) {
9177 netif_err(tp, rx_err, netdev,
9178 "Invalid rx copy break value\n");
9182 if (tp->rx_copybreak != val) {
9183 if (netdev->flags & IFF_UP) {
9184 mutex_lock(&tp->control);
9185 napi_disable(&tp->napi);
9186 tp->rx_copybreak = val;
9187 napi_enable(&tp->napi);
9188 mutex_unlock(&tp->control);
9190 tp->rx_copybreak = val;
9201 static void rtl8152_get_ringparam(struct net_device *netdev,
9202 struct ethtool_ringparam *ring,
9203 struct kernel_ethtool_ringparam *kernel_ring,
9204 struct netlink_ext_ack *extack)
9206 struct r8152 *tp = netdev_priv(netdev);
9208 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
9209 ring->rx_pending = tp->rx_pending;
9212 static int rtl8152_set_ringparam(struct net_device *netdev,
9213 struct ethtool_ringparam *ring,
9214 struct kernel_ethtool_ringparam *kernel_ring,
9215 struct netlink_ext_ack *extack)
9217 struct r8152 *tp = netdev_priv(netdev);
9219 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
9222 if (tp->rx_pending != ring->rx_pending) {
9223 if (netdev->flags & IFF_UP) {
9224 mutex_lock(&tp->control);
9225 napi_disable(&tp->napi);
9226 tp->rx_pending = ring->rx_pending;
9227 napi_enable(&tp->napi);
9228 mutex_unlock(&tp->control);
9230 tp->rx_pending = ring->rx_pending;
9237 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9239 struct r8152 *tp = netdev_priv(netdev);
9240 u16 bmcr, lcladv, rmtadv;
9243 if (usb_autopm_get_interface(tp->intf) < 0)
9246 mutex_lock(&tp->control);
9248 bmcr = r8152_mdio_read(tp, MII_BMCR);
9249 lcladv = r8152_mdio_read(tp, MII_ADVERTISE);
9250 rmtadv = r8152_mdio_read(tp, MII_LPA);
9252 mutex_unlock(&tp->control);
9254 usb_autopm_put_interface(tp->intf);
9256 if (!(bmcr & BMCR_ANENABLE)) {
9258 pause->rx_pause = 0;
9259 pause->tx_pause = 0;
9265 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
9267 if (cap & FLOW_CTRL_RX)
9268 pause->rx_pause = 1;
9270 if (cap & FLOW_CTRL_TX)
9271 pause->tx_pause = 1;
9274 static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9276 struct r8152 *tp = netdev_priv(netdev);
9281 ret = usb_autopm_get_interface(tp->intf);
9285 mutex_lock(&tp->control);
9287 if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) {
9292 if (pause->rx_pause)
9293 cap |= FLOW_CTRL_RX;
9295 if (pause->tx_pause)
9296 cap |= FLOW_CTRL_TX;
9298 old = r8152_mdio_read(tp, MII_ADVERTISE);
9299 new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap);
9301 r8152_mdio_write(tp, MII_ADVERTISE, new1);
9304 mutex_unlock(&tp->control);
9305 usb_autopm_put_interface(tp->intf);
9310 static const struct ethtool_ops ops = {
9311 .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
9312 .get_drvinfo = rtl8152_get_drvinfo,
9313 .get_link = ethtool_op_get_link,
9314 .nway_reset = rtl8152_nway_reset,
9315 .get_msglevel = rtl8152_get_msglevel,
9316 .set_msglevel = rtl8152_set_msglevel,
9317 .get_wol = rtl8152_get_wol,
9318 .set_wol = rtl8152_set_wol,
9319 .get_strings = rtl8152_get_strings,
9320 .get_sset_count = rtl8152_get_sset_count,
9321 .get_ethtool_stats = rtl8152_get_ethtool_stats,
9322 .get_coalesce = rtl8152_get_coalesce,
9323 .set_coalesce = rtl8152_set_coalesce,
9324 .get_eee = rtl_ethtool_get_eee,
9325 .set_eee = rtl_ethtool_set_eee,
9326 .get_link_ksettings = rtl8152_get_link_ksettings,
9327 .set_link_ksettings = rtl8152_set_link_ksettings,
9328 .get_tunable = rtl8152_get_tunable,
9329 .set_tunable = rtl8152_set_tunable,
9330 .get_ringparam = rtl8152_get_ringparam,
9331 .set_ringparam = rtl8152_set_ringparam,
9332 .get_pauseparam = rtl8152_get_pauseparam,
9333 .set_pauseparam = rtl8152_set_pauseparam,
9336 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
9338 struct r8152 *tp = netdev_priv(netdev);
9339 struct mii_ioctl_data *data = if_mii(rq);
9342 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
9345 res = usb_autopm_get_interface(tp->intf);
9351 data->phy_id = R8152_PHY_ID; /* Internal PHY */
9355 mutex_lock(&tp->control);
9356 data->val_out = r8152_mdio_read(tp, data->reg_num);
9357 mutex_unlock(&tp->control);
9361 if (!capable(CAP_NET_ADMIN)) {
9365 mutex_lock(&tp->control);
9366 r8152_mdio_write(tp, data->reg_num, data->val_in);
9367 mutex_unlock(&tp->control);
9374 usb_autopm_put_interface(tp->intf);
9380 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
9382 struct r8152 *tp = netdev_priv(dev);
9385 switch (tp->version) {
9389 WRITE_ONCE(dev->mtu, new_mtu);
9395 ret = usb_autopm_get_interface(tp->intf);
9399 mutex_lock(&tp->control);
9401 WRITE_ONCE(dev->mtu, new_mtu);
9403 if (netif_running(dev)) {
9404 if (tp->rtl_ops.change_mtu)
9405 tp->rtl_ops.change_mtu(tp);
9407 if (netif_carrier_ok(dev)) {
9408 netif_stop_queue(dev);
9409 napi_disable(&tp->napi);
9410 tasklet_disable(&tp->tx_tl);
9411 tp->rtl_ops.disable(tp);
9412 tp->rtl_ops.enable(tp);
9414 tasklet_enable(&tp->tx_tl);
9415 napi_enable(&tp->napi);
9416 rtl8152_set_rx_mode(dev);
9417 netif_wake_queue(dev);
9421 mutex_unlock(&tp->control);
9423 usb_autopm_put_interface(tp->intf);
9428 static const struct net_device_ops rtl8152_netdev_ops = {
9429 .ndo_open = rtl8152_open,
9430 .ndo_stop = rtl8152_close,
9431 .ndo_eth_ioctl = rtl8152_ioctl,
9432 .ndo_start_xmit = rtl8152_start_xmit,
9433 .ndo_tx_timeout = rtl8152_tx_timeout,
9434 .ndo_set_features = rtl8152_set_features,
9435 .ndo_set_rx_mode = rtl8152_set_rx_mode,
9436 .ndo_set_mac_address = rtl8152_set_mac_address,
9437 .ndo_change_mtu = rtl8152_change_mtu,
9438 .ndo_validate_addr = eth_validate_addr,
9439 .ndo_features_check = rtl8152_features_check,
9442 static void rtl8152_unload(struct r8152 *tp)
9444 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
9447 if (tp->version != RTL_VER_01)
9448 r8152_power_cut_en(tp, true);
9451 static void rtl8153_unload(struct r8152 *tp)
9453 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
9456 r8153_power_cut_en(tp, false);
9459 static void rtl8153b_unload(struct r8152 *tp)
9461 if (test_bit(RTL8152_INACCESSIBLE, &tp->flags))
9464 r8153b_power_cut_en(tp, false);
9467 static int rtl_ops_init(struct r8152 *tp)
9469 struct rtl_ops *ops = &tp->rtl_ops;
9472 switch (tp->version) {
9476 ops->init = r8152b_init;
9477 ops->enable = rtl8152_enable;
9478 ops->disable = rtl8152_disable;
9479 ops->up = rtl8152_up;
9480 ops->down = rtl8152_down;
9481 ops->unload = rtl8152_unload;
9482 ops->eee_get = r8152_get_eee;
9483 ops->eee_set = r8152_set_eee;
9484 ops->in_nway = rtl8152_in_nway;
9485 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
9486 ops->autosuspend_en = rtl_runtime_suspend_enable;
9487 tp->rx_buf_sz = 16 * 1024;
9489 tp->eee_adv = MDIO_EEE_100TX;
9496 ops->init = r8153_init;
9497 ops->enable = rtl8153_enable;
9498 ops->disable = rtl8153_disable;
9499 ops->up = rtl8153_up;
9500 ops->down = rtl8153_down;
9501 ops->unload = rtl8153_unload;
9502 ops->eee_get = r8153_get_eee;
9503 ops->eee_set = r8152_set_eee;
9504 ops->in_nway = rtl8153_in_nway;
9505 ops->hw_phy_cfg = r8153_hw_phy_cfg;
9506 ops->autosuspend_en = rtl8153_runtime_enable;
9507 ops->change_mtu = rtl8153_change_mtu;
9508 if (tp->udev->speed < USB_SPEED_SUPER)
9509 tp->rx_buf_sz = 16 * 1024;
9511 tp->rx_buf_sz = 32 * 1024;
9513 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9518 ops->init = r8153b_init;
9519 ops->enable = rtl8153_enable;
9520 ops->disable = rtl8153_disable;
9521 ops->up = rtl8153b_up;
9522 ops->down = rtl8153b_down;
9523 ops->unload = rtl8153b_unload;
9524 ops->eee_get = r8153_get_eee;
9525 ops->eee_set = r8152_set_eee;
9526 ops->in_nway = rtl8153_in_nway;
9527 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
9528 ops->autosuspend_en = rtl8153b_runtime_enable;
9529 ops->change_mtu = rtl8153_change_mtu;
9530 tp->rx_buf_sz = 32 * 1024;
9532 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9537 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9540 ops->init = r8156_init;
9541 ops->enable = rtl8156_enable;
9542 ops->disable = rtl8156_disable;
9543 ops->up = rtl8156_up;
9544 ops->down = rtl8156_down;
9545 ops->unload = rtl8153_unload;
9546 ops->eee_get = r8153_get_eee;
9547 ops->eee_set = r8152_set_eee;
9548 ops->in_nway = rtl8153_in_nway;
9549 ops->hw_phy_cfg = r8156_hw_phy_cfg;
9550 ops->autosuspend_en = rtl8156_runtime_enable;
9551 ops->change_mtu = rtl8156_change_mtu;
9552 tp->rx_buf_sz = 48 * 1024;
9553 tp->support_2500full = 1;
9558 tp->support_2500full = 1;
9562 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9563 ops->init = r8156b_init;
9564 ops->enable = rtl8156b_enable;
9565 ops->disable = rtl8153_disable;
9566 ops->up = rtl8156_up;
9567 ops->down = rtl8156_down;
9568 ops->unload = rtl8153_unload;
9569 ops->eee_get = r8153_get_eee;
9570 ops->eee_set = r8152_set_eee;
9571 ops->in_nway = rtl8153_in_nway;
9572 ops->hw_phy_cfg = r8156b_hw_phy_cfg;
9573 ops->autosuspend_en = rtl8156_runtime_enable;
9574 ops->change_mtu = rtl8156_change_mtu;
9575 tp->rx_buf_sz = 48 * 1024;
9579 ops->init = r8153c_init;
9580 ops->enable = rtl8153_enable;
9581 ops->disable = rtl8153_disable;
9582 ops->up = rtl8153c_up;
9583 ops->down = rtl8153b_down;
9584 ops->unload = rtl8153_unload;
9585 ops->eee_get = r8153_get_eee;
9586 ops->eee_set = r8152_set_eee;
9587 ops->in_nway = rtl8153_in_nway;
9588 ops->hw_phy_cfg = r8153c_hw_phy_cfg;
9589 ops->autosuspend_en = rtl8153c_runtime_enable;
9590 ops->change_mtu = rtl8153c_change_mtu;
9591 tp->rx_buf_sz = 32 * 1024;
9593 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9598 dev_err(&tp->intf->dev, "Unknown Device\n");
9605 #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw"
9606 #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw"
9607 #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw"
9608 #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw"
9609 #define FIRMWARE_8153C_1 "rtl_nic/rtl8153c-1.fw"
9610 #define FIRMWARE_8156A_2 "rtl_nic/rtl8156a-2.fw"
9611 #define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw"
9613 MODULE_FIRMWARE(FIRMWARE_8153A_2);
9614 MODULE_FIRMWARE(FIRMWARE_8153A_3);
9615 MODULE_FIRMWARE(FIRMWARE_8153A_4);
9616 MODULE_FIRMWARE(FIRMWARE_8153B_2);
9617 MODULE_FIRMWARE(FIRMWARE_8153C_1);
9618 MODULE_FIRMWARE(FIRMWARE_8156A_2);
9619 MODULE_FIRMWARE(FIRMWARE_8156B_2);
9621 static int rtl_fw_init(struct r8152 *tp)
9623 struct rtl_fw *rtl_fw = &tp->rtl_fw;
9625 switch (tp->version) {
9627 rtl_fw->fw_name = FIRMWARE_8153A_2;
9628 rtl_fw->pre_fw = r8153_pre_firmware_1;
9629 rtl_fw->post_fw = r8153_post_firmware_1;
9632 rtl_fw->fw_name = FIRMWARE_8153A_3;
9633 rtl_fw->pre_fw = r8153_pre_firmware_2;
9634 rtl_fw->post_fw = r8153_post_firmware_2;
9637 rtl_fw->fw_name = FIRMWARE_8153A_4;
9638 rtl_fw->post_fw = r8153_post_firmware_3;
9641 rtl_fw->fw_name = FIRMWARE_8153B_2;
9642 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9643 rtl_fw->post_fw = r8153b_post_firmware_1;
9646 rtl_fw->fw_name = FIRMWARE_8156A_2;
9647 rtl_fw->post_fw = r8156a_post_firmware_1;
9651 rtl_fw->fw_name = FIRMWARE_8156B_2;
9654 rtl_fw->fw_name = FIRMWARE_8153C_1;
9655 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9656 rtl_fw->post_fw = r8153c_post_firmware_1;
9665 static u8 __rtl_get_hw_ver(struct usb_device *udev)
9673 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
9677 /* Retry up to 3 times in case there is a transitory error. We do this
9678 * since retrying a read of the version is always safe and this
9679 * function doesn't take advantage of r8152_control_msg().
9681 for (i = 0; i < 3; i++) {
9682 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
9683 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
9684 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp),
9685 USB_CTRL_GET_TIMEOUT);
9687 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
9692 if (i != 0 && ret > 0)
9693 dev_warn(&udev->dev, "Needed %d retries to read version\n", i);
9699 version = RTL_VER_01;
9702 version = RTL_VER_02;
9705 version = RTL_VER_03;
9708 version = RTL_VER_04;
9711 version = RTL_VER_05;
9714 version = RTL_VER_06;
9717 version = RTL_VER_07;
9720 version = RTL_VER_08;
9723 version = RTL_VER_09;
9726 version = RTL_TEST_01;
9729 version = RTL_VER_10;
9732 version = RTL_VER_11;
9735 version = RTL_VER_12;
9738 version = RTL_VER_13;
9741 version = RTL_VER_14;
9744 version = RTL_VER_15;
9747 version = RTL_VER_UNKNOWN;
9748 dev_info(&udev->dev, "Unknown version 0x%04x\n", ocp_data);
9755 u8 rtl8152_get_version(struct usb_interface *intf)
9759 version = __rtl_get_hw_ver(interface_to_usbdev(intf));
9761 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
9765 EXPORT_SYMBOL_GPL(rtl8152_get_version);
9767 static bool rtl8152_supports_lenovo_macpassthru(struct usb_device *udev)
9769 int parent_vendor_id = le16_to_cpu(udev->parent->descriptor.idVendor);
9770 int product_id = le16_to_cpu(udev->descriptor.idProduct);
9771 int vendor_id = le16_to_cpu(udev->descriptor.idVendor);
9773 if (vendor_id == VENDOR_ID_LENOVO) {
9774 switch (product_id) {
9775 case DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB:
9776 case DEVICE_ID_THINKPAD_ONELINK_PLUS_DOCK:
9777 case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2:
9778 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2:
9779 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN3:
9780 case DEVICE_ID_THINKPAD_USB_C_DONGLE:
9783 } else if (vendor_id == VENDOR_ID_REALTEK && parent_vendor_id == VENDOR_ID_LENOVO) {
9784 switch (product_id) {
9792 static int rtl8152_probe_once(struct usb_interface *intf,
9793 const struct usb_device_id *id, u8 version)
9795 struct usb_device *udev = interface_to_usbdev(intf);
9797 struct net_device *netdev;
9800 usb_reset_device(udev);
9801 netdev = alloc_etherdev(sizeof(struct r8152));
9803 dev_err(&intf->dev, "Out of memory\n");
9807 SET_NETDEV_DEV(netdev, &intf->dev);
9808 tp = netdev_priv(netdev);
9809 tp->msg_enable = 0x7FFF;
9812 tp->netdev = netdev;
9814 tp->version = version;
9816 tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0);
9817 tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0);
9818 tp->pipe_in = usb_rcvbulkpipe(udev, 1);
9819 tp->pipe_out = usb_sndbulkpipe(udev, 2);
9820 tp->pipe_intr = usb_rcvintpipe(udev, 3);
9826 tp->mii.supports_gmii = 0;
9829 tp->mii.supports_gmii = 1;
9833 ret = rtl_ops_init(tp);
9839 mutex_init(&tp->control);
9840 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
9841 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
9842 tasklet_setup(&tp->tx_tl, bottom_half);
9843 tasklet_disable(&tp->tx_tl);
9845 netdev->netdev_ops = &rtl8152_netdev_ops;
9846 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
9848 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9849 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
9850 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
9851 NETIF_F_HW_VLAN_CTAG_TX;
9852 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9853 NETIF_F_TSO | NETIF_F_FRAGLIST |
9854 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
9855 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
9856 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
9857 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
9858 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
9860 if (tp->version == RTL_VER_01) {
9861 netdev->features &= ~NETIF_F_RXCSUM;
9862 netdev->hw_features &= ~NETIF_F_RXCSUM;
9865 tp->lenovo_macpassthru = rtl8152_supports_lenovo_macpassthru(udev);
9867 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
9868 (!strcmp(udev->serial, "000001000000") ||
9869 !strcmp(udev->serial, "000002000000"))) {
9870 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
9871 tp->dell_tb_rx_agg_bug = 1;
9874 netdev->ethtool_ops = &ops;
9875 netif_set_tso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
9877 /* MTU range: 68 - 1500 or 9194 */
9878 netdev->min_mtu = ETH_MIN_MTU;
9879 switch (tp->version) {
9887 netdev->max_mtu = size_to_mtu(9 * 1024);
9891 netdev->max_mtu = size_to_mtu(15 * 1024);
9896 netdev->max_mtu = size_to_mtu(16 * 1024);
9902 netdev->max_mtu = ETH_DATA_LEN;
9906 tp->mii.dev = netdev;
9907 tp->mii.mdio_read = read_mii_word;
9908 tp->mii.mdio_write = write_mii_word;
9909 tp->mii.phy_id_mask = 0x3f;
9910 tp->mii.reg_num_mask = 0x1f;
9911 tp->mii.phy_id = R8152_PHY_ID;
9913 tp->autoneg = AUTONEG_ENABLE;
9914 tp->speed = SPEED_100;
9915 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
9916 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
9917 if (tp->mii.supports_gmii) {
9918 if (tp->support_2500full &&
9919 tp->udev->speed >= USB_SPEED_SUPER) {
9920 tp->speed = SPEED_2500;
9921 tp->advertising |= RTL_ADVERTISED_2500_FULL;
9923 tp->speed = SPEED_1000;
9925 tp->advertising |= RTL_ADVERTISED_1000_FULL;
9927 tp->duplex = DUPLEX_FULL;
9929 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
9930 tp->rx_pending = 10 * RTL8152_MAX_RX;
9932 intf->needs_remote_wakeup = 1;
9934 if (!rtl_can_wakeup(tp))
9935 __rtl_set_wol(tp, 0);
9937 tp->saved_wolopts = __rtl_get_wol(tp);
9939 tp->rtl_ops.init(tp);
9940 #if IS_BUILTIN(CONFIG_USB_RTL8152)
9941 /* Retry in case request_firmware() is not ready yet. */
9942 tp->rtl_fw.retry = true;
9944 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
9945 set_ethernet_addr(tp, false);
9947 usb_set_intfdata(intf, tp);
9949 netif_napi_add(netdev, &tp->napi, r8152_poll);
9951 ret = register_netdev(netdev);
9953 dev_err(&intf->dev, "couldn't register the device\n");
9957 if (tp->saved_wolopts)
9958 device_set_wakeup_enable(&udev->dev, true);
9960 device_set_wakeup_enable(&udev->dev, false);
9962 /* If we saw a control transfer error while probing then we may
9963 * want to try probe() again. Consider this an error.
9965 if (test_bit(PROBE_SHOULD_RETRY, &tp->flags))
9968 set_bit(PROBED_WITH_NO_ERRORS, &tp->flags);
9969 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
9974 unregister_netdev(netdev);
9977 tasklet_kill(&tp->tx_tl);
9978 cancel_delayed_work_sync(&tp->hw_phy_work);
9979 if (tp->rtl_ops.unload)
9980 tp->rtl_ops.unload(tp);
9981 rtl8152_release_firmware(tp);
9982 usb_set_intfdata(intf, NULL);
9984 if (test_bit(PROBE_SHOULD_RETRY, &tp->flags))
9987 free_netdev(netdev);
9991 #define RTL8152_PROBE_TRIES 3
9993 static int rtl8152_probe(struct usb_interface *intf,
9994 const struct usb_device_id *id)
10000 if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
10003 if (!rtl_check_vendor_ok(intf))
10006 version = rtl8152_get_version(intf);
10007 if (version == RTL_VER_UNKNOWN)
10010 for (i = 0; i < RTL8152_PROBE_TRIES; i++) {
10011 ret = rtl8152_probe_once(intf, id, version);
10012 if (ret != -EAGAIN)
10015 if (ret == -EAGAIN) {
10016 dev_err(&intf->dev,
10017 "r8152 failed probe after %d tries; giving up\n", i);
10024 static void rtl8152_disconnect(struct usb_interface *intf)
10026 struct r8152 *tp = usb_get_intfdata(intf);
10028 usb_set_intfdata(intf, NULL);
10030 rtl_set_unplug(tp);
10032 unregister_netdev(tp->netdev);
10033 tasklet_kill(&tp->tx_tl);
10034 cancel_delayed_work_sync(&tp->hw_phy_work);
10035 if (tp->rtl_ops.unload)
10036 tp->rtl_ops.unload(tp);
10037 rtl8152_release_firmware(tp);
10038 free_netdev(tp->netdev);
10042 /* table of devices that work with this driver */
10043 static const struct usb_device_id rtl8152_table[] = {
10045 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8050) },
10046 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8053) },
10047 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8152) },
10048 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8153) },
10049 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8155) },
10050 { USB_DEVICE(VENDOR_ID_REALTEK, 0x8156) },
10053 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab) },
10054 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6) },
10055 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927) },
10056 { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0c5e) },
10057 { USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101) },
10058 { USB_DEVICE(VENDOR_ID_LENOVO, 0x304f) },
10059 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3054) },
10060 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3062) },
10061 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3069) },
10062 { USB_DEVICE(VENDOR_ID_LENOVO, 0x3082) },
10063 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7205) },
10064 { USB_DEVICE(VENDOR_ID_LENOVO, 0x720c) },
10065 { USB_DEVICE(VENDOR_ID_LENOVO, 0x7214) },
10066 { USB_DEVICE(VENDOR_ID_LENOVO, 0x721e) },
10067 { USB_DEVICE(VENDOR_ID_LENOVO, 0xa387) },
10068 { USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041) },
10069 { USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff) },
10070 { USB_DEVICE(VENDOR_ID_TPLINK, 0x0601) },
10071 { USB_DEVICE(VENDOR_ID_DLINK, 0xb301) },
10072 { USB_DEVICE(VENDOR_ID_ASUS, 0x1976) },
10076 MODULE_DEVICE_TABLE(usb, rtl8152_table);
10078 static struct usb_driver rtl8152_driver = {
10079 .name = MODULENAME,
10080 .id_table = rtl8152_table,
10081 .probe = rtl8152_probe,
10082 .disconnect = rtl8152_disconnect,
10083 .suspend = rtl8152_suspend,
10084 .resume = rtl8152_resume,
10085 .reset_resume = rtl8152_reset_resume,
10086 .pre_reset = rtl8152_pre_reset,
10087 .post_reset = rtl8152_post_reset,
10088 .supports_autosuspend = 1,
10089 .disable_hub_initiated_lpm = 1,
10092 static int rtl8152_cfgselector_choose_configuration(struct usb_device *udev)
10094 struct usb_host_config *c;
10095 int i, num_configs;
10097 /* Switch the device to vendor mode, if and only if the vendor mode
10098 * driver supports it.
10100 if (__rtl_get_hw_ver(udev) == RTL_VER_UNKNOWN)
10103 /* The vendor mode is not always config #1, so to find it out. */
10105 num_configs = udev->descriptor.bNumConfigurations;
10106 for (i = 0; i < num_configs; (i++, c++)) {
10107 struct usb_interface_descriptor *desc = NULL;
10109 if (!c->desc.bNumInterfaces)
10111 desc = &c->intf_cache[0]->altsetting->desc;
10112 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
10116 if (i == num_configs)
10119 return c->desc.bConfigurationValue;
10122 static struct usb_device_driver rtl8152_cfgselector_driver = {
10123 .name = MODULENAME "-cfgselector",
10124 .choose_configuration = rtl8152_cfgselector_choose_configuration,
10125 .id_table = rtl8152_table,
10126 .generic_subclass = 1,
10127 .supports_autosuspend = 1,
10130 static int __init rtl8152_driver_init(void)
10134 ret = usb_register_device_driver(&rtl8152_cfgselector_driver, THIS_MODULE);
10137 return usb_register(&rtl8152_driver);
10140 static void __exit rtl8152_driver_exit(void)
10142 usb_deregister(&rtl8152_driver);
10143 usb_deregister_device_driver(&rtl8152_cfgselector_driver);
10146 module_init(rtl8152_driver_init);
10147 module_exit(rtl8152_driver_exit);
10149 MODULE_AUTHOR(DRIVER_AUTHOR);
10150 MODULE_DESCRIPTION(DRIVER_DESC);
10151 MODULE_LICENSE("GPL");
10152 MODULE_VERSION(DRIVER_VERSION);