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