Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ax88796: use generic mdio_bitbang driver

..instead of using hand-crafted and not proper working version.

Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

+201 -205
+2 -2
drivers/net/Kconfig
··· 238 238 config AX88796 239 239 tristate "ASIX AX88796 NE2000 clone support" 240 240 depends on ARM || MIPS || SUPERH 241 - select CRC32 242 - select MII 241 + select PHYLIB 242 + select MDIO_BITBANG 243 243 help 244 244 AX88796 driver, using platform bus to provide 245 245 chip detection and resources
+199 -203
drivers/net/ax88796.c
··· 24 24 #include <linux/netdevice.h> 25 25 #include <linux/etherdevice.h> 26 26 #include <linux/ethtool.h> 27 - #include <linux/mii.h> 27 + #include <linux/mdio-bitbang.h> 28 + #include <linux/phy.h> 28 29 #include <linux/eeprom_93cx6.h> 29 30 #include <linux/slab.h> 30 31 31 32 #include <net/ax88796.h> 32 33 33 34 #include <asm/system.h> 34 - 35 - static int phy_debug; 36 35 37 36 /* Rename the lib8390.c functions to show that they are in this driver */ 38 37 #define __ei_open ax_ei_open ··· 77 78 #define NESM_START_PG 0x40 /* First page of TX buffer */ 78 79 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */ 79 80 81 + #define AX_GPOC_PPDSET BIT(6) 82 + 80 83 /* device private data */ 81 84 82 85 struct ax_device { 83 - struct timer_list mii_timer; 84 - spinlock_t mii_lock; 85 - struct mii_if_info mii; 86 + struct mii_bus *mii_bus; 87 + struct mdiobb_ctrl bb_ctrl; 88 + struct phy_device *phy_dev; 89 + void __iomem *addr_memr; 90 + u8 reg_memr; 91 + int link; 92 + int speed; 93 + int duplex; 86 94 87 - u32 msg_enable; 88 95 void __iomem *map2; 89 96 const struct ax_plat_data *plat; 90 97 ··· 318 313 #define AX_MEMR_EEO BIT(6) 319 314 #define AX_MEMR_EECLK BIT(7) 320 315 321 - /* 322 - * ax_mii_ei_outbits 323 - * 324 - * write the specified set of bits to the phy 325 - */ 326 - static void 327 - ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len) 316 + static void ax_handle_link_change(struct net_device *dev) 328 317 { 329 - struct ei_device *ei_local = netdev_priv(dev); 330 - void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR; 331 - unsigned int memr; 318 + struct ax_device *ax = to_ax_dev(dev); 319 + struct phy_device *phy_dev = ax->phy_dev; 320 + int status_change = 0; 332 321 333 - /* clock low, data to output mode */ 334 - memr = ei_inb(memr_addr); 335 - memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR); 336 - ei_outb(memr, memr_addr); 322 + if (phy_dev->link && ((ax->speed != phy_dev->speed) || 323 + (ax->duplex != phy_dev->duplex))) { 337 324 338 - for (len--; len >= 0; len--) { 339 - if (bits & (1 << len)) 340 - memr |= AX_MEMR_MDO; 341 - else 342 - memr &= ~AX_MEMR_MDO; 343 - 344 - ei_outb(memr, memr_addr); 345 - 346 - /* clock high */ 347 - 348 - ei_outb(memr | AX_MEMR_MDC, memr_addr); 349 - udelay(1); 350 - 351 - /* clock low */ 352 - ei_outb(memr, memr_addr); 325 + ax->speed = phy_dev->speed; 326 + ax->duplex = phy_dev->duplex; 327 + status_change = 1; 353 328 } 354 329 355 - /* leaves the clock line low, mdir input */ 356 - memr |= AX_MEMR_MDIR; 357 - ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR); 358 - } 330 + if (phy_dev->link != ax->link) { 331 + if (!phy_dev->link) { 332 + ax->speed = 0; 333 + ax->duplex = -1; 334 + } 335 + ax->link = phy_dev->link; 359 336 360 - /* 361 - * ax_phy_ei_inbits 362 - * 363 - * read a specified number of bits from the phy 364 - */ 365 - static unsigned int 366 - ax_phy_ei_inbits(struct net_device *dev, int no) 367 - { 368 - struct ei_device *ei_local = netdev_priv(dev); 369 - void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR; 370 - unsigned int memr; 371 - unsigned int result = 0; 372 - 373 - /* clock low, data to input mode */ 374 - memr = ei_inb(memr_addr); 375 - memr &= ~AX_MEMR_MDC; 376 - memr |= AX_MEMR_MDIR; 377 - ei_outb(memr, memr_addr); 378 - 379 - for (no--; no >= 0; no--) { 380 - ei_outb(memr | AX_MEMR_MDC, memr_addr); 381 - 382 - udelay(1); 383 - 384 - if (ei_inb(memr_addr) & AX_MEMR_MDI) 385 - result |= (1 << no); 386 - 387 - ei_outb(memr, memr_addr); 337 + status_change = 1; 388 338 } 389 339 390 - return result; 340 + if (status_change) 341 + phy_print_status(phy_dev); 391 342 } 392 343 393 - /* 394 - * ax_phy_issueaddr 395 - * 396 - * use the low level bit shifting routines to send the address 397 - * and command to the specified phy 398 - */ 399 - static void 400 - ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc) 344 + static int ax_mii_probe(struct net_device *dev) 401 345 { 402 - if (phy_debug) 403 - netdev_dbg(dev, "%s: dev %p, %04x, %04x, %d\n", 404 - __func__, dev, phy_addr, reg, opc); 346 + struct ax_device *ax = to_ax_dev(dev); 347 + struct phy_device *phy_dev = NULL; 348 + int ret; 405 349 406 - ax_mii_ei_outbits(dev, 0x3f, 6); /* pre-amble */ 407 - ax_mii_ei_outbits(dev, 1, 2); /* frame-start */ 408 - ax_mii_ei_outbits(dev, opc, 2); /* op code */ 409 - ax_mii_ei_outbits(dev, phy_addr, 5); /* phy address */ 410 - ax_mii_ei_outbits(dev, reg, 5); /* reg address */ 350 + /* find the first phy */ 351 + phy_dev = phy_find_first(ax->mii_bus); 352 + if (!phy_dev) { 353 + netdev_err(dev, "no PHY found\n"); 354 + return -ENODEV; 355 + } 356 + 357 + ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change, 0, 358 + PHY_INTERFACE_MODE_MII); 359 + if (ret) { 360 + netdev_err(dev, "Could not attach to PHY\n"); 361 + return ret; 362 + } 363 + 364 + /* mask with MAC supported features */ 365 + phy_dev->supported &= PHY_BASIC_FEATURES; 366 + phy_dev->advertising = phy_dev->supported; 367 + 368 + ax->phy_dev = phy_dev; 369 + 370 + netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n", 371 + phy_dev->drv->name, dev_name(&phy_dev->dev), phy_dev->irq); 372 + 373 + return 0; 411 374 } 412 375 413 - static int 414 - ax_phy_read(struct net_device *dev, int phy_addr, int reg) 376 + static void ax_phy_switch(struct net_device *dev, int on) 415 377 { 416 378 struct ei_device *ei_local = netdev_priv(dev); 417 - unsigned long flags; 418 - unsigned int result; 419 - 420 - spin_lock_irqsave(&ei_local->page_lock, flags); 421 - 422 - ax_phy_issueaddr(dev, phy_addr, reg, 2); 423 - 424 - result = ax_phy_ei_inbits(dev, 17); 425 - result &= ~(3 << 16); 426 - 427 - spin_unlock_irqrestore(&ei_local->page_lock, flags); 428 - 429 - if (phy_debug) 430 - netdev_dbg(dev, "%s: %04x.%04x => read %04x\n", __func__, 431 - phy_addr, reg, result); 432 - 433 - return result; 434 - } 435 - 436 - static void 437 - ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value) 438 - { 439 - struct ei_device *ei = netdev_priv(dev); 440 - unsigned long flags; 441 - 442 - netdev_dbg(dev, "%s: %p, %04x, %04x %04x\n", 443 - __func__, dev, phy_addr, reg, value); 444 - 445 - spin_lock_irqsave(&ei->page_lock, flags); 446 - 447 - ax_phy_issueaddr(dev, phy_addr, reg, 1); 448 - ax_mii_ei_outbits(dev, 2, 2); /* send TA */ 449 - ax_mii_ei_outbits(dev, value, 16); 450 - 451 - spin_unlock_irqrestore(&ei->page_lock, flags); 452 - } 453 - 454 - static void ax_mii_expiry(unsigned long data) 455 - { 456 - struct net_device *dev = (struct net_device *)data; 457 379 struct ax_device *ax = to_ax_dev(dev); 458 - unsigned long flags; 459 380 460 - spin_lock_irqsave(&ax->mii_lock, flags); 461 - mii_check_media(&ax->mii, netif_msg_link(ax), 0); 462 - spin_unlock_irqrestore(&ax->mii_lock, flags); 381 + u8 reg_gpoc = ax->plat->gpoc_val; 463 382 464 - if (ax->running) { 465 - ax->mii_timer.expires = jiffies + HZ*2; 466 - add_timer(&ax->mii_timer); 467 - } 383 + if (!!on) 384 + reg_gpoc &= ~AX_GPOC_PPDSET; 385 + else 386 + reg_gpoc |= AX_GPOC_PPDSET; 387 + 388 + ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17)); 468 389 } 469 390 470 391 static int ax_open(struct net_device *dev) 471 392 { 472 393 struct ax_device *ax = to_ax_dev(dev); 473 - struct ei_device *ei_local = netdev_priv(dev); 474 394 int ret; 475 395 476 396 netdev_dbg(dev, "open\n"); ··· 403 473 ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags, 404 474 dev->name, dev); 405 475 if (ret) 406 - return ret; 407 - 408 - ret = ax_ei_open(dev); 409 - if (ret) { 410 - free_irq(dev->irq, dev); 411 - return ret; 412 - } 476 + goto failed_request_irq; 413 477 414 478 /* turn the phy on (if turned off) */ 479 + ax_phy_switch(dev, 1); 415 480 416 - ei_outb(ax->plat->gpoc_val, ei_local->mem + EI_SHIFT(0x17)); 481 + ret = ax_mii_probe(dev); 482 + if (ret) 483 + goto failed_mii_probe; 484 + phy_start(ax->phy_dev); 485 + 486 + ret = ax_ei_open(dev); 487 + if (ret) 488 + goto failed_ax_ei_open; 489 + 417 490 ax->running = 1; 418 491 419 - /* start the MII timer */ 420 - 421 - init_timer(&ax->mii_timer); 422 - 423 - ax->mii_timer.expires = jiffies + 1; 424 - ax->mii_timer.data = (unsigned long) dev; 425 - ax->mii_timer.function = ax_mii_expiry; 426 - 427 - add_timer(&ax->mii_timer); 428 - 429 492 return 0; 493 + 494 + failed_ax_ei_open: 495 + phy_disconnect(ax->phy_dev); 496 + failed_mii_probe: 497 + ax_phy_switch(dev, 0); 498 + free_irq(dev->irq, dev); 499 + failed_request_irq: 500 + return ret; 430 501 } 431 502 432 503 static int ax_close(struct net_device *dev) 433 504 { 434 505 struct ax_device *ax = to_ax_dev(dev); 435 - struct ei_device *ei_local = netdev_priv(dev); 436 506 437 507 netdev_dbg(dev, "close\n"); 438 - 439 - /* turn the phy off */ 440 - 441 - ei_outb(ax->plat->gpoc_val | (1 << 6), 442 - ei_local->mem + EI_SHIFT(0x17)); 443 508 444 509 ax->running = 0; 445 510 wmb(); 446 511 447 - del_timer_sync(&ax->mii_timer); 448 512 ax_ei_close(dev); 513 + 514 + /* turn the phy off */ 515 + ax_phy_switch(dev, 0); 516 + phy_disconnect(ax->phy_dev); 449 517 450 518 free_irq(dev->irq, dev); 451 519 return 0; ··· 452 524 static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd) 453 525 { 454 526 struct ax_device *ax = to_ax_dev(dev); 455 - unsigned long flags; 456 - int rc; 527 + struct phy_device *phy_dev = ax->phy_dev; 457 528 458 529 if (!netif_running(dev)) 459 530 return -EINVAL; 460 531 461 - spin_lock_irqsave(&ax->mii_lock, flags); 462 - rc = generic_mii_ioctl(&ax->mii, if_mii(req), cmd, NULL); 463 - spin_unlock_irqrestore(&ax->mii_lock, flags); 532 + if (!phy_dev) 533 + return -ENODEV; 464 534 465 - return rc; 535 + return phy_mii_ioctl(phy_dev, req, cmd); 466 536 } 467 537 468 538 /* ethtool ops */ ··· 478 552 static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 479 553 { 480 554 struct ax_device *ax = to_ax_dev(dev); 481 - unsigned long flags; 555 + struct phy_device *phy_dev = ax->phy_dev; 482 556 483 - spin_lock_irqsave(&ax->mii_lock, flags); 484 - mii_ethtool_gset(&ax->mii, cmd); 485 - spin_unlock_irqrestore(&ax->mii_lock, flags); 557 + if (!phy_dev) 558 + return -ENODEV; 486 559 487 - return 0; 560 + return phy_ethtool_gset(phy_dev, cmd); 488 561 } 489 562 490 563 static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 491 564 { 492 565 struct ax_device *ax = to_ax_dev(dev); 493 - unsigned long flags; 494 - int rc; 566 + struct phy_device *phy_dev = ax->phy_dev; 495 567 496 - spin_lock_irqsave(&ax->mii_lock, flags); 497 - rc = mii_ethtool_sset(&ax->mii, cmd); 498 - spin_unlock_irqrestore(&ax->mii_lock, flags); 568 + if (!phy_dev) 569 + return -ENODEV; 499 570 500 - return rc; 501 - } 502 - 503 - static int ax_nway_reset(struct net_device *dev) 504 - { 505 - struct ax_device *ax = to_ax_dev(dev); 506 - return mii_nway_restart(&ax->mii); 507 - } 508 - 509 - static u32 ax_get_link(struct net_device *dev) 510 - { 511 - struct ax_device *ax = to_ax_dev(dev); 512 - return mii_link_ok(&ax->mii); 571 + return phy_ethtool_sset(phy_dev, cmd); 513 572 } 514 573 515 574 static const struct ethtool_ops ax_ethtool_ops = { 516 575 .get_drvinfo = ax_get_drvinfo, 517 576 .get_settings = ax_get_settings, 518 577 .set_settings = ax_set_settings, 519 - .nway_reset = ax_nway_reset, 520 - .get_link = ax_get_link, 578 + .get_link = ethtool_op_get_link, 521 579 }; 522 580 523 581 #ifdef CONFIG_AX88796_93CX6 ··· 552 642 #endif 553 643 }; 554 644 645 + static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level) 646 + { 647 + struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl); 648 + 649 + if (level) 650 + ax->reg_memr |= AX_MEMR_MDC; 651 + else 652 + ax->reg_memr &= ~AX_MEMR_MDC; 653 + 654 + ei_outb(ax->reg_memr, ax->addr_memr); 655 + } 656 + 657 + static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output) 658 + { 659 + struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl); 660 + 661 + if (output) 662 + ax->reg_memr &= ~AX_MEMR_MDIR; 663 + else 664 + ax->reg_memr |= AX_MEMR_MDIR; 665 + 666 + ei_outb(ax->reg_memr, ax->addr_memr); 667 + } 668 + 669 + static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value) 670 + { 671 + struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl); 672 + 673 + if (value) 674 + ax->reg_memr |= AX_MEMR_MDO; 675 + else 676 + ax->reg_memr &= ~AX_MEMR_MDO; 677 + 678 + ei_outb(ax->reg_memr, ax->addr_memr); 679 + } 680 + 681 + static int ax_bb_get_data(struct mdiobb_ctrl *ctrl) 682 + { 683 + struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl); 684 + int reg_memr = ei_inb(ax->addr_memr); 685 + 686 + return reg_memr & AX_MEMR_MDI ? 1 : 0; 687 + } 688 + 689 + static struct mdiobb_ops bb_ops = { 690 + .owner = THIS_MODULE, 691 + .set_mdc = ax_bb_mdc, 692 + .set_mdio_dir = ax_bb_dir, 693 + .set_mdio_data = ax_bb_set_data, 694 + .get_mdio_data = ax_bb_get_data, 695 + }; 696 + 555 697 /* setup code */ 698 + 699 + static int ax_mii_init(struct net_device *dev) 700 + { 701 + struct platform_device *pdev = to_platform_device(dev->dev.parent); 702 + struct ei_device *ei_local = netdev_priv(dev); 703 + struct ax_device *ax = to_ax_dev(dev); 704 + int err, i; 705 + 706 + ax->bb_ctrl.ops = &bb_ops; 707 + ax->addr_memr = ei_local->mem + AX_MEMR; 708 + ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl); 709 + if (!ax->mii_bus) { 710 + err = -ENOMEM; 711 + goto out; 712 + } 713 + 714 + ax->mii_bus->name = "ax88796_mii_bus"; 715 + ax->mii_bus->parent = dev->dev.parent; 716 + snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id); 717 + 718 + ax->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); 719 + if (!ax->mii_bus->irq) { 720 + err = -ENOMEM; 721 + goto out_free_mdio_bitbang; 722 + } 723 + 724 + for (i = 0; i < PHY_MAX_ADDR; i++) 725 + ax->mii_bus->irq[i] = PHY_POLL; 726 + 727 + err = mdiobus_register(ax->mii_bus); 728 + if (err) 729 + goto out_free_irq; 730 + 731 + return 0; 732 + 733 + out_free_irq: 734 + kfree(ax->mii_bus->irq); 735 + out_free_mdio_bitbang: 736 + free_mdio_bitbang(ax->mii_bus); 737 + out: 738 + return err; 739 + } 556 740 557 741 static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local) 558 742 { ··· 767 763 dev->netdev_ops = &ax_netdev_ops; 768 764 dev->ethtool_ops = &ax_ethtool_ops; 769 765 770 - ax->msg_enable = NETIF_MSG_LINK; 771 - ax->mii.phy_id_mask = 0x1f; 772 - ax->mii.reg_num_mask = 0x1f; 773 - ax->mii.phy_id = 0x10; /* onboard phy */ 774 - ax->mii.force_media = 0; 775 - ax->mii.full_duplex = 0; 776 - ax->mii.mdio_read = ax_phy_read; 777 - ax->mii.mdio_write = ax_phy_write; 778 - ax->mii.dev = dev; 766 + ret = ax_mii_init(dev); 767 + if (ret) 768 + goto out_irq; 779 769 780 770 ax_NS8390_init(dev, 0); 781 771 ··· 839 841 SET_NETDEV_DEV(dev, &pdev->dev); 840 842 ei_local = netdev_priv(dev); 841 843 ax = to_ax_dev(dev); 842 - 843 - spin_lock_init(&ax->mii_lock); 844 844 845 845 ax->plat = pdev->dev.platform_data; 846 846 platform_set_drvdata(pdev, dev);