smsc911x: allow mac address to be saved before device reset
[safe/jmp/linux-2.6] / drivers / net / smsc911x.c
1 /***************************************************************************
2  *
3  * Copyright (C) 2004-2008 SMSC
4  * Copyright (C) 2005-2008 ARM
5  *
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.
10  *
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.
15  *
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.
19  *
20  ***************************************************************************
21  * Rewritten, heavily based on smsc911x simple driver by SMSC.
22  * Partly uses io macros from smc91x.c by Nicolas Pitre
23  *
24  * Supported devices:
25  *   LAN9115, LAN9116, LAN9117, LAN9118
26  *   LAN9215, LAN9216, LAN9217, LAN9218
27  *   LAN9210, LAN9211
28  *   LAN9220, LAN9221
29  *
30  */
31
32 #include <linux/crc32.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
35 #include <linux/etherdevice.h>
36 #include <linux/ethtool.h>
37 #include <linux/init.h>
38 #include <linux/ioport.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/netdevice.h>
42 #include <linux/platform_device.h>
43 #include <linux/sched.h>
44 #include <linux/slab.h>
45 #include <linux/timer.h>
46 #include <linux/version.h>
47 #include <linux/bug.h>
48 #include <linux/bitops.h>
49 #include <linux/irq.h>
50 #include <linux/io.h>
51 #include <linux/phy.h>
52 #include <linux/smsc911x.h>
53 #include "smsc911x.h"
54
55 #define SMSC_CHIPNAME           "smsc911x"
56 #define SMSC_MDIONAME           "smsc911x-mdio"
57 #define SMSC_DRV_VERSION        "2008-10-21"
58
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(SMSC_DRV_VERSION);
61
62 #if USE_DEBUG > 0
63 static int debug = 16;
64 #else
65 static int debug = 3;
66 #endif
67
68 module_param(debug, int, 0);
69 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
70
71 struct smsc911x_data {
72         void __iomem *ioaddr;
73
74         unsigned int idrev;
75
76         /* used to decide which workarounds apply */
77         unsigned int generation;
78
79         /* device configuration (copied from platform_data during probe) */
80         struct smsc911x_platform_config config;
81
82         /* This needs to be acquired before calling any of below:
83          * smsc911x_mac_read(), smsc911x_mac_write()
84          */
85         spinlock_t mac_lock;
86
87         /* spinlock to ensure 16-bit accesses are serialised.
88          * unused with a 32-bit bus */
89         spinlock_t dev_lock;
90
91         struct phy_device *phy_dev;
92         struct mii_bus *mii_bus;
93         int phy_irq[PHY_MAX_ADDR];
94         unsigned int using_extphy;
95         int last_duplex;
96         int last_carrier;
97
98         u32 msg_enable;
99         unsigned int gpio_setting;
100         unsigned int gpio_orig_setting;
101         struct net_device *dev;
102         struct napi_struct napi;
103
104         unsigned int software_irq_signal;
105
106 #ifdef USE_PHY_WORK_AROUND
107 #define MIN_PACKET_SIZE (64)
108         char loopback_tx_pkt[MIN_PACKET_SIZE];
109         char loopback_rx_pkt[MIN_PACKET_SIZE];
110         unsigned int resetcount;
111 #endif
112
113         /* Members for Multicast filter workaround */
114         unsigned int multicast_update_pending;
115         unsigned int set_bits_mask;
116         unsigned int clear_bits_mask;
117         unsigned int hashhi;
118         unsigned int hashlo;
119 };
120
121 /* The 16-bit access functions are significantly slower, due to the locking
122  * necessary.  If your bus hardware can be configured to do this for you
123  * (in response to a single 32-bit operation from software), you should use
124  * the 32-bit access functions instead. */
125
126 static inline u32 smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
127 {
128         if (pdata->config.flags & SMSC911X_USE_32BIT)
129                 return readl(pdata->ioaddr + reg);
130
131         if (pdata->config.flags & SMSC911X_USE_16BIT) {
132                 u32 data;
133                 unsigned long flags;
134
135                 /* these two 16-bit reads must be performed consecutively, so
136                  * must not be interrupted by our own ISR (which would start
137                  * another read operation) */
138                 spin_lock_irqsave(&pdata->dev_lock, flags);
139                 data = ((readw(pdata->ioaddr + reg) & 0xFFFF) |
140                         ((readw(pdata->ioaddr + reg + 2) & 0xFFFF) << 16));
141                 spin_unlock_irqrestore(&pdata->dev_lock, flags);
142
143                 return data;
144         }
145
146         BUG();
147         return 0;
148 }
149
150 static inline void smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
151                                       u32 val)
152 {
153         if (pdata->config.flags & SMSC911X_USE_32BIT) {
154                 writel(val, pdata->ioaddr + reg);
155                 return;
156         }
157
158         if (pdata->config.flags & SMSC911X_USE_16BIT) {
159                 unsigned long flags;
160
161                 /* these two 16-bit writes must be performed consecutively, so
162                  * must not be interrupted by our own ISR (which would start
163                  * another read operation) */
164                 spin_lock_irqsave(&pdata->dev_lock, flags);
165                 writew(val & 0xFFFF, pdata->ioaddr + reg);
166                 writew((val >> 16) & 0xFFFF, pdata->ioaddr + reg + 2);
167                 spin_unlock_irqrestore(&pdata->dev_lock, flags);
168                 return;
169         }
170
171         BUG();
172 }
173
174 /* Writes a packet to the TX_DATA_FIFO */
175 static inline void
176 smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
177                       unsigned int wordcount)
178 {
179         if (pdata->config.flags & SMSC911X_USE_32BIT) {
180                 writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
181                 return;
182         }
183
184         if (pdata->config.flags & SMSC911X_USE_16BIT) {
185                 while (wordcount--)
186                         smsc911x_reg_write(pdata, TX_DATA_FIFO, *buf++);
187                 return;
188         }
189
190         BUG();
191 }
192
193 /* Reads a packet out of the RX_DATA_FIFO */
194 static inline void
195 smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
196                      unsigned int wordcount)
197 {
198         if (pdata->config.flags & SMSC911X_USE_32BIT) {
199                 readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
200                 return;
201         }
202
203         if (pdata->config.flags & SMSC911X_USE_16BIT) {
204                 while (wordcount--)
205                         *buf++ = smsc911x_reg_read(pdata, RX_DATA_FIFO);
206                 return;
207         }
208
209         BUG();
210 }
211
212 /* waits for MAC not busy, with timeout.  Only called by smsc911x_mac_read
213  * and smsc911x_mac_write, so assumes mac_lock is held */
214 static int smsc911x_mac_complete(struct smsc911x_data *pdata)
215 {
216         int i;
217         u32 val;
218
219         SMSC_ASSERT_MAC_LOCK(pdata);
220
221         for (i = 0; i < 40; i++) {
222                 val = smsc911x_reg_read(pdata, MAC_CSR_CMD);
223                 if (!(val & MAC_CSR_CMD_CSR_BUSY_))
224                         return 0;
225         }
226         SMSC_WARNING(HW, "Timed out waiting for MAC not BUSY. "
227                 "MAC_CSR_CMD: 0x%08X", val);
228         return -EIO;
229 }
230
231 /* Fetches a MAC register value. Assumes mac_lock is acquired */
232 static u32 smsc911x_mac_read(struct smsc911x_data *pdata, unsigned int offset)
233 {
234         unsigned int temp;
235
236         SMSC_ASSERT_MAC_LOCK(pdata);
237
238         temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
239         if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
240                 SMSC_WARNING(HW, "MAC busy at entry");
241                 return 0xFFFFFFFF;
242         }
243
244         /* Send the MAC cmd */
245         smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
246                 MAC_CSR_CMD_CSR_BUSY_ | MAC_CSR_CMD_R_NOT_W_));
247
248         /* Workaround for hardware read-after-write restriction */
249         temp = smsc911x_reg_read(pdata, BYTE_TEST);
250
251         /* Wait for the read to complete */
252         if (likely(smsc911x_mac_complete(pdata) == 0))
253                 return smsc911x_reg_read(pdata, MAC_CSR_DATA);
254
255         SMSC_WARNING(HW, "MAC busy after read");
256         return 0xFFFFFFFF;
257 }
258
259 /* Set a mac register, mac_lock must be acquired before calling */
260 static void smsc911x_mac_write(struct smsc911x_data *pdata,
261                                unsigned int offset, u32 val)
262 {
263         unsigned int temp;
264
265         SMSC_ASSERT_MAC_LOCK(pdata);
266
267         temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
268         if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
269                 SMSC_WARNING(HW,
270                         "smsc911x_mac_write failed, MAC busy at entry");
271                 return;
272         }
273
274         /* Send data to write */
275         smsc911x_reg_write(pdata, MAC_CSR_DATA, val);
276
277         /* Write the actual data */
278         smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
279                 MAC_CSR_CMD_CSR_BUSY_));
280
281         /* Workaround for hardware read-after-write restriction */
282         temp = smsc911x_reg_read(pdata, BYTE_TEST);
283
284         /* Wait for the write to complete */
285         if (likely(smsc911x_mac_complete(pdata) == 0))
286                 return;
287
288         SMSC_WARNING(HW,
289                 "smsc911x_mac_write failed, MAC busy after write");
290 }
291
292 /* Get a phy register */
293 static int smsc911x_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
294 {
295         struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
296         unsigned long flags;
297         unsigned int addr;
298         int i, reg;
299
300         spin_lock_irqsave(&pdata->mac_lock, flags);
301
302         /* Confirm MII not busy */
303         if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
304                 SMSC_WARNING(HW,
305                         "MII is busy in smsc911x_mii_read???");
306                 reg = -EIO;
307                 goto out;
308         }
309
310         /* Set the address, index & direction (read from PHY) */
311         addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6);
312         smsc911x_mac_write(pdata, MII_ACC, addr);
313
314         /* Wait for read to complete w/ timeout */
315         for (i = 0; i < 100; i++)
316                 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
317                         reg = smsc911x_mac_read(pdata, MII_DATA);
318                         goto out;
319                 }
320
321         SMSC_WARNING(HW, "Timed out waiting for MII write to finish");
322         reg = -EIO;
323
324 out:
325         spin_unlock_irqrestore(&pdata->mac_lock, flags);
326         return reg;
327 }
328
329 /* Set a phy register */
330 static int smsc911x_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
331                            u16 val)
332 {
333         struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
334         unsigned long flags;
335         unsigned int addr;
336         int i, reg;
337
338         spin_lock_irqsave(&pdata->mac_lock, flags);
339
340         /* Confirm MII not busy */
341         if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
342                 SMSC_WARNING(HW,
343                         "MII is busy in smsc911x_mii_write???");
344                 reg = -EIO;
345                 goto out;
346         }
347
348         /* Put the data to write in the MAC */
349         smsc911x_mac_write(pdata, MII_DATA, val);
350
351         /* Set the address, index & direction (write to PHY) */
352         addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
353                 MII_ACC_MII_WRITE_;
354         smsc911x_mac_write(pdata, MII_ACC, addr);
355
356         /* Wait for write to complete w/ timeout */
357         for (i = 0; i < 100; i++)
358                 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
359                         reg = 0;
360                         goto out;
361                 }
362
363         SMSC_WARNING(HW, "Timed out waiting for MII write to finish");
364         reg = -EIO;
365
366 out:
367         spin_unlock_irqrestore(&pdata->mac_lock, flags);
368         return reg;
369 }
370
371 /* Switch to external phy. Assumes tx and rx are stopped. */
372 static void smsc911x_phy_enable_external(struct smsc911x_data *pdata)
373 {
374         unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
375
376         /* Disable phy clocks to the MAC */
377         hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
378         hwcfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
379         smsc911x_reg_write(pdata, HW_CFG, hwcfg);
380         udelay(10);     /* Enough time for clocks to stop */
381
382         /* Switch to external phy */
383         hwcfg |= HW_CFG_EXT_PHY_EN_;
384         smsc911x_reg_write(pdata, HW_CFG, hwcfg);
385
386         /* Enable phy clocks to the MAC */
387         hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
388         hwcfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
389         smsc911x_reg_write(pdata, HW_CFG, hwcfg);
390         udelay(10);     /* Enough time for clocks to restart */
391
392         hwcfg |= HW_CFG_SMI_SEL_;
393         smsc911x_reg_write(pdata, HW_CFG, hwcfg);
394 }
395
396 /* Autodetects and enables external phy if present on supported chips.
397  * autodetection can be overridden by specifying SMSC911X_FORCE_INTERNAL_PHY
398  * or SMSC911X_FORCE_EXTERNAL_PHY in the platform_data flags. */
399 static void smsc911x_phy_initialise_external(struct smsc911x_data *pdata)
400 {
401         unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
402
403         if (pdata->config.flags & SMSC911X_FORCE_INTERNAL_PHY) {
404                 SMSC_TRACE(HW, "Forcing internal PHY");
405                 pdata->using_extphy = 0;
406         } else if (pdata->config.flags & SMSC911X_FORCE_EXTERNAL_PHY) {
407                 SMSC_TRACE(HW, "Forcing external PHY");
408                 smsc911x_phy_enable_external(pdata);
409                 pdata->using_extphy = 1;
410         } else if (hwcfg & HW_CFG_EXT_PHY_DET_) {
411                 SMSC_TRACE(HW, "HW_CFG EXT_PHY_DET set, using external PHY");
412                 smsc911x_phy_enable_external(pdata);
413                 pdata->using_extphy = 1;
414         } else {
415                 SMSC_TRACE(HW, "HW_CFG EXT_PHY_DET clear, using internal PHY");
416                 pdata->using_extphy = 0;
417         }
418 }
419
420 /* Fetches a tx status out of the status fifo */
421 static unsigned int smsc911x_tx_get_txstatus(struct smsc911x_data *pdata)
422 {
423         unsigned int result =
424             smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TSUSED_;
425
426         if (result != 0)
427                 result = smsc911x_reg_read(pdata, TX_STATUS_FIFO);
428
429         return result;
430 }
431
432 /* Fetches the next rx status */
433 static unsigned int smsc911x_rx_get_rxstatus(struct smsc911x_data *pdata)
434 {
435         unsigned int result =
436             smsc911x_reg_read(pdata, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED_;
437
438         if (result != 0)
439                 result = smsc911x_reg_read(pdata, RX_STATUS_FIFO);
440
441         return result;
442 }
443
444 #ifdef USE_PHY_WORK_AROUND
445 static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data *pdata)
446 {
447         unsigned int tries;
448         u32 wrsz;
449         u32 rdsz;
450         ulong bufp;
451
452         for (tries = 0; tries < 10; tries++) {
453                 unsigned int txcmd_a;
454                 unsigned int txcmd_b;
455                 unsigned int status;
456                 unsigned int pktlength;
457                 unsigned int i;
458
459                 /* Zero-out rx packet memory */
460                 memset(pdata->loopback_rx_pkt, 0, MIN_PACKET_SIZE);
461
462                 /* Write tx packet to 118 */
463                 txcmd_a = (u32)((ulong)pdata->loopback_tx_pkt & 0x03) << 16;
464                 txcmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
465                 txcmd_a |= MIN_PACKET_SIZE;
466
467                 txcmd_b = MIN_PACKET_SIZE << 16 | MIN_PACKET_SIZE;
468
469                 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_a);
470                 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_b);
471
472                 bufp = (ulong)pdata->loopback_tx_pkt & (~0x3);
473                 wrsz = MIN_PACKET_SIZE + 3;
474                 wrsz += (u32)((ulong)pdata->loopback_tx_pkt & 0x3);
475                 wrsz >>= 2;
476
477                 smsc911x_tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
478
479                 /* Wait till transmit is done */
480                 i = 60;
481                 do {
482                         udelay(5);
483                         status = smsc911x_tx_get_txstatus(pdata);
484                 } while ((i--) && (!status));
485
486                 if (!status) {
487                         SMSC_WARNING(HW, "Failed to transmit "
488                                 "during loopback test");
489                         continue;
490                 }
491                 if (status & TX_STS_ES_) {
492                         SMSC_WARNING(HW, "Transmit encountered "
493                                 "errors during loopback test");
494                         continue;
495                 }
496
497                 /* Wait till receive is done */
498                 i = 60;
499                 do {
500                         udelay(5);
501                         status = smsc911x_rx_get_rxstatus(pdata);
502                 } while ((i--) && (!status));
503
504                 if (!status) {
505                         SMSC_WARNING(HW,
506                                 "Failed to receive during loopback test");
507                         continue;
508                 }
509                 if (status & RX_STS_ES_) {
510                         SMSC_WARNING(HW, "Receive encountered "
511                                 "errors during loopback test");
512                         continue;
513                 }
514
515                 pktlength = ((status & 0x3FFF0000UL) >> 16);
516                 bufp = (ulong)pdata->loopback_rx_pkt;
517                 rdsz = pktlength + 3;
518                 rdsz += (u32)((ulong)pdata->loopback_rx_pkt & 0x3);
519                 rdsz >>= 2;
520
521                 smsc911x_rx_readfifo(pdata, (unsigned int *)bufp, rdsz);
522
523                 if (pktlength != (MIN_PACKET_SIZE + 4)) {
524                         SMSC_WARNING(HW, "Unexpected packet size "
525                                 "during loop back test, size=%d, will retry",
526                                 pktlength);
527                 } else {
528                         unsigned int j;
529                         int mismatch = 0;
530                         for (j = 0; j < MIN_PACKET_SIZE; j++) {
531                                 if (pdata->loopback_tx_pkt[j]
532                                     != pdata->loopback_rx_pkt[j]) {
533                                         mismatch = 1;
534                                         break;
535                                 }
536                         }
537                         if (!mismatch) {
538                                 SMSC_TRACE(HW, "Successfully verified "
539                                            "loopback packet");
540                                 return 0;
541                         } else {
542                                 SMSC_WARNING(HW, "Data mismatch "
543                                         "during loop back test, will retry");
544                         }
545                 }
546         }
547
548         return -EIO;
549 }
550
551 static int smsc911x_phy_reset(struct smsc911x_data *pdata)
552 {
553         struct phy_device *phy_dev = pdata->phy_dev;
554         unsigned int temp;
555         unsigned int i = 100000;
556
557         BUG_ON(!phy_dev);
558         BUG_ON(!phy_dev->bus);
559
560         SMSC_TRACE(HW, "Performing PHY BCR Reset");
561         smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, BMCR_RESET);
562         do {
563                 msleep(1);
564                 temp = smsc911x_mii_read(phy_dev->bus, phy_dev->addr,
565                         MII_BMCR);
566         } while ((i--) && (temp & BMCR_RESET));
567
568         if (temp & BMCR_RESET) {
569                 SMSC_WARNING(HW, "PHY reset failed to complete.");
570                 return -EIO;
571         }
572         /* Extra delay required because the phy may not be completed with
573         * its reset when BMCR_RESET is cleared. Specs say 256 uS is
574         * enough delay but using 1ms here to be safe */
575         msleep(1);
576
577         return 0;
578 }
579
580 static int smsc911x_phy_loopbacktest(struct net_device *dev)
581 {
582         struct smsc911x_data *pdata = netdev_priv(dev);
583         struct phy_device *phy_dev = pdata->phy_dev;
584         int result = -EIO;
585         unsigned int i, val;
586         unsigned long flags;
587
588         /* Initialise tx packet using broadcast destination address */
589         memset(pdata->loopback_tx_pkt, 0xff, ETH_ALEN);
590
591         /* Use incrementing source address */
592         for (i = 6; i < 12; i++)
593                 pdata->loopback_tx_pkt[i] = (char)i;
594
595         /* Set length type field */
596         pdata->loopback_tx_pkt[12] = 0x00;
597         pdata->loopback_tx_pkt[13] = 0x00;
598
599         for (i = 14; i < MIN_PACKET_SIZE; i++)
600                 pdata->loopback_tx_pkt[i] = (char)i;
601
602         val = smsc911x_reg_read(pdata, HW_CFG);
603         val &= HW_CFG_TX_FIF_SZ_;
604         val |= HW_CFG_SF_;
605         smsc911x_reg_write(pdata, HW_CFG, val);
606
607         smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
608         smsc911x_reg_write(pdata, RX_CFG,
609                 (u32)((ulong)pdata->loopback_rx_pkt & 0x03) << 8);
610
611         for (i = 0; i < 10; i++) {
612                 /* Set PHY to 10/FD, no ANEG, and loopback mode */
613                 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR,
614                         BMCR_LOOPBACK | BMCR_FULLDPLX);
615
616                 /* Enable MAC tx/rx, FD */
617                 spin_lock_irqsave(&pdata->mac_lock, flags);
618                 smsc911x_mac_write(pdata, MAC_CR, MAC_CR_FDPX_
619                                    | MAC_CR_TXEN_ | MAC_CR_RXEN_);
620                 spin_unlock_irqrestore(&pdata->mac_lock, flags);
621
622                 if (smsc911x_phy_check_loopbackpkt(pdata) == 0) {
623                         result = 0;
624                         break;
625                 }
626                 pdata->resetcount++;
627
628                 /* Disable MAC rx */
629                 spin_lock_irqsave(&pdata->mac_lock, flags);
630                 smsc911x_mac_write(pdata, MAC_CR, 0);
631                 spin_unlock_irqrestore(&pdata->mac_lock, flags);
632
633                 smsc911x_phy_reset(pdata);
634         }
635
636         /* Disable MAC */
637         spin_lock_irqsave(&pdata->mac_lock, flags);
638         smsc911x_mac_write(pdata, MAC_CR, 0);
639         spin_unlock_irqrestore(&pdata->mac_lock, flags);
640
641         /* Cancel PHY loopback mode */
642         smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, 0);
643
644         smsc911x_reg_write(pdata, TX_CFG, 0);
645         smsc911x_reg_write(pdata, RX_CFG, 0);
646
647         return result;
648 }
649 #endif                          /* USE_PHY_WORK_AROUND */
650
651 static void smsc911x_phy_update_flowcontrol(struct smsc911x_data *pdata)
652 {
653         struct phy_device *phy_dev = pdata->phy_dev;
654         u32 afc = smsc911x_reg_read(pdata, AFC_CFG);
655         u32 flow;
656         unsigned long flags;
657
658         if (phy_dev->duplex == DUPLEX_FULL) {
659                 u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
660                 u16 rmtadv = phy_read(phy_dev, MII_LPA);
661                 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
662
663                 if (cap & FLOW_CTRL_RX)
664                         flow = 0xFFFF0002;
665                 else
666                         flow = 0;
667
668                 if (cap & FLOW_CTRL_TX)
669                         afc |= 0xF;
670                 else
671                         afc &= ~0xF;
672
673                 SMSC_TRACE(HW, "rx pause %s, tx pause %s",
674                         (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
675                         (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
676         } else {
677                 SMSC_TRACE(HW, "half duplex");
678                 flow = 0;
679                 afc |= 0xF;
680         }
681
682         spin_lock_irqsave(&pdata->mac_lock, flags);
683         smsc911x_mac_write(pdata, FLOW, flow);
684         spin_unlock_irqrestore(&pdata->mac_lock, flags);
685
686         smsc911x_reg_write(pdata, AFC_CFG, afc);
687 }
688
689 /* Update link mode if anything has changed.  Called periodically when the
690  * PHY is in polling mode, even if nothing has changed. */
691 static void smsc911x_phy_adjust_link(struct net_device *dev)
692 {
693         struct smsc911x_data *pdata = netdev_priv(dev);
694         struct phy_device *phy_dev = pdata->phy_dev;
695         unsigned long flags;
696         int carrier;
697
698         if (phy_dev->duplex != pdata->last_duplex) {
699                 unsigned int mac_cr;
700                 SMSC_TRACE(HW, "duplex state has changed");
701
702                 spin_lock_irqsave(&pdata->mac_lock, flags);
703                 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
704                 if (phy_dev->duplex) {
705                         SMSC_TRACE(HW,
706                                 "configuring for full duplex mode");
707                         mac_cr |= MAC_CR_FDPX_;
708                 } else {
709                         SMSC_TRACE(HW,
710                                 "configuring for half duplex mode");
711                         mac_cr &= ~MAC_CR_FDPX_;
712                 }
713                 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
714                 spin_unlock_irqrestore(&pdata->mac_lock, flags);
715
716                 smsc911x_phy_update_flowcontrol(pdata);
717                 pdata->last_duplex = phy_dev->duplex;
718         }
719
720         carrier = netif_carrier_ok(dev);
721         if (carrier != pdata->last_carrier) {
722                 SMSC_TRACE(HW, "carrier state has changed");
723                 if (carrier) {
724                         SMSC_TRACE(HW, "configuring for carrier OK");
725                         if ((pdata->gpio_orig_setting & GPIO_CFG_LED1_EN_) &&
726                             (!pdata->using_extphy)) {
727                                 /* Restore orginal GPIO configuration */
728                                 pdata->gpio_setting = pdata->gpio_orig_setting;
729                                 smsc911x_reg_write(pdata, GPIO_CFG,
730                                         pdata->gpio_setting);
731                         }
732                 } else {
733                         SMSC_TRACE(HW, "configuring for no carrier");
734                         /* Check global setting that LED1
735                          * usage is 10/100 indicator */
736                         pdata->gpio_setting = smsc911x_reg_read(pdata,
737                                 GPIO_CFG);
738                         if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_)
739                             && (!pdata->using_extphy)) {
740                                 /* Force 10/100 LED off, after saving
741                                  * orginal GPIO configuration */
742                                 pdata->gpio_orig_setting = pdata->gpio_setting;
743
744                                 pdata->gpio_setting &= ~GPIO_CFG_LED1_EN_;
745                                 pdata->gpio_setting |= (GPIO_CFG_GPIOBUF0_
746                                                         | GPIO_CFG_GPIODIR0_
747                                                         | GPIO_CFG_GPIOD0_);
748                                 smsc911x_reg_write(pdata, GPIO_CFG,
749                                         pdata->gpio_setting);
750                         }
751                 }
752                 pdata->last_carrier = carrier;
753         }
754 }
755
756 static int smsc911x_mii_probe(struct net_device *dev)
757 {
758         struct smsc911x_data *pdata = netdev_priv(dev);
759         struct phy_device *phydev = NULL;
760         int phy_addr;
761
762         /* find the first phy */
763         for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
764                 if (pdata->mii_bus->phy_map[phy_addr]) {
765                         phydev = pdata->mii_bus->phy_map[phy_addr];
766                         SMSC_TRACE(PROBE, "PHY %d: addr %d, phy_id 0x%08X",
767                                 phy_addr, phydev->addr, phydev->phy_id);
768                         break;
769                 }
770         }
771
772         if (!phydev) {
773                 pr_err("%s: no PHY found\n", dev->name);
774                 return -ENODEV;
775         }
776
777         phydev = phy_connect(dev, dev_name(&phydev->dev),
778                 &smsc911x_phy_adjust_link, 0, pdata->config.phy_interface);
779
780         if (IS_ERR(phydev)) {
781                 pr_err("%s: Could not attach to PHY\n", dev->name);
782                 return PTR_ERR(phydev);
783         }
784
785         pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
786                 dev->name, phydev->drv->name,
787                 dev_name(&phydev->dev), phydev->irq);
788
789         /* mask with MAC supported features */
790         phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
791                               SUPPORTED_Asym_Pause);
792         phydev->advertising = phydev->supported;
793
794         pdata->phy_dev = phydev;
795         pdata->last_duplex = -1;
796         pdata->last_carrier = -1;
797
798 #ifdef USE_PHY_WORK_AROUND
799         if (smsc911x_phy_loopbacktest(dev) < 0) {
800                 SMSC_WARNING(HW, "Failed Loop Back Test");
801                 return -ENODEV;
802         }
803         SMSC_TRACE(HW, "Passed Loop Back Test");
804 #endif                          /* USE_PHY_WORK_AROUND */
805
806         SMSC_TRACE(HW, "phy initialised succesfully");
807         return 0;
808 }
809
810 static int __devinit smsc911x_mii_init(struct platform_device *pdev,
811                                        struct net_device *dev)
812 {
813         struct smsc911x_data *pdata = netdev_priv(dev);
814         int err = -ENXIO, i;
815
816         pdata->mii_bus = mdiobus_alloc();
817         if (!pdata->mii_bus) {
818                 err = -ENOMEM;
819                 goto err_out_1;
820         }
821
822         pdata->mii_bus->name = SMSC_MDIONAME;
823         snprintf(pdata->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
824         pdata->mii_bus->priv = pdata;
825         pdata->mii_bus->read = smsc911x_mii_read;
826         pdata->mii_bus->write = smsc911x_mii_write;
827         pdata->mii_bus->irq = pdata->phy_irq;
828         for (i = 0; i < PHY_MAX_ADDR; ++i)
829                 pdata->mii_bus->irq[i] = PHY_POLL;
830
831         pdata->mii_bus->parent = &pdev->dev;
832
833         switch (pdata->idrev & 0xFFFF0000) {
834         case 0x01170000:
835         case 0x01150000:
836         case 0x117A0000:
837         case 0x115A0000:
838                 /* External PHY supported, try to autodetect */
839                 smsc911x_phy_initialise_external(pdata);
840                 break;
841         default:
842                 SMSC_TRACE(HW, "External PHY is not supported, "
843                         "using internal PHY");
844                 pdata->using_extphy = 0;
845                 break;
846         }
847
848         if (!pdata->using_extphy) {
849                 /* Mask all PHYs except ID 1 (internal) */
850                 pdata->mii_bus->phy_mask = ~(1 << 1);
851         }
852
853         if (mdiobus_register(pdata->mii_bus)) {
854                 SMSC_WARNING(PROBE, "Error registering mii bus");
855                 goto err_out_free_bus_2;
856         }
857
858         if (smsc911x_mii_probe(dev) < 0) {
859                 SMSC_WARNING(PROBE, "Error registering mii bus");
860                 goto err_out_unregister_bus_3;
861         }
862
863         return 0;
864
865 err_out_unregister_bus_3:
866         mdiobus_unregister(pdata->mii_bus);
867 err_out_free_bus_2:
868         mdiobus_free(pdata->mii_bus);
869 err_out_1:
870         return err;
871 }
872
873 /* Gets the number of tx statuses in the fifo */
874 static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data *pdata)
875 {
876         return (smsc911x_reg_read(pdata, TX_FIFO_INF)
877                 & TX_FIFO_INF_TSUSED_) >> 16;
878 }
879
880 /* Reads tx statuses and increments counters where necessary */
881 static void smsc911x_tx_update_txcounters(struct net_device *dev)
882 {
883         struct smsc911x_data *pdata = netdev_priv(dev);
884         unsigned int tx_stat;
885
886         while ((tx_stat = smsc911x_tx_get_txstatus(pdata)) != 0) {
887                 if (unlikely(tx_stat & 0x80000000)) {
888                         /* In this driver the packet tag is used as the packet
889                          * length. Since a packet length can never reach the
890                          * size of 0x8000, this bit is reserved. It is worth
891                          * noting that the "reserved bit" in the warning above
892                          * does not reference a hardware defined reserved bit
893                          * but rather a driver defined one.
894                          */
895                         SMSC_WARNING(HW,
896                                 "Packet tag reserved bit is high");
897                 } else {
898                         if (unlikely(tx_stat & 0x00008000)) {
899                                 dev->stats.tx_errors++;
900                         } else {
901                                 dev->stats.tx_packets++;
902                                 dev->stats.tx_bytes += (tx_stat >> 16);
903                         }
904                         if (unlikely(tx_stat & 0x00000100)) {
905                                 dev->stats.collisions += 16;
906                                 dev->stats.tx_aborted_errors += 1;
907                         } else {
908                                 dev->stats.collisions +=
909                                     ((tx_stat >> 3) & 0xF);
910                         }
911                         if (unlikely(tx_stat & 0x00000800))
912                                 dev->stats.tx_carrier_errors += 1;
913                         if (unlikely(tx_stat & 0x00000200)) {
914                                 dev->stats.collisions++;
915                                 dev->stats.tx_aborted_errors++;
916                         }
917                 }
918         }
919 }
920
921 /* Increments the Rx error counters */
922 static void
923 smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
924 {
925         int crc_err = 0;
926
927         if (unlikely(rxstat & 0x00008000)) {
928                 dev->stats.rx_errors++;
929                 if (unlikely(rxstat & 0x00000002)) {
930                         dev->stats.rx_crc_errors++;
931                         crc_err = 1;
932                 }
933         }
934         if (likely(!crc_err)) {
935                 if (unlikely((rxstat & 0x00001020) == 0x00001020)) {
936                         /* Frame type indicates length,
937                          * and length error is set */
938                         dev->stats.rx_length_errors++;
939                 }
940                 if (rxstat & RX_STS_MCAST_)
941                         dev->stats.multicast++;
942         }
943 }
944
945 /* Quickly dumps bad packets */
946 static void
947 smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int pktbytes)
948 {
949         unsigned int pktwords = (pktbytes + NET_IP_ALIGN + 3) >> 2;
950
951         if (likely(pktwords >= 4)) {
952                 unsigned int timeout = 500;
953                 unsigned int val;
954                 smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
955                 do {
956                         udelay(1);
957                         val = smsc911x_reg_read(pdata, RX_DP_CTRL);
958                 } while (--timeout && (val & RX_DP_CTRL_RX_FFWD_));
959
960                 if (unlikely(timeout == 0))
961                         SMSC_WARNING(HW, "Timed out waiting for "
962                                 "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val);
963         } else {
964                 unsigned int temp;
965                 while (pktwords--)
966                         temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
967         }
968 }
969
970 /* NAPI poll function */
971 static int smsc911x_poll(struct napi_struct *napi, int budget)
972 {
973         struct smsc911x_data *pdata =
974                 container_of(napi, struct smsc911x_data, napi);
975         struct net_device *dev = pdata->dev;
976         int npackets = 0;
977
978         while (likely(netif_running(dev)) && (npackets < budget)) {
979                 unsigned int pktlength;
980                 unsigned int pktwords;
981                 struct sk_buff *skb;
982                 unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);
983
984                 if (!rxstat) {
985                         unsigned int temp;
986                         /* We processed all packets available.  Tell NAPI it can
987                          * stop polling then re-enable rx interrupts */
988                         smsc911x_reg_write(pdata, INT_STS, INT_STS_RSFL_);
989                         napi_complete(napi);
990                         temp = smsc911x_reg_read(pdata, INT_EN);
991                         temp |= INT_EN_RSFL_EN_;
992                         smsc911x_reg_write(pdata, INT_EN, temp);
993                         break;
994                 }
995
996                 /* Count packet for NAPI scheduling, even if it has an error.
997                  * Error packets still require cycles to discard */
998                 npackets++;
999
1000                 pktlength = ((rxstat & 0x3FFF0000) >> 16);
1001                 pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
1002                 smsc911x_rx_counterrors(dev, rxstat);
1003
1004                 if (unlikely(rxstat & RX_STS_ES_)) {
1005                         SMSC_WARNING(RX_ERR,
1006                                 "Discarding packet with error bit set");
1007                         /* Packet has an error, discard it and continue with
1008                          * the next */
1009                         smsc911x_rx_fastforward(pdata, pktwords);
1010                         dev->stats.rx_dropped++;
1011                         continue;
1012                 }
1013
1014                 skb = netdev_alloc_skb(dev, pktlength + NET_IP_ALIGN);
1015                 if (unlikely(!skb)) {
1016                         SMSC_WARNING(RX_ERR,
1017                                 "Unable to allocate skb for rx packet");
1018                         /* Drop the packet and stop this polling iteration */
1019                         smsc911x_rx_fastforward(pdata, pktwords);
1020                         dev->stats.rx_dropped++;
1021                         break;
1022                 }
1023
1024                 skb->data = skb->head;
1025                 skb_reset_tail_pointer(skb);
1026
1027                 /* Align IP on 16B boundary */
1028                 skb_reserve(skb, NET_IP_ALIGN);
1029                 skb_put(skb, pktlength - 4);
1030                 smsc911x_rx_readfifo(pdata, (unsigned int *)skb->head,
1031                                      pktwords);
1032                 skb->protocol = eth_type_trans(skb, dev);
1033                 skb->ip_summed = CHECKSUM_NONE;
1034                 netif_receive_skb(skb);
1035
1036                 /* Update counters */
1037                 dev->stats.rx_packets++;
1038                 dev->stats.rx_bytes += (pktlength - 4);
1039                 dev->last_rx = jiffies;
1040         }
1041
1042         /* Return total received packets */
1043         return npackets;
1044 }
1045
1046 /* Returns hash bit number for given MAC address
1047  * Example:
1048  * 01 00 5E 00 00 01 -> returns bit number 31 */
1049 static unsigned int smsc911x_hash(char addr[ETH_ALEN])
1050 {
1051         return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
1052 }
1053
1054 static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
1055 {
1056         /* Performs the multicast & mac_cr update.  This is called when
1057          * safe on the current hardware, and with the mac_lock held */
1058         unsigned int mac_cr;
1059
1060         SMSC_ASSERT_MAC_LOCK(pdata);
1061
1062         mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1063         mac_cr |= pdata->set_bits_mask;
1064         mac_cr &= ~(pdata->clear_bits_mask);
1065         smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1066         smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
1067         smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
1068         SMSC_TRACE(HW, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
1069                 mac_cr, pdata->hashhi, pdata->hashlo);
1070 }
1071
1072 static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
1073 {
1074         unsigned int mac_cr;
1075
1076         /* This function is only called for older LAN911x devices
1077          * (revA or revB), where MAC_CR, HASHH and HASHL should not
1078          * be modified during Rx - newer devices immediately update the
1079          * registers.
1080          *
1081          * This is called from interrupt context */
1082
1083         spin_lock(&pdata->mac_lock);
1084
1085         /* Check Rx has stopped */
1086         if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
1087                 SMSC_WARNING(DRV, "Rx not stopped");
1088
1089         /* Perform the update - safe to do now Rx has stopped */
1090         smsc911x_rx_multicast_update(pdata);
1091
1092         /* Re-enable Rx */
1093         mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1094         mac_cr |= MAC_CR_RXEN_;
1095         smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1096
1097         pdata->multicast_update_pending = 0;
1098
1099         spin_unlock(&pdata->mac_lock);
1100 }
1101
1102 static int smsc911x_soft_reset(struct smsc911x_data *pdata)
1103 {
1104         unsigned int timeout;
1105         unsigned int temp;
1106
1107         /* Reset the LAN911x */
1108         smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
1109         timeout = 10;
1110         do {
1111                 udelay(10);
1112                 temp = smsc911x_reg_read(pdata, HW_CFG);
1113         } while ((--timeout) && (temp & HW_CFG_SRST_));
1114
1115         if (unlikely(temp & HW_CFG_SRST_)) {
1116                 SMSC_WARNING(DRV, "Failed to complete reset");
1117                 return -EIO;
1118         }
1119         return 0;
1120 }
1121
1122 /* Sets the device MAC address to dev_addr, called with mac_lock held */
1123 static void
1124 smsc911x_set_mac_address(struct smsc911x_data *pdata, u8 dev_addr[6])
1125 {
1126         u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
1127         u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
1128             (dev_addr[1] << 8) | dev_addr[0];
1129
1130         SMSC_ASSERT_MAC_LOCK(pdata);
1131
1132         smsc911x_mac_write(pdata, ADDRH, mac_high16);
1133         smsc911x_mac_write(pdata, ADDRL, mac_low32);
1134 }
1135
1136 static int smsc911x_open(struct net_device *dev)
1137 {
1138         struct smsc911x_data *pdata = netdev_priv(dev);
1139         unsigned int timeout;
1140         unsigned int temp;
1141         unsigned int intcfg;
1142
1143         /* if the phy is not yet registered, retry later*/
1144         if (!pdata->phy_dev) {
1145                 SMSC_WARNING(HW, "phy_dev is NULL");
1146                 return -EAGAIN;
1147         }
1148
1149         if (!is_valid_ether_addr(dev->dev_addr)) {
1150                 SMSC_WARNING(HW, "dev_addr is not a valid MAC address");
1151                 return -EADDRNOTAVAIL;
1152         }
1153
1154         /* Reset the LAN911x */
1155         if (smsc911x_soft_reset(pdata)) {
1156                 SMSC_WARNING(HW, "soft reset failed");
1157                 return -EIO;
1158         }
1159
1160         smsc911x_reg_write(pdata, HW_CFG, 0x00050000);
1161         smsc911x_reg_write(pdata, AFC_CFG, 0x006E3740);
1162
1163         /* Make sure EEPROM has finished loading before setting GPIO_CFG */
1164         timeout = 50;
1165         while ((timeout--) &&
1166                (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
1167                 udelay(10);
1168         }
1169
1170         if (unlikely(timeout == 0))
1171                 SMSC_WARNING(IFUP,
1172                         "Timed out waiting for EEPROM busy bit to clear");
1173
1174         smsc911x_reg_write(pdata, GPIO_CFG, 0x70070000);
1175
1176         /* The soft reset above cleared the device's MAC address,
1177          * restore it from local copy (set in probe) */
1178         spin_lock_irq(&pdata->mac_lock);
1179         smsc911x_set_mac_address(pdata, dev->dev_addr);
1180         spin_unlock_irq(&pdata->mac_lock);
1181
1182         /* Initialise irqs, but leave all sources disabled */
1183         smsc911x_reg_write(pdata, INT_EN, 0);
1184         smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
1185
1186         /* Set interrupt deassertion to 100uS */
1187         intcfg = ((10 << 24) | INT_CFG_IRQ_EN_);
1188
1189         if (pdata->config.irq_polarity) {
1190                 SMSC_TRACE(IFUP, "irq polarity: active high");
1191                 intcfg |= INT_CFG_IRQ_POL_;
1192         } else {
1193                 SMSC_TRACE(IFUP, "irq polarity: active low");
1194         }
1195
1196         if (pdata->config.irq_type) {
1197                 SMSC_TRACE(IFUP, "irq type: push-pull");
1198                 intcfg |= INT_CFG_IRQ_TYPE_;
1199         } else {
1200                 SMSC_TRACE(IFUP, "irq type: open drain");
1201         }
1202
1203         smsc911x_reg_write(pdata, INT_CFG, intcfg);
1204
1205         SMSC_TRACE(IFUP, "Testing irq handler using IRQ %d", dev->irq);
1206         pdata->software_irq_signal = 0;
1207         smp_wmb();
1208
1209         temp = smsc911x_reg_read(pdata, INT_EN);
1210         temp |= INT_EN_SW_INT_EN_;
1211         smsc911x_reg_write(pdata, INT_EN, temp);
1212
1213         timeout = 1000;
1214         while (timeout--) {
1215                 if (pdata->software_irq_signal)
1216                         break;
1217                 msleep(1);
1218         }
1219
1220         if (!pdata->software_irq_signal) {
1221                 dev_warn(&dev->dev, "ISR failed signaling test (IRQ %d)\n",
1222                          dev->irq);
1223                 return -ENODEV;
1224         }
1225         SMSC_TRACE(IFUP, "IRQ handler passed test using IRQ %d", dev->irq);
1226
1227         dev_info(&dev->dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
1228                  (unsigned long)pdata->ioaddr, dev->irq);
1229
1230         /* Bring the PHY up */
1231         phy_start(pdata->phy_dev);
1232
1233         temp = smsc911x_reg_read(pdata, HW_CFG);
1234         /* Preserve TX FIFO size and external PHY configuration */
1235         temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
1236         temp |= HW_CFG_SF_;
1237         smsc911x_reg_write(pdata, HW_CFG, temp);
1238
1239         temp = smsc911x_reg_read(pdata, FIFO_INT);
1240         temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1241         temp &= ~(FIFO_INT_RX_STS_LEVEL_);
1242         smsc911x_reg_write(pdata, FIFO_INT, temp);
1243
1244         /* set RX Data offset to 2 bytes for alignment */
1245         smsc911x_reg_write(pdata, RX_CFG, (2 << 8));
1246
1247         /* enable NAPI polling before enabling RX interrupts */
1248         napi_enable(&pdata->napi);
1249
1250         temp = smsc911x_reg_read(pdata, INT_EN);
1251         temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_RXSTOP_INT_EN_);
1252         smsc911x_reg_write(pdata, INT_EN, temp);
1253
1254         spin_lock_irq(&pdata->mac_lock);
1255         temp = smsc911x_mac_read(pdata, MAC_CR);
1256         temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
1257         smsc911x_mac_write(pdata, MAC_CR, temp);
1258         spin_unlock_irq(&pdata->mac_lock);
1259
1260         smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
1261
1262         netif_start_queue(dev);
1263         return 0;
1264 }
1265
1266 /* Entry point for stopping the interface */
1267 static int smsc911x_stop(struct net_device *dev)
1268 {
1269         struct smsc911x_data *pdata = netdev_priv(dev);
1270         unsigned int temp;
1271
1272         /* Disable all device interrupts */
1273         temp = smsc911x_reg_read(pdata, INT_CFG);
1274         temp &= ~INT_CFG_IRQ_EN_;
1275         smsc911x_reg_write(pdata, INT_CFG, temp);
1276
1277         /* Stop Tx and Rx polling */
1278         netif_stop_queue(dev);
1279         napi_disable(&pdata->napi);
1280
1281         /* At this point all Rx and Tx activity is stopped */
1282         dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1283         smsc911x_tx_update_txcounters(dev);
1284
1285         /* Bring the PHY down */
1286         if (pdata->phy_dev)
1287                 phy_stop(pdata->phy_dev);
1288
1289         SMSC_TRACE(IFDOWN, "Interface stopped");
1290         return 0;
1291 }
1292
1293 /* Entry point for transmitting a packet */
1294 static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1295 {
1296         struct smsc911x_data *pdata = netdev_priv(dev);
1297         unsigned int freespace;
1298         unsigned int tx_cmd_a;
1299         unsigned int tx_cmd_b;
1300         unsigned int temp;
1301         u32 wrsz;
1302         ulong bufp;
1303
1304         freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;
1305
1306         if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
1307                 SMSC_WARNING(TX_ERR,
1308                         "Tx data fifo low, space available: %d", freespace);
1309
1310         /* Word alignment adjustment */
1311         tx_cmd_a = (u32)((ulong)skb->data & 0x03) << 16;
1312         tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1313         tx_cmd_a |= (unsigned int)skb->len;
1314
1315         tx_cmd_b = ((unsigned int)skb->len) << 16;
1316         tx_cmd_b |= (unsigned int)skb->len;
1317
1318         smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_a);
1319         smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_b);
1320
1321         bufp = (ulong)skb->data & (~0x3);
1322         wrsz = (u32)skb->len + 3;
1323         wrsz += (u32)((ulong)skb->data & 0x3);
1324         wrsz >>= 2;
1325
1326         smsc911x_tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
1327         freespace -= (skb->len + 32);
1328         dev_kfree_skb(skb);
1329         dev->trans_start = jiffies;
1330
1331         if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
1332                 smsc911x_tx_update_txcounters(dev);
1333
1334         if (freespace < TX_FIFO_LOW_THRESHOLD) {
1335                 netif_stop_queue(dev);
1336                 temp = smsc911x_reg_read(pdata, FIFO_INT);
1337                 temp &= 0x00FFFFFF;
1338                 temp |= 0x32000000;
1339                 smsc911x_reg_write(pdata, FIFO_INT, temp);
1340         }
1341
1342         return NETDEV_TX_OK;
1343 }
1344
1345 /* Entry point for getting status counters */
1346 static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
1347 {
1348         struct smsc911x_data *pdata = netdev_priv(dev);
1349         smsc911x_tx_update_txcounters(dev);
1350         dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1351         return &dev->stats;
1352 }
1353
1354 /* Entry point for setting addressing modes */
1355 static void smsc911x_set_multicast_list(struct net_device *dev)
1356 {
1357         struct smsc911x_data *pdata = netdev_priv(dev);
1358         unsigned long flags;
1359
1360         if (dev->flags & IFF_PROMISC) {
1361                 /* Enabling promiscuous mode */
1362                 pdata->set_bits_mask = MAC_CR_PRMS_;
1363                 pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1364                 pdata->hashhi = 0;
1365                 pdata->hashlo = 0;
1366         } else if (dev->flags & IFF_ALLMULTI) {
1367                 /* Enabling all multicast mode */
1368                 pdata->set_bits_mask = MAC_CR_MCPAS_;
1369                 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
1370                 pdata->hashhi = 0;
1371                 pdata->hashlo = 0;
1372         } else if (dev->mc_count > 0) {
1373                 /* Enabling specific multicast addresses */
1374                 unsigned int hash_high = 0;
1375                 unsigned int hash_low = 0;
1376                 unsigned int count = 0;
1377                 struct dev_mc_list *mc_list = dev->mc_list;
1378
1379                 pdata->set_bits_mask = MAC_CR_HPFILT_;
1380                 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1381
1382                 while (mc_list) {
1383                         count++;
1384                         if ((mc_list->dmi_addrlen) == ETH_ALEN) {
1385                                 unsigned int bitnum =
1386                                     smsc911x_hash(mc_list->dmi_addr);
1387                                 unsigned int mask = 0x01 << (bitnum & 0x1F);
1388                                 if (bitnum & 0x20)
1389                                         hash_high |= mask;
1390                                 else
1391                                         hash_low |= mask;
1392                         } else {
1393                                 SMSC_WARNING(DRV, "dmi_addrlen != 6");
1394                         }
1395                         mc_list = mc_list->next;
1396                 }
1397                 if (count != (unsigned int)dev->mc_count)
1398                         SMSC_WARNING(DRV, "mc_count != dev->mc_count");
1399
1400                 pdata->hashhi = hash_high;
1401                 pdata->hashlo = hash_low;
1402         } else {
1403                 /* Enabling local MAC address only */
1404                 pdata->set_bits_mask = 0;
1405                 pdata->clear_bits_mask =
1406                     (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1407                 pdata->hashhi = 0;
1408                 pdata->hashlo = 0;
1409         }
1410
1411         spin_lock_irqsave(&pdata->mac_lock, flags);
1412
1413         if (pdata->generation <= 1) {
1414                 /* Older hardware revision - cannot change these flags while
1415                  * receiving data */
1416                 if (!pdata->multicast_update_pending) {
1417                         unsigned int temp;
1418                         SMSC_TRACE(HW, "scheduling mcast update");
1419                         pdata->multicast_update_pending = 1;
1420
1421                         /* Request the hardware to stop, then perform the
1422                          * update when we get an RX_STOP interrupt */
1423                         temp = smsc911x_mac_read(pdata, MAC_CR);
1424                         temp &= ~(MAC_CR_RXEN_);
1425                         smsc911x_mac_write(pdata, MAC_CR, temp);
1426                 } else {
1427                         /* There is another update pending, this should now
1428                          * use the newer values */
1429                 }
1430         } else {
1431                 /* Newer hardware revision - can write immediately */
1432                 smsc911x_rx_multicast_update(pdata);
1433         }
1434
1435         spin_unlock_irqrestore(&pdata->mac_lock, flags);
1436 }
1437
1438 static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
1439 {
1440         struct net_device *dev = dev_id;
1441         struct smsc911x_data *pdata = netdev_priv(dev);
1442         u32 intsts = smsc911x_reg_read(pdata, INT_STS);
1443         u32 inten = smsc911x_reg_read(pdata, INT_EN);
1444         int serviced = IRQ_NONE;
1445         u32 temp;
1446
1447         if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
1448                 temp = smsc911x_reg_read(pdata, INT_EN);
1449                 temp &= (~INT_EN_SW_INT_EN_);
1450                 smsc911x_reg_write(pdata, INT_EN, temp);
1451                 smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
1452                 pdata->software_irq_signal = 1;
1453                 smp_wmb();
1454                 serviced = IRQ_HANDLED;
1455         }
1456
1457         if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
1458                 /* Called when there is a multicast update scheduled and
1459                  * it is now safe to complete the update */
1460                 SMSC_TRACE(INTR, "RX Stop interrupt");
1461                 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
1462                 if (pdata->multicast_update_pending)
1463                         smsc911x_rx_multicast_update_workaround(pdata);
1464                 serviced = IRQ_HANDLED;
1465         }
1466
1467         if (intsts & inten & INT_STS_TDFA_) {
1468                 temp = smsc911x_reg_read(pdata, FIFO_INT);
1469                 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1470                 smsc911x_reg_write(pdata, FIFO_INT, temp);
1471                 smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
1472                 netif_wake_queue(dev);
1473                 serviced = IRQ_HANDLED;
1474         }
1475
1476         if (unlikely(intsts & inten & INT_STS_RXE_)) {
1477                 SMSC_TRACE(INTR, "RX Error interrupt");
1478                 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
1479                 serviced = IRQ_HANDLED;
1480         }
1481
1482         if (likely(intsts & inten & INT_STS_RSFL_)) {
1483                 if (likely(napi_schedule_prep(&pdata->napi))) {
1484                         /* Disable Rx interrupts */
1485                         temp = smsc911x_reg_read(pdata, INT_EN);
1486                         temp &= (~INT_EN_RSFL_EN_);
1487                         smsc911x_reg_write(pdata, INT_EN, temp);
1488                         /* Schedule a NAPI poll */
1489                         __napi_schedule(&pdata->napi);
1490                 } else {
1491                         SMSC_WARNING(RX_ERR,
1492                                 "napi_schedule_prep failed");
1493                 }
1494                 serviced = IRQ_HANDLED;
1495         }
1496
1497         return serviced;
1498 }
1499
1500 #ifdef CONFIG_NET_POLL_CONTROLLER
1501 static void smsc911x_poll_controller(struct net_device *dev)
1502 {
1503         disable_irq(dev->irq);
1504         smsc911x_irqhandler(0, dev);
1505         enable_irq(dev->irq);
1506 }
1507 #endif                          /* CONFIG_NET_POLL_CONTROLLER */
1508
1509 /* Standard ioctls for mii-tool */
1510 static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1511 {
1512         struct smsc911x_data *pdata = netdev_priv(dev);
1513
1514         if (!netif_running(dev) || !pdata->phy_dev)
1515                 return -EINVAL;
1516
1517         return phy_mii_ioctl(pdata->phy_dev, if_mii(ifr), cmd);
1518 }
1519
1520 static int
1521 smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1522 {
1523         struct smsc911x_data *pdata = netdev_priv(dev);
1524
1525         cmd->maxtxpkt = 1;
1526         cmd->maxrxpkt = 1;
1527         return phy_ethtool_gset(pdata->phy_dev, cmd);
1528 }
1529
1530 static int
1531 smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1532 {
1533         struct smsc911x_data *pdata = netdev_priv(dev);
1534
1535         return phy_ethtool_sset(pdata->phy_dev, cmd);
1536 }
1537
1538 static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
1539                                         struct ethtool_drvinfo *info)
1540 {
1541         strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
1542         strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
1543         strlcpy(info->bus_info, dev_name(dev->dev.parent),
1544                 sizeof(info->bus_info));
1545 }
1546
1547 static int smsc911x_ethtool_nwayreset(struct net_device *dev)
1548 {
1549         struct smsc911x_data *pdata = netdev_priv(dev);
1550
1551         return phy_start_aneg(pdata->phy_dev);
1552 }
1553
1554 static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
1555 {
1556         struct smsc911x_data *pdata = netdev_priv(dev);
1557         return pdata->msg_enable;
1558 }
1559
1560 static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
1561 {
1562         struct smsc911x_data *pdata = netdev_priv(dev);
1563         pdata->msg_enable = level;
1564 }
1565
1566 static int smsc911x_ethtool_getregslen(struct net_device *dev)
1567 {
1568         return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
1569             sizeof(u32);
1570 }
1571
1572 static void
1573 smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
1574                          void *buf)
1575 {
1576         struct smsc911x_data *pdata = netdev_priv(dev);
1577         struct phy_device *phy_dev = pdata->phy_dev;
1578         unsigned long flags;
1579         unsigned int i;
1580         unsigned int j = 0;
1581         u32 *data = buf;
1582
1583         regs->version = pdata->idrev;
1584         for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
1585                 data[j++] = smsc911x_reg_read(pdata, i);
1586
1587         for (i = MAC_CR; i <= WUCSR; i++) {
1588                 spin_lock_irqsave(&pdata->mac_lock, flags);
1589                 data[j++] = smsc911x_mac_read(pdata, i);
1590                 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1591         }
1592
1593         for (i = 0; i <= 31; i++)
1594                 data[j++] = smsc911x_mii_read(phy_dev->bus, phy_dev->addr, i);
1595 }
1596
1597 static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
1598 {
1599         unsigned int temp = smsc911x_reg_read(pdata, GPIO_CFG);
1600         temp &= ~GPIO_CFG_EEPR_EN_;
1601         smsc911x_reg_write(pdata, GPIO_CFG, temp);
1602         msleep(1);
1603 }
1604
1605 static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
1606 {
1607         int timeout = 100;
1608         u32 e2cmd;
1609
1610         SMSC_TRACE(DRV, "op 0x%08x", op);
1611         if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
1612                 SMSC_WARNING(DRV, "Busy at start");
1613                 return -EBUSY;
1614         }
1615
1616         e2cmd = op | E2P_CMD_EPC_BUSY_;
1617         smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
1618
1619         do {
1620                 msleep(1);
1621                 e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
1622         } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (timeout--));
1623
1624         if (!timeout) {
1625                 SMSC_TRACE(DRV, "TIMED OUT");
1626                 return -EAGAIN;
1627         }
1628
1629         if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
1630                 SMSC_TRACE(DRV, "Error occured during eeprom operation");
1631                 return -EINVAL;
1632         }
1633
1634         return 0;
1635 }
1636
1637 static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
1638                                          u8 address, u8 *data)
1639 {
1640         u32 op = E2P_CMD_EPC_CMD_READ_ | address;
1641         int ret;
1642
1643         SMSC_TRACE(DRV, "address 0x%x", address);
1644         ret = smsc911x_eeprom_send_cmd(pdata, op);
1645
1646         if (!ret)
1647                 data[address] = smsc911x_reg_read(pdata, E2P_DATA);
1648
1649         return ret;
1650 }
1651
1652 static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
1653                                           u8 address, u8 data)
1654 {
1655         u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
1656         int ret;
1657
1658         SMSC_TRACE(DRV, "address 0x%x, data 0x%x", address, data);
1659         ret = smsc911x_eeprom_send_cmd(pdata, op);
1660
1661         if (!ret) {
1662                 op = E2P_CMD_EPC_CMD_WRITE_ | address;
1663                 smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
1664                 ret = smsc911x_eeprom_send_cmd(pdata, op);
1665         }
1666
1667         return ret;
1668 }
1669
1670 static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
1671 {
1672         return SMSC911X_EEPROM_SIZE;
1673 }
1674
1675 static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
1676                                        struct ethtool_eeprom *eeprom, u8 *data)
1677 {
1678         struct smsc911x_data *pdata = netdev_priv(dev);
1679         u8 eeprom_data[SMSC911X_EEPROM_SIZE];
1680         int len;
1681         int i;
1682
1683         smsc911x_eeprom_enable_access(pdata);
1684
1685         len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
1686         for (i = 0; i < len; i++) {
1687                 int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
1688                 if (ret < 0) {
1689                         eeprom->len = 0;
1690                         return ret;
1691                 }
1692         }
1693
1694         memcpy(data, &eeprom_data[eeprom->offset], len);
1695         eeprom->len = len;
1696         return 0;
1697 }
1698
1699 static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
1700                                        struct ethtool_eeprom *eeprom, u8 *data)
1701 {
1702         int ret;
1703         struct smsc911x_data *pdata = netdev_priv(dev);
1704
1705         smsc911x_eeprom_enable_access(pdata);
1706         smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
1707         ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
1708         smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
1709
1710         /* Single byte write, according to man page */
1711         eeprom->len = 1;
1712
1713         return ret;
1714 }
1715
1716 static const struct ethtool_ops smsc911x_ethtool_ops = {
1717         .get_settings = smsc911x_ethtool_getsettings,
1718         .set_settings = smsc911x_ethtool_setsettings,
1719         .get_link = ethtool_op_get_link,
1720         .get_drvinfo = smsc911x_ethtool_getdrvinfo,
1721         .nway_reset = smsc911x_ethtool_nwayreset,
1722         .get_msglevel = smsc911x_ethtool_getmsglevel,
1723         .set_msglevel = smsc911x_ethtool_setmsglevel,
1724         .get_regs_len = smsc911x_ethtool_getregslen,
1725         .get_regs = smsc911x_ethtool_getregs,
1726         .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
1727         .get_eeprom = smsc911x_ethtool_get_eeprom,
1728         .set_eeprom = smsc911x_ethtool_set_eeprom,
1729 };
1730
1731 static const struct net_device_ops smsc911x_netdev_ops = {
1732         .ndo_open               = smsc911x_open,
1733         .ndo_stop               = smsc911x_stop,
1734         .ndo_start_xmit         = smsc911x_hard_start_xmit,
1735         .ndo_get_stats          = smsc911x_get_stats,
1736         .ndo_set_multicast_list = smsc911x_set_multicast_list,
1737         .ndo_do_ioctl           = smsc911x_do_ioctl,
1738         .ndo_validate_addr      = eth_validate_addr,
1739         .ndo_set_mac_address    = eth_mac_addr,
1740 #ifdef CONFIG_NET_POLL_CONTROLLER
1741         .ndo_poll_controller    = smsc911x_poll_controller,
1742 #endif
1743 };
1744
1745 /* copies the current mac address from hardware to dev->dev_addr */
1746 static void __devinit smsc911x_read_mac_address(struct net_device *dev)
1747 {
1748         struct smsc911x_data *pdata = netdev_priv(dev);
1749         u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
1750         u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
1751
1752         dev->dev_addr[0] = (u8)(mac_low32);
1753         dev->dev_addr[1] = (u8)(mac_low32 >> 8);
1754         dev->dev_addr[2] = (u8)(mac_low32 >> 16);
1755         dev->dev_addr[3] = (u8)(mac_low32 >> 24);
1756         dev->dev_addr[4] = (u8)(mac_high16);
1757         dev->dev_addr[5] = (u8)(mac_high16 >> 8);
1758 }
1759
1760 /* Initializing private device structures, only called from probe */
1761 static int __devinit smsc911x_init(struct net_device *dev)
1762 {
1763         struct smsc911x_data *pdata = netdev_priv(dev);
1764         unsigned int byte_test;
1765
1766         SMSC_TRACE(PROBE, "Driver Parameters:");
1767         SMSC_TRACE(PROBE, "LAN base: 0x%08lX",
1768                 (unsigned long)pdata->ioaddr);
1769         SMSC_TRACE(PROBE, "IRQ: %d", dev->irq);
1770         SMSC_TRACE(PROBE, "PHY will be autodetected.");
1771
1772         spin_lock_init(&pdata->dev_lock);
1773
1774         if (pdata->ioaddr == 0) {
1775                 SMSC_WARNING(PROBE, "pdata->ioaddr: 0x00000000");
1776                 return -ENODEV;
1777         }
1778
1779         /* Check byte ordering */
1780         byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1781         SMSC_TRACE(PROBE, "BYTE_TEST: 0x%08X", byte_test);
1782         if (byte_test == 0x43218765) {
1783                 SMSC_TRACE(PROBE, "BYTE_TEST looks swapped, "
1784                         "applying WORD_SWAP");
1785                 smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
1786
1787                 /* 1 dummy read of BYTE_TEST is needed after a write to
1788                  * WORD_SWAP before its contents are valid */
1789                 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1790
1791                 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1792         }
1793
1794         if (byte_test != 0x87654321) {
1795                 SMSC_WARNING(DRV, "BYTE_TEST: 0x%08X", byte_test);
1796                 if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
1797                         SMSC_WARNING(PROBE,
1798                                 "top 16 bits equal to bottom 16 bits");
1799                         SMSC_TRACE(PROBE, "This may mean the chip is set "
1800                                 "for 32 bit while the bus is reading 16 bit");
1801                 }
1802                 return -ENODEV;
1803         }
1804
1805         /* Default generation to zero (all workarounds apply) */
1806         pdata->generation = 0;
1807
1808         pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
1809         switch (pdata->idrev & 0xFFFF0000) {
1810         case 0x01180000:
1811         case 0x01170000:
1812         case 0x01160000:
1813         case 0x01150000:
1814                 /* LAN911[5678] family */
1815                 pdata->generation = pdata->idrev & 0x0000FFFF;
1816                 break;
1817
1818         case 0x118A0000:
1819         case 0x117A0000:
1820         case 0x116A0000:
1821         case 0x115A0000:
1822                 /* LAN921[5678] family */
1823                 pdata->generation = 3;
1824                 break;
1825
1826         case 0x92100000:
1827         case 0x92110000:
1828         case 0x92200000:
1829         case 0x92210000:
1830                 /* LAN9210/LAN9211/LAN9220/LAN9221 */
1831                 pdata->generation = 4;
1832                 break;
1833
1834         default:
1835                 SMSC_WARNING(PROBE, "LAN911x not identified, idrev: 0x%08X",
1836                         pdata->idrev);
1837                 return -ENODEV;
1838         }
1839
1840         SMSC_TRACE(PROBE, "LAN911x identified, idrev: 0x%08X, generation: %d",
1841                 pdata->idrev, pdata->generation);
1842
1843         if (pdata->generation == 0)
1844                 SMSC_WARNING(PROBE,
1845                         "This driver is not intended for this chip revision");
1846
1847         /* workaround for platforms without an eeprom, where the mac address
1848          * is stored elsewhere and set by the bootloader.  This saves the
1849          * mac address before resetting the device */
1850         if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS)
1851                 smsc911x_read_mac_address(dev);
1852
1853         /* Reset the LAN911x */
1854         if (smsc911x_soft_reset(pdata))
1855                 return -ENODEV;
1856
1857         /* Disable all interrupt sources until we bring the device up */
1858         smsc911x_reg_write(pdata, INT_EN, 0);
1859
1860         ether_setup(dev);
1861         dev->flags |= IFF_MULTICAST;
1862         netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
1863         dev->netdev_ops = &smsc911x_netdev_ops;
1864         dev->ethtool_ops = &smsc911x_ethtool_ops;
1865
1866         return 0;
1867 }
1868
1869 static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
1870 {
1871         struct net_device *dev;
1872         struct smsc911x_data *pdata;
1873         struct resource *res;
1874
1875         dev = platform_get_drvdata(pdev);
1876         BUG_ON(!dev);
1877         pdata = netdev_priv(dev);
1878         BUG_ON(!pdata);
1879         BUG_ON(!pdata->ioaddr);
1880         BUG_ON(!pdata->phy_dev);
1881
1882         SMSC_TRACE(IFDOWN, "Stopping driver.");
1883
1884         phy_disconnect(pdata->phy_dev);
1885         pdata->phy_dev = NULL;
1886         mdiobus_unregister(pdata->mii_bus);
1887         mdiobus_free(pdata->mii_bus);
1888
1889         platform_set_drvdata(pdev, NULL);
1890         unregister_netdev(dev);
1891         free_irq(dev->irq, dev);
1892         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1893                                            "smsc911x-memory");
1894         if (!res)
1895                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1896
1897         release_mem_region(res->start, res->end - res->start);
1898
1899         iounmap(pdata->ioaddr);
1900
1901         free_netdev(dev);
1902
1903         return 0;
1904 }
1905
1906 static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
1907 {
1908         struct net_device *dev;
1909         struct smsc911x_data *pdata;
1910         struct smsc911x_platform_config *config = pdev->dev.platform_data;
1911         struct resource *res, *irq_res;
1912         unsigned int intcfg = 0;
1913         int res_size, irq_flags;
1914         int retval;
1915         DECLARE_MAC_BUF(mac);
1916
1917         pr_info("%s: Driver version %s.\n", SMSC_CHIPNAME, SMSC_DRV_VERSION);
1918
1919         /* platform data specifies irq & dynamic bus configuration */
1920         if (!pdev->dev.platform_data) {
1921                 pr_warning("%s: platform_data not provided\n", SMSC_CHIPNAME);
1922                 retval = -ENODEV;
1923                 goto out_0;
1924         }
1925
1926         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1927                                            "smsc911x-memory");
1928         if (!res)
1929                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1930         if (!res) {
1931                 pr_warning("%s: Could not allocate resource.\n",
1932                         SMSC_CHIPNAME);
1933                 retval = -ENODEV;
1934                 goto out_0;
1935         }
1936         res_size = res->end - res->start;
1937
1938         irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1939         if (!irq_res) {
1940                 pr_warning("%s: Could not allocate irq resource.\n",
1941                         SMSC_CHIPNAME);
1942                 retval = -ENODEV;
1943                 goto out_0;
1944         }
1945
1946         if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
1947                 retval = -EBUSY;
1948                 goto out_0;
1949         }
1950
1951         dev = alloc_etherdev(sizeof(struct smsc911x_data));
1952         if (!dev) {
1953                 pr_warning("%s: Could not allocate device.\n", SMSC_CHIPNAME);
1954                 retval = -ENOMEM;
1955                 goto out_release_io_1;
1956         }
1957
1958         SET_NETDEV_DEV(dev, &pdev->dev);
1959
1960         pdata = netdev_priv(dev);
1961
1962         dev->irq = irq_res->start;
1963         irq_flags = irq_res->flags & IRQF_TRIGGER_MASK;
1964         pdata->ioaddr = ioremap_nocache(res->start, res_size);
1965
1966         /* copy config parameters across to pdata */
1967         memcpy(&pdata->config, config, sizeof(pdata->config));
1968
1969         pdata->dev = dev;
1970         pdata->msg_enable = ((1 << debug) - 1);
1971
1972         if (pdata->ioaddr == NULL) {
1973                 SMSC_WARNING(PROBE,
1974                         "Error smsc911x base address invalid");
1975                 retval = -ENOMEM;
1976                 goto out_free_netdev_2;
1977         }
1978
1979         retval = smsc911x_init(dev);
1980         if (retval < 0)
1981                 goto out_unmap_io_3;
1982
1983         /* configure irq polarity and type before connecting isr */
1984         if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
1985                 intcfg |= INT_CFG_IRQ_POL_;
1986
1987         if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
1988                 intcfg |= INT_CFG_IRQ_TYPE_;
1989
1990         smsc911x_reg_write(pdata, INT_CFG, intcfg);
1991
1992         /* Ensure interrupts are globally disabled before connecting ISR */
1993         smsc911x_reg_write(pdata, INT_EN, 0);
1994         smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
1995
1996         retval = request_irq(dev->irq, smsc911x_irqhandler,
1997                              irq_flags | IRQF_SHARED, dev->name, dev);
1998         if (retval) {
1999                 SMSC_WARNING(PROBE,
2000                         "Unable to claim requested irq: %d", dev->irq);
2001                 goto out_unmap_io_3;
2002         }
2003
2004         platform_set_drvdata(pdev, dev);
2005
2006         retval = register_netdev(dev);
2007         if (retval) {
2008                 SMSC_WARNING(PROBE,
2009                         "Error %i registering device", retval);
2010                 goto out_unset_drvdata_4;
2011         } else {
2012                 SMSC_TRACE(PROBE, "Network interface: \"%s\"", dev->name);
2013         }
2014
2015         spin_lock_init(&pdata->mac_lock);
2016
2017         retval = smsc911x_mii_init(pdev, dev);
2018         if (retval) {
2019                 SMSC_WARNING(PROBE,
2020                         "Error %i initialising mii", retval);
2021                 goto out_unregister_netdev_5;
2022         }
2023
2024         spin_lock_irq(&pdata->mac_lock);
2025
2026         /* Check if mac address has been specified when bringing interface up */
2027         if (is_valid_ether_addr(dev->dev_addr)) {
2028                 smsc911x_set_mac_address(pdata, dev->dev_addr);
2029                 SMSC_TRACE(PROBE, "MAC Address is specified by configuration");
2030         } else {
2031                 /* Try reading mac address from device. if EEPROM is present
2032                  * it will already have been set */
2033                 smsc911x_read_mac_address(dev);
2034
2035                 if (is_valid_ether_addr(dev->dev_addr)) {
2036                         /* eeprom values are valid  so use them */
2037                         SMSC_TRACE(PROBE,
2038                                 "Mac Address is read from LAN911x EEPROM");
2039                 } else {
2040                         /* eeprom values are invalid, generate random MAC */
2041                         random_ether_addr(dev->dev_addr);
2042                         smsc911x_set_mac_address(pdata, dev->dev_addr);
2043                         SMSC_TRACE(PROBE,
2044                                 "MAC Address is set to random_ether_addr");
2045                 }
2046         }
2047
2048         spin_unlock_irq(&pdata->mac_lock);
2049
2050         dev_info(&dev->dev, "MAC Address: %s\n",
2051                  print_mac(mac, dev->dev_addr));
2052
2053         return 0;
2054
2055 out_unregister_netdev_5:
2056         unregister_netdev(dev);
2057 out_unset_drvdata_4:
2058         platform_set_drvdata(pdev, NULL);
2059         free_irq(dev->irq, dev);
2060 out_unmap_io_3:
2061         iounmap(pdata->ioaddr);
2062 out_free_netdev_2:
2063         free_netdev(dev);
2064 out_release_io_1:
2065         release_mem_region(res->start, res->end - res->start);
2066 out_0:
2067         return retval;
2068 }
2069
2070 static struct platform_driver smsc911x_driver = {
2071         .probe = smsc911x_drv_probe,
2072         .remove = smsc911x_drv_remove,
2073         .driver = {
2074                 .name = SMSC_CHIPNAME,
2075         },
2076 };
2077
2078 /* Entry point for loading the module */
2079 static int __init smsc911x_init_module(void)
2080 {
2081         return platform_driver_register(&smsc911x_driver);
2082 }
2083
2084 /* entry point for unloading the module */
2085 static void __exit smsc911x_cleanup_module(void)
2086 {
2087         platform_driver_unregister(&smsc911x_driver);
2088 }
2089
2090 module_init(smsc911x_init_module);
2091 module_exit(smsc911x_cleanup_module);