1 /* 2 * Copyright (c) 2013 Realtek Semiconductor Corp. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * version 2 as published by the Free Software Foundation. 7 * 8 */ 9 10 #include <linux/init.h> 11 #include <linux/signal.h> 12 #include <linux/slab.h> 13 #include <linux/module.h> 14 #include <linux/netdevice.h> 15 #include <linux/etherdevice.h> 16 #include <linux/mii.h> 17 #include <linux/ethtool.h> 18 #include <linux/usb.h> 19 #include <linux/crc32.h> 20 #include <linux/if_vlan.h> 21 #include <linux/uaccess.h> 22 23 /* Version Information */ 24 #define DRIVER_VERSION "v1.0.0 (2013/05/03)" 25 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" 26 #define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters" 27 #define MODULENAME "r8152" 28 29 #define R8152_PHY_ID 32 30 31 #define PLA_IDR 0xc000 32 #define PLA_RCR 0xc010 33 #define PLA_RMS 0xc016 34 #define PLA_RXFIFO_CTRL0 0xc0a0 35 #define PLA_RXFIFO_CTRL1 0xc0a4 36 #define PLA_RXFIFO_CTRL2 0xc0a8 37 #define PLA_FMC 0xc0b4 38 #define PLA_CFG_WOL 0xc0b6 39 #define PLA_MAR 0xcd00 40 #define PAL_BDC_CR 0xd1a0 41 #define PLA_LEDSEL 0xdd90 42 #define PLA_LED_FEATURE 0xdd92 43 #define PLA_PHYAR 0xde00 44 #define PLA_GPHY_INTR_IMR 0xe022 45 #define PLA_EEE_CR 0xe040 46 #define PLA_EEEP_CR 0xe080 47 #define PLA_MAC_PWR_CTRL 0xe0c0 48 #define PLA_TCR0 0xe610 49 #define PLA_TCR1 0xe612 50 #define PLA_TXFIFO_CTRL 0xe618 51 #define PLA_RSTTELLY 0xe800 52 #define PLA_CR 0xe813 53 #define PLA_CRWECR 0xe81c 54 #define PLA_CONFIG5 0xe822 55 #define PLA_PHY_PWR 0xe84c 56 #define PLA_OOB_CTRL 0xe84f 57 #define PLA_CPCR 0xe854 58 #define PLA_MISC_0 0xe858 59 #define PLA_MISC_1 0xe85a 60 #define PLA_OCP_GPHY_BASE 0xe86c 61 #define PLA_TELLYCNT 0xe890 62 #define PLA_SFF_STS_7 0xe8de 63 #define PLA_PHYSTATUS 0xe908 64 #define PLA_BP_BA 0xfc26 65 #define PLA_BP_0 0xfc28 66 #define PLA_BP_1 0xfc2a 67 #define PLA_BP_2 0xfc2c 68 #define PLA_BP_3 0xfc2e 69 #define PLA_BP_4 0xfc30 70 #define PLA_BP_5 0xfc32 71 #define PLA_BP_6 0xfc34 72 #define PLA_BP_7 0xfc36 73 74 #define USB_DEV_STAT 0xb808 75 #define USB_USB_CTRL 0xd406 76 #define USB_PHY_CTRL 0xd408 77 #define USB_TX_AGG 0xd40a 78 #define USB_RX_BUF_TH 0xd40c 79 #define USB_USB_TIMER 0xd428 80 #define USB_PM_CTRL_STATUS 0xd432 81 #define USB_TX_DMA 0xd434 82 #define USB_UPS_CTRL 0xd800 83 #define USB_BP_BA 0xfc26 84 #define USB_BP_0 0xfc28 85 #define USB_BP_1 0xfc2a 86 #define USB_BP_2 0xfc2c 87 #define USB_BP_3 0xfc2e 88 #define USB_BP_4 0xfc30 89 #define USB_BP_5 0xfc32 90 #define USB_BP_6 0xfc34 91 #define USB_BP_7 0xfc36 92 93 /* OCP Registers */ 94 #define OCP_ALDPS_CONFIG 0x2010 95 #define OCP_EEE_CONFIG1 0x2080 96 #define OCP_EEE_CONFIG2 0x2092 97 #define OCP_EEE_CONFIG3 0x2094 98 #define OCP_EEE_AR 0xa41a 99 #define OCP_EEE_DATA 0xa41c 100 101 /* PLA_RCR */ 102 #define RCR_AAP 0x00000001 103 #define RCR_APM 0x00000002 104 #define RCR_AM 0x00000004 105 #define RCR_AB 0x00000008 106 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) 107 108 /* PLA_RXFIFO_CTRL0 */ 109 #define RXFIFO_THR1_NORMAL 0x00080002 110 #define RXFIFO_THR1_OOB 0x01800003 111 112 /* PLA_RXFIFO_CTRL1 */ 113 #define RXFIFO_THR2_FULL 0x00000060 114 #define RXFIFO_THR2_HIGH 0x00000038 115 #define RXFIFO_THR2_OOB 0x0000004a 116 117 /* PLA_RXFIFO_CTRL2 */ 118 #define RXFIFO_THR3_FULL 0x00000078 119 #define RXFIFO_THR3_HIGH 0x00000048 120 #define RXFIFO_THR3_OOB 0x0000005a 121 122 /* PLA_TXFIFO_CTRL */ 123 #define TXFIFO_THR_NORMAL 0x00400008 124 125 /* PLA_FMC */ 126 #define FMC_FCR_MCU_EN 0x0001 127 128 /* PLA_EEEP_CR */ 129 #define EEEP_CR_EEEP_TX 0x0002 130 131 /* PLA_TCR0 */ 132 #define TCR0_TX_EMPTY 0x0800 133 #define TCR0_AUTO_FIFO 0x0080 134 135 /* PLA_TCR1 */ 136 #define VERSION_MASK 0x7cf0 137 138 /* PLA_CR */ 139 #define CR_RST 0x10 140 #define CR_RE 0x08 141 #define CR_TE 0x04 142 143 /* PLA_CRWECR */ 144 #define CRWECR_NORAML 0x00 145 #define CRWECR_CONFIG 0xc0 146 147 /* PLA_OOB_CTRL */ 148 #define NOW_IS_OOB 0x80 149 #define TXFIFO_EMPTY 0x20 150 #define RXFIFO_EMPTY 0x10 151 #define LINK_LIST_READY 0x02 152 #define DIS_MCU_CLROOB 0x01 153 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) 154 155 /* PLA_MISC_1 */ 156 #define RXDY_GATED_EN 0x0008 157 158 /* PLA_SFF_STS_7 */ 159 #define RE_INIT_LL 0x8000 160 #define MCU_BORW_EN 0x4000 161 162 /* PLA_CPCR */ 163 #define CPCR_RX_VLAN 0x0040 164 165 /* PLA_CFG_WOL */ 166 #define MAGIC_EN 0x0001 167 168 /* PAL_BDC_CR */ 169 #define ALDPS_PROXY_MODE 0x0001 170 171 /* PLA_CONFIG5 */ 172 #define LAN_WAKE_EN 0x0002 173 174 /* PLA_LED_FEATURE */ 175 #define LED_MODE_MASK 0x0700 176 177 /* PLA_PHY_PWR */ 178 #define TX_10M_IDLE_EN 0x0080 179 #define PFM_PWM_SWITCH 0x0040 180 181 /* PLA_MAC_PWR_CTRL */ 182 #define D3_CLK_GATED_EN 0x00004000 183 #define MCU_CLK_RATIO 0x07010f07 184 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f 185 186 /* PLA_GPHY_INTR_IMR */ 187 #define GPHY_STS_MSK 0x0001 188 #define SPEED_DOWN_MSK 0x0002 189 #define SPDWN_RXDV_MSK 0x0004 190 #define SPDWN_LINKCHG_MSK 0x0008 191 192 /* PLA_PHYAR */ 193 #define PHYAR_FLAG 0x80000000 194 195 /* PLA_EEE_CR */ 196 #define EEE_RX_EN 0x0001 197 #define EEE_TX_EN 0x0002 198 199 /* USB_DEV_STAT */ 200 #define STAT_SPEED_MASK 0x0006 201 #define STAT_SPEED_HIGH 0x0000 202 #define STAT_SPEED_FULL 0x0001 203 204 /* USB_TX_AGG */ 205 #define TX_AGG_MAX_THRESHOLD 0x03 206 207 /* USB_RX_BUF_TH */ 208 #define RX_BUF_THR 0x7a120180 209 210 /* USB_TX_DMA */ 211 #define TEST_MODE_DISABLE 0x00000001 212 #define TX_SIZE_ADJUST1 0x00000100 213 214 /* USB_UPS_CTRL */ 215 #define POWER_CUT 0x0100 216 217 /* USB_PM_CTRL_STATUS */ 218 #define RWSUME_INDICATE 0x0001 219 220 /* USB_USB_CTRL */ 221 #define RX_AGG_DISABLE 0x0010 222 223 /* OCP_ALDPS_CONFIG */ 224 #define ENPWRSAVE 0x8000 225 #define ENPDNPS 0x0200 226 #define LINKENA 0x0100 227 #define DIS_SDSAVE 0x0010 228 229 /* OCP_EEE_CONFIG1 */ 230 #define RG_TXLPI_MSK_HFDUP 0x8000 231 #define RG_MATCLR_EN 0x4000 232 #define EEE_10_CAP 0x2000 233 #define EEE_NWAY_EN 0x1000 234 #define TX_QUIET_EN 0x0200 235 #define RX_QUIET_EN 0x0100 236 #define SDRISETIME 0x0010 /* bit 4 ~ 6 */ 237 #define RG_RXLPI_MSK_HFDUP 0x0008 238 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ 239 240 /* OCP_EEE_CONFIG2 */ 241 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ 242 #define RG_DACQUIET_EN 0x0400 243 #define RG_LDVQUIET_EN 0x0200 244 #define RG_CKRSEL 0x0020 245 #define RG_EEEPRG_EN 0x0010 246 247 /* OCP_EEE_CONFIG3 */ 248 #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */ 249 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ 250 #define MSK_PH 0x0006 /* bit 0 ~ 3 */ 251 252 /* OCP_EEE_AR */ 253 /* bit[15:14] function */ 254 #define FUN_ADDR 0x0000 255 #define FUN_DATA 0x4000 256 /* bit[4:0] device addr */ 257 #define DEVICE_ADDR 0x0007 258 259 /* OCP_EEE_DATA */ 260 #define EEE_ADDR 0x003C 261 #define EEE_DATA 0x0002 262 263 enum rtl_register_content { 264 _100bps = 0x08, 265 _10bps = 0x04, 266 LINK_STATUS = 0x02, 267 FULL_DUP = 0x01, 268 }; 269 270 #define RTL8152_REQT_READ 0xc0 271 #define RTL8152_REQT_WRITE 0x40 272 #define RTL8152_REQ_GET_REGS 0x05 273 #define RTL8152_REQ_SET_REGS 0x05 274 275 #define BYTE_EN_DWORD 0xff 276 #define BYTE_EN_WORD 0x33 277 #define BYTE_EN_BYTE 0x11 278 #define BYTE_EN_SIX_BYTES 0x3f 279 #define BYTE_EN_START_MASK 0x0f 280 #define BYTE_EN_END_MASK 0xf0 281 282 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN) 283 #define RTL8152_TX_TIMEOUT (HZ) 284 285 /* rtl8152 flags */ 286 enum rtl8152_flags { 287 RTL8152_UNPLUG = 0, 288 RX_URB_FAIL, 289 RTL8152_SET_RX_MODE, 290 WORK_ENABLE 291 }; 292 293 /* Define these values to match your device */ 294 #define VENDOR_ID_REALTEK 0x0bda 295 #define PRODUCT_ID_RTL8152 0x8152 296 297 #define MCU_TYPE_PLA 0x0100 298 #define MCU_TYPE_USB 0x0000 299 300 struct rx_desc { 301 u32 opts1; 302 #define RX_LEN_MASK 0x7fff 303 u32 opts2; 304 u32 opts3; 305 u32 opts4; 306 u32 opts5; 307 u32 opts6; 308 }; 309 310 struct tx_desc { 311 u32 opts1; 312 #define TX_FS (1 << 31) /* First segment of a packet */ 313 #define TX_LS (1 << 30) /* Final segment of a packet */ 314 #define TX_LEN_MASK 0xffff 315 u32 opts2; 316 }; 317 318 struct r8152 { 319 unsigned long flags; 320 struct usb_device *udev; 321 struct tasklet_struct tl; 322 struct net_device *netdev; 323 struct urb *rx_urb, *tx_urb; 324 struct sk_buff *tx_skb, *rx_skb; 325 struct delayed_work schedule; 326 struct mii_if_info mii; 327 u32 msg_enable; 328 u16 ocp_base; 329 u8 version; 330 u8 speed; 331 }; 332 333 enum rtl_version { 334 RTL_VER_UNKNOWN = 0, 335 RTL_VER_01, 336 RTL_VER_02 337 }; 338 339 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). 340 * The RTL chips use a 64 element hash table based on the Ethernet CRC. 341 */ 342 static const int multicast_filter_limit = 32; 343 344 static 345 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 346 { 347 return usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), 348 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, 349 value, index, data, size, 500); 350 } 351 352 static 353 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) 354 { 355 return usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), 356 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, 357 value, index, data, size, 500); 358 } 359 360 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, 361 void *data, u16 type) 362 { 363 u16 limit = 64; 364 int ret = 0; 365 366 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 367 return -ENODEV; 368 369 /* both size and indix must be 4 bytes align */ 370 if ((size & 3) || !size || (index & 3) || !data) 371 return -EPERM; 372 373 if ((u32)index + (u32)size > 0xffff) 374 return -EPERM; 375 376 while (size) { 377 if (size > limit) { 378 ret = get_registers(tp, index, type, limit, data); 379 if (ret < 0) 380 break; 381 382 index += limit; 383 data += limit; 384 size -= limit; 385 } else { 386 ret = get_registers(tp, index, type, size, data); 387 if (ret < 0) 388 break; 389 390 index += size; 391 data += size; 392 size = 0; 393 break; 394 } 395 } 396 397 return ret; 398 } 399 400 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, 401 u16 size, void *data, u16 type) 402 { 403 int ret; 404 u16 byteen_start, byteen_end, byen; 405 u16 limit = 512; 406 407 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 408 return -ENODEV; 409 410 /* both size and indix must be 4 bytes align */ 411 if ((size & 3) || !size || (index & 3) || !data) 412 return -EPERM; 413 414 if ((u32)index + (u32)size > 0xffff) 415 return -EPERM; 416 417 byteen_start = byteen & BYTE_EN_START_MASK; 418 byteen_end = byteen & BYTE_EN_END_MASK; 419 420 byen = byteen_start | (byteen_start << 4); 421 ret = set_registers(tp, index, type | byen, 4, data); 422 if (ret < 0) 423 goto error1; 424 425 index += 4; 426 data += 4; 427 size -= 4; 428 429 if (size) { 430 size -= 4; 431 432 while (size) { 433 if (size > limit) { 434 ret = set_registers(tp, index, 435 type | BYTE_EN_DWORD, 436 limit, data); 437 if (ret < 0) 438 goto error1; 439 440 index += limit; 441 data += limit; 442 size -= limit; 443 } else { 444 ret = set_registers(tp, index, 445 type | BYTE_EN_DWORD, 446 size, data); 447 if (ret < 0) 448 goto error1; 449 450 index += size; 451 data += size; 452 size = 0; 453 break; 454 } 455 } 456 457 byen = byteen_end | (byteen_end >> 4); 458 ret = set_registers(tp, index, type | byen, 4, data); 459 if (ret < 0) 460 goto error1; 461 } 462 463 error1: 464 return ret; 465 } 466 467 static inline 468 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) 469 { 470 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); 471 } 472 473 static inline 474 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 475 { 476 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); 477 } 478 479 static inline 480 int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) 481 { 482 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB); 483 } 484 485 static inline 486 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) 487 { 488 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); 489 } 490 491 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) 492 { 493 u32 data; 494 495 if (type == MCU_TYPE_PLA) 496 pla_ocp_read(tp, index, sizeof(data), &data); 497 else 498 usb_ocp_read(tp, index, sizeof(data), &data); 499 500 return __le32_to_cpu(data); 501 } 502 503 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) 504 { 505 if (type == MCU_TYPE_PLA) 506 pla_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(data), &data); 507 else 508 usb_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(data), &data); 509 } 510 511 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) 512 { 513 u32 data; 514 u8 shift = index & 2; 515 516 index &= ~3; 517 518 if (type == MCU_TYPE_PLA) 519 pla_ocp_read(tp, index, sizeof(data), &data); 520 else 521 usb_ocp_read(tp, index, sizeof(data), &data); 522 523 data = __le32_to_cpu(data); 524 data >>= (shift * 8); 525 data &= 0xffff; 526 527 return (u16)data; 528 } 529 530 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) 531 { 532 u32 tmp, mask = 0xffff; 533 u16 byen = BYTE_EN_WORD; 534 u8 shift = index & 2; 535 536 data &= mask; 537 538 if (index & 2) { 539 byen <<= shift; 540 mask <<= (shift * 8); 541 data <<= (shift * 8); 542 index &= ~3; 543 } 544 545 if (type == MCU_TYPE_PLA) 546 pla_ocp_read(tp, index, sizeof(tmp), &tmp); 547 else 548 usb_ocp_read(tp, index, sizeof(tmp), &tmp); 549 550 tmp = __le32_to_cpu(tmp) & ~mask; 551 tmp |= data; 552 tmp = __cpu_to_le32(tmp); 553 554 if (type == MCU_TYPE_PLA) 555 pla_ocp_write(tp, index, byen, sizeof(tmp), &tmp); 556 else 557 usb_ocp_write(tp, index, byen, sizeof(tmp), &tmp); 558 } 559 560 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) 561 { 562 u32 data; 563 u8 shift = index & 3; 564 565 index &= ~3; 566 567 if (type == MCU_TYPE_PLA) 568 pla_ocp_read(tp, index, sizeof(data), &data); 569 else 570 usb_ocp_read(tp, index, sizeof(data), &data); 571 572 data = __le32_to_cpu(data); 573 data >>= (shift * 8); 574 data &= 0xff; 575 576 return (u8)data; 577 } 578 579 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) 580 { 581 u32 tmp, mask = 0xff; 582 u16 byen = BYTE_EN_BYTE; 583 u8 shift = index & 3; 584 585 data &= mask; 586 587 if (index & 3) { 588 byen <<= shift; 589 mask <<= (shift * 8); 590 data <<= (shift * 8); 591 index &= ~3; 592 } 593 594 if (type == MCU_TYPE_PLA) 595 pla_ocp_read(tp, index, sizeof(tmp), &tmp); 596 else 597 usb_ocp_read(tp, index, sizeof(tmp), &tmp); 598 599 tmp = __le32_to_cpu(tmp) & ~mask; 600 tmp |= data; 601 tmp = __cpu_to_le32(tmp); 602 603 if (type == MCU_TYPE_PLA) 604 pla_ocp_write(tp, index, byen, sizeof(tmp), &tmp); 605 else 606 usb_ocp_write(tp, index, byen, sizeof(tmp), &tmp); 607 } 608 609 static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) 610 { 611 u32 ocp_data; 612 int i; 613 614 ocp_data = PHYAR_FLAG | ((reg_addr & 0x1f) << 16) | 615 (value & 0xffff); 616 617 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data); 618 619 for (i = 20; i > 0; i--) { 620 udelay(25); 621 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR); 622 if (!(ocp_data & PHYAR_FLAG)) 623 break; 624 } 625 udelay(20); 626 } 627 628 static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) 629 { 630 u32 ocp_data; 631 int i; 632 633 ocp_data = (reg_addr & 0x1f) << 16; 634 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data); 635 636 for (i = 20; i > 0; i--) { 637 udelay(25); 638 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR); 639 if (ocp_data & PHYAR_FLAG) 640 break; 641 } 642 udelay(20); 643 644 if (!(ocp_data & PHYAR_FLAG)) 645 return -EAGAIN; 646 647 return (u16)(ocp_data & 0xffff); 648 } 649 650 static int read_mii_word(struct net_device *netdev, int phy_id, int reg) 651 { 652 struct r8152 *tp = netdev_priv(netdev); 653 654 if (phy_id != R8152_PHY_ID) 655 return -EINVAL; 656 657 return r8152_mdio_read(tp, reg); 658 } 659 660 static 661 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) 662 { 663 struct r8152 *tp = netdev_priv(netdev); 664 665 if (phy_id != R8152_PHY_ID) 666 return; 667 668 r8152_mdio_write(tp, reg, val); 669 } 670 671 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) 672 { 673 u16 ocp_base, ocp_index; 674 675 ocp_base = addr & 0xf000; 676 if (ocp_base != tp->ocp_base) { 677 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); 678 tp->ocp_base = ocp_base; 679 } 680 681 ocp_index = (addr & 0x0fff) | 0xb000; 682 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); 683 } 684 685 static inline void set_ethernet_addr(struct r8152 *tp) 686 { 687 struct net_device *dev = tp->netdev; 688 u8 *node_id; 689 690 node_id = kmalloc(sizeof(u8) * 8, GFP_KERNEL); 691 if (!node_id) { 692 netif_err(tp, probe, dev, "out of memory"); 693 return; 694 } 695 696 if (pla_ocp_read(tp, PLA_IDR, sizeof(u8) * 8, node_id) < 0) 697 netif_notice(tp, probe, dev, "inet addr fail\n"); 698 else { 699 memcpy(dev->dev_addr, node_id, dev->addr_len); 700 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); 701 } 702 kfree(node_id); 703 } 704 705 static int rtl8152_set_mac_address(struct net_device *netdev, void *p) 706 { 707 struct r8152 *tp = netdev_priv(netdev); 708 struct sockaddr *addr = p; 709 710 if (!is_valid_ether_addr(addr->sa_data)) 711 return -EADDRNOTAVAIL; 712 713 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); 714 715 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); 716 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); 717 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 718 719 return 0; 720 } 721 722 static int alloc_all_urbs(struct r8152 *tp) 723 { 724 tp->rx_urb = usb_alloc_urb(0, GFP_KERNEL); 725 if (!tp->rx_urb) 726 return 0; 727 tp->tx_urb = usb_alloc_urb(0, GFP_KERNEL); 728 if (!tp->tx_urb) { 729 usb_free_urb(tp->rx_urb); 730 return 0; 731 } 732 733 return 1; 734 } 735 736 static void free_all_urbs(struct r8152 *tp) 737 { 738 usb_free_urb(tp->rx_urb); 739 usb_free_urb(tp->tx_urb); 740 } 741 742 static struct net_device_stats *rtl8152_get_stats(struct net_device *dev) 743 { 744 return &dev->stats; 745 } 746 747 static void read_bulk_callback(struct urb *urb) 748 { 749 struct r8152 *tp; 750 unsigned pkt_len; 751 struct sk_buff *skb; 752 struct net_device *netdev; 753 struct net_device_stats *stats; 754 int status = urb->status; 755 int result; 756 struct rx_desc *rx_desc; 757 758 tp = urb->context; 759 if (!tp) 760 return; 761 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 762 return; 763 netdev = tp->netdev; 764 if (!netif_device_present(netdev)) 765 return; 766 767 stats = rtl8152_get_stats(netdev); 768 switch (status) { 769 case 0: 770 break; 771 case -ESHUTDOWN: 772 set_bit(RTL8152_UNPLUG, &tp->flags); 773 netif_device_detach(tp->netdev); 774 case -ENOENT: 775 return; /* the urb is in unlink state */ 776 case -ETIME: 777 pr_warn_ratelimited("may be reset is needed?..\n"); 778 goto goon; 779 default: 780 pr_warn_ratelimited("Rx status %d\n", status); 781 goto goon; 782 } 783 784 /* protect against short packets (tell me why we got some?!?) */ 785 if (urb->actual_length < sizeof(*rx_desc)) 786 goto goon; 787 788 789 rx_desc = (struct rx_desc *)urb->transfer_buffer; 790 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; 791 if (urb->actual_length < sizeof(struct rx_desc) + pkt_len) 792 goto goon; 793 794 skb = netdev_alloc_skb_ip_align(netdev, pkt_len); 795 if (!skb) 796 goto goon; 797 798 memcpy(skb->data, tp->rx_skb->data + sizeof(struct rx_desc), pkt_len); 799 skb_put(skb, pkt_len); 800 skb->protocol = eth_type_trans(skb, netdev); 801 netif_rx(skb); 802 stats->rx_packets++; 803 stats->rx_bytes += pkt_len; 804 goon: 805 usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), 806 tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc), 807 (usb_complete_t)read_bulk_callback, tp); 808 result = usb_submit_urb(tp->rx_urb, GFP_ATOMIC); 809 if (result == -ENODEV) { 810 netif_device_detach(tp->netdev); 811 } else if (result) { 812 set_bit(RX_URB_FAIL, &tp->flags); 813 goto resched; 814 } else { 815 clear_bit(RX_URB_FAIL, &tp->flags); 816 } 817 818 return; 819 resched: 820 tasklet_schedule(&tp->tl); 821 } 822 823 static void rx_fixup(unsigned long data) 824 { 825 struct r8152 *tp; 826 int status; 827 828 tp = (struct r8152 *)data; 829 if (!test_bit(WORK_ENABLE, &tp->flags)) 830 return; 831 832 status = usb_submit_urb(tp->rx_urb, GFP_ATOMIC); 833 if (status == -ENODEV) { 834 netif_device_detach(tp->netdev); 835 } else if (status) { 836 set_bit(RX_URB_FAIL, &tp->flags); 837 goto tlsched; 838 } else { 839 clear_bit(RX_URB_FAIL, &tp->flags); 840 } 841 842 return; 843 tlsched: 844 tasklet_schedule(&tp->tl); 845 } 846 847 static void write_bulk_callback(struct urb *urb) 848 { 849 struct r8152 *tp; 850 int status = urb->status; 851 852 tp = urb->context; 853 if (!tp) 854 return; 855 dev_kfree_skb_irq(tp->tx_skb); 856 if (!netif_device_present(tp->netdev)) 857 return; 858 if (status) 859 dev_info(&urb->dev->dev, "%s: Tx status %d\n", 860 tp->netdev->name, status); 861 tp->netdev->trans_start = jiffies; 862 netif_wake_queue(tp->netdev); 863 } 864 865 static void rtl8152_tx_timeout(struct net_device *netdev) 866 { 867 struct r8152 *tp = netdev_priv(netdev); 868 struct net_device_stats *stats = rtl8152_get_stats(netdev); 869 netif_warn(tp, tx_err, netdev, "Tx timeout.\n"); 870 usb_unlink_urb(tp->tx_urb); 871 stats->tx_errors++; 872 } 873 874 static void rtl8152_set_rx_mode(struct net_device *netdev) 875 { 876 struct r8152 *tp = netdev_priv(netdev); 877 878 if (tp->speed & LINK_STATUS) 879 set_bit(RTL8152_SET_RX_MODE, &tp->flags); 880 } 881 882 static void _rtl8152_set_rx_mode(struct net_device *netdev) 883 { 884 struct r8152 *tp = netdev_priv(netdev); 885 u32 tmp, *mc_filter; /* Multicast hash filter */ 886 u32 ocp_data; 887 888 mc_filter = kmalloc(sizeof(u32) * 2, GFP_KERNEL); 889 if (!mc_filter) { 890 netif_err(tp, link, netdev, "out of memory"); 891 return; 892 } 893 894 clear_bit(RTL8152_SET_RX_MODE, &tp->flags); 895 netif_stop_queue(netdev); 896 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 897 ocp_data &= ~RCR_ACPT_ALL; 898 ocp_data |= RCR_AB | RCR_APM; 899 900 if (netdev->flags & IFF_PROMISC) { 901 /* Unconditionally log net taps. */ 902 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); 903 ocp_data |= RCR_AM | RCR_AAP; 904 mc_filter[1] = mc_filter[0] = 0xffffffff; 905 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || 906 (netdev->flags & IFF_ALLMULTI)) { 907 /* Too many to filter perfectly -- accept all multicasts. */ 908 ocp_data |= RCR_AM; 909 mc_filter[1] = mc_filter[0] = 0xffffffff; 910 } else { 911 struct netdev_hw_addr *ha; 912 913 mc_filter[1] = mc_filter[0] = 0; 914 netdev_for_each_mc_addr(ha, netdev) { 915 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; 916 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); 917 ocp_data |= RCR_AM; 918 } 919 } 920 921 tmp = mc_filter[0]; 922 mc_filter[0] = __cpu_to_le32(swab32(mc_filter[1])); 923 mc_filter[1] = __cpu_to_le32(swab32(tmp)); 924 925 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(u32) * 2, mc_filter); 926 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 927 netif_wake_queue(netdev); 928 kfree(mc_filter); 929 } 930 931 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, 932 struct net_device *netdev) 933 { 934 struct r8152 *tp = netdev_priv(netdev); 935 struct net_device_stats *stats = rtl8152_get_stats(netdev); 936 struct tx_desc *tx_desc; 937 int len, res; 938 939 netif_stop_queue(netdev); 940 len = skb->len; 941 if (skb_header_cloned(skb) || skb_headroom(skb) < sizeof(*tx_desc)) { 942 struct sk_buff *tx_skb; 943 944 tx_skb = skb_copy_expand(skb, sizeof(*tx_desc), 0, GFP_ATOMIC); 945 dev_kfree_skb_any(skb); 946 if (!tx_skb) { 947 stats->tx_dropped++; 948 netif_wake_queue(netdev); 949 return NETDEV_TX_OK; 950 } 951 skb = tx_skb; 952 } 953 tx_desc = (struct tx_desc *)skb_push(skb, sizeof(*tx_desc)); 954 memset(tx_desc, 0, sizeof(*tx_desc)); 955 tx_desc->opts1 = cpu_to_le32((len & TX_LEN_MASK) | TX_FS | TX_LS); 956 tp->tx_skb = skb; 957 skb_tx_timestamp(skb); 958 usb_fill_bulk_urb(tp->tx_urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), 959 skb->data, skb->len, 960 (usb_complete_t)write_bulk_callback, tp); 961 res = usb_submit_urb(tp->tx_urb, GFP_ATOMIC); 962 if (res) { 963 /* Can we get/handle EPIPE here? */ 964 if (res == -ENODEV) { 965 netif_device_detach(tp->netdev); 966 } else { 967 netif_warn(tp, tx_err, netdev, 968 "failed tx_urb %d\n", res); 969 stats->tx_errors++; 970 netif_start_queue(netdev); 971 } 972 } else { 973 stats->tx_packets++; 974 stats->tx_bytes += skb->len; 975 } 976 977 return NETDEV_TX_OK; 978 } 979 980 static void r8152b_reset_packet_filter(struct r8152 *tp) 981 { 982 u32 ocp_data; 983 984 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); 985 ocp_data &= ~FMC_FCR_MCU_EN; 986 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 987 ocp_data |= FMC_FCR_MCU_EN; 988 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); 989 } 990 991 static void rtl8152_nic_reset(struct r8152 *tp) 992 { 993 int i; 994 995 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); 996 997 for (i = 0; i < 1000; i++) { 998 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) 999 break; 1000 udelay(100); 1001 } 1002 } 1003 1004 static inline u8 rtl8152_get_speed(struct r8152 *tp) 1005 { 1006 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); 1007 } 1008 1009 static int rtl8152_enable(struct r8152 *tp) 1010 { 1011 u32 ocp_data; 1012 u8 speed; 1013 1014 speed = rtl8152_get_speed(tp); 1015 if (speed & _100bps) { 1016 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); 1017 ocp_data &= ~EEEP_CR_EEEP_TX; 1018 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); 1019 } else { 1020 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); 1021 ocp_data |= EEEP_CR_EEEP_TX; 1022 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); 1023 } 1024 1025 r8152b_reset_packet_filter(tp); 1026 1027 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); 1028 ocp_data |= CR_RE | CR_TE; 1029 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); 1030 1031 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); 1032 ocp_data &= ~RXDY_GATED_EN; 1033 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); 1034 1035 usb_fill_bulk_urb(tp->rx_urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), 1036 tp->rx_skb->data, RTL8152_RMS + sizeof(struct rx_desc), 1037 (usb_complete_t)read_bulk_callback, tp); 1038 1039 return usb_submit_urb(tp->rx_urb, GFP_KERNEL); 1040 } 1041 1042 static void rtl8152_disable(struct r8152 *tp) 1043 { 1044 u32 ocp_data; 1045 int i; 1046 1047 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 1048 ocp_data &= ~RCR_ACPT_ALL; 1049 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 1050 1051 usb_kill_urb(tp->tx_urb); 1052 1053 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); 1054 ocp_data |= RXDY_GATED_EN; 1055 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); 1056 1057 for (i = 0; i < 1000; i++) { 1058 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1059 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) 1060 break; 1061 mdelay(1); 1062 } 1063 1064 for (i = 0; i < 1000; i++) { 1065 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) 1066 break; 1067 mdelay(1); 1068 } 1069 1070 usb_kill_urb(tp->rx_urb); 1071 1072 rtl8152_nic_reset(tp); 1073 } 1074 1075 static void r8152b_exit_oob(struct r8152 *tp) 1076 { 1077 u32 ocp_data; 1078 int i; 1079 1080 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 1081 ocp_data &= ~RCR_ACPT_ALL; 1082 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 1083 1084 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); 1085 ocp_data |= RXDY_GATED_EN; 1086 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); 1087 1088 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); 1089 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); 1090 1091 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1092 ocp_data &= ~NOW_IS_OOB; 1093 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 1094 1095 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 1096 ocp_data &= ~MCU_BORW_EN; 1097 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 1098 1099 for (i = 0; i < 1000; i++) { 1100 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1101 if (ocp_data & LINK_LIST_READY) 1102 break; 1103 mdelay(1); 1104 } 1105 1106 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 1107 ocp_data |= RE_INIT_LL; 1108 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 1109 1110 for (i = 0; i < 1000; i++) { 1111 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1112 if (ocp_data & LINK_LIST_READY) 1113 break; 1114 mdelay(1); 1115 } 1116 1117 rtl8152_nic_reset(tp); 1118 1119 /* rx share fifo credit full threshold */ 1120 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); 1121 1122 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT); 1123 ocp_data &= STAT_SPEED_MASK; 1124 if (ocp_data == STAT_SPEED_FULL) { 1125 /* rx share fifo credit near full threshold */ 1126 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 1127 RXFIFO_THR2_FULL); 1128 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 1129 RXFIFO_THR3_FULL); 1130 } else { 1131 /* rx share fifo credit near full threshold */ 1132 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, 1133 RXFIFO_THR2_HIGH); 1134 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, 1135 RXFIFO_THR3_HIGH); 1136 } 1137 1138 /* TX share fifo free credit full threshold */ 1139 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); 1140 1141 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); 1142 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR); 1143 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, 1144 TEST_MODE_DISABLE | TX_SIZE_ADJUST1); 1145 1146 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); 1147 ocp_data &= ~CPCR_RX_VLAN; 1148 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); 1149 1150 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 1151 1152 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); 1153 ocp_data |= TCR0_AUTO_FIFO; 1154 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); 1155 } 1156 1157 static void r8152b_enter_oob(struct r8152 *tp) 1158 { 1159 u32 ocp_data; 1160 int i; 1161 1162 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1163 ocp_data &= ~NOW_IS_OOB; 1164 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 1165 1166 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); 1167 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); 1168 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); 1169 1170 rtl8152_disable(tp); 1171 1172 for (i = 0; i < 1000; i++) { 1173 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1174 if (ocp_data & LINK_LIST_READY) 1175 break; 1176 mdelay(1); 1177 } 1178 1179 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); 1180 ocp_data |= RE_INIT_LL; 1181 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); 1182 1183 for (i = 0; i < 1000; i++) { 1184 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1185 if (ocp_data & LINK_LIST_READY) 1186 break; 1187 mdelay(1); 1188 } 1189 1190 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); 1191 1192 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); 1193 ocp_data |= MAGIC_EN; 1194 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); 1195 1196 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); 1197 ocp_data |= CPCR_RX_VLAN; 1198 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); 1199 1200 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR); 1201 ocp_data |= ALDPS_PROXY_MODE; 1202 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data); 1203 1204 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); 1205 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; 1206 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); 1207 1208 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN); 1209 1210 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); 1211 ocp_data &= ~RXDY_GATED_EN; 1212 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); 1213 1214 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); 1215 ocp_data |= RCR_APM | RCR_AM | RCR_AB; 1216 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); 1217 } 1218 1219 static void r8152b_disable_aldps(struct r8152 *tp) 1220 { 1221 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE); 1222 msleep(20); 1223 } 1224 1225 static inline void r8152b_enable_aldps(struct r8152 *tp) 1226 { 1227 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | 1228 LINKENA | DIS_SDSAVE); 1229 } 1230 1231 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex) 1232 { 1233 u16 bmcr, anar; 1234 int ret = 0; 1235 1236 cancel_delayed_work_sync(&tp->schedule); 1237 anar = r8152_mdio_read(tp, MII_ADVERTISE); 1238 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL | 1239 ADVERTISE_100HALF | ADVERTISE_100FULL); 1240 1241 if (autoneg == AUTONEG_DISABLE) { 1242 if (speed == SPEED_10) { 1243 bmcr = 0; 1244 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; 1245 } else if (speed == SPEED_100) { 1246 bmcr = BMCR_SPEED100; 1247 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; 1248 } else { 1249 ret = -EINVAL; 1250 goto out; 1251 } 1252 1253 if (duplex == DUPLEX_FULL) 1254 bmcr |= BMCR_FULLDPLX; 1255 } else { 1256 if (speed == SPEED_10) { 1257 if (duplex == DUPLEX_FULL) 1258 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; 1259 else 1260 anar |= ADVERTISE_10HALF; 1261 } else if (speed == SPEED_100) { 1262 if (duplex == DUPLEX_FULL) { 1263 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; 1264 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; 1265 } else { 1266 anar |= ADVERTISE_10HALF; 1267 anar |= ADVERTISE_100HALF; 1268 } 1269 } else { 1270 ret = -EINVAL; 1271 goto out; 1272 } 1273 1274 bmcr = BMCR_ANENABLE | BMCR_ANRESTART; 1275 } 1276 1277 r8152_mdio_write(tp, MII_ADVERTISE, anar); 1278 r8152_mdio_write(tp, MII_BMCR, bmcr); 1279 1280 out: 1281 schedule_delayed_work(&tp->schedule, 5 * HZ); 1282 1283 return ret; 1284 } 1285 1286 static void rtl8152_down(struct r8152 *tp) 1287 { 1288 u32 ocp_data; 1289 1290 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); 1291 ocp_data &= ~POWER_CUT; 1292 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); 1293 1294 r8152b_disable_aldps(tp); 1295 r8152b_enter_oob(tp); 1296 r8152b_enable_aldps(tp); 1297 } 1298 1299 static void set_carrier(struct r8152 *tp) 1300 { 1301 struct net_device *netdev = tp->netdev; 1302 u8 speed; 1303 1304 speed = rtl8152_get_speed(tp); 1305 1306 if (speed & LINK_STATUS) { 1307 if (!(tp->speed & LINK_STATUS)) { 1308 rtl8152_enable(tp); 1309 set_bit(RTL8152_SET_RX_MODE, &tp->flags); 1310 netif_carrier_on(netdev); 1311 } 1312 } else { 1313 if (tp->speed & LINK_STATUS) { 1314 netif_carrier_off(netdev); 1315 rtl8152_disable(tp); 1316 } 1317 } 1318 tp->speed = speed; 1319 } 1320 1321 static void rtl_work_func_t(struct work_struct *work) 1322 { 1323 struct r8152 *tp = container_of(work, struct r8152, schedule.work); 1324 1325 if (!test_bit(WORK_ENABLE, &tp->flags)) 1326 goto out1; 1327 1328 if (test_bit(RTL8152_UNPLUG, &tp->flags)) 1329 goto out1; 1330 1331 set_carrier(tp); 1332 1333 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags)) 1334 _rtl8152_set_rx_mode(tp->netdev); 1335 1336 schedule_delayed_work(&tp->schedule, HZ); 1337 1338 out1: 1339 return; 1340 } 1341 1342 static int rtl8152_open(struct net_device *netdev) 1343 { 1344 struct r8152 *tp = netdev_priv(netdev); 1345 int res = 0; 1346 1347 tp->speed = rtl8152_get_speed(tp); 1348 if (tp->speed & LINK_STATUS) { 1349 res = rtl8152_enable(tp); 1350 if (res) { 1351 if (res == -ENODEV) 1352 netif_device_detach(tp->netdev); 1353 1354 netif_err(tp, ifup, netdev, 1355 "rtl8152_open failed: %d\n", res); 1356 return res; 1357 } 1358 1359 netif_carrier_on(netdev); 1360 } else { 1361 netif_stop_queue(netdev); 1362 netif_carrier_off(netdev); 1363 } 1364 1365 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL); 1366 netif_start_queue(netdev); 1367 set_bit(WORK_ENABLE, &tp->flags); 1368 schedule_delayed_work(&tp->schedule, 0); 1369 1370 return res; 1371 } 1372 1373 static int rtl8152_close(struct net_device *netdev) 1374 { 1375 struct r8152 *tp = netdev_priv(netdev); 1376 int res = 0; 1377 1378 clear_bit(WORK_ENABLE, &tp->flags); 1379 cancel_delayed_work_sync(&tp->schedule); 1380 netif_stop_queue(netdev); 1381 rtl8152_disable(tp); 1382 1383 return res; 1384 } 1385 1386 static void rtl_clear_bp(struct r8152 *tp) 1387 { 1388 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0); 1389 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0); 1390 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0); 1391 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0); 1392 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0); 1393 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0); 1394 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0); 1395 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0); 1396 mdelay(3); 1397 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0); 1398 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0); 1399 } 1400 1401 static void r8152b_enable_eee(struct r8152 *tp) 1402 { 1403 u32 ocp_data; 1404 1405 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); 1406 ocp_data |= EEE_RX_EN | EEE_TX_EN; 1407 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); 1408 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN | 1409 EEE_10_CAP | EEE_NWAY_EN | 1410 TX_QUIET_EN | RX_QUIET_EN | 1411 SDRISETIME | RG_RXLPI_MSK_HFDUP | 1412 SDFALLTIME); 1413 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN | 1414 RG_LDVQUIET_EN | RG_CKRSEL | 1415 RG_EEEPRG_EN); 1416 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH); 1417 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR); 1418 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR); 1419 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR); 1420 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA); 1421 ocp_reg_write(tp, OCP_EEE_AR, 0x0000); 1422 } 1423 1424 static void r8152b_enable_fc(struct r8152 *tp) 1425 { 1426 u16 anar; 1427 1428 anar = r8152_mdio_read(tp, MII_ADVERTISE); 1429 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; 1430 r8152_mdio_write(tp, MII_ADVERTISE, anar); 1431 } 1432 1433 static void r8152b_hw_phy_cfg(struct r8152 *tp) 1434 { 1435 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE); 1436 r8152b_disable_aldps(tp); 1437 } 1438 1439 static void r8152b_init(struct r8152 *tp) 1440 { 1441 u32 ocp_data; 1442 int i; 1443 1444 rtl_clear_bp(tp); 1445 1446 if (tp->version == RTL_VER_01) { 1447 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); 1448 ocp_data &= ~LED_MODE_MASK; 1449 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); 1450 } 1451 1452 r8152b_hw_phy_cfg(tp); 1453 1454 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); 1455 ocp_data &= ~POWER_CUT; 1456 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); 1457 1458 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); 1459 ocp_data &= ~RWSUME_INDICATE; 1460 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); 1461 1462 r8152b_exit_oob(tp); 1463 1464 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); 1465 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; 1466 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); 1467 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); 1468 ocp_data &= ~MCU_CLK_RATIO_MASK; 1469 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; 1470 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); 1471 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | 1472 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; 1473 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); 1474 1475 r8152b_enable_eee(tp); 1476 r8152b_enable_aldps(tp); 1477 r8152b_enable_fc(tp); 1478 1479 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE | 1480 BMCR_ANRESTART); 1481 for (i = 0; i < 100; i++) { 1482 udelay(100); 1483 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET)) 1484 break; 1485 } 1486 1487 /* disable rx aggregation */ 1488 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); 1489 ocp_data |= RX_AGG_DISABLE; 1490 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); 1491 } 1492 1493 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) 1494 { 1495 struct r8152 *tp = usb_get_intfdata(intf); 1496 1497 netif_device_detach(tp->netdev); 1498 1499 if (netif_running(tp->netdev)) { 1500 clear_bit(WORK_ENABLE, &tp->flags); 1501 cancel_delayed_work_sync(&tp->schedule); 1502 } 1503 1504 rtl8152_down(tp); 1505 1506 return 0; 1507 } 1508 1509 static int rtl8152_resume(struct usb_interface *intf) 1510 { 1511 struct r8152 *tp = usb_get_intfdata(intf); 1512 1513 r8152b_init(tp); 1514 netif_device_attach(tp->netdev); 1515 if (netif_running(tp->netdev)) { 1516 rtl8152_enable(tp); 1517 set_bit(WORK_ENABLE, &tp->flags); 1518 set_bit(RTL8152_SET_RX_MODE, &tp->flags); 1519 schedule_delayed_work(&tp->schedule, 0); 1520 } 1521 1522 return 0; 1523 } 1524 1525 static void rtl8152_get_drvinfo(struct net_device *netdev, 1526 struct ethtool_drvinfo *info) 1527 { 1528 struct r8152 *tp = netdev_priv(netdev); 1529 1530 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN); 1531 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN); 1532 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); 1533 } 1534 1535 static 1536 int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd) 1537 { 1538 struct r8152 *tp = netdev_priv(netdev); 1539 1540 if (!tp->mii.mdio_read) 1541 return -EOPNOTSUPP; 1542 1543 return mii_ethtool_gset(&tp->mii, cmd); 1544 } 1545 1546 static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 1547 { 1548 struct r8152 *tp = netdev_priv(dev); 1549 1550 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex); 1551 } 1552 1553 static struct ethtool_ops ops = { 1554 .get_drvinfo = rtl8152_get_drvinfo, 1555 .get_settings = rtl8152_get_settings, 1556 .set_settings = rtl8152_set_settings, 1557 .get_link = ethtool_op_get_link, 1558 }; 1559 1560 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) 1561 { 1562 struct r8152 *tp = netdev_priv(netdev); 1563 struct mii_ioctl_data *data = if_mii(rq); 1564 int res = 0; 1565 1566 switch (cmd) { 1567 case SIOCGMIIPHY: 1568 data->phy_id = R8152_PHY_ID; /* Internal PHY */ 1569 break; 1570 1571 case SIOCGMIIREG: 1572 data->val_out = r8152_mdio_read(tp, data->reg_num); 1573 break; 1574 1575 case SIOCSMIIREG: 1576 if (!capable(CAP_NET_ADMIN)) { 1577 res = -EPERM; 1578 break; 1579 } 1580 r8152_mdio_write(tp, data->reg_num, data->val_in); 1581 break; 1582 1583 default: 1584 res = -EOPNOTSUPP; 1585 } 1586 1587 return res; 1588 } 1589 1590 static const struct net_device_ops rtl8152_netdev_ops = { 1591 .ndo_open = rtl8152_open, 1592 .ndo_stop = rtl8152_close, 1593 .ndo_do_ioctl = rtl8152_ioctl, 1594 .ndo_start_xmit = rtl8152_start_xmit, 1595 .ndo_tx_timeout = rtl8152_tx_timeout, 1596 .ndo_set_rx_mode = rtl8152_set_rx_mode, 1597 .ndo_set_mac_address = rtl8152_set_mac_address, 1598 1599 .ndo_change_mtu = eth_change_mtu, 1600 .ndo_validate_addr = eth_validate_addr, 1601 }; 1602 1603 static void r8152b_get_version(struct r8152 *tp) 1604 { 1605 u32 ocp_data; 1606 u16 version; 1607 1608 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1); 1609 version = (u16)(ocp_data & VERSION_MASK); 1610 1611 switch (version) { 1612 case 0x4c00: 1613 tp->version = RTL_VER_01; 1614 break; 1615 case 0x4c10: 1616 tp->version = RTL_VER_02; 1617 break; 1618 default: 1619 netif_info(tp, probe, tp->netdev, 1620 "Unknown version 0x%04x\n", version); 1621 break; 1622 } 1623 } 1624 1625 static int rtl8152_probe(struct usb_interface *intf, 1626 const struct usb_device_id *id) 1627 { 1628 struct usb_device *udev = interface_to_usbdev(intf); 1629 struct r8152 *tp; 1630 struct net_device *netdev; 1631 1632 if (udev->actconfig->desc.bConfigurationValue != 1) { 1633 usb_driver_set_configuration(udev, 1); 1634 return -ENODEV; 1635 } 1636 1637 netdev = alloc_etherdev(sizeof(struct r8152)); 1638 if (!netdev) { 1639 dev_err(&intf->dev, "Out of memory"); 1640 return -ENOMEM; 1641 } 1642 1643 tp = netdev_priv(netdev); 1644 tp->msg_enable = 0x7FFF; 1645 1646 tasklet_init(&tp->tl, rx_fixup, (unsigned long)tp); 1647 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); 1648 1649 tp->udev = udev; 1650 tp->netdev = netdev; 1651 netdev->netdev_ops = &rtl8152_netdev_ops; 1652 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; 1653 netdev->features &= ~NETIF_F_IP_CSUM; 1654 SET_ETHTOOL_OPS(netdev, &ops); 1655 tp->speed = 0; 1656 1657 tp->mii.dev = netdev; 1658 tp->mii.mdio_read = read_mii_word; 1659 tp->mii.mdio_write = write_mii_word; 1660 tp->mii.phy_id_mask = 0x3f; 1661 tp->mii.reg_num_mask = 0x1f; 1662 tp->mii.phy_id = R8152_PHY_ID; 1663 tp->mii.supports_gmii = 0; 1664 1665 r8152b_get_version(tp); 1666 r8152b_init(tp); 1667 set_ethernet_addr(tp); 1668 1669 if (!alloc_all_urbs(tp)) { 1670 netif_err(tp, probe, netdev, "out of memory"); 1671 goto out; 1672 } 1673 1674 tp->rx_skb = netdev_alloc_skb(netdev, 1675 RTL8152_RMS + sizeof(struct rx_desc)); 1676 if (!tp->rx_skb) 1677 goto out1; 1678 1679 usb_set_intfdata(intf, tp); 1680 SET_NETDEV_DEV(netdev, &intf->dev); 1681 1682 1683 if (register_netdev(netdev) != 0) { 1684 netif_err(tp, probe, netdev, "couldn't register the device"); 1685 goto out2; 1686 } 1687 1688 netif_info(tp, probe, netdev, "%s", DRIVER_VERSION); 1689 1690 return 0; 1691 1692 out2: 1693 usb_set_intfdata(intf, NULL); 1694 dev_kfree_skb(tp->rx_skb); 1695 out1: 1696 free_all_urbs(tp); 1697 out: 1698 free_netdev(netdev); 1699 return -EIO; 1700 } 1701 1702 static void rtl8152_unload(struct r8152 *tp) 1703 { 1704 u32 ocp_data; 1705 1706 if (tp->version != RTL_VER_01) { 1707 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); 1708 ocp_data |= POWER_CUT; 1709 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); 1710 } 1711 1712 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); 1713 ocp_data &= ~RWSUME_INDICATE; 1714 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); 1715 } 1716 1717 static void rtl8152_disconnect(struct usb_interface *intf) 1718 { 1719 struct r8152 *tp = usb_get_intfdata(intf); 1720 1721 usb_set_intfdata(intf, NULL); 1722 if (tp) { 1723 set_bit(RTL8152_UNPLUG, &tp->flags); 1724 tasklet_kill(&tp->tl); 1725 unregister_netdev(tp->netdev); 1726 rtl8152_unload(tp); 1727 free_all_urbs(tp); 1728 if (tp->rx_skb) 1729 dev_kfree_skb(tp->rx_skb); 1730 free_netdev(tp->netdev); 1731 } 1732 } 1733 1734 /* table of devices that work with this driver */ 1735 static struct usb_device_id rtl8152_table[] = { 1736 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)}, 1737 {} 1738 }; 1739 1740 MODULE_DEVICE_TABLE(usb, rtl8152_table); 1741 1742 static struct usb_driver rtl8152_driver = { 1743 .name = MODULENAME, 1744 .probe = rtl8152_probe, 1745 .disconnect = rtl8152_disconnect, 1746 .id_table = rtl8152_table, 1747 .suspend = rtl8152_suspend, 1748 .resume = rtl8152_resume 1749 }; 1750 1751 module_usb_driver(rtl8152_driver); 1752 1753 MODULE_AUTHOR(DRIVER_AUTHOR); 1754 MODULE_DESCRIPTION(DRIVER_DESC); 1755 MODULE_LICENSE("GPL"); 1756