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