-
- spin_lock_irq(&lp->lock);
- rc = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
- spin_unlock_irq(&lp->lock);
-
- return rc;
-}
-
-static int tc_mdio_read(struct net_device *dev, int phy_id, int location)
-{
- struct tc35815_regs __iomem *tr =
- (struct tc35815_regs __iomem *)dev->base_addr;
- u32 data;
- tc_writel(MD_CA_Busy | (phy_id << 5) | location, &tr->MD_CA);
- while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
- ;
- data = tc_readl(&tr->MD_Data);
- return data & 0xffff;
-}
-
-static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
- int val)
-{
- struct tc35815_regs __iomem *tr =
- (struct tc35815_regs __iomem *)dev->base_addr;
- tc_writel(val, &tr->MD_Data);
- tc_writel(MD_CA_Busy | MD_CA_Wr | (phy_id << 5) | location, &tr->MD_CA);
- while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
- ;
-}
-
-/* Auto negotiation. The scheme is very simple. We have a timer routine
- * that keeps watching the auto negotiation process as it progresses.
- * The DP83840 is first told to start doing it's thing, we set up the time
- * and place the timer state machine in it's initial state.
- *
- * Here the timer peeks at the DP83840 status registers at each click to see
- * if the auto negotiation has completed, we assume here that the DP83840 PHY
- * will time out at some point and just tell us what (didn't) happen. For
- * complete coverage we only allow so many of the ticks at this level to run,
- * when this has expired we print a warning message and try another strategy.
- * This "other" strategy is to force the interface into various speed/duplex
- * configurations and we stop when we see a link-up condition before the
- * maximum number of "peek" ticks have occurred.
- *
- * Once a valid link status has been detected we configure the BigMAC and
- * the rest of the Happy Meal to speak the most efficient protocol we could
- * get a clean link for. The priority for link configurations, highest first
- * is:
- * 100 Base-T Full Duplex
- * 100 Base-T Half Duplex
- * 10 Base-T Full Duplex
- * 10 Base-T Half Duplex
- *
- * We start a new timer now, after a successful auto negotiation status has
- * been detected. This timer just waits for the link-up bit to get set in
- * the BMCR of the DP83840. When this occurs we print a kernel log message
- * describing the link type in use and the fact that it is up.
- *
- * If a fatal error of some sort is signalled and detected in the interrupt
- * service routine, and the chip is reset, or the link is ifconfig'd down
- * and then back up, this entire process repeats itself all over again.
- */
-/* Note: Above comments are come from sunhme driver. */
-
-static int tc35815_try_next_permutation(struct net_device *dev)
-{
- struct tc35815_local *lp = dev->priv;
- int pid = lp->phy_addr;
- unsigned short bmcr;
-
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
-
- /* Downgrade from full to half duplex. Only possible via ethtool. */
- if (bmcr & BMCR_FULLDPLX) {
- bmcr &= ~BMCR_FULLDPLX;
- printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
- tc_mdio_write(dev, pid, MII_BMCR, bmcr);
- return 0;
- }
-
- /* Downgrade from 100 to 10. */
- if (bmcr & BMCR_SPEED100) {
- bmcr &= ~BMCR_SPEED100;
- printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
- tc_mdio_write(dev, pid, MII_BMCR, bmcr);
- return 0;
- }
-
- /* We've tried everything. */
- return -1;
-}
-
-static void
-tc35815_display_link_mode(struct net_device *dev)
-{
- struct tc35815_local *lp = dev->priv;
- int pid = lp->phy_addr;
- unsigned short lpa, bmcr;
- char *speed = "", *duplex = "";
-
- lpa = tc_mdio_read(dev, pid, MII_LPA);
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
- speed = "100Mb/s";
- else
- speed = "10Mb/s";
- if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
- duplex = "Full Duplex";
- else
- duplex = "Half Duplex";
-
- if (netif_msg_link(lp))
- printk(KERN_INFO "%s: Link is up at %s, %s.\n",
- dev->name, speed, duplex);
- printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
- dev->name,
- bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
-}
-
-static void tc35815_display_forced_link_mode(struct net_device *dev)
-{
- struct tc35815_local *lp = dev->priv;
- int pid = lp->phy_addr;
- unsigned short bmcr;
- char *speed = "", *duplex = "";
-
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- if (bmcr & BMCR_SPEED100)
- speed = "100Mb/s";
- else
- speed = "10Mb/s";
- if (bmcr & BMCR_FULLDPLX)
- duplex = "Full Duplex.\n";
- else
- duplex = "Half Duplex.\n";
-
- if (netif_msg_link(lp))
- printk(KERN_INFO "%s: Link has been forced up at %s, %s",
- dev->name, speed, duplex);
-}
-
-static void tc35815_set_link_modes(struct net_device *dev)
-{
- struct tc35815_local *lp = dev->priv;
- struct tc35815_regs __iomem *tr =
- (struct tc35815_regs __iomem *)dev->base_addr;
- int pid = lp->phy_addr;
- unsigned short bmcr, lpa;
- int speed;
-
- if (lp->timer_state == arbwait) {
- lpa = tc_mdio_read(dev, pid, MII_LPA);
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
- dev->name,
- bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
- if (!(lpa & (LPA_10HALF | LPA_10FULL |
- LPA_100HALF | LPA_100FULL))) {
- /* fall back to 10HALF */
- printk(KERN_INFO "%s: bad ability %04x - falling back to 10HD.\n",
- dev->name, lpa);
- lpa = LPA_10HALF;
- }
- if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
- lp->fullduplex = 1;
- else
- lp->fullduplex = 0;
- if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
- speed = 100;
- else
- speed = 10;
- } else {
- /* Forcing a link mode. */
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- if (bmcr & BMCR_FULLDPLX)
- lp->fullduplex = 1;
- else
- lp->fullduplex = 0;
- if (bmcr & BMCR_SPEED100)
- speed = 100;
- else
- speed = 10;
- }
-
- tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_HaltReq, &tr->MAC_Ctl);
- if (lp->fullduplex) {
- tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_FullDup, &tr->MAC_Ctl);
- } else {
- tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_FullDup, &tr->MAC_Ctl);
- }
- tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_HaltReq, &tr->MAC_Ctl);
-
- /* TX4939 PCFG.SPEEDn bit will be changed on NETDEV_CHANGE event. */
-
-#ifndef NO_CHECK_CARRIER
- /* TX4939 does not have EnLCarr */
- if (lp->boardtype != TC35815_TX4939) {
-#ifdef WORKAROUND_LOSTCAR
- /* WORKAROUND: enable LostCrS only if half duplex operation */
- if (!lp->fullduplex && lp->boardtype != TC35815_TX4939)
- tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr, &tr->Tx_Ctl);
-#endif
- }
-#endif
- lp->mii.full_duplex = lp->fullduplex;
-}
-
-static void tc35815_timer(unsigned long data)
-{
- struct net_device *dev = (struct net_device *)data;
- struct tc35815_local *lp = dev->priv;
- int pid = lp->phy_addr;
- unsigned short bmsr, bmcr, lpa;
- int restart_timer = 0;
-
- spin_lock_irq(&lp->lock);
-
- lp->timer_ticks++;
- switch (lp->timer_state) {
- case arbwait:
- /*
- * Only allow for 5 ticks, thats 10 seconds and much too
- * long to wait for arbitration to complete.
- */
- /* TC35815 need more times... */
- if (lp->timer_ticks >= 10) {
- /* Enter force mode. */
- if (!options.doforce) {
- printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
- " cable probblem?\n", dev->name);
- /* Try to restart the adaptor. */
- tc35815_restart(dev);
- goto out;
- }
- printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
- " trying force link mode\n", dev->name);
- printk(KERN_DEBUG "%s: BMCR %x BMSR %x\n", dev->name,
- tc_mdio_read(dev, pid, MII_BMCR),
- tc_mdio_read(dev, pid, MII_BMSR));
- bmcr = BMCR_SPEED100;
- tc_mdio_write(dev, pid, MII_BMCR, bmcr);
-
- /*
- * OK, seems we need do disable the transceiver
- * for the first tick to make sure we get an
- * accurate link state at the second tick.
- */
-
- lp->timer_state = ltrywait;
- lp->timer_ticks = 0;
- restart_timer = 1;
- } else {
- /* Anything interesting happen? */
- bmsr = tc_mdio_read(dev, pid, MII_BMSR);
- if (bmsr & BMSR_ANEGCOMPLETE) {
- /* Just what we've been waiting for... */
- tc35815_set_link_modes(dev);
-
- /*
- * Success, at least so far, advance our state
- * engine.
- */
- lp->timer_state = lupwait;
- restart_timer = 1;
- } else {
- restart_timer = 1;
- }
- }
- break;
-
- case lupwait:
- /*
- * Auto negotiation was successful and we are awaiting a
- * link up status. I have decided to let this timer run
- * forever until some sort of error is signalled, reporting
- * a message to the user at 10 second intervals.
- */
- bmsr = tc_mdio_read(dev, pid, MII_BMSR);
- if (bmsr & BMSR_LSTATUS) {
- /*
- * Wheee, it's up, display the link mode in use and put
- * the timer to sleep.
- */
- tc35815_display_link_mode(dev);
- netif_carrier_on(dev);
-#ifdef WORKAROUND_100HALF_PROMISC
- /* delayed promiscuous enabling */
- if (dev->flags & IFF_PROMISC)
- tc35815_set_multicast_list(dev);
-#endif
-#if 1
- lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
- lp->timer_state = lcheck;
- restart_timer = 1;
-#else
- lp->timer_state = asleep;
- restart_timer = 0;
-#endif
- } else {
- if (lp->timer_ticks >= 10) {
- printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
- "not completely up.\n", dev->name);
- lp->timer_ticks = 0;
- restart_timer = 1;
- } else {
- restart_timer = 1;
- }
- }
- break;
-
- case ltrywait:
- /*
- * Making the timeout here too long can make it take
- * annoyingly long to attempt all of the link mode
- * permutations, but then again this is essentially
- * error recovery code for the most part.
- */
- bmsr = tc_mdio_read(dev, pid, MII_BMSR);
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- if (lp->timer_ticks == 1) {
- /*
- * Re-enable transceiver, we'll re-enable the
- * transceiver next tick, then check link state
- * on the following tick.
- */
- restart_timer = 1;
- break;
- }
- if (lp->timer_ticks == 2) {
- restart_timer = 1;
- break;
- }
- if (bmsr & BMSR_LSTATUS) {
- /* Force mode selection success. */
- tc35815_display_forced_link_mode(dev);
- netif_carrier_on(dev);
- tc35815_set_link_modes(dev);
-#ifdef WORKAROUND_100HALF_PROMISC
- /* delayed promiscuous enabling */
- if (dev->flags & IFF_PROMISC)
- tc35815_set_multicast_list(dev);
-#endif
-#if 1
- lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
- lp->timer_state = lcheck;
- restart_timer = 1;
-#else
- lp->timer_state = asleep;
- restart_timer = 0;
-#endif
- } else {
- if (lp->timer_ticks >= 4) { /* 6 seconds or so... */
- int ret;
-
- ret = tc35815_try_next_permutation(dev);
- if (ret == -1) {
- /*
- * Aieee, tried them all, reset the
- * chip and try all over again.
- */
- printk(KERN_NOTICE "%s: Link down, "
- "cable problem?\n",
- dev->name);
-
- /* Try to restart the adaptor. */
- tc35815_restart(dev);
- goto out;
- }
- lp->timer_ticks = 0;
- restart_timer = 1;
- } else {
- restart_timer = 1;
- }
- }
- break;
-
- case lcheck:
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- lpa = tc_mdio_read(dev, pid, MII_LPA);
- if (bmcr & (BMCR_PDOWN | BMCR_ISOLATE | BMCR_RESET)) {
- printk(KERN_ERR "%s: PHY down? (BMCR %x)\n", dev->name,
- bmcr);
- } else if ((lp->saved_lpa ^ lpa) &
- (LPA_100FULL|LPA_100HALF|LPA_10FULL|LPA_10HALF)) {
- printk(KERN_NOTICE "%s: link status changed"
- " (BMCR %x LPA %x->%x)\n", dev->name,
- bmcr, lp->saved_lpa, lpa);
- } else {
- /* go on */
- restart_timer = 1;
- break;
- }
- /* Try to restart the adaptor. */
- tc35815_restart(dev);
- goto out;
-
- case asleep:
- default:
- /* Can't happens.... */
- printk(KERN_ERR "%s: Aieee, link timer is asleep but we got "
- "one anyways!\n", dev->name);
- restart_timer = 0;
- lp->timer_ticks = 0;
- lp->timer_state = asleep; /* foo on you */
- break;
- }
-
- if (restart_timer) {
- lp->timer.expires = jiffies + msecs_to_jiffies(1200);
- add_timer(&lp->timer);
- }
-out:
- spin_unlock_irq(&lp->lock);
-}
-
-static void tc35815_start_auto_negotiation(struct net_device *dev,
- struct ethtool_cmd *ep)
-{
- struct tc35815_local *lp = dev->priv;
- int pid = lp->phy_addr;
- unsigned short bmsr, bmcr, advertize;
- int timeout;
-
- netif_carrier_off(dev);
- bmsr = tc_mdio_read(dev, pid, MII_BMSR);
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- advertize = tc_mdio_read(dev, pid, MII_ADVERTISE);
-
- if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
- if (options.speed || options.duplex) {
- /* Advertise only specified configuration. */
- advertize &= ~(ADVERTISE_10HALF |
- ADVERTISE_10FULL |
- ADVERTISE_100HALF |
- ADVERTISE_100FULL);
- if (options.speed != 10) {
- if (options.duplex != 1)
- advertize |= ADVERTISE_100FULL;
- if (options.duplex != 2)
- advertize |= ADVERTISE_100HALF;
- }
- if (options.speed != 100) {
- if (options.duplex != 1)
- advertize |= ADVERTISE_10FULL;
- if (options.duplex != 2)
- advertize |= ADVERTISE_10HALF;
- }
- if (options.speed == 100)
- bmcr |= BMCR_SPEED100;
- else if (options.speed == 10)
- bmcr &= ~BMCR_SPEED100;
- if (options.duplex == 2)
- bmcr |= BMCR_FULLDPLX;
- else if (options.duplex == 1)
- bmcr &= ~BMCR_FULLDPLX;
- } else {
- /* Advertise everything we can support. */
- if (bmsr & BMSR_10HALF)
- advertize |= ADVERTISE_10HALF;
- else
- advertize &= ~ADVERTISE_10HALF;
- if (bmsr & BMSR_10FULL)
- advertize |= ADVERTISE_10FULL;
- else
- advertize &= ~ADVERTISE_10FULL;
- if (bmsr & BMSR_100HALF)
- advertize |= ADVERTISE_100HALF;
- else
- advertize &= ~ADVERTISE_100HALF;
- if (bmsr & BMSR_100FULL)
- advertize |= ADVERTISE_100FULL;
- else
- advertize &= ~ADVERTISE_100FULL;
- }
-
- tc_mdio_write(dev, pid, MII_ADVERTISE, advertize);
-
- /* Enable Auto-Negotiation, this is usually on already... */
- bmcr |= BMCR_ANENABLE;
- tc_mdio_write(dev, pid, MII_BMCR, bmcr);
-
- /* Restart it to make sure it is going. */
- bmcr |= BMCR_ANRESTART;
- tc_mdio_write(dev, pid, MII_BMCR, bmcr);
- printk(KERN_DEBUG "%s: ADVERTISE %x BMCR %x\n", dev->name, advertize, bmcr);
-
- /* BMCR_ANRESTART self clears when the process has begun. */
- timeout = 64; /* More than enough. */
- while (--timeout) {
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- if (!(bmcr & BMCR_ANRESTART))
- break; /* got it. */
- udelay(10);
- }
- if (!timeout) {
- printk(KERN_ERR "%s: TC35815 would not start auto "
- "negotiation BMCR=0x%04x\n",
- dev->name, bmcr);
- printk(KERN_NOTICE "%s: Performing force link "
- "detection.\n", dev->name);
- goto force_link;
- } else {
- printk(KERN_DEBUG "%s: auto negotiation started.\n", dev->name);
- lp->timer_state = arbwait;
- }
- } else {
-force_link:
- /* Force the link up, trying first a particular mode.
- * Either we are here at the request of ethtool or
- * because the Happy Meal would not start to autoneg.
- */
-
- /* Disable auto-negotiation in BMCR, enable the duplex and
- * speed setting, init the timer state machine, and fire it off.
- */
- if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
- bmcr = BMCR_SPEED100;
- } else {
- if (ep->speed == SPEED_100)
- bmcr = BMCR_SPEED100;
- else
- bmcr = 0;
- if (ep->duplex == DUPLEX_FULL)
- bmcr |= BMCR_FULLDPLX;
- }
- tc_mdio_write(dev, pid, MII_BMCR, bmcr);
-
- /* OK, seems we need do disable the transceiver for the first
- * tick to make sure we get an accurate link state at the
- * second tick.
- */
- lp->timer_state = ltrywait;
- }
-
- del_timer(&lp->timer);
- lp->timer_ticks = 0;
- lp->timer.expires = jiffies + msecs_to_jiffies(1200);
- add_timer(&lp->timer);
-}
-
-static void tc35815_find_phy(struct net_device *dev)
-{
- struct tc35815_local *lp = dev->priv;
- int pid = lp->phy_addr;
- unsigned short id0;
-
- /* find MII phy */
- for (pid = 31; pid >= 0; pid--) {
- id0 = tc_mdio_read(dev, pid, MII_BMSR);
- if (id0 != 0xffff && id0 != 0x0000 &&
- (id0 & BMSR_RESV) != (0xffff & BMSR_RESV) /* paranoia? */
- ) {
- lp->phy_addr = pid;
- break;
- }
- }
- if (pid < 0) {
- printk(KERN_ERR "%s: No MII Phy found.\n",
- dev->name);
- lp->phy_addr = pid = 0;
- }
-
- lp->mii_id[0] = tc_mdio_read(dev, pid, MII_PHYSID1);
- lp->mii_id[1] = tc_mdio_read(dev, pid, MII_PHYSID2);
- if (netif_msg_hw(lp))
- printk(KERN_INFO "%s: PHY(%02x) ID %04x %04x\n", dev->name,
- pid, lp->mii_id[0], lp->mii_id[1]);
-}
-
-static void tc35815_phy_chip_init(struct net_device *dev)
-{
- struct tc35815_local *lp = dev->priv;
- int pid = lp->phy_addr;
- unsigned short bmcr;
- struct ethtool_cmd ecmd, *ep;
-
- /* dis-isolate if needed. */
- bmcr = tc_mdio_read(dev, pid, MII_BMCR);
- if (bmcr & BMCR_ISOLATE) {
- int count = 32;
- printk(KERN_DEBUG "%s: unisolating...", dev->name);
- tc_mdio_write(dev, pid, MII_BMCR, bmcr & ~BMCR_ISOLATE);
- while (--count) {
- if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_ISOLATE))
- break;
- udelay(20);
- }
- printk(" %s.\n", count ? "done" : "failed");
- }
-
- if (options.speed && options.duplex) {
- ecmd.autoneg = AUTONEG_DISABLE;
- ecmd.speed = options.speed == 10 ? SPEED_10 : SPEED_100;
- ecmd.duplex = options.duplex == 1 ? DUPLEX_HALF : DUPLEX_FULL;
- ep = &ecmd;
- } else {
- ep = NULL;
- }
- tc35815_start_auto_negotiation(dev, ep);