blob: 8615681096b16f9497fc0afaf496b36489b5833f [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>
hayeswangebc2ec42013-08-14 20:54:38 +080022#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080023#include <linux/ip.h>
24#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000025
26/* Version Information */
hayeswang43779f82014-01-02 11:25:10 +080027#define DRIVER_VERSION "v1.03.0 (2013/12/26)"
hayeswangac718b62013-05-02 16:01:25 +000028#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
29#define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
30#define MODULENAME "r8152"
31
32#define R8152_PHY_ID 32
33
34#define PLA_IDR 0xc000
35#define PLA_RCR 0xc010
36#define PLA_RMS 0xc016
37#define PLA_RXFIFO_CTRL0 0xc0a0
38#define PLA_RXFIFO_CTRL1 0xc0a4
39#define PLA_RXFIFO_CTRL2 0xc0a8
40#define PLA_FMC 0xc0b4
41#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080042#define PLA_TEREDO_CFG 0xc0bc
hayeswangac718b62013-05-02 16:01:25 +000043#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080044#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000045#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080046#define PLA_TEREDO_TIMER 0xd2cc
47#define PLA_REALWOW_TIMER 0xd2e8
hayeswangac718b62013-05-02 16:01:25 +000048#define PLA_LEDSEL 0xdd90
49#define PLA_LED_FEATURE 0xdd92
50#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080051#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_GPHY_INTR_IMR 0xe022
53#define PLA_EEE_CR 0xe040
54#define PLA_EEEP_CR 0xe080
55#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080056#define PLA_MAC_PWR_CTRL2 0xe0ca
57#define PLA_MAC_PWR_CTRL3 0xe0cc
58#define PLA_MAC_PWR_CTRL4 0xe0ce
59#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000060#define PLA_TCR0 0xe610
61#define PLA_TCR1 0xe612
62#define PLA_TXFIFO_CTRL 0xe618
63#define PLA_RSTTELLY 0xe800
64#define PLA_CR 0xe813
65#define PLA_CRWECR 0xe81c
66#define PLA_CONFIG5 0xe822
67#define PLA_PHY_PWR 0xe84c
68#define PLA_OOB_CTRL 0xe84f
69#define PLA_CPCR 0xe854
70#define PLA_MISC_0 0xe858
71#define PLA_MISC_1 0xe85a
72#define PLA_OCP_GPHY_BASE 0xe86c
73#define PLA_TELLYCNT 0xe890
74#define PLA_SFF_STS_7 0xe8de
75#define PLA_PHYSTATUS 0xe908
76#define PLA_BP_BA 0xfc26
77#define PLA_BP_0 0xfc28
78#define PLA_BP_1 0xfc2a
79#define PLA_BP_2 0xfc2c
80#define PLA_BP_3 0xfc2e
81#define PLA_BP_4 0xfc30
82#define PLA_BP_5 0xfc32
83#define PLA_BP_6 0xfc34
84#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +080085#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +000086
hayeswang43779f82014-01-02 11:25:10 +080087#define USB_U2P3_CTRL 0xb460
hayeswangac718b62013-05-02 16:01:25 +000088#define USB_DEV_STAT 0xb808
89#define USB_USB_CTRL 0xd406
90#define USB_PHY_CTRL 0xd408
91#define USB_TX_AGG 0xd40a
92#define USB_RX_BUF_TH 0xd40c
93#define USB_USB_TIMER 0xd428
hayeswang43779f82014-01-02 11:25:10 +080094#define USB_RX_EARLY_AGG 0xd42c
hayeswangac718b62013-05-02 16:01:25 +000095#define USB_PM_CTRL_STATUS 0xd432
96#define USB_TX_DMA 0xd434
hayeswang43779f82014-01-02 11:25:10 +080097#define USB_TOLERANCE 0xd490
98#define USB_LPM_CTRL 0xd41a
hayeswangac718b62013-05-02 16:01:25 +000099#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800100#define USB_MISC_0 0xd81a
101#define USB_POWER_CUT 0xd80a
102#define USB_AFE_CTRL2 0xd824
103#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000104#define USB_BP_BA 0xfc26
105#define USB_BP_0 0xfc28
106#define USB_BP_1 0xfc2a
107#define USB_BP_2 0xfc2c
108#define USB_BP_3 0xfc2e
109#define USB_BP_4 0xfc30
110#define USB_BP_5 0xfc32
111#define USB_BP_6 0xfc34
112#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800113#define USB_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000114
115/* OCP Registers */
116#define OCP_ALDPS_CONFIG 0x2010
117#define OCP_EEE_CONFIG1 0x2080
118#define OCP_EEE_CONFIG2 0x2092
119#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800120#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000121#define OCP_EEE_AR 0xa41a
122#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800123#define OCP_PHY_STATUS 0xa420
124#define OCP_POWER_CFG 0xa430
125#define OCP_EEE_CFG 0xa432
126#define OCP_SRAM_ADDR 0xa436
127#define OCP_SRAM_DATA 0xa438
128#define OCP_DOWN_SPEED 0xa442
129#define OCP_EEE_CFG2 0xa5d0
130#define OCP_ADC_CFG 0xbc06
131
132/* SRAM Register */
133#define SRAM_LPF_CFG 0x8012
134#define SRAM_10M_AMP1 0x8080
135#define SRAM_10M_AMP2 0x8082
136#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000137
138/* PLA_RCR */
139#define RCR_AAP 0x00000001
140#define RCR_APM 0x00000002
141#define RCR_AM 0x00000004
142#define RCR_AB 0x00000008
143#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
144
145/* PLA_RXFIFO_CTRL0 */
146#define RXFIFO_THR1_NORMAL 0x00080002
147#define RXFIFO_THR1_OOB 0x01800003
148
149/* PLA_RXFIFO_CTRL1 */
150#define RXFIFO_THR2_FULL 0x00000060
151#define RXFIFO_THR2_HIGH 0x00000038
152#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800153#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000154
155/* PLA_RXFIFO_CTRL2 */
156#define RXFIFO_THR3_FULL 0x00000078
157#define RXFIFO_THR3_HIGH 0x00000048
158#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800159#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000160
161/* PLA_TXFIFO_CTRL */
162#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800163#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000164
165/* PLA_FMC */
166#define FMC_FCR_MCU_EN 0x0001
167
168/* PLA_EEEP_CR */
169#define EEEP_CR_EEEP_TX 0x0002
170
hayeswang43779f82014-01-02 11:25:10 +0800171/* PLA_WDT6_CTRL */
172#define WDT6_SET_MODE 0x0010
173
hayeswangac718b62013-05-02 16:01:25 +0000174/* PLA_TCR0 */
175#define TCR0_TX_EMPTY 0x0800
176#define TCR0_AUTO_FIFO 0x0080
177
178/* PLA_TCR1 */
179#define VERSION_MASK 0x7cf0
180
181/* PLA_CR */
182#define CR_RST 0x10
183#define CR_RE 0x08
184#define CR_TE 0x04
185
186/* PLA_CRWECR */
187#define CRWECR_NORAML 0x00
188#define CRWECR_CONFIG 0xc0
189
190/* PLA_OOB_CTRL */
191#define NOW_IS_OOB 0x80
192#define TXFIFO_EMPTY 0x20
193#define RXFIFO_EMPTY 0x10
194#define LINK_LIST_READY 0x02
195#define DIS_MCU_CLROOB 0x01
196#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
197
198/* PLA_MISC_1 */
199#define RXDY_GATED_EN 0x0008
200
201/* PLA_SFF_STS_7 */
202#define RE_INIT_LL 0x8000
203#define MCU_BORW_EN 0x4000
204
205/* PLA_CPCR */
206#define CPCR_RX_VLAN 0x0040
207
208/* PLA_CFG_WOL */
209#define MAGIC_EN 0x0001
210
hayeswang43779f82014-01-02 11:25:10 +0800211/* PLA_TEREDO_CFG */
212#define TEREDO_SEL 0x8000
213#define TEREDO_WAKE_MASK 0x7f00
214#define TEREDO_RS_EVENT_MASK 0x00fe
215#define OOB_TEREDO_EN 0x0001
216
hayeswangac718b62013-05-02 16:01:25 +0000217/* PAL_BDC_CR */
218#define ALDPS_PROXY_MODE 0x0001
219
220/* PLA_CONFIG5 */
221#define LAN_WAKE_EN 0x0002
222
223/* PLA_LED_FEATURE */
224#define LED_MODE_MASK 0x0700
225
226/* PLA_PHY_PWR */
227#define TX_10M_IDLE_EN 0x0080
228#define PFM_PWM_SWITCH 0x0040
229
230/* PLA_MAC_PWR_CTRL */
231#define D3_CLK_GATED_EN 0x00004000
232#define MCU_CLK_RATIO 0x07010f07
233#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800234#define ALDPS_SPDWN_RATIO 0x0f87
235
236/* PLA_MAC_PWR_CTRL2 */
237#define EEE_SPDWN_RATIO 0x8007
238
239/* PLA_MAC_PWR_CTRL3 */
240#define PKT_AVAIL_SPDWN_EN 0x0100
241#define SUSPEND_SPDWN_EN 0x0004
242#define U1U2_SPDWN_EN 0x0002
243#define L1_SPDWN_EN 0x0001
244
245/* PLA_MAC_PWR_CTRL4 */
246#define PWRSAVE_SPDWN_EN 0x1000
247#define RXDV_SPDWN_EN 0x0800
248#define TX10MIDLE_EN 0x0100
249#define TP100_SPDWN_EN 0x0020
250#define TP500_SPDWN_EN 0x0010
251#define TP1000_SPDWN_EN 0x0008
252#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000253
254/* PLA_GPHY_INTR_IMR */
255#define GPHY_STS_MSK 0x0001
256#define SPEED_DOWN_MSK 0x0002
257#define SPDWN_RXDV_MSK 0x0004
258#define SPDWN_LINKCHG_MSK 0x0008
259
260/* PLA_PHYAR */
261#define PHYAR_FLAG 0x80000000
262
263/* PLA_EEE_CR */
264#define EEE_RX_EN 0x0001
265#define EEE_TX_EN 0x0002
266
hayeswang43779f82014-01-02 11:25:10 +0800267/* PLA_BOOT_CTRL */
268#define AUTOLOAD_DONE 0x0002
269
hayeswangac718b62013-05-02 16:01:25 +0000270/* USB_DEV_STAT */
271#define STAT_SPEED_MASK 0x0006
272#define STAT_SPEED_HIGH 0x0000
273#define STAT_SPEED_FULL 0x0001
274
275/* USB_TX_AGG */
276#define TX_AGG_MAX_THRESHOLD 0x03
277
278/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800279#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800280#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800281#define RX_THR_SLOW 0xffff0180
hayeswangac718b62013-05-02 16:01:25 +0000282
283/* USB_TX_DMA */
284#define TEST_MODE_DISABLE 0x00000001
285#define TX_SIZE_ADJUST1 0x00000100
286
287/* USB_UPS_CTRL */
288#define POWER_CUT 0x0100
289
290/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800291#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000292
293/* USB_USB_CTRL */
294#define RX_AGG_DISABLE 0x0010
295
hayeswang43779f82014-01-02 11:25:10 +0800296/* USB_U2P3_CTRL */
297#define U2P3_ENABLE 0x0001
298
299/* USB_POWER_CUT */
300#define PWR_EN 0x0001
301#define PHASE2_EN 0x0008
302
303/* USB_MISC_0 */
304#define PCUT_STATUS 0x0001
305
306/* USB_RX_EARLY_AGG */
307#define EARLY_AGG_SUPPER 0x0e832981
308#define EARLY_AGG_HIGH 0x0e837a12
309#define EARLY_AGG_SLOW 0x0e83ffff
310
311/* USB_WDT11_CTRL */
312#define TIMER11_EN 0x0001
313
314/* USB_LPM_CTRL */
315#define LPM_TIMER_MASK 0x0c
316#define LPM_TIMER_500MS 0x04 /* 500 ms */
317#define LPM_TIMER_500US 0x0c /* 500 us */
318
319/* USB_AFE_CTRL2 */
320#define SEN_VAL_MASK 0xf800
321#define SEN_VAL_NORMAL 0xa000
322#define SEL_RXIDLE 0x0100
323
hayeswangac718b62013-05-02 16:01:25 +0000324/* OCP_ALDPS_CONFIG */
325#define ENPWRSAVE 0x8000
326#define ENPDNPS 0x0200
327#define LINKENA 0x0100
328#define DIS_SDSAVE 0x0010
329
hayeswang43779f82014-01-02 11:25:10 +0800330/* OCP_PHY_STATUS */
331#define PHY_STAT_MASK 0x0007
332#define PHY_STAT_LAN_ON 3
333#define PHY_STAT_PWRDN 5
334
335/* OCP_POWER_CFG */
336#define EEE_CLKDIV_EN 0x8000
337#define EN_ALDPS 0x0004
338#define EN_10M_PLLOFF 0x0001
339
hayeswangac718b62013-05-02 16:01:25 +0000340/* OCP_EEE_CONFIG1 */
341#define RG_TXLPI_MSK_HFDUP 0x8000
342#define RG_MATCLR_EN 0x4000
343#define EEE_10_CAP 0x2000
344#define EEE_NWAY_EN 0x1000
345#define TX_QUIET_EN 0x0200
346#define RX_QUIET_EN 0x0100
347#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
348#define RG_RXLPI_MSK_HFDUP 0x0008
349#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
350
351/* OCP_EEE_CONFIG2 */
352#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
353#define RG_DACQUIET_EN 0x0400
354#define RG_LDVQUIET_EN 0x0200
355#define RG_CKRSEL 0x0020
356#define RG_EEEPRG_EN 0x0010
357
358/* OCP_EEE_CONFIG3 */
359#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
360#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
361#define MSK_PH 0x0006 /* bit 0 ~ 3 */
362
363/* OCP_EEE_AR */
364/* bit[15:14] function */
365#define FUN_ADDR 0x0000
366#define FUN_DATA 0x4000
367/* bit[4:0] device addr */
368#define DEVICE_ADDR 0x0007
369
370/* OCP_EEE_DATA */
371#define EEE_ADDR 0x003C
372#define EEE_DATA 0x0002
373
hayeswang43779f82014-01-02 11:25:10 +0800374/* OCP_EEE_CFG */
375#define CTAP_SHORT_EN 0x0040
376#define EEE10_EN 0x0010
377
378/* OCP_DOWN_SPEED */
379#define EN_10M_BGOFF 0x0080
380
381/* OCP_EEE_CFG2 */
382#define MY1000_EEE 0x0004
383#define MY100_EEE 0x0002
384
385/* OCP_ADC_CFG */
386#define CKADSEL_L 0x0100
387#define ADC_EN 0x0080
388#define EN_EMI_L 0x0040
389
390/* SRAM_LPF_CFG */
391#define LPF_AUTO_TUNE 0x8000
392
393/* SRAM_10M_AMP1 */
394#define GDAC_IB_UPALL 0x0008
395
396/* SRAM_10M_AMP2 */
397#define AMP_DN 0x0200
398
399/* SRAM_IMPEDANCE */
400#define RX_DRIVING_MASK 0x6000
401
hayeswangac718b62013-05-02 16:01:25 +0000402enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800403 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000404 _100bps = 0x08,
405 _10bps = 0x04,
406 LINK_STATUS = 0x02,
407 FULL_DUP = 0x01,
408};
409
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-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
hayeswangebc2ec42013-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;
hayeswangebc2ec42013-08-14 20:54:38 +0800938
hayeswangac718b62013-05-02 16:01:25 +0000939 if (test_bit(RTL8152_UNPLUG, &tp->flags))
940 return;
hayeswangebc2ec42013-08-14 20:54:38 +0800941
942 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000943 return;
944
hayeswangebc2ec42013-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 */
hayeswangebc2ec42013-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:
hayeswangebc2ec42013-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);
hayeswangebc2ec42013-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);
hayeswangebc2ec42013-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);
hayeswangebc2ec42013-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:
969 pr_warn_ratelimited("may be reset is needed?..\n");
hayeswangebc2ec42013-08-14 20:54:38 +0800970 break;
hayeswangac718b62013-05-02 16:01:25 +0000971 default:
972 pr_warn_ratelimited("Rx status %d\n", status);
hayeswangebc2ec42013-08-14 20:54:38 +0800973 break;
hayeswangac718b62013-05-02 16:01:25 +0000974 }
975
hayeswangebc2ec42013-08-14 20:54:38 +0800976 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +0000977 if (result == -ENODEV) {
978 netif_device_detach(tp->netdev);
979 } else if (result) {
hayeswanga5a4f462013-08-16 16:09:34 +0800980 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +0800981 list_add_tail(&agg->list, &tp->rx_done);
hayeswanga5a4f462013-08-16 16:09:34 +0800982 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +0800983 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +0000984 }
hayeswangac718b62013-05-02 16:01:25 +0000985}
986
987static void write_bulk_callback(struct urb *urb)
988{
hayeswangebc2ec42013-08-14 20:54:38 +0800989 struct net_device_stats *stats;
hayeswanga5a4f462013-08-16 16:09:34 +0800990 unsigned long flags;
hayeswangebc2ec42013-08-14 20:54:38 +0800991 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +0000992 struct r8152 *tp;
993 int status = urb->status;
994
hayeswangebc2ec42013-08-14 20:54:38 +0800995 agg = urb->context;
996 if (!agg)
997 return;
998
999 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001000 if (!tp)
1001 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001002
1003 stats = rtl8152_get_stats(tp->netdev);
1004 if (status) {
1005 pr_warn_ratelimited("Tx status %d\n", status);
1006 stats->tx_errors += agg->skb_num;
1007 } else {
1008 stats->tx_packets += agg->skb_num;
1009 stats->tx_bytes += agg->skb_len;
1010 }
1011
hayeswanga5a4f462013-08-16 16:09:34 +08001012 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001013 list_add_tail(&agg->list, &tp->tx_free);
hayeswanga5a4f462013-08-16 16:09:34 +08001014 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001015
1016 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +00001017 return;
hayeswangebc2ec42013-08-14 20:54:38 +08001018
1019 if (!test_bit(WORK_ENABLE, &tp->flags))
1020 return;
1021
1022 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1023 return;
1024
1025 if (!skb_queue_empty(&tp->tx_queue))
1026 tasklet_schedule(&tp->tl);
1027}
1028
hayeswang40a82912013-08-14 20:54:40 +08001029static void intr_callback(struct urb *urb)
1030{
1031 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001032 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001033 int status = urb->status;
1034 int res;
1035
1036 tp = urb->context;
1037 if (!tp)
1038 return;
1039
1040 if (!test_bit(WORK_ENABLE, &tp->flags))
1041 return;
1042
1043 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1044 return;
1045
1046 switch (status) {
1047 case 0: /* success */
1048 break;
1049 case -ECONNRESET: /* unlink */
1050 case -ESHUTDOWN:
1051 netif_device_detach(tp->netdev);
1052 case -ENOENT:
1053 return;
1054 case -EOVERFLOW:
1055 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1056 goto resubmit;
1057 /* -EPIPE: should clear the halt */
1058 default:
1059 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1060 goto resubmit;
1061 }
1062
1063 d = urb->transfer_buffer;
1064 if (INTR_LINK & __le16_to_cpu(d[0])) {
1065 if (!(tp->speed & LINK_STATUS)) {
1066 set_bit(RTL8152_LINK_CHG, &tp->flags);
1067 schedule_delayed_work(&tp->schedule, 0);
1068 }
1069 } else {
1070 if (tp->speed & LINK_STATUS) {
1071 set_bit(RTL8152_LINK_CHG, &tp->flags);
1072 schedule_delayed_work(&tp->schedule, 0);
1073 }
1074 }
1075
1076resubmit:
1077 res = usb_submit_urb(urb, GFP_ATOMIC);
1078 if (res == -ENODEV)
1079 netif_device_detach(tp->netdev);
1080 else if (res)
1081 netif_err(tp, intr, tp->netdev,
1082 "can't resubmit intr, status %d\n", res);
1083}
1084
hayeswangebc2ec42013-08-14 20:54:38 +08001085static inline void *rx_agg_align(void *data)
1086{
hayeswang8e1f51b2014-01-02 11:22:41 +08001087 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001088}
1089
1090static inline void *tx_agg_align(void *data)
1091{
hayeswang8e1f51b2014-01-02 11:22:41 +08001092 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec42013-08-14 20:54:38 +08001093}
1094
1095static void free_all_mem(struct r8152 *tp)
1096{
1097 int i;
1098
1099 for (i = 0; i < RTL8152_MAX_RX; i++) {
1100 if (tp->rx_info[i].urb) {
1101 usb_free_urb(tp->rx_info[i].urb);
1102 tp->rx_info[i].urb = NULL;
1103 }
1104
1105 if (tp->rx_info[i].buffer) {
1106 kfree(tp->rx_info[i].buffer);
1107 tp->rx_info[i].buffer = NULL;
1108 tp->rx_info[i].head = NULL;
1109 }
1110 }
1111
1112 for (i = 0; i < RTL8152_MAX_TX; i++) {
1113 if (tp->tx_info[i].urb) {
1114 usb_free_urb(tp->tx_info[i].urb);
1115 tp->tx_info[i].urb = NULL;
1116 }
1117
1118 if (tp->tx_info[i].buffer) {
1119 kfree(tp->tx_info[i].buffer);
1120 tp->tx_info[i].buffer = NULL;
1121 tp->tx_info[i].head = NULL;
1122 }
1123 }
hayeswang40a82912013-08-14 20:54:40 +08001124
1125 if (tp->intr_urb) {
1126 usb_free_urb(tp->intr_urb);
1127 tp->intr_urb = NULL;
1128 }
1129
1130 if (tp->intr_buff) {
1131 kfree(tp->intr_buff);
1132 tp->intr_buff = NULL;
1133 }
hayeswangebc2ec42013-08-14 20:54:38 +08001134}
1135
1136static int alloc_all_mem(struct r8152 *tp)
1137{
1138 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001139 struct usb_interface *intf = tp->intf;
1140 struct usb_host_interface *alt = intf->cur_altsetting;
1141 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec42013-08-14 20:54:38 +08001142 struct urb *urb;
1143 int node, i;
1144 u8 *buf;
1145
1146 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1147
1148 spin_lock_init(&tp->rx_lock);
1149 spin_lock_init(&tp->tx_lock);
1150 INIT_LIST_HEAD(&tp->rx_done);
1151 INIT_LIST_HEAD(&tp->tx_free);
1152 skb_queue_head_init(&tp->tx_queue);
1153
1154 for (i = 0; i < RTL8152_MAX_RX; i++) {
1155 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1156 if (!buf)
1157 goto err1;
1158
1159 if (buf != rx_agg_align(buf)) {
1160 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001161 buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
1162 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001163 if (!buf)
1164 goto err1;
1165 }
1166
1167 urb = usb_alloc_urb(0, GFP_KERNEL);
1168 if (!urb) {
1169 kfree(buf);
1170 goto err1;
1171 }
1172
1173 INIT_LIST_HEAD(&tp->rx_info[i].list);
1174 tp->rx_info[i].context = tp;
1175 tp->rx_info[i].urb = urb;
1176 tp->rx_info[i].buffer = buf;
1177 tp->rx_info[i].head = rx_agg_align(buf);
1178 }
1179
1180 for (i = 0; i < RTL8152_MAX_TX; i++) {
1181 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
1182 if (!buf)
1183 goto err1;
1184
1185 if (buf != tx_agg_align(buf)) {
1186 kfree(buf);
hayeswang8e1f51b2014-01-02 11:22:41 +08001187 buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
1188 node);
hayeswangebc2ec42013-08-14 20:54:38 +08001189 if (!buf)
1190 goto err1;
1191 }
1192
1193 urb = usb_alloc_urb(0, GFP_KERNEL);
1194 if (!urb) {
1195 kfree(buf);
1196 goto err1;
1197 }
1198
1199 INIT_LIST_HEAD(&tp->tx_info[i].list);
1200 tp->tx_info[i].context = tp;
1201 tp->tx_info[i].urb = urb;
1202 tp->tx_info[i].buffer = buf;
1203 tp->tx_info[i].head = tx_agg_align(buf);
1204
1205 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1206 }
1207
hayeswang40a82912013-08-14 20:54:40 +08001208 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1209 if (!tp->intr_urb)
1210 goto err1;
1211
1212 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1213 if (!tp->intr_buff)
1214 goto err1;
1215
1216 tp->intr_interval = (int)ep_intr->desc.bInterval;
1217 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1218 tp->intr_buff, INTBUFSIZE, intr_callback,
1219 tp, tp->intr_interval);
1220
hayeswangebc2ec42013-08-14 20:54:38 +08001221 return 0;
1222
1223err1:
1224 free_all_mem(tp);
1225 return -ENOMEM;
1226}
1227
hayeswang0de98f62013-08-16 16:09:35 +08001228static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1229{
1230 struct tx_agg *agg = NULL;
1231 unsigned long flags;
1232
1233 spin_lock_irqsave(&tp->tx_lock, flags);
1234 if (!list_empty(&tp->tx_free)) {
1235 struct list_head *cursor;
1236
1237 cursor = tp->tx_free.next;
1238 list_del_init(cursor);
1239 agg = list_entry(cursor, struct tx_agg, list);
1240 }
1241 spin_unlock_irqrestore(&tp->tx_lock, flags);
1242
1243 return agg;
1244}
1245
hayeswang5bd23882013-08-14 20:54:39 +08001246static void
1247r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
1248{
1249 memset(desc, 0, sizeof(*desc));
1250
1251 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
1252
1253 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1254 __be16 protocol;
1255 u8 ip_protocol;
1256 u32 opts2 = 0;
1257
1258 if (skb->protocol == htons(ETH_P_8021Q))
1259 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1260 else
1261 protocol = skb->protocol;
1262
1263 switch (protocol) {
1264 case htons(ETH_P_IP):
1265 opts2 |= IPV4_CS;
1266 ip_protocol = ip_hdr(skb)->protocol;
1267 break;
1268
1269 case htons(ETH_P_IPV6):
1270 opts2 |= IPV6_CS;
1271 ip_protocol = ipv6_hdr(skb)->nexthdr;
1272 break;
1273
1274 default:
1275 ip_protocol = IPPROTO_RAW;
1276 break;
1277 }
1278
1279 if (ip_protocol == IPPROTO_TCP) {
1280 opts2 |= TCP_CS;
1281 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1282 } else if (ip_protocol == IPPROTO_UDP) {
1283 opts2 |= UDP_CS;
1284 } else {
1285 WARN_ON_ONCE(1);
1286 }
1287
1288 desc->opts2 = cpu_to_le32(opts2);
1289 }
1290}
1291
hayeswangb1379d92013-08-16 16:09:37 +08001292static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1293{
hayeswang7937f9e2013-11-20 17:30:54 +08001294 int remain;
hayeswangb1379d92013-08-16 16:09:37 +08001295 u8 *tx_data;
1296
1297 tx_data = agg->head;
1298 agg->skb_num = agg->skb_len = 0;
hayeswang7937f9e2013-11-20 17:30:54 +08001299 remain = rx_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001300
hayeswang7937f9e2013-11-20 17:30:54 +08001301 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001302 struct tx_desc *tx_desc;
1303 struct sk_buff *skb;
1304 unsigned int len;
1305
1306 skb = skb_dequeue(&tp->tx_queue);
1307 if (!skb)
1308 break;
1309
hayeswang7937f9e2013-11-20 17:30:54 +08001310 remain -= sizeof(*tx_desc);
hayeswangb1379d92013-08-16 16:09:37 +08001311 len = skb->len;
1312 if (remain < len) {
1313 skb_queue_head(&tp->tx_queue, skb);
1314 break;
1315 }
1316
hayeswang7937f9e2013-11-20 17:30:54 +08001317 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001318 tx_desc = (struct tx_desc *)tx_data;
1319 tx_data += sizeof(*tx_desc);
1320
1321 r8152_tx_csum(tp, tx_desc, skb);
1322 memcpy(tx_data, skb->data, len);
1323 agg->skb_num++;
1324 agg->skb_len += len;
1325 dev_kfree_skb_any(skb);
1326
hayeswang7937f9e2013-11-20 17:30:54 +08001327 tx_data += len;
1328 remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
hayeswangb1379d92013-08-16 16:09:37 +08001329 }
1330
hayeswangdd1b1192013-11-20 17:30:56 +08001331 netif_tx_lock(tp->netdev);
1332
1333 if (netif_queue_stopped(tp->netdev) &&
1334 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1335 netif_wake_queue(tp->netdev);
1336
1337 netif_tx_unlock(tp->netdev);
1338
hayeswangb1379d92013-08-16 16:09:37 +08001339 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1340 agg->head, (int)(tx_data - (u8 *)agg->head),
1341 (usb_complete_t)write_bulk_callback, agg);
1342
1343 return usb_submit_urb(agg->urb, GFP_ATOMIC);
1344}
1345
hayeswangebc2ec42013-08-14 20:54:38 +08001346static void rx_bottom(struct r8152 *tp)
1347{
hayeswanga5a4f462013-08-16 16:09:34 +08001348 unsigned long flags;
hayeswangebc2ec42013-08-14 20:54:38 +08001349 struct list_head *cursor, *next;
hayeswangebc2ec42013-08-14 20:54:38 +08001350
hayeswanga5a4f462013-08-16 16:09:34 +08001351 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001352 list_for_each_safe(cursor, next, &tp->rx_done) {
hayeswang43a44782013-08-16 16:09:36 +08001353 struct rx_desc *rx_desc;
1354 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001355 int len_used = 0;
1356 struct urb *urb;
1357 u8 *rx_data;
1358 int ret;
1359
hayeswangebc2ec42013-08-14 20:54:38 +08001360 list_del_init(cursor);
hayeswanga5a4f462013-08-16 16:09:34 +08001361 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001362
1363 agg = list_entry(cursor, struct rx_agg, list);
1364 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001365 if (urb->actual_length < ETH_ZLEN)
1366 goto submit;
hayeswangebc2ec42013-08-14 20:54:38 +08001367
hayeswangebc2ec42013-08-14 20:54:38 +08001368 rx_desc = agg->head;
1369 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001370 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001371
hayeswang7937f9e2013-11-20 17:30:54 +08001372 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001373 struct net_device *netdev = tp->netdev;
1374 struct net_device_stats *stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001375 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001376 struct sk_buff *skb;
1377
hayeswang7937f9e2013-11-20 17:30:54 +08001378 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec42013-08-14 20:54:38 +08001379 if (pkt_len < ETH_ZLEN)
1380 break;
1381
hayeswang7937f9e2013-11-20 17:30:54 +08001382 len_used += pkt_len;
1383 if (urb->actual_length < len_used)
1384 break;
1385
hayeswang43a44782013-08-16 16:09:36 +08001386 stats = rtl8152_get_stats(netdev);
1387
hayeswang8e1f51b2014-01-02 11:22:41 +08001388 pkt_len -= CRC_SIZE;
hayeswangebc2ec42013-08-14 20:54:38 +08001389 rx_data += sizeof(struct rx_desc);
1390
1391 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1392 if (!skb) {
1393 stats->rx_dropped++;
1394 break;
1395 }
1396 memcpy(skb->data, rx_data, pkt_len);
1397 skb_put(skb, pkt_len);
1398 skb->protocol = eth_type_trans(skb, netdev);
1399 netif_rx(skb);
1400 stats->rx_packets++;
1401 stats->rx_bytes += pkt_len;
1402
hayeswang8e1f51b2014-01-02 11:22:41 +08001403 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
hayeswangebc2ec42013-08-14 20:54:38 +08001404 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec42013-08-14 20:54:38 +08001405 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001406 len_used += sizeof(struct rx_desc);
hayeswangebc2ec42013-08-14 20:54:38 +08001407 }
1408
hayeswang0de98f62013-08-16 16:09:35 +08001409submit:
hayeswangebc2ec42013-08-14 20:54:38 +08001410 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswanga5a4f462013-08-16 16:09:34 +08001411 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001412 if (ret && ret != -ENODEV) {
1413 list_add_tail(&agg->list, next);
1414 tasklet_schedule(&tp->tl);
1415 }
1416 }
hayeswanga5a4f462013-08-16 16:09:34 +08001417 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec42013-08-14 20:54:38 +08001418}
1419
1420static void tx_bottom(struct r8152 *tp)
1421{
hayeswangebc2ec42013-08-14 20:54:38 +08001422 int res;
1423
hayeswangb1379d92013-08-16 16:09:37 +08001424 do {
1425 struct tx_agg *agg;
hayeswangebc2ec42013-08-14 20:54:38 +08001426
hayeswangb1379d92013-08-16 16:09:37 +08001427 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec42013-08-14 20:54:38 +08001428 break;
1429
hayeswangb1379d92013-08-16 16:09:37 +08001430 agg = r8152_get_tx_agg(tp);
1431 if (!agg)
hayeswangebc2ec42013-08-14 20:54:38 +08001432 break;
hayeswangb1379d92013-08-16 16:09:37 +08001433
1434 res = r8152_tx_agg_fill(tp, agg);
1435 if (res) {
1436 struct net_device_stats *stats;
1437 struct net_device *netdev;
1438 unsigned long flags;
1439
1440 netdev = tp->netdev;
1441 stats = rtl8152_get_stats(netdev);
1442
1443 if (res == -ENODEV) {
1444 netif_device_detach(netdev);
1445 } else {
1446 netif_warn(tp, tx_err, netdev,
1447 "failed tx_urb %d\n", res);
1448 stats->tx_dropped += agg->skb_num;
1449 spin_lock_irqsave(&tp->tx_lock, flags);
1450 list_add_tail(&agg->list, &tp->tx_free);
1451 spin_unlock_irqrestore(&tp->tx_lock, flags);
1452 }
hayeswangebc2ec42013-08-14 20:54:38 +08001453 }
hayeswangb1379d92013-08-16 16:09:37 +08001454 } while (res == 0);
hayeswangebc2ec42013-08-14 20:54:38 +08001455}
1456
1457static void bottom_half(unsigned long data)
1458{
1459 struct r8152 *tp;
1460
1461 tp = (struct r8152 *)data;
1462
1463 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1464 return;
1465
1466 if (!test_bit(WORK_ENABLE, &tp->flags))
1467 return;
1468
hayeswang7559fb2f2013-08-16 16:09:38 +08001469 /* When link down, the driver would cancel all bulks. */
1470 /* This avoid the re-submitting bulk */
hayeswangebc2ec42013-08-14 20:54:38 +08001471 if (!netif_carrier_ok(tp->netdev))
1472 return;
1473
1474 rx_bottom(tp);
1475 tx_bottom(tp);
1476}
1477
1478static
1479int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1480{
1481 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1482 agg->head, rx_buf_sz,
1483 (usb_complete_t)read_bulk_callback, agg);
1484
1485 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001486}
1487
1488static void rtl8152_tx_timeout(struct net_device *netdev)
1489{
1490 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08001491 int i;
1492
hayeswangac718b62013-05-02 16:01:25 +00001493 netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
hayeswangebc2ec42013-08-14 20:54:38 +08001494 for (i = 0; i < RTL8152_MAX_TX; i++)
1495 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001496}
1497
1498static void rtl8152_set_rx_mode(struct net_device *netdev)
1499{
1500 struct r8152 *tp = netdev_priv(netdev);
1501
hayeswang40a82912013-08-14 20:54:40 +08001502 if (tp->speed & LINK_STATUS) {
hayeswangac718b62013-05-02 16:01:25 +00001503 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001504 schedule_delayed_work(&tp->schedule, 0);
1505 }
hayeswangac718b62013-05-02 16:01:25 +00001506}
1507
1508static void _rtl8152_set_rx_mode(struct net_device *netdev)
1509{
1510 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001511 u32 mc_filter[2]; /* Multicast hash filter */
1512 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001513 u32 ocp_data;
1514
hayeswangac718b62013-05-02 16:01:25 +00001515 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1516 netif_stop_queue(netdev);
1517 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1518 ocp_data &= ~RCR_ACPT_ALL;
1519 ocp_data |= RCR_AB | RCR_APM;
1520
1521 if (netdev->flags & IFF_PROMISC) {
1522 /* Unconditionally log net taps. */
1523 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1524 ocp_data |= RCR_AM | RCR_AAP;
1525 mc_filter[1] = mc_filter[0] = 0xffffffff;
1526 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1527 (netdev->flags & IFF_ALLMULTI)) {
1528 /* Too many to filter perfectly -- accept all multicasts. */
1529 ocp_data |= RCR_AM;
1530 mc_filter[1] = mc_filter[0] = 0xffffffff;
1531 } else {
1532 struct netdev_hw_addr *ha;
1533
1534 mc_filter[1] = mc_filter[0] = 0;
1535 netdev_for_each_mc_addr(ha, netdev) {
1536 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1537 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1538 ocp_data |= RCR_AM;
1539 }
1540 }
1541
hayeswang31787f52013-07-31 17:21:25 +08001542 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1543 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001544
hayeswang31787f52013-07-31 17:21:25 +08001545 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001546 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1547 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001548}
1549
1550static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1551 struct net_device *netdev)
1552{
1553 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001554
hayeswangac718b62013-05-02 16:01:25 +00001555 skb_tx_timestamp(skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001556
hayeswang61598782013-11-20 17:30:55 +08001557 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec42013-08-14 20:54:38 +08001558
hayeswangdd1b1192013-11-20 17:30:56 +08001559 if (list_empty(&tp->tx_free) &&
1560 skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
1561 netif_stop_queue(netdev);
1562
hayeswang61598782013-11-20 17:30:55 +08001563 if (!list_empty(&tp->tx_free))
1564 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001565
1566 return NETDEV_TX_OK;
1567}
1568
1569static void r8152b_reset_packet_filter(struct r8152 *tp)
1570{
1571 u32 ocp_data;
1572
1573 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1574 ocp_data &= ~FMC_FCR_MCU_EN;
1575 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1576 ocp_data |= FMC_FCR_MCU_EN;
1577 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1578}
1579
1580static void rtl8152_nic_reset(struct r8152 *tp)
1581{
1582 int i;
1583
1584 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1585
1586 for (i = 0; i < 1000; i++) {
1587 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1588 break;
1589 udelay(100);
1590 }
1591}
1592
hayeswangdd1b1192013-11-20 17:30:56 +08001593static void set_tx_qlen(struct r8152 *tp)
1594{
1595 struct net_device *netdev = tp->netdev;
1596
1597 tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1598 sizeof(struct tx_desc));
1599}
1600
hayeswangac718b62013-05-02 16:01:25 +00001601static inline u8 rtl8152_get_speed(struct r8152 *tp)
1602{
1603 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1604}
1605
hayeswang507605a2014-01-02 11:22:43 +08001606static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001607{
hayeswangebc2ec42013-08-14 20:54:38 +08001608 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00001609 u8 speed;
1610
1611 speed = rtl8152_get_speed(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08001612 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001613 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08001614 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001615 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1616 } else {
1617 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec42013-08-14 20:54:38 +08001618 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001619 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1620 }
hayeswang507605a2014-01-02 11:22:43 +08001621}
1622
1623static int rtl_enable(struct r8152 *tp)
1624{
1625 u32 ocp_data;
1626 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001627
1628 r8152b_reset_packet_filter(tp);
1629
1630 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1631 ocp_data |= CR_RE | CR_TE;
1632 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1633
1634 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1635 ocp_data &= ~RXDY_GATED_EN;
1636 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1637
hayeswangebc2ec42013-08-14 20:54:38 +08001638 INIT_LIST_HEAD(&tp->rx_done);
1639 ret = 0;
1640 for (i = 0; i < RTL8152_MAX_RX; i++) {
1641 INIT_LIST_HEAD(&tp->rx_info[i].list);
1642 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1643 }
hayeswangac718b62013-05-02 16:01:25 +00001644
hayeswangebc2ec42013-08-14 20:54:38 +08001645 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001646}
1647
hayeswang507605a2014-01-02 11:22:43 +08001648static int rtl8152_enable(struct r8152 *tp)
1649{
1650 set_tx_qlen(tp);
1651 rtl_set_eee_plus(tp);
1652
1653 return rtl_enable(tp);
1654}
1655
hayeswang43779f82014-01-02 11:25:10 +08001656static void r8153_set_rx_agg(struct r8152 *tp)
1657{
1658 u8 speed;
1659
1660 speed = rtl8152_get_speed(tp);
1661 if (speed & _1000bps) {
1662 if (tp->udev->speed == USB_SPEED_SUPER) {
1663 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1664 RX_THR_SUPPER);
1665 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1666 EARLY_AGG_SUPPER);
1667 } else {
1668 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
1669 RX_THR_HIGH);
1670 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1671 EARLY_AGG_HIGH);
1672 }
1673 } else {
1674 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
1675 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
1676 EARLY_AGG_SLOW);
1677 }
1678}
1679
1680static int rtl8153_enable(struct r8152 *tp)
1681{
1682 set_tx_qlen(tp);
1683 rtl_set_eee_plus(tp);
1684 r8153_set_rx_agg(tp);
1685
1686 return rtl_enable(tp);
1687}
1688
hayeswangac718b62013-05-02 16:01:25 +00001689static void rtl8152_disable(struct r8152 *tp)
1690{
hayeswangebc2ec42013-08-14 20:54:38 +08001691 struct net_device_stats *stats = rtl8152_get_stats(tp->netdev);
1692 struct sk_buff *skb;
1693 u32 ocp_data;
1694 int i;
hayeswangac718b62013-05-02 16:01:25 +00001695
1696 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1697 ocp_data &= ~RCR_ACPT_ALL;
1698 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1699
hayeswangebc2ec42013-08-14 20:54:38 +08001700 while ((skb = skb_dequeue(&tp->tx_queue))) {
1701 dev_kfree_skb(skb);
1702 stats->tx_dropped++;
1703 }
1704
1705 for (i = 0; i < RTL8152_MAX_TX; i++)
1706 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001707
1708 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1709 ocp_data |= RXDY_GATED_EN;
1710 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1711
1712 for (i = 0; i < 1000; i++) {
1713 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1714 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1715 break;
1716 mdelay(1);
1717 }
1718
1719 for (i = 0; i < 1000; i++) {
1720 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1721 break;
1722 mdelay(1);
1723 }
1724
hayeswangebc2ec42013-08-14 20:54:38 +08001725 for (i = 0; i < RTL8152_MAX_RX; i++)
1726 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001727
1728 rtl8152_nic_reset(tp);
1729}
1730
1731static void r8152b_exit_oob(struct r8152 *tp)
1732{
1733 u32 ocp_data;
1734 int i;
1735
1736 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1737 ocp_data &= ~RCR_ACPT_ALL;
1738 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1739
1740 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1741 ocp_data |= RXDY_GATED_EN;
1742 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1743
1744 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1745 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1746
1747 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1748 ocp_data &= ~NOW_IS_OOB;
1749 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1750
1751 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1752 ocp_data &= ~MCU_BORW_EN;
1753 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1754
1755 for (i = 0; i < 1000; i++) {
1756 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1757 if (ocp_data & LINK_LIST_READY)
1758 break;
1759 mdelay(1);
1760 }
1761
1762 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1763 ocp_data |= RE_INIT_LL;
1764 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1765
1766 for (i = 0; i < 1000; i++) {
1767 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1768 if (ocp_data & LINK_LIST_READY)
1769 break;
1770 mdelay(1);
1771 }
1772
1773 rtl8152_nic_reset(tp);
1774
1775 /* rx share fifo credit full threshold */
1776 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1777
1778 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1779 ocp_data &= STAT_SPEED_MASK;
1780 if (ocp_data == STAT_SPEED_FULL) {
1781 /* rx share fifo credit near full threshold */
1782 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1783 RXFIFO_THR2_FULL);
1784 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1785 RXFIFO_THR3_FULL);
1786 } else {
1787 /* rx share fifo credit near full threshold */
1788 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1789 RXFIFO_THR2_HIGH);
1790 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1791 RXFIFO_THR3_HIGH);
1792 }
1793
1794 /* TX share fifo free credit full threshold */
1795 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1796
1797 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08001798 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00001799 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1800 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1801
1802 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1803 ocp_data &= ~CPCR_RX_VLAN;
1804 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1805
1806 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1807
1808 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1809 ocp_data |= TCR0_AUTO_FIFO;
1810 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1811}
1812
1813static void r8152b_enter_oob(struct r8152 *tp)
1814{
hayeswang45f4a192014-01-06 17:08:41 +08001815 u32 ocp_data;
1816 int i;
hayeswangac718b62013-05-02 16:01:25 +00001817
1818 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1819 ocp_data &= ~NOW_IS_OOB;
1820 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1821
1822 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1823 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1824 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1825
1826 rtl8152_disable(tp);
1827
1828 for (i = 0; i < 1000; i++) {
1829 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1830 if (ocp_data & LINK_LIST_READY)
1831 break;
1832 mdelay(1);
1833 }
1834
1835 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1836 ocp_data |= RE_INIT_LL;
1837 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1838
1839 for (i = 0; i < 1000; i++) {
1840 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1841 if (ocp_data & LINK_LIST_READY)
1842 break;
1843 mdelay(1);
1844 }
1845
1846 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1847
1848 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1849 ocp_data |= MAGIC_EN;
1850 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1851
1852 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1853 ocp_data |= CPCR_RX_VLAN;
1854 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1855
1856 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1857 ocp_data |= ALDPS_PROXY_MODE;
1858 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1859
1860 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1861 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1862 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1863
1864 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1865
1866 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1867 ocp_data &= ~RXDY_GATED_EN;
1868 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1869
1870 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1871 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1872 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1873}
1874
1875static void r8152b_disable_aldps(struct r8152 *tp)
1876{
1877 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1878 msleep(20);
1879}
1880
1881static inline void r8152b_enable_aldps(struct r8152 *tp)
1882{
1883 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1884 LINKENA | DIS_SDSAVE);
1885}
1886
hayeswang43779f82014-01-02 11:25:10 +08001887static void r8153_hw_phy_cfg(struct r8152 *tp)
1888{
1889 u32 ocp_data;
1890 u16 data;
1891
1892 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
1893 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1894
1895 if (tp->version == RTL_VER_03) {
1896 data = ocp_reg_read(tp, OCP_EEE_CFG);
1897 data &= ~CTAP_SHORT_EN;
1898 ocp_reg_write(tp, OCP_EEE_CFG, data);
1899 }
1900
1901 data = ocp_reg_read(tp, OCP_POWER_CFG);
1902 data |= EEE_CLKDIV_EN;
1903 ocp_reg_write(tp, OCP_POWER_CFG, data);
1904
1905 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
1906 data |= EN_10M_BGOFF;
1907 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
1908 data = ocp_reg_read(tp, OCP_POWER_CFG);
1909 data |= EN_10M_PLLOFF;
1910 ocp_reg_write(tp, OCP_POWER_CFG, data);
1911 data = sram_read(tp, SRAM_IMPEDANCE);
1912 data &= ~RX_DRIVING_MASK;
1913 sram_write(tp, SRAM_IMPEDANCE, data);
1914
1915 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1916 ocp_data |= PFM_PWM_SWITCH;
1917 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1918
1919 data = sram_read(tp, SRAM_LPF_CFG);
1920 data |= LPF_AUTO_TUNE;
1921 sram_write(tp, SRAM_LPF_CFG, data);
1922
1923 data = sram_read(tp, SRAM_10M_AMP1);
1924 data |= GDAC_IB_UPALL;
1925 sram_write(tp, SRAM_10M_AMP1, data);
1926 data = sram_read(tp, SRAM_10M_AMP2);
1927 data |= AMP_DN;
1928 sram_write(tp, SRAM_10M_AMP2, data);
1929}
1930
1931static void r8153_u1u2en(struct r8152 *tp, int enable)
1932{
1933 u8 u1u2[8];
1934
1935 if (enable)
1936 memset(u1u2, 0xff, sizeof(u1u2));
1937 else
1938 memset(u1u2, 0x00, sizeof(u1u2));
1939
1940 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
1941}
1942
1943static void r8153_u2p3en(struct r8152 *tp, int enable)
1944{
1945 u32 ocp_data;
1946
1947 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
1948 if (enable)
1949 ocp_data |= U2P3_ENABLE;
1950 else
1951 ocp_data &= ~U2P3_ENABLE;
1952 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
1953}
1954
1955static void r8153_power_cut_en(struct r8152 *tp, int enable)
1956{
1957 u32 ocp_data;
1958
1959 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
1960 if (enable)
1961 ocp_data |= PWR_EN | PHASE2_EN;
1962 else
1963 ocp_data &= ~(PWR_EN | PHASE2_EN);
1964 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
1965
1966 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1967 ocp_data &= ~PCUT_STATUS;
1968 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
1969}
1970
1971static void r8153_teredo_off(struct r8152 *tp)
1972{
1973 u32 ocp_data;
1974
1975 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
1976 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
1977 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
1978
1979 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
1980 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
1981 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
1982}
1983
1984static void r8153_first_init(struct r8152 *tp)
1985{
1986 u32 ocp_data;
1987 int i;
1988
1989 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1990 ocp_data |= RXDY_GATED_EN;
1991 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1992
1993 r8153_teredo_off(tp);
1994
1995 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1996 ocp_data &= ~RCR_ACPT_ALL;
1997 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1998
1999 r8153_hw_phy_cfg(tp);
2000
2001 rtl8152_nic_reset(tp);
2002
2003 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2004 ocp_data &= ~NOW_IS_OOB;
2005 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2006
2007 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2008 ocp_data &= ~MCU_BORW_EN;
2009 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2010
2011 for (i = 0; i < 1000; i++) {
2012 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2013 if (ocp_data & LINK_LIST_READY)
2014 break;
2015 mdelay(1);
2016 }
2017
2018 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2019 ocp_data |= RE_INIT_LL;
2020 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2021
2022 for (i = 0; i < 1000; i++) {
2023 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2024 if (ocp_data & LINK_LIST_READY)
2025 break;
2026 mdelay(1);
2027 }
2028
2029 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2030 ocp_data &= ~CPCR_RX_VLAN;
2031 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2032
2033 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2034
2035 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2036 ocp_data |= TCR0_AUTO_FIFO;
2037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2038
2039 rtl8152_nic_reset(tp);
2040
2041 /* rx share fifo credit full threshold */
2042 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2043 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2044 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2045 /* TX share fifo free credit full threshold */
2046 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2047
2048 // rx aggregation
2049 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2050 ocp_data &= ~RX_AGG_DISABLE;
2051 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2052}
2053
2054static void r8153_enter_oob(struct r8152 *tp)
2055{
2056 u32 ocp_data;
2057 int i;
2058
2059 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2060 ocp_data &= ~NOW_IS_OOB;
2061 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2062
2063 rtl8152_disable(tp);
2064
2065 for (i = 0; i < 1000; i++) {
2066 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2067 if (ocp_data & LINK_LIST_READY)
2068 break;
2069 mdelay(1);
2070 }
2071
2072 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2073 ocp_data |= RE_INIT_LL;
2074 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2075
2076 for (i = 0; i < 1000; i++) {
2077 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2078 if (ocp_data & LINK_LIST_READY)
2079 break;
2080 mdelay(1);
2081 }
2082
2083 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2084
2085 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2086 ocp_data |= MAGIC_EN;
2087 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2088
2089 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2090 ocp_data &= ~TEREDO_WAKE_MASK;
2091 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2092
2093 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2094 ocp_data |= CPCR_RX_VLAN;
2095 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2096
2097 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2098 ocp_data |= ALDPS_PROXY_MODE;
2099 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2100
2101 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2102 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2103 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2104
2105 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
2106
2107 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2108 ocp_data &= ~RXDY_GATED_EN;
2109 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2110
2111 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2112 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2113 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2114}
2115
2116static void r8153_disable_aldps(struct r8152 *tp)
2117{
2118 u16 data;
2119
2120 data = ocp_reg_read(tp, OCP_POWER_CFG);
2121 data &= ~EN_ALDPS;
2122 ocp_reg_write(tp, OCP_POWER_CFG, data);
2123 msleep(20);
2124}
2125
2126static void r8153_enable_aldps(struct r8152 *tp)
2127{
2128 u16 data;
2129
2130 data = ocp_reg_read(tp, OCP_POWER_CFG);
2131 data |= EN_ALDPS;
2132 ocp_reg_write(tp, OCP_POWER_CFG, data);
2133}
2134
hayeswangac718b62013-05-02 16:01:25 +00002135static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2136{
hayeswang43779f82014-01-02 11:25:10 +08002137 u16 bmcr, anar, gbcr;
hayeswangac718b62013-05-02 16:01:25 +00002138 int ret = 0;
2139
2140 cancel_delayed_work_sync(&tp->schedule);
2141 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2142 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2143 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08002144 if (tp->mii.supports_gmii) {
2145 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2146 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2147 } else {
2148 gbcr = 0;
2149 }
hayeswangac718b62013-05-02 16:01:25 +00002150
2151 if (autoneg == AUTONEG_DISABLE) {
2152 if (speed == SPEED_10) {
2153 bmcr = 0;
2154 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2155 } else if (speed == SPEED_100) {
2156 bmcr = BMCR_SPEED100;
2157 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang43779f82014-01-02 11:25:10 +08002158 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2159 bmcr = BMCR_SPEED1000;
2160 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswangac718b62013-05-02 16:01:25 +00002161 } else {
2162 ret = -EINVAL;
2163 goto out;
2164 }
2165
2166 if (duplex == DUPLEX_FULL)
2167 bmcr |= BMCR_FULLDPLX;
2168 } else {
2169 if (speed == SPEED_10) {
2170 if (duplex == DUPLEX_FULL)
2171 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2172 else
2173 anar |= ADVERTISE_10HALF;
2174 } else if (speed == SPEED_100) {
2175 if (duplex == DUPLEX_FULL) {
2176 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2177 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2178 } else {
2179 anar |= ADVERTISE_10HALF;
2180 anar |= ADVERTISE_100HALF;
2181 }
hayeswang43779f82014-01-02 11:25:10 +08002182 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2183 if (duplex == DUPLEX_FULL) {
2184 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2185 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2186 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2187 } else {
2188 anar |= ADVERTISE_10HALF;
2189 anar |= ADVERTISE_100HALF;
2190 gbcr |= ADVERTISE_1000HALF;
2191 }
hayeswangac718b62013-05-02 16:01:25 +00002192 } else {
2193 ret = -EINVAL;
2194 goto out;
2195 }
2196
2197 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2198 }
2199
hayeswang43779f82014-01-02 11:25:10 +08002200 if (tp->mii.supports_gmii)
2201 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2202
hayeswangac718b62013-05-02 16:01:25 +00002203 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2204 r8152_mdio_write(tp, MII_BMCR, bmcr);
2205
2206out:
hayeswangac718b62013-05-02 16:01:25 +00002207
2208 return ret;
2209}
2210
2211static void rtl8152_down(struct r8152 *tp)
2212{
2213 u32 ocp_data;
2214
2215 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2216 ocp_data &= ~POWER_CUT;
2217 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2218
2219 r8152b_disable_aldps(tp);
2220 r8152b_enter_oob(tp);
2221 r8152b_enable_aldps(tp);
2222}
2223
hayeswang43779f82014-01-02 11:25:10 +08002224static void rtl8153_down(struct r8152 *tp)
2225{
2226 r8153_u1u2en(tp, 0);
2227 r8153_power_cut_en(tp, 0);
2228 r8153_disable_aldps(tp);
2229 r8153_enter_oob(tp);
2230 r8153_enable_aldps(tp);
2231}
2232
hayeswangac718b62013-05-02 16:01:25 +00002233static void set_carrier(struct r8152 *tp)
2234{
2235 struct net_device *netdev = tp->netdev;
2236 u8 speed;
2237
hayeswang40a82912013-08-14 20:54:40 +08002238 clear_bit(RTL8152_LINK_CHG, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002239 speed = rtl8152_get_speed(tp);
2240
2241 if (speed & LINK_STATUS) {
2242 if (!(tp->speed & LINK_STATUS)) {
hayeswangc81229c2014-01-02 11:22:42 +08002243 tp->rtl_ops.enable(tp);
hayeswangac718b62013-05-02 16:01:25 +00002244 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2245 netif_carrier_on(netdev);
2246 }
2247 } else {
2248 if (tp->speed & LINK_STATUS) {
2249 netif_carrier_off(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08002250 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002251 tp->rtl_ops.disable(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002252 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002253 }
2254 }
2255 tp->speed = speed;
2256}
2257
2258static void rtl_work_func_t(struct work_struct *work)
2259{
2260 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2261
2262 if (!test_bit(WORK_ENABLE, &tp->flags))
2263 goto out1;
2264
2265 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2266 goto out1;
2267
hayeswang40a82912013-08-14 20:54:40 +08002268 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2269 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00002270
2271 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2272 _rtl8152_set_rx_mode(tp->netdev);
2273
hayeswangac718b62013-05-02 16:01:25 +00002274out1:
2275 return;
2276}
2277
2278static int rtl8152_open(struct net_device *netdev)
2279{
2280 struct r8152 *tp = netdev_priv(netdev);
2281 int res = 0;
2282
hayeswang40a82912013-08-14 20:54:40 +08002283 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2284 if (res) {
2285 if (res == -ENODEV)
2286 netif_device_detach(tp->netdev);
2287 netif_warn(tp, ifup, netdev,
2288 "intr_urb submit failed: %d\n", res);
2289 return res;
hayeswangac718b62013-05-02 16:01:25 +00002290 }
2291
hayeswang43779f82014-01-02 11:25:10 +08002292 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2293 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2294 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002295 tp->speed = 0;
2296 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002297 netif_start_queue(netdev);
2298 set_bit(WORK_ENABLE, &tp->flags);
hayeswangac718b62013-05-02 16:01:25 +00002299
2300 return res;
2301}
2302
2303static int rtl8152_close(struct net_device *netdev)
2304{
2305 struct r8152 *tp = netdev_priv(netdev);
2306 int res = 0;
2307
hayeswang40a82912013-08-14 20:54:40 +08002308 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002309 clear_bit(WORK_ENABLE, &tp->flags);
2310 cancel_delayed_work_sync(&tp->schedule);
2311 netif_stop_queue(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08002312 tasklet_disable(&tp->tl);
hayeswangc81229c2014-01-02 11:22:42 +08002313 tp->rtl_ops.disable(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002314 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002315
2316 return res;
2317}
2318
2319static void rtl_clear_bp(struct r8152 *tp)
2320{
2321 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
2322 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
2323 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
2324 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
2325 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
2326 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
2327 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
2328 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
2329 mdelay(3);
2330 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
2331 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
2332}
2333
hayeswang43779f82014-01-02 11:25:10 +08002334static void r8153_clear_bp(struct r8152 *tp)
2335{
2336 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
2337 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
2338 rtl_clear_bp(tp);
2339}
2340
hayeswangac718b62013-05-02 16:01:25 +00002341static void r8152b_enable_eee(struct r8152 *tp)
2342{
hayeswang45f4a192014-01-06 17:08:41 +08002343 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002344
2345 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2346 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2347 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2348 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
2349 EEE_10_CAP | EEE_NWAY_EN |
2350 TX_QUIET_EN | RX_QUIET_EN |
2351 SDRISETIME | RG_RXLPI_MSK_HFDUP |
2352 SDFALLTIME);
2353 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
2354 RG_LDVQUIET_EN | RG_CKRSEL |
2355 RG_EEEPRG_EN);
2356 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
2357 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
2358 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
2359 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
2360 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
2361 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2362}
2363
hayeswang43779f82014-01-02 11:25:10 +08002364static void r8153_enable_eee(struct r8152 *tp)
2365{
2366 u32 ocp_data;
2367 u16 data;
2368
2369 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2370 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2371 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2372 data = ocp_reg_read(tp, OCP_EEE_CFG);
2373 data |= EEE10_EN;
2374 ocp_reg_write(tp, OCP_EEE_CFG, data);
2375 data = ocp_reg_read(tp, OCP_EEE_CFG2);
2376 data |= MY1000_EEE | MY100_EEE;
2377 ocp_reg_write(tp, OCP_EEE_CFG2, data);
2378}
2379
hayeswangac718b62013-05-02 16:01:25 +00002380static void r8152b_enable_fc(struct r8152 *tp)
2381{
2382 u16 anar;
2383
2384 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2385 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
2386 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2387}
2388
2389static void r8152b_hw_phy_cfg(struct r8152 *tp)
2390{
2391 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
2392 r8152b_disable_aldps(tp);
2393}
2394
2395static void r8152b_init(struct r8152 *tp)
2396{
hayeswangebc2ec42013-08-14 20:54:38 +08002397 u32 ocp_data;
2398 int i;
hayeswangac718b62013-05-02 16:01:25 +00002399
2400 rtl_clear_bp(tp);
2401
2402 if (tp->version == RTL_VER_01) {
2403 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2404 ocp_data &= ~LED_MODE_MASK;
2405 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2406 }
2407
2408 r8152b_hw_phy_cfg(tp);
2409
2410 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2411 ocp_data &= ~POWER_CUT;
2412 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2413
2414 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
hayeswang8e1f51b2014-01-02 11:22:41 +08002415 ocp_data &= ~RESUME_INDICATE;
hayeswangac718b62013-05-02 16:01:25 +00002416 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2417
2418 r8152b_exit_oob(tp);
2419
2420 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2421 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
2422 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2423 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
2424 ocp_data &= ~MCU_CLK_RATIO_MASK;
2425 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
2426 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
2427 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
2428 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
2429 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
2430
2431 r8152b_enable_eee(tp);
2432 r8152b_enable_aldps(tp);
2433 r8152b_enable_fc(tp);
2434
2435 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2436 BMCR_ANRESTART);
2437 for (i = 0; i < 100; i++) {
2438 udelay(100);
2439 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
2440 break;
2441 }
2442
hayeswangebc2ec42013-08-14 20:54:38 +08002443 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00002444 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec42013-08-14 20:54:38 +08002445 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00002446 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2447}
2448
hayeswang43779f82014-01-02 11:25:10 +08002449static void r8153_init(struct r8152 *tp)
2450{
2451 u32 ocp_data;
2452 int i;
2453
2454 r8153_u1u2en(tp, 0);
2455
2456 for (i = 0; i < 500; i++) {
2457 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
2458 AUTOLOAD_DONE)
2459 break;
2460 msleep(20);
2461 }
2462
2463 for (i = 0; i < 500; i++) {
2464 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
2465 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
2466 break;
2467 msleep(20);
2468 }
2469
2470 r8153_u2p3en(tp, 0);
2471
2472 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
2473 ocp_data &= ~TIMER11_EN;
2474 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
2475
2476 r8153_clear_bp(tp);
2477
2478 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
2479 ocp_data &= ~LED_MODE_MASK;
2480 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
2481
2482 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
2483 ocp_data &= ~LPM_TIMER_MASK;
2484 if (tp->udev->speed == USB_SPEED_SUPER)
2485 ocp_data |= LPM_TIMER_500US;
2486 else
2487 ocp_data |= LPM_TIMER_500MS;
2488 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
2489
2490 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
2491 ocp_data &= ~SEN_VAL_MASK;
2492 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
2493 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
2494
2495 r8153_power_cut_en(tp, 0);
2496 r8153_u1u2en(tp, 1);
2497
2498 r8153_first_init(tp);
2499
2500 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
2501 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
2502 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2503 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2504 U1U2_SPDWN_EN | L1_SPDWN_EN);
2505 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2506 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2507 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
2508 EEE_SPDWN_EN);
2509
2510 r8153_enable_eee(tp);
2511 r8153_enable_aldps(tp);
2512 r8152b_enable_fc(tp);
2513
2514 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
2515 BMCR_ANRESTART);
2516}
2517
hayeswangac718b62013-05-02 16:01:25 +00002518static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
2519{
2520 struct r8152 *tp = usb_get_intfdata(intf);
2521
2522 netif_device_detach(tp->netdev);
2523
2524 if (netif_running(tp->netdev)) {
2525 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002526 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00002527 cancel_delayed_work_sync(&tp->schedule);
hayeswangebc2ec42013-08-14 20:54:38 +08002528 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002529 }
2530
hayeswangc81229c2014-01-02 11:22:42 +08002531 tp->rtl_ops.down(tp);
hayeswangac718b62013-05-02 16:01:25 +00002532
2533 return 0;
2534}
2535
2536static int rtl8152_resume(struct usb_interface *intf)
2537{
2538 struct r8152 *tp = usb_get_intfdata(intf);
2539
hayeswangc81229c2014-01-02 11:22:42 +08002540 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002541 netif_device_attach(tp->netdev);
2542 if (netif_running(tp->netdev)) {
hayeswang43779f82014-01-02 11:25:10 +08002543 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2544 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2545 DUPLEX_FULL);
hayeswang40a82912013-08-14 20:54:40 +08002546 tp->speed = 0;
2547 netif_carrier_off(tp->netdev);
hayeswangac718b62013-05-02 16:01:25 +00002548 set_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002549 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswangebc2ec42013-08-14 20:54:38 +08002550 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00002551 }
2552
2553 return 0;
2554}
2555
2556static void rtl8152_get_drvinfo(struct net_device *netdev,
2557 struct ethtool_drvinfo *info)
2558{
2559 struct r8152 *tp = netdev_priv(netdev);
2560
2561 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
2562 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
2563 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
2564}
2565
2566static
2567int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
2568{
2569 struct r8152 *tp = netdev_priv(netdev);
2570
2571 if (!tp->mii.mdio_read)
2572 return -EOPNOTSUPP;
2573
2574 return mii_ethtool_gset(&tp->mii, cmd);
2575}
2576
2577static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2578{
2579 struct r8152 *tp = netdev_priv(dev);
2580
2581 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
2582}
2583
2584static struct ethtool_ops ops = {
2585 .get_drvinfo = rtl8152_get_drvinfo,
2586 .get_settings = rtl8152_get_settings,
2587 .set_settings = rtl8152_set_settings,
2588 .get_link = ethtool_op_get_link,
2589};
2590
2591static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
2592{
2593 struct r8152 *tp = netdev_priv(netdev);
2594 struct mii_ioctl_data *data = if_mii(rq);
2595 int res = 0;
2596
2597 switch (cmd) {
2598 case SIOCGMIIPHY:
2599 data->phy_id = R8152_PHY_ID; /* Internal PHY */
2600 break;
2601
2602 case SIOCGMIIREG:
2603 data->val_out = r8152_mdio_read(tp, data->reg_num);
2604 break;
2605
2606 case SIOCSMIIREG:
2607 if (!capable(CAP_NET_ADMIN)) {
2608 res = -EPERM;
2609 break;
2610 }
2611 r8152_mdio_write(tp, data->reg_num, data->val_in);
2612 break;
2613
2614 default:
2615 res = -EOPNOTSUPP;
2616 }
2617
2618 return res;
2619}
2620
2621static const struct net_device_ops rtl8152_netdev_ops = {
2622 .ndo_open = rtl8152_open,
2623 .ndo_stop = rtl8152_close,
2624 .ndo_do_ioctl = rtl8152_ioctl,
2625 .ndo_start_xmit = rtl8152_start_xmit,
2626 .ndo_tx_timeout = rtl8152_tx_timeout,
2627 .ndo_set_rx_mode = rtl8152_set_rx_mode,
2628 .ndo_set_mac_address = rtl8152_set_mac_address,
2629
2630 .ndo_change_mtu = eth_change_mtu,
2631 .ndo_validate_addr = eth_validate_addr,
2632};
2633
2634static void r8152b_get_version(struct r8152 *tp)
2635{
2636 u32 ocp_data;
2637 u16 version;
2638
2639 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2640 version = (u16)(ocp_data & VERSION_MASK);
2641
2642 switch (version) {
2643 case 0x4c00:
2644 tp->version = RTL_VER_01;
2645 break;
2646 case 0x4c10:
2647 tp->version = RTL_VER_02;
2648 break;
hayeswang43779f82014-01-02 11:25:10 +08002649 case 0x5c00:
2650 tp->version = RTL_VER_03;
2651 tp->mii.supports_gmii = 1;
2652 break;
2653 case 0x5c10:
2654 tp->version = RTL_VER_04;
2655 tp->mii.supports_gmii = 1;
2656 break;
2657 case 0x5c20:
2658 tp->version = RTL_VER_05;
2659 tp->mii.supports_gmii = 1;
2660 break;
hayeswangac718b62013-05-02 16:01:25 +00002661 default:
2662 netif_info(tp, probe, tp->netdev,
2663 "Unknown version 0x%04x\n", version);
2664 break;
2665 }
2666}
2667
hayeswange3fe0b12014-01-02 11:22:39 +08002668static void rtl8152_unload(struct r8152 *tp)
2669{
2670 u32 ocp_data;
2671
2672 if (tp->version != RTL_VER_01) {
2673 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2674 ocp_data |= POWER_CUT;
2675 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2676 }
2677
2678 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
hayeswang8e1f51b2014-01-02 11:22:41 +08002679 ocp_data &= ~RESUME_INDICATE;
hayeswange3fe0b12014-01-02 11:22:39 +08002680 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2681}
2682
hayeswang43779f82014-01-02 11:25:10 +08002683static void rtl8153_unload(struct r8152 *tp)
2684{
2685 r8153_power_cut_en(tp, 1);
2686}
2687
hayeswangc81229c2014-01-02 11:22:42 +08002688static bool rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
2689{
2690 struct rtl_ops *ops = &tp->rtl_ops;
2691 bool ret = true;
2692
2693 switch (id->idVendor) {
2694 case VENDOR_ID_REALTEK:
2695 switch (id->idProduct) {
2696 case PRODUCT_ID_RTL8152:
2697 ops->init = r8152b_init;
2698 ops->enable = rtl8152_enable;
2699 ops->disable = rtl8152_disable;
2700 ops->down = rtl8152_down;
2701 ops->unload = rtl8152_unload;
2702 break;
hayeswang43779f82014-01-02 11:25:10 +08002703 case PRODUCT_ID_RTL8153:
2704 ops->init = r8153_init;
2705 ops->enable = rtl8153_enable;
2706 ops->disable = rtl8152_disable;
2707 ops->down = rtl8153_down;
2708 ops->unload = rtl8153_unload;
2709 break;
2710 default:
hayeswang45ea3932014-01-03 11:21:56 +08002711 ret = false;
hayeswang43779f82014-01-02 11:25:10 +08002712 break;
2713 }
2714 break;
2715
2716 case VENDOR_ID_SAMSUNG:
2717 switch (id->idProduct) {
2718 case PRODUCT_ID_SAMSUNG:
2719 ops->init = r8153_init;
2720 ops->enable = rtl8153_enable;
2721 ops->disable = rtl8152_disable;
2722 ops->down = rtl8153_down;
2723 ops->unload = rtl8153_unload;
2724 break;
hayeswangc81229c2014-01-02 11:22:42 +08002725 default:
2726 ret = false;
2727 break;
2728 }
2729 break;
2730
2731 default:
2732 ret = false;
2733 break;
2734 }
2735
2736 return ret;
2737}
2738
hayeswangac718b62013-05-02 16:01:25 +00002739static int rtl8152_probe(struct usb_interface *intf,
2740 const struct usb_device_id *id)
2741{
2742 struct usb_device *udev = interface_to_usbdev(intf);
2743 struct r8152 *tp;
2744 struct net_device *netdev;
hayeswangebc2ec42013-08-14 20:54:38 +08002745 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002746
2747 if (udev->actconfig->desc.bConfigurationValue != 1) {
2748 usb_driver_set_configuration(udev, 1);
2749 return -ENODEV;
2750 }
2751
2752 netdev = alloc_etherdev(sizeof(struct r8152));
2753 if (!netdev) {
2754 dev_err(&intf->dev, "Out of memory");
2755 return -ENOMEM;
2756 }
2757
hayeswangebc2ec42013-08-14 20:54:38 +08002758 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00002759 tp = netdev_priv(netdev);
2760 tp->msg_enable = 0x7FFF;
2761
hayeswange3ad4122014-01-06 17:08:42 +08002762 tp->udev = udev;
2763 tp->netdev = netdev;
2764 tp->intf = intf;
2765
hayeswangc81229c2014-01-02 11:22:42 +08002766 if (!rtl_ops_init(tp, id)) {
2767 netif_err(tp, probe, netdev, "Unknown Device");
2768 return -ENODEV;
2769 }
2770
hayeswangebc2ec42013-08-14 20:54:38 +08002771 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00002772 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2773
hayeswangac718b62013-05-02 16:01:25 +00002774 netdev->netdev_ops = &rtl8152_netdev_ops;
2775 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08002776
2777 netdev->features |= NETIF_F_IP_CSUM;
2778 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00002779 SET_ETHTOOL_OPS(netdev, &ops);
hayeswangac718b62013-05-02 16:01:25 +00002780
2781 tp->mii.dev = netdev;
2782 tp->mii.mdio_read = read_mii_word;
2783 tp->mii.mdio_write = write_mii_word;
2784 tp->mii.phy_id_mask = 0x3f;
2785 tp->mii.reg_num_mask = 0x1f;
2786 tp->mii.phy_id = R8152_PHY_ID;
2787 tp->mii.supports_gmii = 0;
2788
2789 r8152b_get_version(tp);
hayeswangc81229c2014-01-02 11:22:42 +08002790 tp->rtl_ops.init(tp);
hayeswangac718b62013-05-02 16:01:25 +00002791 set_ethernet_addr(tp);
2792
hayeswangebc2ec42013-08-14 20:54:38 +08002793 ret = alloc_all_mem(tp);
2794 if (ret)
hayeswangac718b62013-05-02 16:01:25 +00002795 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002796
2797 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00002798
hayeswangebc2ec42013-08-14 20:54:38 +08002799 ret = register_netdev(netdev);
2800 if (ret != 0) {
hayeswangac718b62013-05-02 16:01:25 +00002801 netif_err(tp, probe, netdev, "couldn't register the device");
hayeswangebc2ec42013-08-14 20:54:38 +08002802 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00002803 }
2804
2805 netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
2806
2807 return 0;
2808
hayeswangac718b62013-05-02 16:01:25 +00002809out1:
hayeswangebc2ec42013-08-14 20:54:38 +08002810 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00002811out:
2812 free_netdev(netdev);
hayeswangebc2ec42013-08-14 20:54:38 +08002813 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002814}
2815
hayeswangac718b62013-05-02 16:01:25 +00002816static void rtl8152_disconnect(struct usb_interface *intf)
2817{
2818 struct r8152 *tp = usb_get_intfdata(intf);
2819
2820 usb_set_intfdata(intf, NULL);
2821 if (tp) {
2822 set_bit(RTL8152_UNPLUG, &tp->flags);
2823 tasklet_kill(&tp->tl);
2824 unregister_netdev(tp->netdev);
hayeswangc81229c2014-01-02 11:22:42 +08002825 tp->rtl_ops.unload(tp);
hayeswangebc2ec42013-08-14 20:54:38 +08002826 free_all_mem(tp);
hayeswangac718b62013-05-02 16:01:25 +00002827 free_netdev(tp->netdev);
2828 }
2829}
2830
2831/* table of devices that work with this driver */
2832static struct usb_device_id rtl8152_table[] = {
2833 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
hayeswang43779f82014-01-02 11:25:10 +08002834 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
2835 {USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
hayeswangac718b62013-05-02 16:01:25 +00002836 {}
2837};
2838
2839MODULE_DEVICE_TABLE(usb, rtl8152_table);
2840
2841static struct usb_driver rtl8152_driver = {
2842 .name = MODULENAME,
hayeswangebc2ec42013-08-14 20:54:38 +08002843 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00002844 .probe = rtl8152_probe,
2845 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00002846 .suspend = rtl8152_suspend,
hayeswangebc2ec42013-08-14 20:54:38 +08002847 .resume = rtl8152_resume,
2848 .reset_resume = rtl8152_resume,
hayeswangac718b62013-05-02 16:01:25 +00002849};
2850
Sachin Kamatb4236daa2013-05-16 17:48:08 +00002851module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00002852
2853MODULE_AUTHOR(DRIVER_AUTHOR);
2854MODULE_DESCRIPTION(DRIVER_DESC);
2855MODULE_LICENSE("GPL");