blob: b8eee365e15dcc733db445cff3cc49566d9347bc [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>
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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;
hayeswanga5a4f462013-08-16 16:09:34 +0800966 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +0000967 int status = urb->status;
hayeswangebc2ec42013-08-14 20:54:38 +0800968 struct rx_agg *agg;
969 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000970 int result;
hayeswangac718b62013-05-02 16:01:25 +0000971
hayeswangebc2ec42013-08-14 20:54:38 +0800972 agg = urb->context;
973 if (!agg)
974 return;
975
976 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000977 if (!tp)
978 return;
hayeswangebc2ec42013-08-14 20:54:38 +0800979
hayeswangac718b62013-05-02 16:01:25 +0000980 if (test_bit(RTL8152_UNPLUG, &tp->flags))
981 return;
hayeswangebc2ec42013-08-14 20:54:38 +0800982
983 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000984 return;
985
hayeswangebc2ec42013-08-14 20:54:38 +0800986 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800987
988 /* When link down, the driver would cancel all bulks. */
989 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +0800990 if (!netif_carrier_ok(netdev))
991 return;
992
hayeswang9a4be1b2014-02-18 21:49:07 +0800993 usb_mark_last_busy(tp->udev);
994
hayeswangac718b62013-05-02 16:01:25 +0000995 switch (status) {
996 case 0:
hayeswangebc2ec42013-08-14 20:54:38 +0800997 if (urb->actual_length < ETH_ZLEN)
998 break;
999
hayeswanga5a4f462013-08-16 16:09:34 +08001000 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001001 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +08001002 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001003 tasklet_schedule(&tp->tl);
1004 return;
hayeswangac718b62013-05-02 16:01:25 +00001005 case -ESHUTDOWN:
1006 set_bit(RTL8152_UNPLUG, &tp->flags);
1007 netif_device_detach(tp->netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001008 return;
hayeswangac718b62013-05-02 16:01:25 +00001009 case -ENOENT:
1010 return; /* the urb is in unlink state */
1011 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001012 if (net_ratelimit())
1013 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001014 break;
hayeswangac718b62013-05-02 16:01:25 +00001015 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001016 if (net_ratelimit())
1017 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001018 break;
hayeswangac718b62013-05-02 16:01:25 +00001019 }
1020
hayeswangebc2ec42013-08-14 20:54:38 +08001021 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001022 if (result == -ENODEV) {
1023 netif_device_detach(tp->netdev);
1024 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +08001025 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001026 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +08001027 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001028 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001029 }
hayeswangac718b62013-05-02 16:01:25 +00001030}
1031
1032static void write_bulk_callback(struct urb *urb)
1033{
hayeswangebc2ec42013-08-14 20:54:38 +08001034 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001035 struct net_device *netdev;
hayeswanga5a4f462013-08-16 16:09:34 +08001036 unsigned long flags;
hayeswangebc2ec42013-08-14 20:54:38 +08001037 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001038 struct r8152 *tp;
1039 int status = urb->status;
1040
hayeswangebc2ec42013-08-14 20:54:38 +08001041 agg = urb->context;
1042 if (!agg)
1043 return;
1044
1045 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001046 if (!tp)
1047 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001048
hayeswangd104eaf2014-03-06 15:07:17 +08001049 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001050 stats = &netdev->stats;
hayeswangebc2ec42013-08-14 20:54:38 +08001051 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001052 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001053 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001054 stats->tx_errors += agg->skb_num;
1055 } else {
1056 stats->tx_packets += agg->skb_num;
1057 stats->tx_bytes += agg->skb_len;
1058 }
1059
hayeswanga5a4f462013-08-16 16:09:34 +08001060 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001061 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +08001062 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001063
hayeswang9a4be1b2014-02-18 21:49:07 +08001064 usb_autopm_put_interface_async(tp->intf);
1065
hayeswangd104eaf2014-03-06 15:07:17 +08001066 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001067 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001068
1069 if (!test_bit(WORK_ENABLE, &tp->flags))
1070 return;
1071
1072 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1073 return;
1074
1075 if (!skb_queue_empty(&tp->tx_queue))
hayeswang9a4be1b2014-02-18 21:49:07 +08001076 schedule_delayed_work(&tp->schedule, 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001077}
1078
hayeswang40a82912013-08-14 20:54:40 +08001079static void intr_callback(struct urb *urb)
1080{
1081 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001082 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001083 int status = urb->status;
1084 int res;
1085
1086 tp = urb->context;
1087 if (!tp)
1088 return;
1089
1090 if (!test_bit(WORK_ENABLE, &tp->flags))
1091 return;
1092
1093 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1094 return;
1095
1096 switch (status) {
1097 case 0: /* success */
1098 break;
1099 case -ECONNRESET: /* unlink */
1100 case -ESHUTDOWN:
1101 netif_device_detach(tp->netdev);
1102 case -ENOENT:
1103 return;
1104 case -EOVERFLOW:
1105 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1106 goto resubmit;
1107 /* -EPIPE: should clear the halt */
1108 default:
1109 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1110 goto resubmit;
1111 }
1112
1113 d = urb->transfer_buffer;
1114 if (INTR_LINK & __le16_to_cpu(d[0])) {
1115 if (!(tp->speed & LINK_STATUS)) {
1116 set_bit(RTL8152_LINK_CHG, &tp->flags);
1117 schedule_delayed_work(&tp->schedule, 0);
1118 }
1119 } else {
1120 if (tp->speed & LINK_STATUS) {
1121 set_bit(RTL8152_LINK_CHG, &tp->flags);
1122 schedule_delayed_work(&tp->schedule, 0);
1123 }
1124 }
1125
1126resubmit:
1127 res = usb_submit_urb(urb, GFP_ATOMIC);
1128 if (res == -ENODEV)
1129 netif_device_detach(tp->netdev);
1130 else if (res)
1131 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001132 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001133}
1134
hayeswangebc2ec42013-08-14 20:54:38 +08001135static inline void *rx_agg_align(void *data)
1136{
hayeswang8e1f51b2014-01-02 11:22:41 +08001137 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001138}
1139
1140static inline void *tx_agg_align(void *data)
1141{
hayeswang8e1f51b2014-01-02 11:22:41 +08001142 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001143}
1144
1145static void free_all_mem(struct r8152 *tp)
1146{
1147 int i;
1148
1149 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001150 usb_free_urb(tp->rx_info[i].urb);
1151 tp->rx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001152
hayeswang9629e3c2014-01-15 10:42:15 +08001153 kfree(tp->rx_info[i].buffer);
1154 tp->rx_info[i].buffer = NULL;
1155 tp->rx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001156 }
1157
1158 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001159 usb_free_urb(tp->tx_info[i].urb);
1160 tp->tx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001161
hayeswang9629e3c2014-01-15 10:42:15 +08001162 kfree(tp->tx_info[i].buffer);
1163 tp->tx_info[i].buffer = NULL;
1164 tp->tx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001165 }
hayeswang40a82912013-08-14 20:54:40 +08001166
hayeswang9629e3c2014-01-15 10:42:15 +08001167 usb_free_urb(tp->intr_urb);
1168 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001169
hayeswang9629e3c2014-01-15 10:42:15 +08001170 kfree(tp->intr_buff);
1171 tp->intr_buff = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001172}
1173
1174static int alloc_all_mem(struct r8152 *tp)
1175{
1176 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001177 struct usb_interface *intf = tp->intf;
1178 struct usb_host_interface *alt = intf->cur_altsetting;
1179 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec42013-08-14 20:54:38 +08001180 struct urb *urb;
1181 int node, i;
1182 u8 *buf;
1183
1184 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1185
1186 spin_lock_init(&tp->rx_lock);
1187 spin_lock_init(&tp->tx_lock);
1188 INIT_LIST_HEAD(&tp->rx_done);
1189 INIT_LIST_HEAD(&tp->tx_free);
1190 skb_queue_head_init(&tp->tx_queue);
1191
1192 for (i = 0; i < RTL8152_MAX_RX; i++) {
1193 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1194 if (!buf)
1195 goto err1;
1196
1197 if (buf != rx_agg_align(buf)) {
1198 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001199 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1200 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001201 if (!buf)
1202 goto err1;
1203 }
1204
1205 urb = usb_alloc_urb(0, GFP_KERNEL);
1206 if (!urb) {
1207 kfree(buf);
1208 goto err1;
1209 }
1210
1211 INIT_LIST_HEAD(&tp->rx_info[i].list);
1212 tp->rx_info[i].context = tp;
1213 tp->rx_info[i].urb = urb;
1214 tp->rx_info[i].buffer = buf;
1215 tp->rx_info[i].head = rx_agg_align(buf);
1216 }
1217
1218 for (i = 0; i < RTL8152_MAX_TX; i++) {
1219 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1220 if (!buf)
1221 goto err1;
1222
1223 if (buf != tx_agg_align(buf)) {
1224 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001225 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1226 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001227 if (!buf)
1228 goto err1;
1229 }
1230
1231 urb = usb_alloc_urb(0, GFP_KERNEL);
1232 if (!urb) {
1233 kfree(buf);
1234 goto err1;
1235 }
1236
1237 INIT_LIST_HEAD(&tp->tx_info[i].list);
1238 tp->tx_info[i].context = tp;
1239 tp->tx_info[i].urb = urb;
1240 tp->tx_info[i].buffer = buf;
1241 tp->tx_info[i].head = tx_agg_align(buf);
1242
1243 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1244 }
1245
hayeswang40a82912013-08-14 20:54:40 +08001246 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1247 if (!tp->intr_urb)
1248 goto err1;
1249
1250 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1251 if (!tp->intr_buff)
1252 goto err1;
1253
1254 tp->intr_interval = (int)ep_intr->desc.bInterval;
1255 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1256 tp->intr_buff, INTBUFSIZE, intr_callback,
1257 tp, tp->intr_interval);
1258
hayeswangebc2ec42013-08-14 20:54:38 +08001259 return 0;
1260
1261err1:
1262 free_all_mem(tp);
1263 return -ENOMEM;
1264}
1265
hayeswang0de98f62013-08-16 16:09:35 +08001266static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1267{
1268 struct tx_agg *agg = NULL;
1269 unsigned long flags;
1270
1271 spin_lock_irqsave(&tp->tx_lock, flags);
1272 if (!list_empty(&tp->tx_free)) {
1273 struct list_head *cursor;
1274
1275 cursor = tp->tx_free.next;
1276 list_del_init(cursor);
1277 agg = list_entry(cursor, struct tx_agg, list);
1278 }
1279 spin_unlock_irqrestore(&tp->tx_lock, flags);
1280
1281 return agg;
1282}
1283
hayeswang5bd23882013-08-14 20:54:39 +08001284static void
1285r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1286{
1287 memset(desc, 0, sizeof(*desc));
1288
1289 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1290
1291 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1292 __be16 protocol;
1293 u8 ip_protocol;
1294 u32 opts2 = 0;
1295
1296 if (skb->protocol == htons(ETH_P_8021Q))
1297 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1298 else
1299 protocol = skb->protocol;
1300
1301 switch (protocol) {
1302 case htons(ETH_P_IP):
1303 opts2 |= IPV4_CS;
1304 ip_protocol = ip_hdr(skb)->protocol;
1305 break;
1306
1307 case htons(ETH_P_IPV6):
1308 opts2 |= IPV6_CS;
1309 ip_protocol = ipv6_hdr(skb)->nexthdr;
1310 break;
1311
1312 default:
1313 ip_protocol = IPPROTO_RAW;
1314 break;
1315 }
1316
1317 if (ip_protocol == IPPROTO_TCP) {
1318 opts2 |= TCP_CS;
1319 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1320 } else if (ip_protocol == IPPROTO_UDP) {
1321 opts2 |= UDP_CS;
1322 } else {
1323 WARN_ON_ONCE(1);
1324 }
1325
1326 desc->opts2 = cpu_to_le32(opts2);
1327 }
1328}
1329
hayeswangb1379d92013-08-16 16:09:37 +08001330static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1331{
hayeswangd84130a2014-02-18 21:49:02 +08001332 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1333 unsigned long flags;
hayeswang9a4be1b2014-02-18 21:49:07 +08001334 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001335 u8 *tx_data;
1336
hayeswangd84130a2014-02-18 21:49:02 +08001337 __skb_queue_head_init(&skb_head);
1338 spin_lock_irqsave(&tx_queue->lock, flags);
1339 skb_queue_splice_init(tx_queue, &skb_head);
1340 spin_unlock_irqrestore(&tx_queue->lock, flags);
1341
hayeswangb1379d92013-08-16 16:09:37 +08001342 tx_data = agg->head;
1343 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001344 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001345
hayeswang7937f9e2013-11-20 17:30:54 +08001346 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001347 struct tx_desc *tx_desc;
1348 struct sk_buff *skb;
1349 unsigned int len;
1350
hayeswangd84130a2014-02-18 21:49:02 +08001351 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001352 if (!skb)
1353 break;
1354
hayeswang7937f9e2013-11-20 17:30:54 +08001355 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001356 len = skb->len;
1357 if (remain < len) {
hayeswangd84130a2014-02-18 21:49:02 +08001358 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001359 break;
1360 }
1361
hayeswang7937f9e2013-11-20 17:30:54 +08001362 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001363 tx_desc = (struct tx_desc *)tx_data;
1364 tx_data += sizeof(*tx_desc);
1365
1366 r8152_tx_csum(tp, tx_desc, skb);
1367 memcpy(tx_data, skb->data, len);
1368 agg->skb_num++;
1369 agg->skb_len += len;
1370 dev_kfree_skb_any(skb);
1371
hayeswang7937f9e2013-11-20 17:30:54 +08001372 tx_data += len;
1373 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001374 }
1375
hayeswangd84130a2014-02-18 21:49:02 +08001376 if (!skb_queue_empty(&skb_head)) {
1377 spin_lock_irqsave(&tx_queue->lock, flags);
1378 skb_queue_splice(&skb_head, tx_queue);
1379 spin_unlock_irqrestore(&tx_queue->lock, flags);
1380 }
1381
hayeswang9a4be1b2014-02-18 21:49:07 +08001382 netif_tx_lock_bh(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001383
1384 if (netif_queue_stopped(tp->netdev) &&
1385 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1386 netif_wake_queue(tp->netdev);
1387
hayeswang9a4be1b2014-02-18 21:49:07 +08001388 netif_tx_unlock_bh(tp->netdev);
1389
1390 ret = usb_autopm_get_interface(tp->intf);
1391 if (ret < 0)
1392 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001393
hayeswangb1379d92013-08-16 16:09:37 +08001394 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1395 agg->head, (int)(tx_data - (u8 *)agg->head),
1396 (usb_complete_t)write_bulk_callback, agg);
1397
hayeswang9a4be1b2014-02-18 21:49:07 +08001398 ret = usb_submit_urb(agg->urb, GFP_KERNEL);
1399 if (ret < 0)
1400 usb_autopm_put_interface(tp->intf);
1401
1402out_tx_fill:
1403 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001404}
1405
hayeswangebc2ec42013-08-14 20:54:38 +08001406static void rx_bottom(struct r8152 *tp)
1407{
hayeswanga5a4f462013-08-16 16:09:34 +08001408 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001409 struct list_head *cursor, *next, rx_queue;
hayeswangebc2ec42013-08-14 20:54:38 +08001410
hayeswangd84130a2014-02-18 21:49:02 +08001411 if (list_empty(&tp->rx_done))
1412 return;
1413
1414 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001415 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001416 list_splice_init(&tp->rx_done, &rx_queue);
1417 spin_unlock_irqrestore(&tp->rx_lock, flags);
1418
1419 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001420 struct rx_desc *rx_desc;
1421 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001422 int len_used = 0;
1423 struct urb *urb;
1424 u8 *rx_data;
1425 int ret;
1426
hayeswangebc2ec42013-08-14 20:54:38 +08001427 list_del_init(cursor);
hayeswangebc2ec42013-08-14 20:54:38 +08001428
1429 agg = list_entry(cursor, struct rx_agg, list);
1430 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001431 if (urb->actual_length < ETH_ZLEN)
1432 goto submit;
hayeswangebc2ec42013-08-14 20:54:38 +08001433
hayeswangebc2ec42013-08-14 20:54:38 +08001434 rx_desc = agg->head;
1435 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001436 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001437
hayeswang7937f9e2013-11-20 17:30:54 +08001438 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001439 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001440 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001441 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001442 struct sk_buff *skb;
1443
hayeswang7937f9e2013-11-20 17:30:54 +08001444 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec42013-08-14 20:54:38 +08001445 if (pkt_len < ETH_ZLEN)
1446 break;
1447
hayeswang7937f9e2013-11-20 17:30:54 +08001448 len_used += pkt_len;
1449 if (urb->actual_length < len_used)
1450 break;
1451
hayeswang8e1f51b2014-01-02 11:22:41 +08001452 pkt_len -= CRC_SIZE;
hayeswangebc2ec42013-08-14 20:54:38 +08001453 rx_data += sizeof(struct rx_desc);
1454
1455 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1456 if (!skb) {
1457 stats->rx_dropped++;
1458 break;
1459 }
1460 memcpy(skb->data, rx_data, pkt_len);
1461 skb_put(skb, pkt_len);
1462 skb->protocol = eth_type_trans(skb, netdev);
hayeswang9d9aafa2014-02-18 21:49:09 +08001463 netif_receive_skb(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001464 stats->rx_packets++;
1465 stats->rx_bytes += pkt_len;
1466
hayeswang8e1f51b2014-01-02 11:22:41 +08001467 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec42013-08-14 20:54:38 +08001468 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec42013-08-14 20:54:38 +08001469 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001470 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001471 }
1472
hayeswang0de98f62013-08-16 16:09:35 +08001473submit:
hayeswangebc2ec42013-08-14 20:54:38 +08001474 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec42013-08-14 20:54:38 +08001475 if (ret && ret != -ENODEV) {
hayeswangd84130a2014-02-18 21:49:02 +08001476 spin_lock_irqsave(&tp->rx_lock, flags);
1477 list_add_tail(&agg->list, &tp->rx_done);
1478 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001479 tasklet_schedule(&tp->tl);
1480 }
1481 }
hayeswangebc2ec42013-08-14 20:54:38 +08001482}
1483
1484static void tx_bottom(struct r8152 *tp)
1485{
hayeswangebc2ec42013-08-14 20:54:38 +08001486 int res;
1487
hayeswangb1379d92013-08-16 16:09:37 +08001488 do {
1489 struct tx_agg *agg;
hayeswangebc2ec42013-08-14 20:54:38 +08001490
hayeswangb1379d92013-08-16 16:09:37 +08001491 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec42013-08-14 20:54:38 +08001492 break;
1493
hayeswangb1379d92013-08-16 16:09:37 +08001494 agg = r8152_get_tx_agg(tp);
1495 if (!agg)
hayeswangebc2ec42013-08-14 20:54:38 +08001496 break;
hayeswangb1379d92013-08-16 16:09:37 +08001497
1498 res = r8152_tx_agg_fill(tp, agg);
1499 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08001500 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08001501
1502 if (res == -ENODEV) {
1503 netif_device_detach(netdev);
1504 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08001505 struct net_device_stats *stats = &netdev->stats;
1506 unsigned long flags;
1507
hayeswangb1379d92013-08-16 16:09:37 +08001508 netif_warn(tp, tx_err, netdev,
1509 "failed tx_urb %d\n", res);
1510 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08001511
hayeswangb1379d92013-08-16 16:09:37 +08001512 spin_lock_irqsave(&tp->tx_lock, flags);
1513 list_add_tail(&agg->list, &tp->tx_free);
1514 spin_unlock_irqrestore(&tp->tx_lock, flags);
1515 }
hayeswangebc2ec42013-08-14 20:54:38 +08001516 }
hayeswangb1379d92013-08-16 16:09:37 +08001517 } while (res == 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001518}
1519
1520static void bottom_half(unsigned long data)
1521{
1522 struct r8152 *tp;
1523
1524 tp = (struct r8152 *)data;
1525
1526 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1527 return;
1528
1529 if (!test_bit(WORK_ENABLE, &tp->flags))
1530 return;
1531
hayeswang7559fb2f2013-08-16 16:09:38 +08001532 /* When link down, the driver would cancel all bulks. */
1533 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001534 if (!netif_carrier_ok(tp->netdev))
1535 return;
1536
1537 rx_bottom(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001538}
1539
1540static
1541int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1542{
1543 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1544 agg->head, rx_buf_sz,
1545 (usb_complete_t)read_bulk_callback, agg);
1546
1547 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001548}
1549
hayeswang00a5e362014-02-18 21:48:59 +08001550static void rtl_drop_queued_tx(struct r8152 *tp)
1551{
1552 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001553 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1554 unsigned long flags;
hayeswang00a5e362014-02-18 21:48:59 +08001555 struct sk_buff *skb;
1556
hayeswangd84130a2014-02-18 21:49:02 +08001557 if (skb_queue_empty(tx_queue))
1558 return;
1559
1560 __skb_queue_head_init(&skb_head);
1561 spin_lock_irqsave(&tx_queue->lock, flags);
1562 skb_queue_splice_init(tx_queue, &skb_head);
1563 spin_unlock_irqrestore(&tx_queue->lock, flags);
1564
1565 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001566 dev_kfree_skb(skb);
1567 stats->tx_dropped++;
1568 }
1569}
1570
hayeswangac718b62013-05-02 16:01:25 +00001571static void rtl8152_tx_timeout(struct net_device *netdev)
1572{
1573 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001574 int i;
1575
Hayes Wang4a8deae2014-01-07 11:18:22 +08001576 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001577 for (i = 0; i < RTL8152_MAX_TX; i++)
1578 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001579}
1580
1581static void rtl8152_set_rx_mode(struct net_device *netdev)
1582{
1583 struct r8152 *tp = netdev_priv(netdev);
1584
hayeswang40a82912013-08-14 20:54:40 +08001585 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001586 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001587 schedule_delayed_work(&tp->schedule, 0);
1588 }
hayeswangac718b62013-05-02 16:01:25 +00001589}
1590
1591static void _rtl8152_set_rx_mode(struct net_device *netdev)
1592{
1593 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001594 u32 mc_filter[2]; /* Multicast hash filter */
1595 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001596 u32 ocp_data;
1597
hayeswangac718b62013-05-02 16:01:25 +00001598 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1599 netif_stop_queue(netdev);
1600 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1601 ocp_data &= ~RCR_ACPT_ALL;
1602 ocp_data |= RCR_AB | RCR_APM;
1603
1604 if (netdev->flags & IFF_PROMISC) {
1605 /* Unconditionally log net taps. */
1606 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1607 ocp_data |= RCR_AM | RCR_AAP;
1608 mc_filter[1] = mc_filter[0] = 0xffffffff;
1609 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1610 (netdev->flags & IFF_ALLMULTI)) {
1611 /* Too many to filter perfectly -- accept all multicasts. */
1612 ocp_data |= RCR_AM;
1613 mc_filter[1] = mc_filter[0] = 0xffffffff;
1614 } else {
1615 struct netdev_hw_addr *ha;
1616
1617 mc_filter[1] = mc_filter[0] = 0;
1618 netdev_for_each_mc_addr(ha, netdev) {
1619 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1620 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1621 ocp_data |= RCR_AM;
1622 }
1623 }
1624
hayeswang31787f52013-07-31 17:21:25 +08001625 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1626 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001627
hayeswang31787f52013-07-31 17:21:25 +08001628 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001629 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1630 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001631}
1632
1633static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1634 struct net_device *netdev)
1635{
1636 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001637
hayeswangac718b62013-05-02 16:01:25 +00001638 skb_tx_timestamp(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001639
hayeswang61598782013-11-20 17:30:55 +08001640 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001641
hayeswangdd1b1192013-11-20 17:30:56 +08001642 if (list_empty(&tp->tx_free) &&
1643 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1644 netif_stop_queue(netdev);
1645
hayeswang61598782013-11-20 17:30:55 +08001646 if (!list_empty(&tp->tx_free))
hayeswang9a4be1b2014-02-18 21:49:07 +08001647 schedule_delayed_work(&tp->schedule, 0);
hayeswangac718b62013-05-02 16:01:25 +00001648
1649 return NETDEV_TX_OK;
1650}
1651
1652static void r8152b_reset_packet_filter(struct r8152 *tp)
1653{
1654 u32 ocp_data;
1655
1656 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1657 ocp_data &= ~FMC_FCR_MCU_EN;
1658 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1659 ocp_data |= FMC_FCR_MCU_EN;
1660 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1661}
1662
1663static void rtl8152_nic_reset(struct r8152 *tp)
1664{
1665 int i;
1666
1667 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1668
1669 for (i = 0; i < 1000; i++) {
1670 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1671 break;
1672 udelay(100);
1673 }
1674}
1675
hayeswangdd1b1192013-11-20 17:30:56 +08001676static void set_tx_qlen(struct r8152 *tp)
1677{
1678 struct net_device *netdev = tp->netdev;
1679
1680 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1681 sizeof(struct tx_desc));
1682}
1683
hayeswangac718b62013-05-02 16:01:25 +00001684static inline u8 rtl8152_get_speed(struct r8152 *tp)
1685{
1686 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1687}
1688
hayeswang507605a2014-01-02 11:22:43 +08001689static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001690{
hayeswangebc2ec42013-08-14 20:54:38 +08001691 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001692 u8 speed;
1693
1694 speed = rtl8152_get_speed(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001695 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001696 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-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 } else {
1700 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08001701 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001702 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1703 }
hayeswang507605a2014-01-02 11:22:43 +08001704}
1705
hayeswang00a5e362014-02-18 21:48:59 +08001706static void rxdy_gated_en(struct r8152 *tp, bool enable)
1707{
1708 u32 ocp_data;
1709
1710 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1711 if (enable)
1712 ocp_data |= RXDY_GATED_EN;
1713 else
1714 ocp_data &= ~RXDY_GATED_EN;
1715 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1716}
1717
hayeswang507605a2014-01-02 11:22:43 +08001718static int rtl_enable(struct r8152 *tp)
1719{
1720 u32 ocp_data;
1721 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001722
1723 r8152b_reset_packet_filter(tp);
1724
1725 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1726 ocp_data |= CR_RE | CR_TE;
1727 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1728
hayeswang00a5e362014-02-18 21:48:59 +08001729 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001730
hayeswangebc2ec42013-08-14 20:54:38 +08001731 INIT_LIST_HEAD(&tp->rx_done);
1732 ret = 0;
1733 for (i = 0; i < RTL8152_MAX_RX; i++) {
1734 INIT_LIST_HEAD(&tp->rx_info[i].list);
1735 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1736 }
hayeswangac718b62013-05-02 16:01:25 +00001737
hayeswangebc2ec42013-08-14 20:54:38 +08001738 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001739}
1740
hayeswang507605a2014-01-02 11:22:43 +08001741static int rtl8152_enable(struct r8152 *tp)
1742{
1743 set_tx_qlen(tp);
1744 rtl_set_eee_plus(tp);
1745
1746 return rtl_enable(tp);
1747}
1748
hayeswang43779f82014-01-02 11:25:10 +08001749static void r8153_set_rx_agg(struct r8152 *tp)
1750{
1751 u8 speed;
1752
1753 speed = rtl8152_get_speed(tp);
1754 if (speed & _1000bps) {
1755 if (tp->udev->speed == USB_SPEED_SUPER) {
1756 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1757 RX_THR_SUPPER);
1758 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1759 EARLY_AGG_SUPPER);
1760 } else {
1761 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1762 RX_THR_HIGH);
1763 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1764 EARLY_AGG_HIGH);
1765 }
1766 } else {
1767 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1768 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1769 EARLY_AGG_SLOW);
1770 }
1771}
1772
1773static int rtl8153_enable(struct r8152 *tp)
1774{
1775 set_tx_qlen(tp);
1776 rtl_set_eee_plus(tp);
1777 r8153_set_rx_agg(tp);
1778
1779 return rtl_enable(tp);
1780}
1781
hayeswangac718b62013-05-02 16:01:25 +00001782static void rtl8152_disable(struct r8152 *tp)
1783{
hayeswangebc2ec42013-08-14 20:54:38 +08001784 u32 ocp_data;
1785 int i;
hayeswangac718b62013-05-02 16:01:25 +00001786
1787 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1788 ocp_data &= ~RCR_ACPT_ALL;
1789 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1790
hayeswang00a5e362014-02-18 21:48:59 +08001791 rtl_drop_queued_tx(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001792
1793 for (i = 0; i < RTL8152_MAX_TX; i++)
1794 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001795
hayeswang00a5e362014-02-18 21:48:59 +08001796 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001797
1798 for (i = 0; i < 1000; i++) {
1799 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1800 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1801 break;
1802 mdelay(1);
1803 }
1804
1805 for (i = 0; i < 1000; i++) {
1806 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1807 break;
1808 mdelay(1);
1809 }
1810
hayeswangebc2ec42013-08-14 20:54:38 +08001811 for (i = 0; i < RTL8152_MAX_RX; i++)
1812 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001813
1814 rtl8152_nic_reset(tp);
1815}
1816
hayeswang00a5e362014-02-18 21:48:59 +08001817static void r8152_power_cut_en(struct r8152 *tp, bool enable)
1818{
1819 u32 ocp_data;
1820
1821 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1822 if (enable)
1823 ocp_data |= POWER_CUT;
1824 else
1825 ocp_data &= ~POWER_CUT;
1826 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1827
1828 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1829 ocp_data &= ~RESUME_INDICATE;
1830 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08001831}
1832
hayeswang21ff2e82014-02-18 21:49:06 +08001833#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1834
1835static u32 __rtl_get_wol(struct r8152 *tp)
1836{
1837 u32 ocp_data;
1838 u32 wolopts = 0;
1839
1840 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1841 if (!(ocp_data & LAN_WAKE_EN))
1842 return 0;
1843
1844 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1845 if (ocp_data & LINK_ON_WAKE_EN)
1846 wolopts |= WAKE_PHY;
1847
1848 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1849 if (ocp_data & UWF_EN)
1850 wolopts |= WAKE_UCAST;
1851 if (ocp_data & BWF_EN)
1852 wolopts |= WAKE_BCAST;
1853 if (ocp_data & MWF_EN)
1854 wolopts |= WAKE_MCAST;
1855
1856 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1857 if (ocp_data & MAGIC_EN)
1858 wolopts |= WAKE_MAGIC;
1859
1860 return wolopts;
1861}
1862
1863static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
1864{
1865 u32 ocp_data;
1866
1867 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1868
1869 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1870 ocp_data &= ~LINK_ON_WAKE_EN;
1871 if (wolopts & WAKE_PHY)
1872 ocp_data |= LINK_ON_WAKE_EN;
1873 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1874
1875 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1876 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
1877 if (wolopts & WAKE_UCAST)
1878 ocp_data |= UWF_EN;
1879 if (wolopts & WAKE_BCAST)
1880 ocp_data |= BWF_EN;
1881 if (wolopts & WAKE_MCAST)
1882 ocp_data |= MWF_EN;
1883 if (wolopts & WAKE_ANY)
1884 ocp_data |= LAN_WAKE_EN;
1885 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
1886
1887 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1888
1889 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1890 ocp_data &= ~MAGIC_EN;
1891 if (wolopts & WAKE_MAGIC)
1892 ocp_data |= MAGIC_EN;
1893 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1894
1895 if (wolopts & WAKE_ANY)
1896 device_set_wakeup_enable(&tp->udev->dev, true);
1897 else
1898 device_set_wakeup_enable(&tp->udev->dev, false);
1899}
1900
hayeswang9a4be1b2014-02-18 21:49:07 +08001901static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
1902{
1903 if (enable) {
1904 u32 ocp_data;
1905
1906 __rtl_set_wol(tp, WAKE_ANY);
1907
1908 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1909
1910 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1911 ocp_data |= LINK_OFF_WAKE_EN;
1912 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1913
1914 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1915 } else {
1916 __rtl_set_wol(tp, tp->saved_wolopts);
1917 }
1918}
1919
hayeswangaa66a5f2014-02-18 21:49:04 +08001920static void rtl_phy_reset(struct r8152 *tp)
1921{
1922 u16 data;
1923 int i;
1924
1925 clear_bit(PHY_RESET, &tp->flags);
1926
1927 data = r8152_mdio_read(tp, MII_BMCR);
1928
1929 /* don't reset again before the previous one complete */
1930 if (data & BMCR_RESET)
1931 return;
1932
1933 data |= BMCR_RESET;
1934 r8152_mdio_write(tp, MII_BMCR, data);
1935
1936 for (i = 0; i < 50; i++) {
1937 msleep(20);
1938 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
1939 break;
1940 }
1941}
1942
hayeswang43499682014-02-18 21:48:58 +08001943static void rtl_clear_bp(struct r8152 *tp)
1944{
1945 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1946 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1947 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1948 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1949 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1950 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1951 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1952 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1953 mdelay(3);
1954 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1955 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1956}
1957
1958static void r8153_clear_bp(struct r8152 *tp)
1959{
1960 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
1961 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
1962 rtl_clear_bp(tp);
1963}
1964
1965static void r8153_teredo_off(struct r8152 *tp)
1966{
1967 u32 ocp_data;
1968
1969 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1970 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1971 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1972
1973 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1974 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1975 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1976}
1977
1978static void r8152b_disable_aldps(struct r8152 *tp)
1979{
1980 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1981 msleep(20);
1982}
1983
1984static inline void r8152b_enable_aldps(struct r8152 *tp)
1985{
1986 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1987 LINKENA | DIS_SDSAVE);
1988}
1989
1990static void r8152b_hw_phy_cfg(struct r8152 *tp)
1991{
hayeswangf0cbe0a2014-02-18 21:49:03 +08001992 u16 data;
1993
1994 data = r8152_mdio_read(tp, MII_BMCR);
1995 if (data & BMCR_PDOWN) {
1996 data &= ~BMCR_PDOWN;
1997 r8152_mdio_write(tp, MII_BMCR, data);
1998 }
1999
hayeswang43499682014-02-18 21:48:58 +08002000 r8152b_disable_aldps(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002001
2002 rtl_clear_bp(tp);
2003
2004 r8152b_enable_aldps(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002005 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002006}
2007
hayeswangac718b62013-05-02 16:01:25 +00002008static void r8152b_exit_oob(struct r8152 *tp)
2009{
hayeswangdb8515e2014-03-06 15:07:16 +08002010 u32 ocp_data;
2011 int i;
hayeswangac718b62013-05-02 16:01:25 +00002012
2013 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2014 ocp_data &= ~RCR_ACPT_ALL;
2015 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2016
hayeswang00a5e362014-02-18 21:48:59 +08002017 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002018 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002019 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002020
2021 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2022 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2023
2024 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2025 ocp_data &= ~NOW_IS_OOB;
2026 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2027
2028 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2029 ocp_data &= ~MCU_BORW_EN;
2030 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2031
2032 for (i = 0; i < 1000; i++) {
2033 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2034 if (ocp_data & LINK_LIST_READY)
2035 break;
2036 mdelay(1);
2037 }
2038
2039 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2040 ocp_data |= RE_INIT_LL;
2041 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2042
2043 for (i = 0; i < 1000; i++) {
2044 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2045 if (ocp_data & LINK_LIST_READY)
2046 break;
2047 mdelay(1);
2048 }
2049
2050 rtl8152_nic_reset(tp);
2051
2052 /* rx share fifo credit full threshold */
2053 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2054
2055 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
2056 ocp_data &= STAT_SPEED_MASK;
2057 if (ocp_data == STAT_SPEED_FULL) {
2058 /* rx share fifo credit near full threshold */
2059 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2060 RXFIFO_THR2_FULL);
2061 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2062 RXFIFO_THR3_FULL);
2063 } else {
2064 /* rx share fifo credit near full threshold */
2065 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2066 RXFIFO_THR2_HIGH);
2067 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2068 RXFIFO_THR3_HIGH);
2069 }
2070
2071 /* TX share fifo free credit full threshold */
2072 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2073
2074 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002075 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002076 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2077 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2078
2079 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2080 ocp_data &= ~CPCR_RX_VLAN;
2081 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2082
2083 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2084
2085 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2086 ocp_data |= TCR0_AUTO_FIFO;
2087 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2088}
2089
2090static void r8152b_enter_oob(struct r8152 *tp)
2091{
hayeswang45f4a192014-01-06 17:08:41 +08002092 u32 ocp_data;
2093 int i;
hayeswangac718b62013-05-02 16:01:25 +00002094
2095 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2096 ocp_data &= ~NOW_IS_OOB;
2097 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2098
2099 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2100 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2101 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2102
2103 rtl8152_disable(tp);
2104
2105 for (i = 0; i < 1000; i++) {
2106 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2107 if (ocp_data & LINK_LIST_READY)
2108 break;
2109 mdelay(1);
2110 }
2111
2112 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2113 ocp_data |= RE_INIT_LL;
2114 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2115
2116 for (i = 0; i < 1000; i++) {
2117 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2118 if (ocp_data & LINK_LIST_READY)
2119 break;
2120 mdelay(1);
2121 }
2122
2123 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2124
hayeswangac718b62013-05-02 16:01:25 +00002125 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2126 ocp_data |= CPCR_RX_VLAN;
2127 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2128
2129 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2130 ocp_data |= ALDPS_PROXY_MODE;
2131 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2132
2133 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2134 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2135 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2136
hayeswang00a5e362014-02-18 21:48:59 +08002137 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002138
2139 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2140 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2141 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2142}
2143
hayeswang43779f82014-01-02 11:25:10 +08002144static void r8153_hw_phy_cfg(struct r8152 *tp)
2145{
2146 u32 ocp_data;
2147 u16 data;
2148
2149 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002150 data = r8152_mdio_read(tp, MII_BMCR);
2151 if (data & BMCR_PDOWN) {
2152 data &= ~BMCR_PDOWN;
2153 r8152_mdio_write(tp, MII_BMCR, data);
2154 }
hayeswang43779f82014-01-02 11:25:10 +08002155
hayeswang7e9da482014-02-18 21:49:05 +08002156 r8153_clear_bp(tp);
2157
hayeswang43779f82014-01-02 11:25:10 +08002158 if (tp->version == RTL_VER_03) {
2159 data = ocp_reg_read(tp, OCP_EEE_CFG);
2160 data &= ~CTAP_SHORT_EN;
2161 ocp_reg_write(tp, OCP_EEE_CFG, data);
2162 }
2163
2164 data = ocp_reg_read(tp, OCP_POWER_CFG);
2165 data |= EEE_CLKDIV_EN;
2166 ocp_reg_write(tp, OCP_POWER_CFG, data);
2167
2168 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2169 data |= EN_10M_BGOFF;
2170 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2171 data = ocp_reg_read(tp, OCP_POWER_CFG);
2172 data |= EN_10M_PLLOFF;
2173 ocp_reg_write(tp, OCP_POWER_CFG, data);
2174 data = sram_read(tp, SRAM_IMPEDANCE);
2175 data &= ~RX_DRIVING_MASK;
2176 sram_write(tp, SRAM_IMPEDANCE, data);
2177
2178 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2179 ocp_data |= PFM_PWM_SWITCH;
2180 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2181
2182 data = sram_read(tp, SRAM_LPF_CFG);
2183 data |= LPF_AUTO_TUNE;
2184 sram_write(tp, SRAM_LPF_CFG, data);
2185
2186 data = sram_read(tp, SRAM_10M_AMP1);
2187 data |= GDAC_IB_UPALL;
2188 sram_write(tp, SRAM_10M_AMP1, data);
2189 data = sram_read(tp, SRAM_10M_AMP2);
2190 data |= AMP_DN;
2191 sram_write(tp, SRAM_10M_AMP2, data);
hayeswangaa66a5f2014-02-18 21:49:04 +08002192
2193 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002194}
2195
hayeswangb9702722014-02-18 21:49:00 +08002196static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002197{
2198 u8 u1u2[8];
2199
2200 if (enable)
2201 memset(u1u2, 0xff, sizeof(u1u2));
2202 else
2203 memset(u1u2, 0x00, sizeof(u1u2));
2204
2205 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2206}
2207
hayeswangb9702722014-02-18 21:49:00 +08002208static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002209{
2210 u32 ocp_data;
2211
2212 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2213 if (enable)
2214 ocp_data |= U2P3_ENABLE;
2215 else
2216 ocp_data &= ~U2P3_ENABLE;
2217 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2218}
2219
hayeswangb9702722014-02-18 21:49:00 +08002220static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002221{
2222 u32 ocp_data;
2223
2224 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2225 if (enable)
2226 ocp_data |= PWR_EN | PHASE2_EN;
2227 else
2228 ocp_data &= ~(PWR_EN | PHASE2_EN);
2229 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2230
2231 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2232 ocp_data &= ~PCUT_STATUS;
2233 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2234}
2235
hayeswang43779f82014-01-02 11:25:10 +08002236static void r8153_first_init(struct r8152 *tp)
2237{
2238 u32 ocp_data;
2239 int i;
2240
hayeswang00a5e362014-02-18 21:48:59 +08002241 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002242 r8153_teredo_off(tp);
2243
2244 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2245 ocp_data &= ~RCR_ACPT_ALL;
2246 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2247
2248 r8153_hw_phy_cfg(tp);
2249
2250 rtl8152_nic_reset(tp);
2251
2252 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2253 ocp_data &= ~NOW_IS_OOB;
2254 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2255
2256 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2257 ocp_data &= ~MCU_BORW_EN;
2258 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2259
2260 for (i = 0; i < 1000; i++) {
2261 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2262 if (ocp_data & LINK_LIST_READY)
2263 break;
2264 mdelay(1);
2265 }
2266
2267 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2268 ocp_data |= RE_INIT_LL;
2269 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2270
2271 for (i = 0; i < 1000; i++) {
2272 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2273 if (ocp_data & LINK_LIST_READY)
2274 break;
2275 mdelay(1);
2276 }
2277
2278 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2279 ocp_data &= ~CPCR_RX_VLAN;
2280 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2281
2282 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2283
2284 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2285 ocp_data |= TCR0_AUTO_FIFO;
2286 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2287
2288 rtl8152_nic_reset(tp);
2289
2290 /* rx share fifo credit full threshold */
2291 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2292 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2293 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2294 /* TX share fifo free credit full threshold */
2295 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2296
hayeswang9629e3c2014-01-15 10:42:15 +08002297 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002298 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2299 ocp_data &= ~RX_AGG_DISABLE;
2300 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2301}
2302
2303static void r8153_enter_oob(struct r8152 *tp)
2304{
2305 u32 ocp_data;
2306 int i;
2307
2308 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2309 ocp_data &= ~NOW_IS_OOB;
2310 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2311
2312 rtl8152_disable(tp);
2313
2314 for (i = 0; i < 1000; i++) {
2315 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2316 if (ocp_data & LINK_LIST_READY)
2317 break;
2318 mdelay(1);
2319 }
2320
2321 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2322 ocp_data |= RE_INIT_LL;
2323 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2324
2325 for (i = 0; i < 1000; i++) {
2326 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2327 if (ocp_data & LINK_LIST_READY)
2328 break;
2329 mdelay(1);
2330 }
2331
2332 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2333
hayeswang43779f82014-01-02 11:25:10 +08002334 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2335 ocp_data &= ~TEREDO_WAKE_MASK;
2336 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2337
2338 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2339 ocp_data |= CPCR_RX_VLAN;
2340 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2341
2342 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2343 ocp_data |= ALDPS_PROXY_MODE;
2344 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2345
2346 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2347 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2348 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2349
hayeswang00a5e362014-02-18 21:48:59 +08002350 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002351
2352 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2353 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2354 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2355}
2356
2357static void r8153_disable_aldps(struct r8152 *tp)
2358{
2359 u16 data;
2360
2361 data = ocp_reg_read(tp, OCP_POWER_CFG);
2362 data &= ~EN_ALDPS;
2363 ocp_reg_write(tp, OCP_POWER_CFG, data);
2364 msleep(20);
2365}
2366
2367static void r8153_enable_aldps(struct r8152 *tp)
2368{
2369 u16 data;
2370
2371 data = ocp_reg_read(tp, OCP_POWER_CFG);
2372 data |= EN_ALDPS;
2373 ocp_reg_write(tp, OCP_POWER_CFG, data);
2374}
2375
hayeswangac718b62013-05-02 16:01:25 +00002376static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2377{
hayeswang43779f82014-01-02 11:25:10 +08002378 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002379 int ret = 0;
2380
2381 cancel_delayed_work_sync(&tp->schedule);
2382 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2383 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2384 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002385 if (tp->mii.supports_gmii) {
2386 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2387 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2388 } else {
2389 gbcr = 0;
2390 }
hayeswangac718b62013-05-02 16:01:25 +00002391
2392 if (autoneg == AUTONEG_DISABLE) {
2393 if (speed == SPEED_10) {
2394 bmcr = 0;
2395 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2396 } else if (speed == SPEED_100) {
2397 bmcr = BMCR_SPEED100;
2398 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002399 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2400 bmcr = BMCR_SPEED1000;
2401 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002402 } else {
2403 ret = -EINVAL;
2404 goto out;
2405 }
2406
2407 if (duplex == DUPLEX_FULL)
2408 bmcr |= BMCR_FULLDPLX;
2409 } else {
2410 if (speed == SPEED_10) {
2411 if (duplex == DUPLEX_FULL)
2412 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2413 else
2414 anar |= ADVERTISE_10HALF;
2415 } else if (speed == SPEED_100) {
2416 if (duplex == DUPLEX_FULL) {
2417 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2418 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2419 } else {
2420 anar |= ADVERTISE_10HALF;
2421 anar |= ADVERTISE_100HALF;
2422 }
hayeswang43779f82014-01-02 11:25:10 +08002423 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2424 if (duplex == DUPLEX_FULL) {
2425 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2426 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2427 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2428 } else {
2429 anar |= ADVERTISE_10HALF;
2430 anar |= ADVERTISE_100HALF;
2431 gbcr |= ADVERTISE_1000HALF;
2432 }
hayeswangac718b62013-05-02 16:01:25 +00002433 } else {
2434 ret = -EINVAL;
2435 goto out;
2436 }
2437
2438 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2439 }
2440
hayeswangaa66a5f2014-02-18 21:49:04 +08002441 if (test_bit(PHY_RESET, &tp->flags))
2442 bmcr |= BMCR_RESET;
2443
hayeswang43779f82014-01-02 11:25:10 +08002444 if (tp->mii.supports_gmii)
2445 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2446
hayeswangac718b62013-05-02 16:01:25 +00002447 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2448 r8152_mdio_write(tp, MII_BMCR, bmcr);
2449
hayeswangaa66a5f2014-02-18 21:49:04 +08002450 if (test_bit(PHY_RESET, &tp->flags)) {
2451 int i;
2452
2453 clear_bit(PHY_RESET, &tp->flags);
2454 for (i = 0; i < 50; i++) {
2455 msleep(20);
2456 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2457 break;
2458 }
2459 }
2460
hayeswangac718b62013-05-02 16:01:25 +00002461out:
hayeswangac718b62013-05-02 16:01:25 +00002462
2463 return ret;
2464}
2465
2466static void rtl8152_down(struct r8152 *tp)
2467{
hayeswang00a5e362014-02-18 21:48:59 +08002468 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002469 r8152b_disable_aldps(tp);
2470 r8152b_enter_oob(tp);
2471 r8152b_enable_aldps(tp);
2472}
2473
hayeswang43779f82014-01-02 11:25:10 +08002474static void rtl8153_down(struct r8152 *tp)
2475{
hayeswangb9702722014-02-18 21:49:00 +08002476 r8153_u1u2en(tp, false);
2477 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002478 r8153_disable_aldps(tp);
2479 r8153_enter_oob(tp);
2480 r8153_enable_aldps(tp);
2481}
2482
hayeswangac718b62013-05-02 16:01:25 +00002483static void set_carrier(struct r8152 *tp)
2484{
2485 struct net_device *netdev = tp->netdev;
2486 u8 speed;
2487
hayeswang40a82912013-08-14 20:54:40 +08002488 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002489 speed = rtl8152_get_speed(tp);
2490
2491 if (speed & LINK_STATUS) {
2492 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002493 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002494 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2495 netif_carrier_on(netdev);
2496 }
2497 } else {
2498 if (tp->speed & LINK_STATUS) {
2499 netif_carrier_off(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08002500 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002501 tp->rtl_ops.disable(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002502 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002503 }
2504 }
2505 tp->speed = speed;
2506}
2507
2508static void rtl_work_func_t(struct work_struct *work)
2509{
2510 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2511
hayeswang9a4be1b2014-02-18 21:49:07 +08002512 if (usb_autopm_get_interface(tp->intf) < 0)
2513 return;
2514
hayeswangac718b62013-05-02 16:01:25 +00002515 if (!test_bit(WORK_ENABLE, &tp->flags))
2516 goto out1;
2517
2518 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2519 goto out1;
2520
hayeswang40a82912013-08-14 20:54:40 +08002521 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2522 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002523
2524 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2525 _rtl8152_set_rx_mode(tp->netdev);
2526
hayeswang9a4be1b2014-02-18 21:49:07 +08002527 if (tp->speed & LINK_STATUS)
2528 tx_bottom(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002529
2530 if (test_bit(PHY_RESET, &tp->flags))
2531 rtl_phy_reset(tp);
2532
hayeswangac718b62013-05-02 16:01:25 +00002533out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002534 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002535}
2536
2537static int rtl8152_open(struct net_device *netdev)
2538{
2539 struct r8152 *tp = netdev_priv(netdev);
2540 int res = 0;
2541
hayeswang7e9da482014-02-18 21:49:05 +08002542 res = alloc_all_mem(tp);
2543 if (res)
2544 goto out;
2545
hayeswang9a4be1b2014-02-18 21:49:07 +08002546 res = usb_autopm_get_interface(tp->intf);
2547 if (res < 0) {
2548 free_all_mem(tp);
2549 goto out;
2550 }
2551
2552 /* The WORK_ENABLE may be set when autoresume occurs */
2553 if (test_bit(WORK_ENABLE, &tp->flags)) {
2554 clear_bit(WORK_ENABLE, &tp->flags);
2555 usb_kill_urb(tp->intr_urb);
2556 cancel_delayed_work_sync(&tp->schedule);
2557 if (tp->speed & LINK_STATUS)
2558 tp->rtl_ops.disable(tp);
2559 }
2560
hayeswang7e9da482014-02-18 21:49:05 +08002561 tp->rtl_ops.up(tp);
2562
hayeswang43779f82014-01-02 11:25:10 +08002563 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2564 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2565 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002566 tp->speed = 0;
2567 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002568 netif_start_queue(netdev);
2569 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08002570
hayeswang3d55f442014-02-06 11:55:48 +08002571 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2572 if (res) {
2573 if (res == -ENODEV)
2574 netif_device_detach(tp->netdev);
2575 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2576 res);
hayeswang7e9da482014-02-18 21:49:05 +08002577 free_all_mem(tp);
hayeswang3d55f442014-02-06 11:55:48 +08002578 }
2579
hayeswang9a4be1b2014-02-18 21:49:07 +08002580 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002581
hayeswang7e9da482014-02-18 21:49:05 +08002582out:
hayeswangac718b62013-05-02 16:01:25 +00002583 return res;
2584}
2585
2586static int rtl8152_close(struct net_device *netdev)
2587{
2588 struct r8152 *tp = netdev_priv(netdev);
2589 int res = 0;
2590
2591 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002592 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002593 cancel_delayed_work_sync(&tp->schedule);
2594 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002595
2596 res = usb_autopm_get_interface(tp->intf);
2597 if (res < 0) {
2598 rtl_drop_queued_tx(tp);
2599 } else {
2600 /*
2601 * The autosuspend may have been enabled and wouldn't
2602 * be disable when autoresume occurs, because the
2603 * netif_running() would be false.
2604 */
2605 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2606 rtl_runtime_suspend_enable(tp, false);
2607 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2608 }
2609
2610 tasklet_disable(&tp->tl);
2611 tp->rtl_ops.down(tp);
2612 tasklet_enable(&tp->tl);
2613 usb_autopm_put_interface(tp->intf);
2614 }
hayeswangac718b62013-05-02 16:01:25 +00002615
hayeswang7e9da482014-02-18 21:49:05 +08002616 free_all_mem(tp);
2617
hayeswangac718b62013-05-02 16:01:25 +00002618 return res;
2619}
2620
hayeswangac718b62013-05-02 16:01:25 +00002621static void r8152b_enable_eee(struct r8152 *tp)
2622{
hayeswang45f4a192014-01-06 17:08:41 +08002623 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002624
2625 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2626 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2627 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2628 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2629 EEE_10_CAP | EEE_NWAY_EN |
2630 TX_QUIET_EN | RX_QUIET_EN |
2631 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2632 SDFALLTIME);
2633 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2634 RG_LDVQUIET_EN | RG_CKRSEL |
2635 RG_EEEPRG_EN);
2636 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2637 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2638 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2639 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2640 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2641 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2642}
2643
hayeswang43779f82014-01-02 11:25:10 +08002644static void r8153_enable_eee(struct r8152 *tp)
2645{
2646 u32 ocp_data;
2647 u16 data;
2648
2649 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2650 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2651 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2652 data = ocp_reg_read(tp, OCP_EEE_CFG);
2653 data |= EEE10_EN;
2654 ocp_reg_write(tp, OCP_EEE_CFG, data);
2655 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2656 data |= MY1000_EEE | MY100_EEE;
2657 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2658}
2659
hayeswangac718b62013-05-02 16:01:25 +00002660static void r8152b_enable_fc(struct r8152 *tp)
2661{
2662 u16 anar;
2663
2664 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2665 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2666 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2667}
2668
hayeswangac718b62013-05-02 16:01:25 +00002669static void r8152b_init(struct r8152 *tp)
2670{
hayeswangebc2ec42013-08-14 20:54:38 +08002671 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002672
hayeswangac718b62013-05-02 16:01:25 +00002673 if (tp->version == RTL_VER_01) {
2674 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2675 ocp_data &= ~LED_MODE_MASK;
2676 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2677 }
2678
hayeswang00a5e362014-02-18 21:48:59 +08002679 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002680
hayeswangac718b62013-05-02 16:01:25 +00002681 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2682 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2683 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2684 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2685 ocp_data &= ~MCU_CLK_RATIO_MASK;
2686 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2687 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2688 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2689 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2690 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2691
2692 r8152b_enable_eee(tp);
2693 r8152b_enable_aldps(tp);
2694 r8152b_enable_fc(tp);
2695
hayeswangebc2ec42013-08-14 20:54:38 +08002696 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002697 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec42013-08-14 20:54:38 +08002698 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002699 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2700}
2701
hayeswang43779f82014-01-02 11:25:10 +08002702static void r8153_init(struct r8152 *tp)
2703{
2704 u32 ocp_data;
2705 int i;
2706
hayeswangb9702722014-02-18 21:49:00 +08002707 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002708
2709 for (i = 0; i < 500; i++) {
2710 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2711 AUTOLOAD_DONE)
2712 break;
2713 msleep(20);
2714 }
2715
2716 for (i = 0; i < 500; i++) {
2717 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2718 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2719 break;
2720 msleep(20);
2721 }
2722
hayeswangb9702722014-02-18 21:49:00 +08002723 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002724
2725 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2726 ocp_data &= ~TIMER11_EN;
2727 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2728
hayeswang43779f82014-01-02 11:25:10 +08002729 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2730 ocp_data &= ~LED_MODE_MASK;
2731 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2732
2733 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2734 ocp_data &= ~LPM_TIMER_MASK;
2735 if (tp->udev->speed == USB_SPEED_SUPER)
2736 ocp_data |= LPM_TIMER_500US;
2737 else
2738 ocp_data |= LPM_TIMER_500MS;
2739 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2740
2741 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2742 ocp_data &= ~SEN_VAL_MASK;
2743 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2744 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2745
hayeswangb9702722014-02-18 21:49:00 +08002746 r8153_power_cut_en(tp, false);
2747 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002748
hayeswang43779f82014-01-02 11:25:10 +08002749 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2750 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2751 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2752 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2753 U1U2_SPDWN_EN | L1_SPDWN_EN);
2754 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2755 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2756 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2757 EEE_SPDWN_EN);
2758
2759 r8153_enable_eee(tp);
2760 r8153_enable_aldps(tp);
2761 r8152b_enable_fc(tp);
hayeswang43779f82014-01-02 11:25:10 +08002762}
2763
hayeswangac718b62013-05-02 16:01:25 +00002764static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2765{
2766 struct r8152 *tp = usb_get_intfdata(intf);
2767
hayeswang9a4be1b2014-02-18 21:49:07 +08002768 if (PMSG_IS_AUTO(message))
2769 set_bit(SELECTIVE_SUSPEND, &tp->flags);
2770 else
2771 netif_device_detach(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002772
2773 if (netif_running(tp->netdev)) {
2774 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002775 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002776 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08002777 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2778 rtl_runtime_suspend_enable(tp, true);
2779 } else {
2780 tasklet_disable(&tp->tl);
2781 tp->rtl_ops.down(tp);
2782 tasklet_enable(&tp->tl);
2783 }
hayeswangac718b62013-05-02 16:01:25 +00002784 }
2785
hayeswangac718b62013-05-02 16:01:25 +00002786 return 0;
2787}
2788
2789static int rtl8152_resume(struct usb_interface *intf)
2790{
2791 struct r8152 *tp = usb_get_intfdata(intf);
2792
hayeswang9a4be1b2014-02-18 21:49:07 +08002793 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2794 tp->rtl_ops.init(tp);
2795 netif_device_attach(tp->netdev);
2796 }
2797
hayeswangac718b62013-05-02 16:01:25 +00002798 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08002799 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2800 rtl_runtime_suspend_enable(tp, false);
2801 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2802 if (tp->speed & LINK_STATUS)
2803 tp->rtl_ops.disable(tp);
2804 } else {
2805 tp->rtl_ops.up(tp);
2806 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswang43779f82014-01-02 11:25:10 +08002807 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2808 DUPLEX_FULL);
hayeswang9a4be1b2014-02-18 21:49:07 +08002809 }
hayeswang40a82912013-08-14 20:54:40 +08002810 tp->speed = 0;
2811 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002812 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002813 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangac718b62013-05-02 16:01:25 +00002814 }
2815
2816 return 0;
2817}
2818
hayeswang21ff2e82014-02-18 21:49:06 +08002819static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2820{
2821 struct r8152 *tp = netdev_priv(dev);
2822
hayeswang9a4be1b2014-02-18 21:49:07 +08002823 if (usb_autopm_get_interface(tp->intf) < 0)
2824 return;
2825
hayeswang21ff2e82014-02-18 21:49:06 +08002826 wol->supported = WAKE_ANY;
2827 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08002828
2829 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08002830}
2831
2832static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2833{
2834 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002835 int ret;
2836
2837 ret = usb_autopm_get_interface(tp->intf);
2838 if (ret < 0)
2839 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08002840
2841 __rtl_set_wol(tp, wol->wolopts);
2842 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
2843
hayeswang9a4be1b2014-02-18 21:49:07 +08002844 usb_autopm_put_interface(tp->intf);
2845
2846out_set_wol:
2847 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08002848}
2849
hayeswanga5ec27c2014-02-18 21:49:11 +08002850static u32 rtl8152_get_msglevel(struct net_device *dev)
2851{
2852 struct r8152 *tp = netdev_priv(dev);
2853
2854 return tp->msg_enable;
2855}
2856
2857static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
2858{
2859 struct r8152 *tp = netdev_priv(dev);
2860
2861 tp->msg_enable = value;
2862}
2863
hayeswangac718b62013-05-02 16:01:25 +00002864static void rtl8152_get_drvinfo(struct net_device *netdev,
2865 struct ethtool_drvinfo *info)
2866{
2867 struct r8152 *tp = netdev_priv(netdev);
2868
2869 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2870 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2871 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2872}
2873
2874static
2875int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2876{
2877 struct r8152 *tp = netdev_priv(netdev);
2878
2879 if (!tp->mii.mdio_read)
2880 return -EOPNOTSUPP;
2881
2882 return mii_ethtool_gset(&tp->mii, cmd);
2883}
2884
2885static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2886{
2887 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002888 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002889
hayeswang9a4be1b2014-02-18 21:49:07 +08002890 ret = usb_autopm_get_interface(tp->intf);
2891 if (ret < 0)
2892 goto out;
2893
2894 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2895
2896 usb_autopm_put_interface(tp->intf);
2897
2898out:
2899 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002900}
2901
2902static struct ethtool_ops ops = {
2903 .get_drvinfo = rtl8152_get_drvinfo,
2904 .get_settings = rtl8152_get_settings,
2905 .set_settings = rtl8152_set_settings,
2906 .get_link = ethtool_op_get_link,
hayeswanga5ec27c2014-02-18 21:49:11 +08002907 .get_msglevel = rtl8152_get_msglevel,
2908 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08002909 .get_wol = rtl8152_get_wol,
2910 .set_wol = rtl8152_set_wol,
hayeswangac718b62013-05-02 16:01:25 +00002911};
2912
2913static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2914{
2915 struct r8152 *tp = netdev_priv(netdev);
2916 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08002917 int res;
2918
2919 res = usb_autopm_get_interface(tp->intf);
2920 if (res < 0)
2921 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002922
2923 switch (cmd) {
2924 case SIOCGMIIPHY:
2925 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2926 break;
2927
2928 case SIOCGMIIREG:
2929 data->val_out = r8152_mdio_read(tp, data->reg_num);
2930 break;
2931
2932 case SIOCSMIIREG:
2933 if (!capable(CAP_NET_ADMIN)) {
2934 res = -EPERM;
2935 break;
2936 }
2937 r8152_mdio_write(tp, data->reg_num, data->val_in);
2938 break;
2939
2940 default:
2941 res = -EOPNOTSUPP;
2942 }
2943
hayeswang9a4be1b2014-02-18 21:49:07 +08002944 usb_autopm_put_interface(tp->intf);
2945
2946out:
hayeswangac718b62013-05-02 16:01:25 +00002947 return res;
2948}
2949
2950static const struct net_device_ops rtl8152_netdev_ops = {
2951 .ndo_open = rtl8152_open,
2952 .ndo_stop = rtl8152_close,
2953 .ndo_do_ioctl = rtl8152_ioctl,
2954 .ndo_start_xmit = rtl8152_start_xmit,
2955 .ndo_tx_timeout = rtl8152_tx_timeout,
2956 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2957 .ndo_set_mac_address = rtl8152_set_mac_address,
2958
2959 .ndo_change_mtu = eth_change_mtu,
2960 .ndo_validate_addr = eth_validate_addr,
2961};
2962
2963static void r8152b_get_version(struct r8152 *tp)
2964{
2965 u32 ocp_data;
2966 u16 version;
2967
2968 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2969 version = (u16)(ocp_data & VERSION_MASK);
2970
2971 switch (version) {
2972 case 0x4c00:
2973 tp->version = RTL_VER_01;
2974 break;
2975 case 0x4c10:
2976 tp->version = RTL_VER_02;
2977 break;
hayeswang43779f82014-01-02 11:25:10 +08002978 case 0x5c00:
2979 tp->version = RTL_VER_03;
2980 tp->mii.supports_gmii = 1;
2981 break;
2982 case 0x5c10:
2983 tp->version = RTL_VER_04;
2984 tp->mii.supports_gmii = 1;
2985 break;
2986 case 0x5c20:
2987 tp->version = RTL_VER_05;
2988 tp->mii.supports_gmii = 1;
2989 break;
hayeswangac718b62013-05-02 16:01:25 +00002990 default:
2991 netif_info(tp, probe, tp->netdev,
2992 "Unknown version 0x%04x\n", version);
2993 break;
2994 }
2995}
2996
hayeswange3fe0b12014-01-02 11:22:39 +08002997static void rtl8152_unload(struct r8152 *tp)
2998{
hayeswang00a5e362014-02-18 21:48:59 +08002999 if (tp->version != RTL_VER_01)
3000 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08003001}
3002
hayeswang43779f82014-01-02 11:25:10 +08003003static void rtl8153_unload(struct r8152 *tp)
3004{
hayeswangb9702722014-02-18 21:49:00 +08003005 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003006}
3007
hayeswang31ca1de2014-01-06 17:08:43 +08003008static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08003009{
3010 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08003011 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08003012
3013 switch (id->idVendor) {
3014 case VENDOR_ID_REALTEK:
3015 switch (id->idProduct) {
3016 case PRODUCT_ID_RTL8152:
3017 ops->init = r8152b_init;
3018 ops->enable = rtl8152_enable;
3019 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003020 ops->up = r8152b_exit_oob;
hayeswangc81229c2014-01-02 11:22:42 +08003021 ops->down = rtl8152_down;
3022 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003023 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003024 break;
hayeswang43779f82014-01-02 11:25:10 +08003025 case PRODUCT_ID_RTL8153:
3026 ops->init = r8153_init;
3027 ops->enable = rtl8153_enable;
3028 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003029 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003030 ops->down = rtl8153_down;
3031 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003032 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003033 break;
3034 default:
hayeswang43779f82014-01-02 11:25:10 +08003035 break;
3036 }
3037 break;
3038
3039 case VENDOR_ID_SAMSUNG:
3040 switch (id->idProduct) {
3041 case PRODUCT_ID_SAMSUNG:
3042 ops->init = r8153_init;
3043 ops->enable = rtl8153_enable;
3044 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003045 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003046 ops->down = rtl8153_down;
3047 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003048 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003049 break;
hayeswangc81229c2014-01-02 11:22:42 +08003050 default:
hayeswangc81229c2014-01-02 11:22:42 +08003051 break;
3052 }
3053 break;
3054
3055 default:
hayeswangc81229c2014-01-02 11:22:42 +08003056 break;
3057 }
3058
hayeswang31ca1de2014-01-06 17:08:43 +08003059 if (ret)
3060 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
3061
hayeswangc81229c2014-01-02 11:22:42 +08003062 return ret;
3063}
3064
hayeswangac718b62013-05-02 16:01:25 +00003065static int rtl8152_probe(struct usb_interface *intf,
3066 const struct usb_device_id *id)
3067{
3068 struct usb_device *udev = interface_to_usbdev(intf);
3069 struct r8152 *tp;
3070 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08003071 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003072
hayeswangac718b62013-05-02 16:01:25 +00003073 netdev = alloc_etherdev(sizeof(struct r8152));
3074 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003075 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003076 return -ENOMEM;
3077 }
3078
hayeswangebc2ec42013-08-14 20:54:38 +08003079 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003080 tp = netdev_priv(netdev);
3081 tp->msg_enable = 0x7FFF;
3082
hayeswange3ad4122014-01-06 17:08:42 +08003083 tp->udev = udev;
3084 tp->netdev = netdev;
3085 tp->intf = intf;
3086
hayeswang31ca1de2014-01-06 17:08:43 +08003087 ret = rtl_ops_init(tp, id);
3088 if (ret)
3089 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003090
hayeswangebc2ec42013-08-14 20:54:38 +08003091 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00003092 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3093
hayeswangac718b62013-05-02 16:01:25 +00003094 netdev->netdev_ops = &rtl8152_netdev_ops;
3095 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003096
3097 netdev->features |= NETIF_F_IP_CSUM;
3098 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangdb8515e2014-03-06 15:07:16 +08003099
hayeswangac718b62013-05-02 16:01:25 +00003100 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00003101
3102 tp->mii.dev = netdev;
3103 tp->mii.mdio_read = read_mii_word;
3104 tp->mii.mdio_write = write_mii_word;
3105 tp->mii.phy_id_mask = 0x3f;
3106 tp->mii.reg_num_mask = 0x1f;
3107 tp->mii.phy_id = R8152_PHY_ID;
3108 tp->mii.supports_gmii = 0;
3109
hayeswang9a4be1b2014-02-18 21:49:07 +08003110 intf->needs_remote_wakeup = 1;
3111
hayeswangac718b62013-05-02 16:01:25 +00003112 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08003113 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003114 set_ethernet_addr(tp);
3115
hayeswangac718b62013-05-02 16:01:25 +00003116 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00003117
hayeswangebc2ec42013-08-14 20:54:38 +08003118 ret = register_netdev(netdev);
3119 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003120 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec42013-08-14 20:54:38 +08003121 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003122 }
3123
hayeswang21ff2e82014-02-18 21:49:06 +08003124 tp->saved_wolopts = __rtl_get_wol(tp);
3125 if (tp->saved_wolopts)
3126 device_set_wakeup_enable(&udev->dev, true);
3127 else
3128 device_set_wakeup_enable(&udev->dev, false);
3129
Hayes Wang4a8deae2014-01-07 11:18:22 +08003130 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003131
3132 return 0;
3133
hayeswangac718b62013-05-02 16:01:25 +00003134out1:
hayeswangebc2ec42013-08-14 20:54:38 +08003135 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00003136out:
3137 free_netdev(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08003138 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003139}
3140
hayeswangac718b62013-05-02 16:01:25 +00003141static void rtl8152_disconnect(struct usb_interface *intf)
3142{
3143 struct r8152 *tp = usb_get_intfdata(intf);
3144
3145 usb_set_intfdata(intf, NULL);
3146 if (tp) {
3147 set_bit(RTL8152_UNPLUG, &tp->flags);
3148 tasklet_kill(&tp->tl);
3149 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08003150 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00003151 free_netdev(tp->netdev);
3152 }
3153}
3154
3155/* table of devices that work with this driver */
3156static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08003157 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
3158 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
3159 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00003160 {}
3161};
3162
3163MODULE_DEVICE_TABLE(usb, rtl8152_table);
3164
3165static struct usb_driver rtl8152_driver = {
3166 .name = MODULENAME,
hayeswangebc2ec42013-08-14 20:54:38 +08003167 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00003168 .probe = rtl8152_probe,
3169 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00003170 .suspend = rtl8152_suspend,
hayeswangebc2ec42013-08-14 20:54:38 +08003171 .resume = rtl8152_resume,
3172 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08003173 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08003174 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00003175};
3176
Sachin Kamatb4236daa2013-05-16 17:48:08 +00003177module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00003178
3179MODULE_AUTHOR(DRIVER_AUTHOR);
3180MODULE_DESCRIPTION(DRIVER_DESC);
3181MODULE_LICENSE("GPL");