blob: ff02d5d8ec4143945bbe0a3d54ec1bc4e4ad773b [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);
596 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000597}
598
599static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
600 void *data, u16 type)
601{
hayeswang45f4a192014-01-06 17:08:41 +0800602 u16 limit = 64;
603 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000604
605 if (test_bit(RTL8152_UNPLUG, &tp->flags))
606 return -ENODEV;
607
608 /* both size and indix must be 4 bytes align */
609 if ((size & 3) || !size || (index & 3) || !data)
610 return -EPERM;
611
612 if ((u32)index + (u32)size > 0xffff)
613 return -EPERM;
614
615 while (size) {
616 if (size > limit) {
617 ret = get_registers(tp, index, type, limit, data);
618 if (ret < 0)
619 break;
620
621 index += limit;
622 data += limit;
623 size -= limit;
624 } else {
625 ret = get_registers(tp, index, type, size, data);
626 if (ret < 0)
627 break;
628
629 index += size;
630 data += size;
631 size = 0;
632 break;
633 }
634 }
635
636 return ret;
637}
638
639static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
640 u16 size, void *data, u16 type)
641{
hayeswang45f4a192014-01-06 17:08:41 +0800642 int ret;
643 u16 byteen_start, byteen_end, byen;
644 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000645
646 if (test_bit(RTL8152_UNPLUG, &tp->flags))
647 return -ENODEV;
648
649 /* both size and indix must be 4 bytes align */
650 if ((size & 3) || !size || (index & 3) || !data)
651 return -EPERM;
652
653 if ((u32)index + (u32)size > 0xffff)
654 return -EPERM;
655
656 byteen_start = byteen & BYTE_EN_START_MASK;
657 byteen_end = byteen & BYTE_EN_END_MASK;
658
659 byen = byteen_start | (byteen_start << 4);
660 ret = set_registers(tp, index, type | byen, 4, data);
661 if (ret < 0)
662 goto error1;
663
664 index += 4;
665 data += 4;
666 size -= 4;
667
668 if (size) {
669 size -= 4;
670
671 while (size) {
672 if (size > limit) {
673 ret = set_registers(tp, index,
674 type | BYTE_EN_DWORD,
675 limit, data);
676 if (ret < 0)
677 goto error1;
678
679 index += limit;
680 data += limit;
681 size -= limit;
682 } else {
683 ret = set_registers(tp, index,
684 type | BYTE_EN_DWORD,
685 size, data);
686 if (ret < 0)
687 goto error1;
688
689 index += size;
690 data += size;
691 size = 0;
692 break;
693 }
694 }
695
696 byen = byteen_end | (byteen_end >> 4);
697 ret = set_registers(tp, index, type | byen, 4, data);
698 if (ret < 0)
699 goto error1;
700 }
701
702error1:
703 return ret;
704}
705
706static inline
707int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
708{
709 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
710}
711
712static inline
713int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
714{
715 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
716}
717
718static inline
719int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
720{
721 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
722}
723
724static inline
725int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
726{
727 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
728}
729
730static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
731{
hayeswangc8826de2013-07-31 17:21:26 +0800732 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000733
hayeswangc8826de2013-07-31 17:21:26 +0800734 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000735
736 return __le32_to_cpu(data);
737}
738
739static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
740{
hayeswangc8826de2013-07-31 17:21:26 +0800741 __le32 tmp = __cpu_to_le32(data);
742
743 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000744}
745
746static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
747{
748 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800749 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000750 u8 shift = index & 2;
751
752 index &= ~3;
753
hayeswangc8826de2013-07-31 17:21:26 +0800754 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000755
hayeswangc8826de2013-07-31 17:21:26 +0800756 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000757 data >>= (shift * 8);
758 data &= 0xffff;
759
760 return (u16)data;
761}
762
763static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
764{
hayeswangc8826de2013-07-31 17:21:26 +0800765 u32 mask = 0xffff;
766 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000767 u16 byen = BYTE_EN_WORD;
768 u8 shift = index & 2;
769
770 data &= mask;
771
772 if (index & 2) {
773 byen <<= shift;
774 mask <<= (shift * 8);
775 data <<= (shift * 8);
776 index &= ~3;
777 }
778
hayeswangc8826de2013-07-31 17:21:26 +0800779 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000780
hayeswangc8826de2013-07-31 17:21:26 +0800781 data |= __le32_to_cpu(tmp) & ~mask;
782 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000783
hayeswangc8826de2013-07-31 17:21:26 +0800784 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000785}
786
787static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
788{
789 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800790 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000791 u8 shift = index & 3;
792
793 index &= ~3;
794
hayeswangc8826de2013-07-31 17:21:26 +0800795 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000796
hayeswangc8826de2013-07-31 17:21:26 +0800797 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000798 data >>= (shift * 8);
799 data &= 0xff;
800
801 return (u8)data;
802}
803
804static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
805{
hayeswangc8826de2013-07-31 17:21:26 +0800806 u32 mask = 0xff;
807 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000808 u16 byen = BYTE_EN_BYTE;
809 u8 shift = index & 3;
810
811 data &= mask;
812
813 if (index & 3) {
814 byen <<= shift;
815 mask <<= (shift * 8);
816 data <<= (shift * 8);
817 index &= ~3;
818 }
819
hayeswangc8826de2013-07-31 17:21:26 +0800820 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000821
hayeswangc8826de2013-07-31 17:21:26 +0800822 data |= __le32_to_cpu(tmp) & ~mask;
823 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000824
hayeswangc8826de2013-07-31 17:21:26 +0800825 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000826}
827
hayeswangac244d32014-01-02 11:22:40 +0800828static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
829{
830 u16 ocp_base, ocp_index;
831
832 ocp_base = addr & 0xf000;
833 if (ocp_base != tp->ocp_base) {
834 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
835 tp->ocp_base = ocp_base;
836 }
837
838 ocp_index = (addr & 0x0fff) | 0xb000;
839 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
840}
841
hayeswange3fe0b12014-01-02 11:22:39 +0800842static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
843{
844 u16 ocp_base, ocp_index;
845
846 ocp_base = addr & 0xf000;
847 if (ocp_base != tp->ocp_base) {
848 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
849 tp->ocp_base = ocp_base;
850 }
851
852 ocp_index = (addr & 0x0fff) | 0xb000;
853 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
854}
855
hayeswangac244d32014-01-02 11:22:40 +0800856static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000857{
hayeswangac244d32014-01-02 11:22:40 +0800858 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000859}
860
hayeswangac244d32014-01-02 11:22:40 +0800861static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000862{
hayeswangac244d32014-01-02 11:22:40 +0800863 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000864}
865
hayeswang43779f82014-01-02 11:25:10 +0800866static void sram_write(struct r8152 *tp, u16 addr, u16 data)
867{
868 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
869 ocp_reg_write(tp, OCP_SRAM_DATA, data);
870}
871
872static u16 sram_read(struct r8152 *tp, u16 addr)
873{
874 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
875 return ocp_reg_read(tp, OCP_SRAM_DATA);
876}
877
hayeswangac718b62013-05-02 16:01:25 +0000878static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
879{
880 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +0800881 int ret;
hayeswangac718b62013-05-02 16:01:25 +0000882
883 if (phy_id != R8152_PHY_ID)
884 return -EINVAL;
885
hayeswang9a4be1b2014-02-18 21:49:07 +0800886 ret = usb_autopm_get_interface(tp->intf);
887 if (ret < 0)
888 goto out;
889
890 ret = r8152_mdio_read(tp, reg);
891
892 usb_autopm_put_interface(tp->intf);
893
894out:
895 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000896}
897
898static
899void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
900{
901 struct r8152 *tp = netdev_priv(netdev);
902
903 if (phy_id != R8152_PHY_ID)
904 return;
905
hayeswang9a4be1b2014-02-18 21:49:07 +0800906 if (usb_autopm_get_interface(tp->intf) < 0)
907 return;
908
hayeswangac718b62013-05-02 16:01:25 +0000909 r8152_mdio_write(tp, reg, val);
hayeswang9a4be1b2014-02-18 21:49:07 +0800910
911 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +0000912}
913
hayeswangebc2ec42013-08-14 20:54:38 +0800914static
915int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
916
hayeswangac718b62013-05-02 16:01:25 +0000917static inline void set_ethernet_addr(struct r8152 *tp)
918{
919 struct net_device *dev = tp->netdev;
hayeswang8a91c822014-02-18 21:49:01 +0800920 int ret;
hayeswang31787f52013-07-31 17:21:25 +0800921 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000922
hayeswang8a91c822014-02-18 21:49:01 +0800923 if (tp->version == RTL_VER_01)
924 ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
925 else
926 ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
927
928 if (ret < 0) {
hayeswangac718b62013-05-02 16:01:25 +0000929 netif_notice(tp, probe, dev, "inet addr fail\n");
hayeswang8a91c822014-02-18 21:49:01 +0800930 } else {
931 if (tp->version != RTL_VER_01) {
932 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
933 CRWECR_CONFIG);
934 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
935 sizeof(node_id), node_id);
936 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
937 CRWECR_NORAML);
938 }
939
hayeswangac718b62013-05-02 16:01:25 +0000940 memcpy(dev->dev_addr, node_id, dev->addr_len);
941 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
942 }
hayeswangac718b62013-05-02 16:01:25 +0000943}
944
945static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
946{
947 struct r8152 *tp = netdev_priv(netdev);
948 struct sockaddr *addr = p;
949
950 if (!is_valid_ether_addr(addr->sa_data))
951 return -EADDRNOTAVAIL;
952
953 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
954
955 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
956 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
957 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
958
959 return 0;
960}
961
hayeswangac718b62013-05-02 16:01:25 +0000962static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
963{
964 return &dev->stats;
965}
966
967static void read_bulk_callback(struct urb *urb)
968{
hayeswangac718b62013-05-02 16:01:25 +0000969 struct net_device *netdev;
hayeswanga5a4f462013-08-16 16:09:34 +0800970 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +0000971 int status = urb->status;
hayeswangebc2ec42013-08-14 20:54:38 +0800972 struct rx_agg *agg;
973 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000974 int result;
hayeswangac718b62013-05-02 16:01:25 +0000975
hayeswangebc2ec42013-08-14 20:54:38 +0800976 agg = urb->context;
977 if (!agg)
978 return;
979
980 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000981 if (!tp)
982 return;
hayeswangebc2ec42013-08-14 20:54:38 +0800983
hayeswangac718b62013-05-02 16:01:25 +0000984 if (test_bit(RTL8152_UNPLUG, &tp->flags))
985 return;
hayeswangebc2ec42013-08-14 20:54:38 +0800986
987 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000988 return;
989
hayeswangebc2ec42013-08-14 20:54:38 +0800990 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800991
992 /* When link down, the driver would cancel all bulks. */
993 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +0800994 if (!netif_carrier_ok(netdev))
995 return;
996
hayeswang9a4be1b2014-02-18 21:49:07 +0800997 usb_mark_last_busy(tp->udev);
998
hayeswangac718b62013-05-02 16:01:25 +0000999 switch (status) {
1000 case 0:
hayeswangebc2ec42013-08-14 20:54:38 +08001001 if (urb->actual_length < ETH_ZLEN)
1002 break;
1003
hayeswanga5a4f462013-08-16 16:09:34 +08001004 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001005 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +08001006 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001007 tasklet_schedule(&tp->tl);
1008 return;
hayeswangac718b62013-05-02 16:01:25 +00001009 case -ESHUTDOWN:
1010 set_bit(RTL8152_UNPLUG, &tp->flags);
1011 netif_device_detach(tp->netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001012 return;
hayeswangac718b62013-05-02 16:01:25 +00001013 case -ENOENT:
1014 return; /* the urb is in unlink state */
1015 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001016 if (net_ratelimit())
1017 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001018 break;
hayeswangac718b62013-05-02 16:01:25 +00001019 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001020 if (net_ratelimit())
1021 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001022 break;
hayeswangac718b62013-05-02 16:01:25 +00001023 }
1024
hayeswangebc2ec42013-08-14 20:54:38 +08001025 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001026 if (result == -ENODEV) {
1027 netif_device_detach(tp->netdev);
1028 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +08001029 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001030 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +08001031 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001032 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001033 }
hayeswangac718b62013-05-02 16:01:25 +00001034}
1035
1036static void write_bulk_callback(struct urb *urb)
1037{
hayeswangebc2ec42013-08-14 20:54:38 +08001038 struct net_device_stats *stats;
hayeswanga5a4f462013-08-16 16:09:34 +08001039 unsigned long flags;
hayeswangebc2ec42013-08-14 20:54:38 +08001040 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001041 struct r8152 *tp;
1042 int status = urb->status;
1043
hayeswangebc2ec42013-08-14 20:54:38 +08001044 agg = urb->context;
1045 if (!agg)
1046 return;
1047
1048 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001049 if (!tp)
1050 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001051
1052 stats = rtl8152_get_stats(tp->netdev);
1053 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001054 if (net_ratelimit())
1055 netdev_warn(tp->netdev, "Tx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +08001056 stats->tx_errors += agg->skb_num;
1057 } else {
1058 stats->tx_packets += agg->skb_num;
1059 stats->tx_bytes += agg->skb_len;
1060 }
1061
hayeswanga5a4f462013-08-16 16:09:34 +08001062 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001063 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +08001064 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001065
hayeswang9a4be1b2014-02-18 21:49:07 +08001066 usb_autopm_put_interface_async(tp->intf);
1067
hayeswangebc2ec42013-08-14 20:54:38 +08001068 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +00001069 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001070
1071 if (!test_bit(WORK_ENABLE, &tp->flags))
1072 return;
1073
1074 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1075 return;
1076
1077 if (!skb_queue_empty(&tp->tx_queue))
hayeswang9a4be1b2014-02-18 21:49:07 +08001078 schedule_delayed_work(&tp->schedule, 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001079}
1080
hayeswang40a82912013-08-14 20:54:40 +08001081static void intr_callback(struct urb *urb)
1082{
1083 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001084 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001085 int status = urb->status;
1086 int res;
1087
1088 tp = urb->context;
1089 if (!tp)
1090 return;
1091
1092 if (!test_bit(WORK_ENABLE, &tp->flags))
1093 return;
1094
1095 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1096 return;
1097
1098 switch (status) {
1099 case 0: /* success */
1100 break;
1101 case -ECONNRESET: /* unlink */
1102 case -ESHUTDOWN:
1103 netif_device_detach(tp->netdev);
1104 case -ENOENT:
1105 return;
1106 case -EOVERFLOW:
1107 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1108 goto resubmit;
1109 /* -EPIPE: should clear the halt */
1110 default:
1111 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1112 goto resubmit;
1113 }
1114
1115 d = urb->transfer_buffer;
1116 if (INTR_LINK & __le16_to_cpu(d[0])) {
1117 if (!(tp->speed & LINK_STATUS)) {
1118 set_bit(RTL8152_LINK_CHG, &tp->flags);
1119 schedule_delayed_work(&tp->schedule, 0);
1120 }
1121 } else {
1122 if (tp->speed & LINK_STATUS) {
1123 set_bit(RTL8152_LINK_CHG, &tp->flags);
1124 schedule_delayed_work(&tp->schedule, 0);
1125 }
1126 }
1127
1128resubmit:
1129 res = usb_submit_urb(urb, GFP_ATOMIC);
1130 if (res == -ENODEV)
1131 netif_device_detach(tp->netdev);
1132 else if (res)
1133 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001134 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001135}
1136
hayeswangebc2ec42013-08-14 20:54:38 +08001137static inline void *rx_agg_align(void *data)
1138{
hayeswang8e1f51b2014-01-02 11:22:41 +08001139 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001140}
1141
1142static inline void *tx_agg_align(void *data)
1143{
hayeswang8e1f51b2014-01-02 11:22:41 +08001144 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001145}
1146
1147static void free_all_mem(struct r8152 *tp)
1148{
1149 int i;
1150
1151 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001152 usb_free_urb(tp->rx_info[i].urb);
1153 tp->rx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001154
hayeswang9629e3c2014-01-15 10:42:15 +08001155 kfree(tp->rx_info[i].buffer);
1156 tp->rx_info[i].buffer = NULL;
1157 tp->rx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001158 }
1159
1160 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001161 usb_free_urb(tp->tx_info[i].urb);
1162 tp->tx_info[i].urb = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001163
hayeswang9629e3c2014-01-15 10:42:15 +08001164 kfree(tp->tx_info[i].buffer);
1165 tp->tx_info[i].buffer = NULL;
1166 tp->tx_info[i].head = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001167 }
hayeswang40a82912013-08-14 20:54:40 +08001168
hayeswang9629e3c2014-01-15 10:42:15 +08001169 usb_free_urb(tp->intr_urb);
1170 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001171
hayeswang9629e3c2014-01-15 10:42:15 +08001172 kfree(tp->intr_buff);
1173 tp->intr_buff = NULL;
hayeswangebc2ec42013-08-14 20:54:38 +08001174}
1175
1176static int alloc_all_mem(struct r8152 *tp)
1177{
1178 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001179 struct usb_interface *intf = tp->intf;
1180 struct usb_host_interface *alt = intf->cur_altsetting;
1181 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec42013-08-14 20:54:38 +08001182 struct urb *urb;
1183 int node, i;
1184 u8 *buf;
1185
1186 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1187
1188 spin_lock_init(&tp->rx_lock);
1189 spin_lock_init(&tp->tx_lock);
1190 INIT_LIST_HEAD(&tp->rx_done);
1191 INIT_LIST_HEAD(&tp->tx_free);
1192 skb_queue_head_init(&tp->tx_queue);
1193
1194 for (i = 0; i < RTL8152_MAX_RX; i++) {
1195 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1196 if (!buf)
1197 goto err1;
1198
1199 if (buf != rx_agg_align(buf)) {
1200 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001201 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1202 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001203 if (!buf)
1204 goto err1;
1205 }
1206
1207 urb = usb_alloc_urb(0, GFP_KERNEL);
1208 if (!urb) {
1209 kfree(buf);
1210 goto err1;
1211 }
1212
1213 INIT_LIST_HEAD(&tp->rx_info[i].list);
1214 tp->rx_info[i].context = tp;
1215 tp->rx_info[i].urb = urb;
1216 tp->rx_info[i].buffer = buf;
1217 tp->rx_info[i].head = rx_agg_align(buf);
1218 }
1219
1220 for (i = 0; i < RTL8152_MAX_TX; i++) {
1221 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1222 if (!buf)
1223 goto err1;
1224
1225 if (buf != tx_agg_align(buf)) {
1226 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001227 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1228 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001229 if (!buf)
1230 goto err1;
1231 }
1232
1233 urb = usb_alloc_urb(0, GFP_KERNEL);
1234 if (!urb) {
1235 kfree(buf);
1236 goto err1;
1237 }
1238
1239 INIT_LIST_HEAD(&tp->tx_info[i].list);
1240 tp->tx_info[i].context = tp;
1241 tp->tx_info[i].urb = urb;
1242 tp->tx_info[i].buffer = buf;
1243 tp->tx_info[i].head = tx_agg_align(buf);
1244
1245 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1246 }
1247
hayeswang40a82912013-08-14 20:54:40 +08001248 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1249 if (!tp->intr_urb)
1250 goto err1;
1251
1252 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1253 if (!tp->intr_buff)
1254 goto err1;
1255
1256 tp->intr_interval = (int)ep_intr->desc.bInterval;
1257 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1258 tp->intr_buff, INTBUFSIZE, intr_callback,
1259 tp, tp->intr_interval);
1260
hayeswangebc2ec42013-08-14 20:54:38 +08001261 return 0;
1262
1263err1:
1264 free_all_mem(tp);
1265 return -ENOMEM;
1266}
1267
hayeswang0de98f62013-08-16 16:09:35 +08001268static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1269{
1270 struct tx_agg *agg = NULL;
1271 unsigned long flags;
1272
1273 spin_lock_irqsave(&tp->tx_lock, flags);
1274 if (!list_empty(&tp->tx_free)) {
1275 struct list_head *cursor;
1276
1277 cursor = tp->tx_free.next;
1278 list_del_init(cursor);
1279 agg = list_entry(cursor, struct tx_agg, list);
1280 }
1281 spin_unlock_irqrestore(&tp->tx_lock, flags);
1282
1283 return agg;
1284}
1285
hayeswang5bd23882013-08-14 20:54:39 +08001286static void
1287r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1288{
1289 memset(desc, 0, sizeof(*desc));
1290
1291 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1292
1293 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1294 __be16 protocol;
1295 u8 ip_protocol;
1296 u32 opts2 = 0;
1297
1298 if (skb->protocol == htons(ETH_P_8021Q))
1299 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1300 else
1301 protocol = skb->protocol;
1302
1303 switch (protocol) {
1304 case htons(ETH_P_IP):
1305 opts2 |= IPV4_CS;
1306 ip_protocol = ip_hdr(skb)->protocol;
1307 break;
1308
1309 case htons(ETH_P_IPV6):
1310 opts2 |= IPV6_CS;
1311 ip_protocol = ipv6_hdr(skb)->nexthdr;
1312 break;
1313
1314 default:
1315 ip_protocol = IPPROTO_RAW;
1316 break;
1317 }
1318
1319 if (ip_protocol == IPPROTO_TCP) {
1320 opts2 |= TCP_CS;
1321 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1322 } else if (ip_protocol == IPPROTO_UDP) {
1323 opts2 |= UDP_CS;
1324 } else {
1325 WARN_ON_ONCE(1);
1326 }
1327
1328 desc->opts2 = cpu_to_le32(opts2);
1329 }
1330}
1331
hayeswangb1379d92013-08-16 16:09:37 +08001332static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1333{
hayeswangd84130a2014-02-18 21:49:02 +08001334 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1335 unsigned long flags;
hayeswang9a4be1b2014-02-18 21:49:07 +08001336 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001337 u8 *tx_data;
1338
hayeswangd84130a2014-02-18 21:49:02 +08001339 __skb_queue_head_init(&skb_head);
1340 spin_lock_irqsave(&tx_queue->lock, flags);
1341 skb_queue_splice_init(tx_queue, &skb_head);
1342 spin_unlock_irqrestore(&tx_queue->lock, flags);
1343
hayeswangb1379d92013-08-16 16:09:37 +08001344 tx_data = agg->head;
1345 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001346 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001347
hayeswang7937f9e2013-11-20 17:30:54 +08001348 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001349 struct tx_desc *tx_desc;
1350 struct sk_buff *skb;
1351 unsigned int len;
1352
hayeswangd84130a2014-02-18 21:49:02 +08001353 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001354 if (!skb)
1355 break;
1356
hayeswang7937f9e2013-11-20 17:30:54 +08001357 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001358 len = skb->len;
1359 if (remain < len) {
hayeswangd84130a2014-02-18 21:49:02 +08001360 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001361 break;
1362 }
1363
hayeswang7937f9e2013-11-20 17:30:54 +08001364 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001365 tx_desc = (struct tx_desc *)tx_data;
1366 tx_data += sizeof(*tx_desc);
1367
1368 r8152_tx_csum(tp, tx_desc, skb);
1369 memcpy(tx_data, skb->data, len);
1370 agg->skb_num++;
1371 agg->skb_len += len;
1372 dev_kfree_skb_any(skb);
1373
hayeswang7937f9e2013-11-20 17:30:54 +08001374 tx_data += len;
1375 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001376 }
1377
hayeswangd84130a2014-02-18 21:49:02 +08001378 if (!skb_queue_empty(&skb_head)) {
1379 spin_lock_irqsave(&tx_queue->lock, flags);
1380 skb_queue_splice(&skb_head, tx_queue);
1381 spin_unlock_irqrestore(&tx_queue->lock, flags);
1382 }
1383
hayeswang9a4be1b2014-02-18 21:49:07 +08001384 netif_tx_lock_bh(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001385
1386 if (netif_queue_stopped(tp->netdev) &&
1387 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1388 netif_wake_queue(tp->netdev);
1389
hayeswang9a4be1b2014-02-18 21:49:07 +08001390 netif_tx_unlock_bh(tp->netdev);
1391
1392 ret = usb_autopm_get_interface(tp->intf);
1393 if (ret < 0)
1394 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001395
hayeswangb1379d92013-08-16 16:09:37 +08001396 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1397 agg->head, (int)(tx_data - (u8 *)agg->head),
1398 (usb_complete_t)write_bulk_callback, agg);
1399
hayeswang9a4be1b2014-02-18 21:49:07 +08001400 ret = usb_submit_urb(agg->urb, GFP_KERNEL);
1401 if (ret < 0)
1402 usb_autopm_put_interface(tp->intf);
1403
1404out_tx_fill:
1405 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001406}
1407
hayeswangebc2ec42013-08-14 20:54:38 +08001408static void rx_bottom(struct r8152 *tp)
1409{
hayeswanga5a4f462013-08-16 16:09:34 +08001410 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001411 struct list_head *cursor, *next, rx_queue;
hayeswangebc2ec42013-08-14 20:54:38 +08001412
hayeswangd84130a2014-02-18 21:49:02 +08001413 if (list_empty(&tp->rx_done))
1414 return;
1415
1416 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001417 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001418 list_splice_init(&tp->rx_done, &rx_queue);
1419 spin_unlock_irqrestore(&tp->rx_lock, flags);
1420
1421 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001422 struct rx_desc *rx_desc;
1423 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001424 int len_used = 0;
1425 struct urb *urb;
1426 u8 *rx_data;
1427 int ret;
1428
hayeswangebc2ec42013-08-14 20:54:38 +08001429 list_del_init(cursor);
hayeswangebc2ec42013-08-14 20:54:38 +08001430
1431 agg = list_entry(cursor, struct rx_agg, list);
1432 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001433 if (urb->actual_length < ETH_ZLEN)
1434 goto submit;
hayeswangebc2ec42013-08-14 20:54:38 +08001435
hayeswangebc2ec42013-08-14 20:54:38 +08001436 rx_desc = agg->head;
1437 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001438 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001439
hayeswang7937f9e2013-11-20 17:30:54 +08001440 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001441 struct net_device *netdev = tp->netdev;
1442 struct net_device_stats *stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001443 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001444 struct sk_buff *skb;
1445
hayeswang7937f9e2013-11-20 17:30:54 +08001446 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec42013-08-14 20:54:38 +08001447 if (pkt_len < ETH_ZLEN)
1448 break;
1449
hayeswang7937f9e2013-11-20 17:30:54 +08001450 len_used += pkt_len;
1451 if (urb->actual_length < len_used)
1452 break;
1453
hayeswang43a44782013-08-16 16:09:36 +08001454 stats = rtl8152_get_stats(netdev);
1455
hayeswang8e1f51b2014-01-02 11:22:41 +08001456 pkt_len -= CRC_SIZE;
hayeswangebc2ec42013-08-14 20:54:38 +08001457 rx_data += sizeof(struct rx_desc);
1458
1459 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1460 if (!skb) {
1461 stats->rx_dropped++;
1462 break;
1463 }
1464 memcpy(skb->data, rx_data, pkt_len);
1465 skb_put(skb, pkt_len);
1466 skb->protocol = eth_type_trans(skb, netdev);
hayeswang9d9aafa2014-02-18 21:49:09 +08001467 netif_receive_skb(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001468 stats->rx_packets++;
1469 stats->rx_bytes += pkt_len;
1470
hayeswang8e1f51b2014-01-02 11:22:41 +08001471 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec42013-08-14 20:54:38 +08001472 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec42013-08-14 20:54:38 +08001473 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001474 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001475 }
1476
hayeswang0de98f62013-08-16 16:09:35 +08001477submit:
hayeswangebc2ec42013-08-14 20:54:38 +08001478 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangebc2ec42013-08-14 20:54:38 +08001479 if (ret && ret != -ENODEV) {
hayeswangd84130a2014-02-18 21:49:02 +08001480 spin_lock_irqsave(&tp->rx_lock, flags);
1481 list_add_tail(&agg->list, &tp->rx_done);
1482 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001483 tasklet_schedule(&tp->tl);
1484 }
1485 }
hayeswangebc2ec42013-08-14 20:54:38 +08001486}
1487
1488static void tx_bottom(struct r8152 *tp)
1489{
hayeswangebc2ec42013-08-14 20:54:38 +08001490 int res;
1491
hayeswangb1379d92013-08-16 16:09:37 +08001492 do {
1493 struct tx_agg *agg;
hayeswangebc2ec42013-08-14 20:54:38 +08001494
hayeswangb1379d92013-08-16 16:09:37 +08001495 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec42013-08-14 20:54:38 +08001496 break;
1497
hayeswangb1379d92013-08-16 16:09:37 +08001498 agg = r8152_get_tx_agg(tp);
1499 if (!agg)
hayeswangebc2ec42013-08-14 20:54:38 +08001500 break;
hayeswangb1379d92013-08-16 16:09:37 +08001501
1502 res = r8152_tx_agg_fill(tp, agg);
1503 if (res) {
1504 struct net_device_stats *stats;
1505 struct net_device *netdev;
1506 unsigned long flags;
1507
1508 netdev = tp->netdev;
1509 stats = rtl8152_get_stats(netdev);
1510
1511 if (res == -ENODEV) {
1512 netif_device_detach(netdev);
1513 } else {
1514 netif_warn(tp, tx_err, netdev,
1515 "failed tx_urb %d\n", res);
1516 stats->tx_dropped += agg->skb_num;
1517 spin_lock_irqsave(&tp->tx_lock, flags);
1518 list_add_tail(&agg->list, &tp->tx_free);
1519 spin_unlock_irqrestore(&tp->tx_lock, flags);
1520 }
hayeswangebc2ec42013-08-14 20:54:38 +08001521 }
hayeswangb1379d92013-08-16 16:09:37 +08001522 } while (res == 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001523}
1524
1525static void bottom_half(unsigned long data)
1526{
1527 struct r8152 *tp;
1528
1529 tp = (struct r8152 *)data;
1530
1531 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1532 return;
1533
1534 if (!test_bit(WORK_ENABLE, &tp->flags))
1535 return;
1536
hayeswang7559fb2f2013-08-16 16:09:38 +08001537 /* When link down, the driver would cancel all bulks. */
1538 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001539 if (!netif_carrier_ok(tp->netdev))
1540 return;
1541
1542 rx_bottom(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001543}
1544
1545static
1546int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1547{
1548 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1549 agg->head, rx_buf_sz,
1550 (usb_complete_t)read_bulk_callback, agg);
1551
1552 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001553}
1554
hayeswang00a5e362014-02-18 21:48:59 +08001555static void rtl_drop_queued_tx(struct r8152 *tp)
1556{
1557 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08001558 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1559 unsigned long flags;
hayeswang00a5e362014-02-18 21:48:59 +08001560 struct sk_buff *skb;
1561
hayeswangd84130a2014-02-18 21:49:02 +08001562 if (skb_queue_empty(tx_queue))
1563 return;
1564
1565 __skb_queue_head_init(&skb_head);
1566 spin_lock_irqsave(&tx_queue->lock, flags);
1567 skb_queue_splice_init(tx_queue, &skb_head);
1568 spin_unlock_irqrestore(&tx_queue->lock, flags);
1569
1570 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08001571 dev_kfree_skb(skb);
1572 stats->tx_dropped++;
1573 }
1574}
1575
hayeswangac718b62013-05-02 16:01:25 +00001576static void rtl8152_tx_timeout(struct net_device *netdev)
1577{
1578 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001579 int i;
1580
Hayes Wang4a8deae2014-01-07 11:18:22 +08001581 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001582 for (i = 0; i < RTL8152_MAX_TX; i++)
1583 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001584}
1585
1586static void rtl8152_set_rx_mode(struct net_device *netdev)
1587{
1588 struct r8152 *tp = netdev_priv(netdev);
1589
hayeswang40a82912013-08-14 20:54:40 +08001590 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001591 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001592 schedule_delayed_work(&tp->schedule, 0);
1593 }
hayeswangac718b62013-05-02 16:01:25 +00001594}
1595
1596static void _rtl8152_set_rx_mode(struct net_device *netdev)
1597{
1598 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001599 u32 mc_filter[2]; /* Multicast hash filter */
1600 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001601 u32 ocp_data;
1602
hayeswangac718b62013-05-02 16:01:25 +00001603 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1604 netif_stop_queue(netdev);
1605 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1606 ocp_data &= ~RCR_ACPT_ALL;
1607 ocp_data |= RCR_AB | RCR_APM;
1608
1609 if (netdev->flags & IFF_PROMISC) {
1610 /* Unconditionally log net taps. */
1611 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1612 ocp_data |= RCR_AM | RCR_AAP;
1613 mc_filter[1] = mc_filter[0] = 0xffffffff;
1614 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1615 (netdev->flags & IFF_ALLMULTI)) {
1616 /* Too many to filter perfectly -- accept all multicasts. */
1617 ocp_data |= RCR_AM;
1618 mc_filter[1] = mc_filter[0] = 0xffffffff;
1619 } else {
1620 struct netdev_hw_addr *ha;
1621
1622 mc_filter[1] = mc_filter[0] = 0;
1623 netdev_for_each_mc_addr(ha, netdev) {
1624 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1625 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1626 ocp_data |= RCR_AM;
1627 }
1628 }
1629
hayeswang31787f52013-07-31 17:21:25 +08001630 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1631 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001632
hayeswang31787f52013-07-31 17:21:25 +08001633 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001634 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1635 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001636}
1637
1638static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1639 struct net_device *netdev)
1640{
1641 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001642
hayeswangac718b62013-05-02 16:01:25 +00001643 skb_tx_timestamp(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001644
hayeswang61598782013-11-20 17:30:55 +08001645 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001646
hayeswangdd1b1192013-11-20 17:30:56 +08001647 if (list_empty(&tp->tx_free) &&
1648 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1649 netif_stop_queue(netdev);
1650
hayeswang61598782013-11-20 17:30:55 +08001651 if (!list_empty(&tp->tx_free))
hayeswang9a4be1b2014-02-18 21:49:07 +08001652 schedule_delayed_work(&tp->schedule, 0);
hayeswangac718b62013-05-02 16:01:25 +00001653
1654 return NETDEV_TX_OK;
1655}
1656
1657static void r8152b_reset_packet_filter(struct r8152 *tp)
1658{
1659 u32 ocp_data;
1660
1661 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1662 ocp_data &= ~FMC_FCR_MCU_EN;
1663 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1664 ocp_data |= FMC_FCR_MCU_EN;
1665 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1666}
1667
1668static void rtl8152_nic_reset(struct r8152 *tp)
1669{
1670 int i;
1671
1672 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1673
1674 for (i = 0; i < 1000; i++) {
1675 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1676 break;
1677 udelay(100);
1678 }
1679}
1680
hayeswangdd1b1192013-11-20 17:30:56 +08001681static void set_tx_qlen(struct r8152 *tp)
1682{
1683 struct net_device *netdev = tp->netdev;
1684
1685 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1686 sizeof(struct tx_desc));
1687}
1688
hayeswangac718b62013-05-02 16:01:25 +00001689static inline u8 rtl8152_get_speed(struct r8152 *tp)
1690{
1691 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1692}
1693
hayeswang507605a2014-01-02 11:22:43 +08001694static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001695{
hayeswangebc2ec42013-08-14 20:54:38 +08001696 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001697 u8 speed;
1698
1699 speed = rtl8152_get_speed(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001700 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001701 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08001702 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001703 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1704 } else {
1705 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08001706 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001707 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1708 }
hayeswang507605a2014-01-02 11:22:43 +08001709}
1710
hayeswang00a5e362014-02-18 21:48:59 +08001711static void rxdy_gated_en(struct r8152 *tp, bool enable)
1712{
1713 u32 ocp_data;
1714
1715 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1716 if (enable)
1717 ocp_data |= RXDY_GATED_EN;
1718 else
1719 ocp_data &= ~RXDY_GATED_EN;
1720 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1721}
1722
hayeswang507605a2014-01-02 11:22:43 +08001723static int rtl_enable(struct r8152 *tp)
1724{
1725 u32 ocp_data;
1726 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001727
1728 r8152b_reset_packet_filter(tp);
1729
1730 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1731 ocp_data |= CR_RE | CR_TE;
1732 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1733
hayeswang00a5e362014-02-18 21:48:59 +08001734 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001735
hayeswangebc2ec42013-08-14 20:54:38 +08001736 INIT_LIST_HEAD(&tp->rx_done);
1737 ret = 0;
1738 for (i = 0; i < RTL8152_MAX_RX; i++) {
1739 INIT_LIST_HEAD(&tp->rx_info[i].list);
1740 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1741 }
hayeswangac718b62013-05-02 16:01:25 +00001742
hayeswangebc2ec42013-08-14 20:54:38 +08001743 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001744}
1745
hayeswang507605a2014-01-02 11:22:43 +08001746static int rtl8152_enable(struct r8152 *tp)
1747{
1748 set_tx_qlen(tp);
1749 rtl_set_eee_plus(tp);
1750
1751 return rtl_enable(tp);
1752}
1753
hayeswang43779f82014-01-02 11:25:10 +08001754static void r8153_set_rx_agg(struct r8152 *tp)
1755{
1756 u8 speed;
1757
1758 speed = rtl8152_get_speed(tp);
1759 if (speed & _1000bps) {
1760 if (tp->udev->speed == USB_SPEED_SUPER) {
1761 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1762 RX_THR_SUPPER);
1763 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1764 EARLY_AGG_SUPPER);
1765 } else {
1766 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1767 RX_THR_HIGH);
1768 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1769 EARLY_AGG_HIGH);
1770 }
1771 } else {
1772 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1773 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1774 EARLY_AGG_SLOW);
1775 }
1776}
1777
1778static int rtl8153_enable(struct r8152 *tp)
1779{
1780 set_tx_qlen(tp);
1781 rtl_set_eee_plus(tp);
1782 r8153_set_rx_agg(tp);
1783
1784 return rtl_enable(tp);
1785}
1786
hayeswangac718b62013-05-02 16:01:25 +00001787static void rtl8152_disable(struct r8152 *tp)
1788{
hayeswangebc2ec42013-08-14 20:54:38 +08001789 u32 ocp_data;
1790 int i;
hayeswangac718b62013-05-02 16:01:25 +00001791
1792 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1793 ocp_data &= ~RCR_ACPT_ALL;
1794 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1795
hayeswang00a5e362014-02-18 21:48:59 +08001796 rtl_drop_queued_tx(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001797
1798 for (i = 0; i < RTL8152_MAX_TX; i++)
1799 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001800
hayeswang00a5e362014-02-18 21:48:59 +08001801 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001802
1803 for (i = 0; i < 1000; i++) {
1804 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1805 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1806 break;
1807 mdelay(1);
1808 }
1809
1810 for (i = 0; i < 1000; i++) {
1811 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1812 break;
1813 mdelay(1);
1814 }
1815
hayeswangebc2ec42013-08-14 20:54:38 +08001816 for (i = 0; i < RTL8152_MAX_RX; i++)
1817 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001818
1819 rtl8152_nic_reset(tp);
1820}
1821
hayeswang00a5e362014-02-18 21:48:59 +08001822static void r8152_power_cut_en(struct r8152 *tp, bool enable)
1823{
1824 u32 ocp_data;
1825
1826 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1827 if (enable)
1828 ocp_data |= POWER_CUT;
1829 else
1830 ocp_data &= ~POWER_CUT;
1831 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1832
1833 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1834 ocp_data &= ~RESUME_INDICATE;
1835 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1836
1837}
1838
hayeswang21ff2e82014-02-18 21:49:06 +08001839#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1840
1841static u32 __rtl_get_wol(struct r8152 *tp)
1842{
1843 u32 ocp_data;
1844 u32 wolopts = 0;
1845
1846 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1847 if (!(ocp_data & LAN_WAKE_EN))
1848 return 0;
1849
1850 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1851 if (ocp_data & LINK_ON_WAKE_EN)
1852 wolopts |= WAKE_PHY;
1853
1854 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1855 if (ocp_data & UWF_EN)
1856 wolopts |= WAKE_UCAST;
1857 if (ocp_data & BWF_EN)
1858 wolopts |= WAKE_BCAST;
1859 if (ocp_data & MWF_EN)
1860 wolopts |= WAKE_MCAST;
1861
1862 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1863 if (ocp_data & MAGIC_EN)
1864 wolopts |= WAKE_MAGIC;
1865
1866 return wolopts;
1867}
1868
1869static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
1870{
1871 u32 ocp_data;
1872
1873 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1874
1875 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1876 ocp_data &= ~LINK_ON_WAKE_EN;
1877 if (wolopts & WAKE_PHY)
1878 ocp_data |= LINK_ON_WAKE_EN;
1879 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1880
1881 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
1882 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
1883 if (wolopts & WAKE_UCAST)
1884 ocp_data |= UWF_EN;
1885 if (wolopts & WAKE_BCAST)
1886 ocp_data |= BWF_EN;
1887 if (wolopts & WAKE_MCAST)
1888 ocp_data |= MWF_EN;
1889 if (wolopts & WAKE_ANY)
1890 ocp_data |= LAN_WAKE_EN;
1891 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
1892
1893 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1894
1895 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1896 ocp_data &= ~MAGIC_EN;
1897 if (wolopts & WAKE_MAGIC)
1898 ocp_data |= MAGIC_EN;
1899 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1900
1901 if (wolopts & WAKE_ANY)
1902 device_set_wakeup_enable(&tp->udev->dev, true);
1903 else
1904 device_set_wakeup_enable(&tp->udev->dev, false);
1905}
1906
hayeswang9a4be1b2014-02-18 21:49:07 +08001907static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
1908{
1909 if (enable) {
1910 u32 ocp_data;
1911
1912 __rtl_set_wol(tp, WAKE_ANY);
1913
1914 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1915
1916 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
1917 ocp_data |= LINK_OFF_WAKE_EN;
1918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
1919
1920 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1921 } else {
1922 __rtl_set_wol(tp, tp->saved_wolopts);
1923 }
1924}
1925
hayeswangaa66a5f2014-02-18 21:49:04 +08001926static void rtl_phy_reset(struct r8152 *tp)
1927{
1928 u16 data;
1929 int i;
1930
1931 clear_bit(PHY_RESET, &tp->flags);
1932
1933 data = r8152_mdio_read(tp, MII_BMCR);
1934
1935 /* don't reset again before the previous one complete */
1936 if (data & BMCR_RESET)
1937 return;
1938
1939 data |= BMCR_RESET;
1940 r8152_mdio_write(tp, MII_BMCR, data);
1941
1942 for (i = 0; i < 50; i++) {
1943 msleep(20);
1944 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
1945 break;
1946 }
1947}
1948
hayeswang43499682014-02-18 21:48:58 +08001949static void rtl_clear_bp(struct r8152 *tp)
1950{
1951 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1952 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1953 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1954 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1955 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1956 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1957 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1958 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1959 mdelay(3);
1960 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1961 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1962}
1963
1964static void r8153_clear_bp(struct r8152 *tp)
1965{
1966 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
1967 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
1968 rtl_clear_bp(tp);
1969}
1970
1971static void r8153_teredo_off(struct r8152 *tp)
1972{
1973 u32 ocp_data;
1974
1975 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1976 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1977 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1978
1979 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1980 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1981 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1982}
1983
1984static void r8152b_disable_aldps(struct r8152 *tp)
1985{
1986 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1987 msleep(20);
1988}
1989
1990static inline void r8152b_enable_aldps(struct r8152 *tp)
1991{
1992 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1993 LINKENA | DIS_SDSAVE);
1994}
1995
1996static void r8152b_hw_phy_cfg(struct r8152 *tp)
1997{
hayeswangf0cbe0a2014-02-18 21:49:03 +08001998 u16 data;
1999
2000 data = r8152_mdio_read(tp, MII_BMCR);
2001 if (data & BMCR_PDOWN) {
2002 data &= ~BMCR_PDOWN;
2003 r8152_mdio_write(tp, MII_BMCR, data);
2004 }
2005
hayeswang43499682014-02-18 21:48:58 +08002006 r8152b_disable_aldps(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002007
2008 rtl_clear_bp(tp);
2009
2010 r8152b_enable_aldps(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002011 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08002012}
2013
hayeswangac718b62013-05-02 16:01:25 +00002014static void r8152b_exit_oob(struct r8152 *tp)
2015{
2016 u32 ocp_data;
2017 int i;
2018
2019 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2020 ocp_data &= ~RCR_ACPT_ALL;
2021 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2022
hayeswang00a5e362014-02-18 21:48:59 +08002023 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08002024 r8153_teredo_off(tp);
hayeswang7e9da482014-02-18 21:49:05 +08002025 r8152b_hw_phy_cfg(tp);
hayeswangac718b62013-05-02 16:01:25 +00002026
2027 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2028 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2029
2030 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2031 ocp_data &= ~NOW_IS_OOB;
2032 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2033
2034 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2035 ocp_data &= ~MCU_BORW_EN;
2036 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2037
2038 for (i = 0; i < 1000; i++) {
2039 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2040 if (ocp_data & LINK_LIST_READY)
2041 break;
2042 mdelay(1);
2043 }
2044
2045 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2046 ocp_data |= RE_INIT_LL;
2047 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2048
2049 for (i = 0; i < 1000; i++) {
2050 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2051 if (ocp_data & LINK_LIST_READY)
2052 break;
2053 mdelay(1);
2054 }
2055
2056 rtl8152_nic_reset(tp);
2057
2058 /* rx share fifo credit full threshold */
2059 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2060
2061 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
2062 ocp_data &= STAT_SPEED_MASK;
2063 if (ocp_data == STAT_SPEED_FULL) {
2064 /* rx share fifo credit near full threshold */
2065 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2066 RXFIFO_THR2_FULL);
2067 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2068 RXFIFO_THR3_FULL);
2069 } else {
2070 /* rx share fifo credit near full threshold */
2071 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2072 RXFIFO_THR2_HIGH);
2073 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2074 RXFIFO_THR3_HIGH);
2075 }
2076
2077 /* TX share fifo free credit full threshold */
2078 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2079
2080 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08002081 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00002082 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2083 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2084
2085 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2086 ocp_data &= ~CPCR_RX_VLAN;
2087 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2088
2089 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2090
2091 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2092 ocp_data |= TCR0_AUTO_FIFO;
2093 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2094}
2095
2096static void r8152b_enter_oob(struct r8152 *tp)
2097{
hayeswang45f4a192014-01-06 17:08:41 +08002098 u32 ocp_data;
2099 int i;
hayeswangac718b62013-05-02 16:01:25 +00002100
2101 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2102 ocp_data &= ~NOW_IS_OOB;
2103 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2104
2105 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2106 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2107 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2108
2109 rtl8152_disable(tp);
2110
2111 for (i = 0; i < 1000; i++) {
2112 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2113 if (ocp_data & LINK_LIST_READY)
2114 break;
2115 mdelay(1);
2116 }
2117
2118 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2119 ocp_data |= RE_INIT_LL;
2120 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2121
2122 for (i = 0; i < 1000; i++) {
2123 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2124 if (ocp_data & LINK_LIST_READY)
2125 break;
2126 mdelay(1);
2127 }
2128
2129 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2130
hayeswangac718b62013-05-02 16:01:25 +00002131 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2132 ocp_data |= CPCR_RX_VLAN;
2133 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2134
2135 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2136 ocp_data |= ALDPS_PROXY_MODE;
2137 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2138
2139 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2140 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2141 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2142
hayeswang00a5e362014-02-18 21:48:59 +08002143 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002144
2145 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2146 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2147 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2148}
2149
hayeswang43779f82014-01-02 11:25:10 +08002150static void r8153_hw_phy_cfg(struct r8152 *tp)
2151{
2152 u32 ocp_data;
2153 u16 data;
2154
2155 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
hayeswangf0cbe0a2014-02-18 21:49:03 +08002156 data = r8152_mdio_read(tp, MII_BMCR);
2157 if (data & BMCR_PDOWN) {
2158 data &= ~BMCR_PDOWN;
2159 r8152_mdio_write(tp, MII_BMCR, data);
2160 }
hayeswang43779f82014-01-02 11:25:10 +08002161
hayeswang7e9da482014-02-18 21:49:05 +08002162 r8153_clear_bp(tp);
2163
hayeswang43779f82014-01-02 11:25:10 +08002164 if (tp->version == RTL_VER_03) {
2165 data = ocp_reg_read(tp, OCP_EEE_CFG);
2166 data &= ~CTAP_SHORT_EN;
2167 ocp_reg_write(tp, OCP_EEE_CFG, data);
2168 }
2169
2170 data = ocp_reg_read(tp, OCP_POWER_CFG);
2171 data |= EEE_CLKDIV_EN;
2172 ocp_reg_write(tp, OCP_POWER_CFG, data);
2173
2174 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2175 data |= EN_10M_BGOFF;
2176 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2177 data = ocp_reg_read(tp, OCP_POWER_CFG);
2178 data |= EN_10M_PLLOFF;
2179 ocp_reg_write(tp, OCP_POWER_CFG, data);
2180 data = sram_read(tp, SRAM_IMPEDANCE);
2181 data &= ~RX_DRIVING_MASK;
2182 sram_write(tp, SRAM_IMPEDANCE, data);
2183
2184 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2185 ocp_data |= PFM_PWM_SWITCH;
2186 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2187
2188 data = sram_read(tp, SRAM_LPF_CFG);
2189 data |= LPF_AUTO_TUNE;
2190 sram_write(tp, SRAM_LPF_CFG, data);
2191
2192 data = sram_read(tp, SRAM_10M_AMP1);
2193 data |= GDAC_IB_UPALL;
2194 sram_write(tp, SRAM_10M_AMP1, data);
2195 data = sram_read(tp, SRAM_10M_AMP2);
2196 data |= AMP_DN;
2197 sram_write(tp, SRAM_10M_AMP2, data);
hayeswangaa66a5f2014-02-18 21:49:04 +08002198
2199 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08002200}
2201
hayeswangb9702722014-02-18 21:49:00 +08002202static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002203{
2204 u8 u1u2[8];
2205
2206 if (enable)
2207 memset(u1u2, 0xff, sizeof(u1u2));
2208 else
2209 memset(u1u2, 0x00, sizeof(u1u2));
2210
2211 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2212}
2213
hayeswangb9702722014-02-18 21:49:00 +08002214static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002215{
2216 u32 ocp_data;
2217
2218 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2219 if (enable)
2220 ocp_data |= U2P3_ENABLE;
2221 else
2222 ocp_data &= ~U2P3_ENABLE;
2223 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2224}
2225
hayeswangb9702722014-02-18 21:49:00 +08002226static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002227{
2228 u32 ocp_data;
2229
2230 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2231 if (enable)
2232 ocp_data |= PWR_EN | PHASE2_EN;
2233 else
2234 ocp_data &= ~(PWR_EN | PHASE2_EN);
2235 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2236
2237 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2238 ocp_data &= ~PCUT_STATUS;
2239 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2240}
2241
hayeswang43779f82014-01-02 11:25:10 +08002242static void r8153_first_init(struct r8152 *tp)
2243{
2244 u32 ocp_data;
2245 int i;
2246
hayeswang00a5e362014-02-18 21:48:59 +08002247 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002248 r8153_teredo_off(tp);
2249
2250 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2251 ocp_data &= ~RCR_ACPT_ALL;
2252 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2253
2254 r8153_hw_phy_cfg(tp);
2255
2256 rtl8152_nic_reset(tp);
2257
2258 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2259 ocp_data &= ~NOW_IS_OOB;
2260 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2261
2262 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2263 ocp_data &= ~MCU_BORW_EN;
2264 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2265
2266 for (i = 0; i < 1000; i++) {
2267 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2268 if (ocp_data & LINK_LIST_READY)
2269 break;
2270 mdelay(1);
2271 }
2272
2273 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2274 ocp_data |= RE_INIT_LL;
2275 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2276
2277 for (i = 0; i < 1000; i++) {
2278 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2279 if (ocp_data & LINK_LIST_READY)
2280 break;
2281 mdelay(1);
2282 }
2283
2284 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2285 ocp_data &= ~CPCR_RX_VLAN;
2286 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2287
2288 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2289
2290 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2291 ocp_data |= TCR0_AUTO_FIFO;
2292 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2293
2294 rtl8152_nic_reset(tp);
2295
2296 /* rx share fifo credit full threshold */
2297 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2298 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2299 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2300 /* TX share fifo free credit full threshold */
2301 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2302
hayeswang9629e3c2014-01-15 10:42:15 +08002303 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002304 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2305 ocp_data &= ~RX_AGG_DISABLE;
2306 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2307}
2308
2309static void r8153_enter_oob(struct r8152 *tp)
2310{
2311 u32 ocp_data;
2312 int i;
2313
2314 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2315 ocp_data &= ~NOW_IS_OOB;
2316 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2317
2318 rtl8152_disable(tp);
2319
2320 for (i = 0; i < 1000; i++) {
2321 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2322 if (ocp_data & LINK_LIST_READY)
2323 break;
2324 mdelay(1);
2325 }
2326
2327 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2328 ocp_data |= RE_INIT_LL;
2329 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2330
2331 for (i = 0; i < 1000; i++) {
2332 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2333 if (ocp_data & LINK_LIST_READY)
2334 break;
2335 mdelay(1);
2336 }
2337
2338 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2339
hayeswang43779f82014-01-02 11:25:10 +08002340 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2341 ocp_data &= ~TEREDO_WAKE_MASK;
2342 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2343
2344 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2345 ocp_data |= CPCR_RX_VLAN;
2346 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2347
2348 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2349 ocp_data |= ALDPS_PROXY_MODE;
2350 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2351
2352 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2353 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2354 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2355
hayeswang00a5e362014-02-18 21:48:59 +08002356 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002357
2358 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2359 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2360 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2361}
2362
2363static void r8153_disable_aldps(struct r8152 *tp)
2364{
2365 u16 data;
2366
2367 data = ocp_reg_read(tp, OCP_POWER_CFG);
2368 data &= ~EN_ALDPS;
2369 ocp_reg_write(tp, OCP_POWER_CFG, data);
2370 msleep(20);
2371}
2372
2373static void r8153_enable_aldps(struct r8152 *tp)
2374{
2375 u16 data;
2376
2377 data = ocp_reg_read(tp, OCP_POWER_CFG);
2378 data |= EN_ALDPS;
2379 ocp_reg_write(tp, OCP_POWER_CFG, data);
2380}
2381
hayeswangac718b62013-05-02 16:01:25 +00002382static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2383{
hayeswang43779f82014-01-02 11:25:10 +08002384 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002385 int ret = 0;
2386
2387 cancel_delayed_work_sync(&tp->schedule);
2388 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2389 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2390 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002391 if (tp->mii.supports_gmii) {
2392 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2393 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2394 } else {
2395 gbcr = 0;
2396 }
hayeswangac718b62013-05-02 16:01:25 +00002397
2398 if (autoneg == AUTONEG_DISABLE) {
2399 if (speed == SPEED_10) {
2400 bmcr = 0;
2401 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2402 } else if (speed == SPEED_100) {
2403 bmcr = BMCR_SPEED100;
2404 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002405 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2406 bmcr = BMCR_SPEED1000;
2407 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002408 } else {
2409 ret = -EINVAL;
2410 goto out;
2411 }
2412
2413 if (duplex == DUPLEX_FULL)
2414 bmcr |= BMCR_FULLDPLX;
2415 } else {
2416 if (speed == SPEED_10) {
2417 if (duplex == DUPLEX_FULL)
2418 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2419 else
2420 anar |= ADVERTISE_10HALF;
2421 } else if (speed == SPEED_100) {
2422 if (duplex == DUPLEX_FULL) {
2423 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2424 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2425 } else {
2426 anar |= ADVERTISE_10HALF;
2427 anar |= ADVERTISE_100HALF;
2428 }
hayeswang43779f82014-01-02 11:25:10 +08002429 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2430 if (duplex == DUPLEX_FULL) {
2431 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2432 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2433 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2434 } else {
2435 anar |= ADVERTISE_10HALF;
2436 anar |= ADVERTISE_100HALF;
2437 gbcr |= ADVERTISE_1000HALF;
2438 }
hayeswangac718b62013-05-02 16:01:25 +00002439 } else {
2440 ret = -EINVAL;
2441 goto out;
2442 }
2443
2444 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2445 }
2446
hayeswangaa66a5f2014-02-18 21:49:04 +08002447 if (test_bit(PHY_RESET, &tp->flags))
2448 bmcr |= BMCR_RESET;
2449
hayeswang43779f82014-01-02 11:25:10 +08002450 if (tp->mii.supports_gmii)
2451 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2452
hayeswangac718b62013-05-02 16:01:25 +00002453 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2454 r8152_mdio_write(tp, MII_BMCR, bmcr);
2455
hayeswangaa66a5f2014-02-18 21:49:04 +08002456 if (test_bit(PHY_RESET, &tp->flags)) {
2457 int i;
2458
2459 clear_bit(PHY_RESET, &tp->flags);
2460 for (i = 0; i < 50; i++) {
2461 msleep(20);
2462 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2463 break;
2464 }
2465 }
2466
hayeswangac718b62013-05-02 16:01:25 +00002467out:
hayeswangac718b62013-05-02 16:01:25 +00002468
2469 return ret;
2470}
2471
2472static void rtl8152_down(struct r8152 *tp)
2473{
hayeswang00a5e362014-02-18 21:48:59 +08002474 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002475 r8152b_disable_aldps(tp);
2476 r8152b_enter_oob(tp);
2477 r8152b_enable_aldps(tp);
2478}
2479
hayeswang43779f82014-01-02 11:25:10 +08002480static void rtl8153_down(struct r8152 *tp)
2481{
hayeswangb9702722014-02-18 21:49:00 +08002482 r8153_u1u2en(tp, false);
2483 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002484 r8153_disable_aldps(tp);
2485 r8153_enter_oob(tp);
2486 r8153_enable_aldps(tp);
2487}
2488
hayeswangac718b62013-05-02 16:01:25 +00002489static void set_carrier(struct r8152 *tp)
2490{
2491 struct net_device *netdev = tp->netdev;
2492 u8 speed;
2493
hayeswang40a82912013-08-14 20:54:40 +08002494 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002495 speed = rtl8152_get_speed(tp);
2496
2497 if (speed & LINK_STATUS) {
2498 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002499 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002500 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2501 netif_carrier_on(netdev);
2502 }
2503 } else {
2504 if (tp->speed & LINK_STATUS) {
2505 netif_carrier_off(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08002506 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002507 tp->rtl_ops.disable(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002508 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002509 }
2510 }
2511 tp->speed = speed;
2512}
2513
2514static void rtl_work_func_t(struct work_struct *work)
2515{
2516 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2517
hayeswang9a4be1b2014-02-18 21:49:07 +08002518 if (usb_autopm_get_interface(tp->intf) < 0)
2519 return;
2520
hayeswangac718b62013-05-02 16:01:25 +00002521 if (!test_bit(WORK_ENABLE, &tp->flags))
2522 goto out1;
2523
2524 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2525 goto out1;
2526
hayeswang40a82912013-08-14 20:54:40 +08002527 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2528 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002529
2530 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2531 _rtl8152_set_rx_mode(tp->netdev);
2532
hayeswang9a4be1b2014-02-18 21:49:07 +08002533 if (tp->speed & LINK_STATUS)
2534 tx_bottom(tp);
hayeswangaa66a5f2014-02-18 21:49:04 +08002535
2536 if (test_bit(PHY_RESET, &tp->flags))
2537 rtl_phy_reset(tp);
2538
hayeswangac718b62013-05-02 16:01:25 +00002539out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08002540 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002541}
2542
2543static int rtl8152_open(struct net_device *netdev)
2544{
2545 struct r8152 *tp = netdev_priv(netdev);
2546 int res = 0;
2547
hayeswang7e9da482014-02-18 21:49:05 +08002548 res = alloc_all_mem(tp);
2549 if (res)
2550 goto out;
2551
hayeswang9a4be1b2014-02-18 21:49:07 +08002552 res = usb_autopm_get_interface(tp->intf);
2553 if (res < 0) {
2554 free_all_mem(tp);
2555 goto out;
2556 }
2557
2558 /* The WORK_ENABLE may be set when autoresume occurs */
2559 if (test_bit(WORK_ENABLE, &tp->flags)) {
2560 clear_bit(WORK_ENABLE, &tp->flags);
2561 usb_kill_urb(tp->intr_urb);
2562 cancel_delayed_work_sync(&tp->schedule);
2563 if (tp->speed & LINK_STATUS)
2564 tp->rtl_ops.disable(tp);
2565 }
2566
hayeswang7e9da482014-02-18 21:49:05 +08002567 tp->rtl_ops.up(tp);
2568
hayeswang43779f82014-01-02 11:25:10 +08002569 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2570 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2571 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002572 tp->speed = 0;
2573 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002574 netif_start_queue(netdev);
2575 set_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002576 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2577 if (res) {
2578 if (res == -ENODEV)
2579 netif_device_detach(tp->netdev);
2580 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2581 res);
hayeswang7e9da482014-02-18 21:49:05 +08002582 free_all_mem(tp);
hayeswang3d55f442014-02-06 11:55:48 +08002583 }
2584
hayeswang9a4be1b2014-02-18 21:49:07 +08002585 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002586
hayeswang7e9da482014-02-18 21:49:05 +08002587out:
hayeswangac718b62013-05-02 16:01:25 +00002588 return res;
2589}
2590
2591static int rtl8152_close(struct net_device *netdev)
2592{
2593 struct r8152 *tp = netdev_priv(netdev);
2594 int res = 0;
2595
2596 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002597 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002598 cancel_delayed_work_sync(&tp->schedule);
2599 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002600
2601 res = usb_autopm_get_interface(tp->intf);
2602 if (res < 0) {
2603 rtl_drop_queued_tx(tp);
2604 } else {
2605 /*
2606 * The autosuspend may have been enabled and wouldn't
2607 * be disable when autoresume occurs, because the
2608 * netif_running() would be false.
2609 */
2610 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2611 rtl_runtime_suspend_enable(tp, false);
2612 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2613 }
2614
2615 tasklet_disable(&tp->tl);
2616 tp->rtl_ops.down(tp);
2617 tasklet_enable(&tp->tl);
2618 usb_autopm_put_interface(tp->intf);
2619 }
hayeswangac718b62013-05-02 16:01:25 +00002620
hayeswang7e9da482014-02-18 21:49:05 +08002621 free_all_mem(tp);
2622
hayeswangac718b62013-05-02 16:01:25 +00002623 return res;
2624}
2625
hayeswangac718b62013-05-02 16:01:25 +00002626static void r8152b_enable_eee(struct r8152 *tp)
2627{
hayeswang45f4a192014-01-06 17:08:41 +08002628 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002629
2630 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2631 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2632 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2633 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2634 EEE_10_CAP | EEE_NWAY_EN |
2635 TX_QUIET_EN | RX_QUIET_EN |
2636 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2637 SDFALLTIME);
2638 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2639 RG_LDVQUIET_EN | RG_CKRSEL |
2640 RG_EEEPRG_EN);
2641 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2642 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2643 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2644 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2645 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2646 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2647}
2648
hayeswang43779f82014-01-02 11:25:10 +08002649static void r8153_enable_eee(struct r8152 *tp)
2650{
2651 u32 ocp_data;
2652 u16 data;
2653
2654 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2655 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2656 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2657 data = ocp_reg_read(tp, OCP_EEE_CFG);
2658 data |= EEE10_EN;
2659 ocp_reg_write(tp, OCP_EEE_CFG, data);
2660 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2661 data |= MY1000_EEE | MY100_EEE;
2662 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2663}
2664
hayeswangac718b62013-05-02 16:01:25 +00002665static void r8152b_enable_fc(struct r8152 *tp)
2666{
2667 u16 anar;
2668
2669 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2670 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2671 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2672}
2673
hayeswangac718b62013-05-02 16:01:25 +00002674static void r8152b_init(struct r8152 *tp)
2675{
hayeswangebc2ec42013-08-14 20:54:38 +08002676 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002677
hayeswangac718b62013-05-02 16:01:25 +00002678 if (tp->version == RTL_VER_01) {
2679 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2680 ocp_data &= ~LED_MODE_MASK;
2681 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2682 }
2683
hayeswang00a5e362014-02-18 21:48:59 +08002684 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002685
hayeswangac718b62013-05-02 16:01:25 +00002686 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2687 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2688 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2689 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2690 ocp_data &= ~MCU_CLK_RATIO_MASK;
2691 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2692 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2693 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2694 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2695 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2696
2697 r8152b_enable_eee(tp);
2698 r8152b_enable_aldps(tp);
2699 r8152b_enable_fc(tp);
2700
hayeswangebc2ec42013-08-14 20:54:38 +08002701 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002702 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec42013-08-14 20:54:38 +08002703 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002704 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2705}
2706
hayeswang43779f82014-01-02 11:25:10 +08002707static void r8153_init(struct r8152 *tp)
2708{
2709 u32 ocp_data;
2710 int i;
2711
hayeswangb9702722014-02-18 21:49:00 +08002712 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002713
2714 for (i = 0; i < 500; i++) {
2715 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2716 AUTOLOAD_DONE)
2717 break;
2718 msleep(20);
2719 }
2720
2721 for (i = 0; i < 500; i++) {
2722 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2723 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2724 break;
2725 msleep(20);
2726 }
2727
hayeswangb9702722014-02-18 21:49:00 +08002728 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002729
2730 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2731 ocp_data &= ~TIMER11_EN;
2732 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2733
hayeswang43779f82014-01-02 11:25:10 +08002734 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2735 ocp_data &= ~LED_MODE_MASK;
2736 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2737
2738 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2739 ocp_data &= ~LPM_TIMER_MASK;
2740 if (tp->udev->speed == USB_SPEED_SUPER)
2741 ocp_data |= LPM_TIMER_500US;
2742 else
2743 ocp_data |= LPM_TIMER_500MS;
2744 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2745
2746 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2747 ocp_data &= ~SEN_VAL_MASK;
2748 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2749 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2750
hayeswangb9702722014-02-18 21:49:00 +08002751 r8153_power_cut_en(tp, false);
2752 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002753
hayeswang43779f82014-01-02 11:25:10 +08002754 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2755 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2756 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2757 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2758 U1U2_SPDWN_EN | L1_SPDWN_EN);
2759 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2760 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2761 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2762 EEE_SPDWN_EN);
2763
2764 r8153_enable_eee(tp);
2765 r8153_enable_aldps(tp);
2766 r8152b_enable_fc(tp);
hayeswang43779f82014-01-02 11:25:10 +08002767}
2768
hayeswangac718b62013-05-02 16:01:25 +00002769static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2770{
2771 struct r8152 *tp = usb_get_intfdata(intf);
2772
hayeswang9a4be1b2014-02-18 21:49:07 +08002773 if (PMSG_IS_AUTO(message))
2774 set_bit(SELECTIVE_SUSPEND, &tp->flags);
2775 else
2776 netif_device_detach(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002777
2778 if (netif_running(tp->netdev)) {
2779 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002780 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002781 cancel_delayed_work_sync(&tp->schedule);
hayeswang9a4be1b2014-02-18 21:49:07 +08002782 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2783 rtl_runtime_suspend_enable(tp, true);
2784 } else {
2785 tasklet_disable(&tp->tl);
2786 tp->rtl_ops.down(tp);
2787 tasklet_enable(&tp->tl);
2788 }
hayeswangac718b62013-05-02 16:01:25 +00002789 }
2790
hayeswangac718b62013-05-02 16:01:25 +00002791 return 0;
2792}
2793
2794static int rtl8152_resume(struct usb_interface *intf)
2795{
2796 struct r8152 *tp = usb_get_intfdata(intf);
2797
hayeswang9a4be1b2014-02-18 21:49:07 +08002798 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2799 tp->rtl_ops.init(tp);
2800 netif_device_attach(tp->netdev);
2801 }
2802
hayeswangac718b62013-05-02 16:01:25 +00002803 if (netif_running(tp->netdev)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08002804 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2805 rtl_runtime_suspend_enable(tp, false);
2806 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2807 if (tp->speed & LINK_STATUS)
2808 tp->rtl_ops.disable(tp);
2809 } else {
2810 tp->rtl_ops.up(tp);
2811 rtl8152_set_speed(tp, AUTONEG_ENABLE,
hayeswang43779f82014-01-02 11:25:10 +08002812 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2813 DUPLEX_FULL);
hayeswang9a4be1b2014-02-18 21:49:07 +08002814 }
hayeswang40a82912013-08-14 20:54:40 +08002815 tp->speed = 0;
2816 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002817 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002818 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangac718b62013-05-02 16:01:25 +00002819 }
2820
2821 return 0;
2822}
2823
hayeswang21ff2e82014-02-18 21:49:06 +08002824static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2825{
2826 struct r8152 *tp = netdev_priv(dev);
2827
hayeswang9a4be1b2014-02-18 21:49:07 +08002828 if (usb_autopm_get_interface(tp->intf) < 0)
2829 return;
2830
hayeswang21ff2e82014-02-18 21:49:06 +08002831 wol->supported = WAKE_ANY;
2832 wol->wolopts = __rtl_get_wol(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08002833
2834 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08002835}
2836
2837static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2838{
2839 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002840 int ret;
2841
2842 ret = usb_autopm_get_interface(tp->intf);
2843 if (ret < 0)
2844 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08002845
2846 __rtl_set_wol(tp, wol->wolopts);
2847 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
2848
hayeswang9a4be1b2014-02-18 21:49:07 +08002849 usb_autopm_put_interface(tp->intf);
2850
2851out_set_wol:
2852 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08002853}
2854
hayeswangac718b62013-05-02 16:01:25 +00002855static void rtl8152_get_drvinfo(struct net_device *netdev,
2856 struct ethtool_drvinfo *info)
2857{
2858 struct r8152 *tp = netdev_priv(netdev);
2859
2860 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2861 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2862 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2863}
2864
2865static
2866int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2867{
2868 struct r8152 *tp = netdev_priv(netdev);
2869
2870 if (!tp->mii.mdio_read)
2871 return -EOPNOTSUPP;
2872
2873 return mii_ethtool_gset(&tp->mii, cmd);
2874}
2875
2876static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2877{
2878 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002879 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002880
hayeswang9a4be1b2014-02-18 21:49:07 +08002881 ret = usb_autopm_get_interface(tp->intf);
2882 if (ret < 0)
2883 goto out;
2884
2885 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2886
2887 usb_autopm_put_interface(tp->intf);
2888
2889out:
2890 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002891}
2892
2893static struct ethtool_ops ops = {
2894 .get_drvinfo = rtl8152_get_drvinfo,
2895 .get_settings = rtl8152_get_settings,
2896 .set_settings = rtl8152_set_settings,
2897 .get_link = ethtool_op_get_link,
hayeswang21ff2e82014-02-18 21:49:06 +08002898 .get_wol = rtl8152_get_wol,
2899 .set_wol = rtl8152_set_wol,
hayeswangac718b62013-05-02 16:01:25 +00002900};
2901
2902static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2903{
2904 struct r8152 *tp = netdev_priv(netdev);
2905 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08002906 int res;
2907
2908 res = usb_autopm_get_interface(tp->intf);
2909 if (res < 0)
2910 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002911
2912 switch (cmd) {
2913 case SIOCGMIIPHY:
2914 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2915 break;
2916
2917 case SIOCGMIIREG:
2918 data->val_out = r8152_mdio_read(tp, data->reg_num);
2919 break;
2920
2921 case SIOCSMIIREG:
2922 if (!capable(CAP_NET_ADMIN)) {
2923 res = -EPERM;
2924 break;
2925 }
2926 r8152_mdio_write(tp, data->reg_num, data->val_in);
2927 break;
2928
2929 default:
2930 res = -EOPNOTSUPP;
2931 }
2932
hayeswang9a4be1b2014-02-18 21:49:07 +08002933 usb_autopm_put_interface(tp->intf);
2934
2935out:
hayeswangac718b62013-05-02 16:01:25 +00002936 return res;
2937}
2938
2939static const struct net_device_ops rtl8152_netdev_ops = {
2940 .ndo_open = rtl8152_open,
2941 .ndo_stop = rtl8152_close,
2942 .ndo_do_ioctl = rtl8152_ioctl,
2943 .ndo_start_xmit = rtl8152_start_xmit,
2944 .ndo_tx_timeout = rtl8152_tx_timeout,
2945 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2946 .ndo_set_mac_address = rtl8152_set_mac_address,
2947
2948 .ndo_change_mtu = eth_change_mtu,
2949 .ndo_validate_addr = eth_validate_addr,
2950};
2951
2952static void r8152b_get_version(struct r8152 *tp)
2953{
2954 u32 ocp_data;
2955 u16 version;
2956
2957 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2958 version = (u16)(ocp_data & VERSION_MASK);
2959
2960 switch (version) {
2961 case 0x4c00:
2962 tp->version = RTL_VER_01;
2963 break;
2964 case 0x4c10:
2965 tp->version = RTL_VER_02;
2966 break;
hayeswang43779f82014-01-02 11:25:10 +08002967 case 0x5c00:
2968 tp->version = RTL_VER_03;
2969 tp->mii.supports_gmii = 1;
2970 break;
2971 case 0x5c10:
2972 tp->version = RTL_VER_04;
2973 tp->mii.supports_gmii = 1;
2974 break;
2975 case 0x5c20:
2976 tp->version = RTL_VER_05;
2977 tp->mii.supports_gmii = 1;
2978 break;
hayeswangac718b62013-05-02 16:01:25 +00002979 default:
2980 netif_info(tp, probe, tp->netdev,
2981 "Unknown version 0x%04x\n", version);
2982 break;
2983 }
2984}
2985
hayeswange3fe0b12014-01-02 11:22:39 +08002986static void rtl8152_unload(struct r8152 *tp)
2987{
hayeswang00a5e362014-02-18 21:48:59 +08002988 if (tp->version != RTL_VER_01)
2989 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08002990}
2991
hayeswang43779f82014-01-02 11:25:10 +08002992static void rtl8153_unload(struct r8152 *tp)
2993{
hayeswangb9702722014-02-18 21:49:00 +08002994 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002995}
2996
hayeswang31ca1de2014-01-06 17:08:43 +08002997static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08002998{
2999 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08003000 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08003001
3002 switch (id->idVendor) {
3003 case VENDOR_ID_REALTEK:
3004 switch (id->idProduct) {
3005 case PRODUCT_ID_RTL8152:
3006 ops->init = r8152b_init;
3007 ops->enable = rtl8152_enable;
3008 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003009 ops->up = r8152b_exit_oob;
hayeswangc81229c2014-01-02 11:22:42 +08003010 ops->down = rtl8152_down;
3011 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003012 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08003013 break;
hayeswang43779f82014-01-02 11:25:10 +08003014 case PRODUCT_ID_RTL8153:
3015 ops->init = r8153_init;
3016 ops->enable = rtl8153_enable;
3017 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003018 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003019 ops->down = rtl8153_down;
3020 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003021 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003022 break;
3023 default:
hayeswang43779f82014-01-02 11:25:10 +08003024 break;
3025 }
3026 break;
3027
3028 case VENDOR_ID_SAMSUNG:
3029 switch (id->idProduct) {
3030 case PRODUCT_ID_SAMSUNG:
3031 ops->init = r8153_init;
3032 ops->enable = rtl8153_enable;
3033 ops->disable = rtl8152_disable;
hayeswang7e9da482014-02-18 21:49:05 +08003034 ops->up = r8153_first_init;
hayeswang43779f82014-01-02 11:25:10 +08003035 ops->down = rtl8153_down;
3036 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08003037 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08003038 break;
hayeswangc81229c2014-01-02 11:22:42 +08003039 default:
hayeswangc81229c2014-01-02 11:22:42 +08003040 break;
3041 }
3042 break;
3043
3044 default:
hayeswangc81229c2014-01-02 11:22:42 +08003045 break;
3046 }
3047
hayeswang31ca1de2014-01-06 17:08:43 +08003048 if (ret)
3049 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
3050
hayeswangc81229c2014-01-02 11:22:42 +08003051 return ret;
3052}
3053
hayeswangac718b62013-05-02 16:01:25 +00003054static int rtl8152_probe(struct usb_interface *intf,
3055 const struct usb_device_id *id)
3056{
3057 struct usb_device *udev = interface_to_usbdev(intf);
3058 struct r8152 *tp;
3059 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08003060 int ret;
hayeswangac718b62013-05-02 16:01:25 +00003061
hayeswangac718b62013-05-02 16:01:25 +00003062 netdev = alloc_etherdev(sizeof(struct r8152));
3063 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003064 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00003065 return -ENOMEM;
3066 }
3067
hayeswangebc2ec42013-08-14 20:54:38 +08003068 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00003069 tp = netdev_priv(netdev);
3070 tp->msg_enable = 0x7FFF;
3071
hayeswange3ad4122014-01-06 17:08:42 +08003072 tp->udev = udev;
3073 tp->netdev = netdev;
3074 tp->intf = intf;
3075
hayeswang31ca1de2014-01-06 17:08:43 +08003076 ret = rtl_ops_init(tp, id);
3077 if (ret)
3078 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08003079
hayeswangebc2ec42013-08-14 20:54:38 +08003080 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00003081 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3082
hayeswangac718b62013-05-02 16:01:25 +00003083 netdev->netdev_ops = &rtl8152_netdev_ops;
3084 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08003085
3086 netdev->features |= NETIF_F_IP_CSUM;
3087 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00003088 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00003089
3090 tp->mii.dev = netdev;
3091 tp->mii.mdio_read = read_mii_word;
3092 tp->mii.mdio_write = write_mii_word;
3093 tp->mii.phy_id_mask = 0x3f;
3094 tp->mii.reg_num_mask = 0x1f;
3095 tp->mii.phy_id = R8152_PHY_ID;
3096 tp->mii.supports_gmii = 0;
3097
hayeswang9a4be1b2014-02-18 21:49:07 +08003098 intf->needs_remote_wakeup = 1;
3099
hayeswangac718b62013-05-02 16:01:25 +00003100 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08003101 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00003102 set_ethernet_addr(tp);
3103
hayeswangac718b62013-05-02 16:01:25 +00003104 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00003105
hayeswangebc2ec42013-08-14 20:54:38 +08003106 ret = register_netdev(netdev);
3107 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08003108 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec42013-08-14 20:54:38 +08003109 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00003110 }
3111
hayeswang21ff2e82014-02-18 21:49:06 +08003112 tp->saved_wolopts = __rtl_get_wol(tp);
3113 if (tp->saved_wolopts)
3114 device_set_wakeup_enable(&udev->dev, true);
3115 else
3116 device_set_wakeup_enable(&udev->dev, false);
3117
Hayes Wang4a8deae2014-01-07 11:18:22 +08003118 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00003119
3120 return 0;
3121
hayeswangac718b62013-05-02 16:01:25 +00003122out1:
hayeswangebc2ec42013-08-14 20:54:38 +08003123 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00003124out:
3125 free_netdev(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08003126 return ret;
hayeswangac718b62013-05-02 16:01:25 +00003127}
3128
hayeswangac718b62013-05-02 16:01:25 +00003129static void rtl8152_disconnect(struct usb_interface *intf)
3130{
3131 struct r8152 *tp = usb_get_intfdata(intf);
3132
3133 usb_set_intfdata(intf, NULL);
3134 if (tp) {
3135 set_bit(RTL8152_UNPLUG, &tp->flags);
3136 tasklet_kill(&tp->tl);
3137 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08003138 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00003139 free_netdev(tp->netdev);
3140 }
3141}
3142
3143/* table of devices that work with this driver */
3144static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08003145 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
3146 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
3147 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00003148 {}
3149};
3150
3151MODULE_DEVICE_TABLE(usb, rtl8152_table);
3152
3153static struct usb_driver rtl8152_driver = {
3154 .name = MODULENAME,
hayeswangebc2ec42013-08-14 20:54:38 +08003155 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00003156 .probe = rtl8152_probe,
3157 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00003158 .suspend = rtl8152_suspend,
hayeswangebc2ec42013-08-14 20:54:38 +08003159 .resume = rtl8152_resume,
3160 .reset_resume = rtl8152_resume,
hayeswang9a4be1b2014-02-18 21:49:07 +08003161 .supports_autosuspend = 1,
hayeswangac718b62013-05-02 16:01:25 +00003162};
3163
Sachin Kamatb4236daa2013-05-16 17:48:08 +00003164module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00003165
3166MODULE_AUTHOR(DRIVER_AUTHOR);
3167MODULE_DESCRIPTION(DRIVER_DESC);
3168MODULE_LICENSE("GPL");