1 /* CoreChip-sz SR9800 one chip USB 2.0 Ethernet Devices 2 * 3 * Author : Liu Junliang <liujunliang_ljl@163.com> 4 * 5 * Based on asix_common.c, asix_devices.c 6 * 7 * This file is licensed under the terms of the GNU General Public License 8 * version 2. This program is licensed "as is" without any warranty of any 9 * kind, whether express or implied.* 10 */ 11 12 #include <linux/module.h> 13 #include <linux/kmod.h> 14 #include <linux/init.h> 15 #include <linux/netdevice.h> 16 #include <linux/etherdevice.h> 17 #include <linux/ethtool.h> 18 #include <linux/workqueue.h> 19 #include <linux/mii.h> 20 #include <linux/usb.h> 21 #include <linux/crc32.h> 22 #include <linux/usb/usbnet.h> 23 #include <linux/slab.h> 24 #include <linux/if_vlan.h> 25 26 #include "sr9800.h" 27 28 static int sr_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 29 u16 size, void *data) 30 { 31 int err; 32 33 err = usbnet_read_cmd(dev, cmd, SR_REQ_RD_REG, value, index, 34 data, size); 35 if ((err != size) && (err >= 0)) 36 err = -EINVAL; 37 38 return err; 39 } 40 41 static int sr_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 42 u16 size, void *data) 43 { 44 int err; 45 46 err = usbnet_write_cmd(dev, cmd, SR_REQ_WR_REG, value, index, 47 data, size); 48 if ((err != size) && (err >= 0)) 49 err = -EINVAL; 50 51 return err; 52 } 53 54 static void 55 sr_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index, 56 u16 size, void *data) 57 { 58 usbnet_write_cmd_async(dev, cmd, SR_REQ_WR_REG, value, index, data, 59 size); 60 } 61 62 static int sr_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 63 { 64 int offset = 0; 65 66 /* This check is no longer done by usbnet */ 67 if (skb->len < dev->net->hard_header_len) 68 return 0; 69 70 while (offset + sizeof(u32) < skb->len) { 71 struct sk_buff *sr_skb; 72 u16 size; 73 u32 header = get_unaligned_le32(skb->data + offset); 74 75 offset += sizeof(u32); 76 /* get the packet length */ 77 size = (u16) (header & 0x7ff); 78 if (size != ((~header >> 16) & 0x07ff)) { 79 netdev_err(dev->net, "%s : Bad Header Length\n", 80 __func__); 81 return 0; 82 } 83 84 if ((size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) || 85 (size + offset > skb->len)) { 86 netdev_err(dev->net, "%s : Bad RX Length %d\n", 87 __func__, size); 88 return 0; 89 } 90 sr_skb = netdev_alloc_skb_ip_align(dev->net, size); 91 if (!sr_skb) 92 return 0; 93 94 skb_put(sr_skb, size); 95 memcpy(sr_skb->data, skb->data + offset, size); 96 usbnet_skb_return(dev, sr_skb); 97 98 offset += (size + 1) & 0xfffe; 99 } 100 101 if (skb->len != offset) { 102 netdev_err(dev->net, "%s : Bad SKB Length %d\n", __func__, 103 skb->len); 104 return 0; 105 } 106 107 return 1; 108 } 109 110 static struct sk_buff *sr_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 111 gfp_t flags) 112 { 113 int headroom = skb_headroom(skb); 114 int tailroom = skb_tailroom(skb); 115 u32 padbytes = 0xffff0000; 116 u32 packet_len; 117 int padlen; 118 void *ptr; 119 120 padlen = ((skb->len + 4) % (dev->maxpacket - 1)) ? 0 : 4; 121 122 if ((!skb_cloned(skb)) && ((headroom + tailroom) >= (4 + padlen))) { 123 if ((headroom < 4) || (tailroom < padlen)) { 124 skb->data = memmove(skb->head + 4, skb->data, 125 skb->len); 126 skb_set_tail_pointer(skb, skb->len); 127 } 128 } else { 129 struct sk_buff *skb2; 130 skb2 = skb_copy_expand(skb, 4, padlen, flags); 131 dev_kfree_skb_any(skb); 132 skb = skb2; 133 if (!skb) 134 return NULL; 135 } 136 137 ptr = skb_push(skb, 4); 138 packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4); 139 put_unaligned_le32(packet_len, ptr); 140 141 if (padlen) { 142 put_unaligned_le32(padbytes, skb_tail_pointer(skb)); 143 skb_put(skb, sizeof(padbytes)); 144 } 145 146 usbnet_set_skb_tx_stats(skb, 1, 0); 147 return skb; 148 } 149 150 static void sr_status(struct usbnet *dev, struct urb *urb) 151 { 152 struct sr9800_int_data *event; 153 int link; 154 155 if (urb->actual_length < 8) 156 return; 157 158 event = urb->transfer_buffer; 159 link = event->link & 0x01; 160 if (netif_carrier_ok(dev->net) != link) { 161 usbnet_link_change(dev, link, 1); 162 netdev_dbg(dev->net, "Link Status is: %d\n", link); 163 } 164 165 return; 166 } 167 168 static inline int sr_set_sw_mii(struct usbnet *dev) 169 { 170 int ret; 171 172 ret = sr_write_cmd(dev, SR_CMD_SET_SW_MII, 0x0000, 0, 0, NULL); 173 if (ret < 0) 174 netdev_err(dev->net, "Failed to enable software MII access\n"); 175 return ret; 176 } 177 178 static inline int sr_set_hw_mii(struct usbnet *dev) 179 { 180 int ret; 181 182 ret = sr_write_cmd(dev, SR_CMD_SET_HW_MII, 0x0000, 0, 0, NULL); 183 if (ret < 0) 184 netdev_err(dev->net, "Failed to enable hardware MII access\n"); 185 return ret; 186 } 187 188 static inline int sr_get_phy_addr(struct usbnet *dev) 189 { 190 u8 buf[2]; 191 int ret; 192 193 ret = sr_read_cmd(dev, SR_CMD_READ_PHY_ID, 0, 0, 2, buf); 194 if (ret < 0) { 195 netdev_err(dev->net, "%s : Error reading PHYID register:%02x\n", 196 __func__, ret); 197 goto out; 198 } 199 netdev_dbg(dev->net, "%s : returning 0x%04x\n", __func__, 200 *((__le16 *)buf)); 201 202 ret = buf[1]; 203 204 out: 205 return ret; 206 } 207 208 static int sr_sw_reset(struct usbnet *dev, u8 flags) 209 { 210 int ret; 211 212 ret = sr_write_cmd(dev, SR_CMD_SW_RESET, flags, 0, 0, NULL); 213 if (ret < 0) 214 netdev_err(dev->net, "Failed to send software reset:%02x\n", 215 ret); 216 217 return ret; 218 } 219 220 static u16 sr_read_rx_ctl(struct usbnet *dev) 221 { 222 __le16 v; 223 int ret; 224 225 ret = sr_read_cmd(dev, SR_CMD_READ_RX_CTL, 0, 0, 2, &v); 226 if (ret < 0) { 227 netdev_err(dev->net, "Error reading RX_CTL register:%02x\n", 228 ret); 229 goto out; 230 } 231 232 ret = le16_to_cpu(v); 233 out: 234 return ret; 235 } 236 237 static int sr_write_rx_ctl(struct usbnet *dev, u16 mode) 238 { 239 int ret; 240 241 netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode); 242 ret = sr_write_cmd(dev, SR_CMD_WRITE_RX_CTL, mode, 0, 0, NULL); 243 if (ret < 0) 244 netdev_err(dev->net, 245 "Failed to write RX_CTL mode to 0x%04x:%02x\n", 246 mode, ret); 247 248 return ret; 249 } 250 251 static u16 sr_read_medium_status(struct usbnet *dev) 252 { 253 __le16 v; 254 int ret; 255 256 ret = sr_read_cmd(dev, SR_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v); 257 if (ret < 0) { 258 netdev_err(dev->net, 259 "Error reading Medium Status register:%02x\n", ret); 260 return ret; /* TODO: callers not checking for error ret */ 261 } 262 263 return le16_to_cpu(v); 264 } 265 266 static int sr_write_medium_mode(struct usbnet *dev, u16 mode) 267 { 268 int ret; 269 270 netdev_dbg(dev->net, "%s : mode = 0x%04x\n", __func__, mode); 271 ret = sr_write_cmd(dev, SR_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL); 272 if (ret < 0) 273 netdev_err(dev->net, 274 "Failed to write Medium Mode mode to 0x%04x:%02x\n", 275 mode, ret); 276 return ret; 277 } 278 279 static int sr_write_gpio(struct usbnet *dev, u16 value, int sleep) 280 { 281 int ret; 282 283 netdev_dbg(dev->net, "%s : value = 0x%04x\n", __func__, value); 284 ret = sr_write_cmd(dev, SR_CMD_WRITE_GPIOS, value, 0, 0, NULL); 285 if (ret < 0) 286 netdev_err(dev->net, "Failed to write GPIO value 0x%04x:%02x\n", 287 value, ret); 288 if (sleep) 289 msleep(sleep); 290 291 return ret; 292 } 293 294 /* SR9800 have a 16-bit RX_CTL value */ 295 static void sr_set_multicast(struct net_device *net) 296 { 297 struct usbnet *dev = netdev_priv(net); 298 struct sr_data *data = (struct sr_data *)&dev->data; 299 u16 rx_ctl = SR_DEFAULT_RX_CTL; 300 301 if (net->flags & IFF_PROMISC) { 302 rx_ctl |= SR_RX_CTL_PRO; 303 } else if (net->flags & IFF_ALLMULTI || 304 netdev_mc_count(net) > SR_MAX_MCAST) { 305 rx_ctl |= SR_RX_CTL_AMALL; 306 } else if (netdev_mc_empty(net)) { 307 /* just broadcast and directed */ 308 } else { 309 /* We use the 20 byte dev->data 310 * for our 8 byte filter buffer 311 * to avoid allocating memory that 312 * is tricky to free later 313 */ 314 struct netdev_hw_addr *ha; 315 u32 crc_bits; 316 317 memset(data->multi_filter, 0, SR_MCAST_FILTER_SIZE); 318 319 /* Build the multicast hash filter. */ 320 netdev_for_each_mc_addr(ha, net) { 321 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26; 322 data->multi_filter[crc_bits >> 3] |= 323 1 << (crc_bits & 7); 324 } 325 326 sr_write_cmd_async(dev, SR_CMD_WRITE_MULTI_FILTER, 0, 0, 327 SR_MCAST_FILTER_SIZE, data->multi_filter); 328 329 rx_ctl |= SR_RX_CTL_AM; 330 } 331 332 sr_write_cmd_async(dev, SR_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL); 333 } 334 335 static int sr_mdio_read(struct net_device *net, int phy_id, int loc) 336 { 337 struct usbnet *dev = netdev_priv(net); 338 __le16 res = 0; 339 340 mutex_lock(&dev->phy_mutex); 341 sr_set_sw_mii(dev); 342 sr_read_cmd(dev, SR_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, &res); 343 sr_set_hw_mii(dev); 344 mutex_unlock(&dev->phy_mutex); 345 346 netdev_dbg(dev->net, 347 "%s : phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", __func__, 348 phy_id, loc, le16_to_cpu(res)); 349 350 return le16_to_cpu(res); 351 } 352 353 static void 354 sr_mdio_write(struct net_device *net, int phy_id, int loc, int val) 355 { 356 struct usbnet *dev = netdev_priv(net); 357 __le16 res = cpu_to_le16(val); 358 359 netdev_dbg(dev->net, 360 "%s : phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", __func__, 361 phy_id, loc, val); 362 mutex_lock(&dev->phy_mutex); 363 sr_set_sw_mii(dev); 364 sr_write_cmd(dev, SR_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res); 365 sr_set_hw_mii(dev); 366 mutex_unlock(&dev->phy_mutex); 367 } 368 369 /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */ 370 static u32 sr_get_phyid(struct usbnet *dev) 371 { 372 int phy_reg; 373 u32 phy_id; 374 int i; 375 376 /* Poll for the rare case the FW or phy isn't ready yet. */ 377 for (i = 0; i < 100; i++) { 378 phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1); 379 if (phy_reg != 0 && phy_reg != 0xFFFF) 380 break; 381 mdelay(1); 382 } 383 384 if (phy_reg <= 0 || phy_reg == 0xFFFF) 385 return 0; 386 387 phy_id = (phy_reg & 0xffff) << 16; 388 389 phy_reg = sr_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2); 390 if (phy_reg < 0) 391 return 0; 392 393 phy_id |= (phy_reg & 0xffff); 394 395 return phy_id; 396 } 397 398 static void 399 sr_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 400 { 401 struct usbnet *dev = netdev_priv(net); 402 u8 opt; 403 404 if (sr_read_cmd(dev, SR_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) { 405 wolinfo->supported = 0; 406 wolinfo->wolopts = 0; 407 return; 408 } 409 wolinfo->supported = WAKE_PHY | WAKE_MAGIC; 410 wolinfo->wolopts = 0; 411 if (opt & SR_MONITOR_LINK) 412 wolinfo->wolopts |= WAKE_PHY; 413 if (opt & SR_MONITOR_MAGIC) 414 wolinfo->wolopts |= WAKE_MAGIC; 415 } 416 417 static int 418 sr_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 419 { 420 struct usbnet *dev = netdev_priv(net); 421 u8 opt = 0; 422 423 if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC)) 424 return -EINVAL; 425 426 if (wolinfo->wolopts & WAKE_PHY) 427 opt |= SR_MONITOR_LINK; 428 if (wolinfo->wolopts & WAKE_MAGIC) 429 opt |= SR_MONITOR_MAGIC; 430 431 if (sr_write_cmd(dev, SR_CMD_WRITE_MONITOR_MODE, 432 opt, 0, 0, NULL) < 0) 433 return -EINVAL; 434 435 return 0; 436 } 437 438 static int sr_get_eeprom_len(struct net_device *net) 439 { 440 struct usbnet *dev = netdev_priv(net); 441 struct sr_data *data = (struct sr_data *)&dev->data; 442 443 return data->eeprom_len; 444 } 445 446 static int sr_get_eeprom(struct net_device *net, 447 struct ethtool_eeprom *eeprom, u8 *data) 448 { 449 struct usbnet *dev = netdev_priv(net); 450 __le16 *ebuf = (__le16 *)data; 451 int ret; 452 int i; 453 454 /* Crude hack to ensure that we don't overwrite memory 455 * if an odd length is supplied 456 */ 457 if (eeprom->len % 2) 458 return -EINVAL; 459 460 eeprom->magic = SR_EEPROM_MAGIC; 461 462 /* sr9800 returns 2 bytes from eeprom on read */ 463 for (i = 0; i < eeprom->len / 2; i++) { 464 ret = sr_read_cmd(dev, SR_CMD_READ_EEPROM, eeprom->offset + i, 465 0, 2, &ebuf[i]); 466 if (ret < 0) 467 return -EINVAL; 468 } 469 return 0; 470 } 471 472 static void sr_get_drvinfo(struct net_device *net, 473 struct ethtool_drvinfo *info) 474 { 475 /* Inherit standard device info */ 476 usbnet_get_drvinfo(net, info); 477 strscpy(info->driver, DRIVER_NAME, sizeof(info->driver)); 478 strscpy(info->version, DRIVER_VERSION, sizeof(info->version)); 479 } 480 481 static int sr_ioctl(struct net_device *net, struct ifreq *rq, int cmd) 482 { 483 struct usbnet *dev = netdev_priv(net); 484 485 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); 486 } 487 488 static int sr_set_mac_address(struct net_device *net, void *p) 489 { 490 struct usbnet *dev = netdev_priv(net); 491 struct sr_data *data = (struct sr_data *)&dev->data; 492 struct sockaddr *addr = p; 493 494 if (netif_running(net)) 495 return -EBUSY; 496 if (!is_valid_ether_addr(addr->sa_data)) 497 return -EADDRNOTAVAIL; 498 499 eth_hw_addr_set(net, addr->sa_data); 500 501 /* We use the 20 byte dev->data 502 * for our 6 byte mac buffer 503 * to avoid allocating memory that 504 * is tricky to free later 505 */ 506 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN); 507 sr_write_cmd_async(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, 508 data->mac_addr); 509 510 return 0; 511 } 512 513 static const struct ethtool_ops sr9800_ethtool_ops = { 514 .get_drvinfo = sr_get_drvinfo, 515 .get_link = usbnet_get_link, 516 .get_msglevel = usbnet_get_msglevel, 517 .set_msglevel = usbnet_set_msglevel, 518 .get_wol = sr_get_wol, 519 .set_wol = sr_set_wol, 520 .get_eeprom_len = sr_get_eeprom_len, 521 .get_eeprom = sr_get_eeprom, 522 .nway_reset = usbnet_nway_reset, 523 .get_link_ksettings = usbnet_get_link_ksettings_mii, 524 .set_link_ksettings = usbnet_set_link_ksettings_mii, 525 }; 526 527 static int sr9800_link_reset(struct usbnet *dev) 528 { 529 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET }; 530 u16 mode; 531 532 mii_check_media(&dev->mii, 1, 1); 533 mii_ethtool_gset(&dev->mii, &ecmd); 534 mode = SR9800_MEDIUM_DEFAULT; 535 536 if (ethtool_cmd_speed(&ecmd) != SPEED_100) 537 mode &= ~SR_MEDIUM_PS; 538 539 if (ecmd.duplex != DUPLEX_FULL) 540 mode &= ~SR_MEDIUM_FD; 541 542 netdev_dbg(dev->net, "%s : speed: %u duplex: %d mode: 0x%04x\n", 543 __func__, ethtool_cmd_speed(&ecmd), ecmd.duplex, mode); 544 545 sr_write_medium_mode(dev, mode); 546 547 return 0; 548 } 549 550 551 static int sr9800_set_default_mode(struct usbnet *dev) 552 { 553 u16 rx_ctl; 554 int ret; 555 556 sr_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); 557 sr_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, 558 ADVERTISE_ALL | ADVERTISE_CSMA); 559 mii_nway_restart(&dev->mii); 560 561 ret = sr_write_medium_mode(dev, SR9800_MEDIUM_DEFAULT); 562 if (ret < 0) 563 goto out; 564 565 ret = sr_write_cmd(dev, SR_CMD_WRITE_IPG012, 566 SR9800_IPG0_DEFAULT | SR9800_IPG1_DEFAULT, 567 SR9800_IPG2_DEFAULT, 0, NULL); 568 if (ret < 0) { 569 netdev_dbg(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret); 570 goto out; 571 } 572 573 /* Set RX_CTL to default values with 2k buffer, and enable cactus */ 574 ret = sr_write_rx_ctl(dev, SR_DEFAULT_RX_CTL); 575 if (ret < 0) 576 goto out; 577 578 rx_ctl = sr_read_rx_ctl(dev); 579 netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n", 580 rx_ctl); 581 582 rx_ctl = sr_read_medium_status(dev); 583 netdev_dbg(dev->net, "Medium Status:0x%04x after all initializations\n", 584 rx_ctl); 585 586 return 0; 587 out: 588 return ret; 589 } 590 591 static int sr9800_reset(struct usbnet *dev) 592 { 593 struct sr_data *data = (struct sr_data *)&dev->data; 594 int ret, embd_phy; 595 u16 rx_ctl; 596 597 ret = sr_write_gpio(dev, 598 SR_GPIO_RSE | SR_GPIO_GPO_2 | SR_GPIO_GPO2EN, 5); 599 if (ret < 0) 600 goto out; 601 602 embd_phy = ((sr_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0); 603 604 ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL); 605 if (ret < 0) { 606 netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret); 607 goto out; 608 } 609 610 ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_PRL); 611 if (ret < 0) 612 goto out; 613 614 msleep(150); 615 616 ret = sr_sw_reset(dev, SR_SWRESET_CLEAR); 617 if (ret < 0) 618 goto out; 619 620 msleep(150); 621 622 if (embd_phy) { 623 ret = sr_sw_reset(dev, SR_SWRESET_IPRL); 624 if (ret < 0) 625 goto out; 626 } else { 627 ret = sr_sw_reset(dev, SR_SWRESET_PRTE); 628 if (ret < 0) 629 goto out; 630 } 631 632 msleep(150); 633 rx_ctl = sr_read_rx_ctl(dev); 634 netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl); 635 ret = sr_write_rx_ctl(dev, 0x0000); 636 if (ret < 0) 637 goto out; 638 639 rx_ctl = sr_read_rx_ctl(dev); 640 netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl); 641 642 ret = sr_sw_reset(dev, SR_SWRESET_PRL); 643 if (ret < 0) 644 goto out; 645 646 msleep(150); 647 648 ret = sr_sw_reset(dev, SR_SWRESET_IPRL | SR_SWRESET_PRL); 649 if (ret < 0) 650 goto out; 651 652 msleep(150); 653 654 ret = sr9800_set_default_mode(dev); 655 if (ret < 0) 656 goto out; 657 658 /* Rewrite MAC address */ 659 memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN); 660 ret = sr_write_cmd(dev, SR_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, 661 data->mac_addr); 662 if (ret < 0) 663 goto out; 664 665 return 0; 666 667 out: 668 return ret; 669 } 670 671 static const struct net_device_ops sr9800_netdev_ops = { 672 .ndo_open = usbnet_open, 673 .ndo_stop = usbnet_stop, 674 .ndo_start_xmit = usbnet_start_xmit, 675 .ndo_tx_timeout = usbnet_tx_timeout, 676 .ndo_change_mtu = usbnet_change_mtu, 677 .ndo_get_stats64 = dev_get_tstats64, 678 .ndo_set_mac_address = sr_set_mac_address, 679 .ndo_validate_addr = eth_validate_addr, 680 .ndo_eth_ioctl = sr_ioctl, 681 .ndo_set_rx_mode = sr_set_multicast, 682 }; 683 684 static int sr9800_phy_powerup(struct usbnet *dev) 685 { 686 int ret; 687 688 /* set the embedded Ethernet PHY in power-down state */ 689 ret = sr_sw_reset(dev, SR_SWRESET_IPPD | SR_SWRESET_IPRL); 690 if (ret < 0) { 691 netdev_err(dev->net, "Failed to power down PHY : %d\n", ret); 692 return ret; 693 } 694 msleep(20); 695 696 /* set the embedded Ethernet PHY in power-up state */ 697 ret = sr_sw_reset(dev, SR_SWRESET_IPRL); 698 if (ret < 0) { 699 netdev_err(dev->net, "Failed to reset PHY: %d\n", ret); 700 return ret; 701 } 702 msleep(600); 703 704 /* set the embedded Ethernet PHY in reset state */ 705 ret = sr_sw_reset(dev, SR_SWRESET_CLEAR); 706 if (ret < 0) { 707 netdev_err(dev->net, "Failed to power up PHY: %d\n", ret); 708 return ret; 709 } 710 msleep(20); 711 712 /* set the embedded Ethernet PHY in power-up state */ 713 ret = sr_sw_reset(dev, SR_SWRESET_IPRL); 714 if (ret < 0) { 715 netdev_err(dev->net, "Failed to reset PHY: %d\n", ret); 716 return ret; 717 } 718 719 return 0; 720 } 721 722 static int sr9800_bind(struct usbnet *dev, struct usb_interface *intf) 723 { 724 struct sr_data *data = (struct sr_data *)&dev->data; 725 u16 led01_mux, led23_mux; 726 int ret, embd_phy; 727 u8 addr[ETH_ALEN]; 728 u32 phyid; 729 u16 rx_ctl; 730 731 data->eeprom_len = SR9800_EEPROM_LEN; 732 733 ret = usbnet_get_endpoints(dev, intf); 734 if (ret) 735 goto out; 736 737 /* LED Setting Rule : 738 * AABB:CCDD 739 * AA : MFA0(LED0) 740 * BB : MFA1(LED1) 741 * CC : MFA2(LED2), Reserved for SR9800 742 * DD : MFA3(LED3), Reserved for SR9800 743 */ 744 led01_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_LINK; 745 led23_mux = (SR_LED_MUX_LINK_ACTIVE << 8) | SR_LED_MUX_TX_ACTIVE; 746 ret = sr_write_cmd(dev, SR_CMD_LED_MUX, led01_mux, led23_mux, 0, NULL); 747 if (ret < 0) { 748 netdev_err(dev->net, "set LINK LED failed : %d\n", ret); 749 goto out; 750 } 751 752 /* Get the MAC address */ 753 ret = sr_read_cmd(dev, SR_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, addr); 754 if (ret < 0) { 755 netdev_dbg(dev->net, "Failed to read MAC address: %d\n", ret); 756 return ret; 757 } 758 eth_hw_addr_set(dev->net, addr); 759 netdev_dbg(dev->net, "mac addr : %pM\n", dev->net->dev_addr); 760 761 /* Initialize MII structure */ 762 dev->mii.dev = dev->net; 763 dev->mii.mdio_read = sr_mdio_read; 764 dev->mii.mdio_write = sr_mdio_write; 765 dev->mii.phy_id_mask = 0x1f; 766 dev->mii.reg_num_mask = 0x1f; 767 dev->mii.phy_id = sr_get_phy_addr(dev); 768 769 dev->net->netdev_ops = &sr9800_netdev_ops; 770 dev->net->ethtool_ops = &sr9800_ethtool_ops; 771 772 embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0); 773 /* Reset the PHY to normal operation mode */ 774 ret = sr_write_cmd(dev, SR_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL); 775 if (ret < 0) { 776 netdev_dbg(dev->net, "Select PHY #1 failed: %d\n", ret); 777 return ret; 778 } 779 780 /* Init PHY routine */ 781 ret = sr9800_phy_powerup(dev); 782 if (ret < 0) 783 goto out; 784 785 rx_ctl = sr_read_rx_ctl(dev); 786 netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl); 787 ret = sr_write_rx_ctl(dev, 0x0000); 788 if (ret < 0) 789 goto out; 790 791 rx_ctl = sr_read_rx_ctl(dev); 792 netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl); 793 794 /* Read PHYID register *AFTER* the PHY was reset properly */ 795 phyid = sr_get_phyid(dev); 796 netdev_dbg(dev->net, "PHYID=0x%08x\n", phyid); 797 798 /* medium mode setting */ 799 ret = sr9800_set_default_mode(dev); 800 if (ret < 0) 801 goto out; 802 803 if (dev->udev->speed == USB_SPEED_HIGH) { 804 ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE, 805 SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].byte_cnt, 806 SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].threshold, 807 0, NULL); 808 if (ret < 0) { 809 netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret); 810 goto out; 811 } 812 dev->rx_urb_size = 813 SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_4K].size; 814 } else { 815 ret = sr_write_cmd(dev, SR_CMD_BULKIN_SIZE, 816 SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].byte_cnt, 817 SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].threshold, 818 0, NULL); 819 if (ret < 0) { 820 netdev_err(dev->net, "Reset RX_CTL failed: %d\n", ret); 821 goto out; 822 } 823 dev->rx_urb_size = 824 SR9800_BULKIN_SIZE[SR9800_MAX_BULKIN_2K].size; 825 } 826 netdev_dbg(dev->net, "%s : setting rx_urb_size with : %zu\n", __func__, 827 dev->rx_urb_size); 828 return 0; 829 830 out: 831 return ret; 832 } 833 834 static const struct driver_info sr9800_driver_info = { 835 .description = "CoreChip SR9800 USB 2.0 Ethernet", 836 .bind = sr9800_bind, 837 .status = sr_status, 838 .link_reset = sr9800_link_reset, 839 .reset = sr9800_reset, 840 .flags = DRIVER_FLAG, 841 .rx_fixup = sr_rx_fixup, 842 .tx_fixup = sr_tx_fixup, 843 }; 844 845 static const struct usb_device_id products[] = { 846 { 847 USB_DEVICE(0x0fe6, 0x9800), /* SR9800 Device */ 848 .driver_info = (unsigned long) &sr9800_driver_info, 849 }, 850 {}, /* END */ 851 }; 852 853 MODULE_DEVICE_TABLE(usb, products); 854 855 static struct usb_driver sr_driver = { 856 .name = DRIVER_NAME, 857 .id_table = products, 858 .probe = usbnet_probe, 859 .suspend = usbnet_suspend, 860 .resume = usbnet_resume, 861 .disconnect = usbnet_disconnect, 862 .supports_autosuspend = 1, 863 }; 864 865 module_usb_driver(sr_driver); 866 867 MODULE_AUTHOR("Liu Junliang <liujunliang_ljl@163.com"); 868 MODULE_VERSION(DRIVER_VERSION); 869 MODULE_DESCRIPTION("SR9800 USB 2.0 USB2NET Dev : http://www.corechip-sz.com"); 870 MODULE_LICENSE("GPL"); 871