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