blob: d52f34cc95269c961daae470697ce94f6b714c1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * de620.c $Revision: 1.40 $ BETA
3 *
4 *
5 * Linux driver for the D-Link DE-620 Ethernet pocket adapter.
6 *
7 * Portions (C) Copyright 1993, 1994 by Bjorn Ekwall <bj0rn@blox.se>
8 *
9 * Based on adapter information gathered from DOS packetdriver
10 * sources from D-Link Inc: (Special thanks to Henry Ngai of D-Link.)
11 * Portions (C) Copyright D-Link SYSTEM Inc. 1991, 1992
12 * Copyright, 1988, Russell Nelson, Crynwr Software
13 *
14 * Adapted to the sample network driver core for linux,
15 * written by: Donald Becker <becker@super.org>
16 * (Now at <becker@scyld.com>)
17 *
18 * Valuable assistance from:
19 * J. Joshua Kopper <kopper@rtsg.mot.com>
20 * Olav Kvittem <Olav.Kvittem@uninett.no>
21 * Germano Caronni <caronni@nessie.cs.id.ethz.ch>
22 * Jeremy Fitzhardinge <jeremy@suite.sw.oz.au>
23 *
24 *****************************************************************************/
25/*
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2, or (at your option)
29 * any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 *
40 *****************************************************************************/
41static const char version[] =
42 "de620.c: $Revision: 1.40 $, Bjorn Ekwall <bj0rn@blox.se>\n";
Jeff Garzik6aa20a22006-09-13 13:24:59 -040043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/***********************************************************************
45 *
46 * "Tuning" section.
47 *
48 * Compile-time options: (see below for descriptions)
49 * -DDE620_IO=0x378 (lpt1)
50 * -DDE620_IRQ=7 (lpt1)
51 * -DDE602_DEBUG=...
52 * -DSHUTDOWN_WHEN_LOST
53 * -DCOUNT_LOOPS
54 * -DLOWSPEED
55 * -DREAD_DELAY
56 * -DWRITE_DELAY
57 */
58
59/*
60 * This driver assumes that the printer port is a "normal",
61 * dumb, uni-directional port!
62 * If your port is "fancy" in any way, please try to set it to "normal"
63 * with your BIOS setup. I have no access to machines with bi-directional
64 * ports, so I can't test such a driver :-(
65 * (Yes, I _know_ it is possible to use DE620 with bidirectional ports...)
66 *
67 * There are some clones of DE620 out there, with different names.
68 * If the current driver does not recognize a clone, try to change
69 * the following #define to:
70 *
71 * #define DE620_CLONE 1
72 */
73#define DE620_CLONE 0
74
75/*
76 * If the adapter has problems with high speeds, enable this #define
77 * otherwise full printerport speed will be attempted.
78 *
79 * You can tune the READ_DELAY/WRITE_DELAY below if you enable LOWSPEED
80 *
81#define LOWSPEED
82 */
83
84#ifndef READ_DELAY
85#define READ_DELAY 100 /* adapter internal read delay in 100ns units */
86#endif
87
88#ifndef WRITE_DELAY
89#define WRITE_DELAY 100 /* adapter internal write delay in 100ns units */
90#endif
91
92/*
93 * Enable this #define if you want the adapter to do a "ifconfig down" on
94 * itself when we have detected that something is possibly wrong with it.
95 * The default behaviour is to retry with "adapter_init()" until success.
96 * This should be used for debugging purposes only.
97 *
98#define SHUTDOWN_WHEN_LOST
99 */
100
101/*
102 * Enable debugging by "-DDE620_DEBUG=3" when compiling,
103 * OR by enabling the following #define
104 *
105 * use 0 for production, 1 for verification, >2 for debug
106 *
107#define DE620_DEBUG 3
108 */
109
110#ifdef LOWSPEED
111/*
112 * Enable this #define if you want to see debugging output that show how long
113 * we have to wait before the DE-620 is ready for the next read/write/command.
114 *
115#define COUNT_LOOPS
116 */
117#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#include <linux/module.h>
120#include <linux/kernel.h>
121#include <linux/types.h>
122#include <linux/fcntl.h>
123#include <linux/string.h>
124#include <linux/interrupt.h>
125#include <linux/ioport.h>
126#include <linux/in.h>
127#include <linux/errno.h>
128#include <linux/init.h>
129#include <linux/inet.h>
130#include <linux/netdevice.h>
131#include <linux/etherdevice.h>
132#include <linux/skbuff.h>
133
134#include <asm/io.h>
135#include <asm/system.h>
136
137/* Constant definitions for the DE-620 registers, commands and bits */
138#include "de620.h"
139
140typedef unsigned char byte;
141
142/*******************************************************
143 * *
144 * Definition of D-Link DE-620 Ethernet Pocket adapter *
145 * See also "de620.h" *
146 * *
147 *******************************************************/
148#ifndef DE620_IO /* Compile-time configurable */
149#define DE620_IO 0x378
150#endif
151
152#ifndef DE620_IRQ /* Compile-time configurable */
153#define DE620_IRQ 7
154#endif
155
156#define DATA_PORT (dev->base_addr)
157#define STATUS_PORT (dev->base_addr + 1)
158#define COMMAND_PORT (dev->base_addr + 2)
159
160#define RUNT 60 /* Too small Ethernet packet */
161#define GIANT 1514 /* largest legal size packet, no fcs */
162
163#ifdef DE620_DEBUG /* Compile-time configurable */
164#define PRINTK(x) if (de620_debug >= 2) printk x
165#else
166#define DE620_DEBUG 0
167#define PRINTK(x) /**/
168#endif
169
170
171/*
172 * Force media with insmod:
173 * insmod de620.o bnc=1
174 * or
175 * insmod de620.o utp=1
176 *
177 * Force io and/or irq with insmod:
178 * insmod de620.o io=0x378 irq=7
179 *
180 * Make a clone skip the Ethernet-address range check:
181 * insmod de620.o clone=1
182 */
183static int bnc;
184static int utp;
185static int io = DE620_IO;
186static int irq = DE620_IRQ;
187static int clone = DE620_CLONE;
188
189static unsigned int de620_debug = DE620_DEBUG;
190
191static spinlock_t de620_lock;
192
193module_param(bnc, int, 0);
194module_param(utp, int, 0);
195module_param(io, int, 0);
196module_param(irq, int, 0);
197module_param(clone, int, 0);
198module_param(de620_debug, int, 0);
199MODULE_PARM_DESC(bnc, "DE-620 set BNC medium (0-1)");
200MODULE_PARM_DESC(utp, "DE-620 set UTP medium (0-1)");
201MODULE_PARM_DESC(io, "DE-620 I/O base address,required");
202MODULE_PARM_DESC(irq, "DE-620 IRQ number,required");
203MODULE_PARM_DESC(clone, "Check also for non-D-Link DE-620 clones (0-1)");
204MODULE_PARM_DESC(de620_debug, "DE-620 debug level (0-2)");
205
206/***********************************************
207 * *
208 * Index to functions, as function prototypes. *
209 * *
210 ***********************************************/
211
212/*
213 * Routines used internally. (See also "convenience macros.. below")
214 */
215
216/* Put in the device structure. */
217static int de620_open(struct net_device *);
218static int de620_close(struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static void de620_set_multicast_list(struct net_device *);
220static int de620_start_xmit(struct sk_buff *, struct net_device *);
221
222/* Dispatch from interrupts. */
David Howells7d12e782006-10-05 14:55:46 +0100223static irqreturn_t de620_interrupt(int, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int de620_rx_intr(struct net_device *);
225
226/* Initialization */
227static int adapter_init(struct net_device *);
228static int read_eeprom(struct net_device *);
229
230
231/*
232 * D-Link driver variables:
233 */
234#define SCR_DEF NIBBLEMODE |INTON | SLEEP | AUTOTX
235#define TCR_DEF RXPB /* not used: | TXSUCINT | T16INT */
236#define DE620_RX_START_PAGE 12 /* 12 pages (=3k) reserved for tx */
237#define DEF_NIC_CMD IRQEN | ICEN | DS1
238
239static volatile byte NIC_Cmd;
240static volatile byte next_rx_page;
241static byte first_rx_page;
242static byte last_rx_page;
243static byte EIPRegister;
244
245static struct nic {
246 byte NodeID[6];
247 byte RAM_Size;
248 byte Model;
249 byte Media;
250 byte SCR;
251} nic_data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/**********************************************************
254 * *
255 * Convenience macros/functions for D-Link DE-620 adapter *
256 * *
257 **********************************************************/
258#define de620_tx_buffs(dd) (inb(STATUS_PORT) & (TXBF0 | TXBF1))
259#define de620_flip_ds(dd) NIC_Cmd ^= DS0 | DS1; outb(NIC_Cmd, COMMAND_PORT);
260
261/* Check for ready-status, and return a nibble (high 4 bits) for data input */
262#ifdef COUNT_LOOPS
263static int tot_cnt;
264#endif
265static inline byte
266de620_ready(struct net_device *dev)
267{
268 byte value;
269 register short int cnt = 0;
270
271 while ((((value = inb(STATUS_PORT)) & READY) == 0) && (cnt <= 1000))
272 ++cnt;
273
274#ifdef COUNT_LOOPS
275 tot_cnt += cnt;
276#endif
277 return value & 0xf0; /* nibble */
278}
279
280static inline void
281de620_send_command(struct net_device *dev, byte cmd)
282{
283 de620_ready(dev);
284 if (cmd == W_DUMMY)
285 outb(NIC_Cmd, COMMAND_PORT);
286
287 outb(cmd, DATA_PORT);
288
289 outb(NIC_Cmd ^ CS0, COMMAND_PORT);
290 de620_ready(dev);
291 outb(NIC_Cmd, COMMAND_PORT);
292}
293
294static inline void
295de620_put_byte(struct net_device *dev, byte value)
296{
297 /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
298 de620_ready(dev);
299 outb(value, DATA_PORT);
300 de620_flip_ds(dev);
301}
302
303static inline byte
304de620_read_byte(struct net_device *dev)
305{
306 byte value;
307
308 /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
309 value = de620_ready(dev); /* High nibble */
310 de620_flip_ds(dev);
311 value |= de620_ready(dev) >> 4; /* Low nibble */
312 return value;
313}
314
315static inline void
316de620_write_block(struct net_device *dev, byte *buffer, int count, int pad)
317{
318#ifndef LOWSPEED
319 byte uflip = NIC_Cmd ^ (DS0 | DS1);
320 byte dflip = NIC_Cmd;
321#else /* LOWSPEED */
322#ifdef COUNT_LOOPS
323 int bytes = count;
324#endif /* COUNT_LOOPS */
325#endif /* LOWSPEED */
326
327#ifdef LOWSPEED
328#ifdef COUNT_LOOPS
329 tot_cnt = 0;
330#endif /* COUNT_LOOPS */
331 /* No further optimization useful, the limit is in the adapter. */
332 for ( ; count > 0; --count, ++buffer) {
333 de620_put_byte(dev,*buffer);
334 }
335 for ( count = pad ; count > 0; --count, ++buffer) {
336 de620_put_byte(dev, 0);
337 }
338 de620_send_command(dev,W_DUMMY);
339#ifdef COUNT_LOOPS
340 /* trial debug output: loops per byte in de620_ready() */
341 printk("WRITE(%d)\n", tot_cnt/((bytes?bytes:1)));
342#endif /* COUNT_LOOPS */
343#else /* not LOWSPEED */
344 for ( ; count > 0; count -=2) {
345 outb(*buffer++, DATA_PORT);
346 outb(uflip, COMMAND_PORT);
347 outb(*buffer++, DATA_PORT);
348 outb(dflip, COMMAND_PORT);
349 }
350 de620_send_command(dev,W_DUMMY);
351#endif /* LOWSPEED */
352}
353
354static inline void
355de620_read_block(struct net_device *dev, byte *data, int count)
356{
357#ifndef LOWSPEED
358 byte value;
359 byte uflip = NIC_Cmd ^ (DS0 | DS1);
360 byte dflip = NIC_Cmd;
361#else /* LOWSPEED */
362#ifdef COUNT_LOOPS
363 int bytes = count;
364
365 tot_cnt = 0;
366#endif /* COUNT_LOOPS */
367#endif /* LOWSPEED */
368
369#ifdef LOWSPEED
370 /* No further optimization useful, the limit is in the adapter. */
371 while (count-- > 0) {
372 *data++ = de620_read_byte(dev);
373 de620_flip_ds(dev);
374 }
375#ifdef COUNT_LOOPS
376 /* trial debug output: loops per byte in de620_ready() */
377 printk("READ(%d)\n", tot_cnt/(2*(bytes?bytes:1)));
378#endif /* COUNT_LOOPS */
379#else /* not LOWSPEED */
380 while (count-- > 0) {
381 value = inb(STATUS_PORT) & 0xf0; /* High nibble */
382 outb(uflip, COMMAND_PORT);
383 *data++ = value | inb(STATUS_PORT) >> 4; /* Low nibble */
384 outb(dflip , COMMAND_PORT);
385 }
386#endif /* LOWSPEED */
387}
388
389static inline void
390de620_set_delay(struct net_device *dev)
391{
392 de620_ready(dev);
393 outb(W_DFR, DATA_PORT);
394 outb(NIC_Cmd ^ CS0, COMMAND_PORT);
395
396 de620_ready(dev);
397#ifdef LOWSPEED
398 outb(WRITE_DELAY, DATA_PORT);
399#else
400 outb(0, DATA_PORT);
401#endif
402 de620_flip_ds(dev);
403
404 de620_ready(dev);
405#ifdef LOWSPEED
406 outb(READ_DELAY, DATA_PORT);
407#else
408 outb(0, DATA_PORT);
409#endif
410 de620_flip_ds(dev);
411}
412
413static inline void
414de620_set_register(struct net_device *dev, byte reg, byte value)
415{
416 de620_ready(dev);
417 outb(reg, DATA_PORT);
418 outb(NIC_Cmd ^ CS0, COMMAND_PORT);
419
420 de620_put_byte(dev, value);
421}
422
423static inline byte
424de620_get_register(struct net_device *dev, byte reg)
425{
426 byte value;
427
428 de620_send_command(dev,reg);
429 value = de620_read_byte(dev);
430 de620_send_command(dev,W_DUMMY);
431
432 return value;
433}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*********************************************************************
436 *
437 * Open/initialize the board.
438 *
439 * This routine should set everything up anew at each open, even
440 * registers that "should" only need to be set once at boot, so that
441 * there is a non-reboot way to recover if something goes wrong.
442 *
443 */
444static int de620_open(struct net_device *dev)
445{
446 int ret = request_irq(dev->irq, de620_interrupt, 0, dev->name, dev);
447 if (ret) {
448 printk (KERN_ERR "%s: unable to get IRQ %d\n", dev->name, dev->irq);
449 return ret;
450 }
451
452 if (adapter_init(dev)) {
453 ret = -EIO;
454 goto out_free_irq;
455 }
456
457 netif_start_queue(dev);
458 return 0;
459
460out_free_irq:
461 free_irq(dev->irq, dev);
462 return ret;
463}
464
465/************************************************
466 *
467 * The inverse routine to de620_open().
468 *
469 */
470
471static int de620_close(struct net_device *dev)
472{
473 netif_stop_queue(dev);
474 /* disable recv */
475 de620_set_register(dev, W_TCR, RXOFF);
476 free_irq(dev->irq, dev);
477 return 0;
478}
479
480/*********************************************
481 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * Set or clear the multicast filter for this adaptor.
483 * (no real multicast implemented for the DE-620, but she can be promiscuous...)
484 *
485 */
486
487static void de620_set_multicast_list(struct net_device *dev)
488{
489 if (dev->mc_count || dev->flags&(IFF_ALLMULTI|IFF_PROMISC))
490 { /* Enable promiscuous mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 de620_set_register(dev, W_TCR, (TCR_DEF & ~RXPBM) | RXALL);
492 }
493 else
494 { /* Disable promiscuous mode, use normal mode */
495 de620_set_register(dev, W_TCR, TCR_DEF);
496 }
497}
498
499/*******************************************************
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400500 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * Handle timeouts on transmit
502 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static void de620_timeout(struct net_device *dev)
505{
506 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name, "network cable problem");
507 /* Restart the adapter. */
508 if (!adapter_init(dev)) /* maybe close it */
509 netif_wake_queue(dev);
510}
511
512/*******************************************************
513 *
514 * Copy a buffer to the adapter transmit page memory.
515 * Start sending.
516 */
517static int de620_start_xmit(struct sk_buff *skb, struct net_device *dev)
518{
519 unsigned long flags;
520 int len;
521 byte *buffer = skb->data;
522 byte using_txbuf;
523
524 using_txbuf = de620_tx_buffs(dev); /* Peek at the adapter */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 netif_stop_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if ((len = skb->len) < RUNT)
530 len = RUNT;
531 if (len & 1) /* send an even number of bytes */
532 ++len;
533
534 /* Start real output */
535
536 spin_lock_irqsave(&de620_lock, flags)
537 PRINTK(("de620_start_xmit: len=%d, bufs 0x%02x\n",
538 (int)skb->len, using_txbuf));
539
540 /* select a free tx buffer. if there is one... */
541 switch (using_txbuf) {
542 default: /* both are free: use TXBF0 */
543 case TXBF1: /* use TXBF0 */
544 de620_send_command(dev,W_CR | RW0);
545 using_txbuf |= TXBF0;
546 break;
547
548 case TXBF0: /* use TXBF1 */
549 de620_send_command(dev,W_CR | RW1);
550 using_txbuf |= TXBF1;
551 break;
552
553 case (TXBF0 | TXBF1): /* NONE!!! */
554 printk(KERN_WARNING "%s: No tx-buffer available!\n", dev->name);
555 spin_unlock_irqrestore(&de620_lock, flags);
556 return 1;
557 }
558 de620_write_block(dev, buffer, skb->len, len-skb->len);
559
560 dev->trans_start = jiffies;
561 if(!(using_txbuf == (TXBF0 | TXBF1)))
562 netif_wake_queue(dev);
563
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700564 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_unlock_irqrestore(&de620_lock, flags);
566 dev_kfree_skb (skb);
567 return 0;
568}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/*****************************************************
571 *
572 * Handle the network interface interrupts.
573 *
574 */
575static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100576de620_interrupt(int irq_in, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct net_device *dev = dev_id;
579 byte irq_status;
580 int bogus_count = 0;
581 int again = 0;
582
583 spin_lock(&de620_lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Read the status register (_not_ the status port) */
586 irq_status = de620_get_register(dev, R_STS);
587
588 PRINTK(("de620_interrupt (%2.2X)\n", irq_status));
589
590 if (irq_status & RXGOOD) {
591 do {
592 again = de620_rx_intr(dev);
593 PRINTK(("again=%d\n", again));
594 }
595 while (again && (++bogus_count < 100));
596 }
597
598 if(de620_tx_buffs(dev) != (TXBF0 | TXBF1))
599 netif_wake_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_unlock(&de620_lock);
602 return IRQ_HANDLED;
603}
604
605/**************************************
606 *
607 * Get a packet from the adapter
608 *
609 * Send it "upstairs"
610 *
611 */
612static int de620_rx_intr(struct net_device *dev)
613{
614 struct header_buf {
615 byte status;
616 byte Rx_NextPage;
617 unsigned short Rx_ByteCount;
618 } header_buf;
619 struct sk_buff *skb;
620 int size;
621 byte *buffer;
622 byte pagelink;
623 byte curr_page;
624
625 PRINTK(("de620_rx_intr: next_rx_page = %d\n", next_rx_page));
626
627 /* Tell the adapter that we are going to read data, and from where */
628 de620_send_command(dev, W_CR | RRN);
629 de620_set_register(dev, W_RSA1, next_rx_page);
630 de620_set_register(dev, W_RSA0, 0);
631
632 /* Deep breath, and away we goooooo */
633 de620_read_block(dev, (byte *)&header_buf, sizeof(struct header_buf));
634 PRINTK(("page status=0x%02x, nextpage=%d, packetsize=%d\n",
635 header_buf.status, header_buf.Rx_NextPage, header_buf.Rx_ByteCount));
636
637 /* Plausible page header? */
638 pagelink = header_buf.Rx_NextPage;
639 if ((pagelink < first_rx_page) || (last_rx_page < pagelink)) {
640 /* Ouch... Forget it! Skip all and start afresh... */
641 printk(KERN_WARNING "%s: Ring overrun? Restoring...\n", dev->name);
642 /* You win some, you lose some. And sometimes plenty... */
643 adapter_init(dev);
644 netif_wake_queue(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700645 dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647 }
648
649 /* OK, this look good, so far. Let's see if it's consistent... */
650 /* Let's compute the start of the next packet, based on where we are */
651 pagelink = next_rx_page +
652 ((header_buf.Rx_ByteCount + (4 - 1 + 0x100)) >> 8);
653
654 /* Are we going to wrap around the page counter? */
655 if (pagelink > last_rx_page)
656 pagelink -= (last_rx_page - first_rx_page + 1);
657
658 /* Is the _computed_ next page number equal to what the adapter says? */
659 if (pagelink != header_buf.Rx_NextPage) {
660 /* Naah, we'll skip this packet. Probably bogus data as well */
661 printk(KERN_WARNING "%s: Page link out of sync! Restoring...\n", dev->name);
662 next_rx_page = header_buf.Rx_NextPage; /* at least a try... */
663 de620_send_command(dev, W_DUMMY);
664 de620_set_register(dev, W_NPRF, next_rx_page);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700665 dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667 }
668 next_rx_page = pagelink;
669
670 size = header_buf.Rx_ByteCount - 4;
671 if ((size < RUNT) || (GIANT < size)) {
672 printk(KERN_WARNING "%s: Illegal packet size: %d!\n", dev->name, size);
673 }
674 else { /* Good packet? */
675 skb = dev_alloc_skb(size+2);
676 if (skb == NULL) { /* Yeah, but no place to put it... */
677 printk(KERN_WARNING "%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, size);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700678 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 else { /* Yep! Go get it! */
681 skb_reserve(skb,2); /* Align */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* skb->data points to the start of sk_buff data area */
683 buffer = skb_put(skb,size);
684 /* copy the packet into the buffer */
685 de620_read_block(dev, buffer, size);
686 PRINTK(("Read %d bytes\n", size));
687 skb->protocol=eth_type_trans(skb,dev);
688 netif_rx(skb); /* deliver it "upstairs" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* count all receives */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700690 dev->stats.rx_packets++;
691 dev->stats.rx_bytes += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 }
694
695 /* Let's peek ahead to see if we have read the last current packet */
696 /* NOTE! We're _not_ checking the 'EMPTY'-flag! This seems better... */
697 curr_page = de620_get_register(dev, R_CPR);
698 de620_set_register(dev, W_NPRF, next_rx_page);
699 PRINTK(("next_rx_page=%d CPR=%d\n", next_rx_page, curr_page));
700
701 return (next_rx_page != curr_page); /* That was slightly tricky... */
702}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*********************************************
705 *
706 * Reset the adapter to a known state
707 *
708 */
709static int adapter_init(struct net_device *dev)
710{
711 int i;
712 static int was_down;
713
714 if ((nic_data.Model == 3) || (nic_data.Model == 0)) { /* CT */
715 EIPRegister = NCTL0;
716 if (nic_data.Media != 1)
717 EIPRegister |= NIS0; /* not BNC */
718 }
719 else if (nic_data.Model == 2) { /* UTP */
720 EIPRegister = NCTL0 | NIS0;
721 }
722
723 if (utp)
724 EIPRegister = NCTL0 | NIS0;
725 if (bnc)
726 EIPRegister = NCTL0;
727
728 de620_send_command(dev, W_CR | RNOP | CLEAR);
729 de620_send_command(dev, W_CR | RNOP);
730
731 de620_set_register(dev, W_SCR, SCR_DEF);
732 /* disable recv to wait init */
733 de620_set_register(dev, W_TCR, RXOFF);
734
735 /* Set the node ID in the adapter */
736 for (i = 0; i < 6; ++i) { /* W_PARn = 0xaa + n */
737 de620_set_register(dev, W_PAR0 + i, dev->dev_addr[i]);
738 }
739
740 de620_set_register(dev, W_EIP, EIPRegister);
741
742 next_rx_page = first_rx_page = DE620_RX_START_PAGE;
743 if (nic_data.RAM_Size)
744 last_rx_page = nic_data.RAM_Size - 1;
745 else /* 64k RAM */
746 last_rx_page = 255;
747
748 de620_set_register(dev, W_SPR, first_rx_page); /* Start Page Register*/
749 de620_set_register(dev, W_EPR, last_rx_page); /* End Page Register */
750 de620_set_register(dev, W_CPR, first_rx_page);/*Current Page Register*/
751 de620_send_command(dev, W_NPR | first_rx_page); /* Next Page Register*/
752 de620_send_command(dev, W_DUMMY);
753 de620_set_delay(dev);
754
755 /* Final sanity check: Anybody out there? */
756 /* Let's hope some bits from the statusregister make a good check */
757#define CHECK_MASK ( 0 | TXSUC | T16 | 0 | RXCRC | RXSHORT | 0 | 0 )
758#define CHECK_OK ( 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 )
759 /* success: X 0 0 X 0 0 X X */
760 /* ignore: EEDI RXGOOD COLS LNKS*/
761
762 if (((i = de620_get_register(dev, R_STS)) & CHECK_MASK) != CHECK_OK) {
763 printk(KERN_ERR "%s: Something has happened to the DE-620! Please check it"
764#ifdef SHUTDOWN_WHEN_LOST
765 " and do a new ifconfig"
766#endif
767 "! (%02x)\n", dev->name, i);
768#ifdef SHUTDOWN_WHEN_LOST
769 /* Goodbye, cruel world... */
770 dev->flags &= ~IFF_UP;
771 de620_close(dev);
772#endif
773 was_down = 1;
774 return 1; /* failed */
775 }
776 if (was_down) {
777 printk(KERN_WARNING "%s: Thanks, I feel much better now!\n", dev->name);
778 was_down = 0;
779 }
780
781 /* All OK, go ahead... */
782 de620_set_register(dev, W_TCR, TCR_DEF);
783
784 return 0; /* all ok */
785}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400786
Stephen Hemmingerb8aa76a2009-01-07 17:34:36 -0800787static const struct net_device_ops de620_netdev_ops = {
788 .ndo_open = de620_open,
789 .ndo_stop = de620_close,
790 .ndo_start_xmit = de620_start_xmit,
791 .ndo_tx_timeout = de620_timeout,
792 .ndo_set_multicast_list = de620_set_multicast_list,
793 .ndo_change_mtu = eth_change_mtu,
794 .ndo_set_mac_address = eth_mac_addr,
795 .ndo_validate_addr = eth_validate_addr,
796};
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/******************************************************************************
799 *
800 * Only start-up code below
801 *
802 */
803/****************************************
804 *
805 * Check if there is a DE-620 connected
806 */
807struct net_device * __init de620_probe(int unit)
808{
809 byte checkbyte = 0xa5;
810 struct net_device *dev;
811 int err = -ENOMEM;
812 int i;
813
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700814 dev = alloc_etherdev(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!dev)
816 goto out;
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 spin_lock_init(&de620_lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * This is where the base_addr and irq gets set.
822 * Tunable at compile-time and insmod-time
823 */
824 dev->base_addr = io;
825 dev->irq = irq;
826
827 /* allow overriding parameters on command line */
828 if (unit >= 0) {
829 sprintf(dev->name, "eth%d", unit);
830 netdev_boot_setup_check(dev);
831 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (de620_debug)
834 printk(version);
835
836 printk(KERN_INFO "D-Link DE-620 pocket adapter");
837
838 if (!request_region(dev->base_addr, 3, "de620")) {
839 printk(" io 0x%3lX, which is busy.\n", dev->base_addr);
840 err = -EBUSY;
841 goto out1;
842 }
843
844 /* Initially, configure basic nibble mode, so we can read the EEPROM */
845 NIC_Cmd = DEF_NIC_CMD;
846 de620_set_register(dev, W_EIP, EIPRegister);
847
848 /* Anybody out there? */
849 de620_set_register(dev, W_CPR, checkbyte);
850 checkbyte = de620_get_register(dev, R_CPR);
851
852 if ((checkbyte != 0xa5) || (read_eeprom(dev) != 0)) {
853 printk(" not identified in the printer port\n");
854 err = -ENODEV;
855 goto out2;
856 }
857
858 /* else, got it! */
Joe Perches0795af52007-10-03 17:59:30 -0700859 dev->dev_addr[0] = nic_data.NodeID[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 for (i = 1; i < ETH_ALEN; i++) {
Joe Perches0795af52007-10-03 17:59:30 -0700861 dev->dev_addr[i] = nic_data.NodeID[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dev->broadcast[i] = 0xff;
863 }
864
Johannes Berge1749612008-10-27 15:59:26 -0700865 printk(", Ethernet Address: %pM", dev->dev_addr);
Joe Perches0795af52007-10-03 17:59:30 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 printk(" (%dk RAM,",
868 (nic_data.RAM_Size) ? (nic_data.RAM_Size >> 2) : 64);
869
870 if (nic_data.Media == 1)
871 printk(" BNC)\n");
872 else
873 printk(" UTP)\n");
874
Stephen Hemmingerb8aa76a2009-01-07 17:34:36 -0800875 dev->netdev_ops = &de620_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 dev->watchdog_timeo = HZ*2;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* base_addr and irq are already set, see above! */
879
880 /* dump eeprom */
881 if (de620_debug) {
882 printk("\nEEPROM contents:\n");
883 printk("RAM_Size = 0x%02X\n", nic_data.RAM_Size);
Johannes Berg7c510e42008-10-27 17:47:26 -0700884 printk("NodeID = %pM\n", nic_data.NodeID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 printk("Model = %d\n", nic_data.Model);
886 printk("Media = %d\n", nic_data.Media);
887 printk("SCR = 0x%02x\n", nic_data.SCR);
888 }
889
890 err = register_netdev(dev);
891 if (err)
892 goto out2;
893 return dev;
894
895out2:
896 release_region(dev->base_addr, 3);
897out1:
898 free_netdev(dev);
899out:
900 return ERR_PTR(err);
901}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/**********************************
904 *
905 * Read info from on-board EEPROM
906 *
907 * Note: Bitwise serial I/O to/from the EEPROM vi the status _register_!
908 */
909#define sendit(dev,data) de620_set_register(dev, W_EIP, data | EIPRegister);
910
911static unsigned short __init ReadAWord(struct net_device *dev, int from)
912{
913 unsigned short data;
914 int nbits;
915
916 /* cs [__~~] SET SEND STATE */
917 /* di [____] */
918 /* sck [_~~_] */
919 sendit(dev, 0); sendit(dev, 1); sendit(dev, 5); sendit(dev, 4);
920
921 /* Send the 9-bit address from where we want to read the 16-bit word */
922 for (nbits = 9; nbits > 0; --nbits, from <<= 1) {
923 if (from & 0x0100) { /* bit set? */
924 /* cs [~~~~] SEND 1 */
925 /* di [~~~~] */
926 /* sck [_~~_] */
927 sendit(dev, 6); sendit(dev, 7); sendit(dev, 7); sendit(dev, 6);
928 }
929 else {
930 /* cs [~~~~] SEND 0 */
931 /* di [____] */
932 /* sck [_~~_] */
933 sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
934 }
935 }
936
937 /* Shift in the 16-bit word. The bits appear serially in EEDI (=0x80) */
938 for (data = 0, nbits = 16; nbits > 0; --nbits) {
939 /* cs [~~~~] SEND 0 */
940 /* di [____] */
941 /* sck [_~~_] */
942 sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
943 data = (data << 1) | ((de620_get_register(dev, R_STS) & EEDI) >> 7);
944 }
945 /* cs [____] RESET SEND STATE */
946 /* di [____] */
947 /* sck [_~~_] */
948 sendit(dev, 0); sendit(dev, 1); sendit(dev, 1); sendit(dev, 0);
949
950 return data;
951}
952
953static int __init read_eeprom(struct net_device *dev)
954{
955 unsigned short wrd;
956
957 /* D-Link Ethernet addresses are in the series 00:80:c8:7X:XX:XX:XX */
958 wrd = ReadAWord(dev, 0x1aa); /* bytes 0 + 1 of NodeID */
959 if (!clone && (wrd != htons(0x0080))) /* Valid D-Link ether sequence? */
960 return -1; /* Nope, not a DE-620 */
961 nic_data.NodeID[0] = wrd & 0xff;
962 nic_data.NodeID[1] = wrd >> 8;
963
964 wrd = ReadAWord(dev, 0x1ab); /* bytes 2 + 3 of NodeID */
965 if (!clone && ((wrd & 0xff) != 0xc8)) /* Valid D-Link ether sequence? */
966 return -1; /* Nope, not a DE-620 */
967 nic_data.NodeID[2] = wrd & 0xff;
968 nic_data.NodeID[3] = wrd >> 8;
969
970 wrd = ReadAWord(dev, 0x1ac); /* bytes 4 + 5 of NodeID */
971 nic_data.NodeID[4] = wrd & 0xff;
972 nic_data.NodeID[5] = wrd >> 8;
973
974 wrd = ReadAWord(dev, 0x1ad); /* RAM size in pages (256 bytes). 0 = 64k */
975 nic_data.RAM_Size = (wrd >> 8);
976
977 wrd = ReadAWord(dev, 0x1ae); /* hardware model (CT = 3) */
978 nic_data.Model = (wrd & 0xff);
979
980 wrd = ReadAWord(dev, 0x1af); /* media (indicates BNC/UTP) */
981 nic_data.Media = (wrd & 0xff);
982
983 wrd = ReadAWord(dev, 0x1a8); /* System Configuration Register */
984 nic_data.SCR = (wrd >> 8);
985
986 return 0; /* no errors */
987}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/******************************************************************************
990 *
991 * Loadable module skeleton
992 *
993 */
994#ifdef MODULE
995static struct net_device *de620_dev;
996
Sam Ravnborgb2cba5b2006-03-08 00:06:33 -0800997int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 de620_dev = de620_probe(-1);
1000 if (IS_ERR(de620_dev))
1001 return PTR_ERR(de620_dev);
1002 return 0;
1003}
1004
1005void cleanup_module(void)
1006{
1007 unregister_netdev(de620_dev);
1008 release_region(de620_dev->base_addr, 3);
1009 free_netdev(de620_dev);
1010}
1011#endif /* MODULE */
1012MODULE_LICENSE("GPL");