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