1 /***************************************************************************
3 * Copyright (C) 2004-2008 SMSC
4 * Copyright (C) 2005-2008 ARM
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 ***************************************************************************
21 * Rewritten, heavily based on smsc911x simple driver by SMSC.
22 * Partly uses io macros from smc91x.c by Nicolas Pitre
25 * LAN9115, LAN9116, LAN9117, LAN9118
26 * LAN9215, LAN9216, LAN9217, LAN9218
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/crc32.h>
35 #include <linux/delay.h>
36 #include <linux/errno.h>
37 #include <linux/etherdevice.h>
38 #include <linux/ethtool.h>
39 #include <linux/init.h>
40 #include <linux/interrupt.h>
41 #include <linux/ioport.h>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/netdevice.h>
45 #include <linux/platform_device.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/bug.h>
49 #include <linux/bitops.h>
50 #include <linux/irq.h>
52 #include <linux/swab.h>
53 #include <linux/phy.h>
54 #include <linux/smsc911x.h>
55 #include <linux/device.h>
58 #define SMSC_CHIPNAME "smsc911x"
59 #define SMSC_MDIONAME "smsc911x-mdio"
60 #define SMSC_DRV_VERSION "2008-10-21"
62 MODULE_LICENSE("GPL");
63 MODULE_VERSION(SMSC_DRV_VERSION);
64 MODULE_ALIAS("platform:smsc911x");
67 static int debug = 16;
72 module_param(debug, int, 0);
73 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
78 u32 (*reg_read)(struct smsc911x_data *pdata, u32 reg);
79 void (*reg_write)(struct smsc911x_data *pdata, u32 reg, u32 val);
80 void (*rx_readfifo)(struct smsc911x_data *pdata,
81 unsigned int *buf, unsigned int wordcount);
82 void (*tx_writefifo)(struct smsc911x_data *pdata,
83 unsigned int *buf, unsigned int wordcount);
86 struct smsc911x_data {
91 /* used to decide which workarounds apply */
92 unsigned int generation;
94 /* device configuration (copied from platform_data during probe) */
95 struct smsc911x_platform_config config;
97 /* This needs to be acquired before calling any of below:
98 * smsc911x_mac_read(), smsc911x_mac_write()
102 /* spinlock to ensure register accesses are serialised */
105 struct phy_device *phy_dev;
106 struct mii_bus *mii_bus;
107 int phy_irq[PHY_MAX_ADDR];
108 unsigned int using_extphy;
113 unsigned int gpio_setting;
114 unsigned int gpio_orig_setting;
115 struct net_device *dev;
116 struct napi_struct napi;
118 unsigned int software_irq_signal;
120 #ifdef USE_PHY_WORK_AROUND
121 #define MIN_PACKET_SIZE (64)
122 char loopback_tx_pkt[MIN_PACKET_SIZE];
123 char loopback_rx_pkt[MIN_PACKET_SIZE];
124 unsigned int resetcount;
127 /* Members for Multicast filter workaround */
128 unsigned int multicast_update_pending;
129 unsigned int set_bits_mask;
130 unsigned int clear_bits_mask;
134 /* register access functions */
135 const struct smsc911x_ops *ops;
138 /* Easy access to information */
139 #define __smsc_shift(pdata, reg) ((reg) << ((pdata)->config.shift))
141 static inline u32 __smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
143 if (pdata->config.flags & SMSC911X_USE_32BIT)
144 return readl(pdata->ioaddr + reg);
146 if (pdata->config.flags & SMSC911X_USE_16BIT)
147 return ((readw(pdata->ioaddr + reg) & 0xFFFF) |
148 ((readw(pdata->ioaddr + reg + 2) & 0xFFFF) << 16));
155 __smsc911x_reg_read_shift(struct smsc911x_data *pdata, u32 reg)
157 if (pdata->config.flags & SMSC911X_USE_32BIT)
158 return readl(pdata->ioaddr + __smsc_shift(pdata, reg));
160 if (pdata->config.flags & SMSC911X_USE_16BIT)
161 return (readw(pdata->ioaddr +
162 __smsc_shift(pdata, reg)) & 0xFFFF) |
163 ((readw(pdata->ioaddr +
164 __smsc_shift(pdata, reg + 2)) & 0xFFFF) << 16);
170 static inline u32 smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
175 spin_lock_irqsave(&pdata->dev_lock, flags);
176 data = pdata->ops->reg_read(pdata, reg);
177 spin_unlock_irqrestore(&pdata->dev_lock, flags);
182 static inline void __smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
185 if (pdata->config.flags & SMSC911X_USE_32BIT) {
186 writel(val, pdata->ioaddr + reg);
190 if (pdata->config.flags & SMSC911X_USE_16BIT) {
191 writew(val & 0xFFFF, pdata->ioaddr + reg);
192 writew((val >> 16) & 0xFFFF, pdata->ioaddr + reg + 2);
200 __smsc911x_reg_write_shift(struct smsc911x_data *pdata, u32 reg, u32 val)
202 if (pdata->config.flags & SMSC911X_USE_32BIT) {
203 writel(val, pdata->ioaddr + __smsc_shift(pdata, reg));
207 if (pdata->config.flags & SMSC911X_USE_16BIT) {
209 pdata->ioaddr + __smsc_shift(pdata, reg));
210 writew((val >> 16) & 0xFFFF,
211 pdata->ioaddr + __smsc_shift(pdata, reg + 2));
218 static inline void smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
223 spin_lock_irqsave(&pdata->dev_lock, flags);
224 pdata->ops->reg_write(pdata, reg, val);
225 spin_unlock_irqrestore(&pdata->dev_lock, flags);
228 /* Writes a packet to the TX_DATA_FIFO */
230 smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
231 unsigned int wordcount)
235 spin_lock_irqsave(&pdata->dev_lock, flags);
237 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
239 __smsc911x_reg_write(pdata, TX_DATA_FIFO,
244 if (pdata->config.flags & SMSC911X_USE_32BIT) {
245 writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
249 if (pdata->config.flags & SMSC911X_USE_16BIT) {
251 __smsc911x_reg_write(pdata, TX_DATA_FIFO, *buf++);
257 spin_unlock_irqrestore(&pdata->dev_lock, flags);
260 /* Writes a packet to the TX_DATA_FIFO - shifted version */
262 smsc911x_tx_writefifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
263 unsigned int wordcount)
267 spin_lock_irqsave(&pdata->dev_lock, flags);
269 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
271 __smsc911x_reg_write_shift(pdata, TX_DATA_FIFO,
276 if (pdata->config.flags & SMSC911X_USE_32BIT) {
277 writesl(pdata->ioaddr + __smsc_shift(pdata,
278 TX_DATA_FIFO), buf, wordcount);
282 if (pdata->config.flags & SMSC911X_USE_16BIT) {
284 __smsc911x_reg_write_shift(pdata,
285 TX_DATA_FIFO, *buf++);
291 spin_unlock_irqrestore(&pdata->dev_lock, flags);
294 /* Reads a packet out of the RX_DATA_FIFO */
296 smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
297 unsigned int wordcount)
301 spin_lock_irqsave(&pdata->dev_lock, flags);
303 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
305 *buf++ = swab32(__smsc911x_reg_read(pdata,
310 if (pdata->config.flags & SMSC911X_USE_32BIT) {
311 readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
315 if (pdata->config.flags & SMSC911X_USE_16BIT) {
317 *buf++ = __smsc911x_reg_read(pdata, RX_DATA_FIFO);
323 spin_unlock_irqrestore(&pdata->dev_lock, flags);
326 /* Reads a packet out of the RX_DATA_FIFO - shifted version */
328 smsc911x_rx_readfifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
329 unsigned int wordcount)
333 spin_lock_irqsave(&pdata->dev_lock, flags);
335 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
337 *buf++ = swab32(__smsc911x_reg_read_shift(pdata,
342 if (pdata->config.flags & SMSC911X_USE_32BIT) {
343 readsl(pdata->ioaddr + __smsc_shift(pdata,
344 RX_DATA_FIFO), buf, wordcount);
348 if (pdata->config.flags & SMSC911X_USE_16BIT) {
350 *buf++ = __smsc911x_reg_read_shift(pdata,
357 spin_unlock_irqrestore(&pdata->dev_lock, flags);
360 /* waits for MAC not busy, with timeout. Only called by smsc911x_mac_read
361 * and smsc911x_mac_write, so assumes mac_lock is held */
362 static int smsc911x_mac_complete(struct smsc911x_data *pdata)
367 SMSC_ASSERT_MAC_LOCK(pdata);
369 for (i = 0; i < 40; i++) {
370 val = smsc911x_reg_read(pdata, MAC_CSR_CMD);
371 if (!(val & MAC_CSR_CMD_CSR_BUSY_))
374 SMSC_WARN(pdata, hw, "Timed out waiting for MAC not BUSY. "
375 "MAC_CSR_CMD: 0x%08X", val);
379 /* Fetches a MAC register value. Assumes mac_lock is acquired */
380 static u32 smsc911x_mac_read(struct smsc911x_data *pdata, unsigned int offset)
384 SMSC_ASSERT_MAC_LOCK(pdata);
386 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
387 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
388 SMSC_WARN(pdata, hw, "MAC busy at entry");
392 /* Send the MAC cmd */
393 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
394 MAC_CSR_CMD_CSR_BUSY_ | MAC_CSR_CMD_R_NOT_W_));
396 /* Workaround for hardware read-after-write restriction */
397 temp = smsc911x_reg_read(pdata, BYTE_TEST);
399 /* Wait for the read to complete */
400 if (likely(smsc911x_mac_complete(pdata) == 0))
401 return smsc911x_reg_read(pdata, MAC_CSR_DATA);
403 SMSC_WARN(pdata, hw, "MAC busy after read");
407 /* Set a mac register, mac_lock must be acquired before calling */
408 static void smsc911x_mac_write(struct smsc911x_data *pdata,
409 unsigned int offset, u32 val)
413 SMSC_ASSERT_MAC_LOCK(pdata);
415 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
416 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
418 "smsc911x_mac_write failed, MAC busy at entry");
422 /* Send data to write */
423 smsc911x_reg_write(pdata, MAC_CSR_DATA, val);
425 /* Write the actual data */
426 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
427 MAC_CSR_CMD_CSR_BUSY_));
429 /* Workaround for hardware read-after-write restriction */
430 temp = smsc911x_reg_read(pdata, BYTE_TEST);
432 /* Wait for the write to complete */
433 if (likely(smsc911x_mac_complete(pdata) == 0))
436 SMSC_WARN(pdata, hw, "smsc911x_mac_write failed, MAC busy after write");
439 /* Get a phy register */
440 static int smsc911x_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
442 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
447 spin_lock_irqsave(&pdata->mac_lock, flags);
449 /* Confirm MII not busy */
450 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
451 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_read???");
456 /* Set the address, index & direction (read from PHY) */
457 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6);
458 smsc911x_mac_write(pdata, MII_ACC, addr);
460 /* Wait for read to complete w/ timeout */
461 for (i = 0; i < 100; i++)
462 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
463 reg = smsc911x_mac_read(pdata, MII_DATA);
467 SMSC_WARN(pdata, hw, "Timed out waiting for MII read to finish");
471 spin_unlock_irqrestore(&pdata->mac_lock, flags);
475 /* Set a phy register */
476 static int smsc911x_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
479 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
484 spin_lock_irqsave(&pdata->mac_lock, flags);
486 /* Confirm MII not busy */
487 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
488 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_write???");
493 /* Put the data to write in the MAC */
494 smsc911x_mac_write(pdata, MII_DATA, val);
496 /* Set the address, index & direction (write to PHY) */
497 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
499 smsc911x_mac_write(pdata, MII_ACC, addr);
501 /* Wait for write to complete w/ timeout */
502 for (i = 0; i < 100; i++)
503 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
508 SMSC_WARN(pdata, hw, "Timed out waiting for MII write to finish");
512 spin_unlock_irqrestore(&pdata->mac_lock, flags);
516 /* Switch to external phy. Assumes tx and rx are stopped. */
517 static void smsc911x_phy_enable_external(struct smsc911x_data *pdata)
519 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
521 /* Disable phy clocks to the MAC */
522 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
523 hwcfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
524 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
525 udelay(10); /* Enough time for clocks to stop */
527 /* Switch to external phy */
528 hwcfg |= HW_CFG_EXT_PHY_EN_;
529 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
531 /* Enable phy clocks to the MAC */
532 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
533 hwcfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
534 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
535 udelay(10); /* Enough time for clocks to restart */
537 hwcfg |= HW_CFG_SMI_SEL_;
538 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
541 /* Autodetects and enables external phy if present on supported chips.
542 * autodetection can be overridden by specifying SMSC911X_FORCE_INTERNAL_PHY
543 * or SMSC911X_FORCE_EXTERNAL_PHY in the platform_data flags. */
544 static void smsc911x_phy_initialise_external(struct smsc911x_data *pdata)
546 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
548 if (pdata->config.flags & SMSC911X_FORCE_INTERNAL_PHY) {
549 SMSC_TRACE(pdata, hw, "Forcing internal PHY");
550 pdata->using_extphy = 0;
551 } else if (pdata->config.flags & SMSC911X_FORCE_EXTERNAL_PHY) {
552 SMSC_TRACE(pdata, hw, "Forcing external PHY");
553 smsc911x_phy_enable_external(pdata);
554 pdata->using_extphy = 1;
555 } else if (hwcfg & HW_CFG_EXT_PHY_DET_) {
556 SMSC_TRACE(pdata, hw,
557 "HW_CFG EXT_PHY_DET set, using external PHY");
558 smsc911x_phy_enable_external(pdata);
559 pdata->using_extphy = 1;
561 SMSC_TRACE(pdata, hw,
562 "HW_CFG EXT_PHY_DET clear, using internal PHY");
563 pdata->using_extphy = 0;
567 /* Fetches a tx status out of the status fifo */
568 static unsigned int smsc911x_tx_get_txstatus(struct smsc911x_data *pdata)
570 unsigned int result =
571 smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TSUSED_;
574 result = smsc911x_reg_read(pdata, TX_STATUS_FIFO);
579 /* Fetches the next rx status */
580 static unsigned int smsc911x_rx_get_rxstatus(struct smsc911x_data *pdata)
582 unsigned int result =
583 smsc911x_reg_read(pdata, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED_;
586 result = smsc911x_reg_read(pdata, RX_STATUS_FIFO);
591 #ifdef USE_PHY_WORK_AROUND
592 static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data *pdata)
599 for (tries = 0; tries < 10; tries++) {
600 unsigned int txcmd_a;
601 unsigned int txcmd_b;
603 unsigned int pktlength;
606 /* Zero-out rx packet memory */
607 memset(pdata->loopback_rx_pkt, 0, MIN_PACKET_SIZE);
609 /* Write tx packet to 118 */
610 txcmd_a = (u32)((ulong)pdata->loopback_tx_pkt & 0x03) << 16;
611 txcmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
612 txcmd_a |= MIN_PACKET_SIZE;
614 txcmd_b = MIN_PACKET_SIZE << 16 | MIN_PACKET_SIZE;
616 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_a);
617 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_b);
619 bufp = (ulong)pdata->loopback_tx_pkt & (~0x3);
620 wrsz = MIN_PACKET_SIZE + 3;
621 wrsz += (u32)((ulong)pdata->loopback_tx_pkt & 0x3);
624 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
626 /* Wait till transmit is done */
630 status = smsc911x_tx_get_txstatus(pdata);
631 } while ((i--) && (!status));
635 "Failed to transmit during loopback test");
638 if (status & TX_STS_ES_) {
640 "Transmit encountered errors during loopback test");
644 /* Wait till receive is done */
648 status = smsc911x_rx_get_rxstatus(pdata);
649 } while ((i--) && (!status));
653 "Failed to receive during loopback test");
656 if (status & RX_STS_ES_) {
658 "Receive encountered errors during loopback test");
662 pktlength = ((status & 0x3FFF0000UL) >> 16);
663 bufp = (ulong)pdata->loopback_rx_pkt;
664 rdsz = pktlength + 3;
665 rdsz += (u32)((ulong)pdata->loopback_rx_pkt & 0x3);
668 pdata->ops->rx_readfifo(pdata, (unsigned int *)bufp, rdsz);
670 if (pktlength != (MIN_PACKET_SIZE + 4)) {
671 SMSC_WARN(pdata, hw, "Unexpected packet size "
672 "during loop back test, size=%d, will retry",
677 for (j = 0; j < MIN_PACKET_SIZE; j++) {
678 if (pdata->loopback_tx_pkt[j]
679 != pdata->loopback_rx_pkt[j]) {
685 SMSC_TRACE(pdata, hw, "Successfully verified "
689 SMSC_WARN(pdata, hw, "Data mismatch "
690 "during loop back test, will retry");
698 static int smsc911x_phy_reset(struct smsc911x_data *pdata)
700 struct phy_device *phy_dev = pdata->phy_dev;
702 unsigned int i = 100000;
705 BUG_ON(!phy_dev->bus);
707 SMSC_TRACE(pdata, hw, "Performing PHY BCR Reset");
708 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, BMCR_RESET);
711 temp = smsc911x_mii_read(phy_dev->bus, phy_dev->addr,
713 } while ((i--) && (temp & BMCR_RESET));
715 if (temp & BMCR_RESET) {
716 SMSC_WARN(pdata, hw, "PHY reset failed to complete");
719 /* Extra delay required because the phy may not be completed with
720 * its reset when BMCR_RESET is cleared. Specs say 256 uS is
721 * enough delay but using 1ms here to be safe */
727 static int smsc911x_phy_loopbacktest(struct net_device *dev)
729 struct smsc911x_data *pdata = netdev_priv(dev);
730 struct phy_device *phy_dev = pdata->phy_dev;
735 /* Initialise tx packet using broadcast destination address */
736 memset(pdata->loopback_tx_pkt, 0xff, ETH_ALEN);
738 /* Use incrementing source address */
739 for (i = 6; i < 12; i++)
740 pdata->loopback_tx_pkt[i] = (char)i;
742 /* Set length type field */
743 pdata->loopback_tx_pkt[12] = 0x00;
744 pdata->loopback_tx_pkt[13] = 0x00;
746 for (i = 14; i < MIN_PACKET_SIZE; i++)
747 pdata->loopback_tx_pkt[i] = (char)i;
749 val = smsc911x_reg_read(pdata, HW_CFG);
750 val &= HW_CFG_TX_FIF_SZ_;
752 smsc911x_reg_write(pdata, HW_CFG, val);
754 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
755 smsc911x_reg_write(pdata, RX_CFG,
756 (u32)((ulong)pdata->loopback_rx_pkt & 0x03) << 8);
758 for (i = 0; i < 10; i++) {
759 /* Set PHY to 10/FD, no ANEG, and loopback mode */
760 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR,
761 BMCR_LOOPBACK | BMCR_FULLDPLX);
763 /* Enable MAC tx/rx, FD */
764 spin_lock_irqsave(&pdata->mac_lock, flags);
765 smsc911x_mac_write(pdata, MAC_CR, MAC_CR_FDPX_
766 | MAC_CR_TXEN_ | MAC_CR_RXEN_);
767 spin_unlock_irqrestore(&pdata->mac_lock, flags);
769 if (smsc911x_phy_check_loopbackpkt(pdata) == 0) {
776 spin_lock_irqsave(&pdata->mac_lock, flags);
777 smsc911x_mac_write(pdata, MAC_CR, 0);
778 spin_unlock_irqrestore(&pdata->mac_lock, flags);
780 smsc911x_phy_reset(pdata);
784 spin_lock_irqsave(&pdata->mac_lock, flags);
785 smsc911x_mac_write(pdata, MAC_CR, 0);
786 spin_unlock_irqrestore(&pdata->mac_lock, flags);
788 /* Cancel PHY loopback mode */
789 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, 0);
791 smsc911x_reg_write(pdata, TX_CFG, 0);
792 smsc911x_reg_write(pdata, RX_CFG, 0);
796 #endif /* USE_PHY_WORK_AROUND */
798 static void smsc911x_phy_update_flowcontrol(struct smsc911x_data *pdata)
800 struct phy_device *phy_dev = pdata->phy_dev;
801 u32 afc = smsc911x_reg_read(pdata, AFC_CFG);
805 if (phy_dev->duplex == DUPLEX_FULL) {
806 u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
807 u16 rmtadv = phy_read(phy_dev, MII_LPA);
808 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
810 if (cap & FLOW_CTRL_RX)
815 if (cap & FLOW_CTRL_TX)
820 SMSC_TRACE(pdata, hw, "rx pause %s, tx pause %s",
821 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
822 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
824 SMSC_TRACE(pdata, hw, "half duplex");
829 spin_lock_irqsave(&pdata->mac_lock, flags);
830 smsc911x_mac_write(pdata, FLOW, flow);
831 spin_unlock_irqrestore(&pdata->mac_lock, flags);
833 smsc911x_reg_write(pdata, AFC_CFG, afc);
836 /* Update link mode if anything has changed. Called periodically when the
837 * PHY is in polling mode, even if nothing has changed. */
838 static void smsc911x_phy_adjust_link(struct net_device *dev)
840 struct smsc911x_data *pdata = netdev_priv(dev);
841 struct phy_device *phy_dev = pdata->phy_dev;
845 if (phy_dev->duplex != pdata->last_duplex) {
847 SMSC_TRACE(pdata, hw, "duplex state has changed");
849 spin_lock_irqsave(&pdata->mac_lock, flags);
850 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
851 if (phy_dev->duplex) {
852 SMSC_TRACE(pdata, hw,
853 "configuring for full duplex mode");
854 mac_cr |= MAC_CR_FDPX_;
856 SMSC_TRACE(pdata, hw,
857 "configuring for half duplex mode");
858 mac_cr &= ~MAC_CR_FDPX_;
860 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
861 spin_unlock_irqrestore(&pdata->mac_lock, flags);
863 smsc911x_phy_update_flowcontrol(pdata);
864 pdata->last_duplex = phy_dev->duplex;
867 carrier = netif_carrier_ok(dev);
868 if (carrier != pdata->last_carrier) {
869 SMSC_TRACE(pdata, hw, "carrier state has changed");
871 SMSC_TRACE(pdata, hw, "configuring for carrier OK");
872 if ((pdata->gpio_orig_setting & GPIO_CFG_LED1_EN_) &&
873 (!pdata->using_extphy)) {
874 /* Restore original GPIO configuration */
875 pdata->gpio_setting = pdata->gpio_orig_setting;
876 smsc911x_reg_write(pdata, GPIO_CFG,
877 pdata->gpio_setting);
880 SMSC_TRACE(pdata, hw, "configuring for no carrier");
881 /* Check global setting that LED1
882 * usage is 10/100 indicator */
883 pdata->gpio_setting = smsc911x_reg_read(pdata,
885 if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_) &&
886 (!pdata->using_extphy)) {
887 /* Force 10/100 LED off, after saving
888 * original GPIO configuration */
889 pdata->gpio_orig_setting = pdata->gpio_setting;
891 pdata->gpio_setting &= ~GPIO_CFG_LED1_EN_;
892 pdata->gpio_setting |= (GPIO_CFG_GPIOBUF0_
895 smsc911x_reg_write(pdata, GPIO_CFG,
896 pdata->gpio_setting);
899 pdata->last_carrier = carrier;
903 static int smsc911x_mii_probe(struct net_device *dev)
905 struct smsc911x_data *pdata = netdev_priv(dev);
906 struct phy_device *phydev = NULL;
909 /* find the first phy */
910 phydev = phy_find_first(pdata->mii_bus);
912 netdev_err(dev, "no PHY found\n");
916 SMSC_TRACE(pdata, probe, "PHY: addr %d, phy_id 0x%08X",
917 phydev->addr, phydev->phy_id);
919 ret = phy_connect_direct(dev, phydev,
920 &smsc911x_phy_adjust_link, 0,
921 pdata->config.phy_interface);
924 netdev_err(dev, "Could not attach to PHY\n");
929 "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
930 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
932 /* mask with MAC supported features */
933 phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
934 SUPPORTED_Asym_Pause);
935 phydev->advertising = phydev->supported;
937 pdata->phy_dev = phydev;
938 pdata->last_duplex = -1;
939 pdata->last_carrier = -1;
941 #ifdef USE_PHY_WORK_AROUND
942 if (smsc911x_phy_loopbacktest(dev) < 0) {
943 SMSC_WARN(pdata, hw, "Failed Loop Back Test");
946 SMSC_TRACE(pdata, hw, "Passed Loop Back Test");
947 #endif /* USE_PHY_WORK_AROUND */
949 SMSC_TRACE(pdata, hw, "phy initialised successfully");
953 static int __devinit smsc911x_mii_init(struct platform_device *pdev,
954 struct net_device *dev)
956 struct smsc911x_data *pdata = netdev_priv(dev);
959 pdata->mii_bus = mdiobus_alloc();
960 if (!pdata->mii_bus) {
965 pdata->mii_bus->name = SMSC_MDIONAME;
966 snprintf(pdata->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
967 pdata->mii_bus->priv = pdata;
968 pdata->mii_bus->read = smsc911x_mii_read;
969 pdata->mii_bus->write = smsc911x_mii_write;
970 pdata->mii_bus->irq = pdata->phy_irq;
971 for (i = 0; i < PHY_MAX_ADDR; ++i)
972 pdata->mii_bus->irq[i] = PHY_POLL;
974 pdata->mii_bus->parent = &pdev->dev;
976 switch (pdata->idrev & 0xFFFF0000) {
981 /* External PHY supported, try to autodetect */
982 smsc911x_phy_initialise_external(pdata);
985 SMSC_TRACE(pdata, hw, "External PHY is not supported, "
986 "using internal PHY");
987 pdata->using_extphy = 0;
991 if (!pdata->using_extphy) {
992 /* Mask all PHYs except ID 1 (internal) */
993 pdata->mii_bus->phy_mask = ~(1 << 1);
996 if (mdiobus_register(pdata->mii_bus)) {
997 SMSC_WARN(pdata, probe, "Error registering mii bus");
998 goto err_out_free_bus_2;
1001 if (smsc911x_mii_probe(dev) < 0) {
1002 SMSC_WARN(pdata, probe, "Error registering mii bus");
1003 goto err_out_unregister_bus_3;
1008 err_out_unregister_bus_3:
1009 mdiobus_unregister(pdata->mii_bus);
1011 mdiobus_free(pdata->mii_bus);
1016 /* Gets the number of tx statuses in the fifo */
1017 static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data *pdata)
1019 return (smsc911x_reg_read(pdata, TX_FIFO_INF)
1020 & TX_FIFO_INF_TSUSED_) >> 16;
1023 /* Reads tx statuses and increments counters where necessary */
1024 static void smsc911x_tx_update_txcounters(struct net_device *dev)
1026 struct smsc911x_data *pdata = netdev_priv(dev);
1027 unsigned int tx_stat;
1029 while ((tx_stat = smsc911x_tx_get_txstatus(pdata)) != 0) {
1030 if (unlikely(tx_stat & 0x80000000)) {
1031 /* In this driver the packet tag is used as the packet
1032 * length. Since a packet length can never reach the
1033 * size of 0x8000, this bit is reserved. It is worth
1034 * noting that the "reserved bit" in the warning above
1035 * does not reference a hardware defined reserved bit
1036 * but rather a driver defined one.
1038 SMSC_WARN(pdata, hw, "Packet tag reserved bit is high");
1040 if (unlikely(tx_stat & TX_STS_ES_)) {
1041 dev->stats.tx_errors++;
1043 dev->stats.tx_packets++;
1044 dev->stats.tx_bytes += (tx_stat >> 16);
1046 if (unlikely(tx_stat & TX_STS_EXCESS_COL_)) {
1047 dev->stats.collisions += 16;
1048 dev->stats.tx_aborted_errors += 1;
1050 dev->stats.collisions +=
1051 ((tx_stat >> 3) & 0xF);
1053 if (unlikely(tx_stat & TX_STS_LOST_CARRIER_))
1054 dev->stats.tx_carrier_errors += 1;
1055 if (unlikely(tx_stat & TX_STS_LATE_COL_)) {
1056 dev->stats.collisions++;
1057 dev->stats.tx_aborted_errors++;
1063 /* Increments the Rx error counters */
1065 smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
1069 if (unlikely(rxstat & RX_STS_ES_)) {
1070 dev->stats.rx_errors++;
1071 if (unlikely(rxstat & RX_STS_CRC_ERR_)) {
1072 dev->stats.rx_crc_errors++;
1076 if (likely(!crc_err)) {
1077 if (unlikely((rxstat & RX_STS_FRAME_TYPE_) &&
1078 (rxstat & RX_STS_LENGTH_ERR_)))
1079 dev->stats.rx_length_errors++;
1080 if (rxstat & RX_STS_MCAST_)
1081 dev->stats.multicast++;
1085 /* Quickly dumps bad packets */
1087 smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int pktbytes)
1089 unsigned int pktwords = (pktbytes + NET_IP_ALIGN + 3) >> 2;
1091 if (likely(pktwords >= 4)) {
1092 unsigned int timeout = 500;
1094 smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
1097 val = smsc911x_reg_read(pdata, RX_DP_CTRL);
1098 } while ((val & RX_DP_CTRL_RX_FFWD_) && --timeout);
1100 if (unlikely(timeout == 0))
1101 SMSC_WARN(pdata, hw, "Timed out waiting for "
1102 "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val);
1106 temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
1110 /* NAPI poll function */
1111 static int smsc911x_poll(struct napi_struct *napi, int budget)
1113 struct smsc911x_data *pdata =
1114 container_of(napi, struct smsc911x_data, napi);
1115 struct net_device *dev = pdata->dev;
1118 while (npackets < budget) {
1119 unsigned int pktlength;
1120 unsigned int pktwords;
1121 struct sk_buff *skb;
1122 unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);
1126 /* We processed all packets available. Tell NAPI it can
1127 * stop polling then re-enable rx interrupts */
1128 smsc911x_reg_write(pdata, INT_STS, INT_STS_RSFL_);
1129 napi_complete(napi);
1130 temp = smsc911x_reg_read(pdata, INT_EN);
1131 temp |= INT_EN_RSFL_EN_;
1132 smsc911x_reg_write(pdata, INT_EN, temp);
1136 /* Count packet for NAPI scheduling, even if it has an error.
1137 * Error packets still require cycles to discard */
1140 pktlength = ((rxstat & 0x3FFF0000) >> 16);
1141 pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
1142 smsc911x_rx_counterrors(dev, rxstat);
1144 if (unlikely(rxstat & RX_STS_ES_)) {
1145 SMSC_WARN(pdata, rx_err,
1146 "Discarding packet with error bit set");
1147 /* Packet has an error, discard it and continue with
1149 smsc911x_rx_fastforward(pdata, pktwords);
1150 dev->stats.rx_dropped++;
1154 skb = netdev_alloc_skb(dev, pktlength + NET_IP_ALIGN);
1155 if (unlikely(!skb)) {
1156 SMSC_WARN(pdata, rx_err,
1157 "Unable to allocate skb for rx packet");
1158 /* Drop the packet and stop this polling iteration */
1159 smsc911x_rx_fastforward(pdata, pktwords);
1160 dev->stats.rx_dropped++;
1164 skb->data = skb->head;
1165 skb_reset_tail_pointer(skb);
1167 /* Align IP on 16B boundary */
1168 skb_reserve(skb, NET_IP_ALIGN);
1169 skb_put(skb, pktlength - 4);
1170 pdata->ops->rx_readfifo(pdata,
1171 (unsigned int *)skb->head, pktwords);
1172 skb->protocol = eth_type_trans(skb, dev);
1173 skb_checksum_none_assert(skb);
1174 netif_receive_skb(skb);
1176 /* Update counters */
1177 dev->stats.rx_packets++;
1178 dev->stats.rx_bytes += (pktlength - 4);
1181 /* Return total received packets */
1185 /* Returns hash bit number for given MAC address
1187 * 01 00 5E 00 00 01 -> returns bit number 31 */
1188 static unsigned int smsc911x_hash(char addr[ETH_ALEN])
1190 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
1193 static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
1195 /* Performs the multicast & mac_cr update. This is called when
1196 * safe on the current hardware, and with the mac_lock held */
1197 unsigned int mac_cr;
1199 SMSC_ASSERT_MAC_LOCK(pdata);
1201 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1202 mac_cr |= pdata->set_bits_mask;
1203 mac_cr &= ~(pdata->clear_bits_mask);
1204 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1205 smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
1206 smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
1207 SMSC_TRACE(pdata, hw, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
1208 mac_cr, pdata->hashhi, pdata->hashlo);
1211 static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
1213 unsigned int mac_cr;
1215 /* This function is only called for older LAN911x devices
1216 * (revA or revB), where MAC_CR, HASHH and HASHL should not
1217 * be modified during Rx - newer devices immediately update the
1220 * This is called from interrupt context */
1222 spin_lock(&pdata->mac_lock);
1224 /* Check Rx has stopped */
1225 if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
1226 SMSC_WARN(pdata, drv, "Rx not stopped");
1228 /* Perform the update - safe to do now Rx has stopped */
1229 smsc911x_rx_multicast_update(pdata);
1232 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1233 mac_cr |= MAC_CR_RXEN_;
1234 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1236 pdata->multicast_update_pending = 0;
1238 spin_unlock(&pdata->mac_lock);
1241 static int smsc911x_soft_reset(struct smsc911x_data *pdata)
1243 unsigned int timeout;
1246 /* Reset the LAN911x */
1247 smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
1251 temp = smsc911x_reg_read(pdata, HW_CFG);
1252 } while ((--timeout) && (temp & HW_CFG_SRST_));
1254 if (unlikely(temp & HW_CFG_SRST_)) {
1255 SMSC_WARN(pdata, drv, "Failed to complete reset");
1261 /* Sets the device MAC address to dev_addr, called with mac_lock held */
1263 smsc911x_set_hw_mac_address(struct smsc911x_data *pdata, u8 dev_addr[6])
1265 u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
1266 u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
1267 (dev_addr[1] << 8) | dev_addr[0];
1269 SMSC_ASSERT_MAC_LOCK(pdata);
1271 smsc911x_mac_write(pdata, ADDRH, mac_high16);
1272 smsc911x_mac_write(pdata, ADDRL, mac_low32);
1275 static int smsc911x_open(struct net_device *dev)
1277 struct smsc911x_data *pdata = netdev_priv(dev);
1278 unsigned int timeout;
1280 unsigned int intcfg;
1282 /* if the phy is not yet registered, retry later*/
1283 if (!pdata->phy_dev) {
1284 SMSC_WARN(pdata, hw, "phy_dev is NULL");
1288 if (!is_valid_ether_addr(dev->dev_addr)) {
1289 SMSC_WARN(pdata, hw, "dev_addr is not a valid MAC address");
1290 return -EADDRNOTAVAIL;
1293 /* Reset the LAN911x */
1294 if (smsc911x_soft_reset(pdata)) {
1295 SMSC_WARN(pdata, hw, "soft reset failed");
1299 smsc911x_reg_write(pdata, HW_CFG, 0x00050000);
1300 smsc911x_reg_write(pdata, AFC_CFG, 0x006E3740);
1302 /* Increase the legal frame size of VLAN tagged frames to 1522 bytes */
1303 spin_lock_irq(&pdata->mac_lock);
1304 smsc911x_mac_write(pdata, VLAN1, ETH_P_8021Q);
1305 spin_unlock_irq(&pdata->mac_lock);
1307 /* Make sure EEPROM has finished loading before setting GPIO_CFG */
1309 while ((smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) &&
1314 if (unlikely(timeout == 0))
1315 SMSC_WARN(pdata, ifup,
1316 "Timed out waiting for EEPROM busy bit to clear");
1318 smsc911x_reg_write(pdata, GPIO_CFG, 0x70070000);
1320 /* The soft reset above cleared the device's MAC address,
1321 * restore it from local copy (set in probe) */
1322 spin_lock_irq(&pdata->mac_lock);
1323 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
1324 spin_unlock_irq(&pdata->mac_lock);
1326 /* Initialise irqs, but leave all sources disabled */
1327 smsc911x_reg_write(pdata, INT_EN, 0);
1328 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
1330 /* Set interrupt deassertion to 100uS */
1331 intcfg = ((10 << 24) | INT_CFG_IRQ_EN_);
1333 if (pdata->config.irq_polarity) {
1334 SMSC_TRACE(pdata, ifup, "irq polarity: active high");
1335 intcfg |= INT_CFG_IRQ_POL_;
1337 SMSC_TRACE(pdata, ifup, "irq polarity: active low");
1340 if (pdata->config.irq_type) {
1341 SMSC_TRACE(pdata, ifup, "irq type: push-pull");
1342 intcfg |= INT_CFG_IRQ_TYPE_;
1344 SMSC_TRACE(pdata, ifup, "irq type: open drain");
1347 smsc911x_reg_write(pdata, INT_CFG, intcfg);
1349 SMSC_TRACE(pdata, ifup, "Testing irq handler using IRQ %d", dev->irq);
1350 pdata->software_irq_signal = 0;
1353 temp = smsc911x_reg_read(pdata, INT_EN);
1354 temp |= INT_EN_SW_INT_EN_;
1355 smsc911x_reg_write(pdata, INT_EN, temp);
1359 if (pdata->software_irq_signal)
1364 if (!pdata->software_irq_signal) {
1365 netdev_warn(dev, "ISR failed signaling test (IRQ %d)\n",
1369 SMSC_TRACE(pdata, ifup, "IRQ handler passed test using IRQ %d",
1372 netdev_info(dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
1373 (unsigned long)pdata->ioaddr, dev->irq);
1375 /* Reset the last known duplex and carrier */
1376 pdata->last_duplex = -1;
1377 pdata->last_carrier = -1;
1379 /* Bring the PHY up */
1380 phy_start(pdata->phy_dev);
1382 temp = smsc911x_reg_read(pdata, HW_CFG);
1383 /* Preserve TX FIFO size and external PHY configuration */
1384 temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
1386 smsc911x_reg_write(pdata, HW_CFG, temp);
1388 temp = smsc911x_reg_read(pdata, FIFO_INT);
1389 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1390 temp &= ~(FIFO_INT_RX_STS_LEVEL_);
1391 smsc911x_reg_write(pdata, FIFO_INT, temp);
1393 /* set RX Data offset to 2 bytes for alignment */
1394 smsc911x_reg_write(pdata, RX_CFG, (2 << 8));
1396 /* enable NAPI polling before enabling RX interrupts */
1397 napi_enable(&pdata->napi);
1399 temp = smsc911x_reg_read(pdata, INT_EN);
1400 temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_RXSTOP_INT_EN_);
1401 smsc911x_reg_write(pdata, INT_EN, temp);
1403 spin_lock_irq(&pdata->mac_lock);
1404 temp = smsc911x_mac_read(pdata, MAC_CR);
1405 temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
1406 smsc911x_mac_write(pdata, MAC_CR, temp);
1407 spin_unlock_irq(&pdata->mac_lock);
1409 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
1411 netif_start_queue(dev);
1415 /* Entry point for stopping the interface */
1416 static int smsc911x_stop(struct net_device *dev)
1418 struct smsc911x_data *pdata = netdev_priv(dev);
1421 /* Disable all device interrupts */
1422 temp = smsc911x_reg_read(pdata, INT_CFG);
1423 temp &= ~INT_CFG_IRQ_EN_;
1424 smsc911x_reg_write(pdata, INT_CFG, temp);
1426 /* Stop Tx and Rx polling */
1427 netif_stop_queue(dev);
1428 napi_disable(&pdata->napi);
1430 /* At this point all Rx and Tx activity is stopped */
1431 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1432 smsc911x_tx_update_txcounters(dev);
1434 /* Bring the PHY down */
1436 phy_stop(pdata->phy_dev);
1438 SMSC_TRACE(pdata, ifdown, "Interface stopped");
1442 /* Entry point for transmitting a packet */
1443 static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1445 struct smsc911x_data *pdata = netdev_priv(dev);
1446 unsigned int freespace;
1447 unsigned int tx_cmd_a;
1448 unsigned int tx_cmd_b;
1453 freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;
1455 if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
1456 SMSC_WARN(pdata, tx_err,
1457 "Tx data fifo low, space available: %d", freespace);
1459 /* Word alignment adjustment */
1460 tx_cmd_a = (u32)((ulong)skb->data & 0x03) << 16;
1461 tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1462 tx_cmd_a |= (unsigned int)skb->len;
1464 tx_cmd_b = ((unsigned int)skb->len) << 16;
1465 tx_cmd_b |= (unsigned int)skb->len;
1467 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_a);
1468 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_b);
1470 bufp = (ulong)skb->data & (~0x3);
1471 wrsz = (u32)skb->len + 3;
1472 wrsz += (u32)((ulong)skb->data & 0x3);
1475 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
1476 freespace -= (skb->len + 32);
1479 if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
1480 smsc911x_tx_update_txcounters(dev);
1482 if (freespace < TX_FIFO_LOW_THRESHOLD) {
1483 netif_stop_queue(dev);
1484 temp = smsc911x_reg_read(pdata, FIFO_INT);
1487 smsc911x_reg_write(pdata, FIFO_INT, temp);
1490 return NETDEV_TX_OK;
1493 /* Entry point for getting status counters */
1494 static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
1496 struct smsc911x_data *pdata = netdev_priv(dev);
1497 smsc911x_tx_update_txcounters(dev);
1498 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1502 /* Entry point for setting addressing modes */
1503 static void smsc911x_set_multicast_list(struct net_device *dev)
1505 struct smsc911x_data *pdata = netdev_priv(dev);
1506 unsigned long flags;
1508 if (dev->flags & IFF_PROMISC) {
1509 /* Enabling promiscuous mode */
1510 pdata->set_bits_mask = MAC_CR_PRMS_;
1511 pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1514 } else if (dev->flags & IFF_ALLMULTI) {
1515 /* Enabling all multicast mode */
1516 pdata->set_bits_mask = MAC_CR_MCPAS_;
1517 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
1520 } else if (!netdev_mc_empty(dev)) {
1521 /* Enabling specific multicast addresses */
1522 unsigned int hash_high = 0;
1523 unsigned int hash_low = 0;
1524 struct netdev_hw_addr *ha;
1526 pdata->set_bits_mask = MAC_CR_HPFILT_;
1527 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1529 netdev_for_each_mc_addr(ha, dev) {
1530 unsigned int bitnum = smsc911x_hash(ha->addr);
1531 unsigned int mask = 0x01 << (bitnum & 0x1F);
1539 pdata->hashhi = hash_high;
1540 pdata->hashlo = hash_low;
1542 /* Enabling local MAC address only */
1543 pdata->set_bits_mask = 0;
1544 pdata->clear_bits_mask =
1545 (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1550 spin_lock_irqsave(&pdata->mac_lock, flags);
1552 if (pdata->generation <= 1) {
1553 /* Older hardware revision - cannot change these flags while
1555 if (!pdata->multicast_update_pending) {
1557 SMSC_TRACE(pdata, hw, "scheduling mcast update");
1558 pdata->multicast_update_pending = 1;
1560 /* Request the hardware to stop, then perform the
1561 * update when we get an RX_STOP interrupt */
1562 temp = smsc911x_mac_read(pdata, MAC_CR);
1563 temp &= ~(MAC_CR_RXEN_);
1564 smsc911x_mac_write(pdata, MAC_CR, temp);
1566 /* There is another update pending, this should now
1567 * use the newer values */
1570 /* Newer hardware revision - can write immediately */
1571 smsc911x_rx_multicast_update(pdata);
1574 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1577 static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
1579 struct net_device *dev = dev_id;
1580 struct smsc911x_data *pdata = netdev_priv(dev);
1581 u32 intsts = smsc911x_reg_read(pdata, INT_STS);
1582 u32 inten = smsc911x_reg_read(pdata, INT_EN);
1583 int serviced = IRQ_NONE;
1586 if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
1587 temp = smsc911x_reg_read(pdata, INT_EN);
1588 temp &= (~INT_EN_SW_INT_EN_);
1589 smsc911x_reg_write(pdata, INT_EN, temp);
1590 smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
1591 pdata->software_irq_signal = 1;
1593 serviced = IRQ_HANDLED;
1596 if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
1597 /* Called when there is a multicast update scheduled and
1598 * it is now safe to complete the update */
1599 SMSC_TRACE(pdata, intr, "RX Stop interrupt");
1600 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
1601 if (pdata->multicast_update_pending)
1602 smsc911x_rx_multicast_update_workaround(pdata);
1603 serviced = IRQ_HANDLED;
1606 if (intsts & inten & INT_STS_TDFA_) {
1607 temp = smsc911x_reg_read(pdata, FIFO_INT);
1608 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1609 smsc911x_reg_write(pdata, FIFO_INT, temp);
1610 smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
1611 netif_wake_queue(dev);
1612 serviced = IRQ_HANDLED;
1615 if (unlikely(intsts & inten & INT_STS_RXE_)) {
1616 SMSC_TRACE(pdata, intr, "RX Error interrupt");
1617 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
1618 serviced = IRQ_HANDLED;
1621 if (likely(intsts & inten & INT_STS_RSFL_)) {
1622 if (likely(napi_schedule_prep(&pdata->napi))) {
1623 /* Disable Rx interrupts */
1624 temp = smsc911x_reg_read(pdata, INT_EN);
1625 temp &= (~INT_EN_RSFL_EN_);
1626 smsc911x_reg_write(pdata, INT_EN, temp);
1627 /* Schedule a NAPI poll */
1628 __napi_schedule(&pdata->napi);
1630 SMSC_WARN(pdata, rx_err, "napi_schedule_prep failed");
1632 serviced = IRQ_HANDLED;
1638 #ifdef CONFIG_NET_POLL_CONTROLLER
1639 static void smsc911x_poll_controller(struct net_device *dev)
1641 disable_irq(dev->irq);
1642 smsc911x_irqhandler(0, dev);
1643 enable_irq(dev->irq);
1645 #endif /* CONFIG_NET_POLL_CONTROLLER */
1647 static int smsc911x_set_mac_address(struct net_device *dev, void *p)
1649 struct smsc911x_data *pdata = netdev_priv(dev);
1650 struct sockaddr *addr = p;
1652 /* On older hardware revisions we cannot change the mac address
1653 * registers while receiving data. Newer devices can safely change
1654 * this at any time. */
1655 if (pdata->generation <= 1 && netif_running(dev))
1658 if (!is_valid_ether_addr(addr->sa_data))
1659 return -EADDRNOTAVAIL;
1661 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1663 spin_lock_irq(&pdata->mac_lock);
1664 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
1665 spin_unlock_irq(&pdata->mac_lock);
1667 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
1672 /* Standard ioctls for mii-tool */
1673 static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1675 struct smsc911x_data *pdata = netdev_priv(dev);
1677 if (!netif_running(dev) || !pdata->phy_dev)
1680 return phy_mii_ioctl(pdata->phy_dev, ifr, cmd);
1684 smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1686 struct smsc911x_data *pdata = netdev_priv(dev);
1690 return phy_ethtool_gset(pdata->phy_dev, cmd);
1694 smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1696 struct smsc911x_data *pdata = netdev_priv(dev);
1698 return phy_ethtool_sset(pdata->phy_dev, cmd);
1701 static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
1702 struct ethtool_drvinfo *info)
1704 strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
1705 strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
1706 strlcpy(info->bus_info, dev_name(dev->dev.parent),
1707 sizeof(info->bus_info));
1710 static int smsc911x_ethtool_nwayreset(struct net_device *dev)
1712 struct smsc911x_data *pdata = netdev_priv(dev);
1714 return phy_start_aneg(pdata->phy_dev);
1717 static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
1719 struct smsc911x_data *pdata = netdev_priv(dev);
1720 return pdata->msg_enable;
1723 static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
1725 struct smsc911x_data *pdata = netdev_priv(dev);
1726 pdata->msg_enable = level;
1729 static int smsc911x_ethtool_getregslen(struct net_device *dev)
1731 return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
1736 smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
1739 struct smsc911x_data *pdata = netdev_priv(dev);
1740 struct phy_device *phy_dev = pdata->phy_dev;
1741 unsigned long flags;
1746 regs->version = pdata->idrev;
1747 for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
1748 data[j++] = smsc911x_reg_read(pdata, i);
1750 for (i = MAC_CR; i <= WUCSR; i++) {
1751 spin_lock_irqsave(&pdata->mac_lock, flags);
1752 data[j++] = smsc911x_mac_read(pdata, i);
1753 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1756 for (i = 0; i <= 31; i++)
1757 data[j++] = smsc911x_mii_read(phy_dev->bus, phy_dev->addr, i);
1760 static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
1762 unsigned int temp = smsc911x_reg_read(pdata, GPIO_CFG);
1763 temp &= ~GPIO_CFG_EEPR_EN_;
1764 smsc911x_reg_write(pdata, GPIO_CFG, temp);
1768 static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
1773 SMSC_TRACE(pdata, drv, "op 0x%08x", op);
1774 if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
1775 SMSC_WARN(pdata, drv, "Busy at start");
1779 e2cmd = op | E2P_CMD_EPC_BUSY_;
1780 smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
1784 e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
1785 } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
1788 SMSC_TRACE(pdata, drv, "TIMED OUT");
1792 if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
1793 SMSC_TRACE(pdata, drv, "Error occurred during eeprom operation");
1800 static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
1801 u8 address, u8 *data)
1803 u32 op = E2P_CMD_EPC_CMD_READ_ | address;
1806 SMSC_TRACE(pdata, drv, "address 0x%x", address);
1807 ret = smsc911x_eeprom_send_cmd(pdata, op);
1810 data[address] = smsc911x_reg_read(pdata, E2P_DATA);
1815 static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
1816 u8 address, u8 data)
1818 u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
1822 SMSC_TRACE(pdata, drv, "address 0x%x, data 0x%x", address, data);
1823 ret = smsc911x_eeprom_send_cmd(pdata, op);
1826 op = E2P_CMD_EPC_CMD_WRITE_ | address;
1827 smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
1829 /* Workaround for hardware read-after-write restriction */
1830 temp = smsc911x_reg_read(pdata, BYTE_TEST);
1832 ret = smsc911x_eeprom_send_cmd(pdata, op);
1838 static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
1840 return SMSC911X_EEPROM_SIZE;
1843 static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
1844 struct ethtool_eeprom *eeprom, u8 *data)
1846 struct smsc911x_data *pdata = netdev_priv(dev);
1847 u8 eeprom_data[SMSC911X_EEPROM_SIZE];
1851 smsc911x_eeprom_enable_access(pdata);
1853 len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
1854 for (i = 0; i < len; i++) {
1855 int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
1862 memcpy(data, &eeprom_data[eeprom->offset], len);
1867 static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
1868 struct ethtool_eeprom *eeprom, u8 *data)
1871 struct smsc911x_data *pdata = netdev_priv(dev);
1873 smsc911x_eeprom_enable_access(pdata);
1874 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
1875 ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
1876 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
1878 /* Single byte write, according to man page */
1884 static const struct ethtool_ops smsc911x_ethtool_ops = {
1885 .get_settings = smsc911x_ethtool_getsettings,
1886 .set_settings = smsc911x_ethtool_setsettings,
1887 .get_link = ethtool_op_get_link,
1888 .get_drvinfo = smsc911x_ethtool_getdrvinfo,
1889 .nway_reset = smsc911x_ethtool_nwayreset,
1890 .get_msglevel = smsc911x_ethtool_getmsglevel,
1891 .set_msglevel = smsc911x_ethtool_setmsglevel,
1892 .get_regs_len = smsc911x_ethtool_getregslen,
1893 .get_regs = smsc911x_ethtool_getregs,
1894 .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
1895 .get_eeprom = smsc911x_ethtool_get_eeprom,
1896 .set_eeprom = smsc911x_ethtool_set_eeprom,
1899 static const struct net_device_ops smsc911x_netdev_ops = {
1900 .ndo_open = smsc911x_open,
1901 .ndo_stop = smsc911x_stop,
1902 .ndo_start_xmit = smsc911x_hard_start_xmit,
1903 .ndo_get_stats = smsc911x_get_stats,
1904 .ndo_set_multicast_list = smsc911x_set_multicast_list,
1905 .ndo_do_ioctl = smsc911x_do_ioctl,
1906 .ndo_change_mtu = eth_change_mtu,
1907 .ndo_validate_addr = eth_validate_addr,
1908 .ndo_set_mac_address = smsc911x_set_mac_address,
1909 #ifdef CONFIG_NET_POLL_CONTROLLER
1910 .ndo_poll_controller = smsc911x_poll_controller,
1914 /* copies the current mac address from hardware to dev->dev_addr */
1915 static void __devinit smsc911x_read_mac_address(struct net_device *dev)
1917 struct smsc911x_data *pdata = netdev_priv(dev);
1918 u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
1919 u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
1921 dev->dev_addr[0] = (u8)(mac_low32);
1922 dev->dev_addr[1] = (u8)(mac_low32 >> 8);
1923 dev->dev_addr[2] = (u8)(mac_low32 >> 16);
1924 dev->dev_addr[3] = (u8)(mac_low32 >> 24);
1925 dev->dev_addr[4] = (u8)(mac_high16);
1926 dev->dev_addr[5] = (u8)(mac_high16 >> 8);
1929 /* Initializing private device structures, only called from probe */
1930 static int __devinit smsc911x_init(struct net_device *dev)
1932 struct smsc911x_data *pdata = netdev_priv(dev);
1933 unsigned int byte_test;
1935 SMSC_TRACE(pdata, probe, "Driver Parameters:");
1936 SMSC_TRACE(pdata, probe, "LAN base: 0x%08lX",
1937 (unsigned long)pdata->ioaddr);
1938 SMSC_TRACE(pdata, probe, "IRQ: %d", dev->irq);
1939 SMSC_TRACE(pdata, probe, "PHY will be autodetected.");
1941 spin_lock_init(&pdata->dev_lock);
1942 spin_lock_init(&pdata->mac_lock);
1944 if (pdata->ioaddr == 0) {
1945 SMSC_WARN(pdata, probe, "pdata->ioaddr: 0x00000000");
1949 /* Check byte ordering */
1950 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1951 SMSC_TRACE(pdata, probe, "BYTE_TEST: 0x%08X", byte_test);
1952 if (byte_test == 0x43218765) {
1953 SMSC_TRACE(pdata, probe, "BYTE_TEST looks swapped, "
1954 "applying WORD_SWAP");
1955 smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
1957 /* 1 dummy read of BYTE_TEST is needed after a write to
1958 * WORD_SWAP before its contents are valid */
1959 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1961 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1964 if (byte_test != 0x87654321) {
1965 SMSC_WARN(pdata, drv, "BYTE_TEST: 0x%08X", byte_test);
1966 if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
1967 SMSC_WARN(pdata, probe,
1968 "top 16 bits equal to bottom 16 bits");
1969 SMSC_TRACE(pdata, probe,
1970 "This may mean the chip is set "
1971 "for 32 bit while the bus is reading 16 bit");
1976 /* Default generation to zero (all workarounds apply) */
1977 pdata->generation = 0;
1979 pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
1980 switch (pdata->idrev & 0xFFFF0000) {
1985 /* LAN911[5678] family */
1986 pdata->generation = pdata->idrev & 0x0000FFFF;
1993 /* LAN921[5678] family */
1994 pdata->generation = 3;
2001 /* LAN9210/LAN9211/LAN9220/LAN9221 */
2002 pdata->generation = 4;
2006 SMSC_WARN(pdata, probe, "LAN911x not identified, idrev: 0x%08X",
2011 SMSC_TRACE(pdata, probe,
2012 "LAN911x identified, idrev: 0x%08X, generation: %d",
2013 pdata->idrev, pdata->generation);
2015 if (pdata->generation == 0)
2016 SMSC_WARN(pdata, probe,
2017 "This driver is not intended for this chip revision");
2019 /* workaround for platforms without an eeprom, where the mac address
2020 * is stored elsewhere and set by the bootloader. This saves the
2021 * mac address before resetting the device */
2022 if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS) {
2023 spin_lock_irq(&pdata->mac_lock);
2024 smsc911x_read_mac_address(dev);
2025 spin_unlock_irq(&pdata->mac_lock);
2028 /* Reset the LAN911x */
2029 if (smsc911x_soft_reset(pdata))
2032 /* Disable all interrupt sources until we bring the device up */
2033 smsc911x_reg_write(pdata, INT_EN, 0);
2036 dev->flags |= IFF_MULTICAST;
2037 netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
2038 dev->netdev_ops = &smsc911x_netdev_ops;
2039 dev->ethtool_ops = &smsc911x_ethtool_ops;
2044 static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
2046 struct net_device *dev;
2047 struct smsc911x_data *pdata;
2048 struct resource *res;
2050 dev = platform_get_drvdata(pdev);
2052 pdata = netdev_priv(dev);
2054 BUG_ON(!pdata->ioaddr);
2055 BUG_ON(!pdata->phy_dev);
2057 SMSC_TRACE(pdata, ifdown, "Stopping driver");
2059 phy_disconnect(pdata->phy_dev);
2060 pdata->phy_dev = NULL;
2061 mdiobus_unregister(pdata->mii_bus);
2062 mdiobus_free(pdata->mii_bus);
2064 platform_set_drvdata(pdev, NULL);
2065 unregister_netdev(dev);
2066 free_irq(dev->irq, dev);
2067 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2070 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2072 release_mem_region(res->start, resource_size(res));
2074 iounmap(pdata->ioaddr);
2081 /* standard register acces */
2082 static const struct smsc911x_ops standard_smsc911x_ops = {
2083 .reg_read = __smsc911x_reg_read,
2084 .reg_write = __smsc911x_reg_write,
2085 .rx_readfifo = smsc911x_rx_readfifo,
2086 .tx_writefifo = smsc911x_tx_writefifo,
2089 /* shifted register access */
2090 static const struct smsc911x_ops shifted_smsc911x_ops = {
2091 .reg_read = __smsc911x_reg_read_shift,
2092 .reg_write = __smsc911x_reg_write_shift,
2093 .rx_readfifo = smsc911x_rx_readfifo_shift,
2094 .tx_writefifo = smsc911x_tx_writefifo_shift,
2097 static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
2099 struct net_device *dev;
2100 struct smsc911x_data *pdata;
2101 struct smsc911x_platform_config *config = pdev->dev.platform_data;
2102 struct resource *res, *irq_res;
2103 unsigned int intcfg = 0;
2104 int res_size, irq_flags;
2107 pr_info("Driver version %s\n", SMSC_DRV_VERSION);
2109 /* platform data specifies irq & dynamic bus configuration */
2110 if (!pdev->dev.platform_data) {
2111 pr_warn("platform_data not provided\n");
2116 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2119 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2121 pr_warn("Could not allocate resource\n");
2125 res_size = resource_size(res);
2127 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2129 pr_warn("Could not allocate irq resource\n");
2134 if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
2139 dev = alloc_etherdev(sizeof(struct smsc911x_data));
2141 pr_warn("Could not allocate device\n");
2143 goto out_release_io_1;
2146 SET_NETDEV_DEV(dev, &pdev->dev);
2148 pdata = netdev_priv(dev);
2150 dev->irq = irq_res->start;
2151 irq_flags = irq_res->flags & IRQF_TRIGGER_MASK;
2152 pdata->ioaddr = ioremap_nocache(res->start, res_size);
2154 /* copy config parameters across to pdata */
2155 memcpy(&pdata->config, config, sizeof(pdata->config));
2158 pdata->msg_enable = ((1 << debug) - 1);
2160 if (pdata->ioaddr == NULL) {
2161 SMSC_WARN(pdata, probe, "Error smsc911x base address invalid");
2163 goto out_free_netdev_2;
2166 /* assume standard, non-shifted, access to HW registers */
2167 pdata->ops = &standard_smsc911x_ops;
2168 /* apply the right access if shifting is needed */
2170 pdata->ops = &shifted_smsc911x_ops;
2172 retval = smsc911x_init(dev);
2174 goto out_unmap_io_3;
2176 /* configure irq polarity and type before connecting isr */
2177 if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
2178 intcfg |= INT_CFG_IRQ_POL_;
2180 if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
2181 intcfg |= INT_CFG_IRQ_TYPE_;
2183 smsc911x_reg_write(pdata, INT_CFG, intcfg);
2185 /* Ensure interrupts are globally disabled before connecting ISR */
2186 smsc911x_reg_write(pdata, INT_EN, 0);
2187 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
2189 retval = request_irq(dev->irq, smsc911x_irqhandler,
2190 irq_flags | IRQF_SHARED, dev->name, dev);
2192 SMSC_WARN(pdata, probe,
2193 "Unable to claim requested irq: %d", dev->irq);
2194 goto out_unmap_io_3;
2197 platform_set_drvdata(pdev, dev);
2199 retval = register_netdev(dev);
2201 SMSC_WARN(pdata, probe, "Error %i registering device", retval);
2202 goto out_unset_drvdata_4;
2204 SMSC_TRACE(pdata, probe,
2205 "Network interface: \"%s\"", dev->name);
2208 retval = smsc911x_mii_init(pdev, dev);
2210 SMSC_WARN(pdata, probe, "Error %i initialising mii", retval);
2211 goto out_unregister_netdev_5;
2214 spin_lock_irq(&pdata->mac_lock);
2216 /* Check if mac address has been specified when bringing interface up */
2217 if (is_valid_ether_addr(dev->dev_addr)) {
2218 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
2219 SMSC_TRACE(pdata, probe,
2220 "MAC Address is specified by configuration");
2221 } else if (is_valid_ether_addr(pdata->config.mac)) {
2222 memcpy(dev->dev_addr, pdata->config.mac, 6);
2223 SMSC_TRACE(pdata, probe,
2224 "MAC Address specified by platform data");
2226 /* Try reading mac address from device. if EEPROM is present
2227 * it will already have been set */
2230 if (is_valid_ether_addr(dev->dev_addr)) {
2231 /* eeprom values are valid so use them */
2232 SMSC_TRACE(pdata, probe,
2233 "Mac Address is read from LAN911x EEPROM");
2235 /* eeprom values are invalid, generate random MAC */
2236 random_ether_addr(dev->dev_addr);
2237 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
2238 SMSC_TRACE(pdata, probe,
2239 "MAC Address is set to random_ether_addr");
2243 spin_unlock_irq(&pdata->mac_lock);
2245 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
2249 out_unregister_netdev_5:
2250 unregister_netdev(dev);
2251 out_unset_drvdata_4:
2252 platform_set_drvdata(pdev, NULL);
2253 free_irq(dev->irq, dev);
2255 iounmap(pdata->ioaddr);
2259 release_mem_region(res->start, resource_size(res));
2265 /* This implementation assumes the devices remains powered on its VDDVARIO
2266 * pins during suspend. */
2268 /* TODO: implement freeze/thaw callbacks for hibernation.*/
2270 static int smsc911x_suspend(struct device *dev)
2272 struct net_device *ndev = dev_get_drvdata(dev);
2273 struct smsc911x_data *pdata = netdev_priv(ndev);
2275 /* enable wake on LAN, energy detection and the external PME
2277 smsc911x_reg_write(pdata, PMT_CTRL,
2278 PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
2279 PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
2284 static int smsc911x_resume(struct device *dev)
2286 struct net_device *ndev = dev_get_drvdata(dev);
2287 struct smsc911x_data *pdata = netdev_priv(ndev);
2288 unsigned int to = 100;
2290 /* Note 3.11 from the datasheet:
2291 * "When the LAN9220 is in a power saving state, a write of any
2292 * data to the BYTE_TEST register will wake-up the device."
2294 smsc911x_reg_write(pdata, BYTE_TEST, 0);
2296 /* poll the READY bit in PMT_CTRL. Any other access to the device is
2297 * forbidden while this bit isn't set. Try for 100ms and return -EIO
2299 while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
2302 return (to == 0) ? -EIO : 0;
2305 static const struct dev_pm_ops smsc911x_pm_ops = {
2306 .suspend = smsc911x_suspend,
2307 .resume = smsc911x_resume,
2310 #define SMSC911X_PM_OPS (&smsc911x_pm_ops)
2313 #define SMSC911X_PM_OPS NULL
2316 static struct platform_driver smsc911x_driver = {
2317 .probe = smsc911x_drv_probe,
2318 .remove = __devexit_p(smsc911x_drv_remove),
2320 .name = SMSC_CHIPNAME,
2321 .owner = THIS_MODULE,
2322 .pm = SMSC911X_PM_OPS,
2326 /* Entry point for loading the module */
2327 static int __init smsc911x_init_module(void)
2330 return platform_driver_register(&smsc911x_driver);
2333 /* entry point for unloading the module */
2334 static void __exit smsc911x_cleanup_module(void)
2336 platform_driver_unregister(&smsc911x_driver);
2339 module_init(smsc911x_init_module);
2340 module_exit(smsc911x_cleanup_module);