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
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/crc32.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/etherdevice.h>
39 #include <linux/ethtool.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42 #include <linux/ioport.h>
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/netdevice.h>
46 #include <linux/platform_device.h>
47 #include <linux/sched.h>
48 #include <linux/timer.h>
49 #include <linux/bug.h>
50 #include <linux/bitops.h>
51 #include <linux/irq.h>
53 #include <linux/swab.h>
54 #include <linux/phy.h>
55 #include <linux/smsc911x.h>
56 #include <linux/device.h>
59 #define SMSC_CHIPNAME "smsc911x"
60 #define SMSC_MDIONAME "smsc911x-mdio"
61 #define SMSC_DRV_VERSION "2008-10-21"
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION(SMSC_DRV_VERSION
);
65 MODULE_ALIAS("platform:smsc911x");
68 static int debug
= 16;
73 module_param(debug
, int, 0);
74 MODULE_PARM_DESC(debug
, "Debug level (0=none,...,16=all)");
79 u32 (*reg_read
)(struct smsc911x_data
*pdata
, u32 reg
);
80 void (*reg_write
)(struct smsc911x_data
*pdata
, u32 reg
, u32 val
);
81 void (*rx_readfifo
)(struct smsc911x_data
*pdata
,
82 unsigned int *buf
, unsigned int wordcount
);
83 void (*tx_writefifo
)(struct smsc911x_data
*pdata
,
84 unsigned int *buf
, unsigned int wordcount
);
87 struct smsc911x_data
{
92 /* used to decide which workarounds apply */
93 unsigned int generation
;
95 /* device configuration (copied from platform_data during probe) */
96 struct smsc911x_platform_config config
;
98 /* This needs to be acquired before calling any of below:
99 * smsc911x_mac_read(), smsc911x_mac_write()
103 /* spinlock to ensure register accesses are serialised */
106 struct phy_device
*phy_dev
;
107 struct mii_bus
*mii_bus
;
108 int phy_irq
[PHY_MAX_ADDR
];
109 unsigned int using_extphy
;
114 unsigned int gpio_setting
;
115 unsigned int gpio_orig_setting
;
116 struct net_device
*dev
;
117 struct napi_struct napi
;
119 unsigned int software_irq_signal
;
121 #ifdef USE_PHY_WORK_AROUND
122 #define MIN_PACKET_SIZE (64)
123 char loopback_tx_pkt
[MIN_PACKET_SIZE
];
124 char loopback_rx_pkt
[MIN_PACKET_SIZE
];
125 unsigned int resetcount
;
128 /* Members for Multicast filter workaround */
129 unsigned int multicast_update_pending
;
130 unsigned int set_bits_mask
;
131 unsigned int clear_bits_mask
;
135 /* register access functions */
136 const struct smsc911x_ops
*ops
;
139 /* Easy access to information */
140 #define __smsc_shift(pdata, reg) ((reg) << ((pdata)->config.shift))
142 static inline u32
__smsc911x_reg_read(struct smsc911x_data
*pdata
, u32 reg
)
144 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
)
145 return readl(pdata
->ioaddr
+ reg
);
147 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
)
148 return ((readw(pdata
->ioaddr
+ reg
) & 0xFFFF) |
149 ((readw(pdata
->ioaddr
+ reg
+ 2) & 0xFFFF) << 16));
156 __smsc911x_reg_read_shift(struct smsc911x_data
*pdata
, u32 reg
)
158 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
)
159 return readl(pdata
->ioaddr
+ __smsc_shift(pdata
, reg
));
161 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
)
162 return (readw(pdata
->ioaddr
+
163 __smsc_shift(pdata
, reg
)) & 0xFFFF) |
164 ((readw(pdata
->ioaddr
+
165 __smsc_shift(pdata
, reg
+ 2)) & 0xFFFF) << 16);
171 static inline u32
smsc911x_reg_read(struct smsc911x_data
*pdata
, u32 reg
)
176 spin_lock_irqsave(&pdata
->dev_lock
, flags
);
177 data
= pdata
->ops
->reg_read(pdata
, reg
);
178 spin_unlock_irqrestore(&pdata
->dev_lock
, flags
);
183 static inline void __smsc911x_reg_write(struct smsc911x_data
*pdata
, u32 reg
,
186 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
) {
187 writel(val
, pdata
->ioaddr
+ reg
);
191 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
) {
192 writew(val
& 0xFFFF, pdata
->ioaddr
+ reg
);
193 writew((val
>> 16) & 0xFFFF, pdata
->ioaddr
+ reg
+ 2);
201 __smsc911x_reg_write_shift(struct smsc911x_data
*pdata
, u32 reg
, u32 val
)
203 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
) {
204 writel(val
, pdata
->ioaddr
+ __smsc_shift(pdata
, reg
));
208 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
) {
210 pdata
->ioaddr
+ __smsc_shift(pdata
, reg
));
211 writew((val
>> 16) & 0xFFFF,
212 pdata
->ioaddr
+ __smsc_shift(pdata
, reg
+ 2));
219 static inline void smsc911x_reg_write(struct smsc911x_data
*pdata
, u32 reg
,
224 spin_lock_irqsave(&pdata
->dev_lock
, flags
);
225 pdata
->ops
->reg_write(pdata
, reg
, val
);
226 spin_unlock_irqrestore(&pdata
->dev_lock
, flags
);
229 /* Writes a packet to the TX_DATA_FIFO */
231 smsc911x_tx_writefifo(struct smsc911x_data
*pdata
, unsigned int *buf
,
232 unsigned int wordcount
)
236 spin_lock_irqsave(&pdata
->dev_lock
, flags
);
238 if (pdata
->config
.flags
& SMSC911X_SWAP_FIFO
) {
240 __smsc911x_reg_write(pdata
, TX_DATA_FIFO
,
245 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
) {
246 writesl(pdata
->ioaddr
+ TX_DATA_FIFO
, buf
, wordcount
);
250 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
) {
252 __smsc911x_reg_write(pdata
, TX_DATA_FIFO
, *buf
++);
258 spin_unlock_irqrestore(&pdata
->dev_lock
, flags
);
261 /* Writes a packet to the TX_DATA_FIFO - shifted version */
263 smsc911x_tx_writefifo_shift(struct smsc911x_data
*pdata
, unsigned int *buf
,
264 unsigned int wordcount
)
268 spin_lock_irqsave(&pdata
->dev_lock
, flags
);
270 if (pdata
->config
.flags
& SMSC911X_SWAP_FIFO
) {
272 __smsc911x_reg_write_shift(pdata
, TX_DATA_FIFO
,
277 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
) {
278 writesl(pdata
->ioaddr
+ __smsc_shift(pdata
,
279 TX_DATA_FIFO
), buf
, wordcount
);
283 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
) {
285 __smsc911x_reg_write_shift(pdata
,
286 TX_DATA_FIFO
, *buf
++);
292 spin_unlock_irqrestore(&pdata
->dev_lock
, flags
);
295 /* Reads a packet out of the RX_DATA_FIFO */
297 smsc911x_rx_readfifo(struct smsc911x_data
*pdata
, unsigned int *buf
,
298 unsigned int wordcount
)
302 spin_lock_irqsave(&pdata
->dev_lock
, flags
);
304 if (pdata
->config
.flags
& SMSC911X_SWAP_FIFO
) {
306 *buf
++ = swab32(__smsc911x_reg_read(pdata
,
311 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
) {
312 readsl(pdata
->ioaddr
+ RX_DATA_FIFO
, buf
, wordcount
);
316 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
) {
318 *buf
++ = __smsc911x_reg_read(pdata
, RX_DATA_FIFO
);
324 spin_unlock_irqrestore(&pdata
->dev_lock
, flags
);
327 /* Reads a packet out of the RX_DATA_FIFO - shifted version */
329 smsc911x_rx_readfifo_shift(struct smsc911x_data
*pdata
, unsigned int *buf
,
330 unsigned int wordcount
)
334 spin_lock_irqsave(&pdata
->dev_lock
, flags
);
336 if (pdata
->config
.flags
& SMSC911X_SWAP_FIFO
) {
338 *buf
++ = swab32(__smsc911x_reg_read_shift(pdata
,
343 if (pdata
->config
.flags
& SMSC911X_USE_32BIT
) {
344 readsl(pdata
->ioaddr
+ __smsc_shift(pdata
,
345 RX_DATA_FIFO
), buf
, wordcount
);
349 if (pdata
->config
.flags
& SMSC911X_USE_16BIT
) {
351 *buf
++ = __smsc911x_reg_read_shift(pdata
,
358 spin_unlock_irqrestore(&pdata
->dev_lock
, flags
);
361 /* waits for MAC not busy, with timeout. Only called by smsc911x_mac_read
362 * and smsc911x_mac_write, so assumes mac_lock is held */
363 static int smsc911x_mac_complete(struct smsc911x_data
*pdata
)
368 SMSC_ASSERT_MAC_LOCK(pdata
);
370 for (i
= 0; i
< 40; i
++) {
371 val
= smsc911x_reg_read(pdata
, MAC_CSR_CMD
);
372 if (!(val
& MAC_CSR_CMD_CSR_BUSY_
))
375 SMSC_WARN(pdata
, hw
, "Timed out waiting for MAC not BUSY. "
376 "MAC_CSR_CMD: 0x%08X", val
);
380 /* Fetches a MAC register value. Assumes mac_lock is acquired */
381 static u32
smsc911x_mac_read(struct smsc911x_data
*pdata
, unsigned int offset
)
385 SMSC_ASSERT_MAC_LOCK(pdata
);
387 temp
= smsc911x_reg_read(pdata
, MAC_CSR_CMD
);
388 if (unlikely(temp
& MAC_CSR_CMD_CSR_BUSY_
)) {
389 SMSC_WARN(pdata
, hw
, "MAC busy at entry");
393 /* Send the MAC cmd */
394 smsc911x_reg_write(pdata
, MAC_CSR_CMD
, ((offset
& 0xFF) |
395 MAC_CSR_CMD_CSR_BUSY_
| MAC_CSR_CMD_R_NOT_W_
));
397 /* Workaround for hardware read-after-write restriction */
398 temp
= smsc911x_reg_read(pdata
, BYTE_TEST
);
400 /* Wait for the read to complete */
401 if (likely(smsc911x_mac_complete(pdata
) == 0))
402 return smsc911x_reg_read(pdata
, MAC_CSR_DATA
);
404 SMSC_WARN(pdata
, hw
, "MAC busy after read");
408 /* Set a mac register, mac_lock must be acquired before calling */
409 static void smsc911x_mac_write(struct smsc911x_data
*pdata
,
410 unsigned int offset
, u32 val
)
414 SMSC_ASSERT_MAC_LOCK(pdata
);
416 temp
= smsc911x_reg_read(pdata
, MAC_CSR_CMD
);
417 if (unlikely(temp
& MAC_CSR_CMD_CSR_BUSY_
)) {
419 "smsc911x_mac_write failed, MAC busy at entry");
423 /* Send data to write */
424 smsc911x_reg_write(pdata
, MAC_CSR_DATA
, val
);
426 /* Write the actual data */
427 smsc911x_reg_write(pdata
, MAC_CSR_CMD
, ((offset
& 0xFF) |
428 MAC_CSR_CMD_CSR_BUSY_
));
430 /* Workaround for hardware read-after-write restriction */
431 temp
= smsc911x_reg_read(pdata
, BYTE_TEST
);
433 /* Wait for the write to complete */
434 if (likely(smsc911x_mac_complete(pdata
) == 0))
437 SMSC_WARN(pdata
, hw
, "smsc911x_mac_write failed, MAC busy after write");
440 /* Get a phy register */
441 static int smsc911x_mii_read(struct mii_bus
*bus
, int phyaddr
, int regidx
)
443 struct smsc911x_data
*pdata
= (struct smsc911x_data
*)bus
->priv
;
448 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
450 /* Confirm MII not busy */
451 if (unlikely(smsc911x_mac_read(pdata
, MII_ACC
) & MII_ACC_MII_BUSY_
)) {
452 SMSC_WARN(pdata
, hw
, "MII is busy in smsc911x_mii_read???");
457 /* Set the address, index & direction (read from PHY) */
458 addr
= ((phyaddr
& 0x1F) << 11) | ((regidx
& 0x1F) << 6);
459 smsc911x_mac_write(pdata
, MII_ACC
, addr
);
461 /* Wait for read to complete w/ timeout */
462 for (i
= 0; i
< 100; i
++)
463 if (!(smsc911x_mac_read(pdata
, MII_ACC
) & MII_ACC_MII_BUSY_
)) {
464 reg
= smsc911x_mac_read(pdata
, MII_DATA
);
468 SMSC_WARN(pdata
, hw
, "Timed out waiting for MII read to finish");
472 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
476 /* Set a phy register */
477 static int smsc911x_mii_write(struct mii_bus
*bus
, int phyaddr
, int regidx
,
480 struct smsc911x_data
*pdata
= (struct smsc911x_data
*)bus
->priv
;
485 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
487 /* Confirm MII not busy */
488 if (unlikely(smsc911x_mac_read(pdata
, MII_ACC
) & MII_ACC_MII_BUSY_
)) {
489 SMSC_WARN(pdata
, hw
, "MII is busy in smsc911x_mii_write???");
494 /* Put the data to write in the MAC */
495 smsc911x_mac_write(pdata
, MII_DATA
, val
);
497 /* Set the address, index & direction (write to PHY) */
498 addr
= ((phyaddr
& 0x1F) << 11) | ((regidx
& 0x1F) << 6) |
500 smsc911x_mac_write(pdata
, MII_ACC
, addr
);
502 /* Wait for write to complete w/ timeout */
503 for (i
= 0; i
< 100; i
++)
504 if (!(smsc911x_mac_read(pdata
, MII_ACC
) & MII_ACC_MII_BUSY_
)) {
509 SMSC_WARN(pdata
, hw
, "Timed out waiting for MII write to finish");
513 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
517 /* Switch to external phy. Assumes tx and rx are stopped. */
518 static void smsc911x_phy_enable_external(struct smsc911x_data
*pdata
)
520 unsigned int hwcfg
= smsc911x_reg_read(pdata
, HW_CFG
);
522 /* Disable phy clocks to the MAC */
523 hwcfg
&= (~HW_CFG_PHY_CLK_SEL_
);
524 hwcfg
|= HW_CFG_PHY_CLK_SEL_CLK_DIS_
;
525 smsc911x_reg_write(pdata
, HW_CFG
, hwcfg
);
526 udelay(10); /* Enough time for clocks to stop */
528 /* Switch to external phy */
529 hwcfg
|= HW_CFG_EXT_PHY_EN_
;
530 smsc911x_reg_write(pdata
, HW_CFG
, hwcfg
);
532 /* Enable phy clocks to the MAC */
533 hwcfg
&= (~HW_CFG_PHY_CLK_SEL_
);
534 hwcfg
|= HW_CFG_PHY_CLK_SEL_EXT_PHY_
;
535 smsc911x_reg_write(pdata
, HW_CFG
, hwcfg
);
536 udelay(10); /* Enough time for clocks to restart */
538 hwcfg
|= HW_CFG_SMI_SEL_
;
539 smsc911x_reg_write(pdata
, HW_CFG
, hwcfg
);
542 /* Autodetects and enables external phy if present on supported chips.
543 * autodetection can be overridden by specifying SMSC911X_FORCE_INTERNAL_PHY
544 * or SMSC911X_FORCE_EXTERNAL_PHY in the platform_data flags. */
545 static void smsc911x_phy_initialise_external(struct smsc911x_data
*pdata
)
547 unsigned int hwcfg
= smsc911x_reg_read(pdata
, HW_CFG
);
549 if (pdata
->config
.flags
& SMSC911X_FORCE_INTERNAL_PHY
) {
550 SMSC_TRACE(pdata
, hw
, "Forcing internal PHY");
551 pdata
->using_extphy
= 0;
552 } else if (pdata
->config
.flags
& SMSC911X_FORCE_EXTERNAL_PHY
) {
553 SMSC_TRACE(pdata
, hw
, "Forcing external PHY");
554 smsc911x_phy_enable_external(pdata
);
555 pdata
->using_extphy
= 1;
556 } else if (hwcfg
& HW_CFG_EXT_PHY_DET_
) {
557 SMSC_TRACE(pdata
, hw
,
558 "HW_CFG EXT_PHY_DET set, using external PHY");
559 smsc911x_phy_enable_external(pdata
);
560 pdata
->using_extphy
= 1;
562 SMSC_TRACE(pdata
, hw
,
563 "HW_CFG EXT_PHY_DET clear, using internal PHY");
564 pdata
->using_extphy
= 0;
568 /* Fetches a tx status out of the status fifo */
569 static unsigned int smsc911x_tx_get_txstatus(struct smsc911x_data
*pdata
)
571 unsigned int result
=
572 smsc911x_reg_read(pdata
, TX_FIFO_INF
) & TX_FIFO_INF_TSUSED_
;
575 result
= smsc911x_reg_read(pdata
, TX_STATUS_FIFO
);
580 /* Fetches the next rx status */
581 static unsigned int smsc911x_rx_get_rxstatus(struct smsc911x_data
*pdata
)
583 unsigned int result
=
584 smsc911x_reg_read(pdata
, RX_FIFO_INF
) & RX_FIFO_INF_RXSUSED_
;
587 result
= smsc911x_reg_read(pdata
, RX_STATUS_FIFO
);
592 #ifdef USE_PHY_WORK_AROUND
593 static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data
*pdata
)
600 for (tries
= 0; tries
< 10; tries
++) {
601 unsigned int txcmd_a
;
602 unsigned int txcmd_b
;
604 unsigned int pktlength
;
607 /* Zero-out rx packet memory */
608 memset(pdata
->loopback_rx_pkt
, 0, MIN_PACKET_SIZE
);
610 /* Write tx packet to 118 */
611 txcmd_a
= (u32
)((ulong
)pdata
->loopback_tx_pkt
& 0x03) << 16;
612 txcmd_a
|= TX_CMD_A_FIRST_SEG_
| TX_CMD_A_LAST_SEG_
;
613 txcmd_a
|= MIN_PACKET_SIZE
;
615 txcmd_b
= MIN_PACKET_SIZE
<< 16 | MIN_PACKET_SIZE
;
617 smsc911x_reg_write(pdata
, TX_DATA_FIFO
, txcmd_a
);
618 smsc911x_reg_write(pdata
, TX_DATA_FIFO
, txcmd_b
);
620 bufp
= (ulong
)pdata
->loopback_tx_pkt
& (~0x3);
621 wrsz
= MIN_PACKET_SIZE
+ 3;
622 wrsz
+= (u32
)((ulong
)pdata
->loopback_tx_pkt
& 0x3);
625 pdata
->ops
->tx_writefifo(pdata
, (unsigned int *)bufp
, wrsz
);
627 /* Wait till transmit is done */
631 status
= smsc911x_tx_get_txstatus(pdata
);
632 } while ((i
--) && (!status
));
636 "Failed to transmit during loopback test");
639 if (status
& TX_STS_ES_
) {
641 "Transmit encountered errors during loopback test");
645 /* Wait till receive is done */
649 status
= smsc911x_rx_get_rxstatus(pdata
);
650 } while ((i
--) && (!status
));
654 "Failed to receive during loopback test");
657 if (status
& RX_STS_ES_
) {
659 "Receive encountered errors during loopback test");
663 pktlength
= ((status
& 0x3FFF0000UL
) >> 16);
664 bufp
= (ulong
)pdata
->loopback_rx_pkt
;
665 rdsz
= pktlength
+ 3;
666 rdsz
+= (u32
)((ulong
)pdata
->loopback_rx_pkt
& 0x3);
669 pdata
->ops
->rx_readfifo(pdata
, (unsigned int *)bufp
, rdsz
);
671 if (pktlength
!= (MIN_PACKET_SIZE
+ 4)) {
672 SMSC_WARN(pdata
, hw
, "Unexpected packet size "
673 "during loop back test, size=%d, will retry",
678 for (j
= 0; j
< MIN_PACKET_SIZE
; j
++) {
679 if (pdata
->loopback_tx_pkt
[j
]
680 != pdata
->loopback_rx_pkt
[j
]) {
686 SMSC_TRACE(pdata
, hw
, "Successfully verified "
690 SMSC_WARN(pdata
, hw
, "Data mismatch "
691 "during loop back test, will retry");
699 static int smsc911x_phy_reset(struct smsc911x_data
*pdata
)
701 struct phy_device
*phy_dev
= pdata
->phy_dev
;
703 unsigned int i
= 100000;
706 BUG_ON(!phy_dev
->bus
);
708 SMSC_TRACE(pdata
, hw
, "Performing PHY BCR Reset");
709 smsc911x_mii_write(phy_dev
->bus
, phy_dev
->addr
, MII_BMCR
, BMCR_RESET
);
712 temp
= smsc911x_mii_read(phy_dev
->bus
, phy_dev
->addr
,
714 } while ((i
--) && (temp
& BMCR_RESET
));
716 if (temp
& BMCR_RESET
) {
717 SMSC_WARN(pdata
, hw
, "PHY reset failed to complete");
720 /* Extra delay required because the phy may not be completed with
721 * its reset when BMCR_RESET is cleared. Specs say 256 uS is
722 * enough delay but using 1ms here to be safe */
728 static int smsc911x_phy_loopbacktest(struct net_device
*dev
)
730 struct smsc911x_data
*pdata
= netdev_priv(dev
);
731 struct phy_device
*phy_dev
= pdata
->phy_dev
;
736 /* Initialise tx packet using broadcast destination address */
737 memset(pdata
->loopback_tx_pkt
, 0xff, ETH_ALEN
);
739 /* Use incrementing source address */
740 for (i
= 6; i
< 12; i
++)
741 pdata
->loopback_tx_pkt
[i
] = (char)i
;
743 /* Set length type field */
744 pdata
->loopback_tx_pkt
[12] = 0x00;
745 pdata
->loopback_tx_pkt
[13] = 0x00;
747 for (i
= 14; i
< MIN_PACKET_SIZE
; i
++)
748 pdata
->loopback_tx_pkt
[i
] = (char)i
;
750 val
= smsc911x_reg_read(pdata
, HW_CFG
);
751 val
&= HW_CFG_TX_FIF_SZ_
;
753 smsc911x_reg_write(pdata
, HW_CFG
, val
);
755 smsc911x_reg_write(pdata
, TX_CFG
, TX_CFG_TX_ON_
);
756 smsc911x_reg_write(pdata
, RX_CFG
,
757 (u32
)((ulong
)pdata
->loopback_rx_pkt
& 0x03) << 8);
759 for (i
= 0; i
< 10; i
++) {
760 /* Set PHY to 10/FD, no ANEG, and loopback mode */
761 smsc911x_mii_write(phy_dev
->bus
, phy_dev
->addr
, MII_BMCR
,
762 BMCR_LOOPBACK
| BMCR_FULLDPLX
);
764 /* Enable MAC tx/rx, FD */
765 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
766 smsc911x_mac_write(pdata
, MAC_CR
, MAC_CR_FDPX_
767 | MAC_CR_TXEN_
| MAC_CR_RXEN_
);
768 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
770 if (smsc911x_phy_check_loopbackpkt(pdata
) == 0) {
777 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
778 smsc911x_mac_write(pdata
, MAC_CR
, 0);
779 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
781 smsc911x_phy_reset(pdata
);
785 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
786 smsc911x_mac_write(pdata
, MAC_CR
, 0);
787 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
789 /* Cancel PHY loopback mode */
790 smsc911x_mii_write(phy_dev
->bus
, phy_dev
->addr
, MII_BMCR
, 0);
792 smsc911x_reg_write(pdata
, TX_CFG
, 0);
793 smsc911x_reg_write(pdata
, RX_CFG
, 0);
797 #endif /* USE_PHY_WORK_AROUND */
799 static void smsc911x_phy_update_flowcontrol(struct smsc911x_data
*pdata
)
801 struct phy_device
*phy_dev
= pdata
->phy_dev
;
802 u32 afc
= smsc911x_reg_read(pdata
, AFC_CFG
);
806 if (phy_dev
->duplex
== DUPLEX_FULL
) {
807 u16 lcladv
= phy_read(phy_dev
, MII_ADVERTISE
);
808 u16 rmtadv
= phy_read(phy_dev
, MII_LPA
);
809 u8 cap
= mii_resolve_flowctrl_fdx(lcladv
, rmtadv
);
811 if (cap
& FLOW_CTRL_RX
)
816 if (cap
& FLOW_CTRL_TX
)
821 SMSC_TRACE(pdata
, hw
, "rx pause %s, tx pause %s",
822 (cap
& FLOW_CTRL_RX
? "enabled" : "disabled"),
823 (cap
& FLOW_CTRL_TX
? "enabled" : "disabled"));
825 SMSC_TRACE(pdata
, hw
, "half duplex");
830 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
831 smsc911x_mac_write(pdata
, FLOW
, flow
);
832 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
834 smsc911x_reg_write(pdata
, AFC_CFG
, afc
);
837 /* Update link mode if anything has changed. Called periodically when the
838 * PHY is in polling mode, even if nothing has changed. */
839 static void smsc911x_phy_adjust_link(struct net_device
*dev
)
841 struct smsc911x_data
*pdata
= netdev_priv(dev
);
842 struct phy_device
*phy_dev
= pdata
->phy_dev
;
846 if (phy_dev
->duplex
!= pdata
->last_duplex
) {
848 SMSC_TRACE(pdata
, hw
, "duplex state has changed");
850 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
851 mac_cr
= smsc911x_mac_read(pdata
, MAC_CR
);
852 if (phy_dev
->duplex
) {
853 SMSC_TRACE(pdata
, hw
,
854 "configuring for full duplex mode");
855 mac_cr
|= MAC_CR_FDPX_
;
857 SMSC_TRACE(pdata
, hw
,
858 "configuring for half duplex mode");
859 mac_cr
&= ~MAC_CR_FDPX_
;
861 smsc911x_mac_write(pdata
, MAC_CR
, mac_cr
);
862 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
864 smsc911x_phy_update_flowcontrol(pdata
);
865 pdata
->last_duplex
= phy_dev
->duplex
;
868 carrier
= netif_carrier_ok(dev
);
869 if (carrier
!= pdata
->last_carrier
) {
870 SMSC_TRACE(pdata
, hw
, "carrier state has changed");
872 SMSC_TRACE(pdata
, hw
, "configuring for carrier OK");
873 if ((pdata
->gpio_orig_setting
& GPIO_CFG_LED1_EN_
) &&
874 (!pdata
->using_extphy
)) {
875 /* Restore original GPIO configuration */
876 pdata
->gpio_setting
= pdata
->gpio_orig_setting
;
877 smsc911x_reg_write(pdata
, GPIO_CFG
,
878 pdata
->gpio_setting
);
881 SMSC_TRACE(pdata
, hw
, "configuring for no carrier");
882 /* Check global setting that LED1
883 * usage is 10/100 indicator */
884 pdata
->gpio_setting
= smsc911x_reg_read(pdata
,
886 if ((pdata
->gpio_setting
& GPIO_CFG_LED1_EN_
) &&
887 (!pdata
->using_extphy
)) {
888 /* Force 10/100 LED off, after saving
889 * original GPIO configuration */
890 pdata
->gpio_orig_setting
= pdata
->gpio_setting
;
892 pdata
->gpio_setting
&= ~GPIO_CFG_LED1_EN_
;
893 pdata
->gpio_setting
|= (GPIO_CFG_GPIOBUF0_
896 smsc911x_reg_write(pdata
, GPIO_CFG
,
897 pdata
->gpio_setting
);
900 pdata
->last_carrier
= carrier
;
904 static int smsc911x_mii_probe(struct net_device
*dev
)
906 struct smsc911x_data
*pdata
= netdev_priv(dev
);
907 struct phy_device
*phydev
= NULL
;
910 /* find the first phy */
911 phydev
= phy_find_first(pdata
->mii_bus
);
913 netdev_err(dev
, "no PHY found\n");
917 SMSC_TRACE(pdata
, probe
, "PHY: addr %d, phy_id 0x%08X",
918 phydev
->addr
, phydev
->phy_id
);
920 ret
= phy_connect_direct(dev
, phydev
,
921 &smsc911x_phy_adjust_link
, 0,
922 pdata
->config
.phy_interface
);
925 netdev_err(dev
, "Could not attach to PHY\n");
930 "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
931 phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
);
933 /* mask with MAC supported features */
934 phydev
->supported
&= (PHY_BASIC_FEATURES
| SUPPORTED_Pause
|
935 SUPPORTED_Asym_Pause
);
936 phydev
->advertising
= phydev
->supported
;
938 pdata
->phy_dev
= phydev
;
939 pdata
->last_duplex
= -1;
940 pdata
->last_carrier
= -1;
942 #ifdef USE_PHY_WORK_AROUND
943 if (smsc911x_phy_loopbacktest(dev
) < 0) {
944 SMSC_WARN(pdata
, hw
, "Failed Loop Back Test");
947 SMSC_TRACE(pdata
, hw
, "Passed Loop Back Test");
948 #endif /* USE_PHY_WORK_AROUND */
950 SMSC_TRACE(pdata
, hw
, "phy initialised successfully");
954 static int __devinit
smsc911x_mii_init(struct platform_device
*pdev
,
955 struct net_device
*dev
)
957 struct smsc911x_data
*pdata
= netdev_priv(dev
);
960 pdata
->mii_bus
= mdiobus_alloc();
961 if (!pdata
->mii_bus
) {
966 pdata
->mii_bus
->name
= SMSC_MDIONAME
;
967 snprintf(pdata
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%x", pdev
->id
);
968 pdata
->mii_bus
->priv
= pdata
;
969 pdata
->mii_bus
->read
= smsc911x_mii_read
;
970 pdata
->mii_bus
->write
= smsc911x_mii_write
;
971 pdata
->mii_bus
->irq
= pdata
->phy_irq
;
972 for (i
= 0; i
< PHY_MAX_ADDR
; ++i
)
973 pdata
->mii_bus
->irq
[i
] = PHY_POLL
;
975 pdata
->mii_bus
->parent
= &pdev
->dev
;
977 switch (pdata
->idrev
& 0xFFFF0000) {
982 /* External PHY supported, try to autodetect */
983 smsc911x_phy_initialise_external(pdata
);
986 SMSC_TRACE(pdata
, hw
, "External PHY is not supported, "
987 "using internal PHY");
988 pdata
->using_extphy
= 0;
992 if (!pdata
->using_extphy
) {
993 /* Mask all PHYs except ID 1 (internal) */
994 pdata
->mii_bus
->phy_mask
= ~(1 << 1);
997 if (mdiobus_register(pdata
->mii_bus
)) {
998 SMSC_WARN(pdata
, probe
, "Error registering mii bus");
999 goto err_out_free_bus_2
;
1002 if (smsc911x_mii_probe(dev
) < 0) {
1003 SMSC_WARN(pdata
, probe
, "Error registering mii bus");
1004 goto err_out_unregister_bus_3
;
1009 err_out_unregister_bus_3
:
1010 mdiobus_unregister(pdata
->mii_bus
);
1012 mdiobus_free(pdata
->mii_bus
);
1017 /* Gets the number of tx statuses in the fifo */
1018 static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data
*pdata
)
1020 return (smsc911x_reg_read(pdata
, TX_FIFO_INF
)
1021 & TX_FIFO_INF_TSUSED_
) >> 16;
1024 /* Reads tx statuses and increments counters where necessary */
1025 static void smsc911x_tx_update_txcounters(struct net_device
*dev
)
1027 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1028 unsigned int tx_stat
;
1030 while ((tx_stat
= smsc911x_tx_get_txstatus(pdata
)) != 0) {
1031 if (unlikely(tx_stat
& 0x80000000)) {
1032 /* In this driver the packet tag is used as the packet
1033 * length. Since a packet length can never reach the
1034 * size of 0x8000, this bit is reserved. It is worth
1035 * noting that the "reserved bit" in the warning above
1036 * does not reference a hardware defined reserved bit
1037 * but rather a driver defined one.
1039 SMSC_WARN(pdata
, hw
, "Packet tag reserved bit is high");
1041 if (unlikely(tx_stat
& TX_STS_ES_
)) {
1042 dev
->stats
.tx_errors
++;
1044 dev
->stats
.tx_packets
++;
1045 dev
->stats
.tx_bytes
+= (tx_stat
>> 16);
1047 if (unlikely(tx_stat
& TX_STS_EXCESS_COL_
)) {
1048 dev
->stats
.collisions
+= 16;
1049 dev
->stats
.tx_aborted_errors
+= 1;
1051 dev
->stats
.collisions
+=
1052 ((tx_stat
>> 3) & 0xF);
1054 if (unlikely(tx_stat
& TX_STS_LOST_CARRIER_
))
1055 dev
->stats
.tx_carrier_errors
+= 1;
1056 if (unlikely(tx_stat
& TX_STS_LATE_COL_
)) {
1057 dev
->stats
.collisions
++;
1058 dev
->stats
.tx_aborted_errors
++;
1064 /* Increments the Rx error counters */
1066 smsc911x_rx_counterrors(struct net_device
*dev
, unsigned int rxstat
)
1070 if (unlikely(rxstat
& RX_STS_ES_
)) {
1071 dev
->stats
.rx_errors
++;
1072 if (unlikely(rxstat
& RX_STS_CRC_ERR_
)) {
1073 dev
->stats
.rx_crc_errors
++;
1077 if (likely(!crc_err
)) {
1078 if (unlikely((rxstat
& RX_STS_FRAME_TYPE_
) &&
1079 (rxstat
& RX_STS_LENGTH_ERR_
)))
1080 dev
->stats
.rx_length_errors
++;
1081 if (rxstat
& RX_STS_MCAST_
)
1082 dev
->stats
.multicast
++;
1086 /* Quickly dumps bad packets */
1088 smsc911x_rx_fastforward(struct smsc911x_data
*pdata
, unsigned int pktbytes
)
1090 unsigned int pktwords
= (pktbytes
+ NET_IP_ALIGN
+ 3) >> 2;
1092 if (likely(pktwords
>= 4)) {
1093 unsigned int timeout
= 500;
1095 smsc911x_reg_write(pdata
, RX_DP_CTRL
, RX_DP_CTRL_RX_FFWD_
);
1098 val
= smsc911x_reg_read(pdata
, RX_DP_CTRL
);
1099 } while ((val
& RX_DP_CTRL_RX_FFWD_
) && --timeout
);
1101 if (unlikely(timeout
== 0))
1102 SMSC_WARN(pdata
, hw
, "Timed out waiting for "
1103 "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val
);
1107 temp
= smsc911x_reg_read(pdata
, RX_DATA_FIFO
);
1111 /* NAPI poll function */
1112 static int smsc911x_poll(struct napi_struct
*napi
, int budget
)
1114 struct smsc911x_data
*pdata
=
1115 container_of(napi
, struct smsc911x_data
, napi
);
1116 struct net_device
*dev
= pdata
->dev
;
1119 while (npackets
< budget
) {
1120 unsigned int pktlength
;
1121 unsigned int pktwords
;
1122 struct sk_buff
*skb
;
1123 unsigned int rxstat
= smsc911x_rx_get_rxstatus(pdata
);
1127 /* We processed all packets available. Tell NAPI it can
1128 * stop polling then re-enable rx interrupts */
1129 smsc911x_reg_write(pdata
, INT_STS
, INT_STS_RSFL_
);
1130 napi_complete(napi
);
1131 temp
= smsc911x_reg_read(pdata
, INT_EN
);
1132 temp
|= INT_EN_RSFL_EN_
;
1133 smsc911x_reg_write(pdata
, INT_EN
, temp
);
1137 /* Count packet for NAPI scheduling, even if it has an error.
1138 * Error packets still require cycles to discard */
1141 pktlength
= ((rxstat
& 0x3FFF0000) >> 16);
1142 pktwords
= (pktlength
+ NET_IP_ALIGN
+ 3) >> 2;
1143 smsc911x_rx_counterrors(dev
, rxstat
);
1145 if (unlikely(rxstat
& RX_STS_ES_
)) {
1146 SMSC_WARN(pdata
, rx_err
,
1147 "Discarding packet with error bit set");
1148 /* Packet has an error, discard it and continue with
1150 smsc911x_rx_fastforward(pdata
, pktwords
);
1151 dev
->stats
.rx_dropped
++;
1155 skb
= netdev_alloc_skb(dev
, pktlength
+ NET_IP_ALIGN
);
1156 if (unlikely(!skb
)) {
1157 SMSC_WARN(pdata
, rx_err
,
1158 "Unable to allocate skb for rx packet");
1159 /* Drop the packet and stop this polling iteration */
1160 smsc911x_rx_fastforward(pdata
, pktwords
);
1161 dev
->stats
.rx_dropped
++;
1165 skb
->data
= skb
->head
;
1166 skb_reset_tail_pointer(skb
);
1168 /* Align IP on 16B boundary */
1169 skb_reserve(skb
, NET_IP_ALIGN
);
1170 skb_put(skb
, pktlength
- 4);
1171 pdata
->ops
->rx_readfifo(pdata
,
1172 (unsigned int *)skb
->head
, pktwords
);
1173 skb
->protocol
= eth_type_trans(skb
, dev
);
1174 skb_checksum_none_assert(skb
);
1175 netif_receive_skb(skb
);
1177 /* Update counters */
1178 dev
->stats
.rx_packets
++;
1179 dev
->stats
.rx_bytes
+= (pktlength
- 4);
1182 /* Return total received packets */
1186 /* Returns hash bit number for given MAC address
1188 * 01 00 5E 00 00 01 -> returns bit number 31 */
1189 static unsigned int smsc911x_hash(char addr
[ETH_ALEN
])
1191 return (ether_crc(ETH_ALEN
, addr
) >> 26) & 0x3f;
1194 static void smsc911x_rx_multicast_update(struct smsc911x_data
*pdata
)
1196 /* Performs the multicast & mac_cr update. This is called when
1197 * safe on the current hardware, and with the mac_lock held */
1198 unsigned int mac_cr
;
1200 SMSC_ASSERT_MAC_LOCK(pdata
);
1202 mac_cr
= smsc911x_mac_read(pdata
, MAC_CR
);
1203 mac_cr
|= pdata
->set_bits_mask
;
1204 mac_cr
&= ~(pdata
->clear_bits_mask
);
1205 smsc911x_mac_write(pdata
, MAC_CR
, mac_cr
);
1206 smsc911x_mac_write(pdata
, HASHH
, pdata
->hashhi
);
1207 smsc911x_mac_write(pdata
, HASHL
, pdata
->hashlo
);
1208 SMSC_TRACE(pdata
, hw
, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
1209 mac_cr
, pdata
->hashhi
, pdata
->hashlo
);
1212 static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data
*pdata
)
1214 unsigned int mac_cr
;
1216 /* This function is only called for older LAN911x devices
1217 * (revA or revB), where MAC_CR, HASHH and HASHL should not
1218 * be modified during Rx - newer devices immediately update the
1221 * This is called from interrupt context */
1223 spin_lock(&pdata
->mac_lock
);
1225 /* Check Rx has stopped */
1226 if (smsc911x_mac_read(pdata
, MAC_CR
) & MAC_CR_RXEN_
)
1227 SMSC_WARN(pdata
, drv
, "Rx not stopped");
1229 /* Perform the update - safe to do now Rx has stopped */
1230 smsc911x_rx_multicast_update(pdata
);
1233 mac_cr
= smsc911x_mac_read(pdata
, MAC_CR
);
1234 mac_cr
|= MAC_CR_RXEN_
;
1235 smsc911x_mac_write(pdata
, MAC_CR
, mac_cr
);
1237 pdata
->multicast_update_pending
= 0;
1239 spin_unlock(&pdata
->mac_lock
);
1242 static int smsc911x_soft_reset(struct smsc911x_data
*pdata
)
1244 unsigned int timeout
;
1247 /* Reset the LAN911x */
1248 smsc911x_reg_write(pdata
, HW_CFG
, HW_CFG_SRST_
);
1252 temp
= smsc911x_reg_read(pdata
, HW_CFG
);
1253 } while ((--timeout
) && (temp
& HW_CFG_SRST_
));
1255 if (unlikely(temp
& HW_CFG_SRST_
)) {
1256 SMSC_WARN(pdata
, drv
, "Failed to complete reset");
1262 /* Sets the device MAC address to dev_addr, called with mac_lock held */
1264 smsc911x_set_hw_mac_address(struct smsc911x_data
*pdata
, u8 dev_addr
[6])
1266 u32 mac_high16
= (dev_addr
[5] << 8) | dev_addr
[4];
1267 u32 mac_low32
= (dev_addr
[3] << 24) | (dev_addr
[2] << 16) |
1268 (dev_addr
[1] << 8) | dev_addr
[0];
1270 SMSC_ASSERT_MAC_LOCK(pdata
);
1272 smsc911x_mac_write(pdata
, ADDRH
, mac_high16
);
1273 smsc911x_mac_write(pdata
, ADDRL
, mac_low32
);
1276 static int smsc911x_open(struct net_device
*dev
)
1278 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1279 unsigned int timeout
;
1281 unsigned int intcfg
;
1283 /* if the phy is not yet registered, retry later*/
1284 if (!pdata
->phy_dev
) {
1285 SMSC_WARN(pdata
, hw
, "phy_dev is NULL");
1289 if (!is_valid_ether_addr(dev
->dev_addr
)) {
1290 SMSC_WARN(pdata
, hw
, "dev_addr is not a valid MAC address");
1291 return -EADDRNOTAVAIL
;
1294 /* Reset the LAN911x */
1295 if (smsc911x_soft_reset(pdata
)) {
1296 SMSC_WARN(pdata
, hw
, "soft reset failed");
1300 smsc911x_reg_write(pdata
, HW_CFG
, 0x00050000);
1301 smsc911x_reg_write(pdata
, AFC_CFG
, 0x006E3740);
1303 /* Increase the legal frame size of VLAN tagged frames to 1522 bytes */
1304 spin_lock_irq(&pdata
->mac_lock
);
1305 smsc911x_mac_write(pdata
, VLAN1
, ETH_P_8021Q
);
1306 spin_unlock_irq(&pdata
->mac_lock
);
1308 /* Make sure EEPROM has finished loading before setting GPIO_CFG */
1310 while ((smsc911x_reg_read(pdata
, E2P_CMD
) & E2P_CMD_EPC_BUSY_
) &&
1315 if (unlikely(timeout
== 0))
1316 SMSC_WARN(pdata
, ifup
,
1317 "Timed out waiting for EEPROM busy bit to clear");
1319 smsc911x_reg_write(pdata
, GPIO_CFG
, 0x70070000);
1321 /* The soft reset above cleared the device's MAC address,
1322 * restore it from local copy (set in probe) */
1323 spin_lock_irq(&pdata
->mac_lock
);
1324 smsc911x_set_hw_mac_address(pdata
, dev
->dev_addr
);
1325 spin_unlock_irq(&pdata
->mac_lock
);
1327 /* Initialise irqs, but leave all sources disabled */
1328 smsc911x_reg_write(pdata
, INT_EN
, 0);
1329 smsc911x_reg_write(pdata
, INT_STS
, 0xFFFFFFFF);
1331 /* Set interrupt deassertion to 100uS */
1332 intcfg
= ((10 << 24) | INT_CFG_IRQ_EN_
);
1334 if (pdata
->config
.irq_polarity
) {
1335 SMSC_TRACE(pdata
, ifup
, "irq polarity: active high");
1336 intcfg
|= INT_CFG_IRQ_POL_
;
1338 SMSC_TRACE(pdata
, ifup
, "irq polarity: active low");
1341 if (pdata
->config
.irq_type
) {
1342 SMSC_TRACE(pdata
, ifup
, "irq type: push-pull");
1343 intcfg
|= INT_CFG_IRQ_TYPE_
;
1345 SMSC_TRACE(pdata
, ifup
, "irq type: open drain");
1348 smsc911x_reg_write(pdata
, INT_CFG
, intcfg
);
1350 SMSC_TRACE(pdata
, ifup
, "Testing irq handler using IRQ %d", dev
->irq
);
1351 pdata
->software_irq_signal
= 0;
1354 temp
= smsc911x_reg_read(pdata
, INT_EN
);
1355 temp
|= INT_EN_SW_INT_EN_
;
1356 smsc911x_reg_write(pdata
, INT_EN
, temp
);
1360 if (pdata
->software_irq_signal
)
1365 if (!pdata
->software_irq_signal
) {
1366 netdev_warn(dev
, "ISR failed signaling test (IRQ %d)\n",
1370 SMSC_TRACE(pdata
, ifup
, "IRQ handler passed test using IRQ %d",
1373 netdev_info(dev
, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
1374 (unsigned long)pdata
->ioaddr
, dev
->irq
);
1376 /* Reset the last known duplex and carrier */
1377 pdata
->last_duplex
= -1;
1378 pdata
->last_carrier
= -1;
1380 /* Bring the PHY up */
1381 phy_start(pdata
->phy_dev
);
1383 temp
= smsc911x_reg_read(pdata
, HW_CFG
);
1384 /* Preserve TX FIFO size and external PHY configuration */
1385 temp
&= (HW_CFG_TX_FIF_SZ_
|0x00000FFF);
1387 smsc911x_reg_write(pdata
, HW_CFG
, temp
);
1389 temp
= smsc911x_reg_read(pdata
, FIFO_INT
);
1390 temp
|= FIFO_INT_TX_AVAIL_LEVEL_
;
1391 temp
&= ~(FIFO_INT_RX_STS_LEVEL_
);
1392 smsc911x_reg_write(pdata
, FIFO_INT
, temp
);
1394 /* set RX Data offset to 2 bytes for alignment */
1395 smsc911x_reg_write(pdata
, RX_CFG
, (2 << 8));
1397 /* enable NAPI polling before enabling RX interrupts */
1398 napi_enable(&pdata
->napi
);
1400 temp
= smsc911x_reg_read(pdata
, INT_EN
);
1401 temp
|= (INT_EN_TDFA_EN_
| INT_EN_RSFL_EN_
| INT_EN_RXSTOP_INT_EN_
);
1402 smsc911x_reg_write(pdata
, INT_EN
, temp
);
1404 spin_lock_irq(&pdata
->mac_lock
);
1405 temp
= smsc911x_mac_read(pdata
, MAC_CR
);
1406 temp
|= (MAC_CR_TXEN_
| MAC_CR_RXEN_
| MAC_CR_HBDIS_
);
1407 smsc911x_mac_write(pdata
, MAC_CR
, temp
);
1408 spin_unlock_irq(&pdata
->mac_lock
);
1410 smsc911x_reg_write(pdata
, TX_CFG
, TX_CFG_TX_ON_
);
1412 netif_start_queue(dev
);
1416 /* Entry point for stopping the interface */
1417 static int smsc911x_stop(struct net_device
*dev
)
1419 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1422 /* Disable all device interrupts */
1423 temp
= smsc911x_reg_read(pdata
, INT_CFG
);
1424 temp
&= ~INT_CFG_IRQ_EN_
;
1425 smsc911x_reg_write(pdata
, INT_CFG
, temp
);
1427 /* Stop Tx and Rx polling */
1428 netif_stop_queue(dev
);
1429 napi_disable(&pdata
->napi
);
1431 /* At this point all Rx and Tx activity is stopped */
1432 dev
->stats
.rx_dropped
+= smsc911x_reg_read(pdata
, RX_DROP
);
1433 smsc911x_tx_update_txcounters(dev
);
1435 /* Bring the PHY down */
1437 phy_stop(pdata
->phy_dev
);
1439 SMSC_TRACE(pdata
, ifdown
, "Interface stopped");
1443 /* Entry point for transmitting a packet */
1444 static int smsc911x_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1446 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1447 unsigned int freespace
;
1448 unsigned int tx_cmd_a
;
1449 unsigned int tx_cmd_b
;
1454 freespace
= smsc911x_reg_read(pdata
, TX_FIFO_INF
) & TX_FIFO_INF_TDFREE_
;
1456 if (unlikely(freespace
< TX_FIFO_LOW_THRESHOLD
))
1457 SMSC_WARN(pdata
, tx_err
,
1458 "Tx data fifo low, space available: %d", freespace
);
1460 /* Word alignment adjustment */
1461 tx_cmd_a
= (u32
)((ulong
)skb
->data
& 0x03) << 16;
1462 tx_cmd_a
|= TX_CMD_A_FIRST_SEG_
| TX_CMD_A_LAST_SEG_
;
1463 tx_cmd_a
|= (unsigned int)skb
->len
;
1465 tx_cmd_b
= ((unsigned int)skb
->len
) << 16;
1466 tx_cmd_b
|= (unsigned int)skb
->len
;
1468 smsc911x_reg_write(pdata
, TX_DATA_FIFO
, tx_cmd_a
);
1469 smsc911x_reg_write(pdata
, TX_DATA_FIFO
, tx_cmd_b
);
1471 bufp
= (ulong
)skb
->data
& (~0x3);
1472 wrsz
= (u32
)skb
->len
+ 3;
1473 wrsz
+= (u32
)((ulong
)skb
->data
& 0x3);
1476 pdata
->ops
->tx_writefifo(pdata
, (unsigned int *)bufp
, wrsz
);
1477 freespace
-= (skb
->len
+ 32);
1478 skb_tx_timestamp(skb
);
1481 if (unlikely(smsc911x_tx_get_txstatcount(pdata
) >= 30))
1482 smsc911x_tx_update_txcounters(dev
);
1484 if (freespace
< TX_FIFO_LOW_THRESHOLD
) {
1485 netif_stop_queue(dev
);
1486 temp
= smsc911x_reg_read(pdata
, FIFO_INT
);
1489 smsc911x_reg_write(pdata
, FIFO_INT
, temp
);
1492 return NETDEV_TX_OK
;
1495 /* Entry point for getting status counters */
1496 static struct net_device_stats
*smsc911x_get_stats(struct net_device
*dev
)
1498 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1499 smsc911x_tx_update_txcounters(dev
);
1500 dev
->stats
.rx_dropped
+= smsc911x_reg_read(pdata
, RX_DROP
);
1504 /* Entry point for setting addressing modes */
1505 static void smsc911x_set_multicast_list(struct net_device
*dev
)
1507 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1508 unsigned long flags
;
1510 if (dev
->flags
& IFF_PROMISC
) {
1511 /* Enabling promiscuous mode */
1512 pdata
->set_bits_mask
= MAC_CR_PRMS_
;
1513 pdata
->clear_bits_mask
= (MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
1516 } else if (dev
->flags
& IFF_ALLMULTI
) {
1517 /* Enabling all multicast mode */
1518 pdata
->set_bits_mask
= MAC_CR_MCPAS_
;
1519 pdata
->clear_bits_mask
= (MAC_CR_PRMS_
| MAC_CR_HPFILT_
);
1522 } else if (!netdev_mc_empty(dev
)) {
1523 /* Enabling specific multicast addresses */
1524 unsigned int hash_high
= 0;
1525 unsigned int hash_low
= 0;
1526 struct netdev_hw_addr
*ha
;
1528 pdata
->set_bits_mask
= MAC_CR_HPFILT_
;
1529 pdata
->clear_bits_mask
= (MAC_CR_PRMS_
| MAC_CR_MCPAS_
);
1531 netdev_for_each_mc_addr(ha
, dev
) {
1532 unsigned int bitnum
= smsc911x_hash(ha
->addr
);
1533 unsigned int mask
= 0x01 << (bitnum
& 0x1F);
1541 pdata
->hashhi
= hash_high
;
1542 pdata
->hashlo
= hash_low
;
1544 /* Enabling local MAC address only */
1545 pdata
->set_bits_mask
= 0;
1546 pdata
->clear_bits_mask
=
1547 (MAC_CR_PRMS_
| MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
1552 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
1554 if (pdata
->generation
<= 1) {
1555 /* Older hardware revision - cannot change these flags while
1557 if (!pdata
->multicast_update_pending
) {
1559 SMSC_TRACE(pdata
, hw
, "scheduling mcast update");
1560 pdata
->multicast_update_pending
= 1;
1562 /* Request the hardware to stop, then perform the
1563 * update when we get an RX_STOP interrupt */
1564 temp
= smsc911x_mac_read(pdata
, MAC_CR
);
1565 temp
&= ~(MAC_CR_RXEN_
);
1566 smsc911x_mac_write(pdata
, MAC_CR
, temp
);
1568 /* There is another update pending, this should now
1569 * use the newer values */
1572 /* Newer hardware revision - can write immediately */
1573 smsc911x_rx_multicast_update(pdata
);
1576 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
1579 static irqreturn_t
smsc911x_irqhandler(int irq
, void *dev_id
)
1581 struct net_device
*dev
= dev_id
;
1582 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1583 u32 intsts
= smsc911x_reg_read(pdata
, INT_STS
);
1584 u32 inten
= smsc911x_reg_read(pdata
, INT_EN
);
1585 int serviced
= IRQ_NONE
;
1588 if (unlikely(intsts
& inten
& INT_STS_SW_INT_
)) {
1589 temp
= smsc911x_reg_read(pdata
, INT_EN
);
1590 temp
&= (~INT_EN_SW_INT_EN_
);
1591 smsc911x_reg_write(pdata
, INT_EN
, temp
);
1592 smsc911x_reg_write(pdata
, INT_STS
, INT_STS_SW_INT_
);
1593 pdata
->software_irq_signal
= 1;
1595 serviced
= IRQ_HANDLED
;
1598 if (unlikely(intsts
& inten
& INT_STS_RXSTOP_INT_
)) {
1599 /* Called when there is a multicast update scheduled and
1600 * it is now safe to complete the update */
1601 SMSC_TRACE(pdata
, intr
, "RX Stop interrupt");
1602 smsc911x_reg_write(pdata
, INT_STS
, INT_STS_RXSTOP_INT_
);
1603 if (pdata
->multicast_update_pending
)
1604 smsc911x_rx_multicast_update_workaround(pdata
);
1605 serviced
= IRQ_HANDLED
;
1608 if (intsts
& inten
& INT_STS_TDFA_
) {
1609 temp
= smsc911x_reg_read(pdata
, FIFO_INT
);
1610 temp
|= FIFO_INT_TX_AVAIL_LEVEL_
;
1611 smsc911x_reg_write(pdata
, FIFO_INT
, temp
);
1612 smsc911x_reg_write(pdata
, INT_STS
, INT_STS_TDFA_
);
1613 netif_wake_queue(dev
);
1614 serviced
= IRQ_HANDLED
;
1617 if (unlikely(intsts
& inten
& INT_STS_RXE_
)) {
1618 SMSC_TRACE(pdata
, intr
, "RX Error interrupt");
1619 smsc911x_reg_write(pdata
, INT_STS
, INT_STS_RXE_
);
1620 serviced
= IRQ_HANDLED
;
1623 if (likely(intsts
& inten
& INT_STS_RSFL_
)) {
1624 if (likely(napi_schedule_prep(&pdata
->napi
))) {
1625 /* Disable Rx interrupts */
1626 temp
= smsc911x_reg_read(pdata
, INT_EN
);
1627 temp
&= (~INT_EN_RSFL_EN_
);
1628 smsc911x_reg_write(pdata
, INT_EN
, temp
);
1629 /* Schedule a NAPI poll */
1630 __napi_schedule(&pdata
->napi
);
1632 SMSC_WARN(pdata
, rx_err
, "napi_schedule_prep failed");
1634 serviced
= IRQ_HANDLED
;
1640 #ifdef CONFIG_NET_POLL_CONTROLLER
1641 static void smsc911x_poll_controller(struct net_device
*dev
)
1643 disable_irq(dev
->irq
);
1644 smsc911x_irqhandler(0, dev
);
1645 enable_irq(dev
->irq
);
1647 #endif /* CONFIG_NET_POLL_CONTROLLER */
1649 static int smsc911x_set_mac_address(struct net_device
*dev
, void *p
)
1651 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1652 struct sockaddr
*addr
= p
;
1654 /* On older hardware revisions we cannot change the mac address
1655 * registers while receiving data. Newer devices can safely change
1656 * this at any time. */
1657 if (pdata
->generation
<= 1 && netif_running(dev
))
1660 if (!is_valid_ether_addr(addr
->sa_data
))
1661 return -EADDRNOTAVAIL
;
1663 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
1665 spin_lock_irq(&pdata
->mac_lock
);
1666 smsc911x_set_hw_mac_address(pdata
, dev
->dev_addr
);
1667 spin_unlock_irq(&pdata
->mac_lock
);
1669 netdev_info(dev
, "MAC Address: %pM\n", dev
->dev_addr
);
1674 /* Standard ioctls for mii-tool */
1675 static int smsc911x_do_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1677 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1679 if (!netif_running(dev
) || !pdata
->phy_dev
)
1682 return phy_mii_ioctl(pdata
->phy_dev
, ifr
, cmd
);
1686 smsc911x_ethtool_getsettings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1688 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1692 return phy_ethtool_gset(pdata
->phy_dev
, cmd
);
1696 smsc911x_ethtool_setsettings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1698 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1700 return phy_ethtool_sset(pdata
->phy_dev
, cmd
);
1703 static void smsc911x_ethtool_getdrvinfo(struct net_device
*dev
,
1704 struct ethtool_drvinfo
*info
)
1706 strlcpy(info
->driver
, SMSC_CHIPNAME
, sizeof(info
->driver
));
1707 strlcpy(info
->version
, SMSC_DRV_VERSION
, sizeof(info
->version
));
1708 strlcpy(info
->bus_info
, dev_name(dev
->dev
.parent
),
1709 sizeof(info
->bus_info
));
1712 static int smsc911x_ethtool_nwayreset(struct net_device
*dev
)
1714 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1716 return phy_start_aneg(pdata
->phy_dev
);
1719 static u32
smsc911x_ethtool_getmsglevel(struct net_device
*dev
)
1721 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1722 return pdata
->msg_enable
;
1725 static void smsc911x_ethtool_setmsglevel(struct net_device
*dev
, u32 level
)
1727 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1728 pdata
->msg_enable
= level
;
1731 static int smsc911x_ethtool_getregslen(struct net_device
*dev
)
1733 return (((E2P_DATA
- ID_REV
) / 4 + 1) + (WUCSR
- MAC_CR
) + 1 + 32) *
1738 smsc911x_ethtool_getregs(struct net_device
*dev
, struct ethtool_regs
*regs
,
1741 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1742 struct phy_device
*phy_dev
= pdata
->phy_dev
;
1743 unsigned long flags
;
1748 regs
->version
= pdata
->idrev
;
1749 for (i
= ID_REV
; i
<= E2P_DATA
; i
+= (sizeof(u32
)))
1750 data
[j
++] = smsc911x_reg_read(pdata
, i
);
1752 for (i
= MAC_CR
; i
<= WUCSR
; i
++) {
1753 spin_lock_irqsave(&pdata
->mac_lock
, flags
);
1754 data
[j
++] = smsc911x_mac_read(pdata
, i
);
1755 spin_unlock_irqrestore(&pdata
->mac_lock
, flags
);
1758 for (i
= 0; i
<= 31; i
++)
1759 data
[j
++] = smsc911x_mii_read(phy_dev
->bus
, phy_dev
->addr
, i
);
1762 static void smsc911x_eeprom_enable_access(struct smsc911x_data
*pdata
)
1764 unsigned int temp
= smsc911x_reg_read(pdata
, GPIO_CFG
);
1765 temp
&= ~GPIO_CFG_EEPR_EN_
;
1766 smsc911x_reg_write(pdata
, GPIO_CFG
, temp
);
1770 static int smsc911x_eeprom_send_cmd(struct smsc911x_data
*pdata
, u32 op
)
1775 SMSC_TRACE(pdata
, drv
, "op 0x%08x", op
);
1776 if (smsc911x_reg_read(pdata
, E2P_CMD
) & E2P_CMD_EPC_BUSY_
) {
1777 SMSC_WARN(pdata
, drv
, "Busy at start");
1781 e2cmd
= op
| E2P_CMD_EPC_BUSY_
;
1782 smsc911x_reg_write(pdata
, E2P_CMD
, e2cmd
);
1786 e2cmd
= smsc911x_reg_read(pdata
, E2P_CMD
);
1787 } while ((e2cmd
& E2P_CMD_EPC_BUSY_
) && (--timeout
));
1790 SMSC_TRACE(pdata
, drv
, "TIMED OUT");
1794 if (e2cmd
& E2P_CMD_EPC_TIMEOUT_
) {
1795 SMSC_TRACE(pdata
, drv
, "Error occurred during eeprom operation");
1802 static int smsc911x_eeprom_read_location(struct smsc911x_data
*pdata
,
1803 u8 address
, u8
*data
)
1805 u32 op
= E2P_CMD_EPC_CMD_READ_
| address
;
1808 SMSC_TRACE(pdata
, drv
, "address 0x%x", address
);
1809 ret
= smsc911x_eeprom_send_cmd(pdata
, op
);
1812 data
[address
] = smsc911x_reg_read(pdata
, E2P_DATA
);
1817 static int smsc911x_eeprom_write_location(struct smsc911x_data
*pdata
,
1818 u8 address
, u8 data
)
1820 u32 op
= E2P_CMD_EPC_CMD_ERASE_
| address
;
1824 SMSC_TRACE(pdata
, drv
, "address 0x%x, data 0x%x", address
, data
);
1825 ret
= smsc911x_eeprom_send_cmd(pdata
, op
);
1828 op
= E2P_CMD_EPC_CMD_WRITE_
| address
;
1829 smsc911x_reg_write(pdata
, E2P_DATA
, (u32
)data
);
1831 /* Workaround for hardware read-after-write restriction */
1832 temp
= smsc911x_reg_read(pdata
, BYTE_TEST
);
1834 ret
= smsc911x_eeprom_send_cmd(pdata
, op
);
1840 static int smsc911x_ethtool_get_eeprom_len(struct net_device
*dev
)
1842 return SMSC911X_EEPROM_SIZE
;
1845 static int smsc911x_ethtool_get_eeprom(struct net_device
*dev
,
1846 struct ethtool_eeprom
*eeprom
, u8
*data
)
1848 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1849 u8 eeprom_data
[SMSC911X_EEPROM_SIZE
];
1853 smsc911x_eeprom_enable_access(pdata
);
1855 len
= min(eeprom
->len
, SMSC911X_EEPROM_SIZE
);
1856 for (i
= 0; i
< len
; i
++) {
1857 int ret
= smsc911x_eeprom_read_location(pdata
, i
, eeprom_data
);
1864 memcpy(data
, &eeprom_data
[eeprom
->offset
], len
);
1869 static int smsc911x_ethtool_set_eeprom(struct net_device
*dev
,
1870 struct ethtool_eeprom
*eeprom
, u8
*data
)
1873 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1875 smsc911x_eeprom_enable_access(pdata
);
1876 smsc911x_eeprom_send_cmd(pdata
, E2P_CMD_EPC_CMD_EWEN_
);
1877 ret
= smsc911x_eeprom_write_location(pdata
, eeprom
->offset
, *data
);
1878 smsc911x_eeprom_send_cmd(pdata
, E2P_CMD_EPC_CMD_EWDS_
);
1880 /* Single byte write, according to man page */
1886 static const struct ethtool_ops smsc911x_ethtool_ops
= {
1887 .get_settings
= smsc911x_ethtool_getsettings
,
1888 .set_settings
= smsc911x_ethtool_setsettings
,
1889 .get_link
= ethtool_op_get_link
,
1890 .get_drvinfo
= smsc911x_ethtool_getdrvinfo
,
1891 .nway_reset
= smsc911x_ethtool_nwayreset
,
1892 .get_msglevel
= smsc911x_ethtool_getmsglevel
,
1893 .set_msglevel
= smsc911x_ethtool_setmsglevel
,
1894 .get_regs_len
= smsc911x_ethtool_getregslen
,
1895 .get_regs
= smsc911x_ethtool_getregs
,
1896 .get_eeprom_len
= smsc911x_ethtool_get_eeprom_len
,
1897 .get_eeprom
= smsc911x_ethtool_get_eeprom
,
1898 .set_eeprom
= smsc911x_ethtool_set_eeprom
,
1901 static const struct net_device_ops smsc911x_netdev_ops
= {
1902 .ndo_open
= smsc911x_open
,
1903 .ndo_stop
= smsc911x_stop
,
1904 .ndo_start_xmit
= smsc911x_hard_start_xmit
,
1905 .ndo_get_stats
= smsc911x_get_stats
,
1906 .ndo_set_multicast_list
= smsc911x_set_multicast_list
,
1907 .ndo_do_ioctl
= smsc911x_do_ioctl
,
1908 .ndo_change_mtu
= eth_change_mtu
,
1909 .ndo_validate_addr
= eth_validate_addr
,
1910 .ndo_set_mac_address
= smsc911x_set_mac_address
,
1911 #ifdef CONFIG_NET_POLL_CONTROLLER
1912 .ndo_poll_controller
= smsc911x_poll_controller
,
1916 /* copies the current mac address from hardware to dev->dev_addr */
1917 static void __devinit
smsc911x_read_mac_address(struct net_device
*dev
)
1919 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1920 u32 mac_high16
= smsc911x_mac_read(pdata
, ADDRH
);
1921 u32 mac_low32
= smsc911x_mac_read(pdata
, ADDRL
);
1923 dev
->dev_addr
[0] = (u8
)(mac_low32
);
1924 dev
->dev_addr
[1] = (u8
)(mac_low32
>> 8);
1925 dev
->dev_addr
[2] = (u8
)(mac_low32
>> 16);
1926 dev
->dev_addr
[3] = (u8
)(mac_low32
>> 24);
1927 dev
->dev_addr
[4] = (u8
)(mac_high16
);
1928 dev
->dev_addr
[5] = (u8
)(mac_high16
>> 8);
1931 /* Initializing private device structures, only called from probe */
1932 static int __devinit
smsc911x_init(struct net_device
*dev
)
1934 struct smsc911x_data
*pdata
= netdev_priv(dev
);
1935 unsigned int byte_test
;
1937 SMSC_TRACE(pdata
, probe
, "Driver Parameters:");
1938 SMSC_TRACE(pdata
, probe
, "LAN base: 0x%08lX",
1939 (unsigned long)pdata
->ioaddr
);
1940 SMSC_TRACE(pdata
, probe
, "IRQ: %d", dev
->irq
);
1941 SMSC_TRACE(pdata
, probe
, "PHY will be autodetected.");
1943 spin_lock_init(&pdata
->dev_lock
);
1944 spin_lock_init(&pdata
->mac_lock
);
1946 if (pdata
->ioaddr
== 0) {
1947 SMSC_WARN(pdata
, probe
, "pdata->ioaddr: 0x00000000");
1951 /* Check byte ordering */
1952 byte_test
= smsc911x_reg_read(pdata
, BYTE_TEST
);
1953 SMSC_TRACE(pdata
, probe
, "BYTE_TEST: 0x%08X", byte_test
);
1954 if (byte_test
== 0x43218765) {
1955 SMSC_TRACE(pdata
, probe
, "BYTE_TEST looks swapped, "
1956 "applying WORD_SWAP");
1957 smsc911x_reg_write(pdata
, WORD_SWAP
, 0xffffffff);
1959 /* 1 dummy read of BYTE_TEST is needed after a write to
1960 * WORD_SWAP before its contents are valid */
1961 byte_test
= smsc911x_reg_read(pdata
, BYTE_TEST
);
1963 byte_test
= smsc911x_reg_read(pdata
, BYTE_TEST
);
1966 if (byte_test
!= 0x87654321) {
1967 SMSC_WARN(pdata
, drv
, "BYTE_TEST: 0x%08X", byte_test
);
1968 if (((byte_test
>> 16) & 0xFFFF) == (byte_test
& 0xFFFF)) {
1969 SMSC_WARN(pdata
, probe
,
1970 "top 16 bits equal to bottom 16 bits");
1971 SMSC_TRACE(pdata
, probe
,
1972 "This may mean the chip is set "
1973 "for 32 bit while the bus is reading 16 bit");
1978 /* Default generation to zero (all workarounds apply) */
1979 pdata
->generation
= 0;
1981 pdata
->idrev
= smsc911x_reg_read(pdata
, ID_REV
);
1982 switch (pdata
->idrev
& 0xFFFF0000) {
1988 /* LAN911[5678] family */
1989 pdata
->generation
= pdata
->idrev
& 0x0000FFFF;
1996 /* LAN921[5678] family */
1997 pdata
->generation
= 3;
2004 /* LAN9210/LAN9211/LAN9220/LAN9221 */
2005 pdata
->generation
= 4;
2009 SMSC_WARN(pdata
, probe
, "LAN911x not identified, idrev: 0x%08X",
2014 SMSC_TRACE(pdata
, probe
,
2015 "LAN911x identified, idrev: 0x%08X, generation: %d",
2016 pdata
->idrev
, pdata
->generation
);
2018 if (pdata
->generation
== 0)
2019 SMSC_WARN(pdata
, probe
,
2020 "This driver is not intended for this chip revision");
2022 /* workaround for platforms without an eeprom, where the mac address
2023 * is stored elsewhere and set by the bootloader. This saves the
2024 * mac address before resetting the device */
2025 if (pdata
->config
.flags
& SMSC911X_SAVE_MAC_ADDRESS
) {
2026 spin_lock_irq(&pdata
->mac_lock
);
2027 smsc911x_read_mac_address(dev
);
2028 spin_unlock_irq(&pdata
->mac_lock
);
2031 /* Reset the LAN911x */
2032 if (smsc911x_soft_reset(pdata
))
2035 /* Disable all interrupt sources until we bring the device up */
2036 smsc911x_reg_write(pdata
, INT_EN
, 0);
2039 dev
->flags
|= IFF_MULTICAST
;
2040 netif_napi_add(dev
, &pdata
->napi
, smsc911x_poll
, SMSC_NAPI_WEIGHT
);
2041 dev
->netdev_ops
= &smsc911x_netdev_ops
;
2042 dev
->ethtool_ops
= &smsc911x_ethtool_ops
;
2047 static int __devexit
smsc911x_drv_remove(struct platform_device
*pdev
)
2049 struct net_device
*dev
;
2050 struct smsc911x_data
*pdata
;
2051 struct resource
*res
;
2053 dev
= platform_get_drvdata(pdev
);
2055 pdata
= netdev_priv(dev
);
2057 BUG_ON(!pdata
->ioaddr
);
2058 BUG_ON(!pdata
->phy_dev
);
2060 SMSC_TRACE(pdata
, ifdown
, "Stopping driver");
2062 phy_disconnect(pdata
->phy_dev
);
2063 pdata
->phy_dev
= NULL
;
2064 mdiobus_unregister(pdata
->mii_bus
);
2065 mdiobus_free(pdata
->mii_bus
);
2067 platform_set_drvdata(pdev
, NULL
);
2068 unregister_netdev(dev
);
2069 free_irq(dev
->irq
, dev
);
2070 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
2073 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2075 release_mem_region(res
->start
, resource_size(res
));
2077 iounmap(pdata
->ioaddr
);
2084 /* standard register acces */
2085 static const struct smsc911x_ops standard_smsc911x_ops
= {
2086 .reg_read
= __smsc911x_reg_read
,
2087 .reg_write
= __smsc911x_reg_write
,
2088 .rx_readfifo
= smsc911x_rx_readfifo
,
2089 .tx_writefifo
= smsc911x_tx_writefifo
,
2092 /* shifted register access */
2093 static const struct smsc911x_ops shifted_smsc911x_ops
= {
2094 .reg_read
= __smsc911x_reg_read_shift
,
2095 .reg_write
= __smsc911x_reg_write_shift
,
2096 .rx_readfifo
= smsc911x_rx_readfifo_shift
,
2097 .tx_writefifo
= smsc911x_tx_writefifo_shift
,
2100 static int __devinit
smsc911x_drv_probe(struct platform_device
*pdev
)
2102 struct net_device
*dev
;
2103 struct smsc911x_data
*pdata
;
2104 struct smsc911x_platform_config
*config
= pdev
->dev
.platform_data
;
2105 struct resource
*res
, *irq_res
;
2106 unsigned int intcfg
= 0;
2107 int res_size
, irq_flags
;
2110 pr_info("Driver version %s\n", SMSC_DRV_VERSION
);
2112 /* platform data specifies irq & dynamic bus configuration */
2113 if (!pdev
->dev
.platform_data
) {
2114 pr_warn("platform_data not provided\n");
2119 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
2122 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2124 pr_warn("Could not allocate resource\n");
2128 res_size
= resource_size(res
);
2130 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
2132 pr_warn("Could not allocate irq resource\n");
2137 if (!request_mem_region(res
->start
, res_size
, SMSC_CHIPNAME
)) {
2142 dev
= alloc_etherdev(sizeof(struct smsc911x_data
));
2144 pr_warn("Could not allocate device\n");
2146 goto out_release_io_1
;
2149 SET_NETDEV_DEV(dev
, &pdev
->dev
);
2151 pdata
= netdev_priv(dev
);
2153 dev
->irq
= irq_res
->start
;
2154 irq_flags
= irq_res
->flags
& IRQF_TRIGGER_MASK
;
2155 pdata
->ioaddr
= ioremap_nocache(res
->start
, res_size
);
2157 /* copy config parameters across to pdata */
2158 memcpy(&pdata
->config
, config
, sizeof(pdata
->config
));
2161 pdata
->msg_enable
= ((1 << debug
) - 1);
2163 if (pdata
->ioaddr
== NULL
) {
2164 SMSC_WARN(pdata
, probe
, "Error smsc911x base address invalid");
2166 goto out_free_netdev_2
;
2169 /* assume standard, non-shifted, access to HW registers */
2170 pdata
->ops
= &standard_smsc911x_ops
;
2171 /* apply the right access if shifting is needed */
2173 pdata
->ops
= &shifted_smsc911x_ops
;
2175 retval
= smsc911x_init(dev
);
2177 goto out_unmap_io_3
;
2179 /* configure irq polarity and type before connecting isr */
2180 if (pdata
->config
.irq_polarity
== SMSC911X_IRQ_POLARITY_ACTIVE_HIGH
)
2181 intcfg
|= INT_CFG_IRQ_POL_
;
2183 if (pdata
->config
.irq_type
== SMSC911X_IRQ_TYPE_PUSH_PULL
)
2184 intcfg
|= INT_CFG_IRQ_TYPE_
;
2186 smsc911x_reg_write(pdata
, INT_CFG
, intcfg
);
2188 /* Ensure interrupts are globally disabled before connecting ISR */
2189 smsc911x_reg_write(pdata
, INT_EN
, 0);
2190 smsc911x_reg_write(pdata
, INT_STS
, 0xFFFFFFFF);
2192 retval
= request_irq(dev
->irq
, smsc911x_irqhandler
,
2193 irq_flags
| IRQF_SHARED
, dev
->name
, dev
);
2195 SMSC_WARN(pdata
, probe
,
2196 "Unable to claim requested irq: %d", dev
->irq
);
2197 goto out_unmap_io_3
;
2200 platform_set_drvdata(pdev
, dev
);
2202 retval
= register_netdev(dev
);
2204 SMSC_WARN(pdata
, probe
, "Error %i registering device", retval
);
2205 goto out_unset_drvdata_4
;
2207 SMSC_TRACE(pdata
, probe
,
2208 "Network interface: \"%s\"", dev
->name
);
2211 retval
= smsc911x_mii_init(pdev
, dev
);
2213 SMSC_WARN(pdata
, probe
, "Error %i initialising mii", retval
);
2214 goto out_unregister_netdev_5
;
2217 spin_lock_irq(&pdata
->mac_lock
);
2219 /* Check if mac address has been specified when bringing interface up */
2220 if (is_valid_ether_addr(dev
->dev_addr
)) {
2221 smsc911x_set_hw_mac_address(pdata
, dev
->dev_addr
);
2222 SMSC_TRACE(pdata
, probe
,
2223 "MAC Address is specified by configuration");
2224 } else if (is_valid_ether_addr(pdata
->config
.mac
)) {
2225 memcpy(dev
->dev_addr
, pdata
->config
.mac
, 6);
2226 SMSC_TRACE(pdata
, probe
,
2227 "MAC Address specified by platform data");
2229 /* Try reading mac address from device. if EEPROM is present
2230 * it will already have been set */
2233 if (is_valid_ether_addr(dev
->dev_addr
)) {
2234 /* eeprom values are valid so use them */
2235 SMSC_TRACE(pdata
, probe
,
2236 "Mac Address is read from LAN911x EEPROM");
2238 /* eeprom values are invalid, generate random MAC */
2239 random_ether_addr(dev
->dev_addr
);
2240 smsc911x_set_hw_mac_address(pdata
, dev
->dev_addr
);
2241 SMSC_TRACE(pdata
, probe
,
2242 "MAC Address is set to random_ether_addr");
2246 spin_unlock_irq(&pdata
->mac_lock
);
2248 netdev_info(dev
, "MAC Address: %pM\n", dev
->dev_addr
);
2252 out_unregister_netdev_5
:
2253 unregister_netdev(dev
);
2254 out_unset_drvdata_4
:
2255 platform_set_drvdata(pdev
, NULL
);
2256 free_irq(dev
->irq
, dev
);
2258 iounmap(pdata
->ioaddr
);
2262 release_mem_region(res
->start
, resource_size(res
));
2268 /* This implementation assumes the devices remains powered on its VDDVARIO
2269 * pins during suspend. */
2271 /* TODO: implement freeze/thaw callbacks for hibernation.*/
2273 static int smsc911x_suspend(struct device
*dev
)
2275 struct net_device
*ndev
= dev_get_drvdata(dev
);
2276 struct smsc911x_data
*pdata
= netdev_priv(ndev
);
2278 /* enable wake on LAN, energy detection and the external PME
2280 smsc911x_reg_write(pdata
, PMT_CTRL
,
2281 PMT_CTRL_PM_MODE_D1_
| PMT_CTRL_WOL_EN_
|
2282 PMT_CTRL_ED_EN_
| PMT_CTRL_PME_EN_
);
2287 static int smsc911x_resume(struct device
*dev
)
2289 struct net_device
*ndev
= dev_get_drvdata(dev
);
2290 struct smsc911x_data
*pdata
= netdev_priv(ndev
);
2291 unsigned int to
= 100;
2293 /* Note 3.11 from the datasheet:
2294 * "When the LAN9220 is in a power saving state, a write of any
2295 * data to the BYTE_TEST register will wake-up the device."
2297 smsc911x_reg_write(pdata
, BYTE_TEST
, 0);
2299 /* poll the READY bit in PMT_CTRL. Any other access to the device is
2300 * forbidden while this bit isn't set. Try for 100ms and return -EIO
2302 while (!(smsc911x_reg_read(pdata
, PMT_CTRL
) & PMT_CTRL_READY_
) && --to
)
2305 return (to
== 0) ? -EIO
: 0;
2308 static const struct dev_pm_ops smsc911x_pm_ops
= {
2309 .suspend
= smsc911x_suspend
,
2310 .resume
= smsc911x_resume
,
2313 #define SMSC911X_PM_OPS (&smsc911x_pm_ops)
2316 #define SMSC911X_PM_OPS NULL
2319 static struct platform_driver smsc911x_driver
= {
2320 .probe
= smsc911x_drv_probe
,
2321 .remove
= __devexit_p(smsc911x_drv_remove
),
2323 .name
= SMSC_CHIPNAME
,
2324 .owner
= THIS_MODULE
,
2325 .pm
= SMSC911X_PM_OPS
,
2329 /* Entry point for loading the module */
2330 static int __init
smsc911x_init_module(void)
2333 return platform_driver_register(&smsc911x_driver
);
2336 /* entry point for unloading the module */
2337 static void __exit
smsc911x_cleanup_module(void)
2339 platform_driver_unregister(&smsc911x_driver
);
2342 module_init(smsc911x_init_module
);
2343 module_exit(smsc911x_cleanup_module
);