blob: 3847c3507624d494dc9eba0a07360421b1437c9b [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
hayeswangc7de7de2014-01-15 10:42:16 +08002 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 *
8 */
9
hayeswangac718b62013-05-02 16:01:25 +000010#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
16#include <linux/ethtool.h>
17#include <linux/usb.h>
18#include <linux/crc32.h>
19#include <linux/if_vlan.h>
20#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080021#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080022#include <linux/ip.h>
23#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000024
25/* Version Information */
hayeswangc7de7de2014-01-15 10:42:16 +080026#define DRIVER_VERSION "v1.04.0 (2014/01/15)"
hayeswangac718b62013-05-02 16:01:25 +000027#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080028#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000029#define MODULENAME "r8152"
30
31#define R8152_PHY_ID 32
32
33#define PLA_IDR 0xc000
34#define PLA_RCR 0xc010
35#define PLA_RMS 0xc016
36#define PLA_RXFIFO_CTRL0 0xc0a0
37#define PLA_RXFIFO_CTRL1 0xc0a4
38#define PLA_RXFIFO_CTRL2 0xc0a8
39#define PLA_FMC 0xc0b4
40#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080041#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000042#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080043#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000044#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080045#define PLA_TEREDO_TIMER 0xd2cc
46#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_LEDSEL 0xdd90
48#define PLA_LED_FEATURE 0xdd92
49#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_GPHY_INTR_IMR 0xe022
52#define PLA_EEE_CR 0xe040
53#define PLA_EEEP_CR 0xe080
54#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_MAC_PWR_CTRL2 0xe0ca
56#define PLA_MAC_PWR_CTRL3 0xe0cc
57#define PLA_MAC_PWR_CTRL4 0xe0ce
58#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000059#define PLA_TCR0 0xe610
60#define PLA_TCR1 0xe612
61#define PLA_TXFIFO_CTRL 0xe618
62#define PLA_RSTTELLY 0xe800
63#define PLA_CR 0xe813
64#define PLA_CRWECR 0xe81c
65#define PLA_CONFIG5 0xe822
66#define PLA_PHY_PWR 0xe84c
67#define PLA_OOB_CTRL 0xe84f
68#define PLA_CPCR 0xe854
69#define PLA_MISC_0 0xe858
70#define PLA_MISC_1 0xe85a
71#define PLA_OCP_GPHY_BASE 0xe86c
72#define PLA_TELLYCNT 0xe890
73#define PLA_SFF_STS_7 0xe8de
74#define PLA_PHYSTATUS 0xe908
75#define PLA_BP_BA 0xfc26
76#define PLA_BP_0 0xfc28
77#define PLA_BP_1 0xfc2a
78#define PLA_BP_2 0xfc2c
79#define PLA_BP_3 0xfc2e
80#define PLA_BP_4 0xfc30
81#define PLA_BP_5 0xfc32
82#define PLA_BP_6 0xfc34
83#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080084#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000085
hayeswang43779f82014-01-02 11:25:10 +080086#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000087#define USB_DEV_STAT 0xb808
88#define USB_USB_CTRL 0xd406
89#define USB_PHY_CTRL 0xd408
90#define USB_TX_AGG 0xd40a
91#define USB_RX_BUF_TH 0xd40c
92#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +080093#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +000094#define USB_PM_CTRL_STATUS 0xd432
95#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +080096#define USB_TOLERANCE 0xd490
97#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +000098#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +080099#define USB_MISC_0 0xd81a
100#define USB_POWER_CUT 0xd80a
101#define USB_AFE_CTRL2 0xd824
102#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000103#define USB_BP_BA 0xfc26
104#define USB_BP_0 0xfc28
105#define USB_BP_1 0xfc2a
106#define USB_BP_2 0xfc2c
107#define USB_BP_3 0xfc2e
108#define USB_BP_4 0xfc30
109#define USB_BP_5 0xfc32
110#define USB_BP_6 0xfc34
111#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800112#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000113
114/* OCP Registers */
115#define OCP_ALDPS_CONFIG 0x2010
116#define OCP_EEE_CONFIG1 0x2080
117#define OCP_EEE_CONFIG2 0x2092
118#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800119#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000120#define OCP_EEE_AR 0xa41a
121#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800122#define OCP_PHY_STATUS 0xa420
123#define OCP_POWER_CFG 0xa430
124#define OCP_EEE_CFG 0xa432
125#define OCP_SRAM_ADDR 0xa436
126#define OCP_SRAM_DATA 0xa438
127#define OCP_DOWN_SPEED 0xa442
128#define OCP_EEE_CFG2 0xa5d0
129#define OCP_ADC_CFG 0xbc06
130
131/* SRAM Register */
132#define SRAM_LPF_CFG 0x8012
133#define SRAM_10M_AMP1 0x8080
134#define SRAM_10M_AMP2 0x8082
135#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000136
137/* PLA_RCR */
138#define RCR_AAP 0x00000001
139#define RCR_APM 0x00000002
140#define RCR_AM 0x00000004
141#define RCR_AB 0x00000008
142#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
143
144/* PLA_RXFIFO_CTRL0 */
145#define RXFIFO_THR1_NORMAL 0x00080002
146#define RXFIFO_THR1_OOB 0x01800003
147
148/* PLA_RXFIFO_CTRL1 */
149#define RXFIFO_THR2_FULL 0x00000060
150#define RXFIFO_THR2_HIGH 0x00000038
151#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800152#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000153
154/* PLA_RXFIFO_CTRL2 */
155#define RXFIFO_THR3_FULL 0x00000078
156#define RXFIFO_THR3_HIGH 0x00000048
157#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800158#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000159
160/* PLA_TXFIFO_CTRL */
161#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800162#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000163
164/* PLA_FMC */
165#define FMC_FCR_MCU_EN 0x0001
166
167/* PLA_EEEP_CR */
168#define EEEP_CR_EEEP_TX 0x0002
169
hayeswang43779f82014-01-02 11:25:10 +0800170/* PLA_WDT6_CTRL */
171#define WDT6_SET_MODE 0x0010
172
hayeswangac718b62013-05-02 16:01:25 +0000173/* PLA_TCR0 */
174#define TCR0_TX_EMPTY 0x0800
175#define TCR0_AUTO_FIFO 0x0080
176
177/* PLA_TCR1 */
178#define VERSION_MASK 0x7cf0
179
180/* PLA_CR */
181#define CR_RST 0x10
182#define CR_RE 0x08
183#define CR_TE 0x04
184
185/* PLA_CRWECR */
186#define CRWECR_NORAML 0x00
187#define CRWECR_CONFIG 0xc0
188
189/* PLA_OOB_CTRL */
190#define NOW_IS_OOB 0x80
191#define TXFIFO_EMPTY 0x20
192#define RXFIFO_EMPTY 0x10
193#define LINK_LIST_READY 0x02
194#define DIS_MCU_CLROOB 0x01
195#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
196
197/* PLA_MISC_1 */
198#define RXDY_GATED_EN 0x0008
199
200/* PLA_SFF_STS_7 */
201#define RE_INIT_LL 0x8000
202#define MCU_BORW_EN 0x4000
203
204/* PLA_CPCR */
205#define CPCR_RX_VLAN 0x0040
206
207/* PLA_CFG_WOL */
208#define MAGIC_EN 0x0001
209
hayeswang43779f82014-01-02 11:25:10 +0800210/* PLA_TEREDO_CFG */
211#define TEREDO_SEL 0x8000
212#define TEREDO_WAKE_MASK 0x7f00
213#define TEREDO_RS_EVENT_MASK 0x00fe
214#define OOB_TEREDO_EN 0x0001
215
hayeswangac718b62013-05-02 16:01:25 +0000216/* PAL_BDC_CR */
217#define ALDPS_PROXY_MODE 0x0001
218
219/* PLA_CONFIG5 */
220#define LAN_WAKE_EN 0x0002
221
222/* PLA_LED_FEATURE */
223#define LED_MODE_MASK 0x0700
224
225/* PLA_PHY_PWR */
226#define TX_10M_IDLE_EN 0x0080
227#define PFM_PWM_SWITCH 0x0040
228
229/* PLA_MAC_PWR_CTRL */
230#define D3_CLK_GATED_EN 0x00004000
231#define MCU_CLK_RATIO 0x07010f07
232#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800233#define ALDPS_SPDWN_RATIO 0x0f87
234
235/* PLA_MAC_PWR_CTRL2 */
236#define EEE_SPDWN_RATIO 0x8007
237
238/* PLA_MAC_PWR_CTRL3 */
239#define PKT_AVAIL_SPDWN_EN 0x0100
240#define SUSPEND_SPDWN_EN 0x0004
241#define U1U2_SPDWN_EN 0x0002
242#define L1_SPDWN_EN 0x0001
243
244/* PLA_MAC_PWR_CTRL4 */
245#define PWRSAVE_SPDWN_EN 0x1000
246#define RXDV_SPDWN_EN 0x0800
247#define TX10MIDLE_EN 0x0100
248#define TP100_SPDWN_EN 0x0020
249#define TP500_SPDWN_EN 0x0010
250#define TP1000_SPDWN_EN 0x0008
251#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000252
253/* PLA_GPHY_INTR_IMR */
254#define GPHY_STS_MSK 0x0001
255#define SPEED_DOWN_MSK 0x0002
256#define SPDWN_RXDV_MSK 0x0004
257#define SPDWN_LINKCHG_MSK 0x0008
258
259/* PLA_PHYAR */
260#define PHYAR_FLAG 0x80000000
261
262/* PLA_EEE_CR */
263#define EEE_RX_EN 0x0001
264#define EEE_TX_EN 0x0002
265
hayeswang43779f82014-01-02 11:25:10 +0800266/* PLA_BOOT_CTRL */
267#define AUTOLOAD_DONE 0x0002
268
hayeswangac718b62013-05-02 16:01:25 +0000269/* USB_DEV_STAT */
270#define STAT_SPEED_MASK 0x0006
271#define STAT_SPEED_HIGH 0x0000
272#define STAT_SPEED_FULL 0x0001
273
274/* USB_TX_AGG */
275#define TX_AGG_MAX_THRESHOLD 0x03
276
277/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800278#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800279#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800280#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000281
282/* USB_TX_DMA */
283#define TEST_MODE_DISABLE 0x00000001
284#define TX_SIZE_ADJUST1 0x00000100
285
286/* USB_UPS_CTRL */
287#define POWER_CUT 0x0100
288
289/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800290#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000291
292/* USB_USB_CTRL */
293#define RX_AGG_DISABLE 0x0010
294
hayeswang43779f82014-01-02 11:25:10 +0800295/* USB_U2P3_CTRL */
296#define U2P3_ENABLE 0x0001
297
298/* USB_POWER_CUT */
299#define PWR_EN 0x0001
300#define PHASE2_EN 0x0008
301
302/* USB_MISC_0 */
303#define PCUT_STATUS 0x0001
304
305/* USB_RX_EARLY_AGG */
306#define EARLY_AGG_SUPPER 0x0e832981
307#define EARLY_AGG_HIGH 0x0e837a12
308#define EARLY_AGG_SLOW 0x0e83ffff
309
310/* USB_WDT11_CTRL */
311#define TIMER11_EN 0x0001
312
313/* USB_LPM_CTRL */
314#define LPM_TIMER_MASK 0x0c
315#define LPM_TIMER_500MS 0x04 /* 500 ms */
316#define LPM_TIMER_500US 0x0c /* 500 us */
317
318/* USB_AFE_CTRL2 */
319#define SEN_VAL_MASK 0xf800
320#define SEN_VAL_NORMAL 0xa000
321#define SEL_RXIDLE 0x0100
322
hayeswangac718b62013-05-02 16:01:25 +0000323/* OCP_ALDPS_CONFIG */
324#define ENPWRSAVE 0x8000
325#define ENPDNPS 0x0200
326#define LINKENA 0x0100
327#define DIS_SDSAVE 0x0010
328
hayeswang43779f82014-01-02 11:25:10 +0800329/* OCP_PHY_STATUS */
330#define PHY_STAT_MASK 0x0007
331#define PHY_STAT_LAN_ON 3
332#define PHY_STAT_PWRDN 5
333
334/* OCP_POWER_CFG */
335#define EEE_CLKDIV_EN 0x8000
336#define EN_ALDPS 0x0004
337#define EN_10M_PLLOFF 0x0001
338
hayeswangac718b62013-05-02 16:01:25 +0000339/* OCP_EEE_CONFIG1 */
340#define RG_TXLPI_MSK_HFDUP 0x8000
341#define RG_MATCLR_EN 0x4000
342#define EEE_10_CAP 0x2000
343#define EEE_NWAY_EN 0x1000
344#define TX_QUIET_EN 0x0200
345#define RX_QUIET_EN 0x0100
346#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
347#define RG_RXLPI_MSK_HFDUP 0x0008
348#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
349
350/* OCP_EEE_CONFIG2 */
351#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
352#define RG_DACQUIET_EN 0x0400
353#define RG_LDVQUIET_EN 0x0200
354#define RG_CKRSEL 0x0020
355#define RG_EEEPRG_EN 0x0010
356
357/* OCP_EEE_CONFIG3 */
358#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
359#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
360#define MSK_PH 0x0006 /* bit 0 ~ 3 */
361
362/* OCP_EEE_AR */
363/* bit[15:14] function */
364#define FUN_ADDR 0x0000
365#define FUN_DATA 0x4000
366/* bit[4:0] device addr */
367#define DEVICE_ADDR 0x0007
368
369/* OCP_EEE_DATA */
370#define EEE_ADDR 0x003C
371#define EEE_DATA 0x0002
372
hayeswang43779f82014-01-02 11:25:10 +0800373/* OCP_EEE_CFG */
374#define CTAP_SHORT_EN 0x0040
375#define EEE10_EN 0x0010
376
377/* OCP_DOWN_SPEED */
378#define EN_10M_BGOFF 0x0080
379
380/* OCP_EEE_CFG2 */
381#define MY1000_EEE 0x0004
382#define MY100_EEE 0x0002
383
384/* OCP_ADC_CFG */
385#define CKADSEL_L 0x0100
386#define ADC_EN 0x0080
387#define EN_EMI_L 0x0040
388
389/* SRAM_LPF_CFG */
390#define LPF_AUTO_TUNE 0x8000
391
392/* SRAM_10M_AMP1 */
393#define GDAC_IB_UPALL 0x0008
394
395/* SRAM_10M_AMP2 */
396#define AMP_DN 0x0200
397
398/* SRAM_IMPEDANCE */
399#define RX_DRIVING_MASK 0x6000
400
hayeswangac718b62013-05-02 16:01:25 +0000401enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800402 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000403 _100bps = 0x08,
404 _10bps = 0x04,
405 LINK_STATUS = 0x02,
406 FULL_DUP = 0x01,
407};
408
hayeswangebc2ec482013-08-14 20:54:38 +0800409#define RTL8152_MAX_TX 10
410#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800411#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800412#define CRC_SIZE 4
413#define TX_ALIGN 4
414#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800415
416#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800417
hayeswangac718b62013-05-02 16:01:25 +0000418#define RTL8152_REQT_READ 0xc0
419#define RTL8152_REQT_WRITE 0x40
420#define RTL8152_REQ_GET_REGS 0x05
421#define RTL8152_REQ_SET_REGS 0x05
422
423#define BYTE_EN_DWORD 0xff
424#define BYTE_EN_WORD 0x33
425#define BYTE_EN_BYTE 0x11
426#define BYTE_EN_SIX_BYTES 0x3f
427#define BYTE_EN_START_MASK 0x0f
428#define BYTE_EN_END_MASK 0xf0
429
430#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
431#define RTL8152_TX_TIMEOUT (HZ)
432
433/* rtl8152 flags */
434enum rtl8152_flags {
435 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000436 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800437 WORK_ENABLE,
438 RTL8152_LINK_CHG,
hayeswangac718b62013-05-02 16:01:25 +0000439};
440
441/* Define these values to match your device */
442#define VENDOR_ID_REALTEK 0x0bda
443#define PRODUCT_ID_RTL8152 0x8152
hayeswang43779f82014-01-02 11:25:10 +0800444#define PRODUCT_ID_RTL8153 0x8153
445
446#define VENDOR_ID_SAMSUNG 0x04e8
447#define PRODUCT_ID_SAMSUNG 0xa101
hayeswangac718b62013-05-02 16:01:25 +0000448
449#define MCU_TYPE_PLA 0x0100
450#define MCU_TYPE_USB 0x0000
451
hayeswangc7de7de2014-01-15 10:42:16 +0800452#define REALTEK_USB_DEVICE(vend, prod) \
453 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
454
hayeswangac718b62013-05-02 16:01:25 +0000455struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800456 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000457#define RX_LEN_MASK 0x7fff
hayeswang500b6d72013-11-20 17:30:57 +0800458 __le32 opts2;
459 __le32 opts3;
460 __le32 opts4;
461 __le32 opts5;
462 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000463};
464
465struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800466 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000467#define TX_FS (1 << 31) /* First segment of a packet */
468#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800469#define TX_LEN_MASK 0x3ffff
470
hayeswang500b6d72013-11-20 17:30:57 +0800471 __le32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800472#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
473#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
474#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
475#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000476};
477
hayeswangdff4e8a2013-08-16 16:09:33 +0800478struct r8152;
479
hayeswangebc2ec482013-08-14 20:54:38 +0800480struct rx_agg {
481 struct list_head list;
482 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800483 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800484 void *buffer;
485 void *head;
486};
487
488struct tx_agg {
489 struct list_head list;
490 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800491 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800492 void *buffer;
493 void *head;
494 u32 skb_num;
495 u32 skb_len;
496};
497
hayeswangac718b62013-05-02 16:01:25 +0000498struct r8152 {
499 unsigned long flags;
500 struct usb_device *udev;
501 struct tasklet_struct tl;
hayeswang40a82912013-08-14 20:54:40 +0800502 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000503 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800504 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800505 struct tx_agg tx_info[RTL8152_MAX_TX];
506 struct rx_agg rx_info[RTL8152_MAX_RX];
507 struct list_head rx_done, tx_free;
508 struct sk_buff_head tx_queue;
509 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000510 struct delayed_work schedule;
511 struct mii_if_info mii;
hayeswangc81229c2014-01-02 11:22:42 +0800512
513 struct rtl_ops {
514 void (*init)(struct r8152 *);
515 int (*enable)(struct r8152 *);
516 void (*disable)(struct r8152 *);
517 void (*down)(struct r8152 *);
518 void (*unload)(struct r8152 *);
519 } rtl_ops;
520
hayeswang40a82912013-08-14 20:54:40 +0800521 int intr_interval;
hayeswangac718b62013-05-02 16:01:25 +0000522 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800523 u32 tx_qlen;
hayeswangac718b62013-05-02 16:01:25 +0000524 u16 ocp_base;
hayeswang40a82912013-08-14 20:54:40 +0800525 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000526 u8 version;
527 u8 speed;
528};
529
530enum rtl_version {
531 RTL_VER_UNKNOWN = 0,
532 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800533 RTL_VER_02,
534 RTL_VER_03,
535 RTL_VER_04,
536 RTL_VER_05,
537 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000538};
539
540/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
541 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
542 */
543static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800544static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000545
546static
547int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
548{
hayeswang31787f52013-07-31 17:21:25 +0800549 int ret;
550 void *tmp;
551
552 tmp = kmalloc(size, GFP_KERNEL);
553 if (!tmp)
554 return -ENOMEM;
555
556 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000557 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800558 value, index, tmp, size, 500);
559
560 memcpy(data, tmp, size);
561 kfree(tmp);
562
563 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000564}
565
566static
567int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
568{
hayeswang31787f52013-07-31 17:21:25 +0800569 int ret;
570 void *tmp;
571
572 tmp = kmalloc(size, GFP_KERNEL);
573 if (!tmp)
574 return -ENOMEM;
575
576 memcpy(tmp, data, size);
577
578 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000579 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800580 value, index, tmp, size, 500);
581
582 kfree(tmp);
583 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000584}
585
586static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
587 void *data, u16 type)
588{
hayeswang45f4a192014-01-06 17:08:41 +0800589 u16 limit = 64;
590 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000591
592 if (test_bit(RTL8152_UNPLUG, &tp->flags))
593 return -ENODEV;
594
595 /* both size and indix must be 4 bytes align */
596 if ((size & 3) || !size || (index & 3) || !data)
597 return -EPERM;
598
599 if ((u32)index + (u32)size > 0xffff)
600 return -EPERM;
601
602 while (size) {
603 if (size > limit) {
604 ret = get_registers(tp, index, type, limit, data);
605 if (ret < 0)
606 break;
607
608 index += limit;
609 data += limit;
610 size -= limit;
611 } else {
612 ret = get_registers(tp, index, type, size, data);
613 if (ret < 0)
614 break;
615
616 index += size;
617 data += size;
618 size = 0;
619 break;
620 }
621 }
622
623 return ret;
624}
625
626static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
627 u16 size, void *data, u16 type)
628{
hayeswang45f4a192014-01-06 17:08:41 +0800629 int ret;
630 u16 byteen_start, byteen_end, byen;
631 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000632
633 if (test_bit(RTL8152_UNPLUG, &tp->flags))
634 return -ENODEV;
635
636 /* both size and indix must be 4 bytes align */
637 if ((size & 3) || !size || (index & 3) || !data)
638 return -EPERM;
639
640 if ((u32)index + (u32)size > 0xffff)
641 return -EPERM;
642
643 byteen_start = byteen & BYTE_EN_START_MASK;
644 byteen_end = byteen & BYTE_EN_END_MASK;
645
646 byen = byteen_start | (byteen_start << 4);
647 ret = set_registers(tp, index, type | byen, 4, data);
648 if (ret < 0)
649 goto error1;
650
651 index += 4;
652 data += 4;
653 size -= 4;
654
655 if (size) {
656 size -= 4;
657
658 while (size) {
659 if (size > limit) {
660 ret = set_registers(tp, index,
661 type | BYTE_EN_DWORD,
662 limit, data);
663 if (ret < 0)
664 goto error1;
665
666 index += limit;
667 data += limit;
668 size -= limit;
669 } else {
670 ret = set_registers(tp, index,
671 type | BYTE_EN_DWORD,
672 size, data);
673 if (ret < 0)
674 goto error1;
675
676 index += size;
677 data += size;
678 size = 0;
679 break;
680 }
681 }
682
683 byen = byteen_end | (byteen_end >> 4);
684 ret = set_registers(tp, index, type | byen, 4, data);
685 if (ret < 0)
686 goto error1;
687 }
688
689error1:
690 return ret;
691}
692
693static inline
694int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
695{
696 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
697}
698
699static inline
700int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
701{
702 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
703}
704
705static inline
706int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
707{
708 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
709}
710
711static inline
712int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
713{
714 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
715}
716
717static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
718{
hayeswangc8826de2013-07-31 17:21:26 +0800719 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000720
hayeswangc8826de2013-07-31 17:21:26 +0800721 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000722
723 return __le32_to_cpu(data);
724}
725
726static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
727{
hayeswangc8826de2013-07-31 17:21:26 +0800728 __le32 tmp = __cpu_to_le32(data);
729
730 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000731}
732
733static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
734{
735 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800736 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000737 u8 shift = index & 2;
738
739 index &= ~3;
740
hayeswangc8826de2013-07-31 17:21:26 +0800741 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000742
hayeswangc8826de2013-07-31 17:21:26 +0800743 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000744 data >>= (shift * 8);
745 data &= 0xffff;
746
747 return (u16)data;
748}
749
750static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
751{
hayeswangc8826de2013-07-31 17:21:26 +0800752 u32 mask = 0xffff;
753 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000754 u16 byen = BYTE_EN_WORD;
755 u8 shift = index & 2;
756
757 data &= mask;
758
759 if (index & 2) {
760 byen <<= shift;
761 mask <<= (shift * 8);
762 data <<= (shift * 8);
763 index &= ~3;
764 }
765
hayeswangc8826de2013-07-31 17:21:26 +0800766 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000767
hayeswangc8826de2013-07-31 17:21:26 +0800768 data |= __le32_to_cpu(tmp) & ~mask;
769 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000770
hayeswangc8826de2013-07-31 17:21:26 +0800771 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000772}
773
774static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
775{
776 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800777 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000778 u8 shift = index & 3;
779
780 index &= ~3;
781
hayeswangc8826de2013-07-31 17:21:26 +0800782 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000783
hayeswangc8826de2013-07-31 17:21:26 +0800784 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000785 data >>= (shift * 8);
786 data &= 0xff;
787
788 return (u8)data;
789}
790
791static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
792{
hayeswangc8826de2013-07-31 17:21:26 +0800793 u32 mask = 0xff;
794 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000795 u16 byen = BYTE_EN_BYTE;
796 u8 shift = index & 3;
797
798 data &= mask;
799
800 if (index & 3) {
801 byen <<= shift;
802 mask <<= (shift * 8);
803 data <<= (shift * 8);
804 index &= ~3;
805 }
806
hayeswangc8826de2013-07-31 17:21:26 +0800807 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000808
hayeswangc8826de2013-07-31 17:21:26 +0800809 data |= __le32_to_cpu(tmp) & ~mask;
810 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000811
hayeswangc8826de2013-07-31 17:21:26 +0800812 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000813}
814
hayeswangac244d32014-01-02 11:22:40 +0800815static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
816{
817 u16 ocp_base, ocp_index;
818
819 ocp_base = addr & 0xf000;
820 if (ocp_base != tp->ocp_base) {
821 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
822 tp->ocp_base = ocp_base;
823 }
824
825 ocp_index = (addr & 0x0fff) | 0xb000;
826 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
827}
828
hayeswange3fe0b12014-01-02 11:22:39 +0800829static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
830{
831 u16 ocp_base, ocp_index;
832
833 ocp_base = addr & 0xf000;
834 if (ocp_base != tp->ocp_base) {
835 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
836 tp->ocp_base = ocp_base;
837 }
838
839 ocp_index = (addr & 0x0fff) | 0xb000;
840 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
841}
842
hayeswangac244d32014-01-02 11:22:40 +0800843static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +0000844{
hayeswangac244d32014-01-02 11:22:40 +0800845 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +0000846}
847
hayeswangac244d32014-01-02 11:22:40 +0800848static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +0000849{
hayeswangac244d32014-01-02 11:22:40 +0800850 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +0000851}
852
hayeswang43779f82014-01-02 11:25:10 +0800853static void sram_write(struct r8152 *tp, u16 addr, u16 data)
854{
855 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
856 ocp_reg_write(tp, OCP_SRAM_DATA, data);
857}
858
859static u16 sram_read(struct r8152 *tp, u16 addr)
860{
861 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
862 return ocp_reg_read(tp, OCP_SRAM_DATA);
863}
864
hayeswangac718b62013-05-02 16:01:25 +0000865static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
866{
867 struct r8152 *tp = netdev_priv(netdev);
868
869 if (phy_id != R8152_PHY_ID)
870 return -EINVAL;
871
872 return r8152_mdio_read(tp, reg);
873}
874
875static
876void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
877{
878 struct r8152 *tp = netdev_priv(netdev);
879
880 if (phy_id != R8152_PHY_ID)
881 return;
882
883 r8152_mdio_write(tp, reg, val);
884}
885
hayeswangebc2ec482013-08-14 20:54:38 +0800886static
887int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
888
hayeswangac718b62013-05-02 16:01:25 +0000889static inline void set_ethernet_addr(struct r8152 *tp)
890{
891 struct net_device *dev = tp->netdev;
hayeswang8a91c822014-02-18 21:49:01 +0800892 int ret;
hayeswang31787f52013-07-31 17:21:25 +0800893 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000894
hayeswang8a91c822014-02-18 21:49:01 +0800895 if (tp->version == RTL_VER_01)
896 ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
897 else
898 ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
899
900 if (ret < 0) {
hayeswangac718b62013-05-02 16:01:25 +0000901 netif_notice(tp, probe, dev, "inet addr fail\n");
hayeswang8a91c822014-02-18 21:49:01 +0800902 } else {
903 if (tp->version != RTL_VER_01) {
904 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
905 CRWECR_CONFIG);
906 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
907 sizeof(node_id), node_id);
908 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
909 CRWECR_NORAML);
910 }
911
hayeswangac718b62013-05-02 16:01:25 +0000912 memcpy(dev->dev_addr, node_id, dev->addr_len);
913 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
914 }
hayeswangac718b62013-05-02 16:01:25 +0000915}
916
917static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
918{
919 struct r8152 *tp = netdev_priv(netdev);
920 struct sockaddr *addr = p;
921
922 if (!is_valid_ether_addr(addr->sa_data))
923 return -EADDRNOTAVAIL;
924
925 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
926
927 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
928 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
929 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
930
931 return 0;
932}
933
hayeswangac718b62013-05-02 16:01:25 +0000934static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
935{
936 return &dev->stats;
937}
938
939static void read_bulk_callback(struct urb *urb)
940{
hayeswangac718b62013-05-02 16:01:25 +0000941 struct net_device *netdev;
hayeswanga5a4f462013-08-16 16:09:34 +0800942 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +0000943 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800944 struct rx_agg *agg;
945 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000946 int result;
hayeswangac718b62013-05-02 16:01:25 +0000947
hayeswangebc2ec482013-08-14 20:54:38 +0800948 agg = urb->context;
949 if (!agg)
950 return;
951
952 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000953 if (!tp)
954 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800955
hayeswangac718b62013-05-02 16:01:25 +0000956 if (test_bit(RTL8152_UNPLUG, &tp->flags))
957 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800958
959 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000960 return;
961
hayeswangebc2ec482013-08-14 20:54:38 +0800962 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +0800963
964 /* When link down, the driver would cancel all bulks. */
965 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +0800966 if (!netif_carrier_ok(netdev))
967 return;
968
hayeswangac718b62013-05-02 16:01:25 +0000969 switch (status) {
970 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +0800971 if (urb->actual_length < ETH_ZLEN)
972 break;
973
hayeswanga5a4f462013-08-16 16:09:34 +0800974 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800975 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800976 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +0800977 tasklet_schedule(&tp->tl);
978 return;
hayeswangac718b62013-05-02 16:01:25 +0000979 case -ESHUTDOWN:
980 set_bit(RTL8152_UNPLUG, &tp->flags);
981 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +0800982 return;
hayeswangac718b62013-05-02 16:01:25 +0000983 case -ENOENT:
984 return; /* the urb is in unlink state */
985 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800986 if (net_ratelimit())
987 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +0800988 break;
hayeswangac718b62013-05-02 16:01:25 +0000989 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +0800990 if (net_ratelimit())
991 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +0800992 break;
hayeswangac718b62013-05-02 16:01:25 +0000993 }
994
hayeswangebc2ec482013-08-14 20:54:38 +0800995 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +0000996 if (result == -ENODEV) {
997 netif_device_detach(tp->netdev);
998 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +0800999 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001000 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +08001001 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001002 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001003 }
hayeswangac718b62013-05-02 16:01:25 +00001004}
1005
1006static void write_bulk_callback(struct urb *urb)
1007{
hayeswangebc2ec482013-08-14 20:54:38 +08001008 struct net_device_stats *stats;
hayeswanga5a4f462013-08-16 16:09:34 +08001009 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001010 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001011 struct r8152 *tp;
1012 int status = urb->status;
1013
hayeswangebc2ec482013-08-14 20:54:38 +08001014 agg = urb->context;
1015 if (!agg)
1016 return;
1017
1018 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001019 if (!tp)
1020 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001021
1022 stats = rtl8152_get_stats(tp->netdev);
1023 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001024 if (net_ratelimit())
1025 netdev_warn(tp->netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001026 stats->tx_errors += agg->skb_num;
1027 } else {
1028 stats->tx_packets += agg->skb_num;
1029 stats->tx_bytes += agg->skb_len;
1030 }
1031
hayeswanga5a4f462013-08-16 16:09:34 +08001032 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001033 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +08001034 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001035
1036 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +00001037 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001038
1039 if (!test_bit(WORK_ENABLE, &tp->flags))
1040 return;
1041
1042 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1043 return;
1044
1045 if (!skb_queue_empty(&tp->tx_queue))
1046 tasklet_schedule(&tp->tl);
1047}
1048
hayeswang40a82912013-08-14 20:54:40 +08001049static void intr_callback(struct urb *urb)
1050{
1051 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001052 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001053 int status = urb->status;
1054 int res;
1055
1056 tp = urb->context;
1057 if (!tp)
1058 return;
1059
1060 if (!test_bit(WORK_ENABLE, &tp->flags))
1061 return;
1062
1063 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1064 return;
1065
1066 switch (status) {
1067 case 0: /* success */
1068 break;
1069 case -ECONNRESET: /* unlink */
1070 case -ESHUTDOWN:
1071 netif_device_detach(tp->netdev);
1072 case -ENOENT:
1073 return;
1074 case -EOVERFLOW:
1075 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1076 goto resubmit;
1077 /* -EPIPE: should clear the halt */
1078 default:
1079 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1080 goto resubmit;
1081 }
1082
1083 d = urb->transfer_buffer;
1084 if (INTR_LINK & __le16_to_cpu(d[0])) {
1085 if (!(tp->speed & LINK_STATUS)) {
1086 set_bit(RTL8152_LINK_CHG, &tp->flags);
1087 schedule_delayed_work(&tp->schedule, 0);
1088 }
1089 } else {
1090 if (tp->speed & LINK_STATUS) {
1091 set_bit(RTL8152_LINK_CHG, &tp->flags);
1092 schedule_delayed_work(&tp->schedule, 0);
1093 }
1094 }
1095
1096resubmit:
1097 res = usb_submit_urb(urb, GFP_ATOMIC);
1098 if (res == -ENODEV)
1099 netif_device_detach(tp->netdev);
1100 else if (res)
1101 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001102 "can't resubmit intr, status %d\n", res);
hayeswang40a82912013-08-14 20:54:40 +08001103}
1104
hayeswangebc2ec482013-08-14 20:54:38 +08001105static inline void *rx_agg_align(void *data)
1106{
hayeswang8e1f51b2014-01-02 11:22:41 +08001107 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001108}
1109
1110static inline void *tx_agg_align(void *data)
1111{
hayeswang8e1f51b2014-01-02 11:22:41 +08001112 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001113}
1114
1115static void free_all_mem(struct r8152 *tp)
1116{
1117 int i;
1118
1119 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001120 usb_free_urb(tp->rx_info[i].urb);
1121 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001122
hayeswang9629e3c2014-01-15 10:42:15 +08001123 kfree(tp->rx_info[i].buffer);
1124 tp->rx_info[i].buffer = NULL;
1125 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001126 }
1127
1128 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001129 usb_free_urb(tp->tx_info[i].urb);
1130 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001131
hayeswang9629e3c2014-01-15 10:42:15 +08001132 kfree(tp->tx_info[i].buffer);
1133 tp->tx_info[i].buffer = NULL;
1134 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001135 }
hayeswang40a82912013-08-14 20:54:40 +08001136
hayeswang9629e3c2014-01-15 10:42:15 +08001137 usb_free_urb(tp->intr_urb);
1138 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001139
hayeswang9629e3c2014-01-15 10:42:15 +08001140 kfree(tp->intr_buff);
1141 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001142}
1143
1144static int alloc_all_mem(struct r8152 *tp)
1145{
1146 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001147 struct usb_interface *intf = tp->intf;
1148 struct usb_host_interface *alt = intf->cur_altsetting;
1149 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001150 struct urb *urb;
1151 int node, i;
1152 u8 *buf;
1153
1154 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1155
1156 spin_lock_init(&tp->rx_lock);
1157 spin_lock_init(&tp->tx_lock);
1158 INIT_LIST_HEAD(&tp->rx_done);
1159 INIT_LIST_HEAD(&tp->tx_free);
1160 skb_queue_head_init(&tp->tx_queue);
1161
1162 for (i = 0; i < RTL8152_MAX_RX; i++) {
1163 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1164 if (!buf)
1165 goto err1;
1166
1167 if (buf != rx_agg_align(buf)) {
1168 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001169 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1170 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001171 if (!buf)
1172 goto err1;
1173 }
1174
1175 urb = usb_alloc_urb(0, GFP_KERNEL);
1176 if (!urb) {
1177 kfree(buf);
1178 goto err1;
1179 }
1180
1181 INIT_LIST_HEAD(&tp->rx_info[i].list);
1182 tp->rx_info[i].context = tp;
1183 tp->rx_info[i].urb = urb;
1184 tp->rx_info[i].buffer = buf;
1185 tp->rx_info[i].head = rx_agg_align(buf);
1186 }
1187
1188 for (i = 0; i < RTL8152_MAX_TX; i++) {
1189 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1190 if (!buf)
1191 goto err1;
1192
1193 if (buf != tx_agg_align(buf)) {
1194 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001195 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1196 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001197 if (!buf)
1198 goto err1;
1199 }
1200
1201 urb = usb_alloc_urb(0, GFP_KERNEL);
1202 if (!urb) {
1203 kfree(buf);
1204 goto err1;
1205 }
1206
1207 INIT_LIST_HEAD(&tp->tx_info[i].list);
1208 tp->tx_info[i].context = tp;
1209 tp->tx_info[i].urb = urb;
1210 tp->tx_info[i].buffer = buf;
1211 tp->tx_info[i].head = tx_agg_align(buf);
1212
1213 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1214 }
1215
hayeswang40a82912013-08-14 20:54:40 +08001216 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1217 if (!tp->intr_urb)
1218 goto err1;
1219
1220 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1221 if (!tp->intr_buff)
1222 goto err1;
1223
1224 tp->intr_interval = (int)ep_intr->desc.bInterval;
1225 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1226 tp->intr_buff, INTBUFSIZE, intr_callback,
1227 tp, tp->intr_interval);
1228
hayeswangebc2ec482013-08-14 20:54:38 +08001229 return 0;
1230
1231err1:
1232 free_all_mem(tp);
1233 return -ENOMEM;
1234}
1235
hayeswang0de98f62013-08-16 16:09:35 +08001236static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1237{
1238 struct tx_agg *agg = NULL;
1239 unsigned long flags;
1240
1241 spin_lock_irqsave(&tp->tx_lock, flags);
1242 if (!list_empty(&tp->tx_free)) {
1243 struct list_head *cursor;
1244
1245 cursor = tp->tx_free.next;
1246 list_del_init(cursor);
1247 agg = list_entry(cursor, struct tx_agg, list);
1248 }
1249 spin_unlock_irqrestore(&tp->tx_lock, flags);
1250
1251 return agg;
1252}
1253
hayeswang5bd23882013-08-14 20:54:39 +08001254static void
1255r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1256{
1257 memset(desc, 0, sizeof(*desc));
1258
1259 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1260
1261 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1262 __be16 protocol;
1263 u8 ip_protocol;
1264 u32 opts2 = 0;
1265
1266 if (skb->protocol == htons(ETH_P_8021Q))
1267 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1268 else
1269 protocol = skb->protocol;
1270
1271 switch (protocol) {
1272 case htons(ETH_P_IP):
1273 opts2 |= IPV4_CS;
1274 ip_protocol = ip_hdr(skb)->protocol;
1275 break;
1276
1277 case htons(ETH_P_IPV6):
1278 opts2 |= IPV6_CS;
1279 ip_protocol = ipv6_hdr(skb)->nexthdr;
1280 break;
1281
1282 default:
1283 ip_protocol = IPPROTO_RAW;
1284 break;
1285 }
1286
1287 if (ip_protocol == IPPROTO_TCP) {
1288 opts2 |= TCP_CS;
1289 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1290 } else if (ip_protocol == IPPROTO_UDP) {
1291 opts2 |= UDP_CS;
1292 } else {
1293 WARN_ON_ONCE(1);
1294 }
1295
1296 desc->opts2 = cpu_to_le32(opts2);
1297 }
1298}
1299
hayeswangb1379d92013-08-16 16:09:37 +08001300static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1301{
hayeswang7937f9e2013-11-20 17:30:54 +08001302 int remain;
hayeswangb1379d92013-08-16 16:09:37 +08001303 u8 *tx_data;
1304
1305 tx_data = agg->head;
1306 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001307 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001308
hayeswang7937f9e2013-11-20 17:30:54 +08001309 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001310 struct tx_desc *tx_desc;
1311 struct sk_buff *skb;
1312 unsigned int len;
1313
1314 skb = skb_dequeue(&tp->tx_queue);
1315 if (!skb)
1316 break;
1317
hayeswang7937f9e2013-11-20 17:30:54 +08001318 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001319 len = skb->len;
1320 if (remain < len) {
1321 skb_queue_head(&tp->tx_queue, skb);
1322 break;
1323 }
1324
hayeswang7937f9e2013-11-20 17:30:54 +08001325 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001326 tx_desc = (struct tx_desc *)tx_data;
1327 tx_data += sizeof(*tx_desc);
1328
1329 r8152_tx_csum(tp, tx_desc, skb);
1330 memcpy(tx_data, skb->data, len);
1331 agg->skb_num++;
1332 agg->skb_len += len;
1333 dev_kfree_skb_any(skb);
1334
hayeswang7937f9e2013-11-20 17:30:54 +08001335 tx_data += len;
1336 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001337 }
1338
hayeswangdd1b1192013-11-20 17:30:56 +08001339 netif_tx_lock(tp->netdev);
1340
1341 if (netif_queue_stopped(tp->netdev) &&
1342 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1343 netif_wake_queue(tp->netdev);
1344
1345 netif_tx_unlock(tp->netdev);
1346
hayeswangb1379d92013-08-16 16:09:37 +08001347 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1348 agg->head, (int)(tx_data - (u8 *)agg->head),
1349 (usb_complete_t)write_bulk_callback, agg);
1350
1351 return usb_submit_urb(agg->urb, GFP_ATOMIC);
1352}
1353
hayeswangebc2ec482013-08-14 20:54:38 +08001354static void rx_bottom(struct r8152 *tp)
1355{
hayeswanga5a4f462013-08-16 16:09:34 +08001356 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001357 struct list_head *cursor, *next;
hayeswangebc2ec482013-08-14 20:54:38 +08001358
hayeswanga5a4f462013-08-16 16:09:34 +08001359 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001360 list_for_each_safe(cursor, next, &tp->rx_done) {
hayeswang43a44782013-08-16 16:09:36 +08001361 struct rx_desc *rx_desc;
1362 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001363 int len_used = 0;
1364 struct urb *urb;
1365 u8 *rx_data;
1366 int ret;
1367
hayeswangebc2ec482013-08-14 20:54:38 +08001368 list_del_init(cursor);
hayeswanga5a4f462013-08-16 16:09:34 +08001369 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001370
1371 agg = list_entry(cursor, struct rx_agg, list);
1372 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001373 if (urb->actual_length < ETH_ZLEN)
1374 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001375
hayeswangebc2ec482013-08-14 20:54:38 +08001376 rx_desc = agg->head;
1377 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001378 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001379
hayeswang7937f9e2013-11-20 17:30:54 +08001380 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001381 struct net_device *netdev = tp->netdev;
1382 struct net_device_stats *stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001383 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001384 struct sk_buff *skb;
1385
hayeswang7937f9e2013-11-20 17:30:54 +08001386 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001387 if (pkt_len < ETH_ZLEN)
1388 break;
1389
hayeswang7937f9e2013-11-20 17:30:54 +08001390 len_used += pkt_len;
1391 if (urb->actual_length < len_used)
1392 break;
1393
hayeswang43a44782013-08-16 16:09:36 +08001394 stats = rtl8152_get_stats(netdev);
1395
hayeswang8e1f51b2014-01-02 11:22:41 +08001396 pkt_len -= CRC_SIZE;
hayeswangebc2ec482013-08-14 20:54:38 +08001397 rx_data += sizeof(struct rx_desc);
1398
1399 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1400 if (!skb) {
1401 stats->rx_dropped++;
1402 break;
1403 }
1404 memcpy(skb->data, rx_data, pkt_len);
1405 skb_put(skb, pkt_len);
1406 skb->protocol = eth_type_trans(skb, netdev);
1407 netif_rx(skb);
1408 stats->rx_packets++;
1409 stats->rx_bytes += pkt_len;
1410
hayeswang8e1f51b2014-01-02 11:22:41 +08001411 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec482013-08-14 20:54:38 +08001412 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001413 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001414 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001415 }
1416
hayeswang0de98f62013-08-16 16:09:35 +08001417submit:
hayeswangebc2ec482013-08-14 20:54:38 +08001418 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswanga5a4f462013-08-16 16:09:34 +08001419 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001420 if (ret && ret != -ENODEV) {
1421 list_add_tail(&agg->list, next);
1422 tasklet_schedule(&tp->tl);
1423 }
1424 }
hayeswanga5a4f462013-08-16 16:09:34 +08001425 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001426}
1427
1428static void tx_bottom(struct r8152 *tp)
1429{
hayeswangebc2ec482013-08-14 20:54:38 +08001430 int res;
1431
hayeswangb1379d92013-08-16 16:09:37 +08001432 do {
1433 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08001434
hayeswangb1379d92013-08-16 16:09:37 +08001435 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08001436 break;
1437
hayeswangb1379d92013-08-16 16:09:37 +08001438 agg = r8152_get_tx_agg(tp);
1439 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08001440 break;
hayeswangb1379d92013-08-16 16:09:37 +08001441
1442 res = r8152_tx_agg_fill(tp, agg);
1443 if (res) {
1444 struct net_device_stats *stats;
1445 struct net_device *netdev;
1446 unsigned long flags;
1447
1448 netdev = tp->netdev;
1449 stats = rtl8152_get_stats(netdev);
1450
1451 if (res == -ENODEV) {
1452 netif_device_detach(netdev);
1453 } else {
1454 netif_warn(tp, tx_err, netdev,
1455 "failed tx_urb %d\n", res);
1456 stats->tx_dropped += agg->skb_num;
1457 spin_lock_irqsave(&tp->tx_lock, flags);
1458 list_add_tail(&agg->list, &tp->tx_free);
1459 spin_unlock_irqrestore(&tp->tx_lock, flags);
1460 }
hayeswangebc2ec482013-08-14 20:54:38 +08001461 }
hayeswangb1379d92013-08-16 16:09:37 +08001462 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001463}
1464
1465static void bottom_half(unsigned long data)
1466{
1467 struct r8152 *tp;
1468
1469 tp = (struct r8152 *)data;
1470
1471 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1472 return;
1473
1474 if (!test_bit(WORK_ENABLE, &tp->flags))
1475 return;
1476
hayeswang7559fb2f2013-08-16 16:09:38 +08001477 /* When link down, the driver would cancel all bulks. */
1478 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001479 if (!netif_carrier_ok(tp->netdev))
1480 return;
1481
1482 rx_bottom(tp);
1483 tx_bottom(tp);
1484}
1485
1486static
1487int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1488{
1489 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1490 agg->head, rx_buf_sz,
1491 (usb_complete_t)read_bulk_callback, agg);
1492
1493 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001494}
1495
hayeswang00a5e362014-02-18 21:48:59 +08001496static void rtl_drop_queued_tx(struct r8152 *tp)
1497{
1498 struct net_device_stats *stats = &tp->netdev->stats;
1499 struct sk_buff *skb;
1500
1501 while ((skb = skb_dequeue(&tp->tx_queue))) {
1502 dev_kfree_skb(skb);
1503 stats->tx_dropped++;
1504 }
1505}
1506
hayeswangac718b62013-05-02 16:01:25 +00001507static void rtl8152_tx_timeout(struct net_device *netdev)
1508{
1509 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001510 int i;
1511
Hayes Wang4a8deae2014-01-07 11:18:22 +08001512 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001513 for (i = 0; i < RTL8152_MAX_TX; i++)
1514 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001515}
1516
1517static void rtl8152_set_rx_mode(struct net_device *netdev)
1518{
1519 struct r8152 *tp = netdev_priv(netdev);
1520
hayeswang40a82912013-08-14 20:54:40 +08001521 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001522 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001523 schedule_delayed_work(&tp->schedule, 0);
1524 }
hayeswangac718b62013-05-02 16:01:25 +00001525}
1526
1527static void _rtl8152_set_rx_mode(struct net_device *netdev)
1528{
1529 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001530 u32 mc_filter[2]; /* Multicast hash filter */
1531 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001532 u32 ocp_data;
1533
hayeswangac718b62013-05-02 16:01:25 +00001534 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1535 netif_stop_queue(netdev);
1536 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1537 ocp_data &= ~RCR_ACPT_ALL;
1538 ocp_data |= RCR_AB | RCR_APM;
1539
1540 if (netdev->flags & IFF_PROMISC) {
1541 /* Unconditionally log net taps. */
1542 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1543 ocp_data |= RCR_AM | RCR_AAP;
1544 mc_filter[1] = mc_filter[0] = 0xffffffff;
1545 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1546 (netdev->flags & IFF_ALLMULTI)) {
1547 /* Too many to filter perfectly -- accept all multicasts. */
1548 ocp_data |= RCR_AM;
1549 mc_filter[1] = mc_filter[0] = 0xffffffff;
1550 } else {
1551 struct netdev_hw_addr *ha;
1552
1553 mc_filter[1] = mc_filter[0] = 0;
1554 netdev_for_each_mc_addr(ha, netdev) {
1555 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1556 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1557 ocp_data |= RCR_AM;
1558 }
1559 }
1560
hayeswang31787f52013-07-31 17:21:25 +08001561 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1562 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001563
hayeswang31787f52013-07-31 17:21:25 +08001564 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001565 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1566 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001567}
1568
1569static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1570 struct net_device *netdev)
1571{
1572 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001573
hayeswangac718b62013-05-02 16:01:25 +00001574 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001575
hayeswang61598782013-11-20 17:30:55 +08001576 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001577
hayeswangdd1b1192013-11-20 17:30:56 +08001578 if (list_empty(&tp->tx_free) &&
1579 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1580 netif_stop_queue(netdev);
1581
hayeswang61598782013-11-20 17:30:55 +08001582 if (!list_empty(&tp->tx_free))
1583 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001584
1585 return NETDEV_TX_OK;
1586}
1587
1588static void r8152b_reset_packet_filter(struct r8152 *tp)
1589{
1590 u32 ocp_data;
1591
1592 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1593 ocp_data &= ~FMC_FCR_MCU_EN;
1594 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1595 ocp_data |= FMC_FCR_MCU_EN;
1596 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1597}
1598
1599static void rtl8152_nic_reset(struct r8152 *tp)
1600{
1601 int i;
1602
1603 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1604
1605 for (i = 0; i < 1000; i++) {
1606 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1607 break;
1608 udelay(100);
1609 }
1610}
1611
hayeswangdd1b1192013-11-20 17:30:56 +08001612static void set_tx_qlen(struct r8152 *tp)
1613{
1614 struct net_device *netdev = tp->netdev;
1615
1616 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1617 sizeof(struct tx_desc));
1618}
1619
hayeswangac718b62013-05-02 16:01:25 +00001620static inline u8 rtl8152_get_speed(struct r8152 *tp)
1621{
1622 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1623}
1624
hayeswang507605a2014-01-02 11:22:43 +08001625static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001626{
hayeswangebc2ec482013-08-14 20:54:38 +08001627 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001628 u8 speed;
1629
1630 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001631 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001632 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001633 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001634 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1635 } else {
1636 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001637 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001638 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1639 }
hayeswang507605a2014-01-02 11:22:43 +08001640}
1641
hayeswang00a5e362014-02-18 21:48:59 +08001642static void rxdy_gated_en(struct r8152 *tp, bool enable)
1643{
1644 u32 ocp_data;
1645
1646 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1647 if (enable)
1648 ocp_data |= RXDY_GATED_EN;
1649 else
1650 ocp_data &= ~RXDY_GATED_EN;
1651 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1652}
1653
hayeswang507605a2014-01-02 11:22:43 +08001654static int rtl_enable(struct r8152 *tp)
1655{
1656 u32 ocp_data;
1657 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001658
1659 r8152b_reset_packet_filter(tp);
1660
1661 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1662 ocp_data |= CR_RE | CR_TE;
1663 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1664
hayeswang00a5e362014-02-18 21:48:59 +08001665 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001666
hayeswangebc2ec482013-08-14 20:54:38 +08001667 INIT_LIST_HEAD(&tp->rx_done);
1668 ret = 0;
1669 for (i = 0; i < RTL8152_MAX_RX; i++) {
1670 INIT_LIST_HEAD(&tp->rx_info[i].list);
1671 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1672 }
hayeswangac718b62013-05-02 16:01:25 +00001673
hayeswangebc2ec482013-08-14 20:54:38 +08001674 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001675}
1676
hayeswang507605a2014-01-02 11:22:43 +08001677static int rtl8152_enable(struct r8152 *tp)
1678{
1679 set_tx_qlen(tp);
1680 rtl_set_eee_plus(tp);
1681
1682 return rtl_enable(tp);
1683}
1684
hayeswang43779f82014-01-02 11:25:10 +08001685static void r8153_set_rx_agg(struct r8152 *tp)
1686{
1687 u8 speed;
1688
1689 speed = rtl8152_get_speed(tp);
1690 if (speed & _1000bps) {
1691 if (tp->udev->speed == USB_SPEED_SUPER) {
1692 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1693 RX_THR_SUPPER);
1694 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1695 EARLY_AGG_SUPPER);
1696 } else {
1697 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1698 RX_THR_HIGH);
1699 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1700 EARLY_AGG_HIGH);
1701 }
1702 } else {
1703 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1704 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1705 EARLY_AGG_SLOW);
1706 }
1707}
1708
1709static int rtl8153_enable(struct r8152 *tp)
1710{
1711 set_tx_qlen(tp);
1712 rtl_set_eee_plus(tp);
1713 r8153_set_rx_agg(tp);
1714
1715 return rtl_enable(tp);
1716}
1717
hayeswangac718b62013-05-02 16:01:25 +00001718static void rtl8152_disable(struct r8152 *tp)
1719{
hayeswangebc2ec482013-08-14 20:54:38 +08001720 u32 ocp_data;
1721 int i;
hayeswangac718b62013-05-02 16:01:25 +00001722
1723 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1724 ocp_data &= ~RCR_ACPT_ALL;
1725 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1726
hayeswang00a5e362014-02-18 21:48:59 +08001727 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001728
1729 for (i = 0; i < RTL8152_MAX_TX; i++)
1730 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001731
hayeswang00a5e362014-02-18 21:48:59 +08001732 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001733
1734 for (i = 0; i < 1000; i++) {
1735 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1736 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1737 break;
1738 mdelay(1);
1739 }
1740
1741 for (i = 0; i < 1000; i++) {
1742 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1743 break;
1744 mdelay(1);
1745 }
1746
hayeswangebc2ec482013-08-14 20:54:38 +08001747 for (i = 0; i < RTL8152_MAX_RX; i++)
1748 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001749
1750 rtl8152_nic_reset(tp);
1751}
1752
hayeswang00a5e362014-02-18 21:48:59 +08001753static void r8152_power_cut_en(struct r8152 *tp, bool enable)
1754{
1755 u32 ocp_data;
1756
1757 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1758 if (enable)
1759 ocp_data |= POWER_CUT;
1760 else
1761 ocp_data &= ~POWER_CUT;
1762 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1763
1764 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1765 ocp_data &= ~RESUME_INDICATE;
1766 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1767
1768}
1769
hayeswang43499682014-02-18 21:48:58 +08001770static void rtl_clear_bp(struct r8152 *tp)
1771{
1772 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1773 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1774 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1775 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1776 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1777 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1778 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1779 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1780 mdelay(3);
1781 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1782 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1783}
1784
1785static void r8153_clear_bp(struct r8152 *tp)
1786{
1787 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
1788 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
1789 rtl_clear_bp(tp);
1790}
1791
1792static void r8153_teredo_off(struct r8152 *tp)
1793{
1794 u32 ocp_data;
1795
1796 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1797 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1798 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1799
1800 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1801 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1802 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1803}
1804
1805static void r8152b_disable_aldps(struct r8152 *tp)
1806{
1807 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1808 msleep(20);
1809}
1810
1811static inline void r8152b_enable_aldps(struct r8152 *tp)
1812{
1813 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1814 LINKENA | DIS_SDSAVE);
1815}
1816
1817static void r8152b_hw_phy_cfg(struct r8152 *tp)
1818{
1819 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1820 r8152b_disable_aldps(tp);
1821}
1822
hayeswangac718b62013-05-02 16:01:25 +00001823static void r8152b_exit_oob(struct r8152 *tp)
1824{
1825 u32 ocp_data;
1826 int i;
1827
1828 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1829 ocp_data &= ~RCR_ACPT_ALL;
1830 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1831
hayeswang00a5e362014-02-18 21:48:59 +08001832 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00001833
1834 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1835 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1836
1837 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1838 ocp_data &= ~NOW_IS_OOB;
1839 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1840
1841 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1842 ocp_data &= ~MCU_BORW_EN;
1843 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1844
1845 for (i = 0; i < 1000; i++) {
1846 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1847 if (ocp_data & LINK_LIST_READY)
1848 break;
1849 mdelay(1);
1850 }
1851
1852 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1853 ocp_data |= RE_INIT_LL;
1854 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1855
1856 for (i = 0; i < 1000; i++) {
1857 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1858 if (ocp_data & LINK_LIST_READY)
1859 break;
1860 mdelay(1);
1861 }
1862
1863 rtl8152_nic_reset(tp);
1864
1865 /* rx share fifo credit full threshold */
1866 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1867
1868 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1869 ocp_data &= STAT_SPEED_MASK;
1870 if (ocp_data == STAT_SPEED_FULL) {
1871 /* rx share fifo credit near full threshold */
1872 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1873 RXFIFO_THR2_FULL);
1874 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1875 RXFIFO_THR3_FULL);
1876 } else {
1877 /* rx share fifo credit near full threshold */
1878 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1879 RXFIFO_THR2_HIGH);
1880 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1881 RXFIFO_THR3_HIGH);
1882 }
1883
1884 /* TX share fifo free credit full threshold */
1885 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1886
1887 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08001888 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00001889 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1890 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1891
1892 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1893 ocp_data &= ~CPCR_RX_VLAN;
1894 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1895
1896 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1897
1898 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1899 ocp_data |= TCR0_AUTO_FIFO;
1900 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1901}
1902
1903static void r8152b_enter_oob(struct r8152 *tp)
1904{
hayeswang45f4a192014-01-06 17:08:41 +08001905 u32 ocp_data;
1906 int i;
hayeswangac718b62013-05-02 16:01:25 +00001907
1908 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1909 ocp_data &= ~NOW_IS_OOB;
1910 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1911
1912 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1913 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1914 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1915
1916 rtl8152_disable(tp);
1917
1918 for (i = 0; i < 1000; i++) {
1919 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1920 if (ocp_data & LINK_LIST_READY)
1921 break;
1922 mdelay(1);
1923 }
1924
1925 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1926 ocp_data |= RE_INIT_LL;
1927 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1928
1929 for (i = 0; i < 1000; i++) {
1930 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1931 if (ocp_data & LINK_LIST_READY)
1932 break;
1933 mdelay(1);
1934 }
1935
1936 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1937
1938 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1939 ocp_data |= MAGIC_EN;
1940 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1941
1942 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1943 ocp_data |= CPCR_RX_VLAN;
1944 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1945
1946 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1947 ocp_data |= ALDPS_PROXY_MODE;
1948 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1949
1950 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1951 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1952 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1953
1954 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1955
hayeswang00a5e362014-02-18 21:48:59 +08001956 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00001957
1958 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1959 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1960 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1961}
1962
hayeswang43779f82014-01-02 11:25:10 +08001963static void r8153_hw_phy_cfg(struct r8152 *tp)
1964{
1965 u32 ocp_data;
1966 u16 data;
1967
1968 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
1969 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1970
1971 if (tp->version == RTL_VER_03) {
1972 data = ocp_reg_read(tp, OCP_EEE_CFG);
1973 data &= ~CTAP_SHORT_EN;
1974 ocp_reg_write(tp, OCP_EEE_CFG, data);
1975 }
1976
1977 data = ocp_reg_read(tp, OCP_POWER_CFG);
1978 data |= EEE_CLKDIV_EN;
1979 ocp_reg_write(tp, OCP_POWER_CFG, data);
1980
1981 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
1982 data |= EN_10M_BGOFF;
1983 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
1984 data = ocp_reg_read(tp, OCP_POWER_CFG);
1985 data |= EN_10M_PLLOFF;
1986 ocp_reg_write(tp, OCP_POWER_CFG, data);
1987 data = sram_read(tp, SRAM_IMPEDANCE);
1988 data &= ~RX_DRIVING_MASK;
1989 sram_write(tp, SRAM_IMPEDANCE, data);
1990
1991 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1992 ocp_data |= PFM_PWM_SWITCH;
1993 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1994
1995 data = sram_read(tp, SRAM_LPF_CFG);
1996 data |= LPF_AUTO_TUNE;
1997 sram_write(tp, SRAM_LPF_CFG, data);
1998
1999 data = sram_read(tp, SRAM_10M_AMP1);
2000 data |= GDAC_IB_UPALL;
2001 sram_write(tp, SRAM_10M_AMP1, data);
2002 data = sram_read(tp, SRAM_10M_AMP2);
2003 data |= AMP_DN;
2004 sram_write(tp, SRAM_10M_AMP2, data);
2005}
2006
hayeswangb9702722014-02-18 21:49:00 +08002007static void r8153_u1u2en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002008{
2009 u8 u1u2[8];
2010
2011 if (enable)
2012 memset(u1u2, 0xff, sizeof(u1u2));
2013 else
2014 memset(u1u2, 0x00, sizeof(u1u2));
2015
2016 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2017}
2018
hayeswangb9702722014-02-18 21:49:00 +08002019static void r8153_u2p3en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002020{
2021 u32 ocp_data;
2022
2023 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2024 if (enable)
2025 ocp_data |= U2P3_ENABLE;
2026 else
2027 ocp_data &= ~U2P3_ENABLE;
2028 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2029}
2030
hayeswangb9702722014-02-18 21:49:00 +08002031static void r8153_power_cut_en(struct r8152 *tp, bool enable)
hayeswang43779f82014-01-02 11:25:10 +08002032{
2033 u32 ocp_data;
2034
2035 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2036 if (enable)
2037 ocp_data |= PWR_EN | PHASE2_EN;
2038 else
2039 ocp_data &= ~(PWR_EN | PHASE2_EN);
2040 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2041
2042 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2043 ocp_data &= ~PCUT_STATUS;
2044 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2045}
2046
hayeswang43779f82014-01-02 11:25:10 +08002047static void r8153_first_init(struct r8152 *tp)
2048{
2049 u32 ocp_data;
2050 int i;
2051
hayeswang00a5e362014-02-18 21:48:59 +08002052 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002053 r8153_teredo_off(tp);
2054
2055 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2056 ocp_data &= ~RCR_ACPT_ALL;
2057 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2058
2059 r8153_hw_phy_cfg(tp);
2060
2061 rtl8152_nic_reset(tp);
2062
2063 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2064 ocp_data &= ~NOW_IS_OOB;
2065 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2066
2067 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2068 ocp_data &= ~MCU_BORW_EN;
2069 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2070
2071 for (i = 0; i < 1000; i++) {
2072 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2073 if (ocp_data & LINK_LIST_READY)
2074 break;
2075 mdelay(1);
2076 }
2077
2078 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2079 ocp_data |= RE_INIT_LL;
2080 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2081
2082 for (i = 0; i < 1000; i++) {
2083 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2084 if (ocp_data & LINK_LIST_READY)
2085 break;
2086 mdelay(1);
2087 }
2088
2089 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2090 ocp_data &= ~CPCR_RX_VLAN;
2091 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2092
2093 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2094
2095 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2096 ocp_data |= TCR0_AUTO_FIFO;
2097 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2098
2099 rtl8152_nic_reset(tp);
2100
2101 /* rx share fifo credit full threshold */
2102 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2103 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2104 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2105 /* TX share fifo free credit full threshold */
2106 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2107
hayeswang9629e3c2014-01-15 10:42:15 +08002108 /* rx aggregation */
hayeswang43779f82014-01-02 11:25:10 +08002109 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2110 ocp_data &= ~RX_AGG_DISABLE;
2111 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2112}
2113
2114static void r8153_enter_oob(struct r8152 *tp)
2115{
2116 u32 ocp_data;
2117 int i;
2118
2119 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2120 ocp_data &= ~NOW_IS_OOB;
2121 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2122
2123 rtl8152_disable(tp);
2124
2125 for (i = 0; i < 1000; i++) {
2126 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2127 if (ocp_data & LINK_LIST_READY)
2128 break;
2129 mdelay(1);
2130 }
2131
2132 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2133 ocp_data |= RE_INIT_LL;
2134 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2135
2136 for (i = 0; i < 1000; i++) {
2137 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2138 if (ocp_data & LINK_LIST_READY)
2139 break;
2140 mdelay(1);
2141 }
2142
2143 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2144
2145 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2146 ocp_data |= MAGIC_EN;
2147 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2148
2149 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2150 ocp_data &= ~TEREDO_WAKE_MASK;
2151 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2152
2153 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2154 ocp_data |= CPCR_RX_VLAN;
2155 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2156
2157 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2158 ocp_data |= ALDPS_PROXY_MODE;
2159 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2160
2161 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2162 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2163 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2164
2165 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
2166
hayeswang00a5e362014-02-18 21:48:59 +08002167 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002168
2169 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2170 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2171 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2172}
2173
2174static void r8153_disable_aldps(struct r8152 *tp)
2175{
2176 u16 data;
2177
2178 data = ocp_reg_read(tp, OCP_POWER_CFG);
2179 data &= ~EN_ALDPS;
2180 ocp_reg_write(tp, OCP_POWER_CFG, data);
2181 msleep(20);
2182}
2183
2184static void r8153_enable_aldps(struct r8152 *tp)
2185{
2186 u16 data;
2187
2188 data = ocp_reg_read(tp, OCP_POWER_CFG);
2189 data |= EN_ALDPS;
2190 ocp_reg_write(tp, OCP_POWER_CFG, data);
2191}
2192
hayeswangac718b62013-05-02 16:01:25 +00002193static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2194{
hayeswang43779f82014-01-02 11:25:10 +08002195 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002196 int ret = 0;
2197
2198 cancel_delayed_work_sync(&tp->schedule);
2199 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2200 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2201 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002202 if (tp->mii.supports_gmii) {
2203 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2204 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2205 } else {
2206 gbcr = 0;
2207 }
hayeswangac718b62013-05-02 16:01:25 +00002208
2209 if (autoneg == AUTONEG_DISABLE) {
2210 if (speed == SPEED_10) {
2211 bmcr = 0;
2212 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2213 } else if (speed == SPEED_100) {
2214 bmcr = BMCR_SPEED100;
2215 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002216 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2217 bmcr = BMCR_SPEED1000;
2218 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002219 } else {
2220 ret = -EINVAL;
2221 goto out;
2222 }
2223
2224 if (duplex == DUPLEX_FULL)
2225 bmcr |= BMCR_FULLDPLX;
2226 } else {
2227 if (speed == SPEED_10) {
2228 if (duplex == DUPLEX_FULL)
2229 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2230 else
2231 anar |= ADVERTISE_10HALF;
2232 } else if (speed == SPEED_100) {
2233 if (duplex == DUPLEX_FULL) {
2234 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2235 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2236 } else {
2237 anar |= ADVERTISE_10HALF;
2238 anar |= ADVERTISE_100HALF;
2239 }
hayeswang43779f82014-01-02 11:25:10 +08002240 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2241 if (duplex == DUPLEX_FULL) {
2242 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2243 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2244 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2245 } else {
2246 anar |= ADVERTISE_10HALF;
2247 anar |= ADVERTISE_100HALF;
2248 gbcr |= ADVERTISE_1000HALF;
2249 }
hayeswangac718b62013-05-02 16:01:25 +00002250 } else {
2251 ret = -EINVAL;
2252 goto out;
2253 }
2254
2255 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2256 }
2257
hayeswang43779f82014-01-02 11:25:10 +08002258 if (tp->mii.supports_gmii)
2259 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2260
hayeswangac718b62013-05-02 16:01:25 +00002261 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2262 r8152_mdio_write(tp, MII_BMCR, bmcr);
2263
2264out:
hayeswangac718b62013-05-02 16:01:25 +00002265
2266 return ret;
2267}
2268
2269static void rtl8152_down(struct r8152 *tp)
2270{
hayeswang00a5e362014-02-18 21:48:59 +08002271 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002272 r8152b_disable_aldps(tp);
2273 r8152b_enter_oob(tp);
2274 r8152b_enable_aldps(tp);
2275}
2276
hayeswang43779f82014-01-02 11:25:10 +08002277static void rtl8153_down(struct r8152 *tp)
2278{
hayeswangb9702722014-02-18 21:49:00 +08002279 r8153_u1u2en(tp, false);
2280 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002281 r8153_disable_aldps(tp);
2282 r8153_enter_oob(tp);
2283 r8153_enable_aldps(tp);
2284}
2285
hayeswangac718b62013-05-02 16:01:25 +00002286static void set_carrier(struct r8152 *tp)
2287{
2288 struct net_device *netdev = tp->netdev;
2289 u8 speed;
2290
hayeswang40a82912013-08-14 20:54:40 +08002291 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002292 speed = rtl8152_get_speed(tp);
2293
2294 if (speed & LINK_STATUS) {
2295 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002296 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002297 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2298 netif_carrier_on(netdev);
2299 }
2300 } else {
2301 if (tp->speed & LINK_STATUS) {
2302 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002303 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002304 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002305 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002306 }
2307 }
2308 tp->speed = speed;
2309}
2310
2311static void rtl_work_func_t(struct work_struct *work)
2312{
2313 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2314
2315 if (!test_bit(WORK_ENABLE, &tp->flags))
2316 goto out1;
2317
2318 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2319 goto out1;
2320
hayeswang40a82912013-08-14 20:54:40 +08002321 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2322 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002323
2324 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2325 _rtl8152_set_rx_mode(tp->netdev);
2326
hayeswangac718b62013-05-02 16:01:25 +00002327out1:
2328 return;
2329}
2330
2331static int rtl8152_open(struct net_device *netdev)
2332{
2333 struct r8152 *tp = netdev_priv(netdev);
2334 int res = 0;
2335
hayeswang43779f82014-01-02 11:25:10 +08002336 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2337 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2338 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002339 tp->speed = 0;
2340 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002341 netif_start_queue(netdev);
2342 set_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002343 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2344 if (res) {
2345 if (res == -ENODEV)
2346 netif_device_detach(tp->netdev);
2347 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2348 res);
2349 }
2350
hayeswangac718b62013-05-02 16:01:25 +00002351
2352 return res;
2353}
2354
2355static int rtl8152_close(struct net_device *netdev)
2356{
2357 struct r8152 *tp = netdev_priv(netdev);
2358 int res = 0;
2359
2360 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08002361 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002362 cancel_delayed_work_sync(&tp->schedule);
2363 netif_stop_queue(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002364 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002365 tp->rtl_ops.disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002366 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002367
2368 return res;
2369}
2370
hayeswangac718b62013-05-02 16:01:25 +00002371static void r8152b_enable_eee(struct r8152 *tp)
2372{
hayeswang45f4a192014-01-06 17:08:41 +08002373 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002374
2375 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2376 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2377 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2378 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2379 EEE_10_CAP | EEE_NWAY_EN |
2380 TX_QUIET_EN | RX_QUIET_EN |
2381 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2382 SDFALLTIME);
2383 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2384 RG_LDVQUIET_EN | RG_CKRSEL |
2385 RG_EEEPRG_EN);
2386 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2387 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2388 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2389 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2390 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2391 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2392}
2393
hayeswang43779f82014-01-02 11:25:10 +08002394static void r8153_enable_eee(struct r8152 *tp)
2395{
2396 u32 ocp_data;
2397 u16 data;
2398
2399 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2400 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2401 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2402 data = ocp_reg_read(tp, OCP_EEE_CFG);
2403 data |= EEE10_EN;
2404 ocp_reg_write(tp, OCP_EEE_CFG, data);
2405 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2406 data |= MY1000_EEE | MY100_EEE;
2407 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2408}
2409
hayeswangac718b62013-05-02 16:01:25 +00002410static void r8152b_enable_fc(struct r8152 *tp)
2411{
2412 u16 anar;
2413
2414 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2415 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2416 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2417}
2418
hayeswangac718b62013-05-02 16:01:25 +00002419static void r8152b_init(struct r8152 *tp)
2420{
hayeswangebc2ec482013-08-14 20:54:38 +08002421 u32 ocp_data;
2422 int i;
hayeswangac718b62013-05-02 16:01:25 +00002423
2424 rtl_clear_bp(tp);
2425
2426 if (tp->version == RTL_VER_01) {
2427 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2428 ocp_data &= ~LED_MODE_MASK;
2429 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2430 }
2431
2432 r8152b_hw_phy_cfg(tp);
2433
hayeswang00a5e362014-02-18 21:48:59 +08002434 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002435
hayeswangac718b62013-05-02 16:01:25 +00002436
2437 r8152b_exit_oob(tp);
2438
2439 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2440 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2441 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2442 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2443 ocp_data &= ~MCU_CLK_RATIO_MASK;
2444 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2445 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2446 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2447 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2449
2450 r8152b_enable_eee(tp);
2451 r8152b_enable_aldps(tp);
2452 r8152b_enable_fc(tp);
2453
2454 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2455 BMCR_ANRESTART);
2456 for (i = 0; i < 100; i++) {
2457 udelay(100);
2458 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
2459 break;
2460 }
2461
hayeswangebc2ec482013-08-14 20:54:38 +08002462 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002463 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08002464 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002465 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2466}
2467
hayeswang43779f82014-01-02 11:25:10 +08002468static void r8153_init(struct r8152 *tp)
2469{
2470 u32 ocp_data;
2471 int i;
2472
hayeswangb9702722014-02-18 21:49:00 +08002473 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002474
2475 for (i = 0; i < 500; i++) {
2476 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2477 AUTOLOAD_DONE)
2478 break;
2479 msleep(20);
2480 }
2481
2482 for (i = 0; i < 500; i++) {
2483 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2484 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2485 break;
2486 msleep(20);
2487 }
2488
hayeswangb9702722014-02-18 21:49:00 +08002489 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08002490
2491 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2492 ocp_data &= ~TIMER11_EN;
2493 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2494
2495 r8153_clear_bp(tp);
2496
2497 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2498 ocp_data &= ~LED_MODE_MASK;
2499 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2500
2501 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2502 ocp_data &= ~LPM_TIMER_MASK;
2503 if (tp->udev->speed == USB_SPEED_SUPER)
2504 ocp_data |= LPM_TIMER_500US;
2505 else
2506 ocp_data |= LPM_TIMER_500MS;
2507 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2508
2509 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2510 ocp_data &= ~SEN_VAL_MASK;
2511 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2512 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2513
hayeswangb9702722014-02-18 21:49:00 +08002514 r8153_power_cut_en(tp, false);
2515 r8153_u1u2en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002516
2517 r8153_first_init(tp);
2518
2519 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2520 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2521 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2522 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2523 U1U2_SPDWN_EN | L1_SPDWN_EN);
2524 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2525 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2526 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2527 EEE_SPDWN_EN);
2528
2529 r8153_enable_eee(tp);
2530 r8153_enable_aldps(tp);
2531 r8152b_enable_fc(tp);
2532
2533 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2534 BMCR_ANRESTART);
2535}
2536
hayeswangac718b62013-05-02 16:01:25 +00002537static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2538{
2539 struct r8152 *tp = usb_get_intfdata(intf);
2540
2541 netif_device_detach(tp->netdev);
2542
2543 if (netif_running(tp->netdev)) {
2544 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002545 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002546 cancel_delayed_work_sync(&tp->schedule);
hayeswangebc2ec482013-08-14 20:54:38 +08002547 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002548 }
2549
hayeswangc81229c2014-01-02 11:22:42 +08002550 tp->rtl_ops.down(tp);
hayeswangac718b62013-05-02 16:01:25 +00002551
2552 return 0;
2553}
2554
2555static int rtl8152_resume(struct usb_interface *intf)
2556{
2557 struct r8152 *tp = usb_get_intfdata(intf);
2558
hayeswangc81229c2014-01-02 11:22:42 +08002559 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002560 netif_device_attach(tp->netdev);
2561 if (netif_running(tp->netdev)) {
hayeswang43779f82014-01-02 11:25:10 +08002562 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2563 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2564 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002565 tp->speed = 0;
2566 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002567 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002568 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangebc2ec482013-08-14 20:54:38 +08002569 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002570 }
2571
2572 return 0;
2573}
2574
2575static void rtl8152_get_drvinfo(struct net_device *netdev,
2576 struct ethtool_drvinfo *info)
2577{
2578 struct r8152 *tp = netdev_priv(netdev);
2579
2580 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2581 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2582 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2583}
2584
2585static
2586int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2587{
2588 struct r8152 *tp = netdev_priv(netdev);
2589
2590 if (!tp->mii.mdio_read)
2591 return -EOPNOTSUPP;
2592
2593 return mii_ethtool_gset(&tp->mii, cmd);
2594}
2595
2596static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2597{
2598 struct r8152 *tp = netdev_priv(dev);
2599
2600 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2601}
2602
2603static struct ethtool_ops ops = {
2604 .get_drvinfo = rtl8152_get_drvinfo,
2605 .get_settings = rtl8152_get_settings,
2606 .set_settings = rtl8152_set_settings,
2607 .get_link = ethtool_op_get_link,
2608};
2609
2610static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2611{
2612 struct r8152 *tp = netdev_priv(netdev);
2613 struct mii_ioctl_data *data = if_mii(rq);
2614 int res = 0;
2615
2616 switch (cmd) {
2617 case SIOCGMIIPHY:
2618 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2619 break;
2620
2621 case SIOCGMIIREG:
2622 data->val_out = r8152_mdio_read(tp, data->reg_num);
2623 break;
2624
2625 case SIOCSMIIREG:
2626 if (!capable(CAP_NET_ADMIN)) {
2627 res = -EPERM;
2628 break;
2629 }
2630 r8152_mdio_write(tp, data->reg_num, data->val_in);
2631 break;
2632
2633 default:
2634 res = -EOPNOTSUPP;
2635 }
2636
2637 return res;
2638}
2639
2640static const struct net_device_ops rtl8152_netdev_ops = {
2641 .ndo_open = rtl8152_open,
2642 .ndo_stop = rtl8152_close,
2643 .ndo_do_ioctl = rtl8152_ioctl,
2644 .ndo_start_xmit = rtl8152_start_xmit,
2645 .ndo_tx_timeout = rtl8152_tx_timeout,
2646 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2647 .ndo_set_mac_address = rtl8152_set_mac_address,
2648
2649 .ndo_change_mtu = eth_change_mtu,
2650 .ndo_validate_addr = eth_validate_addr,
2651};
2652
2653static void r8152b_get_version(struct r8152 *tp)
2654{
2655 u32 ocp_data;
2656 u16 version;
2657
2658 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2659 version = (u16)(ocp_data & VERSION_MASK);
2660
2661 switch (version) {
2662 case 0x4c00:
2663 tp->version = RTL_VER_01;
2664 break;
2665 case 0x4c10:
2666 tp->version = RTL_VER_02;
2667 break;
hayeswang43779f82014-01-02 11:25:10 +08002668 case 0x5c00:
2669 tp->version = RTL_VER_03;
2670 tp->mii.supports_gmii = 1;
2671 break;
2672 case 0x5c10:
2673 tp->version = RTL_VER_04;
2674 tp->mii.supports_gmii = 1;
2675 break;
2676 case 0x5c20:
2677 tp->version = RTL_VER_05;
2678 tp->mii.supports_gmii = 1;
2679 break;
hayeswangac718b62013-05-02 16:01:25 +00002680 default:
2681 netif_info(tp, probe, tp->netdev,
2682 "Unknown version 0x%04x\n", version);
2683 break;
2684 }
2685}
2686
hayeswange3fe0b12014-01-02 11:22:39 +08002687static void rtl8152_unload(struct r8152 *tp)
2688{
hayeswang00a5e362014-02-18 21:48:59 +08002689 if (tp->version != RTL_VER_01)
2690 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08002691}
2692
hayeswang43779f82014-01-02 11:25:10 +08002693static void rtl8153_unload(struct r8152 *tp)
2694{
hayeswangb9702722014-02-18 21:49:00 +08002695 r8153_power_cut_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08002696}
2697
hayeswang31ca1de2014-01-06 17:08:43 +08002698static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
hayeswangc81229c2014-01-02 11:22:42 +08002699{
2700 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang31ca1de2014-01-06 17:08:43 +08002701 int ret = -ENODEV;
hayeswangc81229c2014-01-02 11:22:42 +08002702
2703 switch (id->idVendor) {
2704 case VENDOR_ID_REALTEK:
2705 switch (id->idProduct) {
2706 case PRODUCT_ID_RTL8152:
2707 ops->init = r8152b_init;
2708 ops->enable = rtl8152_enable;
2709 ops->disable = rtl8152_disable;
2710 ops->down = rtl8152_down;
2711 ops->unload = rtl8152_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002712 ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08002713 break;
hayeswang43779f82014-01-02 11:25:10 +08002714 case PRODUCT_ID_RTL8153:
2715 ops->init = r8153_init;
2716 ops->enable = rtl8153_enable;
2717 ops->disable = rtl8152_disable;
2718 ops->down = rtl8153_down;
2719 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002720 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002721 break;
2722 default:
hayeswang43779f82014-01-02 11:25:10 +08002723 break;
2724 }
2725 break;
2726
2727 case VENDOR_ID_SAMSUNG:
2728 switch (id->idProduct) {
2729 case PRODUCT_ID_SAMSUNG:
2730 ops->init = r8153_init;
2731 ops->enable = rtl8153_enable;
2732 ops->disable = rtl8152_disable;
2733 ops->down = rtl8153_down;
2734 ops->unload = rtl8153_unload;
hayeswang31ca1de2014-01-06 17:08:43 +08002735 ret = 0;
hayeswang43779f82014-01-02 11:25:10 +08002736 break;
hayeswangc81229c2014-01-02 11:22:42 +08002737 default:
hayeswangc81229c2014-01-02 11:22:42 +08002738 break;
2739 }
2740 break;
2741
2742 default:
hayeswangc81229c2014-01-02 11:22:42 +08002743 break;
2744 }
2745
hayeswang31ca1de2014-01-06 17:08:43 +08002746 if (ret)
2747 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
2748
hayeswangc81229c2014-01-02 11:22:42 +08002749 return ret;
2750}
2751
hayeswangac718b62013-05-02 16:01:25 +00002752static int rtl8152_probe(struct usb_interface *intf,
2753 const struct usb_device_id *id)
2754{
2755 struct usb_device *udev = interface_to_usbdev(intf);
2756 struct r8152 *tp;
2757 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08002758 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002759
hayeswangac718b62013-05-02 16:01:25 +00002760 netdev = alloc_etherdev(sizeof(struct r8152));
2761 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002762 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00002763 return -ENOMEM;
2764 }
2765
hayeswangebc2ec482013-08-14 20:54:38 +08002766 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00002767 tp = netdev_priv(netdev);
2768 tp->msg_enable = 0x7FFF;
2769
hayeswange3ad4122014-01-06 17:08:42 +08002770 tp->udev = udev;
2771 tp->netdev = netdev;
2772 tp->intf = intf;
2773
hayeswang31ca1de2014-01-06 17:08:43 +08002774 ret = rtl_ops_init(tp, id);
2775 if (ret)
2776 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08002777
hayeswangebc2ec482013-08-14 20:54:38 +08002778 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00002779 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2780
hayeswangac718b62013-05-02 16:01:25 +00002781 netdev->netdev_ops = &rtl8152_netdev_ops;
2782 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08002783
2784 netdev->features |= NETIF_F_IP_CSUM;
2785 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00002786 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00002787
2788 tp->mii.dev = netdev;
2789 tp->mii.mdio_read = read_mii_word;
2790 tp->mii.mdio_write = write_mii_word;
2791 tp->mii.phy_id_mask = 0x3f;
2792 tp->mii.reg_num_mask = 0x1f;
2793 tp->mii.phy_id = R8152_PHY_ID;
2794 tp->mii.supports_gmii = 0;
2795
2796 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08002797 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002798 set_ethernet_addr(tp);
2799
hayeswangebc2ec482013-08-14 20:54:38 +08002800 ret = alloc_all_mem(tp);
2801 if (ret)
hayeswangac718b62013-05-02 16:01:25 +00002802 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002803
2804 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00002805
hayeswangebc2ec482013-08-14 20:54:38 +08002806 ret = register_netdev(netdev);
2807 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08002808 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08002809 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00002810 }
2811
Hayes Wang4a8deae2014-01-07 11:18:22 +08002812 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00002813
2814 return 0;
2815
hayeswangac718b62013-05-02 16:01:25 +00002816out1:
hayeswangebc2ec482013-08-14 20:54:38 +08002817 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00002818out:
2819 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002820 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002821}
2822
hayeswangac718b62013-05-02 16:01:25 +00002823static void rtl8152_disconnect(struct usb_interface *intf)
2824{
2825 struct r8152 *tp = usb_get_intfdata(intf);
2826
2827 usb_set_intfdata(intf, NULL);
2828 if (tp) {
2829 set_bit(RTL8152_UNPLUG, &tp->flags);
2830 tasklet_kill(&tp->tl);
2831 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08002832 tp->rtl_ops.unload(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002833 free_all_mem(tp);
hayeswangac718b62013-05-02 16:01:25 +00002834 free_netdev(tp->netdev);
2835 }
2836}
2837
2838/* table of devices that work with this driver */
2839static struct usb_device_id rtl8152_table[] = {
hayeswangc7de7de2014-01-15 10:42:16 +08002840 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
2841 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
2842 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00002843 {}
2844};
2845
2846MODULE_DEVICE_TABLE(usb, rtl8152_table);
2847
2848static struct usb_driver rtl8152_driver = {
2849 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08002850 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00002851 .probe = rtl8152_probe,
2852 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00002853 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08002854 .resume = rtl8152_resume,
2855 .reset_resume = rtl8152_resume,
hayeswangac718b62013-05-02 16:01:25 +00002856};
2857
Sachin Kamatb4236daa2013-05-16 17:48:08 +00002858module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00002859
2860MODULE_AUTHOR(DRIVER_AUTHOR);
2861MODULE_DESCRIPTION(DRIVER_DESC);
2862MODULE_LICENSE("GPL");