hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1 | /* |
hayeswang | c7de7de | 2014-01-15 10:42:16 +0800 | [diff] [blame] | 2 | * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved. |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 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 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 10 | #include <linux/signal.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/module.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/etherdevice.h> |
| 15 | #include <linux/mii.h> |
| 16 | #include <linux/ethtool.h> |
| 17 | #include <linux/usb.h> |
| 18 | #include <linux/crc32.h> |
| 19 | #include <linux/if_vlan.h> |
| 20 | #include <linux/uaccess.h> |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 21 | #include <linux/list.h> |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 22 | #include <linux/ip.h> |
| 23 | #include <linux/ipv6.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 24 | |
| 25 | /* Version Information */ |
hayeswang | c7de7de | 2014-01-15 10:42:16 +0800 | [diff] [blame] | 26 | #define DRIVER_VERSION "v1.04.0 (2014/01/15)" |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 27 | #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" |
hayeswang | 44d942a | 2014-01-15 10:42:14 +0800 | [diff] [blame] | 28 | #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 29 | #define MODULENAME "r8152" |
| 30 | |
| 31 | #define R8152_PHY_ID 32 |
| 32 | |
| 33 | #define PLA_IDR 0xc000 |
| 34 | #define PLA_RCR 0xc010 |
| 35 | #define PLA_RMS 0xc016 |
| 36 | #define PLA_RXFIFO_CTRL0 0xc0a0 |
| 37 | #define PLA_RXFIFO_CTRL1 0xc0a4 |
| 38 | #define PLA_RXFIFO_CTRL2 0xc0a8 |
| 39 | #define PLA_FMC 0xc0b4 |
| 40 | #define PLA_CFG_WOL 0xc0b6 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 41 | #define PLA_TEREDO_CFG 0xc0bc |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 42 | #define PLA_MAR 0xcd00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 43 | #define PLA_BACKUP 0xd000 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 44 | #define PAL_BDC_CR 0xd1a0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 45 | #define PLA_TEREDO_TIMER 0xd2cc |
| 46 | #define PLA_REALWOW_TIMER 0xd2e8 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 47 | #define PLA_LEDSEL 0xdd90 |
| 48 | #define PLA_LED_FEATURE 0xdd92 |
| 49 | #define PLA_PHYAR 0xde00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 50 | #define PLA_BOOT_CTRL 0xe004 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 51 | #define PLA_GPHY_INTR_IMR 0xe022 |
| 52 | #define PLA_EEE_CR 0xe040 |
| 53 | #define PLA_EEEP_CR 0xe080 |
| 54 | #define PLA_MAC_PWR_CTRL 0xe0c0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 55 | #define PLA_MAC_PWR_CTRL2 0xe0ca |
| 56 | #define PLA_MAC_PWR_CTRL3 0xe0cc |
| 57 | #define PLA_MAC_PWR_CTRL4 0xe0ce |
| 58 | #define PLA_WDT6_CTRL 0xe428 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 59 | #define PLA_TCR0 0xe610 |
| 60 | #define PLA_TCR1 0xe612 |
| 61 | #define PLA_TXFIFO_CTRL 0xe618 |
| 62 | #define PLA_RSTTELLY 0xe800 |
| 63 | #define PLA_CR 0xe813 |
| 64 | #define PLA_CRWECR 0xe81c |
| 65 | #define PLA_CONFIG5 0xe822 |
| 66 | #define PLA_PHY_PWR 0xe84c |
| 67 | #define PLA_OOB_CTRL 0xe84f |
| 68 | #define PLA_CPCR 0xe854 |
| 69 | #define PLA_MISC_0 0xe858 |
| 70 | #define PLA_MISC_1 0xe85a |
| 71 | #define PLA_OCP_GPHY_BASE 0xe86c |
| 72 | #define PLA_TELLYCNT 0xe890 |
| 73 | #define PLA_SFF_STS_7 0xe8de |
| 74 | #define PLA_PHYSTATUS 0xe908 |
| 75 | #define PLA_BP_BA 0xfc26 |
| 76 | #define PLA_BP_0 0xfc28 |
| 77 | #define PLA_BP_1 0xfc2a |
| 78 | #define PLA_BP_2 0xfc2c |
| 79 | #define PLA_BP_3 0xfc2e |
| 80 | #define PLA_BP_4 0xfc30 |
| 81 | #define PLA_BP_5 0xfc32 |
| 82 | #define PLA_BP_6 0xfc34 |
| 83 | #define PLA_BP_7 0xfc36 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 84 | #define PLA_BP_EN 0xfc38 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 85 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 86 | #define USB_U2P3_CTRL 0xb460 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 87 | #define USB_DEV_STAT 0xb808 |
| 88 | #define USB_USB_CTRL 0xd406 |
| 89 | #define USB_PHY_CTRL 0xd408 |
| 90 | #define USB_TX_AGG 0xd40a |
| 91 | #define USB_RX_BUF_TH 0xd40c |
| 92 | #define USB_USB_TIMER 0xd428 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 93 | #define USB_RX_EARLY_AGG 0xd42c |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 94 | #define USB_PM_CTRL_STATUS 0xd432 |
| 95 | #define USB_TX_DMA 0xd434 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 96 | #define USB_TOLERANCE 0xd490 |
| 97 | #define USB_LPM_CTRL 0xd41a |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 98 | #define USB_UPS_CTRL 0xd800 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 99 | #define USB_MISC_0 0xd81a |
| 100 | #define USB_POWER_CUT 0xd80a |
| 101 | #define USB_AFE_CTRL2 0xd824 |
| 102 | #define USB_WDT11_CTRL 0xe43c |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 103 | #define USB_BP_BA 0xfc26 |
| 104 | #define USB_BP_0 0xfc28 |
| 105 | #define USB_BP_1 0xfc2a |
| 106 | #define USB_BP_2 0xfc2c |
| 107 | #define USB_BP_3 0xfc2e |
| 108 | #define USB_BP_4 0xfc30 |
| 109 | #define USB_BP_5 0xfc32 |
| 110 | #define USB_BP_6 0xfc34 |
| 111 | #define USB_BP_7 0xfc36 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 112 | #define USB_BP_EN 0xfc38 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 113 | |
| 114 | /* OCP Registers */ |
| 115 | #define OCP_ALDPS_CONFIG 0x2010 |
| 116 | #define OCP_EEE_CONFIG1 0x2080 |
| 117 | #define OCP_EEE_CONFIG2 0x2092 |
| 118 | #define OCP_EEE_CONFIG3 0x2094 |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 119 | #define OCP_BASE_MII 0xa400 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 120 | #define OCP_EEE_AR 0xa41a |
| 121 | #define OCP_EEE_DATA 0xa41c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 122 | #define OCP_PHY_STATUS 0xa420 |
| 123 | #define OCP_POWER_CFG 0xa430 |
| 124 | #define OCP_EEE_CFG 0xa432 |
| 125 | #define OCP_SRAM_ADDR 0xa436 |
| 126 | #define OCP_SRAM_DATA 0xa438 |
| 127 | #define OCP_DOWN_SPEED 0xa442 |
| 128 | #define OCP_EEE_CFG2 0xa5d0 |
| 129 | #define OCP_ADC_CFG 0xbc06 |
| 130 | |
| 131 | /* SRAM Register */ |
| 132 | #define SRAM_LPF_CFG 0x8012 |
| 133 | #define SRAM_10M_AMP1 0x8080 |
| 134 | #define SRAM_10M_AMP2 0x8082 |
| 135 | #define SRAM_IMPEDANCE 0x8084 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 136 | |
| 137 | /* PLA_RCR */ |
| 138 | #define RCR_AAP 0x00000001 |
| 139 | #define RCR_APM 0x00000002 |
| 140 | #define RCR_AM 0x00000004 |
| 141 | #define RCR_AB 0x00000008 |
| 142 | #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) |
| 143 | |
| 144 | /* PLA_RXFIFO_CTRL0 */ |
| 145 | #define RXFIFO_THR1_NORMAL 0x00080002 |
| 146 | #define RXFIFO_THR1_OOB 0x01800003 |
| 147 | |
| 148 | /* PLA_RXFIFO_CTRL1 */ |
| 149 | #define RXFIFO_THR2_FULL 0x00000060 |
| 150 | #define RXFIFO_THR2_HIGH 0x00000038 |
| 151 | #define RXFIFO_THR2_OOB 0x0000004a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 152 | #define RXFIFO_THR2_NORMAL 0x00a0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 153 | |
| 154 | /* PLA_RXFIFO_CTRL2 */ |
| 155 | #define RXFIFO_THR3_FULL 0x00000078 |
| 156 | #define RXFIFO_THR3_HIGH 0x00000048 |
| 157 | #define RXFIFO_THR3_OOB 0x0000005a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 158 | #define RXFIFO_THR3_NORMAL 0x0110 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 159 | |
| 160 | /* PLA_TXFIFO_CTRL */ |
| 161 | #define TXFIFO_THR_NORMAL 0x00400008 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 162 | #define TXFIFO_THR_NORMAL2 0x01000008 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 163 | |
| 164 | /* PLA_FMC */ |
| 165 | #define FMC_FCR_MCU_EN 0x0001 |
| 166 | |
| 167 | /* PLA_EEEP_CR */ |
| 168 | #define EEEP_CR_EEEP_TX 0x0002 |
| 169 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 170 | /* PLA_WDT6_CTRL */ |
| 171 | #define WDT6_SET_MODE 0x0010 |
| 172 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 173 | /* PLA_TCR0 */ |
| 174 | #define TCR0_TX_EMPTY 0x0800 |
| 175 | #define TCR0_AUTO_FIFO 0x0080 |
| 176 | |
| 177 | /* PLA_TCR1 */ |
| 178 | #define VERSION_MASK 0x7cf0 |
| 179 | |
| 180 | /* PLA_CR */ |
| 181 | #define CR_RST 0x10 |
| 182 | #define CR_RE 0x08 |
| 183 | #define CR_TE 0x04 |
| 184 | |
| 185 | /* PLA_CRWECR */ |
| 186 | #define CRWECR_NORAML 0x00 |
| 187 | #define CRWECR_CONFIG 0xc0 |
| 188 | |
| 189 | /* PLA_OOB_CTRL */ |
| 190 | #define NOW_IS_OOB 0x80 |
| 191 | #define TXFIFO_EMPTY 0x20 |
| 192 | #define RXFIFO_EMPTY 0x10 |
| 193 | #define LINK_LIST_READY 0x02 |
| 194 | #define DIS_MCU_CLROOB 0x01 |
| 195 | #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) |
| 196 | |
| 197 | /* PLA_MISC_1 */ |
| 198 | #define RXDY_GATED_EN 0x0008 |
| 199 | |
| 200 | /* PLA_SFF_STS_7 */ |
| 201 | #define RE_INIT_LL 0x8000 |
| 202 | #define MCU_BORW_EN 0x4000 |
| 203 | |
| 204 | /* PLA_CPCR */ |
| 205 | #define CPCR_RX_VLAN 0x0040 |
| 206 | |
| 207 | /* PLA_CFG_WOL */ |
| 208 | #define MAGIC_EN 0x0001 |
| 209 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 210 | /* PLA_TEREDO_CFG */ |
| 211 | #define TEREDO_SEL 0x8000 |
| 212 | #define TEREDO_WAKE_MASK 0x7f00 |
| 213 | #define TEREDO_RS_EVENT_MASK 0x00fe |
| 214 | #define OOB_TEREDO_EN 0x0001 |
| 215 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 216 | /* PAL_BDC_CR */ |
| 217 | #define ALDPS_PROXY_MODE 0x0001 |
| 218 | |
| 219 | /* PLA_CONFIG5 */ |
| 220 | #define LAN_WAKE_EN 0x0002 |
| 221 | |
| 222 | /* PLA_LED_FEATURE */ |
| 223 | #define LED_MODE_MASK 0x0700 |
| 224 | |
| 225 | /* PLA_PHY_PWR */ |
| 226 | #define TX_10M_IDLE_EN 0x0080 |
| 227 | #define PFM_PWM_SWITCH 0x0040 |
| 228 | |
| 229 | /* PLA_MAC_PWR_CTRL */ |
| 230 | #define D3_CLK_GATED_EN 0x00004000 |
| 231 | #define MCU_CLK_RATIO 0x07010f07 |
| 232 | #define MCU_CLK_RATIO_MASK 0x0f0f0f0f |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 233 | #define ALDPS_SPDWN_RATIO 0x0f87 |
| 234 | |
| 235 | /* PLA_MAC_PWR_CTRL2 */ |
| 236 | #define EEE_SPDWN_RATIO 0x8007 |
| 237 | |
| 238 | /* PLA_MAC_PWR_CTRL3 */ |
| 239 | #define PKT_AVAIL_SPDWN_EN 0x0100 |
| 240 | #define SUSPEND_SPDWN_EN 0x0004 |
| 241 | #define U1U2_SPDWN_EN 0x0002 |
| 242 | #define L1_SPDWN_EN 0x0001 |
| 243 | |
| 244 | /* PLA_MAC_PWR_CTRL4 */ |
| 245 | #define PWRSAVE_SPDWN_EN 0x1000 |
| 246 | #define RXDV_SPDWN_EN 0x0800 |
| 247 | #define TX10MIDLE_EN 0x0100 |
| 248 | #define TP100_SPDWN_EN 0x0020 |
| 249 | #define TP500_SPDWN_EN 0x0010 |
| 250 | #define TP1000_SPDWN_EN 0x0008 |
| 251 | #define EEE_SPDWN_EN 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 252 | |
| 253 | /* PLA_GPHY_INTR_IMR */ |
| 254 | #define GPHY_STS_MSK 0x0001 |
| 255 | #define SPEED_DOWN_MSK 0x0002 |
| 256 | #define SPDWN_RXDV_MSK 0x0004 |
| 257 | #define SPDWN_LINKCHG_MSK 0x0008 |
| 258 | |
| 259 | /* PLA_PHYAR */ |
| 260 | #define PHYAR_FLAG 0x80000000 |
| 261 | |
| 262 | /* PLA_EEE_CR */ |
| 263 | #define EEE_RX_EN 0x0001 |
| 264 | #define EEE_TX_EN 0x0002 |
| 265 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 266 | /* PLA_BOOT_CTRL */ |
| 267 | #define AUTOLOAD_DONE 0x0002 |
| 268 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 269 | /* USB_DEV_STAT */ |
| 270 | #define STAT_SPEED_MASK 0x0006 |
| 271 | #define STAT_SPEED_HIGH 0x0000 |
| 272 | #define STAT_SPEED_FULL 0x0001 |
| 273 | |
| 274 | /* USB_TX_AGG */ |
| 275 | #define TX_AGG_MAX_THRESHOLD 0x03 |
| 276 | |
| 277 | /* USB_RX_BUF_TH */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 278 | #define RX_THR_SUPPER 0x0c350180 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 279 | #define RX_THR_HIGH 0x7a120180 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 280 | #define RX_THR_SLOW 0xffff0180 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 281 | |
| 282 | /* USB_TX_DMA */ |
| 283 | #define TEST_MODE_DISABLE 0x00000001 |
| 284 | #define TX_SIZE_ADJUST1 0x00000100 |
| 285 | |
| 286 | /* USB_UPS_CTRL */ |
| 287 | #define POWER_CUT 0x0100 |
| 288 | |
| 289 | /* USB_PM_CTRL_STATUS */ |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 290 | #define RESUME_INDICATE 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 291 | |
| 292 | /* USB_USB_CTRL */ |
| 293 | #define RX_AGG_DISABLE 0x0010 |
| 294 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 295 | /* USB_U2P3_CTRL */ |
| 296 | #define U2P3_ENABLE 0x0001 |
| 297 | |
| 298 | /* USB_POWER_CUT */ |
| 299 | #define PWR_EN 0x0001 |
| 300 | #define PHASE2_EN 0x0008 |
| 301 | |
| 302 | /* USB_MISC_0 */ |
| 303 | #define PCUT_STATUS 0x0001 |
| 304 | |
| 305 | /* USB_RX_EARLY_AGG */ |
| 306 | #define EARLY_AGG_SUPPER 0x0e832981 |
| 307 | #define EARLY_AGG_HIGH 0x0e837a12 |
| 308 | #define EARLY_AGG_SLOW 0x0e83ffff |
| 309 | |
| 310 | /* USB_WDT11_CTRL */ |
| 311 | #define TIMER11_EN 0x0001 |
| 312 | |
| 313 | /* USB_LPM_CTRL */ |
| 314 | #define LPM_TIMER_MASK 0x0c |
| 315 | #define LPM_TIMER_500MS 0x04 /* 500 ms */ |
| 316 | #define LPM_TIMER_500US 0x0c /* 500 us */ |
| 317 | |
| 318 | /* USB_AFE_CTRL2 */ |
| 319 | #define SEN_VAL_MASK 0xf800 |
| 320 | #define SEN_VAL_NORMAL 0xa000 |
| 321 | #define SEL_RXIDLE 0x0100 |
| 322 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 323 | /* OCP_ALDPS_CONFIG */ |
| 324 | #define ENPWRSAVE 0x8000 |
| 325 | #define ENPDNPS 0x0200 |
| 326 | #define LINKENA 0x0100 |
| 327 | #define DIS_SDSAVE 0x0010 |
| 328 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 329 | /* OCP_PHY_STATUS */ |
| 330 | #define PHY_STAT_MASK 0x0007 |
| 331 | #define PHY_STAT_LAN_ON 3 |
| 332 | #define PHY_STAT_PWRDN 5 |
| 333 | |
| 334 | /* OCP_POWER_CFG */ |
| 335 | #define EEE_CLKDIV_EN 0x8000 |
| 336 | #define EN_ALDPS 0x0004 |
| 337 | #define EN_10M_PLLOFF 0x0001 |
| 338 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 339 | /* OCP_EEE_CONFIG1 */ |
| 340 | #define RG_TXLPI_MSK_HFDUP 0x8000 |
| 341 | #define RG_MATCLR_EN 0x4000 |
| 342 | #define EEE_10_CAP 0x2000 |
| 343 | #define EEE_NWAY_EN 0x1000 |
| 344 | #define TX_QUIET_EN 0x0200 |
| 345 | #define RX_QUIET_EN 0x0100 |
| 346 | #define SDRISETIME 0x0010 /* bit 4 ~ 6 */ |
| 347 | #define RG_RXLPI_MSK_HFDUP 0x0008 |
| 348 | #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ |
| 349 | |
| 350 | /* OCP_EEE_CONFIG2 */ |
| 351 | #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ |
| 352 | #define RG_DACQUIET_EN 0x0400 |
| 353 | #define RG_LDVQUIET_EN 0x0200 |
| 354 | #define RG_CKRSEL 0x0020 |
| 355 | #define RG_EEEPRG_EN 0x0010 |
| 356 | |
| 357 | /* OCP_EEE_CONFIG3 */ |
| 358 | #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */ |
| 359 | #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ |
| 360 | #define MSK_PH 0x0006 /* bit 0 ~ 3 */ |
| 361 | |
| 362 | /* OCP_EEE_AR */ |
| 363 | /* bit[15:14] function */ |
| 364 | #define FUN_ADDR 0x0000 |
| 365 | #define FUN_DATA 0x4000 |
| 366 | /* bit[4:0] device addr */ |
| 367 | #define DEVICE_ADDR 0x0007 |
| 368 | |
| 369 | /* OCP_EEE_DATA */ |
| 370 | #define EEE_ADDR 0x003C |
| 371 | #define EEE_DATA 0x0002 |
| 372 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 373 | /* OCP_EEE_CFG */ |
| 374 | #define CTAP_SHORT_EN 0x0040 |
| 375 | #define EEE10_EN 0x0010 |
| 376 | |
| 377 | /* OCP_DOWN_SPEED */ |
| 378 | #define EN_10M_BGOFF 0x0080 |
| 379 | |
| 380 | /* OCP_EEE_CFG2 */ |
| 381 | #define MY1000_EEE 0x0004 |
| 382 | #define MY100_EEE 0x0002 |
| 383 | |
| 384 | /* OCP_ADC_CFG */ |
| 385 | #define CKADSEL_L 0x0100 |
| 386 | #define ADC_EN 0x0080 |
| 387 | #define EN_EMI_L 0x0040 |
| 388 | |
| 389 | /* SRAM_LPF_CFG */ |
| 390 | #define LPF_AUTO_TUNE 0x8000 |
| 391 | |
| 392 | /* SRAM_10M_AMP1 */ |
| 393 | #define GDAC_IB_UPALL 0x0008 |
| 394 | |
| 395 | /* SRAM_10M_AMP2 */ |
| 396 | #define AMP_DN 0x0200 |
| 397 | |
| 398 | /* SRAM_IMPEDANCE */ |
| 399 | #define RX_DRIVING_MASK 0x6000 |
| 400 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 401 | enum rtl_register_content { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 402 | _1000bps = 0x10, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 403 | _100bps = 0x08, |
| 404 | _10bps = 0x04, |
| 405 | LINK_STATUS = 0x02, |
| 406 | FULL_DUP = 0x01, |
| 407 | }; |
| 408 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 409 | #define RTL8152_MAX_TX 10 |
| 410 | #define RTL8152_MAX_RX 10 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 411 | #define INTBUFSIZE 2 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 412 | #define CRC_SIZE 4 |
| 413 | #define TX_ALIGN 4 |
| 414 | #define RX_ALIGN 8 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 415 | |
| 416 | #define INTR_LINK 0x0004 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 417 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 418 | #define RTL8152_REQT_READ 0xc0 |
| 419 | #define RTL8152_REQT_WRITE 0x40 |
| 420 | #define RTL8152_REQ_GET_REGS 0x05 |
| 421 | #define RTL8152_REQ_SET_REGS 0x05 |
| 422 | |
| 423 | #define BYTE_EN_DWORD 0xff |
| 424 | #define BYTE_EN_WORD 0x33 |
| 425 | #define BYTE_EN_BYTE 0x11 |
| 426 | #define BYTE_EN_SIX_BYTES 0x3f |
| 427 | #define BYTE_EN_START_MASK 0x0f |
| 428 | #define BYTE_EN_END_MASK 0xf0 |
| 429 | |
| 430 | #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN) |
| 431 | #define RTL8152_TX_TIMEOUT (HZ) |
| 432 | |
| 433 | /* rtl8152 flags */ |
| 434 | enum rtl8152_flags { |
| 435 | RTL8152_UNPLUG = 0, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 436 | RTL8152_SET_RX_MODE, |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 437 | WORK_ENABLE, |
| 438 | RTL8152_LINK_CHG, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | /* Define these values to match your device */ |
| 442 | #define VENDOR_ID_REALTEK 0x0bda |
| 443 | #define PRODUCT_ID_RTL8152 0x8152 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 444 | #define PRODUCT_ID_RTL8153 0x8153 |
| 445 | |
| 446 | #define VENDOR_ID_SAMSUNG 0x04e8 |
| 447 | #define PRODUCT_ID_SAMSUNG 0xa101 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 448 | |
| 449 | #define MCU_TYPE_PLA 0x0100 |
| 450 | #define MCU_TYPE_USB 0x0000 |
| 451 | |
hayeswang | c7de7de | 2014-01-15 10:42:16 +0800 | [diff] [blame] | 452 | #define REALTEK_USB_DEVICE(vend, prod) \ |
| 453 | USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC) |
| 454 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 455 | struct rx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 456 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 457 | #define RX_LEN_MASK 0x7fff |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 458 | __le32 opts2; |
| 459 | __le32 opts3; |
| 460 | __le32 opts4; |
| 461 | __le32 opts5; |
| 462 | __le32 opts6; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | struct tx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 466 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 467 | #define TX_FS (1 << 31) /* First segment of a packet */ |
| 468 | #define TX_LS (1 << 30) /* Final segment of a packet */ |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 469 | #define TX_LEN_MASK 0x3ffff |
| 470 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 471 | __le32 opts2; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 472 | #define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */ |
| 473 | #define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */ |
| 474 | #define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */ |
| 475 | #define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 478 | struct r8152; |
| 479 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 480 | struct rx_agg { |
| 481 | struct list_head list; |
| 482 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 483 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 484 | void *buffer; |
| 485 | void *head; |
| 486 | }; |
| 487 | |
| 488 | struct tx_agg { |
| 489 | struct list_head list; |
| 490 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 491 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 492 | void *buffer; |
| 493 | void *head; |
| 494 | u32 skb_num; |
| 495 | u32 skb_len; |
| 496 | }; |
| 497 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 498 | struct r8152 { |
| 499 | unsigned long flags; |
| 500 | struct usb_device *udev; |
| 501 | struct tasklet_struct tl; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 502 | struct usb_interface *intf; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 503 | struct net_device *netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 504 | struct urb *intr_urb; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 505 | struct tx_agg tx_info[RTL8152_MAX_TX]; |
| 506 | struct rx_agg rx_info[RTL8152_MAX_RX]; |
| 507 | struct list_head rx_done, tx_free; |
| 508 | struct sk_buff_head tx_queue; |
| 509 | spinlock_t rx_lock, tx_lock; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 510 | struct delayed_work schedule; |
| 511 | struct mii_if_info mii; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 512 | |
| 513 | struct rtl_ops { |
| 514 | void (*init)(struct r8152 *); |
| 515 | int (*enable)(struct r8152 *); |
| 516 | void (*disable)(struct r8152 *); |
| 517 | void (*down)(struct r8152 *); |
| 518 | void (*unload)(struct r8152 *); |
| 519 | } rtl_ops; |
| 520 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 521 | int intr_interval; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 522 | u32 msg_enable; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 523 | u32 tx_qlen; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 524 | u16 ocp_base; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 525 | u8 *intr_buff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 526 | u8 version; |
| 527 | u8 speed; |
| 528 | }; |
| 529 | |
| 530 | enum rtl_version { |
| 531 | RTL_VER_UNKNOWN = 0, |
| 532 | RTL_VER_01, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 533 | RTL_VER_02, |
| 534 | RTL_VER_03, |
| 535 | RTL_VER_04, |
| 536 | RTL_VER_05, |
| 537 | RTL_VER_MAX |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). |
| 541 | * The RTL chips use a 64 element hash table based on the Ethernet CRC. |
| 542 | */ |
| 543 | static const int multicast_filter_limit = 32; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 544 | static unsigned int rx_buf_sz = 16384; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 545 | |
| 546 | static |
| 547 | int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 548 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 549 | int ret; |
| 550 | void *tmp; |
| 551 | |
| 552 | tmp = kmalloc(size, GFP_KERNEL); |
| 553 | if (!tmp) |
| 554 | return -ENOMEM; |
| 555 | |
| 556 | ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 557 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 558 | value, index, tmp, size, 500); |
| 559 | |
| 560 | memcpy(data, tmp, size); |
| 561 | kfree(tmp); |
| 562 | |
| 563 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static |
| 567 | int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 568 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 569 | int ret; |
| 570 | void *tmp; |
| 571 | |
| 572 | tmp = kmalloc(size, GFP_KERNEL); |
| 573 | if (!tmp) |
| 574 | return -ENOMEM; |
| 575 | |
| 576 | memcpy(tmp, data, size); |
| 577 | |
| 578 | ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 579 | RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 580 | value, index, tmp, size, 500); |
| 581 | |
| 582 | kfree(tmp); |
| 583 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, |
| 587 | void *data, u16 type) |
| 588 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 589 | u16 limit = 64; |
| 590 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 591 | |
| 592 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 593 | return -ENODEV; |
| 594 | |
| 595 | /* both size and indix must be 4 bytes align */ |
| 596 | if ((size & 3) || !size || (index & 3) || !data) |
| 597 | return -EPERM; |
| 598 | |
| 599 | if ((u32)index + (u32)size > 0xffff) |
| 600 | return -EPERM; |
| 601 | |
| 602 | while (size) { |
| 603 | if (size > limit) { |
| 604 | ret = get_registers(tp, index, type, limit, data); |
| 605 | if (ret < 0) |
| 606 | break; |
| 607 | |
| 608 | index += limit; |
| 609 | data += limit; |
| 610 | size -= limit; |
| 611 | } else { |
| 612 | ret = get_registers(tp, index, type, size, data); |
| 613 | if (ret < 0) |
| 614 | break; |
| 615 | |
| 616 | index += size; |
| 617 | data += size; |
| 618 | size = 0; |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, |
| 627 | u16 size, void *data, u16 type) |
| 628 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 629 | int ret; |
| 630 | u16 byteen_start, byteen_end, byen; |
| 631 | u16 limit = 512; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 632 | |
| 633 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 634 | return -ENODEV; |
| 635 | |
| 636 | /* both size and indix must be 4 bytes align */ |
| 637 | if ((size & 3) || !size || (index & 3) || !data) |
| 638 | return -EPERM; |
| 639 | |
| 640 | if ((u32)index + (u32)size > 0xffff) |
| 641 | return -EPERM; |
| 642 | |
| 643 | byteen_start = byteen & BYTE_EN_START_MASK; |
| 644 | byteen_end = byteen & BYTE_EN_END_MASK; |
| 645 | |
| 646 | byen = byteen_start | (byteen_start << 4); |
| 647 | ret = set_registers(tp, index, type | byen, 4, data); |
| 648 | if (ret < 0) |
| 649 | goto error1; |
| 650 | |
| 651 | index += 4; |
| 652 | data += 4; |
| 653 | size -= 4; |
| 654 | |
| 655 | if (size) { |
| 656 | size -= 4; |
| 657 | |
| 658 | while (size) { |
| 659 | if (size > limit) { |
| 660 | ret = set_registers(tp, index, |
| 661 | type | BYTE_EN_DWORD, |
| 662 | limit, data); |
| 663 | if (ret < 0) |
| 664 | goto error1; |
| 665 | |
| 666 | index += limit; |
| 667 | data += limit; |
| 668 | size -= limit; |
| 669 | } else { |
| 670 | ret = set_registers(tp, index, |
| 671 | type | BYTE_EN_DWORD, |
| 672 | size, data); |
| 673 | if (ret < 0) |
| 674 | goto error1; |
| 675 | |
| 676 | index += size; |
| 677 | data += size; |
| 678 | size = 0; |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | byen = byteen_end | (byteen_end >> 4); |
| 684 | ret = set_registers(tp, index, type | byen, 4, data); |
| 685 | if (ret < 0) |
| 686 | goto error1; |
| 687 | } |
| 688 | |
| 689 | error1: |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static inline |
| 694 | int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 695 | { |
| 696 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); |
| 697 | } |
| 698 | |
| 699 | static inline |
| 700 | int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 701 | { |
| 702 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); |
| 703 | } |
| 704 | |
| 705 | static inline |
| 706 | int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 707 | { |
| 708 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB); |
| 709 | } |
| 710 | |
| 711 | static inline |
| 712 | int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 713 | { |
| 714 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); |
| 715 | } |
| 716 | |
| 717 | static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) |
| 718 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 719 | __le32 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 720 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 721 | generic_ocp_read(tp, index, sizeof(data), &data, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 722 | |
| 723 | return __le32_to_cpu(data); |
| 724 | } |
| 725 | |
| 726 | static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 727 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 728 | __le32 tmp = __cpu_to_le32(data); |
| 729 | |
| 730 | generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) |
| 734 | { |
| 735 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 736 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 737 | u8 shift = index & 2; |
| 738 | |
| 739 | index &= ~3; |
| 740 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 741 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 742 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 743 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 744 | data >>= (shift * 8); |
| 745 | data &= 0xffff; |
| 746 | |
| 747 | return (u16)data; |
| 748 | } |
| 749 | |
| 750 | static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 751 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 752 | u32 mask = 0xffff; |
| 753 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 754 | u16 byen = BYTE_EN_WORD; |
| 755 | u8 shift = index & 2; |
| 756 | |
| 757 | data &= mask; |
| 758 | |
| 759 | if (index & 2) { |
| 760 | byen <<= shift; |
| 761 | mask <<= (shift * 8); |
| 762 | data <<= (shift * 8); |
| 763 | index &= ~3; |
| 764 | } |
| 765 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 766 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 767 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 768 | data |= __le32_to_cpu(tmp) & ~mask; |
| 769 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 770 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 771 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) |
| 775 | { |
| 776 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 777 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 778 | u8 shift = index & 3; |
| 779 | |
| 780 | index &= ~3; |
| 781 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 782 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 783 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 784 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 785 | data >>= (shift * 8); |
| 786 | data &= 0xff; |
| 787 | |
| 788 | return (u8)data; |
| 789 | } |
| 790 | |
| 791 | static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 792 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 793 | u32 mask = 0xff; |
| 794 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 795 | u16 byen = BYTE_EN_BYTE; |
| 796 | u8 shift = index & 3; |
| 797 | |
| 798 | data &= mask; |
| 799 | |
| 800 | if (index & 3) { |
| 801 | byen <<= shift; |
| 802 | mask <<= (shift * 8); |
| 803 | data <<= (shift * 8); |
| 804 | index &= ~3; |
| 805 | } |
| 806 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 807 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 808 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 809 | data |= __le32_to_cpu(tmp) & ~mask; |
| 810 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 811 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 812 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 813 | } |
| 814 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 815 | static u16 ocp_reg_read(struct r8152 *tp, u16 addr) |
| 816 | { |
| 817 | u16 ocp_base, ocp_index; |
| 818 | |
| 819 | ocp_base = addr & 0xf000; |
| 820 | if (ocp_base != tp->ocp_base) { |
| 821 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 822 | tp->ocp_base = ocp_base; |
| 823 | } |
| 824 | |
| 825 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 826 | return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); |
| 827 | } |
| 828 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 829 | static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) |
| 830 | { |
| 831 | u16 ocp_base, ocp_index; |
| 832 | |
| 833 | ocp_base = addr & 0xf000; |
| 834 | if (ocp_base != tp->ocp_base) { |
| 835 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 836 | tp->ocp_base = ocp_base; |
| 837 | } |
| 838 | |
| 839 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 840 | ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); |
| 841 | } |
| 842 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 843 | static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 844 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 845 | ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 846 | } |
| 847 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 848 | static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 849 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 850 | return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 851 | } |
| 852 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 853 | static void sram_write(struct r8152 *tp, u16 addr, u16 data) |
| 854 | { |
| 855 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 856 | ocp_reg_write(tp, OCP_SRAM_DATA, data); |
| 857 | } |
| 858 | |
| 859 | static u16 sram_read(struct r8152 *tp, u16 addr) |
| 860 | { |
| 861 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 862 | return ocp_reg_read(tp, OCP_SRAM_DATA); |
| 863 | } |
| 864 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 865 | static int read_mii_word(struct net_device *netdev, int phy_id, int reg) |
| 866 | { |
| 867 | struct r8152 *tp = netdev_priv(netdev); |
| 868 | |
| 869 | if (phy_id != R8152_PHY_ID) |
| 870 | return -EINVAL; |
| 871 | |
| 872 | return r8152_mdio_read(tp, reg); |
| 873 | } |
| 874 | |
| 875 | static |
| 876 | void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) |
| 877 | { |
| 878 | struct r8152 *tp = netdev_priv(netdev); |
| 879 | |
| 880 | if (phy_id != R8152_PHY_ID) |
| 881 | return; |
| 882 | |
| 883 | r8152_mdio_write(tp, reg, val); |
| 884 | } |
| 885 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 886 | static |
| 887 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags); |
| 888 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 889 | static inline void set_ethernet_addr(struct r8152 *tp) |
| 890 | { |
| 891 | struct net_device *dev = tp->netdev; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 892 | int ret; |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 893 | u8 node_id[8] = {0}; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 894 | |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 895 | if (tp->version == RTL_VER_01) |
| 896 | ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id); |
| 897 | else |
| 898 | ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id); |
| 899 | |
| 900 | if (ret < 0) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 901 | netif_notice(tp, probe, dev, "inet addr fail\n"); |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 902 | } else { |
| 903 | if (tp->version != RTL_VER_01) { |
| 904 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, |
| 905 | CRWECR_CONFIG); |
| 906 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, |
| 907 | sizeof(node_id), node_id); |
| 908 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, |
| 909 | CRWECR_NORAML); |
| 910 | } |
| 911 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 912 | memcpy(dev->dev_addr, node_id, dev->addr_len); |
| 913 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); |
| 914 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | static int rtl8152_set_mac_address(struct net_device *netdev, void *p) |
| 918 | { |
| 919 | struct r8152 *tp = netdev_priv(netdev); |
| 920 | struct sockaddr *addr = p; |
| 921 | |
| 922 | if (!is_valid_ether_addr(addr->sa_data)) |
| 923 | return -EADDRNOTAVAIL; |
| 924 | |
| 925 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); |
| 926 | |
| 927 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 928 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); |
| 929 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 934 | static struct net_device_stats *rtl8152_get_stats(struct net_device *dev) |
| 935 | { |
| 936 | return &dev->stats; |
| 937 | } |
| 938 | |
| 939 | static void read_bulk_callback(struct urb *urb) |
| 940 | { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 941 | struct net_device *netdev; |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 942 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 943 | int status = urb->status; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 944 | struct rx_agg *agg; |
| 945 | struct r8152 *tp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 946 | int result; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 947 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 948 | agg = urb->context; |
| 949 | if (!agg) |
| 950 | return; |
| 951 | |
| 952 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 953 | if (!tp) |
| 954 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 955 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 956 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 957 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 958 | |
| 959 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 960 | return; |
| 961 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 962 | netdev = tp->netdev; |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 963 | |
| 964 | /* When link down, the driver would cancel all bulks. */ |
| 965 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 966 | if (!netif_carrier_ok(netdev)) |
| 967 | return; |
| 968 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 969 | switch (status) { |
| 970 | case 0: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 971 | if (urb->actual_length < ETH_ZLEN) |
| 972 | break; |
| 973 | |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 974 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 975 | list_add_tail(&agg->list, &tp->rx_done); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 976 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 977 | tasklet_schedule(&tp->tl); |
| 978 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 979 | case -ESHUTDOWN: |
| 980 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 981 | netif_device_detach(tp->netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 982 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 983 | case -ENOENT: |
| 984 | return; /* the urb is in unlink state */ |
| 985 | case -ETIME: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 986 | if (net_ratelimit()) |
| 987 | netdev_warn(netdev, "maybe reset is needed?\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 988 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 989 | default: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 990 | if (net_ratelimit()) |
| 991 | netdev_warn(netdev, "Rx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 992 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 993 | } |
| 994 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 995 | result = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 996 | if (result == -ENODEV) { |
| 997 | netif_device_detach(tp->netdev); |
| 998 | } else if (result) { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 999 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1000 | list_add_tail(&agg->list, &tp->rx_done); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1001 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1002 | tasklet_schedule(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1003 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | static void write_bulk_callback(struct urb *urb) |
| 1007 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1008 | struct net_device_stats *stats; |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1009 | unsigned long flags; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1010 | struct tx_agg *agg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1011 | struct r8152 *tp; |
| 1012 | int status = urb->status; |
| 1013 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1014 | agg = urb->context; |
| 1015 | if (!agg) |
| 1016 | return; |
| 1017 | |
| 1018 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1019 | if (!tp) |
| 1020 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1021 | |
| 1022 | stats = rtl8152_get_stats(tp->netdev); |
| 1023 | if (status) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1024 | if (net_ratelimit()) |
| 1025 | netdev_warn(tp->netdev, "Tx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1026 | stats->tx_errors += agg->skb_num; |
| 1027 | } else { |
| 1028 | stats->tx_packets += agg->skb_num; |
| 1029 | stats->tx_bytes += agg->skb_len; |
| 1030 | } |
| 1031 | |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1032 | spin_lock_irqsave(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1033 | list_add_tail(&agg->list, &tp->tx_free); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1034 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1035 | |
| 1036 | if (!netif_carrier_ok(tp->netdev)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1037 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1038 | |
| 1039 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1040 | return; |
| 1041 | |
| 1042 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1043 | return; |
| 1044 | |
| 1045 | if (!skb_queue_empty(&tp->tx_queue)) |
| 1046 | tasklet_schedule(&tp->tl); |
| 1047 | } |
| 1048 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1049 | static void intr_callback(struct urb *urb) |
| 1050 | { |
| 1051 | struct r8152 *tp; |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 1052 | __le16 *d; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1053 | int status = urb->status; |
| 1054 | int res; |
| 1055 | |
| 1056 | tp = urb->context; |
| 1057 | if (!tp) |
| 1058 | return; |
| 1059 | |
| 1060 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1061 | return; |
| 1062 | |
| 1063 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1064 | return; |
| 1065 | |
| 1066 | switch (status) { |
| 1067 | case 0: /* success */ |
| 1068 | break; |
| 1069 | case -ECONNRESET: /* unlink */ |
| 1070 | case -ESHUTDOWN: |
| 1071 | netif_device_detach(tp->netdev); |
| 1072 | case -ENOENT: |
| 1073 | return; |
| 1074 | case -EOVERFLOW: |
| 1075 | netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); |
| 1076 | goto resubmit; |
| 1077 | /* -EPIPE: should clear the halt */ |
| 1078 | default: |
| 1079 | netif_info(tp, intr, tp->netdev, "intr status %d\n", status); |
| 1080 | goto resubmit; |
| 1081 | } |
| 1082 | |
| 1083 | d = urb->transfer_buffer; |
| 1084 | if (INTR_LINK & __le16_to_cpu(d[0])) { |
| 1085 | if (!(tp->speed & LINK_STATUS)) { |
| 1086 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1087 | schedule_delayed_work(&tp->schedule, 0); |
| 1088 | } |
| 1089 | } else { |
| 1090 | if (tp->speed & LINK_STATUS) { |
| 1091 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1092 | schedule_delayed_work(&tp->schedule, 0); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | resubmit: |
| 1097 | res = usb_submit_urb(urb, GFP_ATOMIC); |
| 1098 | if (res == -ENODEV) |
| 1099 | netif_device_detach(tp->netdev); |
| 1100 | else if (res) |
| 1101 | netif_err(tp, intr, tp->netdev, |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1102 | "can't resubmit intr, status %d\n", res); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1103 | } |
| 1104 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1105 | static inline void *rx_agg_align(void *data) |
| 1106 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1107 | return (void *)ALIGN((uintptr_t)data, RX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | static inline void *tx_agg_align(void *data) |
| 1111 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1112 | return (void *)ALIGN((uintptr_t)data, TX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static void free_all_mem(struct r8152 *tp) |
| 1116 | { |
| 1117 | int i; |
| 1118 | |
| 1119 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1120 | usb_free_urb(tp->rx_info[i].urb); |
| 1121 | tp->rx_info[i].urb = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1122 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1123 | kfree(tp->rx_info[i].buffer); |
| 1124 | tp->rx_info[i].buffer = NULL; |
| 1125 | tp->rx_info[i].head = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1129 | usb_free_urb(tp->tx_info[i].urb); |
| 1130 | tp->tx_info[i].urb = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1131 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1132 | kfree(tp->tx_info[i].buffer); |
| 1133 | tp->tx_info[i].buffer = NULL; |
| 1134 | tp->tx_info[i].head = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1135 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1136 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1137 | usb_free_urb(tp->intr_urb); |
| 1138 | tp->intr_urb = NULL; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1139 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1140 | kfree(tp->intr_buff); |
| 1141 | tp->intr_buff = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | static int alloc_all_mem(struct r8152 *tp) |
| 1145 | { |
| 1146 | struct net_device *netdev = tp->netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1147 | struct usb_interface *intf = tp->intf; |
| 1148 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1149 | struct usb_host_endpoint *ep_intr = alt->endpoint + 2; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1150 | struct urb *urb; |
| 1151 | int node, i; |
| 1152 | u8 *buf; |
| 1153 | |
| 1154 | node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
| 1155 | |
| 1156 | spin_lock_init(&tp->rx_lock); |
| 1157 | spin_lock_init(&tp->tx_lock); |
| 1158 | INIT_LIST_HEAD(&tp->rx_done); |
| 1159 | INIT_LIST_HEAD(&tp->tx_free); |
| 1160 | skb_queue_head_init(&tp->tx_queue); |
| 1161 | |
| 1162 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
| 1163 | buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node); |
| 1164 | if (!buf) |
| 1165 | goto err1; |
| 1166 | |
| 1167 | if (buf != rx_agg_align(buf)) { |
| 1168 | kfree(buf); |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1169 | buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL, |
| 1170 | node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1171 | if (!buf) |
| 1172 | goto err1; |
| 1173 | } |
| 1174 | |
| 1175 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1176 | if (!urb) { |
| 1177 | kfree(buf); |
| 1178 | goto err1; |
| 1179 | } |
| 1180 | |
| 1181 | INIT_LIST_HEAD(&tp->rx_info[i].list); |
| 1182 | tp->rx_info[i].context = tp; |
| 1183 | tp->rx_info[i].urb = urb; |
| 1184 | tp->rx_info[i].buffer = buf; |
| 1185 | tp->rx_info[i].head = rx_agg_align(buf); |
| 1186 | } |
| 1187 | |
| 1188 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
| 1189 | buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node); |
| 1190 | if (!buf) |
| 1191 | goto err1; |
| 1192 | |
| 1193 | if (buf != tx_agg_align(buf)) { |
| 1194 | kfree(buf); |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1195 | buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL, |
| 1196 | node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1197 | if (!buf) |
| 1198 | goto err1; |
| 1199 | } |
| 1200 | |
| 1201 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1202 | if (!urb) { |
| 1203 | kfree(buf); |
| 1204 | goto err1; |
| 1205 | } |
| 1206 | |
| 1207 | INIT_LIST_HEAD(&tp->tx_info[i].list); |
| 1208 | tp->tx_info[i].context = tp; |
| 1209 | tp->tx_info[i].urb = urb; |
| 1210 | tp->tx_info[i].buffer = buf; |
| 1211 | tp->tx_info[i].head = tx_agg_align(buf); |
| 1212 | |
| 1213 | list_add_tail(&tp->tx_info[i].list, &tp->tx_free); |
| 1214 | } |
| 1215 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1216 | tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1217 | if (!tp->intr_urb) |
| 1218 | goto err1; |
| 1219 | |
| 1220 | tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); |
| 1221 | if (!tp->intr_buff) |
| 1222 | goto err1; |
| 1223 | |
| 1224 | tp->intr_interval = (int)ep_intr->desc.bInterval; |
| 1225 | usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3), |
| 1226 | tp->intr_buff, INTBUFSIZE, intr_callback, |
| 1227 | tp, tp->intr_interval); |
| 1228 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1229 | return 0; |
| 1230 | |
| 1231 | err1: |
| 1232 | free_all_mem(tp); |
| 1233 | return -ENOMEM; |
| 1234 | } |
| 1235 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1236 | static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) |
| 1237 | { |
| 1238 | struct tx_agg *agg = NULL; |
| 1239 | unsigned long flags; |
| 1240 | |
| 1241 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1242 | if (!list_empty(&tp->tx_free)) { |
| 1243 | struct list_head *cursor; |
| 1244 | |
| 1245 | cursor = tp->tx_free.next; |
| 1246 | list_del_init(cursor); |
| 1247 | agg = list_entry(cursor, struct tx_agg, list); |
| 1248 | } |
| 1249 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1250 | |
| 1251 | return agg; |
| 1252 | } |
| 1253 | |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1254 | static void |
| 1255 | r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb) |
| 1256 | { |
| 1257 | memset(desc, 0, sizeof(*desc)); |
| 1258 | |
| 1259 | desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS); |
| 1260 | |
| 1261 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1262 | __be16 protocol; |
| 1263 | u8 ip_protocol; |
| 1264 | u32 opts2 = 0; |
| 1265 | |
| 1266 | if (skb->protocol == htons(ETH_P_8021Q)) |
| 1267 | protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto; |
| 1268 | else |
| 1269 | protocol = skb->protocol; |
| 1270 | |
| 1271 | switch (protocol) { |
| 1272 | case htons(ETH_P_IP): |
| 1273 | opts2 |= IPV4_CS; |
| 1274 | ip_protocol = ip_hdr(skb)->protocol; |
| 1275 | break; |
| 1276 | |
| 1277 | case htons(ETH_P_IPV6): |
| 1278 | opts2 |= IPV6_CS; |
| 1279 | ip_protocol = ipv6_hdr(skb)->nexthdr; |
| 1280 | break; |
| 1281 | |
| 1282 | default: |
| 1283 | ip_protocol = IPPROTO_RAW; |
| 1284 | break; |
| 1285 | } |
| 1286 | |
| 1287 | if (ip_protocol == IPPROTO_TCP) { |
| 1288 | opts2 |= TCP_CS; |
| 1289 | opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17; |
| 1290 | } else if (ip_protocol == IPPROTO_UDP) { |
| 1291 | opts2 |= UDP_CS; |
| 1292 | } else { |
| 1293 | WARN_ON_ONCE(1); |
| 1294 | } |
| 1295 | |
| 1296 | desc->opts2 = cpu_to_le32(opts2); |
| 1297 | } |
| 1298 | } |
| 1299 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1300 | static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) |
| 1301 | { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1302 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
| 1303 | unsigned long flags; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1304 | int remain; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1305 | u8 *tx_data; |
| 1306 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1307 | __skb_queue_head_init(&skb_head); |
| 1308 | spin_lock_irqsave(&tx_queue->lock, flags); |
| 1309 | skb_queue_splice_init(tx_queue, &skb_head); |
| 1310 | spin_unlock_irqrestore(&tx_queue->lock, flags); |
| 1311 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1312 | tx_data = agg->head; |
| 1313 | agg->skb_num = agg->skb_len = 0; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1314 | remain = rx_buf_sz; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1315 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1316 | while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1317 | struct tx_desc *tx_desc; |
| 1318 | struct sk_buff *skb; |
| 1319 | unsigned int len; |
| 1320 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1321 | skb = __skb_dequeue(&skb_head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1322 | if (!skb) |
| 1323 | break; |
| 1324 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1325 | remain -= sizeof(*tx_desc); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1326 | len = skb->len; |
| 1327 | if (remain < len) { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1328 | __skb_queue_head(&skb_head, skb); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1329 | break; |
| 1330 | } |
| 1331 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1332 | tx_data = tx_agg_align(tx_data); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1333 | tx_desc = (struct tx_desc *)tx_data; |
| 1334 | tx_data += sizeof(*tx_desc); |
| 1335 | |
| 1336 | r8152_tx_csum(tp, tx_desc, skb); |
| 1337 | memcpy(tx_data, skb->data, len); |
| 1338 | agg->skb_num++; |
| 1339 | agg->skb_len += len; |
| 1340 | dev_kfree_skb_any(skb); |
| 1341 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1342 | tx_data += len; |
| 1343 | remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1344 | } |
| 1345 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1346 | if (!skb_queue_empty(&skb_head)) { |
| 1347 | spin_lock_irqsave(&tx_queue->lock, flags); |
| 1348 | skb_queue_splice(&skb_head, tx_queue); |
| 1349 | spin_unlock_irqrestore(&tx_queue->lock, flags); |
| 1350 | } |
| 1351 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1352 | netif_tx_lock(tp->netdev); |
| 1353 | |
| 1354 | if (netif_queue_stopped(tp->netdev) && |
| 1355 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) |
| 1356 | netif_wake_queue(tp->netdev); |
| 1357 | |
| 1358 | netif_tx_unlock(tp->netdev); |
| 1359 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1360 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), |
| 1361 | agg->head, (int)(tx_data - (u8 *)agg->head), |
| 1362 | (usb_complete_t)write_bulk_callback, agg); |
| 1363 | |
| 1364 | return usb_submit_urb(agg->urb, GFP_ATOMIC); |
| 1365 | } |
| 1366 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1367 | static void rx_bottom(struct r8152 *tp) |
| 1368 | { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1369 | unsigned long flags; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1370 | struct list_head *cursor, *next, rx_queue; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1371 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1372 | if (list_empty(&tp->rx_done)) |
| 1373 | return; |
| 1374 | |
| 1375 | INIT_LIST_HEAD(&rx_queue); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 1376 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1377 | list_splice_init(&tp->rx_done, &rx_queue); |
| 1378 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1379 | |
| 1380 | list_for_each_safe(cursor, next, &rx_queue) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1381 | struct rx_desc *rx_desc; |
| 1382 | struct rx_agg *agg; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1383 | int len_used = 0; |
| 1384 | struct urb *urb; |
| 1385 | u8 *rx_data; |
| 1386 | int ret; |
| 1387 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1388 | list_del_init(cursor); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1389 | |
| 1390 | agg = list_entry(cursor, struct rx_agg, list); |
| 1391 | urb = agg->urb; |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1392 | if (urb->actual_length < ETH_ZLEN) |
| 1393 | goto submit; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1394 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1395 | rx_desc = agg->head; |
| 1396 | rx_data = agg->head; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1397 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1398 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1399 | while (urb->actual_length > len_used) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1400 | struct net_device *netdev = tp->netdev; |
| 1401 | struct net_device_stats *stats; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1402 | unsigned int pkt_len; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1403 | struct sk_buff *skb; |
| 1404 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1405 | pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1406 | if (pkt_len < ETH_ZLEN) |
| 1407 | break; |
| 1408 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1409 | len_used += pkt_len; |
| 1410 | if (urb->actual_length < len_used) |
| 1411 | break; |
| 1412 | |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 1413 | stats = rtl8152_get_stats(netdev); |
| 1414 | |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1415 | pkt_len -= CRC_SIZE; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1416 | rx_data += sizeof(struct rx_desc); |
| 1417 | |
| 1418 | skb = netdev_alloc_skb_ip_align(netdev, pkt_len); |
| 1419 | if (!skb) { |
| 1420 | stats->rx_dropped++; |
| 1421 | break; |
| 1422 | } |
| 1423 | memcpy(skb->data, rx_data, pkt_len); |
| 1424 | skb_put(skb, pkt_len); |
| 1425 | skb->protocol = eth_type_trans(skb, netdev); |
| 1426 | netif_rx(skb); |
| 1427 | stats->rx_packets++; |
| 1428 | stats->rx_bytes += pkt_len; |
| 1429 | |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1430 | rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1431 | rx_desc = (struct rx_desc *)rx_data; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1432 | len_used = (int)(rx_data - (u8 *)agg->head); |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1433 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1434 | } |
| 1435 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1436 | submit: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1437 | ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1438 | if (ret && ret != -ENODEV) { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1439 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 1440 | list_add_tail(&agg->list, &tp->rx_done); |
| 1441 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1442 | tasklet_schedule(&tp->tl); |
| 1443 | } |
| 1444 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | static void tx_bottom(struct r8152 *tp) |
| 1448 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1449 | int res; |
| 1450 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1451 | do { |
| 1452 | struct tx_agg *agg; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1453 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1454 | if (skb_queue_empty(&tp->tx_queue)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1455 | break; |
| 1456 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1457 | agg = r8152_get_tx_agg(tp); |
| 1458 | if (!agg) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1459 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1460 | |
| 1461 | res = r8152_tx_agg_fill(tp, agg); |
| 1462 | if (res) { |
| 1463 | struct net_device_stats *stats; |
| 1464 | struct net_device *netdev; |
| 1465 | unsigned long flags; |
| 1466 | |
| 1467 | netdev = tp->netdev; |
| 1468 | stats = rtl8152_get_stats(netdev); |
| 1469 | |
| 1470 | if (res == -ENODEV) { |
| 1471 | netif_device_detach(netdev); |
| 1472 | } else { |
| 1473 | netif_warn(tp, tx_err, netdev, |
| 1474 | "failed tx_urb %d\n", res); |
| 1475 | stats->tx_dropped += agg->skb_num; |
| 1476 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1477 | list_add_tail(&agg->list, &tp->tx_free); |
| 1478 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1479 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1480 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1481 | } while (res == 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static void bottom_half(unsigned long data) |
| 1485 | { |
| 1486 | struct r8152 *tp; |
| 1487 | |
| 1488 | tp = (struct r8152 *)data; |
| 1489 | |
| 1490 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1491 | return; |
| 1492 | |
| 1493 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1494 | return; |
| 1495 | |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 1496 | /* When link down, the driver would cancel all bulks. */ |
| 1497 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1498 | if (!netif_carrier_ok(tp->netdev)) |
| 1499 | return; |
| 1500 | |
| 1501 | rx_bottom(tp); |
| 1502 | tx_bottom(tp); |
| 1503 | } |
| 1504 | |
| 1505 | static |
| 1506 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) |
| 1507 | { |
| 1508 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), |
| 1509 | agg->head, rx_buf_sz, |
| 1510 | (usb_complete_t)read_bulk_callback, agg); |
| 1511 | |
| 1512 | return usb_submit_urb(agg->urb, mem_flags); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1515 | static void rtl_drop_queued_tx(struct r8152 *tp) |
| 1516 | { |
| 1517 | struct net_device_stats *stats = &tp->netdev->stats; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1518 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
| 1519 | unsigned long flags; |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1520 | struct sk_buff *skb; |
| 1521 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame^] | 1522 | if (skb_queue_empty(tx_queue)) |
| 1523 | return; |
| 1524 | |
| 1525 | __skb_queue_head_init(&skb_head); |
| 1526 | spin_lock_irqsave(&tx_queue->lock, flags); |
| 1527 | skb_queue_splice_init(tx_queue, &skb_head); |
| 1528 | spin_unlock_irqrestore(&tx_queue->lock, flags); |
| 1529 | |
| 1530 | while ((skb = __skb_dequeue(&skb_head))) { |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1531 | dev_kfree_skb(skb); |
| 1532 | stats->tx_dropped++; |
| 1533 | } |
| 1534 | } |
| 1535 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1536 | static void rtl8152_tx_timeout(struct net_device *netdev) |
| 1537 | { |
| 1538 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1539 | int i; |
| 1540 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1541 | netif_warn(tp, tx_err, netdev, "Tx timeout\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1542 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 1543 | usb_unlink_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | static void rtl8152_set_rx_mode(struct net_device *netdev) |
| 1547 | { |
| 1548 | struct r8152 *tp = netdev_priv(netdev); |
| 1549 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1550 | if (tp->speed & LINK_STATUS) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1551 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1552 | schedule_delayed_work(&tp->schedule, 0); |
| 1553 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | static void _rtl8152_set_rx_mode(struct net_device *netdev) |
| 1557 | { |
| 1558 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1559 | u32 mc_filter[2]; /* Multicast hash filter */ |
| 1560 | __le32 tmp[2]; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1561 | u32 ocp_data; |
| 1562 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1563 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 1564 | netif_stop_queue(netdev); |
| 1565 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1566 | ocp_data &= ~RCR_ACPT_ALL; |
| 1567 | ocp_data |= RCR_AB | RCR_APM; |
| 1568 | |
| 1569 | if (netdev->flags & IFF_PROMISC) { |
| 1570 | /* Unconditionally log net taps. */ |
| 1571 | netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); |
| 1572 | ocp_data |= RCR_AM | RCR_AAP; |
| 1573 | mc_filter[1] = mc_filter[0] = 0xffffffff; |
| 1574 | } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || |
| 1575 | (netdev->flags & IFF_ALLMULTI)) { |
| 1576 | /* Too many to filter perfectly -- accept all multicasts. */ |
| 1577 | ocp_data |= RCR_AM; |
| 1578 | mc_filter[1] = mc_filter[0] = 0xffffffff; |
| 1579 | } else { |
| 1580 | struct netdev_hw_addr *ha; |
| 1581 | |
| 1582 | mc_filter[1] = mc_filter[0] = 0; |
| 1583 | netdev_for_each_mc_addr(ha, netdev) { |
| 1584 | int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; |
| 1585 | mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); |
| 1586 | ocp_data |= RCR_AM; |
| 1587 | } |
| 1588 | } |
| 1589 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1590 | tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); |
| 1591 | tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1592 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1593 | pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1594 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1595 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, |
| 1599 | struct net_device *netdev) |
| 1600 | { |
| 1601 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1602 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1603 | skb_tx_timestamp(skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1604 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 1605 | skb_queue_tail(&tp->tx_queue, skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1606 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1607 | if (list_empty(&tp->tx_free) && |
| 1608 | skb_queue_len(&tp->tx_queue) > tp->tx_qlen) |
| 1609 | netif_stop_queue(netdev); |
| 1610 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 1611 | if (!list_empty(&tp->tx_free)) |
| 1612 | tasklet_schedule(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1613 | |
| 1614 | return NETDEV_TX_OK; |
| 1615 | } |
| 1616 | |
| 1617 | static void r8152b_reset_packet_filter(struct r8152 *tp) |
| 1618 | { |
| 1619 | u32 ocp_data; |
| 1620 | |
| 1621 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); |
| 1622 | ocp_data &= ~FMC_FCR_MCU_EN; |
| 1623 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 1624 | ocp_data |= FMC_FCR_MCU_EN; |
| 1625 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 1626 | } |
| 1627 | |
| 1628 | static void rtl8152_nic_reset(struct r8152 *tp) |
| 1629 | { |
| 1630 | int i; |
| 1631 | |
| 1632 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); |
| 1633 | |
| 1634 | for (i = 0; i < 1000; i++) { |
| 1635 | if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) |
| 1636 | break; |
| 1637 | udelay(100); |
| 1638 | } |
| 1639 | } |
| 1640 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1641 | static void set_tx_qlen(struct r8152 *tp) |
| 1642 | { |
| 1643 | struct net_device *netdev = tp->netdev; |
| 1644 | |
| 1645 | tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN + |
| 1646 | sizeof(struct tx_desc)); |
| 1647 | } |
| 1648 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1649 | static inline u8 rtl8152_get_speed(struct r8152 *tp) |
| 1650 | { |
| 1651 | return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); |
| 1652 | } |
| 1653 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 1654 | static void rtl_set_eee_plus(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1655 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1656 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1657 | u8 speed; |
| 1658 | |
| 1659 | speed = rtl8152_get_speed(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1660 | if (speed & _10bps) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1661 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1662 | ocp_data |= EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1663 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 1664 | } else { |
| 1665 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1666 | ocp_data &= ~EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1667 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 1668 | } |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 1669 | } |
| 1670 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1671 | static void rxdy_gated_en(struct r8152 *tp, bool enable) |
| 1672 | { |
| 1673 | u32 ocp_data; |
| 1674 | |
| 1675 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 1676 | if (enable) |
| 1677 | ocp_data |= RXDY_GATED_EN; |
| 1678 | else |
| 1679 | ocp_data &= ~RXDY_GATED_EN; |
| 1680 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 1681 | } |
| 1682 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 1683 | static int rtl_enable(struct r8152 *tp) |
| 1684 | { |
| 1685 | u32 ocp_data; |
| 1686 | int i, ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1687 | |
| 1688 | r8152b_reset_packet_filter(tp); |
| 1689 | |
| 1690 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); |
| 1691 | ocp_data |= CR_RE | CR_TE; |
| 1692 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); |
| 1693 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1694 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1695 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1696 | INIT_LIST_HEAD(&tp->rx_done); |
| 1697 | ret = 0; |
| 1698 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
| 1699 | INIT_LIST_HEAD(&tp->rx_info[i].list); |
| 1700 | ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL); |
| 1701 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1702 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1703 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 1706 | static int rtl8152_enable(struct r8152 *tp) |
| 1707 | { |
| 1708 | set_tx_qlen(tp); |
| 1709 | rtl_set_eee_plus(tp); |
| 1710 | |
| 1711 | return rtl_enable(tp); |
| 1712 | } |
| 1713 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 1714 | static void r8153_set_rx_agg(struct r8152 *tp) |
| 1715 | { |
| 1716 | u8 speed; |
| 1717 | |
| 1718 | speed = rtl8152_get_speed(tp); |
| 1719 | if (speed & _1000bps) { |
| 1720 | if (tp->udev->speed == USB_SPEED_SUPER) { |
| 1721 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, |
| 1722 | RX_THR_SUPPER); |
| 1723 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG, |
| 1724 | EARLY_AGG_SUPPER); |
| 1725 | } else { |
| 1726 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, |
| 1727 | RX_THR_HIGH); |
| 1728 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG, |
| 1729 | EARLY_AGG_HIGH); |
| 1730 | } |
| 1731 | } else { |
| 1732 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW); |
| 1733 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG, |
| 1734 | EARLY_AGG_SLOW); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | static int rtl8153_enable(struct r8152 *tp) |
| 1739 | { |
| 1740 | set_tx_qlen(tp); |
| 1741 | rtl_set_eee_plus(tp); |
| 1742 | r8153_set_rx_agg(tp); |
| 1743 | |
| 1744 | return rtl_enable(tp); |
| 1745 | } |
| 1746 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1747 | static void rtl8152_disable(struct r8152 *tp) |
| 1748 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1749 | u32 ocp_data; |
| 1750 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1751 | |
| 1752 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1753 | ocp_data &= ~RCR_ACPT_ALL; |
| 1754 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1755 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1756 | rtl_drop_queued_tx(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1757 | |
| 1758 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 1759 | usb_kill_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1760 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1761 | rxdy_gated_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1762 | |
| 1763 | for (i = 0; i < 1000; i++) { |
| 1764 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1765 | if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) |
| 1766 | break; |
| 1767 | mdelay(1); |
| 1768 | } |
| 1769 | |
| 1770 | for (i = 0; i < 1000; i++) { |
| 1771 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) |
| 1772 | break; |
| 1773 | mdelay(1); |
| 1774 | } |
| 1775 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1776 | for (i = 0; i < RTL8152_MAX_RX; i++) |
| 1777 | usb_kill_urb(tp->rx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1778 | |
| 1779 | rtl8152_nic_reset(tp); |
| 1780 | } |
| 1781 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1782 | static void r8152_power_cut_en(struct r8152 *tp, bool enable) |
| 1783 | { |
| 1784 | u32 ocp_data; |
| 1785 | |
| 1786 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 1787 | if (enable) |
| 1788 | ocp_data |= POWER_CUT; |
| 1789 | else |
| 1790 | ocp_data &= ~POWER_CUT; |
| 1791 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 1792 | |
| 1793 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 1794 | ocp_data &= ~RESUME_INDICATE; |
| 1795 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
| 1796 | |
| 1797 | } |
| 1798 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 1799 | static void rtl_clear_bp(struct r8152 *tp) |
| 1800 | { |
| 1801 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0); |
| 1802 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0); |
| 1803 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0); |
| 1804 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0); |
| 1805 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0); |
| 1806 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0); |
| 1807 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0); |
| 1808 | ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0); |
| 1809 | mdelay(3); |
| 1810 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0); |
| 1811 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0); |
| 1812 | } |
| 1813 | |
| 1814 | static void r8153_clear_bp(struct r8152 *tp) |
| 1815 | { |
| 1816 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0); |
| 1817 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0); |
| 1818 | rtl_clear_bp(tp); |
| 1819 | } |
| 1820 | |
| 1821 | static void r8153_teredo_off(struct r8152 *tp) |
| 1822 | { |
| 1823 | u32 ocp_data; |
| 1824 | |
| 1825 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 1826 | ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN); |
| 1827 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 1828 | |
| 1829 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); |
| 1830 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); |
| 1831 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); |
| 1832 | } |
| 1833 | |
| 1834 | static void r8152b_disable_aldps(struct r8152 *tp) |
| 1835 | { |
| 1836 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE); |
| 1837 | msleep(20); |
| 1838 | } |
| 1839 | |
| 1840 | static inline void r8152b_enable_aldps(struct r8152 *tp) |
| 1841 | { |
| 1842 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 1843 | LINKENA | DIS_SDSAVE); |
| 1844 | } |
| 1845 | |
| 1846 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 1847 | { |
| 1848 | r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE); |
| 1849 | r8152b_disable_aldps(tp); |
| 1850 | } |
| 1851 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1852 | static void r8152b_exit_oob(struct r8152 *tp) |
| 1853 | { |
| 1854 | u32 ocp_data; |
| 1855 | int i; |
| 1856 | |
| 1857 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1858 | ocp_data &= ~RCR_ACPT_ALL; |
| 1859 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1860 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1861 | rxdy_gated_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1862 | |
| 1863 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 1864 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 1865 | |
| 1866 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1867 | ocp_data &= ~NOW_IS_OOB; |
| 1868 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 1869 | |
| 1870 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 1871 | ocp_data &= ~MCU_BORW_EN; |
| 1872 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 1873 | |
| 1874 | for (i = 0; i < 1000; i++) { |
| 1875 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1876 | if (ocp_data & LINK_LIST_READY) |
| 1877 | break; |
| 1878 | mdelay(1); |
| 1879 | } |
| 1880 | |
| 1881 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 1882 | ocp_data |= RE_INIT_LL; |
| 1883 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 1884 | |
| 1885 | for (i = 0; i < 1000; i++) { |
| 1886 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1887 | if (ocp_data & LINK_LIST_READY) |
| 1888 | break; |
| 1889 | mdelay(1); |
| 1890 | } |
| 1891 | |
| 1892 | rtl8152_nic_reset(tp); |
| 1893 | |
| 1894 | /* rx share fifo credit full threshold */ |
| 1895 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 1896 | |
| 1897 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT); |
| 1898 | ocp_data &= STAT_SPEED_MASK; |
| 1899 | if (ocp_data == STAT_SPEED_FULL) { |
| 1900 | /* rx share fifo credit near full threshold */ |
| 1901 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 1902 | RXFIFO_THR2_FULL); |
| 1903 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 1904 | RXFIFO_THR3_FULL); |
| 1905 | } else { |
| 1906 | /* rx share fifo credit near full threshold */ |
| 1907 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 1908 | RXFIFO_THR2_HIGH); |
| 1909 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 1910 | RXFIFO_THR3_HIGH); |
| 1911 | } |
| 1912 | |
| 1913 | /* TX share fifo free credit full threshold */ |
| 1914 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 1915 | |
| 1916 | ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1917 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1918 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 1919 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 1920 | |
| 1921 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 1922 | ocp_data &= ~CPCR_RX_VLAN; |
| 1923 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 1924 | |
| 1925 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 1926 | |
| 1927 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 1928 | ocp_data |= TCR0_AUTO_FIFO; |
| 1929 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 1930 | } |
| 1931 | |
| 1932 | static void r8152b_enter_oob(struct r8152 *tp) |
| 1933 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 1934 | u32 ocp_data; |
| 1935 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1936 | |
| 1937 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1938 | ocp_data &= ~NOW_IS_OOB; |
| 1939 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 1940 | |
| 1941 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 1942 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 1943 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 1944 | |
| 1945 | rtl8152_disable(tp); |
| 1946 | |
| 1947 | for (i = 0; i < 1000; i++) { |
| 1948 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1949 | if (ocp_data & LINK_LIST_READY) |
| 1950 | break; |
| 1951 | mdelay(1); |
| 1952 | } |
| 1953 | |
| 1954 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 1955 | ocp_data |= RE_INIT_LL; |
| 1956 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 1957 | |
| 1958 | for (i = 0; i < 1000; i++) { |
| 1959 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1960 | if (ocp_data & LINK_LIST_READY) |
| 1961 | break; |
| 1962 | mdelay(1); |
| 1963 | } |
| 1964 | |
| 1965 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 1966 | |
| 1967 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 1968 | ocp_data |= MAGIC_EN; |
| 1969 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 1970 | |
| 1971 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 1972 | ocp_data |= CPCR_RX_VLAN; |
| 1973 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 1974 | |
| 1975 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR); |
| 1976 | ocp_data |= ALDPS_PROXY_MODE; |
| 1977 | ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data); |
| 1978 | |
| 1979 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 1980 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 1981 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 1982 | |
| 1983 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN); |
| 1984 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 1985 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1986 | |
| 1987 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 1988 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 1989 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 1990 | } |
| 1991 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 1992 | static void r8153_hw_phy_cfg(struct r8152 *tp) |
| 1993 | { |
| 1994 | u32 ocp_data; |
| 1995 | u16 data; |
| 1996 | |
| 1997 | ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); |
| 1998 | r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE); |
| 1999 | |
| 2000 | if (tp->version == RTL_VER_03) { |
| 2001 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 2002 | data &= ~CTAP_SHORT_EN; |
| 2003 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 2004 | } |
| 2005 | |
| 2006 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2007 | data |= EEE_CLKDIV_EN; |
| 2008 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 2009 | |
| 2010 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 2011 | data |= EN_10M_BGOFF; |
| 2012 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 2013 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2014 | data |= EN_10M_PLLOFF; |
| 2015 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 2016 | data = sram_read(tp, SRAM_IMPEDANCE); |
| 2017 | data &= ~RX_DRIVING_MASK; |
| 2018 | sram_write(tp, SRAM_IMPEDANCE, data); |
| 2019 | |
| 2020 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 2021 | ocp_data |= PFM_PWM_SWITCH; |
| 2022 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 2023 | |
| 2024 | data = sram_read(tp, SRAM_LPF_CFG); |
| 2025 | data |= LPF_AUTO_TUNE; |
| 2026 | sram_write(tp, SRAM_LPF_CFG, data); |
| 2027 | |
| 2028 | data = sram_read(tp, SRAM_10M_AMP1); |
| 2029 | data |= GDAC_IB_UPALL; |
| 2030 | sram_write(tp, SRAM_10M_AMP1, data); |
| 2031 | data = sram_read(tp, SRAM_10M_AMP2); |
| 2032 | data |= AMP_DN; |
| 2033 | sram_write(tp, SRAM_10M_AMP2, data); |
| 2034 | } |
| 2035 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2036 | static void r8153_u1u2en(struct r8152 *tp, bool enable) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2037 | { |
| 2038 | u8 u1u2[8]; |
| 2039 | |
| 2040 | if (enable) |
| 2041 | memset(u1u2, 0xff, sizeof(u1u2)); |
| 2042 | else |
| 2043 | memset(u1u2, 0x00, sizeof(u1u2)); |
| 2044 | |
| 2045 | usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); |
| 2046 | } |
| 2047 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2048 | static void r8153_u2p3en(struct r8152 *tp, bool enable) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2049 | { |
| 2050 | u32 ocp_data; |
| 2051 | |
| 2052 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); |
| 2053 | if (enable) |
| 2054 | ocp_data |= U2P3_ENABLE; |
| 2055 | else |
| 2056 | ocp_data &= ~U2P3_ENABLE; |
| 2057 | ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); |
| 2058 | } |
| 2059 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2060 | static void r8153_power_cut_en(struct r8152 *tp, bool enable) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2061 | { |
| 2062 | u32 ocp_data; |
| 2063 | |
| 2064 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2065 | if (enable) |
| 2066 | ocp_data |= PWR_EN | PHASE2_EN; |
| 2067 | else |
| 2068 | ocp_data &= ~(PWR_EN | PHASE2_EN); |
| 2069 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2070 | |
| 2071 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2072 | ocp_data &= ~PCUT_STATUS; |
| 2073 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2074 | } |
| 2075 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2076 | static void r8153_first_init(struct r8152 *tp) |
| 2077 | { |
| 2078 | u32 ocp_data; |
| 2079 | int i; |
| 2080 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2081 | rxdy_gated_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2082 | r8153_teredo_off(tp); |
| 2083 | |
| 2084 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2085 | ocp_data &= ~RCR_ACPT_ALL; |
| 2086 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2087 | |
| 2088 | r8153_hw_phy_cfg(tp); |
| 2089 | |
| 2090 | rtl8152_nic_reset(tp); |
| 2091 | |
| 2092 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2093 | ocp_data &= ~NOW_IS_OOB; |
| 2094 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2095 | |
| 2096 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2097 | ocp_data &= ~MCU_BORW_EN; |
| 2098 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2099 | |
| 2100 | for (i = 0; i < 1000; i++) { |
| 2101 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2102 | if (ocp_data & LINK_LIST_READY) |
| 2103 | break; |
| 2104 | mdelay(1); |
| 2105 | } |
| 2106 | |
| 2107 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2108 | ocp_data |= RE_INIT_LL; |
| 2109 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2110 | |
| 2111 | for (i = 0; i < 1000; i++) { |
| 2112 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2113 | if (ocp_data & LINK_LIST_READY) |
| 2114 | break; |
| 2115 | mdelay(1); |
| 2116 | } |
| 2117 | |
| 2118 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 2119 | ocp_data &= ~CPCR_RX_VLAN; |
| 2120 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 2121 | |
| 2122 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 2123 | |
| 2124 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 2125 | ocp_data |= TCR0_AUTO_FIFO; |
| 2126 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 2127 | |
| 2128 | rtl8152_nic_reset(tp); |
| 2129 | |
| 2130 | /* rx share fifo credit full threshold */ |
| 2131 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 2132 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); |
| 2133 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); |
| 2134 | /* TX share fifo free credit full threshold */ |
| 2135 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); |
| 2136 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 2137 | /* rx aggregation */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2138 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 2139 | ocp_data &= ~RX_AGG_DISABLE; |
| 2140 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 2141 | } |
| 2142 | |
| 2143 | static void r8153_enter_oob(struct r8152 *tp) |
| 2144 | { |
| 2145 | u32 ocp_data; |
| 2146 | int i; |
| 2147 | |
| 2148 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2149 | ocp_data &= ~NOW_IS_OOB; |
| 2150 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2151 | |
| 2152 | rtl8152_disable(tp); |
| 2153 | |
| 2154 | for (i = 0; i < 1000; i++) { |
| 2155 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2156 | if (ocp_data & LINK_LIST_READY) |
| 2157 | break; |
| 2158 | mdelay(1); |
| 2159 | } |
| 2160 | |
| 2161 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 2162 | ocp_data |= RE_INIT_LL; |
| 2163 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 2164 | |
| 2165 | for (i = 0; i < 1000; i++) { |
| 2166 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2167 | if (ocp_data & LINK_LIST_READY) |
| 2168 | break; |
| 2169 | mdelay(1); |
| 2170 | } |
| 2171 | |
| 2172 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 2173 | |
| 2174 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2175 | ocp_data |= MAGIC_EN; |
| 2176 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 2177 | |
| 2178 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 2179 | ocp_data &= ~TEREDO_WAKE_MASK; |
| 2180 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 2181 | |
| 2182 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 2183 | ocp_data |= CPCR_RX_VLAN; |
| 2184 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 2185 | |
| 2186 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR); |
| 2187 | ocp_data |= ALDPS_PROXY_MODE; |
| 2188 | ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data); |
| 2189 | |
| 2190 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2191 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 2192 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 2193 | |
| 2194 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN); |
| 2195 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2196 | rxdy_gated_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2197 | |
| 2198 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2199 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 2200 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2201 | } |
| 2202 | |
| 2203 | static void r8153_disable_aldps(struct r8152 *tp) |
| 2204 | { |
| 2205 | u16 data; |
| 2206 | |
| 2207 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2208 | data &= ~EN_ALDPS; |
| 2209 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 2210 | msleep(20); |
| 2211 | } |
| 2212 | |
| 2213 | static void r8153_enable_aldps(struct r8152 *tp) |
| 2214 | { |
| 2215 | u16 data; |
| 2216 | |
| 2217 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 2218 | data |= EN_ALDPS; |
| 2219 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 2220 | } |
| 2221 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2222 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex) |
| 2223 | { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2224 | u16 bmcr, anar, gbcr; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2225 | int ret = 0; |
| 2226 | |
| 2227 | cancel_delayed_work_sync(&tp->schedule); |
| 2228 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 2229 | anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 2230 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2231 | if (tp->mii.supports_gmii) { |
| 2232 | gbcr = r8152_mdio_read(tp, MII_CTRL1000); |
| 2233 | gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 2234 | } else { |
| 2235 | gbcr = 0; |
| 2236 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2237 | |
| 2238 | if (autoneg == AUTONEG_DISABLE) { |
| 2239 | if (speed == SPEED_10) { |
| 2240 | bmcr = 0; |
| 2241 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2242 | } else if (speed == SPEED_100) { |
| 2243 | bmcr = BMCR_SPEED100; |
| 2244 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2245 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 2246 | bmcr = BMCR_SPEED1000; |
| 2247 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2248 | } else { |
| 2249 | ret = -EINVAL; |
| 2250 | goto out; |
| 2251 | } |
| 2252 | |
| 2253 | if (duplex == DUPLEX_FULL) |
| 2254 | bmcr |= BMCR_FULLDPLX; |
| 2255 | } else { |
| 2256 | if (speed == SPEED_10) { |
| 2257 | if (duplex == DUPLEX_FULL) |
| 2258 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2259 | else |
| 2260 | anar |= ADVERTISE_10HALF; |
| 2261 | } else if (speed == SPEED_100) { |
| 2262 | if (duplex == DUPLEX_FULL) { |
| 2263 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2264 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 2265 | } else { |
| 2266 | anar |= ADVERTISE_10HALF; |
| 2267 | anar |= ADVERTISE_100HALF; |
| 2268 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2269 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 2270 | if (duplex == DUPLEX_FULL) { |
| 2271 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 2272 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 2273 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
| 2274 | } else { |
| 2275 | anar |= ADVERTISE_10HALF; |
| 2276 | anar |= ADVERTISE_100HALF; |
| 2277 | gbcr |= ADVERTISE_1000HALF; |
| 2278 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2279 | } else { |
| 2280 | ret = -EINVAL; |
| 2281 | goto out; |
| 2282 | } |
| 2283 | |
| 2284 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART; |
| 2285 | } |
| 2286 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2287 | if (tp->mii.supports_gmii) |
| 2288 | r8152_mdio_write(tp, MII_CTRL1000, gbcr); |
| 2289 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2290 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 2291 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 2292 | |
| 2293 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2294 | |
| 2295 | return ret; |
| 2296 | } |
| 2297 | |
| 2298 | static void rtl8152_down(struct r8152 *tp) |
| 2299 | { |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2300 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2301 | r8152b_disable_aldps(tp); |
| 2302 | r8152b_enter_oob(tp); |
| 2303 | r8152b_enable_aldps(tp); |
| 2304 | } |
| 2305 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2306 | static void rtl8153_down(struct r8152 *tp) |
| 2307 | { |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2308 | r8153_u1u2en(tp, false); |
| 2309 | r8153_power_cut_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2310 | r8153_disable_aldps(tp); |
| 2311 | r8153_enter_oob(tp); |
| 2312 | r8153_enable_aldps(tp); |
| 2313 | } |
| 2314 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2315 | static void set_carrier(struct r8152 *tp) |
| 2316 | { |
| 2317 | struct net_device *netdev = tp->netdev; |
| 2318 | u8 speed; |
| 2319 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2320 | clear_bit(RTL8152_LINK_CHG, &tp->flags); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2321 | speed = rtl8152_get_speed(tp); |
| 2322 | |
| 2323 | if (speed & LINK_STATUS) { |
| 2324 | if (!(tp->speed & LINK_STATUS)) { |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2325 | tp->rtl_ops.enable(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2326 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 2327 | netif_carrier_on(netdev); |
| 2328 | } |
| 2329 | } else { |
| 2330 | if (tp->speed & LINK_STATUS) { |
| 2331 | netif_carrier_off(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2332 | tasklet_disable(&tp->tl); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2333 | tp->rtl_ops.disable(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2334 | tasklet_enable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2335 | } |
| 2336 | } |
| 2337 | tp->speed = speed; |
| 2338 | } |
| 2339 | |
| 2340 | static void rtl_work_func_t(struct work_struct *work) |
| 2341 | { |
| 2342 | struct r8152 *tp = container_of(work, struct r8152, schedule.work); |
| 2343 | |
| 2344 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 2345 | goto out1; |
| 2346 | |
| 2347 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2348 | goto out1; |
| 2349 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2350 | if (test_bit(RTL8152_LINK_CHG, &tp->flags)) |
| 2351 | set_carrier(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2352 | |
| 2353 | if (test_bit(RTL8152_SET_RX_MODE, &tp->flags)) |
| 2354 | _rtl8152_set_rx_mode(tp->netdev); |
| 2355 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2356 | out1: |
| 2357 | return; |
| 2358 | } |
| 2359 | |
| 2360 | static int rtl8152_open(struct net_device *netdev) |
| 2361 | { |
| 2362 | struct r8152 *tp = netdev_priv(netdev); |
| 2363 | int res = 0; |
| 2364 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2365 | rtl8152_set_speed(tp, AUTONEG_ENABLE, |
| 2366 | tp->mii.supports_gmii ? SPEED_1000 : SPEED_100, |
| 2367 | DUPLEX_FULL); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2368 | tp->speed = 0; |
| 2369 | netif_carrier_off(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2370 | netif_start_queue(netdev); |
| 2371 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 2372 | res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
| 2373 | if (res) { |
| 2374 | if (res == -ENODEV) |
| 2375 | netif_device_detach(tp->netdev); |
| 2376 | netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", |
| 2377 | res); |
| 2378 | } |
| 2379 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2380 | |
| 2381 | return res; |
| 2382 | } |
| 2383 | |
| 2384 | static int rtl8152_close(struct net_device *netdev) |
| 2385 | { |
| 2386 | struct r8152 *tp = netdev_priv(netdev); |
| 2387 | int res = 0; |
| 2388 | |
| 2389 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 2390 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2391 | cancel_delayed_work_sync(&tp->schedule); |
| 2392 | netif_stop_queue(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2393 | tasklet_disable(&tp->tl); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2394 | tp->rtl_ops.disable(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2395 | tasklet_enable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2396 | |
| 2397 | return res; |
| 2398 | } |
| 2399 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2400 | static void r8152b_enable_eee(struct r8152 *tp) |
| 2401 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 2402 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2403 | |
| 2404 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 2405 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 2406 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 2407 | ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN | |
| 2408 | EEE_10_CAP | EEE_NWAY_EN | |
| 2409 | TX_QUIET_EN | RX_QUIET_EN | |
| 2410 | SDRISETIME | RG_RXLPI_MSK_HFDUP | |
| 2411 | SDFALLTIME); |
| 2412 | ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN | |
| 2413 | RG_LDVQUIET_EN | RG_CKRSEL | |
| 2414 | RG_EEEPRG_EN); |
| 2415 | ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH); |
| 2416 | ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR); |
| 2417 | ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR); |
| 2418 | ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR); |
| 2419 | ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA); |
| 2420 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 2421 | } |
| 2422 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2423 | static void r8153_enable_eee(struct r8152 *tp) |
| 2424 | { |
| 2425 | u32 ocp_data; |
| 2426 | u16 data; |
| 2427 | |
| 2428 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 2429 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 2430 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 2431 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 2432 | data |= EEE10_EN; |
| 2433 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 2434 | data = ocp_reg_read(tp, OCP_EEE_CFG2); |
| 2435 | data |= MY1000_EEE | MY100_EEE; |
| 2436 | ocp_reg_write(tp, OCP_EEE_CFG2, data); |
| 2437 | } |
| 2438 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2439 | static void r8152b_enable_fc(struct r8152 *tp) |
| 2440 | { |
| 2441 | u16 anar; |
| 2442 | |
| 2443 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 2444 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 2445 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 2446 | } |
| 2447 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2448 | static void r8152b_init(struct r8152 *tp) |
| 2449 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2450 | u32 ocp_data; |
| 2451 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2452 | |
| 2453 | rtl_clear_bp(tp); |
| 2454 | |
| 2455 | if (tp->version == RTL_VER_01) { |
| 2456 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 2457 | ocp_data &= ~LED_MODE_MASK; |
| 2458 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 2459 | } |
| 2460 | |
| 2461 | r8152b_hw_phy_cfg(tp); |
| 2462 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2463 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2464 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2465 | |
| 2466 | r8152b_exit_oob(tp); |
| 2467 | |
| 2468 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 2469 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 2470 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 2471 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 2472 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 2473 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 2474 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 2475 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 2476 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 2477 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 2478 | |
| 2479 | r8152b_enable_eee(tp); |
| 2480 | r8152b_enable_aldps(tp); |
| 2481 | r8152b_enable_fc(tp); |
| 2482 | |
| 2483 | r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE | |
| 2484 | BMCR_ANRESTART); |
| 2485 | for (i = 0; i < 100; i++) { |
| 2486 | udelay(100); |
| 2487 | if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET)) |
| 2488 | break; |
| 2489 | } |
| 2490 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2491 | /* enable rx aggregation */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2492 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2493 | ocp_data &= ~RX_AGG_DISABLE; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2494 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 2495 | } |
| 2496 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2497 | static void r8153_init(struct r8152 *tp) |
| 2498 | { |
| 2499 | u32 ocp_data; |
| 2500 | int i; |
| 2501 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2502 | r8153_u1u2en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2503 | |
| 2504 | for (i = 0; i < 500; i++) { |
| 2505 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 2506 | AUTOLOAD_DONE) |
| 2507 | break; |
| 2508 | msleep(20); |
| 2509 | } |
| 2510 | |
| 2511 | for (i = 0; i < 500; i++) { |
| 2512 | ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK; |
| 2513 | if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN) |
| 2514 | break; |
| 2515 | msleep(20); |
| 2516 | } |
| 2517 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2518 | r8153_u2p3en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2519 | |
| 2520 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); |
| 2521 | ocp_data &= ~TIMER11_EN; |
| 2522 | ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); |
| 2523 | |
| 2524 | r8153_clear_bp(tp); |
| 2525 | |
| 2526 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 2527 | ocp_data &= ~LED_MODE_MASK; |
| 2528 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 2529 | |
| 2530 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL); |
| 2531 | ocp_data &= ~LPM_TIMER_MASK; |
| 2532 | if (tp->udev->speed == USB_SPEED_SUPER) |
| 2533 | ocp_data |= LPM_TIMER_500US; |
| 2534 | else |
| 2535 | ocp_data |= LPM_TIMER_500MS; |
| 2536 | ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); |
| 2537 | |
| 2538 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); |
| 2539 | ocp_data &= ~SEN_VAL_MASK; |
| 2540 | ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; |
| 2541 | ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); |
| 2542 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2543 | r8153_power_cut_en(tp, false); |
| 2544 | r8153_u1u2en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2545 | |
| 2546 | r8153_first_init(tp); |
| 2547 | |
| 2548 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO); |
| 2549 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO); |
| 2550 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, |
| 2551 | PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN | |
| 2552 | U1U2_SPDWN_EN | L1_SPDWN_EN); |
| 2553 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, |
| 2554 | PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN | |
| 2555 | TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN | |
| 2556 | EEE_SPDWN_EN); |
| 2557 | |
| 2558 | r8153_enable_eee(tp); |
| 2559 | r8153_enable_aldps(tp); |
| 2560 | r8152b_enable_fc(tp); |
| 2561 | |
| 2562 | r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE | |
| 2563 | BMCR_ANRESTART); |
| 2564 | } |
| 2565 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2566 | static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) |
| 2567 | { |
| 2568 | struct r8152 *tp = usb_get_intfdata(intf); |
| 2569 | |
| 2570 | netif_device_detach(tp->netdev); |
| 2571 | |
| 2572 | if (netif_running(tp->netdev)) { |
| 2573 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2574 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2575 | cancel_delayed_work_sync(&tp->schedule); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2576 | tasklet_disable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2579 | tp->rtl_ops.down(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2580 | |
| 2581 | return 0; |
| 2582 | } |
| 2583 | |
| 2584 | static int rtl8152_resume(struct usb_interface *intf) |
| 2585 | { |
| 2586 | struct r8152 *tp = usb_get_intfdata(intf); |
| 2587 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2588 | tp->rtl_ops.init(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2589 | netif_device_attach(tp->netdev); |
| 2590 | if (netif_running(tp->netdev)) { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2591 | rtl8152_set_speed(tp, AUTONEG_ENABLE, |
| 2592 | tp->mii.supports_gmii ? SPEED_1000 : SPEED_100, |
| 2593 | DUPLEX_FULL); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2594 | tp->speed = 0; |
| 2595 | netif_carrier_off(tp->netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2596 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2597 | usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2598 | tasklet_enable(&tp->tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | return 0; |
| 2602 | } |
| 2603 | |
| 2604 | static void rtl8152_get_drvinfo(struct net_device *netdev, |
| 2605 | struct ethtool_drvinfo *info) |
| 2606 | { |
| 2607 | struct r8152 *tp = netdev_priv(netdev); |
| 2608 | |
| 2609 | strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN); |
| 2610 | strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN); |
| 2611 | usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); |
| 2612 | } |
| 2613 | |
| 2614 | static |
| 2615 | int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd) |
| 2616 | { |
| 2617 | struct r8152 *tp = netdev_priv(netdev); |
| 2618 | |
| 2619 | if (!tp->mii.mdio_read) |
| 2620 | return -EOPNOTSUPP; |
| 2621 | |
| 2622 | return mii_ethtool_gset(&tp->mii, cmd); |
| 2623 | } |
| 2624 | |
| 2625 | static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 2626 | { |
| 2627 | struct r8152 *tp = netdev_priv(dev); |
| 2628 | |
| 2629 | return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex); |
| 2630 | } |
| 2631 | |
| 2632 | static struct ethtool_ops ops = { |
| 2633 | .get_drvinfo = rtl8152_get_drvinfo, |
| 2634 | .get_settings = rtl8152_get_settings, |
| 2635 | .set_settings = rtl8152_set_settings, |
| 2636 | .get_link = ethtool_op_get_link, |
| 2637 | }; |
| 2638 | |
| 2639 | static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) |
| 2640 | { |
| 2641 | struct r8152 *tp = netdev_priv(netdev); |
| 2642 | struct mii_ioctl_data *data = if_mii(rq); |
| 2643 | int res = 0; |
| 2644 | |
| 2645 | switch (cmd) { |
| 2646 | case SIOCGMIIPHY: |
| 2647 | data->phy_id = R8152_PHY_ID; /* Internal PHY */ |
| 2648 | break; |
| 2649 | |
| 2650 | case SIOCGMIIREG: |
| 2651 | data->val_out = r8152_mdio_read(tp, data->reg_num); |
| 2652 | break; |
| 2653 | |
| 2654 | case SIOCSMIIREG: |
| 2655 | if (!capable(CAP_NET_ADMIN)) { |
| 2656 | res = -EPERM; |
| 2657 | break; |
| 2658 | } |
| 2659 | r8152_mdio_write(tp, data->reg_num, data->val_in); |
| 2660 | break; |
| 2661 | |
| 2662 | default: |
| 2663 | res = -EOPNOTSUPP; |
| 2664 | } |
| 2665 | |
| 2666 | return res; |
| 2667 | } |
| 2668 | |
| 2669 | static const struct net_device_ops rtl8152_netdev_ops = { |
| 2670 | .ndo_open = rtl8152_open, |
| 2671 | .ndo_stop = rtl8152_close, |
| 2672 | .ndo_do_ioctl = rtl8152_ioctl, |
| 2673 | .ndo_start_xmit = rtl8152_start_xmit, |
| 2674 | .ndo_tx_timeout = rtl8152_tx_timeout, |
| 2675 | .ndo_set_rx_mode = rtl8152_set_rx_mode, |
| 2676 | .ndo_set_mac_address = rtl8152_set_mac_address, |
| 2677 | |
| 2678 | .ndo_change_mtu = eth_change_mtu, |
| 2679 | .ndo_validate_addr = eth_validate_addr, |
| 2680 | }; |
| 2681 | |
| 2682 | static void r8152b_get_version(struct r8152 *tp) |
| 2683 | { |
| 2684 | u32 ocp_data; |
| 2685 | u16 version; |
| 2686 | |
| 2687 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1); |
| 2688 | version = (u16)(ocp_data & VERSION_MASK); |
| 2689 | |
| 2690 | switch (version) { |
| 2691 | case 0x4c00: |
| 2692 | tp->version = RTL_VER_01; |
| 2693 | break; |
| 2694 | case 0x4c10: |
| 2695 | tp->version = RTL_VER_02; |
| 2696 | break; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2697 | case 0x5c00: |
| 2698 | tp->version = RTL_VER_03; |
| 2699 | tp->mii.supports_gmii = 1; |
| 2700 | break; |
| 2701 | case 0x5c10: |
| 2702 | tp->version = RTL_VER_04; |
| 2703 | tp->mii.supports_gmii = 1; |
| 2704 | break; |
| 2705 | case 0x5c20: |
| 2706 | tp->version = RTL_VER_05; |
| 2707 | tp->mii.supports_gmii = 1; |
| 2708 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2709 | default: |
| 2710 | netif_info(tp, probe, tp->netdev, |
| 2711 | "Unknown version 0x%04x\n", version); |
| 2712 | break; |
| 2713 | } |
| 2714 | } |
| 2715 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 2716 | static void rtl8152_unload(struct r8152 *tp) |
| 2717 | { |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2718 | if (tp->version != RTL_VER_01) |
| 2719 | r8152_power_cut_en(tp, true); |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 2720 | } |
| 2721 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2722 | static void rtl8153_unload(struct r8152 *tp) |
| 2723 | { |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 2724 | r8153_power_cut_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2725 | } |
| 2726 | |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 2727 | static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id) |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2728 | { |
| 2729 | struct rtl_ops *ops = &tp->rtl_ops; |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 2730 | int ret = -ENODEV; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2731 | |
| 2732 | switch (id->idVendor) { |
| 2733 | case VENDOR_ID_REALTEK: |
| 2734 | switch (id->idProduct) { |
| 2735 | case PRODUCT_ID_RTL8152: |
| 2736 | ops->init = r8152b_init; |
| 2737 | ops->enable = rtl8152_enable; |
| 2738 | ops->disable = rtl8152_disable; |
| 2739 | ops->down = rtl8152_down; |
| 2740 | ops->unload = rtl8152_unload; |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 2741 | ret = 0; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2742 | break; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2743 | case PRODUCT_ID_RTL8153: |
| 2744 | ops->init = r8153_init; |
| 2745 | ops->enable = rtl8153_enable; |
| 2746 | ops->disable = rtl8152_disable; |
| 2747 | ops->down = rtl8153_down; |
| 2748 | ops->unload = rtl8153_unload; |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 2749 | ret = 0; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2750 | break; |
| 2751 | default: |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2752 | break; |
| 2753 | } |
| 2754 | break; |
| 2755 | |
| 2756 | case VENDOR_ID_SAMSUNG: |
| 2757 | switch (id->idProduct) { |
| 2758 | case PRODUCT_ID_SAMSUNG: |
| 2759 | ops->init = r8153_init; |
| 2760 | ops->enable = rtl8153_enable; |
| 2761 | ops->disable = rtl8152_disable; |
| 2762 | ops->down = rtl8153_down; |
| 2763 | ops->unload = rtl8153_unload; |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 2764 | ret = 0; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2765 | break; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2766 | default: |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2767 | break; |
| 2768 | } |
| 2769 | break; |
| 2770 | |
| 2771 | default: |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2772 | break; |
| 2773 | } |
| 2774 | |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 2775 | if (ret) |
| 2776 | netif_err(tp, probe, tp->netdev, "Unknown Device\n"); |
| 2777 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2778 | return ret; |
| 2779 | } |
| 2780 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2781 | static int rtl8152_probe(struct usb_interface *intf, |
| 2782 | const struct usb_device_id *id) |
| 2783 | { |
| 2784 | struct usb_device *udev = interface_to_usbdev(intf); |
| 2785 | struct r8152 *tp; |
| 2786 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2787 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2788 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2789 | netdev = alloc_etherdev(sizeof(struct r8152)); |
| 2790 | if (!netdev) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 2791 | dev_err(&intf->dev, "Out of memory\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2792 | return -ENOMEM; |
| 2793 | } |
| 2794 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2795 | SET_NETDEV_DEV(netdev, &intf->dev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2796 | tp = netdev_priv(netdev); |
| 2797 | tp->msg_enable = 0x7FFF; |
| 2798 | |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 2799 | tp->udev = udev; |
| 2800 | tp->netdev = netdev; |
| 2801 | tp->intf = intf; |
| 2802 | |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 2803 | ret = rtl_ops_init(tp, id); |
| 2804 | if (ret) |
| 2805 | goto out; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2806 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2807 | tasklet_init(&tp->tl, bottom_half, (unsigned long)tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2808 | INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); |
| 2809 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2810 | netdev->netdev_ops = &rtl8152_netdev_ops; |
| 2811 | netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2812 | |
| 2813 | netdev->features |= NETIF_F_IP_CSUM; |
| 2814 | netdev->hw_features = NETIF_F_IP_CSUM; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2815 | SET_ETHTOOL_OPS(netdev, &ops); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2816 | |
| 2817 | tp->mii.dev = netdev; |
| 2818 | tp->mii.mdio_read = read_mii_word; |
| 2819 | tp->mii.mdio_write = write_mii_word; |
| 2820 | tp->mii.phy_id_mask = 0x3f; |
| 2821 | tp->mii.reg_num_mask = 0x1f; |
| 2822 | tp->mii.phy_id = R8152_PHY_ID; |
| 2823 | tp->mii.supports_gmii = 0; |
| 2824 | |
| 2825 | r8152b_get_version(tp); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2826 | tp->rtl_ops.init(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2827 | set_ethernet_addr(tp); |
| 2828 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2829 | ret = alloc_all_mem(tp); |
| 2830 | if (ret) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2831 | goto out; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2832 | |
| 2833 | usb_set_intfdata(intf, tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2834 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2835 | ret = register_netdev(netdev); |
| 2836 | if (ret != 0) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 2837 | netif_err(tp, probe, netdev, "couldn't register the device\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2838 | goto out1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 2841 | netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2842 | |
| 2843 | return 0; |
| 2844 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2845 | out1: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2846 | usb_set_intfdata(intf, NULL); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2847 | out: |
| 2848 | free_netdev(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2849 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2852 | static void rtl8152_disconnect(struct usb_interface *intf) |
| 2853 | { |
| 2854 | struct r8152 *tp = usb_get_intfdata(intf); |
| 2855 | |
| 2856 | usb_set_intfdata(intf, NULL); |
| 2857 | if (tp) { |
| 2858 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 2859 | tasklet_kill(&tp->tl); |
| 2860 | unregister_netdev(tp->netdev); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 2861 | tp->rtl_ops.unload(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2862 | free_all_mem(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2863 | free_netdev(tp->netdev); |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | /* table of devices that work with this driver */ |
| 2868 | static struct usb_device_id rtl8152_table[] = { |
hayeswang | c7de7de | 2014-01-15 10:42:16 +0800 | [diff] [blame] | 2869 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)}, |
| 2870 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)}, |
| 2871 | {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)}, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2872 | {} |
| 2873 | }; |
| 2874 | |
| 2875 | MODULE_DEVICE_TABLE(usb, rtl8152_table); |
| 2876 | |
| 2877 | static struct usb_driver rtl8152_driver = { |
| 2878 | .name = MODULENAME, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2879 | .id_table = rtl8152_table, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2880 | .probe = rtl8152_probe, |
| 2881 | .disconnect = rtl8152_disconnect, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2882 | .suspend = rtl8152_suspend, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2883 | .resume = rtl8152_resume, |
| 2884 | .reset_resume = rtl8152_resume, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2885 | }; |
| 2886 | |
Sachin Kamat | b4236daa | 2013-05-16 17:48:08 +0000 | [diff] [blame] | 2887 | module_usb_driver(rtl8152_driver); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2888 | |
| 2889 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 2890 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 2891 | MODULE_LICENSE("GPL"); |