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