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