blob: 8ecb41b2df099cbfd16dbde6869222e858dfd3d1 [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
hayeswangc7de7de2014-01-15 10:42:16 +08002 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 *
8 */
9
hayeswangac718b62013-05-02 16:01:25 +000010#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
16#include <linux/ethtool.h>
17#include <linux/usb.h>
18#include <linux/crc32.h>
19#include <linux/if_vlan.h>
20#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080021#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080022#include <linux/ip.h>
23#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000024
25/* Version Information */
hayeswang21ff2e82014-02-18 21:49:06 +080026#define DRIVER_VERSION "v1.05.0 (2014/02/18)"
hayeswangac718b62013-05-02 16:01:25 +000027#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080028#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000029#define MODULENAME "r8152"
30
31#define R8152_PHY_ID 32
32
33#define PLA_IDR 0xc000
34#define PLA_RCR 0xc010
35#define PLA_RMS 0xc016
36#define PLA_RXFIFO_CTRL0 0xc0a0
37#define PLA_RXFIFO_CTRL1 0xc0a4
38#define PLA_RXFIFO_CTRL2 0xc0a8
39#define PLA_FMC 0xc0b4
40#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080041#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000042#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080043#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000044#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080045#define PLA_TEREDO_TIMER 0xd2cc
46#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_LEDSEL 0xdd90
48#define PLA_LED_FEATURE 0xdd92
49#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_GPHY_INTR_IMR 0xe022
52#define PLA_EEE_CR 0xe040
53#define PLA_EEEP_CR 0xe080
54#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_MAC_PWR_CTRL2 0xe0ca
56#define PLA_MAC_PWR_CTRL3 0xe0cc
57#define PLA_MAC_PWR_CTRL4 0xe0ce
58#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000059#define PLA_TCR0 0xe610
60#define PLA_TCR1 0xe612
61#define PLA_TXFIFO_CTRL 0xe618
62#define PLA_RSTTELLY 0xe800
63#define PLA_CR 0xe813
64#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080065#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
66#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000067#define PLA_CONFIG5 0xe822
68#define PLA_PHY_PWR 0xe84c
69#define PLA_OOB_CTRL 0xe84f
70#define PLA_CPCR 0xe854
71#define PLA_MISC_0 0xe858
72#define PLA_MISC_1 0xe85a
73#define PLA_OCP_GPHY_BASE 0xe86c
74#define PLA_TELLYCNT 0xe890
75#define PLA_SFF_STS_7 0xe8de
76#define PLA_PHYSTATUS 0xe908
77#define PLA_BP_BA 0xfc26
78#define PLA_BP_0 0xfc28
79#define PLA_BP_1 0xfc2a
80#define PLA_BP_2 0xfc2c
81#define PLA_BP_3 0xfc2e
82#define PLA_BP_4 0xfc30
83#define PLA_BP_5 0xfc32
84#define PLA_BP_6 0xfc34
85#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080086#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000087
hayeswang43779f82014-01-02 11:25:10 +080088#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000089#define USB_DEV_STAT 0xb808
90#define USB_USB_CTRL 0xd406
91#define USB_PHY_CTRL 0xd408
92#define USB_TX_AGG 0xd40a
93#define USB_RX_BUF_TH 0xd40c
94#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +080095#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +000096#define USB_PM_CTRL_STATUS 0xd432
97#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +080098#define USB_TOLERANCE 0xd490
99#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +0000100#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800101#define USB_MISC_0 0xd81a
102#define USB_POWER_CUT 0xd80a
103#define USB_AFE_CTRL2 0xd824
104#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000105#define USB_BP_BA 0xfc26
106#define USB_BP_0 0xfc28
107#define USB_BP_1 0xfc2a
108#define USB_BP_2 0xfc2c
109#define USB_BP_3 0xfc2e
110#define USB_BP_4 0xfc30
111#define USB_BP_5 0xfc32
112#define USB_BP_6 0xfc34
113#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800114#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000115
116/* OCP Registers */
117#define OCP_ALDPS_CONFIG 0x2010
118#define OCP_EEE_CONFIG1 0x2080
119#define OCP_EEE_CONFIG2 0x2092
120#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800121#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000122#define OCP_EEE_AR 0xa41a
123#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800124#define OCP_PHY_STATUS 0xa420
125#define OCP_POWER_CFG 0xa430
126#define OCP_EEE_CFG 0xa432
127#define OCP_SRAM_ADDR 0xa436
128#define OCP_SRAM_DATA 0xa438
129#define OCP_DOWN_SPEED 0xa442
130#define OCP_EEE_CFG2 0xa5d0
131#define OCP_ADC_CFG 0xbc06
132
133/* SRAM Register */
134#define SRAM_LPF_CFG 0x8012
135#define SRAM_10M_AMP1 0x8080
136#define SRAM_10M_AMP2 0x8082
137#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000138
139/* PLA_RCR */
140#define RCR_AAP 0x00000001
141#define RCR_APM 0x00000002
142#define RCR_AM 0x00000004
143#define RCR_AB 0x00000008
144#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
145
146/* PLA_RXFIFO_CTRL0 */
147#define RXFIFO_THR1_NORMAL 0x00080002
148#define RXFIFO_THR1_OOB 0x01800003
149
150/* PLA_RXFIFO_CTRL1 */
151#define RXFIFO_THR2_FULL 0x00000060
152#define RXFIFO_THR2_HIGH 0x00000038
153#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800154#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000155
156/* PLA_RXFIFO_CTRL2 */
157#define RXFIFO_THR3_FULL 0x00000078
158#define RXFIFO_THR3_HIGH 0x00000048
159#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800160#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000161
162/* PLA_TXFIFO_CTRL */
163#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800164#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000165
166/* PLA_FMC */
167#define FMC_FCR_MCU_EN 0x0001
168
169/* PLA_EEEP_CR */
170#define EEEP_CR_EEEP_TX 0x0002
171
hayeswang43779f82014-01-02 11:25:10 +0800172/* PLA_WDT6_CTRL */
173#define WDT6_SET_MODE 0x0010
174
hayeswangac718b62013-05-02 16:01:25 +0000175/* PLA_TCR0 */
176#define TCR0_TX_EMPTY 0x0800
177#define TCR0_AUTO_FIFO 0x0080
178
179/* PLA_TCR1 */
180#define VERSION_MASK 0x7cf0
181
182/* PLA_CR */
183#define CR_RST 0x10
184#define CR_RE 0x08
185#define CR_TE 0x04
186
187/* PLA_CRWECR */
188#define CRWECR_NORAML 0x00
189#define CRWECR_CONFIG 0xc0
190
191/* PLA_OOB_CTRL */
192#define NOW_IS_OOB 0x80
193#define TXFIFO_EMPTY 0x20
194#define RXFIFO_EMPTY 0x10
195#define LINK_LIST_READY 0x02
196#define DIS_MCU_CLROOB 0x01
197#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
198
199/* PLA_MISC_1 */
200#define RXDY_GATED_EN 0x0008
201
202/* PLA_SFF_STS_7 */
203#define RE_INIT_LL 0x8000
204#define MCU_BORW_EN 0x4000
205
206/* PLA_CPCR */
207#define CPCR_RX_VLAN 0x0040
208
209/* PLA_CFG_WOL */
210#define MAGIC_EN 0x0001
211
hayeswang43779f82014-01-02 11:25:10 +0800212/* PLA_TEREDO_CFG */
213#define TEREDO_SEL 0x8000
214#define TEREDO_WAKE_MASK 0x7f00
215#define TEREDO_RS_EVENT_MASK 0x00fe
216#define OOB_TEREDO_EN 0x0001
217
hayeswangac718b62013-05-02 16:01:25 +0000218/* PAL_BDC_CR */
219#define ALDPS_PROXY_MODE 0x0001
220
hayeswang21ff2e82014-02-18 21:49:06 +0800221/* PLA_CONFIG34 */
222#define LINK_ON_WAKE_EN 0x0010
223#define LINK_OFF_WAKE_EN 0x0008
224
hayeswangac718b62013-05-02 16:01:25 +0000225/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800226#define BWF_EN 0x0040
227#define MWF_EN 0x0020
228#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000229#define LAN_WAKE_EN 0x0002
230
231/* PLA_LED_FEATURE */
232#define LED_MODE_MASK 0x0700
233
234/* PLA_PHY_PWR */
235#define TX_10M_IDLE_EN 0x0080
236#define PFM_PWM_SWITCH 0x0040
237
238/* PLA_MAC_PWR_CTRL */
239#define D3_CLK_GATED_EN 0x00004000
240#define MCU_CLK_RATIO 0x07010f07
241#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800242#define ALDPS_SPDWN_RATIO 0x0f87
243
244/* PLA_MAC_PWR_CTRL2 */
245#define EEE_SPDWN_RATIO 0x8007
246
247/* PLA_MAC_PWR_CTRL3 */
248#define PKT_AVAIL_SPDWN_EN 0x0100
249#define SUSPEND_SPDWN_EN 0x0004
250#define U1U2_SPDWN_EN 0x0002
251#define L1_SPDWN_EN 0x0001
252
253/* PLA_MAC_PWR_CTRL4 */
254#define PWRSAVE_SPDWN_EN 0x1000
255#define RXDV_SPDWN_EN 0x0800
256#define TX10MIDLE_EN 0x0100
257#define TP100_SPDWN_EN 0x0020
258#define TP500_SPDWN_EN 0x0010
259#define TP1000_SPDWN_EN 0x0008
260#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000261
262/* PLA_GPHY_INTR_IMR */
263#define GPHY_STS_MSK 0x0001
264#define SPEED_DOWN_MSK 0x0002
265#define SPDWN_RXDV_MSK 0x0004
266#define SPDWN_LINKCHG_MSK 0x0008
267
268/* PLA_PHYAR */
269#define PHYAR_FLAG 0x80000000
270
271/* PLA_EEE_CR */
272#define EEE_RX_EN 0x0001
273#define EEE_TX_EN 0x0002
274
hayeswang43779f82014-01-02 11:25:10 +0800275/* PLA_BOOT_CTRL */
276#define AUTOLOAD_DONE 0x0002
277
hayeswangac718b62013-05-02 16:01:25 +0000278/* USB_DEV_STAT */
279#define STAT_SPEED_MASK 0x0006
280#define STAT_SPEED_HIGH 0x0000
281#define STAT_SPEED_FULL 0x0001
282
283/* USB_TX_AGG */
284#define TX_AGG_MAX_THRESHOLD 0x03
285
286/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800287#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800288#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800289#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000290
291/* USB_TX_DMA */
292#define TEST_MODE_DISABLE 0x00000001
293#define TX_SIZE_ADJUST1 0x00000100
294
295/* USB_UPS_CTRL */
296#define POWER_CUT 0x0100
297
298/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800299#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000300
301/* USB_USB_CTRL */
302#define RX_AGG_DISABLE 0x0010
303
hayeswang43779f82014-01-02 11:25:10 +0800304/* USB_U2P3_CTRL */
305#define U2P3_ENABLE 0x0001
306
307/* USB_POWER_CUT */
308#define PWR_EN 0x0001
309#define PHASE2_EN 0x0008
310
311/* USB_MISC_0 */
312#define PCUT_STATUS 0x0001
313
314/* USB_RX_EARLY_AGG */
315#define EARLY_AGG_SUPPER 0x0e832981
316#define EARLY_AGG_HIGH 0x0e837a12
317#define EARLY_AGG_SLOW 0x0e83ffff
318
319/* USB_WDT11_CTRL */
320#define TIMER11_EN 0x0001
321
322/* USB_LPM_CTRL */
323#define LPM_TIMER_MASK 0x0c
324#define LPM_TIMER_500MS 0x04 /* 500 ms */
325#define LPM_TIMER_500US 0x0c /* 500 us */
326
327/* USB_AFE_CTRL2 */
328#define SEN_VAL_MASK 0xf800
329#define SEN_VAL_NORMAL 0xa000
330#define SEL_RXIDLE 0x0100
331
hayeswangac718b62013-05-02 16:01:25 +0000332/* OCP_ALDPS_CONFIG */
333#define ENPWRSAVE 0x8000
334#define ENPDNPS 0x0200
335#define LINKENA 0x0100
336#define DIS_SDSAVE 0x0010
337
hayeswang43779f82014-01-02 11:25:10 +0800338/* OCP_PHY_STATUS */
339#define PHY_STAT_MASK 0x0007
340#define PHY_STAT_LAN_ON 3
341#define PHY_STAT_PWRDN 5
342
343/* OCP_POWER_CFG */
344#define EEE_CLKDIV_EN 0x8000
345#define EN_ALDPS 0x0004
346#define EN_10M_PLLOFF 0x0001
347
hayeswangac718b62013-05-02 16:01:25 +0000348/* OCP_EEE_CONFIG1 */
349#define RG_TXLPI_MSK_HFDUP 0x8000
350#define RG_MATCLR_EN 0x4000
351#define EEE_10_CAP 0x2000
352#define EEE_NWAY_EN 0x1000
353#define TX_QUIET_EN 0x0200
354#define RX_QUIET_EN 0x0100
355#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
356#define RG_RXLPI_MSK_HFDUP 0x0008
357#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
358
359/* OCP_EEE_CONFIG2 */
360#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
361#define RG_DACQUIET_EN 0x0400
362#define RG_LDVQUIET_EN 0x0200
363#define RG_CKRSEL 0x0020
364#define RG_EEEPRG_EN 0x0010
365
366/* OCP_EEE_CONFIG3 */
367#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
368#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
369#define MSK_PH 0x0006 /* bit 0 ~ 3 */
370
371/* OCP_EEE_AR */
372/* bit[15:14] function */
373#define FUN_ADDR 0x0000
374#define FUN_DATA 0x4000
375/* bit[4:0] device addr */
376#define DEVICE_ADDR 0x0007
377
378/* OCP_EEE_DATA */
379#define EEE_ADDR 0x003C
380#define EEE_DATA 0x0002
381
hayeswang43779f82014-01-02 11:25:10 +0800382/* OCP_EEE_CFG */
383#define CTAP_SHORT_EN 0x0040
384#define EEE10_EN 0x0010
385
386/* OCP_DOWN_SPEED */
387#define EN_10M_BGOFF 0x0080
388
389/* OCP_EEE_CFG2 */
390#define MY1000_EEE 0x0004
391#define MY100_EEE 0x0002
392
393/* OCP_ADC_CFG */
394#define CKADSEL_L 0x0100
395#define ADC_EN 0x0080
396#define EN_EMI_L 0x0040
397
398/* SRAM_LPF_CFG */
399#define LPF_AUTO_TUNE 0x8000
400
401/* SRAM_10M_AMP1 */
402#define GDAC_IB_UPALL 0x0008
403
404/* SRAM_10M_AMP2 */
405#define AMP_DN 0x0200
406
407/* SRAM_IMPEDANCE */
408#define RX_DRIVING_MASK 0x6000
409
hayeswangac718b62013-05-02 16:01:25 +0000410enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800411 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000412 _100bps = 0x08,
413 _10bps = 0x04,
414 LINK_STATUS = 0x02,
415 FULL_DUP = 0x01,
416};
417
hayeswangebc2ec482013-08-14 20:54:38 +0800418#define RTL8152_MAX_TX 10
419#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800420#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800421#define CRC_SIZE 4
422#define TX_ALIGN 4
423#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800424
425#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800426
hayeswangac718b62013-05-02 16:01:25 +0000427#define RTL8152_REQT_READ 0xc0
428#define RTL8152_REQT_WRITE 0x40
429#define RTL8152_REQ_GET_REGS 0x05
430#define RTL8152_REQ_SET_REGS 0x05
431
432#define BYTE_EN_DWORD 0xff
433#define BYTE_EN_WORD 0x33
434#define BYTE_EN_BYTE 0x11
435#define BYTE_EN_SIX_BYTES 0x3f
436#define BYTE_EN_START_MASK 0x0f
437#define BYTE_EN_END_MASK 0xf0
438
439#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
440#define RTL8152_TX_TIMEOUT (HZ)
441
442/* rtl8152 flags */
443enum rtl8152_flags {
444 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000445 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800446 WORK_ENABLE,
447 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800448 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800449 PHY_RESET,
hayeswangac718b62013-05-02 16:01:25 +0000450};
451
452/* Define these values to match your device */
453#define VENDOR_ID_REALTEK 0x0bda
454#define PRODUCT_ID_RTL8152 0x8152
hayeswang43779f82014-01-02 11:25:10 +0800455#define PRODUCT_ID_RTL8153 0x8153
456
457#define VENDOR_ID_SAMSUNG 0x04e8
458#define PRODUCT_ID_SAMSUNG 0xa101
hayeswangac718b62013-05-02 16:01:25 +0000459
460#define MCU_TYPE_PLA 0x0100
461#define MCU_TYPE_USB 0x0000
462
hayeswangc7de7de2014-01-15 10:42:16 +0800463#define REALTEK_USB_DEVICE(vend, prod) \
464 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
465
hayeswangac718b62013-05-02 16:01:25 +0000466struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800467 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000468#define RX_LEN_MASK 0x7fff
hayeswang500b6d72013-11-20 17:30:57 +0800469 __le32 opts2;
470 __le32 opts3;
471 __le32 opts4;
472 __le32 opts5;
473 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000474};
475
476struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800477 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000478#define TX_FS (1 << 31) /* First segment of a packet */
479#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800480#define TX_LEN_MASK 0x3ffff
481
hayeswang500b6d72013-11-20 17:30:57 +0800482 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800483#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
484#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
485#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
486#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000487};
488
hayeswangdff4e8a2013-08-16 16:09:33 +0800489struct r8152;
490
hayeswangebc2ec482013-08-14 20:54:38 +0800491struct rx_agg {
492 struct list_head list;
493 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800494 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800495 void *buffer;
496 void *head;
497};
498
499struct tx_agg {
500 struct list_head list;
501 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800502 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800503 void *buffer;
504 void *head;
505 u32 skb_num;
506 u32 skb_len;
507};
508
hayeswangac718b62013-05-02 16:01:25 +0000509struct r8152 {
510 unsigned long flags;
511 struct usb_device *udev;
512 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800513 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000514 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800515 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800516 struct tx_agg tx_info[RTL8152_MAX_TX];
517 struct rx_agg rx_info[RTL8152_MAX_RX];
518 struct list_head rx_done, tx_free;
519 struct sk_buff_head tx_queue;
520 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000521 struct delayed_work schedule;
522 struct mii_if_info mii;
hayeswangc81229c2014-01-02 11:22:42 +0800523
524 struct rtl_ops {
525 void (*init)(struct r8152 *);
526 int (*enable)(struct r8152 *);
527 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800528 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800529 void (*down)(struct r8152 *);
530 void (*unload)(struct r8152 *);
531 } rtl_ops;
532
hayeswang40a82912013-08-14 20:54:40 +0800533 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800534 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000535 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800536 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000537 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800538 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000539 u8 version;
540 u8 speed;
541};
542
543enum rtl_version {
544 RTL_VER_UNKNOWN = 0,
545 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800546 RTL_VER_02,
547 RTL_VER_03,
548 RTL_VER_04,
549 RTL_VER_05,
550 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000551};
552
553/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
554 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
555 */
556static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800557static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000558
559static
560int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
561{
hayeswang31787f52013-07-31 17:21:25 +0800562 int ret;
563 void *tmp;
564
565 tmp = kmalloc(size, GFP_KERNEL);
566 if (!tmp)
567 return -ENOMEM;
568
569 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000570 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800571 value, index, tmp, size, 500);
572
573 memcpy(data, tmp, size);
574 kfree(tmp);
575
576 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000577}
578
579static
580int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
581{
hayeswang31787f52013-07-31 17:21:25 +0800582 int ret;
583 void *tmp;
584
585 tmp = kmalloc(size, GFP_KERNEL);
586 if (!tmp)
587 return -ENOMEM;
588
589 memcpy(tmp, data, size);
590
591 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000592 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800593 value, index, tmp, size, 500);
594
595 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800596
hayeswang31787f52013-07-31 17:21:25 +0800597 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000598}
599
600static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
601 void *data, u16 type)
602{
hayeswang45f4a192014-01-06 17:08:41 +0800603 u16 limit = 64;
604 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000605
606 if (test_bit(RTL8152_UNPLUG, &tp->flags))
607 return -ENODEV;
608
609 /* both size and indix must be 4 bytes align */
610 if ((size & 3) || !size || (index & 3) || !data)
611 return -EPERM;
612
613 if ((u32)index + (u32)size > 0xffff)
614 return -EPERM;
615
616 while (size) {
617 if (size > limit) {
618 ret = get_registers(tp, index, type, limit, data);
619 if (ret < 0)
620 break;
621
622 index += limit;
623 data += limit;
624 size -= limit;
625 } else {
626 ret = get_registers(tp, index, type, size, data);
627 if (ret < 0)
628 break;
629
630 index += size;
631 data += size;
632 size = 0;
633 break;
634 }
635 }
636
637 return ret;
638}
639
640static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
641 u16 size, void *data, u16 type)
642{
hayeswang45f4a192014-01-06 17:08:41 +0800643 int ret;
644 u16 byteen_start, byteen_end, byen;
645 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000646
647 if (test_bit(RTL8152_UNPLUG, &tp->flags))
648 return -ENODEV;
649
650 /* both size and indix must be 4 bytes align */
651 if ((size & 3) || !size || (index & 3) || !data)
652 return -EPERM;
653
654 if ((u32)index + (u32)size > 0xffff)
655 return -EPERM;
656
657 byteen_start = byteen & BYTE_EN_START_MASK;
658 byteen_end = byteen & BYTE_EN_END_MASK;
659
660 byen = byteen_start | (byteen_start << 4);
661 ret = set_registers(tp, index, type | byen, 4, data);
662 if (ret < 0)
663 goto error1;
664
665 index += 4;
666 data += 4;
667 size -= 4;
668
669 if (size) {
670 size -= 4;
671
672 while (size) {
673 if (size > limit) {
674 ret = set_registers(tp, index,
675 type | BYTE_EN_DWORD,
676 limit, data);
677 if (ret < 0)
678 goto error1;
679
680 index += limit;
681 data += limit;
682 size -= limit;
683 } else {
684 ret = set_registers(tp, index,
685 type | BYTE_EN_DWORD,
686 size, data);
687 if (ret < 0)
688 goto error1;
689
690 index += size;
691 data += size;
692 size = 0;
693 break;
694 }
695 }
696
697 byen = byteen_end | (byteen_end >> 4);
698 ret = set_registers(tp, index, type | byen, 4, data);
699 if (ret < 0)
700 goto error1;
701 }
702
703error1:
704 return ret;
705}
706
707static inline
708int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
709{
710 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
711}
712
713static inline
714int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
715{
716 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
717}
718
719static inline
720int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
721{
722 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
723}
724
725static inline
726int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
727{
728 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
729}
730
731static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
732{
hayeswangc8826de2013-07-31 17:21:26 +0800733 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000734
hayeswangc8826de2013-07-31 17:21:26 +0800735 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000736
737 return __le32_to_cpu(data);
738}
739
740static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
741{
hayeswangc8826de2013-07-31 17:21:26 +0800742 __le32 tmp = __cpu_to_le32(data);
743
744 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000745}
746
747static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
748{
749 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800750 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000751 u8 shift = index & 2;
752
753 index &= ~3;
754
hayeswangc8826de2013-07-31 17:21:26 +0800755 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000756
hayeswangc8826de2013-07-31 17:21:26 +0800757 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000758 data >>= (shift * 8);
759 data &= 0xffff;
760
761 return (u16)data;
762}
763
764static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
765{
hayeswangc8826de2013-07-31 17:21:26 +0800766 u32 mask = 0xffff;
767 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000768 u16 byen = BYTE_EN_WORD;
769 u8 shift = index & 2;
770
771 data &= mask;
772
773 if (index & 2) {
774 byen <<= shift;
775 mask <<= (shift * 8);
776 data <<= (shift * 8);
777 index &= ~3;
778 }
779
hayeswangc8826de2013-07-31 17:21:26 +0800780 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000781
hayeswangc8826de2013-07-31 17:21:26 +0800782 data |= __le32_to_cpu(tmp) & ~mask;
783 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000784
hayeswangc8826de2013-07-31 17:21:26 +0800785 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000786}
787
788static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
789{
790 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800791 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000792 u8 shift = index & 3;
793
794 index &= ~3;
795
hayeswangc8826de2013-07-31 17:21:26 +0800796 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000797
hayeswangc8826de2013-07-31 17:21:26 +0800798 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000799 data >>= (shift * 8);
800 data &= 0xff;
801
802 return (u8)data;
803}
804
805static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
806{
hayeswangc8826de2013-07-31 17:21:26 +0800807 u32 mask = 0xff;
808 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000809 u16 byen = BYTE_EN_BYTE;
810 u8 shift = index & 3;
811
812 data &= mask;
813
814 if (index & 3) {
815 byen <<= shift;
816 mask <<= (shift * 8);
817 data <<= (shift * 8);
818 index &= ~3;
819 }
820
hayeswangc8826de2013-07-31 17:21:26 +0800821 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000822
hayeswangc8826de2013-07-31 17:21:26 +0800823 data |= __le32_to_cpu(tmp) & ~mask;
824 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000825
hayeswangc8826de2013-07-31 17:21:26 +0800826 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000827}
828
hayeswangac244d32014-01-02 11:22:40 +0800829static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
830{
831 u16 ocp_base, ocp_index;
832
833 ocp_base = addr & 0xf000;
834 if (ocp_base != tp->ocp_base) {
835 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
836 tp->ocp_base = ocp_base;
837 }
838
839 ocp_index = (addr & 0x0fff) | 0xb000;
840 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
841}
842
hayeswange3fe0b12014-01-02 11:22:39 +0800843static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
844{
845 u16 ocp_base, ocp_index;
846
847 ocp_base = addr & 0xf000;
848 if (ocp_base != tp->ocp_base) {
849 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
850 tp->ocp_base = ocp_base;
851 }
852
853 ocp_index = (addr & 0x0fff) | 0xb000;
854 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
855}
856
hayeswangac244d32014-01-02 11:22:40 +0800857static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000858{
hayeswangac244d32014-01-02 11:22:40 +0800859 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000860}
861
hayeswangac244d32014-01-02 11:22:40 +0800862static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000863{
hayeswangac244d32014-01-02 11:22:40 +0800864 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000865}
866
hayeswang43779f82014-01-02 11:25:10 +0800867static void sram_write(struct r8152 *tp, u16 addr, u16 data)
868{
869 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
870 ocp_reg_write(tp, OCP_SRAM_DATA, data);
871}
872
873static u16 sram_read(struct r8152 *tp, u16 addr)
874{
875 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
876 return ocp_reg_read(tp, OCP_SRAM_DATA);
877}
878
hayeswangac718b62013-05-02 16:01:25 +0000879static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
880{
881 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800882 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000883
884 if (phy_id != R8152_PHY_ID)
885 return -EINVAL;
886
hayeswang9a4be1b2014-02-18 21:49:07 +0800887 ret = usb_autopm_get_interface(tp->intf);
888 if (ret < 0)
889 goto out;
890
891 ret = r8152_mdio_read(tp, reg);
892
893 usb_autopm_put_interface(tp->intf);
894
895out:
896 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000897}
898
899static
900void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
901{
902 struct r8152 *tp = netdev_priv(netdev);
903
904 if (phy_id != R8152_PHY_ID)
905 return;
906
hayeswang9a4be1b2014-02-18 21:49:07 +0800907 if (usb_autopm_get_interface(tp->intf) < 0)
908 return;
909
hayeswangac718b62013-05-02 16:01:25 +0000910 r8152_mdio_write(tp, reg, val);
hayeswang9a4be1b2014-02-18 21:49:07 +0800911
912 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +0000913}
914
hayeswangebc2ec482013-08-14 20:54:38 +0800915static
916int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
917
hayeswangac718b62013-05-02 16:01:25 +0000918static inline void set_ethernet_addr(struct r8152 *tp)
919{
920 struct net_device *dev = tp->netdev;
hayeswang8a91c822014-02-18 21:49:01 +0800921 int ret;
hayeswang31787f52013-07-31 17:21:25 +0800922 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000923
hayeswang8a91c822014-02-18 21:49:01 +0800924 if (tp->version == RTL_VER_01)
925 ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
926 else
927 ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
928
929 if (ret < 0) {
hayeswangac718b62013-05-02 16:01:25 +0000930 netif_notice(tp, probe, dev, "inet addr fail\n");
hayeswang8a91c822014-02-18 21:49:01 +0800931 } else {
932 if (tp->version != RTL_VER_01) {
933 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
934 CRWECR_CONFIG);
935 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
936 sizeof(node_id), node_id);
937 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
938 CRWECR_NORAML);
939 }
940
hayeswangac718b62013-05-02 16:01:25 +0000941 memcpy(dev->dev_addr, node_id, dev->addr_len);
942 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
943 }
hayeswangac718b62013-05-02 16:01:25 +0000944}
945
946static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
947{
948 struct r8152 *tp = netdev_priv(netdev);
949 struct sockaddr *addr = p;
950
951 if (!is_valid_ether_addr(addr->sa_data))
952 return -EADDRNOTAVAIL;
953
954 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
955
956 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
957 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
958 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
959
960 return 0;
961}
962
hayeswangac718b62013-05-02 16:01:25 +0000963static void read_bulk_callback(struct urb *urb)
964{
hayeswangac718b62013-05-02 16:01:25 +0000965 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +0000966 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800967 struct rx_agg *agg;
968 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000969 int result;
hayeswangac718b62013-05-02 16:01:25 +0000970
hayeswangebc2ec482013-08-14 20:54:38 +0800971 agg = urb->context;
972 if (!agg)
973 return;
974
975 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000976 if (!tp)
977 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800978
hayeswangac718b62013-05-02 16:01:25 +0000979 if (test_bit(RTL8152_UNPLUG, &tp->flags))
980 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800981
982 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000983 return;
984
hayeswangebc2ec482013-08-14 20:54:38 +0800985 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800986
987 /* When link down, the driver would cancel all bulks. */
988 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +0800989 if (!netif_carrier_ok(netdev))
990 return;
991
hayeswang9a4be1b2014-02-18 21:49:07 +0800992 usb_mark_last_busy(tp->udev);
993
hayeswangac718b62013-05-02 16:01:25 +0000994 switch (status) {
995 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +0800996 if (urb->actual_length < ETH_ZLEN)
997 break;
998
hayeswang2685d412014-03-07 11:04:34 +0800999 spin_lock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001000 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001001 spin_unlock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001002 tasklet_schedule(&tp->tl);
1003 return;
hayeswangac718b62013-05-02 16:01:25 +00001004 case -ESHUTDOWN:
1005 set_bit(RTL8152_UNPLUG, &tp->flags);
1006 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001007 return;
hayeswangac718b62013-05-02 16:01:25 +00001008 case -ENOENT:
1009 return; /* the urb is in unlink state */
1010 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001011 if (net_ratelimit())
1012 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001013 break;
hayeswangac718b62013-05-02 16:01:25 +00001014 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001015 if (net_ratelimit())
1016 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001017 break;
hayeswangac718b62013-05-02 16:01:25 +00001018 }
1019
hayeswangebc2ec482013-08-14 20:54:38 +08001020 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001021 if (result == -ENODEV) {
1022 netif_device_detach(tp->netdev);
1023 } else if (result) {
hayeswang2685d412014-03-07 11:04:34 +08001024 spin_lock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001025 list_add_tail(&agg->list, &tp->rx_done);
hayeswang2685d412014-03-07 11:04:34 +08001026 spin_unlock(&tp->rx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001027 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001028 }
hayeswangac718b62013-05-02 16:01:25 +00001029}
1030
1031static void write_bulk_callback(struct urb *urb)
1032{
hayeswangebc2ec482013-08-14 20:54:38 +08001033 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001034 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001035 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001036 struct r8152 *tp;
1037 int status = urb->status;
1038
hayeswangebc2ec482013-08-14 20:54:38 +08001039 agg = urb->context;
1040 if (!agg)
1041 return;
1042
1043 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001044 if (!tp)
1045 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001046
hayeswangd104eaf2014-03-06 15:07:17 +08001047 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001048 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001049 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001050 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001051 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001052 stats->tx_errors += agg->skb_num;
1053 } else {
1054 stats->tx_packets += agg->skb_num;
1055 stats->tx_bytes += agg->skb_len;
1056 }
1057
hayeswang2685d412014-03-07 11:04:34 +08001058 spin_lock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001059 list_add_tail(&agg->list, &tp->tx_free);
hayeswang2685d412014-03-07 11:04:34 +08001060 spin_unlock(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001061
hayeswang9a4be1b2014-02-18 21:49:07 +08001062 usb_autopm_put_interface_async(tp->intf);
1063
hayeswangd104eaf2014-03-06 15:07:17 +08001064 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001065 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001066
1067 if (!test_bit(WORK_ENABLE, &tp->flags))
1068 return;
1069
1070 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1071 return;
1072
1073 if (!skb_queue_empty(&tp->tx_queue))
hayeswang9a4be1b2014-02-18 21:49:07 +08001074 schedule_delayed_work(&tp->schedule, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001075}
1076
hayeswang40a82912013-08-14 20:54:40 +08001077static void intr_callback(struct urb *urb)
1078{
1079 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001080 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001081 int status = urb->status;
1082 int res;
1083
1084 tp = urb->context;
1085 if (!tp)
1086 return;
1087
1088 if (!test_bit(WORK_ENABLE, &tp->flags))
1089 return;
1090
1091 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1092 return;
1093
1094 switch (status) {
1095 case 0: /* success */
1096 break;
1097 case -ECONNRESET: /* unlink */
1098 case -ESHUTDOWN:
1099 netif_device_detach(tp->netdev);
1100 case -ENOENT:
1101 return;
1102 case -EOVERFLOW:
1103 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1104 goto resubmit;
1105 /* -EPIPE: should clear the halt */
1106 default:
1107 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1108 goto resubmit;
1109 }
1110
1111 d = urb->transfer_buffer;
1112 if (INTR_LINK & __le16_to_cpu(d[0])) {
1113 if (!(tp->speed & LINK_STATUS)) {
1114 set_bit(RTL8152_LINK_CHG, &tp->flags);
1115 schedule_delayed_work(&tp->schedule, 0);
1116 }
1117 } else {
1118 if (tp->speed & LINK_STATUS) {
1119 set_bit(RTL8152_LINK_CHG, &tp->flags);
1120 schedule_delayed_work(&tp->schedule, 0);
1121 }
1122 }
1123
1124resubmit:
1125 res = usb_submit_urb(urb, GFP_ATOMIC);
1126 if (res == -ENODEV)
1127 netif_device_detach(tp->netdev);
1128 else if (res)
1129 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001130 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001131}
1132
hayeswangebc2ec482013-08-14 20:54:38 +08001133static inline void *rx_agg_align(void *data)
1134{
hayeswang8e1f51b2014-01-02 11:22:41 +08001135 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001136}
1137
1138static inline void *tx_agg_align(void *data)
1139{
hayeswang8e1f51b2014-01-02 11:22:41 +08001140 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001141}
1142
1143static void free_all_mem(struct r8152 *tp)
1144{
1145 int i;
1146
1147 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001148 usb_free_urb(tp->rx_info[i].urb);
1149 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001150
hayeswang9629e3c2014-01-15 10:42:15 +08001151 kfree(tp->rx_info[i].buffer);
1152 tp->rx_info[i].buffer = NULL;
1153 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001154 }
1155
1156 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001157 usb_free_urb(tp->tx_info[i].urb);
1158 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001159
hayeswang9629e3c2014-01-15 10:42:15 +08001160 kfree(tp->tx_info[i].buffer);
1161 tp->tx_info[i].buffer = NULL;
1162 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001163 }
hayeswang40a82912013-08-14 20:54:40 +08001164
hayeswang9629e3c2014-01-15 10:42:15 +08001165 usb_free_urb(tp->intr_urb);
1166 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001167
hayeswang9629e3c2014-01-15 10:42:15 +08001168 kfree(tp->intr_buff);
1169 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001170}
1171
1172static int alloc_all_mem(struct r8152 *tp)
1173{
1174 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001175 struct usb_interface *intf = tp->intf;
1176 struct usb_host_interface *alt = intf->cur_altsetting;
1177 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001178 struct urb *urb;
1179 int node, i;
1180 u8 *buf;
1181
1182 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1183
1184 spin_lock_init(&tp->rx_lock);
1185 spin_lock_init(&tp->tx_lock);
1186 INIT_LIST_HEAD(&tp->rx_done);
1187 INIT_LIST_HEAD(&tp->tx_free);
1188 skb_queue_head_init(&tp->tx_queue);
1189
1190 for (i = 0; i < RTL8152_MAX_RX; i++) {
1191 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1192 if (!buf)
1193 goto err1;
1194
1195 if (buf != rx_agg_align(buf)) {
1196 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001197 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1198 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001199 if (!buf)
1200 goto err1;
1201 }
1202
1203 urb = usb_alloc_urb(0, GFP_KERNEL);
1204 if (!urb) {
1205 kfree(buf);
1206 goto err1;
1207 }
1208
1209 INIT_LIST_HEAD(&tp->rx_info[i].list);
1210 tp->rx_info[i].context = tp;
1211 tp->rx_info[i].urb = urb;
1212 tp->rx_info[i].buffer = buf;
1213 tp->rx_info[i].head = rx_agg_align(buf);
1214 }
1215
1216 for (i = 0; i < RTL8152_MAX_TX; i++) {
1217 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1218 if (!buf)
1219 goto err1;
1220
1221 if (buf != tx_agg_align(buf)) {
1222 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001223 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1224 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001225 if (!buf)
1226 goto err1;
1227 }
1228
1229 urb = usb_alloc_urb(0, GFP_KERNEL);
1230 if (!urb) {
1231 kfree(buf);
1232 goto err1;
1233 }
1234
1235 INIT_LIST_HEAD(&tp->tx_info[i].list);
1236 tp->tx_info[i].context = tp;
1237 tp->tx_info[i].urb = urb;
1238 tp->tx_info[i].buffer = buf;
1239 tp->tx_info[i].head = tx_agg_align(buf);
1240
1241 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1242 }
1243
hayeswang40a82912013-08-14 20:54:40 +08001244 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1245 if (!tp->intr_urb)
1246 goto err1;
1247
1248 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1249 if (!tp->intr_buff)
1250 goto err1;
1251
1252 tp->intr_interval = (int)ep_intr->desc.bInterval;
1253 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1254 tp->intr_buff, INTBUFSIZE, intr_callback,
1255 tp, tp->intr_interval);
1256
hayeswangebc2ec482013-08-14 20:54:38 +08001257 return 0;
1258
1259err1:
1260 free_all_mem(tp);
1261 return -ENOMEM;
1262}
1263
hayeswang0de98f62013-08-16 16:09:35 +08001264static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1265{
1266 struct tx_agg *agg = NULL;
1267 unsigned long flags;
1268
1269 spin_lock_irqsave(&tp->tx_lock, flags);
1270 if (!list_empty(&tp->tx_free)) {
1271 struct list_head *cursor;
1272
1273 cursor = tp->tx_free.next;
1274 list_del_init(cursor);
1275 agg = list_entry(cursor, struct tx_agg, list);
1276 }
1277 spin_unlock_irqrestore(&tp->tx_lock, flags);
1278
1279 return agg;
1280}
1281
hayeswang5bd23882013-08-14 20:54:39 +08001282static void
1283r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1284{
1285 memset(desc, 0, sizeof(*desc));
1286
1287 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1288
1289 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1290 __be16 protocol;
1291 u8 ip_protocol;
1292 u32 opts2 = 0;
1293
1294 if (skb->protocol == htons(ETH_P_8021Q))
1295 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1296 else
1297 protocol = skb->protocol;
1298
1299 switch (protocol) {
1300 case htons(ETH_P_IP):
1301 opts2 |= IPV4_CS;
1302 ip_protocol = ip_hdr(skb)->protocol;
1303 break;
1304
1305 case htons(ETH_P_IPV6):
1306 opts2 |= IPV6_CS;
1307 ip_protocol = ipv6_hdr(skb)->nexthdr;
1308 break;
1309
1310 default:
1311 ip_protocol = IPPROTO_RAW;
1312 break;
1313 }
1314
1315 if (ip_protocol == IPPROTO_TCP) {
1316 opts2 |= TCP_CS;
1317 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1318 } else if (ip_protocol == IPPROTO_UDP) {
1319 opts2 |= UDP_CS;
1320 } else {
1321 WARN_ON_ONCE(1);
1322 }
1323
1324 desc->opts2 = cpu_to_le32(opts2);
1325 }
1326}
1327
hayeswangb1379d92013-08-16 16:09:37 +08001328static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1329{
hayeswangd84130a2014-02-18 21:49:02 +08001330 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001331 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001332 u8 *tx_data;
1333
hayeswangd84130a2014-02-18 21:49:02 +08001334 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001335 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001336 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001337 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001338
hayeswangb1379d92013-08-16 16:09:37 +08001339 tx_data = agg->head;
1340 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001341 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001342
hayeswang7937f9e2013-11-20 17:30:54 +08001343 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001344 struct tx_desc *tx_desc;
1345 struct sk_buff *skb;
1346 unsigned int len;
1347
hayeswangd84130a2014-02-18 21:49:02 +08001348 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001349 if (!skb)
1350 break;
1351
hayeswang7937f9e2013-11-20 17:30:54 +08001352 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001353 len = skb->len;
1354 if (remain < len) {
hayeswangd84130a2014-02-18 21:49:02 +08001355 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001356 break;
1357 }
1358
hayeswang7937f9e2013-11-20 17:30:54 +08001359 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001360 tx_desc = (struct tx_desc *)tx_data;
1361 tx_data += sizeof(*tx_desc);
1362
1363 r8152_tx_csum(tp, tx_desc, skb);
1364 memcpy(tx_data, skb->data, len);
1365 agg->skb_num++;
1366 agg->skb_len += len;
1367 dev_kfree_skb_any(skb);
1368
hayeswang7937f9e2013-11-20 17:30:54 +08001369 tx_data += len;
1370 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001371 }
1372
hayeswangd84130a2014-02-18 21:49:02 +08001373 if (!skb_queue_empty(&skb_head)) {
hayeswang2685d412014-03-07 11:04:34 +08001374 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001375 skb_queue_splice(&skb_head, tx_queue);
hayeswang2685d412014-03-07 11:04:34 +08001376 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001377 }
1378
hayeswang9a4be1b2014-02-18 21:49:07 +08001379 netif_tx_lock_bh(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001380
1381 if (netif_queue_stopped(tp->netdev) &&
1382 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1383 netif_wake_queue(tp->netdev);
1384
hayeswang9a4be1b2014-02-18 21:49:07 +08001385 netif_tx_unlock_bh(tp->netdev);
1386
1387 ret = usb_autopm_get_interface(tp->intf);
1388 if (ret < 0)
1389 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001390
hayeswangb1379d92013-08-16 16:09:37 +08001391 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1392 agg->head, (int)(tx_data - (u8 *)agg->head),
1393 (usb_complete_t)write_bulk_callback, agg);
1394
hayeswang9a4be1b2014-02-18 21:49:07 +08001395 ret = usb_submit_urb(agg->urb, GFP_KERNEL);
1396 if (ret < 0)
1397 usb_autopm_put_interface(tp->intf);
1398
1399out_tx_fill:
1400 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001401}
1402
hayeswangebc2ec482013-08-14 20:54:38 +08001403static void rx_bottom(struct r8152 *tp)
1404{
hayeswanga5a4f462013-08-16 16:09:34 +08001405 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001406 struct list_head *cursor, *next, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +08001407
hayeswangd84130a2014-02-18 21:49:02 +08001408 if (list_empty(&tp->rx_done))
1409 return;
1410
1411 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001412 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001413 list_splice_init(&tp->rx_done, &rx_queue);
1414 spin_unlock_irqrestore(&tp->rx_lock, flags);
1415
1416 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001417 struct rx_desc *rx_desc;
1418 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001419 int len_used = 0;
1420 struct urb *urb;
1421 u8 *rx_data;
1422 int ret;
1423
hayeswangebc2ec482013-08-14 20:54:38 +08001424 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001425
1426 agg = list_entry(cursor, struct rx_agg, list);
1427 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001428 if (urb->actual_length < ETH_ZLEN)
1429 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001430
hayeswangebc2ec482013-08-14 20:54:38 +08001431 rx_desc = agg->head;
1432 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001433 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001434
hayeswang7937f9e2013-11-20 17:30:54 +08001435 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001436 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001437 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001438 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001439 struct sk_buff *skb;
1440
hayeswang7937f9e2013-11-20 17:30:54 +08001441 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001442 if (pkt_len < ETH_ZLEN)
1443 break;
1444
hayeswang7937f9e2013-11-20 17:30:54 +08001445 len_used += pkt_len;
1446 if (urb->actual_length < len_used)
1447 break;
1448
hayeswang8e1f51b2014-01-02 11:22:41 +08001449 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001450 rx_data += sizeof(struct rx_desc);
1451
1452 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1453 if (!skb) {
1454 stats->rx_dropped++;
1455 break;
1456 }
1457 memcpy(skb->data, rx_data, pkt_len);
1458 skb_put(skb, pkt_len);
1459 skb->protocol = eth_type_trans(skb, netdev);
hayeswang9d9aafa2014-02-18 21:49:09 +08001460 netif_receive_skb(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001461 stats->rx_packets++;
1462 stats->rx_bytes += pkt_len;
1463
hayeswang8e1f51b2014-01-02 11:22:41 +08001464 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001465 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001466 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001467 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001468 }
1469
hayeswang0de98f62013-08-16 16:09:35 +08001470submit:
hayeswangebc2ec482013-08-14 20:54:38 +08001471 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec482013-08-14 20:54:38 +08001472 if (ret && ret != -ENODEV) {
hayeswangd84130a2014-02-18 21:49:02 +08001473 spin_lock_irqsave(&tp->rx_lock, flags);
1474 list_add_tail(&agg->list, &tp->rx_done);
1475 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001476 tasklet_schedule(&tp->tl);
1477 }
1478 }
hayeswangebc2ec482013-08-14 20:54:38 +08001479}
1480
1481static void tx_bottom(struct r8152 *tp)
1482{
hayeswangebc2ec482013-08-14 20:54:38 +08001483 int res;
1484
hayeswangb1379d92013-08-16 16:09:37 +08001485 do {
1486 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001487
hayeswangb1379d92013-08-16 16:09:37 +08001488 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001489 break;
1490
hayeswangb1379d92013-08-16 16:09:37 +08001491 agg = r8152_get_tx_agg(tp);
1492 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001493 break;
hayeswangb1379d92013-08-16 16:09:37 +08001494
1495 res = r8152_tx_agg_fill(tp, agg);
1496 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001497 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001498
1499 if (res == -ENODEV) {
1500 netif_device_detach(netdev);
1501 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001502 struct net_device_stats *stats = &netdev->stats;
1503 unsigned long flags;
1504
hayeswangb1379d92013-08-16 16:09:37 +08001505 netif_warn(tp, tx_err, netdev,
1506 "failed tx_urb %d\n", res);
1507 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001508
hayeswangb1379d92013-08-16 16:09:37 +08001509 spin_lock_irqsave(&tp->tx_lock, flags);
1510 list_add_tail(&agg->list, &tp->tx_free);
1511 spin_unlock_irqrestore(&tp->tx_lock, flags);
1512 }
hayeswangebc2ec482013-08-14 20:54:38 +08001513 }
hayeswangb1379d92013-08-16 16:09:37 +08001514 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001515}
1516
1517static void bottom_half(unsigned long data)
1518{
1519 struct r8152 *tp;
1520
1521 tp = (struct r8152 *)data;
1522
1523 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1524 return;
1525
1526 if (!test_bit(WORK_ENABLE, &tp->flags))
1527 return;
1528
hayeswang7559fb2f2013-08-16 16:09:38 +08001529 /* When link down, the driver would cancel all bulks. */
1530 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001531 if (!netif_carrier_ok(tp->netdev))
1532 return;
1533
1534 rx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001535}
1536
1537static
1538int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1539{
1540 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1541 agg->head, rx_buf_sz,
1542 (usb_complete_t)read_bulk_callback, agg);
1543
1544 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001545}
1546
hayeswang00a5e362014-02-18 21:48:59 +08001547static void rtl_drop_queued_tx(struct r8152 *tp)
1548{
1549 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001550 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08001551 struct sk_buff *skb;
1552
hayeswangd84130a2014-02-18 21:49:02 +08001553 if (skb_queue_empty(tx_queue))
1554 return;
1555
1556 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001557 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001558 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08001559 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001560
1561 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001562 dev_kfree_skb(skb);
1563 stats->tx_dropped++;
1564 }
1565}
1566
hayeswangac718b62013-05-02 16:01:25 +00001567static void rtl8152_tx_timeout(struct net_device *netdev)
1568{
1569 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001570 int i;
1571
Hayes Wang4a8deae2014-01-07 11:18:22 +08001572 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001573 for (i = 0; i < RTL8152_MAX_TX; i++)
1574 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001575}
1576
1577static void rtl8152_set_rx_mode(struct net_device *netdev)
1578{
1579 struct r8152 *tp = netdev_priv(netdev);
1580
hayeswang40a82912013-08-14 20:54:40 +08001581 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001582 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001583 schedule_delayed_work(&tp->schedule, 0);
1584 }
hayeswangac718b62013-05-02 16:01:25 +00001585}
1586
1587static void _rtl8152_set_rx_mode(struct net_device *netdev)
1588{
1589 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001590 u32 mc_filter[2]; /* Multicast hash filter */
1591 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001592 u32 ocp_data;
1593
hayeswangac718b62013-05-02 16:01:25 +00001594 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1595 netif_stop_queue(netdev);
1596 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1597 ocp_data &= ~RCR_ACPT_ALL;
1598 ocp_data |= RCR_AB | RCR_APM;
1599
1600 if (netdev->flags & IFF_PROMISC) {
1601 /* Unconditionally log net taps. */
1602 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1603 ocp_data |= RCR_AM | RCR_AAP;
1604 mc_filter[1] = mc_filter[0] = 0xffffffff;
1605 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1606 (netdev->flags & IFF_ALLMULTI)) {
1607 /* Too many to filter perfectly -- accept all multicasts. */
1608 ocp_data |= RCR_AM;
1609 mc_filter[1] = mc_filter[0] = 0xffffffff;
1610 } else {
1611 struct netdev_hw_addr *ha;
1612
1613 mc_filter[1] = mc_filter[0] = 0;
1614 netdev_for_each_mc_addr(ha, netdev) {
1615 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1616 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1617 ocp_data |= RCR_AM;
1618 }
1619 }
1620
hayeswang31787f52013-07-31 17:21:25 +08001621 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1622 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001623
hayeswang31787f52013-07-31 17:21:25 +08001624 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001625 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1626 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001627}
1628
1629static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1630 struct net_device *netdev)
1631{
1632 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001633
hayeswangac718b62013-05-02 16:01:25 +00001634 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001635
hayeswang61598782013-11-20 17:30:55 +08001636 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001637
hayeswangdd1b1192013-11-20 17:30:56 +08001638 if (list_empty(&tp->tx_free) &&
1639 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1640 netif_stop_queue(netdev);
1641
hayeswang61598782013-11-20 17:30:55 +08001642 if (!list_empty(&tp->tx_free))
hayeswang9a4be1b2014-02-18 21:49:07 +08001643 schedule_delayed_work(&tp->schedule, 0);
hayeswangac718b62013-05-02 16:01:25 +00001644
1645 return NETDEV_TX_OK;
1646}
1647
1648static void r8152b_reset_packet_filter(struct r8152 *tp)
1649{
1650 u32 ocp_data;
1651
1652 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1653 ocp_data &= ~FMC_FCR_MCU_EN;
1654 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1655 ocp_data |= FMC_FCR_MCU_EN;
1656 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1657}
1658
1659static void rtl8152_nic_reset(struct r8152 *tp)
1660{
1661 int i;
1662
1663 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1664
1665 for (i = 0; i < 1000; i++) {
1666 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1667 break;
1668 udelay(100);
1669 }
1670}
1671
hayeswangdd1b1192013-11-20 17:30:56 +08001672static void set_tx_qlen(struct r8152 *tp)
1673{
1674 struct net_device *netdev = tp->netdev;
1675
1676 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1677 sizeof(struct tx_desc));
1678}
1679
hayeswangac718b62013-05-02 16:01:25 +00001680static inline u8 rtl8152_get_speed(struct r8152 *tp)
1681{
1682 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1683}
1684
hayeswang507605a2014-01-02 11:22:43 +08001685static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001686{
hayeswangebc2ec482013-08-14 20:54:38 +08001687 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001688 u8 speed;
1689
1690 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001691 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001692 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001693 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001694 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1695 } else {
1696 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001697 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001698 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1699 }
hayeswang507605a2014-01-02 11:22:43 +08001700}
1701
hayeswang00a5e362014-02-18 21:48:59 +08001702static void rxdy_gated_en(struct r8152 *tp, bool enable)
1703{
1704 u32 ocp_data;
1705
1706 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1707 if (enable)
1708 ocp_data |= RXDY_GATED_EN;
1709 else
1710 ocp_data &= ~RXDY_GATED_EN;
1711 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1712}
1713
hayeswang507605a2014-01-02 11:22:43 +08001714static int rtl_enable(struct r8152 *tp)
1715{
1716 u32 ocp_data;
1717 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001718
1719 r8152b_reset_packet_filter(tp);
1720
1721 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1722 ocp_data |= CR_RE | CR_TE;
1723 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1724
hayeswang00a5e362014-02-18 21:48:59 +08001725 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001726
hayeswangebc2ec482013-08-14 20:54:38 +08001727 INIT_LIST_HEAD(&tp->rx_done);
1728 ret = 0;
1729 for (i = 0; i < RTL8152_MAX_RX; i++) {
1730 INIT_LIST_HEAD(&tp->rx_info[i].list);
1731 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1732 }
hayeswangac718b62013-05-02 16:01:25 +00001733
hayeswangebc2ec482013-08-14 20:54:38 +08001734 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001735}
1736
hayeswang507605a2014-01-02 11:22:43 +08001737static int rtl8152_enable(struct r8152 *tp)
1738{
1739 set_tx_qlen(tp);
1740 rtl_set_eee_plus(tp);
1741
1742 return rtl_enable(tp);
1743}
1744
hayeswang43779f82014-01-02 11:25:10 +08001745static void r8153_set_rx_agg(struct r8152 *tp)
1746{
1747 u8 speed;
1748
1749 speed = rtl8152_get_speed(tp);
1750 if (speed & _1000bps) {
1751 if (tp->udev->speed == USB_SPEED_SUPER) {
1752 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1753 RX_THR_SUPPER);
1754 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1755 EARLY_AGG_SUPPER);
1756 } else {
1757 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1758 RX_THR_HIGH);
1759 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1760 EARLY_AGG_HIGH);
1761 }
1762 } else {
1763 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1764 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1765 EARLY_AGG_SLOW);
1766 }
1767}
1768
1769static int rtl8153_enable(struct r8152 *tp)
1770{
1771 set_tx_qlen(tp);
1772 rtl_set_eee_plus(tp);
1773 r8153_set_rx_agg(tp);
1774
1775 return rtl_enable(tp);
1776}
1777
hayeswangac718b62013-05-02 16:01:25 +00001778static void rtl8152_disable(struct r8152 *tp)
1779{
hayeswangebc2ec482013-08-14 20:54:38 +08001780 u32 ocp_data;
1781 int i;
hayeswangac718b62013-05-02 16:01:25 +00001782
1783 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1784 ocp_data &= ~RCR_ACPT_ALL;
1785 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1786
hayeswang00a5e362014-02-18 21:48:59 +08001787 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001788
1789 for (i = 0; i < RTL8152_MAX_TX; i++)
1790 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001791
hayeswang00a5e362014-02-18 21:48:59 +08001792 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001793
1794 for (i = 0; i < 1000; i++) {
1795 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1796 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1797 break;
1798 mdelay(1);
1799 }
1800
1801 for (i = 0; i < 1000; i++) {
1802 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1803 break;
1804 mdelay(1);
1805 }
1806
hayeswangebc2ec482013-08-14 20:54:38 +08001807 for (i = 0; i < RTL8152_MAX_RX; i++)
1808 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001809
1810 rtl8152_nic_reset(tp);
1811}
1812
hayeswang00a5e362014-02-18 21:48:59 +08001813static void r8152_power_cut_en(struct r8152 *tp, bool enable)
1814{
1815 u32 ocp_data;
1816
1817 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1818 if (enable)
1819 ocp_data |= POWER_CUT;
1820 else
1821 ocp_data &= ~POWER_CUT;
1822 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1823
1824 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1825 ocp_data &= ~RESUME_INDICATE;
1826 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08001827}
1828
hayeswang21ff2e82014-02-18 21:49:06 +08001829#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1830
1831static u32 __rtl_get_wol(struct r8152 *tp)
1832{
1833 u32 ocp_data;
1834 u32 wolopts = 0;
1835
1836 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1837 if (!(ocp_data & LAN_WAKE_EN))
1838 return 0;
1839
1840 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1841 if (ocp_data & LINK_ON_WAKE_EN)
1842 wolopts |= WAKE_PHY;
1843
1844 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1845 if (ocp_data & UWF_EN)
1846 wolopts |= WAKE_UCAST;
1847 if (ocp_data & BWF_EN)
1848 wolopts |= WAKE_BCAST;
1849 if (ocp_data & MWF_EN)
1850 wolopts |= WAKE_MCAST;
1851
1852 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1853 if (ocp_data & MAGIC_EN)
1854 wolopts |= WAKE_MAGIC;
1855
1856 return wolopts;
1857}
1858
1859static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
1860{
1861 u32 ocp_data;
1862
1863 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1864
1865 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1866 ocp_data &= ~LINK_ON_WAKE_EN;
1867 if (wolopts & WAKE_PHY)
1868 ocp_data |= LINK_ON_WAKE_EN;
1869 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1870
1871 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1872 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
1873 if (wolopts & WAKE_UCAST)
1874 ocp_data |= UWF_EN;
1875 if (wolopts & WAKE_BCAST)
1876 ocp_data |= BWF_EN;
1877 if (wolopts & WAKE_MCAST)
1878 ocp_data |= MWF_EN;
1879 if (wolopts & WAKE_ANY)
1880 ocp_data |= LAN_WAKE_EN;
1881 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
1882
1883 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1884
1885 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1886 ocp_data &= ~MAGIC_EN;
1887 if (wolopts & WAKE_MAGIC)
1888 ocp_data |= MAGIC_EN;
1889 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1890
1891 if (wolopts & WAKE_ANY)
1892 device_set_wakeup_enable(&tp->udev->dev, true);
1893 else
1894 device_set_wakeup_enable(&tp->udev->dev, false);
1895}
1896
hayeswang9a4be1b2014-02-18 21:49:07 +08001897static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
1898{
1899 if (enable) {
1900 u32 ocp_data;
1901
1902 __rtl_set_wol(tp, WAKE_ANY);
1903
1904 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1905
1906 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1907 ocp_data |= LINK_OFF_WAKE_EN;
1908 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1909
1910 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1911 } else {
1912 __rtl_set_wol(tp, tp->saved_wolopts);
1913 }
1914}
1915
hayeswangaa66a5f2014-02-18 21:49:04 +08001916static void rtl_phy_reset(struct r8152 *tp)
1917{
1918 u16 data;
1919 int i;
1920
1921 clear_bit(PHY_RESET, &tp->flags);
1922
1923 data = r8152_mdio_read(tp, MII_BMCR);
1924
1925 /* don't reset again before the previous one complete */
1926 if (data & BMCR_RESET)
1927 return;
1928
1929 data |= BMCR_RESET;
1930 r8152_mdio_write(tp, MII_BMCR, data);
1931
1932 for (i = 0; i < 50; i++) {
1933 msleep(20);
1934 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
1935 break;
1936 }
1937}
1938
hayeswang43499682014-02-18 21:48:58 +08001939static void rtl_clear_bp(struct r8152 *tp)
1940{
1941 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1942 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1943 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1944 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1945 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1946 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1947 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1948 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1949 mdelay(3);
1950 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1951 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1952}
1953
1954static void r8153_clear_bp(struct r8152 *tp)
1955{
1956 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
1957 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
1958 rtl_clear_bp(tp);
1959}
1960
1961static void r8153_teredo_off(struct r8152 *tp)
1962{
1963 u32 ocp_data;
1964
1965 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1966 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1967 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1968
1969 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1970 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1971 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1972}
1973
1974static void r8152b_disable_aldps(struct r8152 *tp)
1975{
1976 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1977 msleep(20);
1978}
1979
1980static inline void r8152b_enable_aldps(struct r8152 *tp)
1981{
1982 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1983 LINKENA | DIS_SDSAVE);
1984}
1985
1986static void r8152b_hw_phy_cfg(struct r8152 *tp)
1987{
hayeswangf0cbe0a2014-02-18 21:49:03 +08001988 u16 data;
1989
1990 data = r8152_mdio_read(tp, MII_BMCR);
1991 if (data & BMCR_PDOWN) {
1992 data &= ~BMCR_PDOWN;
1993 r8152_mdio_write(tp, MII_BMCR, data);
1994 }
1995
hayeswang43499682014-02-18 21:48:58 +08001996 r8152b_disable_aldps(tp);
hayeswang7e9da482014-02-18 21:49:05 +08001997
1998 rtl_clear_bp(tp);
1999
2000 r8152b_enable_aldps(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002001 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002002}
2003
hayeswangac718b62013-05-02 16:01:25 +00002004static void r8152b_exit_oob(struct r8152 *tp)
2005{
hayeswangdb8515e2014-03-06 15:07:16 +08002006 u32 ocp_data;
2007 int i;
hayeswangac718b62013-05-02 16:01:25 +00002008
2009 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2010 ocp_data &= ~RCR_ACPT_ALL;
2011 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2012
hayeswang00a5e362014-02-18 21:48:59 +08002013 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002014 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002015 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002016
2017 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2018 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2019
2020 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2021 ocp_data &= ~NOW_IS_OOB;
2022 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2023
2024 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2025 ocp_data &= ~MCU_BORW_EN;
2026 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2027
2028 for (i = 0; i < 1000; i++) {
2029 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2030 if (ocp_data & LINK_LIST_READY)
2031 break;
2032 mdelay(1);
2033 }
2034
2035 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2036 ocp_data |= RE_INIT_LL;
2037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2038
2039 for (i = 0; i < 1000; i++) {
2040 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2041 if (ocp_data & LINK_LIST_READY)
2042 break;
2043 mdelay(1);
2044 }
2045
2046 rtl8152_nic_reset(tp);
2047
2048 /* rx share fifo credit full threshold */
2049 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2050
2051 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
2052 ocp_data &= STAT_SPEED_MASK;
2053 if (ocp_data == STAT_SPEED_FULL) {
2054 /* rx share fifo credit near full threshold */
2055 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2056 RXFIFO_THR2_FULL);
2057 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2058 RXFIFO_THR3_FULL);
2059 } else {
2060 /* rx share fifo credit near full threshold */
2061 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2062 RXFIFO_THR2_HIGH);
2063 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2064 RXFIFO_THR3_HIGH);
2065 }
2066
2067 /* TX share fifo free credit full threshold */
2068 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2069
2070 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002071 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002072 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2073 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2074
2075 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2076 ocp_data &= ~CPCR_RX_VLAN;
2077 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2078
2079 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2080
2081 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2082 ocp_data |= TCR0_AUTO_FIFO;
2083 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2084}
2085
2086static void r8152b_enter_oob(struct r8152 *tp)
2087{
hayeswang45f4a192014-01-06 17:08:41 +08002088 u32 ocp_data;
2089 int i;
hayeswangac718b62013-05-02 16:01:25 +00002090
2091 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2092 ocp_data &= ~NOW_IS_OOB;
2093 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2094
2095 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2096 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2097 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2098
2099 rtl8152_disable(tp);
2100
2101 for (i = 0; i < 1000; i++) {
2102 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2103 if (ocp_data & LINK_LIST_READY)
2104 break;
2105 mdelay(1);
2106 }
2107
2108 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2109 ocp_data |= RE_INIT_LL;
2110 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2111
2112 for (i = 0; i < 1000; i++) {
2113 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2114 if (ocp_data & LINK_LIST_READY)
2115 break;
2116 mdelay(1);
2117 }
2118
2119 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2120
hayeswangac718b62013-05-02 16:01:25 +00002121 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2122 ocp_data |= CPCR_RX_VLAN;
2123 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2124
2125 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2126 ocp_data |= ALDPS_PROXY_MODE;
2127 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2128
2129 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2130 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2131 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2132
hayeswang00a5e362014-02-18 21:48:59 +08002133 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002134
2135 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2136 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2137 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2138}
2139
hayeswang43779f82014-01-02 11:25:10 +08002140static void r8153_hw_phy_cfg(struct r8152 *tp)
2141{
2142 u32 ocp_data;
2143 u16 data;
2144
2145 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002146 data = r8152_mdio_read(tp, MII_BMCR);
2147 if (data & BMCR_PDOWN) {
2148 data &= ~BMCR_PDOWN;
2149 r8152_mdio_write(tp, MII_BMCR, data);
2150 }
hayeswang43779f82014-01-02 11:25:10 +08002151
hayeswang7e9da482014-02-18 21:49:05 +08002152 r8153_clear_bp(tp);
2153
hayeswang43779f82014-01-02 11:25:10 +08002154 if (tp->version == RTL_VER_03) {
2155 data = ocp_reg_read(tp, OCP_EEE_CFG);
2156 data &= ~CTAP_SHORT_EN;
2157 ocp_reg_write(tp, OCP_EEE_CFG, data);
2158 }
2159
2160 data = ocp_reg_read(tp, OCP_POWER_CFG);
2161 data |= EEE_CLKDIV_EN;
2162 ocp_reg_write(tp, OCP_POWER_CFG, data);
2163
2164 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2165 data |= EN_10M_BGOFF;
2166 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2167 data = ocp_reg_read(tp, OCP_POWER_CFG);
2168 data |= EN_10M_PLLOFF;
2169 ocp_reg_write(tp, OCP_POWER_CFG, data);
2170 data = sram_read(tp, SRAM_IMPEDANCE);
2171 data &= ~RX_DRIVING_MASK;
2172 sram_write(tp, SRAM_IMPEDANCE, data);
2173
2174 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2175 ocp_data |= PFM_PWM_SWITCH;
2176 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2177
2178 data = sram_read(tp, SRAM_LPF_CFG);
2179 data |= LPF_AUTO_TUNE;
2180 sram_write(tp, SRAM_LPF_CFG, data);
2181
2182 data = sram_read(tp, SRAM_10M_AMP1);
2183 data |= GDAC_IB_UPALL;
2184 sram_write(tp, SRAM_10M_AMP1, data);
2185 data = sram_read(tp, SRAM_10M_AMP2);
2186 data |= AMP_DN;
2187 sram_write(tp, SRAM_10M_AMP2, data);
hayeswangaa66a5f2014-02-18 21:49:04 +08002188
2189 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002190}
2191
hayeswangb9702722014-02-18 21:49:00 +08002192static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002193{
2194 u8 u1u2[8];
2195
2196 if (enable)
2197 memset(u1u2, 0xff, sizeof(u1u2));
2198 else
2199 memset(u1u2, 0x00, sizeof(u1u2));
2200
2201 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2202}
2203
hayeswangb9702722014-02-18 21:49:00 +08002204static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002205{
2206 u32 ocp_data;
2207
2208 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2209 if (enable)
2210 ocp_data |= U2P3_ENABLE;
2211 else
2212 ocp_data &= ~U2P3_ENABLE;
2213 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2214}
2215
hayeswangb9702722014-02-18 21:49:00 +08002216static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002217{
2218 u32 ocp_data;
2219
2220 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2221 if (enable)
2222 ocp_data |= PWR_EN | PHASE2_EN;
2223 else
2224 ocp_data &= ~(PWR_EN | PHASE2_EN);
2225 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2226
2227 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2228 ocp_data &= ~PCUT_STATUS;
2229 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2230}
2231
hayeswang43779f82014-01-02 11:25:10 +08002232static void r8153_first_init(struct r8152 *tp)
2233{
2234 u32 ocp_data;
2235 int i;
2236
hayeswang00a5e362014-02-18 21:48:59 +08002237 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002238 r8153_teredo_off(tp);
2239
2240 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2241 ocp_data &= ~RCR_ACPT_ALL;
2242 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2243
2244 r8153_hw_phy_cfg(tp);
2245
2246 rtl8152_nic_reset(tp);
2247
2248 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2249 ocp_data &= ~NOW_IS_OOB;
2250 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2251
2252 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2253 ocp_data &= ~MCU_BORW_EN;
2254 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2255
2256 for (i = 0; i < 1000; i++) {
2257 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2258 if (ocp_data & LINK_LIST_READY)
2259 break;
2260 mdelay(1);
2261 }
2262
2263 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2264 ocp_data |= RE_INIT_LL;
2265 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2266
2267 for (i = 0; i < 1000; i++) {
2268 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2269 if (ocp_data & LINK_LIST_READY)
2270 break;
2271 mdelay(1);
2272 }
2273
2274 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2275 ocp_data &= ~CPCR_RX_VLAN;
2276 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2277
2278 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2279
2280 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2281 ocp_data |= TCR0_AUTO_FIFO;
2282 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2283
2284 rtl8152_nic_reset(tp);
2285
2286 /* rx share fifo credit full threshold */
2287 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2288 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2289 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2290 /* TX share fifo free credit full threshold */
2291 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2292
hayeswang9629e3c2014-01-15 10:42:15 +08002293 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002294 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2295 ocp_data &= ~RX_AGG_DISABLE;
2296 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2297}
2298
2299static void r8153_enter_oob(struct r8152 *tp)
2300{
2301 u32 ocp_data;
2302 int i;
2303
2304 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2305 ocp_data &= ~NOW_IS_OOB;
2306 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2307
2308 rtl8152_disable(tp);
2309
2310 for (i = 0; i < 1000; i++) {
2311 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2312 if (ocp_data & LINK_LIST_READY)
2313 break;
2314 mdelay(1);
2315 }
2316
2317 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2318 ocp_data |= RE_INIT_LL;
2319 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2320
2321 for (i = 0; i < 1000; i++) {
2322 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2323 if (ocp_data & LINK_LIST_READY)
2324 break;
2325 mdelay(1);
2326 }
2327
2328 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2329
hayeswang43779f82014-01-02 11:25:10 +08002330 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2331 ocp_data &= ~TEREDO_WAKE_MASK;
2332 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2333
2334 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2335 ocp_data |= CPCR_RX_VLAN;
2336 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2337
2338 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2339 ocp_data |= ALDPS_PROXY_MODE;
2340 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2341
2342 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2343 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2344 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2345
hayeswang00a5e362014-02-18 21:48:59 +08002346 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002347
2348 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2349 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2350 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2351}
2352
2353static void r8153_disable_aldps(struct r8152 *tp)
2354{
2355 u16 data;
2356
2357 data = ocp_reg_read(tp, OCP_POWER_CFG);
2358 data &= ~EN_ALDPS;
2359 ocp_reg_write(tp, OCP_POWER_CFG, data);
2360 msleep(20);
2361}
2362
2363static void r8153_enable_aldps(struct r8152 *tp)
2364{
2365 u16 data;
2366
2367 data = ocp_reg_read(tp, OCP_POWER_CFG);
2368 data |= EN_ALDPS;
2369 ocp_reg_write(tp, OCP_POWER_CFG, data);
2370}
2371
hayeswangac718b62013-05-02 16:01:25 +00002372static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2373{
hayeswang43779f82014-01-02 11:25:10 +08002374 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002375 int ret = 0;
2376
2377 cancel_delayed_work_sync(&tp->schedule);
2378 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2379 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2380 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002381 if (tp->mii.supports_gmii) {
2382 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2383 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2384 } else {
2385 gbcr = 0;
2386 }
hayeswangac718b62013-05-02 16:01:25 +00002387
2388 if (autoneg == AUTONEG_DISABLE) {
2389 if (speed == SPEED_10) {
2390 bmcr = 0;
2391 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2392 } else if (speed == SPEED_100) {
2393 bmcr = BMCR_SPEED100;
2394 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002395 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2396 bmcr = BMCR_SPEED1000;
2397 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002398 } else {
2399 ret = -EINVAL;
2400 goto out;
2401 }
2402
2403 if (duplex == DUPLEX_FULL)
2404 bmcr |= BMCR_FULLDPLX;
2405 } else {
2406 if (speed == SPEED_10) {
2407 if (duplex == DUPLEX_FULL)
2408 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2409 else
2410 anar |= ADVERTISE_10HALF;
2411 } else if (speed == SPEED_100) {
2412 if (duplex == DUPLEX_FULL) {
2413 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2414 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2415 } else {
2416 anar |= ADVERTISE_10HALF;
2417 anar |= ADVERTISE_100HALF;
2418 }
hayeswang43779f82014-01-02 11:25:10 +08002419 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2420 if (duplex == DUPLEX_FULL) {
2421 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2422 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2423 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2424 } else {
2425 anar |= ADVERTISE_10HALF;
2426 anar |= ADVERTISE_100HALF;
2427 gbcr |= ADVERTISE_1000HALF;
2428 }
hayeswangac718b62013-05-02 16:01:25 +00002429 } else {
2430 ret = -EINVAL;
2431 goto out;
2432 }
2433
2434 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2435 }
2436
hayeswangaa66a5f2014-02-18 21:49:04 +08002437 if (test_bit(PHY_RESET, &tp->flags))
2438 bmcr |= BMCR_RESET;
2439
hayeswang43779f82014-01-02 11:25:10 +08002440 if (tp->mii.supports_gmii)
2441 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2442
hayeswangac718b62013-05-02 16:01:25 +00002443 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2444 r8152_mdio_write(tp, MII_BMCR, bmcr);
2445
hayeswangaa66a5f2014-02-18 21:49:04 +08002446 if (test_bit(PHY_RESET, &tp->flags)) {
2447 int i;
2448
2449 clear_bit(PHY_RESET, &tp->flags);
2450 for (i = 0; i < 50; i++) {
2451 msleep(20);
2452 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2453 break;
2454 }
2455 }
2456
hayeswangac718b62013-05-02 16:01:25 +00002457out:
hayeswangac718b62013-05-02 16:01:25 +00002458
2459 return ret;
2460}
2461
2462static void rtl8152_down(struct r8152 *tp)
2463{
hayeswang00a5e362014-02-18 21:48:59 +08002464 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002465 r8152b_disable_aldps(tp);
2466 r8152b_enter_oob(tp);
2467 r8152b_enable_aldps(tp);
2468}
2469
hayeswang43779f82014-01-02 11:25:10 +08002470static void rtl8153_down(struct r8152 *tp)
2471{
hayeswangb9702722014-02-18 21:49:00 +08002472 r8153_u1u2en(tp, false);
2473 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002474 r8153_disable_aldps(tp);
2475 r8153_enter_oob(tp);
2476 r8153_enable_aldps(tp);
2477}
2478
hayeswangac718b62013-05-02 16:01:25 +00002479static void set_carrier(struct r8152 *tp)
2480{
2481 struct net_device *netdev = tp->netdev;
2482 u8 speed;
2483
hayeswang40a82912013-08-14 20:54:40 +08002484 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002485 speed = rtl8152_get_speed(tp);
2486
2487 if (speed & LINK_STATUS) {
2488 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002489 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002490 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2491 netif_carrier_on(netdev);
2492 }
2493 } else {
2494 if (tp->speed & LINK_STATUS) {
2495 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002496 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002497 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002498 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002499 }
2500 }
2501 tp->speed = speed;
2502}
2503
2504static void rtl_work_func_t(struct work_struct *work)
2505{
2506 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2507
hayeswang9a4be1b2014-02-18 21:49:07 +08002508 if (usb_autopm_get_interface(tp->intf) < 0)
2509 return;
2510
hayeswangac718b62013-05-02 16:01:25 +00002511 if (!test_bit(WORK_ENABLE, &tp->flags))
2512 goto out1;
2513
2514 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2515 goto out1;
2516
hayeswang40a82912013-08-14 20:54:40 +08002517 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2518 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002519
2520 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2521 _rtl8152_set_rx_mode(tp->netdev);
2522
hayeswang9a4be1b2014-02-18 21:49:07 +08002523 if (tp->speed & LINK_STATUS)
2524 tx_bottom(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002525
2526 if (test_bit(PHY_RESET, &tp->flags))
2527 rtl_phy_reset(tp);
2528
hayeswangac718b62013-05-02 16:01:25 +00002529out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002530 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002531}
2532
2533static int rtl8152_open(struct net_device *netdev)
2534{
2535 struct r8152 *tp = netdev_priv(netdev);
2536 int res = 0;
2537
hayeswang7e9da482014-02-18 21:49:05 +08002538 res = alloc_all_mem(tp);
2539 if (res)
2540 goto out;
2541
hayeswang9a4be1b2014-02-18 21:49:07 +08002542 res = usb_autopm_get_interface(tp->intf);
2543 if (res < 0) {
2544 free_all_mem(tp);
2545 goto out;
2546 }
2547
2548 /* The WORK_ENABLE may be set when autoresume occurs */
2549 if (test_bit(WORK_ENABLE, &tp->flags)) {
2550 clear_bit(WORK_ENABLE, &tp->flags);
2551 usb_kill_urb(tp->intr_urb);
2552 cancel_delayed_work_sync(&tp->schedule);
2553 if (tp->speed & LINK_STATUS)
2554 tp->rtl_ops.disable(tp);
2555 }
2556
hayeswang7e9da482014-02-18 21:49:05 +08002557 tp->rtl_ops.up(tp);
2558
hayeswang43779f82014-01-02 11:25:10 +08002559 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2560 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2561 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002562 tp->speed = 0;
2563 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002564 netif_start_queue(netdev);
2565 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08002566
hayeswang3d55f442014-02-06 11:55:48 +08002567 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2568 if (res) {
2569 if (res == -ENODEV)
2570 netif_device_detach(tp->netdev);
2571 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2572 res);
hayeswang7e9da482014-02-18 21:49:05 +08002573 free_all_mem(tp);
hayeswang3d55f442014-02-06 11:55:48 +08002574 }
2575
hayeswang9a4be1b2014-02-18 21:49:07 +08002576 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002577
hayeswang7e9da482014-02-18 21:49:05 +08002578out:
hayeswangac718b62013-05-02 16:01:25 +00002579 return res;
2580}
2581
2582static int rtl8152_close(struct net_device *netdev)
2583{
2584 struct r8152 *tp = netdev_priv(netdev);
2585 int res = 0;
2586
2587 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002588 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002589 cancel_delayed_work_sync(&tp->schedule);
2590 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002591
2592 res = usb_autopm_get_interface(tp->intf);
2593 if (res < 0) {
2594 rtl_drop_queued_tx(tp);
2595 } else {
2596 /*
2597 * The autosuspend may have been enabled and wouldn't
2598 * be disable when autoresume occurs, because the
2599 * netif_running() would be false.
2600 */
2601 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2602 rtl_runtime_suspend_enable(tp, false);
2603 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2604 }
2605
2606 tasklet_disable(&tp->tl);
2607 tp->rtl_ops.down(tp);
2608 tasklet_enable(&tp->tl);
2609 usb_autopm_put_interface(tp->intf);
2610 }
hayeswangac718b62013-05-02 16:01:25 +00002611
hayeswang7e9da482014-02-18 21:49:05 +08002612 free_all_mem(tp);
2613
hayeswangac718b62013-05-02 16:01:25 +00002614 return res;
2615}
2616
hayeswangac718b62013-05-02 16:01:25 +00002617static void r8152b_enable_eee(struct r8152 *tp)
2618{
hayeswang45f4a192014-01-06 17:08:41 +08002619 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002620
2621 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2622 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2623 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2624 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2625 EEE_10_CAP | EEE_NWAY_EN |
2626 TX_QUIET_EN | RX_QUIET_EN |
2627 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2628 SDFALLTIME);
2629 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2630 RG_LDVQUIET_EN | RG_CKRSEL |
2631 RG_EEEPRG_EN);
2632 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2633 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2634 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2635 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2636 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2637 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2638}
2639
hayeswang43779f82014-01-02 11:25:10 +08002640static void r8153_enable_eee(struct r8152 *tp)
2641{
2642 u32 ocp_data;
2643 u16 data;
2644
2645 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2646 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2647 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2648 data = ocp_reg_read(tp, OCP_EEE_CFG);
2649 data |= EEE10_EN;
2650 ocp_reg_write(tp, OCP_EEE_CFG, data);
2651 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2652 data |= MY1000_EEE | MY100_EEE;
2653 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2654}
2655
hayeswangac718b62013-05-02 16:01:25 +00002656static void r8152b_enable_fc(struct r8152 *tp)
2657{
2658 u16 anar;
2659
2660 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2661 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2662 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2663}
2664
hayeswangac718b62013-05-02 16:01:25 +00002665static void r8152b_init(struct r8152 *tp)
2666{
hayeswangebc2ec482013-08-14 20:54:38 +08002667 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002668
hayeswangac718b62013-05-02 16:01:25 +00002669 if (tp->version == RTL_VER_01) {
2670 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2671 ocp_data &= ~LED_MODE_MASK;
2672 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2673 }
2674
hayeswang00a5e362014-02-18 21:48:59 +08002675 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002676
hayeswangac718b62013-05-02 16:01:25 +00002677 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2678 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2679 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2680 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2681 ocp_data &= ~MCU_CLK_RATIO_MASK;
2682 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2683 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2684 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2685 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2686 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2687
2688 r8152b_enable_eee(tp);
2689 r8152b_enable_aldps(tp);
2690 r8152b_enable_fc(tp);
2691
hayeswangebc2ec482013-08-14 20:54:38 +08002692 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002693 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08002694 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002695 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2696}
2697
hayeswang43779f82014-01-02 11:25:10 +08002698static void r8153_init(struct r8152 *tp)
2699{
2700 u32 ocp_data;
2701 int i;
2702
hayeswangb9702722014-02-18 21:49:00 +08002703 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002704
2705 for (i = 0; i < 500; i++) {
2706 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2707 AUTOLOAD_DONE)
2708 break;
2709 msleep(20);
2710 }
2711
2712 for (i = 0; i < 500; i++) {
2713 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2714 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2715 break;
2716 msleep(20);
2717 }
2718
hayeswangb9702722014-02-18 21:49:00 +08002719 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002720
2721 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2722 ocp_data &= ~TIMER11_EN;
2723 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2724
hayeswang43779f82014-01-02 11:25:10 +08002725 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2726 ocp_data &= ~LED_MODE_MASK;
2727 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2728
2729 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2730 ocp_data &= ~LPM_TIMER_MASK;
2731 if (tp->udev->speed == USB_SPEED_SUPER)
2732 ocp_data |= LPM_TIMER_500US;
2733 else
2734 ocp_data |= LPM_TIMER_500MS;
2735 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2736
2737 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2738 ocp_data &= ~SEN_VAL_MASK;
2739 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2740 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2741
hayeswangb9702722014-02-18 21:49:00 +08002742 r8153_power_cut_en(tp, false);
2743 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002744
hayeswang43779f82014-01-02 11:25:10 +08002745 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2746 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2747 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2748 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2749 U1U2_SPDWN_EN | L1_SPDWN_EN);
2750 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2751 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2752 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2753 EEE_SPDWN_EN);
2754
2755 r8153_enable_eee(tp);
2756 r8153_enable_aldps(tp);
2757 r8152b_enable_fc(tp);
hayeswang43779f82014-01-02 11:25:10 +08002758}
2759
hayeswangac718b62013-05-02 16:01:25 +00002760static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2761{
2762 struct r8152 *tp = usb_get_intfdata(intf);
2763
hayeswang9a4be1b2014-02-18 21:49:07 +08002764 if (PMSG_IS_AUTO(message))
2765 set_bit(SELECTIVE_SUSPEND, &tp->flags);
2766 else
2767 netif_device_detach(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002768
2769 if (netif_running(tp->netdev)) {
2770 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002771 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002772 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08002773 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2774 rtl_runtime_suspend_enable(tp, true);
2775 } else {
2776 tasklet_disable(&tp->tl);
2777 tp->rtl_ops.down(tp);
2778 tasklet_enable(&tp->tl);
2779 }
hayeswangac718b62013-05-02 16:01:25 +00002780 }
2781
hayeswangac718b62013-05-02 16:01:25 +00002782 return 0;
2783}
2784
2785static int rtl8152_resume(struct usb_interface *intf)
2786{
2787 struct r8152 *tp = usb_get_intfdata(intf);
2788
hayeswang9a4be1b2014-02-18 21:49:07 +08002789 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2790 tp->rtl_ops.init(tp);
2791 netif_device_attach(tp->netdev);
2792 }
2793
hayeswangac718b62013-05-02 16:01:25 +00002794 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08002795 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2796 rtl_runtime_suspend_enable(tp, false);
2797 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2798 if (tp->speed & LINK_STATUS)
2799 tp->rtl_ops.disable(tp);
2800 } else {
2801 tp->rtl_ops.up(tp);
2802 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswang43779f82014-01-02 11:25:10 +08002803 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2804 DUPLEX_FULL);
hayeswang9a4be1b2014-02-18 21:49:07 +08002805 }
hayeswang40a82912013-08-14 20:54:40 +08002806 tp->speed = 0;
2807 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002808 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002809 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangac718b62013-05-02 16:01:25 +00002810 }
2811
2812 return 0;
2813}
2814
hayeswang21ff2e82014-02-18 21:49:06 +08002815static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2816{
2817 struct r8152 *tp = netdev_priv(dev);
2818
hayeswang9a4be1b2014-02-18 21:49:07 +08002819 if (usb_autopm_get_interface(tp->intf) < 0)
2820 return;
2821
hayeswang21ff2e82014-02-18 21:49:06 +08002822 wol->supported = WAKE_ANY;
2823 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08002824
2825 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08002826}
2827
2828static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2829{
2830 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002831 int ret;
2832
2833 ret = usb_autopm_get_interface(tp->intf);
2834 if (ret < 0)
2835 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08002836
2837 __rtl_set_wol(tp, wol->wolopts);
2838 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
2839
hayeswang9a4be1b2014-02-18 21:49:07 +08002840 usb_autopm_put_interface(tp->intf);
2841
2842out_set_wol:
2843 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08002844}
2845
hayeswanga5ec27c2014-02-18 21:49:11 +08002846static u32 rtl8152_get_msglevel(struct net_device *dev)
2847{
2848 struct r8152 *tp = netdev_priv(dev);
2849
2850 return tp->msg_enable;
2851}
2852
2853static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
2854{
2855 struct r8152 *tp = netdev_priv(dev);
2856
2857 tp->msg_enable = value;
2858}
2859
hayeswangac718b62013-05-02 16:01:25 +00002860static void rtl8152_get_drvinfo(struct net_device *netdev,
2861 struct ethtool_drvinfo *info)
2862{
2863 struct r8152 *tp = netdev_priv(netdev);
2864
2865 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2866 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2867 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2868}
2869
2870static
2871int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2872{
2873 struct r8152 *tp = netdev_priv(netdev);
2874
2875 if (!tp->mii.mdio_read)
2876 return -EOPNOTSUPP;
2877
2878 return mii_ethtool_gset(&tp->mii, cmd);
2879}
2880
2881static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2882{
2883 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002884 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002885
hayeswang9a4be1b2014-02-18 21:49:07 +08002886 ret = usb_autopm_get_interface(tp->intf);
2887 if (ret < 0)
2888 goto out;
2889
2890 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2891
2892 usb_autopm_put_interface(tp->intf);
2893
2894out:
2895 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002896}
2897
2898static struct ethtool_ops ops = {
2899 .get_drvinfo = rtl8152_get_drvinfo,
2900 .get_settings = rtl8152_get_settings,
2901 .set_settings = rtl8152_set_settings,
2902 .get_link = ethtool_op_get_link,
hayeswanga5ec27c2014-02-18 21:49:11 +08002903 .get_msglevel = rtl8152_get_msglevel,
2904 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08002905 .get_wol = rtl8152_get_wol,
2906 .set_wol = rtl8152_set_wol,
hayeswangac718b62013-05-02 16:01:25 +00002907};
2908
2909static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2910{
2911 struct r8152 *tp = netdev_priv(netdev);
2912 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08002913 int res;
2914
2915 res = usb_autopm_get_interface(tp->intf);
2916 if (res < 0)
2917 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002918
2919 switch (cmd) {
2920 case SIOCGMIIPHY:
2921 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2922 break;
2923
2924 case SIOCGMIIREG:
2925 data->val_out = r8152_mdio_read(tp, data->reg_num);
2926 break;
2927
2928 case SIOCSMIIREG:
2929 if (!capable(CAP_NET_ADMIN)) {
2930 res = -EPERM;
2931 break;
2932 }
2933 r8152_mdio_write(tp, data->reg_num, data->val_in);
2934 break;
2935
2936 default:
2937 res = -EOPNOTSUPP;
2938 }
2939
hayeswang9a4be1b2014-02-18 21:49:07 +08002940 usb_autopm_put_interface(tp->intf);
2941
2942out:
hayeswangac718b62013-05-02 16:01:25 +00002943 return res;
2944}
2945
2946static const struct net_device_ops rtl8152_netdev_ops = {
2947 .ndo_open = rtl8152_open,
2948 .ndo_stop = rtl8152_close,
2949 .ndo_do_ioctl = rtl8152_ioctl,
2950 .ndo_start_xmit = rtl8152_start_xmit,
2951 .ndo_tx_timeout = rtl8152_tx_timeout,
2952 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2953 .ndo_set_mac_address = rtl8152_set_mac_address,
2954
2955 .ndo_change_mtu = eth_change_mtu,
2956 .ndo_validate_addr = eth_validate_addr,
2957};
2958
2959static void r8152b_get_version(struct r8152 *tp)
2960{
2961 u32 ocp_data;
2962 u16 version;
2963
2964 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2965 version = (u16)(ocp_data & VERSION_MASK);
2966
2967 switch (version) {
2968 case 0x4c00:
2969 tp->version = RTL_VER_01;
2970 break;
2971 case 0x4c10:
2972 tp->version = RTL_VER_02;
2973 break;
hayeswang43779f82014-01-02 11:25:10 +08002974 case 0x5c00:
2975 tp->version = RTL_VER_03;
2976 tp->mii.supports_gmii = 1;
2977 break;
2978 case 0x5c10:
2979 tp->version = RTL_VER_04;
2980 tp->mii.supports_gmii = 1;
2981 break;
2982 case 0x5c20:
2983 tp->version = RTL_VER_05;
2984 tp->mii.supports_gmii = 1;
2985 break;
hayeswangac718b62013-05-02 16:01:25 +00002986 default:
2987 netif_info(tp, probe, tp->netdev,
2988 "Unknown version 0x%04x\n", version);
2989 break;
2990 }
2991}
2992
hayeswange3fe0b12014-01-02 11:22:39 +08002993static void rtl8152_unload(struct r8152 *tp)
2994{
hayeswang00a5e362014-02-18 21:48:59 +08002995 if (tp->version != RTL_VER_01)
2996 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08002997}
2998
hayeswang43779f82014-01-02 11:25:10 +08002999static void rtl8153_unload(struct r8152 *tp)
3000{
hayeswangb9702722014-02-18 21:49:00 +08003001 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003002}
3003
hayeswang31ca1de2014-01-06 17:08:43 +08003004static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08003005{
3006 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08003007 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08003008
3009 switch (id->idVendor) {
3010 case VENDOR_ID_REALTEK:
3011 switch (id->idProduct) {
3012 case PRODUCT_ID_RTL8152:
3013 ops->init = r8152b_init;
3014 ops->enable = rtl8152_enable;
3015 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003016 ops->up = r8152b_exit_oob;
hayeswangc81229c2014-01-02 11:22:42 +08003017 ops->down = rtl8152_down;
3018 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003019 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003020 break;
hayeswang43779f82014-01-02 11:25:10 +08003021 case PRODUCT_ID_RTL8153:
3022 ops->init = r8153_init;
3023 ops->enable = rtl8153_enable;
3024 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003025 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003026 ops->down = rtl8153_down;
3027 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003028 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003029 break;
3030 default:
hayeswang43779f82014-01-02 11:25:10 +08003031 break;
3032 }
3033 break;
3034
3035 case VENDOR_ID_SAMSUNG:
3036 switch (id->idProduct) {
3037 case PRODUCT_ID_SAMSUNG:
3038 ops->init = r8153_init;
3039 ops->enable = rtl8153_enable;
3040 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003041 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003042 ops->down = rtl8153_down;
3043 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003044 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003045 break;
hayeswangc81229c2014-01-02 11:22:42 +08003046 default:
hayeswangc81229c2014-01-02 11:22:42 +08003047 break;
3048 }
3049 break;
3050
3051 default:
hayeswangc81229c2014-01-02 11:22:42 +08003052 break;
3053 }
3054
hayeswang31ca1de2014-01-06 17:08:43 +08003055 if (ret)
3056 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
3057
hayeswangc81229c2014-01-02 11:22:42 +08003058 return ret;
3059}
3060
hayeswangac718b62013-05-02 16:01:25 +00003061static int rtl8152_probe(struct usb_interface *intf,
3062 const struct usb_device_id *id)
3063{
3064 struct usb_device *udev = interface_to_usbdev(intf);
3065 struct r8152 *tp;
3066 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08003067 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003068
hayeswangac718b62013-05-02 16:01:25 +00003069 netdev = alloc_etherdev(sizeof(struct r8152));
3070 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003071 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003072 return -ENOMEM;
3073 }
3074
hayeswangebc2ec482013-08-14 20:54:38 +08003075 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003076 tp = netdev_priv(netdev);
3077 tp->msg_enable = 0x7FFF;
3078
hayeswange3ad4122014-01-06 17:08:42 +08003079 tp->udev = udev;
3080 tp->netdev = netdev;
3081 tp->intf = intf;
3082
hayeswang31ca1de2014-01-06 17:08:43 +08003083 ret = rtl_ops_init(tp, id);
3084 if (ret)
3085 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003086
hayeswangebc2ec482013-08-14 20:54:38 +08003087 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00003088 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3089
hayeswangac718b62013-05-02 16:01:25 +00003090 netdev->netdev_ops = &rtl8152_netdev_ops;
3091 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003092
3093 netdev->features |= NETIF_F_IP_CSUM;
3094 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangdb8515e2014-03-06 15:07:16 +08003095
hayeswangac718b62013-05-02 16:01:25 +00003096 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00003097
3098 tp->mii.dev = netdev;
3099 tp->mii.mdio_read = read_mii_word;
3100 tp->mii.mdio_write = write_mii_word;
3101 tp->mii.phy_id_mask = 0x3f;
3102 tp->mii.reg_num_mask = 0x1f;
3103 tp->mii.phy_id = R8152_PHY_ID;
3104 tp->mii.supports_gmii = 0;
3105
hayeswang9a4be1b2014-02-18 21:49:07 +08003106 intf->needs_remote_wakeup = 1;
3107
hayeswangac718b62013-05-02 16:01:25 +00003108 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08003109 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003110 set_ethernet_addr(tp);
3111
hayeswangac718b62013-05-02 16:01:25 +00003112 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00003113
hayeswangebc2ec482013-08-14 20:54:38 +08003114 ret = register_netdev(netdev);
3115 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003116 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08003117 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003118 }
3119
hayeswang21ff2e82014-02-18 21:49:06 +08003120 tp->saved_wolopts = __rtl_get_wol(tp);
3121 if (tp->saved_wolopts)
3122 device_set_wakeup_enable(&udev->dev, true);
3123 else
3124 device_set_wakeup_enable(&udev->dev, false);
3125
Hayes Wang4a8deae2014-01-07 11:18:22 +08003126 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003127
3128 return 0;
3129
hayeswangac718b62013-05-02 16:01:25 +00003130out1:
hayeswangebc2ec482013-08-14 20:54:38 +08003131 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00003132out:
3133 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08003134 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003135}
3136
hayeswangac718b62013-05-02 16:01:25 +00003137static void rtl8152_disconnect(struct usb_interface *intf)
3138{
3139 struct r8152 *tp = usb_get_intfdata(intf);
3140
3141 usb_set_intfdata(intf, NULL);
3142 if (tp) {
3143 set_bit(RTL8152_UNPLUG, &tp->flags);
3144 tasklet_kill(&tp->tl);
3145 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08003146 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00003147 free_netdev(tp->netdev);
3148 }
3149}
3150
3151/* table of devices that work with this driver */
3152static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08003153 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
3154 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
3155 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00003156 {}
3157};
3158
3159MODULE_DEVICE_TABLE(usb, rtl8152_table);
3160
3161static struct usb_driver rtl8152_driver = {
3162 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08003163 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00003164 .probe = rtl8152_probe,
3165 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00003166 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08003167 .resume = rtl8152_resume,
3168 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08003169 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08003170 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00003171};
3172
Sachin Kamatb4236daa2013-05-16 17:48:08 +00003173module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00003174
3175MODULE_AUTHOR(DRIVER_AUTHOR);
3176MODULE_DESCRIPTION(DRIVER_DESC);
3177MODULE_LICENSE("GPL");