Merge remote-tracking branches 'asoc/fix/adsp', 'asoc/fix/arizona', 'asoc/fix/atmel...
[linux-drm-fsl-dcu.git] / drivers / net / ethernet / smsc / smc91x.c
1 /*
2  * smc91x.c
3  * This is a driver for SMSC's 91C9x/91C1xx single-chip Ethernet devices.
4  *
5  * Copyright (C) 1996 by Erik Stahlman
6  * Copyright (C) 2001 Standard Microsystems Corporation
7  *      Developed by Simple Network Magic Corporation
8  * Copyright (C) 2003 Monta Vista Software, Inc.
9  *      Unified SMC91x driver by Nicolas Pitre
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  * Arguments:
26  *      io      = for the base address
27  *      irq     = for the IRQ
28  *      nowait  = 0 for normal wait states, 1 eliminates additional wait states
29  *
30  * original author:
31  *      Erik Stahlman <erik@vt.edu>
32  *
33  * hardware multicast code:
34  *    Peter Cammaert <pc@denkart.be>
35  *
36  * contributors:
37  *      Daris A Nevil <dnevil@snmc.com>
38  *      Nicolas Pitre <nico@fluxnic.net>
39  *      Russell King <rmk@arm.linux.org.uk>
40  *
41  * History:
42  *   08/20/00  Arnaldo Melo       fix kfree(skb) in smc_hardware_send_packet
43  *   12/15/00  Christian Jullien  fix "Warning: kfree_skb on hard IRQ"
44  *   03/16/01  Daris A Nevil      modified smc9194.c for use with LAN91C111
45  *   08/22/01  Scott Anderson     merge changes from smc9194 to smc91111
46  *   08/21/01  Pramod B Bhardwaj  added support for RevB of LAN91C111
47  *   12/20/01  Jeff Sutherland    initial port to Xscale PXA with DMA support
48  *   04/07/03  Nicolas Pitre      unified SMC91x driver, killed irq races,
49  *                                more bus abstraction, big cleanup, etc.
50  *   29/09/03  Russell King       - add driver model support
51  *                                - ethtool support
52  *                                - convert to use generic MII interface
53  *                                - add link up/down notification
54  *                                - don't try to handle full negotiation in
55  *                                  smc_phy_configure
56  *                                - clean up (and fix stack overrun) in PHY
57  *                                  MII read/write functions
58  *   22/09/04  Nicolas Pitre      big update (see commit log for details)
59  */
60 static const char version[] =
61         "smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre <nico@fluxnic.net>";
62
63 /* Debugging level */
64 #ifndef SMC_DEBUG
65 #define SMC_DEBUG               0
66 #endif
67
68
69 #include <linux/init.h>
70 #include <linux/module.h>
71 #include <linux/kernel.h>
72 #include <linux/sched.h>
73 #include <linux/delay.h>
74 #include <linux/interrupt.h>
75 #include <linux/irq.h>
76 #include <linux/errno.h>
77 #include <linux/ioport.h>
78 #include <linux/crc32.h>
79 #include <linux/platform_device.h>
80 #include <linux/spinlock.h>
81 #include <linux/ethtool.h>
82 #include <linux/mii.h>
83 #include <linux/workqueue.h>
84 #include <linux/of.h>
85 #include <linux/of_device.h>
86
87 #include <linux/netdevice.h>
88 #include <linux/etherdevice.h>
89 #include <linux/skbuff.h>
90
91 #include <asm/io.h>
92
93 #include "smc91x.h"
94
95 #ifndef SMC_NOWAIT
96 # define SMC_NOWAIT             0
97 #endif
98 static int nowait = SMC_NOWAIT;
99 module_param(nowait, int, 0400);
100 MODULE_PARM_DESC(nowait, "set to 1 for no wait state");
101
102 /*
103  * Transmit timeout, default 5 seconds.
104  */
105 static int watchdog = 1000;
106 module_param(watchdog, int, 0400);
107 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
108
109 MODULE_LICENSE("GPL");
110 MODULE_ALIAS("platform:smc91x");
111
112 /*
113  * The internal workings of the driver.  If you are changing anything
114  * here with the SMC stuff, you should have the datasheet and know
115  * what you are doing.
116  */
117 #define CARDNAME "smc91x"
118
119 /*
120  * Use power-down feature of the chip
121  */
122 #define POWER_DOWN              1
123
124 /*
125  * Wait time for memory to be free.  This probably shouldn't be
126  * tuned that much, as waiting for this means nothing else happens
127  * in the system
128  */
129 #define MEMORY_WAIT_TIME        16
130
131 /*
132  * The maximum number of processing loops allowed for each call to the
133  * IRQ handler.
134  */
135 #define MAX_IRQ_LOOPS           8
136
137 /*
138  * This selects whether TX packets are sent one by one to the SMC91x internal
139  * memory and throttled until transmission completes.  This may prevent
140  * RX overruns a litle by keeping much of the memory free for RX packets
141  * but to the expense of reduced TX throughput and increased IRQ overhead.
142  * Note this is not a cure for a too slow data bus or too high IRQ latency.
143  */
144 #define THROTTLE_TX_PKTS        0
145
146 /*
147  * The MII clock high/low times.  2x this number gives the MII clock period
148  * in microseconds. (was 50, but this gives 6.4ms for each MII transaction!)
149  */
150 #define MII_DELAY               1
151
152 #if SMC_DEBUG > 0
153 #define DBG(n, dev, args...)                            \
154         do {                                            \
155                 if (SMC_DEBUG >= (n))                   \
156                         netdev_dbg(dev, args);          \
157         } while (0)
158
159 #define PRINTK(dev, args...)   netdev_info(dev, args)
160 #else
161 #define DBG(n, dev, args...)   do { } while (0)
162 #define PRINTK(dev, args...)   netdev_dbg(dev, args)
163 #endif
164
165 #if SMC_DEBUG > 3
166 static void PRINT_PKT(u_char *buf, int length)
167 {
168         int i;
169         int remainder;
170         int lines;
171
172         lines = length / 16;
173         remainder = length % 16;
174
175         for (i = 0; i < lines ; i ++) {
176                 int cur;
177                 printk(KERN_DEBUG);
178                 for (cur = 0; cur < 8; cur++) {
179                         u_char a, b;
180                         a = *buf++;
181                         b = *buf++;
182                         pr_cont("%02x%02x ", a, b);
183                 }
184                 pr_cont("\n");
185         }
186         printk(KERN_DEBUG);
187         for (i = 0; i < remainder/2 ; i++) {
188                 u_char a, b;
189                 a = *buf++;
190                 b = *buf++;
191                 pr_cont("%02x%02x ", a, b);
192         }
193         pr_cont("\n");
194 }
195 #else
196 #define PRINT_PKT(x...)  do { } while (0)
197 #endif
198
199
200 /* this enables an interrupt in the interrupt mask register */
201 #define SMC_ENABLE_INT(lp, x) do {                                      \
202         unsigned char mask;                                             \
203         unsigned long smc_enable_flags;                                 \
204         spin_lock_irqsave(&lp->lock, smc_enable_flags);                 \
205         mask = SMC_GET_INT_MASK(lp);                                    \
206         mask |= (x);                                                    \
207         SMC_SET_INT_MASK(lp, mask);                                     \
208         spin_unlock_irqrestore(&lp->lock, smc_enable_flags);            \
209 } while (0)
210
211 /* this disables an interrupt from the interrupt mask register */
212 #define SMC_DISABLE_INT(lp, x) do {                                     \
213         unsigned char mask;                                             \
214         unsigned long smc_disable_flags;                                \
215         spin_lock_irqsave(&lp->lock, smc_disable_flags);                \
216         mask = SMC_GET_INT_MASK(lp);                                    \
217         mask &= ~(x);                                                   \
218         SMC_SET_INT_MASK(lp, mask);                                     \
219         spin_unlock_irqrestore(&lp->lock, smc_disable_flags);           \
220 } while (0)
221
222 /*
223  * Wait while MMU is busy.  This is usually in the order of a few nanosecs
224  * if at all, but let's avoid deadlocking the system if the hardware
225  * decides to go south.
226  */
227 #define SMC_WAIT_MMU_BUSY(lp) do {                                      \
228         if (unlikely(SMC_GET_MMU_CMD(lp) & MC_BUSY)) {          \
229                 unsigned long timeout = jiffies + 2;                    \
230                 while (SMC_GET_MMU_CMD(lp) & MC_BUSY) {         \
231                         if (time_after(jiffies, timeout)) {             \
232                                 netdev_dbg(dev, "timeout %s line %d\n", \
233                                            __FILE__, __LINE__);         \
234                                 break;                                  \
235                         }                                               \
236                         cpu_relax();                                    \
237                 }                                                       \
238         }                                                               \
239 } while (0)
240
241
242 /*
243  * this does a soft reset on the device
244  */
245 static void smc_reset(struct net_device *dev)
246 {
247         struct smc_local *lp = netdev_priv(dev);
248         void __iomem *ioaddr = lp->base;
249         unsigned int ctl, cfg;
250         struct sk_buff *pending_skb;
251
252         DBG(2, dev, "%s\n", __func__);
253
254         /* Disable all interrupts, block TX tasklet */
255         spin_lock_irq(&lp->lock);
256         SMC_SELECT_BANK(lp, 2);
257         SMC_SET_INT_MASK(lp, 0);
258         pending_skb = lp->pending_tx_skb;
259         lp->pending_tx_skb = NULL;
260         spin_unlock_irq(&lp->lock);
261
262         /* free any pending tx skb */
263         if (pending_skb) {
264                 dev_kfree_skb(pending_skb);
265                 dev->stats.tx_errors++;
266                 dev->stats.tx_aborted_errors++;
267         }
268
269         /*
270          * This resets the registers mostly to defaults, but doesn't
271          * affect EEPROM.  That seems unnecessary
272          */
273         SMC_SELECT_BANK(lp, 0);
274         SMC_SET_RCR(lp, RCR_SOFTRST);
275
276         /*
277          * Setup the Configuration Register
278          * This is necessary because the CONFIG_REG is not affected
279          * by a soft reset
280          */
281         SMC_SELECT_BANK(lp, 1);
282
283         cfg = CONFIG_DEFAULT;
284
285         /*
286          * Setup for fast accesses if requested.  If the card/system
287          * can't handle it then there will be no recovery except for
288          * a hard reset or power cycle
289          */
290         if (lp->cfg.flags & SMC91X_NOWAIT)
291                 cfg |= CONFIG_NO_WAIT;
292
293         /*
294          * Release from possible power-down state
295          * Configuration register is not affected by Soft Reset
296          */
297         cfg |= CONFIG_EPH_POWER_EN;
298
299         SMC_SET_CONFIG(lp, cfg);
300
301         /* this should pause enough for the chip to be happy */
302         /*
303          * elaborate?  What does the chip _need_? --jgarzik
304          *
305          * This seems to be undocumented, but something the original
306          * driver(s) have always done.  Suspect undocumented timing
307          * info/determined empirically. --rmk
308          */
309         udelay(1);
310
311         /* Disable transmit and receive functionality */
312         SMC_SELECT_BANK(lp, 0);
313         SMC_SET_RCR(lp, RCR_CLEAR);
314         SMC_SET_TCR(lp, TCR_CLEAR);
315
316         SMC_SELECT_BANK(lp, 1);
317         ctl = SMC_GET_CTL(lp) | CTL_LE_ENABLE;
318
319         /*
320          * Set the control register to automatically release successfully
321          * transmitted packets, to make the best use out of our limited
322          * memory
323          */
324         if(!THROTTLE_TX_PKTS)
325                 ctl |= CTL_AUTO_RELEASE;
326         else
327                 ctl &= ~CTL_AUTO_RELEASE;
328         SMC_SET_CTL(lp, ctl);
329
330         /* Reset the MMU */
331         SMC_SELECT_BANK(lp, 2);
332         SMC_SET_MMU_CMD(lp, MC_RESET);
333         SMC_WAIT_MMU_BUSY(lp);
334 }
335
336 /*
337  * Enable Interrupts, Receive, and Transmit
338  */
339 static void smc_enable(struct net_device *dev)
340 {
341         struct smc_local *lp = netdev_priv(dev);
342         void __iomem *ioaddr = lp->base;
343         int mask;
344
345         DBG(2, dev, "%s\n", __func__);
346
347         /* see the header file for options in TCR/RCR DEFAULT */
348         SMC_SELECT_BANK(lp, 0);
349         SMC_SET_TCR(lp, lp->tcr_cur_mode);
350         SMC_SET_RCR(lp, lp->rcr_cur_mode);
351
352         SMC_SELECT_BANK(lp, 1);
353         SMC_SET_MAC_ADDR(lp, dev->dev_addr);
354
355         /* now, enable interrupts */
356         mask = IM_EPH_INT|IM_RX_OVRN_INT|IM_RCV_INT;
357         if (lp->version >= (CHIP_91100 << 4))
358                 mask |= IM_MDINT;
359         SMC_SELECT_BANK(lp, 2);
360         SMC_SET_INT_MASK(lp, mask);
361
362         /*
363          * From this point the register bank must _NOT_ be switched away
364          * to something else than bank 2 without proper locking against
365          * races with any tasklet or interrupt handlers until smc_shutdown()
366          * or smc_reset() is called.
367          */
368 }
369
370 /*
371  * this puts the device in an inactive state
372  */
373 static void smc_shutdown(struct net_device *dev)
374 {
375         struct smc_local *lp = netdev_priv(dev);
376         void __iomem *ioaddr = lp->base;
377         struct sk_buff *pending_skb;
378
379         DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
380
381         /* no more interrupts for me */
382         spin_lock_irq(&lp->lock);
383         SMC_SELECT_BANK(lp, 2);
384         SMC_SET_INT_MASK(lp, 0);
385         pending_skb = lp->pending_tx_skb;
386         lp->pending_tx_skb = NULL;
387         spin_unlock_irq(&lp->lock);
388         if (pending_skb)
389                 dev_kfree_skb(pending_skb);
390
391         /* and tell the card to stay away from that nasty outside world */
392         SMC_SELECT_BANK(lp, 0);
393         SMC_SET_RCR(lp, RCR_CLEAR);
394         SMC_SET_TCR(lp, TCR_CLEAR);
395
396 #ifdef POWER_DOWN
397         /* finally, shut the chip down */
398         SMC_SELECT_BANK(lp, 1);
399         SMC_SET_CONFIG(lp, SMC_GET_CONFIG(lp) & ~CONFIG_EPH_POWER_EN);
400 #endif
401 }
402
403 /*
404  * This is the procedure to handle the receipt of a packet.
405  */
406 static inline void  smc_rcv(struct net_device *dev)
407 {
408         struct smc_local *lp = netdev_priv(dev);
409         void __iomem *ioaddr = lp->base;
410         unsigned int packet_number, status, packet_len;
411
412         DBG(3, dev, "%s\n", __func__);
413
414         packet_number = SMC_GET_RXFIFO(lp);
415         if (unlikely(packet_number & RXFIFO_REMPTY)) {
416                 PRINTK(dev, "smc_rcv with nothing on FIFO.\n");
417                 return;
418         }
419
420         /* read from start of packet */
421         SMC_SET_PTR(lp, PTR_READ | PTR_RCV | PTR_AUTOINC);
422
423         /* First two words are status and packet length */
424         SMC_GET_PKT_HDR(lp, status, packet_len);
425         packet_len &= 0x07ff;  /* mask off top bits */
426         DBG(2, dev, "RX PNR 0x%x STATUS 0x%04x LENGTH 0x%04x (%d)\n",
427             packet_number, status, packet_len, packet_len);
428
429         back:
430         if (unlikely(packet_len < 6 || status & RS_ERRORS)) {
431                 if (status & RS_TOOLONG && packet_len <= (1514 + 4 + 6)) {
432                         /* accept VLAN packets */
433                         status &= ~RS_TOOLONG;
434                         goto back;
435                 }
436                 if (packet_len < 6) {
437                         /* bloody hardware */
438                         netdev_err(dev, "fubar (rxlen %u status %x\n",
439                                    packet_len, status);
440                         status |= RS_TOOSHORT;
441                 }
442                 SMC_WAIT_MMU_BUSY(lp);
443                 SMC_SET_MMU_CMD(lp, MC_RELEASE);
444                 dev->stats.rx_errors++;
445                 if (status & RS_ALGNERR)
446                         dev->stats.rx_frame_errors++;
447                 if (status & (RS_TOOSHORT | RS_TOOLONG))
448                         dev->stats.rx_length_errors++;
449                 if (status & RS_BADCRC)
450                         dev->stats.rx_crc_errors++;
451         } else {
452                 struct sk_buff *skb;
453                 unsigned char *data;
454                 unsigned int data_len;
455
456                 /* set multicast stats */
457                 if (status & RS_MULTICAST)
458                         dev->stats.multicast++;
459
460                 /*
461                  * Actual payload is packet_len - 6 (or 5 if odd byte).
462                  * We want skb_reserve(2) and the final ctrl word
463                  * (2 bytes, possibly containing the payload odd byte).
464                  * Furthermore, we add 2 bytes to allow rounding up to
465                  * multiple of 4 bytes on 32 bit buses.
466                  * Hence packet_len - 6 + 2 + 2 + 2.
467                  */
468                 skb = netdev_alloc_skb(dev, packet_len);
469                 if (unlikely(skb == NULL)) {
470                         SMC_WAIT_MMU_BUSY(lp);
471                         SMC_SET_MMU_CMD(lp, MC_RELEASE);
472                         dev->stats.rx_dropped++;
473                         return;
474                 }
475
476                 /* Align IP header to 32 bits */
477                 skb_reserve(skb, 2);
478
479                 /* BUG: the LAN91C111 rev A never sets this bit. Force it. */
480                 if (lp->version == 0x90)
481                         status |= RS_ODDFRAME;
482
483                 /*
484                  * If odd length: packet_len - 5,
485                  * otherwise packet_len - 6.
486                  * With the trailing ctrl byte it's packet_len - 4.
487                  */
488                 data_len = packet_len - ((status & RS_ODDFRAME) ? 5 : 6);
489                 data = skb_put(skb, data_len);
490                 SMC_PULL_DATA(lp, data, packet_len - 4);
491
492                 SMC_WAIT_MMU_BUSY(lp);
493                 SMC_SET_MMU_CMD(lp, MC_RELEASE);
494
495                 PRINT_PKT(data, packet_len - 4);
496
497                 skb->protocol = eth_type_trans(skb, dev);
498                 netif_rx(skb);
499                 dev->stats.rx_packets++;
500                 dev->stats.rx_bytes += data_len;
501         }
502 }
503
504 #ifdef CONFIG_SMP
505 /*
506  * On SMP we have the following problem:
507  *
508  *      A = smc_hardware_send_pkt()
509  *      B = smc_hard_start_xmit()
510  *      C = smc_interrupt()
511  *
512  * A and B can never be executed simultaneously.  However, at least on UP,
513  * it is possible (and even desirable) for C to interrupt execution of
514  * A or B in order to have better RX reliability and avoid overruns.
515  * C, just like A and B, must have exclusive access to the chip and
516  * each of them must lock against any other concurrent access.
517  * Unfortunately this is not possible to have C suspend execution of A or
518  * B taking place on another CPU. On UP this is no an issue since A and B
519  * are run from softirq context and C from hard IRQ context, and there is
520  * no other CPU where concurrent access can happen.
521  * If ever there is a way to force at least B and C to always be executed
522  * on the same CPU then we could use read/write locks to protect against
523  * any other concurrent access and C would always interrupt B. But life
524  * isn't that easy in a SMP world...
525  */
526 #define smc_special_trylock(lock, flags)                                \
527 ({                                                                      \
528         int __ret;                                                      \
529         local_irq_save(flags);                                          \
530         __ret = spin_trylock(lock);                                     \
531         if (!__ret)                                                     \
532                 local_irq_restore(flags);                               \
533         __ret;                                                          \
534 })
535 #define smc_special_lock(lock, flags)           spin_lock_irqsave(lock, flags)
536 #define smc_special_unlock(lock, flags)         spin_unlock_irqrestore(lock, flags)
537 #else
538 #define smc_special_trylock(lock, flags)        (flags == flags)
539 #define smc_special_lock(lock, flags)           do { flags = 0; } while (0)
540 #define smc_special_unlock(lock, flags) do { flags = 0; } while (0)
541 #endif
542
543 /*
544  * This is called to actually send a packet to the chip.
545  */
546 static void smc_hardware_send_pkt(unsigned long data)
547 {
548         struct net_device *dev = (struct net_device *)data;
549         struct smc_local *lp = netdev_priv(dev);
550         void __iomem *ioaddr = lp->base;
551         struct sk_buff *skb;
552         unsigned int packet_no, len;
553         unsigned char *buf;
554         unsigned long flags;
555
556         DBG(3, dev, "%s\n", __func__);
557
558         if (!smc_special_trylock(&lp->lock, flags)) {
559                 netif_stop_queue(dev);
560                 tasklet_schedule(&lp->tx_task);
561                 return;
562         }
563
564         skb = lp->pending_tx_skb;
565         if (unlikely(!skb)) {
566                 smc_special_unlock(&lp->lock, flags);
567                 return;
568         }
569         lp->pending_tx_skb = NULL;
570
571         packet_no = SMC_GET_AR(lp);
572         if (unlikely(packet_no & AR_FAILED)) {
573                 netdev_err(dev, "Memory allocation failed.\n");
574                 dev->stats.tx_errors++;
575                 dev->stats.tx_fifo_errors++;
576                 smc_special_unlock(&lp->lock, flags);
577                 goto done;
578         }
579
580         /* point to the beginning of the packet */
581         SMC_SET_PN(lp, packet_no);
582         SMC_SET_PTR(lp, PTR_AUTOINC);
583
584         buf = skb->data;
585         len = skb->len;
586         DBG(2, dev, "TX PNR 0x%x LENGTH 0x%04x (%d) BUF 0x%p\n",
587             packet_no, len, len, buf);
588         PRINT_PKT(buf, len);
589
590         /*
591          * Send the packet length (+6 for status words, length, and ctl.
592          * The card will pad to 64 bytes with zeroes if packet is too small.
593          */
594         SMC_PUT_PKT_HDR(lp, 0, len + 6);
595
596         /* send the actual data */
597         SMC_PUSH_DATA(lp, buf, len & ~1);
598
599         /* Send final ctl word with the last byte if there is one */
600         SMC_outw(((len & 1) ? (0x2000 | buf[len-1]) : 0), ioaddr, DATA_REG(lp));
601
602         /*
603          * If THROTTLE_TX_PKTS is set, we stop the queue here. This will
604          * have the effect of having at most one packet queued for TX
605          * in the chip's memory at all time.
606          *
607          * If THROTTLE_TX_PKTS is not set then the queue is stopped only
608          * when memory allocation (MC_ALLOC) does not succeed right away.
609          */
610         if (THROTTLE_TX_PKTS)
611                 netif_stop_queue(dev);
612
613         /* queue the packet for TX */
614         SMC_SET_MMU_CMD(lp, MC_ENQUEUE);
615         smc_special_unlock(&lp->lock, flags);
616
617         dev->trans_start = jiffies;
618         dev->stats.tx_packets++;
619         dev->stats.tx_bytes += len;
620
621         SMC_ENABLE_INT(lp, IM_TX_INT | IM_TX_EMPTY_INT);
622
623 done:   if (!THROTTLE_TX_PKTS)
624                 netif_wake_queue(dev);
625
626         dev_kfree_skb(skb);
627 }
628
629 /*
630  * Since I am not sure if I will have enough room in the chip's ram
631  * to store the packet, I call this routine which either sends it
632  * now, or set the card to generates an interrupt when ready
633  * for the packet.
634  */
635 static int smc_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
636 {
637         struct smc_local *lp = netdev_priv(dev);
638         void __iomem *ioaddr = lp->base;
639         unsigned int numPages, poll_count, status;
640         unsigned long flags;
641
642         DBG(3, dev, "%s\n", __func__);
643
644         BUG_ON(lp->pending_tx_skb != NULL);
645
646         /*
647          * The MMU wants the number of pages to be the number of 256 bytes
648          * 'pages', minus 1 (since a packet can't ever have 0 pages :))
649          *
650          * The 91C111 ignores the size bits, but earlier models don't.
651          *
652          * Pkt size for allocating is data length +6 (for additional status
653          * words, length and ctl)
654          *
655          * If odd size then last byte is included in ctl word.
656          */
657         numPages = ((skb->len & ~1) + (6 - 1)) >> 8;
658         if (unlikely(numPages > 7)) {
659                 netdev_warn(dev, "Far too big packet error.\n");
660                 dev->stats.tx_errors++;
661                 dev->stats.tx_dropped++;
662                 dev_kfree_skb(skb);
663                 return NETDEV_TX_OK;
664         }
665
666         smc_special_lock(&lp->lock, flags);
667
668         /* now, try to allocate the memory */
669         SMC_SET_MMU_CMD(lp, MC_ALLOC | numPages);
670
671         /*
672          * Poll the chip for a short amount of time in case the
673          * allocation succeeds quickly.
674          */
675         poll_count = MEMORY_WAIT_TIME;
676         do {
677                 status = SMC_GET_INT(lp);
678                 if (status & IM_ALLOC_INT) {
679                         SMC_ACK_INT(lp, IM_ALLOC_INT);
680                         break;
681                 }
682         } while (--poll_count);
683
684         smc_special_unlock(&lp->lock, flags);
685
686         lp->pending_tx_skb = skb;
687         if (!poll_count) {
688                 /* oh well, wait until the chip finds memory later */
689                 netif_stop_queue(dev);
690                 DBG(2, dev, "TX memory allocation deferred.\n");
691                 SMC_ENABLE_INT(lp, IM_ALLOC_INT);
692         } else {
693                 /*
694                  * Allocation succeeded: push packet to the chip's own memory
695                  * immediately.
696                  */
697                 smc_hardware_send_pkt((unsigned long)dev);
698         }
699
700         return NETDEV_TX_OK;
701 }
702
703 /*
704  * This handles a TX interrupt, which is only called when:
705  * - a TX error occurred, or
706  * - CTL_AUTO_RELEASE is not set and TX of a packet completed.
707  */
708 static void smc_tx(struct net_device *dev)
709 {
710         struct smc_local *lp = netdev_priv(dev);
711         void __iomem *ioaddr = lp->base;
712         unsigned int saved_packet, packet_no, tx_status, pkt_len;
713
714         DBG(3, dev, "%s\n", __func__);
715
716         /* If the TX FIFO is empty then nothing to do */
717         packet_no = SMC_GET_TXFIFO(lp);
718         if (unlikely(packet_no & TXFIFO_TEMPTY)) {
719                 PRINTK(dev, "smc_tx with nothing on FIFO.\n");
720                 return;
721         }
722
723         /* select packet to read from */
724         saved_packet = SMC_GET_PN(lp);
725         SMC_SET_PN(lp, packet_no);
726
727         /* read the first word (status word) from this packet */
728         SMC_SET_PTR(lp, PTR_AUTOINC | PTR_READ);
729         SMC_GET_PKT_HDR(lp, tx_status, pkt_len);
730         DBG(2, dev, "TX STATUS 0x%04x PNR 0x%02x\n",
731             tx_status, packet_no);
732
733         if (!(tx_status & ES_TX_SUC))
734                 dev->stats.tx_errors++;
735
736         if (tx_status & ES_LOSTCARR)
737                 dev->stats.tx_carrier_errors++;
738
739         if (tx_status & (ES_LATCOL | ES_16COL)) {
740                 PRINTK(dev, "%s occurred on last xmit\n",
741                        (tx_status & ES_LATCOL) ?
742                         "late collision" : "too many collisions");
743                 dev->stats.tx_window_errors++;
744                 if (!(dev->stats.tx_window_errors & 63) && net_ratelimit()) {
745                         netdev_info(dev, "unexpectedly large number of bad collisions. Please check duplex setting.\n");
746                 }
747         }
748
749         /* kill the packet */
750         SMC_WAIT_MMU_BUSY(lp);
751         SMC_SET_MMU_CMD(lp, MC_FREEPKT);
752
753         /* Don't restore Packet Number Reg until busy bit is cleared */
754         SMC_WAIT_MMU_BUSY(lp);
755         SMC_SET_PN(lp, saved_packet);
756
757         /* re-enable transmit */
758         SMC_SELECT_BANK(lp, 0);
759         SMC_SET_TCR(lp, lp->tcr_cur_mode);
760         SMC_SELECT_BANK(lp, 2);
761 }
762
763
764 /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
765
766 static void smc_mii_out(struct net_device *dev, unsigned int val, int bits)
767 {
768         struct smc_local *lp = netdev_priv(dev);
769         void __iomem *ioaddr = lp->base;
770         unsigned int mii_reg, mask;
771
772         mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
773         mii_reg |= MII_MDOE;
774
775         for (mask = 1 << (bits - 1); mask; mask >>= 1) {
776                 if (val & mask)
777                         mii_reg |= MII_MDO;
778                 else
779                         mii_reg &= ~MII_MDO;
780
781                 SMC_SET_MII(lp, mii_reg);
782                 udelay(MII_DELAY);
783                 SMC_SET_MII(lp, mii_reg | MII_MCLK);
784                 udelay(MII_DELAY);
785         }
786 }
787
788 static unsigned int smc_mii_in(struct net_device *dev, int bits)
789 {
790         struct smc_local *lp = netdev_priv(dev);
791         void __iomem *ioaddr = lp->base;
792         unsigned int mii_reg, mask, val;
793
794         mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
795         SMC_SET_MII(lp, mii_reg);
796
797         for (mask = 1 << (bits - 1), val = 0; mask; mask >>= 1) {
798                 if (SMC_GET_MII(lp) & MII_MDI)
799                         val |= mask;
800
801                 SMC_SET_MII(lp, mii_reg);
802                 udelay(MII_DELAY);
803                 SMC_SET_MII(lp, mii_reg | MII_MCLK);
804                 udelay(MII_DELAY);
805         }
806
807         return val;
808 }
809
810 /*
811  * Reads a register from the MII Management serial interface
812  */
813 static int smc_phy_read(struct net_device *dev, int phyaddr, int phyreg)
814 {
815         struct smc_local *lp = netdev_priv(dev);
816         void __iomem *ioaddr = lp->base;
817         unsigned int phydata;
818
819         SMC_SELECT_BANK(lp, 3);
820
821         /* Idle - 32 ones */
822         smc_mii_out(dev, 0xffffffff, 32);
823
824         /* Start code (01) + read (10) + phyaddr + phyreg */
825         smc_mii_out(dev, 6 << 10 | phyaddr << 5 | phyreg, 14);
826
827         /* Turnaround (2bits) + phydata */
828         phydata = smc_mii_in(dev, 18);
829
830         /* Return to idle state */
831         SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
832
833         DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
834             __func__, phyaddr, phyreg, phydata);
835
836         SMC_SELECT_BANK(lp, 2);
837         return phydata;
838 }
839
840 /*
841  * Writes a register to the MII Management serial interface
842  */
843 static void smc_phy_write(struct net_device *dev, int phyaddr, int phyreg,
844                           int phydata)
845 {
846         struct smc_local *lp = netdev_priv(dev);
847         void __iomem *ioaddr = lp->base;
848
849         SMC_SELECT_BANK(lp, 3);
850
851         /* Idle - 32 ones */
852         smc_mii_out(dev, 0xffffffff, 32);
853
854         /* Start code (01) + write (01) + phyaddr + phyreg + turnaround + phydata */
855         smc_mii_out(dev, 5 << 28 | phyaddr << 23 | phyreg << 18 | 2 << 16 | phydata, 32);
856
857         /* Return to idle state */
858         SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
859
860         DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
861             __func__, phyaddr, phyreg, phydata);
862
863         SMC_SELECT_BANK(lp, 2);
864 }
865
866 /*
867  * Finds and reports the PHY address
868  */
869 static void smc_phy_detect(struct net_device *dev)
870 {
871         struct smc_local *lp = netdev_priv(dev);
872         int phyaddr;
873
874         DBG(2, dev, "%s\n", __func__);
875
876         lp->phy_type = 0;
877
878         /*
879          * Scan all 32 PHY addresses if necessary, starting at
880          * PHY#1 to PHY#31, and then PHY#0 last.
881          */
882         for (phyaddr = 1; phyaddr < 33; ++phyaddr) {
883                 unsigned int id1, id2;
884
885                 /* Read the PHY identifiers */
886                 id1 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID1);
887                 id2 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID2);
888
889                 DBG(3, dev, "phy_id1=0x%x, phy_id2=0x%x\n",
890                     id1, id2);
891
892                 /* Make sure it is a valid identifier */
893                 if (id1 != 0x0000 && id1 != 0xffff && id1 != 0x8000 &&
894                     id2 != 0x0000 && id2 != 0xffff && id2 != 0x8000) {
895                         /* Save the PHY's address */
896                         lp->mii.phy_id = phyaddr & 31;
897                         lp->phy_type = id1 << 16 | id2;
898                         break;
899                 }
900         }
901 }
902
903 /*
904  * Sets the PHY to a configuration as determined by the user
905  */
906 static int smc_phy_fixed(struct net_device *dev)
907 {
908         struct smc_local *lp = netdev_priv(dev);
909         void __iomem *ioaddr = lp->base;
910         int phyaddr = lp->mii.phy_id;
911         int bmcr, cfg1;
912
913         DBG(3, dev, "%s\n", __func__);
914
915         /* Enter Link Disable state */
916         cfg1 = smc_phy_read(dev, phyaddr, PHY_CFG1_REG);
917         cfg1 |= PHY_CFG1_LNKDIS;
918         smc_phy_write(dev, phyaddr, PHY_CFG1_REG, cfg1);
919
920         /*
921          * Set our fixed capabilities
922          * Disable auto-negotiation
923          */
924         bmcr = 0;
925
926         if (lp->ctl_rfduplx)
927                 bmcr |= BMCR_FULLDPLX;
928
929         if (lp->ctl_rspeed == 100)
930                 bmcr |= BMCR_SPEED100;
931
932         /* Write our capabilities to the phy control register */
933         smc_phy_write(dev, phyaddr, MII_BMCR, bmcr);
934
935         /* Re-Configure the Receive/Phy Control register */
936         SMC_SELECT_BANK(lp, 0);
937         SMC_SET_RPC(lp, lp->rpc_cur_mode);
938         SMC_SELECT_BANK(lp, 2);
939
940         return 1;
941 }
942
943 /**
944  * smc_phy_reset - reset the phy
945  * @dev: net device
946  * @phy: phy address
947  *
948  * Issue a software reset for the specified PHY and
949  * wait up to 100ms for the reset to complete.  We should
950  * not access the PHY for 50ms after issuing the reset.
951  *
952  * The time to wait appears to be dependent on the PHY.
953  *
954  * Must be called with lp->lock locked.
955  */
956 static int smc_phy_reset(struct net_device *dev, int phy)
957 {
958         struct smc_local *lp = netdev_priv(dev);
959         unsigned int bmcr;
960         int timeout;
961
962         smc_phy_write(dev, phy, MII_BMCR, BMCR_RESET);
963
964         for (timeout = 2; timeout; timeout--) {
965                 spin_unlock_irq(&lp->lock);
966                 msleep(50);
967                 spin_lock_irq(&lp->lock);
968
969                 bmcr = smc_phy_read(dev, phy, MII_BMCR);
970                 if (!(bmcr & BMCR_RESET))
971                         break;
972         }
973
974         return bmcr & BMCR_RESET;
975 }
976
977 /**
978  * smc_phy_powerdown - powerdown phy
979  * @dev: net device
980  *
981  * Power down the specified PHY
982  */
983 static void smc_phy_powerdown(struct net_device *dev)
984 {
985         struct smc_local *lp = netdev_priv(dev);
986         unsigned int bmcr;
987         int phy = lp->mii.phy_id;
988
989         if (lp->phy_type == 0)
990                 return;
991
992         /* We need to ensure that no calls to smc_phy_configure are
993            pending.
994         */
995         cancel_work_sync(&lp->phy_configure);
996
997         bmcr = smc_phy_read(dev, phy, MII_BMCR);
998         smc_phy_write(dev, phy, MII_BMCR, bmcr | BMCR_PDOWN);
999 }
1000
1001 /**
1002  * smc_phy_check_media - check the media status and adjust TCR
1003  * @dev: net device
1004  * @init: set true for initialisation
1005  *
1006  * Select duplex mode depending on negotiation state.  This
1007  * also updates our carrier state.
1008  */
1009 static void smc_phy_check_media(struct net_device *dev, int init)
1010 {
1011         struct smc_local *lp = netdev_priv(dev);
1012         void __iomem *ioaddr = lp->base;
1013
1014         if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {
1015                 /* duplex state has changed */
1016                 if (lp->mii.full_duplex) {
1017                         lp->tcr_cur_mode |= TCR_SWFDUP;
1018                 } else {
1019                         lp->tcr_cur_mode &= ~TCR_SWFDUP;
1020                 }
1021
1022                 SMC_SELECT_BANK(lp, 0);
1023                 SMC_SET_TCR(lp, lp->tcr_cur_mode);
1024         }
1025 }
1026
1027 /*
1028  * Configures the specified PHY through the MII management interface
1029  * using Autonegotiation.
1030  * Calls smc_phy_fixed() if the user has requested a certain config.
1031  * If RPC ANEG bit is set, the media selection is dependent purely on
1032  * the selection by the MII (either in the MII BMCR reg or the result
1033  * of autonegotiation.)  If the RPC ANEG bit is cleared, the selection
1034  * is controlled by the RPC SPEED and RPC DPLX bits.
1035  */
1036 static void smc_phy_configure(struct work_struct *work)
1037 {
1038         struct smc_local *lp =
1039                 container_of(work, struct smc_local, phy_configure);
1040         struct net_device *dev = lp->dev;
1041         void __iomem *ioaddr = lp->base;
1042         int phyaddr = lp->mii.phy_id;
1043         int my_phy_caps; /* My PHY capabilities */
1044         int my_ad_caps; /* My Advertised capabilities */
1045         int status;
1046
1047         DBG(3, dev, "smc_program_phy()\n");
1048
1049         spin_lock_irq(&lp->lock);
1050
1051         /*
1052          * We should not be called if phy_type is zero.
1053          */
1054         if (lp->phy_type == 0)
1055                 goto smc_phy_configure_exit;
1056
1057         if (smc_phy_reset(dev, phyaddr)) {
1058                 netdev_info(dev, "PHY reset timed out\n");
1059                 goto smc_phy_configure_exit;
1060         }
1061
1062         /*
1063          * Enable PHY Interrupts (for register 18)
1064          * Interrupts listed here are disabled
1065          */
1066         smc_phy_write(dev, phyaddr, PHY_MASK_REG,
1067                 PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |
1068                 PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |
1069                 PHY_INT_SPDDET | PHY_INT_DPLXDET);
1070
1071         /* Configure the Receive/Phy Control register */
1072         SMC_SELECT_BANK(lp, 0);
1073         SMC_SET_RPC(lp, lp->rpc_cur_mode);
1074
1075         /* If the user requested no auto neg, then go set his request */
1076         if (lp->mii.force_media) {
1077                 smc_phy_fixed(dev);
1078                 goto smc_phy_configure_exit;
1079         }
1080
1081         /* Copy our capabilities from MII_BMSR to MII_ADVERTISE */
1082         my_phy_caps = smc_phy_read(dev, phyaddr, MII_BMSR);
1083
1084         if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {
1085                 netdev_info(dev, "Auto negotiation NOT supported\n");
1086                 smc_phy_fixed(dev);
1087                 goto smc_phy_configure_exit;
1088         }
1089
1090         my_ad_caps = ADVERTISE_CSMA; /* I am CSMA capable */
1091
1092         if (my_phy_caps & BMSR_100BASE4)
1093                 my_ad_caps |= ADVERTISE_100BASE4;
1094         if (my_phy_caps & BMSR_100FULL)
1095                 my_ad_caps |= ADVERTISE_100FULL;
1096         if (my_phy_caps & BMSR_100HALF)
1097                 my_ad_caps |= ADVERTISE_100HALF;
1098         if (my_phy_caps & BMSR_10FULL)
1099                 my_ad_caps |= ADVERTISE_10FULL;
1100         if (my_phy_caps & BMSR_10HALF)
1101                 my_ad_caps |= ADVERTISE_10HALF;
1102
1103         /* Disable capabilities not selected by our user */
1104         if (lp->ctl_rspeed != 100)
1105                 my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
1106
1107         if (!lp->ctl_rfduplx)
1108                 my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
1109
1110         /* Update our Auto-Neg Advertisement Register */
1111         smc_phy_write(dev, phyaddr, MII_ADVERTISE, my_ad_caps);
1112         lp->mii.advertising = my_ad_caps;
1113
1114         /*
1115          * Read the register back.  Without this, it appears that when
1116          * auto-negotiation is restarted, sometimes it isn't ready and
1117          * the link does not come up.
1118          */
1119         status = smc_phy_read(dev, phyaddr, MII_ADVERTISE);
1120
1121         DBG(2, dev, "phy caps=%x\n", my_phy_caps);
1122         DBG(2, dev, "phy advertised caps=%x\n", my_ad_caps);
1123
1124         /* Restart auto-negotiation process in order to advertise my caps */
1125         smc_phy_write(dev, phyaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
1126
1127         smc_phy_check_media(dev, 1);
1128
1129 smc_phy_configure_exit:
1130         SMC_SELECT_BANK(lp, 2);
1131         spin_unlock_irq(&lp->lock);
1132 }
1133
1134 /*
1135  * smc_phy_interrupt
1136  *
1137  * Purpose:  Handle interrupts relating to PHY register 18. This is
1138  *  called from the "hard" interrupt handler under our private spinlock.
1139  */
1140 static void smc_phy_interrupt(struct net_device *dev)
1141 {
1142         struct smc_local *lp = netdev_priv(dev);
1143         int phyaddr = lp->mii.phy_id;
1144         int phy18;
1145
1146         DBG(2, dev, "%s\n", __func__);
1147
1148         if (lp->phy_type == 0)
1149                 return;
1150
1151         for(;;) {
1152                 smc_phy_check_media(dev, 0);
1153
1154                 /* Read PHY Register 18, Status Output */
1155                 phy18 = smc_phy_read(dev, phyaddr, PHY_INT_REG);
1156                 if ((phy18 & PHY_INT_INT) == 0)
1157                         break;
1158         }
1159 }
1160
1161 /*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
1162
1163 static void smc_10bt_check_media(struct net_device *dev, int init)
1164 {
1165         struct smc_local *lp = netdev_priv(dev);
1166         void __iomem *ioaddr = lp->base;
1167         unsigned int old_carrier, new_carrier;
1168
1169         old_carrier = netif_carrier_ok(dev) ? 1 : 0;
1170
1171         SMC_SELECT_BANK(lp, 0);
1172         new_carrier = (SMC_GET_EPH_STATUS(lp) & ES_LINK_OK) ? 1 : 0;
1173         SMC_SELECT_BANK(lp, 2);
1174
1175         if (init || (old_carrier != new_carrier)) {
1176                 if (!new_carrier) {
1177                         netif_carrier_off(dev);
1178                 } else {
1179                         netif_carrier_on(dev);
1180                 }
1181                 if (netif_msg_link(lp))
1182                         netdev_info(dev, "link %s\n",
1183                                     new_carrier ? "up" : "down");
1184         }
1185 }
1186
1187 static void smc_eph_interrupt(struct net_device *dev)
1188 {
1189         struct smc_local *lp = netdev_priv(dev);
1190         void __iomem *ioaddr = lp->base;
1191         unsigned int ctl;
1192
1193         smc_10bt_check_media(dev, 0);
1194
1195         SMC_SELECT_BANK(lp, 1);
1196         ctl = SMC_GET_CTL(lp);
1197         SMC_SET_CTL(lp, ctl & ~CTL_LE_ENABLE);
1198         SMC_SET_CTL(lp, ctl);
1199         SMC_SELECT_BANK(lp, 2);
1200 }
1201
1202 /*
1203  * This is the main routine of the driver, to handle the device when
1204  * it needs some attention.
1205  */
1206 static irqreturn_t smc_interrupt(int irq, void *dev_id)
1207 {
1208         struct net_device *dev = dev_id;
1209         struct smc_local *lp = netdev_priv(dev);
1210         void __iomem *ioaddr = lp->base;
1211         int status, mask, timeout, card_stats;
1212         int saved_pointer;
1213
1214         DBG(3, dev, "%s\n", __func__);
1215
1216         spin_lock(&lp->lock);
1217
1218         /* A preamble may be used when there is a potential race
1219          * between the interruptible transmit functions and this
1220          * ISR. */
1221         SMC_INTERRUPT_PREAMBLE;
1222
1223         saved_pointer = SMC_GET_PTR(lp);
1224         mask = SMC_GET_INT_MASK(lp);
1225         SMC_SET_INT_MASK(lp, 0);
1226
1227         /* set a timeout value, so I don't stay here forever */
1228         timeout = MAX_IRQ_LOOPS;
1229
1230         do {
1231                 status = SMC_GET_INT(lp);
1232
1233                 DBG(2, dev, "INT 0x%02x MASK 0x%02x MEM 0x%04x FIFO 0x%04x\n",
1234                     status, mask,
1235                     ({ int meminfo; SMC_SELECT_BANK(lp, 0);
1236                        meminfo = SMC_GET_MIR(lp);
1237                        SMC_SELECT_BANK(lp, 2); meminfo; }),
1238                     SMC_GET_FIFO(lp));
1239
1240                 status &= mask;
1241                 if (!status)
1242                         break;
1243
1244                 if (status & IM_TX_INT) {
1245                         /* do this before RX as it will free memory quickly */
1246                         DBG(3, dev, "TX int\n");
1247                         smc_tx(dev);
1248                         SMC_ACK_INT(lp, IM_TX_INT);
1249                         if (THROTTLE_TX_PKTS)
1250                                 netif_wake_queue(dev);
1251                 } else if (status & IM_RCV_INT) {
1252                         DBG(3, dev, "RX irq\n");
1253                         smc_rcv(dev);
1254                 } else if (status & IM_ALLOC_INT) {
1255                         DBG(3, dev, "Allocation irq\n");
1256                         tasklet_hi_schedule(&lp->tx_task);
1257                         mask &= ~IM_ALLOC_INT;
1258                 } else if (status & IM_TX_EMPTY_INT) {
1259                         DBG(3, dev, "TX empty\n");
1260                         mask &= ~IM_TX_EMPTY_INT;
1261
1262                         /* update stats */
1263                         SMC_SELECT_BANK(lp, 0);
1264                         card_stats = SMC_GET_COUNTER(lp);
1265                         SMC_SELECT_BANK(lp, 2);
1266
1267                         /* single collisions */
1268                         dev->stats.collisions += card_stats & 0xF;
1269                         card_stats >>= 4;
1270
1271                         /* multiple collisions */
1272                         dev->stats.collisions += card_stats & 0xF;
1273                 } else if (status & IM_RX_OVRN_INT) {
1274                         DBG(1, dev, "RX overrun (EPH_ST 0x%04x)\n",
1275                             ({ int eph_st; SMC_SELECT_BANK(lp, 0);
1276                                eph_st = SMC_GET_EPH_STATUS(lp);
1277                                SMC_SELECT_BANK(lp, 2); eph_st; }));
1278                         SMC_ACK_INT(lp, IM_RX_OVRN_INT);
1279                         dev->stats.rx_errors++;
1280                         dev->stats.rx_fifo_errors++;
1281                 } else if (status & IM_EPH_INT) {
1282                         smc_eph_interrupt(dev);
1283                 } else if (status & IM_MDINT) {
1284                         SMC_ACK_INT(lp, IM_MDINT);
1285                         smc_phy_interrupt(dev);
1286                 } else if (status & IM_ERCV_INT) {
1287                         SMC_ACK_INT(lp, IM_ERCV_INT);
1288                         PRINTK(dev, "UNSUPPORTED: ERCV INTERRUPT\n");
1289                 }
1290         } while (--timeout);
1291
1292         /* restore register states */
1293         SMC_SET_PTR(lp, saved_pointer);
1294         SMC_SET_INT_MASK(lp, mask);
1295         spin_unlock(&lp->lock);
1296
1297 #ifndef CONFIG_NET_POLL_CONTROLLER
1298         if (timeout == MAX_IRQ_LOOPS)
1299                 PRINTK(dev, "spurious interrupt (mask = 0x%02x)\n",
1300                        mask);
1301 #endif
1302         DBG(3, dev, "Interrupt done (%d loops)\n",
1303             MAX_IRQ_LOOPS - timeout);
1304
1305         /*
1306          * We return IRQ_HANDLED unconditionally here even if there was
1307          * nothing to do.  There is a possibility that a packet might
1308          * get enqueued into the chip right after TX_EMPTY_INT is raised
1309          * but just before the CPU acknowledges the IRQ.
1310          * Better take an unneeded IRQ in some occasions than complexifying
1311          * the code for all cases.
1312          */
1313         return IRQ_HANDLED;
1314 }
1315
1316 #ifdef CONFIG_NET_POLL_CONTROLLER
1317 /*
1318  * Polling receive - used by netconsole and other diagnostic tools
1319  * to allow network i/o with interrupts disabled.
1320  */
1321 static void smc_poll_controller(struct net_device *dev)
1322 {
1323         disable_irq(dev->irq);
1324         smc_interrupt(dev->irq, dev);
1325         enable_irq(dev->irq);
1326 }
1327 #endif
1328
1329 /* Our watchdog timed out. Called by the networking layer */
1330 static void smc_timeout(struct net_device *dev)
1331 {
1332         struct smc_local *lp = netdev_priv(dev);
1333         void __iomem *ioaddr = lp->base;
1334         int status, mask, eph_st, meminfo, fifo;
1335
1336         DBG(2, dev, "%s\n", __func__);
1337
1338         spin_lock_irq(&lp->lock);
1339         status = SMC_GET_INT(lp);
1340         mask = SMC_GET_INT_MASK(lp);
1341         fifo = SMC_GET_FIFO(lp);
1342         SMC_SELECT_BANK(lp, 0);
1343         eph_st = SMC_GET_EPH_STATUS(lp);
1344         meminfo = SMC_GET_MIR(lp);
1345         SMC_SELECT_BANK(lp, 2);
1346         spin_unlock_irq(&lp->lock);
1347         PRINTK(dev, "TX timeout (INT 0x%02x INTMASK 0x%02x MEM 0x%04x FIFO 0x%04x EPH_ST 0x%04x)\n",
1348                status, mask, meminfo, fifo, eph_st);
1349
1350         smc_reset(dev);
1351         smc_enable(dev);
1352
1353         /*
1354          * Reconfiguring the PHY doesn't seem like a bad idea here, but
1355          * smc_phy_configure() calls msleep() which calls schedule_timeout()
1356          * which calls schedule().  Hence we use a work queue.
1357          */
1358         if (lp->phy_type != 0)
1359                 schedule_work(&lp->phy_configure);
1360
1361         /* We can accept TX packets again */
1362         dev->trans_start = jiffies; /* prevent tx timeout */
1363         netif_wake_queue(dev);
1364 }
1365
1366 /*
1367  * This routine will, depending on the values passed to it,
1368  * either make it accept multicast packets, go into
1369  * promiscuous mode (for TCPDUMP and cousins) or accept
1370  * a select set of multicast packets
1371  */
1372 static void smc_set_multicast_list(struct net_device *dev)
1373 {
1374         struct smc_local *lp = netdev_priv(dev);
1375         void __iomem *ioaddr = lp->base;
1376         unsigned char multicast_table[8];
1377         int update_multicast = 0;
1378
1379         DBG(2, dev, "%s\n", __func__);
1380
1381         if (dev->flags & IFF_PROMISC) {
1382                 DBG(2, dev, "RCR_PRMS\n");
1383                 lp->rcr_cur_mode |= RCR_PRMS;
1384         }
1385
1386 /* BUG?  I never disable promiscuous mode if multicasting was turned on.
1387    Now, I turn off promiscuous mode, but I don't do anything to multicasting
1388    when promiscuous mode is turned on.
1389 */
1390
1391         /*
1392          * Here, I am setting this to accept all multicast packets.
1393          * I don't need to zero the multicast table, because the flag is
1394          * checked before the table is
1395          */
1396         else if (dev->flags & IFF_ALLMULTI || netdev_mc_count(dev) > 16) {
1397                 DBG(2, dev, "RCR_ALMUL\n");
1398                 lp->rcr_cur_mode |= RCR_ALMUL;
1399         }
1400
1401         /*
1402          * This sets the internal hardware table to filter out unwanted
1403          * multicast packets before they take up memory.
1404          *
1405          * The SMC chip uses a hash table where the high 6 bits of the CRC of
1406          * address are the offset into the table.  If that bit is 1, then the
1407          * multicast packet is accepted.  Otherwise, it's dropped silently.
1408          *
1409          * To use the 6 bits as an offset into the table, the high 3 bits are
1410          * the number of the 8 bit register, while the low 3 bits are the bit
1411          * within that register.
1412          */
1413         else if (!netdev_mc_empty(dev)) {
1414                 struct netdev_hw_addr *ha;
1415
1416                 /* table for flipping the order of 3 bits */
1417                 static const unsigned char invert3[] = {0, 4, 2, 6, 1, 5, 3, 7};
1418
1419                 /* start with a table of all zeros: reject all */
1420                 memset(multicast_table, 0, sizeof(multicast_table));
1421
1422                 netdev_for_each_mc_addr(ha, dev) {
1423                         int position;
1424
1425                         /* only use the low order bits */
1426                         position = crc32_le(~0, ha->addr, 6) & 0x3f;
1427
1428                         /* do some messy swapping to put the bit in the right spot */
1429                         multicast_table[invert3[position&7]] |=
1430                                 (1<<invert3[(position>>3)&7]);
1431                 }
1432
1433                 /* be sure I get rid of flags I might have set */
1434                 lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1435
1436                 /* now, the table can be loaded into the chipset */
1437                 update_multicast = 1;
1438         } else  {
1439                 DBG(2, dev, "~(RCR_PRMS|RCR_ALMUL)\n");
1440                 lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1441
1442                 /*
1443                  * since I'm disabling all multicast entirely, I need to
1444                  * clear the multicast list
1445                  */
1446                 memset(multicast_table, 0, sizeof(multicast_table));
1447                 update_multicast = 1;
1448         }
1449
1450         spin_lock_irq(&lp->lock);
1451         SMC_SELECT_BANK(lp, 0);
1452         SMC_SET_RCR(lp, lp->rcr_cur_mode);
1453         if (update_multicast) {
1454                 SMC_SELECT_BANK(lp, 3);
1455                 SMC_SET_MCAST(lp, multicast_table);
1456         }
1457         SMC_SELECT_BANK(lp, 2);
1458         spin_unlock_irq(&lp->lock);
1459 }
1460
1461
1462 /*
1463  * Open and Initialize the board
1464  *
1465  * Set up everything, reset the card, etc..
1466  */
1467 static int
1468 smc_open(struct net_device *dev)
1469 {
1470         struct smc_local *lp = netdev_priv(dev);
1471
1472         DBG(2, dev, "%s\n", __func__);
1473
1474         /* Setup the default Register Modes */
1475         lp->tcr_cur_mode = TCR_DEFAULT;
1476         lp->rcr_cur_mode = RCR_DEFAULT;
1477         lp->rpc_cur_mode = RPC_DEFAULT |
1478                                 lp->cfg.leda << RPC_LSXA_SHFT |
1479                                 lp->cfg.ledb << RPC_LSXB_SHFT;
1480
1481         /*
1482          * If we are not using a MII interface, we need to
1483          * monitor our own carrier signal to detect faults.
1484          */
1485         if (lp->phy_type == 0)
1486                 lp->tcr_cur_mode |= TCR_MON_CSN;
1487
1488         /* reset the hardware */
1489         smc_reset(dev);
1490         smc_enable(dev);
1491
1492         /* Configure the PHY, initialize the link state */
1493         if (lp->phy_type != 0)
1494                 smc_phy_configure(&lp->phy_configure);
1495         else {
1496                 spin_lock_irq(&lp->lock);
1497                 smc_10bt_check_media(dev, 1);
1498                 spin_unlock_irq(&lp->lock);
1499         }
1500
1501         netif_start_queue(dev);
1502         return 0;
1503 }
1504
1505 /*
1506  * smc_close
1507  *
1508  * this makes the board clean up everything that it can
1509  * and not talk to the outside world.   Caused by
1510  * an 'ifconfig ethX down'
1511  */
1512 static int smc_close(struct net_device *dev)
1513 {
1514         struct smc_local *lp = netdev_priv(dev);
1515
1516         DBG(2, dev, "%s\n", __func__);
1517
1518         netif_stop_queue(dev);
1519         netif_carrier_off(dev);
1520
1521         /* clear everything */
1522         smc_shutdown(dev);
1523         tasklet_kill(&lp->tx_task);
1524         smc_phy_powerdown(dev);
1525         return 0;
1526 }
1527
1528 /*
1529  * Ethtool support
1530  */
1531 static int
1532 smc_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1533 {
1534         struct smc_local *lp = netdev_priv(dev);
1535         int ret;
1536
1537         cmd->maxtxpkt = 1;
1538         cmd->maxrxpkt = 1;
1539
1540         if (lp->phy_type != 0) {
1541                 spin_lock_irq(&lp->lock);
1542                 ret = mii_ethtool_gset(&lp->mii, cmd);
1543                 spin_unlock_irq(&lp->lock);
1544         } else {
1545                 cmd->supported = SUPPORTED_10baseT_Half |
1546                                  SUPPORTED_10baseT_Full |
1547                                  SUPPORTED_TP | SUPPORTED_AUI;
1548
1549                 if (lp->ctl_rspeed == 10)
1550                         ethtool_cmd_speed_set(cmd, SPEED_10);
1551                 else if (lp->ctl_rspeed == 100)
1552                         ethtool_cmd_speed_set(cmd, SPEED_100);
1553
1554                 cmd->autoneg = AUTONEG_DISABLE;
1555                 cmd->transceiver = XCVR_INTERNAL;
1556                 cmd->port = 0;
1557                 cmd->duplex = lp->tcr_cur_mode & TCR_SWFDUP ? DUPLEX_FULL : DUPLEX_HALF;
1558
1559                 ret = 0;
1560         }
1561
1562         return ret;
1563 }
1564
1565 static int
1566 smc_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1567 {
1568         struct smc_local *lp = netdev_priv(dev);
1569         int ret;
1570
1571         if (lp->phy_type != 0) {
1572                 spin_lock_irq(&lp->lock);
1573                 ret = mii_ethtool_sset(&lp->mii, cmd);
1574                 spin_unlock_irq(&lp->lock);
1575         } else {
1576                 if (cmd->autoneg != AUTONEG_DISABLE ||
1577                     cmd->speed != SPEED_10 ||
1578                     (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL) ||
1579                     (cmd->port != PORT_TP && cmd->port != PORT_AUI))
1580                         return -EINVAL;
1581
1582 //              lp->port = cmd->port;
1583                 lp->ctl_rfduplx = cmd->duplex == DUPLEX_FULL;
1584
1585 //              if (netif_running(dev))
1586 //                      smc_set_port(dev);
1587
1588                 ret = 0;
1589         }
1590
1591         return ret;
1592 }
1593
1594 static void
1595 smc_ethtool_getdrvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1596 {
1597         strlcpy(info->driver, CARDNAME, sizeof(info->driver));
1598         strlcpy(info->version, version, sizeof(info->version));
1599         strlcpy(info->bus_info, dev_name(dev->dev.parent),
1600                 sizeof(info->bus_info));
1601 }
1602
1603 static int smc_ethtool_nwayreset(struct net_device *dev)
1604 {
1605         struct smc_local *lp = netdev_priv(dev);
1606         int ret = -EINVAL;
1607
1608         if (lp->phy_type != 0) {
1609                 spin_lock_irq(&lp->lock);
1610                 ret = mii_nway_restart(&lp->mii);
1611                 spin_unlock_irq(&lp->lock);
1612         }
1613
1614         return ret;
1615 }
1616
1617 static u32 smc_ethtool_getmsglevel(struct net_device *dev)
1618 {
1619         struct smc_local *lp = netdev_priv(dev);
1620         return lp->msg_enable;
1621 }
1622
1623 static void smc_ethtool_setmsglevel(struct net_device *dev, u32 level)
1624 {
1625         struct smc_local *lp = netdev_priv(dev);
1626         lp->msg_enable = level;
1627 }
1628
1629 static int smc_write_eeprom_word(struct net_device *dev, u16 addr, u16 word)
1630 {
1631         u16 ctl;
1632         struct smc_local *lp = netdev_priv(dev);
1633         void __iomem *ioaddr = lp->base;
1634
1635         spin_lock_irq(&lp->lock);
1636         /* load word into GP register */
1637         SMC_SELECT_BANK(lp, 1);
1638         SMC_SET_GP(lp, word);
1639         /* set the address to put the data in EEPROM */
1640         SMC_SELECT_BANK(lp, 2);
1641         SMC_SET_PTR(lp, addr);
1642         /* tell it to write */
1643         SMC_SELECT_BANK(lp, 1);
1644         ctl = SMC_GET_CTL(lp);
1645         SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_STORE));
1646         /* wait for it to finish */
1647         do {
1648                 udelay(1);
1649         } while (SMC_GET_CTL(lp) & CTL_STORE);
1650         /* clean up */
1651         SMC_SET_CTL(lp, ctl);
1652         SMC_SELECT_BANK(lp, 2);
1653         spin_unlock_irq(&lp->lock);
1654         return 0;
1655 }
1656
1657 static int smc_read_eeprom_word(struct net_device *dev, u16 addr, u16 *word)
1658 {
1659         u16 ctl;
1660         struct smc_local *lp = netdev_priv(dev);
1661         void __iomem *ioaddr = lp->base;
1662
1663         spin_lock_irq(&lp->lock);
1664         /* set the EEPROM address to get the data from */
1665         SMC_SELECT_BANK(lp, 2);
1666         SMC_SET_PTR(lp, addr | PTR_READ);
1667         /* tell it to load */
1668         SMC_SELECT_BANK(lp, 1);
1669         SMC_SET_GP(lp, 0xffff); /* init to known */
1670         ctl = SMC_GET_CTL(lp);
1671         SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_RELOAD));
1672         /* wait for it to finish */
1673         do {
1674                 udelay(1);
1675         } while (SMC_GET_CTL(lp) & CTL_RELOAD);
1676         /* read word from GP register */
1677         *word = SMC_GET_GP(lp);
1678         /* clean up */
1679         SMC_SET_CTL(lp, ctl);
1680         SMC_SELECT_BANK(lp, 2);
1681         spin_unlock_irq(&lp->lock);
1682         return 0;
1683 }
1684
1685 static int smc_ethtool_geteeprom_len(struct net_device *dev)
1686 {
1687         return 0x23 * 2;
1688 }
1689
1690 static int smc_ethtool_geteeprom(struct net_device *dev,
1691                 struct ethtool_eeprom *eeprom, u8 *data)
1692 {
1693         int i;
1694         int imax;
1695
1696         DBG(1, dev, "Reading %d bytes at %d(0x%x)\n",
1697                 eeprom->len, eeprom->offset, eeprom->offset);
1698         imax = smc_ethtool_geteeprom_len(dev);
1699         for (i = 0; i < eeprom->len; i += 2) {
1700                 int ret;
1701                 u16 wbuf;
1702                 int offset = i + eeprom->offset;
1703                 if (offset > imax)
1704                         break;
1705                 ret = smc_read_eeprom_word(dev, offset >> 1, &wbuf);
1706                 if (ret != 0)
1707                         return ret;
1708                 DBG(2, dev, "Read 0x%x from 0x%x\n", wbuf, offset >> 1);
1709                 data[i] = (wbuf >> 8) & 0xff;
1710                 data[i+1] = wbuf & 0xff;
1711         }
1712         return 0;
1713 }
1714
1715 static int smc_ethtool_seteeprom(struct net_device *dev,
1716                 struct ethtool_eeprom *eeprom, u8 *data)
1717 {
1718         int i;
1719         int imax;
1720
1721         DBG(1, dev, "Writing %d bytes to %d(0x%x)\n",
1722             eeprom->len, eeprom->offset, eeprom->offset);
1723         imax = smc_ethtool_geteeprom_len(dev);
1724         for (i = 0; i < eeprom->len; i += 2) {
1725                 int ret;
1726                 u16 wbuf;
1727                 int offset = i + eeprom->offset;
1728                 if (offset > imax)
1729                         break;
1730                 wbuf = (data[i] << 8) | data[i + 1];
1731                 DBG(2, dev, "Writing 0x%x to 0x%x\n", wbuf, offset >> 1);
1732                 ret = smc_write_eeprom_word(dev, offset >> 1, wbuf);
1733                 if (ret != 0)
1734                         return ret;
1735         }
1736         return 0;
1737 }
1738
1739
1740 static const struct ethtool_ops smc_ethtool_ops = {
1741         .get_settings   = smc_ethtool_getsettings,
1742         .set_settings   = smc_ethtool_setsettings,
1743         .get_drvinfo    = smc_ethtool_getdrvinfo,
1744
1745         .get_msglevel   = smc_ethtool_getmsglevel,
1746         .set_msglevel   = smc_ethtool_setmsglevel,
1747         .nway_reset     = smc_ethtool_nwayreset,
1748         .get_link       = ethtool_op_get_link,
1749         .get_eeprom_len = smc_ethtool_geteeprom_len,
1750         .get_eeprom     = smc_ethtool_geteeprom,
1751         .set_eeprom     = smc_ethtool_seteeprom,
1752 };
1753
1754 static const struct net_device_ops smc_netdev_ops = {
1755         .ndo_open               = smc_open,
1756         .ndo_stop               = smc_close,
1757         .ndo_start_xmit         = smc_hard_start_xmit,
1758         .ndo_tx_timeout         = smc_timeout,
1759         .ndo_set_rx_mode        = smc_set_multicast_list,
1760         .ndo_change_mtu         = eth_change_mtu,
1761         .ndo_validate_addr      = eth_validate_addr,
1762         .ndo_set_mac_address    = eth_mac_addr,
1763 #ifdef CONFIG_NET_POLL_CONTROLLER
1764         .ndo_poll_controller    = smc_poll_controller,
1765 #endif
1766 };
1767
1768 /*
1769  * smc_findirq
1770  *
1771  * This routine has a simple purpose -- make the SMC chip generate an
1772  * interrupt, so an auto-detect routine can detect it, and find the IRQ,
1773  */
1774 /*
1775  * does this still work?
1776  *
1777  * I just deleted auto_irq.c, since it was never built...
1778  *   --jgarzik
1779  */
1780 static int smc_findirq(struct smc_local *lp)
1781 {
1782         void __iomem *ioaddr = lp->base;
1783         int timeout = 20;
1784         unsigned long cookie;
1785
1786         DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
1787
1788         cookie = probe_irq_on();
1789
1790         /*
1791          * What I try to do here is trigger an ALLOC_INT. This is done
1792          * by allocating a small chunk of memory, which will give an interrupt
1793          * when done.
1794          */
1795         /* enable ALLOCation interrupts ONLY */
1796         SMC_SELECT_BANK(lp, 2);
1797         SMC_SET_INT_MASK(lp, IM_ALLOC_INT);
1798
1799         /*
1800          * Allocate 512 bytes of memory.  Note that the chip was just
1801          * reset so all the memory is available
1802          */
1803         SMC_SET_MMU_CMD(lp, MC_ALLOC | 1);
1804
1805         /*
1806          * Wait until positive that the interrupt has been generated
1807          */
1808         do {
1809                 int int_status;
1810                 udelay(10);
1811                 int_status = SMC_GET_INT(lp);
1812                 if (int_status & IM_ALLOC_INT)
1813                         break;          /* got the interrupt */
1814         } while (--timeout);
1815
1816         /*
1817          * there is really nothing that I can do here if timeout fails,
1818          * as autoirq_report will return a 0 anyway, which is what I
1819          * want in this case.   Plus, the clean up is needed in both
1820          * cases.
1821          */
1822
1823         /* and disable all interrupts again */
1824         SMC_SET_INT_MASK(lp, 0);
1825
1826         /* and return what I found */
1827         return probe_irq_off(cookie);
1828 }
1829
1830 /*
1831  * Function: smc_probe(unsigned long ioaddr)
1832  *
1833  * Purpose:
1834  *      Tests to see if a given ioaddr points to an SMC91x chip.
1835  *      Returns a 0 on success
1836  *
1837  * Algorithm:
1838  *      (1) see if the high byte of BANK_SELECT is 0x33
1839  *      (2) compare the ioaddr with the base register's address
1840  *      (3) see if I recognize the chip ID in the appropriate register
1841  *
1842  * Here I do typical initialization tasks.
1843  *
1844  * o  Initialize the structure if needed
1845  * o  print out my vanity message if not done so already
1846  * o  print out what type of hardware is detected
1847  * o  print out the ethernet address
1848  * o  find the IRQ
1849  * o  set up my private data
1850  * o  configure the dev structure with my subroutines
1851  * o  actually GRAB the irq.
1852  * o  GRAB the region
1853  */
1854 static int smc_probe(struct net_device *dev, void __iomem *ioaddr,
1855                      unsigned long irq_flags)
1856 {
1857         struct smc_local *lp = netdev_priv(dev);
1858         int retval;
1859         unsigned int val, revision_register;
1860         const char *version_string;
1861
1862         DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
1863
1864         /* First, see if the high byte is 0x33 */
1865         val = SMC_CURRENT_BANK(lp);
1866         DBG(2, dev, "%s: bank signature probe returned 0x%04x\n",
1867             CARDNAME, val);
1868         if ((val & 0xFF00) != 0x3300) {
1869                 if ((val & 0xFF) == 0x33) {
1870                         netdev_warn(dev,
1871                                     "%s: Detected possible byte-swapped interface at IOADDR %p\n",
1872                                     CARDNAME, ioaddr);
1873                 }
1874                 retval = -ENODEV;
1875                 goto err_out;
1876         }
1877
1878         /*
1879          * The above MIGHT indicate a device, but I need to write to
1880          * further test this.
1881          */
1882         SMC_SELECT_BANK(lp, 0);
1883         val = SMC_CURRENT_BANK(lp);
1884         if ((val & 0xFF00) != 0x3300) {
1885                 retval = -ENODEV;
1886                 goto err_out;
1887         }
1888
1889         /*
1890          * well, we've already written once, so hopefully another
1891          * time won't hurt.  This time, I need to switch the bank
1892          * register to bank 1, so I can access the base address
1893          * register
1894          */
1895         SMC_SELECT_BANK(lp, 1);
1896         val = SMC_GET_BASE(lp);
1897         val = ((val & 0x1F00) >> 3) << SMC_IO_SHIFT;
1898         if (((unsigned int)ioaddr & (0x3e0 << SMC_IO_SHIFT)) != val) {
1899                 netdev_warn(dev, "%s: IOADDR %p doesn't match configuration (%x).\n",
1900                             CARDNAME, ioaddr, val);
1901         }
1902
1903         /*
1904          * check if the revision register is something that I
1905          * recognize.  These might need to be added to later,
1906          * as future revisions could be added.
1907          */
1908         SMC_SELECT_BANK(lp, 3);
1909         revision_register = SMC_GET_REV(lp);
1910         DBG(2, dev, "%s: revision = 0x%04x\n", CARDNAME, revision_register);
1911         version_string = chip_ids[ (revision_register >> 4) & 0xF];
1912         if (!version_string || (revision_register & 0xff00) != 0x3300) {
1913                 /* I don't recognize this chip, so... */
1914                 netdev_warn(dev, "%s: IO %p: Unrecognized revision register 0x%04x, Contact author.\n",
1915                             CARDNAME, ioaddr, revision_register);
1916
1917                 retval = -ENODEV;
1918                 goto err_out;
1919         }
1920
1921         /* At this point I'll assume that the chip is an SMC91x. */
1922         pr_info_once("%s\n", version);
1923
1924         /* fill in some of the fields */
1925         dev->base_addr = (unsigned long)ioaddr;
1926         lp->base = ioaddr;
1927         lp->version = revision_register & 0xff;
1928         spin_lock_init(&lp->lock);
1929
1930         /* Get the MAC address */
1931         SMC_SELECT_BANK(lp, 1);
1932         SMC_GET_MAC_ADDR(lp, dev->dev_addr);
1933
1934         /* now, reset the chip, and put it into a known state */
1935         smc_reset(dev);
1936
1937         /*
1938          * If dev->irq is 0, then the device has to be banged on to see
1939          * what the IRQ is.
1940          *
1941          * This banging doesn't always detect the IRQ, for unknown reasons.
1942          * a workaround is to reset the chip and try again.
1943          *
1944          * Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1945          * be what is requested on the command line.   I don't do that, mostly
1946          * because the card that I have uses a non-standard method of accessing
1947          * the IRQs, and because this _should_ work in most configurations.
1948          *
1949          * Specifying an IRQ is done with the assumption that the user knows
1950          * what (s)he is doing.  No checking is done!!!!
1951          */
1952         if (dev->irq < 1) {
1953                 int trials;
1954
1955                 trials = 3;
1956                 while (trials--) {
1957                         dev->irq = smc_findirq(lp);
1958                         if (dev->irq)
1959                                 break;
1960                         /* kick the card and try again */
1961                         smc_reset(dev);
1962                 }
1963         }
1964         if (dev->irq == 0) {
1965                 netdev_warn(dev, "Couldn't autodetect your IRQ. Use irq=xx.\n");
1966                 retval = -ENODEV;
1967                 goto err_out;
1968         }
1969         dev->irq = irq_canonicalize(dev->irq);
1970
1971         /* Fill in the fields of the device structure with ethernet values. */
1972         ether_setup(dev);
1973
1974         dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1975         dev->netdev_ops = &smc_netdev_ops;
1976         dev->ethtool_ops = &smc_ethtool_ops;
1977
1978         tasklet_init(&lp->tx_task, smc_hardware_send_pkt, (unsigned long)dev);
1979         INIT_WORK(&lp->phy_configure, smc_phy_configure);
1980         lp->dev = dev;
1981         lp->mii.phy_id_mask = 0x1f;
1982         lp->mii.reg_num_mask = 0x1f;
1983         lp->mii.force_media = 0;
1984         lp->mii.full_duplex = 0;
1985         lp->mii.dev = dev;
1986         lp->mii.mdio_read = smc_phy_read;
1987         lp->mii.mdio_write = smc_phy_write;
1988
1989         /*
1990          * Locate the phy, if any.
1991          */
1992         if (lp->version >= (CHIP_91100 << 4))
1993                 smc_phy_detect(dev);
1994
1995         /* then shut everything down to save power */
1996         smc_shutdown(dev);
1997         smc_phy_powerdown(dev);
1998
1999         /* Set default parameters */
2000         lp->msg_enable = NETIF_MSG_LINK;
2001         lp->ctl_rfduplx = 0;
2002         lp->ctl_rspeed = 10;
2003
2004         if (lp->version >= (CHIP_91100 << 4)) {
2005                 lp->ctl_rfduplx = 1;
2006                 lp->ctl_rspeed = 100;
2007         }
2008
2009         /* Grab the IRQ */
2010         retval = request_irq(dev->irq, smc_interrupt, irq_flags, dev->name, dev);
2011         if (retval)
2012                 goto err_out;
2013
2014 #ifdef CONFIG_ARCH_PXA
2015 #  ifdef SMC_USE_PXA_DMA
2016         lp->cfg.flags |= SMC91X_USE_DMA;
2017 #  endif
2018         if (lp->cfg.flags & SMC91X_USE_DMA) {
2019                 int dma = pxa_request_dma(dev->name, DMA_PRIO_LOW,
2020                                           smc_pxa_dma_irq, NULL);
2021                 if (dma >= 0)
2022                         dev->dma = dma;
2023         }
2024 #endif
2025
2026         retval = register_netdev(dev);
2027         if (retval == 0) {
2028                 /* now, print out the card info, in a short format.. */
2029                 netdev_info(dev, "%s (rev %d) at %p IRQ %d",
2030                             version_string, revision_register & 0x0f,
2031                             lp->base, dev->irq);
2032
2033                 if (dev->dma != (unsigned char)-1)
2034                         pr_cont(" DMA %d", dev->dma);
2035
2036                 pr_cont("%s%s\n",
2037                         lp->cfg.flags & SMC91X_NOWAIT ? " [nowait]" : "",
2038                         THROTTLE_TX_PKTS ? " [throttle_tx]" : "");
2039
2040                 if (!is_valid_ether_addr(dev->dev_addr)) {
2041                         netdev_warn(dev, "Invalid ethernet MAC address. Please set using ifconfig\n");
2042                 } else {
2043                         /* Print the Ethernet address */
2044                         netdev_info(dev, "Ethernet addr: %pM\n",
2045                                     dev->dev_addr);
2046                 }
2047
2048                 if (lp->phy_type == 0) {
2049                         PRINTK(dev, "No PHY found\n");
2050                 } else if ((lp->phy_type & 0xfffffff0) == 0x0016f840) {
2051                         PRINTK(dev, "PHY LAN83C183 (LAN91C111 Internal)\n");
2052                 } else if ((lp->phy_type & 0xfffffff0) == 0x02821c50) {
2053                         PRINTK(dev, "PHY LAN83C180\n");
2054                 }
2055         }
2056
2057 err_out:
2058 #ifdef CONFIG_ARCH_PXA
2059         if (retval && dev->dma != (unsigned char)-1)
2060                 pxa_free_dma(dev->dma);
2061 #endif
2062         return retval;
2063 }
2064
2065 static int smc_enable_device(struct platform_device *pdev)
2066 {
2067         struct net_device *ndev = platform_get_drvdata(pdev);
2068         struct smc_local *lp = netdev_priv(ndev);
2069         unsigned long flags;
2070         unsigned char ecor, ecsr;
2071         void __iomem *addr;
2072         struct resource * res;
2073
2074         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2075         if (!res)
2076                 return 0;
2077
2078         /*
2079          * Map the attribute space.  This is overkill, but clean.
2080          */
2081         addr = ioremap(res->start, ATTRIB_SIZE);
2082         if (!addr)
2083                 return -ENOMEM;
2084
2085         /*
2086          * Reset the device.  We must disable IRQs around this
2087          * since a reset causes the IRQ line become active.
2088          */
2089         local_irq_save(flags);
2090         ecor = readb(addr + (ECOR << SMC_IO_SHIFT)) & ~ECOR_RESET;
2091         writeb(ecor | ECOR_RESET, addr + (ECOR << SMC_IO_SHIFT));
2092         readb(addr + (ECOR << SMC_IO_SHIFT));
2093
2094         /*
2095          * Wait 100us for the chip to reset.
2096          */
2097         udelay(100);
2098
2099         /*
2100          * The device will ignore all writes to the enable bit while
2101          * reset is asserted, even if the reset bit is cleared in the
2102          * same write.  Must clear reset first, then enable the device.
2103          */
2104         writeb(ecor, addr + (ECOR << SMC_IO_SHIFT));
2105         writeb(ecor | ECOR_ENABLE, addr + (ECOR << SMC_IO_SHIFT));
2106
2107         /*
2108          * Set the appropriate byte/word mode.
2109          */
2110         ecsr = readb(addr + (ECSR << SMC_IO_SHIFT)) & ~ECSR_IOIS8;
2111         if (!SMC_16BIT(lp))
2112                 ecsr |= ECSR_IOIS8;
2113         writeb(ecsr, addr + (ECSR << SMC_IO_SHIFT));
2114         local_irq_restore(flags);
2115
2116         iounmap(addr);
2117
2118         /*
2119          * Wait for the chip to wake up.  We could poll the control
2120          * register in the main register space, but that isn't mapped
2121          * yet.  We know this is going to take 750us.
2122          */
2123         msleep(1);
2124
2125         return 0;
2126 }
2127
2128 static int smc_request_attrib(struct platform_device *pdev,
2129                               struct net_device *ndev)
2130 {
2131         struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2132         struct smc_local *lp __maybe_unused = netdev_priv(ndev);
2133
2134         if (!res)
2135                 return 0;
2136
2137         if (!request_mem_region(res->start, ATTRIB_SIZE, CARDNAME))
2138                 return -EBUSY;
2139
2140         return 0;
2141 }
2142
2143 static void smc_release_attrib(struct platform_device *pdev,
2144                                struct net_device *ndev)
2145 {
2146         struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2147         struct smc_local *lp __maybe_unused = netdev_priv(ndev);
2148
2149         if (res)
2150                 release_mem_region(res->start, ATTRIB_SIZE);
2151 }
2152
2153 static inline void smc_request_datacs(struct platform_device *pdev, struct net_device *ndev)
2154 {
2155         if (SMC_CAN_USE_DATACS) {
2156                 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
2157                 struct smc_local *lp = netdev_priv(ndev);
2158
2159                 if (!res)
2160                         return;
2161
2162                 if(!request_mem_region(res->start, SMC_DATA_EXTENT, CARDNAME)) {
2163                         netdev_info(ndev, "%s: failed to request datacs memory region.\n",
2164                                     CARDNAME);
2165                         return;
2166                 }
2167
2168                 lp->datacs = ioremap(res->start, SMC_DATA_EXTENT);
2169         }
2170 }
2171
2172 static void smc_release_datacs(struct platform_device *pdev, struct net_device *ndev)
2173 {
2174         if (SMC_CAN_USE_DATACS) {
2175                 struct smc_local *lp = netdev_priv(ndev);
2176                 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
2177
2178                 if (lp->datacs)
2179                         iounmap(lp->datacs);
2180
2181                 lp->datacs = NULL;
2182
2183                 if (res)
2184                         release_mem_region(res->start, SMC_DATA_EXTENT);
2185         }
2186 }
2187
2188 #if IS_BUILTIN(CONFIG_OF)
2189 static const struct of_device_id smc91x_match[] = {
2190         { .compatible = "smsc,lan91c94", },
2191         { .compatible = "smsc,lan91c111", },
2192         {},
2193 };
2194 MODULE_DEVICE_TABLE(of, smc91x_match);
2195 #endif
2196
2197 /*
2198  * smc_init(void)
2199  *   Input parameters:
2200  *      dev->base_addr == 0, try to find all possible locations
2201  *      dev->base_addr > 0x1ff, this is the address to check
2202  *      dev->base_addr == <anything else>, return failure code
2203  *
2204  *   Output:
2205  *      0 --> there is a device
2206  *      anything else, error
2207  */
2208 static int smc_drv_probe(struct platform_device *pdev)
2209 {
2210         struct smc91x_platdata *pd = dev_get_platdata(&pdev->dev);
2211         const struct of_device_id *match = NULL;
2212         struct smc_local *lp;
2213         struct net_device *ndev;
2214         struct resource *res, *ires;
2215         unsigned int __iomem *addr;
2216         unsigned long irq_flags = SMC_IRQ_FLAGS;
2217         int ret;
2218
2219         ndev = alloc_etherdev(sizeof(struct smc_local));
2220         if (!ndev) {
2221                 ret = -ENOMEM;
2222                 goto out;
2223         }
2224         SET_NETDEV_DEV(ndev, &pdev->dev);
2225
2226         /* get configuration from platform data, only allow use of
2227          * bus width if both SMC_CAN_USE_xxx and SMC91X_USE_xxx are set.
2228          */
2229
2230         lp = netdev_priv(ndev);
2231         lp->cfg.flags = 0;
2232
2233         if (pd) {
2234                 memcpy(&lp->cfg, pd, sizeof(lp->cfg));
2235                 lp->io_shift = SMC91X_IO_SHIFT(lp->cfg.flags);
2236         }
2237
2238 #if IS_BUILTIN(CONFIG_OF)
2239         match = of_match_device(of_match_ptr(smc91x_match), &pdev->dev);
2240         if (match) {
2241                 struct device_node *np = pdev->dev.of_node;
2242                 u32 val;
2243
2244                 /* Combination of IO widths supported, default to 16-bit */
2245                 if (!of_property_read_u32(np, "reg-io-width", &val)) {
2246                         if (val & 1)
2247                                 lp->cfg.flags |= SMC91X_USE_8BIT;
2248                         if ((val == 0) || (val & 2))
2249                                 lp->cfg.flags |= SMC91X_USE_16BIT;
2250                         if (val & 4)
2251                                 lp->cfg.flags |= SMC91X_USE_32BIT;
2252                 } else {
2253                         lp->cfg.flags |= SMC91X_USE_16BIT;
2254                 }
2255         }
2256 #endif
2257
2258         if (!pd && !match) {
2259                 lp->cfg.flags |= (SMC_CAN_USE_8BIT)  ? SMC91X_USE_8BIT  : 0;
2260                 lp->cfg.flags |= (SMC_CAN_USE_16BIT) ? SMC91X_USE_16BIT : 0;
2261                 lp->cfg.flags |= (SMC_CAN_USE_32BIT) ? SMC91X_USE_32BIT : 0;
2262                 lp->cfg.flags |= (nowait) ? SMC91X_NOWAIT : 0;
2263         }
2264
2265         if (!lp->cfg.leda && !lp->cfg.ledb) {
2266                 lp->cfg.leda = RPC_LSA_DEFAULT;
2267                 lp->cfg.ledb = RPC_LSB_DEFAULT;
2268         }
2269
2270         ndev->dma = (unsigned char)-1;
2271
2272         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2273         if (!res)
2274                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2275         if (!res) {
2276                 ret = -ENODEV;
2277                 goto out_free_netdev;
2278         }
2279
2280
2281         if (!request_mem_region(res->start, SMC_IO_EXTENT, CARDNAME)) {
2282                 ret = -EBUSY;
2283                 goto out_free_netdev;
2284         }
2285
2286         ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2287         if (!ires) {
2288                 ret = -ENODEV;
2289                 goto out_release_io;
2290         }
2291
2292         ndev->irq = ires->start;
2293
2294         if (irq_flags == -1 || ires->flags & IRQF_TRIGGER_MASK)
2295                 irq_flags = ires->flags & IRQF_TRIGGER_MASK;
2296
2297         ret = smc_request_attrib(pdev, ndev);
2298         if (ret)
2299                 goto out_release_io;
2300 #if defined(CONFIG_SA1100_ASSABET)
2301         neponset_ncr_set(NCR_ENET_OSC_EN);
2302 #endif
2303         platform_set_drvdata(pdev, ndev);
2304         ret = smc_enable_device(pdev);
2305         if (ret)
2306                 goto out_release_attrib;
2307
2308         addr = ioremap(res->start, SMC_IO_EXTENT);
2309         if (!addr) {
2310                 ret = -ENOMEM;
2311                 goto out_release_attrib;
2312         }
2313
2314 #ifdef CONFIG_ARCH_PXA
2315         {
2316                 struct smc_local *lp = netdev_priv(ndev);
2317                 lp->device = &pdev->dev;
2318                 lp->physaddr = res->start;
2319         }
2320 #endif
2321
2322         ret = smc_probe(ndev, addr, irq_flags);
2323         if (ret != 0)
2324                 goto out_iounmap;
2325
2326         smc_request_datacs(pdev, ndev);
2327
2328         return 0;
2329
2330  out_iounmap:
2331         iounmap(addr);
2332  out_release_attrib:
2333         smc_release_attrib(pdev, ndev);
2334  out_release_io:
2335         release_mem_region(res->start, SMC_IO_EXTENT);
2336  out_free_netdev:
2337         free_netdev(ndev);
2338  out:
2339         pr_info("%s: not found (%d).\n", CARDNAME, ret);
2340
2341         return ret;
2342 }
2343
2344 static int smc_drv_remove(struct platform_device *pdev)
2345 {
2346         struct net_device *ndev = platform_get_drvdata(pdev);
2347         struct smc_local *lp = netdev_priv(ndev);
2348         struct resource *res;
2349
2350         unregister_netdev(ndev);
2351
2352         free_irq(ndev->irq, ndev);
2353
2354 #ifdef CONFIG_ARCH_PXA
2355         if (ndev->dma != (unsigned char)-1)
2356                 pxa_free_dma(ndev->dma);
2357 #endif
2358         iounmap(lp->base);
2359
2360         smc_release_datacs(pdev,ndev);
2361         smc_release_attrib(pdev,ndev);
2362
2363         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2364         if (!res)
2365                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2366         release_mem_region(res->start, SMC_IO_EXTENT);
2367
2368         free_netdev(ndev);
2369
2370         return 0;
2371 }
2372
2373 static int smc_drv_suspend(struct device *dev)
2374 {
2375         struct platform_device *pdev = to_platform_device(dev);
2376         struct net_device *ndev = platform_get_drvdata(pdev);
2377
2378         if (ndev) {
2379                 if (netif_running(ndev)) {
2380                         netif_device_detach(ndev);
2381                         smc_shutdown(ndev);
2382                         smc_phy_powerdown(ndev);
2383                 }
2384         }
2385         return 0;
2386 }
2387
2388 static int smc_drv_resume(struct device *dev)
2389 {
2390         struct platform_device *pdev = to_platform_device(dev);
2391         struct net_device *ndev = platform_get_drvdata(pdev);
2392
2393         if (ndev) {
2394                 struct smc_local *lp = netdev_priv(ndev);
2395                 smc_enable_device(pdev);
2396                 if (netif_running(ndev)) {
2397                         smc_reset(ndev);
2398                         smc_enable(ndev);
2399                         if (lp->phy_type != 0)
2400                                 smc_phy_configure(&lp->phy_configure);
2401                         netif_device_attach(ndev);
2402                 }
2403         }
2404         return 0;
2405 }
2406
2407 static struct dev_pm_ops smc_drv_pm_ops = {
2408         .suspend        = smc_drv_suspend,
2409         .resume         = smc_drv_resume,
2410 };
2411
2412 static struct platform_driver smc_driver = {
2413         .probe          = smc_drv_probe,
2414         .remove         = smc_drv_remove,
2415         .driver         = {
2416                 .name   = CARDNAME,
2417                 .owner  = THIS_MODULE,
2418                 .pm     = &smc_drv_pm_ops,
2419                 .of_match_table = of_match_ptr(smc91x_match),
2420         },
2421 };
2422
2423 module_platform_driver(smc_driver);