blob: ee41088c5251865b25702ae332384eb195f78537 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
hayeswangac718b62013-05-02 16:01:25 +00002/*
hayeswangc7de7de2014-01-15 10:42:16 +08003 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00004 */
5
hayeswangac718b62013-05-02 16:01:25 +00006#include <linux/signal.h>
7#include <linux/slab.h>
8#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +00009#include <linux/netdevice.h>
10#include <linux/etherdevice.h>
11#include <linux/mii.h>
12#include <linux/ethtool.h>
13#include <linux/usb.h>
14#include <linux/crc32.h>
15#include <linux/if_vlan.h>
16#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080017#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080018#include <linux/ip.h>
19#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080020#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080021#include <uapi/linux/mdio.h>
22#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080023#include <linux/usb/cdc.h>
hayeswang5ee3c602016-01-07 17:12:17 +080024#include <linux/suspend.h>
Hayes Wang252df8b2019-08-13 11:42:06 +080025#include <linux/atomic.h>
Mario Limonciello34ee32c2016-07-11 19:58:04 -050026#include <linux/acpi.h>
Hayes Wang9370f2d2019-10-16 11:02:42 +080027#include <linux/firmware.h>
28#include <crypto/hash.h>
Hayes Wangc1aedf02020-11-04 10:19:22 +080029#include <linux/usb/r8152.h>
hayeswangac718b62013-05-02 16:01:25 +000030
hayeswangd0942472015-09-07 11:57:43 +080031/* Information for net-next */
Hayes Wangc2198942021-04-16 16:04:37 +080032#define NETNEXT_VERSION "12"
hayeswangd0942472015-09-07 11:57:43 +080033
34/* Information for net */
Hayes Wangb24edca2021-12-23 17:27:02 +080035#define NET_VERSION "12"
hayeswangd0942472015-09-07 11:57:43 +080036
37#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000038#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080039#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000040#define MODULENAME "r8152"
41
42#define R8152_PHY_ID 32
43
44#define PLA_IDR 0xc000
45#define PLA_RCR 0xc010
Hayes Wang195aae32021-04-16 16:04:35 +080046#define PLA_RCR1 0xc012
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_RMS 0xc016
48#define PLA_RXFIFO_CTRL0 0xc0a0
Hayes Wang195aae32021-04-16 16:04:35 +080049#define PLA_RXFIFO_FULL 0xc0a2
hayeswangac718b62013-05-02 16:01:25 +000050#define PLA_RXFIFO_CTRL1 0xc0a4
Hayes Wang195aae32021-04-16 16:04:35 +080051#define PLA_RX_FIFO_FULL 0xc0a6
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_RXFIFO_CTRL2 0xc0a8
Hayes Wang195aae32021-04-16 16:04:35 +080053#define PLA_RX_FIFO_EMPTY 0xc0aa
hayeswang65bab842015-02-12 16:20:46 +080054#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000055#define PLA_FMC 0xc0b4
56#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080057#define PLA_TEREDO_CFG 0xc0bc
hayeswang65b82d62017-06-15 14:44:03 +080058#define PLA_TEREDO_WAKE_BASE 0xc0c4
hayeswangac718b62013-05-02 16:01:25 +000059#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080060#define PLA_BACKUP 0xd000
Kevin Lo59c0b472019-08-01 11:29:38 +080061#define PLA_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080062#define PLA_TEREDO_TIMER 0xd2cc
63#define PLA_REALWOW_TIMER 0xd2e8
Hayes Wang9370f2d2019-10-16 11:02:42 +080064#define PLA_UPHY_TIMER 0xd388
Hayes Wang13e04fbf2019-07-01 15:53:19 +080065#define PLA_SUSPEND_FLAG 0xd38a
66#define PLA_INDICATE_FALG 0xd38c
Hayes Wang9370f2d2019-10-16 11:02:42 +080067#define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */
68#define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */
Hayes Wang13e04fbf2019-07-01 15:53:19 +080069#define PLA_EXTRA_STATUS 0xd398
Hayes Wang195aae32021-04-16 16:04:35 +080070#define PLA_GPHY_CTRL 0xd3ae
71#define PLA_POL_GPIO_CTRL 0xdc6a
hayeswang65b82d62017-06-15 14:44:03 +080072#define PLA_EFUSE_DATA 0xdd00
73#define PLA_EFUSE_CMD 0xdd02
hayeswangac718b62013-05-02 16:01:25 +000074#define PLA_LEDSEL 0xdd90
75#define PLA_LED_FEATURE 0xdd92
76#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080077#define PLA_BOOT_CTRL 0xe004
Hayes Wang19813162020-01-22 16:02:12 +080078#define PLA_LWAKE_CTRL_REG 0xe007
hayeswangac718b62013-05-02 16:01:25 +000079#define PLA_GPHY_INTR_IMR 0xe022
80#define PLA_EEE_CR 0xe040
Hayes Wang195aae32021-04-16 16:04:35 +080081#define PLA_EEE_TXTWSYS 0xe04c
82#define PLA_EEE_TXTWSYS_2P5G 0xe058
hayeswangac718b62013-05-02 16:01:25 +000083#define PLA_EEEP_CR 0xe080
84#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080085#define PLA_MAC_PWR_CTRL2 0xe0ca
86#define PLA_MAC_PWR_CTRL3 0xe0cc
87#define PLA_MAC_PWR_CTRL4 0xe0ce
88#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000089#define PLA_TCR0 0xe610
90#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080091#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000092#define PLA_TXFIFO_CTRL 0xe618
Hayes Wang195aae32021-04-16 16:04:35 +080093#define PLA_TXFIFO_FULL 0xe61a
hayeswang4f1d4d52014-03-11 16:24:19 +080094#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000095#define PLA_CR 0xe813
96#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080097#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
98#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000099#define PLA_CONFIG5 0xe822
100#define PLA_PHY_PWR 0xe84c
101#define PLA_OOB_CTRL 0xe84f
102#define PLA_CPCR 0xe854
103#define PLA_MISC_0 0xe858
104#define PLA_MISC_1 0xe85a
105#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +0800106#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +0000107#define PLA_SFF_STS_7 0xe8de
108#define PLA_PHYSTATUS 0xe908
Hayes Wang19813162020-01-22 16:02:12 +0800109#define PLA_CONFIG6 0xe90a /* CONFIG6 */
Hayes Wang195aae32021-04-16 16:04:35 +0800110#define PLA_USB_CFG 0xe952
hayeswangac718b62013-05-02 16:01:25 +0000111#define PLA_BP_BA 0xfc26
112#define PLA_BP_0 0xfc28
113#define PLA_BP_1 0xfc2a
114#define PLA_BP_2 0xfc2c
115#define PLA_BP_3 0xfc2e
116#define PLA_BP_4 0xfc30
117#define PLA_BP_5 0xfc32
118#define PLA_BP_6 0xfc34
119#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800120#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000121
hayeswang65bab842015-02-12 16:20:46 +0800122#define USB_USB2PHY 0xb41e
Hayes Wangaa475d92020-01-22 16:02:13 +0800123#define USB_SSPHYLINK1 0xb426
hayeswang65bab842015-02-12 16:20:46 +0800124#define USB_SSPHYLINK2 0xb428
Hayes Wang195aae32021-04-16 16:04:35 +0800125#define USB_L1_CTRL 0xb45e
hayeswang43779f82014-01-02 11:25:10 +0800126#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800127#define USB_CSR_DUMMY1 0xb464
128#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000129#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800130#define USB_CONNECT_TIMER 0xcbf8
hayeswang65b82d62017-06-15 14:44:03 +0800131#define USB_MSC_TIMER 0xcbfc
hayeswang65bab842015-02-12 16:20:46 +0800132#define USB_BURST_SIZE 0xcfc0
Hayes Wang9370f2d2019-10-16 11:02:42 +0800133#define USB_FW_FIX_EN0 0xcfca
134#define USB_FW_FIX_EN1 0xcfcc
hayeswang65b82d62017-06-15 14:44:03 +0800135#define USB_LPM_CONFIG 0xcfd8
Hayes Wang195aae32021-04-16 16:04:35 +0800136#define USB_ECM_OPTION 0xcfee
Hayes Wang9370f2d2019-10-16 11:02:42 +0800137#define USB_CSTMR 0xcfef /* RTL8153A */
Hayes Wang195aae32021-04-16 16:04:35 +0800138#define USB_MISC_2 0xcfff
139#define USB_ECM_OP 0xd26b
140#define USB_GPHY_CTRL 0xd284
141#define USB_SPEED_OPTION 0xd32a
Hayes Wang9370f2d2019-10-16 11:02:42 +0800142#define USB_FW_CTRL 0xd334 /* RTL8153B */
143#define USB_FC_TIMER 0xd340
hayeswangac718b62013-05-02 16:01:25 +0000144#define USB_USB_CTRL 0xd406
145#define USB_PHY_CTRL 0xd408
146#define USB_TX_AGG 0xd40a
147#define USB_RX_BUF_TH 0xd40c
148#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800149#define USB_RX_EARLY_TIMEOUT 0xd42c
150#define USB_RX_EARLY_SIZE 0xd42e
hayeswang65b82d62017-06-15 14:44:03 +0800151#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
152#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000153#define USB_TX_DMA 0xd434
hayeswang65b82d62017-06-15 14:44:03 +0800154#define USB_UPT_RXDMA_OWN 0xd437
Hayes Wang195aae32021-04-16 16:04:35 +0800155#define USB_UPHY3_MDCMDIO 0xd480
hayeswang43779f82014-01-02 11:25:10 +0800156#define USB_TOLERANCE 0xd490
157#define USB_LPM_CTRL 0xd41a
hayeswang93fe9b12016-06-16 10:55:18 +0800158#define USB_BMU_RESET 0xd4b0
Hayes Wang195aae32021-04-16 16:04:35 +0800159#define USB_BMU_CONFIG 0xd4b4
hayeswang65b82d62017-06-15 14:44:03 +0800160#define USB_U1U2_TIMER 0xd4da
Hayes Wang9370f2d2019-10-16 11:02:42 +0800161#define USB_FW_TASK 0xd4e8 /* RTL8153B */
Hayes Wang195aae32021-04-16 16:04:35 +0800162#define USB_RX_AGGR_NUM 0xd4ee
hayeswangac718b62013-05-02 16:01:25 +0000163#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800164#define USB_POWER_CUT 0xd80a
hayeswang65b82d62017-06-15 14:44:03 +0800165#define USB_MISC_0 0xd81a
Mario Limonciello9c273692018-12-11 08:16:14 -0600166#define USB_MISC_1 0xd81f
hayeswang43779f82014-01-02 11:25:10 +0800167#define USB_AFE_CTRL2 0xd824
Hayes Wang195aae32021-04-16 16:04:35 +0800168#define USB_UPHY_XTAL 0xd826
hayeswang65b82d62017-06-15 14:44:03 +0800169#define USB_UPS_CFG 0xd842
170#define USB_UPS_FLAGS 0xd848
Hayes Wang9370f2d2019-10-16 11:02:42 +0800171#define USB_WDT1_CTRL 0xe404
hayeswang43779f82014-01-02 11:25:10 +0800172#define USB_WDT11_CTRL 0xe43c
Hayes Wang9370f2d2019-10-16 11:02:42 +0800173#define USB_BP_BA PLA_BP_BA
174#define USB_BP_0 PLA_BP_0
175#define USB_BP_1 PLA_BP_1
176#define USB_BP_2 PLA_BP_2
177#define USB_BP_3 PLA_BP_3
178#define USB_BP_4 PLA_BP_4
179#define USB_BP_5 PLA_BP_5
180#define USB_BP_6 PLA_BP_6
181#define USB_BP_7 PLA_BP_7
182#define USB_BP_EN PLA_BP_EN /* RTL8153A */
183#define USB_BP_8 0xfc38 /* RTL8153B */
hayeswang65b82d62017-06-15 14:44:03 +0800184#define USB_BP_9 0xfc3a
185#define USB_BP_10 0xfc3c
186#define USB_BP_11 0xfc3e
187#define USB_BP_12 0xfc40
188#define USB_BP_13 0xfc42
189#define USB_BP_14 0xfc44
190#define USB_BP_15 0xfc46
191#define USB_BP2_EN 0xfc48
hayeswangac718b62013-05-02 16:01:25 +0000192
193/* OCP Registers */
194#define OCP_ALDPS_CONFIG 0x2010
195#define OCP_EEE_CONFIG1 0x2080
196#define OCP_EEE_CONFIG2 0x2092
197#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800198#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000199#define OCP_EEE_AR 0xa41a
200#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800201#define OCP_PHY_STATUS 0xa420
hayeswang65b82d62017-06-15 14:44:03 +0800202#define OCP_NCTL_CFG 0xa42c
hayeswang43779f82014-01-02 11:25:10 +0800203#define OCP_POWER_CFG 0xa430
204#define OCP_EEE_CFG 0xa432
205#define OCP_SRAM_ADDR 0xa436
206#define OCP_SRAM_DATA 0xa438
207#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800208#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800209#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800210#define OCP_EEE_LPABLE 0xa5d2
Hayes Wang195aae32021-04-16 16:04:35 +0800211#define OCP_10GBT_CTRL 0xa5d4
212#define OCP_10GBT_STAT 0xa5d6
213#define OCP_EEE_ADV2 0xa6d4
hayeswang2dd49e02015-09-07 11:57:44 +0800214#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
hayeswang65b82d62017-06-15 14:44:03 +0800215#define OCP_PHY_PATCH_STAT 0xb800
216#define OCP_PHY_PATCH_CMD 0xb820
Hayes Wangaf142882019-10-21 11:41:13 +0800217#define OCP_PHY_LOCK 0xb82e
hayeswang65b82d62017-06-15 14:44:03 +0800218#define OCP_ADC_IOFFSET 0xbcfc
hayeswang43779f82014-01-02 11:25:10 +0800219#define OCP_ADC_CFG 0xbc06
hayeswang65b82d62017-06-15 14:44:03 +0800220#define OCP_SYSCLK_CFG 0xc416
hayeswang43779f82014-01-02 11:25:10 +0800221
222/* SRAM Register */
hayeswang65b82d62017-06-15 14:44:03 +0800223#define SRAM_GREEN_CFG 0x8011
hayeswang43779f82014-01-02 11:25:10 +0800224#define SRAM_LPF_CFG 0x8012
Hayes Wang195aae32021-04-16 16:04:35 +0800225#define SRAM_GPHY_FW_VER 0x801e
hayeswang43779f82014-01-02 11:25:10 +0800226#define SRAM_10M_AMP1 0x8080
227#define SRAM_10M_AMP2 0x8082
228#define SRAM_IMPEDANCE 0x8084
Hayes Wangaf142882019-10-21 11:41:13 +0800229#define SRAM_PHY_LOCK 0xb82e
hayeswangac718b62013-05-02 16:01:25 +0000230
231/* PLA_RCR */
232#define RCR_AAP 0x00000001
233#define RCR_APM 0x00000002
234#define RCR_AM 0x00000004
235#define RCR_AB 0x00000008
236#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
Hayes Wang195aae32021-04-16 16:04:35 +0800237#define SLOT_EN BIT(11)
238
239/* PLA_RCR1 */
240#define OUTER_VLAN BIT(7)
241#define INNER_VLAN BIT(6)
hayeswangac718b62013-05-02 16:01:25 +0000242
243/* PLA_RXFIFO_CTRL0 */
244#define RXFIFO_THR1_NORMAL 0x00080002
245#define RXFIFO_THR1_OOB 0x01800003
246
Hayes Wang195aae32021-04-16 16:04:35 +0800247/* PLA_RXFIFO_FULL */
248#define RXFIFO_FULL_MASK 0xfff
249
hayeswangac718b62013-05-02 16:01:25 +0000250/* PLA_RXFIFO_CTRL1 */
251#define RXFIFO_THR2_FULL 0x00000060
252#define RXFIFO_THR2_HIGH 0x00000038
253#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800254#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000255
256/* PLA_RXFIFO_CTRL2 */
257#define RXFIFO_THR3_FULL 0x00000078
258#define RXFIFO_THR3_HIGH 0x00000048
259#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800260#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000261
262/* PLA_TXFIFO_CTRL */
263#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800264#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000265
hayeswang65bab842015-02-12 16:20:46 +0800266/* PLA_DMY_REG0 */
267#define ECM_ALDPS 0x0002
268
hayeswangac718b62013-05-02 16:01:25 +0000269/* PLA_FMC */
270#define FMC_FCR_MCU_EN 0x0001
271
272/* PLA_EEEP_CR */
273#define EEEP_CR_EEEP_TX 0x0002
274
hayeswang43779f82014-01-02 11:25:10 +0800275/* PLA_WDT6_CTRL */
276#define WDT6_SET_MODE 0x0010
277
hayeswangac718b62013-05-02 16:01:25 +0000278/* PLA_TCR0 */
279#define TCR0_TX_EMPTY 0x0800
280#define TCR0_AUTO_FIFO 0x0080
281
282/* PLA_TCR1 */
283#define VERSION_MASK 0x7cf0
Hayes Wang5133bcc72021-04-16 16:04:32 +0800284#define IFG_MASK (BIT(3) | BIT(9) | BIT(8))
285#define IFG_144NS BIT(9)
286#define IFG_96NS (BIT(9) | BIT(8))
hayeswangac718b62013-05-02 16:01:25 +0000287
hayeswang69b4b7a2014-07-10 10:58:54 +0800288/* PLA_MTPS */
289#define MTPS_JUMBO (12 * 1024 / 64)
290#define MTPS_DEFAULT (6 * 1024 / 64)
291
hayeswang4f1d4d52014-03-11 16:24:19 +0800292/* PLA_RSTTALLY */
293#define TALLY_RESET 0x0001
294
hayeswangac718b62013-05-02 16:01:25 +0000295/* PLA_CR */
296#define CR_RST 0x10
297#define CR_RE 0x08
298#define CR_TE 0x04
299
300/* PLA_CRWECR */
301#define CRWECR_NORAML 0x00
302#define CRWECR_CONFIG 0xc0
303
304/* PLA_OOB_CTRL */
305#define NOW_IS_OOB 0x80
306#define TXFIFO_EMPTY 0x20
307#define RXFIFO_EMPTY 0x10
308#define LINK_LIST_READY 0x02
309#define DIS_MCU_CLROOB 0x01
310#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
311
312/* PLA_MISC_1 */
313#define RXDY_GATED_EN 0x0008
314
315/* PLA_SFF_STS_7 */
316#define RE_INIT_LL 0x8000
317#define MCU_BORW_EN 0x4000
318
319/* PLA_CPCR */
Hayes Wang195aae32021-04-16 16:04:35 +0800320#define FLOW_CTRL_EN BIT(0)
hayeswangac718b62013-05-02 16:01:25 +0000321#define CPCR_RX_VLAN 0x0040
322
323/* PLA_CFG_WOL */
324#define MAGIC_EN 0x0001
325
hayeswang43779f82014-01-02 11:25:10 +0800326/* PLA_TEREDO_CFG */
327#define TEREDO_SEL 0x8000
328#define TEREDO_WAKE_MASK 0x7f00
329#define TEREDO_RS_EVENT_MASK 0x00fe
330#define OOB_TEREDO_EN 0x0001
331
Kevin Lo59c0b472019-08-01 11:29:38 +0800332/* PLA_BDC_CR */
hayeswangac718b62013-05-02 16:01:25 +0000333#define ALDPS_PROXY_MODE 0x0001
334
hayeswang65b82d62017-06-15 14:44:03 +0800335/* PLA_EFUSE_CMD */
336#define EFUSE_READ_CMD BIT(15)
337#define EFUSE_DATA_BIT16 BIT(7)
338
hayeswang21ff2e82014-02-18 21:49:06 +0800339/* PLA_CONFIG34 */
340#define LINK_ON_WAKE_EN 0x0010
341#define LINK_OFF_WAKE_EN 0x0008
342
Hayes Wang19813162020-01-22 16:02:12 +0800343/* PLA_CONFIG6 */
344#define LANWAKE_CLR_EN BIT(0)
345
Hayes Wang195aae32021-04-16 16:04:35 +0800346/* PLA_USB_CFG */
347#define EN_XG_LIP BIT(1)
348#define EN_G_LIP BIT(2)
349
hayeswangac718b62013-05-02 16:01:25 +0000350/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800351#define BWF_EN 0x0040
352#define MWF_EN 0x0020
353#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000354#define LAN_WAKE_EN 0x0002
355
356/* PLA_LED_FEATURE */
357#define LED_MODE_MASK 0x0700
358
359/* PLA_PHY_PWR */
360#define TX_10M_IDLE_EN 0x0080
361#define PFM_PWM_SWITCH 0x0040
Hayes Wangd7f1b592020-01-22 16:02:10 +0800362#define TEST_IO_OFF BIT(4)
hayeswangac718b62013-05-02 16:01:25 +0000363
364/* PLA_MAC_PWR_CTRL */
365#define D3_CLK_GATED_EN 0x00004000
366#define MCU_CLK_RATIO 0x07010f07
367#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800368#define ALDPS_SPDWN_RATIO 0x0f87
369
370/* PLA_MAC_PWR_CTRL2 */
371#define EEE_SPDWN_RATIO 0x8007
hayeswang65b82d62017-06-15 14:44:03 +0800372#define MAC_CLK_SPDWN_EN BIT(15)
Hayes Wang195aae32021-04-16 16:04:35 +0800373#define EEE_SPDWN_RATIO_MASK 0xff
hayeswang43779f82014-01-02 11:25:10 +0800374
375/* PLA_MAC_PWR_CTRL3 */
Hayes Wang08997b52020-01-22 16:02:09 +0800376#define PLA_MCU_SPDWN_EN BIT(14)
hayeswang43779f82014-01-02 11:25:10 +0800377#define PKT_AVAIL_SPDWN_EN 0x0100
378#define SUSPEND_SPDWN_EN 0x0004
379#define U1U2_SPDWN_EN 0x0002
380#define L1_SPDWN_EN 0x0001
381
382/* PLA_MAC_PWR_CTRL4 */
383#define PWRSAVE_SPDWN_EN 0x1000
384#define RXDV_SPDWN_EN 0x0800
385#define TX10MIDLE_EN 0x0100
Hayes Wang195aae32021-04-16 16:04:35 +0800386#define IDLE_SPDWN_EN BIT(6)
hayeswang43779f82014-01-02 11:25:10 +0800387#define TP100_SPDWN_EN 0x0020
388#define TP500_SPDWN_EN 0x0010
389#define TP1000_SPDWN_EN 0x0008
390#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000391
392/* PLA_GPHY_INTR_IMR */
393#define GPHY_STS_MSK 0x0001
394#define SPEED_DOWN_MSK 0x0002
395#define SPDWN_RXDV_MSK 0x0004
396#define SPDWN_LINKCHG_MSK 0x0008
397
398/* PLA_PHYAR */
399#define PHYAR_FLAG 0x80000000
400
401/* PLA_EEE_CR */
402#define EEE_RX_EN 0x0001
403#define EEE_TX_EN 0x0002
404
hayeswang43779f82014-01-02 11:25:10 +0800405/* PLA_BOOT_CTRL */
406#define AUTOLOAD_DONE 0x0002
407
Hayes Wang19813162020-01-22 16:02:12 +0800408/* PLA_LWAKE_CTRL_REG */
409#define LANWAKE_PIN BIT(7)
410
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800411/* PLA_SUSPEND_FLAG */
412#define LINK_CHG_EVENT BIT(0)
413
414/* PLA_INDICATE_FALG */
415#define UPCOMING_RUNTIME_D3 BIT(0)
416
Hayes Wang9370f2d2019-10-16 11:02:42 +0800417/* PLA_MACDBG_PRE and PLA_MACDBG_POST */
418#define DEBUG_OE BIT(0)
419#define DEBUG_LTSSM 0x0082
420
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800421/* PLA_EXTRA_STATUS */
Hayes Wanga3914272020-01-22 16:02:05 +0800422#define CUR_LINK_OK BIT(15)
Hayes Wang9370f2d2019-10-16 11:02:42 +0800423#define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800424#define LINK_CHANGE_FLAG BIT(8)
Hayes Wanga3914272020-01-22 16:02:05 +0800425#define POLL_LINK_CHG BIT(0)
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800426
Hayes Wang195aae32021-04-16 16:04:35 +0800427/* PLA_GPHY_CTRL */
428#define GPHY_FLASH BIT(1)
429
430/* PLA_POL_GPIO_CTRL */
431#define DACK_DET_EN BIT(15)
432#define POL_GPHY_PATCH BIT(4)
433
hayeswang65bab842015-02-12 16:20:46 +0800434/* USB_USB2PHY */
435#define USB2PHY_SUSPEND 0x0001
436#define USB2PHY_L1 0x0002
437
Hayes Wangaa475d92020-01-22 16:02:13 +0800438/* USB_SSPHYLINK1 */
439#define DELAY_PHY_PWR_CHG BIT(1)
440
hayeswang65bab842015-02-12 16:20:46 +0800441/* USB_SSPHYLINK2 */
442#define pwd_dn_scale_mask 0x3ffe
443#define pwd_dn_scale(x) ((x) << 1)
444
445/* USB_CSR_DUMMY1 */
446#define DYNAMIC_BURST 0x0001
447
448/* USB_CSR_DUMMY2 */
449#define EP4_FULL_FC 0x0001
450
hayeswangac718b62013-05-02 16:01:25 +0000451/* USB_DEV_STAT */
452#define STAT_SPEED_MASK 0x0006
453#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800454#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000455
Hayes Wang9370f2d2019-10-16 11:02:42 +0800456/* USB_FW_FIX_EN0 */
457#define FW_FIX_SUSPEND BIT(14)
458
459/* USB_FW_FIX_EN1 */
460#define FW_IP_RESET_EN BIT(9)
461
hayeswang65b82d62017-06-15 14:44:03 +0800462/* USB_LPM_CONFIG */
463#define LPM_U1U2_EN BIT(0)
464
hayeswangac718b62013-05-02 16:01:25 +0000465/* USB_TX_AGG */
466#define TX_AGG_MAX_THRESHOLD 0x03
467
468/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800469#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800470#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800471#define RX_THR_SLOW 0xffff0180
hayeswang65b82d62017-06-15 14:44:03 +0800472#define RX_THR_B 0x00010001
hayeswangac718b62013-05-02 16:01:25 +0000473
474/* USB_TX_DMA */
475#define TEST_MODE_DISABLE 0x00000001
476#define TX_SIZE_ADJUST1 0x00000100
477
hayeswang93fe9b12016-06-16 10:55:18 +0800478/* USB_BMU_RESET */
479#define BMU_RESET_EP_IN 0x01
480#define BMU_RESET_EP_OUT 0x02
481
Hayes Wang195aae32021-04-16 16:04:35 +0800482/* USB_BMU_CONFIG */
483#define ACT_ODMA BIT(1)
484
hayeswang65b82d62017-06-15 14:44:03 +0800485/* USB_UPT_RXDMA_OWN */
486#define OWN_UPDATE BIT(0)
487#define OWN_CLEAR BIT(1)
488
Hayes Wang9370f2d2019-10-16 11:02:42 +0800489/* USB_FW_TASK */
490#define FC_PATCH_TASK BIT(1)
491
Hayes Wang195aae32021-04-16 16:04:35 +0800492/* USB_RX_AGGR_NUM */
493#define RX_AGGR_NUM_MASK 0x1ff
494
hayeswangac718b62013-05-02 16:01:25 +0000495/* USB_UPS_CTRL */
496#define POWER_CUT 0x0100
497
498/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800499#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000500
Hayes Wang195aae32021-04-16 16:04:35 +0800501/* USB_ECM_OPTION */
502#define BYPASS_MAC_RESET BIT(5)
503
Hayes Wang9370f2d2019-10-16 11:02:42 +0800504/* USB_CSTMR */
505#define FORCE_SUPER BIT(0)
506
Hayes Wang195aae32021-04-16 16:04:35 +0800507/* USB_MISC_2 */
508#define UPS_FORCE_PWR_DOWN BIT(0)
509
510/* USB_ECM_OP */
511#define EN_ALL_SPEED BIT(0)
512
513/* USB_GPHY_CTRL */
514#define GPHY_PATCH_DONE BIT(2)
515#define BYPASS_FLASH BIT(5)
516#define BACKUP_RESTRORE BIT(6)
517
518/* USB_SPEED_OPTION */
519#define RG_PWRDN_EN BIT(8)
520#define ALL_SPEED_OFF BIT(9)
521
Hayes Wang9370f2d2019-10-16 11:02:42 +0800522/* USB_FW_CTRL */
523#define FLOW_CTRL_PATCH_OPT BIT(1)
Hayes Wang195aae32021-04-16 16:04:35 +0800524#define AUTO_SPEEDUP BIT(3)
525#define FLOW_CTRL_PATCH_2 BIT(8)
Hayes Wang9370f2d2019-10-16 11:02:42 +0800526
527/* USB_FC_TIMER */
528#define CTRL_TIMER_EN BIT(15)
529
hayeswangac718b62013-05-02 16:01:25 +0000530/* USB_USB_CTRL */
Hayes Wang195aae32021-04-16 16:04:35 +0800531#define CDC_ECM_EN BIT(3)
hayeswangac718b62013-05-02 16:01:25 +0000532#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800533#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000534
hayeswang43779f82014-01-02 11:25:10 +0800535/* USB_U2P3_CTRL */
536#define U2P3_ENABLE 0x0001
Hayes Wang195aae32021-04-16 16:04:35 +0800537#define RX_DETECT8 BIT(3)
hayeswang43779f82014-01-02 11:25:10 +0800538
539/* USB_POWER_CUT */
540#define PWR_EN 0x0001
541#define PHASE2_EN 0x0008
hayeswang65b82d62017-06-15 14:44:03 +0800542#define UPS_EN BIT(4)
543#define USP_PREWAKE BIT(5)
hayeswang43779f82014-01-02 11:25:10 +0800544
545/* USB_MISC_0 */
546#define PCUT_STATUS 0x0001
547
hayeswang464ec102015-02-12 14:33:46 +0800548/* USB_RX_EARLY_TIMEOUT */
549#define COALESCE_SUPER 85000U
550#define COALESCE_HIGH 250000U
551#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800552
Hayes Wang9370f2d2019-10-16 11:02:42 +0800553/* USB_WDT1_CTRL */
554#define WTD1_EN BIT(0)
555
hayeswang43779f82014-01-02 11:25:10 +0800556/* USB_WDT11_CTRL */
557#define TIMER11_EN 0x0001
558
559/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800560/* bit 4 ~ 5: fifo empty boundary */
561#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
562/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800563#define LPM_TIMER_MASK 0x0c
564#define LPM_TIMER_500MS 0x04 /* 500 ms */
565#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800566#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800567
568/* USB_AFE_CTRL2 */
569#define SEN_VAL_MASK 0xf800
570#define SEN_VAL_NORMAL 0xa000
571#define SEL_RXIDLE 0x0100
572
Hayes Wang195aae32021-04-16 16:04:35 +0800573/* USB_UPHY_XTAL */
574#define OOBS_POLLING BIT(8)
575
hayeswang65b82d62017-06-15 14:44:03 +0800576/* USB_UPS_CFG */
577#define SAW_CNT_1MS_MASK 0x0fff
Hayes Wang195aae32021-04-16 16:04:35 +0800578#define MID_REVERSE BIT(5) /* RTL8156A */
hayeswang65b82d62017-06-15 14:44:03 +0800579
580/* USB_UPS_FLAGS */
581#define UPS_FLAGS_R_TUNE BIT(0)
582#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
583#define UPS_FLAGS_250M_CKDIV BIT(2)
584#define UPS_FLAGS_EN_ALDPS BIT(3)
585#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
Hayes Wang195aae32021-04-16 16:04:35 +0800586#define UPS_FLAGS_SPEED_MASK (0xf << 16)
hayeswang65b82d62017-06-15 14:44:03 +0800587#define ups_flags_speed(x) ((x) << 16)
588#define UPS_FLAGS_EN_EEE BIT(20)
589#define UPS_FLAGS_EN_500M_EEE BIT(21)
590#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
Hayes Wang0e5b36b2019-09-05 10:46:20 +0800591#define UPS_FLAGS_EEE_PLLOFF_100 BIT(23)
hayeswang65b82d62017-06-15 14:44:03 +0800592#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
593#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
594#define UPS_FLAGS_EN_GREEN BIT(26)
595#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
596
597enum spd_duplex {
Hayes Wang0e5b36b2019-09-05 10:46:20 +0800598 NWAY_10M_HALF,
hayeswang65b82d62017-06-15 14:44:03 +0800599 NWAY_10M_FULL,
600 NWAY_100M_HALF,
601 NWAY_100M_FULL,
602 NWAY_1000M_FULL,
603 FORCE_10M_HALF,
604 FORCE_10M_FULL,
605 FORCE_100M_HALF,
606 FORCE_100M_FULL,
Hayes Wang195aae32021-04-16 16:04:35 +0800607 FORCE_1000M_FULL,
608 NWAY_2500M_FULL,
hayeswang65b82d62017-06-15 14:44:03 +0800609};
610
hayeswangac718b62013-05-02 16:01:25 +0000611/* OCP_ALDPS_CONFIG */
612#define ENPWRSAVE 0x8000
613#define ENPDNPS 0x0200
614#define LINKENA 0x0100
615#define DIS_SDSAVE 0x0010
616
hayeswang43779f82014-01-02 11:25:10 +0800617/* OCP_PHY_STATUS */
618#define PHY_STAT_MASK 0x0007
hayeswangc564b872017-06-09 17:11:38 +0800619#define PHY_STAT_EXT_INIT 2
hayeswang43779f82014-01-02 11:25:10 +0800620#define PHY_STAT_LAN_ON 3
621#define PHY_STAT_PWRDN 5
622
hayeswang65b82d62017-06-15 14:44:03 +0800623/* OCP_NCTL_CFG */
624#define PGA_RETURN_EN BIT(1)
625
hayeswang43779f82014-01-02 11:25:10 +0800626/* OCP_POWER_CFG */
627#define EEE_CLKDIV_EN 0x8000
628#define EN_ALDPS 0x0004
629#define EN_10M_PLLOFF 0x0001
630
hayeswangac718b62013-05-02 16:01:25 +0000631/* OCP_EEE_CONFIG1 */
632#define RG_TXLPI_MSK_HFDUP 0x8000
633#define RG_MATCLR_EN 0x4000
634#define EEE_10_CAP 0x2000
635#define EEE_NWAY_EN 0x1000
636#define TX_QUIET_EN 0x0200
637#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800638#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800639#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000640#define RG_RXLPI_MSK_HFDUP 0x0008
641#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
642
643/* OCP_EEE_CONFIG2 */
644#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
645#define RG_DACQUIET_EN 0x0400
646#define RG_LDVQUIET_EN 0x0200
647#define RG_CKRSEL 0x0020
648#define RG_EEEPRG_EN 0x0010
649
650/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800651#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800652#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000653#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
654#define MSK_PH 0x0006 /* bit 0 ~ 3 */
655
656/* OCP_EEE_AR */
657/* bit[15:14] function */
658#define FUN_ADDR 0x0000
659#define FUN_DATA 0x4000
660/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000661
hayeswang43779f82014-01-02 11:25:10 +0800662/* OCP_EEE_CFG */
663#define CTAP_SHORT_EN 0x0040
664#define EEE10_EN 0x0010
665
666/* OCP_DOWN_SPEED */
hayeswang65b82d62017-06-15 14:44:03 +0800667#define EN_EEE_CMODE BIT(14)
668#define EN_EEE_1000 BIT(13)
669#define EN_EEE_100 BIT(12)
670#define EN_10M_CLKDIV BIT(11)
hayeswang43779f82014-01-02 11:25:10 +0800671#define EN_10M_BGOFF 0x0080
672
Hayes Wang195aae32021-04-16 16:04:35 +0800673/* OCP_10GBT_CTRL */
674#define RTL_ADV2_5G_F_R BIT(5) /* Advertise 2.5GBASE-T fast-retrain */
675
hayeswang2dd49e02015-09-07 11:57:44 +0800676/* OCP_PHY_STATE */
677#define TXDIS_STATE 0x01
678#define ABD_STATE 0x02
679
hayeswang65b82d62017-06-15 14:44:03 +0800680/* OCP_PHY_PATCH_STAT */
681#define PATCH_READY BIT(6)
682
683/* OCP_PHY_PATCH_CMD */
684#define PATCH_REQUEST BIT(4)
685
Hayes Wangaf142882019-10-21 11:41:13 +0800686/* OCP_PHY_LOCK */
687#define PATCH_LOCK BIT(0)
688
hayeswang43779f82014-01-02 11:25:10 +0800689/* OCP_ADC_CFG */
690#define CKADSEL_L 0x0100
691#define ADC_EN 0x0080
692#define EN_EMI_L 0x0040
693
hayeswang65b82d62017-06-15 14:44:03 +0800694/* OCP_SYSCLK_CFG */
Hayes Wang195aae32021-04-16 16:04:35 +0800695#define sysclk_div_expo(x) (min(x, 5) << 8)
696#define clk_div_expo(x) (min(x, 5) << 4)
hayeswang65b82d62017-06-15 14:44:03 +0800697
698/* SRAM_GREEN_CFG */
699#define GREEN_ETH_EN BIT(15)
700#define R_TUNE_EN BIT(11)
701
hayeswang43779f82014-01-02 11:25:10 +0800702/* SRAM_LPF_CFG */
703#define LPF_AUTO_TUNE 0x8000
704
705/* SRAM_10M_AMP1 */
706#define GDAC_IB_UPALL 0x0008
707
708/* SRAM_10M_AMP2 */
709#define AMP_DN 0x0200
710
711/* SRAM_IMPEDANCE */
712#define RX_DRIVING_MASK 0x6000
713
Hayes Wangaf142882019-10-21 11:41:13 +0800714/* SRAM_PHY_LOCK */
715#define PHY_PATCH_LOCK 0x0001
716
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500717/* MAC PASSTHRU */
718#define AD_MASK 0xfee0
Mario Limonciello9c273692018-12-11 08:16:14 -0600719#define BND_MASK 0x0004
David Chen8e29d232019-02-16 17:16:42 +0800720#define BD_MASK 0x0001
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500721#define EFUSE 0xcfdb
722#define PASS_THRU_MASK 0x1
723
Hayes Wang9370f2d2019-10-16 11:02:42 +0800724#define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */
725
hayeswangac718b62013-05-02 16:01:25 +0000726enum rtl_register_content {
Hayes Wang195aae32021-04-16 16:04:35 +0800727 _2500bps = BIT(10),
728 _1250bps = BIT(9),
729 _500bps = BIT(8),
730 _tx_flow = BIT(6),
731 _rx_flow = BIT(5),
hayeswang43779f82014-01-02 11:25:10 +0800732 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000733 _100bps = 0x08,
734 _10bps = 0x04,
735 LINK_STATUS = 0x02,
736 FULL_DUP = 0x01,
737};
738
Hayes Wang195aae32021-04-16 16:04:35 +0800739#define is_speed_2500(_speed) (((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS))
740#define is_flow_control(_speed) (((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow))
741
hayeswang1764bcd2014-08-28 10:24:18 +0800742#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800743#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800744#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800745#define TX_ALIGN 4
746#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800747
Hayes Wange4a50172019-08-13 11:42:09 +0800748#define RTL8152_RX_MAX_PENDING 4096
Hayes Wang47922fc2019-08-13 11:42:08 +0800749#define RTL8152_RXFG_HEADSZ 256
750
hayeswang40a82912013-08-14 20:54:40 +0800751#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800752
hayeswangb65c0c92017-06-21 11:25:18 +0800753#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800754#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800755#define RTL8152_TX_TIMEOUT (5 * HZ)
Hayes Wang67ce1a82021-04-16 16:04:34 +0800756#define mtu_to_size(m) ((m) + VLAN_ETH_HLEN + ETH_FCS_LEN)
757#define size_to_mtu(s) ((s) - VLAN_ETH_HLEN - ETH_FCS_LEN)
758#define rx_reserved_size(x) (mtu_to_size(x) + sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000759
760/* rtl8152 flags */
761enum rtl8152_flags {
762 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000763 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800764 WORK_ENABLE,
765 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800766 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800767 PHY_RESET,
Hayes Wangd2187f82019-08-19 14:40:36 +0800768 SCHEDULE_TASKLET,
hayeswang65b82d62017-06-15 14:44:03 +0800769 GREEN_ETHERNET,
Hayes Wangbaf33d72021-10-04 14:28:58 +0800770 RX_EPROTO,
hayeswangac718b62013-05-02 16:01:25 +0000771};
772
Kai-Heng Fengb4b771f2020-02-04 13:33:13 +0800773#define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
774#define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
775
hayeswang4f1d4d52014-03-11 16:24:19 +0800776struct tally_counter {
777 __le64 tx_packets;
778 __le64 rx_packets;
779 __le64 tx_errors;
780 __le32 rx_errors;
781 __le16 rx_missed;
782 __le16 align_errors;
783 __le32 tx_one_collision;
784 __le32 tx_multi_collision;
785 __le64 rx_unicast;
786 __le64 rx_broadcast;
787 __le32 rx_multicast;
788 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800789 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800790};
791
hayeswangac718b62013-05-02 16:01:25 +0000792struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800793 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000794#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800795
hayeswang500b6d72013-11-20 17:30:57 +0800796 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800797#define RD_UDP_CS BIT(23)
798#define RD_TCP_CS BIT(22)
799#define RD_IPV6_CS BIT(20)
800#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800801
hayeswang500b6d72013-11-20 17:30:57 +0800802 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800803#define IPF BIT(23) /* IP checksum fail */
804#define UDPF BIT(22) /* UDP checksum fail */
805#define TCPF BIT(21) /* TCP checksum fail */
806#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800807
hayeswang500b6d72013-11-20 17:30:57 +0800808 __le32 opts4;
809 __le32 opts5;
810 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000811};
812
813struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800814 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800815#define TX_FS BIT(31) /* First segment of a packet */
816#define TX_LS BIT(30) /* Final segment of a packet */
817#define GTSENDV4 BIT(28)
818#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800819#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800820#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800821#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800822
hayeswang500b6d72013-11-20 17:30:57 +0800823 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800824#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
825#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
826#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
827#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800828#define MSS_SHIFT 17
829#define MSS_MAX 0x7ffU
830#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800831#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800832#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000833};
834
hayeswangdff4e8a2013-08-16 16:09:33 +0800835struct r8152;
836
hayeswangebc2ec482013-08-14 20:54:38 +0800837struct rx_agg {
Hayes Wang252df8b2019-08-13 11:42:06 +0800838 struct list_head list, info_list;
hayeswangebc2ec482013-08-14 20:54:38 +0800839 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800840 struct r8152 *context;
Hayes Wangd55d7082019-08-13 11:42:07 +0800841 struct page *page;
hayeswangebc2ec482013-08-14 20:54:38 +0800842 void *buffer;
hayeswangebc2ec482013-08-14 20:54:38 +0800843};
844
845struct tx_agg {
846 struct list_head list;
847 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800848 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800849 void *buffer;
850 void *head;
851 u32 skb_num;
852 u32 skb_len;
853};
854
hayeswangac718b62013-05-02 16:01:25 +0000855struct r8152 {
856 unsigned long flags;
857 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800858 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800859 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000860 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800861 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800862 struct tx_agg tx_info[RTL8152_MAX_TX];
Hayes Wang47922fc2019-08-13 11:42:08 +0800863 struct list_head rx_info, rx_used;
hayeswangebc2ec482013-08-14 20:54:38 +0800864 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800865 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800866 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800867 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000868 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800869 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800870#ifdef CONFIG_PM_SLEEP
871 struct notifier_block pm_notifier;
872#endif
Hayes Wangd2187f82019-08-19 14:40:36 +0800873 struct tasklet_struct tx_tl;
hayeswangc81229c2014-01-02 11:22:42 +0800874
875 struct rtl_ops {
Prashant Malani151ea092019-10-02 14:09:33 -0700876 void (*init)(struct r8152 *tp);
877 int (*enable)(struct r8152 *tp);
878 void (*disable)(struct r8152 *tp);
879 void (*up)(struct r8152 *tp);
880 void (*down)(struct r8152 *tp);
881 void (*unload)(struct r8152 *tp);
882 int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee);
883 int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee);
884 bool (*in_nway)(struct r8152 *tp);
885 void (*hw_phy_cfg)(struct r8152 *tp);
hayeswang2609af12016-07-05 16:11:46 +0800886 void (*autosuspend_en)(struct r8152 *tp, bool enable);
Hayes Wang67ce1a82021-04-16 16:04:34 +0800887 void (*change_mtu)(struct r8152 *tp);
hayeswangc81229c2014-01-02 11:22:42 +0800888 } rtl_ops;
889
Hayes Wang0e5b36b2019-09-05 10:46:20 +0800890 struct ups_info {
Hayes Wang195aae32021-04-16 16:04:35 +0800891 u32 r_tune:1;
Hayes Wang0e5b36b2019-09-05 10:46:20 +0800892 u32 _10m_ckdiv:1;
893 u32 _250m_ckdiv:1;
894 u32 aldps:1;
895 u32 lite_mode:2;
896 u32 speed_duplex:4;
897 u32 eee:1;
898 u32 eee_lite:1;
899 u32 eee_ckdiv:1;
900 u32 eee_plloff_100:1;
901 u32 eee_plloff_giga:1;
902 u32 eee_cmod_lv:1;
903 u32 green:1;
904 u32 flow_control:1;
905 u32 ctap_short_off:1;
906 } ups_info;
907
Hayes Wang9370f2d2019-10-16 11:02:42 +0800908#define RTL_VER_SIZE 32
909
910 struct rtl_fw {
911 const char *fw_name;
912 const struct firmware *fw;
913
914 char version[RTL_VER_SIZE];
915 int (*pre_fw)(struct r8152 *tp);
916 int (*post_fw)(struct r8152 *tp);
917
918 bool retry;
919 } rtl_fw;
920
Hayes Wang252df8b2019-08-13 11:42:06 +0800921 atomic_t rx_count;
922
Hayes Wangf4a93be2019-08-23 15:33:40 +0800923 bool eee_en;
hayeswang40a82912013-08-14 20:54:40 +0800924 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800925 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000926 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800927 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800928 u32 coalesce;
Hayes Wang771efed2019-09-02 19:52:28 +0800929 u32 advertising;
Hayes Wangec5791c2019-08-13 11:42:05 +0800930 u32 rx_buf_sz;
Hayes Wange4a50172019-08-13 11:42:09 +0800931 u32 rx_copybreak;
932 u32 rx_pending;
Hayes Wang195aae32021-04-16 16:04:35 +0800933 u32 fc_pause_on, fc_pause_off;
Hayes Wange4a50172019-08-13 11:42:09 +0800934
Hayes Wangb67fda92021-06-17 18:00:15 +0800935 unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out;
936
Hayes Wang195aae32021-04-16 16:04:35 +0800937 u32 support_2500full:1;
Hayes Wang9c680112021-04-24 14:09:03 +0800938 u32 lenovo_macpassthru:1;
939 u32 dell_tb_rx_agg_bug:1;
hayeswangac718b62013-05-02 16:01:25 +0000940 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800941 u16 speed;
Hayes Wangf4a93be2019-08-23 15:33:40 +0800942 u16 eee_adv;
hayeswang40a82912013-08-14 20:54:40 +0800943 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000944 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800945 u8 duplex;
946 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000947};
948
Hayes Wang9370f2d2019-10-16 11:02:42 +0800949/**
950 * struct fw_block - block type and total length
951 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
952 * RTL_FW_USB and so on.
953 * @length: total length of the current block.
954 */
955struct fw_block {
956 __le32 type;
957 __le32 length;
958} __packed;
959
960/**
961 * struct fw_header - header of the firmware file
962 * @checksum: checksum of sha256 which is calculated from the whole file
963 * except the checksum field of the file. That is, calculate sha256
964 * from the version field to the end of the file.
965 * @version: version of this firmware.
966 * @blocks: the first firmware block of the file
967 */
968struct fw_header {
969 u8 checksum[32];
970 char version[RTL_VER_SIZE];
Gustavo A. R. Silvaee3bc9c2020-03-02 06:23:05 -0600971 struct fw_block blocks[];
Hayes Wang9370f2d2019-10-16 11:02:42 +0800972} __packed;
973
Hayes Wanga8a7be12021-04-16 16:04:33 +0800974enum rtl8152_fw_flags {
975 FW_FLAGS_USB = 0,
976 FW_FLAGS_PLA,
977 FW_FLAGS_START,
978 FW_FLAGS_STOP,
979 FW_FLAGS_NC,
Hayes Wang4a51b0e2021-04-16 16:04:36 +0800980 FW_FLAGS_NC1,
981 FW_FLAGS_NC2,
982 FW_FLAGS_UC2,
983 FW_FLAGS_UC,
984 FW_FLAGS_SPEED_UP,
985 FW_FLAGS_VER,
Hayes Wanga8a7be12021-04-16 16:04:33 +0800986};
987
Hayes Wang4a51b0e2021-04-16 16:04:36 +0800988enum rtl8152_fw_fixup_cmd {
989 FW_FIXUP_AND = 0,
990 FW_FIXUP_OR,
991 FW_FIXUP_NOT,
992 FW_FIXUP_XOR,
993};
994
995struct fw_phy_set {
996 __le16 addr;
997 __le16 data;
998} __packed;
999
1000struct fw_phy_speed_up {
1001 struct fw_block blk_hdr;
1002 __le16 fw_offset;
1003 __le16 version;
1004 __le16 fw_reg;
1005 __le16 reserved;
1006 char info[];
1007} __packed;
1008
1009struct fw_phy_ver {
1010 struct fw_block blk_hdr;
1011 struct fw_phy_set ver;
1012 __le32 reserved;
1013} __packed;
1014
1015struct fw_phy_fixup {
1016 struct fw_block blk_hdr;
1017 struct fw_phy_set setting;
1018 __le16 bit_cmd;
1019 __le16 reserved;
1020} __packed;
1021
1022struct fw_phy_union {
1023 struct fw_block blk_hdr;
1024 __le16 fw_offset;
1025 __le16 fw_reg;
1026 struct fw_phy_set pre_set[2];
1027 struct fw_phy_set bp[8];
1028 struct fw_phy_set bp_en;
1029 u8 pre_num;
1030 u8 bp_num;
1031 char info[];
1032} __packed;
1033
Hayes Wang9370f2d2019-10-16 11:02:42 +08001034/**
Hayes Wanga66edaa2019-10-21 11:41:10 +08001035 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
Hayes Wang9370f2d2019-10-16 11:02:42 +08001036 * The layout of the firmware block is:
Hayes Wanga66edaa2019-10-21 11:41:10 +08001037 * <struct fw_mac> + <info> + <firmware data>.
Lee Jones34e653e2020-11-02 11:45:04 +00001038 * @blk_hdr: firmware descriptor (type, length)
Hayes Wang9370f2d2019-10-16 11:02:42 +08001039 * @fw_offset: offset of the firmware binary data. The start address of
Hayes Wanga66edaa2019-10-21 11:41:10 +08001040 * the data would be the address of struct fw_mac + @fw_offset.
Hayes Wang9370f2d2019-10-16 11:02:42 +08001041 * @fw_reg: the register to load the firmware. Depends on chip.
1042 * @bp_ba_addr: the register to write break point base address. Depends on
1043 * chip.
1044 * @bp_ba_value: break point base address. Depends on chip.
1045 * @bp_en_addr: the register to write break point enabled mask. Depends
1046 * on chip.
1047 * @bp_en_value: break point enabled mask. Depends on the firmware.
1048 * @bp_start: the start register of break points. Depends on chip.
1049 * @bp_num: the break point number which needs to be set for this firmware.
1050 * Depends on the firmware.
1051 * @bp: break points. Depends on firmware.
Lee Jones34e653e2020-11-02 11:45:04 +00001052 * @reserved: reserved space (unused)
Hayes Wang9370f2d2019-10-16 11:02:42 +08001053 * @fw_ver_reg: the register to store the fw version.
1054 * @fw_ver_data: the firmware version of the current type.
1055 * @info: additional information for debugging, and is followed by the
1056 * binary data of firmware.
1057 */
Hayes Wanga66edaa2019-10-21 11:41:10 +08001058struct fw_mac {
Hayes Wang9370f2d2019-10-16 11:02:42 +08001059 struct fw_block blk_hdr;
1060 __le16 fw_offset;
1061 __le16 fw_reg;
1062 __le16 bp_ba_addr;
1063 __le16 bp_ba_value;
1064 __le16 bp_en_addr;
1065 __le16 bp_en_value;
1066 __le16 bp_start;
1067 __le16 bp_num;
1068 __le16 bp[16]; /* any value determined by firmware */
1069 __le32 reserved;
1070 __le16 fw_ver_reg;
1071 u8 fw_ver_data;
Gustavo A. R. Silvaee3bc9c2020-03-02 06:23:05 -06001072 char info[];
Hayes Wang9370f2d2019-10-16 11:02:42 +08001073} __packed;
1074
Hayes Wangaf142882019-10-21 11:41:13 +08001075/**
1076 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
1077 * This is used to set patch key when loading the firmware of PHY.
Lee Jones34e653e2020-11-02 11:45:04 +00001078 * @blk_hdr: firmware descriptor (type, length)
Hayes Wangaf142882019-10-21 11:41:13 +08001079 * @key_reg: the register to write the patch key.
1080 * @key_data: patch key.
Lee Jones34e653e2020-11-02 11:45:04 +00001081 * @reserved: reserved space (unused)
Hayes Wangaf142882019-10-21 11:41:13 +08001082 */
1083struct fw_phy_patch_key {
1084 struct fw_block blk_hdr;
1085 __le16 key_reg;
1086 __le16 key_data;
1087 __le32 reserved;
1088} __packed;
1089
1090/**
1091 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
1092 * The layout of the firmware block is:
1093 * <struct fw_phy_nc> + <info> + <firmware data>.
Lee Jones34e653e2020-11-02 11:45:04 +00001094 * @blk_hdr: firmware descriptor (type, length)
Hayes Wangaf142882019-10-21 11:41:13 +08001095 * @fw_offset: offset of the firmware binary data. The start address of
1096 * the data would be the address of struct fw_phy_nc + @fw_offset.
1097 * @fw_reg: the register to load the firmware. Depends on chip.
1098 * @ba_reg: the register to write the base address. Depends on chip.
1099 * @ba_data: base address. Depends on chip.
1100 * @patch_en_addr: the register of enabling patch mode. Depends on chip.
1101 * @patch_en_value: patch mode enabled mask. Depends on the firmware.
1102 * @mode_reg: the regitster of switching the mode.
Lee Jones9f078142020-11-02 11:45:09 +00001103 * @mode_pre: the mode needing to be set before loading the firmware.
1104 * @mode_post: the mode to be set when finishing to load the firmware.
Lee Jones34e653e2020-11-02 11:45:04 +00001105 * @reserved: reserved space (unused)
Hayes Wangaf142882019-10-21 11:41:13 +08001106 * @bp_start: the start register of break points. Depends on chip.
1107 * @bp_num: the break point number which needs to be set for this firmware.
1108 * Depends on the firmware.
1109 * @bp: break points. Depends on firmware.
1110 * @info: additional information for debugging, and is followed by the
1111 * binary data of firmware.
1112 */
1113struct fw_phy_nc {
1114 struct fw_block blk_hdr;
1115 __le16 fw_offset;
1116 __le16 fw_reg;
1117 __le16 ba_reg;
1118 __le16 ba_data;
1119 __le16 patch_en_addr;
1120 __le16 patch_en_value;
1121 __le16 mode_reg;
1122 __le16 mode_pre;
1123 __le16 mode_post;
1124 __le16 reserved;
1125 __le16 bp_start;
1126 __le16 bp_num;
1127 __le16 bp[4];
Gustavo A. R. Silvaee3bc9c2020-03-02 06:23:05 -06001128 char info[];
Hayes Wangaf142882019-10-21 11:41:13 +08001129} __packed;
1130
Hayes Wang9370f2d2019-10-16 11:02:42 +08001131enum rtl_fw_type {
1132 RTL_FW_END = 0,
1133 RTL_FW_PLA,
1134 RTL_FW_USB,
Hayes Wangaf142882019-10-21 11:41:13 +08001135 RTL_FW_PHY_START,
1136 RTL_FW_PHY_STOP,
1137 RTL_FW_PHY_NC,
Hayes Wang4a51b0e2021-04-16 16:04:36 +08001138 RTL_FW_PHY_FIXUP,
1139 RTL_FW_PHY_UNION_NC,
1140 RTL_FW_PHY_UNION_NC1,
1141 RTL_FW_PHY_UNION_NC2,
1142 RTL_FW_PHY_UNION_UC2,
1143 RTL_FW_PHY_UNION_UC,
1144 RTL_FW_PHY_UNION_MISC,
1145 RTL_FW_PHY_SPEED_UP,
1146 RTL_FW_PHY_VER,
Hayes Wang9370f2d2019-10-16 11:02:42 +08001147};
1148
hayeswangac718b62013-05-02 16:01:25 +00001149enum rtl_version {
1150 RTL_VER_UNKNOWN = 0,
1151 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +08001152 RTL_VER_02,
1153 RTL_VER_03,
1154 RTL_VER_04,
1155 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +08001156 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +08001157 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +08001158 RTL_VER_08,
1159 RTL_VER_09,
Hayes Wang195aae32021-04-16 16:04:35 +08001160
1161 RTL_TEST_01,
1162 RTL_VER_10,
1163 RTL_VER_11,
1164 RTL_VER_12,
1165 RTL_VER_13,
1166 RTL_VER_14,
1167 RTL_VER_15,
1168
hayeswang43779f82014-01-02 11:25:10 +08001169 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +00001170};
1171
hayeswang60c89072014-03-07 11:04:39 +08001172enum tx_csum_stat {
1173 TX_CSUM_SUCCESS = 0,
1174 TX_CSUM_TSO,
1175 TX_CSUM_NONE
1176};
1177
Hayes Wang771efed2019-09-02 19:52:28 +08001178#define RTL_ADVERTISED_10_HALF BIT(0)
1179#define RTL_ADVERTISED_10_FULL BIT(1)
1180#define RTL_ADVERTISED_100_HALF BIT(2)
1181#define RTL_ADVERTISED_100_FULL BIT(3)
1182#define RTL_ADVERTISED_1000_HALF BIT(4)
1183#define RTL_ADVERTISED_1000_FULL BIT(5)
Hayes Wang195aae32021-04-16 16:04:35 +08001184#define RTL_ADVERTISED_2500_FULL BIT(6)
Hayes Wang771efed2019-09-02 19:52:28 +08001185
hayeswangac718b62013-05-02 16:01:25 +00001186/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
1187 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
1188 */
1189static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +08001190static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +00001191
Hayes Wang67ce1a82021-04-16 16:04:34 +08001192#define RTL_LIMITED_TSO_SIZE (size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc))
hayeswang60c89072014-03-07 11:04:39 +08001193
hayeswangac718b62013-05-02 16:01:25 +00001194static
1195int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1196{
hayeswang31787f52013-07-31 17:21:25 +08001197 int ret;
1198 void *tmp;
1199
1200 tmp = kmalloc(size, GFP_KERNEL);
1201 if (!tmp)
1202 return -ENOMEM;
1203
Hayes Wangb67fda92021-06-17 18:00:15 +08001204 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_in,
hayeswangb209af92014-08-25 15:53:00 +08001205 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
1206 value, index, tmp, size, 500);
Prashant Malanif53a7ad2019-08-24 01:36:19 -07001207 if (ret < 0)
1208 memset(data, 0xff, size);
1209 else
1210 memcpy(data, tmp, size);
hayeswang31787f52013-07-31 17:21:25 +08001211
hayeswang31787f52013-07-31 17:21:25 +08001212 kfree(tmp);
1213
1214 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001215}
1216
1217static
1218int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1219{
hayeswang31787f52013-07-31 17:21:25 +08001220 int ret;
1221 void *tmp;
1222
Benoit Tainec4438f02014-05-26 17:21:23 +02001223 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +08001224 if (!tmp)
1225 return -ENOMEM;
1226
Hayes Wangb67fda92021-06-17 18:00:15 +08001227 ret = usb_control_msg(tp->udev, tp->pipe_ctrl_out,
hayeswangb209af92014-08-25 15:53:00 +08001228 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
1229 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +08001230
1231 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +08001232
hayeswang31787f52013-07-31 17:21:25 +08001233 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001234}
1235
Hayes Wangffa9fec2019-07-04 17:36:32 +08001236static void rtl_set_unplug(struct r8152 *tp)
1237{
1238 if (tp->udev->state == USB_STATE_NOTATTACHED) {
1239 set_bit(RTL8152_UNPLUG, &tp->flags);
1240 smp_mb__after_atomic();
1241 }
1242}
1243
hayeswangac718b62013-05-02 16:01:25 +00001244static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +08001245 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +00001246{
hayeswang45f4a192014-01-06 17:08:41 +08001247 u16 limit = 64;
1248 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00001249
1250 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1251 return -ENODEV;
1252
1253 /* both size and indix must be 4 bytes align */
1254 if ((size & 3) || !size || (index & 3) || !data)
1255 return -EPERM;
1256
1257 if ((u32)index + (u32)size > 0xffff)
1258 return -EPERM;
1259
1260 while (size) {
1261 if (size > limit) {
1262 ret = get_registers(tp, index, type, limit, data);
1263 if (ret < 0)
1264 break;
1265
1266 index += limit;
1267 data += limit;
1268 size -= limit;
1269 } else {
1270 ret = get_registers(tp, index, type, size, data);
1271 if (ret < 0)
1272 break;
1273
1274 index += size;
1275 data += size;
1276 size = 0;
1277 break;
1278 }
1279 }
1280
hayeswang67610492014-10-30 11:46:40 +08001281 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +08001282 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +08001283
hayeswangac718b62013-05-02 16:01:25 +00001284 return ret;
1285}
1286
1287static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +08001288 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +00001289{
hayeswang45f4a192014-01-06 17:08:41 +08001290 int ret;
1291 u16 byteen_start, byteen_end, byen;
1292 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +00001293
1294 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1295 return -ENODEV;
1296
1297 /* both size and indix must be 4 bytes align */
1298 if ((size & 3) || !size || (index & 3) || !data)
1299 return -EPERM;
1300
1301 if ((u32)index + (u32)size > 0xffff)
1302 return -EPERM;
1303
1304 byteen_start = byteen & BYTE_EN_START_MASK;
1305 byteen_end = byteen & BYTE_EN_END_MASK;
1306
1307 byen = byteen_start | (byteen_start << 4);
1308 ret = set_registers(tp, index, type | byen, 4, data);
1309 if (ret < 0)
1310 goto error1;
1311
1312 index += 4;
1313 data += 4;
1314 size -= 4;
1315
1316 if (size) {
1317 size -= 4;
1318
1319 while (size) {
1320 if (size > limit) {
1321 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +08001322 type | BYTE_EN_DWORD,
1323 limit, data);
hayeswangac718b62013-05-02 16:01:25 +00001324 if (ret < 0)
1325 goto error1;
1326
1327 index += limit;
1328 data += limit;
1329 size -= limit;
1330 } else {
1331 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +08001332 type | BYTE_EN_DWORD,
1333 size, data);
hayeswangac718b62013-05-02 16:01:25 +00001334 if (ret < 0)
1335 goto error1;
1336
1337 index += size;
1338 data += size;
1339 size = 0;
1340 break;
1341 }
1342 }
1343
1344 byen = byteen_end | (byteen_end >> 4);
1345 ret = set_registers(tp, index, type | byen, 4, data);
1346 if (ret < 0)
1347 goto error1;
1348 }
1349
1350error1:
hayeswang67610492014-10-30 11:46:40 +08001351 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +08001352 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +08001353
hayeswangac718b62013-05-02 16:01:25 +00001354 return ret;
1355}
1356
1357static inline
1358int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1359{
1360 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1361}
1362
1363static inline
1364int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1365{
1366 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1367}
1368
1369static inline
hayeswangac718b62013-05-02 16:01:25 +00001370int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1371{
1372 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1373}
1374
1375static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1376{
hayeswangc8826de2013-07-31 17:21:26 +08001377 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +00001378
hayeswangc8826de2013-07-31 17:21:26 +08001379 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +00001380
1381 return __le32_to_cpu(data);
1382}
1383
1384static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1385{
hayeswangc8826de2013-07-31 17:21:26 +08001386 __le32 tmp = __cpu_to_le32(data);
1387
1388 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001389}
1390
1391static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1392{
1393 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001394 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +08001395 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +00001396 u8 shift = index & 2;
1397
1398 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +08001399 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +00001400
hayeswangd8fbd272017-06-15 14:44:04 +08001401 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +00001402
hayeswangc8826de2013-07-31 17:21:26 +08001403 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001404 data >>= (shift * 8);
1405 data &= 0xffff;
1406
1407 return (u16)data;
1408}
1409
1410static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1411{
hayeswangc8826de2013-07-31 17:21:26 +08001412 u32 mask = 0xffff;
1413 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001414 u16 byen = BYTE_EN_WORD;
1415 u8 shift = index & 2;
1416
1417 data &= mask;
1418
1419 if (index & 2) {
1420 byen <<= shift;
1421 mask <<= (shift * 8);
1422 data <<= (shift * 8);
1423 index &= ~3;
1424 }
1425
hayeswangc8826de2013-07-31 17:21:26 +08001426 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001427
hayeswangc8826de2013-07-31 17:21:26 +08001428 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001429}
1430
1431static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1432{
1433 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001434 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001435 u8 shift = index & 3;
1436
1437 index &= ~3;
1438
hayeswangc8826de2013-07-31 17:21:26 +08001439 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001440
hayeswangc8826de2013-07-31 17:21:26 +08001441 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001442 data >>= (shift * 8);
1443 data &= 0xff;
1444
1445 return (u8)data;
1446}
1447
1448static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1449{
hayeswangc8826de2013-07-31 17:21:26 +08001450 u32 mask = 0xff;
1451 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001452 u16 byen = BYTE_EN_BYTE;
1453 u8 shift = index & 3;
1454
1455 data &= mask;
1456
1457 if (index & 3) {
1458 byen <<= shift;
1459 mask <<= (shift * 8);
1460 data <<= (shift * 8);
1461 index &= ~3;
1462 }
1463
hayeswangc8826de2013-07-31 17:21:26 +08001464 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001465
hayeswangc8826de2013-07-31 17:21:26 +08001466 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001467}
1468
hayeswangac244d32014-01-02 11:22:40 +08001469static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1470{
1471 u16 ocp_base, ocp_index;
1472
1473 ocp_base = addr & 0xf000;
1474 if (ocp_base != tp->ocp_base) {
1475 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1476 tp->ocp_base = ocp_base;
1477 }
1478
1479 ocp_index = (addr & 0x0fff) | 0xb000;
1480 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1481}
1482
hayeswange3fe0b12014-01-02 11:22:39 +08001483static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1484{
1485 u16 ocp_base, ocp_index;
1486
1487 ocp_base = addr & 0xf000;
1488 if (ocp_base != tp->ocp_base) {
1489 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1490 tp->ocp_base = ocp_base;
1491 }
1492
1493 ocp_index = (addr & 0x0fff) | 0xb000;
1494 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1495}
1496
hayeswangac244d32014-01-02 11:22:40 +08001497static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001498{
hayeswangac244d32014-01-02 11:22:40 +08001499 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001500}
1501
hayeswangac244d32014-01-02 11:22:40 +08001502static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001503{
hayeswangac244d32014-01-02 11:22:40 +08001504 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001505}
1506
hayeswang43779f82014-01-02 11:25:10 +08001507static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1508{
1509 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1510 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1511}
1512
hayeswang65b82d62017-06-15 14:44:03 +08001513static u16 sram_read(struct r8152 *tp, u16 addr)
1514{
1515 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1516 return ocp_reg_read(tp, OCP_SRAM_DATA);
1517}
1518
hayeswangac718b62013-05-02 16:01:25 +00001519static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1520{
1521 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001522 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001523
hayeswang68714382014-04-11 17:54:31 +08001524 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1525 return -ENODEV;
1526
hayeswangac718b62013-05-02 16:01:25 +00001527 if (phy_id != R8152_PHY_ID)
1528 return -EINVAL;
1529
hayeswang9a4be1b2014-02-18 21:49:07 +08001530 ret = r8152_mdio_read(tp, reg);
1531
hayeswang9a4be1b2014-02-18 21:49:07 +08001532 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001533}
1534
1535static
1536void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1537{
1538 struct r8152 *tp = netdev_priv(netdev);
1539
hayeswang68714382014-04-11 17:54:31 +08001540 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1541 return;
1542
hayeswangac718b62013-05-02 16:01:25 +00001543 if (phy_id != R8152_PHY_ID)
1544 return;
1545
1546 r8152_mdio_write(tp, reg, val);
1547}
1548
hayeswangb209af92014-08-25 15:53:00 +08001549static int
1550r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001551
Hayes Wang80fd8502021-02-03 17:14:29 +08001552static int
1553rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
1554 u32 advertising);
1555
Takashi Iwai776ac632021-07-14 19:00:22 +02001556static int __rtl8152_set_mac_address(struct net_device *netdev, void *p,
1557 bool in_resume)
hayeswang8ba789a2014-09-04 16:15:41 +08001558{
1559 struct r8152 *tp = netdev_priv(netdev);
1560 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001561 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001562
1563 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001564 goto out1;
1565
Takashi Iwai776ac632021-07-14 19:00:22 +02001566 if (!in_resume) {
1567 ret = usb_autopm_get_interface(tp->intf);
1568 if (ret < 0)
1569 goto out1;
1570 }
hayeswang8ba789a2014-09-04 16:15:41 +08001571
hayeswangb5403272014-10-09 18:00:26 +08001572 mutex_lock(&tp->control);
1573
Jakub Kicinski49ed8dd2021-10-04 09:05:22 -07001574 eth_hw_addr_set(netdev, addr->sa_data);
hayeswang8ba789a2014-09-04 16:15:41 +08001575
1576 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1577 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1578 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1579
hayeswangb5403272014-10-09 18:00:26 +08001580 mutex_unlock(&tp->control);
1581
Takashi Iwai776ac632021-07-14 19:00:22 +02001582 if (!in_resume)
1583 usb_autopm_put_interface(tp->intf);
hayeswangea6a7112014-10-02 17:03:12 +08001584out1:
1585 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001586}
1587
Takashi Iwai776ac632021-07-14 19:00:22 +02001588static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1589{
1590 return __rtl8152_set_mac_address(netdev, p, false);
1591}
1592
Mario Limonciello9c273692018-12-11 08:16:14 -06001593/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001594 * host system provided MAC address.
1595 * Examples of this are Dell TB15 and Dell WD15 docks
1596 */
1597static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1598{
1599 acpi_status status;
1600 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1601 union acpi_object *obj;
1602 int ret = -EINVAL;
1603 u32 ocp_data;
1604 unsigned char buf[6];
Kai-Heng Feng96477222019-11-05 19:24:52 +08001605 char *mac_obj_name;
1606 acpi_object_type mac_obj_type;
1607 int mac_strlen;
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001608
Hayes Wang9c680112021-04-24 14:09:03 +08001609 if (tp->lenovo_macpassthru) {
Kai-Heng Feng96477222019-11-05 19:24:52 +08001610 mac_obj_name = "\\MACA";
1611 mac_obj_type = ACPI_TYPE_STRING;
1612 mac_strlen = 0x16;
Mario Limonciello9c273692018-12-11 08:16:14 -06001613 } else {
Kai-Heng Feng96477222019-11-05 19:24:52 +08001614 /* test for -AD variant of RTL8153 */
1615 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1616 if ((ocp_data & AD_MASK) == 0x1000) {
1617 /* test for MAC address pass-through bit */
1618 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1619 if ((ocp_data & PASS_THRU_MASK) != 1) {
1620 netif_dbg(tp, probe, tp->netdev,
1621 "No efuse for RTL8153-AD MAC pass through\n");
1622 return -ENODEV;
1623 }
1624 } else {
1625 /* test for RTL8153-BND and RTL8153-BD */
1626 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1627 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1628 netif_dbg(tp, probe, tp->netdev,
1629 "Invalid variant for MAC pass through\n");
1630 return -ENODEV;
1631 }
Mario Limonciello9c273692018-12-11 08:16:14 -06001632 }
Kai-Heng Feng96477222019-11-05 19:24:52 +08001633
1634 mac_obj_name = "\\_SB.AMAC";
1635 mac_obj_type = ACPI_TYPE_BUFFER;
1636 mac_strlen = 0x17;
Mario Limonciello9c273692018-12-11 08:16:14 -06001637 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001638
1639 /* returns _AUXMAC_#AABBCCDDEEFF# */
Kai-Heng Feng96477222019-11-05 19:24:52 +08001640 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001641 obj = (union acpi_object *)buffer.pointer;
1642 if (!ACPI_SUCCESS(status))
1643 return -ENODEV;
Kai-Heng Feng96477222019-11-05 19:24:52 +08001644 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001645 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001646 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001647 obj->type, obj->string.length);
1648 goto amacout;
1649 }
Kai-Heng Feng96477222019-11-05 19:24:52 +08001650
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001651 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1652 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1653 netif_warn(tp, probe, tp->netdev,
1654 "Invalid header when reading pass-thru MAC addr\n");
1655 goto amacout;
1656 }
1657 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1658 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1659 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001660 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1661 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001662 ret = -EINVAL;
1663 goto amacout;
1664 }
1665 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001666 netif_info(tp, probe, tp->netdev,
1667 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1668
1669amacout:
1670 kfree(obj);
1671 return ret;
1672}
1673
Mario Limonciello25766272019-04-04 13:46:53 -05001674static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1675{
1676 struct net_device *dev = tp->netdev;
1677 int ret;
1678
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001679 sa->sa_family = dev->type;
1680
Thierry Redingb9b40ee2020-08-07 09:36:32 +02001681 ret = eth_platform_get_mac_address(&tp->udev->dev, sa->sa_data);
Thierry Redingacb6d372020-05-14 14:38:48 +02001682 if (ret < 0) {
1683 if (tp->version == RTL_VER_01) {
1684 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1685 } else {
1686 /* if device doesn't support MAC pass through this will
1687 * be expected to be non-zero
1688 */
1689 ret = vendor_mac_passthru_addr_read(tp, sa);
1690 if (ret < 0)
1691 ret = pla_ocp_read(tp, PLA_BACKUP, 8,
1692 sa->sa_data);
1693 }
Mario Limonciello25766272019-04-04 13:46:53 -05001694 }
1695
1696 if (ret < 0) {
1697 netif_err(tp, probe, dev, "Get ether addr fail\n");
1698 } else if (!is_valid_ether_addr(sa->sa_data)) {
1699 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1700 sa->sa_data);
1701 eth_hw_addr_random(dev);
1702 ether_addr_copy(sa->sa_data, dev->dev_addr);
1703 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1704 sa->sa_data);
1705 return 0;
1706 }
1707
1708 return ret;
1709}
1710
Takashi Iwai776ac632021-07-14 19:00:22 +02001711static int set_ethernet_addr(struct r8152 *tp, bool in_resume)
hayeswangac718b62013-05-02 16:01:25 +00001712{
1713 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001714 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001715 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001716
Mario Limonciello25766272019-04-04 13:46:53 -05001717 ret = determine_ethernet_addr(tp, &sa);
1718 if (ret < 0)
1719 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001720
Mario Limonciello25766272019-04-04 13:46:53 -05001721 if (tp->version == RTL_VER_01)
Jakub Kicinskiaf804e62021-10-01 14:32:24 -07001722 eth_hw_addr_set(dev, sa.sa_data);
Mario Limonciello25766272019-04-04 13:46:53 -05001723 else
Takashi Iwai776ac632021-07-14 19:00:22 +02001724 ret = __rtl8152_set_mac_address(dev, &sa, in_resume);
hayeswang179bb6d2014-09-04 16:15:42 +08001725
1726 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001727}
1728
hayeswangac718b62013-05-02 16:01:25 +00001729static void read_bulk_callback(struct urb *urb)
1730{
hayeswangac718b62013-05-02 16:01:25 +00001731 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001732 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001733 struct rx_agg *agg;
1734 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001735 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001736
hayeswangebc2ec482013-08-14 20:54:38 +08001737 agg = urb->context;
1738 if (!agg)
1739 return;
1740
1741 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001742 if (!tp)
1743 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001744
hayeswangac718b62013-05-02 16:01:25 +00001745 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1746 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001747
1748 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001749 return;
1750
hayeswangebc2ec482013-08-14 20:54:38 +08001751 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001752
1753 /* When link down, the driver would cancel all bulks. */
1754 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001755 if (!netif_carrier_ok(netdev))
1756 return;
1757
hayeswang9a4be1b2014-02-18 21:49:07 +08001758 usb_mark_last_busy(tp->udev);
1759
hayeswangac718b62013-05-02 16:01:25 +00001760 switch (status) {
1761 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001762 if (urb->actual_length < ETH_ZLEN)
1763 break;
1764
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001765 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001766 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001767 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001768 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001769 return;
hayeswangac718b62013-05-02 16:01:25 +00001770 case -ESHUTDOWN:
Hayes Wangffa9fec2019-07-04 17:36:32 +08001771 rtl_set_unplug(tp);
hayeswangac718b62013-05-02 16:01:25 +00001772 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001773 return;
Hayes Wangbaf33d72021-10-04 14:28:58 +08001774 case -EPROTO:
1775 urb->actual_length = 0;
1776 spin_lock_irqsave(&tp->rx_lock, flags);
1777 list_add_tail(&agg->list, &tp->rx_done);
1778 spin_unlock_irqrestore(&tp->rx_lock, flags);
1779 set_bit(RX_EPROTO, &tp->flags);
1780 schedule_delayed_work(&tp->schedule, 1);
1781 return;
hayeswangac718b62013-05-02 16:01:25 +00001782 case -ENOENT:
1783 return; /* the urb is in unlink state */
1784 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001785 if (net_ratelimit())
1786 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001787 break;
hayeswangac718b62013-05-02 16:01:25 +00001788 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001789 if (net_ratelimit())
1790 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001791 break;
hayeswangac718b62013-05-02 16:01:25 +00001792 }
1793
hayeswanga0fccd42014-11-20 10:29:05 +08001794 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001795}
1796
1797static void write_bulk_callback(struct urb *urb)
1798{
hayeswangebc2ec482013-08-14 20:54:38 +08001799 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001800 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001801 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001802 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001803 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001804 int status = urb->status;
1805
hayeswangebc2ec482013-08-14 20:54:38 +08001806 agg = urb->context;
1807 if (!agg)
1808 return;
1809
1810 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001811 if (!tp)
1812 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001813
hayeswangd104eaf2014-03-06 15:07:17 +08001814 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001815 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001816 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001817 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001818 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001819 stats->tx_errors += agg->skb_num;
1820 } else {
1821 stats->tx_packets += agg->skb_num;
1822 stats->tx_bytes += agg->skb_len;
1823 }
1824
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001825 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001826 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001827 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001828
hayeswang9a4be1b2014-02-18 21:49:07 +08001829 usb_autopm_put_interface_async(tp->intf);
1830
hayeswangd104eaf2014-03-06 15:07:17 +08001831 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001832 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001833
1834 if (!test_bit(WORK_ENABLE, &tp->flags))
1835 return;
1836
1837 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1838 return;
1839
1840 if (!skb_queue_empty(&tp->tx_queue))
Hayes Wangd2187f82019-08-19 14:40:36 +08001841 tasklet_schedule(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08001842}
1843
hayeswang40a82912013-08-14 20:54:40 +08001844static void intr_callback(struct urb *urb)
1845{
1846 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001847 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001848 int status = urb->status;
1849 int res;
1850
1851 tp = urb->context;
1852 if (!tp)
1853 return;
1854
1855 if (!test_bit(WORK_ENABLE, &tp->flags))
1856 return;
1857
1858 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1859 return;
1860
1861 switch (status) {
1862 case 0: /* success */
1863 break;
1864 case -ECONNRESET: /* unlink */
1865 case -ESHUTDOWN:
1866 netif_device_detach(tp->netdev);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001867 fallthrough;
hayeswang40a82912013-08-14 20:54:40 +08001868 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001869 case -EPROTO:
1870 netif_info(tp, intr, tp->netdev,
1871 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001872 return;
1873 case -EOVERFLOW:
1874 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1875 goto resubmit;
1876 /* -EPIPE: should clear the halt */
1877 default:
1878 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1879 goto resubmit;
1880 }
1881
1882 d = urb->transfer_buffer;
1883 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001884 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001885 set_bit(RTL8152_LINK_CHG, &tp->flags);
1886 schedule_delayed_work(&tp->schedule, 0);
1887 }
1888 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001889 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001890 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001891 set_bit(RTL8152_LINK_CHG, &tp->flags);
1892 schedule_delayed_work(&tp->schedule, 0);
1893 }
1894 }
1895
1896resubmit:
1897 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001898 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08001899 rtl_set_unplug(tp);
hayeswang40a82912013-08-14 20:54:40 +08001900 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001901 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001902 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001903 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001904 }
hayeswang40a82912013-08-14 20:54:40 +08001905}
1906
hayeswangebc2ec482013-08-14 20:54:38 +08001907static inline void *rx_agg_align(void *data)
1908{
hayeswang8e1f51b2014-01-02 11:22:41 +08001909 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001910}
1911
1912static inline void *tx_agg_align(void *data)
1913{
hayeswang8e1f51b2014-01-02 11:22:41 +08001914 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001915}
1916
Hayes Wang252df8b2019-08-13 11:42:06 +08001917static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1918{
1919 list_del(&agg->info_list);
1920
1921 usb_free_urb(agg->urb);
Hayes Wang47922fc2019-08-13 11:42:08 +08001922 put_page(agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001923 kfree(agg);
1924
1925 atomic_dec(&tp->rx_count);
1926}
1927
1928static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1929{
1930 struct net_device *netdev = tp->netdev;
1931 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
Hayes Wangd55d7082019-08-13 11:42:07 +08001932 unsigned int order = get_order(tp->rx_buf_sz);
Hayes Wang252df8b2019-08-13 11:42:06 +08001933 struct rx_agg *rx_agg;
1934 unsigned long flags;
Hayes Wang252df8b2019-08-13 11:42:06 +08001935
1936 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1937 if (!rx_agg)
1938 return NULL;
1939
Hayes Wang47922fc2019-08-13 11:42:08 +08001940 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
Hayes Wangd55d7082019-08-13 11:42:07 +08001941 if (!rx_agg->page)
Hayes Wang252df8b2019-08-13 11:42:06 +08001942 goto free_rx;
1943
Hayes Wangd55d7082019-08-13 11:42:07 +08001944 rx_agg->buffer = page_address(rx_agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001945
1946 rx_agg->urb = usb_alloc_urb(0, mflags);
1947 if (!rx_agg->urb)
1948 goto free_buf;
1949
1950 rx_agg->context = tp;
1951
1952 INIT_LIST_HEAD(&rx_agg->list);
1953 INIT_LIST_HEAD(&rx_agg->info_list);
1954 spin_lock_irqsave(&tp->rx_lock, flags);
1955 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1956 spin_unlock_irqrestore(&tp->rx_lock, flags);
1957
1958 atomic_inc(&tp->rx_count);
1959
1960 return rx_agg;
1961
1962free_buf:
Hayes Wangd55d7082019-08-13 11:42:07 +08001963 __free_pages(rx_agg->page, order);
Hayes Wang252df8b2019-08-13 11:42:06 +08001964free_rx:
1965 kfree(rx_agg);
1966 return NULL;
1967}
1968
hayeswangebc2ec482013-08-14 20:54:38 +08001969static void free_all_mem(struct r8152 *tp)
1970{
Hayes Wang252df8b2019-08-13 11:42:06 +08001971 struct rx_agg *agg, *agg_next;
1972 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001973 int i;
1974
Hayes Wang252df8b2019-08-13 11:42:06 +08001975 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001976
Hayes Wang252df8b2019-08-13 11:42:06 +08001977 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1978 free_rx_agg(tp, agg);
1979
1980 spin_unlock_irqrestore(&tp->rx_lock, flags);
1981
1982 WARN_ON(atomic_read(&tp->rx_count));
hayeswangebc2ec482013-08-14 20:54:38 +08001983
1984 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001985 usb_free_urb(tp->tx_info[i].urb);
1986 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001987
hayeswang9629e3c2014-01-15 10:42:15 +08001988 kfree(tp->tx_info[i].buffer);
1989 tp->tx_info[i].buffer = NULL;
1990 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001991 }
hayeswang40a82912013-08-14 20:54:40 +08001992
hayeswang9629e3c2014-01-15 10:42:15 +08001993 usb_free_urb(tp->intr_urb);
1994 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001995
hayeswang9629e3c2014-01-15 10:42:15 +08001996 kfree(tp->intr_buff);
1997 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001998}
1999
2000static int alloc_all_mem(struct r8152 *tp)
2001{
2002 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08002003 struct usb_interface *intf = tp->intf;
2004 struct usb_host_interface *alt = intf->cur_altsetting;
2005 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08002006 int node, i;
hayeswangebc2ec482013-08-14 20:54:38 +08002007
2008 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
2009
2010 spin_lock_init(&tp->rx_lock);
2011 spin_lock_init(&tp->tx_lock);
Hayes Wang252df8b2019-08-13 11:42:06 +08002012 INIT_LIST_HEAD(&tp->rx_info);
hayeswangebc2ec482013-08-14 20:54:38 +08002013 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08002014 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08002015 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08002016 skb_queue_head_init(&tp->rx_queue);
Hayes Wang252df8b2019-08-13 11:42:06 +08002017 atomic_set(&tp->rx_count, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002018
2019 for (i = 0; i < RTL8152_MAX_RX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08002020 if (!alloc_rx_agg(tp, GFP_KERNEL))
hayeswangebc2ec482013-08-14 20:54:38 +08002021 goto err1;
hayeswangebc2ec482013-08-14 20:54:38 +08002022 }
2023
2024 for (i = 0; i < RTL8152_MAX_TX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08002025 struct urb *urb;
2026 u8 *buf;
2027
hayeswang52aec122014-09-02 10:27:52 +08002028 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08002029 if (!buf)
2030 goto err1;
2031
2032 if (buf != tx_agg_align(buf)) {
2033 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08002034 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08002035 node);
hayeswangebc2ec482013-08-14 20:54:38 +08002036 if (!buf)
2037 goto err1;
2038 }
2039
2040 urb = usb_alloc_urb(0, GFP_KERNEL);
2041 if (!urb) {
2042 kfree(buf);
2043 goto err1;
2044 }
2045
2046 INIT_LIST_HEAD(&tp->tx_info[i].list);
2047 tp->tx_info[i].context = tp;
2048 tp->tx_info[i].urb = urb;
2049 tp->tx_info[i].buffer = buf;
2050 tp->tx_info[i].head = tx_agg_align(buf);
2051
2052 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
2053 }
2054
hayeswang40a82912013-08-14 20:54:40 +08002055 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2056 if (!tp->intr_urb)
2057 goto err1;
2058
2059 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
2060 if (!tp->intr_buff)
2061 goto err1;
2062
2063 tp->intr_interval = (int)ep_intr->desc.bInterval;
Hayes Wangb67fda92021-06-17 18:00:15 +08002064 usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr,
hayeswangb209af92014-08-25 15:53:00 +08002065 tp->intr_buff, INTBUFSIZE, intr_callback,
2066 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08002067
hayeswangebc2ec482013-08-14 20:54:38 +08002068 return 0;
2069
2070err1:
2071 free_all_mem(tp);
2072 return -ENOMEM;
2073}
2074
hayeswang0de98f62013-08-16 16:09:35 +08002075static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
2076{
2077 struct tx_agg *agg = NULL;
2078 unsigned long flags;
2079
hayeswang21949ab2014-03-07 11:04:35 +08002080 if (list_empty(&tp->tx_free))
2081 return NULL;
2082
hayeswang0de98f62013-08-16 16:09:35 +08002083 spin_lock_irqsave(&tp->tx_lock, flags);
2084 if (!list_empty(&tp->tx_free)) {
2085 struct list_head *cursor;
2086
2087 cursor = tp->tx_free.next;
2088 list_del_init(cursor);
2089 agg = list_entry(cursor, struct tx_agg, list);
2090 }
2091 spin_unlock_irqrestore(&tp->tx_lock, flags);
2092
2093 return agg;
2094}
2095
hayeswangb209af92014-08-25 15:53:00 +08002096/* r8152_csum_workaround()
Prashant Malanic01ebd62019-09-30 12:38:18 -07002097 * The hw limits the value of the transport offset. When the offset is out of
hayeswang6128d1bb2014-03-07 11:04:40 +08002098 * range, calculate the checksum by sw.
2099 */
2100static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
2101 struct sk_buff_head *list)
2102{
2103 if (skb_shinfo(skb)->gso_size) {
2104 netdev_features_t features = tp->netdev->features;
Jason A. Donenfeld1d7a7432020-01-08 16:59:04 -05002105 struct sk_buff *segs, *seg, *next;
hayeswang6128d1bb2014-03-07 11:04:40 +08002106 struct sk_buff_head seg_list;
hayeswang6128d1bb2014-03-07 11:04:40 +08002107
hayeswanga91d45f2014-07-11 16:48:27 +08002108 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08002109 segs = skb_gso_segment(skb, features);
2110 if (IS_ERR(segs) || !segs)
2111 goto drop;
2112
2113 __skb_queue_head_init(&seg_list);
2114
Jason A. Donenfeld1d7a7432020-01-08 16:59:04 -05002115 skb_list_walk_safe(segs, seg, next) {
2116 skb_mark_not_on_list(seg);
2117 __skb_queue_tail(&seg_list, seg);
2118 }
hayeswang6128d1bb2014-03-07 11:04:40 +08002119
2120 skb_queue_splice(&seg_list, list);
2121 dev_kfree_skb(skb);
2122 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2123 if (skb_checksum_help(skb) < 0)
2124 goto drop;
2125
2126 __skb_queue_head(list, skb);
2127 } else {
2128 struct net_device_stats *stats;
2129
2130drop:
2131 stats = &tp->netdev->stats;
2132 stats->tx_dropped++;
2133 dev_kfree_skb(skb);
2134 }
2135}
2136
hayeswangc5554292014-09-12 10:43:11 +08002137static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
2138{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002139 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08002140 u32 opts2;
2141
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002142 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08002143 desc->opts2 |= cpu_to_le32(opts2);
2144 }
2145}
2146
2147static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
2148{
2149 u32 opts2 = le32_to_cpu(desc->opts2);
2150
2151 if (opts2 & RX_VLAN_TAG)
2152 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2153 swab16(opts2 & 0xffff));
2154}
2155
hayeswang60c89072014-03-07 11:04:39 +08002156static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
2157 struct sk_buff *skb, u32 len, u32 transport_offset)
2158{
2159 u32 mss = skb_shinfo(skb)->gso_size;
2160 u32 opts1, opts2 = 0;
2161 int ret = TX_CSUM_SUCCESS;
2162
2163 WARN_ON_ONCE(len > TX_LEN_MAX);
2164
2165 opts1 = len | TX_FS | TX_LS;
2166
2167 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08002168 if (transport_offset > GTTCPHO_MAX) {
2169 netif_warn(tp, tx_err, tp->netdev,
2170 "Invalid transport offset 0x%x for TSO\n",
2171 transport_offset);
2172 ret = TX_CSUM_TSO;
2173 goto unavailable;
2174 }
2175
hayeswang6e74d172015-02-06 11:30:50 +08002176 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08002177 case htons(ETH_P_IP):
2178 opts1 |= GTSENDV4;
2179 break;
2180
hayeswang6128d1bb2014-03-07 11:04:40 +08002181 case htons(ETH_P_IPV6):
Heiner Kallweit23441202020-02-18 21:12:52 +01002182 if (skb_cow_head(skb, 0)) {
hayeswangfcb308d2014-03-11 10:20:32 +08002183 ret = TX_CSUM_TSO;
2184 goto unavailable;
2185 }
Heiner Kallweit23441202020-02-18 21:12:52 +01002186 tcp_v6_gso_csum_prep(skb);
hayeswang6128d1bb2014-03-07 11:04:40 +08002187 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08002188 break;
2189
hayeswang60c89072014-03-07 11:04:39 +08002190 default:
2191 WARN_ON_ONCE(1);
2192 break;
2193 }
2194
2195 opts1 |= transport_offset << GTTCPHO_SHIFT;
2196 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
2197 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08002198 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08002199
hayeswang6128d1bb2014-03-07 11:04:40 +08002200 if (transport_offset > TCPHO_MAX) {
2201 netif_warn(tp, tx_err, tp->netdev,
2202 "Invalid transport offset 0x%x\n",
2203 transport_offset);
2204 ret = TX_CSUM_NONE;
2205 goto unavailable;
2206 }
2207
hayeswang6e74d172015-02-06 11:30:50 +08002208 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08002209 case htons(ETH_P_IP):
2210 opts2 |= IPV4_CS;
2211 ip_protocol = ip_hdr(skb)->protocol;
2212 break;
2213
2214 case htons(ETH_P_IPV6):
2215 opts2 |= IPV6_CS;
2216 ip_protocol = ipv6_hdr(skb)->nexthdr;
2217 break;
2218
2219 default:
2220 ip_protocol = IPPROTO_RAW;
2221 break;
2222 }
2223
hayeswang60c89072014-03-07 11:04:39 +08002224 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08002225 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08002226 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08002227 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08002228 else
hayeswang5bd23882013-08-14 20:54:39 +08002229 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08002230
hayeswang60c89072014-03-07 11:04:39 +08002231 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08002232 }
hayeswang60c89072014-03-07 11:04:39 +08002233
2234 desc->opts2 = cpu_to_le32(opts2);
2235 desc->opts1 = cpu_to_le32(opts1);
2236
hayeswang6128d1bb2014-03-07 11:04:40 +08002237unavailable:
hayeswang60c89072014-03-07 11:04:39 +08002238 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08002239}
2240
hayeswangb1379d92013-08-16 16:09:37 +08002241static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
2242{
hayeswangd84130a2014-02-18 21:49:02 +08002243 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08002244 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08002245 u8 *tx_data;
2246
hayeswangd84130a2014-02-18 21:49:02 +08002247 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08002248 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002249 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08002250 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002251
hayeswangb1379d92013-08-16 16:09:37 +08002252 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08002253 agg->skb_num = 0;
2254 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08002255 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08002256
hayeswang7937f9e2013-11-20 17:30:54 +08002257 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08002258 struct tx_desc *tx_desc;
2259 struct sk_buff *skb;
2260 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08002261 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08002262
hayeswangd84130a2014-02-18 21:49:02 +08002263 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08002264 if (!skb)
2265 break;
2266
hayeswang60c89072014-03-07 11:04:39 +08002267 len = skb->len + sizeof(*tx_desc);
2268
2269 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08002270 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08002271 break;
2272 }
2273
hayeswang7937f9e2013-11-20 17:30:54 +08002274 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08002275 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08002276
2277 offset = (u32)skb_transport_offset(skb);
2278
hayeswang6128d1bb2014-03-07 11:04:40 +08002279 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
2280 r8152_csum_workaround(tp, skb, &skb_head);
2281 continue;
2282 }
hayeswang60c89072014-03-07 11:04:39 +08002283
hayeswangc5554292014-09-12 10:43:11 +08002284 rtl_tx_vlan_tag(tx_desc, skb);
2285
hayeswangb1379d92013-08-16 16:09:37 +08002286 tx_data += sizeof(*tx_desc);
2287
hayeswang60c89072014-03-07 11:04:39 +08002288 len = skb->len;
2289 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
2290 struct net_device_stats *stats = &tp->netdev->stats;
2291
2292 stats->tx_dropped++;
2293 dev_kfree_skb_any(skb);
2294 tx_data -= sizeof(*tx_desc);
2295 continue;
2296 }
hayeswangb1379d92013-08-16 16:09:37 +08002297
hayeswang7937f9e2013-11-20 17:30:54 +08002298 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08002299 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08002300 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08002301
2302 dev_kfree_skb_any(skb);
2303
hayeswang52aec122014-09-02 10:27:52 +08002304 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08002305
Hayes Wang9c680112021-04-24 14:09:03 +08002306 if (tp->dell_tb_rx_agg_bug)
Kai-Heng Feng0b165512018-01-16 16:46:27 +08002307 break;
hayeswangb1379d92013-08-16 16:09:37 +08002308 }
2309
hayeswangd84130a2014-02-18 21:49:02 +08002310 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08002311 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002312 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08002313 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002314 }
2315
hayeswang0c3121f2014-03-07 11:04:36 +08002316 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08002317
2318 if (netif_queue_stopped(tp->netdev) &&
2319 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
2320 netif_wake_queue(tp->netdev);
2321
hayeswang0c3121f2014-03-07 11:04:36 +08002322 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002323
hayeswang0c3121f2014-03-07 11:04:36 +08002324 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08002325 if (ret < 0)
2326 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08002327
Hayes Wangb67fda92021-06-17 18:00:15 +08002328 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out,
hayeswangb1379d92013-08-16 16:09:37 +08002329 agg->head, (int)(tx_data - (u8 *)agg->head),
2330 (usb_complete_t)write_bulk_callback, agg);
2331
hayeswang0c3121f2014-03-07 11:04:36 +08002332 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08002333 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08002334 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08002335
2336out_tx_fill:
2337 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08002338}
2339
hayeswang565cab02014-03-07 11:04:38 +08002340static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
2341{
2342 u8 checksum = CHECKSUM_NONE;
2343 u32 opts2, opts3;
2344
hayeswang19c0f402017-01-11 16:25:34 +08002345 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08002346 goto return_result;
2347
2348 opts2 = le32_to_cpu(rx_desc->opts2);
2349 opts3 = le32_to_cpu(rx_desc->opts3);
2350
2351 if (opts2 & RD_IPV4_CS) {
2352 if (opts3 & IPF)
2353 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08002354 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2355 checksum = CHECKSUM_UNNECESSARY;
2356 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08002357 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04002358 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08002359 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2360 checksum = CHECKSUM_UNNECESSARY;
2361 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2362 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08002363 }
2364
2365return_result:
2366 return checksum;
2367}
2368
Hayes Wang47922fc2019-08-13 11:42:08 +08002369static inline bool rx_count_exceed(struct r8152 *tp)
2370{
2371 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2372}
2373
2374static inline int agg_offset(struct rx_agg *agg, void *addr)
2375{
2376 return (int)(addr - agg->buffer);
2377}
2378
2379static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2380{
2381 struct rx_agg *agg, *agg_next, *agg_free = NULL;
2382 unsigned long flags;
2383
2384 spin_lock_irqsave(&tp->rx_lock, flags);
2385
2386 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2387 if (page_count(agg->page) == 1) {
2388 if (!agg_free) {
2389 list_del_init(&agg->list);
2390 agg_free = agg;
2391 continue;
2392 }
2393 if (rx_count_exceed(tp)) {
2394 list_del_init(&agg->list);
2395 free_rx_agg(tp, agg);
2396 }
2397 break;
2398 }
2399 }
2400
2401 spin_unlock_irqrestore(&tp->rx_lock, flags);
2402
Hayes Wange4a50172019-08-13 11:42:09 +08002403 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
Hayes Wang47922fc2019-08-13 11:42:08 +08002404 agg_free = alloc_rx_agg(tp, mflags);
2405
2406 return agg_free;
2407}
2408
hayeswangd823ab62015-01-12 12:06:23 +08002409static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08002410{
hayeswanga5a4f462013-08-16 16:09:34 +08002411 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08002412 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08002413 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08002414 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08002415
2416 if (!skb_queue_empty(&tp->rx_queue)) {
2417 while (work_done < budget) {
2418 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2419 struct net_device *netdev = tp->netdev;
2420 struct net_device_stats *stats = &netdev->stats;
2421 unsigned int pkt_len;
2422
2423 if (!skb)
2424 break;
2425
2426 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08002427 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002428 work_done++;
2429 stats->rx_packets++;
2430 stats->rx_bytes += pkt_len;
2431 }
2432 }
hayeswangebc2ec482013-08-14 20:54:38 +08002433
hayeswangd84130a2014-02-18 21:49:02 +08002434 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08002435 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08002436
Hayes Wangbaf33d72021-10-04 14:28:58 +08002437 clear_bit(RX_EPROTO, &tp->flags);
hayeswangd84130a2014-02-18 21:49:02 +08002438 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08002439 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08002440 list_splice_init(&tp->rx_done, &rx_queue);
2441 spin_unlock_irqrestore(&tp->rx_lock, flags);
2442
2443 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08002444 struct rx_desc *rx_desc;
Hayes Wang47922fc2019-08-13 11:42:08 +08002445 struct rx_agg *agg, *agg_free;
hayeswang43a44782013-08-16 16:09:36 +08002446 int len_used = 0;
2447 struct urb *urb;
2448 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08002449
hayeswangebc2ec482013-08-14 20:54:38 +08002450 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08002451
2452 agg = list_entry(cursor, struct rx_agg, list);
2453 urb = agg->urb;
Hayes Wangbaf33d72021-10-04 14:28:58 +08002454 if (urb->status != 0 || urb->actual_length < ETH_ZLEN)
hayeswang0de98f62013-08-16 16:09:35 +08002455 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08002456
Hayes Wang47922fc2019-08-13 11:42:08 +08002457 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2458
Hayes Wangd55d7082019-08-13 11:42:07 +08002459 rx_desc = agg->buffer;
2460 rx_data = agg->buffer;
hayeswang7937f9e2013-11-20 17:30:54 +08002461 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002462
hayeswang7937f9e2013-11-20 17:30:54 +08002463 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08002464 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08002465 struct net_device_stats *stats = &netdev->stats;
Hayes Wang47922fc2019-08-13 11:42:08 +08002466 unsigned int pkt_len, rx_frag_head_sz;
hayeswang43a44782013-08-16 16:09:36 +08002467 struct sk_buff *skb;
2468
Zheng Yongjunf62c4f32021-06-01 22:18:13 +08002469 /* limit the skb numbers for rx_queue */
hayeswang74544452017-06-09 17:11:47 +08002470 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2471 break;
2472
hayeswang7937f9e2013-11-20 17:30:54 +08002473 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08002474 if (pkt_len < ETH_ZLEN)
2475 break;
2476
hayeswang7937f9e2013-11-20 17:30:54 +08002477 len_used += pkt_len;
2478 if (urb->actual_length < len_used)
2479 break;
2480
hayeswangb65c0c92017-06-21 11:25:18 +08002481 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08002482 rx_data += sizeof(struct rx_desc);
2483
Hayes Wange4a50172019-08-13 11:42:09 +08002484 if (!agg_free || tp->rx_copybreak > pkt_len)
Hayes Wang47922fc2019-08-13 11:42:08 +08002485 rx_frag_head_sz = pkt_len;
2486 else
Hayes Wange4a50172019-08-13 11:42:09 +08002487 rx_frag_head_sz = tp->rx_copybreak;
Hayes Wang47922fc2019-08-13 11:42:08 +08002488
2489 skb = napi_alloc_skb(napi, rx_frag_head_sz);
hayeswangebc2ec482013-08-14 20:54:38 +08002490 if (!skb) {
2491 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08002492 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08002493 }
hayeswang565cab02014-03-07 11:04:38 +08002494
2495 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
Hayes Wang47922fc2019-08-13 11:42:08 +08002496 memcpy(skb->data, rx_data, rx_frag_head_sz);
2497 skb_put(skb, rx_frag_head_sz);
2498 pkt_len -= rx_frag_head_sz;
2499 rx_data += rx_frag_head_sz;
2500 if (pkt_len) {
2501 skb_add_rx_frag(skb, 0, agg->page,
2502 agg_offset(agg, rx_data),
2503 pkt_len,
2504 SKB_DATA_ALIGN(pkt_len));
2505 get_page(agg->page);
2506 }
2507
hayeswangebc2ec482013-08-14 20:54:38 +08002508 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08002509 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002510 if (work_done < budget) {
hayeswangd823ab62015-01-12 12:06:23 +08002511 work_done++;
2512 stats->rx_packets++;
Hayes Wang47922fc2019-08-13 11:42:08 +08002513 stats->rx_bytes += skb->len;
Hayes Wang6636fb32019-08-19 11:15:19 +08002514 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002515 } else {
2516 __skb_queue_tail(&tp->rx_queue, skb);
2517 }
hayeswangebc2ec482013-08-14 20:54:38 +08002518
hayeswang5e2f7482014-03-07 11:04:37 +08002519find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08002520 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08002521 rx_desc = (struct rx_desc *)rx_data;
Hayes Wang47922fc2019-08-13 11:42:08 +08002522 len_used = agg_offset(agg, rx_data);
hayeswang7937f9e2013-11-20 17:30:54 +08002523 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002524 }
2525
Hayes Wang47922fc2019-08-13 11:42:08 +08002526 WARN_ON(!agg_free && page_count(agg->page) > 1);
2527
2528 if (agg_free) {
2529 spin_lock_irqsave(&tp->rx_lock, flags);
2530 if (page_count(agg->page) == 1) {
2531 list_add(&agg_free->list, &tp->rx_used);
2532 } else {
2533 list_add_tail(&agg->list, &tp->rx_used);
2534 agg = agg_free;
2535 urb = agg->urb;
2536 }
2537 spin_unlock_irqrestore(&tp->rx_lock, flags);
2538 }
2539
hayeswang0de98f62013-08-16 16:09:35 +08002540submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002541 if (!ret) {
2542 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2543 } else {
2544 urb->actual_length = 0;
2545 list_add_tail(&agg->list, next);
2546 }
2547 }
2548
2549 if (!list_empty(&rx_queue)) {
2550 spin_lock_irqsave(&tp->rx_lock, flags);
2551 list_splice_tail(&rx_queue, &tp->rx_done);
2552 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002553 }
hayeswangd823ab62015-01-12 12:06:23 +08002554
2555out1:
2556 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002557}
2558
2559static void tx_bottom(struct r8152 *tp)
2560{
hayeswangebc2ec482013-08-14 20:54:38 +08002561 int res;
2562
hayeswangb1379d92013-08-16 16:09:37 +08002563 do {
Prashant Malanic01ebd62019-09-30 12:38:18 -07002564 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002565 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002566
hayeswangb1379d92013-08-16 16:09:37 +08002567 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002568 break;
2569
hayeswangb1379d92013-08-16 16:09:37 +08002570 agg = r8152_get_tx_agg(tp);
2571 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002572 break;
hayeswangb1379d92013-08-16 16:09:37 +08002573
2574 res = r8152_tx_agg_fill(tp, agg);
Prashant Malanic01ebd62019-09-30 12:38:18 -07002575 if (!res)
2576 continue;
hayeswangb1379d92013-08-16 16:09:37 +08002577
Prashant Malanic01ebd62019-09-30 12:38:18 -07002578 if (res == -ENODEV) {
2579 rtl_set_unplug(tp);
2580 netif_device_detach(netdev);
2581 } else {
2582 struct net_device_stats *stats = &netdev->stats;
2583 unsigned long flags;
hayeswang05e0f1a2014-03-06 15:07:18 +08002584
Prashant Malanic01ebd62019-09-30 12:38:18 -07002585 netif_warn(tp, tx_err, netdev,
2586 "failed tx_urb %d\n", res);
2587 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002588
Prashant Malanic01ebd62019-09-30 12:38:18 -07002589 spin_lock_irqsave(&tp->tx_lock, flags);
2590 list_add_tail(&agg->list, &tp->tx_free);
2591 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002592 }
hayeswangb1379d92013-08-16 16:09:37 +08002593 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002594}
2595
Emil Renner Berthingf3163f12021-01-31 00:47:29 +01002596static void bottom_half(struct tasklet_struct *t)
hayeswangebc2ec482013-08-14 20:54:38 +08002597{
Emil Renner Berthingf3163f12021-01-31 00:47:29 +01002598 struct r8152 *tp = from_tasklet(tp, t, tx_tl);
Hayes Wangd2187f82019-08-19 14:40:36 +08002599
hayeswangebc2ec482013-08-14 20:54:38 +08002600 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2601 return;
2602
2603 if (!test_bit(WORK_ENABLE, &tp->flags))
2604 return;
2605
hayeswang7559fb2f2013-08-16 16:09:38 +08002606 /* When link down, the driver would cancel all bulks. */
2607 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002608 if (!netif_carrier_ok(tp->netdev))
2609 return;
2610
Hayes Wangd2187f82019-08-19 14:40:36 +08002611 clear_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002612
hayeswang0c3121f2014-03-07 11:04:36 +08002613 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002614}
2615
hayeswangd823ab62015-01-12 12:06:23 +08002616static int r8152_poll(struct napi_struct *napi, int budget)
2617{
2618 struct r8152 *tp = container_of(napi, struct r8152, napi);
2619 int work_done;
2620
2621 work_done = rx_bottom(tp, budget);
hayeswangd823ab62015-01-12 12:06:23 +08002622
2623 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002624 if (!napi_complete_done(napi, work_done))
2625 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002626 if (!list_empty(&tp->rx_done))
2627 napi_schedule(napi);
2628 }
2629
hayeswanga3307f92017-06-09 17:11:48 +08002630out:
hayeswangd823ab62015-01-12 12:06:23 +08002631 return work_done;
2632}
2633
hayeswangebc2ec482013-08-14 20:54:38 +08002634static
2635int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2636{
hayeswanga0fccd42014-11-20 10:29:05 +08002637 int ret;
2638
hayeswangef827a52015-01-09 10:26:36 +08002639 /* The rx would be stopped, so skip submitting */
2640 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2641 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2642 return 0;
2643
Hayes Wangb67fda92021-06-17 18:00:15 +08002644 usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in,
Hayes Wangd55d7082019-08-13 11:42:07 +08002645 agg->buffer, tp->rx_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002646 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002647
hayeswanga0fccd42014-11-20 10:29:05 +08002648 ret = usb_submit_urb(agg->urb, mem_flags);
2649 if (ret == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002650 rtl_set_unplug(tp);
hayeswanga0fccd42014-11-20 10:29:05 +08002651 netif_device_detach(tp->netdev);
2652 } else if (ret) {
2653 struct urb *urb = agg->urb;
2654 unsigned long flags;
2655
2656 urb->actual_length = 0;
2657 spin_lock_irqsave(&tp->rx_lock, flags);
2658 list_add_tail(&agg->list, &tp->rx_done);
2659 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002660
2661 netif_err(tp, rx_err, tp->netdev,
2662 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2663
2664 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002665 }
2666
2667 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002668}
2669
hayeswang00a5e362014-02-18 21:48:59 +08002670static void rtl_drop_queued_tx(struct r8152 *tp)
2671{
2672 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002673 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002674 struct sk_buff *skb;
2675
hayeswangd84130a2014-02-18 21:49:02 +08002676 if (skb_queue_empty(tx_queue))
2677 return;
2678
2679 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002680 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002681 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002682 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002683
2684 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002685 dev_kfree_skb(skb);
2686 stats->tx_dropped++;
2687 }
2688}
2689
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002690static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue)
hayeswangac718b62013-05-02 16:01:25 +00002691{
2692 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002693
Hayes Wang4a8deae2014-01-07 11:18:22 +08002694 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002695
2696 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002697}
2698
2699static void rtl8152_set_rx_mode(struct net_device *netdev)
2700{
2701 struct r8152 *tp = netdev_priv(netdev);
2702
hayeswang51d979f2015-02-06 11:30:47 +08002703 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002704 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002705 schedule_delayed_work(&tp->schedule, 0);
2706 }
hayeswangac718b62013-05-02 16:01:25 +00002707}
2708
2709static void _rtl8152_set_rx_mode(struct net_device *netdev)
2710{
2711 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002712 u32 mc_filter[2]; /* Multicast hash filter */
2713 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002714 u32 ocp_data;
2715
hayeswangac718b62013-05-02 16:01:25 +00002716 netif_stop_queue(netdev);
2717 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2718 ocp_data &= ~RCR_ACPT_ALL;
2719 ocp_data |= RCR_AB | RCR_APM;
2720
2721 if (netdev->flags & IFF_PROMISC) {
2722 /* Unconditionally log net taps. */
2723 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2724 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002725 mc_filter[1] = 0xffffffff;
2726 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002727 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2728 (netdev->flags & IFF_ALLMULTI)) {
2729 /* Too many to filter perfectly -- accept all multicasts. */
2730 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002731 mc_filter[1] = 0xffffffff;
2732 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002733 } else {
2734 struct netdev_hw_addr *ha;
2735
hayeswangb209af92014-08-25 15:53:00 +08002736 mc_filter[1] = 0;
2737 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002738 netdev_for_each_mc_addr(ha, netdev) {
2739 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002740
hayeswangac718b62013-05-02 16:01:25 +00002741 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2742 ocp_data |= RCR_AM;
2743 }
2744 }
2745
hayeswang31787f52013-07-31 17:21:25 +08002746 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2747 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002748
hayeswang31787f52013-07-31 17:21:25 +08002749 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002750 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2751 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002752}
2753
hayeswanga5e31252015-01-06 17:41:58 +08002754static netdev_features_t
2755rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2756 netdev_features_t features)
2757{
2758 u32 mss = skb_shinfo(skb)->gso_size;
2759 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2760 int offset = skb_transport_offset(skb);
2761
2762 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002763 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002764 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2765 features &= ~NETIF_F_GSO_MASK;
2766
2767 return features;
2768}
2769
hayeswangac718b62013-05-02 16:01:25 +00002770static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002771 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002772{
2773 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002774
hayeswangac718b62013-05-02 16:01:25 +00002775 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002776
hayeswang61598782013-11-20 17:30:55 +08002777 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002778
hayeswang0c3121f2014-03-07 11:04:36 +08002779 if (!list_empty(&tp->tx_free)) {
2780 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
Hayes Wangd2187f82019-08-19 14:40:36 +08002781 set_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002782 schedule_delayed_work(&tp->schedule, 0);
2783 } else {
2784 usb_mark_last_busy(tp->udev);
Hayes Wangd2187f82019-08-19 14:40:36 +08002785 tasklet_schedule(&tp->tx_tl);
hayeswang0c3121f2014-03-07 11:04:36 +08002786 }
hayeswangb209af92014-08-25 15:53:00 +08002787 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002788 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002789 }
hayeswangdd1b1192013-11-20 17:30:56 +08002790
hayeswangac718b62013-05-02 16:01:25 +00002791 return NETDEV_TX_OK;
2792}
2793
2794static void r8152b_reset_packet_filter(struct r8152 *tp)
2795{
Hayes Wang195aae32021-04-16 16:04:35 +08002796 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002797
2798 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2799 ocp_data &= ~FMC_FCR_MCU_EN;
2800 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2801 ocp_data |= FMC_FCR_MCU_EN;
2802 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2803}
2804
2805static void rtl8152_nic_reset(struct r8152 *tp)
2806{
Hayes Wang195aae32021-04-16 16:04:35 +08002807 u32 ocp_data;
2808 int i;
hayeswangac718b62013-05-02 16:01:25 +00002809
Hayes Wang195aae32021-04-16 16:04:35 +08002810 switch (tp->version) {
2811 case RTL_TEST_01:
2812 case RTL_VER_10:
2813 case RTL_VER_11:
2814 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2815 ocp_data &= ~CR_TE;
2816 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00002817
Hayes Wang195aae32021-04-16 16:04:35 +08002818 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2819 ocp_data &= ~BMU_RESET_EP_IN;
2820 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2821
2822 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2823 ocp_data |= CDC_ECM_EN;
2824 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2825
2826 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2827 ocp_data &= ~CR_RE;
2828 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2829
2830 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2831 ocp_data |= BMU_RESET_EP_IN;
2832 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2833
2834 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2835 ocp_data &= ~CDC_ECM_EN;
2836 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2837 break;
2838
2839 default:
2840 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2841
2842 for (i = 0; i < 1000; i++) {
2843 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2844 break;
2845 usleep_range(100, 400);
2846 }
2847 break;
hayeswangac718b62013-05-02 16:01:25 +00002848 }
2849}
2850
hayeswangdd1b1192013-11-20 17:30:56 +08002851static void set_tx_qlen(struct r8152 *tp)
2852{
Hayes Wang67ce1a82021-04-16 16:04:34 +08002853 tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002854}
2855
Hayes Wang195aae32021-04-16 16:04:35 +08002856static inline u16 rtl8152_get_speed(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002857{
Hayes Wang195aae32021-04-16 16:04:35 +08002858 return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
hayeswangac718b62013-05-02 16:01:25 +00002859}
2860
Hayes Wang40fa7562021-02-19 17:04:43 +08002861static void rtl_eee_plus_en(struct r8152 *tp, bool enable)
hayeswangac718b62013-05-02 16:01:25 +00002862{
hayeswangebc2ec482013-08-14 20:54:38 +08002863 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002864
Hayes Wang40fa7562021-02-19 17:04:43 +08002865 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2866 if (enable)
hayeswangebc2ec482013-08-14 20:54:38 +08002867 ocp_data |= EEEP_CR_EEEP_TX;
Hayes Wang40fa7562021-02-19 17:04:43 +08002868 else
hayeswangebc2ec482013-08-14 20:54:38 +08002869 ocp_data &= ~EEEP_CR_EEEP_TX;
Hayes Wang40fa7562021-02-19 17:04:43 +08002870 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2871}
2872
2873static void rtl_set_eee_plus(struct r8152 *tp)
2874{
2875 if (rtl8152_get_speed(tp) & _10bps)
2876 rtl_eee_plus_en(tp, true);
2877 else
2878 rtl_eee_plus_en(tp, false);
hayeswang507605a2014-01-02 11:22:43 +08002879}
2880
hayeswang00a5e362014-02-18 21:48:59 +08002881static void rxdy_gated_en(struct r8152 *tp, bool enable)
2882{
2883 u32 ocp_data;
2884
2885 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2886 if (enable)
2887 ocp_data |= RXDY_GATED_EN;
2888 else
2889 ocp_data &= ~RXDY_GATED_EN;
2890 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2891}
2892
hayeswang445f7f42014-09-23 16:31:47 +08002893static int rtl_start_rx(struct r8152 *tp)
2894{
Hayes Wang252df8b2019-08-13 11:42:06 +08002895 struct rx_agg *agg, *agg_next;
2896 struct list_head tmp_list;
2897 unsigned long flags;
Hayes Wang47922fc2019-08-13 11:42:08 +08002898 int ret = 0, i = 0;
Hayes Wang252df8b2019-08-13 11:42:06 +08002899
2900 INIT_LIST_HEAD(&tmp_list);
2901
2902 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002903
2904 INIT_LIST_HEAD(&tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002905 INIT_LIST_HEAD(&tp->rx_used);
Hayes Wang252df8b2019-08-13 11:42:06 +08002906
2907 list_splice_init(&tp->rx_info, &tmp_list);
2908
2909 spin_unlock_irqrestore(&tp->rx_lock, flags);
2910
2911 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2912 INIT_LIST_HEAD(&agg->list);
2913
Hayes Wang47922fc2019-08-13 11:42:08 +08002914 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2915 if (++i > RTL8152_MAX_RX) {
2916 spin_lock_irqsave(&tp->rx_lock, flags);
2917 list_add_tail(&agg->list, &tp->rx_used);
2918 spin_unlock_irqrestore(&tp->rx_lock, flags);
2919 } else if (unlikely(ret < 0)) {
2920 spin_lock_irqsave(&tp->rx_lock, flags);
Hayes Wang252df8b2019-08-13 11:42:06 +08002921 list_add_tail(&agg->list, &tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002922 spin_unlock_irqrestore(&tp->rx_lock, flags);
2923 } else {
Hayes Wang252df8b2019-08-13 11:42:06 +08002924 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
Hayes Wang47922fc2019-08-13 11:42:08 +08002925 }
hayeswang445f7f42014-09-23 16:31:47 +08002926 }
2927
Hayes Wang252df8b2019-08-13 11:42:06 +08002928 spin_lock_irqsave(&tp->rx_lock, flags);
2929 WARN_ON(!list_empty(&tp->rx_info));
2930 list_splice(&tmp_list, &tp->rx_info);
2931 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang7bcf4f62014-11-20 10:29:06 +08002932
hayeswang445f7f42014-09-23 16:31:47 +08002933 return ret;
2934}
2935
2936static int rtl_stop_rx(struct r8152 *tp)
2937{
Hayes Wang252df8b2019-08-13 11:42:06 +08002938 struct rx_agg *agg, *agg_next;
2939 struct list_head tmp_list;
2940 unsigned long flags;
hayeswang445f7f42014-09-23 16:31:47 +08002941
Hayes Wang252df8b2019-08-13 11:42:06 +08002942 INIT_LIST_HEAD(&tmp_list);
2943
2944 /* The usb_kill_urb() couldn't be used in atomic.
2945 * Therefore, move the list of rx_info to a tmp one.
2946 * Then, list_for_each_entry_safe could be used without
2947 * spin lock.
2948 */
2949
2950 spin_lock_irqsave(&tp->rx_lock, flags);
2951 list_splice_init(&tp->rx_info, &tmp_list);
2952 spin_unlock_irqrestore(&tp->rx_lock, flags);
2953
Hayes Wang47922fc2019-08-13 11:42:08 +08002954 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2955 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2956 * equal to 1, so the other ones could be freed safely.
2957 */
2958 if (page_count(agg->page) > 1)
2959 free_rx_agg(tp, agg);
2960 else
2961 usb_kill_urb(agg->urb);
2962 }
Hayes Wang252df8b2019-08-13 11:42:06 +08002963
2964 /* Move back the list of temp to the rx_info */
2965 spin_lock_irqsave(&tp->rx_lock, flags);
2966 WARN_ON(!list_empty(&tp->rx_info));
2967 list_splice(&tmp_list, &tp->rx_info);
2968 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002969
hayeswangd823ab62015-01-12 12:06:23 +08002970 while (!skb_queue_empty(&tp->rx_queue))
2971 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2972
hayeswang445f7f42014-09-23 16:31:47 +08002973 return 0;
2974}
2975
Hayes Wang5133bcc72021-04-16 16:04:32 +08002976static void rtl_set_ifg(struct r8152 *tp, u16 speed)
2977{
2978 u32 ocp_data;
2979
2980 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2981 ocp_data &= ~IFG_MASK;
2982 if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) {
2983 ocp_data |= IFG_144NS;
2984 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
2985
2986 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
2987 ocp_data &= ~TX10MIDLE_EN;
2988 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
2989 } else {
2990 ocp_data |= IFG_96NS;
2991 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
2992
2993 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
2994 ocp_data |= TX10MIDLE_EN;
2995 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
2996 }
2997}
2998
Hayes Wang9fae5412019-07-03 15:11:56 +08002999static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
3000{
3001 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
3002 OWN_UPDATE | OWN_CLEAR);
3003}
3004
hayeswang507605a2014-01-02 11:22:43 +08003005static int rtl_enable(struct r8152 *tp)
3006{
3007 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00003008
3009 r8152b_reset_packet_filter(tp);
3010
3011 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
3012 ocp_data |= CR_RE | CR_TE;
3013 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
3014
Hayes Wang9fae5412019-07-03 15:11:56 +08003015 switch (tp->version) {
3016 case RTL_VER_08:
3017 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08003018 case RTL_VER_14:
Hayes Wang9fae5412019-07-03 15:11:56 +08003019 r8153b_rx_agg_chg_indicate(tp);
3020 break;
3021 default:
3022 break;
3023 }
3024
hayeswang00a5e362014-02-18 21:48:59 +08003025 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003026
hayeswangaa2e0922015-01-09 10:26:35 +08003027 return 0;
hayeswangac718b62013-05-02 16:01:25 +00003028}
3029
hayeswang507605a2014-01-02 11:22:43 +08003030static int rtl8152_enable(struct r8152 *tp)
3031{
hayeswang68714382014-04-11 17:54:31 +08003032 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3033 return -ENODEV;
3034
hayeswang507605a2014-01-02 11:22:43 +08003035 set_tx_qlen(tp);
3036 rtl_set_eee_plus(tp);
3037
3038 return rtl_enable(tp);
3039}
3040
hayeswang464ec102015-02-12 14:33:46 +08003041static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08003042{
hayeswang464ec102015-02-12 14:33:46 +08003043 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08003044
hayeswang65b82d62017-06-15 14:44:03 +08003045 switch (tp->version) {
3046 case RTL_VER_03:
3047 case RTL_VER_04:
3048 case RTL_VER_05:
3049 case RTL_VER_06:
3050 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3051 ocp_data);
3052 break;
3053
3054 case RTL_VER_08:
3055 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08003056 case RTL_VER_14:
hayeswang65b82d62017-06-15 14:44:03 +08003057 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
3058 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
3059 */
3060 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3061 128 / 8);
3062 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3063 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003064 break;
3065
Hayes Wang195aae32021-04-16 16:04:35 +08003066 case RTL_VER_10:
3067 case RTL_VER_11:
3068 case RTL_VER_12:
3069 case RTL_VER_13:
3070 case RTL_VER_15:
3071 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3072 640 / 8);
3073 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3074 ocp_data);
3075 r8153b_rx_agg_chg_indicate(tp);
3076 break;
3077
hayeswang65b82d62017-06-15 14:44:03 +08003078 default:
3079 break;
3080 }
hayeswang464ec102015-02-12 14:33:46 +08003081}
3082
3083static void r8153_set_rx_early_size(struct r8152 *tp)
3084{
Hayes Wangec5791c2019-08-13 11:42:05 +08003085 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08003086
hayeswang65b82d62017-06-15 14:44:03 +08003087 switch (tp->version) {
3088 case RTL_VER_03:
3089 case RTL_VER_04:
3090 case RTL_VER_05:
3091 case RTL_VER_06:
3092 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3093 ocp_data / 4);
3094 break;
3095 case RTL_VER_08:
3096 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08003097 case RTL_VER_14:
hayeswang65b82d62017-06-15 14:44:03 +08003098 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3099 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08003100 break;
Hayes Wang195aae32021-04-16 16:04:35 +08003101 case RTL_TEST_01:
3102 case RTL_VER_10:
3103 case RTL_VER_11:
3104 case RTL_VER_12:
3105 case RTL_VER_13:
3106 case RTL_VER_15:
3107 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3108 ocp_data / 8);
3109 r8153b_rx_agg_chg_indicate(tp);
3110 break;
hayeswang65b82d62017-06-15 14:44:03 +08003111 default:
3112 WARN_ON_ONCE(1);
3113 break;
3114 }
hayeswang43779f82014-01-02 11:25:10 +08003115}
3116
3117static int rtl8153_enable(struct r8152 *tp)
3118{
Hayes Wang195aae32021-04-16 16:04:35 +08003119 u32 ocp_data;
3120
hayeswang68714382014-04-11 17:54:31 +08003121 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3122 return -ENODEV;
3123
hayeswang43779f82014-01-02 11:25:10 +08003124 set_tx_qlen(tp);
3125 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08003126 r8153_set_rx_early_timeout(tp);
3127 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08003128
Hayes Wang5133bcc72021-04-16 16:04:32 +08003129 rtl_set_ifg(tp, rtl8152_get_speed(tp));
3130
Hayes Wang195aae32021-04-16 16:04:35 +08003131 switch (tp->version) {
3132 case RTL_VER_09:
3133 case RTL_VER_14:
Hayes Wangf99cd202020-01-22 16:02:06 +08003134 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
3135 ocp_data &= ~FC_PATCH_TASK;
3136 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3137 usleep_range(1000, 2000);
3138 ocp_data |= FC_PATCH_TASK;
3139 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
Hayes Wang195aae32021-04-16 16:04:35 +08003140 break;
3141 default:
3142 break;
Hayes Wangf99cd202020-01-22 16:02:06 +08003143 }
3144
hayeswang43779f82014-01-02 11:25:10 +08003145 return rtl_enable(tp);
3146}
3147
hayeswangd70b1132014-09-19 15:17:18 +08003148static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00003149{
hayeswangebc2ec482013-08-14 20:54:38 +08003150 u32 ocp_data;
3151 int i;
hayeswangac718b62013-05-02 16:01:25 +00003152
hayeswang68714382014-04-11 17:54:31 +08003153 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3154 rtl_drop_queued_tx(tp);
3155 return;
3156 }
3157
hayeswangac718b62013-05-02 16:01:25 +00003158 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3159 ocp_data &= ~RCR_ACPT_ALL;
3160 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3161
hayeswang00a5e362014-02-18 21:48:59 +08003162 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08003163
3164 for (i = 0; i < RTL8152_MAX_TX; i++)
3165 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00003166
hayeswang00a5e362014-02-18 21:48:59 +08003167 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003168
3169 for (i = 0; i < 1000; i++) {
3170 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3171 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
3172 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003173 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003174 }
3175
3176 for (i = 0; i < 1000; i++) {
3177 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
3178 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003179 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003180 }
3181
hayeswang445f7f42014-09-23 16:31:47 +08003182 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00003183
3184 rtl8152_nic_reset(tp);
3185}
3186
hayeswang00a5e362014-02-18 21:48:59 +08003187static void r8152_power_cut_en(struct r8152 *tp, bool enable)
3188{
3189 u32 ocp_data;
3190
3191 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
3192 if (enable)
3193 ocp_data |= POWER_CUT;
3194 else
3195 ocp_data &= ~POWER_CUT;
3196 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
3197
3198 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
3199 ocp_data &= ~RESUME_INDICATE;
3200 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08003201}
3202
hayeswangc5554292014-09-12 10:43:11 +08003203static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
3204{
3205 u32 ocp_data;
3206
Hayes Wang195aae32021-04-16 16:04:35 +08003207 switch (tp->version) {
3208 case RTL_VER_01:
3209 case RTL_VER_02:
3210 case RTL_VER_03:
3211 case RTL_VER_04:
3212 case RTL_VER_05:
3213 case RTL_VER_06:
3214 case RTL_VER_07:
3215 case RTL_VER_08:
3216 case RTL_VER_09:
3217 case RTL_VER_14:
3218 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
3219 if (enable)
3220 ocp_data |= CPCR_RX_VLAN;
3221 else
3222 ocp_data &= ~CPCR_RX_VLAN;
3223 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
3224 break;
3225
3226 case RTL_TEST_01:
3227 case RTL_VER_10:
3228 case RTL_VER_11:
3229 case RTL_VER_12:
3230 case RTL_VER_13:
3231 case RTL_VER_15:
3232 default:
3233 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1);
3234 if (enable)
3235 ocp_data |= OUTER_VLAN | INNER_VLAN;
3236 else
3237 ocp_data &= ~(OUTER_VLAN | INNER_VLAN);
3238 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data);
3239 break;
3240 }
hayeswangc5554292014-09-12 10:43:11 +08003241}
3242
3243static int rtl8152_set_features(struct net_device *dev,
3244 netdev_features_t features)
3245{
3246 netdev_features_t changed = features ^ dev->features;
3247 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08003248 int ret;
3249
3250 ret = usb_autopm_get_interface(tp->intf);
3251 if (ret < 0)
3252 goto out;
hayeswangc5554292014-09-12 10:43:11 +08003253
hayeswangb5403272014-10-09 18:00:26 +08003254 mutex_lock(&tp->control);
3255
hayeswangc5554292014-09-12 10:43:11 +08003256 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3257 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3258 rtl_rx_vlan_en(tp, true);
3259 else
3260 rtl_rx_vlan_en(tp, false);
3261 }
3262
hayeswangb5403272014-10-09 18:00:26 +08003263 mutex_unlock(&tp->control);
3264
hayeswang405f8a02014-10-09 18:00:24 +08003265 usb_autopm_put_interface(tp->intf);
3266
3267out:
3268 return ret;
hayeswangc5554292014-09-12 10:43:11 +08003269}
3270
hayeswang21ff2e82014-02-18 21:49:06 +08003271#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
3272
3273static u32 __rtl_get_wol(struct r8152 *tp)
3274{
3275 u32 ocp_data;
3276 u32 wolopts = 0;
3277
hayeswang21ff2e82014-02-18 21:49:06 +08003278 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3279 if (ocp_data & LINK_ON_WAKE_EN)
3280 wolopts |= WAKE_PHY;
3281
3282 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3283 if (ocp_data & UWF_EN)
3284 wolopts |= WAKE_UCAST;
3285 if (ocp_data & BWF_EN)
3286 wolopts |= WAKE_BCAST;
3287 if (ocp_data & MWF_EN)
3288 wolopts |= WAKE_MCAST;
3289
3290 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3291 if (ocp_data & MAGIC_EN)
3292 wolopts |= WAKE_MAGIC;
3293
3294 return wolopts;
3295}
3296
3297static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
3298{
3299 u32 ocp_data;
3300
3301 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3302
3303 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3304 ocp_data &= ~LINK_ON_WAKE_EN;
3305 if (wolopts & WAKE_PHY)
3306 ocp_data |= LINK_ON_WAKE_EN;
3307 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3308
3309 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08003310 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08003311 if (wolopts & WAKE_UCAST)
3312 ocp_data |= UWF_EN;
3313 if (wolopts & WAKE_BCAST)
3314 ocp_data |= BWF_EN;
3315 if (wolopts & WAKE_MCAST)
3316 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08003317 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3318
3319 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3320
3321 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3322 ocp_data &= ~MAGIC_EN;
3323 if (wolopts & WAKE_MAGIC)
3324 ocp_data |= MAGIC_EN;
3325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3326
3327 if (wolopts & WAKE_ANY)
3328 device_set_wakeup_enable(&tp->udev->dev, true);
3329 else
3330 device_set_wakeup_enable(&tp->udev->dev, false);
3331}
3332
Hayes Wang195aae32021-04-16 16:04:35 +08003333static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable)
3334{
3335 u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3336
3337 /* MAC clock speed down */
3338 if (enable)
3339 ocp_data |= MAC_CLK_SPDWN_EN;
3340 else
3341 ocp_data &= ~MAC_CLK_SPDWN_EN;
3342
3343 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3344}
3345
3346static void r8156_mac_clk_spd(struct r8152 *tp, bool enable)
3347{
3348 u32 ocp_data;
3349
3350 /* MAC clock speed down */
3351 if (enable) {
3352 /* aldps_spdwn_ratio, tp10_spdwn_ratio */
3353 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3354 0x0403);
3355
3356 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3357 ocp_data &= ~EEE_SPDWN_RATIO_MASK;
3358 ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */
3359 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3360 } else {
3361 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3362 ocp_data &= ~MAC_CLK_SPDWN_EN;
3363 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3364 }
3365}
3366
hayeswangb2143962015-07-24 13:54:23 +08003367static void r8153_u1u2en(struct r8152 *tp, bool enable)
3368{
3369 u8 u1u2[8];
3370
3371 if (enable)
3372 memset(u1u2, 0xff, sizeof(u1u2));
3373 else
3374 memset(u1u2, 0x00, sizeof(u1u2));
3375
3376 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3377}
3378
hayeswang65b82d62017-06-15 14:44:03 +08003379static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3380{
3381 u32 ocp_data;
3382
3383 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3384 if (enable)
3385 ocp_data |= LPM_U1U2_EN;
3386 else
3387 ocp_data &= ~LPM_U1U2_EN;
3388
3389 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3390}
3391
hayeswangb2143962015-07-24 13:54:23 +08003392static void r8153_u2p3en(struct r8152 *tp, bool enable)
3393{
3394 u32 ocp_data;
3395
3396 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08003397 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08003398 ocp_data |= U2P3_ENABLE;
3399 else
3400 ocp_data &= ~U2P3_ENABLE;
3401 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3402}
3403
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003404static void r8153b_ups_flags(struct r8152 *tp)
hayeswang65b82d62017-06-15 14:44:03 +08003405{
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003406 u32 ups_flags = 0;
hayeswang65b82d62017-06-15 14:44:03 +08003407
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003408 if (tp->ups_info.green)
3409 ups_flags |= UPS_FLAGS_EN_GREEN;
3410
3411 if (tp->ups_info.aldps)
3412 ups_flags |= UPS_FLAGS_EN_ALDPS;
3413
3414 if (tp->ups_info.eee)
3415 ups_flags |= UPS_FLAGS_EN_EEE;
3416
3417 if (tp->ups_info.flow_control)
3418 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3419
3420 if (tp->ups_info.eee_ckdiv)
3421 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3422
3423 if (tp->ups_info.eee_cmod_lv)
3424 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3425
Hayes Wang195aae32021-04-16 16:04:35 +08003426 if (tp->ups_info.r_tune)
3427 ups_flags |= UPS_FLAGS_R_TUNE;
3428
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003429 if (tp->ups_info._10m_ckdiv)
3430 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3431
3432 if (tp->ups_info.eee_plloff_100)
3433 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3434
3435 if (tp->ups_info.eee_plloff_giga)
3436 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3437
3438 if (tp->ups_info._250m_ckdiv)
3439 ups_flags |= UPS_FLAGS_250M_CKDIV;
3440
3441 if (tp->ups_info.ctap_short_off)
3442 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3443
3444 switch (tp->ups_info.speed_duplex) {
3445 case NWAY_10M_HALF:
3446 ups_flags |= ups_flags_speed(1);
3447 break;
3448 case NWAY_10M_FULL:
3449 ups_flags |= ups_flags_speed(2);
3450 break;
3451 case NWAY_100M_HALF:
3452 ups_flags |= ups_flags_speed(3);
3453 break;
3454 case NWAY_100M_FULL:
3455 ups_flags |= ups_flags_speed(4);
3456 break;
3457 case NWAY_1000M_FULL:
3458 ups_flags |= ups_flags_speed(5);
3459 break;
3460 case FORCE_10M_HALF:
3461 ups_flags |= ups_flags_speed(6);
3462 break;
3463 case FORCE_10M_FULL:
3464 ups_flags |= ups_flags_speed(7);
3465 break;
3466 case FORCE_100M_HALF:
3467 ups_flags |= ups_flags_speed(8);
3468 break;
3469 case FORCE_100M_FULL:
3470 ups_flags |= ups_flags_speed(9);
3471 break;
3472 default:
3473 break;
3474 }
3475
3476 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
hayeswang65b82d62017-06-15 14:44:03 +08003477}
3478
Hayes Wang195aae32021-04-16 16:04:35 +08003479static void r8156_ups_flags(struct r8152 *tp)
3480{
3481 u32 ups_flags = 0;
3482
3483 if (tp->ups_info.green)
3484 ups_flags |= UPS_FLAGS_EN_GREEN;
3485
3486 if (tp->ups_info.aldps)
3487 ups_flags |= UPS_FLAGS_EN_ALDPS;
3488
3489 if (tp->ups_info.eee)
3490 ups_flags |= UPS_FLAGS_EN_EEE;
3491
3492 if (tp->ups_info.flow_control)
3493 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3494
3495 if (tp->ups_info.eee_ckdiv)
3496 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3497
3498 if (tp->ups_info._10m_ckdiv)
3499 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3500
3501 if (tp->ups_info.eee_plloff_100)
3502 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3503
3504 if (tp->ups_info.eee_plloff_giga)
3505 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3506
3507 if (tp->ups_info._250m_ckdiv)
3508 ups_flags |= UPS_FLAGS_250M_CKDIV;
3509
3510 switch (tp->ups_info.speed_duplex) {
3511 case FORCE_10M_HALF:
3512 ups_flags |= ups_flags_speed(0);
3513 break;
3514 case FORCE_10M_FULL:
3515 ups_flags |= ups_flags_speed(1);
3516 break;
3517 case FORCE_100M_HALF:
3518 ups_flags |= ups_flags_speed(2);
3519 break;
3520 case FORCE_100M_FULL:
3521 ups_flags |= ups_flags_speed(3);
3522 break;
3523 case NWAY_10M_HALF:
3524 ups_flags |= ups_flags_speed(4);
3525 break;
3526 case NWAY_10M_FULL:
3527 ups_flags |= ups_flags_speed(5);
3528 break;
3529 case NWAY_100M_HALF:
3530 ups_flags |= ups_flags_speed(6);
3531 break;
3532 case NWAY_100M_FULL:
3533 ups_flags |= ups_flags_speed(7);
3534 break;
3535 case NWAY_1000M_FULL:
3536 ups_flags |= ups_flags_speed(8);
3537 break;
3538 case NWAY_2500M_FULL:
3539 ups_flags |= ups_flags_speed(9);
3540 break;
3541 default:
3542 break;
3543 }
3544
3545 switch (tp->ups_info.lite_mode) {
3546 case 1:
3547 ups_flags |= 0 << 5;
3548 break;
3549 case 2:
3550 ups_flags |= 2 << 5;
3551 break;
3552 case 0:
3553 default:
3554 ups_flags |= 1 << 5;
3555 break;
3556 }
3557
3558 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3559}
3560
Hayes Wang40fa7562021-02-19 17:04:43 +08003561static void rtl_green_en(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08003562{
3563 u16 data;
3564
Hayes Wang40fa7562021-02-19 17:04:43 +08003565 data = sram_read(tp, SRAM_GREEN_CFG);
3566 if (enable)
3567 data |= GREEN_ETH_EN;
3568 else
3569 data &= ~GREEN_ETH_EN;
3570 sram_write(tp, SRAM_GREEN_CFG, data);
3571
3572 tp->ups_info.green = enable;
3573}
3574
3575static void r8153b_green_en(struct r8152 *tp, bool enable)
3576{
hayeswang65b82d62017-06-15 14:44:03 +08003577 if (enable) {
3578 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
3579 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
3580 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
3581 } else {
3582 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
3583 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
3584 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
3585 }
3586
Hayes Wang40fa7562021-02-19 17:04:43 +08003587 rtl_green_en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003588}
3589
hayeswangc564b872017-06-09 17:11:38 +08003590static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3591{
3592 u16 data;
3593 int i;
3594
3595 for (i = 0; i < 500; i++) {
3596 data = ocp_reg_read(tp, OCP_PHY_STATUS);
3597 data &= PHY_STAT_MASK;
3598 if (desired) {
3599 if (data == desired)
3600 break;
3601 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3602 data == PHY_STAT_EXT_INIT) {
3603 break;
3604 }
3605
3606 msleep(20);
You-Sheng Yangd64c7a02020-02-26 23:37:10 +08003607 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3608 break;
hayeswangc564b872017-06-09 17:11:38 +08003609 }
3610
3611 return data;
3612}
3613
hayeswang65b82d62017-06-15 14:44:03 +08003614static void r8153b_ups_en(struct r8152 *tp, bool enable)
3615{
3616 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3617
3618 if (enable) {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003619 r8153b_ups_flags(tp);
3620
hayeswang65b82d62017-06-15 14:44:03 +08003621 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3622 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3623
Hayes Wang195aae32021-04-16 16:04:35 +08003624 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3625 ocp_data |= UPS_FORCE_PWR_DOWN;
3626 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003627 } else {
hayeswang65b82d62017-06-15 14:44:03 +08003628 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3629 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3630
Hayes Wang195aae32021-04-16 16:04:35 +08003631 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3632 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3633 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003634
Hayes Wang80fd8502021-02-03 17:14:29 +08003635 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3636 int i;
hayeswang65b82d62017-06-15 14:44:03 +08003637
Hayes Wang80fd8502021-02-03 17:14:29 +08003638 for (i = 0; i < 500; i++) {
3639 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3640 AUTOLOAD_DONE)
3641 break;
3642 msleep(20);
3643 }
hayeswang65b82d62017-06-15 14:44:03 +08003644
Hayes Wang80fd8502021-02-03 17:14:29 +08003645 tp->rtl_ops.hw_phy_cfg(tp);
hayeswang65b82d62017-06-15 14:44:03 +08003646
Hayes Wang80fd8502021-02-03 17:14:29 +08003647 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3648 tp->duplex, tp->advertising);
hayeswang65b82d62017-06-15 14:44:03 +08003649 }
3650 }
3651}
3652
Hayes Wang195aae32021-04-16 16:04:35 +08003653static void r8153c_ups_en(struct r8152 *tp, bool enable)
3654{
3655 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3656
3657 if (enable) {
3658 r8153b_ups_flags(tp);
3659
3660 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3661 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3662
3663 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3664 ocp_data |= UPS_FORCE_PWR_DOWN;
3665 ocp_data &= ~BIT(7);
3666 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3667 } else {
3668 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3669 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3670
3671 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3672 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3673 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3674
3675 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3676 int i;
3677
3678 for (i = 0; i < 500; i++) {
3679 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3680 AUTOLOAD_DONE)
3681 break;
3682 msleep(20);
3683 }
3684
3685 tp->rtl_ops.hw_phy_cfg(tp);
3686
3687 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3688 tp->duplex, tp->advertising);
3689 }
3690
3691 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3692
3693 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3694 ocp_data |= BIT(8);
3695 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3696
3697 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3698 }
3699}
3700
3701static void r8156_ups_en(struct r8152 *tp, bool enable)
3702{
3703 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3704
3705 if (enable) {
3706 r8156_ups_flags(tp);
3707
3708 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3709 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3710
3711 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3712 ocp_data |= UPS_FORCE_PWR_DOWN;
3713 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3714
3715 switch (tp->version) {
3716 case RTL_VER_13:
3717 case RTL_VER_15:
3718 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL);
3719 ocp_data &= ~OOBS_POLLING;
3720 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data);
3721 break;
3722 default:
3723 break;
3724 }
3725 } else {
3726 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3727 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3728
3729 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3730 ocp_data &= ~UPS_FORCE_PWR_DOWN;
3731 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3732
3733 if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3734 tp->rtl_ops.hw_phy_cfg(tp);
3735
3736 rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3737 tp->duplex, tp->advertising);
3738 }
3739 }
3740}
3741
hayeswangb2143962015-07-24 13:54:23 +08003742static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3743{
3744 u32 ocp_data;
3745
3746 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3747 if (enable)
3748 ocp_data |= PWR_EN | PHASE2_EN;
3749 else
3750 ocp_data &= ~(PWR_EN | PHASE2_EN);
3751 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3752
3753 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3754 ocp_data &= ~PCUT_STATUS;
3755 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3756}
3757
hayeswang65b82d62017-06-15 14:44:03 +08003758static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3759{
3760 u32 ocp_data;
3761
3762 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3763 if (enable)
3764 ocp_data |= PWR_EN | PHASE2_EN;
3765 else
3766 ocp_data &= ~PWR_EN;
3767 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3768
3769 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3770 ocp_data &= ~PCUT_STATUS;
3771 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3772}
3773
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003774static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08003775{
3776 u32 ocp_data;
3777
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003778 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08003779 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003780 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08003781 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003782 ocp_data &= ~UPCOMING_RUNTIME_D3;
3783 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003784
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003785 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3786 ocp_data &= ~LINK_CHG_EVENT;
3787 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3788
3789 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3790 ocp_data &= ~LINK_CHANGE_FLAG;
3791 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003792}
3793
hayeswang7daed8d2015-07-24 13:54:24 +08003794static bool rtl_can_wakeup(struct r8152 *tp)
3795{
3796 struct usb_device *udev = tp->udev;
3797
3798 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3799}
3800
hayeswang9a4be1b2014-02-18 21:49:07 +08003801static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3802{
3803 if (enable) {
3804 u32 ocp_data;
3805
3806 __rtl_set_wol(tp, WAKE_ANY);
3807
3808 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3809
3810 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3811 ocp_data |= LINK_OFF_WAKE_EN;
3812 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3813
3814 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3815 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08003816 u32 ocp_data;
3817
hayeswang9a4be1b2014-02-18 21:49:07 +08003818 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08003819
3820 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3821
3822 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3823 ocp_data &= ~LINK_OFF_WAKE_EN;
3824 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3825
3826 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08003827 }
3828}
hayeswangf95ae8a2016-06-30 15:33:35 +08003829
hayeswang2609af12016-07-05 16:11:46 +08003830static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3831{
hayeswang2609af12016-07-05 16:11:46 +08003832 if (enable) {
3833 r8153_u1u2en(tp, false);
3834 r8153_u2p3en(tp, false);
hayeswang02552752017-06-09 17:11:42 +08003835 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08003836 } else {
hayeswang02552752017-06-09 17:11:42 +08003837 rtl_runtime_suspend_enable(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003838
3839 switch (tp->version) {
3840 case RTL_VER_03:
3841 case RTL_VER_04:
3842 break;
3843 case RTL_VER_05:
3844 case RTL_VER_06:
3845 default:
3846 r8153_u2p3en(tp, true);
3847 break;
3848 }
3849
hayeswangb2143962015-07-24 13:54:23 +08003850 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08003851 }
3852}
3853
hayeswang65b82d62017-06-15 14:44:03 +08003854static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3855{
3856 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003857 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003858 r8153b_u1u2en(tp, false);
3859 r8153_u2p3en(tp, false);
3860 rtl_runtime_suspend_enable(tp, true);
3861 r8153b_ups_en(tp, true);
3862 } else {
3863 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003864 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08003865 rtl_runtime_suspend_enable(tp, false);
Hayes Wang7a0ae612021-02-19 17:04:40 +08003866 if (tp->udev->speed >= USB_SPEED_SUPER)
Hayes Wanga0246da2020-01-22 16:02:11 +08003867 r8153b_u1u2en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003868 }
3869}
3870
Hayes Wang195aae32021-04-16 16:04:35 +08003871static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable)
3872{
3873 if (enable) {
3874 r8153_queue_wake(tp, true);
3875 r8153b_u1u2en(tp, false);
3876 r8153_u2p3en(tp, false);
3877 rtl_runtime_suspend_enable(tp, true);
3878 r8153c_ups_en(tp, true);
3879 } else {
3880 r8153c_ups_en(tp, false);
3881 r8153_queue_wake(tp, false);
3882 rtl_runtime_suspend_enable(tp, false);
3883 r8153b_u1u2en(tp, true);
3884 }
3885}
3886
3887static void rtl8156_runtime_enable(struct r8152 *tp, bool enable)
3888{
3889 if (enable) {
3890 r8153_queue_wake(tp, true);
3891 r8153b_u1u2en(tp, false);
3892 r8153_u2p3en(tp, false);
3893 rtl_runtime_suspend_enable(tp, true);
3894 } else {
3895 r8153_queue_wake(tp, false);
3896 rtl_runtime_suspend_enable(tp, false);
3897 r8153_u2p3en(tp, true);
3898 if (tp->udev->speed >= USB_SPEED_SUPER)
3899 r8153b_u1u2en(tp, true);
3900 }
3901}
3902
hayeswang43499682014-02-18 21:48:58 +08003903static void r8153_teredo_off(struct r8152 *tp)
3904{
3905 u32 ocp_data;
3906
hayeswang65b82d62017-06-15 14:44:03 +08003907 switch (tp->version) {
3908 case RTL_VER_01:
3909 case RTL_VER_02:
3910 case RTL_VER_03:
3911 case RTL_VER_04:
3912 case RTL_VER_05:
3913 case RTL_VER_06:
3914 case RTL_VER_07:
3915 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3916 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3917 OOB_TEREDO_EN);
3918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3919 break;
3920
3921 case RTL_VER_08:
3922 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08003923 case RTL_TEST_01:
3924 case RTL_VER_10:
3925 case RTL_VER_11:
3926 case RTL_VER_12:
3927 case RTL_VER_13:
3928 case RTL_VER_14:
3929 case RTL_VER_15:
3930 default:
hayeswang65b82d62017-06-15 14:44:03 +08003931 /* The bit 0 ~ 7 are relative with teredo settings. They are
3932 * W1C (write 1 to clear), so set all 1 to disable it.
3933 */
3934 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3935 break;
hayeswang65b82d62017-06-15 14:44:03 +08003936 }
hayeswang43499682014-02-18 21:48:58 +08003937
3938 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3939 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3940 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3941}
3942
hayeswang93fe9b12016-06-16 10:55:18 +08003943static void rtl_reset_bmu(struct r8152 *tp)
3944{
3945 u32 ocp_data;
3946
3947 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3948 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3949 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3950 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3951 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3952}
3953
Hayes Wang9370f2d2019-10-16 11:02:42 +08003954/* Clear the bp to stop the firmware before loading a new one */
3955static void rtl_clear_bp(struct r8152 *tp, u16 type)
3956{
3957 switch (tp->version) {
3958 case RTL_VER_01:
3959 case RTL_VER_02:
3960 case RTL_VER_07:
3961 break;
3962 case RTL_VER_03:
3963 case RTL_VER_04:
3964 case RTL_VER_05:
3965 case RTL_VER_06:
3966 ocp_write_byte(tp, type, PLA_BP_EN, 0);
3967 break;
Hayes Wang6633fb82021-08-19 11:05:37 +08003968 case RTL_VER_14:
3969 ocp_write_word(tp, type, USB_BP2_EN, 0);
3970
3971 ocp_write_word(tp, type, USB_BP_8, 0);
3972 ocp_write_word(tp, type, USB_BP_9, 0);
3973 ocp_write_word(tp, type, USB_BP_10, 0);
3974 ocp_write_word(tp, type, USB_BP_11, 0);
3975 ocp_write_word(tp, type, USB_BP_12, 0);
3976 ocp_write_word(tp, type, USB_BP_13, 0);
3977 ocp_write_word(tp, type, USB_BP_14, 0);
3978 ocp_write_word(tp, type, USB_BP_15, 0);
3979 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003980 case RTL_VER_08:
3981 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08003982 case RTL_VER_10:
3983 case RTL_VER_11:
3984 case RTL_VER_12:
3985 case RTL_VER_13:
Hayes Wang195aae32021-04-16 16:04:35 +08003986 case RTL_VER_15:
Hayes Wang9370f2d2019-10-16 11:02:42 +08003987 default:
3988 if (type == MCU_TYPE_USB) {
Hayes Wanga876a332021-08-19 11:05:36 +08003989 ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
Hayes Wang9370f2d2019-10-16 11:02:42 +08003990
3991 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0);
3992 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0);
3993 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0);
3994 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0);
3995 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0);
3996 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0);
3997 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0);
3998 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0);
3999 } else {
4000 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
4001 }
4002 break;
4003 }
4004
4005 ocp_write_word(tp, type, PLA_BP_0, 0);
4006 ocp_write_word(tp, type, PLA_BP_1, 0);
4007 ocp_write_word(tp, type, PLA_BP_2, 0);
4008 ocp_write_word(tp, type, PLA_BP_3, 0);
4009 ocp_write_word(tp, type, PLA_BP_4, 0);
4010 ocp_write_word(tp, type, PLA_BP_5, 0);
4011 ocp_write_word(tp, type, PLA_BP_6, 0);
4012 ocp_write_word(tp, type, PLA_BP_7, 0);
4013
4014 /* wait 3 ms to make sure the firmware is stopped */
4015 usleep_range(3000, 6000);
4016 ocp_write_word(tp, type, PLA_BP_BA, 0);
4017}
4018
Hayes Wangb24edca2021-12-23 17:27:02 +08004019static inline void rtl_reset_ocp_base(struct r8152 *tp)
4020{
4021 tp->ocp_base = -1;
4022}
4023
Hayes Wanga08c0d32021-02-03 17:14:28 +08004024static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
Hayes Wang470e3912019-10-21 11:41:12 +08004025{
Hayes Wanga08c0d32021-02-03 17:14:28 +08004026 u16 data, check;
Hayes Wang470e3912019-10-21 11:41:12 +08004027 int i;
4028
4029 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
Hayes Wanga08c0d32021-02-03 17:14:28 +08004030 if (request) {
Hayes Wang470e3912019-10-21 11:41:12 +08004031 data |= PATCH_REQUEST;
Hayes Wanga08c0d32021-02-03 17:14:28 +08004032 check = 0;
4033 } else {
Hayes Wang470e3912019-10-21 11:41:12 +08004034 data &= ~PATCH_REQUEST;
Hayes Wanga08c0d32021-02-03 17:14:28 +08004035 check = PATCH_READY;
4036 }
Hayes Wang470e3912019-10-21 11:41:12 +08004037 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
4038
Hayes Wanga08c0d32021-02-03 17:14:28 +08004039 for (i = 0; wait && i < 5000; i++) {
4040 u32 ocp_data;
4041
Hayes Wang470e3912019-10-21 11:41:12 +08004042 usleep_range(1000, 2000);
Hayes Wanga08c0d32021-02-03 17:14:28 +08004043 ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
4044 if ((ocp_data & PATCH_READY) ^ check)
Hayes Wang470e3912019-10-21 11:41:12 +08004045 break;
4046 }
4047
Hayes Wanga08c0d32021-02-03 17:14:28 +08004048 if (request && wait &&
4049 !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
4050 dev_err(&tp->intf->dev, "PHY patch request fail\n");
4051 rtl_phy_patch_request(tp, false, false);
Hayes Wang470e3912019-10-21 11:41:12 +08004052 return -ETIME;
4053 } else {
4054 return 0;
4055 }
4056}
4057
Hayes Wanga08c0d32021-02-03 17:14:28 +08004058static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
Hayes Wangaf142882019-10-21 11:41:13 +08004059{
Hayes Wanga08c0d32021-02-03 17:14:28 +08004060 if (patch_key && key_addr) {
4061 sram_write(tp, key_addr, patch_key);
4062 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
4063 } else if (key_addr) {
4064 u16 data;
Hayes Wangaf142882019-10-21 11:41:13 +08004065
Hayes Wanga08c0d32021-02-03 17:14:28 +08004066 sram_write(tp, 0x0000, 0x0000);
4067
4068 data = ocp_reg_read(tp, OCP_PHY_LOCK);
4069 data &= ~PATCH_LOCK;
4070 ocp_reg_write(tp, OCP_PHY_LOCK, data);
4071
4072 sram_write(tp, key_addr, 0x0000);
4073 } else {
4074 WARN_ON_ONCE(1);
4075 }
4076}
4077
4078static int
4079rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
4080{
4081 if (rtl_phy_patch_request(tp, true, wait))
4082 return -ETIME;
4083
4084 rtl_patch_key_set(tp, key_addr, patch_key);
Hayes Wangaf142882019-10-21 11:41:13 +08004085
4086 return 0;
4087}
4088
Hayes Wanga08c0d32021-02-03 17:14:28 +08004089static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
Hayes Wangaf142882019-10-21 11:41:13 +08004090{
Hayes Wanga08c0d32021-02-03 17:14:28 +08004091 rtl_patch_key_set(tp, key_addr, 0);
Hayes Wangaf142882019-10-21 11:41:13 +08004092
Hayes Wanga08c0d32021-02-03 17:14:28 +08004093 rtl_phy_patch_request(tp, false, wait);
Hayes Wangaf142882019-10-21 11:41:13 +08004094
Hayes Wangaf142882019-10-21 11:41:13 +08004095 return 0;
4096}
4097
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004098static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy)
4099{
4100 u16 fw_offset;
4101 u32 length;
4102 bool rc = false;
4103
4104 switch (tp->version) {
4105 case RTL_VER_01:
4106 case RTL_VER_02:
4107 case RTL_VER_03:
4108 case RTL_VER_04:
4109 case RTL_VER_05:
4110 case RTL_VER_06:
4111 case RTL_VER_07:
4112 case RTL_VER_08:
4113 case RTL_VER_09:
4114 case RTL_VER_10:
4115 case RTL_VER_11:
4116 case RTL_VER_12:
4117 case RTL_VER_14:
4118 goto out;
4119 case RTL_VER_13:
4120 case RTL_VER_15:
4121 default:
4122 break;
4123 }
4124
4125 fw_offset = __le16_to_cpu(phy->fw_offset);
4126 length = __le32_to_cpu(phy->blk_hdr.length);
4127 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4128 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4129 goto out;
4130 }
4131
4132 length -= fw_offset;
4133 if (length & 3) {
4134 dev_err(&tp->intf->dev, "invalid block length\n");
4135 goto out;
4136 }
4137
4138 if (__le16_to_cpu(phy->fw_reg) != 0x9A00) {
4139 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4140 goto out;
4141 }
4142
4143 rc = true;
4144out:
4145 return rc;
4146}
4147
4148static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver)
4149{
4150 bool rc = false;
4151
4152 switch (tp->version) {
4153 case RTL_VER_10:
4154 case RTL_VER_11:
4155 case RTL_VER_12:
4156 case RTL_VER_13:
4157 case RTL_VER_15:
4158 break;
4159 default:
4160 goto out;
4161 }
4162
4163 if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) {
4164 dev_err(&tp->intf->dev, "invalid block length\n");
4165 goto out;
4166 }
4167
4168 if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) {
4169 dev_err(&tp->intf->dev, "invalid phy ver addr\n");
4170 goto out;
4171 }
4172
4173 rc = true;
4174out:
4175 return rc;
4176}
4177
4178static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix)
4179{
4180 bool rc = false;
4181
4182 switch (tp->version) {
4183 case RTL_VER_10:
4184 case RTL_VER_11:
4185 case RTL_VER_12:
4186 case RTL_VER_13:
4187 case RTL_VER_15:
4188 break;
4189 default:
4190 goto out;
4191 }
4192
4193 if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) {
4194 dev_err(&tp->intf->dev, "invalid block length\n");
4195 goto out;
4196 }
4197
4198 if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD ||
4199 __le16_to_cpu(fix->setting.data) != BIT(7)) {
4200 dev_err(&tp->intf->dev, "invalid phy fixup\n");
4201 goto out;
4202 }
4203
4204 rc = true;
4205out:
4206 return rc;
4207}
4208
4209static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy)
4210{
4211 u16 fw_offset;
4212 u32 length;
4213 bool rc = false;
4214
4215 switch (tp->version) {
4216 case RTL_VER_10:
4217 case RTL_VER_11:
4218 case RTL_VER_12:
4219 case RTL_VER_13:
4220 case RTL_VER_15:
4221 break;
4222 default:
4223 goto out;
4224 }
4225
4226 fw_offset = __le16_to_cpu(phy->fw_offset);
4227 length = __le32_to_cpu(phy->blk_hdr.length);
4228 if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4229 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4230 goto out;
4231 }
4232
4233 length -= fw_offset;
4234 if (length & 1) {
4235 dev_err(&tp->intf->dev, "invalid block length\n");
4236 goto out;
4237 }
4238
4239 if (phy->pre_num > 2) {
4240 dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num);
4241 goto out;
4242 }
4243
4244 if (phy->bp_num > 8) {
4245 dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num);
4246 goto out;
4247 }
4248
4249 rc = true;
4250out:
4251 return rc;
4252}
4253
Hayes Wangaf142882019-10-21 11:41:13 +08004254static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
4255{
4256 u32 length;
4257 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
4258 bool rc = false;
4259
4260 switch (tp->version) {
4261 case RTL_VER_04:
4262 case RTL_VER_05:
4263 case RTL_VER_06:
4264 fw_reg = 0xa014;
4265 ba_reg = 0xa012;
4266 patch_en_addr = 0xa01a;
4267 mode_reg = 0xb820;
4268 bp_start = 0xa000;
4269 break;
4270 default:
4271 goto out;
4272 }
4273
4274 fw_offset = __le16_to_cpu(phy->fw_offset);
4275 if (fw_offset < sizeof(*phy)) {
4276 dev_err(&tp->intf->dev, "fw_offset too small\n");
4277 goto out;
4278 }
4279
4280 length = __le32_to_cpu(phy->blk_hdr.length);
4281 if (length < fw_offset) {
4282 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4283 goto out;
4284 }
4285
4286 length -= __le16_to_cpu(phy->fw_offset);
4287 if (!length || (length & 1)) {
4288 dev_err(&tp->intf->dev, "invalid block length\n");
4289 goto out;
4290 }
4291
4292 if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
4293 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4294 goto out;
4295 }
4296
4297 if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
4298 dev_err(&tp->intf->dev, "invalid base address register\n");
4299 goto out;
4300 }
4301
4302 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
4303 dev_err(&tp->intf->dev,
4304 "invalid patch mode enabled register\n");
4305 goto out;
4306 }
4307
4308 if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
4309 dev_err(&tp->intf->dev,
4310 "invalid register to switch the mode\n");
4311 goto out;
4312 }
4313
4314 if (__le16_to_cpu(phy->bp_start) != bp_start) {
4315 dev_err(&tp->intf->dev,
4316 "invalid start register of break point\n");
4317 goto out;
4318 }
4319
4320 if (__le16_to_cpu(phy->bp_num) > 4) {
4321 dev_err(&tp->intf->dev, "invalid break point number\n");
4322 goto out;
4323 }
4324
4325 rc = true;
4326out:
4327 return rc;
4328}
4329
Hayes Wanga66edaa2019-10-21 11:41:10 +08004330static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
Hayes Wang9370f2d2019-10-16 11:02:42 +08004331{
Hayes Wang5a16a3d2019-10-21 11:41:11 +08004332 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004333 bool rc = false;
4334 u32 length, type;
4335 int i, max_bp;
4336
Hayes Wanga66edaa2019-10-21 11:41:10 +08004337 type = __le32_to_cpu(mac->blk_hdr.type);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004338 if (type == RTL_FW_PLA) {
4339 switch (tp->version) {
4340 case RTL_VER_01:
4341 case RTL_VER_02:
4342 case RTL_VER_07:
4343 fw_reg = 0xf800;
4344 bp_ba_addr = PLA_BP_BA;
4345 bp_en_addr = 0;
4346 bp_start = PLA_BP_0;
4347 max_bp = 8;
4348 break;
4349 case RTL_VER_03:
4350 case RTL_VER_04:
4351 case RTL_VER_05:
4352 case RTL_VER_06:
4353 case RTL_VER_08:
4354 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08004355 case RTL_VER_11:
4356 case RTL_VER_12:
4357 case RTL_VER_13:
Hayes Wang195aae32021-04-16 16:04:35 +08004358 case RTL_VER_15:
Hayes Wang9370f2d2019-10-16 11:02:42 +08004359 fw_reg = 0xf800;
4360 bp_ba_addr = PLA_BP_BA;
4361 bp_en_addr = PLA_BP_EN;
4362 bp_start = PLA_BP_0;
4363 max_bp = 8;
4364 break;
Hayes Wang6633fb82021-08-19 11:05:37 +08004365 case RTL_VER_14:
4366 fw_reg = 0xf800;
4367 bp_ba_addr = PLA_BP_BA;
4368 bp_en_addr = USB_BP2_EN;
4369 bp_start = PLA_BP_0;
4370 max_bp = 16;
4371 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004372 default:
4373 goto out;
4374 }
4375 } else if (type == RTL_FW_USB) {
4376 switch (tp->version) {
4377 case RTL_VER_03:
4378 case RTL_VER_04:
4379 case RTL_VER_05:
4380 case RTL_VER_06:
4381 fw_reg = 0xf800;
4382 bp_ba_addr = USB_BP_BA;
4383 bp_en_addr = USB_BP_EN;
4384 bp_start = USB_BP_0;
4385 max_bp = 8;
4386 break;
4387 case RTL_VER_08:
4388 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08004389 case RTL_VER_11:
4390 case RTL_VER_12:
4391 case RTL_VER_13:
4392 case RTL_VER_14:
4393 case RTL_VER_15:
Hayes Wang9370f2d2019-10-16 11:02:42 +08004394 fw_reg = 0xe600;
4395 bp_ba_addr = USB_BP_BA;
4396 bp_en_addr = USB_BP2_EN;
4397 bp_start = USB_BP_0;
4398 max_bp = 16;
4399 break;
4400 case RTL_VER_01:
4401 case RTL_VER_02:
4402 case RTL_VER_07:
4403 default:
4404 goto out;
4405 }
4406 } else {
4407 goto out;
4408 }
4409
Hayes Wang5a16a3d2019-10-21 11:41:11 +08004410 fw_offset = __le16_to_cpu(mac->fw_offset);
4411 if (fw_offset < sizeof(*mac)) {
4412 dev_err(&tp->intf->dev, "fw_offset too small\n");
4413 goto out;
4414 }
4415
Hayes Wanga66edaa2019-10-21 11:41:10 +08004416 length = __le32_to_cpu(mac->blk_hdr.length);
Hayes Wang5a16a3d2019-10-21 11:41:11 +08004417 if (length < fw_offset) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004418 dev_err(&tp->intf->dev, "invalid fw_offset\n");
4419 goto out;
4420 }
4421
Hayes Wang5a16a3d2019-10-21 11:41:11 +08004422 length -= fw_offset;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004423 if (length < 4 || (length & 3)) {
4424 dev_err(&tp->intf->dev, "invalid block length\n");
4425 goto out;
4426 }
4427
Hayes Wanga66edaa2019-10-21 11:41:10 +08004428 if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004429 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4430 goto out;
4431 }
4432
Hayes Wanga66edaa2019-10-21 11:41:10 +08004433 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004434 dev_err(&tp->intf->dev, "invalid base address register\n");
4435 goto out;
4436 }
4437
Hayes Wanga66edaa2019-10-21 11:41:10 +08004438 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004439 dev_err(&tp->intf->dev, "invalid enabled mask register\n");
4440 goto out;
4441 }
4442
Hayes Wanga66edaa2019-10-21 11:41:10 +08004443 if (__le16_to_cpu(mac->bp_start) != bp_start) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004444 dev_err(&tp->intf->dev,
4445 "invalid start register of break point\n");
4446 goto out;
4447 }
4448
Hayes Wanga66edaa2019-10-21 11:41:10 +08004449 if (__le16_to_cpu(mac->bp_num) > max_bp) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004450 dev_err(&tp->intf->dev, "invalid break point number\n");
4451 goto out;
4452 }
4453
Hayes Wanga66edaa2019-10-21 11:41:10 +08004454 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
4455 if (mac->bp[i]) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004456 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
4457 goto out;
4458 }
4459 }
4460
4461 rc = true;
4462out:
4463 return rc;
4464}
4465
4466/* Verify the checksum for the firmware file. It is calculated from the version
4467 * field to the end of the file. Compare the result with the checksum field to
4468 * make sure the file is correct.
4469 */
4470static long rtl8152_fw_verify_checksum(struct r8152 *tp,
4471 struct fw_header *fw_hdr, size_t size)
4472{
4473 unsigned char checksum[sizeof(fw_hdr->checksum)];
4474 struct crypto_shash *alg;
4475 struct shash_desc *sdesc;
4476 size_t len;
4477 long rc;
4478
4479 alg = crypto_alloc_shash("sha256", 0, 0);
4480 if (IS_ERR(alg)) {
4481 rc = PTR_ERR(alg);
4482 goto out;
4483 }
4484
4485 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
4486 rc = -EFAULT;
4487 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
4488 crypto_shash_digestsize(alg));
4489 goto free_shash;
4490 }
4491
4492 len = sizeof(*sdesc) + crypto_shash_descsize(alg);
4493 sdesc = kmalloc(len, GFP_KERNEL);
4494 if (!sdesc) {
4495 rc = -ENOMEM;
4496 goto free_shash;
4497 }
4498 sdesc->tfm = alg;
4499
4500 len = size - sizeof(fw_hdr->checksum);
4501 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
4502 kfree(sdesc);
4503 if (rc)
4504 goto free_shash;
4505
4506 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
4507 dev_err(&tp->intf->dev, "checksum fail\n");
4508 rc = -EFAULT;
4509 }
4510
4511free_shash:
4512 crypto_free_shash(alg);
4513out:
4514 return rc;
4515}
4516
4517static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
4518{
4519 const struct firmware *fw = rtl_fw->fw;
4520 struct fw_header *fw_hdr = (struct fw_header *)fw->data;
Hayes Wanga8a7be12021-04-16 16:04:33 +08004521 unsigned long fw_flags = 0;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004522 long ret = -EFAULT;
4523 int i;
4524
4525 if (fw->size < sizeof(*fw_hdr)) {
4526 dev_err(&tp->intf->dev, "file too small\n");
4527 goto fail;
4528 }
4529
4530 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
4531 if (ret)
4532 goto fail;
4533
4534 ret = -EFAULT;
4535
4536 for (i = sizeof(*fw_hdr); i < fw->size;) {
4537 struct fw_block *block = (struct fw_block *)&fw->data[i];
4538 u32 type;
4539
4540 if ((i + sizeof(*block)) > fw->size)
4541 goto fail;
4542
4543 type = __le32_to_cpu(block->type);
4544 switch (type) {
4545 case RTL_FW_END:
4546 if (__le32_to_cpu(block->length) != sizeof(*block))
4547 goto fail;
Hayes Wangaf142882019-10-21 11:41:13 +08004548 goto fw_end;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004549 case RTL_FW_PLA:
Hayes Wanga8a7be12021-04-16 16:04:33 +08004550 if (test_bit(FW_FLAGS_PLA, &fw_flags)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004551 dev_err(&tp->intf->dev,
4552 "multiple PLA firmware encountered");
4553 goto fail;
4554 }
4555
Hayes Wanga8a7be12021-04-16 16:04:33 +08004556 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004557 dev_err(&tp->intf->dev,
Hayes Wanga66edaa2019-10-21 11:41:10 +08004558 "check PLA firmware failed\n");
Hayes Wang9370f2d2019-10-16 11:02:42 +08004559 goto fail;
4560 }
Hayes Wanga8a7be12021-04-16 16:04:33 +08004561 __set_bit(FW_FLAGS_PLA, &fw_flags);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004562 break;
4563 case RTL_FW_USB:
Hayes Wanga8a7be12021-04-16 16:04:33 +08004564 if (test_bit(FW_FLAGS_USB, &fw_flags)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004565 dev_err(&tp->intf->dev,
4566 "multiple USB firmware encountered");
4567 goto fail;
4568 }
4569
Hayes Wanga8a7be12021-04-16 16:04:33 +08004570 if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004571 dev_err(&tp->intf->dev,
Hayes Wanga66edaa2019-10-21 11:41:10 +08004572 "check USB firmware failed\n");
Hayes Wang9370f2d2019-10-16 11:02:42 +08004573 goto fail;
4574 }
Hayes Wanga8a7be12021-04-16 16:04:33 +08004575 __set_bit(FW_FLAGS_USB, &fw_flags);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004576 break;
Hayes Wangaf142882019-10-21 11:41:13 +08004577 case RTL_FW_PHY_START:
Hayes Wanga8a7be12021-04-16 16:04:33 +08004578 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4579 test_bit(FW_FLAGS_NC, &fw_flags) ||
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004580 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4581 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4582 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4583 test_bit(FW_FLAGS_UC, &fw_flags) ||
Hayes Wanga8a7be12021-04-16 16:04:33 +08004584 test_bit(FW_FLAGS_STOP, &fw_flags)) {
Hayes Wangaf142882019-10-21 11:41:13 +08004585 dev_err(&tp->intf->dev,
4586 "check PHY_START fail\n");
4587 goto fail;
4588 }
4589
Hayes Wanga8a7be12021-04-16 16:04:33 +08004590 if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) {
Hayes Wangaf142882019-10-21 11:41:13 +08004591 dev_err(&tp->intf->dev,
4592 "Invalid length for PHY_START\n");
4593 goto fail;
4594 }
Hayes Wanga8a7be12021-04-16 16:04:33 +08004595 __set_bit(FW_FLAGS_START, &fw_flags);
Hayes Wangaf142882019-10-21 11:41:13 +08004596 break;
4597 case RTL_FW_PHY_STOP:
Hayes Wanga8a7be12021-04-16 16:04:33 +08004598 if (test_bit(FW_FLAGS_STOP, &fw_flags) ||
4599 !test_bit(FW_FLAGS_START, &fw_flags)) {
Hayes Wangaf142882019-10-21 11:41:13 +08004600 dev_err(&tp->intf->dev,
4601 "Check PHY_STOP fail\n");
4602 goto fail;
4603 }
4604
4605 if (__le32_to_cpu(block->length) != sizeof(*block)) {
4606 dev_err(&tp->intf->dev,
4607 "Invalid length for PHY_STOP\n");
4608 goto fail;
4609 }
Hayes Wanga8a7be12021-04-16 16:04:33 +08004610 __set_bit(FW_FLAGS_STOP, &fw_flags);
Hayes Wangaf142882019-10-21 11:41:13 +08004611 break;
4612 case RTL_FW_PHY_NC:
Hayes Wanga8a7be12021-04-16 16:04:33 +08004613 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4614 test_bit(FW_FLAGS_STOP, &fw_flags)) {
Hayes Wangaf142882019-10-21 11:41:13 +08004615 dev_err(&tp->intf->dev,
4616 "check PHY_NC fail\n");
4617 goto fail;
4618 }
4619
Hayes Wanga8a7be12021-04-16 16:04:33 +08004620 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
Hayes Wangaf142882019-10-21 11:41:13 +08004621 dev_err(&tp->intf->dev,
4622 "multiple PHY NC encountered\n");
4623 goto fail;
4624 }
4625
Hayes Wanga8a7be12021-04-16 16:04:33 +08004626 if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) {
Hayes Wangaf142882019-10-21 11:41:13 +08004627 dev_err(&tp->intf->dev,
4628 "check PHY NC firmware failed\n");
4629 goto fail;
4630 }
Hayes Wanga8a7be12021-04-16 16:04:33 +08004631 __set_bit(FW_FLAGS_NC, &fw_flags);
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004632 break;
4633 case RTL_FW_PHY_UNION_NC:
4634 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4635 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4636 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4637 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4638 test_bit(FW_FLAGS_UC, &fw_flags) ||
4639 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4640 dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n");
4641 goto fail;
4642 }
Hayes Wangaf142882019-10-21 11:41:13 +08004643
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004644 if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4645 dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n");
4646 goto fail;
4647 }
4648
4649 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4650 dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n");
4651 goto fail;
4652 }
4653 __set_bit(FW_FLAGS_NC, &fw_flags);
4654 break;
4655 case RTL_FW_PHY_UNION_NC1:
4656 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4657 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4658 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4659 test_bit(FW_FLAGS_UC, &fw_flags) ||
4660 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4661 dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n");
4662 goto fail;
4663 }
4664
4665 if (test_bit(FW_FLAGS_NC1, &fw_flags)) {
4666 dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n");
4667 goto fail;
4668 }
4669
4670 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4671 dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n");
4672 goto fail;
4673 }
4674 __set_bit(FW_FLAGS_NC1, &fw_flags);
4675 break;
4676 case RTL_FW_PHY_UNION_NC2:
4677 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4678 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4679 test_bit(FW_FLAGS_UC, &fw_flags) ||
4680 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4681 dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n");
4682 goto fail;
4683 }
4684
4685 if (test_bit(FW_FLAGS_NC2, &fw_flags)) {
4686 dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n");
4687 goto fail;
4688 }
4689
4690 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4691 dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n");
4692 goto fail;
4693 }
4694 __set_bit(FW_FLAGS_NC2, &fw_flags);
4695 break;
4696 case RTL_FW_PHY_UNION_UC2:
4697 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4698 test_bit(FW_FLAGS_UC, &fw_flags) ||
4699 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4700 dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n");
4701 goto fail;
4702 }
4703
4704 if (test_bit(FW_FLAGS_UC2, &fw_flags)) {
4705 dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n");
4706 goto fail;
4707 }
4708
4709 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4710 dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n");
4711 goto fail;
4712 }
4713 __set_bit(FW_FLAGS_UC2, &fw_flags);
4714 break;
4715 case RTL_FW_PHY_UNION_UC:
4716 if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4717 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4718 dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n");
4719 goto fail;
4720 }
4721
4722 if (test_bit(FW_FLAGS_UC, &fw_flags)) {
4723 dev_err(&tp->intf->dev, "multiple PHY UC encountered\n");
4724 goto fail;
4725 }
4726
4727 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4728 dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n");
4729 goto fail;
4730 }
4731 __set_bit(FW_FLAGS_UC, &fw_flags);
4732 break;
4733 case RTL_FW_PHY_UNION_MISC:
4734 if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4735 dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n");
4736 goto fail;
4737 }
4738 break;
4739 case RTL_FW_PHY_FIXUP:
4740 if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) {
4741 dev_err(&tp->intf->dev, "check PHY fixup failed\n");
4742 goto fail;
4743 }
4744 break;
4745 case RTL_FW_PHY_SPEED_UP:
4746 if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) {
4747 dev_err(&tp->intf->dev, "multiple PHY firmware encountered");
4748 goto fail;
4749 }
4750
4751 if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) {
4752 dev_err(&tp->intf->dev, "check PHY speed up failed\n");
4753 goto fail;
4754 }
4755 __set_bit(FW_FLAGS_SPEED_UP, &fw_flags);
4756 break;
4757 case RTL_FW_PHY_VER:
4758 if (test_bit(FW_FLAGS_START, &fw_flags) ||
4759 test_bit(FW_FLAGS_NC, &fw_flags) ||
4760 test_bit(FW_FLAGS_NC1, &fw_flags) ||
4761 test_bit(FW_FLAGS_NC2, &fw_flags) ||
4762 test_bit(FW_FLAGS_UC2, &fw_flags) ||
4763 test_bit(FW_FLAGS_UC, &fw_flags) ||
4764 test_bit(FW_FLAGS_STOP, &fw_flags)) {
4765 dev_err(&tp->intf->dev, "Invalid order to set PHY version\n");
4766 goto fail;
4767 }
4768
4769 if (test_bit(FW_FLAGS_VER, &fw_flags)) {
4770 dev_err(&tp->intf->dev, "multiple PHY version encountered");
4771 goto fail;
4772 }
4773
4774 if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) {
4775 dev_err(&tp->intf->dev, "check PHY version failed\n");
4776 goto fail;
4777 }
4778 __set_bit(FW_FLAGS_VER, &fw_flags);
Hayes Wangaf142882019-10-21 11:41:13 +08004779 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004780 default:
4781 dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
4782 type);
4783 break;
4784 }
4785
4786 /* next block */
4787 i += ALIGN(__le32_to_cpu(block->length), 8);
4788 }
4789
Hayes Wangaf142882019-10-21 11:41:13 +08004790fw_end:
Hayes Wanga8a7be12021-04-16 16:04:33 +08004791 if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) {
Hayes Wangaf142882019-10-21 11:41:13 +08004792 dev_err(&tp->intf->dev, "without PHY_STOP\n");
4793 goto fail;
4794 }
4795
Hayes Wang9370f2d2019-10-16 11:02:42 +08004796 return 0;
4797fail:
4798 return ret;
4799}
4800
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004801static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait)
4802{
4803 u32 len;
4804 u8 *data;
4805
Hayes Wangb24edca2021-12-23 17:27:02 +08004806 rtl_reset_ocp_base(tp);
4807
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004808 if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) {
4809 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4810 return;
4811 }
4812
4813 len = __le32_to_cpu(phy->blk_hdr.length);
4814 len -= __le16_to_cpu(phy->fw_offset);
4815 data = (u8 *)phy + __le16_to_cpu(phy->fw_offset);
4816
4817 if (rtl_phy_patch_request(tp, true, wait))
4818 return;
4819
4820 while (len) {
4821 u32 ocp_data, size;
4822 int i;
4823
4824 if (len < 2048)
4825 size = len;
4826 else
4827 size = 2048;
4828
4829 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL);
4830 ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE;
4831 ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data);
4832
4833 generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB);
4834
4835 data += size;
4836 len -= size;
4837
4838 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL);
4839 ocp_data |= POL_GPHY_PATCH;
4840 ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data);
4841
4842 for (i = 0; i < 1000; i++) {
4843 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH))
4844 break;
4845 }
4846
4847 if (i == 1000) {
4848 dev_err(&tp->intf->dev, "ram code speedup mode timeout\n");
Hayes Wangf49c35b2021-04-22 16:48:02 +08004849 break;
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004850 }
4851 }
4852
Hayes Wangb24edca2021-12-23 17:27:02 +08004853 rtl_reset_ocp_base(tp);
4854
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004855 rtl_phy_patch_request(tp, false, wait);
4856
4857 if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version))
4858 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4859 else
4860 dev_err(&tp->intf->dev, "ram code speedup mode fail\n");
4861}
4862
4863static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver)
4864{
4865 u16 ver_addr, ver;
4866
4867 ver_addr = __le16_to_cpu(phy_ver->ver.addr);
4868 ver = __le16_to_cpu(phy_ver->ver.data);
4869
Hayes Wangb24edca2021-12-23 17:27:02 +08004870 rtl_reset_ocp_base(tp);
4871
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004872 if (sram_read(tp, ver_addr) >= ver) {
4873 dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4874 return 0;
4875 }
4876
4877 sram_write(tp, ver_addr, ver);
4878
4879 dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver);
4880
4881 return ver;
4882}
4883
4884static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix)
4885{
4886 u16 addr, data;
4887
Hayes Wangb24edca2021-12-23 17:27:02 +08004888 rtl_reset_ocp_base(tp);
4889
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004890 addr = __le16_to_cpu(fix->setting.addr);
4891 data = ocp_reg_read(tp, addr);
4892
4893 switch (__le16_to_cpu(fix->bit_cmd)) {
4894 case FW_FIXUP_AND:
4895 data &= __le16_to_cpu(fix->setting.data);
4896 break;
4897 case FW_FIXUP_OR:
4898 data |= __le16_to_cpu(fix->setting.data);
4899 break;
4900 case FW_FIXUP_NOT:
4901 data &= ~__le16_to_cpu(fix->setting.data);
4902 break;
4903 case FW_FIXUP_XOR:
4904 data ^= __le16_to_cpu(fix->setting.data);
4905 break;
4906 default:
4907 return;
4908 }
4909
4910 ocp_reg_write(tp, addr, data);
4911
4912 dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data);
4913}
4914
4915static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy)
4916{
4917 __le16 *data;
4918 u32 length;
4919 int i, num;
4920
Hayes Wangb24edca2021-12-23 17:27:02 +08004921 rtl_reset_ocp_base(tp);
4922
Hayes Wang4a51b0e2021-04-16 16:04:36 +08004923 num = phy->pre_num;
4924 for (i = 0; i < num; i++)
4925 sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr),
4926 __le16_to_cpu(phy->pre_set[i].data));
4927
4928 length = __le32_to_cpu(phy->blk_hdr.length);
4929 length -= __le16_to_cpu(phy->fw_offset);
4930 num = length / 2;
4931 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4932
4933 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4934 for (i = 0; i < num; i++)
4935 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4936
4937 num = phy->bp_num;
4938 for (i = 0; i < num; i++)
4939 sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data));
4940
4941 if (phy->bp_num && phy->bp_en.addr)
4942 sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data));
4943
4944 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4945}
4946
Hayes Wangaf142882019-10-21 11:41:13 +08004947static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
4948{
4949 u16 mode_reg, bp_index;
4950 u32 length, i, num;
4951 __le16 *data;
4952
Hayes Wangb24edca2021-12-23 17:27:02 +08004953 rtl_reset_ocp_base(tp);
4954
Hayes Wangaf142882019-10-21 11:41:13 +08004955 mode_reg = __le16_to_cpu(phy->mode_reg);
4956 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
4957 sram_write(tp, __le16_to_cpu(phy->ba_reg),
4958 __le16_to_cpu(phy->ba_data));
4959
4960 length = __le32_to_cpu(phy->blk_hdr.length);
4961 length -= __le16_to_cpu(phy->fw_offset);
4962 num = length / 2;
4963 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4964
4965 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4966 for (i = 0; i < num; i++)
4967 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4968
4969 sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
4970 __le16_to_cpu(phy->patch_en_value));
4971
4972 bp_index = __le16_to_cpu(phy->bp_start);
4973 num = __le16_to_cpu(phy->bp_num);
4974 for (i = 0; i < num; i++) {
4975 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
4976 bp_index += 2;
4977 }
4978
4979 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
4980
4981 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4982}
4983
Hayes Wanga66edaa2019-10-21 11:41:10 +08004984static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
Hayes Wang9370f2d2019-10-16 11:02:42 +08004985{
4986 u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg;
4987 u32 length;
4988 u8 *data;
4989 int i;
4990
Hayes Wanga66edaa2019-10-21 11:41:10 +08004991 switch (__le32_to_cpu(mac->blk_hdr.type)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08004992 case RTL_FW_PLA:
4993 type = MCU_TYPE_PLA;
4994 break;
4995 case RTL_FW_USB:
4996 type = MCU_TYPE_USB;
4997 break;
4998 default:
4999 return;
5000 }
5001
Hayes Wang4a51b0e2021-04-16 16:04:36 +08005002 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
5003 if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) {
5004 dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB");
5005 return;
5006 }
5007
Hayes Wang9370f2d2019-10-16 11:02:42 +08005008 rtl_clear_bp(tp, type);
5009
5010 /* Enable backup/restore of MACDBG. This is required after clearing PLA
5011 * break points and before applying the PLA firmware.
5012 */
5013 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
5014 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
5015 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
5016 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
5017 }
5018
Hayes Wanga66edaa2019-10-21 11:41:10 +08005019 length = __le32_to_cpu(mac->blk_hdr.length);
5020 length -= __le16_to_cpu(mac->fw_offset);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005021
Hayes Wanga66edaa2019-10-21 11:41:10 +08005022 data = (u8 *)mac;
5023 data += __le16_to_cpu(mac->fw_offset);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005024
Hayes Wanga66edaa2019-10-21 11:41:10 +08005025 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
Hayes Wang9370f2d2019-10-16 11:02:42 +08005026 type);
5027
Hayes Wanga66edaa2019-10-21 11:41:10 +08005028 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
5029 __le16_to_cpu(mac->bp_ba_value));
Hayes Wang9370f2d2019-10-16 11:02:42 +08005030
Hayes Wanga66edaa2019-10-21 11:41:10 +08005031 bp_index = __le16_to_cpu(mac->bp_start);
5032 bp_num = __le16_to_cpu(mac->bp_num);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005033 for (i = 0; i < bp_num; i++) {
Hayes Wanga66edaa2019-10-21 11:41:10 +08005034 ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i]));
Hayes Wang9370f2d2019-10-16 11:02:42 +08005035 bp_index += 2;
5036 }
5037
Hayes Wanga66edaa2019-10-21 11:41:10 +08005038 bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005039 if (bp_en_addr)
5040 ocp_write_word(tp, type, bp_en_addr,
Hayes Wanga66edaa2019-10-21 11:41:10 +08005041 __le16_to_cpu(mac->bp_en_value));
Hayes Wang9370f2d2019-10-16 11:02:42 +08005042
Hayes Wang9370f2d2019-10-16 11:02:42 +08005043 if (fw_ver_reg)
5044 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
Hayes Wanga66edaa2019-10-21 11:41:10 +08005045 mac->fw_ver_data);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005046
Hayes Wanga66edaa2019-10-21 11:41:10 +08005047 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005048}
5049
Hayes Wanga08c0d32021-02-03 17:14:28 +08005050static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
Hayes Wang9370f2d2019-10-16 11:02:42 +08005051{
5052 struct rtl_fw *rtl_fw = &tp->rtl_fw;
Hayes Wang8e484eb2019-10-23 21:24:44 +08005053 const struct firmware *fw;
5054 struct fw_header *fw_hdr;
Hayes Wangaf142882019-10-21 11:41:13 +08005055 struct fw_phy_patch_key *key;
5056 u16 key_addr = 0;
Hayes Wang4a51b0e2021-04-16 16:04:36 +08005057 int i, patch_phy = 1;
Hayes Wang9370f2d2019-10-16 11:02:42 +08005058
5059 if (IS_ERR_OR_NULL(rtl_fw->fw))
5060 return;
5061
Hayes Wang8e484eb2019-10-23 21:24:44 +08005062 fw = rtl_fw->fw;
5063 fw_hdr = (struct fw_header *)fw->data;
5064
Hayes Wang9370f2d2019-10-16 11:02:42 +08005065 if (rtl_fw->pre_fw)
5066 rtl_fw->pre_fw(tp);
5067
5068 for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
5069 struct fw_block *block = (struct fw_block *)&fw->data[i];
5070
5071 switch (__le32_to_cpu(block->type)) {
5072 case RTL_FW_END:
5073 goto post_fw;
5074 case RTL_FW_PLA:
5075 case RTL_FW_USB:
Hayes Wanga66edaa2019-10-21 11:41:10 +08005076 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005077 break;
Hayes Wangaf142882019-10-21 11:41:13 +08005078 case RTL_FW_PHY_START:
Hayes Wang4a51b0e2021-04-16 16:04:36 +08005079 if (!patch_phy)
5080 break;
Hayes Wangaf142882019-10-21 11:41:13 +08005081 key = (struct fw_phy_patch_key *)block;
5082 key_addr = __le16_to_cpu(key->key_reg);
Hayes Wanga08c0d32021-02-03 17:14:28 +08005083 rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut);
Hayes Wangaf142882019-10-21 11:41:13 +08005084 break;
5085 case RTL_FW_PHY_STOP:
Hayes Wang4a51b0e2021-04-16 16:04:36 +08005086 if (!patch_phy)
5087 break;
Hayes Wangaf142882019-10-21 11:41:13 +08005088 WARN_ON(!key_addr);
Hayes Wanga08c0d32021-02-03 17:14:28 +08005089 rtl_post_ram_code(tp, key_addr, !power_cut);
Hayes Wangaf142882019-10-21 11:41:13 +08005090 break;
5091 case RTL_FW_PHY_NC:
5092 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
5093 break;
Hayes Wang4a51b0e2021-04-16 16:04:36 +08005094 case RTL_FW_PHY_VER:
5095 patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block);
5096 break;
5097 case RTL_FW_PHY_UNION_NC:
5098 case RTL_FW_PHY_UNION_NC1:
5099 case RTL_FW_PHY_UNION_NC2:
5100 case RTL_FW_PHY_UNION_UC2:
5101 case RTL_FW_PHY_UNION_UC:
5102 case RTL_FW_PHY_UNION_MISC:
5103 if (patch_phy)
5104 rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block);
5105 break;
5106 case RTL_FW_PHY_FIXUP:
5107 if (patch_phy)
5108 rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block);
5109 break;
5110 case RTL_FW_PHY_SPEED_UP:
5111 rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut);
5112 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08005113 default:
5114 break;
5115 }
5116
5117 i += ALIGN(__le32_to_cpu(block->length), 8);
5118 }
5119
5120post_fw:
5121 if (rtl_fw->post_fw)
5122 rtl_fw->post_fw(tp);
5123
Hayes Wangb24edca2021-12-23 17:27:02 +08005124 rtl_reset_ocp_base(tp);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005125 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
5126 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
5127}
5128
5129static void rtl8152_release_firmware(struct r8152 *tp)
5130{
5131 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5132
5133 if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
5134 release_firmware(rtl_fw->fw);
5135 rtl_fw->fw = NULL;
5136 }
5137}
5138
5139static int rtl8152_request_firmware(struct r8152 *tp)
5140{
5141 struct rtl_fw *rtl_fw = &tp->rtl_fw;
5142 long rc;
5143
5144 if (rtl_fw->fw || !rtl_fw->fw_name) {
5145 dev_info(&tp->intf->dev, "skip request firmware\n");
5146 rc = 0;
5147 goto result;
5148 }
5149
5150 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
5151 if (rc < 0)
5152 goto result;
5153
5154 rc = rtl8152_check_firmware(tp, rtl_fw);
5155 if (rc < 0)
5156 release_firmware(rtl_fw->fw);
5157
5158result:
5159 if (rc) {
5160 rtl_fw->fw = ERR_PTR(rc);
5161
5162 dev_warn(&tp->intf->dev,
5163 "unable to load firmware patch %s (%ld)\n",
5164 rtl_fw->fw_name, rc);
5165 }
5166
5167 return rc;
5168}
5169
hayeswangcda9fb02016-01-07 17:51:12 +08005170static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08005171{
hayeswangcda9fb02016-01-07 17:51:12 +08005172 if (enable) {
5173 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
5174 LINKENA | DIS_SDSAVE);
5175 } else {
5176 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
5177 DIS_SDSAVE);
5178 msleep(20);
5179 }
hayeswang43499682014-02-18 21:48:58 +08005180}
5181
hayeswange6449532016-09-20 16:22:05 +08005182static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
5183{
5184 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
5185 ocp_reg_write(tp, OCP_EEE_DATA, reg);
5186 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
5187}
5188
5189static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
5190{
5191 u16 data;
5192
5193 r8152_mmd_indirect(tp, dev, reg);
5194 data = ocp_reg_read(tp, OCP_EEE_DATA);
5195 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5196
5197 return data;
5198}
5199
5200static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
5201{
5202 r8152_mmd_indirect(tp, dev, reg);
5203 ocp_reg_write(tp, OCP_EEE_DATA, data);
5204 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5205}
5206
5207static void r8152_eee_en(struct r8152 *tp, bool enable)
5208{
5209 u16 config1, config2, config3;
5210 u32 ocp_data;
5211
5212 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5213 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
5214 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
5215 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
5216
5217 if (enable) {
5218 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5219 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
5220 config1 |= sd_rise_time(1);
5221 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
5222 config3 |= fast_snr(42);
5223 } else {
5224 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5225 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
5226 RX_QUIET_EN);
5227 config1 |= sd_rise_time(7);
5228 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
5229 config3 |= fast_snr(511);
5230 }
5231
5232 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5233 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
5234 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
5235 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
5236}
5237
Hayes Wange7bde562019-08-23 15:33:41 +08005238static void r8153_eee_en(struct r8152 *tp, bool enable)
hayeswange6449532016-09-20 16:22:05 +08005239{
Hayes Wange7bde562019-08-23 15:33:41 +08005240 u32 ocp_data;
5241 u16 config;
5242
5243 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5244 config = ocp_reg_read(tp, OCP_EEE_CFG);
5245
5246 if (enable) {
5247 ocp_data |= EEE_RX_EN | EEE_TX_EN;
5248 config |= EEE10_EN;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005249 } else {
Hayes Wange7bde562019-08-23 15:33:41 +08005250 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5251 config &= ~EEE10_EN;
5252 }
5253
5254 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5255 ocp_reg_write(tp, OCP_EEE_CFG, config);
Hayes Wange7bde562019-08-23 15:33:41 +08005256
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005257 tp->ups_info.eee = enable;
Hayes Wange7bde562019-08-23 15:33:41 +08005258}
5259
Hayes Wang195aae32021-04-16 16:04:35 +08005260static void r8156_eee_en(struct r8152 *tp, bool enable)
5261{
5262 u16 config;
5263
5264 r8153_eee_en(tp, enable);
5265
5266 config = ocp_reg_read(tp, OCP_EEE_ADV2);
5267
5268 if (enable)
5269 config |= MDIO_EEE_2_5GT;
5270 else
5271 config &= ~MDIO_EEE_2_5GT;
5272
5273 ocp_reg_write(tp, OCP_EEE_ADV2, config);
5274}
5275
Hayes Wange7bde562019-08-23 15:33:41 +08005276static void rtl_eee_enable(struct r8152 *tp, bool enable)
5277{
5278 switch (tp->version) {
5279 case RTL_VER_01:
5280 case RTL_VER_02:
5281 case RTL_VER_07:
5282 if (enable) {
5283 r8152_eee_en(tp, true);
5284 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
5285 tp->eee_adv);
5286 } else {
5287 r8152_eee_en(tp, false);
5288 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
5289 }
5290 break;
5291 case RTL_VER_03:
5292 case RTL_VER_04:
5293 case RTL_VER_05:
5294 case RTL_VER_06:
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005295 case RTL_VER_08:
5296 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08005297 case RTL_VER_14:
Hayes Wange7bde562019-08-23 15:33:41 +08005298 if (enable) {
5299 r8153_eee_en(tp, true);
5300 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5301 } else {
5302 r8153_eee_en(tp, false);
5303 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5304 }
5305 break;
Hayes Wang195aae32021-04-16 16:04:35 +08005306 case RTL_VER_10:
5307 case RTL_VER_11:
5308 case RTL_VER_12:
5309 case RTL_VER_13:
5310 case RTL_VER_15:
5311 if (enable) {
5312 r8156_eee_en(tp, true);
5313 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5314 } else {
5315 r8156_eee_en(tp, false);
5316 ocp_reg_write(tp, OCP_EEE_ADV, 0);
5317 }
5318 break;
Hayes Wange7bde562019-08-23 15:33:41 +08005319 default:
5320 break;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005321 }
hayeswange6449532016-09-20 16:22:05 +08005322}
5323
5324static void r8152b_enable_fc(struct r8152 *tp)
5325{
5326 u16 anar;
5327
5328 anar = r8152_mdio_read(tp, MII_ADVERTISE);
5329 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
5330 r8152_mdio_write(tp, MII_ADVERTISE, anar);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005331
5332 tp->ups_info.flow_control = true;
hayeswange6449532016-09-20 16:22:05 +08005333}
5334
hayeswangd70b1132014-09-19 15:17:18 +08005335static void rtl8152_disable(struct r8152 *tp)
5336{
hayeswangcda9fb02016-01-07 17:51:12 +08005337 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08005338 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08005339 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08005340}
5341
hayeswang43499682014-02-18 21:48:58 +08005342static void r8152b_hw_phy_cfg(struct r8152 *tp)
5343{
Hayes Wanga08c0d32021-02-03 17:14:28 +08005344 rtl8152_apply_firmware(tp, false);
Hayes Wange7bde562019-08-23 15:33:41 +08005345 rtl_eee_enable(tp, tp->eee_en);
hayeswangef39df82016-09-20 16:22:07 +08005346 r8152_aldps_en(tp, true);
5347 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08005348
hayeswangaa66a5f2014-02-18 21:49:04 +08005349 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08005350}
5351
Prashant Malani5f71c842019-10-01 01:35:57 -07005352static void wait_oob_link_list_ready(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00005353{
hayeswangdb8515e2014-03-06 15:07:16 +08005354 u32 ocp_data;
5355 int i;
hayeswangac718b62013-05-02 16:01:25 +00005356
Prashant Malani5f71c842019-10-01 01:35:57 -07005357 for (i = 0; i < 1000; i++) {
5358 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5359 if (ocp_data & LINK_LIST_READY)
5360 break;
5361 usleep_range(1000, 2000);
5362 }
5363}
5364
Hayes Wang195aae32021-04-16 16:04:35 +08005365static void r8156b_wait_loading_flash(struct r8152 *tp)
5366{
5367 if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) &&
5368 !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) {
5369 int i;
5370
5371 for (i = 0; i < 100; i++) {
5372 if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE)
5373 break;
5374 usleep_range(1000, 2000);
5375 }
5376 }
5377}
5378
Prashant Malani5f71c842019-10-01 01:35:57 -07005379static void r8152b_exit_oob(struct r8152 *tp)
5380{
5381 u32 ocp_data;
5382
hayeswangac718b62013-05-02 16:01:25 +00005383 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5384 ocp_data &= ~RCR_ACPT_ALL;
5385 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5386
hayeswang00a5e362014-02-18 21:48:59 +08005387 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08005388 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00005389 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
5390 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
5391
5392 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5393 ocp_data &= ~NOW_IS_OOB;
5394 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5395
5396 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5397 ocp_data &= ~MCU_BORW_EN;
5398 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5399
Prashant Malani5f71c842019-10-01 01:35:57 -07005400 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00005401
5402 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5403 ocp_data |= RE_INIT_LL;
5404 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5405
Prashant Malani5f71c842019-10-01 01:35:57 -07005406 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00005407
5408 rtl8152_nic_reset(tp);
5409
5410 /* rx share fifo credit full threshold */
5411 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5412
hayeswanga3cc4652014-07-24 16:37:43 +08005413 if (tp->udev->speed == USB_SPEED_FULL ||
5414 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00005415 /* rx share fifo credit near full threshold */
5416 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5417 RXFIFO_THR2_FULL);
5418 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5419 RXFIFO_THR3_FULL);
5420 } else {
5421 /* rx share fifo credit near full threshold */
5422 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5423 RXFIFO_THR2_HIGH);
5424 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5425 RXFIFO_THR3_HIGH);
5426 }
5427
5428 /* TX share fifo free credit full threshold */
Hayes Wang195aae32021-04-16 16:04:35 +08005429 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswangac718b62013-05-02 16:01:25 +00005430
5431 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08005432 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00005433 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
5434 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
5435
hayeswangc5554292014-09-12 10:43:11 +08005436 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00005437
5438 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5439
5440 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5441 ocp_data |= TCR0_AUTO_FIFO;
5442 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5443}
5444
5445static void r8152b_enter_oob(struct r8152 *tp)
5446{
hayeswang45f4a192014-01-06 17:08:41 +08005447 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00005448
5449 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5450 ocp_data &= ~NOW_IS_OOB;
5451 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5452
5453 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
5454 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
5455 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
5456
hayeswangd70b1132014-09-19 15:17:18 +08005457 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00005458
Prashant Malani5f71c842019-10-01 01:35:57 -07005459 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00005460
5461 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5462 ocp_data |= RE_INIT_LL;
5463 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5464
Prashant Malani5f71c842019-10-01 01:35:57 -07005465 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00005466
5467 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5468
hayeswangc5554292014-09-12 10:43:11 +08005469 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00005470
Kevin Lo59c0b472019-08-01 11:29:38 +08005471 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00005472 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08005473 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00005474
5475 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5476 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5477 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5478
hayeswang00a5e362014-02-18 21:48:59 +08005479 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00005480
5481 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5482 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5483 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5484}
5485
Hayes Wang9370f2d2019-10-16 11:02:42 +08005486static int r8153_pre_firmware_1(struct r8152 *tp)
5487{
5488 int i;
5489
5490 /* Wait till the WTD timer is ready. It would take at most 104 ms. */
5491 for (i = 0; i < 104; i++) {
5492 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
5493
5494 if (!(ocp_data & WTD1_EN))
5495 break;
5496 usleep_range(1000, 2000);
5497 }
5498
5499 return 0;
5500}
5501
5502static int r8153_post_firmware_1(struct r8152 *tp)
5503{
5504 /* set USB_BP_4 to support USB_SPEED_SUPER only */
5505 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
5506 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
5507
5508 /* reset UPHY timer to 36 ms */
5509 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5510
5511 return 0;
5512}
5513
5514static int r8153_pre_firmware_2(struct r8152 *tp)
5515{
5516 u32 ocp_data;
5517
5518 r8153_pre_firmware_1(tp);
5519
5520 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5521 ocp_data &= ~FW_FIX_SUSPEND;
5522 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5523
5524 return 0;
5525}
5526
5527static int r8153_post_firmware_2(struct r8152 *tp)
5528{
5529 u32 ocp_data;
5530
5531 /* enable bp0 if support USB_SPEED_SUPER only */
5532 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
5533 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5534 ocp_data |= BIT(0);
5535 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5536 }
5537
5538 /* reset UPHY timer to 36 ms */
5539 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5540
5541 /* enable U3P3 check, set the counter to 4 */
5542 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
5543
5544 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5545 ocp_data |= FW_FIX_SUSPEND;
5546 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5547
5548 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5549 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5550 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5551
5552 return 0;
5553}
5554
5555static int r8153_post_firmware_3(struct r8152 *tp)
5556{
5557 u32 ocp_data;
5558
5559 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5560 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5561 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5562
5563 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5564 ocp_data |= FW_IP_RESET_EN;
5565 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5566
5567 return 0;
5568}
5569
5570static int r8153b_pre_firmware_1(struct r8152 *tp)
5571{
5572 /* enable fc timer and set timer to 1 second. */
5573 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
5574 CTRL_TIMER_EN | (1000 / 8));
5575
5576 return 0;
5577}
5578
5579static int r8153b_post_firmware_1(struct r8152 *tp)
5580{
5581 u32 ocp_data;
5582
5583 /* enable bp0 for RTL8153-BND */
5584 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
5585 if (ocp_data & BND_MASK) {
5586 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5587 ocp_data |= BIT(0);
5588 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5589 }
5590
5591 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5592 ocp_data |= FLOW_CTRL_PATCH_OPT;
5593 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5594
5595 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5596 ocp_data |= FC_PATCH_TASK;
5597 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5598
5599 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5600 ocp_data |= FW_IP_RESET_EN;
5601 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5602
5603 return 0;
5604}
5605
Hayes Wang195aae32021-04-16 16:04:35 +08005606static int r8153c_post_firmware_1(struct r8152 *tp)
5607{
5608 u32 ocp_data;
5609
5610 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5611 ocp_data |= FLOW_CTRL_PATCH_2;
5612 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5613
5614 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5615 ocp_data |= FC_PATCH_TASK;
5616 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5617
5618 return 0;
5619}
5620
Hayes Wang4a51b0e2021-04-16 16:04:36 +08005621static int r8156a_post_firmware_1(struct r8152 *tp)
5622{
5623 u32 ocp_data;
5624
5625 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5626 ocp_data |= FW_IP_RESET_EN;
5627 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5628
5629 /* Modify U3PHY parameter for compatibility issue */
5630 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e);
5631 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9);
5632
5633 return 0;
5634}
5635
hayeswange6449532016-09-20 16:22:05 +08005636static void r8153_aldps_en(struct r8152 *tp, bool enable)
5637{
5638 u16 data;
5639
5640 data = ocp_reg_read(tp, OCP_POWER_CFG);
5641 if (enable) {
5642 data |= EN_ALDPS;
5643 ocp_reg_write(tp, OCP_POWER_CFG, data);
5644 } else {
hayeswang4214cc52017-06-09 17:11:46 +08005645 int i;
5646
hayeswange6449532016-09-20 16:22:05 +08005647 data &= ~EN_ALDPS;
5648 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08005649 for (i = 0; i < 20; i++) {
5650 usleep_range(1000, 2000);
5651 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
5652 break;
5653 }
hayeswange6449532016-09-20 16:22:05 +08005654 }
hayeswange6449532016-09-20 16:22:05 +08005655
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005656 tp->ups_info.aldps = enable;
hayeswang65b82d62017-06-15 14:44:03 +08005657}
5658
hayeswang43779f82014-01-02 11:25:10 +08005659static void r8153_hw_phy_cfg(struct r8152 *tp)
5660{
5661 u32 ocp_data;
5662 u16 data;
5663
hayeswangd768c612016-09-20 16:22:09 +08005664 /* disable ALDPS before updating the PHY parameters */
5665 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08005666
hayeswangd768c612016-09-20 16:22:09 +08005667 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08005668 rtl_eee_enable(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005669
Hayes Wanga08c0d32021-02-03 17:14:28 +08005670 rtl8152_apply_firmware(tp, false);
Hayes Wang9370f2d2019-10-16 11:02:42 +08005671
hayeswang43779f82014-01-02 11:25:10 +08005672 if (tp->version == RTL_VER_03) {
5673 data = ocp_reg_read(tp, OCP_EEE_CFG);
5674 data &= ~CTAP_SHORT_EN;
5675 ocp_reg_write(tp, OCP_EEE_CFG, data);
5676 }
5677
5678 data = ocp_reg_read(tp, OCP_POWER_CFG);
5679 data |= EEE_CLKDIV_EN;
5680 ocp_reg_write(tp, OCP_POWER_CFG, data);
5681
5682 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5683 data |= EN_10M_BGOFF;
5684 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5685 data = ocp_reg_read(tp, OCP_POWER_CFG);
5686 data |= EN_10M_PLLOFF;
5687 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08005688 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08005689
5690 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5691 ocp_data |= PFM_PWM_SWITCH;
5692 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5693
hayeswangb4d99de2015-01-19 17:02:46 +08005694 /* Enable LPF corner auto tune */
5695 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08005696
hayeswangb4d99de2015-01-19 17:02:46 +08005697 /* Adjust 10M Amplitude */
5698 sram_write(tp, SRAM_10M_AMP1, 0x00af);
5699 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08005700
Hayes Wange7bde562019-08-23 15:33:41 +08005701 if (tp->eee_en)
5702 rtl_eee_enable(tp, true);
hayeswangaf0287e2016-09-20 16:22:08 +08005703
hayeswangef39df82016-09-20 16:22:07 +08005704 r8153_aldps_en(tp, true);
5705 r8152b_enable_fc(tp);
5706
hayeswang3cb32342017-06-09 17:11:43 +08005707 switch (tp->version) {
5708 case RTL_VER_03:
5709 case RTL_VER_04:
5710 break;
5711 case RTL_VER_05:
5712 case RTL_VER_06:
5713 default:
5714 r8153_u2p3en(tp, true);
5715 break;
5716 }
5717
hayeswangaa66a5f2014-02-18 21:49:04 +08005718 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08005719}
5720
hayeswang65b82d62017-06-15 14:44:03 +08005721static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
5722{
5723 u32 ocp_data;
5724
5725 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
5726 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
5727 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
5728 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
5729
5730 return ocp_data;
5731}
5732
5733static void r8153b_hw_phy_cfg(struct r8152 *tp)
5734{
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005735 u32 ocp_data;
hayeswang65b82d62017-06-15 14:44:03 +08005736 u16 data;
5737
Hayes Wang80fd8502021-02-03 17:14:29 +08005738 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
5739 if (ocp_data & PCUT_STATUS) {
5740 ocp_data &= ~PCUT_STATUS;
5741 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
5742 }
5743
hayeswang65b82d62017-06-15 14:44:03 +08005744 /* disable ALDPS before updating the PHY parameters */
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005745 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005746
5747 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08005748 rtl_eee_enable(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005749
Hayes Wang80fd8502021-02-03 17:14:29 +08005750 /* U1/U2/L1 idle timer. 500 us */
5751 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5752
5753 data = r8153_phy_status(tp, 0);
5754
5755 switch (data) {
5756 case PHY_STAT_PWRDN:
5757 case PHY_STAT_EXT_INIT:
5758 rtl8152_apply_firmware(tp, true);
5759
5760 data = r8152_mdio_read(tp, MII_BMCR);
5761 data &= ~BMCR_PDOWN;
5762 r8152_mdio_write(tp, MII_BMCR, data);
5763 break;
5764 case PHY_STAT_LAN_ON:
5765 default:
5766 rtl8152_apply_firmware(tp, false);
5767 break;
5768 }
Hayes Wang9370f2d2019-10-16 11:02:42 +08005769
hayeswang65b82d62017-06-15 14:44:03 +08005770 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
5771
5772 data = sram_read(tp, SRAM_GREEN_CFG);
5773 data |= R_TUNE_EN;
5774 sram_write(tp, SRAM_GREEN_CFG, data);
5775 data = ocp_reg_read(tp, OCP_NCTL_CFG);
5776 data |= PGA_RETURN_EN;
5777 ocp_reg_write(tp, OCP_NCTL_CFG, data);
5778
5779 /* ADC Bias Calibration:
5780 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
5781 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
5782 * ADC ioffset.
5783 */
5784 ocp_data = r8152_efuse_read(tp, 0x7d);
5785 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
5786 if (data != 0xffff)
5787 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
5788
5789 /* ups mode tx-link-pulse timing adjustment:
5790 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
5791 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
5792 */
5793 ocp_data = ocp_reg_read(tp, 0xc426);
5794 ocp_data &= 0x3fff;
5795 if (ocp_data) {
5796 u32 swr_cnt_1ms_ini;
5797
5798 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
5799 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
5800 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
5801 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
5802 }
5803
5804 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5805 ocp_data |= PFM_PWM_SWITCH;
5806 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5807
5808 /* Advnace EEE */
Hayes Wanga08c0d32021-02-03 17:14:28 +08005809 if (!rtl_phy_patch_request(tp, true, true)) {
hayeswang65b82d62017-06-15 14:44:03 +08005810 data = ocp_reg_read(tp, OCP_POWER_CFG);
5811 data |= EEE_CLKDIV_EN;
5812 ocp_reg_write(tp, OCP_POWER_CFG, data);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005813 tp->ups_info.eee_ckdiv = true;
hayeswang65b82d62017-06-15 14:44:03 +08005814
5815 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5816 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
5817 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005818 tp->ups_info.eee_cmod_lv = true;
5819 tp->ups_info._10m_ckdiv = true;
5820 tp->ups_info.eee_plloff_giga = true;
hayeswang65b82d62017-06-15 14:44:03 +08005821
5822 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
5823 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005824 tp->ups_info._250m_ckdiv = true;
hayeswang65b82d62017-06-15 14:44:03 +08005825
Hayes Wanga08c0d32021-02-03 17:14:28 +08005826 rtl_phy_patch_request(tp, false, true);
hayeswang65b82d62017-06-15 14:44:03 +08005827 }
5828
Hayes Wange7bde562019-08-23 15:33:41 +08005829 if (tp->eee_en)
5830 rtl_eee_enable(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08005831
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005832 r8153_aldps_en(tp, true);
5833 r8152b_enable_fc(tp);
hayeswang65b82d62017-06-15 14:44:03 +08005834
5835 set_bit(PHY_RESET, &tp->flags);
5836}
5837
Hayes Wang195aae32021-04-16 16:04:35 +08005838static void r8153c_hw_phy_cfg(struct r8152 *tp)
5839{
5840 r8153b_hw_phy_cfg(tp);
5841
5842 tp->ups_info.r_tune = true;
5843}
5844
Hayes Wang67ce1a82021-04-16 16:04:34 +08005845static void rtl8153_change_mtu(struct r8152 *tp)
5846{
5847 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
5848 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
5849}
5850
hayeswang43779f82014-01-02 11:25:10 +08005851static void r8153_first_init(struct r8152 *tp)
5852{
5853 u32 ocp_data;
hayeswang43779f82014-01-02 11:25:10 +08005854
hayeswang00a5e362014-02-18 21:48:59 +08005855 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08005856 r8153_teredo_off(tp);
5857
5858 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5859 ocp_data &= ~RCR_ACPT_ALL;
5860 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5861
hayeswang43779f82014-01-02 11:25:10 +08005862 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08005863 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08005864
5865 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5866 ocp_data &= ~NOW_IS_OOB;
5867 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5868
5869 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5870 ocp_data &= ~MCU_BORW_EN;
5871 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5872
Prashant Malani5f71c842019-10-01 01:35:57 -07005873 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08005874
5875 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5876 ocp_data |= RE_INIT_LL;
5877 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5878
Prashant Malani5f71c842019-10-01 01:35:57 -07005879 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08005880
hayeswangc5554292014-09-12 10:43:11 +08005881 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08005882
Hayes Wang67ce1a82021-04-16 16:04:34 +08005883 rtl8153_change_mtu(tp);
hayeswang43779f82014-01-02 11:25:10 +08005884
5885 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5886 ocp_data |= TCR0_AUTO_FIFO;
5887 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5888
5889 rtl8152_nic_reset(tp);
5890
5891 /* rx share fifo credit full threshold */
5892 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5893 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
5894 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
5895 /* TX share fifo free credit full threshold */
5896 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08005897}
5898
5899static void r8153_enter_oob(struct r8152 *tp)
5900{
5901 u32 ocp_data;
hayeswang43779f82014-01-02 11:25:10 +08005902
5903 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5904 ocp_data &= ~NOW_IS_OOB;
5905 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5906
hayeswangd70b1132014-09-19 15:17:18 +08005907 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08005908 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08005909
Prashant Malani5f71c842019-10-01 01:35:57 -07005910 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08005911
5912 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5913 ocp_data |= RE_INIT_LL;
5914 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5915
Prashant Malani5f71c842019-10-01 01:35:57 -07005916 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08005917
Hayes Wang67ce1a82021-04-16 16:04:34 +08005918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
hayeswang43779f82014-01-02 11:25:10 +08005919
hayeswang65b82d62017-06-15 14:44:03 +08005920 switch (tp->version) {
5921 case RTL_VER_03:
5922 case RTL_VER_04:
5923 case RTL_VER_05:
5924 case RTL_VER_06:
5925 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
5926 ocp_data &= ~TEREDO_WAKE_MASK;
5927 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
5928 break;
5929
5930 case RTL_VER_08:
5931 case RTL_VER_09:
Hayes Wang195aae32021-04-16 16:04:35 +08005932 case RTL_VER_14:
hayeswang65b82d62017-06-15 14:44:03 +08005933 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
5934 * type. Set it to zero. bits[7:0] are the W1C bits about
5935 * the events. Set them to all 1 to clear them.
5936 */
5937 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
5938 break;
5939
5940 default:
5941 break;
5942 }
hayeswang43779f82014-01-02 11:25:10 +08005943
hayeswangc5554292014-09-12 10:43:11 +08005944 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08005945
Kevin Lo59c0b472019-08-01 11:29:38 +08005946 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08005947 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08005948 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08005949
5950 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5951 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5952 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5953
hayeswang00a5e362014-02-18 21:48:59 +08005954 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005955
5956 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5957 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5958 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5959}
5960
hayeswangd70b1132014-09-19 15:17:18 +08005961static void rtl8153_disable(struct r8152 *tp)
5962{
hayeswangcda9fb02016-01-07 17:51:12 +08005963 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08005964 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08005965 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08005966 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08005967}
5968
Hayes Wang195aae32021-04-16 16:04:35 +08005969static int rtl8156_enable(struct r8152 *tp)
5970{
5971 u32 ocp_data;
5972 u16 speed;
5973
5974 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5975 return -ENODEV;
5976
5977 set_tx_qlen(tp);
5978 rtl_set_eee_plus(tp);
5979 r8153_set_rx_early_timeout(tp);
5980 r8153_set_rx_early_size(tp);
5981
5982 speed = rtl8152_get_speed(tp);
5983 rtl_set_ifg(tp, speed);
5984
5985 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
5986 if (speed & _2500bps)
5987 ocp_data &= ~IDLE_SPDWN_EN;
5988 else
5989 ocp_data |= IDLE_SPDWN_EN;
5990 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
5991
5992 if (speed & _1000bps)
5993 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
5994 else if (speed & _500bps)
5995 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
5996
5997 if (tp->udev->speed == USB_SPEED_HIGH) {
5998 /* USB 0xb45e[3:0] l1_nyet_hird */
5999 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6000 ocp_data &= ~0xf;
6001 if (is_flow_control(speed))
6002 ocp_data |= 0xf;
6003 else
6004 ocp_data |= 0x1;
6005 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6006 }
6007
6008 return rtl_enable(tp);
6009}
6010
6011static int rtl8156b_enable(struct r8152 *tp)
6012{
6013 u32 ocp_data;
6014 u16 speed;
6015
6016 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6017 return -ENODEV;
6018
6019 set_tx_qlen(tp);
6020 rtl_set_eee_plus(tp);
6021
6022 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM);
6023 ocp_data &= ~RX_AGGR_NUM_MASK;
6024 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data);
6025
6026 r8153_set_rx_early_timeout(tp);
6027 r8153_set_rx_early_size(tp);
6028
6029 speed = rtl8152_get_speed(tp);
6030 rtl_set_ifg(tp, speed);
6031
6032 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
6033 if (speed & _2500bps)
6034 ocp_data &= ~IDLE_SPDWN_EN;
6035 else
6036 ocp_data |= IDLE_SPDWN_EN;
6037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
6038
6039 if (tp->udev->speed == USB_SPEED_HIGH) {
6040 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6041 ocp_data &= ~0xf;
6042 if (is_flow_control(speed))
6043 ocp_data |= 0xf;
6044 else
6045 ocp_data |= 0x1;
6046 ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6047 }
6048
6049 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
6050 ocp_data &= ~FC_PATCH_TASK;
6051 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6052 usleep_range(1000, 2000);
6053 ocp_data |= FC_PATCH_TASK;
6054 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6055
6056 return rtl_enable(tp);
6057}
6058
Hayes Wang771efed2019-09-02 19:52:28 +08006059static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
6060 u32 advertising)
hayeswangac718b62013-05-02 16:01:25 +00006061{
Hayes Wang771efed2019-09-02 19:52:28 +08006062 u16 bmcr;
hayeswangac718b62013-05-02 16:01:25 +00006063 int ret = 0;
6064
hayeswangac718b62013-05-02 16:01:25 +00006065 if (autoneg == AUTONEG_DISABLE) {
Hayes Wang771efed2019-09-02 19:52:28 +08006066 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
6067 return -EINVAL;
6068
6069 switch (speed) {
6070 case SPEED_10:
6071 bmcr = BMCR_SPEED10;
6072 if (duplex == DUPLEX_FULL) {
6073 bmcr |= BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006074 tp->ups_info.speed_duplex = FORCE_10M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08006075 } else {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006076 tp->ups_info.speed_duplex = FORCE_10M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08006077 }
6078 break;
6079 case SPEED_100:
hayeswangac718b62013-05-02 16:01:25 +00006080 bmcr = BMCR_SPEED100;
Hayes Wang771efed2019-09-02 19:52:28 +08006081 if (duplex == DUPLEX_FULL) {
6082 bmcr |= BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006083 tp->ups_info.speed_duplex = FORCE_100M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08006084 } else {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006085 tp->ups_info.speed_duplex = FORCE_100M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08006086 }
6087 break;
6088 case SPEED_1000:
6089 if (tp->mii.supports_gmii) {
6090 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006091 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08006092 break;
6093 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006094 fallthrough;
Hayes Wang771efed2019-09-02 19:52:28 +08006095 default:
hayeswangac718b62013-05-02 16:01:25 +00006096 ret = -EINVAL;
6097 goto out;
6098 }
6099
Hayes Wang771efed2019-09-02 19:52:28 +08006100 if (duplex == DUPLEX_FULL)
6101 tp->mii.full_duplex = 1;
6102 else
6103 tp->mii.full_duplex = 0;
6104
6105 tp->mii.force_media = 1;
hayeswangac718b62013-05-02 16:01:25 +00006106 } else {
Hayes Wang195aae32021-04-16 16:04:35 +08006107 u16 orig, new1;
Hayes Wang771efed2019-09-02 19:52:28 +08006108 u32 support;
6109
6110 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6111 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6112
Hayes Wang195aae32021-04-16 16:04:35 +08006113 if (tp->mii.supports_gmii) {
Hayes Wang771efed2019-09-02 19:52:28 +08006114 support |= RTL_ADVERTISED_1000_FULL;
6115
Hayes Wang195aae32021-04-16 16:04:35 +08006116 if (tp->support_2500full)
6117 support |= RTL_ADVERTISED_2500_FULL;
6118 }
6119
Hayes Wang771efed2019-09-02 19:52:28 +08006120 if (!(advertising & support))
6121 return -EINVAL;
6122
Hayes Wang195aae32021-04-16 16:04:35 +08006123 orig = r8152_mdio_read(tp, MII_ADVERTISE);
6124 new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
Hayes Wang771efed2019-09-02 19:52:28 +08006125 ADVERTISE_100HALF | ADVERTISE_100FULL);
6126 if (advertising & RTL_ADVERTISED_10_HALF) {
Hayes Wang195aae32021-04-16 16:04:35 +08006127 new1 |= ADVERTISE_10HALF;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006128 tp->ups_info.speed_duplex = NWAY_10M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08006129 }
6130 if (advertising & RTL_ADVERTISED_10_FULL) {
Hayes Wang195aae32021-04-16 16:04:35 +08006131 new1 |= ADVERTISE_10FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006132 tp->ups_info.speed_duplex = NWAY_10M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08006133 }
6134
6135 if (advertising & RTL_ADVERTISED_100_HALF) {
Hayes Wang195aae32021-04-16 16:04:35 +08006136 new1 |= ADVERTISE_100HALF;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006137 tp->ups_info.speed_duplex = NWAY_100M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08006138 }
6139 if (advertising & RTL_ADVERTISED_100_FULL) {
Hayes Wang195aae32021-04-16 16:04:35 +08006140 new1 |= ADVERTISE_100FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006141 tp->ups_info.speed_duplex = NWAY_100M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08006142 }
6143
Hayes Wang195aae32021-04-16 16:04:35 +08006144 if (orig != new1) {
6145 r8152_mdio_write(tp, MII_ADVERTISE, new1);
6146 tp->mii.advertising = new1;
Hayes Wang771efed2019-09-02 19:52:28 +08006147 }
6148
6149 if (tp->mii.supports_gmii) {
Hayes Wang195aae32021-04-16 16:04:35 +08006150 orig = r8152_mdio_read(tp, MII_CTRL1000);
6151 new1 = orig & ~(ADVERTISE_1000FULL |
Hayes Wang771efed2019-09-02 19:52:28 +08006152 ADVERTISE_1000HALF);
6153
6154 if (advertising & RTL_ADVERTISED_1000_FULL) {
Hayes Wang195aae32021-04-16 16:04:35 +08006155 new1 |= ADVERTISE_1000FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006156 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
hayeswang65b82d62017-06-15 14:44:03 +08006157 }
Hayes Wang771efed2019-09-02 19:52:28 +08006158
Hayes Wang195aae32021-04-16 16:04:35 +08006159 if (orig != new1)
6160 r8152_mdio_write(tp, MII_CTRL1000, new1);
6161 }
6162
6163 if (tp->support_2500full) {
6164 orig = ocp_reg_read(tp, OCP_10GBT_CTRL);
6165 new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G;
6166
6167 if (advertising & RTL_ADVERTISED_2500_FULL) {
6168 new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G;
6169 tp->ups_info.speed_duplex = NWAY_2500M_FULL;
6170 }
6171
6172 if (orig != new1)
6173 ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
hayeswangac718b62013-05-02 16:01:25 +00006174 }
6175
6176 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Hayes Wang771efed2019-09-02 19:52:28 +08006177
6178 tp->mii.force_media = 0;
hayeswangac718b62013-05-02 16:01:25 +00006179 }
6180
hayeswangfae56172016-06-16 14:08:29 +08006181 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08006182 bmcr |= BMCR_RESET;
6183
hayeswangac718b62013-05-02 16:01:25 +00006184 r8152_mdio_write(tp, MII_BMCR, bmcr);
6185
hayeswangfae56172016-06-16 14:08:29 +08006186 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08006187 int i;
6188
hayeswangaa66a5f2014-02-18 21:49:04 +08006189 for (i = 0; i < 50; i++) {
6190 msleep(20);
6191 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
6192 break;
6193 }
6194 }
6195
hayeswangac718b62013-05-02 16:01:25 +00006196out:
hayeswangac718b62013-05-02 16:01:25 +00006197 return ret;
6198}
6199
hayeswangd70b1132014-09-19 15:17:18 +08006200static void rtl8152_up(struct r8152 *tp)
6201{
6202 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6203 return;
6204
hayeswangcda9fb02016-01-07 17:51:12 +08006205 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08006206 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08006207 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08006208}
6209
hayeswangac718b62013-05-02 16:01:25 +00006210static void rtl8152_down(struct r8152 *tp)
6211{
hayeswang68714382014-04-11 17:54:31 +08006212 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6213 rtl_drop_queued_tx(tp);
6214 return;
6215 }
6216
hayeswang00a5e362014-02-18 21:48:59 +08006217 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08006218 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00006219 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08006220 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00006221}
6222
hayeswangd70b1132014-09-19 15:17:18 +08006223static void rtl8153_up(struct r8152 *tp)
6224{
Hayes Wang19813162020-01-22 16:02:12 +08006225 u32 ocp_data;
6226
hayeswangd70b1132014-09-19 15:17:18 +08006227 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6228 return;
6229
hayeswangb2143962015-07-24 13:54:23 +08006230 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08006231 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08006232 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08006233 r8153_first_init(tp);
Hayes Wang19813162020-01-22 16:02:12 +08006234
6235 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6236 ocp_data |= LANWAKE_CLR_EN;
6237 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6238
6239 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
6240 ocp_data &= ~LANWAKE_PIN;
6241 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
6242
Hayes Wangaa475d92020-01-22 16:02:13 +08006243 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
6244 ocp_data &= ~DELAY_PHY_PWR_CHG;
6245 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
6246
hayeswangcda9fb02016-01-07 17:51:12 +08006247 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08006248
6249 switch (tp->version) {
6250 case RTL_VER_03:
6251 case RTL_VER_04:
6252 break;
6253 case RTL_VER_05:
6254 case RTL_VER_06:
6255 default:
6256 r8153_u2p3en(tp, true);
6257 break;
6258 }
6259
hayeswangb2143962015-07-24 13:54:23 +08006260 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08006261}
6262
hayeswang43779f82014-01-02 11:25:10 +08006263static void rtl8153_down(struct r8152 *tp)
6264{
Hayes Wang19813162020-01-22 16:02:12 +08006265 u32 ocp_data;
6266
hayeswang68714382014-04-11 17:54:31 +08006267 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6268 rtl_drop_queued_tx(tp);
6269 return;
6270 }
6271
Hayes Wang19813162020-01-22 16:02:12 +08006272 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6273 ocp_data &= ~LANWAKE_CLR_EN;
6274 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6275
hayeswangb9702722014-02-18 21:49:00 +08006276 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08006277 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08006278 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08006279 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08006280 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08006281 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08006282}
6283
hayeswang65b82d62017-06-15 14:44:03 +08006284static void rtl8153b_up(struct r8152 *tp)
6285{
Hayes Wang08997b52020-01-22 16:02:09 +08006286 u32 ocp_data;
6287
hayeswang65b82d62017-06-15 14:44:03 +08006288 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6289 return;
6290
6291 r8153b_u1u2en(tp, false);
6292 r8153_u2p3en(tp, false);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006293 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08006294
6295 r8153_first_init(tp);
6296 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6297
Hayes Wang08997b52020-01-22 16:02:09 +08006298 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6299 ocp_data &= ~PLA_MCU_SPDWN_EN;
6300 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6301
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006302 r8153_aldps_en(tp, true);
Hayes Wanga0246da2020-01-22 16:02:11 +08006303
Hayes Wang7a0ae612021-02-19 17:04:40 +08006304 if (tp->udev->speed >= USB_SPEED_SUPER)
Hayes Wanga0246da2020-01-22 16:02:11 +08006305 r8153b_u1u2en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08006306}
6307
6308static void rtl8153b_down(struct r8152 *tp)
6309{
Hayes Wang08997b52020-01-22 16:02:09 +08006310 u32 ocp_data;
6311
hayeswang65b82d62017-06-15 14:44:03 +08006312 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6313 rtl_drop_queued_tx(tp);
6314 return;
6315 }
6316
Hayes Wang08997b52020-01-22 16:02:09 +08006317 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6318 ocp_data |= PLA_MCU_SPDWN_EN;
6319 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6320
hayeswang65b82d62017-06-15 14:44:03 +08006321 r8153b_u1u2en(tp, false);
6322 r8153_u2p3en(tp, false);
6323 r8153b_power_cut_en(tp, false);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006324 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08006325 r8153_enter_oob(tp);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006326 r8153_aldps_en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08006327}
6328
Hayes Wang195aae32021-04-16 16:04:35 +08006329static void rtl8153c_change_mtu(struct r8152 *tp)
6330{
6331 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
6332 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
6333
6334 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6335
6336 /* Adjust the tx fifo free credit full threshold, otherwise
6337 * the fifo would be too small to send a jumbo frame packet.
6338 */
6339 if (tp->netdev->mtu < 8000)
6340 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8);
6341 else
6342 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8);
6343}
6344
6345static void rtl8153c_up(struct r8152 *tp)
6346{
6347 u32 ocp_data;
6348
6349 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6350 return;
6351
6352 r8153b_u1u2en(tp, false);
6353 r8153_u2p3en(tp, false);
6354 r8153_aldps_en(tp, false);
6355
6356 rxdy_gated_en(tp, true);
6357 r8153_teredo_off(tp);
6358
6359 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6360 ocp_data &= ~RCR_ACPT_ALL;
6361 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6362
6363 rtl8152_nic_reset(tp);
6364 rtl_reset_bmu(tp);
6365
6366 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6367 ocp_data &= ~NOW_IS_OOB;
6368 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6369
6370 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6371 ocp_data &= ~MCU_BORW_EN;
6372 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6373
6374 wait_oob_link_list_ready(tp);
6375
6376 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6377 ocp_data |= RE_INIT_LL;
6378 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6379
6380 wait_oob_link_list_ready(tp);
6381
6382 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6383
6384 rtl8153c_change_mtu(tp);
6385
6386 rtl8152_nic_reset(tp);
6387
6388 /* rx share fifo credit full threshold */
6389 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02);
6390 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08);
6391 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
6392 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
6393
6394 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6395
6396 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
6397
6398 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
6399 ocp_data |= BIT(8);
6400 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
6401
6402 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
6403
6404 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6405 ocp_data &= ~PLA_MCU_SPDWN_EN;
6406 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6407
6408 r8153_aldps_en(tp, true);
6409 r8153b_u1u2en(tp, true);
6410}
6411
6412static inline u32 fc_pause_on_auto(struct r8152 *tp)
6413{
6414 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024);
6415}
6416
6417static inline u32 fc_pause_off_auto(struct r8152 *tp)
6418{
6419 return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024);
6420}
6421
6422static void r8156_fc_parameter(struct r8152 *tp)
6423{
6424 u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp);
6425 u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp);
6426
6427 switch (tp->version) {
6428 case RTL_VER_10:
6429 case RTL_VER_11:
6430 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 8);
6431 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 8);
6432 break;
6433 case RTL_VER_12:
6434 case RTL_VER_13:
6435 case RTL_VER_15:
6436 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16);
6437 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16);
6438 break;
6439 default:
6440 break;
6441 }
6442}
6443
6444static void rtl8156_change_mtu(struct r8152 *tp)
6445{
6446 u32 rx_max_size = mtu_to_size(tp->netdev->mtu);
6447
6448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size);
6449 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
6450 r8156_fc_parameter(tp);
6451
6452 /* TX share fifo free credit full threshold */
6453 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6454 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
6455 ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16);
6456}
6457
6458static void rtl8156_up(struct r8152 *tp)
6459{
6460 u32 ocp_data;
6461
6462 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6463 return;
6464
6465 r8153b_u1u2en(tp, false);
6466 r8153_u2p3en(tp, false);
6467 r8153_aldps_en(tp, false);
6468
6469 rxdy_gated_en(tp, true);
6470 r8153_teredo_off(tp);
6471
6472 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6473 ocp_data &= ~RCR_ACPT_ALL;
6474 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6475
6476 rtl8152_nic_reset(tp);
6477 rtl_reset_bmu(tp);
6478
6479 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6480 ocp_data &= ~NOW_IS_OOB;
6481 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6482
6483 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6484 ocp_data &= ~MCU_BORW_EN;
6485 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6486
6487 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6488
6489 rtl8156_change_mtu(tp);
6490
6491 switch (tp->version) {
6492 case RTL_TEST_01:
6493 case RTL_VER_10:
6494 case RTL_VER_11:
6495 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
6496 ocp_data |= ACT_ODMA;
6497 ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
6498 break;
6499 default:
6500 break;
6501 }
6502
6503 /* share FIFO settings */
6504 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL);
6505 ocp_data &= ~RXFIFO_FULL_MASK;
6506 ocp_data |= 0x08;
6507 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data);
6508
6509 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6510 ocp_data &= ~PLA_MCU_SPDWN_EN;
6511 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6512
6513 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION);
6514 ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF);
6515 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data);
6516
6517 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
6518
6519 if (tp->saved_wolopts != __rtl_get_wol(tp)) {
6520 netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n");
6521 __rtl_set_wol(tp, tp->saved_wolopts);
6522 }
6523
6524 r8153_aldps_en(tp, true);
6525 r8153_u2p3en(tp, true);
6526
6527 if (tp->udev->speed >= USB_SPEED_SUPER)
6528 r8153b_u1u2en(tp, true);
6529}
6530
6531static void rtl8156_down(struct r8152 *tp)
6532{
6533 u32 ocp_data;
6534
6535 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6536 rtl_drop_queued_tx(tp);
6537 return;
6538 }
6539
6540 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6541 ocp_data |= PLA_MCU_SPDWN_EN;
6542 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6543
6544 r8153b_u1u2en(tp, false);
6545 r8153_u2p3en(tp, false);
6546 r8153b_power_cut_en(tp, false);
6547 r8153_aldps_en(tp, false);
6548
6549 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6550 ocp_data &= ~NOW_IS_OOB;
6551 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6552
6553 rtl_disable(tp);
6554 rtl_reset_bmu(tp);
6555
6556 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
6557 * type. Set it to zero. bits[7:0] are the W1C bits about
6558 * the events. Set them to all 1 to clear them.
6559 */
6560 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
6561
6562 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6563 ocp_data |= NOW_IS_OOB;
6564 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6565
6566 rtl_rx_vlan_en(tp, true);
6567 rxdy_gated_en(tp, false);
6568
6569 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6570 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
6571 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6572
6573 r8153_aldps_en(tp, true);
6574}
6575
hayeswang2dd49e02015-09-07 11:57:44 +08006576static bool rtl8152_in_nway(struct r8152 *tp)
6577{
6578 u16 nway_state;
6579
6580 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
6581 tp->ocp_base = 0x2000;
6582 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
6583 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
6584
6585 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
6586 if (nway_state & 0xc000)
6587 return false;
6588 else
6589 return true;
6590}
6591
6592static bool rtl8153_in_nway(struct r8152 *tp)
6593{
6594 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
6595
6596 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
6597 return false;
6598 else
6599 return true;
6600}
6601
Hayes Wang45bf9442021-12-23 17:27:01 +08006602static void r8156_mdio_force_mode(struct r8152 *tp)
6603{
6604 u16 data;
6605
6606 /* Select force mode through 0xa5b4 bit 15
6607 * 0: MDIO force mode
6608 * 1: MMD force mode
6609 */
6610 data = ocp_reg_read(tp, 0xa5b4);
6611 if (data & BIT(15)) {
6612 data &= ~BIT(15);
6613 ocp_reg_write(tp, 0xa5b4, data);
6614 }
6615}
6616
hayeswangac718b62013-05-02 16:01:25 +00006617static void set_carrier(struct r8152 *tp)
6618{
6619 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08006620 struct napi_struct *napi = &tp->napi;
Hayes Wang195aae32021-04-16 16:04:35 +08006621 u16 speed;
hayeswangac718b62013-05-02 16:01:25 +00006622
6623 speed = rtl8152_get_speed(tp);
6624
6625 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08006626 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08006627 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08006628 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08006629 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00006630 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08006631 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08006632 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6633 _rtl8152_set_rx_mode(netdev);
Hayes Wang195aae32021-04-16 16:04:35 +08006634 napi_enable(napi);
hayeswangde9bf292017-01-26 09:38:32 +08006635 netif_wake_queue(netdev);
6636 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08006637 } else if (netif_queue_stopped(netdev) &&
6638 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
6639 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00006640 }
6641 } else {
hayeswang51d979f2015-02-06 11:30:47 +08006642 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00006643 netif_carrier_off(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08006644 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08006645 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08006646 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08006647 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08006648 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08006649 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00006650 }
6651 }
hayeswangac718b62013-05-02 16:01:25 +00006652}
6653
6654static void rtl_work_func_t(struct work_struct *work)
6655{
6656 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
6657
hayeswanga1f83fe2014-11-12 10:05:05 +08006658 /* If the device is unplugged or !netif_running(), the workqueue
6659 * doesn't need to wake the device, and could return directly.
6660 */
6661 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
6662 return;
6663
hayeswang9a4be1b2014-02-18 21:49:07 +08006664 if (usb_autopm_get_interface(tp->intf) < 0)
6665 return;
6666
hayeswangac718b62013-05-02 16:01:25 +00006667 if (!test_bit(WORK_ENABLE, &tp->flags))
6668 goto out1;
6669
hayeswangb5403272014-10-09 18:00:26 +08006670 if (!mutex_trylock(&tp->control)) {
6671 schedule_delayed_work(&tp->schedule, 0);
6672 goto out1;
6673 }
6674
hayeswang216a8342016-01-07 17:51:11 +08006675 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08006676 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00006677
hayeswang216a8342016-01-07 17:51:11 +08006678 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00006679 _rtl8152_set_rx_mode(tp->netdev);
6680
Hayes Wangd2187f82019-08-19 14:40:36 +08006681 /* don't schedule tasket before linking */
6682 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
hayeswang216a8342016-01-07 17:51:11 +08006683 netif_carrier_ok(tp->netdev))
Hayes Wangd2187f82019-08-19 14:40:36 +08006684 tasklet_schedule(&tp->tx_tl);
hayeswangaa66a5f2014-02-18 21:49:04 +08006685
Hayes Wangbaf33d72021-10-04 14:28:58 +08006686 if (test_and_clear_bit(RX_EPROTO, &tp->flags) &&
6687 !list_empty(&tp->rx_done))
6688 napi_schedule(&tp->napi);
6689
hayeswangb5403272014-10-09 18:00:26 +08006690 mutex_unlock(&tp->control);
6691
hayeswangac718b62013-05-02 16:01:25 +00006692out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08006693 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00006694}
6695
hayeswanga028a9e2016-06-13 17:49:36 +08006696static void rtl_hw_phy_work_func_t(struct work_struct *work)
6697{
6698 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
6699
6700 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6701 return;
6702
6703 if (usb_autopm_get_interface(tp->intf) < 0)
6704 return;
6705
6706 mutex_lock(&tp->control);
6707
Hayes Wang9370f2d2019-10-16 11:02:42 +08006708 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
6709 tp->rtl_fw.retry = false;
6710 tp->rtl_fw.fw = NULL;
6711
6712 /* Delay execution in case request_firmware() is not ready yet.
6713 */
6714 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
6715 goto ignore_once;
6716 }
6717
hayeswanga028a9e2016-06-13 17:49:36 +08006718 tp->rtl_ops.hw_phy_cfg(tp);
6719
Hayes Wang771efed2019-09-02 19:52:28 +08006720 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
6721 tp->advertising);
hayeswang9d21c0d2016-06-13 17:49:37 +08006722
Hayes Wang9370f2d2019-10-16 11:02:42 +08006723ignore_once:
hayeswanga028a9e2016-06-13 17:49:36 +08006724 mutex_unlock(&tp->control);
6725
6726 usb_autopm_put_interface(tp->intf);
6727}
6728
hayeswang5ee3c602016-01-07 17:12:17 +08006729#ifdef CONFIG_PM_SLEEP
6730static int rtl_notifier(struct notifier_block *nb, unsigned long action,
6731 void *data)
6732{
6733 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
6734
6735 switch (action) {
6736 case PM_HIBERNATION_PREPARE:
6737 case PM_SUSPEND_PREPARE:
6738 usb_autopm_get_interface(tp->intf);
6739 break;
6740
6741 case PM_POST_HIBERNATION:
6742 case PM_POST_SUSPEND:
6743 usb_autopm_put_interface(tp->intf);
6744 break;
6745
6746 case PM_POST_RESTORE:
6747 case PM_RESTORE_PREPARE:
6748 default:
6749 break;
6750 }
6751
6752 return NOTIFY_DONE;
6753}
6754#endif
6755
hayeswangac718b62013-05-02 16:01:25 +00006756static int rtl8152_open(struct net_device *netdev)
6757{
6758 struct r8152 *tp = netdev_priv(netdev);
6759 int res = 0;
6760
Hayes Wang9370f2d2019-10-16 11:02:42 +08006761 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
6762 cancel_delayed_work_sync(&tp->hw_phy_work);
6763 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
6764 }
6765
hayeswang7e9da482014-02-18 21:49:05 +08006766 res = alloc_all_mem(tp);
6767 if (res)
6768 goto out;
6769
hayeswang9a4be1b2014-02-18 21:49:07 +08006770 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08006771 if (res < 0)
6772 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08006773
hayeswangb5403272014-10-09 18:00:26 +08006774 mutex_lock(&tp->control);
6775
hayeswang7e9da482014-02-18 21:49:05 +08006776 tp->rtl_ops.up(tp);
6777
hayeswang40a82912013-08-14 20:54:40 +08006778 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00006779 netif_start_queue(netdev);
6780 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08006781
hayeswang3d55f442014-02-06 11:55:48 +08006782 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
6783 if (res) {
6784 if (res == -ENODEV)
6785 netif_device_detach(tp->netdev);
6786 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
6787 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08006788 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08006789 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08006790 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08006791 tasklet_enable(&tp->tx_tl);
hayeswang3d55f442014-02-06 11:55:48 +08006792
hayeswangb5403272014-10-09 18:00:26 +08006793 mutex_unlock(&tp->control);
6794
hayeswang9a4be1b2014-02-18 21:49:07 +08006795 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08006796#ifdef CONFIG_PM_SLEEP
6797 tp->pm_notifier.notifier_call = rtl_notifier;
6798 register_pm_notifier(&tp->pm_notifier);
6799#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08006800 return 0;
hayeswangac718b62013-05-02 16:01:25 +00006801
Guenter Roeckca0a7532016-11-09 19:51:25 -08006802out_unlock:
6803 mutex_unlock(&tp->control);
6804 usb_autopm_put_interface(tp->intf);
6805out_free:
6806 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08006807out:
hayeswangac718b62013-05-02 16:01:25 +00006808 return res;
6809}
6810
6811static int rtl8152_close(struct net_device *netdev)
6812{
6813 struct r8152 *tp = netdev_priv(netdev);
6814 int res = 0;
6815
hayeswang5ee3c602016-01-07 17:12:17 +08006816#ifdef CONFIG_PM_SLEEP
6817 unregister_pm_notifier(&tp->pm_notifier);
6818#endif
Hayes Wangd2187f82019-08-19 14:40:36 +08006819 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00006820 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08006821 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00006822 cancel_delayed_work_sync(&tp->schedule);
Prashant Malani84811412019-11-20 11:40:21 -08006823 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00006824 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08006825
6826 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08006827 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08006828 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08006829 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08006830 } else {
hayeswangb5403272014-10-09 18:00:26 +08006831 mutex_lock(&tp->control);
6832
hayeswang9a4be1b2014-02-18 21:49:07 +08006833 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08006834
6835 mutex_unlock(&tp->control);
hayeswang9a4be1b2014-02-18 21:49:07 +08006836 }
hayeswangac718b62013-05-02 16:01:25 +00006837
Takashi Iwai9c23aa52021-07-14 19:00:21 +02006838 if (!res)
6839 usb_autopm_put_interface(tp->intf);
6840
hayeswang7e9da482014-02-18 21:49:05 +08006841 free_all_mem(tp);
6842
hayeswangac718b62013-05-02 16:01:25 +00006843 return res;
6844}
6845
hayeswang4f1d4d52014-03-11 16:24:19 +08006846static void rtl_tally_reset(struct r8152 *tp)
6847{
6848 u32 ocp_data;
6849
6850 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
6851 ocp_data |= TALLY_RESET;
6852 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
6853}
6854
hayeswangac718b62013-05-02 16:01:25 +00006855static void r8152b_init(struct r8152 *tp)
6856{
hayeswangebc2ec482013-08-14 20:54:38 +08006857 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08006858 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00006859
hayeswang68714382014-04-11 17:54:31 +08006860 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6861 return;
6862
hayeswang2dd436d2016-09-20 16:22:06 +08006863 data = r8152_mdio_read(tp, MII_BMCR);
6864 if (data & BMCR_PDOWN) {
6865 data &= ~BMCR_PDOWN;
6866 r8152_mdio_write(tp, MII_BMCR, data);
6867 }
6868
hayeswangcda9fb02016-01-07 17:51:12 +08006869 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08006870
hayeswangac718b62013-05-02 16:01:25 +00006871 if (tp->version == RTL_VER_01) {
6872 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
6873 ocp_data &= ~LED_MODE_MASK;
6874 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
6875 }
6876
hayeswang00a5e362014-02-18 21:48:59 +08006877 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00006878
hayeswangac718b62013-05-02 16:01:25 +00006879 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
6880 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
6881 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
6882 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
6883 ocp_data &= ~MCU_CLK_RATIO_MASK;
6884 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
6885 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
6886 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
6887 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
6888 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
6889
hayeswang4f1d4d52014-03-11 16:24:19 +08006890 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00006891
hayeswangebc2ec482013-08-14 20:54:38 +08006892 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00006893 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08006894 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00006895 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
6896}
6897
hayeswang43779f82014-01-02 11:25:10 +08006898static void r8153_init(struct r8152 *tp)
6899{
6900 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08006901 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08006902 int i;
6903
hayeswang68714382014-04-11 17:54:31 +08006904 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6905 return;
6906
hayeswangb9702722014-02-18 21:49:00 +08006907 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08006908
6909 for (i = 0; i < 500; i++) {
6910 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
6911 AUTOLOAD_DONE)
6912 break;
You-Sheng Yangd64c7a02020-02-26 23:37:10 +08006913
hayeswang43779f82014-01-02 11:25:10 +08006914 msleep(20);
You-Sheng Yangd64c7a02020-02-26 23:37:10 +08006915 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6916 break;
hayeswang43779f82014-01-02 11:25:10 +08006917 }
6918
hayeswangc564b872017-06-09 17:11:38 +08006919 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08006920
hayeswang2dd436d2016-09-20 16:22:06 +08006921 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
6922 tp->version == RTL_VER_05)
6923 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
6924
6925 data = r8152_mdio_read(tp, MII_BMCR);
6926 if (data & BMCR_PDOWN) {
6927 data &= ~BMCR_PDOWN;
6928 r8152_mdio_write(tp, MII_BMCR, data);
6929 }
6930
hayeswangc564b872017-06-09 17:11:38 +08006931 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08006932
hayeswangb9702722014-02-18 21:49:00 +08006933 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08006934
hayeswang65bab842015-02-12 16:20:46 +08006935 if (tp->version == RTL_VER_04) {
6936 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
6937 ocp_data &= ~pwd_dn_scale_mask;
6938 ocp_data |= pwd_dn_scale(96);
6939 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
6940
6941 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
6942 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
6943 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
6944 } else if (tp->version == RTL_VER_05) {
6945 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
6946 ocp_data &= ~ECM_ALDPS;
6947 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
6948
6949 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
6950 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
6951 ocp_data &= ~DYNAMIC_BURST;
6952 else
6953 ocp_data |= DYNAMIC_BURST;
6954 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08006955 } else if (tp->version == RTL_VER_06) {
6956 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
6957 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
6958 ocp_data &= ~DYNAMIC_BURST;
6959 else
6960 ocp_data |= DYNAMIC_BURST;
6961 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
Hayes Wanga3914272020-01-22 16:02:05 +08006962
6963 r8153_queue_wake(tp, false);
6964
6965 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
6966 if (rtl8152_get_speed(tp) & LINK_STATUS)
6967 ocp_data |= CUR_LINK_OK;
6968 else
6969 ocp_data &= ~CUR_LINK_OK;
6970 ocp_data |= POLL_LINK_CHG;
6971 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08006972 }
6973
6974 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
6975 ocp_data |= EP4_FULL_FC;
6976 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
6977
hayeswang43779f82014-01-02 11:25:10 +08006978 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
6979 ocp_data &= ~TIMER11_EN;
6980 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
6981
hayeswang43779f82014-01-02 11:25:10 +08006982 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
6983 ocp_data &= ~LED_MODE_MASK;
6984 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
6985
hayeswang65bab842015-02-12 16:20:46 +08006986 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02006987 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08006988 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08006989 else
6990 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08006991 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
6992
6993 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
6994 ocp_data &= ~SEN_VAL_MASK;
6995 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
6996 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
6997
hayeswang65bab842015-02-12 16:20:46 +08006998 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
6999
hayeswangb9702722014-02-18 21:49:00 +08007000 r8153_power_cut_en(tp, false);
Hayes Wanga3914272020-01-22 16:02:05 +08007001 rtl_runtime_suspend_enable(tp, false);
Hayes Wang195aae32021-04-16 16:04:35 +08007002 r8153_mac_clk_speed_down(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08007003 r8153_u1u2en(tp, true);
hayeswangee4761c2017-06-09 17:11:39 +08007004 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08007005
Hayes Wang19813162020-01-22 16:02:12 +08007006 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
7007 ocp_data |= LANWAKE_CLR_EN;
7008 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
7009
7010 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
7011 ocp_data &= ~LANWAKE_PIN;
7012 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
7013
hayeswange31f6362017-06-09 17:11:41 +08007014 /* rx aggregation */
7015 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7016 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Hayes Wang9c680112021-04-24 14:09:03 +08007017 if (tp->dell_tb_rx_agg_bug)
Kai-Heng Feng0b165512018-01-16 16:46:27 +08007018 ocp_data |= RX_AGG_DISABLE;
7019
hayeswange31f6362017-06-09 17:11:41 +08007020 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08007021
hayeswang4f1d4d52014-03-11 16:24:19 +08007022 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08007023
7024 switch (tp->udev->speed) {
7025 case USB_SPEED_SUPER:
7026 case USB_SPEED_SUPER_PLUS:
7027 tp->coalesce = COALESCE_SUPER;
7028 break;
7029 case USB_SPEED_HIGH:
7030 tp->coalesce = COALESCE_HIGH;
7031 break;
7032 default:
7033 tp->coalesce = COALESCE_SLOW;
7034 break;
7035 }
hayeswang43779f82014-01-02 11:25:10 +08007036}
7037
hayeswang65b82d62017-06-15 14:44:03 +08007038static void r8153b_init(struct r8152 *tp)
7039{
7040 u32 ocp_data;
7041 u16 data;
7042 int i;
7043
7044 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7045 return;
7046
7047 r8153b_u1u2en(tp, false);
7048
7049 for (i = 0; i < 500; i++) {
7050 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7051 AUTOLOAD_DONE)
7052 break;
You-Sheng Yangd64c7a02020-02-26 23:37:10 +08007053
hayeswang65b82d62017-06-15 14:44:03 +08007054 msleep(20);
You-Sheng Yangd64c7a02020-02-26 23:37:10 +08007055 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7056 break;
hayeswang65b82d62017-06-15 14:44:03 +08007057 }
7058
7059 data = r8153_phy_status(tp, 0);
7060
7061 data = r8152_mdio_read(tp, MII_BMCR);
7062 if (data & BMCR_PDOWN) {
7063 data &= ~BMCR_PDOWN;
7064 r8152_mdio_write(tp, MII_BMCR, data);
7065 }
7066
7067 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7068
7069 r8153_u2p3en(tp, false);
7070
7071 /* MSC timer = 0xfff * 8ms = 32760 ms */
7072 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7073
hayeswang65b82d62017-06-15 14:44:03 +08007074 r8153b_power_cut_en(tp, false);
7075 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08007076 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08007077 rtl_runtime_suspend_enable(tp, false);
Hayes Wanga3914272020-01-22 16:02:05 +08007078
7079 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7080 if (rtl8152_get_speed(tp) & LINK_STATUS)
7081 ocp_data |= CUR_LINK_OK;
7082 else
7083 ocp_data &= ~CUR_LINK_OK;
7084 ocp_data |= POLL_LINK_CHG;
7085 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
Hayes Wanga0246da2020-01-22 16:02:11 +08007086
Hayes Wang7a0ae612021-02-19 17:04:40 +08007087 if (tp->udev->speed >= USB_SPEED_SUPER)
Hayes Wanga0246da2020-01-22 16:02:11 +08007088 r8153b_u1u2en(tp, true);
Hayes Wang7a0ae612021-02-19 17:04:40 +08007089
hayeswang65b82d62017-06-15 14:44:03 +08007090 usb_enable_lpm(tp->udev);
7091
7092 /* MAC clock speed down */
Hayes Wang195aae32021-04-16 16:04:35 +08007093 r8153_mac_clk_speed_down(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08007094
Hayes Wang08997b52020-01-22 16:02:09 +08007095 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
7096 ocp_data &= ~PLA_MCU_SPDWN_EN;
7097 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
7098
Hayes Wangd7f1b592020-01-22 16:02:10 +08007099 if (tp->version == RTL_VER_09) {
7100 /* Disable Test IO for 32QFN */
7101 if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
7102 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7103 ocp_data |= TEST_IO_OFF;
7104 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7105 }
7106 }
7107
hayeswang65b82d62017-06-15 14:44:03 +08007108 set_bit(GREEN_ETHERNET, &tp->flags);
7109
7110 /* rx aggregation */
7111 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7112 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7113 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7114
7115 rtl_tally_reset(tp);
7116
7117 tp->coalesce = 15000; /* 15 us */
7118}
7119
Hayes Wang195aae32021-04-16 16:04:35 +08007120static void r8153c_init(struct r8152 *tp)
7121{
7122 u32 ocp_data;
7123 u16 data;
7124 int i;
7125
7126 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7127 return;
7128
7129 r8153b_u1u2en(tp, false);
7130
7131 /* Disable spi_en */
7132 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
7133 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
7134 ocp_data &= ~BIT(3);
7135 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
7136 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0);
7137 ocp_data |= BIT(1);
7138 ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data);
7139
7140 for (i = 0; i < 500; i++) {
7141 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7142 AUTOLOAD_DONE)
7143 break;
7144
7145 msleep(20);
7146 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7147 return;
7148 }
7149
7150 data = r8153_phy_status(tp, 0);
7151
7152 data = r8152_mdio_read(tp, MII_BMCR);
7153 if (data & BMCR_PDOWN) {
7154 data &= ~BMCR_PDOWN;
7155 r8152_mdio_write(tp, MII_BMCR, data);
7156 }
7157
7158 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7159
7160 r8153_u2p3en(tp, false);
7161
7162 /* MSC timer = 0xfff * 8ms = 32760 ms */
7163 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7164
7165 r8153b_power_cut_en(tp, false);
7166 r8153c_ups_en(tp, false);
7167 r8153_queue_wake(tp, false);
7168 rtl_runtime_suspend_enable(tp, false);
7169
7170 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7171 if (rtl8152_get_speed(tp) & LINK_STATUS)
7172 ocp_data |= CUR_LINK_OK;
7173 else
7174 ocp_data &= ~CUR_LINK_OK;
7175
7176 ocp_data |= POLL_LINK_CHG;
7177 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7178
7179 r8153b_u1u2en(tp, true);
7180
7181 usb_enable_lpm(tp->udev);
7182
7183 /* MAC clock speed down */
7184 r8153_mac_clk_speed_down(tp, true);
7185
7186 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
7187 ocp_data &= ~BIT(7);
7188 ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
7189
7190 set_bit(GREEN_ETHERNET, &tp->flags);
7191
7192 /* rx aggregation */
7193 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7194 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7195 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7196
7197 rtl_tally_reset(tp);
7198
7199 tp->coalesce = 15000; /* 15 us */
7200}
7201
7202static void r8156_hw_phy_cfg(struct r8152 *tp)
7203{
7204 u32 ocp_data;
7205 u16 data;
7206
7207 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7208 if (ocp_data & PCUT_STATUS) {
7209 ocp_data &= ~PCUT_STATUS;
7210 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7211 }
7212
7213 data = r8153_phy_status(tp, 0);
7214 switch (data) {
7215 case PHY_STAT_EXT_INIT:
7216 rtl8152_apply_firmware(tp, true);
7217
7218 data = ocp_reg_read(tp, 0xa468);
7219 data &= ~(BIT(3) | BIT(1));
7220 ocp_reg_write(tp, 0xa468, data);
7221 break;
7222 case PHY_STAT_LAN_ON:
7223 case PHY_STAT_PWRDN:
7224 default:
7225 rtl8152_apply_firmware(tp, false);
7226 break;
7227 }
7228
7229 /* disable ALDPS before updating the PHY parameters */
7230 r8153_aldps_en(tp, false);
7231
7232 /* disable EEE before updating the PHY parameters */
7233 rtl_eee_enable(tp, false);
7234
7235 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7236 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7237
7238 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7239 ocp_data |= PFM_PWM_SWITCH;
7240 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7241
7242 switch (tp->version) {
7243 case RTL_VER_10:
7244 data = ocp_reg_read(tp, 0xad40);
7245 data &= ~0x3ff;
7246 data |= BIT(7) | BIT(2);
7247 ocp_reg_write(tp, 0xad40, data);
7248
7249 data = ocp_reg_read(tp, 0xad4e);
7250 data |= BIT(4);
7251 ocp_reg_write(tp, 0xad4e, data);
7252 data = ocp_reg_read(tp, 0xad16);
7253 data &= ~0x3ff;
7254 data |= 0x6;
7255 ocp_reg_write(tp, 0xad16, data);
7256 data = ocp_reg_read(tp, 0xad32);
7257 data &= ~0x3f;
7258 data |= 6;
7259 ocp_reg_write(tp, 0xad32, data);
7260 data = ocp_reg_read(tp, 0xac08);
7261 data &= ~(BIT(12) | BIT(8));
7262 ocp_reg_write(tp, 0xac08, data);
7263 data = ocp_reg_read(tp, 0xac8a);
7264 data |= BIT(12) | BIT(13) | BIT(14);
7265 data &= ~BIT(15);
7266 ocp_reg_write(tp, 0xac8a, data);
7267 data = ocp_reg_read(tp, 0xad18);
7268 data |= BIT(10);
7269 ocp_reg_write(tp, 0xad18, data);
7270 data = ocp_reg_read(tp, 0xad1a);
7271 data |= 0x3ff;
7272 ocp_reg_write(tp, 0xad1a, data);
7273 data = ocp_reg_read(tp, 0xad1c);
7274 data |= 0x3ff;
7275 ocp_reg_write(tp, 0xad1c, data);
7276
7277 data = sram_read(tp, 0x80ea);
7278 data &= ~0xff00;
7279 data |= 0xc400;
7280 sram_write(tp, 0x80ea, data);
7281 data = sram_read(tp, 0x80eb);
7282 data &= ~0x0700;
7283 data |= 0x0300;
7284 sram_write(tp, 0x80eb, data);
7285 data = sram_read(tp, 0x80f8);
7286 data &= ~0xff00;
7287 data |= 0x1c00;
7288 sram_write(tp, 0x80f8, data);
7289 data = sram_read(tp, 0x80f1);
7290 data &= ~0xff00;
7291 data |= 0x3000;
7292 sram_write(tp, 0x80f1, data);
7293
7294 data = sram_read(tp, 0x80fe);
7295 data &= ~0xff00;
7296 data |= 0xa500;
7297 sram_write(tp, 0x80fe, data);
7298 data = sram_read(tp, 0x8102);
7299 data &= ~0xff00;
7300 data |= 0x5000;
7301 sram_write(tp, 0x8102, data);
7302 data = sram_read(tp, 0x8015);
7303 data &= ~0xff00;
7304 data |= 0x3300;
7305 sram_write(tp, 0x8015, data);
7306 data = sram_read(tp, 0x8100);
7307 data &= ~0xff00;
7308 data |= 0x7000;
7309 sram_write(tp, 0x8100, data);
7310 data = sram_read(tp, 0x8014);
7311 data &= ~0xff00;
7312 data |= 0xf000;
7313 sram_write(tp, 0x8014, data);
7314 data = sram_read(tp, 0x8016);
7315 data &= ~0xff00;
7316 data |= 0x6500;
7317 sram_write(tp, 0x8016, data);
7318 data = sram_read(tp, 0x80dc);
7319 data &= ~0xff00;
7320 data |= 0xed00;
7321 sram_write(tp, 0x80dc, data);
7322 data = sram_read(tp, 0x80df);
7323 data |= BIT(8);
7324 sram_write(tp, 0x80df, data);
7325 data = sram_read(tp, 0x80e1);
7326 data &= ~BIT(8);
7327 sram_write(tp, 0x80e1, data);
7328
7329 data = ocp_reg_read(tp, 0xbf06);
7330 data &= ~0x003f;
7331 data |= 0x0038;
7332 ocp_reg_write(tp, 0xbf06, data);
7333
7334 sram_write(tp, 0x819f, 0xddb6);
7335
7336 ocp_reg_write(tp, 0xbc34, 0x5555);
7337 data = ocp_reg_read(tp, 0xbf0a);
7338 data &= ~0x0e00;
7339 data |= 0x0a00;
7340 ocp_reg_write(tp, 0xbf0a, data);
7341
7342 data = ocp_reg_read(tp, 0xbd2c);
7343 data &= ~BIT(13);
7344 ocp_reg_write(tp, 0xbd2c, data);
7345 break;
7346 case RTL_VER_11:
7347 data = ocp_reg_read(tp, 0xad16);
7348 data |= 0x3ff;
7349 ocp_reg_write(tp, 0xad16, data);
7350 data = ocp_reg_read(tp, 0xad32);
7351 data &= ~0x3f;
7352 data |= 6;
7353 ocp_reg_write(tp, 0xad32, data);
7354 data = ocp_reg_read(tp, 0xac08);
7355 data &= ~(BIT(12) | BIT(8));
7356 ocp_reg_write(tp, 0xac08, data);
7357 data = ocp_reg_read(tp, 0xacc0);
7358 data &= ~0x3;
7359 data |= BIT(1);
7360 ocp_reg_write(tp, 0xacc0, data);
7361 data = ocp_reg_read(tp, 0xad40);
7362 data &= ~0xe7;
7363 data |= BIT(6) | BIT(2);
7364 ocp_reg_write(tp, 0xad40, data);
7365 data = ocp_reg_read(tp, 0xac14);
7366 data &= ~BIT(7);
7367 ocp_reg_write(tp, 0xac14, data);
7368 data = ocp_reg_read(tp, 0xac80);
7369 data &= ~(BIT(8) | BIT(9));
7370 ocp_reg_write(tp, 0xac80, data);
7371 data = ocp_reg_read(tp, 0xac5e);
7372 data &= ~0x7;
7373 data |= BIT(1);
7374 ocp_reg_write(tp, 0xac5e, data);
7375 ocp_reg_write(tp, 0xad4c, 0x00a8);
7376 ocp_reg_write(tp, 0xac5c, 0x01ff);
7377 data = ocp_reg_read(tp, 0xac8a);
7378 data &= ~0xf0;
7379 data |= BIT(4) | BIT(5);
7380 ocp_reg_write(tp, 0xac8a, data);
7381 ocp_reg_write(tp, 0xb87c, 0x8157);
7382 data = ocp_reg_read(tp, 0xb87e);
7383 data &= ~0xff00;
7384 data |= 0x0500;
7385 ocp_reg_write(tp, 0xb87e, data);
7386 ocp_reg_write(tp, 0xb87c, 0x8159);
7387 data = ocp_reg_read(tp, 0xb87e);
7388 data &= ~0xff00;
7389 data |= 0x0700;
7390 ocp_reg_write(tp, 0xb87e, data);
7391
7392 /* AAGC */
7393 ocp_reg_write(tp, 0xb87c, 0x80a2);
7394 ocp_reg_write(tp, 0xb87e, 0x0153);
7395 ocp_reg_write(tp, 0xb87c, 0x809c);
7396 ocp_reg_write(tp, 0xb87e, 0x0153);
7397
7398 /* EEE parameter */
7399 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056);
7400
7401 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7402 ocp_data |= EN_XG_LIP | EN_G_LIP;
7403 ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7404
7405 sram_write(tp, 0x8257, 0x020f); /* XG PLL */
7406 sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */
7407
7408 if (rtl_phy_patch_request(tp, true, true))
7409 return;
7410
7411 /* Advance EEE */
7412 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7413 ocp_data |= EEE_SPDWN_EN;
7414 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7415
7416 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7417 data &= ~(EN_EEE_100 | EN_EEE_1000);
7418 data |= EN_10M_CLKDIV;
7419 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7420 tp->ups_info._10m_ckdiv = true;
7421 tp->ups_info.eee_plloff_100 = false;
7422 tp->ups_info.eee_plloff_giga = false;
7423
7424 data = ocp_reg_read(tp, OCP_POWER_CFG);
7425 data &= ~EEE_CLKDIV_EN;
7426 ocp_reg_write(tp, OCP_POWER_CFG, data);
7427 tp->ups_info.eee_ckdiv = false;
7428
7429 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
7430 ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5));
7431 tp->ups_info._250m_ckdiv = false;
7432
7433 rtl_phy_patch_request(tp, false, true);
7434
7435 /* enable ADC Ibias Cal */
7436 data = ocp_reg_read(tp, 0xd068);
7437 data |= BIT(13);
7438 ocp_reg_write(tp, 0xd068, data);
7439
7440 /* enable Thermal Sensor */
7441 data = sram_read(tp, 0x81a2);
7442 data &= ~BIT(8);
7443 sram_write(tp, 0x81a2, data);
7444 data = ocp_reg_read(tp, 0xb54c);
7445 data &= ~0xff00;
7446 data |= 0xdb00;
7447 ocp_reg_write(tp, 0xb54c, data);
7448
7449 /* Nway 2.5G Lite */
7450 data = ocp_reg_read(tp, 0xa454);
7451 data &= ~BIT(0);
7452 ocp_reg_write(tp, 0xa454, data);
7453
7454 /* CS DSP solution */
7455 data = ocp_reg_read(tp, OCP_10GBT_CTRL);
7456 data |= RTL_ADV2_5G_F_R;
7457 ocp_reg_write(tp, OCP_10GBT_CTRL, data);
7458 data = ocp_reg_read(tp, 0xad4e);
7459 data &= ~BIT(4);
7460 ocp_reg_write(tp, 0xad4e, data);
7461 data = ocp_reg_read(tp, 0xa86a);
7462 data &= ~BIT(0);
7463 ocp_reg_write(tp, 0xa86a, data);
7464
7465 /* MDI SWAP */
7466 if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) &&
7467 (ocp_reg_read(tp, 0xd068) & BIT(1))) {
7468 u16 swap_a, swap_b;
7469
7470 data = ocp_reg_read(tp, 0xd068);
7471 data &= ~0x1f;
7472 data |= 0x1; /* p0 */
7473 ocp_reg_write(tp, 0xd068, data);
7474 swap_a = ocp_reg_read(tp, 0xd06a);
7475 data &= ~0x18;
7476 data |= 0x18; /* p3 */
7477 ocp_reg_write(tp, 0xd068, data);
7478 swap_b = ocp_reg_read(tp, 0xd06a);
7479 data &= ~0x18; /* p0 */
7480 ocp_reg_write(tp, 0xd068, data);
7481 ocp_reg_write(tp, 0xd06a,
7482 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7483 data |= 0x18; /* p3 */
7484 ocp_reg_write(tp, 0xd068, data);
7485 ocp_reg_write(tp, 0xd06a,
7486 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7487 data &= ~0x18;
7488 data |= 0x08; /* p1 */
7489 ocp_reg_write(tp, 0xd068, data);
7490 swap_a = ocp_reg_read(tp, 0xd06a);
7491 data &= ~0x18;
7492 data |= 0x10; /* p2 */
7493 ocp_reg_write(tp, 0xd068, data);
7494 swap_b = ocp_reg_read(tp, 0xd06a);
7495 data &= ~0x18;
7496 data |= 0x08; /* p1 */
7497 ocp_reg_write(tp, 0xd068, data);
7498 ocp_reg_write(tp, 0xd06a,
7499 (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7500 data &= ~0x18;
7501 data |= 0x10; /* p2 */
7502 ocp_reg_write(tp, 0xd068, data);
7503 ocp_reg_write(tp, 0xd06a,
7504 (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7505 swap_a = ocp_reg_read(tp, 0xbd5a);
7506 swap_b = ocp_reg_read(tp, 0xbd5c);
7507 ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) |
7508 ((swap_b & 0x1f) << 8) |
7509 ((swap_b >> 8) & 0x1f));
7510 ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) |
7511 ((swap_a & 0x1f) << 8) |
7512 ((swap_a >> 8) & 0x1f));
7513 swap_a = ocp_reg_read(tp, 0xbc18);
7514 swap_b = ocp_reg_read(tp, 0xbc1a);
7515 ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) |
7516 ((swap_b & 0x1f) << 8) |
7517 ((swap_b >> 8) & 0x1f));
7518 ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) |
7519 ((swap_a & 0x1f) << 8) |
7520 ((swap_a >> 8) & 0x1f));
7521 }
7522 break;
7523 default:
7524 break;
7525 }
7526
7527 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7528
7529 data = ocp_reg_read(tp, 0xa428);
7530 data &= ~BIT(9);
7531 ocp_reg_write(tp, 0xa428, data);
7532 data = ocp_reg_read(tp, 0xa5ea);
7533 data &= ~BIT(0);
7534 ocp_reg_write(tp, 0xa5ea, data);
7535 tp->ups_info.lite_mode = 0;
7536
7537 if (tp->eee_en)
7538 rtl_eee_enable(tp, true);
7539
7540 r8153_aldps_en(tp, true);
7541 r8152b_enable_fc(tp);
7542 r8153_u2p3en(tp, true);
7543
7544 set_bit(PHY_RESET, &tp->flags);
7545}
7546
7547static void r8156b_hw_phy_cfg(struct r8152 *tp)
7548{
7549 u32 ocp_data;
7550 u16 data;
7551
7552 switch (tp->version) {
7553 case RTL_VER_12:
7554 ocp_reg_write(tp, 0xbf86, 0x9000);
7555 data = ocp_reg_read(tp, 0xc402);
7556 data |= BIT(10);
7557 ocp_reg_write(tp, 0xc402, data);
7558 data &= ~BIT(10);
7559 ocp_reg_write(tp, 0xc402, data);
7560 ocp_reg_write(tp, 0xbd86, 0x1010);
7561 ocp_reg_write(tp, 0xbd88, 0x1010);
7562 data = ocp_reg_read(tp, 0xbd4e);
7563 data &= ~(BIT(10) | BIT(11));
7564 data |= BIT(11);
7565 ocp_reg_write(tp, 0xbd4e, data);
7566 data = ocp_reg_read(tp, 0xbf46);
7567 data &= ~0xf00;
7568 data |= 0x700;
7569 ocp_reg_write(tp, 0xbf46, data);
7570 break;
7571 case RTL_VER_13:
7572 case RTL_VER_15:
7573 r8156b_wait_loading_flash(tp);
7574 break;
7575 default:
7576 break;
7577 }
7578
7579 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7580 if (ocp_data & PCUT_STATUS) {
7581 ocp_data &= ~PCUT_STATUS;
7582 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7583 }
7584
7585 data = r8153_phy_status(tp, 0);
7586 switch (data) {
7587 case PHY_STAT_EXT_INIT:
7588 rtl8152_apply_firmware(tp, true);
7589
7590 data = ocp_reg_read(tp, 0xa466);
7591 data &= ~BIT(0);
7592 ocp_reg_write(tp, 0xa466, data);
7593
7594 data = ocp_reg_read(tp, 0xa468);
7595 data &= ~(BIT(3) | BIT(1));
7596 ocp_reg_write(tp, 0xa468, data);
7597 break;
7598 case PHY_STAT_LAN_ON:
7599 case PHY_STAT_PWRDN:
7600 default:
7601 rtl8152_apply_firmware(tp, false);
7602 break;
7603 }
7604
7605 data = r8152_mdio_read(tp, MII_BMCR);
7606 if (data & BMCR_PDOWN) {
7607 data &= ~BMCR_PDOWN;
7608 r8152_mdio_write(tp, MII_BMCR, data);
7609 }
7610
7611 /* disable ALDPS before updating the PHY parameters */
7612 r8153_aldps_en(tp, false);
7613
7614 /* disable EEE before updating the PHY parameters */
7615 rtl_eee_enable(tp, false);
7616
7617 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7618 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7619
7620 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7621 ocp_data |= PFM_PWM_SWITCH;
7622 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7623
7624 switch (tp->version) {
7625 case RTL_VER_12:
7626 data = ocp_reg_read(tp, 0xbc08);
7627 data |= BIT(3) | BIT(2);
7628 ocp_reg_write(tp, 0xbc08, data);
7629
7630 data = sram_read(tp, 0x8fff);
7631 data &= ~0xff00;
7632 data |= 0x0400;
7633 sram_write(tp, 0x8fff, data);
7634
7635 data = ocp_reg_read(tp, 0xacda);
7636 data |= 0xff00;
7637 ocp_reg_write(tp, 0xacda, data);
7638 data = ocp_reg_read(tp, 0xacde);
7639 data |= 0xf000;
7640 ocp_reg_write(tp, 0xacde, data);
7641 ocp_reg_write(tp, 0xac8c, 0x0ffc);
7642 ocp_reg_write(tp, 0xac46, 0xb7b4);
7643 ocp_reg_write(tp, 0xac50, 0x0fbc);
7644 ocp_reg_write(tp, 0xac3c, 0x9240);
7645 ocp_reg_write(tp, 0xac4e, 0x0db4);
7646 ocp_reg_write(tp, 0xacc6, 0x0707);
7647 ocp_reg_write(tp, 0xacc8, 0xa0d3);
7648 ocp_reg_write(tp, 0xad08, 0x0007);
7649
7650 ocp_reg_write(tp, 0xb87c, 0x8560);
7651 ocp_reg_write(tp, 0xb87e, 0x19cc);
7652 ocp_reg_write(tp, 0xb87c, 0x8562);
7653 ocp_reg_write(tp, 0xb87e, 0x19cc);
7654 ocp_reg_write(tp, 0xb87c, 0x8564);
7655 ocp_reg_write(tp, 0xb87e, 0x19cc);
7656 ocp_reg_write(tp, 0xb87c, 0x8566);
7657 ocp_reg_write(tp, 0xb87e, 0x147d);
7658 ocp_reg_write(tp, 0xb87c, 0x8568);
7659 ocp_reg_write(tp, 0xb87e, 0x147d);
7660 ocp_reg_write(tp, 0xb87c, 0x856a);
7661 ocp_reg_write(tp, 0xb87e, 0x147d);
7662 ocp_reg_write(tp, 0xb87c, 0x8ffe);
7663 ocp_reg_write(tp, 0xb87e, 0x0907);
7664 ocp_reg_write(tp, 0xb87c, 0x80d6);
7665 ocp_reg_write(tp, 0xb87e, 0x2801);
7666 ocp_reg_write(tp, 0xb87c, 0x80f2);
7667 ocp_reg_write(tp, 0xb87e, 0x2801);
7668 ocp_reg_write(tp, 0xb87c, 0x80f4);
7669 ocp_reg_write(tp, 0xb87e, 0x6077);
7670 ocp_reg_write(tp, 0xb506, 0x01e7);
7671
7672 ocp_reg_write(tp, 0xb87c, 0x8013);
7673 ocp_reg_write(tp, 0xb87e, 0x0700);
7674 ocp_reg_write(tp, 0xb87c, 0x8fb9);
7675 ocp_reg_write(tp, 0xb87e, 0x2801);
7676 ocp_reg_write(tp, 0xb87c, 0x8fba);
7677 ocp_reg_write(tp, 0xb87e, 0x0100);
7678 ocp_reg_write(tp, 0xb87c, 0x8fbc);
7679 ocp_reg_write(tp, 0xb87e, 0x1900);
7680 ocp_reg_write(tp, 0xb87c, 0x8fbe);
7681 ocp_reg_write(tp, 0xb87e, 0xe100);
7682 ocp_reg_write(tp, 0xb87c, 0x8fc0);
7683 ocp_reg_write(tp, 0xb87e, 0x0800);
7684 ocp_reg_write(tp, 0xb87c, 0x8fc2);
7685 ocp_reg_write(tp, 0xb87e, 0xe500);
7686 ocp_reg_write(tp, 0xb87c, 0x8fc4);
7687 ocp_reg_write(tp, 0xb87e, 0x0f00);
7688 ocp_reg_write(tp, 0xb87c, 0x8fc6);
7689 ocp_reg_write(tp, 0xb87e, 0xf100);
7690 ocp_reg_write(tp, 0xb87c, 0x8fc8);
7691 ocp_reg_write(tp, 0xb87e, 0x0400);
7692 ocp_reg_write(tp, 0xb87c, 0x8fca);
7693 ocp_reg_write(tp, 0xb87e, 0xf300);
7694 ocp_reg_write(tp, 0xb87c, 0x8fcc);
7695 ocp_reg_write(tp, 0xb87e, 0xfd00);
7696 ocp_reg_write(tp, 0xb87c, 0x8fce);
7697 ocp_reg_write(tp, 0xb87e, 0xff00);
7698 ocp_reg_write(tp, 0xb87c, 0x8fd0);
7699 ocp_reg_write(tp, 0xb87e, 0xfb00);
7700 ocp_reg_write(tp, 0xb87c, 0x8fd2);
7701 ocp_reg_write(tp, 0xb87e, 0x0100);
7702 ocp_reg_write(tp, 0xb87c, 0x8fd4);
7703 ocp_reg_write(tp, 0xb87e, 0xf400);
7704 ocp_reg_write(tp, 0xb87c, 0x8fd6);
7705 ocp_reg_write(tp, 0xb87e, 0xff00);
7706 ocp_reg_write(tp, 0xb87c, 0x8fd8);
7707 ocp_reg_write(tp, 0xb87e, 0xf600);
7708
7709 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7710 ocp_data |= EN_XG_LIP | EN_G_LIP;
7711 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7712 ocp_reg_write(tp, 0xb87c, 0x813d);
7713 ocp_reg_write(tp, 0xb87e, 0x390e);
7714 ocp_reg_write(tp, 0xb87c, 0x814f);
7715 ocp_reg_write(tp, 0xb87e, 0x790e);
7716 ocp_reg_write(tp, 0xb87c, 0x80b0);
7717 ocp_reg_write(tp, 0xb87e, 0x0f31);
7718 data = ocp_reg_read(tp, 0xbf4c);
7719 data |= BIT(1);
7720 ocp_reg_write(tp, 0xbf4c, data);
7721 data = ocp_reg_read(tp, 0xbcca);
7722 data |= BIT(9) | BIT(8);
7723 ocp_reg_write(tp, 0xbcca, data);
7724 ocp_reg_write(tp, 0xb87c, 0x8141);
7725 ocp_reg_write(tp, 0xb87e, 0x320e);
7726 ocp_reg_write(tp, 0xb87c, 0x8153);
7727 ocp_reg_write(tp, 0xb87e, 0x720e);
7728 ocp_reg_write(tp, 0xb87c, 0x8529);
7729 ocp_reg_write(tp, 0xb87e, 0x050e);
7730 data = ocp_reg_read(tp, OCP_EEE_CFG);
7731 data &= ~CTAP_SHORT_EN;
7732 ocp_reg_write(tp, OCP_EEE_CFG, data);
7733
7734 sram_write(tp, 0x816c, 0xc4a0);
7735 sram_write(tp, 0x8170, 0xc4a0);
7736 sram_write(tp, 0x8174, 0x04a0);
7737 sram_write(tp, 0x8178, 0x04a0);
7738 sram_write(tp, 0x817c, 0x0719);
7739 sram_write(tp, 0x8ff4, 0x0400);
7740 sram_write(tp, 0x8ff1, 0x0404);
7741
7742 ocp_reg_write(tp, 0xbf4a, 0x001b);
7743 ocp_reg_write(tp, 0xb87c, 0x8033);
7744 ocp_reg_write(tp, 0xb87e, 0x7c13);
7745 ocp_reg_write(tp, 0xb87c, 0x8037);
7746 ocp_reg_write(tp, 0xb87e, 0x7c13);
7747 ocp_reg_write(tp, 0xb87c, 0x803b);
7748 ocp_reg_write(tp, 0xb87e, 0xfc32);
7749 ocp_reg_write(tp, 0xb87c, 0x803f);
7750 ocp_reg_write(tp, 0xb87e, 0x7c13);
7751 ocp_reg_write(tp, 0xb87c, 0x8043);
7752 ocp_reg_write(tp, 0xb87e, 0x7c13);
7753 ocp_reg_write(tp, 0xb87c, 0x8047);
7754 ocp_reg_write(tp, 0xb87e, 0x7c13);
7755
7756 ocp_reg_write(tp, 0xb87c, 0x8145);
7757 ocp_reg_write(tp, 0xb87e, 0x370e);
7758 ocp_reg_write(tp, 0xb87c, 0x8157);
7759 ocp_reg_write(tp, 0xb87e, 0x770e);
7760 ocp_reg_write(tp, 0xb87c, 0x8169);
7761 ocp_reg_write(tp, 0xb87e, 0x0d0a);
7762 ocp_reg_write(tp, 0xb87c, 0x817b);
7763 ocp_reg_write(tp, 0xb87e, 0x1d0a);
7764
7765 data = sram_read(tp, 0x8217);
7766 data &= ~0xff00;
7767 data |= 0x5000;
7768 sram_write(tp, 0x8217, data);
7769 data = sram_read(tp, 0x821a);
7770 data &= ~0xff00;
7771 data |= 0x5000;
7772 sram_write(tp, 0x821a, data);
7773 sram_write(tp, 0x80da, 0x0403);
7774 data = sram_read(tp, 0x80dc);
7775 data &= ~0xff00;
7776 data |= 0x1000;
7777 sram_write(tp, 0x80dc, data);
7778 sram_write(tp, 0x80b3, 0x0384);
7779 sram_write(tp, 0x80b7, 0x2007);
7780 data = sram_read(tp, 0x80ba);
7781 data &= ~0xff00;
7782 data |= 0x6c00;
7783 sram_write(tp, 0x80ba, data);
7784 sram_write(tp, 0x80b5, 0xf009);
7785 data = sram_read(tp, 0x80bd);
7786 data &= ~0xff00;
7787 data |= 0x9f00;
7788 sram_write(tp, 0x80bd, data);
7789 sram_write(tp, 0x80c7, 0xf083);
7790 sram_write(tp, 0x80dd, 0x03f0);
7791 data = sram_read(tp, 0x80df);
7792 data &= ~0xff00;
7793 data |= 0x1000;
7794 sram_write(tp, 0x80df, data);
7795 sram_write(tp, 0x80cb, 0x2007);
7796 data = sram_read(tp, 0x80ce);
7797 data &= ~0xff00;
7798 data |= 0x6c00;
7799 sram_write(tp, 0x80ce, data);
7800 sram_write(tp, 0x80c9, 0x8009);
7801 data = sram_read(tp, 0x80d1);
7802 data &= ~0xff00;
7803 data |= 0x8000;
7804 sram_write(tp, 0x80d1, data);
7805 sram_write(tp, 0x80a3, 0x200a);
7806 sram_write(tp, 0x80a5, 0xf0ad);
7807 sram_write(tp, 0x809f, 0x6073);
7808 sram_write(tp, 0x80a1, 0x000b);
7809 data = sram_read(tp, 0x80a9);
7810 data &= ~0xff00;
7811 data |= 0xc000;
7812 sram_write(tp, 0x80a9, data);
7813
7814 if (rtl_phy_patch_request(tp, true, true))
7815 return;
7816
7817 data = ocp_reg_read(tp, 0xb896);
7818 data &= ~BIT(0);
7819 ocp_reg_write(tp, 0xb896, data);
7820 data = ocp_reg_read(tp, 0xb892);
7821 data &= ~0xff00;
7822 ocp_reg_write(tp, 0xb892, data);
7823 ocp_reg_write(tp, 0xb88e, 0xc23e);
7824 ocp_reg_write(tp, 0xb890, 0x0000);
7825 ocp_reg_write(tp, 0xb88e, 0xc240);
7826 ocp_reg_write(tp, 0xb890, 0x0103);
7827 ocp_reg_write(tp, 0xb88e, 0xc242);
7828 ocp_reg_write(tp, 0xb890, 0x0507);
7829 ocp_reg_write(tp, 0xb88e, 0xc244);
7830 ocp_reg_write(tp, 0xb890, 0x090b);
7831 ocp_reg_write(tp, 0xb88e, 0xc246);
7832 ocp_reg_write(tp, 0xb890, 0x0c0e);
7833 ocp_reg_write(tp, 0xb88e, 0xc248);
7834 ocp_reg_write(tp, 0xb890, 0x1012);
7835 ocp_reg_write(tp, 0xb88e, 0xc24a);
7836 ocp_reg_write(tp, 0xb890, 0x1416);
7837 data = ocp_reg_read(tp, 0xb896);
7838 data |= BIT(0);
7839 ocp_reg_write(tp, 0xb896, data);
7840
7841 rtl_phy_patch_request(tp, false, true);
7842
7843 data = ocp_reg_read(tp, 0xa86a);
7844 data |= BIT(0);
7845 ocp_reg_write(tp, 0xa86a, data);
7846 data = ocp_reg_read(tp, 0xa6f0);
7847 data |= BIT(0);
7848 ocp_reg_write(tp, 0xa6f0, data);
7849
7850 ocp_reg_write(tp, 0xbfa0, 0xd70d);
7851 ocp_reg_write(tp, 0xbfa2, 0x4100);
7852 ocp_reg_write(tp, 0xbfa4, 0xe868);
7853 ocp_reg_write(tp, 0xbfa6, 0xdc59);
7854 ocp_reg_write(tp, 0xb54c, 0x3c18);
7855 data = ocp_reg_read(tp, 0xbfa4);
7856 data &= ~BIT(5);
7857 ocp_reg_write(tp, 0xbfa4, data);
7858 data = sram_read(tp, 0x817d);
7859 data |= BIT(12);
7860 sram_write(tp, 0x817d, data);
7861 break;
7862 case RTL_VER_13:
7863 /* 2.5G INRX */
7864 data = ocp_reg_read(tp, 0xac46);
7865 data &= ~0x00f0;
7866 data |= 0x0090;
7867 ocp_reg_write(tp, 0xac46, data);
7868 data = ocp_reg_read(tp, 0xad30);
7869 data &= ~0x0003;
7870 data |= 0x0001;
7871 ocp_reg_write(tp, 0xad30, data);
7872 fallthrough;
7873 case RTL_VER_15:
7874 /* EEE parameter */
7875 ocp_reg_write(tp, 0xb87c, 0x80f5);
7876 ocp_reg_write(tp, 0xb87e, 0x760e);
7877 ocp_reg_write(tp, 0xb87c, 0x8107);
7878 ocp_reg_write(tp, 0xb87e, 0x360e);
7879 ocp_reg_write(tp, 0xb87c, 0x8551);
7880 data = ocp_reg_read(tp, 0xb87e);
7881 data &= ~0xff00;
7882 data |= 0x0800;
7883 ocp_reg_write(tp, 0xb87e, data);
7884
7885 /* ADC_PGA parameter */
7886 data = ocp_reg_read(tp, 0xbf00);
7887 data &= ~0xe000;
7888 data |= 0xa000;
7889 ocp_reg_write(tp, 0xbf00, data);
7890 data = ocp_reg_read(tp, 0xbf46);
7891 data &= ~0x0f00;
7892 data |= 0x0300;
7893 ocp_reg_write(tp, 0xbf46, data);
7894
7895 /* Green Table-PGA, 1G full viterbi */
7896 sram_write(tp, 0x8044, 0x2417);
7897 sram_write(tp, 0x804a, 0x2417);
7898 sram_write(tp, 0x8050, 0x2417);
7899 sram_write(tp, 0x8056, 0x2417);
7900 sram_write(tp, 0x805c, 0x2417);
7901 sram_write(tp, 0x8062, 0x2417);
7902 sram_write(tp, 0x8068, 0x2417);
7903 sram_write(tp, 0x806e, 0x2417);
7904 sram_write(tp, 0x8074, 0x2417);
7905 sram_write(tp, 0x807a, 0x2417);
7906
7907 /* XG PLL */
7908 data = ocp_reg_read(tp, 0xbf84);
7909 data &= ~0xe000;
7910 data |= 0xa000;
7911 ocp_reg_write(tp, 0xbf84, data);
7912 break;
7913 default:
7914 break;
7915 }
7916
7917 if (rtl_phy_patch_request(tp, true, true))
7918 return;
7919
7920 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7921 ocp_data |= EEE_SPDWN_EN;
7922 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7923
7924 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7925 data &= ~(EN_EEE_100 | EN_EEE_1000);
7926 data |= EN_10M_CLKDIV;
7927 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7928 tp->ups_info._10m_ckdiv = true;
7929 tp->ups_info.eee_plloff_100 = false;
7930 tp->ups_info.eee_plloff_giga = false;
7931
7932 data = ocp_reg_read(tp, OCP_POWER_CFG);
7933 data &= ~EEE_CLKDIV_EN;
7934 ocp_reg_write(tp, OCP_POWER_CFG, data);
7935 tp->ups_info.eee_ckdiv = false;
7936
7937 rtl_phy_patch_request(tp, false, true);
7938
7939 rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7940
7941 data = ocp_reg_read(tp, 0xa428);
7942 data &= ~BIT(9);
7943 ocp_reg_write(tp, 0xa428, data);
7944 data = ocp_reg_read(tp, 0xa5ea);
7945 data &= ~BIT(0);
7946 ocp_reg_write(tp, 0xa5ea, data);
7947 tp->ups_info.lite_mode = 0;
7948
7949 if (tp->eee_en)
7950 rtl_eee_enable(tp, true);
7951
7952 r8153_aldps_en(tp, true);
7953 r8152b_enable_fc(tp);
7954 r8153_u2p3en(tp, true);
7955
7956 set_bit(PHY_RESET, &tp->flags);
7957}
7958
7959static void r8156_init(struct r8152 *tp)
7960{
7961 u32 ocp_data;
7962 u16 data;
7963 int i;
7964
7965 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7966 return;
7967
7968 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
7969 ocp_data &= ~EN_ALL_SPEED;
7970 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
7971
7972 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
7973
7974 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
7975 ocp_data |= BYPASS_MAC_RESET;
7976 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
7977
7978 r8153b_u1u2en(tp, false);
7979
7980 for (i = 0; i < 500; i++) {
7981 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7982 AUTOLOAD_DONE)
7983 break;
7984
7985 msleep(20);
7986 if (test_bit(RTL8152_UNPLUG, &tp->flags))
7987 return;
7988 }
7989
7990 data = r8153_phy_status(tp, 0);
7991 if (data == PHY_STAT_EXT_INIT) {
7992 data = ocp_reg_read(tp, 0xa468);
7993 data &= ~(BIT(3) | BIT(1));
7994 ocp_reg_write(tp, 0xa468, data);
7995 }
7996
7997 data = r8152_mdio_read(tp, MII_BMCR);
7998 if (data & BMCR_PDOWN) {
7999 data &= ~BMCR_PDOWN;
8000 r8152_mdio_write(tp, MII_BMCR, data);
8001 }
8002
8003 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8004 WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
8005
8006 r8153_u2p3en(tp, false);
8007
8008 /* MSC timer = 0xfff * 8ms = 32760 ms */
8009 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8010
8011 /* U1/U2/L1 idle timer. 500 us */
8012 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8013
8014 r8153b_power_cut_en(tp, false);
8015 r8156_ups_en(tp, false);
8016 r8153_queue_wake(tp, false);
8017 rtl_runtime_suspend_enable(tp, false);
8018
8019 if (tp->udev->speed >= USB_SPEED_SUPER)
8020 r8153b_u1u2en(tp, true);
8021
8022 usb_enable_lpm(tp->udev);
8023
8024 r8156_mac_clk_spd(tp, true);
8025
8026 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8027 ocp_data &= ~PLA_MCU_SPDWN_EN;
8028 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8029
8030 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8031 if (rtl8152_get_speed(tp) & LINK_STATUS)
8032 ocp_data |= CUR_LINK_OK;
8033 else
8034 ocp_data &= ~CUR_LINK_OK;
8035 ocp_data |= POLL_LINK_CHG;
8036 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8037
8038 set_bit(GREEN_ETHERNET, &tp->flags);
8039
8040 /* rx aggregation */
8041 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8042 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8043 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8044
8045 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
8046 ocp_data |= ACT_ODMA;
8047 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
8048
Hayes Wang45bf9442021-12-23 17:27:01 +08008049 r8156_mdio_force_mode(tp);
Hayes Wang195aae32021-04-16 16:04:35 +08008050 rtl_tally_reset(tp);
8051
8052 tp->coalesce = 15000; /* 15 us */
8053}
8054
8055static void r8156b_init(struct r8152 *tp)
8056{
8057 u32 ocp_data;
8058 u16 data;
8059 int i;
8060
8061 if (test_bit(RTL8152_UNPLUG, &tp->flags))
8062 return;
8063
8064 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
8065 ocp_data &= ~EN_ALL_SPEED;
8066 ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
8067
8068 ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
8069
8070 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
8071 ocp_data |= BYPASS_MAC_RESET;
8072 ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
8073
8074 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
8075 ocp_data |= RX_DETECT8;
8076 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
8077
8078 r8153b_u1u2en(tp, false);
8079
8080 switch (tp->version) {
8081 case RTL_VER_13:
8082 case RTL_VER_15:
8083 r8156b_wait_loading_flash(tp);
8084 break;
8085 default:
8086 break;
8087 }
8088
8089 for (i = 0; i < 500; i++) {
8090 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
8091 AUTOLOAD_DONE)
8092 break;
8093
8094 msleep(20);
8095 if (test_bit(RTL8152_UNPLUG, &tp->flags))
8096 return;
8097 }
8098
8099 data = r8153_phy_status(tp, 0);
8100 if (data == PHY_STAT_EXT_INIT) {
8101 data = ocp_reg_read(tp, 0xa468);
8102 data &= ~(BIT(3) | BIT(1));
8103 ocp_reg_write(tp, 0xa468, data);
8104
8105 data = ocp_reg_read(tp, 0xa466);
8106 data &= ~BIT(0);
8107 ocp_reg_write(tp, 0xa466, data);
8108 }
8109
8110 data = r8152_mdio_read(tp, MII_BMCR);
8111 if (data & BMCR_PDOWN) {
8112 data &= ~BMCR_PDOWN;
8113 r8152_mdio_write(tp, MII_BMCR, data);
8114 }
8115
8116 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8117
8118 r8153_u2p3en(tp, false);
8119
8120 /* MSC timer = 0xfff * 8ms = 32760 ms */
8121 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8122
8123 /* U1/U2/L1 idle timer. 500 us */
8124 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8125
8126 r8153b_power_cut_en(tp, false);
8127 r8156_ups_en(tp, false);
8128 r8153_queue_wake(tp, false);
8129 rtl_runtime_suspend_enable(tp, false);
8130
8131 if (tp->udev->speed >= USB_SPEED_SUPER)
8132 r8153b_u1u2en(tp, true);
8133
8134 usb_enable_lpm(tp->udev);
8135
8136 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR);
8137 ocp_data &= ~SLOT_EN;
8138 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8139
8140 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
8141 ocp_data |= FLOW_CTRL_EN;
8142 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
8143
8144 /* enable fc timer and set timer to 600 ms. */
8145 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
8146 CTRL_TIMER_EN | (600 / 8));
8147
8148 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
8149 if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN))
8150 ocp_data |= FLOW_CTRL_PATCH_2;
8151 ocp_data &= ~AUTO_SPEEDUP;
8152 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
8153
8154 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
8155 ocp_data |= FC_PATCH_TASK;
8156 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
8157
8158 r8156_mac_clk_spd(tp, true);
8159
8160 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8161 ocp_data &= ~PLA_MCU_SPDWN_EN;
8162 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8163
8164 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8165 if (rtl8152_get_speed(tp) & LINK_STATUS)
8166 ocp_data |= CUR_LINK_OK;
8167 else
8168 ocp_data &= ~CUR_LINK_OK;
8169 ocp_data |= POLL_LINK_CHG;
8170 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8171
8172 set_bit(GREEN_ETHERNET, &tp->flags);
8173
8174 /* rx aggregation */
8175 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8176 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8177 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8178
Hayes Wang45bf9442021-12-23 17:27:01 +08008179 r8156_mdio_force_mode(tp);
Hayes Wang195aae32021-04-16 16:04:35 +08008180 rtl_tally_reset(tp);
8181
8182 tp->coalesce = 15000; /* 15 us */
8183}
8184
Hayes Wang1a44fb32021-05-24 14:49:42 +08008185static bool rtl_check_vendor_ok(struct usb_interface *intf)
8186{
8187 struct usb_host_interface *alt = intf->cur_altsetting;
8188 struct usb_endpoint_descriptor *in, *out, *intr;
8189
8190 if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) {
8191 dev_err(&intf->dev, "Expected endpoints are not found\n");
8192 return false;
8193 }
8194
8195 /* Check Rx endpoint address */
8196 if (usb_endpoint_num(in) != 1) {
8197 dev_err(&intf->dev, "Invalid Rx endpoint address\n");
8198 return false;
8199 }
8200
8201 /* Check Tx endpoint address */
8202 if (usb_endpoint_num(out) != 2) {
8203 dev_err(&intf->dev, "Invalid Tx endpoint address\n");
8204 return false;
8205 }
8206
8207 /* Check interrupt endpoint address */
8208 if (usb_endpoint_num(intr) != 3) {
8209 dev_err(&intf->dev, "Invalid interrupt endpoint address\n");
8210 return false;
8211 }
8212
8213 return true;
8214}
8215
Hayes Wangc2198942021-04-16 16:04:37 +08008216static bool rtl_vendor_mode(struct usb_interface *intf)
8217{
8218 struct usb_host_interface *alt = intf->cur_altsetting;
8219 struct usb_device *udev;
8220 struct usb_host_config *c;
8221 int i, num_configs;
8222
8223 if (alt->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC)
Hayes Wang1a44fb32021-05-24 14:49:42 +08008224 return rtl_check_vendor_ok(intf);
Hayes Wangc2198942021-04-16 16:04:37 +08008225
8226 /* The vendor mode is not always config #1, so to find it out. */
8227 udev = interface_to_usbdev(intf);
8228 c = udev->config;
8229 num_configs = udev->descriptor.bNumConfigurations;
Hayes Wang1a44fb32021-05-24 14:49:42 +08008230 if (num_configs < 2)
8231 return false;
8232
Hayes Wangc2198942021-04-16 16:04:37 +08008233 for (i = 0; i < num_configs; (i++, c++)) {
8234 struct usb_interface_descriptor *desc = NULL;
8235
8236 if (c->desc.bNumInterfaces > 0)
8237 desc = &c->intf_cache[0]->altsetting->desc;
8238 else
8239 continue;
8240
8241 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
8242 usb_driver_set_configuration(udev, c->desc.bConfigurationValue);
8243 break;
8244 }
8245 }
8246
Hayes Wang1a44fb32021-05-24 14:49:42 +08008247 if (i == num_configs)
8248 dev_err(&intf->dev, "Unexpected Device\n");
Hayes Wangc2198942021-04-16 16:04:37 +08008249
8250 return false;
8251}
8252
hayeswange5011392015-07-29 20:39:08 +08008253static int rtl8152_pre_reset(struct usb_interface *intf)
8254{
8255 struct r8152 *tp = usb_get_intfdata(intf);
8256 struct net_device *netdev;
8257
8258 if (!tp)
8259 return 0;
8260
8261 netdev = tp->netdev;
8262 if (!netif_running(netdev))
8263 return 0;
8264
hayeswangde9bf292017-01-26 09:38:32 +08008265 netif_stop_queue(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08008266 tasklet_disable(&tp->tx_tl);
hayeswange5011392015-07-29 20:39:08 +08008267 clear_bit(WORK_ENABLE, &tp->flags);
8268 usb_kill_urb(tp->intr_urb);
8269 cancel_delayed_work_sync(&tp->schedule);
Hayes Wang5b1d9c12019-11-22 16:21:09 +08008270 napi_disable(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08008271 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08008272 mutex_lock(&tp->control);
8273 tp->rtl_ops.disable(tp);
8274 mutex_unlock(&tp->control);
8275 }
8276
8277 return 0;
8278}
8279
8280static int rtl8152_post_reset(struct usb_interface *intf)
8281{
8282 struct r8152 *tp = usb_get_intfdata(intf);
8283 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05008284 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08008285
8286 if (!tp)
8287 return 0;
8288
Zheng Yongjunf62c4f32021-06-01 22:18:13 +08008289 /* reset the MAC address in case of policy change */
Mario Limonciello25766272019-04-04 13:46:53 -05008290 if (determine_ethernet_addr(tp, &sa) >= 0) {
8291 rtnl_lock();
8292 dev_set_mac_address (tp->netdev, &sa, NULL);
8293 rtnl_unlock();
8294 }
8295
hayeswange5011392015-07-29 20:39:08 +08008296 netdev = tp->netdev;
8297 if (!netif_running(netdev))
8298 return 0;
8299
8300 set_bit(WORK_ENABLE, &tp->flags);
8301 if (netif_carrier_ok(netdev)) {
8302 mutex_lock(&tp->control);
8303 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08008304 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08008305 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08008306 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08008307 }
8308
8309 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08008310 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08008311 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08008312 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08008313
hayeswang7489bda2017-01-26 09:38:34 +08008314 if (!list_empty(&tp->rx_done))
8315 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08008316
8317 return 0;
hayeswangac718b62013-05-02 16:01:25 +00008318}
8319
hayeswang2dd49e02015-09-07 11:57:44 +08008320static bool delay_autosuspend(struct r8152 *tp)
8321{
8322 bool sw_linking = !!netif_carrier_ok(tp->netdev);
8323 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
8324
8325 /* This means a linking change occurs and the driver doesn't detect it,
8326 * yet. If the driver has disabled tx/rx and hw is linking on, the
8327 * device wouldn't wake up by receiving any packet.
8328 */
8329 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
8330 return true;
8331
8332 /* If the linking down is occurred by nway, the device may miss the
8333 * linking change event. And it wouldn't wake when linking on.
8334 */
8335 if (!sw_linking && tp->rtl_ops.in_nway(tp))
8336 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08008337 else if (!skb_queue_empty(&tp->tx_queue))
8338 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08008339 else
8340 return false;
8341}
8342
hayeswang21cbd0e2017-06-13 15:14:39 +08008343static int rtl8152_runtime_resume(struct r8152 *tp)
8344{
8345 struct net_device *netdev = tp->netdev;
8346
8347 if (netif_running(netdev) && netdev->flags & IFF_UP) {
8348 struct napi_struct *napi = &tp->napi;
8349
8350 tp->rtl_ops.autosuspend_en(tp, false);
8351 napi_disable(napi);
8352 set_bit(WORK_ENABLE, &tp->flags);
8353
8354 if (netif_carrier_ok(netdev)) {
8355 if (rtl8152_get_speed(tp) & LINK_STATUS) {
8356 rtl_start_rx(tp);
8357 } else {
8358 netif_carrier_off(netdev);
8359 tp->rtl_ops.disable(tp);
8360 netif_info(tp, link, netdev, "linking down\n");
8361 }
8362 }
8363
8364 napi_enable(napi);
8365 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8366 smp_mb__after_atomic();
8367
8368 if (!list_empty(&tp->rx_done))
8369 napi_schedule(&tp->napi);
8370
8371 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8372 } else {
8373 if (netdev->flags & IFF_UP)
8374 tp->rtl_ops.autosuspend_en(tp, false);
8375
8376 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8377 }
8378
8379 return 0;
8380}
8381
8382static int rtl8152_system_resume(struct r8152 *tp)
8383{
8384 struct net_device *netdev = tp->netdev;
8385
8386 netif_device_attach(netdev);
8387
Hayes Wang5b1d9c12019-11-22 16:21:09 +08008388 if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
hayeswang21cbd0e2017-06-13 15:14:39 +08008389 tp->rtl_ops.up(tp);
8390 netif_carrier_off(netdev);
8391 set_bit(WORK_ENABLE, &tp->flags);
8392 usb_submit_urb(tp->intr_urb, GFP_NOIO);
8393 }
8394
8395 return 0;
8396}
8397
hayeswanga9c54ad2017-01-25 13:41:45 +08008398static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00008399{
hayeswang6cc69f22014-10-17 16:55:08 +08008400 struct net_device *netdev = tp->netdev;
8401 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00008402
Hayes Wangc79515e2021-02-19 17:04:41 +08008403 if (!tp->rtl_ops.autosuspend_en)
8404 return -EBUSY;
8405
hayeswang26afec32017-01-26 09:38:31 +08008406 set_bit(SELECTIVE_SUSPEND, &tp->flags);
8407 smp_mb__after_atomic();
8408
hayeswang8fb28062017-01-10 17:04:06 +08008409 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08008410 u32 rcr = 0;
8411
hayeswang75dc6922017-01-10 17:04:07 +08008412 if (netif_carrier_ok(netdev)) {
8413 u32 ocp_data;
8414
8415 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
8416 ocp_data = rcr & ~RCR_ACPT_ALL;
8417 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8418 rxdy_gated_en(tp, true);
8419 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
8420 PLA_OOB_CTRL);
8421 if (!(ocp_data & RXFIFO_EMPTY)) {
8422 rxdy_gated_en(tp, false);
8423 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08008424 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8425 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08008426 ret = -EBUSY;
8427 goto out1;
8428 }
8429 }
8430
hayeswang8fb28062017-01-10 17:04:06 +08008431 clear_bit(WORK_ENABLE, &tp->flags);
8432 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08008433
hayeswang8fb28062017-01-10 17:04:06 +08008434 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08008435
8436 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08008437 struct napi_struct *napi = &tp->napi;
8438
8439 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08008440 rtl_stop_rx(tp);
8441 rxdy_gated_en(tp, false);
8442 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08008443 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08008444 }
hayeswangbd882982017-06-13 15:14:40 +08008445
8446 if (delay_autosuspend(tp)) {
8447 rtl8152_runtime_resume(tp);
8448 ret = -EBUSY;
8449 }
hayeswang6cc69f22014-10-17 16:55:08 +08008450 }
8451
hayeswang8fb28062017-01-10 17:04:06 +08008452out1:
8453 return ret;
8454}
8455
8456static int rtl8152_system_suspend(struct r8152 *tp)
8457{
8458 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08008459
8460 netif_device_detach(netdev);
8461
hayeswange3bd1a82014-10-29 11:12:17 +08008462 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08008463 struct napi_struct *napi = &tp->napi;
8464
hayeswangac718b62013-05-02 16:01:25 +00008465 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08008466 usb_kill_urb(tp->intr_urb);
Hayes Wangd2187f82019-08-19 14:40:36 +08008467 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08008468 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08008469 cancel_delayed_work_sync(&tp->schedule);
8470 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08008471 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08008472 tasklet_enable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00008473 }
hayeswang8fb28062017-01-10 17:04:06 +08008474
zhong jiangf7419172018-08-09 09:39:13 +08008475 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08008476}
8477
8478static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
8479{
8480 struct r8152 *tp = usb_get_intfdata(intf);
8481 int ret;
8482
8483 mutex_lock(&tp->control);
8484
8485 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08008486 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08008487 else
8488 ret = rtl8152_system_suspend(tp);
8489
hayeswangb5403272014-10-09 18:00:26 +08008490 mutex_unlock(&tp->control);
8491
hayeswang6cc69f22014-10-17 16:55:08 +08008492 return ret;
hayeswangac718b62013-05-02 16:01:25 +00008493}
8494
8495static int rtl8152_resume(struct usb_interface *intf)
8496{
8497 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08008498 int ret;
hayeswangac718b62013-05-02 16:01:25 +00008499
hayeswangb5403272014-10-09 18:00:26 +08008500 mutex_lock(&tp->control);
8501
Hayes Wangb24edca2021-12-23 17:27:02 +08008502 rtl_reset_ocp_base(tp);
8503
hayeswang21cbd0e2017-06-13 15:14:39 +08008504 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
8505 ret = rtl8152_runtime_resume(tp);
8506 else
8507 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00008508
hayeswangb5403272014-10-09 18:00:26 +08008509 mutex_unlock(&tp->control);
8510
hayeswang21cbd0e2017-06-13 15:14:39 +08008511 return ret;
hayeswangac718b62013-05-02 16:01:25 +00008512}
8513
hayeswang7ec25412016-01-04 14:38:46 +08008514static int rtl8152_reset_resume(struct usb_interface *intf)
8515{
8516 struct r8152 *tp = usb_get_intfdata(intf);
8517
8518 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
Hayes Wangb24edca2021-12-23 17:27:02 +08008519 rtl_reset_ocp_base(tp);
hayeswangbefb2de2017-06-09 17:11:45 +08008520 tp->rtl_ops.init(tp);
8521 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
Takashi Iwai776ac632021-07-14 19:00:22 +02008522 set_ethernet_addr(tp, true);
hayeswang7ec25412016-01-04 14:38:46 +08008523 return rtl8152_resume(intf);
8524}
8525
hayeswang21ff2e82014-02-18 21:49:06 +08008526static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8527{
8528 struct r8152 *tp = netdev_priv(dev);
8529
hayeswang9a4be1b2014-02-18 21:49:07 +08008530 if (usb_autopm_get_interface(tp->intf) < 0)
8531 return;
8532
hayeswang7daed8d2015-07-24 13:54:24 +08008533 if (!rtl_can_wakeup(tp)) {
8534 wol->supported = 0;
8535 wol->wolopts = 0;
8536 } else {
8537 mutex_lock(&tp->control);
8538 wol->supported = WAKE_ANY;
8539 wol->wolopts = __rtl_get_wol(tp);
8540 mutex_unlock(&tp->control);
8541 }
hayeswangb5403272014-10-09 18:00:26 +08008542
hayeswang9a4be1b2014-02-18 21:49:07 +08008543 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08008544}
8545
8546static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8547{
8548 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08008549 int ret;
8550
hayeswang7daed8d2015-07-24 13:54:24 +08008551 if (!rtl_can_wakeup(tp))
8552 return -EOPNOTSUPP;
8553
Florian Fainellif2750df2018-09-28 16:18:54 -07008554 if (wol->wolopts & ~WAKE_ANY)
8555 return -EINVAL;
8556
hayeswang9a4be1b2014-02-18 21:49:07 +08008557 ret = usb_autopm_get_interface(tp->intf);
8558 if (ret < 0)
8559 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08008560
hayeswangb5403272014-10-09 18:00:26 +08008561 mutex_lock(&tp->control);
8562
hayeswang21ff2e82014-02-18 21:49:06 +08008563 __rtl_set_wol(tp, wol->wolopts);
8564 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
8565
hayeswangb5403272014-10-09 18:00:26 +08008566 mutex_unlock(&tp->control);
8567
hayeswang9a4be1b2014-02-18 21:49:07 +08008568 usb_autopm_put_interface(tp->intf);
8569
8570out_set_wol:
8571 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08008572}
8573
hayeswanga5ec27c2014-02-18 21:49:11 +08008574static u32 rtl8152_get_msglevel(struct net_device *dev)
8575{
8576 struct r8152 *tp = netdev_priv(dev);
8577
8578 return tp->msg_enable;
8579}
8580
8581static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
8582{
8583 struct r8152 *tp = netdev_priv(dev);
8584
8585 tp->msg_enable = value;
8586}
8587
hayeswangac718b62013-05-02 16:01:25 +00008588static void rtl8152_get_drvinfo(struct net_device *netdev,
8589 struct ethtool_drvinfo *info)
8590{
8591 struct r8152 *tp = netdev_priv(netdev);
8592
hayeswangb0b46c72014-08-26 10:08:23 +08008593 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
8594 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00008595 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
Hayes Wang9370f2d2019-10-16 11:02:42 +08008596 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
8597 strlcpy(info->fw_version, tp->rtl_fw.version,
8598 sizeof(info->fw_version));
hayeswangac718b62013-05-02 16:01:25 +00008599}
8600
8601static
Philippe Reynes06144dc2017-03-12 22:41:58 +01008602int rtl8152_get_link_ksettings(struct net_device *netdev,
8603 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00008604{
8605 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08008606 int ret;
hayeswangac718b62013-05-02 16:01:25 +00008607
8608 if (!tp->mii.mdio_read)
8609 return -EOPNOTSUPP;
8610
hayeswang8d4a4d72014-10-09 18:00:25 +08008611 ret = usb_autopm_get_interface(tp->intf);
8612 if (ret < 0)
8613 goto out;
8614
hayeswangb5403272014-10-09 18:00:26 +08008615 mutex_lock(&tp->control);
8616
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03008617 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08008618
Hayes Wang195aae32021-04-16 16:04:35 +08008619 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8620 cmd->link_modes.supported, tp->support_2500full);
8621
8622 if (tp->support_2500full) {
8623 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8624 cmd->link_modes.advertising,
8625 ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G);
8626
8627 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8628 cmd->link_modes.lp_advertising,
8629 ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G);
8630
8631 if (is_speed_2500(rtl8152_get_speed(tp)))
8632 cmd->base.speed = SPEED_2500;
8633 }
8634
hayeswangb5403272014-10-09 18:00:26 +08008635 mutex_unlock(&tp->control);
8636
hayeswang8d4a4d72014-10-09 18:00:25 +08008637 usb_autopm_put_interface(tp->intf);
8638
8639out:
8640 return ret;
hayeswangac718b62013-05-02 16:01:25 +00008641}
8642
Philippe Reynes06144dc2017-03-12 22:41:58 +01008643static int rtl8152_set_link_ksettings(struct net_device *dev,
8644 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00008645{
8646 struct r8152 *tp = netdev_priv(dev);
Hayes Wang771efed2019-09-02 19:52:28 +08008647 u32 advertising = 0;
hayeswang9a4be1b2014-02-18 21:49:07 +08008648 int ret;
hayeswangac718b62013-05-02 16:01:25 +00008649
hayeswang9a4be1b2014-02-18 21:49:07 +08008650 ret = usb_autopm_get_interface(tp->intf);
8651 if (ret < 0)
8652 goto out;
8653
Hayes Wang771efed2019-09-02 19:52:28 +08008654 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
8655 cmd->link_modes.advertising))
8656 advertising |= RTL_ADVERTISED_10_HALF;
8657
8658 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
8659 cmd->link_modes.advertising))
8660 advertising |= RTL_ADVERTISED_10_FULL;
8661
8662 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
8663 cmd->link_modes.advertising))
8664 advertising |= RTL_ADVERTISED_100_HALF;
8665
8666 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
8667 cmd->link_modes.advertising))
8668 advertising |= RTL_ADVERTISED_100_FULL;
8669
8670 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
8671 cmd->link_modes.advertising))
8672 advertising |= RTL_ADVERTISED_1000_HALF;
8673
8674 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
8675 cmd->link_modes.advertising))
8676 advertising |= RTL_ADVERTISED_1000_FULL;
8677
Hayes Wang195aae32021-04-16 16:04:35 +08008678 if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8679 cmd->link_modes.advertising))
8680 advertising |= RTL_ADVERTISED_2500_FULL;
8681
hayeswangb5403272014-10-09 18:00:26 +08008682 mutex_lock(&tp->control);
8683
Philippe Reynes06144dc2017-03-12 22:41:58 +01008684 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
Hayes Wang771efed2019-09-02 19:52:28 +08008685 cmd->base.duplex, advertising);
hayeswangaa7e26b2016-06-13 17:49:38 +08008686 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01008687 tp->autoneg = cmd->base.autoneg;
8688 tp->speed = cmd->base.speed;
8689 tp->duplex = cmd->base.duplex;
Hayes Wang771efed2019-09-02 19:52:28 +08008690 tp->advertising = advertising;
hayeswangaa7e26b2016-06-13 17:49:38 +08008691 }
hayeswang9a4be1b2014-02-18 21:49:07 +08008692
hayeswangb5403272014-10-09 18:00:26 +08008693 mutex_unlock(&tp->control);
8694
hayeswang9a4be1b2014-02-18 21:49:07 +08008695 usb_autopm_put_interface(tp->intf);
8696
8697out:
8698 return ret;
hayeswangac718b62013-05-02 16:01:25 +00008699}
8700
hayeswang4f1d4d52014-03-11 16:24:19 +08008701static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
8702 "tx_packets",
8703 "rx_packets",
8704 "tx_errors",
8705 "rx_errors",
8706 "rx_missed",
8707 "align_errors",
8708 "tx_single_collisions",
8709 "tx_multi_collisions",
8710 "rx_unicast",
8711 "rx_broadcast",
8712 "rx_multicast",
8713 "tx_aborted",
8714 "tx_underrun",
8715};
8716
8717static int rtl8152_get_sset_count(struct net_device *dev, int sset)
8718{
8719 switch (sset) {
8720 case ETH_SS_STATS:
8721 return ARRAY_SIZE(rtl8152_gstrings);
8722 default:
8723 return -EOPNOTSUPP;
8724 }
8725}
8726
8727static void rtl8152_get_ethtool_stats(struct net_device *dev,
8728 struct ethtool_stats *stats, u64 *data)
8729{
8730 struct r8152 *tp = netdev_priv(dev);
8731 struct tally_counter tally;
8732
hayeswang0b030242014-07-08 14:49:28 +08008733 if (usb_autopm_get_interface(tp->intf) < 0)
8734 return;
8735
hayeswang4f1d4d52014-03-11 16:24:19 +08008736 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
8737
hayeswang0b030242014-07-08 14:49:28 +08008738 usb_autopm_put_interface(tp->intf);
8739
hayeswang4f1d4d52014-03-11 16:24:19 +08008740 data[0] = le64_to_cpu(tally.tx_packets);
8741 data[1] = le64_to_cpu(tally.rx_packets);
8742 data[2] = le64_to_cpu(tally.tx_errors);
8743 data[3] = le32_to_cpu(tally.rx_errors);
8744 data[4] = le16_to_cpu(tally.rx_missed);
8745 data[5] = le16_to_cpu(tally.align_errors);
8746 data[6] = le32_to_cpu(tally.tx_one_collision);
8747 data[7] = le32_to_cpu(tally.tx_multi_collision);
8748 data[8] = le64_to_cpu(tally.rx_unicast);
8749 data[9] = le64_to_cpu(tally.rx_broadcast);
8750 data[10] = le32_to_cpu(tally.rx_multicast);
8751 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08008752 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08008753}
8754
8755static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
8756{
8757 switch (stringset) {
8758 case ETH_SS_STATS:
Kees Cook99718ab2021-06-16 12:53:03 -07008759 memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings));
hayeswang4f1d4d52014-03-11 16:24:19 +08008760 break;
8761 }
8762}
8763
hayeswangdf35d282014-09-25 20:54:02 +08008764static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8765{
Hayes Wangf4a93be2019-08-23 15:33:40 +08008766 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08008767 u16 val;
8768
8769 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
8770 supported = mmd_eee_cap_to_ethtool_sup_t(val);
8771
8772 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
8773 adv = mmd_eee_adv_to_ethtool_adv_t(val);
8774
8775 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
8776 lp = mmd_eee_adv_to_ethtool_adv_t(val);
8777
Hayes Wangf4a93be2019-08-23 15:33:40 +08008778 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08008779 eee->eee_active = !!(supported & adv & lp);
8780 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08008781 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08008782 eee->lp_advertised = lp;
8783
8784 return 0;
8785}
8786
8787static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
8788{
8789 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
8790
Hayes Wangf4a93be2019-08-23 15:33:40 +08008791 tp->eee_en = eee->eee_enabled;
8792 tp->eee_adv = val;
8793
Hayes Wange7bde562019-08-23 15:33:41 +08008794 rtl_eee_enable(tp, tp->eee_en);
hayeswangdf35d282014-09-25 20:54:02 +08008795
8796 return 0;
8797}
8798
8799static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8800{
Hayes Wangf4a93be2019-08-23 15:33:40 +08008801 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08008802 u16 val;
8803
8804 val = ocp_reg_read(tp, OCP_EEE_ABLE);
8805 supported = mmd_eee_cap_to_ethtool_sup_t(val);
8806
8807 val = ocp_reg_read(tp, OCP_EEE_ADV);
8808 adv = mmd_eee_adv_to_ethtool_adv_t(val);
8809
8810 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
8811 lp = mmd_eee_adv_to_ethtool_adv_t(val);
8812
Hayes Wangf4a93be2019-08-23 15:33:40 +08008813 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08008814 eee->eee_active = !!(supported & adv & lp);
8815 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08008816 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08008817 eee->lp_advertised = lp;
8818
8819 return 0;
8820}
8821
hayeswangdf35d282014-09-25 20:54:02 +08008822static int
8823rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
8824{
8825 struct r8152 *tp = netdev_priv(net);
8826 int ret;
8827
Hayes Wangc79515e2021-02-19 17:04:41 +08008828 if (!tp->rtl_ops.eee_get) {
8829 ret = -EOPNOTSUPP;
8830 goto out;
8831 }
8832
hayeswangdf35d282014-09-25 20:54:02 +08008833 ret = usb_autopm_get_interface(tp->intf);
8834 if (ret < 0)
8835 goto out;
8836
hayeswangb5403272014-10-09 18:00:26 +08008837 mutex_lock(&tp->control);
8838
hayeswangdf35d282014-09-25 20:54:02 +08008839 ret = tp->rtl_ops.eee_get(tp, edata);
8840
hayeswangb5403272014-10-09 18:00:26 +08008841 mutex_unlock(&tp->control);
8842
hayeswangdf35d282014-09-25 20:54:02 +08008843 usb_autopm_put_interface(tp->intf);
8844
8845out:
8846 return ret;
8847}
8848
8849static int
8850rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
8851{
8852 struct r8152 *tp = netdev_priv(net);
8853 int ret;
8854
Hayes Wangc79515e2021-02-19 17:04:41 +08008855 if (!tp->rtl_ops.eee_set) {
8856 ret = -EOPNOTSUPP;
8857 goto out;
8858 }
8859
hayeswangdf35d282014-09-25 20:54:02 +08008860 ret = usb_autopm_get_interface(tp->intf);
8861 if (ret < 0)
8862 goto out;
8863
hayeswangb5403272014-10-09 18:00:26 +08008864 mutex_lock(&tp->control);
8865
hayeswangdf35d282014-09-25 20:54:02 +08008866 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08008867 if (!ret)
8868 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08008869
hayeswangb5403272014-10-09 18:00:26 +08008870 mutex_unlock(&tp->control);
8871
hayeswangdf35d282014-09-25 20:54:02 +08008872 usb_autopm_put_interface(tp->intf);
8873
8874out:
8875 return ret;
8876}
8877
hayeswang8884f502014-10-28 14:05:52 +08008878static int rtl8152_nway_reset(struct net_device *dev)
8879{
8880 struct r8152 *tp = netdev_priv(dev);
8881 int ret;
8882
8883 ret = usb_autopm_get_interface(tp->intf);
8884 if (ret < 0)
8885 goto out;
8886
8887 mutex_lock(&tp->control);
8888
8889 ret = mii_nway_restart(&tp->mii);
8890
8891 mutex_unlock(&tp->control);
8892
8893 usb_autopm_put_interface(tp->intf);
8894
8895out:
8896 return ret;
8897}
8898
hayeswangefb3dd82015-02-12 14:33:48 +08008899static int rtl8152_get_coalesce(struct net_device *netdev,
Yufeng Mof3ccfda12021-08-20 15:35:18 +08008900 struct ethtool_coalesce *coalesce,
8901 struct kernel_ethtool_coalesce *kernel_coal,
8902 struct netlink_ext_ack *extack)
hayeswangefb3dd82015-02-12 14:33:48 +08008903{
8904 struct r8152 *tp = netdev_priv(netdev);
8905
8906 switch (tp->version) {
8907 case RTL_VER_01:
8908 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08008909 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08008910 return -EOPNOTSUPP;
8911 default:
8912 break;
8913 }
8914
8915 coalesce->rx_coalesce_usecs = tp->coalesce;
8916
8917 return 0;
8918}
8919
8920static int rtl8152_set_coalesce(struct net_device *netdev,
Yufeng Mof3ccfda12021-08-20 15:35:18 +08008921 struct ethtool_coalesce *coalesce,
8922 struct kernel_ethtool_coalesce *kernel_coal,
8923 struct netlink_ext_ack *extack)
hayeswangefb3dd82015-02-12 14:33:48 +08008924{
8925 struct r8152 *tp = netdev_priv(netdev);
8926 int ret;
8927
8928 switch (tp->version) {
8929 case RTL_VER_01:
8930 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08008931 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08008932 return -EOPNOTSUPP;
8933 default:
8934 break;
8935 }
8936
8937 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
8938 return -EINVAL;
8939
8940 ret = usb_autopm_get_interface(tp->intf);
8941 if (ret < 0)
8942 return ret;
8943
8944 mutex_lock(&tp->control);
8945
8946 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
8947 tp->coalesce = coalesce->rx_coalesce_usecs;
8948
Hayes Wang9fae5412019-07-03 15:11:56 +08008949 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
8950 netif_stop_queue(netdev);
8951 napi_disable(&tp->napi);
8952 tp->rtl_ops.disable(tp);
8953 tp->rtl_ops.enable(tp);
8954 rtl_start_rx(tp);
8955 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
8956 _rtl8152_set_rx_mode(netdev);
8957 napi_enable(&tp->napi);
8958 netif_wake_queue(netdev);
8959 }
hayeswangefb3dd82015-02-12 14:33:48 +08008960 }
8961
8962 mutex_unlock(&tp->control);
8963
8964 usb_autopm_put_interface(tp->intf);
8965
8966 return ret;
8967}
8968
Hayes Wange4a50172019-08-13 11:42:09 +08008969static int rtl8152_get_tunable(struct net_device *netdev,
8970 const struct ethtool_tunable *tunable, void *d)
8971{
8972 struct r8152 *tp = netdev_priv(netdev);
8973
8974 switch (tunable->id) {
8975 case ETHTOOL_RX_COPYBREAK:
8976 *(u32 *)d = tp->rx_copybreak;
8977 break;
8978 default:
8979 return -EOPNOTSUPP;
8980 }
8981
8982 return 0;
8983}
8984
8985static int rtl8152_set_tunable(struct net_device *netdev,
8986 const struct ethtool_tunable *tunable,
8987 const void *d)
8988{
8989 struct r8152 *tp = netdev_priv(netdev);
8990 u32 val;
8991
8992 switch (tunable->id) {
8993 case ETHTOOL_RX_COPYBREAK:
8994 val = *(u32 *)d;
8995 if (val < ETH_ZLEN) {
8996 netif_err(tp, rx_err, netdev,
8997 "Invalid rx copy break value\n");
8998 return -EINVAL;
8999 }
9000
9001 if (tp->rx_copybreak != val) {
Hayes Wang5b1d9c12019-11-22 16:21:09 +08009002 if (netdev->flags & IFF_UP) {
9003 mutex_lock(&tp->control);
9004 napi_disable(&tp->napi);
9005 tp->rx_copybreak = val;
9006 napi_enable(&tp->napi);
9007 mutex_unlock(&tp->control);
9008 } else {
9009 tp->rx_copybreak = val;
9010 }
Hayes Wange4a50172019-08-13 11:42:09 +08009011 }
9012 break;
9013 default:
9014 return -EOPNOTSUPP;
9015 }
9016
9017 return 0;
9018}
9019
9020static void rtl8152_get_ringparam(struct net_device *netdev,
Hao Chen74624942021-11-18 20:12:43 +08009021 struct ethtool_ringparam *ring,
9022 struct kernel_ethtool_ringparam *kernel_ring,
9023 struct netlink_ext_ack *extack)
Hayes Wange4a50172019-08-13 11:42:09 +08009024{
9025 struct r8152 *tp = netdev_priv(netdev);
9026
9027 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
9028 ring->rx_pending = tp->rx_pending;
9029}
9030
9031static int rtl8152_set_ringparam(struct net_device *netdev,
Hao Chen74624942021-11-18 20:12:43 +08009032 struct ethtool_ringparam *ring,
9033 struct kernel_ethtool_ringparam *kernel_ring,
9034 struct netlink_ext_ack *extack)
Hayes Wange4a50172019-08-13 11:42:09 +08009035{
9036 struct r8152 *tp = netdev_priv(netdev);
9037
9038 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
9039 return -EINVAL;
9040
9041 if (tp->rx_pending != ring->rx_pending) {
Hayes Wang5b1d9c12019-11-22 16:21:09 +08009042 if (netdev->flags & IFF_UP) {
9043 mutex_lock(&tp->control);
9044 napi_disable(&tp->napi);
9045 tp->rx_pending = ring->rx_pending;
9046 napi_enable(&tp->napi);
9047 mutex_unlock(&tp->control);
9048 } else {
9049 tp->rx_pending = ring->rx_pending;
9050 }
Hayes Wange4a50172019-08-13 11:42:09 +08009051 }
9052
9053 return 0;
9054}
9055
Hayes Wang163d01c52021-06-01 15:37:12 +08009056static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9057{
9058 struct r8152 *tp = netdev_priv(netdev);
9059 u16 bmcr, lcladv, rmtadv;
9060 u8 cap;
9061
9062 if (usb_autopm_get_interface(tp->intf) < 0)
9063 return;
9064
9065 mutex_lock(&tp->control);
9066
9067 bmcr = r8152_mdio_read(tp, MII_BMCR);
9068 lcladv = r8152_mdio_read(tp, MII_ADVERTISE);
9069 rmtadv = r8152_mdio_read(tp, MII_LPA);
9070
9071 mutex_unlock(&tp->control);
9072
9073 usb_autopm_put_interface(tp->intf);
9074
9075 if (!(bmcr & BMCR_ANENABLE)) {
9076 pause->autoneg = 0;
9077 pause->rx_pause = 0;
9078 pause->tx_pause = 0;
9079 return;
9080 }
9081
9082 pause->autoneg = 1;
9083
9084 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
9085
9086 if (cap & FLOW_CTRL_RX)
9087 pause->rx_pause = 1;
9088
9089 if (cap & FLOW_CTRL_TX)
9090 pause->tx_pause = 1;
9091}
9092
9093static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9094{
9095 struct r8152 *tp = netdev_priv(netdev);
9096 u16 old, new1;
9097 u8 cap = 0;
9098 int ret;
9099
9100 ret = usb_autopm_get_interface(tp->intf);
9101 if (ret < 0)
9102 return ret;
9103
9104 mutex_lock(&tp->control);
9105
9106 if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) {
9107 ret = -EINVAL;
9108 goto out;
9109 }
9110
9111 if (pause->rx_pause)
9112 cap |= FLOW_CTRL_RX;
9113
9114 if (pause->tx_pause)
9115 cap |= FLOW_CTRL_TX;
9116
9117 old = r8152_mdio_read(tp, MII_ADVERTISE);
9118 new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap);
9119 if (old != new1)
9120 r8152_mdio_write(tp, MII_ADVERTISE, new1);
9121
9122out:
9123 mutex_unlock(&tp->control);
9124 usb_autopm_put_interface(tp->intf);
9125
9126 return ret;
9127}
9128
Julia Lawall407a4712016-09-01 00:21:22 +02009129static const struct ethtool_ops ops = {
Jakub Kicinskie52a6462020-03-05 17:05:59 -08009130 .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
hayeswangac718b62013-05-02 16:01:25 +00009131 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00009132 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08009133 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08009134 .get_msglevel = rtl8152_get_msglevel,
9135 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08009136 .get_wol = rtl8152_get_wol,
9137 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08009138 .get_strings = rtl8152_get_strings,
9139 .get_sset_count = rtl8152_get_sset_count,
9140 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08009141 .get_coalesce = rtl8152_get_coalesce,
9142 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08009143 .get_eee = rtl_ethtool_get_eee,
9144 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01009145 .get_link_ksettings = rtl8152_get_link_ksettings,
9146 .set_link_ksettings = rtl8152_set_link_ksettings,
Hayes Wange4a50172019-08-13 11:42:09 +08009147 .get_tunable = rtl8152_get_tunable,
9148 .set_tunable = rtl8152_set_tunable,
9149 .get_ringparam = rtl8152_get_ringparam,
9150 .set_ringparam = rtl8152_set_ringparam,
Hayes Wang163d01c52021-06-01 15:37:12 +08009151 .get_pauseparam = rtl8152_get_pauseparam,
9152 .set_pauseparam = rtl8152_set_pauseparam,
hayeswangac718b62013-05-02 16:01:25 +00009153};
9154
9155static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
9156{
9157 struct r8152 *tp = netdev_priv(netdev);
9158 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08009159 int res;
9160
hayeswang68714382014-04-11 17:54:31 +08009161 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9162 return -ENODEV;
9163
hayeswang9a4be1b2014-02-18 21:49:07 +08009164 res = usb_autopm_get_interface(tp->intf);
9165 if (res < 0)
9166 goto out;
hayeswangac718b62013-05-02 16:01:25 +00009167
9168 switch (cmd) {
9169 case SIOCGMIIPHY:
9170 data->phy_id = R8152_PHY_ID; /* Internal PHY */
9171 break;
9172
9173 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08009174 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00009175 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08009176 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00009177 break;
9178
9179 case SIOCSMIIREG:
9180 if (!capable(CAP_NET_ADMIN)) {
9181 res = -EPERM;
9182 break;
9183 }
hayeswangb5403272014-10-09 18:00:26 +08009184 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00009185 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08009186 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00009187 break;
9188
9189 default:
9190 res = -EOPNOTSUPP;
9191 }
9192
hayeswang9a4be1b2014-02-18 21:49:07 +08009193 usb_autopm_put_interface(tp->intf);
9194
9195out:
hayeswangac718b62013-05-02 16:01:25 +00009196 return res;
9197}
9198
hayeswang69b4b7a2014-07-10 10:58:54 +08009199static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
9200{
9201 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08009202 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08009203
9204 switch (tp->version) {
9205 case RTL_VER_01:
9206 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08009207 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04009208 dev->mtu = new_mtu;
9209 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08009210 default:
9211 break;
9212 }
9213
hayeswang396e2e22015-02-12 14:33:47 +08009214 ret = usb_autopm_get_interface(tp->intf);
9215 if (ret < 0)
9216 return ret;
9217
9218 mutex_lock(&tp->control);
9219
hayeswang69b4b7a2014-07-10 10:58:54 +08009220 dev->mtu = new_mtu;
9221
hayeswang210c4f72017-03-20 16:13:44 +08009222 if (netif_running(dev)) {
Hayes Wang67ce1a82021-04-16 16:04:34 +08009223 if (tp->rtl_ops.change_mtu)
9224 tp->rtl_ops.change_mtu(tp);
hayeswang210c4f72017-03-20 16:13:44 +08009225
Hayes Wang67ce1a82021-04-16 16:04:34 +08009226 if (netif_carrier_ok(dev)) {
9227 netif_stop_queue(dev);
9228 napi_disable(&tp->napi);
9229 tasklet_disable(&tp->tx_tl);
9230 tp->rtl_ops.disable(tp);
9231 tp->rtl_ops.enable(tp);
9232 rtl_start_rx(tp);
9233 tasklet_enable(&tp->tx_tl);
9234 napi_enable(&tp->napi);
9235 rtl8152_set_rx_mode(dev);
9236 netif_wake_queue(dev);
9237 }
hayeswang210c4f72017-03-20 16:13:44 +08009238 }
hayeswang396e2e22015-02-12 14:33:47 +08009239
9240 mutex_unlock(&tp->control);
9241
9242 usb_autopm_put_interface(tp->intf);
9243
9244 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08009245}
9246
hayeswangac718b62013-05-02 16:01:25 +00009247static const struct net_device_ops rtl8152_netdev_ops = {
9248 .ndo_open = rtl8152_open,
9249 .ndo_stop = rtl8152_close,
Arnd Bergmanna7605372021-07-27 15:45:13 +02009250 .ndo_eth_ioctl = rtl8152_ioctl,
hayeswangac718b62013-05-02 16:01:25 +00009251 .ndo_start_xmit = rtl8152_start_xmit,
9252 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08009253 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00009254 .ndo_set_rx_mode = rtl8152_set_rx_mode,
9255 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08009256 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00009257 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08009258 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00009259};
9260
hayeswange3fe0b12014-01-02 11:22:39 +08009261static void rtl8152_unload(struct r8152 *tp)
9262{
hayeswang68714382014-04-11 17:54:31 +08009263 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9264 return;
9265
hayeswang00a5e362014-02-18 21:48:59 +08009266 if (tp->version != RTL_VER_01)
9267 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08009268}
9269
hayeswang43779f82014-01-02 11:25:10 +08009270static void rtl8153_unload(struct r8152 *tp)
9271{
hayeswang68714382014-04-11 17:54:31 +08009272 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9273 return;
9274
hayeswang49be1722014-10-01 13:25:11 +08009275 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08009276}
9277
hayeswang65b82d62017-06-15 14:44:03 +08009278static void rtl8153b_unload(struct r8152 *tp)
9279{
9280 if (test_bit(RTL8152_UNPLUG, &tp->flags))
9281 return;
9282
9283 r8153b_power_cut_en(tp, false);
9284}
9285
hayeswang55b65472014-11-06 12:47:39 +08009286static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08009287{
9288 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08009289 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08009290
hayeswang55b65472014-11-06 12:47:39 +08009291 switch (tp->version) {
9292 case RTL_VER_01:
9293 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08009294 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08009295 ops->init = r8152b_init;
9296 ops->enable = rtl8152_enable;
9297 ops->disable = rtl8152_disable;
9298 ops->up = rtl8152_up;
9299 ops->down = rtl8152_down;
9300 ops->unload = rtl8152_unload;
9301 ops->eee_get = r8152_get_eee;
9302 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08009303 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08009304 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08009305 ops->autosuspend_en = rtl_runtime_suspend_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08009306 tp->rx_buf_sz = 16 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08009307 tp->eee_en = true;
9308 tp->eee_adv = MDIO_EEE_100TX;
hayeswang43779f82014-01-02 11:25:10 +08009309 break;
9310
hayeswang55b65472014-11-06 12:47:39 +08009311 case RTL_VER_03:
9312 case RTL_VER_04:
9313 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08009314 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08009315 ops->init = r8153_init;
9316 ops->enable = rtl8153_enable;
9317 ops->disable = rtl8153_disable;
9318 ops->up = rtl8153_up;
9319 ops->down = rtl8153_down;
9320 ops->unload = rtl8153_unload;
9321 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08009322 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08009323 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08009324 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08009325 ops->autosuspend_en = rtl8153_runtime_enable;
Hayes Wang67ce1a82021-04-16 16:04:34 +08009326 ops->change_mtu = rtl8153_change_mtu;
Hayes Wangf91a50d2021-03-19 15:37:21 +08009327 if (tp->udev->speed < USB_SPEED_SUPER)
9328 tp->rx_buf_sz = 16 * 1024;
9329 else
9330 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08009331 tp->eee_en = true;
9332 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswangc81229c2014-01-02 11:22:42 +08009333 break;
9334
hayeswang65b82d62017-06-15 14:44:03 +08009335 case RTL_VER_08:
9336 case RTL_VER_09:
9337 ops->init = r8153b_init;
9338 ops->enable = rtl8153_enable;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08009339 ops->disable = rtl8153_disable;
hayeswang65b82d62017-06-15 14:44:03 +08009340 ops->up = rtl8153b_up;
9341 ops->down = rtl8153b_down;
9342 ops->unload = rtl8153b_unload;
9343 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08009344 ops->eee_set = r8152_set_eee;
hayeswang65b82d62017-06-15 14:44:03 +08009345 ops->in_nway = rtl8153_in_nway;
9346 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
9347 ops->autosuspend_en = rtl8153b_runtime_enable;
Hayes Wang67ce1a82021-04-16 16:04:34 +08009348 ops->change_mtu = rtl8153_change_mtu;
Hayes Wangec5791c2019-08-13 11:42:05 +08009349 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08009350 tp->eee_en = true;
9351 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswang65b82d62017-06-15 14:44:03 +08009352 break;
9353
Hayes Wang195aae32021-04-16 16:04:35 +08009354 case RTL_VER_11:
9355 tp->eee_en = true;
9356 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9357 fallthrough;
9358 case RTL_VER_10:
9359 ops->init = r8156_init;
9360 ops->enable = rtl8156_enable;
9361 ops->disable = rtl8153_disable;
9362 ops->up = rtl8156_up;
9363 ops->down = rtl8156_down;
9364 ops->unload = rtl8153_unload;
9365 ops->eee_get = r8153_get_eee;
9366 ops->eee_set = r8152_set_eee;
9367 ops->in_nway = rtl8153_in_nway;
9368 ops->hw_phy_cfg = r8156_hw_phy_cfg;
9369 ops->autosuspend_en = rtl8156_runtime_enable;
9370 ops->change_mtu = rtl8156_change_mtu;
9371 tp->rx_buf_sz = 48 * 1024;
9372 tp->support_2500full = 1;
9373 break;
9374
9375 case RTL_VER_12:
9376 case RTL_VER_13:
9377 tp->support_2500full = 1;
9378 fallthrough;
9379 case RTL_VER_15:
9380 tp->eee_en = true;
9381 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9382 ops->init = r8156b_init;
9383 ops->enable = rtl8156b_enable;
9384 ops->disable = rtl8153_disable;
9385 ops->up = rtl8156_up;
9386 ops->down = rtl8156_down;
9387 ops->unload = rtl8153_unload;
9388 ops->eee_get = r8153_get_eee;
9389 ops->eee_set = r8152_set_eee;
9390 ops->in_nway = rtl8153_in_nway;
9391 ops->hw_phy_cfg = r8156b_hw_phy_cfg;
9392 ops->autosuspend_en = rtl8156_runtime_enable;
9393 ops->change_mtu = rtl8156_change_mtu;
9394 tp->rx_buf_sz = 48 * 1024;
9395 break;
9396
9397 case RTL_VER_14:
9398 ops->init = r8153c_init;
9399 ops->enable = rtl8153_enable;
9400 ops->disable = rtl8153_disable;
9401 ops->up = rtl8153c_up;
9402 ops->down = rtl8153b_down;
9403 ops->unload = rtl8153_unload;
9404 ops->eee_get = r8153_get_eee;
9405 ops->eee_set = r8152_set_eee;
9406 ops->in_nway = rtl8153_in_nway;
9407 ops->hw_phy_cfg = r8153c_hw_phy_cfg;
9408 ops->autosuspend_en = rtl8153c_runtime_enable;
9409 ops->change_mtu = rtl8153c_change_mtu;
9410 tp->rx_buf_sz = 32 * 1024;
9411 tp->eee_en = true;
9412 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
9413 break;
9414
hayeswangc81229c2014-01-02 11:22:42 +08009415 default:
hayeswang55b65472014-11-06 12:47:39 +08009416 ret = -ENODEV;
Hayes Wang156c3202021-02-19 17:04:42 +08009417 dev_err(&tp->intf->dev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08009418 break;
9419 }
9420
9421 return ret;
9422}
9423
Hayes Wang9370f2d2019-10-16 11:02:42 +08009424#define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw"
9425#define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw"
9426#define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw"
9427#define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw"
Hayes Wang195aae32021-04-16 16:04:35 +08009428#define FIRMWARE_8153C_1 "rtl_nic/rtl8153c-1.fw"
Hayes Wang4a51b0e2021-04-16 16:04:36 +08009429#define FIRMWARE_8156A_2 "rtl_nic/rtl8156a-2.fw"
9430#define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw"
Hayes Wang9370f2d2019-10-16 11:02:42 +08009431
9432MODULE_FIRMWARE(FIRMWARE_8153A_2);
9433MODULE_FIRMWARE(FIRMWARE_8153A_3);
9434MODULE_FIRMWARE(FIRMWARE_8153A_4);
9435MODULE_FIRMWARE(FIRMWARE_8153B_2);
Hayes Wang195aae32021-04-16 16:04:35 +08009436MODULE_FIRMWARE(FIRMWARE_8153C_1);
Hayes Wang4a51b0e2021-04-16 16:04:36 +08009437MODULE_FIRMWARE(FIRMWARE_8156A_2);
9438MODULE_FIRMWARE(FIRMWARE_8156B_2);
Hayes Wang9370f2d2019-10-16 11:02:42 +08009439
9440static int rtl_fw_init(struct r8152 *tp)
9441{
9442 struct rtl_fw *rtl_fw = &tp->rtl_fw;
9443
9444 switch (tp->version) {
9445 case RTL_VER_04:
9446 rtl_fw->fw_name = FIRMWARE_8153A_2;
9447 rtl_fw->pre_fw = r8153_pre_firmware_1;
9448 rtl_fw->post_fw = r8153_post_firmware_1;
9449 break;
9450 case RTL_VER_05:
9451 rtl_fw->fw_name = FIRMWARE_8153A_3;
9452 rtl_fw->pre_fw = r8153_pre_firmware_2;
9453 rtl_fw->post_fw = r8153_post_firmware_2;
9454 break;
9455 case RTL_VER_06:
9456 rtl_fw->fw_name = FIRMWARE_8153A_4;
9457 rtl_fw->post_fw = r8153_post_firmware_3;
9458 break;
9459 case RTL_VER_09:
9460 rtl_fw->fw_name = FIRMWARE_8153B_2;
9461 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9462 rtl_fw->post_fw = r8153b_post_firmware_1;
9463 break;
Hayes Wang4a51b0e2021-04-16 16:04:36 +08009464 case RTL_VER_11:
9465 rtl_fw->fw_name = FIRMWARE_8156A_2;
9466 rtl_fw->post_fw = r8156a_post_firmware_1;
9467 break;
9468 case RTL_VER_13:
9469 case RTL_VER_15:
9470 rtl_fw->fw_name = FIRMWARE_8156B_2;
9471 break;
Hayes Wang195aae32021-04-16 16:04:35 +08009472 case RTL_VER_14:
9473 rtl_fw->fw_name = FIRMWARE_8153C_1;
9474 rtl_fw->pre_fw = r8153b_pre_firmware_1;
9475 rtl_fw->post_fw = r8153c_post_firmware_1;
9476 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08009477 default:
9478 break;
9479 }
9480
9481 return 0;
9482}
9483
Hayes Wangc1aedf02020-11-04 10:19:22 +08009484u8 rtl8152_get_version(struct usb_interface *intf)
hayeswang33928ee2017-03-17 11:20:13 +08009485{
9486 struct usb_device *udev = interface_to_usbdev(intf);
9487 u32 ocp_data = 0;
9488 __le32 *tmp;
9489 u8 version;
9490 int ret;
9491
9492 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
9493 if (!tmp)
9494 return 0;
9495
9496 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
9497 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
9498 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
9499 if (ret > 0)
9500 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
9501
9502 kfree(tmp);
9503
9504 switch (ocp_data) {
9505 case 0x4c00:
9506 version = RTL_VER_01;
9507 break;
9508 case 0x4c10:
9509 version = RTL_VER_02;
9510 break;
9511 case 0x5c00:
9512 version = RTL_VER_03;
9513 break;
9514 case 0x5c10:
9515 version = RTL_VER_04;
9516 break;
9517 case 0x5c20:
9518 version = RTL_VER_05;
9519 break;
9520 case 0x5c30:
9521 version = RTL_VER_06;
9522 break;
hayeswangc27b32c2017-06-15 14:44:02 +08009523 case 0x4800:
9524 version = RTL_VER_07;
9525 break;
hayeswang65b82d62017-06-15 14:44:03 +08009526 case 0x6000:
9527 version = RTL_VER_08;
9528 break;
9529 case 0x6010:
9530 version = RTL_VER_09;
9531 break;
Hayes Wang195aae32021-04-16 16:04:35 +08009532 case 0x7010:
9533 version = RTL_TEST_01;
9534 break;
9535 case 0x7020:
9536 version = RTL_VER_10;
9537 break;
9538 case 0x7030:
9539 version = RTL_VER_11;
9540 break;
9541 case 0x7400:
9542 version = RTL_VER_12;
9543 break;
9544 case 0x7410:
9545 version = RTL_VER_13;
9546 break;
9547 case 0x6400:
9548 version = RTL_VER_14;
9549 break;
9550 case 0x7420:
9551 version = RTL_VER_15;
9552 break;
hayeswang33928ee2017-03-17 11:20:13 +08009553 default:
9554 version = RTL_VER_UNKNOWN;
9555 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
9556 break;
9557 }
9558
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02009559 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
9560
hayeswang33928ee2017-03-17 11:20:13 +08009561 return version;
9562}
Hayes Wangc1aedf02020-11-04 10:19:22 +08009563EXPORT_SYMBOL_GPL(rtl8152_get_version);
hayeswang33928ee2017-03-17 11:20:13 +08009564
hayeswangac718b62013-05-02 16:01:25 +00009565static int rtl8152_probe(struct usb_interface *intf,
9566 const struct usb_device_id *id)
9567{
9568 struct usb_device *udev = interface_to_usbdev(intf);
Hayes Wangc1aedf02020-11-04 10:19:22 +08009569 u8 version = rtl8152_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00009570 struct r8152 *tp;
9571 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08009572 int ret;
hayeswangac718b62013-05-02 16:01:25 +00009573
hayeswang33928ee2017-03-17 11:20:13 +08009574 if (version == RTL_VER_UNKNOWN)
9575 return -ENODEV;
9576
Hayes Wangc2198942021-04-16 16:04:37 +08009577 if (!rtl_vendor_mode(intf))
hayeswang10c32712014-03-04 20:47:48 +08009578 return -ENODEV;
hayeswang10c32712014-03-04 20:47:48 +08009579
9580 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00009581 netdev = alloc_etherdev(sizeof(struct r8152));
9582 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08009583 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00009584 return -ENOMEM;
9585 }
9586
hayeswangebc2ec482013-08-14 20:54:38 +08009587 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00009588 tp = netdev_priv(netdev);
9589 tp->msg_enable = 0x7FFF;
9590
hayeswange3ad4122014-01-06 17:08:42 +08009591 tp->udev = udev;
9592 tp->netdev = netdev;
9593 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08009594 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08009595
Hayes Wangb67fda92021-06-17 18:00:15 +08009596 tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0);
9597 tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0);
9598 tp->pipe_in = usb_rcvbulkpipe(udev, 1);
9599 tp->pipe_out = usb_sndbulkpipe(udev, 2);
9600 tp->pipe_intr = usb_rcvintpipe(udev, 3);
9601
hayeswang33928ee2017-03-17 11:20:13 +08009602 switch (version) {
9603 case RTL_VER_01:
9604 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08009605 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08009606 tp->mii.supports_gmii = 0;
9607 break;
9608 default:
9609 tp->mii.supports_gmii = 1;
9610 break;
9611 }
9612
hayeswang55b65472014-11-06 12:47:39 +08009613 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08009614 if (ret)
9615 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08009616
Hayes Wang9370f2d2019-10-16 11:02:42 +08009617 rtl_fw_init(tp);
9618
hayeswangb5403272014-10-09 18:00:26 +08009619 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00009620 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08009621 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
Emil Renner Berthingf3163f12021-01-31 00:47:29 +01009622 tasklet_setup(&tp->tx_tl, bottom_half);
Hayes Wangd2187f82019-08-19 14:40:36 +08009623 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00009624
hayeswangac718b62013-05-02 16:01:25 +00009625 netdev->netdev_ops = &rtl8152_netdev_ops;
9626 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08009627
hayeswang60c89072014-03-07 11:04:39 +08009628 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08009629 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08009630 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
9631 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08009632 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08009633 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08009634 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08009635 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08009636 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
9637 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
9638 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08009639
hayeswang19c0f402017-01-11 16:25:34 +08009640 if (tp->version == RTL_VER_01) {
9641 netdev->features &= ~NETIF_F_RXCSUM;
9642 netdev->hw_features &= ~NETIF_F_RXCSUM;
9643 }
9644
Aaron Ma00fcf8c2022-01-05 23:51:02 +08009645 if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO) {
9646 switch (le16_to_cpu(udev->descriptor.idProduct)) {
9647 case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2:
9648 case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2:
9649 tp->lenovo_macpassthru = 1;
9650 }
Kai-Heng Fengb4b771f2020-02-04 13:33:13 +08009651 }
Kai-Heng Feng96477222019-11-05 19:24:52 +08009652
Kai-Heng Feng176eb612018-08-20 12:43:51 +08009653 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
Prashant Malani151ea092019-10-02 14:09:33 -07009654 (!strcmp(udev->serial, "000001000000") ||
9655 !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08009656 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
Hayes Wang9c680112021-04-24 14:09:03 +08009657 tp->dell_tb_rx_agg_bug = 1;
Kai-Heng Feng0b165512018-01-16 16:46:27 +08009658 }
9659
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00009660 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08009661 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00009662
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04009663 /* MTU range: 68 - 1500 or 9194 */
9664 netdev->min_mtu = ETH_MIN_MTU;
9665 switch (tp->version) {
Hayes Wang195aae32021-04-16 16:04:35 +08009666 case RTL_VER_03:
9667 case RTL_VER_04:
9668 case RTL_VER_05:
9669 case RTL_VER_06:
9670 case RTL_VER_08:
9671 case RTL_VER_09:
9672 case RTL_VER_14:
9673 netdev->max_mtu = size_to_mtu(9 * 1024);
9674 break;
9675 case RTL_VER_10:
9676 case RTL_VER_11:
9677 netdev->max_mtu = size_to_mtu(15 * 1024);
9678 break;
9679 case RTL_VER_12:
9680 case RTL_VER_13:
9681 case RTL_VER_15:
9682 netdev->max_mtu = size_to_mtu(16 * 1024);
9683 break;
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04009684 case RTL_VER_01:
9685 case RTL_VER_02:
Hayes Wang195aae32021-04-16 16:04:35 +08009686 case RTL_VER_07:
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04009687 default:
Hayes Wang195aae32021-04-16 16:04:35 +08009688 netdev->max_mtu = ETH_DATA_LEN;
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04009689 break;
9690 }
9691
hayeswangac718b62013-05-02 16:01:25 +00009692 tp->mii.dev = netdev;
9693 tp->mii.mdio_read = read_mii_word;
9694 tp->mii.mdio_write = write_mii_word;
9695 tp->mii.phy_id_mask = 0x3f;
9696 tp->mii.reg_num_mask = 0x1f;
9697 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00009698
hayeswangaa7e26b2016-06-13 17:49:38 +08009699 tp->autoneg = AUTONEG_ENABLE;
Hayes Wang771efed2019-09-02 19:52:28 +08009700 tp->speed = SPEED_100;
9701 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
9702 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
9703 if (tp->mii.supports_gmii) {
Hayes Wang195aae32021-04-16 16:04:35 +08009704 if (tp->support_2500full &&
9705 tp->udev->speed >= USB_SPEED_SUPER) {
9706 tp->speed = SPEED_2500;
9707 tp->advertising |= RTL_ADVERTISED_2500_FULL;
9708 } else {
9709 tp->speed = SPEED_1000;
9710 }
Hayes Wang771efed2019-09-02 19:52:28 +08009711 tp->advertising |= RTL_ADVERTISED_1000_FULL;
9712 }
hayeswangaa7e26b2016-06-13 17:49:38 +08009713 tp->duplex = DUPLEX_FULL;
9714
Hayes Wange4a50172019-08-13 11:42:09 +08009715 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
9716 tp->rx_pending = 10 * RTL8152_MAX_RX;
9717
hayeswang9a4be1b2014-02-18 21:49:07 +08009718 intf->needs_remote_wakeup = 1;
9719
Hayes Wang9583a362020-01-22 16:02:07 +08009720 if (!rtl_can_wakeup(tp))
9721 __rtl_set_wol(tp, 0);
9722 else
9723 tp->saved_wolopts = __rtl_get_wol(tp);
9724
hayeswangc81229c2014-01-02 11:22:42 +08009725 tp->rtl_ops.init(tp);
Hayes Wang9370f2d2019-10-16 11:02:42 +08009726#if IS_BUILTIN(CONFIG_USB_RTL8152)
9727 /* Retry in case request_firmware() is not ready yet. */
9728 tp->rtl_fw.retry = true;
9729#endif
hayeswanga028a9e2016-06-13 17:49:36 +08009730 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
Takashi Iwai776ac632021-07-14 19:00:22 +02009731 set_ethernet_addr(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00009732
hayeswangac718b62013-05-02 16:01:25 +00009733 usb_set_intfdata(intf, tp);
Hayes Wang195aae32021-04-16 16:04:35 +08009734
9735 if (tp->support_2500full)
9736 netif_napi_add(netdev, &tp->napi, r8152_poll, 256);
9737 else
9738 netif_napi_add(netdev, &tp->napi, r8152_poll, 64);
hayeswangac718b62013-05-02 16:01:25 +00009739
hayeswangebc2ec482013-08-14 20:54:38 +08009740 ret = register_netdev(netdev);
9741 if (ret != 0) {
Hayes Wang156c3202021-02-19 17:04:42 +08009742 dev_err(&intf->dev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08009743 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00009744 }
9745
hayeswang21ff2e82014-02-18 21:49:06 +08009746 if (tp->saved_wolopts)
9747 device_set_wakeup_enable(&udev->dev, true);
9748 else
9749 device_set_wakeup_enable(&udev->dev, false);
9750
Hayes Wang4a8deae2014-01-07 11:18:22 +08009751 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00009752
9753 return 0;
9754
hayeswangac718b62013-05-02 16:01:25 +00009755out1:
Hayes Wangd2187f82019-08-19 14:40:36 +08009756 tasklet_kill(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08009757 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00009758out:
9759 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08009760 return ret;
hayeswangac718b62013-05-02 16:01:25 +00009761}
9762
hayeswangac718b62013-05-02 16:01:25 +00009763static void rtl8152_disconnect(struct usb_interface *intf)
9764{
9765 struct r8152 *tp = usb_get_intfdata(intf);
9766
9767 usb_set_intfdata(intf, NULL);
9768 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08009769 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08009770
hayeswangac718b62013-05-02 16:01:25 +00009771 unregister_netdev(tp->netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08009772 tasklet_kill(&tp->tx_tl);
hayeswanga028a9e2016-06-13 17:49:36 +08009773 cancel_delayed_work_sync(&tp->hw_phy_work);
Hayes Wang195aae32021-04-16 16:04:35 +08009774 if (tp->rtl_ops.unload)
9775 tp->rtl_ops.unload(tp);
Hayes Wang9370f2d2019-10-16 11:02:42 +08009776 rtl8152_release_firmware(tp);
hayeswangac718b62013-05-02 16:01:25 +00009777 free_netdev(tp->netdev);
9778 }
9779}
9780
Hayes Wang55319ee2021-04-23 17:44:55 +08009781#define REALTEK_USB_DEVICE(vend, prod) { \
9782 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC), \
hayeswangd9a28c52014-12-04 10:43:11 +08009783}, \
9784{ \
Hayes Wang55319ee2021-04-23 17:44:55 +08009785 USB_DEVICE_AND_INTERFACE_INFO(vend, prod, USB_CLASS_COMM, \
9786 USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), \
9787}
hayeswangd9a28c52014-12-04 10:43:11 +08009788
hayeswangac718b62013-05-02 16:01:25 +00009789/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05309790static const struct usb_device_id rtl8152_table[] = {
Hayes Wang195aae32021-04-16 16:04:35 +08009791 /* Realtek */
Hayes Wang55319ee2021-04-23 17:44:55 +08009792 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050),
9793 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8053),
9794 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152),
9795 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153),
9796 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8155),
9797 REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8156),
Hayes Wang195aae32021-04-16 16:04:35 +08009798
9799 /* Microsoft */
Hayes Wang55319ee2021-04-23 17:44:55 +08009800 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab),
9801 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6),
9802 REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927),
9803 REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101),
9804 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f),
9805 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062),
9806 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069),
9807 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082),
9808 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205),
9809 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c),
9810 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214),
9811 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x721e),
9812 REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387),
9813 REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041),
9814 REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff),
9815 REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601),
hayeswangac718b62013-05-02 16:01:25 +00009816 {}
9817};
9818
9819MODULE_DEVICE_TABLE(usb, rtl8152_table);
9820
9821static struct usb_driver rtl8152_driver = {
9822 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08009823 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00009824 .probe = rtl8152_probe,
9825 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00009826 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08009827 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08009828 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08009829 .pre_reset = rtl8152_pre_reset,
9830 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08009831 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08009832 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00009833};
9834
Sachin Kamatb4236daa2013-05-16 17:48:08 +00009835module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00009836
9837MODULE_AUTHOR(DRIVER_AUTHOR);
9838MODULE_DESCRIPTION(DRIVER_DESC);
9839MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07009840MODULE_VERSION(DRIVER_VERSION);