1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
6 #include <linux/signal.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <linux/mii.h>
12 #include <linux/ethtool.h>
13 #include <linux/usb.h>
14 #include <linux/crc32.h>
15 #include <linux/if_vlan.h>
16 #include <linux/uaccess.h>
17 #include <linux/list.h>
19 #include <linux/ipv6.h>
20 #include <net/ip6_checksum.h>
21 #include <uapi/linux/mdio.h>
22 #include <linux/mdio.h>
23 #include <linux/usb/cdc.h>
24 #include <linux/suspend.h>
25 #include <linux/atomic.h>
26 #include <linux/acpi.h>
27 #include <linux/firmware.h>
28 #include <crypto/hash.h>
29 #include <linux/usb/r8152.h>
31 /* Information for net-next */
32 #define NETNEXT_VERSION "12"
34 /* Information for net */
35 #define NET_VERSION "12"
37 #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
38 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
39 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
40 #define MODULENAME "r8152"
42 #define R8152_PHY_ID 32
44 #define PLA_IDR 0xc000
45 #define PLA_RCR 0xc010
46 #define PLA_RCR1 0xc012
47 #define PLA_RMS 0xc016
48 #define PLA_RXFIFO_CTRL0 0xc0a0
49 #define PLA_RXFIFO_FULL 0xc0a2
50 #define PLA_RXFIFO_CTRL1 0xc0a4
51 #define PLA_RX_FIFO_FULL 0xc0a6
52 #define PLA_RXFIFO_CTRL2 0xc0a8
53 #define PLA_RX_FIFO_EMPTY 0xc0aa
54 #define PLA_DMY_REG0 0xc0b0
55 #define PLA_FMC 0xc0b4
56 #define PLA_CFG_WOL 0xc0b6
57 #define PLA_TEREDO_CFG 0xc0bc
58 #define PLA_TEREDO_WAKE_BASE 0xc0c4
59 #define PLA_MAR 0xcd00
60 #define PLA_BACKUP 0xd000
61 #define PLA_BDC_CR 0xd1a0
62 #define PLA_TEREDO_TIMER 0xd2cc
63 #define PLA_REALWOW_TIMER 0xd2e8
64 #define PLA_UPHY_TIMER 0xd388
65 #define PLA_SUSPEND_FLAG 0xd38a
66 #define PLA_INDICATE_FALG 0xd38c
67 #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */
68 #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */
69 #define PLA_EXTRA_STATUS 0xd398
70 #define PLA_GPHY_CTRL 0xd3ae
71 #define PLA_POL_GPIO_CTRL 0xdc6a
72 #define PLA_EFUSE_DATA 0xdd00
73 #define PLA_EFUSE_CMD 0xdd02
74 #define PLA_LEDSEL 0xdd90
75 #define PLA_LED_FEATURE 0xdd92
76 #define PLA_PHYAR 0xde00
77 #define PLA_BOOT_CTRL 0xe004
78 #define PLA_LWAKE_CTRL_REG 0xe007
79 #define PLA_GPHY_INTR_IMR 0xe022
80 #define PLA_EEE_CR 0xe040
81 #define PLA_EEE_TXTWSYS 0xe04c
82 #define PLA_EEE_TXTWSYS_2P5G 0xe058
83 #define PLA_EEEP_CR 0xe080
84 #define PLA_MAC_PWR_CTRL 0xe0c0
85 #define PLA_MAC_PWR_CTRL2 0xe0ca
86 #define PLA_MAC_PWR_CTRL3 0xe0cc
87 #define PLA_MAC_PWR_CTRL4 0xe0ce
88 #define PLA_WDT6_CTRL 0xe428
89 #define PLA_TCR0 0xe610
90 #define PLA_TCR1 0xe612
91 #define PLA_MTPS 0xe615
92 #define PLA_TXFIFO_CTRL 0xe618
93 #define PLA_TXFIFO_FULL 0xe61a
94 #define PLA_RSTTALLY 0xe800
96 #define PLA_CRWECR 0xe81c
97 #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
98 #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
99 #define PLA_CONFIG5 0xe822
100 #define PLA_PHY_PWR 0xe84c
101 #define PLA_OOB_CTRL 0xe84f
102 #define PLA_CPCR 0xe854
103 #define PLA_MISC_0 0xe858
104 #define PLA_MISC_1 0xe85a
105 #define PLA_OCP_GPHY_BASE 0xe86c
106 #define PLA_TALLYCNT 0xe890
107 #define PLA_SFF_STS_7 0xe8de
108 #define PLA_PHYSTATUS 0xe908
109 #define PLA_CONFIG6 0xe90a /* CONFIG6 */
110 #define PLA_USB_CFG 0xe952
111 #define PLA_BP_BA 0xfc26
112 #define PLA_BP_0 0xfc28
113 #define PLA_BP_1 0xfc2a
114 #define PLA_BP_2 0xfc2c
115 #define PLA_BP_3 0xfc2e
116 #define PLA_BP_4 0xfc30
117 #define PLA_BP_5 0xfc32
118 #define PLA_BP_6 0xfc34
119 #define PLA_BP_7 0xfc36
120 #define PLA_BP_EN 0xfc38
122 #define USB_USB2PHY 0xb41e
123 #define USB_SSPHYLINK1 0xb426
124 #define USB_SSPHYLINK2 0xb428
125 #define USB_L1_CTRL 0xb45e
126 #define USB_U2P3_CTRL 0xb460
127 #define USB_CSR_DUMMY1 0xb464
128 #define USB_CSR_DUMMY2 0xb466
129 #define USB_DEV_STAT 0xb808
130 #define USB_CONNECT_TIMER 0xcbf8
131 #define USB_MSC_TIMER 0xcbfc
132 #define USB_BURST_SIZE 0xcfc0
133 #define USB_FW_FIX_EN0 0xcfca
134 #define USB_FW_FIX_EN1 0xcfcc
135 #define USB_LPM_CONFIG 0xcfd8
136 #define USB_ECM_OPTION 0xcfee
137 #define USB_CSTMR 0xcfef /* RTL8153A */
138 #define USB_MISC_2 0xcfff
139 #define USB_ECM_OP 0xd26b
140 #define USB_GPHY_CTRL 0xd284
141 #define USB_SPEED_OPTION 0xd32a
142 #define USB_FW_CTRL 0xd334 /* RTL8153B */
143 #define USB_FC_TIMER 0xd340
144 #define USB_USB_CTRL 0xd406
145 #define USB_PHY_CTRL 0xd408
146 #define USB_TX_AGG 0xd40a
147 #define USB_RX_BUF_TH 0xd40c
148 #define USB_USB_TIMER 0xd428
149 #define USB_RX_EARLY_TIMEOUT 0xd42c
150 #define USB_RX_EARLY_SIZE 0xd42e
151 #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
152 #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
153 #define USB_TX_DMA 0xd434
154 #define USB_UPT_RXDMA_OWN 0xd437
155 #define USB_UPHY3_MDCMDIO 0xd480
156 #define USB_TOLERANCE 0xd490
157 #define USB_LPM_CTRL 0xd41a
158 #define USB_BMU_RESET 0xd4b0
159 #define USB_BMU_CONFIG 0xd4b4
160 #define USB_U1U2_TIMER 0xd4da
161 #define USB_FW_TASK 0xd4e8 /* RTL8153B */
162 #define USB_RX_AGGR_NUM 0xd4ee
163 #define USB_UPS_CTRL 0xd800
164 #define USB_POWER_CUT 0xd80a
165 #define USB_MISC_0 0xd81a
166 #define USB_MISC_1 0xd81f
167 #define USB_AFE_CTRL2 0xd824
168 #define USB_UPHY_XTAL 0xd826
169 #define USB_UPS_CFG 0xd842
170 #define USB_UPS_FLAGS 0xd848
171 #define USB_WDT1_CTRL 0xe404
172 #define USB_WDT11_CTRL 0xe43c
173 #define USB_BP_BA PLA_BP_BA
174 #define USB_BP_0 PLA_BP_0
175 #define USB_BP_1 PLA_BP_1
176 #define USB_BP_2 PLA_BP_2
177 #define USB_BP_3 PLA_BP_3
178 #define USB_BP_4 PLA_BP_4
179 #define USB_BP_5 PLA_BP_5
180 #define USB_BP_6 PLA_BP_6
181 #define USB_BP_7 PLA_BP_7
182 #define USB_BP_EN PLA_BP_EN /* RTL8153A */
183 #define USB_BP_8 0xfc38 /* RTL8153B */
184 #define USB_BP_9 0xfc3a
185 #define USB_BP_10 0xfc3c
186 #define USB_BP_11 0xfc3e
187 #define USB_BP_12 0xfc40
188 #define USB_BP_13 0xfc42
189 #define USB_BP_14 0xfc44
190 #define USB_BP_15 0xfc46
191 #define USB_BP2_EN 0xfc48
194 #define OCP_ALDPS_CONFIG 0x2010
195 #define OCP_EEE_CONFIG1 0x2080
196 #define OCP_EEE_CONFIG2 0x2092
197 #define OCP_EEE_CONFIG3 0x2094
198 #define OCP_BASE_MII 0xa400
199 #define OCP_EEE_AR 0xa41a
200 #define OCP_EEE_DATA 0xa41c
201 #define OCP_PHY_STATUS 0xa420
202 #define OCP_NCTL_CFG 0xa42c
203 #define OCP_POWER_CFG 0xa430
204 #define OCP_EEE_CFG 0xa432
205 #define OCP_SRAM_ADDR 0xa436
206 #define OCP_SRAM_DATA 0xa438
207 #define OCP_DOWN_SPEED 0xa442
208 #define OCP_EEE_ABLE 0xa5c4
209 #define OCP_EEE_ADV 0xa5d0
210 #define OCP_EEE_LPABLE 0xa5d2
211 #define OCP_10GBT_CTRL 0xa5d4
212 #define OCP_10GBT_STAT 0xa5d6
213 #define OCP_EEE_ADV2 0xa6d4
214 #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
215 #define OCP_PHY_PATCH_STAT 0xb800
216 #define OCP_PHY_PATCH_CMD 0xb820
217 #define OCP_PHY_LOCK 0xb82e
218 #define OCP_ADC_IOFFSET 0xbcfc
219 #define OCP_ADC_CFG 0xbc06
220 #define OCP_SYSCLK_CFG 0xc416
223 #define SRAM_GREEN_CFG 0x8011
224 #define SRAM_LPF_CFG 0x8012
225 #define SRAM_GPHY_FW_VER 0x801e
226 #define SRAM_10M_AMP1 0x8080
227 #define SRAM_10M_AMP2 0x8082
228 #define SRAM_IMPEDANCE 0x8084
229 #define SRAM_PHY_LOCK 0xb82e
232 #define RCR_AAP 0x00000001
233 #define RCR_APM 0x00000002
234 #define RCR_AM 0x00000004
235 #define RCR_AB 0x00000008
236 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
237 #define SLOT_EN BIT(11)
240 #define OUTER_VLAN BIT(7)
241 #define INNER_VLAN BIT(6)
243 /* PLA_RXFIFO_CTRL0 */
244 #define RXFIFO_THR1_NORMAL 0x00080002
245 #define RXFIFO_THR1_OOB 0x01800003
247 /* PLA_RXFIFO_FULL */
248 #define RXFIFO_FULL_MASK 0xfff
250 /* PLA_RXFIFO_CTRL1 */
251 #define RXFIFO_THR2_FULL 0x00000060
252 #define RXFIFO_THR2_HIGH 0x00000038
253 #define RXFIFO_THR2_OOB 0x0000004a
254 #define RXFIFO_THR2_NORMAL 0x00a0
256 /* PLA_RXFIFO_CTRL2 */
257 #define RXFIFO_THR3_FULL 0x00000078
258 #define RXFIFO_THR3_HIGH 0x00000048
259 #define RXFIFO_THR3_OOB 0x0000005a
260 #define RXFIFO_THR3_NORMAL 0x0110
262 /* PLA_TXFIFO_CTRL */
263 #define TXFIFO_THR_NORMAL 0x00400008
264 #define TXFIFO_THR_NORMAL2 0x01000008
267 #define ECM_ALDPS 0x0002
270 #define FMC_FCR_MCU_EN 0x0001
273 #define EEEP_CR_EEEP_TX 0x0002
276 #define WDT6_SET_MODE 0x0010
279 #define TCR0_TX_EMPTY 0x0800
280 #define TCR0_AUTO_FIFO 0x0080
283 #define VERSION_MASK 0x7cf0
284 #define IFG_MASK (BIT(3) | BIT(9) | BIT(8))
285 #define IFG_144NS BIT(9)
286 #define IFG_96NS (BIT(9) | BIT(8))
289 #define MTPS_JUMBO (12 * 1024 / 64)
290 #define MTPS_DEFAULT (6 * 1024 / 64)
293 #define TALLY_RESET 0x0001
301 #define CRWECR_NORAML 0x00
302 #define CRWECR_CONFIG 0xc0
305 #define NOW_IS_OOB 0x80
306 #define TXFIFO_EMPTY 0x20
307 #define RXFIFO_EMPTY 0x10
308 #define LINK_LIST_READY 0x02
309 #define DIS_MCU_CLROOB 0x01
310 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
313 #define RXDY_GATED_EN 0x0008
316 #define RE_INIT_LL 0x8000
317 #define MCU_BORW_EN 0x4000
320 #define FLOW_CTRL_EN BIT(0)
321 #define CPCR_RX_VLAN 0x0040
324 #define MAGIC_EN 0x0001
327 #define TEREDO_SEL 0x8000
328 #define TEREDO_WAKE_MASK 0x7f00
329 #define TEREDO_RS_EVENT_MASK 0x00fe
330 #define OOB_TEREDO_EN 0x0001
333 #define ALDPS_PROXY_MODE 0x0001
336 #define EFUSE_READ_CMD BIT(15)
337 #define EFUSE_DATA_BIT16 BIT(7)
340 #define LINK_ON_WAKE_EN 0x0010
341 #define LINK_OFF_WAKE_EN 0x0008
344 #define LANWAKE_CLR_EN BIT(0)
347 #define EN_XG_LIP BIT(1)
348 #define EN_G_LIP BIT(2)
351 #define BWF_EN 0x0040
352 #define MWF_EN 0x0020
353 #define UWF_EN 0x0010
354 #define LAN_WAKE_EN 0x0002
356 /* PLA_LED_FEATURE */
357 #define LED_MODE_MASK 0x0700
360 #define TX_10M_IDLE_EN 0x0080
361 #define PFM_PWM_SWITCH 0x0040
362 #define TEST_IO_OFF BIT(4)
364 /* PLA_MAC_PWR_CTRL */
365 #define D3_CLK_GATED_EN 0x00004000
366 #define MCU_CLK_RATIO 0x07010f07
367 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
368 #define ALDPS_SPDWN_RATIO 0x0f87
370 /* PLA_MAC_PWR_CTRL2 */
371 #define EEE_SPDWN_RATIO 0x8007
372 #define MAC_CLK_SPDWN_EN BIT(15)
373 #define EEE_SPDWN_RATIO_MASK 0xff
375 /* PLA_MAC_PWR_CTRL3 */
376 #define PLA_MCU_SPDWN_EN BIT(14)
377 #define PKT_AVAIL_SPDWN_EN 0x0100
378 #define SUSPEND_SPDWN_EN 0x0004
379 #define U1U2_SPDWN_EN 0x0002
380 #define L1_SPDWN_EN 0x0001
382 /* PLA_MAC_PWR_CTRL4 */
383 #define PWRSAVE_SPDWN_EN 0x1000
384 #define RXDV_SPDWN_EN 0x0800
385 #define TX10MIDLE_EN 0x0100
386 #define IDLE_SPDWN_EN BIT(6)
387 #define TP100_SPDWN_EN 0x0020
388 #define TP500_SPDWN_EN 0x0010
389 #define TP1000_SPDWN_EN 0x0008
390 #define EEE_SPDWN_EN 0x0001
392 /* PLA_GPHY_INTR_IMR */
393 #define GPHY_STS_MSK 0x0001
394 #define SPEED_DOWN_MSK 0x0002
395 #define SPDWN_RXDV_MSK 0x0004
396 #define SPDWN_LINKCHG_MSK 0x0008
399 #define PHYAR_FLAG 0x80000000
402 #define EEE_RX_EN 0x0001
403 #define EEE_TX_EN 0x0002
406 #define AUTOLOAD_DONE 0x0002
408 /* PLA_LWAKE_CTRL_REG */
409 #define LANWAKE_PIN BIT(7)
411 /* PLA_SUSPEND_FLAG */
412 #define LINK_CHG_EVENT BIT(0)
414 /* PLA_INDICATE_FALG */
415 #define UPCOMING_RUNTIME_D3 BIT(0)
417 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */
418 #define DEBUG_OE BIT(0)
419 #define DEBUG_LTSSM 0x0082
421 /* PLA_EXTRA_STATUS */
422 #define CUR_LINK_OK BIT(15)
423 #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */
424 #define LINK_CHANGE_FLAG BIT(8)
425 #define POLL_LINK_CHG BIT(0)
428 #define GPHY_FLASH BIT(1)
430 /* PLA_POL_GPIO_CTRL */
431 #define DACK_DET_EN BIT(15)
432 #define POL_GPHY_PATCH BIT(4)
435 #define USB2PHY_SUSPEND 0x0001
436 #define USB2PHY_L1 0x0002
439 #define DELAY_PHY_PWR_CHG BIT(1)
442 #define pwd_dn_scale_mask 0x3ffe
443 #define pwd_dn_scale(x) ((x) << 1)
446 #define DYNAMIC_BURST 0x0001
449 #define EP4_FULL_FC 0x0001
452 #define STAT_SPEED_MASK 0x0006
453 #define STAT_SPEED_HIGH 0x0000
454 #define STAT_SPEED_FULL 0x0002
457 #define FW_FIX_SUSPEND BIT(14)
460 #define FW_IP_RESET_EN BIT(9)
463 #define LPM_U1U2_EN BIT(0)
466 #define TX_AGG_MAX_THRESHOLD 0x03
469 #define RX_THR_SUPPER 0x0c350180
470 #define RX_THR_HIGH 0x7a120180
471 #define RX_THR_SLOW 0xffff0180
472 #define RX_THR_B 0x00010001
475 #define TEST_MODE_DISABLE 0x00000001
476 #define TX_SIZE_ADJUST1 0x00000100
479 #define BMU_RESET_EP_IN 0x01
480 #define BMU_RESET_EP_OUT 0x02
483 #define ACT_ODMA BIT(1)
485 /* USB_UPT_RXDMA_OWN */
486 #define OWN_UPDATE BIT(0)
487 #define OWN_CLEAR BIT(1)
490 #define FC_PATCH_TASK BIT(1)
492 /* USB_RX_AGGR_NUM */
493 #define RX_AGGR_NUM_MASK 0x1ff
496 #define POWER_CUT 0x0100
498 /* USB_PM_CTRL_STATUS */
499 #define RESUME_INDICATE 0x0001
502 #define BYPASS_MAC_RESET BIT(5)
505 #define FORCE_SUPER BIT(0)
508 #define UPS_FORCE_PWR_DOWN BIT(0)
511 #define EN_ALL_SPEED BIT(0)
514 #define GPHY_PATCH_DONE BIT(2)
515 #define BYPASS_FLASH BIT(5)
516 #define BACKUP_RESTRORE BIT(6)
518 /* USB_SPEED_OPTION */
519 #define RG_PWRDN_EN BIT(8)
520 #define ALL_SPEED_OFF BIT(9)
523 #define FLOW_CTRL_PATCH_OPT BIT(1)
524 #define AUTO_SPEEDUP BIT(3)
525 #define FLOW_CTRL_PATCH_2 BIT(8)
528 #define CTRL_TIMER_EN BIT(15)
531 #define CDC_ECM_EN BIT(3)
532 #define RX_AGG_DISABLE 0x0010
533 #define RX_ZERO_EN 0x0080
536 #define U2P3_ENABLE 0x0001
537 #define RX_DETECT8 BIT(3)
540 #define PWR_EN 0x0001
541 #define PHASE2_EN 0x0008
542 #define UPS_EN BIT(4)
543 #define USP_PREWAKE BIT(5)
546 #define PCUT_STATUS 0x0001
548 /* USB_RX_EARLY_TIMEOUT */
549 #define COALESCE_SUPER 85000U
550 #define COALESCE_HIGH 250000U
551 #define COALESCE_SLOW 524280U
554 #define WTD1_EN BIT(0)
557 #define TIMER11_EN 0x0001
560 /* bit 4 ~ 5: fifo empty boundary */
561 #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
562 /* bit 2 ~ 3: LMP timer */
563 #define LPM_TIMER_MASK 0x0c
564 #define LPM_TIMER_500MS 0x04 /* 500 ms */
565 #define LPM_TIMER_500US 0x0c /* 500 us */
566 #define ROK_EXIT_LPM 0x02
569 #define SEN_VAL_MASK 0xf800
570 #define SEN_VAL_NORMAL 0xa000
571 #define SEL_RXIDLE 0x0100
574 #define OOBS_POLLING BIT(8)
577 #define SAW_CNT_1MS_MASK 0x0fff
578 #define MID_REVERSE BIT(5) /* RTL8156A */
581 #define UPS_FLAGS_R_TUNE BIT(0)
582 #define UPS_FLAGS_EN_10M_CKDIV BIT(1)
583 #define UPS_FLAGS_250M_CKDIV BIT(2)
584 #define UPS_FLAGS_EN_ALDPS BIT(3)
585 #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
586 #define UPS_FLAGS_SPEED_MASK (0xf << 16)
587 #define ups_flags_speed(x) ((x) << 16)
588 #define UPS_FLAGS_EN_EEE BIT(20)
589 #define UPS_FLAGS_EN_500M_EEE BIT(21)
590 #define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
591 #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23)
592 #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
593 #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
594 #define UPS_FLAGS_EN_GREEN BIT(26)
595 #define UPS_FLAGS_EN_FLOW_CTR BIT(27)
611 /* OCP_ALDPS_CONFIG */
612 #define ENPWRSAVE 0x8000
613 #define ENPDNPS 0x0200
614 #define LINKENA 0x0100
615 #define DIS_SDSAVE 0x0010
618 #define PHY_STAT_MASK 0x0007
619 #define PHY_STAT_EXT_INIT 2
620 #define PHY_STAT_LAN_ON 3
621 #define PHY_STAT_PWRDN 5
624 #define PGA_RETURN_EN BIT(1)
627 #define EEE_CLKDIV_EN 0x8000
628 #define EN_ALDPS 0x0004
629 #define EN_10M_PLLOFF 0x0001
631 /* OCP_EEE_CONFIG1 */
632 #define RG_TXLPI_MSK_HFDUP 0x8000
633 #define RG_MATCLR_EN 0x4000
634 #define EEE_10_CAP 0x2000
635 #define EEE_NWAY_EN 0x1000
636 #define TX_QUIET_EN 0x0200
637 #define RX_QUIET_EN 0x0100
638 #define sd_rise_time_mask 0x0070
639 #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
640 #define RG_RXLPI_MSK_HFDUP 0x0008
641 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
643 /* OCP_EEE_CONFIG2 */
644 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
645 #define RG_DACQUIET_EN 0x0400
646 #define RG_LDVQUIET_EN 0x0200
647 #define RG_CKRSEL 0x0020
648 #define RG_EEEPRG_EN 0x0010
650 /* OCP_EEE_CONFIG3 */
651 #define fast_snr_mask 0xff80
652 #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
653 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
654 #define MSK_PH 0x0006 /* bit 0 ~ 3 */
657 /* bit[15:14] function */
658 #define FUN_ADDR 0x0000
659 #define FUN_DATA 0x4000
660 /* bit[4:0] device addr */
663 #define CTAP_SHORT_EN 0x0040
664 #define EEE10_EN 0x0010
667 #define EN_EEE_CMODE BIT(14)
668 #define EN_EEE_1000 BIT(13)
669 #define EN_EEE_100 BIT(12)
670 #define EN_10M_CLKDIV BIT(11)
671 #define EN_10M_BGOFF 0x0080
674 #define RTL_ADV2_5G_F_R BIT(5) /* Advertise 2.5GBASE-T fast-retrain */
677 #define TXDIS_STATE 0x01
678 #define ABD_STATE 0x02
680 /* OCP_PHY_PATCH_STAT */
681 #define PATCH_READY BIT(6)
683 /* OCP_PHY_PATCH_CMD */
684 #define PATCH_REQUEST BIT(4)
687 #define PATCH_LOCK BIT(0)
690 #define CKADSEL_L 0x0100
691 #define ADC_EN 0x0080
692 #define EN_EMI_L 0x0040
695 #define sysclk_div_expo(x) (min(x, 5) << 8)
696 #define clk_div_expo(x) (min(x, 5) << 4)
699 #define GREEN_ETH_EN BIT(15)
700 #define R_TUNE_EN BIT(11)
703 #define LPF_AUTO_TUNE 0x8000
706 #define GDAC_IB_UPALL 0x0008
709 #define AMP_DN 0x0200
712 #define RX_DRIVING_MASK 0x6000
715 #define PHY_PATCH_LOCK 0x0001
718 #define AD_MASK 0xfee0
719 #define BND_MASK 0x0004
720 #define BD_MASK 0x0001
722 #define PASS_THRU_MASK 0x1
724 #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */
726 enum rtl_register_content {
739 #define is_speed_2500(_speed) (((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS))
740 #define is_flow_control(_speed) (((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow))
742 #define RTL8152_MAX_TX 4
743 #define RTL8152_MAX_RX 10
748 #define RTL8152_RX_MAX_PENDING 4096
749 #define RTL8152_RXFG_HEADSZ 256
751 #define INTR_LINK 0x0004
753 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
754 #define RTL8153_RMS RTL8153_MAX_PACKET
755 #define RTL8152_TX_TIMEOUT (5 * HZ)
756 #define mtu_to_size(m) ((m) + VLAN_ETH_HLEN + ETH_FCS_LEN)
757 #define size_to_mtu(s) ((s) - VLAN_ETH_HLEN - ETH_FCS_LEN)
758 #define rx_reserved_size(x) (mtu_to_size(x) + sizeof(struct rx_desc) + RX_ALIGN)
773 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
774 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
776 struct tally_counter {
783 __le32 tx_one_collision;
784 __le32 tx_multi_collision;
794 #define RX_LEN_MASK 0x7fff
797 #define RD_UDP_CS BIT(23)
798 #define RD_TCP_CS BIT(22)
799 #define RD_IPV6_CS BIT(20)
800 #define RD_IPV4_CS BIT(19)
803 #define IPF BIT(23) /* IP checksum fail */
804 #define UDPF BIT(22) /* UDP checksum fail */
805 #define TCPF BIT(21) /* TCP checksum fail */
806 #define RX_VLAN_TAG BIT(16)
815 #define TX_FS BIT(31) /* First segment of a packet */
816 #define TX_LS BIT(30) /* Final segment of a packet */
817 #define GTSENDV4 BIT(28)
818 #define GTSENDV6 BIT(27)
819 #define GTTCPHO_SHIFT 18
820 #define GTTCPHO_MAX 0x7fU
821 #define TX_LEN_MAX 0x3ffffU
824 #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
825 #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
826 #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
827 #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
829 #define MSS_MAX 0x7ffU
830 #define TCPHO_SHIFT 17
831 #define TCPHO_MAX 0x7ffU
832 #define TX_VLAN_TAG BIT(16)
838 struct list_head list, info_list;
840 struct r8152 *context;
846 struct list_head list;
848 struct r8152 *context;
857 struct usb_device *udev;
858 struct napi_struct napi;
859 struct usb_interface *intf;
860 struct net_device *netdev;
861 struct urb *intr_urb;
862 struct tx_agg tx_info[RTL8152_MAX_TX];
863 struct list_head rx_info, rx_used;
864 struct list_head rx_done, tx_free;
865 struct sk_buff_head tx_queue, rx_queue;
866 spinlock_t rx_lock, tx_lock;
867 struct delayed_work schedule, hw_phy_work;
868 struct mii_if_info mii;
869 struct mutex control; /* use for hw setting */
870 #ifdef CONFIG_PM_SLEEP
871 struct notifier_block pm_notifier;
873 struct tasklet_struct tx_tl;
876 void (*init)(struct r8152 *tp);
877 int (*enable)(struct r8152 *tp);
878 void (*disable)(struct r8152 *tp);
879 void (*up)(struct r8152 *tp);
880 void (*down)(struct r8152 *tp);
881 void (*unload)(struct r8152 *tp);
882 int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee);
883 int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee);
884 bool (*in_nway)(struct r8152 *tp);
885 void (*hw_phy_cfg)(struct r8152 *tp);
886 void (*autosuspend_en)(struct r8152 *tp, bool enable);
887 void (*change_mtu)(struct r8152 *tp);
900 u32 eee_plloff_100:1;
901 u32 eee_plloff_giga:1;
905 u32 ctap_short_off:1;
908 #define RTL_VER_SIZE 32
912 const struct firmware *fw;
914 char version[RTL_VER_SIZE];
915 int (*pre_fw)(struct r8152 *tp);
916 int (*post_fw)(struct r8152 *tp);
933 u32 fc_pause_on, fc_pause_off;
935 unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out;
937 u32 support_2500full:1;
938 u32 lenovo_macpassthru:1;
939 u32 dell_tb_rx_agg_bug:1;
950 * struct fw_block - block type and total length
951 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
952 * RTL_FW_USB and so on.
953 * @length: total length of the current block.
961 * struct fw_header - header of the firmware file
962 * @checksum: checksum of sha256 which is calculated from the whole file
963 * except the checksum field of the file. That is, calculate sha256
964 * from the version field to the end of the file.
965 * @version: version of this firmware.
966 * @blocks: the first firmware block of the file
970 char version[RTL_VER_SIZE];
971 struct fw_block blocks[];
974 enum rtl8152_fw_flags {
988 enum rtl8152_fw_fixup_cmd {
1000 struct fw_phy_speed_up {
1001 struct fw_block blk_hdr;
1010 struct fw_block blk_hdr;
1011 struct fw_phy_set ver;
1015 struct fw_phy_fixup {
1016 struct fw_block blk_hdr;
1017 struct fw_phy_set setting;
1022 struct fw_phy_union {
1023 struct fw_block blk_hdr;
1026 struct fw_phy_set pre_set[2];
1027 struct fw_phy_set bp[8];
1028 struct fw_phy_set bp_en;
1035 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
1036 * The layout of the firmware block is:
1037 * <struct fw_mac> + <info> + <firmware data>.
1038 * @blk_hdr: firmware descriptor (type, length)
1039 * @fw_offset: offset of the firmware binary data. The start address of
1040 * the data would be the address of struct fw_mac + @fw_offset.
1041 * @fw_reg: the register to load the firmware. Depends on chip.
1042 * @bp_ba_addr: the register to write break point base address. Depends on
1044 * @bp_ba_value: break point base address. Depends on chip.
1045 * @bp_en_addr: the register to write break point enabled mask. Depends
1047 * @bp_en_value: break point enabled mask. Depends on the firmware.
1048 * @bp_start: the start register of break points. Depends on chip.
1049 * @bp_num: the break point number which needs to be set for this firmware.
1050 * Depends on the firmware.
1051 * @bp: break points. Depends on firmware.
1052 * @reserved: reserved space (unused)
1053 * @fw_ver_reg: the register to store the fw version.
1054 * @fw_ver_data: the firmware version of the current type.
1055 * @info: additional information for debugging, and is followed by the
1056 * binary data of firmware.
1059 struct fw_block blk_hdr;
1068 __le16 bp[16]; /* any value determined by firmware */
1076 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
1077 * This is used to set patch key when loading the firmware of PHY.
1078 * @blk_hdr: firmware descriptor (type, length)
1079 * @key_reg: the register to write the patch key.
1080 * @key_data: patch key.
1081 * @reserved: reserved space (unused)
1083 struct fw_phy_patch_key {
1084 struct fw_block blk_hdr;
1091 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
1092 * The layout of the firmware block is:
1093 * <struct fw_phy_nc> + <info> + <firmware data>.
1094 * @blk_hdr: firmware descriptor (type, length)
1095 * @fw_offset: offset of the firmware binary data. The start address of
1096 * the data would be the address of struct fw_phy_nc + @fw_offset.
1097 * @fw_reg: the register to load the firmware. Depends on chip.
1098 * @ba_reg: the register to write the base address. Depends on chip.
1099 * @ba_data: base address. Depends on chip.
1100 * @patch_en_addr: the register of enabling patch mode. Depends on chip.
1101 * @patch_en_value: patch mode enabled mask. Depends on the firmware.
1102 * @mode_reg: the regitster of switching the mode.
1103 * @mode_pre: the mode needing to be set before loading the firmware.
1104 * @mode_post: the mode to be set when finishing to load the firmware.
1105 * @reserved: reserved space (unused)
1106 * @bp_start: the start register of break points. Depends on chip.
1107 * @bp_num: the break point number which needs to be set for this firmware.
1108 * Depends on the firmware.
1109 * @bp: break points. Depends on firmware.
1110 * @info: additional information for debugging, and is followed by the
1111 * binary data of firmware.
1114 struct fw_block blk_hdr;
1119 __le16 patch_en_addr;
1120 __le16 patch_en_value;
1139 RTL_FW_PHY_UNION_NC,
1140 RTL_FW_PHY_UNION_NC1,
1141 RTL_FW_PHY_UNION_NC2,
1142 RTL_FW_PHY_UNION_UC2,
1143 RTL_FW_PHY_UNION_UC,
1144 RTL_FW_PHY_UNION_MISC,
1145 RTL_FW_PHY_SPEED_UP,
1150 RTL_VER_UNKNOWN = 0,
1173 TX_CSUM_SUCCESS = 0,
1178 #define RTL_ADVERTISED_10_HALF BIT(0)
1179 #define RTL_ADVERTISED_10_FULL BIT(1)
1180 #define RTL_ADVERTISED_100_HALF BIT(2)
1181 #define RTL_ADVERTISED_100_FULL BIT(3)
1182 #define RTL_ADVERTISED_1000_HALF BIT(4)
1183 #define RTL_ADVERTISED_1000_FULL BIT(5)
1184 #define RTL_ADVERTISED_2500_FULL BIT(6)
1186 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
1187 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
1189 static const int multicast_filter_limit = 32;
1190 static unsigned int agg_buf_sz = 16384;
1192 #define RTL_LIMITED_TSO_SIZE (size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc))
1195 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1200 tmp = kmalloc(size, GFP_KERNEL);
1204 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_in,
1205 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
1206 value, index, tmp, size, 500);
1208 memset(data, 0xff, size);
1210 memcpy(data, tmp, size);
1218 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1223 tmp = kmemdup(data, size, GFP_KERNEL);
1227 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_out,
1228 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
1229 value, index, tmp, size, 500);
1236 static void rtl_set_unplug(struct r8152 *tp)
1238 if (tp->udev->state == USB_STATE_NOTATTACHED) {
1239 set_bit(RTL8152_UNPLUG, &tp->flags);
1240 smp_mb__after_atomic();
1244 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
1245 void *data, u16 type)
1250 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1253 /* both size and indix must be 4 bytes align */
1254 if ((size & 3) || !size || (index & 3) || !data)
1257 if ((u32)index + (u32)size > 0xffff)
1262 ret = get_registers(tp, index, type, limit, data);
1270 ret = get_registers(tp, index, type, size, data);
1287 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
1288 u16 size, void *data, u16 type)
1291 u16 byteen_start, byteen_end, byen;
1294 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1297 /* both size and indix must be 4 bytes align */
1298 if ((size & 3) || !size || (index & 3) || !data)
1301 if ((u32)index + (u32)size > 0xffff)
1304 byteen_start = byteen & BYTE_EN_START_MASK;
1305 byteen_end = byteen & BYTE_EN_END_MASK;
1307 byen = byteen_start | (byteen_start << 4);
1308 ret = set_registers(tp, index, type | byen, 4, data);
1321 ret = set_registers(tp, index,
1322 type | BYTE_EN_DWORD,
1331 ret = set_registers(tp, index,
1332 type | BYTE_EN_DWORD,
1344 byen = byteen_end | (byteen_end >> 4);
1345 ret = set_registers(tp, index, type | byen, 4, data);
1358 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1360 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1364 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1366 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1370 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1372 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1375 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1379 generic_ocp_read(tp, index, sizeof(data), &data, type);
1381 return __le32_to_cpu(data);
1384 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1386 __le32 tmp = __cpu_to_le32(data);
1388 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
1391 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1395 u16 byen = BYTE_EN_WORD;
1396 u8 shift = index & 2;
1401 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
1403 data = __le32_to_cpu(tmp);
1404 data >>= (shift * 8);
1410 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1414 u16 byen = BYTE_EN_WORD;
1415 u8 shift = index & 2;
1421 mask <<= (shift * 8);
1422 data <<= (shift * 8);
1426 tmp = __cpu_to_le32(data);
1428 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1431 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1435 u8 shift = index & 3;
1439 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
1441 data = __le32_to_cpu(tmp);
1442 data >>= (shift * 8);
1448 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1452 u16 byen = BYTE_EN_BYTE;
1453 u8 shift = index & 3;
1459 mask <<= (shift * 8);
1460 data <<= (shift * 8);
1464 tmp = __cpu_to_le32(data);
1466 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1469 static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1471 u16 ocp_base, ocp_index;
1473 ocp_base = addr & 0xf000;
1474 if (ocp_base != tp->ocp_base) {
1475 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1476 tp->ocp_base = ocp_base;
1479 ocp_index = (addr & 0x0fff) | 0xb000;
1480 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1483 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1485 u16 ocp_base, ocp_index;
1487 ocp_base = addr & 0xf000;
1488 if (ocp_base != tp->ocp_base) {
1489 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1490 tp->ocp_base = ocp_base;
1493 ocp_index = (addr & 0x0fff) | 0xb000;
1494 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1497 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
1499 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
1502 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
1504 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
1507 static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1509 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1510 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1513 static u16 sram_read(struct r8152 *tp, u16 addr)
1515 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1516 return ocp_reg_read(tp, OCP_SRAM_DATA);
1519 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1521 struct r8152 *tp = netdev_priv(netdev);
1524 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1527 if (phy_id != R8152_PHY_ID)
1530 ret = r8152_mdio_read(tp, reg);
1536 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1538 struct r8152 *tp = netdev_priv(netdev);
1540 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1543 if (phy_id != R8152_PHY_ID)
1546 r8152_mdio_write(tp, reg, val);
1550 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
1553 rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
1556 static int __rtl8152_set_mac_address(struct net_device *netdev, void *p,
1559 struct r8152 *tp = netdev_priv(netdev);
1560 struct sockaddr *addr = p;
1561 int ret = -EADDRNOTAVAIL;
1563 if (!is_valid_ether_addr(addr->sa_data))
1567 ret = usb_autopm_get_interface(tp->intf);
1572 mutex_lock(&tp->control);
1574 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1576 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1577 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1578 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1580 mutex_unlock(&tp->control);
1583 usb_autopm_put_interface(tp->intf);
1588 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1590 return __rtl8152_set_mac_address(netdev, p, false);
1593 /* Devices containing proper chips can support a persistent
1594 * host system provided MAC address.
1595 * Examples of this are Dell TB15 and Dell WD15 docks
1597 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1600 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1601 union acpi_object *obj;
1604 unsigned char buf[6];
1606 acpi_object_type mac_obj_type;
1609 if (tp->lenovo_macpassthru) {
1610 mac_obj_name = "\\MACA";
1611 mac_obj_type = ACPI_TYPE_STRING;
1614 /* test for -AD variant of RTL8153 */
1615 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1616 if ((ocp_data & AD_MASK) == 0x1000) {
1617 /* test for MAC address pass-through bit */
1618 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1619 if ((ocp_data & PASS_THRU_MASK) != 1) {
1620 netif_dbg(tp, probe, tp->netdev,
1621 "No efuse for RTL8153-AD MAC pass through\n");
1625 /* test for RTL8153-BND and RTL8153-BD */
1626 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1627 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1628 netif_dbg(tp, probe, tp->netdev,
1629 "Invalid variant for MAC pass through\n");
1634 mac_obj_name = "\\_SB.AMAC";
1635 mac_obj_type = ACPI_TYPE_BUFFER;
1639 /* returns _AUXMAC_#AABBCCDDEEFF# */
1640 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
1641 obj = (union acpi_object *)buffer.pointer;
1642 if (!ACPI_SUCCESS(status))
1644 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
1645 netif_warn(tp, probe, tp->netdev,
1646 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
1647 obj->type, obj->string.length);
1651 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1652 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1653 netif_warn(tp, probe, tp->netdev,
1654 "Invalid header when reading pass-thru MAC addr\n");
1657 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1658 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1659 netif_warn(tp, probe, tp->netdev,
1660 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1665 memcpy(sa->sa_data, buf, 6);
1666 netif_info(tp, probe, tp->netdev,
1667 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1674 static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1676 struct net_device *dev = tp->netdev;
1679 sa->sa_family = dev->type;
1681 ret = eth_platform_get_mac_address(&tp->udev->dev, sa->sa_data);
1683 if (tp->version == RTL_VER_01) {
1684 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1686 /* if device doesn't support MAC pass through this will
1687 * be expected to be non-zero
1689 ret = vendor_mac_passthru_addr_read(tp, sa);
1691 ret = pla_ocp_read(tp, PLA_BACKUP, 8,
1697 netif_err(tp, probe, dev, "Get ether addr fail\n");
1698 } else if (!is_valid_ether_addr(sa->sa_data)) {
1699 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1701 eth_hw_addr_random(dev);
1702 ether_addr_copy(sa->sa_data, dev->dev_addr);
1703 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1711 static int set_ethernet_addr(struct r8152 *tp, bool in_resume)
1713 struct net_device *dev = tp->netdev;
1717 ret = determine_ethernet_addr(tp, &sa);
1721 if (tp->version == RTL_VER_01)
1722 ether_addr_copy(dev->dev_addr, sa.sa_data);
1724 ret = __rtl8152_set_mac_address(dev, &sa, in_resume);
1729 static void read_bulk_callback(struct urb *urb)
1731 struct net_device *netdev;
1732 int status = urb->status;
1735 unsigned long flags;
1745 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1748 if (!test_bit(WORK_ENABLE, &tp->flags))
1751 netdev = tp->netdev;
1753 /* When link down, the driver would cancel all bulks. */
1754 /* This avoid the re-submitting bulk */
1755 if (!netif_carrier_ok(netdev))
1758 usb_mark_last_busy(tp->udev);
1762 if (urb->actual_length < ETH_ZLEN)
1765 spin_lock_irqsave(&tp->rx_lock, flags);
1766 list_add_tail(&agg->list, &tp->rx_done);
1767 spin_unlock_irqrestore(&tp->rx_lock, flags);
1768 napi_schedule(&tp->napi);
1772 netif_device_detach(tp->netdev);
1775 urb->actual_length = 0;
1776 spin_lock_irqsave(&tp->rx_lock, flags);
1777 list_add_tail(&agg->list, &tp->rx_done);
1778 spin_unlock_irqrestore(&tp->rx_lock, flags);
1779 set_bit(RX_EPROTO, &tp->flags);
1780 schedule_delayed_work(&tp->schedule, 1);
1783 return; /* the urb is in unlink state */
1785 if (net_ratelimit())
1786 netdev_warn(netdev, "maybe reset is needed?\n");
1789 if (net_ratelimit())
1790 netdev_warn(netdev, "Rx status %d\n", status);
1794 r8152_submit_rx(tp, agg, GFP_ATOMIC);
1797 static void write_bulk_callback(struct urb *urb)
1799 struct net_device_stats *stats;
1800 struct net_device *netdev;
1803 unsigned long flags;
1804 int status = urb->status;
1814 netdev = tp->netdev;
1815 stats = &netdev->stats;
1817 if (net_ratelimit())
1818 netdev_warn(netdev, "Tx status %d\n", status);
1819 stats->tx_errors += agg->skb_num;
1821 stats->tx_packets += agg->skb_num;
1822 stats->tx_bytes += agg->skb_len;
1825 spin_lock_irqsave(&tp->tx_lock, flags);
1826 list_add_tail(&agg->list, &tp->tx_free);
1827 spin_unlock_irqrestore(&tp->tx_lock, flags);
1829 usb_autopm_put_interface_async(tp->intf);
1831 if (!netif_carrier_ok(netdev))
1834 if (!test_bit(WORK_ENABLE, &tp->flags))
1837 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1840 if (!skb_queue_empty(&tp->tx_queue))
1841 tasklet_schedule(&tp->tx_tl);
1844 static void intr_callback(struct urb *urb)
1848 int status = urb->status;
1855 if (!test_bit(WORK_ENABLE, &tp->flags))
1858 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1862 case 0: /* success */
1864 case -ECONNRESET: /* unlink */
1866 netif_device_detach(tp->netdev);
1870 netif_info(tp, intr, tp->netdev,
1871 "Stop submitting intr, status %d\n", status);
1874 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1876 /* -EPIPE: should clear the halt */
1878 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1882 d = urb->transfer_buffer;
1883 if (INTR_LINK & __le16_to_cpu(d[0])) {
1884 if (!netif_carrier_ok(tp->netdev)) {
1885 set_bit(RTL8152_LINK_CHG, &tp->flags);
1886 schedule_delayed_work(&tp->schedule, 0);
1889 if (netif_carrier_ok(tp->netdev)) {
1890 netif_stop_queue(tp->netdev);
1891 set_bit(RTL8152_LINK_CHG, &tp->flags);
1892 schedule_delayed_work(&tp->schedule, 0);
1897 res = usb_submit_urb(urb, GFP_ATOMIC);
1898 if (res == -ENODEV) {
1900 netif_device_detach(tp->netdev);
1902 netif_err(tp, intr, tp->netdev,
1903 "can't resubmit intr, status %d\n", res);
1907 static inline void *rx_agg_align(void *data)
1909 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
1912 static inline void *tx_agg_align(void *data)
1914 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
1917 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1919 list_del(&agg->info_list);
1921 usb_free_urb(agg->urb);
1922 put_page(agg->page);
1925 atomic_dec(&tp->rx_count);
1928 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1930 struct net_device *netdev = tp->netdev;
1931 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1932 unsigned int order = get_order(tp->rx_buf_sz);
1933 struct rx_agg *rx_agg;
1934 unsigned long flags;
1936 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1940 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
1944 rx_agg->buffer = page_address(rx_agg->page);
1946 rx_agg->urb = usb_alloc_urb(0, mflags);
1950 rx_agg->context = tp;
1952 INIT_LIST_HEAD(&rx_agg->list);
1953 INIT_LIST_HEAD(&rx_agg->info_list);
1954 spin_lock_irqsave(&tp->rx_lock, flags);
1955 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1956 spin_unlock_irqrestore(&tp->rx_lock, flags);
1958 atomic_inc(&tp->rx_count);
1963 __free_pages(rx_agg->page, order);
1969 static void free_all_mem(struct r8152 *tp)
1971 struct rx_agg *agg, *agg_next;
1972 unsigned long flags;
1975 spin_lock_irqsave(&tp->rx_lock, flags);
1977 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1978 free_rx_agg(tp, agg);
1980 spin_unlock_irqrestore(&tp->rx_lock, flags);
1982 WARN_ON(atomic_read(&tp->rx_count));
1984 for (i = 0; i < RTL8152_MAX_TX; i++) {
1985 usb_free_urb(tp->tx_info[i].urb);
1986 tp->tx_info[i].urb = NULL;
1988 kfree(tp->tx_info[i].buffer);
1989 tp->tx_info[i].buffer = NULL;
1990 tp->tx_info[i].head = NULL;
1993 usb_free_urb(tp->intr_urb);
1994 tp->intr_urb = NULL;
1996 kfree(tp->intr_buff);
1997 tp->intr_buff = NULL;
2000 static int alloc_all_mem(struct r8152 *tp)
2002 struct net_device *netdev = tp->netdev;
2003 struct usb_interface *intf = tp->intf;
2004 struct usb_host_interface *alt = intf->cur_altsetting;
2005 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
2008 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
2010 spin_lock_init(&tp->rx_lock);
2011 spin_lock_init(&tp->tx_lock);
2012 INIT_LIST_HEAD(&tp->rx_info);
2013 INIT_LIST_HEAD(&tp->tx_free);
2014 INIT_LIST_HEAD(&tp->rx_done);
2015 skb_queue_head_init(&tp->tx_queue);
2016 skb_queue_head_init(&tp->rx_queue);
2017 atomic_set(&tp->rx_count, 0);
2019 for (i = 0; i < RTL8152_MAX_RX; i++) {
2020 if (!alloc_rx_agg(tp, GFP_KERNEL))
2024 for (i = 0; i < RTL8152_MAX_TX; i++) {
2028 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
2032 if (buf != tx_agg_align(buf)) {
2034 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
2040 urb = usb_alloc_urb(0, GFP_KERNEL);
2046 INIT_LIST_HEAD(&tp->tx_info[i].list);
2047 tp->tx_info[i].context = tp;
2048 tp->tx_info[i].urb = urb;
2049 tp->tx_info[i].buffer = buf;
2050 tp->tx_info[i].head = tx_agg_align(buf);
2052 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
2055 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2059 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
2063 tp->intr_interval = (int)ep_intr->desc.bInterval;
2064 usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr,
2065 tp->intr_buff, INTBUFSIZE, intr_callback,
2066 tp, tp->intr_interval);
2075 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
2077 struct tx_agg *agg = NULL;
2078 unsigned long flags;
2080 if (list_empty(&tp->tx_free))
2083 spin_lock_irqsave(&tp->tx_lock, flags);
2084 if (!list_empty(&tp->tx_free)) {
2085 struct list_head *cursor;
2087 cursor = tp->tx_free.next;
2088 list_del_init(cursor);
2089 agg = list_entry(cursor, struct tx_agg, list);
2091 spin_unlock_irqrestore(&tp->tx_lock, flags);
2096 /* r8152_csum_workaround()
2097 * The hw limits the value of the transport offset. When the offset is out of
2098 * range, calculate the checksum by sw.
2100 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
2101 struct sk_buff_head *list)
2103 if (skb_shinfo(skb)->gso_size) {
2104 netdev_features_t features = tp->netdev->features;
2105 struct sk_buff *segs, *seg, *next;
2106 struct sk_buff_head seg_list;
2108 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
2109 segs = skb_gso_segment(skb, features);
2110 if (IS_ERR(segs) || !segs)
2113 __skb_queue_head_init(&seg_list);
2115 skb_list_walk_safe(segs, seg, next) {
2116 skb_mark_not_on_list(seg);
2117 __skb_queue_tail(&seg_list, seg);
2120 skb_queue_splice(&seg_list, list);
2122 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2123 if (skb_checksum_help(skb) < 0)
2126 __skb_queue_head(list, skb);
2128 struct net_device_stats *stats;
2131 stats = &tp->netdev->stats;
2132 stats->tx_dropped++;
2137 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
2139 if (skb_vlan_tag_present(skb)) {
2142 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
2143 desc->opts2 |= cpu_to_le32(opts2);
2147 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
2149 u32 opts2 = le32_to_cpu(desc->opts2);
2151 if (opts2 & RX_VLAN_TAG)
2152 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2153 swab16(opts2 & 0xffff));
2156 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
2157 struct sk_buff *skb, u32 len, u32 transport_offset)
2159 u32 mss = skb_shinfo(skb)->gso_size;
2160 u32 opts1, opts2 = 0;
2161 int ret = TX_CSUM_SUCCESS;
2163 WARN_ON_ONCE(len > TX_LEN_MAX);
2165 opts1 = len | TX_FS | TX_LS;
2168 if (transport_offset > GTTCPHO_MAX) {
2169 netif_warn(tp, tx_err, tp->netdev,
2170 "Invalid transport offset 0x%x for TSO\n",
2176 switch (vlan_get_protocol(skb)) {
2177 case htons(ETH_P_IP):
2181 case htons(ETH_P_IPV6):
2182 if (skb_cow_head(skb, 0)) {
2186 tcp_v6_gso_csum_prep(skb);
2195 opts1 |= transport_offset << GTTCPHO_SHIFT;
2196 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
2197 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2200 if (transport_offset > TCPHO_MAX) {
2201 netif_warn(tp, tx_err, tp->netdev,
2202 "Invalid transport offset 0x%x\n",
2208 switch (vlan_get_protocol(skb)) {
2209 case htons(ETH_P_IP):
2211 ip_protocol = ip_hdr(skb)->protocol;
2214 case htons(ETH_P_IPV6):
2216 ip_protocol = ipv6_hdr(skb)->nexthdr;
2220 ip_protocol = IPPROTO_RAW;
2224 if (ip_protocol == IPPROTO_TCP)
2226 else if (ip_protocol == IPPROTO_UDP)
2231 opts2 |= transport_offset << TCPHO_SHIFT;
2234 desc->opts2 = cpu_to_le32(opts2);
2235 desc->opts1 = cpu_to_le32(opts1);
2241 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
2243 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2247 __skb_queue_head_init(&skb_head);
2248 spin_lock(&tx_queue->lock);
2249 skb_queue_splice_init(tx_queue, &skb_head);
2250 spin_unlock(&tx_queue->lock);
2252 tx_data = agg->head;
2255 remain = agg_buf_sz;
2257 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
2258 struct tx_desc *tx_desc;
2259 struct sk_buff *skb;
2263 skb = __skb_dequeue(&skb_head);
2267 len = skb->len + sizeof(*tx_desc);
2270 __skb_queue_head(&skb_head, skb);
2274 tx_data = tx_agg_align(tx_data);
2275 tx_desc = (struct tx_desc *)tx_data;
2277 offset = (u32)skb_transport_offset(skb);
2279 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
2280 r8152_csum_workaround(tp, skb, &skb_head);
2284 rtl_tx_vlan_tag(tx_desc, skb);
2286 tx_data += sizeof(*tx_desc);
2289 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
2290 struct net_device_stats *stats = &tp->netdev->stats;
2292 stats->tx_dropped++;
2293 dev_kfree_skb_any(skb);
2294 tx_data -= sizeof(*tx_desc);
2299 agg->skb_len += len;
2300 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
2302 dev_kfree_skb_any(skb);
2304 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
2306 if (tp->dell_tb_rx_agg_bug)
2310 if (!skb_queue_empty(&skb_head)) {
2311 spin_lock(&tx_queue->lock);
2312 skb_queue_splice(&skb_head, tx_queue);
2313 spin_unlock(&tx_queue->lock);
2316 netif_tx_lock(tp->netdev);
2318 if (netif_queue_stopped(tp->netdev) &&
2319 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
2320 netif_wake_queue(tp->netdev);
2322 netif_tx_unlock(tp->netdev);
2324 ret = usb_autopm_get_interface_async(tp->intf);
2328 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out,
2329 agg->head, (int)(tx_data - (u8 *)agg->head),
2330 (usb_complete_t)write_bulk_callback, agg);
2332 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
2334 usb_autopm_put_interface_async(tp->intf);
2340 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
2342 u8 checksum = CHECKSUM_NONE;
2345 if (!(tp->netdev->features & NETIF_F_RXCSUM))
2348 opts2 = le32_to_cpu(rx_desc->opts2);
2349 opts3 = le32_to_cpu(rx_desc->opts3);
2351 if (opts2 & RD_IPV4_CS) {
2353 checksum = CHECKSUM_NONE;
2354 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2355 checksum = CHECKSUM_UNNECESSARY;
2356 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2357 checksum = CHECKSUM_UNNECESSARY;
2358 } else if (opts2 & RD_IPV6_CS) {
2359 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2360 checksum = CHECKSUM_UNNECESSARY;
2361 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2362 checksum = CHECKSUM_UNNECESSARY;
2369 static inline bool rx_count_exceed(struct r8152 *tp)
2371 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2374 static inline int agg_offset(struct rx_agg *agg, void *addr)
2376 return (int)(addr - agg->buffer);
2379 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2381 struct rx_agg *agg, *agg_next, *agg_free = NULL;
2382 unsigned long flags;
2384 spin_lock_irqsave(&tp->rx_lock, flags);
2386 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2387 if (page_count(agg->page) == 1) {
2389 list_del_init(&agg->list);
2393 if (rx_count_exceed(tp)) {
2394 list_del_init(&agg->list);
2395 free_rx_agg(tp, agg);
2401 spin_unlock_irqrestore(&tp->rx_lock, flags);
2403 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
2404 agg_free = alloc_rx_agg(tp, mflags);
2409 static int rx_bottom(struct r8152 *tp, int budget)
2411 unsigned long flags;
2412 struct list_head *cursor, *next, rx_queue;
2413 int ret = 0, work_done = 0;
2414 struct napi_struct *napi = &tp->napi;
2416 if (!skb_queue_empty(&tp->rx_queue)) {
2417 while (work_done < budget) {
2418 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2419 struct net_device *netdev = tp->netdev;
2420 struct net_device_stats *stats = &netdev->stats;
2421 unsigned int pkt_len;
2427 napi_gro_receive(napi, skb);
2429 stats->rx_packets++;
2430 stats->rx_bytes += pkt_len;
2434 if (list_empty(&tp->rx_done))
2437 clear_bit(RX_EPROTO, &tp->flags);
2438 INIT_LIST_HEAD(&rx_queue);
2439 spin_lock_irqsave(&tp->rx_lock, flags);
2440 list_splice_init(&tp->rx_done, &rx_queue);
2441 spin_unlock_irqrestore(&tp->rx_lock, flags);
2443 list_for_each_safe(cursor, next, &rx_queue) {
2444 struct rx_desc *rx_desc;
2445 struct rx_agg *agg, *agg_free;
2450 list_del_init(cursor);
2452 agg = list_entry(cursor, struct rx_agg, list);
2454 if (urb->status != 0 || urb->actual_length < ETH_ZLEN)
2457 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2459 rx_desc = agg->buffer;
2460 rx_data = agg->buffer;
2461 len_used += sizeof(struct rx_desc);
2463 while (urb->actual_length > len_used) {
2464 struct net_device *netdev = tp->netdev;
2465 struct net_device_stats *stats = &netdev->stats;
2466 unsigned int pkt_len, rx_frag_head_sz;
2467 struct sk_buff *skb;
2469 /* limit the skb numbers for rx_queue */
2470 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2473 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
2474 if (pkt_len < ETH_ZLEN)
2477 len_used += pkt_len;
2478 if (urb->actual_length < len_used)
2481 pkt_len -= ETH_FCS_LEN;
2482 rx_data += sizeof(struct rx_desc);
2484 if (!agg_free || tp->rx_copybreak > pkt_len)
2485 rx_frag_head_sz = pkt_len;
2487 rx_frag_head_sz = tp->rx_copybreak;
2489 skb = napi_alloc_skb(napi, rx_frag_head_sz);
2491 stats->rx_dropped++;
2495 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
2496 memcpy(skb->data, rx_data, rx_frag_head_sz);
2497 skb_put(skb, rx_frag_head_sz);
2498 pkt_len -= rx_frag_head_sz;
2499 rx_data += rx_frag_head_sz;
2501 skb_add_rx_frag(skb, 0, agg->page,
2502 agg_offset(agg, rx_data),
2504 SKB_DATA_ALIGN(pkt_len));
2505 get_page(agg->page);
2508 skb->protocol = eth_type_trans(skb, netdev);
2509 rtl_rx_vlan_tag(rx_desc, skb);
2510 if (work_done < budget) {
2512 stats->rx_packets++;
2513 stats->rx_bytes += skb->len;
2514 napi_gro_receive(napi, skb);
2516 __skb_queue_tail(&tp->rx_queue, skb);
2520 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
2521 rx_desc = (struct rx_desc *)rx_data;
2522 len_used = agg_offset(agg, rx_data);
2523 len_used += sizeof(struct rx_desc);
2526 WARN_ON(!agg_free && page_count(agg->page) > 1);
2529 spin_lock_irqsave(&tp->rx_lock, flags);
2530 if (page_count(agg->page) == 1) {
2531 list_add(&agg_free->list, &tp->rx_used);
2533 list_add_tail(&agg->list, &tp->rx_used);
2537 spin_unlock_irqrestore(&tp->rx_lock, flags);
2542 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2544 urb->actual_length = 0;
2545 list_add_tail(&agg->list, next);
2549 if (!list_empty(&rx_queue)) {
2550 spin_lock_irqsave(&tp->rx_lock, flags);
2551 list_splice_tail(&rx_queue, &tp->rx_done);
2552 spin_unlock_irqrestore(&tp->rx_lock, flags);
2559 static void tx_bottom(struct r8152 *tp)
2564 struct net_device *netdev = tp->netdev;
2567 if (skb_queue_empty(&tp->tx_queue))
2570 agg = r8152_get_tx_agg(tp);
2574 res = r8152_tx_agg_fill(tp, agg);
2578 if (res == -ENODEV) {
2580 netif_device_detach(netdev);
2582 struct net_device_stats *stats = &netdev->stats;
2583 unsigned long flags;
2585 netif_warn(tp, tx_err, netdev,
2586 "failed tx_urb %d\n", res);
2587 stats->tx_dropped += agg->skb_num;
2589 spin_lock_irqsave(&tp->tx_lock, flags);
2590 list_add_tail(&agg->list, &tp->tx_free);
2591 spin_unlock_irqrestore(&tp->tx_lock, flags);
2596 static void bottom_half(struct tasklet_struct *t)
2598 struct r8152 *tp = from_tasklet(tp, t, tx_tl);
2600 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2603 if (!test_bit(WORK_ENABLE, &tp->flags))
2606 /* When link down, the driver would cancel all bulks. */
2607 /* This avoid the re-submitting bulk */
2608 if (!netif_carrier_ok(tp->netdev))
2611 clear_bit(SCHEDULE_TASKLET, &tp->flags);
2616 static int r8152_poll(struct napi_struct *napi, int budget)
2618 struct r8152 *tp = container_of(napi, struct r8152, napi);
2621 work_done = rx_bottom(tp, budget);
2623 if (work_done < budget) {
2624 if (!napi_complete_done(napi, work_done))
2626 if (!list_empty(&tp->rx_done))
2627 napi_schedule(napi);
2635 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2639 /* The rx would be stopped, so skip submitting */
2640 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2641 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2644 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in,
2645 agg->buffer, tp->rx_buf_sz,
2646 (usb_complete_t)read_bulk_callback, agg);
2648 ret = usb_submit_urb(agg->urb, mem_flags);
2649 if (ret == -ENODEV) {
2651 netif_device_detach(tp->netdev);
2653 struct urb *urb = agg->urb;
2654 unsigned long flags;
2656 urb->actual_length = 0;
2657 spin_lock_irqsave(&tp->rx_lock, flags);
2658 list_add_tail(&agg->list, &tp->rx_done);
2659 spin_unlock_irqrestore(&tp->rx_lock, flags);
2661 netif_err(tp, rx_err, tp->netdev,
2662 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2664 napi_schedule(&tp->napi);
2670 static void rtl_drop_queued_tx(struct r8152 *tp)
2672 struct net_device_stats *stats = &tp->netdev->stats;
2673 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2674 struct sk_buff *skb;
2676 if (skb_queue_empty(tx_queue))
2679 __skb_queue_head_init(&skb_head);
2680 spin_lock_bh(&tx_queue->lock);
2681 skb_queue_splice_init(tx_queue, &skb_head);
2682 spin_unlock_bh(&tx_queue->lock);
2684 while ((skb = __skb_dequeue(&skb_head))) {
2686 stats->tx_dropped++;
2690 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2692 struct r8152 *tp = netdev_priv(netdev);
2694 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
2696 usb_queue_reset_device(tp->intf);
2699 static void rtl8152_set_rx_mode(struct net_device *netdev)
2701 struct r8152 *tp = netdev_priv(netdev);
2703 if (netif_carrier_ok(netdev)) {
2704 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2705 schedule_delayed_work(&tp->schedule, 0);
2709 static void _rtl8152_set_rx_mode(struct net_device *netdev)
2711 struct r8152 *tp = netdev_priv(netdev);
2712 u32 mc_filter[2]; /* Multicast hash filter */
2716 netif_stop_queue(netdev);
2717 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2718 ocp_data &= ~RCR_ACPT_ALL;
2719 ocp_data |= RCR_AB | RCR_APM;
2721 if (netdev->flags & IFF_PROMISC) {
2722 /* Unconditionally log net taps. */
2723 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2724 ocp_data |= RCR_AM | RCR_AAP;
2725 mc_filter[1] = 0xffffffff;
2726 mc_filter[0] = 0xffffffff;
2727 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2728 (netdev->flags & IFF_ALLMULTI)) {
2729 /* Too many to filter perfectly -- accept all multicasts. */
2731 mc_filter[1] = 0xffffffff;
2732 mc_filter[0] = 0xffffffff;
2734 struct netdev_hw_addr *ha;
2738 netdev_for_each_mc_addr(ha, netdev) {
2739 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2741 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2746 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2747 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
2749 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
2750 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2751 netif_wake_queue(netdev);
2754 static netdev_features_t
2755 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2756 netdev_features_t features)
2758 u32 mss = skb_shinfo(skb)->gso_size;
2759 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2760 int offset = skb_transport_offset(skb);
2762 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
2763 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2764 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2765 features &= ~NETIF_F_GSO_MASK;
2770 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
2771 struct net_device *netdev)
2773 struct r8152 *tp = netdev_priv(netdev);
2775 skb_tx_timestamp(skb);
2777 skb_queue_tail(&tp->tx_queue, skb);
2779 if (!list_empty(&tp->tx_free)) {
2780 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2781 set_bit(SCHEDULE_TASKLET, &tp->flags);
2782 schedule_delayed_work(&tp->schedule, 0);
2784 usb_mark_last_busy(tp->udev);
2785 tasklet_schedule(&tp->tx_tl);
2787 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
2788 netif_stop_queue(netdev);
2791 return NETDEV_TX_OK;
2794 static void r8152b_reset_packet_filter(struct r8152 *tp)
2798 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2799 ocp_data &= ~FMC_FCR_MCU_EN;
2800 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2801 ocp_data |= FMC_FCR_MCU_EN;
2802 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2805 static void rtl8152_nic_reset(struct r8152 *tp)
2810 switch (tp->version) {
2814 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2816 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2818 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2819 ocp_data &= ~BMU_RESET_EP_IN;
2820 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2822 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2823 ocp_data |= CDC_ECM_EN;
2824 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2826 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2828 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2830 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2831 ocp_data |= BMU_RESET_EP_IN;
2832 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2834 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2835 ocp_data &= ~CDC_ECM_EN;
2836 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2840 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2842 for (i = 0; i < 1000; i++) {
2843 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2845 usleep_range(100, 400);
2851 static void set_tx_qlen(struct r8152 *tp)
2853 tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc));
2856 static inline u16 rtl8152_get_speed(struct r8152 *tp)
2858 return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2861 static void rtl_eee_plus_en(struct r8152 *tp, bool enable)
2865 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2867 ocp_data |= EEEP_CR_EEEP_TX;
2869 ocp_data &= ~EEEP_CR_EEEP_TX;
2870 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2873 static void rtl_set_eee_plus(struct r8152 *tp)
2875 if (rtl8152_get_speed(tp) & _10bps)
2876 rtl_eee_plus_en(tp, true);
2878 rtl_eee_plus_en(tp, false);
2881 static void rxdy_gated_en(struct r8152 *tp, bool enable)
2885 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2887 ocp_data |= RXDY_GATED_EN;
2889 ocp_data &= ~RXDY_GATED_EN;
2890 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2893 static int rtl_start_rx(struct r8152 *tp)
2895 struct rx_agg *agg, *agg_next;
2896 struct list_head tmp_list;
2897 unsigned long flags;
2900 INIT_LIST_HEAD(&tmp_list);
2902 spin_lock_irqsave(&tp->rx_lock, flags);
2904 INIT_LIST_HEAD(&tp->rx_done);
2905 INIT_LIST_HEAD(&tp->rx_used);
2907 list_splice_init(&tp->rx_info, &tmp_list);
2909 spin_unlock_irqrestore(&tp->rx_lock, flags);
2911 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2912 INIT_LIST_HEAD(&agg->list);
2914 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2915 if (++i > RTL8152_MAX_RX) {
2916 spin_lock_irqsave(&tp->rx_lock, flags);
2917 list_add_tail(&agg->list, &tp->rx_used);
2918 spin_unlock_irqrestore(&tp->rx_lock, flags);
2919 } else if (unlikely(ret < 0)) {
2920 spin_lock_irqsave(&tp->rx_lock, flags);
2921 list_add_tail(&agg->list, &tp->rx_done);
2922 spin_unlock_irqrestore(&tp->rx_lock, flags);
2924 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
2928 spin_lock_irqsave(&tp->rx_lock, flags);
2929 WARN_ON(!list_empty(&tp->rx_info));
2930 list_splice(&tmp_list, &tp->rx_info);
2931 spin_unlock_irqrestore(&tp->rx_lock, flags);
2936 static int rtl_stop_rx(struct r8152 *tp)
2938 struct rx_agg *agg, *agg_next;
2939 struct list_head tmp_list;
2940 unsigned long flags;
2942 INIT_LIST_HEAD(&tmp_list);
2944 /* The usb_kill_urb() couldn't be used in atomic.
2945 * Therefore, move the list of rx_info to a tmp one.
2946 * Then, list_for_each_entry_safe could be used without
2950 spin_lock_irqsave(&tp->rx_lock, flags);
2951 list_splice_init(&tp->rx_info, &tmp_list);
2952 spin_unlock_irqrestore(&tp->rx_lock, flags);
2954 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2955 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2956 * equal to 1, so the other ones could be freed safely.
2958 if (page_count(agg->page) > 1)
2959 free_rx_agg(tp, agg);
2961 usb_kill_urb(agg->urb);
2964 /* Move back the list of temp to the rx_info */
2965 spin_lock_irqsave(&tp->rx_lock, flags);
2966 WARN_ON(!list_empty(&tp->rx_info));
2967 list_splice(&tmp_list, &tp->rx_info);
2968 spin_unlock_irqrestore(&tp->rx_lock, flags);
2970 while (!skb_queue_empty(&tp->rx_queue))
2971 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2976 static void rtl_set_ifg(struct r8152 *tp, u16 speed)
2980 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2981 ocp_data &= ~IFG_MASK;
2982 if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) {
2983 ocp_data |= IFG_144NS;
2984 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
2986 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
2987 ocp_data &= ~TX10MIDLE_EN;
2988 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
2990 ocp_data |= IFG_96NS;
2991 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
2993 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
2994 ocp_data |= TX10MIDLE_EN;
2995 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
2999 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
3001 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
3002 OWN_UPDATE | OWN_CLEAR);
3005 static int rtl_enable(struct r8152 *tp)
3009 r8152b_reset_packet_filter(tp);
3011 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
3012 ocp_data |= CR_RE | CR_TE;
3013 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
3015 switch (tp->version) {
3019 r8153b_rx_agg_chg_indicate(tp);
3025 rxdy_gated_en(tp, false);
3030 static int rtl8152_enable(struct r8152 *tp)
3032 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3036 rtl_set_eee_plus(tp);
3038 return rtl_enable(tp);
3041 static void r8153_set_rx_early_timeout(struct r8152 *tp)
3043 u32 ocp_data = tp->coalesce / 8;
3045 switch (tp->version) {
3050 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3057 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
3058 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
3060 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3062 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3071 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3073 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3075 r8153b_rx_agg_chg_indicate(tp);
3083 static void r8153_set_rx_early_size(struct r8152 *tp)
3085 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
3087 switch (tp->version) {
3092 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3098 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3107 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3109 r8153b_rx_agg_chg_indicate(tp);
3117 static int rtl8153_enable(struct r8152 *tp)
3121 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3125 rtl_set_eee_plus(tp);
3126 r8153_set_rx_early_timeout(tp);
3127 r8153_set_rx_early_size(tp);
3129 rtl_set_ifg(tp, rtl8152_get_speed(tp));
3131 switch (tp->version) {
3134 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
3135 ocp_data &= ~FC_PATCH_TASK;
3136 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3137 usleep_range(1000, 2000);
3138 ocp_data |= FC_PATCH_TASK;
3139 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3145 return rtl_enable(tp);
3148 static void rtl_disable(struct r8152 *tp)
3153 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3154 rtl_drop_queued_tx(tp);
3158 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3159 ocp_data &= ~RCR_ACPT_ALL;
3160 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3162 rtl_drop_queued_tx(tp);
3164 for (i = 0; i < RTL8152_MAX_TX; i++)
3165 usb_kill_urb(tp->tx_info[i].urb);
3167 rxdy_gated_en(tp, true);
3169 for (i = 0; i < 1000; i++) {
3170 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3171 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
3173 usleep_range(1000, 2000);
3176 for (i = 0; i < 1000; i++) {
3177 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
3179 usleep_range(1000, 2000);
3184 rtl8152_nic_reset(tp);
3187 static void r8152_power_cut_en(struct r8152 *tp, bool enable)
3191 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
3193 ocp_data |= POWER_CUT;
3195 ocp_data &= ~POWER_CUT;
3196 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
3198 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
3199 ocp_data &= ~RESUME_INDICATE;
3200 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
3203 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
3207 switch (tp->version) {
3218 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
3220 ocp_data |= CPCR_RX_VLAN;
3222 ocp_data &= ~CPCR_RX_VLAN;
3223 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
3233 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1);
3235 ocp_data |= OUTER_VLAN | INNER_VLAN;
3237 ocp_data &= ~(OUTER_VLAN | INNER_VLAN);
3238 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data);
3243 static int rtl8152_set_features(struct net_device *dev,
3244 netdev_features_t features)
3246 netdev_features_t changed = features ^ dev->features;
3247 struct r8152 *tp = netdev_priv(dev);
3250 ret = usb_autopm_get_interface(tp->intf);
3254 mutex_lock(&tp->control);
3256 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3257 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3258 rtl_rx_vlan_en(tp, true);
3260 rtl_rx_vlan_en(tp, false);
3263 mutex_unlock(&tp->control);
3265 usb_autopm_put_interface(tp->intf);
3271 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
3273 static u32 __rtl_get_wol(struct r8152 *tp)
3278 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3279 if (ocp_data & LINK_ON_WAKE_EN)
3280 wolopts |= WAKE_PHY;
3282 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3283 if (ocp_data & UWF_EN)
3284 wolopts |= WAKE_UCAST;
3285 if (ocp_data & BWF_EN)
3286 wolopts |= WAKE_BCAST;
3287 if (ocp_data & MWF_EN)
3288 wolopts |= WAKE_MCAST;
3290 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3291 if (ocp_data & MAGIC_EN)
3292 wolopts |= WAKE_MAGIC;
3297 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
3301 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3303 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3304 ocp_data &= ~LINK_ON_WAKE_EN;
3305 if (wolopts & WAKE_PHY)
3306 ocp_data |= LINK_ON_WAKE_EN;
3307 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3309 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3310 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
3311 if (wolopts & WAKE_UCAST)
3313 if (wolopts & WAKE_BCAST)
3315 if (wolopts & WAKE_MCAST)
3317 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3319 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3321 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3322 ocp_data &= ~MAGIC_EN;
3323 if (wolopts & WAKE_MAGIC)
3324 ocp_data |= MAGIC_EN;
3325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3327 if (wolopts & WAKE_ANY)
3328 device_set_wakeup_enable(&tp->udev->dev, true);
3330 device_set_wakeup_enable(&tp->udev->dev, false);
3333 static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable)
3335 u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3337 /* MAC clock speed down */
3339 ocp_data |= MAC_CLK_SPDWN_EN;
3341 ocp_data &= ~MAC_CLK_SPDWN_EN;
3343 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3346 static void r8156_mac_clk_spd(struct r8152 *tp, bool enable)
3350 /* MAC clock speed down */
3352 /* aldps_spdwn_ratio, tp10_spdwn_ratio */
3353 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3356 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3357 ocp_data &= ~EEE_SPDWN_RATIO_MASK;
3358 ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */
3359 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3361 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3362 ocp_data &= ~MAC_CLK_SPDWN_EN;
3363 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3367 static void r8153_u1u2en(struct r8152 *tp, bool enable)
3372 memset(u1u2, 0xff, sizeof(u1u2));
3374 memset(u1u2, 0x00, sizeof(u1u2));
3376 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3379 static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3383 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3385 ocp_data |= LPM_U1U2_EN;
3387 ocp_data &= ~LPM_U1U2_EN;
3389 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3392 static void r8153_u2p3en(struct r8152 *tp, bool enable)
3396 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
3398 ocp_data |= U2P3_ENABLE;
3400 ocp_data &= ~U2P3_ENABLE;
3401 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3404 static void r8153b_ups_flags(struct r8152 *tp)
3408 if (tp->ups_info.green)
3409 ups_flags |= UPS_FLAGS_EN_GREEN;
3411 if (tp->ups_info.aldps)
3412 ups_flags |= UPS_FLAGS_EN_ALDPS;
3414 if (tp->ups_info.eee)
3415 ups_flags |= UPS_FLAGS_EN_EEE;
3417 if (tp->ups_info.flow_control)
3418 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3420 if (tp->ups_info.eee_ckdiv)
3421 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3423 if (tp->ups_info.eee_cmod_lv)
3424 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3426 if (tp->ups_info.r_tune)
3427 ups_flags |= UPS_FLAGS_R_TUNE;
3429 if (tp->ups_info._10m_ckdiv)
3430 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3432 if (tp->ups_info.eee_plloff_100)
3433 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3435 if (tp->ups_info.eee_plloff_giga)
3436 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3438 if (tp->ups_info._250m_ckdiv)
3439 ups_flags |= UPS_FLAGS_250M_CKDIV;
3441 if (tp->ups_info.ctap_short_off)
3442 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3444 switch (tp->ups_info.speed_duplex) {
3446 ups_flags |= ups_flags_speed(1);
3449 ups_flags |= ups_flags_speed(2);
3451 case NWAY_100M_HALF:
3452 ups_flags |= ups_flags_speed(3);
3454 case NWAY_100M_FULL:
3455 ups_flags |= ups_flags_speed(4);
3457 case NWAY_1000M_FULL:
3458 ups_flags |= ups_flags_speed(5);
3460 case FORCE_10M_HALF:
3461 ups_flags |= ups_flags_speed(6);
3463 case FORCE_10M_FULL:
3464 ups_flags |= ups_flags_speed(7);
3466 case FORCE_100M_HALF:
3467 ups_flags |= ups_flags_speed(8);
3469 case FORCE_100M_FULL:
3470 ups_flags |= ups_flags_speed(9);
3476 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3479 static void r8156_ups_flags(struct r8152 *tp)
3483 if (tp->ups_info.green)
3484 ups_flags |= UPS_FLAGS_EN_GREEN;
3486 if (tp->ups_info.aldps)
3487 ups_flags |= UPS_FLAGS_EN_ALDPS;
3489 if (tp->ups_info.eee)
3490 ups_flags |= UPS_FLAGS_EN_EEE;
3492 if (tp->ups_info.flow_control)
3493 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3495 if (tp->ups_info.eee_ckdiv)
3496 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3498 if (tp->ups_info._10m_ckdiv)
3499 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3501 if (tp->ups_info.eee_plloff_100)
3502 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3504 if (tp->ups_info.eee_plloff_giga)
3505 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3507 if (tp->ups_info._250m_ckdiv)
3508 ups_flags |= UPS_FLAGS_250M_CKDIV;
3510 switch (tp->ups_info.speed_duplex) {
3511 case FORCE_10M_HALF:
3512 ups_flags |= ups_flags_speed(0);
3514 case FORCE_10M_FULL:
3515 ups_flags |= ups_flags_speed(1);
3517 case FORCE_100M_HALF:
3518 ups_flags |= ups_flags_speed(2);
3520 case FORCE_100M_FULL:
3521 ups_flags |= ups_flags_speed(3);
3524 ups_flags |= ups_flags_speed(4);
3527 ups_flags |= ups_flags_speed(5);
3529 case NWAY_100M_HALF:
3530 ups_flags |= ups_flags_speed(6);
3532 case NWAY_100M_FULL:
3533 ups_flags |= ups_flags_speed(7);
3535 case NWAY_1000M_FULL:
3536 ups_flags |= ups_flags_speed(8);
3538 case NWAY_2500M_FULL:
3539 ups_flags |= ups_flags_speed(9);
3545 switch (tp->ups_info.lite_mode) {
3547 ups_flags |= 0 << 5;
3550 ups_flags |= 2 << 5;
3554 ups_flags |= 1 << 5;
3558 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3561 static void rtl_green_en(struct r8152 *tp, bool enable)
3565 data = sram_read(tp, SRAM_GREEN_CFG);
3567 data |= GREEN_ETH_EN;
3569 data &= ~GREEN_ETH_EN;
3570 sram_write(tp, SRAM_GREEN_CFG, data);
3572 tp->ups_info.green = enable;
3575 static void r8153b_green_en(struct r8152 *tp, bool enable)
3578 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
3579 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
3580 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
3582 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
3583 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
3584 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
3587 rtl_green_en(tp, true);
3590 static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3595 for (i = 0; i < 500; i++) {
3596 data = ocp_reg_read(tp, OCP_PHY_STATUS);
3597 data &= PHY_STAT_MASK;
3599 if (data == desired)
3601 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3602 data == PHY_STAT_EXT_INIT) {
3607 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3614 static void r8153b_ups_en(struct r8152 *tp, bool enable)
3616 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3619 r8153b_ups_flags(tp);
3621 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3622 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3624 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3625 ocp_data |= UPS_FORCE_PWR_DOWN;
3626 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3628 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3629 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3631 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3632 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3633 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3635 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3638 for (i = 0; i < 500; i++) {
3639 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3645 tp->rtl_ops.hw_phy_cfg(tp);
3647 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3648 tp->duplex, tp->advertising);
3653 static void r8153c_ups_en(struct r8152 *tp, bool enable)
3655 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3658 r8153b_ups_flags(tp);
3660 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3661 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3663 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3664 ocp_data |= UPS_FORCE_PWR_DOWN;
3665 ocp_data &= ~BIT(7);
3666 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3668 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3669 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3671 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3672 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3673 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3675 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3678 for (i = 0; i < 500; i++) {
3679 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3685 tp->rtl_ops.hw_phy_cfg(tp);
3687 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3688 tp->duplex, tp->advertising);
3691 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3693 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3695 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3697 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3701 static void r8156_ups_en(struct r8152 *tp, bool enable)
3703 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3706 r8156_ups_flags(tp);
3708 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3709 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3711 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3712 ocp_data |= UPS_FORCE_PWR_DOWN;
3713 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3715 switch (tp->version) {
3718 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL);
3719 ocp_data &= ~OOBS_POLLING;
3720 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data);
3726 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3727 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3729 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3730 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3731 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3733 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3734 tp->rtl_ops.hw_phy_cfg(tp);
3736 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3737 tp->duplex, tp->advertising);
3742 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3746 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3748 ocp_data |= PWR_EN | PHASE2_EN;
3750 ocp_data &= ~(PWR_EN | PHASE2_EN);
3751 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3753 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3754 ocp_data &= ~PCUT_STATUS;
3755 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3758 static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3762 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3764 ocp_data |= PWR_EN | PHASE2_EN;
3766 ocp_data &= ~PWR_EN;
3767 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3769 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3770 ocp_data &= ~PCUT_STATUS;
3771 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3774 static void r8153_queue_wake(struct r8152 *tp, bool enable)
3778 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
3780 ocp_data |= UPCOMING_RUNTIME_D3;
3782 ocp_data &= ~UPCOMING_RUNTIME_D3;
3783 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
3785 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3786 ocp_data &= ~LINK_CHG_EVENT;
3787 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3789 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3790 ocp_data &= ~LINK_CHANGE_FLAG;
3791 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
3794 static bool rtl_can_wakeup(struct r8152 *tp)
3796 struct usb_device *udev = tp->udev;
3798 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3801 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3806 __rtl_set_wol(tp, WAKE_ANY);
3808 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3810 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3811 ocp_data |= LINK_OFF_WAKE_EN;
3812 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3814 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3818 __rtl_set_wol(tp, tp->saved_wolopts);
3820 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3822 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3823 ocp_data &= ~LINK_OFF_WAKE_EN;
3824 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3826 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3830 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3833 r8153_u1u2en(tp, false);
3834 r8153_u2p3en(tp, false);
3835 rtl_runtime_suspend_enable(tp, true);
3837 rtl_runtime_suspend_enable(tp, false);
3839 switch (tp->version) {
3846 r8153_u2p3en(tp, true);
3850 r8153_u1u2en(tp, true);
3854 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3857 r8153_queue_wake(tp, true);
3858 r8153b_u1u2en(tp, false);
3859 r8153_u2p3en(tp, false);
3860 rtl_runtime_suspend_enable(tp, true);
3861 r8153b_ups_en(tp, true);
3863 r8153b_ups_en(tp, false);
3864 r8153_queue_wake(tp, false);
3865 rtl_runtime_suspend_enable(tp, false);
3866 if (tp->udev->speed >= USB_SPEED_SUPER)
3867 r8153b_u1u2en(tp, true);
3871 static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable)
3874 r8153_queue_wake(tp, true);
3875 r8153b_u1u2en(tp, false);
3876 r8153_u2p3en(tp, false);
3877 rtl_runtime_suspend_enable(tp, true);
3878 r8153c_ups_en(tp, true);
3880 r8153c_ups_en(tp, false);
3881 r8153_queue_wake(tp, false);
3882 rtl_runtime_suspend_enable(tp, false);
3883 r8153b_u1u2en(tp, true);
3887 static void rtl8156_runtime_enable(struct r8152 *tp, bool enable)
3890 r8153_queue_wake(tp, true);
3891 r8153b_u1u2en(tp, false);
3892 r8153_u2p3en(tp, false);
3893 rtl_runtime_suspend_enable(tp, true);
3895 r8153_queue_wake(tp, false);
3896 rtl_runtime_suspend_enable(tp, false);
3897 r8153_u2p3en(tp, true);
3898 if (tp->udev->speed >= USB_SPEED_SUPER)
3899 r8153b_u1u2en(tp, true);
3903 static void r8153_teredo_off(struct r8152 *tp)
3907 switch (tp->version) {
3915 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3916 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3931 /* The bit 0 ~ 7 are relative with teredo settings. They are
3932 * W1C (write 1 to clear), so set all 1 to disable it.
3934 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3938 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3939 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3940 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3943 static void rtl_reset_bmu(struct r8152 *tp)
3947 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3948 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3949 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3950 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3951 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3954 /* Clear the bp to stop the firmware before loading a new one */
3955 static void rtl_clear_bp(struct r8152 *tp, u16 type)
3957 switch (tp->version) {
3966 ocp_write_byte(tp, type, PLA_BP_EN, 0);
3969 ocp_write_word(tp, type, USB_BP2_EN, 0);
3971 ocp_write_word(tp, type, USB_BP_8, 0);
3972 ocp_write_word(tp, type, USB_BP_9, 0);
3973 ocp_write_word(tp, type, USB_BP_10, 0);
3974 ocp_write_word(tp, type, USB_BP_11, 0);
3975 ocp_write_word(tp, type, USB_BP_12, 0);
3976 ocp_write_word(tp, type, USB_BP_13, 0);
3977 ocp_write_word(tp, type, USB_BP_14, 0);
3978 ocp_write_word(tp, type, USB_BP_15, 0);
3988 if (type == MCU_TYPE_USB) {
3989 ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
3991 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0);
3992 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0);
3993 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0);
3994 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0);
3995 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0);
3996 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0);
3997 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0);
3998 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0);
4000 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
4005 ocp_write_word(tp, type, PLA_BP_0, 0);
4006 ocp_write_word(tp, type, PLA_BP_1, 0);
4007 ocp_write_word(tp, type, PLA_BP_2, 0);
4008 ocp_write_word(tp, type, PLA_BP_3, 0);
4009 ocp_write_word(tp, type, PLA_BP_4, 0);
4010 ocp_write_word(tp, type, PLA_BP_5, 0);
4011 ocp_write_word(tp, type, PLA_BP_6, 0);
4012 ocp_write_word(tp, type, PLA_BP_7, 0);
4014 /* wait 3 ms to make sure the firmware is stopped */
4015 usleep_range(3000, 6000);
4016 ocp_write_word(tp, type, PLA_BP_BA, 0);
4019 static inline void rtl_reset_ocp_base(struct r8152 *tp)
4024 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
4029 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
4031 data |= PATCH_REQUEST;
4034 data &= ~PATCH_REQUEST;
4035 check = PATCH_READY;
4037 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
4039 for (i = 0; wait && i < 5000; i++) {
4042 usleep_range(1000, 2000);
4043 ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
4044 if ((ocp_data & PATCH_READY) ^ check)
4048 if (request && wait &&
4049 !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
4050 dev_err(&tp->intf->dev, "PHY patch request fail\n");
4051 rtl_phy_patch_request(tp, false, false);
4058 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
4060 if (patch_key && key_addr) {
4061 sram_write(tp, key_addr, patch_key);
4062 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
4063 } else if (key_addr) {
4066 sram_write(tp, 0x0000, 0x0000);
4068 data = ocp_reg_read(tp, OCP_PHY_LOCK);
4069 data &= ~PATCH_LOCK;
4070 ocp_reg_write(tp, OCP_PHY_LOCK, data);
4072 sram_write(tp, key_addr, 0x0000);
4079 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
4081 if (rtl_phy_patch_request(tp, true, wait))
4084 rtl_patch_key_set(tp, key_addr, patch_key);
4089 static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
4091 rtl_patch_key_set(tp, key_addr, 0);
4093 rtl_phy_patch_request(tp, false, wait);
4098 static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy)
4104 switch (tp->version) {
4125 fw_offset = __le16_to_cpu(phy->fw_offset);
4126 length = __le32_to_cpu(phy->blk_hdr.length);
4127 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4128 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4132 length -= fw_offset;
4134 dev_err(&tp->intf->dev, "invalid block length\n");
4138 if (__le16_to_cpu(phy->fw_reg) != 0x9A00) {
4139 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4148 static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver)
4152 switch (tp->version) {
4163 if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) {
4164 dev_err(&tp->intf->dev, "invalid block length\n");
4168 if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) {
4169 dev_err(&tp->intf->dev, "invalid phy ver addr\n");
4178 static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix)
4182 switch (tp->version) {
4193 if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) {
4194 dev_err(&tp->intf->dev, "invalid block length\n");
4198 if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD ||
4199 __le16_to_cpu(fix->setting.data) != BIT(7)) {
4200 dev_err(&tp->intf->dev, "invalid phy fixup\n");
4209 static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy)
4215 switch (tp->version) {
4226 fw_offset = __le16_to_cpu(phy->fw_offset);
4227 length = __le32_to_cpu(phy->blk_hdr.length);
4228 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4229 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4233 length -= fw_offset;
4235 dev_err(&tp->intf->dev, "invalid block length\n");
4239 if (phy->pre_num > 2) {
4240 dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num);
4244 if (phy->bp_num > 8) {
4245 dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num);
4254 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
4257 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
4260 switch (tp->version) {
4266 patch_en_addr = 0xa01a;
4274 fw_offset = __le16_to_cpu(phy->fw_offset);
4275 if (fw_offset < sizeof(*phy)) {
4276 dev_err(&tp->intf->dev, "fw_offset too small\n");
4280 length = __le32_to_cpu(phy->blk_hdr.length);
4281 if (length < fw_offset) {
4282 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4286 length -= __le16_to_cpu(phy->fw_offset);
4287 if (!length || (length & 1)) {
4288 dev_err(&tp->intf->dev, "invalid block length\n");
4292 if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
4293 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4297 if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
4298 dev_err(&tp->intf->dev, "invalid base address register\n");
4302 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
4303 dev_err(&tp->intf->dev,
4304 "invalid patch mode enabled register\n");
4308 if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
4309 dev_err(&tp->intf->dev,
4310 "invalid register to switch the mode\n");
4314 if (__le16_to_cpu(phy->bp_start) != bp_start) {
4315 dev_err(&tp->intf->dev,
4316 "invalid start register of break point\n");
4320 if (__le16_to_cpu(phy->bp_num) > 4) {
4321 dev_err(&tp->intf->dev, "invalid break point number\n");
4330 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
4332 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
4337 type = __le32_to_cpu(mac->blk_hdr.type);
4338 if (type == RTL_FW_PLA) {
4339 switch (tp->version) {
4344 bp_ba_addr = PLA_BP_BA;
4346 bp_start = PLA_BP_0;
4360 bp_ba_addr = PLA_BP_BA;
4361 bp_en_addr = PLA_BP_EN;
4362 bp_start = PLA_BP_0;
4367 bp_ba_addr = PLA_BP_BA;
4368 bp_en_addr = USB_BP2_EN;
4369 bp_start = PLA_BP_0;
4375 } else if (type == RTL_FW_USB) {
4376 switch (tp->version) {
4382 bp_ba_addr = USB_BP_BA;
4383 bp_en_addr = USB_BP_EN;
4384 bp_start = USB_BP_0;
4395 bp_ba_addr = USB_BP_BA;
4396 bp_en_addr = USB_BP2_EN;
4397 bp_start = USB_BP_0;
4410 fw_offset = __le16_to_cpu(mac->fw_offset);
4411 if (fw_offset < sizeof(*mac)) {
4412 dev_err(&tp->intf->dev, "fw_offset too small\n");
4416 length = __le32_to_cpu(mac->blk_hdr.length);
4417 if (length < fw_offset) {
4418 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4422 length -= fw_offset;
4423 if (length < 4 || (length & 3)) {
4424 dev_err(&tp->intf->dev, "invalid block length\n");
4428 if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
4429 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4433 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
4434 dev_err(&tp->intf->dev, "invalid base address register\n");
4438 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
4439 dev_err(&tp->intf->dev, "invalid enabled mask register\n");
4443 if (__le16_to_cpu(mac->bp_start) != bp_start) {
4444 dev_err(&tp->intf->dev,
4445 "invalid start register of break point\n");
4449 if (__le16_to_cpu(mac->bp_num) > max_bp) {
4450 dev_err(&tp->intf->dev, "invalid break point number\n");
4454 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
4456 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
4466 /* Verify the checksum for the firmware file. It is calculated from the version
4467 * field to the end of the file. Compare the result with the checksum field to
4468 * make sure the file is correct.
4470 static long rtl8152_fw_verify_checksum(struct r8152 *tp,
4471 struct fw_header *fw_hdr, size_t size)
4473 unsigned char checksum[sizeof(fw_hdr->checksum)];
4474 struct crypto_shash *alg;
4475 struct shash_desc *sdesc;
4479 alg = crypto_alloc_shash("sha256", 0, 0);
4485 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
4487 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
4488 crypto_shash_digestsize(alg));
4492 len = sizeof(*sdesc) + crypto_shash_descsize(alg);
4493 sdesc = kmalloc(len, GFP_KERNEL);
4500 len = size - sizeof(fw_hdr->checksum);
4501 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
4506 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
4507 dev_err(&tp->intf->dev, "checksum fail\n");
4512 crypto_free_shash(alg);
4517 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
4519 const struct firmware *fw = rtl_fw->fw;
4520 struct fw_header *fw_hdr = (struct fw_header *)fw->data;
4521 unsigned long fw_flags = 0;
4525 if (fw->size < sizeof(*fw_hdr)) {
4526 dev_err(&tp->intf->dev, "file too small\n");
4530 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
4536 for (i = sizeof(*fw_hdr); i < fw->size;) {
4537 struct fw_block *block = (struct fw_block *)&fw->data[i];
4540 if ((i + sizeof(*block)) > fw->size)
4543 type = __le32_to_cpu(block->type);
4546 if (__le32_to_cpu(block->length) != sizeof(*block))
4550 if (test_bit(FW_FLAGS_PLA, &fw_flags)) {
4551 dev_err(&tp->intf->dev,
4552 "multiple PLA firmware encountered");
4556 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4557 dev_err(&tp->intf->dev,
4558 "check PLA firmware failed\n");
4561 __set_bit(FW_FLAGS_PLA, &fw_flags);
4564 if (test_bit(FW_FLAGS_USB, &fw_flags)) {
4565 dev_err(&tp->intf->dev,
4566 "multiple USB firmware encountered");
4570 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4571 dev_err(&tp->intf->dev,
4572 "check USB firmware failed\n");
4575 __set_bit(FW_FLAGS_USB, &fw_flags);
4577 case RTL_FW_PHY_START:
4578 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4579 test_bit(FW_FLAGS_NC, &fw_flags) ||
4580 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4581 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4582 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4583 test_bit(FW_FLAGS_UC, &fw_flags) ||
4584 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4585 dev_err(&tp->intf->dev,
4586 "check PHY_START fail\n");
4590 if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) {
4591 dev_err(&tp->intf->dev,
4592 "Invalid length for PHY_START\n");
4595 __set_bit(FW_FLAGS_START, &fw_flags);
4597 case RTL_FW_PHY_STOP:
4598 if (test_bit(FW_FLAGS_STOP, &fw_flags) ||
4599 !test_bit(FW_FLAGS_START, &fw_flags)) {
4600 dev_err(&tp->intf->dev,
4601 "Check PHY_STOP fail\n");
4605 if (__le32_to_cpu(block->length) != sizeof(*block)) {
4606 dev_err(&tp->intf->dev,
4607 "Invalid length for PHY_STOP\n");
4610 __set_bit(FW_FLAGS_STOP, &fw_flags);
4613 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4614 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4615 dev_err(&tp->intf->dev,
4616 "check PHY_NC fail\n");
4620 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4621 dev_err(&tp->intf->dev,
4622 "multiple PHY NC encountered\n");
4626 if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) {
4627 dev_err(&tp->intf->dev,
4628 "check PHY NC firmware failed\n");
4631 __set_bit(FW_FLAGS_NC, &fw_flags);
4633 case RTL_FW_PHY_UNION_NC:
4634 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4635 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4636 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4637 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4638 test_bit(FW_FLAGS_UC, &fw_flags) ||
4639 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4640 dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n");
4644 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4645 dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n");
4649 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4650 dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n");
4653 __set_bit(FW_FLAGS_NC, &fw_flags);
4655 case RTL_FW_PHY_UNION_NC1:
4656 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4657 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4658 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4659 test_bit(FW_FLAGS_UC, &fw_flags) ||
4660 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4661 dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n");
4665 if (test_bit(FW_FLAGS_NC1, &fw_flags)) {
4666 dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n");
4670 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4671 dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n");
4674 __set_bit(FW_FLAGS_NC1, &fw_flags);
4676 case RTL_FW_PHY_UNION_NC2:
4677 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4678 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4679 test_bit(FW_FLAGS_UC, &fw_flags) ||
4680 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4681 dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n");
4685 if (test_bit(FW_FLAGS_NC2, &fw_flags)) {
4686 dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n");
4690 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4691 dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n");
4694 __set_bit(FW_FLAGS_NC2, &fw_flags);
4696 case RTL_FW_PHY_UNION_UC2:
4697 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4698 test_bit(FW_FLAGS_UC, &fw_flags) ||
4699 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4700 dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n");
4704 if (test_bit(FW_FLAGS_UC2, &fw_flags)) {
4705 dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n");
4709 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4710 dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n");
4713 __set_bit(FW_FLAGS_UC2, &fw_flags);
4715 case RTL_FW_PHY_UNION_UC:
4716 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4717 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4718 dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n");
4722 if (test_bit(FW_FLAGS_UC, &fw_flags)) {
4723 dev_err(&tp->intf->dev, "multiple PHY UC encountered\n");
4727 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4728 dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n");
4731 __set_bit(FW_FLAGS_UC, &fw_flags);
4733 case RTL_FW_PHY_UNION_MISC:
4734 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4735 dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n");
4739 case RTL_FW_PHY_FIXUP:
4740 if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) {
4741 dev_err(&tp->intf->dev, "check PHY fixup failed\n");
4745 case RTL_FW_PHY_SPEED_UP:
4746 if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) {
4747 dev_err(&tp->intf->dev, "multiple PHY firmware encountered");
4751 if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) {
4752 dev_err(&tp->intf->dev, "check PHY speed up failed\n");
4755 __set_bit(FW_FLAGS_SPEED_UP, &fw_flags);
4757 case RTL_FW_PHY_VER:
4758 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4759 test_bit(FW_FLAGS_NC, &fw_flags) ||
4760 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4761 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4762 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4763 test_bit(FW_FLAGS_UC, &fw_flags) ||
4764 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4765 dev_err(&tp->intf->dev, "Invalid order to set PHY version\n");
4769 if (test_bit(FW_FLAGS_VER, &fw_flags)) {
4770 dev_err(&tp->intf->dev, "multiple PHY version encountered");
4774 if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) {
4775 dev_err(&tp->intf->dev, "check PHY version failed\n");
4778 __set_bit(FW_FLAGS_VER, &fw_flags);
4781 dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
4787 i += ALIGN(__le32_to_cpu(block->length), 8);
4791 if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) {
4792 dev_err(&tp->intf->dev, "without PHY_STOP\n");
4801 static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait)
4806 rtl_reset_ocp_base(tp);
4808 if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) {
4809 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4813 len = __le32_to_cpu(phy->blk_hdr.length);
4814 len -= __le16_to_cpu(phy->fw_offset);
4815 data = (u8 *)phy + __le16_to_cpu(phy->fw_offset);
4817 if (rtl_phy_patch_request(tp, true, wait))
4829 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL);
4830 ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE;
4831 ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data);
4833 generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB);
4838 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL);
4839 ocp_data |= POL_GPHY_PATCH;
4840 ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data);
4842 for (i = 0; i < 1000; i++) {
4843 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH))
4848 dev_err(&tp->intf->dev, "ram code speedup mode timeout\n");
4853 rtl_reset_ocp_base(tp);
4855 rtl_phy_patch_request(tp, false, wait);
4857 if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version))
4858 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4860 dev_err(&tp->intf->dev, "ram code speedup mode fail\n");
4863 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver)
4867 ver_addr = __le16_to_cpu(phy_ver->ver.addr);
4868 ver = __le16_to_cpu(phy_ver->ver.data);
4870 rtl_reset_ocp_base(tp);
4872 if (sram_read(tp, ver_addr) >= ver) {
4873 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4877 sram_write(tp, ver_addr, ver);
4879 dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver);
4884 static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix)
4888 rtl_reset_ocp_base(tp);
4890 addr = __le16_to_cpu(fix->setting.addr);
4891 data = ocp_reg_read(tp, addr);
4893 switch (__le16_to_cpu(fix->bit_cmd)) {
4895 data &= __le16_to_cpu(fix->setting.data);
4898 data |= __le16_to_cpu(fix->setting.data);
4901 data &= ~__le16_to_cpu(fix->setting.data);
4904 data ^= __le16_to_cpu(fix->setting.data);
4910 ocp_reg_write(tp, addr, data);
4912 dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data);
4915 static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy)
4921 rtl_reset_ocp_base(tp);
4924 for (i = 0; i < num; i++)
4925 sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr),
4926 __le16_to_cpu(phy->pre_set[i].data));
4928 length = __le32_to_cpu(phy->blk_hdr.length);
4929 length -= __le16_to_cpu(phy->fw_offset);
4931 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4933 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4934 for (i = 0; i < num; i++)
4935 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4938 for (i = 0; i < num; i++)
4939 sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data));
4941 if (phy->bp_num && phy->bp_en.addr)
4942 sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data));
4944 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4947 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
4949 u16 mode_reg, bp_index;
4953 rtl_reset_ocp_base(tp);
4955 mode_reg = __le16_to_cpu(phy->mode_reg);
4956 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
4957 sram_write(tp, __le16_to_cpu(phy->ba_reg),
4958 __le16_to_cpu(phy->ba_data));
4960 length = __le32_to_cpu(phy->blk_hdr.length);
4961 length -= __le16_to_cpu(phy->fw_offset);
4963 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4965 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4966 for (i = 0; i < num; i++)
4967 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4969 sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
4970 __le16_to_cpu(phy->patch_en_value));
4972 bp_index = __le16_to_cpu(phy->bp_start);
4973 num = __le16_to_cpu(phy->bp_num);
4974 for (i = 0; i < num; i++) {
4975 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
4979 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
4981 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4984 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
4986 u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg;
4991 switch (__le32_to_cpu(mac->blk_hdr.type)) {
4993 type = MCU_TYPE_PLA;
4996 type = MCU_TYPE_USB;
5002 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
5003 if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) {
5004 dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB");
5008 rtl_clear_bp(tp, type);
5010 /* Enable backup/restore of MACDBG. This is required after clearing PLA
5011 * break points and before applying the PLA firmware.
5013 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
5014 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
5015 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
5016 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
5019 length = __le32_to_cpu(mac->blk_hdr.length);
5020 length -= __le16_to_cpu(mac->fw_offset);
5023 data += __le16_to_cpu(mac->fw_offset);
5025 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
5028 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
5029 __le16_to_cpu(mac->bp_ba_value));
5031 bp_index = __le16_to_cpu(mac->bp_start);
5032 bp_num = __le16_to_cpu(mac->bp_num);
5033 for (i = 0; i < bp_num; i++) {
5034 ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i]));
5038 bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
5040 ocp_write_word(tp, type, bp_en_addr,
5041 __le16_to_cpu(mac->bp_en_value));
5044 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
5047 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
5050 static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
5052 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5053 const struct firmware *fw;
5054 struct fw_header *fw_hdr;
5055 struct fw_phy_patch_key *key;
5057 int i, patch_phy = 1;
5059 if (IS_ERR_OR_NULL(rtl_fw->fw))
5063 fw_hdr = (struct fw_header *)fw->data;
5068 for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
5069 struct fw_block *block = (struct fw_block *)&fw->data[i];
5071 switch (__le32_to_cpu(block->type)) {
5076 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
5078 case RTL_FW_PHY_START:
5081 key = (struct fw_phy_patch_key *)block;
5082 key_addr = __le16_to_cpu(key->key_reg);
5083 rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut);
5085 case RTL_FW_PHY_STOP:
5089 rtl_post_ram_code(tp, key_addr, !power_cut);
5092 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
5094 case RTL_FW_PHY_VER:
5095 patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block);
5097 case RTL_FW_PHY_UNION_NC:
5098 case RTL_FW_PHY_UNION_NC1:
5099 case RTL_FW_PHY_UNION_NC2:
5100 case RTL_FW_PHY_UNION_UC2:
5101 case RTL_FW_PHY_UNION_UC:
5102 case RTL_FW_PHY_UNION_MISC:
5104 rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block);
5106 case RTL_FW_PHY_FIXUP:
5108 rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block);
5110 case RTL_FW_PHY_SPEED_UP:
5111 rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut);
5117 i += ALIGN(__le32_to_cpu(block->length), 8);
5121 if (rtl_fw->post_fw)
5122 rtl_fw->post_fw(tp);
5124 rtl_reset_ocp_base(tp);
5125 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
5126 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
5129 static void rtl8152_release_firmware(struct r8152 *tp)
5131 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5133 if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
5134 release_firmware(rtl_fw->fw);
5139 static int rtl8152_request_firmware(struct r8152 *tp)
5141 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5144 if (rtl_fw->fw || !rtl_fw->fw_name) {
5145 dev_info(&tp->intf->dev, "skip request firmware\n");
5150 rc = reject_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
5154 rc = rtl8152_check_firmware(tp, rtl_fw);
5156 release_firmware(rtl_fw->fw);
5160 rtl_fw->fw = ERR_PTR(rc);
5162 dev_warn(&tp->intf->dev,
5163 "unable to load firmware patch %s (%ld)\n",
5164 rtl_fw->fw_name, rc);
5170 static void r8152_aldps_en(struct r8152 *tp, bool enable)
5173 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
5174 LINKENA | DIS_SDSAVE);
5176 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
5182 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
5184 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
5185 ocp_reg_write(tp, OCP_EEE_DATA, reg);
5186 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
5189 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
5193 r8152_mmd_indirect(tp, dev, reg);
5194 data = ocp_reg_read(tp, OCP_EEE_DATA);
5195 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5200 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
5202 r8152_mmd_indirect(tp, dev, reg);
5203 ocp_reg_write(tp, OCP_EEE_DATA, data);
5204 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5207 static void r8152_eee_en(struct r8152 *tp, bool enable)
5209 u16 config1, config2, config3;
5212 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5213 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
5214 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
5215 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
5218 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5219 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
5220 config1 |= sd_rise_time(1);
5221 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
5222 config3 |= fast_snr(42);
5224 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5225 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
5227 config1 |= sd_rise_time(7);
5228 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
5229 config3 |= fast_snr(511);
5232 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5233 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
5234 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
5235 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
5238 static void r8153_eee_en(struct r8152 *tp, bool enable)
5243 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5244 config = ocp_reg_read(tp, OCP_EEE_CFG);
5247 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5250 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5251 config &= ~EEE10_EN;
5254 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5255 ocp_reg_write(tp, OCP_EEE_CFG, config);
5257 tp->ups_info.eee = enable;
5260 static void r8156_eee_en(struct r8152 *tp, bool enable)
5264 r8153_eee_en(tp, enable);
5266 config = ocp_reg_read(tp, OCP_EEE_ADV2);
5269 config |= MDIO_EEE_2_5GT;
5271 config &= ~MDIO_EEE_2_5GT;
5273 ocp_reg_write(tp, OCP_EEE_ADV2, config);
5276 static void rtl_eee_enable(struct r8152 *tp, bool enable)
5278 switch (tp->version) {
5283 r8152_eee_en(tp, true);
5284 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
5287 r8152_eee_en(tp, false);
5288 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
5299 r8153_eee_en(tp, true);
5300 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5302 r8153_eee_en(tp, false);
5303 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5312 r8156_eee_en(tp, true);
5313 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5315 r8156_eee_en(tp, false);
5316 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5324 static void r8152b_enable_fc(struct r8152 *tp)
5328 anar = r8152_mdio_read(tp, MII_ADVERTISE);
5329 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
5330 r8152_mdio_write(tp, MII_ADVERTISE, anar);
5332 tp->ups_info.flow_control = true;
5335 static void rtl8152_disable(struct r8152 *tp)
5337 r8152_aldps_en(tp, false);
5339 r8152_aldps_en(tp, true);
5342 static void r8152b_hw_phy_cfg(struct r8152 *tp)
5344 rtl8152_apply_firmware(tp, false);
5345 rtl_eee_enable(tp, tp->eee_en);
5346 r8152_aldps_en(tp, true);
5347 r8152b_enable_fc(tp);
5349 set_bit(PHY_RESET, &tp->flags);
5352 static void wait_oob_link_list_ready(struct r8152 *tp)
5357 for (i = 0; i < 1000; i++) {
5358 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5359 if (ocp_data & LINK_LIST_READY)
5361 usleep_range(1000, 2000);
5365 static void r8156b_wait_loading_flash(struct r8152 *tp)
5367 if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) &&
5368 !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) {
5371 for (i = 0; i < 100; i++) {
5372 if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE)
5374 usleep_range(1000, 2000);
5379 static void r8152b_exit_oob(struct r8152 *tp)
5383 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5384 ocp_data &= ~RCR_ACPT_ALL;
5385 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5387 rxdy_gated_en(tp, true);
5388 r8153_teredo_off(tp);
5389 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
5390 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
5392 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5393 ocp_data &= ~NOW_IS_OOB;
5394 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5396 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5397 ocp_data &= ~MCU_BORW_EN;
5398 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5400 wait_oob_link_list_ready(tp);
5402 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5403 ocp_data |= RE_INIT_LL;
5404 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5406 wait_oob_link_list_ready(tp);
5408 rtl8152_nic_reset(tp);
5410 /* rx share fifo credit full threshold */
5411 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5413 if (tp->udev->speed == USB_SPEED_FULL ||
5414 tp->udev->speed == USB_SPEED_LOW) {
5415 /* rx share fifo credit near full threshold */
5416 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5418 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5421 /* rx share fifo credit near full threshold */
5422 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5424 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5428 /* TX share fifo free credit full threshold */
5429 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
5431 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
5432 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
5433 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
5434 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
5436 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
5438 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5440 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5441 ocp_data |= TCR0_AUTO_FIFO;
5442 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5445 static void r8152b_enter_oob(struct r8152 *tp)
5449 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5450 ocp_data &= ~NOW_IS_OOB;
5451 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5453 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
5454 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
5455 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
5459 wait_oob_link_list_ready(tp);
5461 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5462 ocp_data |= RE_INIT_LL;
5463 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5465 wait_oob_link_list_ready(tp);
5467 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5469 rtl_rx_vlan_en(tp, true);
5471 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
5472 ocp_data |= ALDPS_PROXY_MODE;
5473 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
5475 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5476 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5477 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5479 rxdy_gated_en(tp, false);
5481 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5482 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5483 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5486 static int r8153_pre_firmware_1(struct r8152 *tp)
5490 /* Wait till the WTD timer is ready. It would take at most 104 ms. */
5491 for (i = 0; i < 104; i++) {
5492 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
5494 if (!(ocp_data & WTD1_EN))
5496 usleep_range(1000, 2000);
5502 static int r8153_post_firmware_1(struct r8152 *tp)
5504 /* set USB_BP_4 to support USB_SPEED_SUPER only */
5505 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
5506 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
5508 /* reset UPHY timer to 36 ms */
5509 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5514 static int r8153_pre_firmware_2(struct r8152 *tp)
5518 r8153_pre_firmware_1(tp);
5520 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5521 ocp_data &= ~FW_FIX_SUSPEND;
5522 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5527 static int r8153_post_firmware_2(struct r8152 *tp)
5531 /* enable bp0 if support USB_SPEED_SUPER only */
5532 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
5533 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5535 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5538 /* reset UPHY timer to 36 ms */
5539 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5541 /* enable U3P3 check, set the counter to 4 */
5542 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
5544 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5545 ocp_data |= FW_FIX_SUSPEND;
5546 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5548 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5549 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5550 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5555 static int r8153_post_firmware_3(struct r8152 *tp)
5559 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5560 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5561 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5563 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5564 ocp_data |= FW_IP_RESET_EN;
5565 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5570 static int r8153b_pre_firmware_1(struct r8152 *tp)
5572 /* enable fc timer and set timer to 1 second. */
5573 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
5574 CTRL_TIMER_EN | (1000 / 8));
5579 static int r8153b_post_firmware_1(struct r8152 *tp)
5583 /* enable bp0 for RTL8153-BND */
5584 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
5585 if (ocp_data & BND_MASK) {
5586 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5588 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5591 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5592 ocp_data |= FLOW_CTRL_PATCH_OPT;
5593 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5595 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5596 ocp_data |= FC_PATCH_TASK;
5597 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5599 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5600 ocp_data |= FW_IP_RESET_EN;
5601 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5606 static int r8153c_post_firmware_1(struct r8152 *tp)
5610 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5611 ocp_data |= FLOW_CTRL_PATCH_2;
5612 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5614 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5615 ocp_data |= FC_PATCH_TASK;
5616 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5621 static int r8156a_post_firmware_1(struct r8152 *tp)
5625 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5626 ocp_data |= FW_IP_RESET_EN;
5627 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5629 /* Modify U3PHY parameter for compatibility issue */
5630 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e);
5631 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9);
5636 static void r8153_aldps_en(struct r8152 *tp, bool enable)
5640 data = ocp_reg_read(tp, OCP_POWER_CFG);
5643 ocp_reg_write(tp, OCP_POWER_CFG, data);
5648 ocp_reg_write(tp, OCP_POWER_CFG, data);
5649 for (i = 0; i < 20; i++) {
5650 usleep_range(1000, 2000);
5651 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
5656 tp->ups_info.aldps = enable;
5659 static void r8153_hw_phy_cfg(struct r8152 *tp)
5664 /* disable ALDPS before updating the PHY parameters */
5665 r8153_aldps_en(tp, false);
5667 /* disable EEE before updating the PHY parameters */
5668 rtl_eee_enable(tp, false);
5670 rtl8152_apply_firmware(tp, false);
5672 if (tp->version == RTL_VER_03) {
5673 data = ocp_reg_read(tp, OCP_EEE_CFG);
5674 data &= ~CTAP_SHORT_EN;
5675 ocp_reg_write(tp, OCP_EEE_CFG, data);
5678 data = ocp_reg_read(tp, OCP_POWER_CFG);
5679 data |= EEE_CLKDIV_EN;
5680 ocp_reg_write(tp, OCP_POWER_CFG, data);
5682 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5683 data |= EN_10M_BGOFF;
5684 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5685 data = ocp_reg_read(tp, OCP_POWER_CFG);
5686 data |= EN_10M_PLLOFF;
5687 ocp_reg_write(tp, OCP_POWER_CFG, data);
5688 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
5690 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5691 ocp_data |= PFM_PWM_SWITCH;
5692 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5694 /* Enable LPF corner auto tune */
5695 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
5697 /* Adjust 10M Amplitude */
5698 sram_write(tp, SRAM_10M_AMP1, 0x00af);
5699 sram_write(tp, SRAM_10M_AMP2, 0x0208);
5702 rtl_eee_enable(tp, true);
5704 r8153_aldps_en(tp, true);
5705 r8152b_enable_fc(tp);
5707 switch (tp->version) {
5714 r8153_u2p3en(tp, true);
5718 set_bit(PHY_RESET, &tp->flags);
5721 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
5725 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
5726 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
5727 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
5728 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
5733 static void r8153b_hw_phy_cfg(struct r8152 *tp)
5738 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
5739 if (ocp_data & PCUT_STATUS) {
5740 ocp_data &= ~PCUT_STATUS;
5741 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
5744 /* disable ALDPS before updating the PHY parameters */
5745 r8153_aldps_en(tp, false);
5747 /* disable EEE before updating the PHY parameters */
5748 rtl_eee_enable(tp, false);
5750 /* U1/U2/L1 idle timer. 500 us */
5751 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5753 data = r8153_phy_status(tp, 0);
5756 case PHY_STAT_PWRDN:
5757 case PHY_STAT_EXT_INIT:
5758 rtl8152_apply_firmware(tp, true);
5760 data = r8152_mdio_read(tp, MII_BMCR);
5761 data &= ~BMCR_PDOWN;
5762 r8152_mdio_write(tp, MII_BMCR, data);
5764 case PHY_STAT_LAN_ON:
5766 rtl8152_apply_firmware(tp, false);
5770 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
5772 data = sram_read(tp, SRAM_GREEN_CFG);
5774 sram_write(tp, SRAM_GREEN_CFG, data);
5775 data = ocp_reg_read(tp, OCP_NCTL_CFG);
5776 data |= PGA_RETURN_EN;
5777 ocp_reg_write(tp, OCP_NCTL_CFG, data);
5779 /* ADC Bias Calibration:
5780 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
5781 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
5784 ocp_data = r8152_efuse_read(tp, 0x7d);
5785 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
5787 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
5789 /* ups mode tx-link-pulse timing adjustment:
5790 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
5791 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
5793 ocp_data = ocp_reg_read(tp, 0xc426);
5796 u32 swr_cnt_1ms_ini;
5798 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
5799 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
5800 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
5801 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
5804 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5805 ocp_data |= PFM_PWM_SWITCH;
5806 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5809 if (!rtl_phy_patch_request(tp, true, true)) {
5810 data = ocp_reg_read(tp, OCP_POWER_CFG);
5811 data |= EEE_CLKDIV_EN;
5812 ocp_reg_write(tp, OCP_POWER_CFG, data);
5813 tp->ups_info.eee_ckdiv = true;
5815 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5816 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
5817 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5818 tp->ups_info.eee_cmod_lv = true;
5819 tp->ups_info._10m_ckdiv = true;
5820 tp->ups_info.eee_plloff_giga = true;
5822 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
5823 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
5824 tp->ups_info._250m_ckdiv = true;
5826 rtl_phy_patch_request(tp, false, true);
5830 rtl_eee_enable(tp, true);
5832 r8153_aldps_en(tp, true);
5833 r8152b_enable_fc(tp);
5835 set_bit(PHY_RESET, &tp->flags);
5838 static void r8153c_hw_phy_cfg(struct r8152 *tp)
5840 r8153b_hw_phy_cfg(tp);
5842 tp->ups_info.r_tune = true;
5845 static void rtl8153_change_mtu(struct r8152 *tp)
5847 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
5848 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
5851 static void r8153_first_init(struct r8152 *tp)
5855 rxdy_gated_en(tp, true);
5856 r8153_teredo_off(tp);
5858 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5859 ocp_data &= ~RCR_ACPT_ALL;
5860 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5862 rtl8152_nic_reset(tp);
5865 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5866 ocp_data &= ~NOW_IS_OOB;
5867 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5869 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5870 ocp_data &= ~MCU_BORW_EN;
5871 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5873 wait_oob_link_list_ready(tp);
5875 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5876 ocp_data |= RE_INIT_LL;
5877 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5879 wait_oob_link_list_ready(tp);
5881 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
5883 rtl8153_change_mtu(tp);
5885 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5886 ocp_data |= TCR0_AUTO_FIFO;
5887 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5889 rtl8152_nic_reset(tp);
5891 /* rx share fifo credit full threshold */
5892 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5893 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
5894 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
5895 /* TX share fifo free credit full threshold */
5896 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
5899 static void r8153_enter_oob(struct r8152 *tp)
5903 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5904 ocp_data &= ~NOW_IS_OOB;
5905 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5910 wait_oob_link_list_ready(tp);
5912 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5913 ocp_data |= RE_INIT_LL;
5914 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5916 wait_oob_link_list_ready(tp);
5918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
5920 switch (tp->version) {
5925 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
5926 ocp_data &= ~TEREDO_WAKE_MASK;
5927 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
5933 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
5934 * type. Set it to zero. bits[7:0] are the W1C bits about
5935 * the events. Set them to all 1 to clear them.
5937 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
5944 rtl_rx_vlan_en(tp, true);
5946 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
5947 ocp_data |= ALDPS_PROXY_MODE;
5948 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
5950 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5951 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5952 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5954 rxdy_gated_en(tp, false);
5956 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5957 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5958 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5961 static void rtl8153_disable(struct r8152 *tp)
5963 r8153_aldps_en(tp, false);
5966 r8153_aldps_en(tp, true);
5969 static int rtl8156_enable(struct r8152 *tp)
5974 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5978 rtl_set_eee_plus(tp);
5979 r8153_set_rx_early_timeout(tp);
5980 r8153_set_rx_early_size(tp);
5982 speed = rtl8152_get_speed(tp);
5983 rtl_set_ifg(tp, speed);
5985 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
5986 if (speed & _2500bps)
5987 ocp_data &= ~IDLE_SPDWN_EN;
5989 ocp_data |= IDLE_SPDWN_EN;
5990 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
5992 if (speed & _1000bps)
5993 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
5994 else if (speed & _500bps)
5995 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
5997 if (tp->udev->speed == USB_SPEED_HIGH) {
5998 /* USB 0xb45e[3:0] l1_nyet_hird */
5999 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6001 if (is_flow_control(speed))
6005 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6008 return rtl_enable(tp);
6011 static int rtl8156b_enable(struct r8152 *tp)
6016 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6020 rtl_set_eee_plus(tp);
6022 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM);
6023 ocp_data &= ~RX_AGGR_NUM_MASK;
6024 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data);
6026 r8153_set_rx_early_timeout(tp);
6027 r8153_set_rx_early_size(tp);
6029 speed = rtl8152_get_speed(tp);
6030 rtl_set_ifg(tp, speed);
6032 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
6033 if (speed & _2500bps)
6034 ocp_data &= ~IDLE_SPDWN_EN;
6036 ocp_data |= IDLE_SPDWN_EN;
6037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
6039 if (tp->udev->speed == USB_SPEED_HIGH) {
6040 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6042 if (is_flow_control(speed))
6046 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6049 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
6050 ocp_data &= ~FC_PATCH_TASK;
6051 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6052 usleep_range(1000, 2000);
6053 ocp_data |= FC_PATCH_TASK;
6054 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6056 return rtl_enable(tp);
6059 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
6065 if (autoneg == AUTONEG_DISABLE) {
6066 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
6071 bmcr = BMCR_SPEED10;
6072 if (duplex == DUPLEX_FULL) {
6073 bmcr |= BMCR_FULLDPLX;
6074 tp->ups_info.speed_duplex = FORCE_10M_FULL;
6076 tp->ups_info.speed_duplex = FORCE_10M_HALF;
6080 bmcr = BMCR_SPEED100;
6081 if (duplex == DUPLEX_FULL) {
6082 bmcr |= BMCR_FULLDPLX;
6083 tp->ups_info.speed_duplex = FORCE_100M_FULL;
6085 tp->ups_info.speed_duplex = FORCE_100M_HALF;
6089 if (tp->mii.supports_gmii) {
6090 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
6091 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6100 if (duplex == DUPLEX_FULL)
6101 tp->mii.full_duplex = 1;
6103 tp->mii.full_duplex = 0;
6105 tp->mii.force_media = 1;
6110 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6111 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6113 if (tp->mii.supports_gmii) {
6114 support |= RTL_ADVERTISED_1000_FULL;
6116 if (tp->support_2500full)
6117 support |= RTL_ADVERTISED_2500_FULL;
6120 if (!(advertising & support))
6123 orig = r8152_mdio_read(tp, MII_ADVERTISE);
6124 new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
6125 ADVERTISE_100HALF | ADVERTISE_100FULL);
6126 if (advertising & RTL_ADVERTISED_10_HALF) {
6127 new1 |= ADVERTISE_10HALF;
6128 tp->ups_info.speed_duplex = NWAY_10M_HALF;
6130 if (advertising & RTL_ADVERTISED_10_FULL) {
6131 new1 |= ADVERTISE_10FULL;
6132 tp->ups_info.speed_duplex = NWAY_10M_FULL;
6135 if (advertising & RTL_ADVERTISED_100_HALF) {
6136 new1 |= ADVERTISE_100HALF;
6137 tp->ups_info.speed_duplex = NWAY_100M_HALF;
6139 if (advertising & RTL_ADVERTISED_100_FULL) {
6140 new1 |= ADVERTISE_100FULL;
6141 tp->ups_info.speed_duplex = NWAY_100M_FULL;
6145 r8152_mdio_write(tp, MII_ADVERTISE, new1);
6146 tp->mii.advertising = new1;
6149 if (tp->mii.supports_gmii) {
6150 orig = r8152_mdio_read(tp, MII_CTRL1000);
6151 new1 = orig & ~(ADVERTISE_1000FULL |
6152 ADVERTISE_1000HALF);
6154 if (advertising & RTL_ADVERTISED_1000_FULL) {
6155 new1 |= ADVERTISE_1000FULL;
6156 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6160 r8152_mdio_write(tp, MII_CTRL1000, new1);
6163 if (tp->support_2500full) {
6164 orig = ocp_reg_read(tp, OCP_10GBT_CTRL);
6165 new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G;
6167 if (advertising & RTL_ADVERTISED_2500_FULL) {
6168 new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G;
6169 tp->ups_info.speed_duplex = NWAY_2500M_FULL;
6173 ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
6176 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
6178 tp->mii.force_media = 0;
6181 if (test_and_clear_bit(PHY_RESET, &tp->flags))
6184 r8152_mdio_write(tp, MII_BMCR, bmcr);
6186 if (bmcr & BMCR_RESET) {
6189 for (i = 0; i < 50; i++) {
6191 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
6200 static void rtl8152_up(struct r8152 *tp)
6202 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6205 r8152_aldps_en(tp, false);
6206 r8152b_exit_oob(tp);
6207 r8152_aldps_en(tp, true);
6210 static void rtl8152_down(struct r8152 *tp)
6212 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6213 rtl_drop_queued_tx(tp);
6217 r8152_power_cut_en(tp, false);
6218 r8152_aldps_en(tp, false);
6219 r8152b_enter_oob(tp);
6220 r8152_aldps_en(tp, true);
6223 static void rtl8153_up(struct r8152 *tp)
6227 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6230 r8153_u1u2en(tp, false);
6231 r8153_u2p3en(tp, false);
6232 r8153_aldps_en(tp, false);
6233 r8153_first_init(tp);
6235 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6236 ocp_data |= LANWAKE_CLR_EN;
6237 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6239 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
6240 ocp_data &= ~LANWAKE_PIN;
6241 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
6243 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
6244 ocp_data &= ~DELAY_PHY_PWR_CHG;
6245 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
6247 r8153_aldps_en(tp, true);
6249 switch (tp->version) {
6256 r8153_u2p3en(tp, true);
6260 r8153_u1u2en(tp, true);
6263 static void rtl8153_down(struct r8152 *tp)
6267 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6268 rtl_drop_queued_tx(tp);
6272 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6273 ocp_data &= ~LANWAKE_CLR_EN;
6274 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6276 r8153_u1u2en(tp, false);
6277 r8153_u2p3en(tp, false);
6278 r8153_power_cut_en(tp, false);
6279 r8153_aldps_en(tp, false);
6280 r8153_enter_oob(tp);
6281 r8153_aldps_en(tp, true);
6284 static void rtl8153b_up(struct r8152 *tp)
6288 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6291 r8153b_u1u2en(tp, false);
6292 r8153_u2p3en(tp, false);
6293 r8153_aldps_en(tp, false);
6295 r8153_first_init(tp);
6296 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6298 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6299 ocp_data &= ~PLA_MCU_SPDWN_EN;
6300 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6302 r8153_aldps_en(tp, true);
6304 if (tp->udev->speed >= USB_SPEED_SUPER)
6305 r8153b_u1u2en(tp, true);
6308 static void rtl8153b_down(struct r8152 *tp)
6312 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6313 rtl_drop_queued_tx(tp);
6317 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6318 ocp_data |= PLA_MCU_SPDWN_EN;
6319 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6321 r8153b_u1u2en(tp, false);
6322 r8153_u2p3en(tp, false);
6323 r8153b_power_cut_en(tp, false);
6324 r8153_aldps_en(tp, false);
6325 r8153_enter_oob(tp);
6326 r8153_aldps_en(tp, true);
6329 static void rtl8153c_change_mtu(struct r8152 *tp)
6331 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
6332 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
6334 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6336 /* Adjust the tx fifo free credit full threshold, otherwise
6337 * the fifo would be too small to send a jumbo frame packet.
6339 if (tp->netdev->mtu < 8000)
6340 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8);
6342 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8);
6345 static void rtl8153c_up(struct r8152 *tp)
6349 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6352 r8153b_u1u2en(tp, false);
6353 r8153_u2p3en(tp, false);
6354 r8153_aldps_en(tp, false);
6356 rxdy_gated_en(tp, true);
6357 r8153_teredo_off(tp);
6359 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6360 ocp_data &= ~RCR_ACPT_ALL;
6361 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6363 rtl8152_nic_reset(tp);
6366 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6367 ocp_data &= ~NOW_IS_OOB;
6368 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6370 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6371 ocp_data &= ~MCU_BORW_EN;
6372 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6374 wait_oob_link_list_ready(tp);
6376 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6377 ocp_data |= RE_INIT_LL;
6378 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6380 wait_oob_link_list_ready(tp);
6382 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6384 rtl8153c_change_mtu(tp);
6386 rtl8152_nic_reset(tp);
6388 /* rx share fifo credit full threshold */
6389 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02);
6390 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08);
6391 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
6392 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
6394 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6396 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
6398 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
6400 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
6402 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
6404 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6405 ocp_data &= ~PLA_MCU_SPDWN_EN;
6406 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6408 r8153_aldps_en(tp, true);
6409 r8153b_u1u2en(tp, true);
6412 static inline u32 fc_pause_on_auto(struct r8152 *tp)
6414 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024);
6417 static inline u32 fc_pause_off_auto(struct r8152 *tp)
6419 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024);
6422 static void r8156_fc_parameter(struct r8152 *tp)
6424 u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp);
6425 u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp);
6427 switch (tp->version) {
6430 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 8);
6431 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 8);
6436 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16);
6437 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16);
6444 static void rtl8156_change_mtu(struct r8152 *tp)
6446 u32 rx_max_size = mtu_to_size(tp->netdev->mtu);
6448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size);
6449 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
6450 r8156_fc_parameter(tp);
6452 /* TX share fifo free credit full threshold */
6453 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6454 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
6455 ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16);
6458 static void rtl8156_up(struct r8152 *tp)
6462 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6465 r8153b_u1u2en(tp, false);
6466 r8153_u2p3en(tp, false);
6467 r8153_aldps_en(tp, false);
6469 rxdy_gated_en(tp, true);
6470 r8153_teredo_off(tp);
6472 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6473 ocp_data &= ~RCR_ACPT_ALL;
6474 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6476 rtl8152_nic_reset(tp);
6479 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6480 ocp_data &= ~NOW_IS_OOB;
6481 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6483 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6484 ocp_data &= ~MCU_BORW_EN;
6485 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6487 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6489 rtl8156_change_mtu(tp);
6491 switch (tp->version) {
6495 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
6496 ocp_data |= ACT_ODMA;
6497 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
6503 /* share FIFO settings */
6504 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL);
6505 ocp_data &= ~RXFIFO_FULL_MASK;
6507 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data);
6509 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6510 ocp_data &= ~PLA_MCU_SPDWN_EN;
6511 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6513 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION);
6514 ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF);
6515 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data);
6517 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
6519 if (tp->saved_wolopts != __rtl_get_wol(tp)) {
6520 netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n");
6521 __rtl_set_wol(tp, tp->saved_wolopts);
6524 r8153_aldps_en(tp, true);
6525 r8153_u2p3en(tp, true);
6527 if (tp->udev->speed >= USB_SPEED_SUPER)
6528 r8153b_u1u2en(tp, true);
6531 static void rtl8156_down(struct r8152 *tp)
6535 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6536 rtl_drop_queued_tx(tp);
6540 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6541 ocp_data |= PLA_MCU_SPDWN_EN;
6542 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6544 r8153b_u1u2en(tp, false);
6545 r8153_u2p3en(tp, false);
6546 r8153b_power_cut_en(tp, false);
6547 r8153_aldps_en(tp, false);
6549 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6550 ocp_data &= ~NOW_IS_OOB;
6551 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6556 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
6557 * type. Set it to zero. bits[7:0] are the W1C bits about
6558 * the events. Set them to all 1 to clear them.
6560 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
6562 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6563 ocp_data |= NOW_IS_OOB;
6564 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6566 rtl_rx_vlan_en(tp, true);
6567 rxdy_gated_en(tp, false);
6569 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6570 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
6571 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6573 r8153_aldps_en(tp, true);
6576 static bool rtl8152_in_nway(struct r8152 *tp)
6580 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
6581 tp->ocp_base = 0x2000;
6582 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
6583 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
6585 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
6586 if (nway_state & 0xc000)
6592 static bool rtl8153_in_nway(struct r8152 *tp)
6594 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
6596 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
6602 static void r8156_mdio_force_mode(struct r8152 *tp)
6606 /* Select force mode through 0xa5b4 bit 15
6607 * 0: MDIO force mode
6610 data = ocp_reg_read(tp, 0xa5b4);
6611 if (data & BIT(15)) {
6613 ocp_reg_write(tp, 0xa5b4, data);
6617 static void set_carrier(struct r8152 *tp)
6619 struct net_device *netdev = tp->netdev;
6620 struct napi_struct *napi = &tp->napi;
6623 speed = rtl8152_get_speed(tp);
6625 if (speed & LINK_STATUS) {
6626 if (!netif_carrier_ok(netdev)) {
6627 tp->rtl_ops.enable(tp);
6628 netif_stop_queue(netdev);
6630 netif_carrier_on(netdev);
6632 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6633 _rtl8152_set_rx_mode(netdev);
6635 netif_wake_queue(netdev);
6636 netif_info(tp, link, netdev, "carrier on\n");
6637 } else if (netif_queue_stopped(netdev) &&
6638 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
6639 netif_wake_queue(netdev);
6642 if (netif_carrier_ok(netdev)) {
6643 netif_carrier_off(netdev);
6644 tasklet_disable(&tp->tx_tl);
6646 tp->rtl_ops.disable(tp);
6648 tasklet_enable(&tp->tx_tl);
6649 netif_info(tp, link, netdev, "carrier off\n");
6654 static void rtl_work_func_t(struct work_struct *work)
6656 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
6658 /* If the device is unplugged or !netif_running(), the workqueue
6659 * doesn't need to wake the device, and could return directly.
6661 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
6664 if (usb_autopm_get_interface(tp->intf) < 0)
6667 if (!test_bit(WORK_ENABLE, &tp->flags))
6670 if (!mutex_trylock(&tp->control)) {
6671 schedule_delayed_work(&tp->schedule, 0);
6675 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
6678 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
6679 _rtl8152_set_rx_mode(tp->netdev);
6681 /* don't schedule tasket before linking */
6682 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
6683 netif_carrier_ok(tp->netdev))
6684 tasklet_schedule(&tp->tx_tl);
6686 if (test_and_clear_bit(RX_EPROTO, &tp->flags) &&
6687 !list_empty(&tp->rx_done))
6688 napi_schedule(&tp->napi);
6690 mutex_unlock(&tp->control);
6693 usb_autopm_put_interface(tp->intf);
6696 static void rtl_hw_phy_work_func_t(struct work_struct *work)
6698 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
6700 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6703 if (usb_autopm_get_interface(tp->intf) < 0)
6706 mutex_lock(&tp->control);
6708 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
6709 tp->rtl_fw.retry = false;
6710 tp->rtl_fw.fw = NULL;
6712 /* Delay execution in case reject_firmware() is not ready yet.
6714 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
6718 tp->rtl_ops.hw_phy_cfg(tp);
6720 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
6724 mutex_unlock(&tp->control);
6726 usb_autopm_put_interface(tp->intf);
6729 #ifdef CONFIG_PM_SLEEP
6730 static int rtl_notifier(struct notifier_block *nb, unsigned long action,
6733 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
6736 case PM_HIBERNATION_PREPARE:
6737 case PM_SUSPEND_PREPARE:
6738 usb_autopm_get_interface(tp->intf);
6741 case PM_POST_HIBERNATION:
6742 case PM_POST_SUSPEND:
6743 usb_autopm_put_interface(tp->intf);
6746 case PM_POST_RESTORE:
6747 case PM_RESTORE_PREPARE:
6756 static int rtl8152_open(struct net_device *netdev)
6758 struct r8152 *tp = netdev_priv(netdev);
6761 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
6762 cancel_delayed_work_sync(&tp->hw_phy_work);
6763 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
6766 res = alloc_all_mem(tp);
6770 res = usb_autopm_get_interface(tp->intf);
6774 mutex_lock(&tp->control);
6778 netif_carrier_off(netdev);
6779 netif_start_queue(netdev);
6780 set_bit(WORK_ENABLE, &tp->flags);
6782 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
6785 netif_device_detach(tp->netdev);
6786 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
6790 napi_enable(&tp->napi);
6791 tasklet_enable(&tp->tx_tl);
6793 mutex_unlock(&tp->control);
6795 usb_autopm_put_interface(tp->intf);
6796 #ifdef CONFIG_PM_SLEEP
6797 tp->pm_notifier.notifier_call = rtl_notifier;
6798 register_pm_notifier(&tp->pm_notifier);
6803 mutex_unlock(&tp->control);
6804 usb_autopm_put_interface(tp->intf);
6811 static int rtl8152_close(struct net_device *netdev)
6813 struct r8152 *tp = netdev_priv(netdev);
6816 #ifdef CONFIG_PM_SLEEP
6817 unregister_pm_notifier(&tp->pm_notifier);
6819 tasklet_disable(&tp->tx_tl);
6820 clear_bit(WORK_ENABLE, &tp->flags);
6821 usb_kill_urb(tp->intr_urb);
6822 cancel_delayed_work_sync(&tp->schedule);
6823 napi_disable(&tp->napi);
6824 netif_stop_queue(netdev);
6826 res = usb_autopm_get_interface(tp->intf);
6827 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
6828 rtl_drop_queued_tx(tp);
6831 mutex_lock(&tp->control);
6833 tp->rtl_ops.down(tp);
6835 mutex_unlock(&tp->control);
6839 usb_autopm_put_interface(tp->intf);
6846 static void rtl_tally_reset(struct r8152 *tp)
6850 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
6851 ocp_data |= TALLY_RESET;
6852 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
6855 static void r8152b_init(struct r8152 *tp)
6860 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6863 data = r8152_mdio_read(tp, MII_BMCR);
6864 if (data & BMCR_PDOWN) {
6865 data &= ~BMCR_PDOWN;
6866 r8152_mdio_write(tp, MII_BMCR, data);
6869 r8152_aldps_en(tp, false);
6871 if (tp->version == RTL_VER_01) {
6872 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
6873 ocp_data &= ~LED_MODE_MASK;
6874 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
6877 r8152_power_cut_en(tp, false);
6879 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
6880 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
6881 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
6882 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
6883 ocp_data &= ~MCU_CLK_RATIO_MASK;
6884 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
6885 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
6886 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
6887 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
6888 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
6890 rtl_tally_reset(tp);
6892 /* enable rx aggregation */
6893 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
6894 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
6895 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
6898 static void r8153_init(struct r8152 *tp)
6904 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6907 r8153_u1u2en(tp, false);
6909 for (i = 0; i < 500; i++) {
6910 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
6915 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6919 data = r8153_phy_status(tp, 0);
6921 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
6922 tp->version == RTL_VER_05)
6923 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
6925 data = r8152_mdio_read(tp, MII_BMCR);
6926 if (data & BMCR_PDOWN) {
6927 data &= ~BMCR_PDOWN;
6928 r8152_mdio_write(tp, MII_BMCR, data);
6931 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
6933 r8153_u2p3en(tp, false);
6935 if (tp->version == RTL_VER_04) {
6936 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
6937 ocp_data &= ~pwd_dn_scale_mask;
6938 ocp_data |= pwd_dn_scale(96);
6939 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
6941 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
6942 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
6943 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
6944 } else if (tp->version == RTL_VER_05) {
6945 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
6946 ocp_data &= ~ECM_ALDPS;
6947 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
6949 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
6950 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
6951 ocp_data &= ~DYNAMIC_BURST;
6953 ocp_data |= DYNAMIC_BURST;
6954 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
6955 } else if (tp->version == RTL_VER_06) {
6956 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
6957 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
6958 ocp_data &= ~DYNAMIC_BURST;
6960 ocp_data |= DYNAMIC_BURST;
6961 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
6963 r8153_queue_wake(tp, false);
6965 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
6966 if (rtl8152_get_speed(tp) & LINK_STATUS)
6967 ocp_data |= CUR_LINK_OK;
6969 ocp_data &= ~CUR_LINK_OK;
6970 ocp_data |= POLL_LINK_CHG;
6971 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
6974 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
6975 ocp_data |= EP4_FULL_FC;
6976 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
6978 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
6979 ocp_data &= ~TIMER11_EN;
6980 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
6982 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
6983 ocp_data &= ~LED_MODE_MASK;
6984 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
6986 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
6987 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
6988 ocp_data |= LPM_TIMER_500MS;
6990 ocp_data |= LPM_TIMER_500US;
6991 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
6993 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
6994 ocp_data &= ~SEN_VAL_MASK;
6995 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
6996 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
6998 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
7000 r8153_power_cut_en(tp, false);
7001 rtl_runtime_suspend_enable(tp, false);
7002 r8153_mac_clk_speed_down(tp, false);
7003 r8153_u1u2en(tp, true);
7004 usb_enable_lpm(tp->udev);
7006 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
7007 ocp_data |= LANWAKE_CLR_EN;
7008 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
7010 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
7011 ocp_data &= ~LANWAKE_PIN;
7012 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
7014 /* rx aggregation */
7015 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7016 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7017 if (tp->dell_tb_rx_agg_bug)
7018 ocp_data |= RX_AGG_DISABLE;
7020 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7022 rtl_tally_reset(tp);
7024 switch (tp->udev->speed) {
7025 case USB_SPEED_SUPER:
7026 case USB_SPEED_SUPER_PLUS:
7027 tp->coalesce = COALESCE_SUPER;
7029 case USB_SPEED_HIGH:
7030 tp->coalesce = COALESCE_HIGH;
7033 tp->coalesce = COALESCE_SLOW;
7038 static void r8153b_init(struct r8152 *tp)
7044 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7047 r8153b_u1u2en(tp, false);
7049 for (i = 0; i < 500; i++) {
7050 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7055 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7059 data = r8153_phy_status(tp, 0);
7061 data = r8152_mdio_read(tp, MII_BMCR);
7062 if (data & BMCR_PDOWN) {
7063 data &= ~BMCR_PDOWN;
7064 r8152_mdio_write(tp, MII_BMCR, data);
7067 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7069 r8153_u2p3en(tp, false);
7071 /* MSC timer = 0xfff * 8ms = 32760 ms */
7072 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7074 r8153b_power_cut_en(tp, false);
7075 r8153b_ups_en(tp, false);
7076 r8153_queue_wake(tp, false);
7077 rtl_runtime_suspend_enable(tp, false);
7079 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7080 if (rtl8152_get_speed(tp) & LINK_STATUS)
7081 ocp_data |= CUR_LINK_OK;
7083 ocp_data &= ~CUR_LINK_OK;
7084 ocp_data |= POLL_LINK_CHG;
7085 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7087 if (tp->udev->speed >= USB_SPEED_SUPER)
7088 r8153b_u1u2en(tp, true);
7090 usb_enable_lpm(tp->udev);
7092 /* MAC clock speed down */
7093 r8153_mac_clk_speed_down(tp, true);
7095 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
7096 ocp_data &= ~PLA_MCU_SPDWN_EN;
7097 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
7099 if (tp->version == RTL_VER_09) {
7100 /* Disable Test IO for 32QFN */
7101 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
7102 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7103 ocp_data |= TEST_IO_OFF;
7104 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7108 set_bit(GREEN_ETHERNET, &tp->flags);
7110 /* rx aggregation */
7111 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7112 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7113 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7115 rtl_tally_reset(tp);
7117 tp->coalesce = 15000; /* 15 us */
7120 static void r8153c_init(struct r8152 *tp)
7126 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7129 r8153b_u1u2en(tp, false);
7131 /* Disable spi_en */
7132 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
7133 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
7134 ocp_data &= ~BIT(3);
7135 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
7136 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0);
7138 ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data);
7140 for (i = 0; i < 500; i++) {
7141 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7146 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7150 data = r8153_phy_status(tp, 0);
7152 data = r8152_mdio_read(tp, MII_BMCR);
7153 if (data & BMCR_PDOWN) {
7154 data &= ~BMCR_PDOWN;
7155 r8152_mdio_write(tp, MII_BMCR, data);
7158 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7160 r8153_u2p3en(tp, false);
7162 /* MSC timer = 0xfff * 8ms = 32760 ms */
7163 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7165 r8153b_power_cut_en(tp, false);
7166 r8153c_ups_en(tp, false);
7167 r8153_queue_wake(tp, false);
7168 rtl_runtime_suspend_enable(tp, false);
7170 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7171 if (rtl8152_get_speed(tp) & LINK_STATUS)
7172 ocp_data |= CUR_LINK_OK;
7174 ocp_data &= ~CUR_LINK_OK;
7176 ocp_data |= POLL_LINK_CHG;
7177 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7179 r8153b_u1u2en(tp, true);
7181 usb_enable_lpm(tp->udev);
7183 /* MAC clock speed down */
7184 r8153_mac_clk_speed_down(tp, true);
7186 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
7187 ocp_data &= ~BIT(7);
7188 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
7190 set_bit(GREEN_ETHERNET, &tp->flags);
7192 /* rx aggregation */
7193 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7194 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7195 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7197 rtl_tally_reset(tp);
7199 tp->coalesce = 15000; /* 15 us */
7202 static void r8156_hw_phy_cfg(struct r8152 *tp)
7207 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7208 if (ocp_data & PCUT_STATUS) {
7209 ocp_data &= ~PCUT_STATUS;
7210 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7213 data = r8153_phy_status(tp, 0);
7215 case PHY_STAT_EXT_INIT:
7216 rtl8152_apply_firmware(tp, true);
7218 data = ocp_reg_read(tp, 0xa468);
7219 data &= ~(BIT(3) | BIT(1));
7220 ocp_reg_write(tp, 0xa468, data);
7222 case PHY_STAT_LAN_ON:
7223 case PHY_STAT_PWRDN:
7225 rtl8152_apply_firmware(tp, false);
7229 /* disable ALDPS before updating the PHY parameters */
7230 r8153_aldps_en(tp, false);
7232 /* disable EEE before updating the PHY parameters */
7233 rtl_eee_enable(tp, false);
7235 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7236 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7238 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7239 ocp_data |= PFM_PWM_SWITCH;
7240 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7242 switch (tp->version) {
7244 data = ocp_reg_read(tp, 0xad40);
7246 data |= BIT(7) | BIT(2);
7247 ocp_reg_write(tp, 0xad40, data);
7249 data = ocp_reg_read(tp, 0xad4e);
7251 ocp_reg_write(tp, 0xad4e, data);
7252 data = ocp_reg_read(tp, 0xad16);
7255 ocp_reg_write(tp, 0xad16, data);
7256 data = ocp_reg_read(tp, 0xad32);
7259 ocp_reg_write(tp, 0xad32, data);
7260 data = ocp_reg_read(tp, 0xac08);
7261 data &= ~(BIT(12) | BIT(8));
7262 ocp_reg_write(tp, 0xac08, data);
7263 data = ocp_reg_read(tp, 0xac8a);
7264 data |= BIT(12) | BIT(13) | BIT(14);
7266 ocp_reg_write(tp, 0xac8a, data);
7267 data = ocp_reg_read(tp, 0xad18);
7269 ocp_reg_write(tp, 0xad18, data);
7270 data = ocp_reg_read(tp, 0xad1a);
7272 ocp_reg_write(tp, 0xad1a, data);
7273 data = ocp_reg_read(tp, 0xad1c);
7275 ocp_reg_write(tp, 0xad1c, data);
7277 data = sram_read(tp, 0x80ea);
7280 sram_write(tp, 0x80ea, data);
7281 data = sram_read(tp, 0x80eb);
7284 sram_write(tp, 0x80eb, data);
7285 data = sram_read(tp, 0x80f8);
7288 sram_write(tp, 0x80f8, data);
7289 data = sram_read(tp, 0x80f1);
7292 sram_write(tp, 0x80f1, data);
7294 data = sram_read(tp, 0x80fe);
7297 sram_write(tp, 0x80fe, data);
7298 data = sram_read(tp, 0x8102);
7301 sram_write(tp, 0x8102, data);
7302 data = sram_read(tp, 0x8015);
7305 sram_write(tp, 0x8015, data);
7306 data = sram_read(tp, 0x8100);
7309 sram_write(tp, 0x8100, data);
7310 data = sram_read(tp, 0x8014);
7313 sram_write(tp, 0x8014, data);
7314 data = sram_read(tp, 0x8016);
7317 sram_write(tp, 0x8016, data);
7318 data = sram_read(tp, 0x80dc);
7321 sram_write(tp, 0x80dc, data);
7322 data = sram_read(tp, 0x80df);
7324 sram_write(tp, 0x80df, data);
7325 data = sram_read(tp, 0x80e1);
7327 sram_write(tp, 0x80e1, data);
7329 data = ocp_reg_read(tp, 0xbf06);
7332 ocp_reg_write(tp, 0xbf06, data);
7334 sram_write(tp, 0x819f, 0xddb6);
7336 ocp_reg_write(tp, 0xbc34, 0x5555);
7337 data = ocp_reg_read(tp, 0xbf0a);
7340 ocp_reg_write(tp, 0xbf0a, data);
7342 data = ocp_reg_read(tp, 0xbd2c);
7344 ocp_reg_write(tp, 0xbd2c, data);
7347 data = ocp_reg_read(tp, 0xad16);
7349 ocp_reg_write(tp, 0xad16, data);
7350 data = ocp_reg_read(tp, 0xad32);
7353 ocp_reg_write(tp, 0xad32, data);
7354 data = ocp_reg_read(tp, 0xac08);
7355 data &= ~(BIT(12) | BIT(8));
7356 ocp_reg_write(tp, 0xac08, data);
7357 data = ocp_reg_read(tp, 0xacc0);
7360 ocp_reg_write(tp, 0xacc0, data);
7361 data = ocp_reg_read(tp, 0xad40);
7363 data |= BIT(6) | BIT(2);
7364 ocp_reg_write(tp, 0xad40, data);
7365 data = ocp_reg_read(tp, 0xac14);
7367 ocp_reg_write(tp, 0xac14, data);
7368 data = ocp_reg_read(tp, 0xac80);
7369 data &= ~(BIT(8) | BIT(9));
7370 ocp_reg_write(tp, 0xac80, data);
7371 data = ocp_reg_read(tp, 0xac5e);
7374 ocp_reg_write(tp, 0xac5e, data);
7375 ocp_reg_write(tp, 0xad4c, 0x00a8);
7376 ocp_reg_write(tp, 0xac5c, 0x01ff);
7377 data = ocp_reg_read(tp, 0xac8a);
7379 data |= BIT(4) | BIT(5);
7380 ocp_reg_write(tp, 0xac8a, data);
7381 ocp_reg_write(tp, 0xb87c, 0x8157);
7382 data = ocp_reg_read(tp, 0xb87e);
7385 ocp_reg_write(tp, 0xb87e, data);
7386 ocp_reg_write(tp, 0xb87c, 0x8159);
7387 data = ocp_reg_read(tp, 0xb87e);
7390 ocp_reg_write(tp, 0xb87e, data);
7393 ocp_reg_write(tp, 0xb87c, 0x80a2);
7394 ocp_reg_write(tp, 0xb87e, 0x0153);
7395 ocp_reg_write(tp, 0xb87c, 0x809c);
7396 ocp_reg_write(tp, 0xb87e, 0x0153);
7399 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056);
7401 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7402 ocp_data |= EN_XG_LIP | EN_G_LIP;
7403 ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7405 sram_write(tp, 0x8257, 0x020f); /* XG PLL */
7406 sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */
7408 if (rtl_phy_patch_request(tp, true, true))
7412 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7413 ocp_data |= EEE_SPDWN_EN;
7414 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7416 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7417 data &= ~(EN_EEE_100 | EN_EEE_1000);
7418 data |= EN_10M_CLKDIV;
7419 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7420 tp->ups_info._10m_ckdiv = true;
7421 tp->ups_info.eee_plloff_100 = false;
7422 tp->ups_info.eee_plloff_giga = false;
7424 data = ocp_reg_read(tp, OCP_POWER_CFG);
7425 data &= ~EEE_CLKDIV_EN;
7426 ocp_reg_write(tp, OCP_POWER_CFG, data);
7427 tp->ups_info.eee_ckdiv = false;
7429 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
7430 ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5));
7431 tp->ups_info._250m_ckdiv = false;
7433 rtl_phy_patch_request(tp, false, true);
7435 /* enable ADC Ibias Cal */
7436 data = ocp_reg_read(tp, 0xd068);
7438 ocp_reg_write(tp, 0xd068, data);
7440 /* enable Thermal Sensor */
7441 data = sram_read(tp, 0x81a2);
7443 sram_write(tp, 0x81a2, data);
7444 data = ocp_reg_read(tp, 0xb54c);
7447 ocp_reg_write(tp, 0xb54c, data);
7449 /* Nway 2.5G Lite */
7450 data = ocp_reg_read(tp, 0xa454);
7452 ocp_reg_write(tp, 0xa454, data);
7454 /* CS DSP solution */
7455 data = ocp_reg_read(tp, OCP_10GBT_CTRL);
7456 data |= RTL_ADV2_5G_F_R;
7457 ocp_reg_write(tp, OCP_10GBT_CTRL, data);
7458 data = ocp_reg_read(tp, 0xad4e);
7460 ocp_reg_write(tp, 0xad4e, data);
7461 data = ocp_reg_read(tp, 0xa86a);
7463 ocp_reg_write(tp, 0xa86a, data);
7466 if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) &&
7467 (ocp_reg_read(tp, 0xd068) & BIT(1))) {
7470 data = ocp_reg_read(tp, 0xd068);
7472 data |= 0x1; /* p0 */
7473 ocp_reg_write(tp, 0xd068, data);
7474 swap_a = ocp_reg_read(tp, 0xd06a);
7476 data |= 0x18; /* p3 */
7477 ocp_reg_write(tp, 0xd068, data);
7478 swap_b = ocp_reg_read(tp, 0xd06a);
7479 data &= ~0x18; /* p0 */
7480 ocp_reg_write(tp, 0xd068, data);
7481 ocp_reg_write(tp, 0xd06a,
7482 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7483 data |= 0x18; /* p3 */
7484 ocp_reg_write(tp, 0xd068, data);
7485 ocp_reg_write(tp, 0xd06a,
7486 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7488 data |= 0x08; /* p1 */
7489 ocp_reg_write(tp, 0xd068, data);
7490 swap_a = ocp_reg_read(tp, 0xd06a);
7492 data |= 0x10; /* p2 */
7493 ocp_reg_write(tp, 0xd068, data);
7494 swap_b = ocp_reg_read(tp, 0xd06a);
7496 data |= 0x08; /* p1 */
7497 ocp_reg_write(tp, 0xd068, data);
7498 ocp_reg_write(tp, 0xd06a,
7499 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7501 data |= 0x10; /* p2 */
7502 ocp_reg_write(tp, 0xd068, data);
7503 ocp_reg_write(tp, 0xd06a,
7504 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7505 swap_a = ocp_reg_read(tp, 0xbd5a);
7506 swap_b = ocp_reg_read(tp, 0xbd5c);
7507 ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) |
7508 ((swap_b & 0x1f) << 8) |
7509 ((swap_b >> 8) & 0x1f));
7510 ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) |
7511 ((swap_a & 0x1f) << 8) |
7512 ((swap_a >> 8) & 0x1f));
7513 swap_a = ocp_reg_read(tp, 0xbc18);
7514 swap_b = ocp_reg_read(tp, 0xbc1a);
7515 ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) |
7516 ((swap_b & 0x1f) << 8) |
7517 ((swap_b >> 8) & 0x1f));
7518 ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) |
7519 ((swap_a & 0x1f) << 8) |
7520 ((swap_a >> 8) & 0x1f));
7527 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7529 data = ocp_reg_read(tp, 0xa428);
7531 ocp_reg_write(tp, 0xa428, data);
7532 data = ocp_reg_read(tp, 0xa5ea);
7534 ocp_reg_write(tp, 0xa5ea, data);
7535 tp->ups_info.lite_mode = 0;
7538 rtl_eee_enable(tp, true);
7540 r8153_aldps_en(tp, true);
7541 r8152b_enable_fc(tp);
7542 r8153_u2p3en(tp, true);
7544 set_bit(PHY_RESET, &tp->flags);
7547 static void r8156b_hw_phy_cfg(struct r8152 *tp)
7552 switch (tp->version) {
7554 ocp_reg_write(tp, 0xbf86, 0x9000);
7555 data = ocp_reg_read(tp, 0xc402);
7557 ocp_reg_write(tp, 0xc402, data);
7559 ocp_reg_write(tp, 0xc402, data);
7560 ocp_reg_write(tp, 0xbd86, 0x1010);
7561 ocp_reg_write(tp, 0xbd88, 0x1010);
7562 data = ocp_reg_read(tp, 0xbd4e);
7563 data &= ~(BIT(10) | BIT(11));
7565 ocp_reg_write(tp, 0xbd4e, data);
7566 data = ocp_reg_read(tp, 0xbf46);
7569 ocp_reg_write(tp, 0xbf46, data);
7573 r8156b_wait_loading_flash(tp);
7579 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7580 if (ocp_data & PCUT_STATUS) {
7581 ocp_data &= ~PCUT_STATUS;
7582 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7585 data = r8153_phy_status(tp, 0);
7587 case PHY_STAT_EXT_INIT:
7588 rtl8152_apply_firmware(tp, true);
7590 data = ocp_reg_read(tp, 0xa466);
7592 ocp_reg_write(tp, 0xa466, data);
7594 data = ocp_reg_read(tp, 0xa468);
7595 data &= ~(BIT(3) | BIT(1));
7596 ocp_reg_write(tp, 0xa468, data);
7598 case PHY_STAT_LAN_ON:
7599 case PHY_STAT_PWRDN:
7601 rtl8152_apply_firmware(tp, false);
7605 data = r8152_mdio_read(tp, MII_BMCR);
7606 if (data & BMCR_PDOWN) {
7607 data &= ~BMCR_PDOWN;
7608 r8152_mdio_write(tp, MII_BMCR, data);
7611 /* disable ALDPS before updating the PHY parameters */
7612 r8153_aldps_en(tp, false);
7614 /* disable EEE before updating the PHY parameters */
7615 rtl_eee_enable(tp, false);
7617 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7618 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7620 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7621 ocp_data |= PFM_PWM_SWITCH;
7622 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7624 switch (tp->version) {
7626 data = ocp_reg_read(tp, 0xbc08);
7627 data |= BIT(3) | BIT(2);
7628 ocp_reg_write(tp, 0xbc08, data);
7630 data = sram_read(tp, 0x8fff);
7633 sram_write(tp, 0x8fff, data);
7635 data = ocp_reg_read(tp, 0xacda);
7637 ocp_reg_write(tp, 0xacda, data);
7638 data = ocp_reg_read(tp, 0xacde);
7640 ocp_reg_write(tp, 0xacde, data);
7641 ocp_reg_write(tp, 0xac8c, 0x0ffc);
7642 ocp_reg_write(tp, 0xac46, 0xb7b4);
7643 ocp_reg_write(tp, 0xac50, 0x0fbc);
7644 ocp_reg_write(tp, 0xac3c, 0x9240);
7645 ocp_reg_write(tp, 0xac4e, 0x0db4);
7646 ocp_reg_write(tp, 0xacc6, 0x0707);
7647 ocp_reg_write(tp, 0xacc8, 0xa0d3);
7648 ocp_reg_write(tp, 0xad08, 0x0007);
7650 ocp_reg_write(tp, 0xb87c, 0x8560);
7651 ocp_reg_write(tp, 0xb87e, 0x19cc);
7652 ocp_reg_write(tp, 0xb87c, 0x8562);
7653 ocp_reg_write(tp, 0xb87e, 0x19cc);
7654 ocp_reg_write(tp, 0xb87c, 0x8564);
7655 ocp_reg_write(tp, 0xb87e, 0x19cc);
7656 ocp_reg_write(tp, 0xb87c, 0x8566);
7657 ocp_reg_write(tp, 0xb87e, 0x147d);
7658 ocp_reg_write(tp, 0xb87c, 0x8568);
7659 ocp_reg_write(tp, 0xb87e, 0x147d);
7660 ocp_reg_write(tp, 0xb87c, 0x856a);
7661 ocp_reg_write(tp, 0xb87e, 0x147d);
7662 ocp_reg_write(tp, 0xb87c, 0x8ffe);
7663 ocp_reg_write(tp, 0xb87e, 0x0907);
7664 ocp_reg_write(tp, 0xb87c, 0x80d6);
7665 ocp_reg_write(tp, 0xb87e, 0x2801);
7666 ocp_reg_write(tp, 0xb87c, 0x80f2);
7667 ocp_reg_write(tp, 0xb87e, 0x2801);
7668 ocp_reg_write(tp, 0xb87c, 0x80f4);
7669 ocp_reg_write(tp, 0xb87e, 0x6077);
7670 ocp_reg_write(tp, 0xb506, 0x01e7);
7672 ocp_reg_write(tp, 0xb87c, 0x8013);
7673 ocp_reg_write(tp, 0xb87e, 0x0700);
7674 ocp_reg_write(tp, 0xb87c, 0x8fb9);
7675 ocp_reg_write(tp, 0xb87e, 0x2801);
7676 ocp_reg_write(tp, 0xb87c, 0x8fba);
7677 ocp_reg_write(tp, 0xb87e, 0x0100);
7678 ocp_reg_write(tp, 0xb87c, 0x8fbc);
7679 ocp_reg_write(tp, 0xb87e, 0x1900);
7680 ocp_reg_write(tp, 0xb87c, 0x8fbe);
7681 ocp_reg_write(tp, 0xb87e, 0xe100);
7682 ocp_reg_write(tp, 0xb87c, 0x8fc0);
7683 ocp_reg_write(tp, 0xb87e, 0x0800);
7684 ocp_reg_write(tp, 0xb87c, 0x8fc2);
7685 ocp_reg_write(tp, 0xb87e, 0xe500);
7686 ocp_reg_write(tp, 0xb87c, 0x8fc4);
7687 ocp_reg_write(tp, 0xb87e, 0x0f00);
7688 ocp_reg_write(tp, 0xb87c, 0x8fc6);
7689 ocp_reg_write(tp, 0xb87e, 0xf100);
7690 ocp_reg_write(tp, 0xb87c, 0x8fc8);
7691 ocp_reg_write(tp, 0xb87e, 0x0400);
7692 ocp_reg_write(tp, 0xb87c, 0x8fca);
7693 ocp_reg_write(tp, 0xb87e, 0xf300);
7694 ocp_reg_write(tp, 0xb87c, 0x8fcc);
7695 ocp_reg_write(tp, 0xb87e, 0xfd00);
7696 ocp_reg_write(tp, 0xb87c, 0x8fce);
7697 ocp_reg_write(tp, 0xb87e, 0xff00);
7698 ocp_reg_write(tp, 0xb87c, 0x8fd0);
7699 ocp_reg_write(tp, 0xb87e, 0xfb00);
7700 ocp_reg_write(tp, 0xb87c, 0x8fd2);
7701 ocp_reg_write(tp, 0xb87e, 0x0100);
7702 ocp_reg_write(tp, 0xb87c, 0x8fd4);
7703 ocp_reg_write(tp, 0xb87e, 0xf400);
7704 ocp_reg_write(tp, 0xb87c, 0x8fd6);
7705 ocp_reg_write(tp, 0xb87e, 0xff00);
7706 ocp_reg_write(tp, 0xb87c, 0x8fd8);
7707 ocp_reg_write(tp, 0xb87e, 0xf600);
7709 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7710 ocp_data |= EN_XG_LIP | EN_G_LIP;
7711 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7712 ocp_reg_write(tp, 0xb87c, 0x813d);
7713 ocp_reg_write(tp, 0xb87e, 0x390e);
7714 ocp_reg_write(tp, 0xb87c, 0x814f);
7715 ocp_reg_write(tp, 0xb87e, 0x790e);
7716 ocp_reg_write(tp, 0xb87c, 0x80b0);
7717 ocp_reg_write(tp, 0xb87e, 0x0f31);
7718 data = ocp_reg_read(tp, 0xbf4c);
7720 ocp_reg_write(tp, 0xbf4c, data);
7721 data = ocp_reg_read(tp, 0xbcca);
7722 data |= BIT(9) | BIT(8);
7723 ocp_reg_write(tp, 0xbcca, data);
7724 ocp_reg_write(tp, 0xb87c, 0x8141);
7725 ocp_reg_write(tp, 0xb87e, 0x320e);
7726 ocp_reg_write(tp, 0xb87c, 0x8153);
7727 ocp_reg_write(tp, 0xb87e, 0x720e);
7728 ocp_reg_write(tp, 0xb87c, 0x8529);
7729 ocp_reg_write(tp, 0xb87e, 0x050e);
7730 data = ocp_reg_read(tp, OCP_EEE_CFG);
7731 data &= ~CTAP_SHORT_EN;
7732 ocp_reg_write(tp, OCP_EEE_CFG, data);
7734 sram_write(tp, 0x816c, 0xc4a0);
7735 sram_write(tp, 0x8170, 0xc4a0);
7736 sram_write(tp, 0x8174, 0x04a0);
7737 sram_write(tp, 0x8178, 0x04a0);
7738 sram_write(tp, 0x817c, 0x0719);
7739 sram_write(tp, 0x8ff4, 0x0400);
7740 sram_write(tp, 0x8ff1, 0x0404);
7742 ocp_reg_write(tp, 0xbf4a, 0x001b);
7743 ocp_reg_write(tp, 0xb87c, 0x8033);
7744 ocp_reg_write(tp, 0xb87e, 0x7c13);
7745 ocp_reg_write(tp, 0xb87c, 0x8037);
7746 ocp_reg_write(tp, 0xb87e, 0x7c13);
7747 ocp_reg_write(tp, 0xb87c, 0x803b);
7748 ocp_reg_write(tp, 0xb87e, 0xfc32);
7749 ocp_reg_write(tp, 0xb87c, 0x803f);
7750 ocp_reg_write(tp, 0xb87e, 0x7c13);
7751 ocp_reg_write(tp, 0xb87c, 0x8043);
7752 ocp_reg_write(tp, 0xb87e, 0x7c13);
7753 ocp_reg_write(tp, 0xb87c, 0x8047);
7754 ocp_reg_write(tp, 0xb87e, 0x7c13);
7756 ocp_reg_write(tp, 0xb87c, 0x8145);
7757 ocp_reg_write(tp, 0xb87e, 0x370e);
7758 ocp_reg_write(tp, 0xb87c, 0x8157);
7759 ocp_reg_write(tp, 0xb87e, 0x770e);
7760 ocp_reg_write(tp, 0xb87c, 0x8169);
7761 ocp_reg_write(tp, 0xb87e, 0x0d0a);
7762 ocp_reg_write(tp, 0xb87c, 0x817b);
7763 ocp_reg_write(tp, 0xb87e, 0x1d0a);
7765 data = sram_read(tp, 0x8217);
7768 sram_write(tp, 0x8217, data);
7769 data = sram_read(tp, 0x821a);
7772 sram_write(tp, 0x821a, data);
7773 sram_write(tp, 0x80da, 0x0403);
7774 data = sram_read(tp, 0x80dc);
7777 sram_write(tp, 0x80dc, data);
7778 sram_write(tp, 0x80b3, 0x0384);
7779 sram_write(tp, 0x80b7, 0x2007);
7780 data = sram_read(tp, 0x80ba);
7783 sram_write(tp, 0x80ba, data);
7784 sram_write(tp, 0x80b5, 0xf009);
7785 data = sram_read(tp, 0x80bd);
7788 sram_write(tp, 0x80bd, data);
7789 sram_write(tp, 0x80c7, 0xf083);
7790 sram_write(tp, 0x80dd, 0x03f0);
7791 data = sram_read(tp, 0x80df);
7794 sram_write(tp, 0x80df, data);
7795 sram_write(tp, 0x80cb, 0x2007);
7796 data = sram_read(tp, 0x80ce);
7799 sram_write(tp, 0x80ce, data);
7800 sram_write(tp, 0x80c9, 0x8009);
7801 data = sram_read(tp, 0x80d1);
7804 sram_write(tp, 0x80d1, data);
7805 sram_write(tp, 0x80a3, 0x200a);
7806 sram_write(tp, 0x80a5, 0xf0ad);
7807 sram_write(tp, 0x809f, 0x6073);
7808 sram_write(tp, 0x80a1, 0x000b);
7809 data = sram_read(tp, 0x80a9);
7812 sram_write(tp, 0x80a9, data);
7814 if (rtl_phy_patch_request(tp, true, true))
7817 data = ocp_reg_read(tp, 0xb896);
7819 ocp_reg_write(tp, 0xb896, data);
7820 data = ocp_reg_read(tp, 0xb892);
7822 ocp_reg_write(tp, 0xb892, data);
7823 ocp_reg_write(tp, 0xb88e, 0xc23e);
7824 ocp_reg_write(tp, 0xb890, 0x0000);
7825 ocp_reg_write(tp, 0xb88e, 0xc240);
7826 ocp_reg_write(tp, 0xb890, 0x0103);
7827 ocp_reg_write(tp, 0xb88e, 0xc242);
7828 ocp_reg_write(tp, 0xb890, 0x0507);
7829 ocp_reg_write(tp, 0xb88e, 0xc244);
7830 ocp_reg_write(tp, 0xb890, 0x090b);
7831 ocp_reg_write(tp, 0xb88e, 0xc246);
7832 ocp_reg_write(tp, 0xb890, 0x0c0e);
7833 ocp_reg_write(tp, 0xb88e, 0xc248);
7834 ocp_reg_write(tp, 0xb890, 0x1012);
7835 ocp_reg_write(tp, 0xb88e, 0xc24a);
7836 ocp_reg_write(tp, 0xb890, 0x1416);
7837 data = ocp_reg_read(tp, 0xb896);
7839 ocp_reg_write(tp, 0xb896, data);
7841 rtl_phy_patch_request(tp, false, true);
7843 data = ocp_reg_read(tp, 0xa86a);
7845 ocp_reg_write(tp, 0xa86a, data);
7846 data = ocp_reg_read(tp, 0xa6f0);
7848 ocp_reg_write(tp, 0xa6f0, data);
7850 ocp_reg_write(tp, 0xbfa0, 0xd70d);
7851 ocp_reg_write(tp, 0xbfa2, 0x4100);
7852 ocp_reg_write(tp, 0xbfa4, 0xe868);
7853 ocp_reg_write(tp, 0xbfa6, 0xdc59);
7854 ocp_reg_write(tp, 0xb54c, 0x3c18);
7855 data = ocp_reg_read(tp, 0xbfa4);
7857 ocp_reg_write(tp, 0xbfa4, data);
7858 data = sram_read(tp, 0x817d);
7860 sram_write(tp, 0x817d, data);
7864 data = ocp_reg_read(tp, 0xac46);
7867 ocp_reg_write(tp, 0xac46, data);
7868 data = ocp_reg_read(tp, 0xad30);
7871 ocp_reg_write(tp, 0xad30, data);
7875 ocp_reg_write(tp, 0xb87c, 0x80f5);
7876 ocp_reg_write(tp, 0xb87e, 0x760e);
7877 ocp_reg_write(tp, 0xb87c, 0x8107);
7878 ocp_reg_write(tp, 0xb87e, 0x360e);
7879 ocp_reg_write(tp, 0xb87c, 0x8551);
7880 data = ocp_reg_read(tp, 0xb87e);
7883 ocp_reg_write(tp, 0xb87e, data);
7885 /* ADC_PGA parameter */
7886 data = ocp_reg_read(tp, 0xbf00);
7889 ocp_reg_write(tp, 0xbf00, data);
7890 data = ocp_reg_read(tp, 0xbf46);
7893 ocp_reg_write(tp, 0xbf46, data);
7895 /* Green Table-PGA, 1G full viterbi */
7896 sram_write(tp, 0x8044, 0x2417);
7897 sram_write(tp, 0x804a, 0x2417);
7898 sram_write(tp, 0x8050, 0x2417);
7899 sram_write(tp, 0x8056, 0x2417);
7900 sram_write(tp, 0x805c, 0x2417);
7901 sram_write(tp, 0x8062, 0x2417);
7902 sram_write(tp, 0x8068, 0x2417);
7903 sram_write(tp, 0x806e, 0x2417);
7904 sram_write(tp, 0x8074, 0x2417);
7905 sram_write(tp, 0x807a, 0x2417);
7908 data = ocp_reg_read(tp, 0xbf84);
7911 ocp_reg_write(tp, 0xbf84, data);
7917 if (rtl_phy_patch_request(tp, true, true))
7920 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7921 ocp_data |= EEE_SPDWN_EN;
7922 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7924 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7925 data &= ~(EN_EEE_100 | EN_EEE_1000);
7926 data |= EN_10M_CLKDIV;
7927 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7928 tp->ups_info._10m_ckdiv = true;
7929 tp->ups_info.eee_plloff_100 = false;
7930 tp->ups_info.eee_plloff_giga = false;
7932 data = ocp_reg_read(tp, OCP_POWER_CFG);
7933 data &= ~EEE_CLKDIV_EN;
7934 ocp_reg_write(tp, OCP_POWER_CFG, data);
7935 tp->ups_info.eee_ckdiv = false;
7937 rtl_phy_patch_request(tp, false, true);
7939 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7941 data = ocp_reg_read(tp, 0xa428);
7943 ocp_reg_write(tp, 0xa428, data);
7944 data = ocp_reg_read(tp, 0xa5ea);
7946 ocp_reg_write(tp, 0xa5ea, data);
7947 tp->ups_info.lite_mode = 0;
7950 rtl_eee_enable(tp, true);
7952 r8153_aldps_en(tp, true);
7953 r8152b_enable_fc(tp);
7954 r8153_u2p3en(tp, true);
7956 set_bit(PHY_RESET, &tp->flags);
7959 static void r8156_init(struct r8152 *tp)
7965 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7968 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
7969 ocp_data &= ~EN_ALL_SPEED;
7970 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
7972 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
7974 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
7975 ocp_data |= BYPASS_MAC_RESET;
7976 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
7978 r8153b_u1u2en(tp, false);
7980 for (i = 0; i < 500; i++) {
7981 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7986 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7990 data = r8153_phy_status(tp, 0);
7991 if (data == PHY_STAT_EXT_INIT) {
7992 data = ocp_reg_read(tp, 0xa468);
7993 data &= ~(BIT(3) | BIT(1));
7994 ocp_reg_write(tp, 0xa468, data);
7997 data = r8152_mdio_read(tp, MII_BMCR);
7998 if (data & BMCR_PDOWN) {
7999 data &= ~BMCR_PDOWN;
8000 r8152_mdio_write(tp, MII_BMCR, data);
8003 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8004 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
8006 r8153_u2p3en(tp, false);
8008 /* MSC timer = 0xfff * 8ms = 32760 ms */
8009 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8011 /* U1/U2/L1 idle timer. 500 us */
8012 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8014 r8153b_power_cut_en(tp, false);
8015 r8156_ups_en(tp, false);
8016 r8153_queue_wake(tp, false);
8017 rtl_runtime_suspend_enable(tp, false);
8019 if (tp->udev->speed >= USB_SPEED_SUPER)
8020 r8153b_u1u2en(tp, true);
8022 usb_enable_lpm(tp->udev);
8024 r8156_mac_clk_spd(tp, true);
8026 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8027 ocp_data &= ~PLA_MCU_SPDWN_EN;
8028 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8030 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8031 if (rtl8152_get_speed(tp) & LINK_STATUS)
8032 ocp_data |= CUR_LINK_OK;
8034 ocp_data &= ~CUR_LINK_OK;
8035 ocp_data |= POLL_LINK_CHG;
8036 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8038 set_bit(GREEN_ETHERNET, &tp->flags);
8040 /* rx aggregation */
8041 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8042 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8043 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8045 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
8046 ocp_data |= ACT_ODMA;
8047 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
8049 r8156_mdio_force_mode(tp);
8050 rtl_tally_reset(tp);
8052 tp->coalesce = 15000; /* 15 us */
8055 static void r8156b_init(struct r8152 *tp)
8061 if (test_bit(RTL8152_UNPLUG, &tp->flags))
8064 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
8065 ocp_data &= ~EN_ALL_SPEED;
8066 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
8068 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
8070 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
8071 ocp_data |= BYPASS_MAC_RESET;
8072 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
8074 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
8075 ocp_data |= RX_DETECT8;
8076 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
8078 r8153b_u1u2en(tp, false);
8080 switch (tp->version) {
8083 r8156b_wait_loading_flash(tp);
8089 for (i = 0; i < 500; i++) {
8090 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
8095 if (test_bit(RTL8152_UNPLUG, &tp->flags))
8099 data = r8153_phy_status(tp, 0);
8100 if (data == PHY_STAT_EXT_INIT) {
8101 data = ocp_reg_read(tp, 0xa468);
8102 data &= ~(BIT(3) | BIT(1));
8103 ocp_reg_write(tp, 0xa468, data);
8105 data = ocp_reg_read(tp, 0xa466);
8107 ocp_reg_write(tp, 0xa466, data);
8110 data = r8152_mdio_read(tp, MII_BMCR);
8111 if (data & BMCR_PDOWN) {
8112 data &= ~BMCR_PDOWN;
8113 r8152_mdio_write(tp, MII_BMCR, data);
8116 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8118 r8153_u2p3en(tp, false);
8120 /* MSC timer = 0xfff * 8ms = 32760 ms */
8121 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8123 /* U1/U2/L1 idle timer. 500 us */
8124 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8126 r8153b_power_cut_en(tp, false);
8127 r8156_ups_en(tp, false);
8128 r8153_queue_wake(tp, false);
8129 rtl_runtime_suspend_enable(tp, false);
8131 if (tp->udev->speed >= USB_SPEED_SUPER)
8132 r8153b_u1u2en(tp, true);
8134 usb_enable_lpm(tp->udev);
8136 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR);
8137 ocp_data &= ~SLOT_EN;
8138 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8140 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
8141 ocp_data |= FLOW_CTRL_EN;
8142 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
8144 /* enable fc timer and set timer to 600 ms. */
8145 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
8146 CTRL_TIMER_EN | (600 / 8));
8148 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
8149 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN))
8150 ocp_data |= FLOW_CTRL_PATCH_2;
8151 ocp_data &= ~AUTO_SPEEDUP;
8152 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
8154 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
8155 ocp_data |= FC_PATCH_TASK;
8156 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
8158 r8156_mac_clk_spd(tp, true);
8160 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8161 ocp_data &= ~PLA_MCU_SPDWN_EN;
8162 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8164 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8165 if (rtl8152_get_speed(tp) & LINK_STATUS)
8166 ocp_data |= CUR_LINK_OK;
8168 ocp_data &= ~CUR_LINK_OK;
8169 ocp_data |= POLL_LINK_CHG;
8170 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8172 set_bit(GREEN_ETHERNET, &tp->flags);
8174 /* rx aggregation */
8175 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8176 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8177 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8179 r8156_mdio_force_mode(tp);
8180 rtl_tally_reset(tp);
8182 tp->coalesce = 15000; /* 15 us */
8185 static bool rtl_check_vendor_ok(struct usb_interface *intf)
8187 struct usb_host_interface *alt = intf->cur_altsetting;
8188 struct usb_endpoint_descriptor *in, *out, *intr;
8190 if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) {
8191 dev_err(&intf->dev, "Expected endpoints are not found\n");
8195 /* Check Rx endpoint address */
8196 if (usb_endpoint_num(in) != 1) {
8197 dev_err(&intf->dev, "Invalid Rx endpoint address\n");
8201 /* Check Tx endpoint address */
8202 if (usb_endpoint_num(out) != 2) {
8203 dev_err(&intf->dev, "Invalid Tx endpoint address\n");
8207 /* Check interrupt endpoint address */
8208 if (usb_endpoint_num(intr) != 3) {
8209 dev_err(&intf->dev, "Invalid interrupt endpoint address\n");
8216 static bool rtl_vendor_mode(struct usb_interface *intf)
8218 struct usb_host_interface *alt = intf->cur_altsetting;
8219 struct usb_device *udev;
8220 struct usb_host_config *c;
8223 if (alt->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC)
8224 return rtl_check_vendor_ok(intf);
8226 /* The vendor mode is not always config #1, so to find it out. */
8227 udev = interface_to_usbdev(intf);
8229 num_configs = udev->descriptor.bNumConfigurations;
8230 if (num_configs < 2)
8233 for (i = 0; i < num_configs; (i++, c++)) {
8234 struct usb_interface_descriptor *desc = NULL;
8236 if (c->desc.bNumInterfaces > 0)
8237 desc = &c->intf_cache[0]->altsetting->desc;
8241 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
8242 usb_driver_set_configuration(udev, c->desc.bConfigurationValue);
8247 if (i == num_configs)
8248 dev_err(&intf->dev, "Unexpected Device\n");
8253 static int rtl8152_pre_reset(struct usb_interface *intf)
8255 struct r8152 *tp = usb_get_intfdata(intf);
8256 struct net_device *netdev;
8261 netdev = tp->netdev;
8262 if (!netif_running(netdev))
8265 netif_stop_queue(netdev);
8266 tasklet_disable(&tp->tx_tl);
8267 clear_bit(WORK_ENABLE, &tp->flags);
8268 usb_kill_urb(tp->intr_urb);
8269 cancel_delayed_work_sync(&tp->schedule);
8270 napi_disable(&tp->napi);
8271 if (netif_carrier_ok(netdev)) {
8272 mutex_lock(&tp->control);
8273 tp->rtl_ops.disable(tp);
8274 mutex_unlock(&tp->control);
8280 static int rtl8152_post_reset(struct usb_interface *intf)
8282 struct r8152 *tp = usb_get_intfdata(intf);
8283 struct net_device *netdev;
8289 /* reset the MAC address in case of policy change */
8290 if (determine_ethernet_addr(tp, &sa) >= 0) {
8292 dev_set_mac_address (tp->netdev, &sa, NULL);
8296 netdev = tp->netdev;
8297 if (!netif_running(netdev))
8300 set_bit(WORK_ENABLE, &tp->flags);
8301 if (netif_carrier_ok(netdev)) {
8302 mutex_lock(&tp->control);
8303 tp->rtl_ops.enable(tp);
8305 _rtl8152_set_rx_mode(netdev);
8306 mutex_unlock(&tp->control);
8309 napi_enable(&tp->napi);
8310 tasklet_enable(&tp->tx_tl);
8311 netif_wake_queue(netdev);
8312 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
8314 if (!list_empty(&tp->rx_done))
8315 napi_schedule(&tp->napi);
8320 static bool delay_autosuspend(struct r8152 *tp)
8322 bool sw_linking = !!netif_carrier_ok(tp->netdev);
8323 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
8325 /* This means a linking change occurs and the driver doesn't detect it,
8326 * yet. If the driver has disabled tx/rx and hw is linking on, the
8327 * device wouldn't wake up by receiving any packet.
8329 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
8332 /* If the linking down is occurred by nway, the device may miss the
8333 * linking change event. And it wouldn't wake when linking on.
8335 if (!sw_linking && tp->rtl_ops.in_nway(tp))
8337 else if (!skb_queue_empty(&tp->tx_queue))
8343 static int rtl8152_runtime_resume(struct r8152 *tp)
8345 struct net_device *netdev = tp->netdev;
8347 if (netif_running(netdev) && netdev->flags & IFF_UP) {
8348 struct napi_struct *napi = &tp->napi;
8350 tp->rtl_ops.autosuspend_en(tp, false);
8352 set_bit(WORK_ENABLE, &tp->flags);
8354 if (netif_carrier_ok(netdev)) {
8355 if (rtl8152_get_speed(tp) & LINK_STATUS) {
8358 netif_carrier_off(netdev);
8359 tp->rtl_ops.disable(tp);
8360 netif_info(tp, link, netdev, "linking down\n");
8365 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8366 smp_mb__after_atomic();
8368 if (!list_empty(&tp->rx_done))
8369 napi_schedule(&tp->napi);
8371 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8373 if (netdev->flags & IFF_UP)
8374 tp->rtl_ops.autosuspend_en(tp, false);
8376 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8382 static int rtl8152_system_resume(struct r8152 *tp)
8384 struct net_device *netdev = tp->netdev;
8386 netif_device_attach(netdev);
8388 if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
8390 netif_carrier_off(netdev);
8391 set_bit(WORK_ENABLE, &tp->flags);
8392 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8398 static int rtl8152_runtime_suspend(struct r8152 *tp)
8400 struct net_device *netdev = tp->netdev;
8403 if (!tp->rtl_ops.autosuspend_en)
8406 set_bit(SELECTIVE_SUSPEND, &tp->flags);
8407 smp_mb__after_atomic();
8409 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8412 if (netif_carrier_ok(netdev)) {
8415 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
8416 ocp_data = rcr & ~RCR_ACPT_ALL;
8417 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8418 rxdy_gated_en(tp, true);
8419 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
8421 if (!(ocp_data & RXFIFO_EMPTY)) {
8422 rxdy_gated_en(tp, false);
8423 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8424 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8425 smp_mb__after_atomic();
8431 clear_bit(WORK_ENABLE, &tp->flags);
8432 usb_kill_urb(tp->intr_urb);
8434 tp->rtl_ops.autosuspend_en(tp, true);
8436 if (netif_carrier_ok(netdev)) {
8437 struct napi_struct *napi = &tp->napi;
8441 rxdy_gated_en(tp, false);
8442 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8446 if (delay_autosuspend(tp)) {
8447 rtl8152_runtime_resume(tp);
8456 static int rtl8152_system_suspend(struct r8152 *tp)
8458 struct net_device *netdev = tp->netdev;
8460 netif_device_detach(netdev);
8462 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8463 struct napi_struct *napi = &tp->napi;
8465 clear_bit(WORK_ENABLE, &tp->flags);
8466 usb_kill_urb(tp->intr_urb);
8467 tasklet_disable(&tp->tx_tl);
8469 cancel_delayed_work_sync(&tp->schedule);
8470 tp->rtl_ops.down(tp);
8472 tasklet_enable(&tp->tx_tl);
8478 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
8480 struct r8152 *tp = usb_get_intfdata(intf);
8483 mutex_lock(&tp->control);
8485 if (PMSG_IS_AUTO(message))
8486 ret = rtl8152_runtime_suspend(tp);
8488 ret = rtl8152_system_suspend(tp);
8490 mutex_unlock(&tp->control);
8495 static int rtl8152_resume(struct usb_interface *intf)
8497 struct r8152 *tp = usb_get_intfdata(intf);
8500 mutex_lock(&tp->control);
8502 rtl_reset_ocp_base(tp);
8504 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
8505 ret = rtl8152_runtime_resume(tp);
8507 ret = rtl8152_system_resume(tp);
8509 mutex_unlock(&tp->control);
8514 static int rtl8152_reset_resume(struct usb_interface *intf)
8516 struct r8152 *tp = usb_get_intfdata(intf);
8518 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8519 rtl_reset_ocp_base(tp);
8520 tp->rtl_ops.init(tp);
8521 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
8522 set_ethernet_addr(tp, true);
8523 return rtl8152_resume(intf);
8526 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8528 struct r8152 *tp = netdev_priv(dev);
8530 if (usb_autopm_get_interface(tp->intf) < 0)
8533 if (!rtl_can_wakeup(tp)) {
8537 mutex_lock(&tp->control);
8538 wol->supported = WAKE_ANY;
8539 wol->wolopts = __rtl_get_wol(tp);
8540 mutex_unlock(&tp->control);
8543 usb_autopm_put_interface(tp->intf);
8546 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8548 struct r8152 *tp = netdev_priv(dev);
8551 if (!rtl_can_wakeup(tp))
8554 if (wol->wolopts & ~WAKE_ANY)
8557 ret = usb_autopm_get_interface(tp->intf);
8561 mutex_lock(&tp->control);
8563 __rtl_set_wol(tp, wol->wolopts);
8564 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
8566 mutex_unlock(&tp->control);
8568 usb_autopm_put_interface(tp->intf);
8574 static u32 rtl8152_get_msglevel(struct net_device *dev)
8576 struct r8152 *tp = netdev_priv(dev);
8578 return tp->msg_enable;
8581 static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
8583 struct r8152 *tp = netdev_priv(dev);
8585 tp->msg_enable = value;
8588 static void rtl8152_get_drvinfo(struct net_device *netdev,
8589 struct ethtool_drvinfo *info)
8591 struct r8152 *tp = netdev_priv(netdev);
8593 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
8594 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
8595 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
8596 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
8597 strlcpy(info->fw_version, tp->rtl_fw.version,
8598 sizeof(info->fw_version));
8602 int rtl8152_get_link_ksettings(struct net_device *netdev,
8603 struct ethtool_link_ksettings *cmd)
8605 struct r8152 *tp = netdev_priv(netdev);
8608 if (!tp->mii.mdio_read)
8611 ret = usb_autopm_get_interface(tp->intf);
8615 mutex_lock(&tp->control);
8617 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
8619 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8620 cmd->link_modes.supported, tp->support_2500full);
8622 if (tp->support_2500full) {
8623 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8624 cmd->link_modes.advertising,
8625 ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G);
8627 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8628 cmd->link_modes.lp_advertising,
8629 ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G);
8631 if (is_speed_2500(rtl8152_get_speed(tp)))
8632 cmd->base.speed = SPEED_2500;
8635 mutex_unlock(&tp->control);
8637 usb_autopm_put_interface(tp->intf);
8643 static int rtl8152_set_link_ksettings(struct net_device *dev,
8644 const struct ethtool_link_ksettings *cmd)
8646 struct r8152 *tp = netdev_priv(dev);
8647 u32 advertising = 0;
8650 ret = usb_autopm_get_interface(tp->intf);
8654 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
8655 cmd->link_modes.advertising))
8656 advertising |= RTL_ADVERTISED_10_HALF;
8658 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
8659 cmd->link_modes.advertising))
8660 advertising |= RTL_ADVERTISED_10_FULL;
8662 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
8663 cmd->link_modes.advertising))
8664 advertising |= RTL_ADVERTISED_100_HALF;
8666 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
8667 cmd->link_modes.advertising))
8668 advertising |= RTL_ADVERTISED_100_FULL;
8670 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
8671 cmd->link_modes.advertising))
8672 advertising |= RTL_ADVERTISED_1000_HALF;
8674 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
8675 cmd->link_modes.advertising))
8676 advertising |= RTL_ADVERTISED_1000_FULL;
8678 if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8679 cmd->link_modes.advertising))
8680 advertising |= RTL_ADVERTISED_2500_FULL;
8682 mutex_lock(&tp->control);
8684 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
8685 cmd->base.duplex, advertising);
8687 tp->autoneg = cmd->base.autoneg;
8688 tp->speed = cmd->base.speed;
8689 tp->duplex = cmd->base.duplex;
8690 tp->advertising = advertising;
8693 mutex_unlock(&tp->control);
8695 usb_autopm_put_interface(tp->intf);
8701 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
8708 "tx_single_collisions",
8709 "tx_multi_collisions",
8717 static int rtl8152_get_sset_count(struct net_device *dev, int sset)
8721 return ARRAY_SIZE(rtl8152_gstrings);
8727 static void rtl8152_get_ethtool_stats(struct net_device *dev,
8728 struct ethtool_stats *stats, u64 *data)
8730 struct r8152 *tp = netdev_priv(dev);
8731 struct tally_counter tally;
8733 if (usb_autopm_get_interface(tp->intf) < 0)
8736 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
8738 usb_autopm_put_interface(tp->intf);
8740 data[0] = le64_to_cpu(tally.tx_packets);
8741 data[1] = le64_to_cpu(tally.rx_packets);
8742 data[2] = le64_to_cpu(tally.tx_errors);
8743 data[3] = le32_to_cpu(tally.rx_errors);
8744 data[4] = le16_to_cpu(tally.rx_missed);
8745 data[5] = le16_to_cpu(tally.align_errors);
8746 data[6] = le32_to_cpu(tally.tx_one_collision);
8747 data[7] = le32_to_cpu(tally.tx_multi_collision);
8748 data[8] = le64_to_cpu(tally.rx_unicast);
8749 data[9] = le64_to_cpu(tally.rx_broadcast);
8750 data[10] = le32_to_cpu(tally.rx_multicast);
8751 data[11] = le16_to_cpu(tally.tx_aborted);
8752 data[12] = le16_to_cpu(tally.tx_underrun);
8755 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
8757 switch (stringset) {
8759 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings));
8764 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8766 u32 lp, adv, supported = 0;
8769 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
8770 supported = mmd_eee_cap_to_ethtool_sup_t(val);
8772 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
8773 adv = mmd_eee_adv_to_ethtool_adv_t(val);
8775 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
8776 lp = mmd_eee_adv_to_ethtool_adv_t(val);
8778 eee->eee_enabled = tp->eee_en;
8779 eee->eee_active = !!(supported & adv & lp);
8780 eee->supported = supported;
8781 eee->advertised = tp->eee_adv;
8782 eee->lp_advertised = lp;
8787 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
8789 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
8791 tp->eee_en = eee->eee_enabled;
8794 rtl_eee_enable(tp, tp->eee_en);
8799 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8801 u32 lp, adv, supported = 0;
8804 val = ocp_reg_read(tp, OCP_EEE_ABLE);
8805 supported = mmd_eee_cap_to_ethtool_sup_t(val);
8807 val = ocp_reg_read(tp, OCP_EEE_ADV);
8808 adv = mmd_eee_adv_to_ethtool_adv_t(val);
8810 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
8811 lp = mmd_eee_adv_to_ethtool_adv_t(val);
8813 eee->eee_enabled = tp->eee_en;
8814 eee->eee_active = !!(supported & adv & lp);
8815 eee->supported = supported;
8816 eee->advertised = tp->eee_adv;
8817 eee->lp_advertised = lp;
8823 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
8825 struct r8152 *tp = netdev_priv(net);
8828 if (!tp->rtl_ops.eee_get) {
8833 ret = usb_autopm_get_interface(tp->intf);
8837 mutex_lock(&tp->control);
8839 ret = tp->rtl_ops.eee_get(tp, edata);
8841 mutex_unlock(&tp->control);
8843 usb_autopm_put_interface(tp->intf);
8850 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
8852 struct r8152 *tp = netdev_priv(net);
8855 if (!tp->rtl_ops.eee_set) {
8860 ret = usb_autopm_get_interface(tp->intf);
8864 mutex_lock(&tp->control);
8866 ret = tp->rtl_ops.eee_set(tp, edata);
8868 ret = mii_nway_restart(&tp->mii);
8870 mutex_unlock(&tp->control);
8872 usb_autopm_put_interface(tp->intf);
8878 static int rtl8152_nway_reset(struct net_device *dev)
8880 struct r8152 *tp = netdev_priv(dev);
8883 ret = usb_autopm_get_interface(tp->intf);
8887 mutex_lock(&tp->control);
8889 ret = mii_nway_restart(&tp->mii);
8891 mutex_unlock(&tp->control);
8893 usb_autopm_put_interface(tp->intf);
8899 static int rtl8152_get_coalesce(struct net_device *netdev,
8900 struct ethtool_coalesce *coalesce,
8901 struct kernel_ethtool_coalesce *kernel_coal,
8902 struct netlink_ext_ack *extack)
8904 struct r8152 *tp = netdev_priv(netdev);
8906 switch (tp->version) {
8915 coalesce->rx_coalesce_usecs = tp->coalesce;
8920 static int rtl8152_set_coalesce(struct net_device *netdev,
8921 struct ethtool_coalesce *coalesce,
8922 struct kernel_ethtool_coalesce *kernel_coal,
8923 struct netlink_ext_ack *extack)
8925 struct r8152 *tp = netdev_priv(netdev);
8928 switch (tp->version) {
8937 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
8940 ret = usb_autopm_get_interface(tp->intf);
8944 mutex_lock(&tp->control);
8946 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
8947 tp->coalesce = coalesce->rx_coalesce_usecs;
8949 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
8950 netif_stop_queue(netdev);
8951 napi_disable(&tp->napi);
8952 tp->rtl_ops.disable(tp);
8953 tp->rtl_ops.enable(tp);
8955 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
8956 _rtl8152_set_rx_mode(netdev);
8957 napi_enable(&tp->napi);
8958 netif_wake_queue(netdev);
8962 mutex_unlock(&tp->control);
8964 usb_autopm_put_interface(tp->intf);
8969 static int rtl8152_get_tunable(struct net_device *netdev,
8970 const struct ethtool_tunable *tunable, void *d)
8972 struct r8152 *tp = netdev_priv(netdev);
8974 switch (tunable->id) {
8975 case ETHTOOL_RX_COPYBREAK:
8976 *(u32 *)d = tp->rx_copybreak;
8985 static int rtl8152_set_tunable(struct net_device *netdev,
8986 const struct ethtool_tunable *tunable,
8989 struct r8152 *tp = netdev_priv(netdev);
8992 switch (tunable->id) {
8993 case ETHTOOL_RX_COPYBREAK:
8995 if (val < ETH_ZLEN) {
8996 netif_err(tp, rx_err, netdev,
8997 "Invalid rx copy break value\n");
9001 if (tp->rx_copybreak != val) {
9002 if (netdev->flags & IFF_UP) {
9003 mutex_lock(&tp->control);
9004 napi_disable(&tp->napi);
9005 tp->rx_copybreak = val;
9006 napi_enable(&tp->napi);
9007 mutex_unlock(&tp->control);
9009 tp->rx_copybreak = val;
9020 static void rtl8152_get_ringparam(struct net_device *netdev,
9021 struct ethtool_ringparam *ring)
9023 struct r8152 *tp = netdev_priv(netdev);
9025 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
9026 ring->rx_pending = tp->rx_pending;
9029 static int rtl8152_set_ringparam(struct net_device *netdev,
9030 struct ethtool_ringparam *ring)
9032 struct r8152 *tp = netdev_priv(netdev);
9034 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
9037 if (tp->rx_pending != ring->rx_pending) {
9038 if (netdev->flags & IFF_UP) {
9039 mutex_lock(&tp->control);
9040 napi_disable(&tp->napi);
9041 tp->rx_pending = ring->rx_pending;
9042 napi_enable(&tp->napi);
9043 mutex_unlock(&tp->control);
9045 tp->rx_pending = ring->rx_pending;
9052 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9054 struct r8152 *tp = netdev_priv(netdev);
9055 u16 bmcr, lcladv, rmtadv;
9058 if (usb_autopm_get_interface(tp->intf) < 0)
9061 mutex_lock(&tp->control);
9063 bmcr = r8152_mdio_read(tp, MII_BMCR);
9064 lcladv = r8152_mdio_read(tp, MII_ADVERTISE);
9065 rmtadv = r8152_mdio_read(tp, MII_LPA);
9067 mutex_unlock(&tp->control);
9069 usb_autopm_put_interface(tp->intf);
9071 if (!(bmcr & BMCR_ANENABLE)) {
9073 pause->rx_pause = 0;
9074 pause->tx_pause = 0;
9080 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
9082 if (cap & FLOW_CTRL_RX)
9083 pause->rx_pause = 1;
9085 if (cap & FLOW_CTRL_TX)
9086 pause->tx_pause = 1;
9089 static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9091 struct r8152 *tp = netdev_priv(netdev);
9096 ret = usb_autopm_get_interface(tp->intf);
9100 mutex_lock(&tp->control);
9102 if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) {
9107 if (pause->rx_pause)
9108 cap |= FLOW_CTRL_RX;
9110 if (pause->tx_pause)
9111 cap |= FLOW_CTRL_TX;
9113 old = r8152_mdio_read(tp, MII_ADVERTISE);
9114 new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap);
9116 r8152_mdio_write(tp, MII_ADVERTISE, new1);
9119 mutex_unlock(&tp->control);
9120 usb_autopm_put_interface(tp->intf);
9125 static const struct ethtool_ops ops = {
9126 .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
9127 .get_drvinfo = rtl8152_get_drvinfo,
9128 .get_link = ethtool_op_get_link,
9129 .nway_reset = rtl8152_nway_reset,
9130 .get_msglevel = rtl8152_get_msglevel,
9131 .set_msglevel = rtl8152_set_msglevel,
9132 .get_wol = rtl8152_get_wol,
9133 .set_wol = rtl8152_set_wol,
9134 .get_strings = rtl8152_get_strings,
9135 .get_sset_count = rtl8152_get_sset_count,
9136 .get_ethtool_stats = rtl8152_get_ethtool_stats,
9137 .get_coalesce = rtl8152_get_coalesce,
9138 .set_coalesce = rtl8152_set_coalesce,
9139 .get_eee = rtl_ethtool_get_eee,
9140 .set_eee = rtl_ethtool_set_eee,
9141 .get_link_ksettings = rtl8152_get_link_ksettings,
9142 .set_link_ksettings = rtl8152_set_link_ksettings,
9143 .get_tunable = rtl8152_get_tunable,
9144 .set_tunable = rtl8152_set_tunable,
9145 .get_ringparam = rtl8152_get_ringparam,
9146 .set_ringparam = rtl8152_set_ringparam,
9147 .get_pauseparam = rtl8152_get_pauseparam,
9148 .set_pauseparam = rtl8152_set_pauseparam,
9151 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
9153 struct r8152 *tp = netdev_priv(netdev);
9154 struct mii_ioctl_data *data = if_mii(rq);
9157 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9160 res = usb_autopm_get_interface(tp->intf);
9166 data->phy_id = R8152_PHY_ID; /* Internal PHY */
9170 mutex_lock(&tp->control);
9171 data->val_out = r8152_mdio_read(tp, data->reg_num);
9172 mutex_unlock(&tp->control);
9176 if (!capable(CAP_NET_ADMIN)) {
9180 mutex_lock(&tp->control);
9181 r8152_mdio_write(tp, data->reg_num, data->val_in);
9182 mutex_unlock(&tp->control);
9189 usb_autopm_put_interface(tp->intf);
9195 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
9197 struct r8152 *tp = netdev_priv(dev);
9200 switch (tp->version) {
9210 ret = usb_autopm_get_interface(tp->intf);
9214 mutex_lock(&tp->control);
9218 if (netif_running(dev)) {
9219 if (tp->rtl_ops.change_mtu)
9220 tp->rtl_ops.change_mtu(tp);
9222 if (netif_carrier_ok(dev)) {
9223 netif_stop_queue(dev);
9224 napi_disable(&tp->napi);
9225 tasklet_disable(&tp->tx_tl);
9226 tp->rtl_ops.disable(tp);
9227 tp->rtl_ops.enable(tp);
9229 tasklet_enable(&tp->tx_tl);
9230 napi_enable(&tp->napi);
9231 rtl8152_set_rx_mode(dev);
9232 netif_wake_queue(dev);
9236 mutex_unlock(&tp->control);
9238 usb_autopm_put_interface(tp->intf);
9243 static const struct net_device_ops rtl8152_netdev_ops = {
9244 .ndo_open = rtl8152_open,
9245 .ndo_stop = rtl8152_close,
9246 .ndo_eth_ioctl = rtl8152_ioctl,
9247 .ndo_start_xmit = rtl8152_start_xmit,
9248 .ndo_tx_timeout = rtl8152_tx_timeout,
9249 .ndo_set_features = rtl8152_set_features,
9250 .ndo_set_rx_mode = rtl8152_set_rx_mode,
9251 .ndo_set_mac_address = rtl8152_set_mac_address,
9252 .ndo_change_mtu = rtl8152_change_mtu,
9253 .ndo_validate_addr = eth_validate_addr,
9254 .ndo_features_check = rtl8152_features_check,
9257 static void rtl8152_unload(struct r8152 *tp)
9259 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9262 if (tp->version != RTL_VER_01)
9263 r8152_power_cut_en(tp, true);
9266 static void rtl8153_unload(struct r8152 *tp)
9268 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9271 r8153_power_cut_en(tp, false);
9274 static void rtl8153b_unload(struct r8152 *tp)
9276 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9279 r8153b_power_cut_en(tp, false);
9282 static int rtl_ops_init(struct r8152 *tp)
9284 struct rtl_ops *ops = &tp->rtl_ops;
9287 switch (tp->version) {
9291 ops->init = r8152b_init;
9292 ops->enable = rtl8152_enable;
9293 ops->disable = rtl8152_disable;
9294 ops->up = rtl8152_up;
9295 ops->down = rtl8152_down;
9296 ops->unload = rtl8152_unload;
9297 ops->eee_get = r8152_get_eee;
9298 ops->eee_set = r8152_set_eee;
9299 ops->in_nway = rtl8152_in_nway;
9300 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
9301 ops->autosuspend_en = rtl_runtime_suspend_enable;
9302 tp->rx_buf_sz = 16 * 1024;
9304 tp->eee_adv = MDIO_EEE_100TX;
9311 ops->init = r8153_init;
9312 ops->enable = rtl8153_enable;
9313 ops->disable = rtl8153_disable;
9314 ops->up = rtl8153_up;
9315 ops->down = rtl8153_down;
9316 ops->unload = rtl8153_unload;
9317 ops->eee_get = r8153_get_eee;
9318 ops->eee_set = r8152_set_eee;
9319 ops->in_nway = rtl8153_in_nway;
9320 ops->hw_phy_cfg = r8153_hw_phy_cfg;
9321 ops->autosuspend_en = rtl8153_runtime_enable;
9322 ops->change_mtu = rtl8153_change_mtu;
9323 if (tp->udev->speed < USB_SPEED_SUPER)
9324 tp->rx_buf_sz = 16 * 1024;
9326 tp->rx_buf_sz = 32 * 1024;
9328 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9333 ops->init = r8153b_init;
9334 ops->enable = rtl8153_enable;
9335 ops->disable = rtl8153_disable;
9336 ops->up = rtl8153b_up;
9337 ops->down = rtl8153b_down;
9338 ops->unload = rtl8153b_unload;
9339 ops->eee_get = r8153_get_eee;
9340 ops->eee_set = r8152_set_eee;
9341 ops->in_nway = rtl8153_in_nway;
9342 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
9343 ops->autosuspend_en = rtl8153b_runtime_enable;
9344 ops->change_mtu = rtl8153_change_mtu;
9345 tp->rx_buf_sz = 32 * 1024;
9347 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9352 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9355 ops->init = r8156_init;
9356 ops->enable = rtl8156_enable;
9357 ops->disable = rtl8153_disable;
9358 ops->up = rtl8156_up;
9359 ops->down = rtl8156_down;
9360 ops->unload = rtl8153_unload;
9361 ops->eee_get = r8153_get_eee;
9362 ops->eee_set = r8152_set_eee;
9363 ops->in_nway = rtl8153_in_nway;
9364 ops->hw_phy_cfg = r8156_hw_phy_cfg;
9365 ops->autosuspend_en = rtl8156_runtime_enable;
9366 ops->change_mtu = rtl8156_change_mtu;
9367 tp->rx_buf_sz = 48 * 1024;
9368 tp->support_2500full = 1;
9373 tp->support_2500full = 1;
9377 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9378 ops->init = r8156b_init;
9379 ops->enable = rtl8156b_enable;
9380 ops->disable = rtl8153_disable;
9381 ops->up = rtl8156_up;
9382 ops->down = rtl8156_down;
9383 ops->unload = rtl8153_unload;
9384 ops->eee_get = r8153_get_eee;
9385 ops->eee_set = r8152_set_eee;
9386 ops->in_nway = rtl8153_in_nway;
9387 ops->hw_phy_cfg = r8156b_hw_phy_cfg;
9388 ops->autosuspend_en = rtl8156_runtime_enable;
9389 ops->change_mtu = rtl8156_change_mtu;
9390 tp->rx_buf_sz = 48 * 1024;
9394 ops->init = r8153c_init;
9395 ops->enable = rtl8153_enable;
9396 ops->disable = rtl8153_disable;
9397 ops->up = rtl8153c_up;
9398 ops->down = rtl8153b_down;
9399 ops->unload = rtl8153_unload;
9400 ops->eee_get = r8153_get_eee;
9401 ops->eee_set = r8152_set_eee;
9402 ops->in_nway = rtl8153_in_nway;
9403 ops->hw_phy_cfg = r8153c_hw_phy_cfg;
9404 ops->autosuspend_en = rtl8153c_runtime_enable;
9405 ops->change_mtu = rtl8153c_change_mtu;
9406 tp->rx_buf_sz = 32 * 1024;
9408 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9413 dev_err(&tp->intf->dev, "Unknown Device\n");
9420 #define FIRMWARE_8153A_2 "/*(DEBLOBBED)*/"
9421 #define FIRMWARE_8153A_3 "/*(DEBLOBBED)*/"
9422 #define FIRMWARE_8153A_4 "/*(DEBLOBBED)*/"
9423 #define FIRMWARE_8153B_2 "/*(DEBLOBBED)*/"
9424 #define FIRMWARE_8153C_1 "/*(DEBLOBBED)*/"
9425 #define FIRMWARE_8156A_2 "/*(DEBLOBBED)*/"
9426 #define FIRMWARE_8156B_2 "/*(DEBLOBBED)*/"
9430 static int rtl_fw_init(struct r8152 *tp)
9432 struct rtl_fw *rtl_fw = &tp->rtl_fw;
9434 switch (tp->version) {
9436 rtl_fw->fw_name = FIRMWARE_8153A_2;
9437 rtl_fw->pre_fw = r8153_pre_firmware_1;
9438 rtl_fw->post_fw = r8153_post_firmware_1;
9441 rtl_fw->fw_name = FIRMWARE_8153A_3;
9442 rtl_fw->pre_fw = r8153_pre_firmware_2;
9443 rtl_fw->post_fw = r8153_post_firmware_2;
9446 rtl_fw->fw_name = FIRMWARE_8153A_4;
9447 rtl_fw->post_fw = r8153_post_firmware_3;
9450 rtl_fw->fw_name = FIRMWARE_8153B_2;
9451 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9452 rtl_fw->post_fw = r8153b_post_firmware_1;
9455 rtl_fw->fw_name = FIRMWARE_8156A_2;
9456 rtl_fw->post_fw = r8156a_post_firmware_1;
9460 rtl_fw->fw_name = FIRMWARE_8156B_2;
9463 rtl_fw->fw_name = FIRMWARE_8153C_1;
9464 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9465 rtl_fw->post_fw = r8153c_post_firmware_1;
9474 u8 rtl8152_get_version(struct usb_interface *intf)
9476 struct usb_device *udev = interface_to_usbdev(intf);
9482 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
9486 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
9487 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
9488 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
9490 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
9496 version = RTL_VER_01;
9499 version = RTL_VER_02;
9502 version = RTL_VER_03;
9505 version = RTL_VER_04;
9508 version = RTL_VER_05;
9511 version = RTL_VER_06;
9514 version = RTL_VER_07;
9517 version = RTL_VER_08;
9520 version = RTL_VER_09;
9523 version = RTL_TEST_01;
9526 version = RTL_VER_10;
9529 version = RTL_VER_11;
9532 version = RTL_VER_12;
9535 version = RTL_VER_13;
9538 version = RTL_VER_14;
9541 version = RTL_VER_15;
9544 version = RTL_VER_UNKNOWN;
9545 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
9549 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
9553 EXPORT_SYMBOL_GPL(rtl8152_get_version);
9555 static int rtl8152_probe(struct usb_interface *intf,
9556 const struct usb_device_id *id)
9558 struct usb_device *udev = interface_to_usbdev(intf);
9559 u8 version = rtl8152_get_version(intf);
9561 struct net_device *netdev;
9564 if (version == RTL_VER_UNKNOWN)
9567 if (!rtl_vendor_mode(intf))
9570 usb_reset_device(udev);
9571 netdev = alloc_etherdev(sizeof(struct r8152));
9573 dev_err(&intf->dev, "Out of memory\n");
9577 SET_NETDEV_DEV(netdev, &intf->dev);
9578 tp = netdev_priv(netdev);
9579 tp->msg_enable = 0x7FFF;
9582 tp->netdev = netdev;
9584 tp->version = version;
9586 tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0);
9587 tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0);
9588 tp->pipe_in = usb_rcvbulkpipe(udev, 1);
9589 tp->pipe_out = usb_sndbulkpipe(udev, 2);
9590 tp->pipe_intr = usb_rcvintpipe(udev, 3);
9596 tp->mii.supports_gmii = 0;
9599 tp->mii.supports_gmii = 1;
9603 ret = rtl_ops_init(tp);
9609 mutex_init(&tp->control);
9610 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
9611 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
9612 tasklet_setup(&tp->tx_tl, bottom_half);
9613 tasklet_disable(&tp->tx_tl);
9615 netdev->netdev_ops = &rtl8152_netdev_ops;
9616 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
9618 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9619 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
9620 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
9621 NETIF_F_HW_VLAN_CTAG_TX;
9622 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9623 NETIF_F_TSO | NETIF_F_FRAGLIST |
9624 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
9625 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
9626 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
9627 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
9628 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
9630 if (tp->version == RTL_VER_01) {
9631 netdev->features &= ~NETIF_F_RXCSUM;
9632 netdev->hw_features &= ~NETIF_F_RXCSUM;
9635 if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO) {
9636 switch (le16_to_cpu(udev->descriptor.idProduct)) {
9637 case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2:
9638 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2:
9639 tp->lenovo_macpassthru = 1;
9643 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
9644 (!strcmp(udev->serial, "000001000000") ||
9645 !strcmp(udev->serial, "000002000000"))) {
9646 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
9647 tp->dell_tb_rx_agg_bug = 1;
9650 netdev->ethtool_ops = &ops;
9651 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
9653 /* MTU range: 68 - 1500 or 9194 */
9654 netdev->min_mtu = ETH_MIN_MTU;
9655 switch (tp->version) {
9663 netdev->max_mtu = size_to_mtu(9 * 1024);
9667 netdev->max_mtu = size_to_mtu(15 * 1024);
9672 netdev->max_mtu = size_to_mtu(16 * 1024);
9678 netdev->max_mtu = ETH_DATA_LEN;
9682 tp->mii.dev = netdev;
9683 tp->mii.mdio_read = read_mii_word;
9684 tp->mii.mdio_write = write_mii_word;
9685 tp->mii.phy_id_mask = 0x3f;
9686 tp->mii.reg_num_mask = 0x1f;
9687 tp->mii.phy_id = R8152_PHY_ID;
9689 tp->autoneg = AUTONEG_ENABLE;
9690 tp->speed = SPEED_100;
9691 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
9692 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
9693 if (tp->mii.supports_gmii) {
9694 if (tp->support_2500full &&
9695 tp->udev->speed >= USB_SPEED_SUPER) {
9696 tp->speed = SPEED_2500;
9697 tp->advertising |= RTL_ADVERTISED_2500_FULL;
9699 tp->speed = SPEED_1000;
9701 tp->advertising |= RTL_ADVERTISED_1000_FULL;
9703 tp->duplex = DUPLEX_FULL;
9705 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
9706 tp->rx_pending = 10 * RTL8152_MAX_RX;
9708 intf->needs_remote_wakeup = 1;
9710 if (!rtl_can_wakeup(tp))
9711 __rtl_set_wol(tp, 0);
9713 tp->saved_wolopts = __rtl_get_wol(tp);
9715 tp->rtl_ops.init(tp);
9716 #if IS_BUILTIN(CONFIG_USB_RTL8152)
9717 /* Retry in case reject_firmware() is not ready yet. */
9718 tp->rtl_fw.retry = true;
9720 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
9721 set_ethernet_addr(tp, false);
9723 usb_set_intfdata(intf, tp);
9725 if (tp->support_2500full)
9726 netif_napi_add(netdev, &tp->napi, r8152_poll, 256);
9728 netif_napi_add(netdev, &tp->napi, r8152_poll, 64);
9730 ret = register_netdev(netdev);
9732 dev_err(&intf->dev, "couldn't register the device\n");
9736 if (tp->saved_wolopts)
9737 device_set_wakeup_enable(&udev->dev, true);
9739 device_set_wakeup_enable(&udev->dev, false);
9741 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
9746 tasklet_kill(&tp->tx_tl);
9747 usb_set_intfdata(intf, NULL);
9749 free_netdev(netdev);
9753 static void rtl8152_disconnect(struct usb_interface *intf)
9755 struct r8152 *tp = usb_get_intfdata(intf);
9757 usb_set_intfdata(intf, NULL);
9761 unregister_netdev(tp->netdev);
9762 tasklet_kill(&tp->tx_tl);
9763 cancel_delayed_work_sync(&tp->hw_phy_work);
9764 if (tp->rtl_ops.unload)
9765 tp->rtl_ops.unload(tp);
9766 rtl8152_release_firmware(tp);
9767 free_netdev(tp->netdev);
9771 #define REALTEK_USB_DEVICE(vend, prod) { \
9772 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC), \
9775 USB_DEVICE_AND_INTERFACE_INFO(vend, prod, USB_CLASS_COMM, \
9776 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), \
9779 /* table of devices that work with this driver */
9780 static const struct usb_device_id rtl8152_table[] = {
9782 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050),
9783 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8053),
9784 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152),
9785 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153),
9786 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8155),
9787 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8156),
9790 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab),
9791 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6),
9792 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927),
9793 REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101),
9794 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f),
9795 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062),
9796 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069),
9797 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082),
9798 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205),
9799 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c),
9800 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214),
9801 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x721e),
9802 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387),
9803 REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041),
9804 REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff),
9805 REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601),
9809 MODULE_DEVICE_TABLE(usb, rtl8152_table);
9811 static struct usb_driver rtl8152_driver = {
9813 .id_table = rtl8152_table,
9814 .probe = rtl8152_probe,
9815 .disconnect = rtl8152_disconnect,
9816 .suspend = rtl8152_suspend,
9817 .resume = rtl8152_resume,
9818 .reset_resume = rtl8152_reset_resume,
9819 .pre_reset = rtl8152_pre_reset,
9820 .post_reset = rtl8152_post_reset,
9821 .supports_autosuspend = 1,
9822 .disable_hub_initiated_lpm = 1,
9825 module_usb_driver(rtl8152_driver);
9827 MODULE_AUTHOR(DRIVER_AUTHOR);
9828 MODULE_DESCRIPTION(DRIVER_DESC);
9829 MODULE_LICENSE("GPL");
9830 MODULE_VERSION(DRIVER_VERSION);