hv: remove free_channel() from hyperv.h
[zen-stable.git] / drivers / net / smsc911x.c
blobb9016a30cdc55fe6e9d47742ab0d6ba56af9bcf9
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
24 * Supported devices:
25 * LAN9115, LAN9116, LAN9117, LAN9118
26 * LAN9215, LAN9216, LAN9217, LAN9218
27 * LAN9210, LAN9211
28 * LAN9220, LAN9221
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>
51 #include <linux/io.h>
52 #include <linux/swab.h>
53 #include <linux/phy.h>
54 #include <linux/smsc911x.h>
55 #include <linux/device.h>
56 #include "smsc911x.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");
66 #if USE_DEBUG > 0
67 static int debug = 16;
68 #else
69 static int debug = 3;
70 #endif
72 module_param(debug, int, 0);
73 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
75 struct smsc911x_data;
77 struct smsc911x_ops {
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 {
87 void __iomem *ioaddr;
89 unsigned int idrev;
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()
100 spinlock_t mac_lock;
102 /* spinlock to ensure register accesses are serialised */
103 spinlock_t dev_lock;
105 struct phy_device *phy_dev;
106 struct mii_bus *mii_bus;
107 int phy_irq[PHY_MAX_ADDR];
108 unsigned int using_extphy;
109 int last_duplex;
110 int last_carrier;
112 u32 msg_enable;
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;
125 #endif
127 /* Members for Multicast filter workaround */
128 unsigned int multicast_update_pending;
129 unsigned int set_bits_mask;
130 unsigned int clear_bits_mask;
131 unsigned int hashhi;
132 unsigned int hashlo;
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));
150 BUG();
151 return 0;
154 static inline u32
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);
166 BUG();
167 return 0;
170 static inline u32 smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
172 u32 data;
173 unsigned long flags;
175 spin_lock_irqsave(&pdata->dev_lock, flags);
176 data = pdata->ops->reg_read(pdata, reg);
177 spin_unlock_irqrestore(&pdata->dev_lock, flags);
179 return data;
182 static inline void __smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
183 u32 val)
185 if (pdata->config.flags & SMSC911X_USE_32BIT) {
186 writel(val, pdata->ioaddr + reg);
187 return;
190 if (pdata->config.flags & SMSC911X_USE_16BIT) {
191 writew(val & 0xFFFF, pdata->ioaddr + reg);
192 writew((val >> 16) & 0xFFFF, pdata->ioaddr + reg + 2);
193 return;
196 BUG();
199 static inline void
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));
204 return;
207 if (pdata->config.flags & SMSC911X_USE_16BIT) {
208 writew(val & 0xFFFF,
209 pdata->ioaddr + __smsc_shift(pdata, reg));
210 writew((val >> 16) & 0xFFFF,
211 pdata->ioaddr + __smsc_shift(pdata, reg + 2));
212 return;
215 BUG();
218 static inline void smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
219 u32 val)
221 unsigned long flags;
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 */
229 static inline void
230 smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
231 unsigned int wordcount)
233 unsigned long flags;
235 spin_lock_irqsave(&pdata->dev_lock, flags);
237 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
238 while (wordcount--)
239 __smsc911x_reg_write(pdata, TX_DATA_FIFO,
240 swab32(*buf++));
241 goto out;
244 if (pdata->config.flags & SMSC911X_USE_32BIT) {
245 writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
246 goto out;
249 if (pdata->config.flags & SMSC911X_USE_16BIT) {
250 while (wordcount--)
251 __smsc911x_reg_write(pdata, TX_DATA_FIFO, *buf++);
252 goto out;
255 BUG();
256 out:
257 spin_unlock_irqrestore(&pdata->dev_lock, flags);
260 /* Writes a packet to the TX_DATA_FIFO - shifted version */
261 static inline void
262 smsc911x_tx_writefifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
263 unsigned int wordcount)
265 unsigned long flags;
267 spin_lock_irqsave(&pdata->dev_lock, flags);
269 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
270 while (wordcount--)
271 __smsc911x_reg_write_shift(pdata, TX_DATA_FIFO,
272 swab32(*buf++));
273 goto out;
276 if (pdata->config.flags & SMSC911X_USE_32BIT) {
277 writesl(pdata->ioaddr + __smsc_shift(pdata,
278 TX_DATA_FIFO), buf, wordcount);
279 goto out;
282 if (pdata->config.flags & SMSC911X_USE_16BIT) {
283 while (wordcount--)
284 __smsc911x_reg_write_shift(pdata,
285 TX_DATA_FIFO, *buf++);
286 goto out;
289 BUG();
290 out:
291 spin_unlock_irqrestore(&pdata->dev_lock, flags);
294 /* Reads a packet out of the RX_DATA_FIFO */
295 static inline void
296 smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
297 unsigned int wordcount)
299 unsigned long flags;
301 spin_lock_irqsave(&pdata->dev_lock, flags);
303 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
304 while (wordcount--)
305 *buf++ = swab32(__smsc911x_reg_read(pdata,
306 RX_DATA_FIFO));
307 goto out;
310 if (pdata->config.flags & SMSC911X_USE_32BIT) {
311 readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
312 goto out;
315 if (pdata->config.flags & SMSC911X_USE_16BIT) {
316 while (wordcount--)
317 *buf++ = __smsc911x_reg_read(pdata, RX_DATA_FIFO);
318 goto out;
321 BUG();
322 out:
323 spin_unlock_irqrestore(&pdata->dev_lock, flags);
326 /* Reads a packet out of the RX_DATA_FIFO - shifted version */
327 static inline void
328 smsc911x_rx_readfifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
329 unsigned int wordcount)
331 unsigned long flags;
333 spin_lock_irqsave(&pdata->dev_lock, flags);
335 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
336 while (wordcount--)
337 *buf++ = swab32(__smsc911x_reg_read_shift(pdata,
338 RX_DATA_FIFO));
339 goto out;
342 if (pdata->config.flags & SMSC911X_USE_32BIT) {
343 readsl(pdata->ioaddr + __smsc_shift(pdata,
344 RX_DATA_FIFO), buf, wordcount);
345 goto out;
348 if (pdata->config.flags & SMSC911X_USE_16BIT) {
349 while (wordcount--)
350 *buf++ = __smsc911x_reg_read_shift(pdata,
351 RX_DATA_FIFO);
352 goto out;
355 BUG();
356 out:
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)
364 int i;
365 u32 val;
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_))
372 return 0;
374 SMSC_WARN(pdata, hw, "Timed out waiting for MAC not BUSY. "
375 "MAC_CSR_CMD: 0x%08X", val);
376 return -EIO;
379 /* Fetches a MAC register value. Assumes mac_lock is acquired */
380 static u32 smsc911x_mac_read(struct smsc911x_data *pdata, unsigned int offset)
382 unsigned int temp;
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");
389 return 0xFFFFFFFF;
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");
404 return 0xFFFFFFFF;
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)
411 unsigned int temp;
413 SMSC_ASSERT_MAC_LOCK(pdata);
415 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
416 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
417 SMSC_WARN(pdata, hw,
418 "smsc911x_mac_write failed, MAC busy at entry");
419 return;
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))
434 return;
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;
443 unsigned long flags;
444 unsigned int addr;
445 int i, reg;
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???");
452 reg = -EIO;
453 goto out;
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);
464 goto out;
467 SMSC_WARN(pdata, hw, "Timed out waiting for MII read to finish");
468 reg = -EIO;
470 out:
471 spin_unlock_irqrestore(&pdata->mac_lock, flags);
472 return reg;
475 /* Set a phy register */
476 static int smsc911x_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
477 u16 val)
479 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
480 unsigned long flags;
481 unsigned int addr;
482 int i, reg;
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???");
489 reg = -EIO;
490 goto out;
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) |
498 MII_ACC_MII_WRITE_;
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_)) {
504 reg = 0;
505 goto out;
508 SMSC_WARN(pdata, hw, "Timed out waiting for MII write to finish");
509 reg = -EIO;
511 out:
512 spin_unlock_irqrestore(&pdata->mac_lock, flags);
513 return reg;
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;
560 } else {
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_;
573 if (result != 0)
574 result = smsc911x_reg_read(pdata, TX_STATUS_FIFO);
576 return result;
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_;
585 if (result != 0)
586 result = smsc911x_reg_read(pdata, RX_STATUS_FIFO);
588 return result;
591 #ifdef USE_PHY_WORK_AROUND
592 static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data *pdata)
594 unsigned int tries;
595 u32 wrsz;
596 u32 rdsz;
597 ulong bufp;
599 for (tries = 0; tries < 10; tries++) {
600 unsigned int txcmd_a;
601 unsigned int txcmd_b;
602 unsigned int status;
603 unsigned int pktlength;
604 unsigned int i;
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);
622 wrsz >>= 2;
624 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
626 /* Wait till transmit is done */
627 i = 60;
628 do {
629 udelay(5);
630 status = smsc911x_tx_get_txstatus(pdata);
631 } while ((i--) && (!status));
633 if (!status) {
634 SMSC_WARN(pdata, hw,
635 "Failed to transmit during loopback test");
636 continue;
638 if (status & TX_STS_ES_) {
639 SMSC_WARN(pdata, hw,
640 "Transmit encountered errors during loopback test");
641 continue;
644 /* Wait till receive is done */
645 i = 60;
646 do {
647 udelay(5);
648 status = smsc911x_rx_get_rxstatus(pdata);
649 } while ((i--) && (!status));
651 if (!status) {
652 SMSC_WARN(pdata, hw,
653 "Failed to receive during loopback test");
654 continue;
656 if (status & RX_STS_ES_) {
657 SMSC_WARN(pdata, hw,
658 "Receive encountered errors during loopback test");
659 continue;
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);
666 rdsz >>= 2;
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",
673 pktlength);
674 } else {
675 unsigned int j;
676 int mismatch = 0;
677 for (j = 0; j < MIN_PACKET_SIZE; j++) {
678 if (pdata->loopback_tx_pkt[j]
679 != pdata->loopback_rx_pkt[j]) {
680 mismatch = 1;
681 break;
684 if (!mismatch) {
685 SMSC_TRACE(pdata, hw, "Successfully verified "
686 "loopback packet");
687 return 0;
688 } else {
689 SMSC_WARN(pdata, hw, "Data mismatch "
690 "during loop back test, will retry");
695 return -EIO;
698 static int smsc911x_phy_reset(struct smsc911x_data *pdata)
700 struct phy_device *phy_dev = pdata->phy_dev;
701 unsigned int temp;
702 unsigned int i = 100000;
704 BUG_ON(!phy_dev);
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);
709 do {
710 msleep(1);
711 temp = smsc911x_mii_read(phy_dev->bus, phy_dev->addr,
712 MII_BMCR);
713 } while ((i--) && (temp & BMCR_RESET));
715 if (temp & BMCR_RESET) {
716 SMSC_WARN(pdata, hw, "PHY reset failed to complete");
717 return -EIO;
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 */
722 msleep(1);
724 return 0;
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;
731 int result = -EIO;
732 unsigned int i, val;
733 unsigned long flags;
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_;
751 val |= HW_CFG_SF_;
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) {
770 result = 0;
771 break;
773 pdata->resetcount++;
775 /* Disable MAC rx */
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);
783 /* Disable MAC */
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);
794 return result;
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);
802 u32 flow;
803 unsigned long flags;
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)
811 flow = 0xFFFF0002;
812 else
813 flow = 0;
815 if (cap & FLOW_CTRL_TX)
816 afc |= 0xF;
817 else
818 afc &= ~0xF;
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"));
823 } else {
824 SMSC_TRACE(pdata, hw, "half duplex");
825 flow = 0;
826 afc |= 0xF;
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;
842 unsigned long flags;
843 int carrier;
845 if (phy_dev->duplex != pdata->last_duplex) {
846 unsigned int mac_cr;
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_;
855 } else {
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");
870 if (carrier) {
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);
879 } else {
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,
884 GPIO_CFG);
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_
893 | GPIO_CFG_GPIODIR0_
894 | GPIO_CFG_GPIOD0_);
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;
907 int ret;
909 /* find the first phy */
910 phydev = phy_find_first(pdata->mii_bus);
911 if (!phydev) {
912 netdev_err(dev, "no PHY found\n");
913 return -ENODEV;
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);
923 if (ret) {
924 netdev_err(dev, "Could not attach to PHY\n");
925 return ret;
928 netdev_info(dev,
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");
944 return -ENODEV;
946 SMSC_TRACE(pdata, hw, "Passed Loop Back Test");
947 #endif /* USE_PHY_WORK_AROUND */
949 SMSC_TRACE(pdata, hw, "phy initialised successfully");
950 return 0;
953 static int __devinit smsc911x_mii_init(struct platform_device *pdev,
954 struct net_device *dev)
956 struct smsc911x_data *pdata = netdev_priv(dev);
957 int err = -ENXIO, i;
959 pdata->mii_bus = mdiobus_alloc();
960 if (!pdata->mii_bus) {
961 err = -ENOMEM;
962 goto err_out_1;
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) {
977 case 0x01170000:
978 case 0x01150000:
979 case 0x117A0000:
980 case 0x115A0000:
981 /* External PHY supported, try to autodetect */
982 smsc911x_phy_initialise_external(pdata);
983 break;
984 default:
985 SMSC_TRACE(pdata, hw, "External PHY is not supported, "
986 "using internal PHY");
987 pdata->using_extphy = 0;
988 break;
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;
1006 return 0;
1008 err_out_unregister_bus_3:
1009 mdiobus_unregister(pdata->mii_bus);
1010 err_out_free_bus_2:
1011 mdiobus_free(pdata->mii_bus);
1012 err_out_1:
1013 return err;
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");
1039 } else {
1040 if (unlikely(tx_stat & TX_STS_ES_)) {
1041 dev->stats.tx_errors++;
1042 } else {
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;
1049 } else {
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 */
1064 static void
1065 smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
1067 int crc_err = 0;
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++;
1073 crc_err = 1;
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 */
1086 static void
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;
1093 unsigned int val;
1094 smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
1095 do {
1096 udelay(1);
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);
1103 } else {
1104 unsigned int temp;
1105 while (pktwords--)
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;
1116 int npackets = 0;
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);
1124 if (!rxstat) {
1125 unsigned int temp;
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);
1133 break;
1136 /* Count packet for NAPI scheduling, even if it has an error.
1137 * Error packets still require cycles to discard */
1138 npackets++;
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
1148 * the next */
1149 smsc911x_rx_fastforward(pdata, pktwords);
1150 dev->stats.rx_dropped++;
1151 continue;
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++;
1161 break;
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 */
1182 return npackets;
1185 /* Returns hash bit number for given MAC address
1186 * Example:
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
1218 * registers.
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);
1231 /* Re-enable Rx */
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;
1244 unsigned int temp;
1246 /* Reset the LAN911x */
1247 smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
1248 timeout = 10;
1249 do {
1250 udelay(10);
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");
1256 return -EIO;
1258 return 0;
1261 /* Sets the device MAC address to dev_addr, called with mac_lock held */
1262 static void
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;
1279 unsigned int temp;
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");
1285 return -EAGAIN;
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");
1296 return -EIO;
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 */
1308 timeout = 50;
1309 while ((smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) &&
1310 --timeout) {
1311 udelay(10);
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_;
1336 } else {
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_;
1343 } else {
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;
1351 smp_wmb();
1353 temp = smsc911x_reg_read(pdata, INT_EN);
1354 temp |= INT_EN_SW_INT_EN_;
1355 smsc911x_reg_write(pdata, INT_EN, temp);
1357 timeout = 1000;
1358 while (timeout--) {
1359 if (pdata->software_irq_signal)
1360 break;
1361 msleep(1);
1364 if (!pdata->software_irq_signal) {
1365 netdev_warn(dev, "ISR failed signaling test (IRQ %d)\n",
1366 dev->irq);
1367 return -ENODEV;
1369 SMSC_TRACE(pdata, ifup, "IRQ handler passed test using IRQ %d",
1370 dev->irq);
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);
1385 temp |= HW_CFG_SF_;
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);
1412 return 0;
1415 /* Entry point for stopping the interface */
1416 static int smsc911x_stop(struct net_device *dev)
1418 struct smsc911x_data *pdata = netdev_priv(dev);
1419 unsigned int temp;
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 */
1435 if (pdata->phy_dev)
1436 phy_stop(pdata->phy_dev);
1438 SMSC_TRACE(pdata, ifdown, "Interface stopped");
1439 return 0;
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;
1449 unsigned int temp;
1450 u32 wrsz;
1451 ulong bufp;
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);
1473 wrsz >>= 2;
1475 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
1476 freespace -= (skb->len + 32);
1477 skb_tx_timestamp(skb);
1478 dev_kfree_skb(skb);
1480 if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
1481 smsc911x_tx_update_txcounters(dev);
1483 if (freespace < TX_FIFO_LOW_THRESHOLD) {
1484 netif_stop_queue(dev);
1485 temp = smsc911x_reg_read(pdata, FIFO_INT);
1486 temp &= 0x00FFFFFF;
1487 temp |= 0x32000000;
1488 smsc911x_reg_write(pdata, FIFO_INT, temp);
1491 return NETDEV_TX_OK;
1494 /* Entry point for getting status counters */
1495 static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
1497 struct smsc911x_data *pdata = netdev_priv(dev);
1498 smsc911x_tx_update_txcounters(dev);
1499 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1500 return &dev->stats;
1503 /* Entry point for setting addressing modes */
1504 static void smsc911x_set_multicast_list(struct net_device *dev)
1506 struct smsc911x_data *pdata = netdev_priv(dev);
1507 unsigned long flags;
1509 if (dev->flags & IFF_PROMISC) {
1510 /* Enabling promiscuous mode */
1511 pdata->set_bits_mask = MAC_CR_PRMS_;
1512 pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1513 pdata->hashhi = 0;
1514 pdata->hashlo = 0;
1515 } else if (dev->flags & IFF_ALLMULTI) {
1516 /* Enabling all multicast mode */
1517 pdata->set_bits_mask = MAC_CR_MCPAS_;
1518 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
1519 pdata->hashhi = 0;
1520 pdata->hashlo = 0;
1521 } else if (!netdev_mc_empty(dev)) {
1522 /* Enabling specific multicast addresses */
1523 unsigned int hash_high = 0;
1524 unsigned int hash_low = 0;
1525 struct netdev_hw_addr *ha;
1527 pdata->set_bits_mask = MAC_CR_HPFILT_;
1528 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1530 netdev_for_each_mc_addr(ha, dev) {
1531 unsigned int bitnum = smsc911x_hash(ha->addr);
1532 unsigned int mask = 0x01 << (bitnum & 0x1F);
1534 if (bitnum & 0x20)
1535 hash_high |= mask;
1536 else
1537 hash_low |= mask;
1540 pdata->hashhi = hash_high;
1541 pdata->hashlo = hash_low;
1542 } else {
1543 /* Enabling local MAC address only */
1544 pdata->set_bits_mask = 0;
1545 pdata->clear_bits_mask =
1546 (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1547 pdata->hashhi = 0;
1548 pdata->hashlo = 0;
1551 spin_lock_irqsave(&pdata->mac_lock, flags);
1553 if (pdata->generation <= 1) {
1554 /* Older hardware revision - cannot change these flags while
1555 * receiving data */
1556 if (!pdata->multicast_update_pending) {
1557 unsigned int temp;
1558 SMSC_TRACE(pdata, hw, "scheduling mcast update");
1559 pdata->multicast_update_pending = 1;
1561 /* Request the hardware to stop, then perform the
1562 * update when we get an RX_STOP interrupt */
1563 temp = smsc911x_mac_read(pdata, MAC_CR);
1564 temp &= ~(MAC_CR_RXEN_);
1565 smsc911x_mac_write(pdata, MAC_CR, temp);
1566 } else {
1567 /* There is another update pending, this should now
1568 * use the newer values */
1570 } else {
1571 /* Newer hardware revision - can write immediately */
1572 smsc911x_rx_multicast_update(pdata);
1575 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1578 static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
1580 struct net_device *dev = dev_id;
1581 struct smsc911x_data *pdata = netdev_priv(dev);
1582 u32 intsts = smsc911x_reg_read(pdata, INT_STS);
1583 u32 inten = smsc911x_reg_read(pdata, INT_EN);
1584 int serviced = IRQ_NONE;
1585 u32 temp;
1587 if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
1588 temp = smsc911x_reg_read(pdata, INT_EN);
1589 temp &= (~INT_EN_SW_INT_EN_);
1590 smsc911x_reg_write(pdata, INT_EN, temp);
1591 smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
1592 pdata->software_irq_signal = 1;
1593 smp_wmb();
1594 serviced = IRQ_HANDLED;
1597 if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
1598 /* Called when there is a multicast update scheduled and
1599 * it is now safe to complete the update */
1600 SMSC_TRACE(pdata, intr, "RX Stop interrupt");
1601 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
1602 if (pdata->multicast_update_pending)
1603 smsc911x_rx_multicast_update_workaround(pdata);
1604 serviced = IRQ_HANDLED;
1607 if (intsts & inten & INT_STS_TDFA_) {
1608 temp = smsc911x_reg_read(pdata, FIFO_INT);
1609 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1610 smsc911x_reg_write(pdata, FIFO_INT, temp);
1611 smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
1612 netif_wake_queue(dev);
1613 serviced = IRQ_HANDLED;
1616 if (unlikely(intsts & inten & INT_STS_RXE_)) {
1617 SMSC_TRACE(pdata, intr, "RX Error interrupt");
1618 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
1619 serviced = IRQ_HANDLED;
1622 if (likely(intsts & inten & INT_STS_RSFL_)) {
1623 if (likely(napi_schedule_prep(&pdata->napi))) {
1624 /* Disable Rx interrupts */
1625 temp = smsc911x_reg_read(pdata, INT_EN);
1626 temp &= (~INT_EN_RSFL_EN_);
1627 smsc911x_reg_write(pdata, INT_EN, temp);
1628 /* Schedule a NAPI poll */
1629 __napi_schedule(&pdata->napi);
1630 } else {
1631 SMSC_WARN(pdata, rx_err, "napi_schedule_prep failed");
1633 serviced = IRQ_HANDLED;
1636 return serviced;
1639 #ifdef CONFIG_NET_POLL_CONTROLLER
1640 static void smsc911x_poll_controller(struct net_device *dev)
1642 disable_irq(dev->irq);
1643 smsc911x_irqhandler(0, dev);
1644 enable_irq(dev->irq);
1646 #endif /* CONFIG_NET_POLL_CONTROLLER */
1648 static int smsc911x_set_mac_address(struct net_device *dev, void *p)
1650 struct smsc911x_data *pdata = netdev_priv(dev);
1651 struct sockaddr *addr = p;
1653 /* On older hardware revisions we cannot change the mac address
1654 * registers while receiving data. Newer devices can safely change
1655 * this at any time. */
1656 if (pdata->generation <= 1 && netif_running(dev))
1657 return -EBUSY;
1659 if (!is_valid_ether_addr(addr->sa_data))
1660 return -EADDRNOTAVAIL;
1662 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1664 spin_lock_irq(&pdata->mac_lock);
1665 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
1666 spin_unlock_irq(&pdata->mac_lock);
1668 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
1670 return 0;
1673 /* Standard ioctls for mii-tool */
1674 static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1676 struct smsc911x_data *pdata = netdev_priv(dev);
1678 if (!netif_running(dev) || !pdata->phy_dev)
1679 return -EINVAL;
1681 return phy_mii_ioctl(pdata->phy_dev, ifr, cmd);
1684 static int
1685 smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1687 struct smsc911x_data *pdata = netdev_priv(dev);
1689 cmd->maxtxpkt = 1;
1690 cmd->maxrxpkt = 1;
1691 return phy_ethtool_gset(pdata->phy_dev, cmd);
1694 static int
1695 smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1697 struct smsc911x_data *pdata = netdev_priv(dev);
1699 return phy_ethtool_sset(pdata->phy_dev, cmd);
1702 static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
1703 struct ethtool_drvinfo *info)
1705 strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
1706 strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
1707 strlcpy(info->bus_info, dev_name(dev->dev.parent),
1708 sizeof(info->bus_info));
1711 static int smsc911x_ethtool_nwayreset(struct net_device *dev)
1713 struct smsc911x_data *pdata = netdev_priv(dev);
1715 return phy_start_aneg(pdata->phy_dev);
1718 static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
1720 struct smsc911x_data *pdata = netdev_priv(dev);
1721 return pdata->msg_enable;
1724 static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
1726 struct smsc911x_data *pdata = netdev_priv(dev);
1727 pdata->msg_enable = level;
1730 static int smsc911x_ethtool_getregslen(struct net_device *dev)
1732 return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
1733 sizeof(u32);
1736 static void
1737 smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
1738 void *buf)
1740 struct smsc911x_data *pdata = netdev_priv(dev);
1741 struct phy_device *phy_dev = pdata->phy_dev;
1742 unsigned long flags;
1743 unsigned int i;
1744 unsigned int j = 0;
1745 u32 *data = buf;
1747 regs->version = pdata->idrev;
1748 for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
1749 data[j++] = smsc911x_reg_read(pdata, i);
1751 for (i = MAC_CR; i <= WUCSR; i++) {
1752 spin_lock_irqsave(&pdata->mac_lock, flags);
1753 data[j++] = smsc911x_mac_read(pdata, i);
1754 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1757 for (i = 0; i <= 31; i++)
1758 data[j++] = smsc911x_mii_read(phy_dev->bus, phy_dev->addr, i);
1761 static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
1763 unsigned int temp = smsc911x_reg_read(pdata, GPIO_CFG);
1764 temp &= ~GPIO_CFG_EEPR_EN_;
1765 smsc911x_reg_write(pdata, GPIO_CFG, temp);
1766 msleep(1);
1769 static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
1771 int timeout = 100;
1772 u32 e2cmd;
1774 SMSC_TRACE(pdata, drv, "op 0x%08x", op);
1775 if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
1776 SMSC_WARN(pdata, drv, "Busy at start");
1777 return -EBUSY;
1780 e2cmd = op | E2P_CMD_EPC_BUSY_;
1781 smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
1783 do {
1784 msleep(1);
1785 e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
1786 } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
1788 if (!timeout) {
1789 SMSC_TRACE(pdata, drv, "TIMED OUT");
1790 return -EAGAIN;
1793 if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
1794 SMSC_TRACE(pdata, drv, "Error occurred during eeprom operation");
1795 return -EINVAL;
1798 return 0;
1801 static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
1802 u8 address, u8 *data)
1804 u32 op = E2P_CMD_EPC_CMD_READ_ | address;
1805 int ret;
1807 SMSC_TRACE(pdata, drv, "address 0x%x", address);
1808 ret = smsc911x_eeprom_send_cmd(pdata, op);
1810 if (!ret)
1811 data[address] = smsc911x_reg_read(pdata, E2P_DATA);
1813 return ret;
1816 static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
1817 u8 address, u8 data)
1819 u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
1820 u32 temp;
1821 int ret;
1823 SMSC_TRACE(pdata, drv, "address 0x%x, data 0x%x", address, data);
1824 ret = smsc911x_eeprom_send_cmd(pdata, op);
1826 if (!ret) {
1827 op = E2P_CMD_EPC_CMD_WRITE_ | address;
1828 smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
1830 /* Workaround for hardware read-after-write restriction */
1831 temp = smsc911x_reg_read(pdata, BYTE_TEST);
1833 ret = smsc911x_eeprom_send_cmd(pdata, op);
1836 return ret;
1839 static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
1841 return SMSC911X_EEPROM_SIZE;
1844 static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
1845 struct ethtool_eeprom *eeprom, u8 *data)
1847 struct smsc911x_data *pdata = netdev_priv(dev);
1848 u8 eeprom_data[SMSC911X_EEPROM_SIZE];
1849 int len;
1850 int i;
1852 smsc911x_eeprom_enable_access(pdata);
1854 len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
1855 for (i = 0; i < len; i++) {
1856 int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
1857 if (ret < 0) {
1858 eeprom->len = 0;
1859 return ret;
1863 memcpy(data, &eeprom_data[eeprom->offset], len);
1864 eeprom->len = len;
1865 return 0;
1868 static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
1869 struct ethtool_eeprom *eeprom, u8 *data)
1871 int ret;
1872 struct smsc911x_data *pdata = netdev_priv(dev);
1874 smsc911x_eeprom_enable_access(pdata);
1875 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
1876 ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
1877 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
1879 /* Single byte write, according to man page */
1880 eeprom->len = 1;
1882 return ret;
1885 static const struct ethtool_ops smsc911x_ethtool_ops = {
1886 .get_settings = smsc911x_ethtool_getsettings,
1887 .set_settings = smsc911x_ethtool_setsettings,
1888 .get_link = ethtool_op_get_link,
1889 .get_drvinfo = smsc911x_ethtool_getdrvinfo,
1890 .nway_reset = smsc911x_ethtool_nwayreset,
1891 .get_msglevel = smsc911x_ethtool_getmsglevel,
1892 .set_msglevel = smsc911x_ethtool_setmsglevel,
1893 .get_regs_len = smsc911x_ethtool_getregslen,
1894 .get_regs = smsc911x_ethtool_getregs,
1895 .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
1896 .get_eeprom = smsc911x_ethtool_get_eeprom,
1897 .set_eeprom = smsc911x_ethtool_set_eeprom,
1900 static const struct net_device_ops smsc911x_netdev_ops = {
1901 .ndo_open = smsc911x_open,
1902 .ndo_stop = smsc911x_stop,
1903 .ndo_start_xmit = smsc911x_hard_start_xmit,
1904 .ndo_get_stats = smsc911x_get_stats,
1905 .ndo_set_multicast_list = smsc911x_set_multicast_list,
1906 .ndo_do_ioctl = smsc911x_do_ioctl,
1907 .ndo_change_mtu = eth_change_mtu,
1908 .ndo_validate_addr = eth_validate_addr,
1909 .ndo_set_mac_address = smsc911x_set_mac_address,
1910 #ifdef CONFIG_NET_POLL_CONTROLLER
1911 .ndo_poll_controller = smsc911x_poll_controller,
1912 #endif
1915 /* copies the current mac address from hardware to dev->dev_addr */
1916 static void __devinit smsc911x_read_mac_address(struct net_device *dev)
1918 struct smsc911x_data *pdata = netdev_priv(dev);
1919 u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
1920 u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
1922 dev->dev_addr[0] = (u8)(mac_low32);
1923 dev->dev_addr[1] = (u8)(mac_low32 >> 8);
1924 dev->dev_addr[2] = (u8)(mac_low32 >> 16);
1925 dev->dev_addr[3] = (u8)(mac_low32 >> 24);
1926 dev->dev_addr[4] = (u8)(mac_high16);
1927 dev->dev_addr[5] = (u8)(mac_high16 >> 8);
1930 /* Initializing private device structures, only called from probe */
1931 static int __devinit smsc911x_init(struct net_device *dev)
1933 struct smsc911x_data *pdata = netdev_priv(dev);
1934 unsigned int byte_test;
1936 SMSC_TRACE(pdata, probe, "Driver Parameters:");
1937 SMSC_TRACE(pdata, probe, "LAN base: 0x%08lX",
1938 (unsigned long)pdata->ioaddr);
1939 SMSC_TRACE(pdata, probe, "IRQ: %d", dev->irq);
1940 SMSC_TRACE(pdata, probe, "PHY will be autodetected.");
1942 spin_lock_init(&pdata->dev_lock);
1943 spin_lock_init(&pdata->mac_lock);
1945 if (pdata->ioaddr == 0) {
1946 SMSC_WARN(pdata, probe, "pdata->ioaddr: 0x00000000");
1947 return -ENODEV;
1950 /* Check byte ordering */
1951 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1952 SMSC_TRACE(pdata, probe, "BYTE_TEST: 0x%08X", byte_test);
1953 if (byte_test == 0x43218765) {
1954 SMSC_TRACE(pdata, probe, "BYTE_TEST looks swapped, "
1955 "applying WORD_SWAP");
1956 smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
1958 /* 1 dummy read of BYTE_TEST is needed after a write to
1959 * WORD_SWAP before its contents are valid */
1960 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1962 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1965 if (byte_test != 0x87654321) {
1966 SMSC_WARN(pdata, drv, "BYTE_TEST: 0x%08X", byte_test);
1967 if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
1968 SMSC_WARN(pdata, probe,
1969 "top 16 bits equal to bottom 16 bits");
1970 SMSC_TRACE(pdata, probe,
1971 "This may mean the chip is set "
1972 "for 32 bit while the bus is reading 16 bit");
1974 return -ENODEV;
1977 /* Default generation to zero (all workarounds apply) */
1978 pdata->generation = 0;
1980 pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
1981 switch (pdata->idrev & 0xFFFF0000) {
1982 case 0x01180000:
1983 case 0x01170000:
1984 case 0x01160000:
1985 case 0x01150000:
1986 /* LAN911[5678] family */
1987 pdata->generation = pdata->idrev & 0x0000FFFF;
1988 break;
1990 case 0x118A0000:
1991 case 0x117A0000:
1992 case 0x116A0000:
1993 case 0x115A0000:
1994 /* LAN921[5678] family */
1995 pdata->generation = 3;
1996 break;
1998 case 0x92100000:
1999 case 0x92110000:
2000 case 0x92200000:
2001 case 0x92210000:
2002 /* LAN9210/LAN9211/LAN9220/LAN9221 */
2003 pdata->generation = 4;
2004 break;
2006 default:
2007 SMSC_WARN(pdata, probe, "LAN911x not identified, idrev: 0x%08X",
2008 pdata->idrev);
2009 return -ENODEV;
2012 SMSC_TRACE(pdata, probe,
2013 "LAN911x identified, idrev: 0x%08X, generation: %d",
2014 pdata->idrev, pdata->generation);
2016 if (pdata->generation == 0)
2017 SMSC_WARN(pdata, probe,
2018 "This driver is not intended for this chip revision");
2020 /* workaround for platforms without an eeprom, where the mac address
2021 * is stored elsewhere and set by the bootloader. This saves the
2022 * mac address before resetting the device */
2023 if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS) {
2024 spin_lock_irq(&pdata->mac_lock);
2025 smsc911x_read_mac_address(dev);
2026 spin_unlock_irq(&pdata->mac_lock);
2029 /* Reset the LAN911x */
2030 if (smsc911x_soft_reset(pdata))
2031 return -ENODEV;
2033 /* Disable all interrupt sources until we bring the device up */
2034 smsc911x_reg_write(pdata, INT_EN, 0);
2036 ether_setup(dev);
2037 dev->flags |= IFF_MULTICAST;
2038 netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
2039 dev->netdev_ops = &smsc911x_netdev_ops;
2040 dev->ethtool_ops = &smsc911x_ethtool_ops;
2042 return 0;
2045 static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
2047 struct net_device *dev;
2048 struct smsc911x_data *pdata;
2049 struct resource *res;
2051 dev = platform_get_drvdata(pdev);
2052 BUG_ON(!dev);
2053 pdata = netdev_priv(dev);
2054 BUG_ON(!pdata);
2055 BUG_ON(!pdata->ioaddr);
2056 BUG_ON(!pdata->phy_dev);
2058 SMSC_TRACE(pdata, ifdown, "Stopping driver");
2060 phy_disconnect(pdata->phy_dev);
2061 pdata->phy_dev = NULL;
2062 mdiobus_unregister(pdata->mii_bus);
2063 mdiobus_free(pdata->mii_bus);
2065 platform_set_drvdata(pdev, NULL);
2066 unregister_netdev(dev);
2067 free_irq(dev->irq, dev);
2068 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2069 "smsc911x-memory");
2070 if (!res)
2071 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2073 release_mem_region(res->start, resource_size(res));
2075 iounmap(pdata->ioaddr);
2077 free_netdev(dev);
2079 return 0;
2082 /* standard register acces */
2083 static const struct smsc911x_ops standard_smsc911x_ops = {
2084 .reg_read = __smsc911x_reg_read,
2085 .reg_write = __smsc911x_reg_write,
2086 .rx_readfifo = smsc911x_rx_readfifo,
2087 .tx_writefifo = smsc911x_tx_writefifo,
2090 /* shifted register access */
2091 static const struct smsc911x_ops shifted_smsc911x_ops = {
2092 .reg_read = __smsc911x_reg_read_shift,
2093 .reg_write = __smsc911x_reg_write_shift,
2094 .rx_readfifo = smsc911x_rx_readfifo_shift,
2095 .tx_writefifo = smsc911x_tx_writefifo_shift,
2098 static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
2100 struct net_device *dev;
2101 struct smsc911x_data *pdata;
2102 struct smsc911x_platform_config *config = pdev->dev.platform_data;
2103 struct resource *res, *irq_res;
2104 unsigned int intcfg = 0;
2105 int res_size, irq_flags;
2106 int retval;
2108 pr_info("Driver version %s\n", SMSC_DRV_VERSION);
2110 /* platform data specifies irq & dynamic bus configuration */
2111 if (!pdev->dev.platform_data) {
2112 pr_warn("platform_data not provided\n");
2113 retval = -ENODEV;
2114 goto out_0;
2117 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2118 "smsc911x-memory");
2119 if (!res)
2120 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2121 if (!res) {
2122 pr_warn("Could not allocate resource\n");
2123 retval = -ENODEV;
2124 goto out_0;
2126 res_size = resource_size(res);
2128 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2129 if (!irq_res) {
2130 pr_warn("Could not allocate irq resource\n");
2131 retval = -ENODEV;
2132 goto out_0;
2135 if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
2136 retval = -EBUSY;
2137 goto out_0;
2140 dev = alloc_etherdev(sizeof(struct smsc911x_data));
2141 if (!dev) {
2142 pr_warn("Could not allocate device\n");
2143 retval = -ENOMEM;
2144 goto out_release_io_1;
2147 SET_NETDEV_DEV(dev, &pdev->dev);
2149 pdata = netdev_priv(dev);
2151 dev->irq = irq_res->start;
2152 irq_flags = irq_res->flags & IRQF_TRIGGER_MASK;
2153 pdata->ioaddr = ioremap_nocache(res->start, res_size);
2155 /* copy config parameters across to pdata */
2156 memcpy(&pdata->config, config, sizeof(pdata->config));
2158 pdata->dev = dev;
2159 pdata->msg_enable = ((1 << debug) - 1);
2161 if (pdata->ioaddr == NULL) {
2162 SMSC_WARN(pdata, probe, "Error smsc911x base address invalid");
2163 retval = -ENOMEM;
2164 goto out_free_netdev_2;
2167 /* assume standard, non-shifted, access to HW registers */
2168 pdata->ops = &standard_smsc911x_ops;
2169 /* apply the right access if shifting is needed */
2170 if (config->shift)
2171 pdata->ops = &shifted_smsc911x_ops;
2173 retval = smsc911x_init(dev);
2174 if (retval < 0)
2175 goto out_unmap_io_3;
2177 /* configure irq polarity and type before connecting isr */
2178 if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
2179 intcfg |= INT_CFG_IRQ_POL_;
2181 if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
2182 intcfg |= INT_CFG_IRQ_TYPE_;
2184 smsc911x_reg_write(pdata, INT_CFG, intcfg);
2186 /* Ensure interrupts are globally disabled before connecting ISR */
2187 smsc911x_reg_write(pdata, INT_EN, 0);
2188 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
2190 retval = request_irq(dev->irq, smsc911x_irqhandler,
2191 irq_flags | IRQF_SHARED, dev->name, dev);
2192 if (retval) {
2193 SMSC_WARN(pdata, probe,
2194 "Unable to claim requested irq: %d", dev->irq);
2195 goto out_unmap_io_3;
2198 platform_set_drvdata(pdev, dev);
2200 retval = register_netdev(dev);
2201 if (retval) {
2202 SMSC_WARN(pdata, probe, "Error %i registering device", retval);
2203 goto out_unset_drvdata_4;
2204 } else {
2205 SMSC_TRACE(pdata, probe,
2206 "Network interface: \"%s\"", dev->name);
2209 retval = smsc911x_mii_init(pdev, dev);
2210 if (retval) {
2211 SMSC_WARN(pdata, probe, "Error %i initialising mii", retval);
2212 goto out_unregister_netdev_5;
2215 spin_lock_irq(&pdata->mac_lock);
2217 /* Check if mac address has been specified when bringing interface up */
2218 if (is_valid_ether_addr(dev->dev_addr)) {
2219 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
2220 SMSC_TRACE(pdata, probe,
2221 "MAC Address is specified by configuration");
2222 } else if (is_valid_ether_addr(pdata->config.mac)) {
2223 memcpy(dev->dev_addr, pdata->config.mac, 6);
2224 SMSC_TRACE(pdata, probe,
2225 "MAC Address specified by platform data");
2226 } else {
2227 /* Try reading mac address from device. if EEPROM is present
2228 * it will already have been set */
2229 smsc_get_mac(dev);
2231 if (is_valid_ether_addr(dev->dev_addr)) {
2232 /* eeprom values are valid so use them */
2233 SMSC_TRACE(pdata, probe,
2234 "Mac Address is read from LAN911x EEPROM");
2235 } else {
2236 /* eeprom values are invalid, generate random MAC */
2237 random_ether_addr(dev->dev_addr);
2238 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
2239 SMSC_TRACE(pdata, probe,
2240 "MAC Address is set to random_ether_addr");
2244 spin_unlock_irq(&pdata->mac_lock);
2246 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
2248 return 0;
2250 out_unregister_netdev_5:
2251 unregister_netdev(dev);
2252 out_unset_drvdata_4:
2253 platform_set_drvdata(pdev, NULL);
2254 free_irq(dev->irq, dev);
2255 out_unmap_io_3:
2256 iounmap(pdata->ioaddr);
2257 out_free_netdev_2:
2258 free_netdev(dev);
2259 out_release_io_1:
2260 release_mem_region(res->start, resource_size(res));
2261 out_0:
2262 return retval;
2265 #ifdef CONFIG_PM
2266 /* This implementation assumes the devices remains powered on its VDDVARIO
2267 * pins during suspend. */
2269 /* TODO: implement freeze/thaw callbacks for hibernation.*/
2271 static int smsc911x_suspend(struct device *dev)
2273 struct net_device *ndev = dev_get_drvdata(dev);
2274 struct smsc911x_data *pdata = netdev_priv(ndev);
2276 /* enable wake on LAN, energy detection and the external PME
2277 * signal. */
2278 smsc911x_reg_write(pdata, PMT_CTRL,
2279 PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
2280 PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
2282 return 0;
2285 static int smsc911x_resume(struct device *dev)
2287 struct net_device *ndev = dev_get_drvdata(dev);
2288 struct smsc911x_data *pdata = netdev_priv(ndev);
2289 unsigned int to = 100;
2291 /* Note 3.11 from the datasheet:
2292 * "When the LAN9220 is in a power saving state, a write of any
2293 * data to the BYTE_TEST register will wake-up the device."
2295 smsc911x_reg_write(pdata, BYTE_TEST, 0);
2297 /* poll the READY bit in PMT_CTRL. Any other access to the device is
2298 * forbidden while this bit isn't set. Try for 100ms and return -EIO
2299 * if it failed. */
2300 while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
2301 udelay(1000);
2303 return (to == 0) ? -EIO : 0;
2306 static const struct dev_pm_ops smsc911x_pm_ops = {
2307 .suspend = smsc911x_suspend,
2308 .resume = smsc911x_resume,
2311 #define SMSC911X_PM_OPS (&smsc911x_pm_ops)
2313 #else
2314 #define SMSC911X_PM_OPS NULL
2315 #endif
2317 static struct platform_driver smsc911x_driver = {
2318 .probe = smsc911x_drv_probe,
2319 .remove = __devexit_p(smsc911x_drv_remove),
2320 .driver = {
2321 .name = SMSC_CHIPNAME,
2322 .owner = THIS_MODULE,
2323 .pm = SMSC911X_PM_OPS,
2327 /* Entry point for loading the module */
2328 static int __init smsc911x_init_module(void)
2330 SMSC_INITIALIZE();
2331 return platform_driver_register(&smsc911x_driver);
2334 /* entry point for unloading the module */
2335 static void __exit smsc911x_cleanup_module(void)
2337 platform_driver_unregister(&smsc911x_driver);
2340 module_init(smsc911x_init_module);
2341 module_exit(smsc911x_cleanup_module);