blob: bf7d549ab51114334edad8051d27b3e87381dca8 [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
2 * Copyright (c) 2013 Realtek Semiconductor Corp. All rights reserved.
3 *
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
10#include <linux/init.h>
11#include <linux/signal.h>
12#include <linux/slab.h>
13#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000014#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/mii.h>
17#include <linux/ethtool.h>
18#include <linux/usb.h>
19#include <linux/crc32.h>
20#include <linux/if_vlan.h>
21#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080022#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080023#include <linux/ip.h>
24#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000025
26/* Version Information */
hayeswang43779f82014-01-02 11:25:10 +080027#define DRIVER_VERSION "v1.03.0 (2013/12/26)"
hayeswangac718b62013-05-02 16:01:25 +000028#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
29#define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
30#define MODULENAME "r8152"
31
32#define R8152_PHY_ID 32
33
34#define PLA_IDR 0xc000
35#define PLA_RCR 0xc010
36#define PLA_RMS 0xc016
37#define PLA_RXFIFO_CTRL0 0xc0a0
38#define PLA_RXFIFO_CTRL1 0xc0a4
39#define PLA_RXFIFO_CTRL2 0xc0a8
40#define PLA_FMC 0xc0b4
41#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080042#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000043#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080044#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000045#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080046#define PLA_TEREDO_TIMER 0xd2cc
47#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000048#define PLA_LEDSEL 0xdd90
49#define PLA_LED_FEATURE 0xdd92
50#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080051#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_GPHY_INTR_IMR 0xe022
53#define PLA_EEE_CR 0xe040
54#define PLA_EEEP_CR 0xe080
55#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080056#define PLA_MAC_PWR_CTRL2 0xe0ca
57#define PLA_MAC_PWR_CTRL3 0xe0cc
58#define PLA_MAC_PWR_CTRL4 0xe0ce
59#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000060#define PLA_TCR0 0xe610
61#define PLA_TCR1 0xe612
62#define PLA_TXFIFO_CTRL 0xe618
63#define PLA_RSTTELLY 0xe800
64#define PLA_CR 0xe813
65#define PLA_CRWECR 0xe81c
66#define PLA_CONFIG5 0xe822
67#define PLA_PHY_PWR 0xe84c
68#define PLA_OOB_CTRL 0xe84f
69#define PLA_CPCR 0xe854
70#define PLA_MISC_0 0xe858
71#define PLA_MISC_1 0xe85a
72#define PLA_OCP_GPHY_BASE 0xe86c
73#define PLA_TELLYCNT 0xe890
74#define PLA_SFF_STS_7 0xe8de
75#define PLA_PHYSTATUS 0xe908
76#define PLA_BP_BA 0xfc26
77#define PLA_BP_0 0xfc28
78#define PLA_BP_1 0xfc2a
79#define PLA_BP_2 0xfc2c
80#define PLA_BP_3 0xfc2e
81#define PLA_BP_4 0xfc30
82#define PLA_BP_5 0xfc32
83#define PLA_BP_6 0xfc34
84#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080085#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000086
hayeswang43779f82014-01-02 11:25:10 +080087#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000088#define USB_DEV_STAT 0xb808
89#define USB_USB_CTRL 0xd406
90#define USB_PHY_CTRL 0xd408
91#define USB_TX_AGG 0xd40a
92#define USB_RX_BUF_TH 0xd40c
93#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +080094#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +000095#define USB_PM_CTRL_STATUS 0xd432
96#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +080097#define USB_TOLERANCE 0xd490
98#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +000099#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800100#define USB_MISC_0 0xd81a
101#define USB_POWER_CUT 0xd80a
102#define USB_AFE_CTRL2 0xd824
103#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000104#define USB_BP_BA 0xfc26
105#define USB_BP_0 0xfc28
106#define USB_BP_1 0xfc2a
107#define USB_BP_2 0xfc2c
108#define USB_BP_3 0xfc2e
109#define USB_BP_4 0xfc30
110#define USB_BP_5 0xfc32
111#define USB_BP_6 0xfc34
112#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800113#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000114
115/* OCP Registers */
116#define OCP_ALDPS_CONFIG 0x2010
117#define OCP_EEE_CONFIG1 0x2080
118#define OCP_EEE_CONFIG2 0x2092
119#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800120#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000121#define OCP_EEE_AR 0xa41a
122#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800123#define OCP_PHY_STATUS 0xa420
124#define OCP_POWER_CFG 0xa430
125#define OCP_EEE_CFG 0xa432
126#define OCP_SRAM_ADDR 0xa436
127#define OCP_SRAM_DATA 0xa438
128#define OCP_DOWN_SPEED 0xa442
129#define OCP_EEE_CFG2 0xa5d0
130#define OCP_ADC_CFG 0xbc06
131
132/* SRAM Register */
133#define SRAM_LPF_CFG 0x8012
134#define SRAM_10M_AMP1 0x8080
135#define SRAM_10M_AMP2 0x8082
136#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000137
138/* PLA_RCR */
139#define RCR_AAP 0x00000001
140#define RCR_APM 0x00000002
141#define RCR_AM 0x00000004
142#define RCR_AB 0x00000008
143#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
144
145/* PLA_RXFIFO_CTRL0 */
146#define RXFIFO_THR1_NORMAL 0x00080002
147#define RXFIFO_THR1_OOB 0x01800003
148
149/* PLA_RXFIFO_CTRL1 */
150#define RXFIFO_THR2_FULL 0x00000060
151#define RXFIFO_THR2_HIGH 0x00000038
152#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800153#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000154
155/* PLA_RXFIFO_CTRL2 */
156#define RXFIFO_THR3_FULL 0x00000078
157#define RXFIFO_THR3_HIGH 0x00000048
158#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800159#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000160
161/* PLA_TXFIFO_CTRL */
162#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800163#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000164
165/* PLA_FMC */
166#define FMC_FCR_MCU_EN 0x0001
167
168/* PLA_EEEP_CR */
169#define EEEP_CR_EEEP_TX 0x0002
170
hayeswang43779f82014-01-02 11:25:10 +0800171/* PLA_WDT6_CTRL */
172#define WDT6_SET_MODE 0x0010
173
hayeswangac718b62013-05-02 16:01:25 +0000174/* PLA_TCR0 */
175#define TCR0_TX_EMPTY 0x0800
176#define TCR0_AUTO_FIFO 0x0080
177
178/* PLA_TCR1 */
179#define VERSION_MASK 0x7cf0
180
181/* PLA_CR */
182#define CR_RST 0x10
183#define CR_RE 0x08
184#define CR_TE 0x04
185
186/* PLA_CRWECR */
187#define CRWECR_NORAML 0x00
188#define CRWECR_CONFIG 0xc0
189
190/* PLA_OOB_CTRL */
191#define NOW_IS_OOB 0x80
192#define TXFIFO_EMPTY 0x20
193#define RXFIFO_EMPTY 0x10
194#define LINK_LIST_READY 0x02
195#define DIS_MCU_CLROOB 0x01
196#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
197
198/* PLA_MISC_1 */
199#define RXDY_GATED_EN 0x0008
200
201/* PLA_SFF_STS_7 */
202#define RE_INIT_LL 0x8000
203#define MCU_BORW_EN 0x4000
204
205/* PLA_CPCR */
206#define CPCR_RX_VLAN 0x0040
207
208/* PLA_CFG_WOL */
209#define MAGIC_EN 0x0001
210
hayeswang43779f82014-01-02 11:25:10 +0800211/* PLA_TEREDO_CFG */
212#define TEREDO_SEL 0x8000
213#define TEREDO_WAKE_MASK 0x7f00
214#define TEREDO_RS_EVENT_MASK 0x00fe
215#define OOB_TEREDO_EN 0x0001
216
hayeswangac718b62013-05-02 16:01:25 +0000217/* PAL_BDC_CR */
218#define ALDPS_PROXY_MODE 0x0001
219
220/* PLA_CONFIG5 */
221#define LAN_WAKE_EN 0x0002
222
223/* PLA_LED_FEATURE */
224#define LED_MODE_MASK 0x0700
225
226/* PLA_PHY_PWR */
227#define TX_10M_IDLE_EN 0x0080
228#define PFM_PWM_SWITCH 0x0040
229
230/* PLA_MAC_PWR_CTRL */
231#define D3_CLK_GATED_EN 0x00004000
232#define MCU_CLK_RATIO 0x07010f07
233#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800234#define ALDPS_SPDWN_RATIO 0x0f87
235
236/* PLA_MAC_PWR_CTRL2 */
237#define EEE_SPDWN_RATIO 0x8007
238
239/* PLA_MAC_PWR_CTRL3 */
240#define PKT_AVAIL_SPDWN_EN 0x0100
241#define SUSPEND_SPDWN_EN 0x0004
242#define U1U2_SPDWN_EN 0x0002
243#define L1_SPDWN_EN 0x0001
244
245/* PLA_MAC_PWR_CTRL4 */
246#define PWRSAVE_SPDWN_EN 0x1000
247#define RXDV_SPDWN_EN 0x0800
248#define TX10MIDLE_EN 0x0100
249#define TP100_SPDWN_EN 0x0020
250#define TP500_SPDWN_EN 0x0010
251#define TP1000_SPDWN_EN 0x0008
252#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000253
254/* PLA_GPHY_INTR_IMR */
255#define GPHY_STS_MSK 0x0001
256#define SPEED_DOWN_MSK 0x0002
257#define SPDWN_RXDV_MSK 0x0004
258#define SPDWN_LINKCHG_MSK 0x0008
259
260/* PLA_PHYAR */
261#define PHYAR_FLAG 0x80000000
262
263/* PLA_EEE_CR */
264#define EEE_RX_EN 0x0001
265#define EEE_TX_EN 0x0002
266
hayeswang43779f82014-01-02 11:25:10 +0800267/* PLA_BOOT_CTRL */
268#define AUTOLOAD_DONE 0x0002
269
hayeswangac718b62013-05-02 16:01:25 +0000270/* USB_DEV_STAT */
271#define STAT_SPEED_MASK 0x0006
272#define STAT_SPEED_HIGH 0x0000
273#define STAT_SPEED_FULL 0x0001
274
275/* USB_TX_AGG */
276#define TX_AGG_MAX_THRESHOLD 0x03
277
278/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800279#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800280#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800281#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000282
283/* USB_TX_DMA */
284#define TEST_MODE_DISABLE 0x00000001
285#define TX_SIZE_ADJUST1 0x00000100
286
287/* USB_UPS_CTRL */
288#define POWER_CUT 0x0100
289
290/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800291#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000292
293/* USB_USB_CTRL */
294#define RX_AGG_DISABLE 0x0010
295
hayeswang43779f82014-01-02 11:25:10 +0800296/* USB_U2P3_CTRL */
297#define U2P3_ENABLE 0x0001
298
299/* USB_POWER_CUT */
300#define PWR_EN 0x0001
301#define PHASE2_EN 0x0008
302
303/* USB_MISC_0 */
304#define PCUT_STATUS 0x0001
305
306/* USB_RX_EARLY_AGG */
307#define EARLY_AGG_SUPPER 0x0e832981
308#define EARLY_AGG_HIGH 0x0e837a12
309#define EARLY_AGG_SLOW 0x0e83ffff
310
311/* USB_WDT11_CTRL */
312#define TIMER11_EN 0x0001
313
314/* USB_LPM_CTRL */
315#define LPM_TIMER_MASK 0x0c
316#define LPM_TIMER_500MS 0x04 /* 500 ms */
317#define LPM_TIMER_500US 0x0c /* 500 us */
318
319/* USB_AFE_CTRL2 */
320#define SEN_VAL_MASK 0xf800
321#define SEN_VAL_NORMAL 0xa000
322#define SEL_RXIDLE 0x0100
323
hayeswangac718b62013-05-02 16:01:25 +0000324/* OCP_ALDPS_CONFIG */
325#define ENPWRSAVE 0x8000
326#define ENPDNPS 0x0200
327#define LINKENA 0x0100
328#define DIS_SDSAVE 0x0010
329
hayeswang43779f82014-01-02 11:25:10 +0800330/* OCP_PHY_STATUS */
331#define PHY_STAT_MASK 0x0007
332#define PHY_STAT_LAN_ON 3
333#define PHY_STAT_PWRDN 5
334
335/* OCP_POWER_CFG */
336#define EEE_CLKDIV_EN 0x8000
337#define EN_ALDPS 0x0004
338#define EN_10M_PLLOFF 0x0001
339
hayeswangac718b62013-05-02 16:01:25 +0000340/* OCP_EEE_CONFIG1 */
341#define RG_TXLPI_MSK_HFDUP 0x8000
342#define RG_MATCLR_EN 0x4000
343#define EEE_10_CAP 0x2000
344#define EEE_NWAY_EN 0x1000
345#define TX_QUIET_EN 0x0200
346#define RX_QUIET_EN 0x0100
347#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
348#define RG_RXLPI_MSK_HFDUP 0x0008
349#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
350
351/* OCP_EEE_CONFIG2 */
352#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
353#define RG_DACQUIET_EN 0x0400
354#define RG_LDVQUIET_EN 0x0200
355#define RG_CKRSEL 0x0020
356#define RG_EEEPRG_EN 0x0010
357
358/* OCP_EEE_CONFIG3 */
359#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
360#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
361#define MSK_PH 0x0006 /* bit 0 ~ 3 */
362
363/* OCP_EEE_AR */
364/* bit[15:14] function */
365#define FUN_ADDR 0x0000
366#define FUN_DATA 0x4000
367/* bit[4:0] device addr */
368#define DEVICE_ADDR 0x0007
369
370/* OCP_EEE_DATA */
371#define EEE_ADDR 0x003C
372#define EEE_DATA 0x0002
373
hayeswang43779f82014-01-02 11:25:10 +0800374/* OCP_EEE_CFG */
375#define CTAP_SHORT_EN 0x0040
376#define EEE10_EN 0x0010
377
378/* OCP_DOWN_SPEED */
379#define EN_10M_BGOFF 0x0080
380
381/* OCP_EEE_CFG2 */
382#define MY1000_EEE 0x0004
383#define MY100_EEE 0x0002
384
385/* OCP_ADC_CFG */
386#define CKADSEL_L 0x0100
387#define ADC_EN 0x0080
388#define EN_EMI_L 0x0040
389
390/* SRAM_LPF_CFG */
391#define LPF_AUTO_TUNE 0x8000
392
393/* SRAM_10M_AMP1 */
394#define GDAC_IB_UPALL 0x0008
395
396/* SRAM_10M_AMP2 */
397#define AMP_DN 0x0200
398
399/* SRAM_IMPEDANCE */
400#define RX_DRIVING_MASK 0x6000
401
hayeswangac718b62013-05-02 16:01:25 +0000402enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800403 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000404 _100bps = 0x08,
405 _10bps = 0x04,
406 LINK_STATUS = 0x02,
407 FULL_DUP = 0x01,
408};
409
hayeswangebc2ec482013-08-14 20:54:38 +0800410#define RTL8152_MAX_TX 10
411#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800412#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800413#define CRC_SIZE 4
414#define TX_ALIGN 4
415#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800416
417#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800418
hayeswangac718b62013-05-02 16:01:25 +0000419#define RTL8152_REQT_READ 0xc0
420#define RTL8152_REQT_WRITE 0x40
421#define RTL8152_REQ_GET_REGS 0x05
422#define RTL8152_REQ_SET_REGS 0x05
423
424#define BYTE_EN_DWORD 0xff
425#define BYTE_EN_WORD 0x33
426#define BYTE_EN_BYTE 0x11
427#define BYTE_EN_SIX_BYTES 0x3f
428#define BYTE_EN_START_MASK 0x0f
429#define BYTE_EN_END_MASK 0xf0
430
431#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
432#define RTL8152_TX_TIMEOUT (HZ)
433
434/* rtl8152 flags */
435enum rtl8152_flags {
436 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000437 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800438 WORK_ENABLE,
439 RTL8152_LINK_CHG,
hayeswangac718b62013-05-02 16:01:25 +0000440};
441
442/* Define these values to match your device */
443#define VENDOR_ID_REALTEK 0x0bda
444#define PRODUCT_ID_RTL8152 0x8152
hayeswang43779f82014-01-02 11:25:10 +0800445#define PRODUCT_ID_RTL8153 0x8153
446
447#define VENDOR_ID_SAMSUNG 0x04e8
448#define PRODUCT_ID_SAMSUNG 0xa101
hayeswangac718b62013-05-02 16:01:25 +0000449
450#define MCU_TYPE_PLA 0x0100
451#define MCU_TYPE_USB 0x0000
452
453struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800454 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000455#define RX_LEN_MASK 0x7fff
hayeswang500b6d72013-11-20 17:30:57 +0800456 __le32 opts2;
457 __le32 opts3;
458 __le32 opts4;
459 __le32 opts5;
460 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000461};
462
463struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800464 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000465#define TX_FS (1 << 31) /* First segment of a packet */
466#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800467#define TX_LEN_MASK 0x3ffff
468
hayeswang500b6d72013-11-20 17:30:57 +0800469 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800470#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
471#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
472#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
473#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000474};
475
hayeswangdff4e8a2013-08-16 16:09:33 +0800476struct r8152;
477
hayeswangebc2ec482013-08-14 20:54:38 +0800478struct rx_agg {
479 struct list_head list;
480 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800481 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800482 void *buffer;
483 void *head;
484};
485
486struct tx_agg {
487 struct list_head list;
488 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800489 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800490 void *buffer;
491 void *head;
492 u32 skb_num;
493 u32 skb_len;
494};
495
hayeswangac718b62013-05-02 16:01:25 +0000496struct r8152 {
497 unsigned long flags;
498 struct usb_device *udev;
499 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800500 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000501 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800502 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800503 struct tx_agg tx_info[RTL8152_MAX_TX];
504 struct rx_agg rx_info[RTL8152_MAX_RX];
505 struct list_head rx_done, tx_free;
506 struct sk_buff_head tx_queue;
507 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000508 struct delayed_work schedule;
509 struct mii_if_info mii;
hayeswangc81229c2014-01-02 11:22:42 +0800510
511 struct rtl_ops {
512 void (*init)(struct r8152 *);
513 int (*enable)(struct r8152 *);
514 void (*disable)(struct r8152 *);
515 void (*down)(struct r8152 *);
516 void (*unload)(struct r8152 *);
517 } rtl_ops;
518
hayeswang40a82912013-08-14 20:54:40 +0800519 int intr_interval;
hayeswangac718b62013-05-02 16:01:25 +0000520 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800521 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000522 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800523 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000524 u8 version;
525 u8 speed;
526};
527
528enum rtl_version {
529 RTL_VER_UNKNOWN = 0,
530 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800531 RTL_VER_02,
532 RTL_VER_03,
533 RTL_VER_04,
534 RTL_VER_05,
535 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000536};
537
538/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
539 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
540 */
541static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800542static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000543
544static
545int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
546{
hayeswang31787f52013-07-31 17:21:25 +0800547 int ret;
548 void *tmp;
549
550 tmp = kmalloc(size, GFP_KERNEL);
551 if (!tmp)
552 return -ENOMEM;
553
554 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000555 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800556 value, index, tmp, size, 500);
557
558 memcpy(data, tmp, size);
559 kfree(tmp);
560
561 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000562}
563
564static
565int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
566{
hayeswang31787f52013-07-31 17:21:25 +0800567 int ret;
568 void *tmp;
569
570 tmp = kmalloc(size, GFP_KERNEL);
571 if (!tmp)
572 return -ENOMEM;
573
574 memcpy(tmp, data, size);
575
576 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000577 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800578 value, index, tmp, size, 500);
579
580 kfree(tmp);
581 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000582}
583
584static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
585 void *data, u16 type)
586{
hayeswang45f4a192014-01-06 17:08:41 +0800587 u16 limit = 64;
588 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000589
590 if (test_bit(RTL8152_UNPLUG, &tp->flags))
591 return -ENODEV;
592
593 /* both size and indix must be 4 bytes align */
594 if ((size & 3) || !size || (index & 3) || !data)
595 return -EPERM;
596
597 if ((u32)index + (u32)size > 0xffff)
598 return -EPERM;
599
600 while (size) {
601 if (size > limit) {
602 ret = get_registers(tp, index, type, limit, data);
603 if (ret < 0)
604 break;
605
606 index += limit;
607 data += limit;
608 size -= limit;
609 } else {
610 ret = get_registers(tp, index, type, size, data);
611 if (ret < 0)
612 break;
613
614 index += size;
615 data += size;
616 size = 0;
617 break;
618 }
619 }
620
621 return ret;
622}
623
624static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
625 u16 size, void *data, u16 type)
626{
hayeswang45f4a192014-01-06 17:08:41 +0800627 int ret;
628 u16 byteen_start, byteen_end, byen;
629 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000630
631 if (test_bit(RTL8152_UNPLUG, &tp->flags))
632 return -ENODEV;
633
634 /* both size and indix must be 4 bytes align */
635 if ((size & 3) || !size || (index & 3) || !data)
636 return -EPERM;
637
638 if ((u32)index + (u32)size > 0xffff)
639 return -EPERM;
640
641 byteen_start = byteen & BYTE_EN_START_MASK;
642 byteen_end = byteen & BYTE_EN_END_MASK;
643
644 byen = byteen_start | (byteen_start << 4);
645 ret = set_registers(tp, index, type | byen, 4, data);
646 if (ret < 0)
647 goto error1;
648
649 index += 4;
650 data += 4;
651 size -= 4;
652
653 if (size) {
654 size -= 4;
655
656 while (size) {
657 if (size > limit) {
658 ret = set_registers(tp, index,
659 type | BYTE_EN_DWORD,
660 limit, data);
661 if (ret < 0)
662 goto error1;
663
664 index += limit;
665 data += limit;
666 size -= limit;
667 } else {
668 ret = set_registers(tp, index,
669 type | BYTE_EN_DWORD,
670 size, data);
671 if (ret < 0)
672 goto error1;
673
674 index += size;
675 data += size;
676 size = 0;
677 break;
678 }
679 }
680
681 byen = byteen_end | (byteen_end >> 4);
682 ret = set_registers(tp, index, type | byen, 4, data);
683 if (ret < 0)
684 goto error1;
685 }
686
687error1:
688 return ret;
689}
690
691static inline
692int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
693{
694 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
695}
696
697static inline
698int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
699{
700 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
701}
702
703static inline
704int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
705{
706 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
707}
708
709static inline
710int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
711{
712 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
713}
714
715static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
716{
hayeswangc8826de2013-07-31 17:21:26 +0800717 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000718
hayeswangc8826de2013-07-31 17:21:26 +0800719 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000720
721 return __le32_to_cpu(data);
722}
723
724static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
725{
hayeswangc8826de2013-07-31 17:21:26 +0800726 __le32 tmp = __cpu_to_le32(data);
727
728 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000729}
730
731static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
732{
733 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800734 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000735 u8 shift = index & 2;
736
737 index &= ~3;
738
hayeswangc8826de2013-07-31 17:21:26 +0800739 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000740
hayeswangc8826de2013-07-31 17:21:26 +0800741 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000742 data >>= (shift * 8);
743 data &= 0xffff;
744
745 return (u16)data;
746}
747
748static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
749{
hayeswangc8826de2013-07-31 17:21:26 +0800750 u32 mask = 0xffff;
751 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000752 u16 byen = BYTE_EN_WORD;
753 u8 shift = index & 2;
754
755 data &= mask;
756
757 if (index & 2) {
758 byen <<= shift;
759 mask <<= (shift * 8);
760 data <<= (shift * 8);
761 index &= ~3;
762 }
763
hayeswangc8826de2013-07-31 17:21:26 +0800764 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000765
hayeswangc8826de2013-07-31 17:21:26 +0800766 data |= __le32_to_cpu(tmp) & ~mask;
767 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000768
hayeswangc8826de2013-07-31 17:21:26 +0800769 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000770}
771
772static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
773{
774 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800775 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000776 u8 shift = index & 3;
777
778 index &= ~3;
779
hayeswangc8826de2013-07-31 17:21:26 +0800780 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000781
hayeswangc8826de2013-07-31 17:21:26 +0800782 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000783 data >>= (shift * 8);
784 data &= 0xff;
785
786 return (u8)data;
787}
788
789static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
790{
hayeswangc8826de2013-07-31 17:21:26 +0800791 u32 mask = 0xff;
792 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000793 u16 byen = BYTE_EN_BYTE;
794 u8 shift = index & 3;
795
796 data &= mask;
797
798 if (index & 3) {
799 byen <<= shift;
800 mask <<= (shift * 8);
801 data <<= (shift * 8);
802 index &= ~3;
803 }
804
hayeswangc8826de2013-07-31 17:21:26 +0800805 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000806
hayeswangc8826de2013-07-31 17:21:26 +0800807 data |= __le32_to_cpu(tmp) & ~mask;
808 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000809
hayeswangc8826de2013-07-31 17:21:26 +0800810 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000811}
812
hayeswangac244d32014-01-02 11:22:40 +0800813static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
814{
815 u16 ocp_base, ocp_index;
816
817 ocp_base = addr & 0xf000;
818 if (ocp_base != tp->ocp_base) {
819 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
820 tp->ocp_base = ocp_base;
821 }
822
823 ocp_index = (addr & 0x0fff) | 0xb000;
824 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
825}
826
hayeswange3fe0b12014-01-02 11:22:39 +0800827static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
828{
829 u16 ocp_base, ocp_index;
830
831 ocp_base = addr & 0xf000;
832 if (ocp_base != tp->ocp_base) {
833 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
834 tp->ocp_base = ocp_base;
835 }
836
837 ocp_index = (addr & 0x0fff) | 0xb000;
838 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
839}
840
hayeswangac244d32014-01-02 11:22:40 +0800841static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000842{
hayeswangac244d32014-01-02 11:22:40 +0800843 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000844}
845
hayeswangac244d32014-01-02 11:22:40 +0800846static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000847{
hayeswangac244d32014-01-02 11:22:40 +0800848 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000849}
850
hayeswang43779f82014-01-02 11:25:10 +0800851static void sram_write(struct r8152 *tp, u16 addr, u16 data)
852{
853 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
854 ocp_reg_write(tp, OCP_SRAM_DATA, data);
855}
856
857static u16 sram_read(struct r8152 *tp, u16 addr)
858{
859 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
860 return ocp_reg_read(tp, OCP_SRAM_DATA);
861}
862
hayeswangac718b62013-05-02 16:01:25 +0000863static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
864{
865 struct r8152 *tp = netdev_priv(netdev);
866
867 if (phy_id != R8152_PHY_ID)
868 return -EINVAL;
869
870 return r8152_mdio_read(tp, reg);
871}
872
873static
874void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
875{
876 struct r8152 *tp = netdev_priv(netdev);
877
878 if (phy_id != R8152_PHY_ID)
879 return;
880
881 r8152_mdio_write(tp, reg, val);
882}
883
hayeswangebc2ec482013-08-14 20:54:38 +0800884static
885int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
886
hayeswangac718b62013-05-02 16:01:25 +0000887static inline void set_ethernet_addr(struct r8152 *tp)
888{
889 struct net_device *dev = tp->netdev;
hayeswang31787f52013-07-31 17:21:25 +0800890 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000891
hayeswang31787f52013-07-31 17:21:25 +0800892 if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
hayeswangac718b62013-05-02 16:01:25 +0000893 netif_notice(tp, probe, dev, "inet addr fail\n");
894 else {
895 memcpy(dev->dev_addr, node_id, dev->addr_len);
896 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
897 }
hayeswangac718b62013-05-02 16:01:25 +0000898}
899
900static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
901{
902 struct r8152 *tp = netdev_priv(netdev);
903 struct sockaddr *addr = p;
904
905 if (!is_valid_ether_addr(addr->sa_data))
906 return -EADDRNOTAVAIL;
907
908 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
909
910 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
911 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
912 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
913
914 return 0;
915}
916
hayeswangac718b62013-05-02 16:01:25 +0000917static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
918{
919 return &dev->stats;
920}
921
922static void read_bulk_callback(struct urb *urb)
923{
hayeswangac718b62013-05-02 16:01:25 +0000924 struct net_device *netdev;
hayeswanga5a4f462013-08-16 16:09:34 +0800925 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +0000926 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800927 struct rx_agg *agg;
928 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000929 int result;
hayeswangac718b62013-05-02 16:01:25 +0000930
hayeswangebc2ec482013-08-14 20:54:38 +0800931 agg = urb->context;
932 if (!agg)
933 return;
934
935 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000936 if (!tp)
937 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800938
hayeswangac718b62013-05-02 16:01:25 +0000939 if (test_bit(RTL8152_UNPLUG, &tp->flags))
940 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800941
942 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000943 return;
944
hayeswangebc2ec482013-08-14 20:54:38 +0800945 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800946
947 /* When link down, the driver would cancel all bulks. */
948 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +0800949 if (!netif_carrier_ok(netdev))
950 return;
951
hayeswangac718b62013-05-02 16:01:25 +0000952 switch (status) {
953 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +0800954 if (urb->actual_length < ETH_ZLEN)
955 break;
956
hayeswanga5a4f462013-08-16 16:09:34 +0800957 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800958 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800959 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800960 tasklet_schedule(&tp->tl);
961 return;
hayeswangac718b62013-05-02 16:01:25 +0000962 case -ESHUTDOWN:
963 set_bit(RTL8152_UNPLUG, &tp->flags);
964 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +0800965 return;
hayeswangac718b62013-05-02 16:01:25 +0000966 case -ENOENT:
967 return; /* the urb is in unlink state */
968 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800969 if (net_ratelimit())
970 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +0800971 break;
hayeswangac718b62013-05-02 16:01:25 +0000972 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800973 if (net_ratelimit())
974 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +0800975 break;
hayeswangac718b62013-05-02 16:01:25 +0000976 }
977
hayeswangebc2ec482013-08-14 20:54:38 +0800978 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +0000979 if (result == -ENODEV) {
980 netif_device_detach(tp->netdev);
981 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +0800982 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800983 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800984 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800985 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +0000986 }
hayeswangac718b62013-05-02 16:01:25 +0000987}
988
989static void write_bulk_callback(struct urb *urb)
990{
hayeswangebc2ec482013-08-14 20:54:38 +0800991 struct net_device_stats *stats;
hayeswanga5a4f462013-08-16 16:09:34 +0800992 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +0800993 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +0000994 struct r8152 *tp;
995 int status = urb->status;
996
hayeswangebc2ec482013-08-14 20:54:38 +0800997 agg = urb->context;
998 if (!agg)
999 return;
1000
1001 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001002 if (!tp)
1003 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001004
1005 stats = rtl8152_get_stats(tp->netdev);
1006 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001007 if (net_ratelimit())
1008 netdev_warn(tp->netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001009 stats->tx_errors += agg->skb_num;
1010 } else {
1011 stats->tx_packets += agg->skb_num;
1012 stats->tx_bytes += agg->skb_len;
1013 }
1014
hayeswanga5a4f462013-08-16 16:09:34 +08001015 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001016 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +08001017 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001018
1019 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +00001020 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001021
1022 if (!test_bit(WORK_ENABLE, &tp->flags))
1023 return;
1024
1025 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1026 return;
1027
1028 if (!skb_queue_empty(&tp->tx_queue))
1029 tasklet_schedule(&tp->tl);
1030}
1031
hayeswang40a82912013-08-14 20:54:40 +08001032static void intr_callback(struct urb *urb)
1033{
1034 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001035 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001036 int status = urb->status;
1037 int res;
1038
1039 tp = urb->context;
1040 if (!tp)
1041 return;
1042
1043 if (!test_bit(WORK_ENABLE, &tp->flags))
1044 return;
1045
1046 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1047 return;
1048
1049 switch (status) {
1050 case 0: /* success */
1051 break;
1052 case -ECONNRESET: /* unlink */
1053 case -ESHUTDOWN:
1054 netif_device_detach(tp->netdev);
1055 case -ENOENT:
1056 return;
1057 case -EOVERFLOW:
1058 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1059 goto resubmit;
1060 /* -EPIPE: should clear the halt */
1061 default:
1062 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1063 goto resubmit;
1064 }
1065
1066 d = urb->transfer_buffer;
1067 if (INTR_LINK & __le16_to_cpu(d[0])) {
1068 if (!(tp->speed & LINK_STATUS)) {
1069 set_bit(RTL8152_LINK_CHG, &tp->flags);
1070 schedule_delayed_work(&tp->schedule, 0);
1071 }
1072 } else {
1073 if (tp->speed & LINK_STATUS) {
1074 set_bit(RTL8152_LINK_CHG, &tp->flags);
1075 schedule_delayed_work(&tp->schedule, 0);
1076 }
1077 }
1078
1079resubmit:
1080 res = usb_submit_urb(urb, GFP_ATOMIC);
1081 if (res == -ENODEV)
1082 netif_device_detach(tp->netdev);
1083 else if (res)
1084 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001085 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001086}
1087
hayeswangebc2ec482013-08-14 20:54:38 +08001088static inline void *rx_agg_align(void *data)
1089{
hayeswang8e1f51b2014-01-02 11:22:41 +08001090 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001091}
1092
1093static inline void *tx_agg_align(void *data)
1094{
hayeswang8e1f51b2014-01-02 11:22:41 +08001095 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001096}
1097
1098static void free_all_mem(struct r8152 *tp)
1099{
1100 int i;
1101
1102 for (i = 0; i < RTL8152_MAX_RX; i++) {
1103 if (tp->rx_info[i].urb) {
1104 usb_free_urb(tp->rx_info[i].urb);
1105 tp->rx_info[i].urb = NULL;
1106 }
1107
1108 if (tp->rx_info[i].buffer) {
1109 kfree(tp->rx_info[i].buffer);
1110 tp->rx_info[i].buffer = NULL;
1111 tp->rx_info[i].head = NULL;
1112 }
1113 }
1114
1115 for (i = 0; i < RTL8152_MAX_TX; i++) {
1116 if (tp->tx_info[i].urb) {
1117 usb_free_urb(tp->tx_info[i].urb);
1118 tp->tx_info[i].urb = NULL;
1119 }
1120
1121 if (tp->tx_info[i].buffer) {
1122 kfree(tp->tx_info[i].buffer);
1123 tp->tx_info[i].buffer = NULL;
1124 tp->tx_info[i].head = NULL;
1125 }
1126 }
hayeswang40a82912013-08-14 20:54:40 +08001127
1128 if (tp->intr_urb) {
1129 usb_free_urb(tp->intr_urb);
1130 tp->intr_urb = NULL;
1131 }
1132
1133 if (tp->intr_buff) {
1134 kfree(tp->intr_buff);
1135 tp->intr_buff = NULL;
1136 }
hayeswangebc2ec482013-08-14 20:54:38 +08001137}
1138
1139static int alloc_all_mem(struct r8152 *tp)
1140{
1141 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001142 struct usb_interface *intf = tp->intf;
1143 struct usb_host_interface *alt = intf->cur_altsetting;
1144 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001145 struct urb *urb;
1146 int node, i;
1147 u8 *buf;
1148
1149 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1150
1151 spin_lock_init(&tp->rx_lock);
1152 spin_lock_init(&tp->tx_lock);
1153 INIT_LIST_HEAD(&tp->rx_done);
1154 INIT_LIST_HEAD(&tp->tx_free);
1155 skb_queue_head_init(&tp->tx_queue);
1156
1157 for (i = 0; i < RTL8152_MAX_RX; i++) {
1158 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1159 if (!buf)
1160 goto err1;
1161
1162 if (buf != rx_agg_align(buf)) {
1163 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001164 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1165 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001166 if (!buf)
1167 goto err1;
1168 }
1169
1170 urb = usb_alloc_urb(0, GFP_KERNEL);
1171 if (!urb) {
1172 kfree(buf);
1173 goto err1;
1174 }
1175
1176 INIT_LIST_HEAD(&tp->rx_info[i].list);
1177 tp->rx_info[i].context = tp;
1178 tp->rx_info[i].urb = urb;
1179 tp->rx_info[i].buffer = buf;
1180 tp->rx_info[i].head = rx_agg_align(buf);
1181 }
1182
1183 for (i = 0; i < RTL8152_MAX_TX; i++) {
1184 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1185 if (!buf)
1186 goto err1;
1187
1188 if (buf != tx_agg_align(buf)) {
1189 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001190 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1191 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001192 if (!buf)
1193 goto err1;
1194 }
1195
1196 urb = usb_alloc_urb(0, GFP_KERNEL);
1197 if (!urb) {
1198 kfree(buf);
1199 goto err1;
1200 }
1201
1202 INIT_LIST_HEAD(&tp->tx_info[i].list);
1203 tp->tx_info[i].context = tp;
1204 tp->tx_info[i].urb = urb;
1205 tp->tx_info[i].buffer = buf;
1206 tp->tx_info[i].head = tx_agg_align(buf);
1207
1208 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1209 }
1210
hayeswang40a82912013-08-14 20:54:40 +08001211 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1212 if (!tp->intr_urb)
1213 goto err1;
1214
1215 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1216 if (!tp->intr_buff)
1217 goto err1;
1218
1219 tp->intr_interval = (int)ep_intr->desc.bInterval;
1220 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1221 tp->intr_buff, INTBUFSIZE, intr_callback,
1222 tp, tp->intr_interval);
1223
hayeswangebc2ec482013-08-14 20:54:38 +08001224 return 0;
1225
1226err1:
1227 free_all_mem(tp);
1228 return -ENOMEM;
1229}
1230
hayeswang0de98f62013-08-16 16:09:35 +08001231static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1232{
1233 struct tx_agg *agg = NULL;
1234 unsigned long flags;
1235
1236 spin_lock_irqsave(&tp->tx_lock, flags);
1237 if (!list_empty(&tp->tx_free)) {
1238 struct list_head *cursor;
1239
1240 cursor = tp->tx_free.next;
1241 list_del_init(cursor);
1242 agg = list_entry(cursor, struct tx_agg, list);
1243 }
1244 spin_unlock_irqrestore(&tp->tx_lock, flags);
1245
1246 return agg;
1247}
1248
hayeswang5bd23882013-08-14 20:54:39 +08001249static void
1250r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1251{
1252 memset(desc, 0, sizeof(*desc));
1253
1254 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1255
1256 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1257 __be16 protocol;
1258 u8 ip_protocol;
1259 u32 opts2 = 0;
1260
1261 if (skb->protocol == htons(ETH_P_8021Q))
1262 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1263 else
1264 protocol = skb->protocol;
1265
1266 switch (protocol) {
1267 case htons(ETH_P_IP):
1268 opts2 |= IPV4_CS;
1269 ip_protocol = ip_hdr(skb)->protocol;
1270 break;
1271
1272 case htons(ETH_P_IPV6):
1273 opts2 |= IPV6_CS;
1274 ip_protocol = ipv6_hdr(skb)->nexthdr;
1275 break;
1276
1277 default:
1278 ip_protocol = IPPROTO_RAW;
1279 break;
1280 }
1281
1282 if (ip_protocol == IPPROTO_TCP) {
1283 opts2 |= TCP_CS;
1284 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1285 } else if (ip_protocol == IPPROTO_UDP) {
1286 opts2 |= UDP_CS;
1287 } else {
1288 WARN_ON_ONCE(1);
1289 }
1290
1291 desc->opts2 = cpu_to_le32(opts2);
1292 }
1293}
1294
hayeswangb1379d92013-08-16 16:09:37 +08001295static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1296{
hayeswang7937f9e2013-11-20 17:30:54 +08001297 int remain;
hayeswangb1379d92013-08-16 16:09:37 +08001298 u8 *tx_data;
1299
1300 tx_data = agg->head;
1301 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001302 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001303
hayeswang7937f9e2013-11-20 17:30:54 +08001304 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001305 struct tx_desc *tx_desc;
1306 struct sk_buff *skb;
1307 unsigned int len;
1308
1309 skb = skb_dequeue(&tp->tx_queue);
1310 if (!skb)
1311 break;
1312
hayeswang7937f9e2013-11-20 17:30:54 +08001313 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001314 len = skb->len;
1315 if (remain < len) {
1316 skb_queue_head(&tp->tx_queue, skb);
1317 break;
1318 }
1319
hayeswang7937f9e2013-11-20 17:30:54 +08001320 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001321 tx_desc = (struct tx_desc *)tx_data;
1322 tx_data += sizeof(*tx_desc);
1323
1324 r8152_tx_csum(tp, tx_desc, skb);
1325 memcpy(tx_data, skb->data, len);
1326 agg->skb_num++;
1327 agg->skb_len += len;
1328 dev_kfree_skb_any(skb);
1329
hayeswang7937f9e2013-11-20 17:30:54 +08001330 tx_data += len;
1331 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001332 }
1333
hayeswangdd1b1192013-11-20 17:30:56 +08001334 netif_tx_lock(tp->netdev);
1335
1336 if (netif_queue_stopped(tp->netdev) &&
1337 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1338 netif_wake_queue(tp->netdev);
1339
1340 netif_tx_unlock(tp->netdev);
1341
hayeswangb1379d92013-08-16 16:09:37 +08001342 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1343 agg->head, (int)(tx_data - (u8 *)agg->head),
1344 (usb_complete_t)write_bulk_callback, agg);
1345
1346 return usb_submit_urb(agg->urb, GFP_ATOMIC);
1347}
1348
hayeswangebc2ec482013-08-14 20:54:38 +08001349static void rx_bottom(struct r8152 *tp)
1350{
hayeswanga5a4f462013-08-16 16:09:34 +08001351 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001352 struct list_head *cursor, *next;
hayeswangebc2ec482013-08-14 20:54:38 +08001353
hayeswanga5a4f462013-08-16 16:09:34 +08001354 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001355 list_for_each_safe(cursor, next, &tp->rx_done) {
hayeswang43a44782013-08-16 16:09:36 +08001356 struct rx_desc *rx_desc;
1357 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001358 int len_used = 0;
1359 struct urb *urb;
1360 u8 *rx_data;
1361 int ret;
1362
hayeswangebc2ec482013-08-14 20:54:38 +08001363 list_del_init(cursor);
hayeswanga5a4f462013-08-16 16:09:34 +08001364 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001365
1366 agg = list_entry(cursor, struct rx_agg, list);
1367 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001368 if (urb->actual_length < ETH_ZLEN)
1369 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001370
hayeswangebc2ec482013-08-14 20:54:38 +08001371 rx_desc = agg->head;
1372 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001373 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001374
hayeswang7937f9e2013-11-20 17:30:54 +08001375 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001376 struct net_device *netdev = tp->netdev;
1377 struct net_device_stats *stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001378 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001379 struct sk_buff *skb;
1380
hayeswang7937f9e2013-11-20 17:30:54 +08001381 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001382 if (pkt_len < ETH_ZLEN)
1383 break;
1384
hayeswang7937f9e2013-11-20 17:30:54 +08001385 len_used += pkt_len;
1386 if (urb->actual_length < len_used)
1387 break;
1388
hayeswang43a44782013-08-16 16:09:36 +08001389 stats = rtl8152_get_stats(netdev);
1390
hayeswang8e1f51b2014-01-02 11:22:41 +08001391 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001392 rx_data += sizeof(struct rx_desc);
1393
1394 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1395 if (!skb) {
1396 stats->rx_dropped++;
1397 break;
1398 }
1399 memcpy(skb->data, rx_data, pkt_len);
1400 skb_put(skb, pkt_len);
1401 skb->protocol = eth_type_trans(skb, netdev);
1402 netif_rx(skb);
1403 stats->rx_packets++;
1404 stats->rx_bytes += pkt_len;
1405
hayeswang8e1f51b2014-01-02 11:22:41 +08001406 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001407 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001408 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001409 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001410 }
1411
hayeswang0de98f62013-08-16 16:09:35 +08001412submit:
hayeswangebc2ec482013-08-14 20:54:38 +08001413 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswanga5a4f462013-08-16 16:09:34 +08001414 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001415 if (ret && ret != -ENODEV) {
1416 list_add_tail(&agg->list, next);
1417 tasklet_schedule(&tp->tl);
1418 }
1419 }
hayeswanga5a4f462013-08-16 16:09:34 +08001420 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001421}
1422
1423static void tx_bottom(struct r8152 *tp)
1424{
hayeswangebc2ec482013-08-14 20:54:38 +08001425 int res;
1426
hayeswangb1379d92013-08-16 16:09:37 +08001427 do {
1428 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001429
hayeswangb1379d92013-08-16 16:09:37 +08001430 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001431 break;
1432
hayeswangb1379d92013-08-16 16:09:37 +08001433 agg = r8152_get_tx_agg(tp);
1434 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001435 break;
hayeswangb1379d92013-08-16 16:09:37 +08001436
1437 res = r8152_tx_agg_fill(tp, agg);
1438 if (res) {
1439 struct net_device_stats *stats;
1440 struct net_device *netdev;
1441 unsigned long flags;
1442
1443 netdev = tp->netdev;
1444 stats = rtl8152_get_stats(netdev);
1445
1446 if (res == -ENODEV) {
1447 netif_device_detach(netdev);
1448 } else {
1449 netif_warn(tp, tx_err, netdev,
1450 "failed tx_urb %d\n", res);
1451 stats->tx_dropped += agg->skb_num;
1452 spin_lock_irqsave(&tp->tx_lock, flags);
1453 list_add_tail(&agg->list, &tp->tx_free);
1454 spin_unlock_irqrestore(&tp->tx_lock, flags);
1455 }
hayeswangebc2ec482013-08-14 20:54:38 +08001456 }
hayeswangb1379d92013-08-16 16:09:37 +08001457 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001458}
1459
1460static void bottom_half(unsigned long data)
1461{
1462 struct r8152 *tp;
1463
1464 tp = (struct r8152 *)data;
1465
1466 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1467 return;
1468
1469 if (!test_bit(WORK_ENABLE, &tp->flags))
1470 return;
1471
hayeswang7559fb2f2013-08-16 16:09:38 +08001472 /* When link down, the driver would cancel all bulks. */
1473 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001474 if (!netif_carrier_ok(tp->netdev))
1475 return;
1476
1477 rx_bottom(tp);
1478 tx_bottom(tp);
1479}
1480
1481static
1482int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1483{
1484 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1485 agg->head, rx_buf_sz,
1486 (usb_complete_t)read_bulk_callback, agg);
1487
1488 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001489}
1490
1491static void rtl8152_tx_timeout(struct net_device *netdev)
1492{
1493 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001494 int i;
1495
Hayes Wang4a8deae2014-01-07 11:18:22 +08001496 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001497 for (i = 0; i < RTL8152_MAX_TX; i++)
1498 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001499}
1500
1501static void rtl8152_set_rx_mode(struct net_device *netdev)
1502{
1503 struct r8152 *tp = netdev_priv(netdev);
1504
hayeswang40a82912013-08-14 20:54:40 +08001505 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001506 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001507 schedule_delayed_work(&tp->schedule, 0);
1508 }
hayeswangac718b62013-05-02 16:01:25 +00001509}
1510
1511static void _rtl8152_set_rx_mode(struct net_device *netdev)
1512{
1513 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001514 u32 mc_filter[2]; /* Multicast hash filter */
1515 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001516 u32 ocp_data;
1517
hayeswangac718b62013-05-02 16:01:25 +00001518 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1519 netif_stop_queue(netdev);
1520 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1521 ocp_data &= ~RCR_ACPT_ALL;
1522 ocp_data |= RCR_AB | RCR_APM;
1523
1524 if (netdev->flags & IFF_PROMISC) {
1525 /* Unconditionally log net taps. */
1526 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1527 ocp_data |= RCR_AM | RCR_AAP;
1528 mc_filter[1] = mc_filter[0] = 0xffffffff;
1529 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1530 (netdev->flags & IFF_ALLMULTI)) {
1531 /* Too many to filter perfectly -- accept all multicasts. */
1532 ocp_data |= RCR_AM;
1533 mc_filter[1] = mc_filter[0] = 0xffffffff;
1534 } else {
1535 struct netdev_hw_addr *ha;
1536
1537 mc_filter[1] = mc_filter[0] = 0;
1538 netdev_for_each_mc_addr(ha, netdev) {
1539 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1540 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1541 ocp_data |= RCR_AM;
1542 }
1543 }
1544
hayeswang31787f52013-07-31 17:21:25 +08001545 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1546 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001547
hayeswang31787f52013-07-31 17:21:25 +08001548 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001549 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1550 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001551}
1552
1553static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1554 struct net_device *netdev)
1555{
1556 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001557
hayeswangac718b62013-05-02 16:01:25 +00001558 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001559
hayeswang61598782013-11-20 17:30:55 +08001560 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001561
hayeswangdd1b1192013-11-20 17:30:56 +08001562 if (list_empty(&tp->tx_free) &&
1563 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1564 netif_stop_queue(netdev);
1565
hayeswang61598782013-11-20 17:30:55 +08001566 if (!list_empty(&tp->tx_free))
1567 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001568
1569 return NETDEV_TX_OK;
1570}
1571
1572static void r8152b_reset_packet_filter(struct r8152 *tp)
1573{
1574 u32 ocp_data;
1575
1576 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1577 ocp_data &= ~FMC_FCR_MCU_EN;
1578 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1579 ocp_data |= FMC_FCR_MCU_EN;
1580 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1581}
1582
1583static void rtl8152_nic_reset(struct r8152 *tp)
1584{
1585 int i;
1586
1587 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1588
1589 for (i = 0; i < 1000; i++) {
1590 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1591 break;
1592 udelay(100);
1593 }
1594}
1595
hayeswangdd1b1192013-11-20 17:30:56 +08001596static void set_tx_qlen(struct r8152 *tp)
1597{
1598 struct net_device *netdev = tp->netdev;
1599
1600 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1601 sizeof(struct tx_desc));
1602}
1603
hayeswangac718b62013-05-02 16:01:25 +00001604static inline u8 rtl8152_get_speed(struct r8152 *tp)
1605{
1606 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1607}
1608
hayeswang507605a2014-01-02 11:22:43 +08001609static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001610{
hayeswangebc2ec482013-08-14 20:54:38 +08001611 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001612 u8 speed;
1613
1614 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001615 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001616 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001617 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001618 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1619 } else {
1620 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001621 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001622 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1623 }
hayeswang507605a2014-01-02 11:22:43 +08001624}
1625
1626static int rtl_enable(struct r8152 *tp)
1627{
1628 u32 ocp_data;
1629 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001630
1631 r8152b_reset_packet_filter(tp);
1632
1633 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1634 ocp_data |= CR_RE | CR_TE;
1635 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1636
1637 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1638 ocp_data &= ~RXDY_GATED_EN;
1639 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1640
hayeswangebc2ec482013-08-14 20:54:38 +08001641 INIT_LIST_HEAD(&tp->rx_done);
1642 ret = 0;
1643 for (i = 0; i < RTL8152_MAX_RX; i++) {
1644 INIT_LIST_HEAD(&tp->rx_info[i].list);
1645 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1646 }
hayeswangac718b62013-05-02 16:01:25 +00001647
hayeswangebc2ec482013-08-14 20:54:38 +08001648 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001649}
1650
hayeswang507605a2014-01-02 11:22:43 +08001651static int rtl8152_enable(struct r8152 *tp)
1652{
1653 set_tx_qlen(tp);
1654 rtl_set_eee_plus(tp);
1655
1656 return rtl_enable(tp);
1657}
1658
hayeswang43779f82014-01-02 11:25:10 +08001659static void r8153_set_rx_agg(struct r8152 *tp)
1660{
1661 u8 speed;
1662
1663 speed = rtl8152_get_speed(tp);
1664 if (speed & _1000bps) {
1665 if (tp->udev->speed == USB_SPEED_SUPER) {
1666 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1667 RX_THR_SUPPER);
1668 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1669 EARLY_AGG_SUPPER);
1670 } else {
1671 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1672 RX_THR_HIGH);
1673 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1674 EARLY_AGG_HIGH);
1675 }
1676 } else {
1677 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1678 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1679 EARLY_AGG_SLOW);
1680 }
1681}
1682
1683static int rtl8153_enable(struct r8152 *tp)
1684{
1685 set_tx_qlen(tp);
1686 rtl_set_eee_plus(tp);
1687 r8153_set_rx_agg(tp);
1688
1689 return rtl_enable(tp);
1690}
1691
hayeswangac718b62013-05-02 16:01:25 +00001692static void rtl8152_disable(struct r8152 *tp)
1693{
hayeswangebc2ec482013-08-14 20:54:38 +08001694 struct net_device_stats *stats = rtl8152_get_stats(tp->netdev);
1695 struct sk_buff *skb;
1696 u32 ocp_data;
1697 int i;
hayeswangac718b62013-05-02 16:01:25 +00001698
1699 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1700 ocp_data &= ~RCR_ACPT_ALL;
1701 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1702
hayeswangebc2ec482013-08-14 20:54:38 +08001703 while ((skb = skb_dequeue(&tp->tx_queue))) {
1704 dev_kfree_skb(skb);
1705 stats->tx_dropped++;
1706 }
1707
1708 for (i = 0; i < RTL8152_MAX_TX; i++)
1709 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001710
1711 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1712 ocp_data |= RXDY_GATED_EN;
1713 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1714
1715 for (i = 0; i < 1000; i++) {
1716 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1717 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1718 break;
1719 mdelay(1);
1720 }
1721
1722 for (i = 0; i < 1000; i++) {
1723 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1724 break;
1725 mdelay(1);
1726 }
1727
hayeswangebc2ec482013-08-14 20:54:38 +08001728 for (i = 0; i < RTL8152_MAX_RX; i++)
1729 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001730
1731 rtl8152_nic_reset(tp);
1732}
1733
1734static void r8152b_exit_oob(struct r8152 *tp)
1735{
1736 u32 ocp_data;
1737 int i;
1738
1739 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1740 ocp_data &= ~RCR_ACPT_ALL;
1741 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1742
1743 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1744 ocp_data |= RXDY_GATED_EN;
1745 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1746
1747 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1748 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1749
1750 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1751 ocp_data &= ~NOW_IS_OOB;
1752 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1753
1754 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1755 ocp_data &= ~MCU_BORW_EN;
1756 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1757
1758 for (i = 0; i < 1000; i++) {
1759 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1760 if (ocp_data & LINK_LIST_READY)
1761 break;
1762 mdelay(1);
1763 }
1764
1765 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1766 ocp_data |= RE_INIT_LL;
1767 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1768
1769 for (i = 0; i < 1000; i++) {
1770 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1771 if (ocp_data & LINK_LIST_READY)
1772 break;
1773 mdelay(1);
1774 }
1775
1776 rtl8152_nic_reset(tp);
1777
1778 /* rx share fifo credit full threshold */
1779 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1780
1781 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1782 ocp_data &= STAT_SPEED_MASK;
1783 if (ocp_data == STAT_SPEED_FULL) {
1784 /* rx share fifo credit near full threshold */
1785 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1786 RXFIFO_THR2_FULL);
1787 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1788 RXFIFO_THR3_FULL);
1789 } else {
1790 /* rx share fifo credit near full threshold */
1791 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1792 RXFIFO_THR2_HIGH);
1793 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1794 RXFIFO_THR3_HIGH);
1795 }
1796
1797 /* TX share fifo free credit full threshold */
1798 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1799
1800 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08001801 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00001802 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1803 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1804
1805 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1806 ocp_data &= ~CPCR_RX_VLAN;
1807 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1808
1809 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1810
1811 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1812 ocp_data |= TCR0_AUTO_FIFO;
1813 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1814}
1815
1816static void r8152b_enter_oob(struct r8152 *tp)
1817{
hayeswang45f4a192014-01-06 17:08:41 +08001818 u32 ocp_data;
1819 int i;
hayeswangac718b62013-05-02 16:01:25 +00001820
1821 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1822 ocp_data &= ~NOW_IS_OOB;
1823 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1824
1825 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1826 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1827 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1828
1829 rtl8152_disable(tp);
1830
1831 for (i = 0; i < 1000; i++) {
1832 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1833 if (ocp_data & LINK_LIST_READY)
1834 break;
1835 mdelay(1);
1836 }
1837
1838 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1839 ocp_data |= RE_INIT_LL;
1840 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1841
1842 for (i = 0; i < 1000; i++) {
1843 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1844 if (ocp_data & LINK_LIST_READY)
1845 break;
1846 mdelay(1);
1847 }
1848
1849 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1850
1851 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1852 ocp_data |= MAGIC_EN;
1853 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1854
1855 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1856 ocp_data |= CPCR_RX_VLAN;
1857 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1858
1859 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1860 ocp_data |= ALDPS_PROXY_MODE;
1861 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1862
1863 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1864 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1865 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1866
1867 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1868
1869 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1870 ocp_data &= ~RXDY_GATED_EN;
1871 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1872
1873 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1874 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1875 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1876}
1877
1878static void r8152b_disable_aldps(struct r8152 *tp)
1879{
1880 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1881 msleep(20);
1882}
1883
1884static inline void r8152b_enable_aldps(struct r8152 *tp)
1885{
1886 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1887 LINKENA | DIS_SDSAVE);
1888}
1889
hayeswang43779f82014-01-02 11:25:10 +08001890static void r8153_hw_phy_cfg(struct r8152 *tp)
1891{
1892 u32 ocp_data;
1893 u16 data;
1894
1895 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
1896 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1897
1898 if (tp->version == RTL_VER_03) {
1899 data = ocp_reg_read(tp, OCP_EEE_CFG);
1900 data &= ~CTAP_SHORT_EN;
1901 ocp_reg_write(tp, OCP_EEE_CFG, data);
1902 }
1903
1904 data = ocp_reg_read(tp, OCP_POWER_CFG);
1905 data |= EEE_CLKDIV_EN;
1906 ocp_reg_write(tp, OCP_POWER_CFG, data);
1907
1908 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
1909 data |= EN_10M_BGOFF;
1910 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
1911 data = ocp_reg_read(tp, OCP_POWER_CFG);
1912 data |= EN_10M_PLLOFF;
1913 ocp_reg_write(tp, OCP_POWER_CFG, data);
1914 data = sram_read(tp, SRAM_IMPEDANCE);
1915 data &= ~RX_DRIVING_MASK;
1916 sram_write(tp, SRAM_IMPEDANCE, data);
1917
1918 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1919 ocp_data |= PFM_PWM_SWITCH;
1920 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1921
1922 data = sram_read(tp, SRAM_LPF_CFG);
1923 data |= LPF_AUTO_TUNE;
1924 sram_write(tp, SRAM_LPF_CFG, data);
1925
1926 data = sram_read(tp, SRAM_10M_AMP1);
1927 data |= GDAC_IB_UPALL;
1928 sram_write(tp, SRAM_10M_AMP1, data);
1929 data = sram_read(tp, SRAM_10M_AMP2);
1930 data |= AMP_DN;
1931 sram_write(tp, SRAM_10M_AMP2, data);
1932}
1933
1934static void r8153_u1u2en(struct r8152 *tp, int enable)
1935{
1936 u8 u1u2[8];
1937
1938 if (enable)
1939 memset(u1u2, 0xff, sizeof(u1u2));
1940 else
1941 memset(u1u2, 0x00, sizeof(u1u2));
1942
1943 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
1944}
1945
1946static void r8153_u2p3en(struct r8152 *tp, int enable)
1947{
1948 u32 ocp_data;
1949
1950 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
1951 if (enable)
1952 ocp_data |= U2P3_ENABLE;
1953 else
1954 ocp_data &= ~U2P3_ENABLE;
1955 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
1956}
1957
1958static void r8153_power_cut_en(struct r8152 *tp, int enable)
1959{
1960 u32 ocp_data;
1961
1962 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
1963 if (enable)
1964 ocp_data |= PWR_EN | PHASE2_EN;
1965 else
1966 ocp_data &= ~(PWR_EN | PHASE2_EN);
1967 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
1968
1969 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1970 ocp_data &= ~PCUT_STATUS;
1971 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
1972}
1973
1974static void r8153_teredo_off(struct r8152 *tp)
1975{
1976 u32 ocp_data;
1977
1978 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1979 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1980 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1981
1982 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1983 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1984 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1985}
1986
1987static void r8153_first_init(struct r8152 *tp)
1988{
1989 u32 ocp_data;
1990 int i;
1991
1992 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1993 ocp_data |= RXDY_GATED_EN;
1994 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1995
1996 r8153_teredo_off(tp);
1997
1998 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1999 ocp_data &= ~RCR_ACPT_ALL;
2000 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2001
2002 r8153_hw_phy_cfg(tp);
2003
2004 rtl8152_nic_reset(tp);
2005
2006 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2007 ocp_data &= ~NOW_IS_OOB;
2008 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2009
2010 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2011 ocp_data &= ~MCU_BORW_EN;
2012 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2013
2014 for (i = 0; i < 1000; i++) {
2015 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2016 if (ocp_data & LINK_LIST_READY)
2017 break;
2018 mdelay(1);
2019 }
2020
2021 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2022 ocp_data |= RE_INIT_LL;
2023 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2024
2025 for (i = 0; i < 1000; i++) {
2026 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2027 if (ocp_data & LINK_LIST_READY)
2028 break;
2029 mdelay(1);
2030 }
2031
2032 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2033 ocp_data &= ~CPCR_RX_VLAN;
2034 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2035
2036 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2037
2038 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2039 ocp_data |= TCR0_AUTO_FIFO;
2040 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2041
2042 rtl8152_nic_reset(tp);
2043
2044 /* rx share fifo credit full threshold */
2045 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2046 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2047 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2048 /* TX share fifo free credit full threshold */
2049 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2050
2051 // rx aggregation
2052 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2053 ocp_data &= ~RX_AGG_DISABLE;
2054 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2055}
2056
2057static void r8153_enter_oob(struct r8152 *tp)
2058{
2059 u32 ocp_data;
2060 int i;
2061
2062 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2063 ocp_data &= ~NOW_IS_OOB;
2064 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2065
2066 rtl8152_disable(tp);
2067
2068 for (i = 0; i < 1000; i++) {
2069 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2070 if (ocp_data & LINK_LIST_READY)
2071 break;
2072 mdelay(1);
2073 }
2074
2075 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2076 ocp_data |= RE_INIT_LL;
2077 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2078
2079 for (i = 0; i < 1000; i++) {
2080 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2081 if (ocp_data & LINK_LIST_READY)
2082 break;
2083 mdelay(1);
2084 }
2085
2086 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2087
2088 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2089 ocp_data |= MAGIC_EN;
2090 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2091
2092 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2093 ocp_data &= ~TEREDO_WAKE_MASK;
2094 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2095
2096 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2097 ocp_data |= CPCR_RX_VLAN;
2098 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2099
2100 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2101 ocp_data |= ALDPS_PROXY_MODE;
2102 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2103
2104 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2105 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2106 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2107
2108 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
2109
2110 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2111 ocp_data &= ~RXDY_GATED_EN;
2112 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2113
2114 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2115 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2116 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2117}
2118
2119static void r8153_disable_aldps(struct r8152 *tp)
2120{
2121 u16 data;
2122
2123 data = ocp_reg_read(tp, OCP_POWER_CFG);
2124 data &= ~EN_ALDPS;
2125 ocp_reg_write(tp, OCP_POWER_CFG, data);
2126 msleep(20);
2127}
2128
2129static void r8153_enable_aldps(struct r8152 *tp)
2130{
2131 u16 data;
2132
2133 data = ocp_reg_read(tp, OCP_POWER_CFG);
2134 data |= EN_ALDPS;
2135 ocp_reg_write(tp, OCP_POWER_CFG, data);
2136}
2137
hayeswangac718b62013-05-02 16:01:25 +00002138static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2139{
hayeswang43779f82014-01-02 11:25:10 +08002140 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002141 int ret = 0;
2142
2143 cancel_delayed_work_sync(&tp->schedule);
2144 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2145 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2146 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002147 if (tp->mii.supports_gmii) {
2148 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2149 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2150 } else {
2151 gbcr = 0;
2152 }
hayeswangac718b62013-05-02 16:01:25 +00002153
2154 if (autoneg == AUTONEG_DISABLE) {
2155 if (speed == SPEED_10) {
2156 bmcr = 0;
2157 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2158 } else if (speed == SPEED_100) {
2159 bmcr = BMCR_SPEED100;
2160 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002161 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2162 bmcr = BMCR_SPEED1000;
2163 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002164 } else {
2165 ret = -EINVAL;
2166 goto out;
2167 }
2168
2169 if (duplex == DUPLEX_FULL)
2170 bmcr |= BMCR_FULLDPLX;
2171 } else {
2172 if (speed == SPEED_10) {
2173 if (duplex == DUPLEX_FULL)
2174 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2175 else
2176 anar |= ADVERTISE_10HALF;
2177 } else if (speed == SPEED_100) {
2178 if (duplex == DUPLEX_FULL) {
2179 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2180 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2181 } else {
2182 anar |= ADVERTISE_10HALF;
2183 anar |= ADVERTISE_100HALF;
2184 }
hayeswang43779f82014-01-02 11:25:10 +08002185 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2186 if (duplex == DUPLEX_FULL) {
2187 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2188 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2189 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2190 } else {
2191 anar |= ADVERTISE_10HALF;
2192 anar |= ADVERTISE_100HALF;
2193 gbcr |= ADVERTISE_1000HALF;
2194 }
hayeswangac718b62013-05-02 16:01:25 +00002195 } else {
2196 ret = -EINVAL;
2197 goto out;
2198 }
2199
2200 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2201 }
2202
hayeswang43779f82014-01-02 11:25:10 +08002203 if (tp->mii.supports_gmii)
2204 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2205
hayeswangac718b62013-05-02 16:01:25 +00002206 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2207 r8152_mdio_write(tp, MII_BMCR, bmcr);
2208
2209out:
hayeswangac718b62013-05-02 16:01:25 +00002210
2211 return ret;
2212}
2213
2214static void rtl8152_down(struct r8152 *tp)
2215{
2216 u32 ocp_data;
2217
2218 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2219 ocp_data &= ~POWER_CUT;
2220 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2221
2222 r8152b_disable_aldps(tp);
2223 r8152b_enter_oob(tp);
2224 r8152b_enable_aldps(tp);
2225}
2226
hayeswang43779f82014-01-02 11:25:10 +08002227static void rtl8153_down(struct r8152 *tp)
2228{
2229 r8153_u1u2en(tp, 0);
2230 r8153_power_cut_en(tp, 0);
2231 r8153_disable_aldps(tp);
2232 r8153_enter_oob(tp);
2233 r8153_enable_aldps(tp);
2234}
2235
hayeswangac718b62013-05-02 16:01:25 +00002236static void set_carrier(struct r8152 *tp)
2237{
2238 struct net_device *netdev = tp->netdev;
2239 u8 speed;
2240
hayeswang40a82912013-08-14 20:54:40 +08002241 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002242 speed = rtl8152_get_speed(tp);
2243
2244 if (speed & LINK_STATUS) {
2245 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002246 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002247 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2248 netif_carrier_on(netdev);
2249 }
2250 } else {
2251 if (tp->speed & LINK_STATUS) {
2252 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002253 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002254 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002255 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002256 }
2257 }
2258 tp->speed = speed;
2259}
2260
2261static void rtl_work_func_t(struct work_struct *work)
2262{
2263 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2264
2265 if (!test_bit(WORK_ENABLE, &tp->flags))
2266 goto out1;
2267
2268 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2269 goto out1;
2270
hayeswang40a82912013-08-14 20:54:40 +08002271 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2272 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002273
2274 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2275 _rtl8152_set_rx_mode(tp->netdev);
2276
hayeswangac718b62013-05-02 16:01:25 +00002277out1:
2278 return;
2279}
2280
2281static int rtl8152_open(struct net_device *netdev)
2282{
2283 struct r8152 *tp = netdev_priv(netdev);
2284 int res = 0;
2285
hayeswang40a82912013-08-14 20:54:40 +08002286 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2287 if (res) {
2288 if (res == -ENODEV)
2289 netif_device_detach(tp->netdev);
Hayes Wang4a8deae2014-01-07 11:18:22 +08002290 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2291 res);
hayeswang40a82912013-08-14 20:54:40 +08002292 return res;
hayeswangac718b62013-05-02 16:01:25 +00002293 }
2294
hayeswang43779f82014-01-02 11:25:10 +08002295 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2296 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2297 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002298 tp->speed = 0;
2299 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002300 netif_start_queue(netdev);
2301 set_bit(WORK_ENABLE, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002302
2303 return res;
2304}
2305
2306static int rtl8152_close(struct net_device *netdev)
2307{
2308 struct r8152 *tp = netdev_priv(netdev);
2309 int res = 0;
2310
hayeswang40a82912013-08-14 20:54:40 +08002311 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002312 clear_bit(WORK_ENABLE, &tp->flags);
2313 cancel_delayed_work_sync(&tp->schedule);
2314 netif_stop_queue(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002315 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002316 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002317 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002318
2319 return res;
2320}
2321
2322static void rtl_clear_bp(struct r8152 *tp)
2323{
2324 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
2325 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
2326 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
2327 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
2328 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
2329 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
2330 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
2331 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
2332 mdelay(3);
2333 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
2334 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
2335}
2336
hayeswang43779f82014-01-02 11:25:10 +08002337static void r8153_clear_bp(struct r8152 *tp)
2338{
2339 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
2340 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
2341 rtl_clear_bp(tp);
2342}
2343
hayeswangac718b62013-05-02 16:01:25 +00002344static void r8152b_enable_eee(struct r8152 *tp)
2345{
hayeswang45f4a192014-01-06 17:08:41 +08002346 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002347
2348 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2349 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2350 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2351 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2352 EEE_10_CAP | EEE_NWAY_EN |
2353 TX_QUIET_EN | RX_QUIET_EN |
2354 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2355 SDFALLTIME);
2356 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2357 RG_LDVQUIET_EN | RG_CKRSEL |
2358 RG_EEEPRG_EN);
2359 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2360 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2361 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2362 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2363 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2364 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2365}
2366
hayeswang43779f82014-01-02 11:25:10 +08002367static void r8153_enable_eee(struct r8152 *tp)
2368{
2369 u32 ocp_data;
2370 u16 data;
2371
2372 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2373 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2374 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2375 data = ocp_reg_read(tp, OCP_EEE_CFG);
2376 data |= EEE10_EN;
2377 ocp_reg_write(tp, OCP_EEE_CFG, data);
2378 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2379 data |= MY1000_EEE | MY100_EEE;
2380 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2381}
2382
hayeswangac718b62013-05-02 16:01:25 +00002383static void r8152b_enable_fc(struct r8152 *tp)
2384{
2385 u16 anar;
2386
2387 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2388 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2389 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2390}
2391
2392static void r8152b_hw_phy_cfg(struct r8152 *tp)
2393{
2394 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
2395 r8152b_disable_aldps(tp);
2396}
2397
2398static void r8152b_init(struct r8152 *tp)
2399{
hayeswangebc2ec482013-08-14 20:54:38 +08002400 u32 ocp_data;
2401 int i;
hayeswangac718b62013-05-02 16:01:25 +00002402
2403 rtl_clear_bp(tp);
2404
2405 if (tp->version == RTL_VER_01) {
2406 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2407 ocp_data &= ~LED_MODE_MASK;
2408 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2409 }
2410
2411 r8152b_hw_phy_cfg(tp);
2412
2413 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2414 ocp_data &= ~POWER_CUT;
2415 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2416
2417 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
hayeswang8e1f51b2014-01-02 11:22:41 +08002418 ocp_data &= ~RESUME_INDICATE;
hayeswangac718b62013-05-02 16:01:25 +00002419 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2420
2421 r8152b_exit_oob(tp);
2422
2423 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2424 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2425 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2426 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2427 ocp_data &= ~MCU_CLK_RATIO_MASK;
2428 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2429 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2430 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2431 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2432 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2433
2434 r8152b_enable_eee(tp);
2435 r8152b_enable_aldps(tp);
2436 r8152b_enable_fc(tp);
2437
2438 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2439 BMCR_ANRESTART);
2440 for (i = 0; i < 100; i++) {
2441 udelay(100);
2442 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
2443 break;
2444 }
2445
hayeswangebc2ec482013-08-14 20:54:38 +08002446 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002447 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08002448 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002449 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2450}
2451
hayeswang43779f82014-01-02 11:25:10 +08002452static void r8153_init(struct r8152 *tp)
2453{
2454 u32 ocp_data;
2455 int i;
2456
2457 r8153_u1u2en(tp, 0);
2458
2459 for (i = 0; i < 500; i++) {
2460 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2461 AUTOLOAD_DONE)
2462 break;
2463 msleep(20);
2464 }
2465
2466 for (i = 0; i < 500; i++) {
2467 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2468 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2469 break;
2470 msleep(20);
2471 }
2472
2473 r8153_u2p3en(tp, 0);
2474
2475 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2476 ocp_data &= ~TIMER11_EN;
2477 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2478
2479 r8153_clear_bp(tp);
2480
2481 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2482 ocp_data &= ~LED_MODE_MASK;
2483 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2484
2485 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2486 ocp_data &= ~LPM_TIMER_MASK;
2487 if (tp->udev->speed == USB_SPEED_SUPER)
2488 ocp_data |= LPM_TIMER_500US;
2489 else
2490 ocp_data |= LPM_TIMER_500MS;
2491 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2492
2493 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2494 ocp_data &= ~SEN_VAL_MASK;
2495 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2496 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2497
2498 r8153_power_cut_en(tp, 0);
2499 r8153_u1u2en(tp, 1);
2500
2501 r8153_first_init(tp);
2502
2503 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2504 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2505 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2506 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2507 U1U2_SPDWN_EN | L1_SPDWN_EN);
2508 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2509 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2510 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2511 EEE_SPDWN_EN);
2512
2513 r8153_enable_eee(tp);
2514 r8153_enable_aldps(tp);
2515 r8152b_enable_fc(tp);
2516
2517 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2518 BMCR_ANRESTART);
2519}
2520
hayeswangac718b62013-05-02 16:01:25 +00002521static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2522{
2523 struct r8152 *tp = usb_get_intfdata(intf);
2524
2525 netif_device_detach(tp->netdev);
2526
2527 if (netif_running(tp->netdev)) {
2528 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002529 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002530 cancel_delayed_work_sync(&tp->schedule);
hayeswangebc2ec482013-08-14 20:54:38 +08002531 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002532 }
2533
hayeswangc81229c2014-01-02 11:22:42 +08002534 tp->rtl_ops.down(tp);
hayeswangac718b62013-05-02 16:01:25 +00002535
2536 return 0;
2537}
2538
2539static int rtl8152_resume(struct usb_interface *intf)
2540{
2541 struct r8152 *tp = usb_get_intfdata(intf);
2542
hayeswangc81229c2014-01-02 11:22:42 +08002543 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002544 netif_device_attach(tp->netdev);
2545 if (netif_running(tp->netdev)) {
hayeswang43779f82014-01-02 11:25:10 +08002546 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2547 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2548 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002549 tp->speed = 0;
2550 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002551 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002552 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangebc2ec482013-08-14 20:54:38 +08002553 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002554 }
2555
2556 return 0;
2557}
2558
2559static void rtl8152_get_drvinfo(struct net_device *netdev,
2560 struct ethtool_drvinfo *info)
2561{
2562 struct r8152 *tp = netdev_priv(netdev);
2563
2564 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2565 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2566 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2567}
2568
2569static
2570int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2571{
2572 struct r8152 *tp = netdev_priv(netdev);
2573
2574 if (!tp->mii.mdio_read)
2575 return -EOPNOTSUPP;
2576
2577 return mii_ethtool_gset(&tp->mii, cmd);
2578}
2579
2580static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2581{
2582 struct r8152 *tp = netdev_priv(dev);
2583
2584 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2585}
2586
2587static struct ethtool_ops ops = {
2588 .get_drvinfo = rtl8152_get_drvinfo,
2589 .get_settings = rtl8152_get_settings,
2590 .set_settings = rtl8152_set_settings,
2591 .get_link = ethtool_op_get_link,
2592};
2593
2594static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2595{
2596 struct r8152 *tp = netdev_priv(netdev);
2597 struct mii_ioctl_data *data = if_mii(rq);
2598 int res = 0;
2599
2600 switch (cmd) {
2601 case SIOCGMIIPHY:
2602 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2603 break;
2604
2605 case SIOCGMIIREG:
2606 data->val_out = r8152_mdio_read(tp, data->reg_num);
2607 break;
2608
2609 case SIOCSMIIREG:
2610 if (!capable(CAP_NET_ADMIN)) {
2611 res = -EPERM;
2612 break;
2613 }
2614 r8152_mdio_write(tp, data->reg_num, data->val_in);
2615 break;
2616
2617 default:
2618 res = -EOPNOTSUPP;
2619 }
2620
2621 return res;
2622}
2623
2624static const struct net_device_ops rtl8152_netdev_ops = {
2625 .ndo_open = rtl8152_open,
2626 .ndo_stop = rtl8152_close,
2627 .ndo_do_ioctl = rtl8152_ioctl,
2628 .ndo_start_xmit = rtl8152_start_xmit,
2629 .ndo_tx_timeout = rtl8152_tx_timeout,
2630 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2631 .ndo_set_mac_address = rtl8152_set_mac_address,
2632
2633 .ndo_change_mtu = eth_change_mtu,
2634 .ndo_validate_addr = eth_validate_addr,
2635};
2636
2637static void r8152b_get_version(struct r8152 *tp)
2638{
2639 u32 ocp_data;
2640 u16 version;
2641
2642 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2643 version = (u16)(ocp_data & VERSION_MASK);
2644
2645 switch (version) {
2646 case 0x4c00:
2647 tp->version = RTL_VER_01;
2648 break;
2649 case 0x4c10:
2650 tp->version = RTL_VER_02;
2651 break;
hayeswang43779f82014-01-02 11:25:10 +08002652 case 0x5c00:
2653 tp->version = RTL_VER_03;
2654 tp->mii.supports_gmii = 1;
2655 break;
2656 case 0x5c10:
2657 tp->version = RTL_VER_04;
2658 tp->mii.supports_gmii = 1;
2659 break;
2660 case 0x5c20:
2661 tp->version = RTL_VER_05;
2662 tp->mii.supports_gmii = 1;
2663 break;
hayeswangac718b62013-05-02 16:01:25 +00002664 default:
2665 netif_info(tp, probe, tp->netdev,
2666 "Unknown version 0x%04x\n", version);
2667 break;
2668 }
2669}
2670
hayeswange3fe0b12014-01-02 11:22:39 +08002671static void rtl8152_unload(struct r8152 *tp)
2672{
2673 u32 ocp_data;
2674
2675 if (tp->version != RTL_VER_01) {
2676 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2677 ocp_data |= POWER_CUT;
2678 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2679 }
2680
2681 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
hayeswang8e1f51b2014-01-02 11:22:41 +08002682 ocp_data &= ~RESUME_INDICATE;
hayeswange3fe0b12014-01-02 11:22:39 +08002683 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2684}
2685
hayeswang43779f82014-01-02 11:25:10 +08002686static void rtl8153_unload(struct r8152 *tp)
2687{
2688 r8153_power_cut_en(tp, 1);
2689}
2690
hayeswang31ca1de2014-01-06 17:08:43 +08002691static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08002692{
2693 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08002694 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08002695
2696 switch (id->idVendor) {
2697 case VENDOR_ID_REALTEK:
2698 switch (id->idProduct) {
2699 case PRODUCT_ID_RTL8152:
2700 ops->init = r8152b_init;
2701 ops->enable = rtl8152_enable;
2702 ops->disable = rtl8152_disable;
2703 ops->down = rtl8152_down;
2704 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002705 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08002706 break;
hayeswang43779f82014-01-02 11:25:10 +08002707 case PRODUCT_ID_RTL8153:
2708 ops->init = r8153_init;
2709 ops->enable = rtl8153_enable;
2710 ops->disable = rtl8152_disable;
2711 ops->down = rtl8153_down;
2712 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002713 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002714 break;
2715 default:
hayeswang43779f82014-01-02 11:25:10 +08002716 break;
2717 }
2718 break;
2719
2720 case VENDOR_ID_SAMSUNG:
2721 switch (id->idProduct) {
2722 case PRODUCT_ID_SAMSUNG:
2723 ops->init = r8153_init;
2724 ops->enable = rtl8153_enable;
2725 ops->disable = rtl8152_disable;
2726 ops->down = rtl8153_down;
2727 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002728 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002729 break;
hayeswangc81229c2014-01-02 11:22:42 +08002730 default:
hayeswangc81229c2014-01-02 11:22:42 +08002731 break;
2732 }
2733 break;
2734
2735 default:
hayeswangc81229c2014-01-02 11:22:42 +08002736 break;
2737 }
2738
hayeswang31ca1de2014-01-06 17:08:43 +08002739 if (ret)
2740 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
2741
hayeswangc81229c2014-01-02 11:22:42 +08002742 return ret;
2743}
2744
hayeswangac718b62013-05-02 16:01:25 +00002745static int rtl8152_probe(struct usb_interface *intf,
2746 const struct usb_device_id *id)
2747{
2748 struct usb_device *udev = interface_to_usbdev(intf);
2749 struct r8152 *tp;
2750 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08002751 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002752
2753 if (udev->actconfig->desc.bConfigurationValue != 1) {
2754 usb_driver_set_configuration(udev, 1);
2755 return -ENODEV;
2756 }
2757
2758 netdev = alloc_etherdev(sizeof(struct r8152));
2759 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002760 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00002761 return -ENOMEM;
2762 }
2763
hayeswangebc2ec482013-08-14 20:54:38 +08002764 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00002765 tp = netdev_priv(netdev);
2766 tp->msg_enable = 0x7FFF;
2767
hayeswange3ad4122014-01-06 17:08:42 +08002768 tp->udev = udev;
2769 tp->netdev = netdev;
2770 tp->intf = intf;
2771
hayeswang31ca1de2014-01-06 17:08:43 +08002772 ret = rtl_ops_init(tp, id);
2773 if (ret)
2774 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08002775
hayeswangebc2ec482013-08-14 20:54:38 +08002776 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00002777 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2778
hayeswangac718b62013-05-02 16:01:25 +00002779 netdev->netdev_ops = &rtl8152_netdev_ops;
2780 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08002781
2782 netdev->features |= NETIF_F_IP_CSUM;
2783 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00002784 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00002785
2786 tp->mii.dev = netdev;
2787 tp->mii.mdio_read = read_mii_word;
2788 tp->mii.mdio_write = write_mii_word;
2789 tp->mii.phy_id_mask = 0x3f;
2790 tp->mii.reg_num_mask = 0x1f;
2791 tp->mii.phy_id = R8152_PHY_ID;
2792 tp->mii.supports_gmii = 0;
2793
2794 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08002795 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002796 set_ethernet_addr(tp);
2797
hayeswangebc2ec482013-08-14 20:54:38 +08002798 ret = alloc_all_mem(tp);
2799 if (ret)
hayeswangac718b62013-05-02 16:01:25 +00002800 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002801
2802 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00002803
hayeswangebc2ec482013-08-14 20:54:38 +08002804 ret = register_netdev(netdev);
2805 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002806 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08002807 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00002808 }
2809
Hayes Wang4a8deae2014-01-07 11:18:22 +08002810 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00002811
2812 return 0;
2813
hayeswangac718b62013-05-02 16:01:25 +00002814out1:
hayeswangebc2ec482013-08-14 20:54:38 +08002815 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00002816out:
2817 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002818 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002819}
2820
hayeswangac718b62013-05-02 16:01:25 +00002821static void rtl8152_disconnect(struct usb_interface *intf)
2822{
2823 struct r8152 *tp = usb_get_intfdata(intf);
2824
2825 usb_set_intfdata(intf, NULL);
2826 if (tp) {
2827 set_bit(RTL8152_UNPLUG, &tp->flags);
2828 tasklet_kill(&tp->tl);
2829 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08002830 tp->rtl_ops.unload(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002831 free_all_mem(tp);
hayeswangac718b62013-05-02 16:01:25 +00002832 free_netdev(tp->netdev);
2833 }
2834}
2835
2836/* table of devices that work with this driver */
2837static struct usb_device_id rtl8152_table[] = {
2838 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
hayeswang43779f82014-01-02 11:25:10 +08002839 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
2840 {USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00002841 {}
2842};
2843
2844MODULE_DEVICE_TABLE(usb, rtl8152_table);
2845
2846static struct usb_driver rtl8152_driver = {
2847 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08002848 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00002849 .probe = rtl8152_probe,
2850 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00002851 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08002852 .resume = rtl8152_resume,
2853 .reset_resume = rtl8152_resume,
hayeswangac718b62013-05-02 16:01:25 +00002854};
2855
Sachin Kamatb4236daa2013-05-16 17:48:08 +00002856module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00002857
2858MODULE_AUTHOR(DRIVER_AUTHOR);
2859MODULE_DESCRIPTION(DRIVER_DESC);
2860MODULE_LICENSE("GPL");