1 /*
2  *
3  *    Copyright (c) 1999-2000 Grant Erickson <grant@lcse.umn.edu>
4  *
5  *    Module name: oaknet.c
6  *
7  *    Description:
8  *      Driver for the National Semiconductor DP83902AV Ethernet controller
9  *      on-board the IBM PowerPC "Oak" evaluation board. Adapted from the
10  *      various other 8390 drivers written by Donald Becker and Paul Gortmaker.
11  *
12  *      Additional inspiration from the "tcd8390.c" driver from TiVo, Inc.
13  *      and "enetLib.c" from IBM.
14  *
15  */
16 
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/delay.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/init.h>
23 
24 #include <asm/board.h>
25 #include <asm/io.h>
26 
27 #include "8390.h"
28 
29 
30 /* Preprocessor Defines */
31 
32 #if !defined(TRUE) || TRUE != 1
33 #define	TRUE	1
34 #endif
35 
36 #if !defined(FALSE) || FALSE != 0
37 #define	FALSE	0
38 #endif
39 
40 #define	OAKNET_START_PG		0x20	/* First page of TX buffer */
41 #define	OAKNET_STOP_PG		0x40	/* Last pagge +1 of RX ring */
42 
43 #define	OAKNET_WAIT		(2 * HZ / 100)	/* 20 ms */
44 
45 /* Experimenting with some fixes for a broken driver... */
46 
47 #define	OAKNET_DISINT
48 #define	OAKNET_HEADCHECK
49 #define	OAKNET_RWFIX
50 
51 
52 /* Global Variables */
53 
54 static const char *name = "National DP83902AV";
55 
56 static struct net_device *oaknet_devs;
57 
58 
59 /* Function Prototypes */
60 
61 static int	 oaknet_open(struct net_device *dev);
62 static int	 oaknet_close(struct net_device *dev);
63 
64 static void	 oaknet_reset_8390(struct net_device *dev);
65 static void	 oaknet_get_8390_hdr(struct net_device *dev,
66 				     struct e8390_pkt_hdr *hdr, int ring_page);
67 static void	 oaknet_block_input(struct net_device *dev, int count,
68 				    struct sk_buff *skb, int ring_offset);
69 static void	 oaknet_block_output(struct net_device *dev, int count,
70 				     const unsigned char *buf, int start_page);
71 
72 static void	 oaknet_dma_error(struct net_device *dev, const char *name);
73 
74 
75 /*
76  * int oaknet_init()
77  *
78  * Description:
79  *   This routine performs all the necessary platform-specific initiali-
80  *   zation and set-up for the IBM "Oak" evaluation board's National
81  *   Semiconductor DP83902AV "ST-NIC" Ethernet controller.
82  *
83  * Input(s):
84  *   N/A
85  *
86  * Output(s):
87  *   N/A
88  *
89  * Returns:
90  *   0 if OK, otherwise system error number on error.
91  *
92  */
oaknet_init(void)93 static int __init oaknet_init(void)
94 {
95 	register int i;
96 	int reg0, regd;
97 	int ret;
98 	struct net_device tmp, *dev = NULL;
99 #if 0
100 	unsigned long ioaddr = OAKNET_IO_BASE;
101 #else
102 	unsigned long ioaddr = ioremap(OAKNET_IO_BASE, OAKNET_IO_SIZE);
103 #endif
104 	bd_t *bip = (bd_t *)__res;
105 
106 	if (!ioaddr)
107 		return -ENOMEM;
108 	/*
109 	 * This MUST happen here because of the nic_* macros
110 	 * which have an implicit dependency on dev->base_addr.
111 	 */
112 
113 	tmp.base_addr = ioaddr;
114 	dev = &tmp;
115 
116 	ret = -EBUSY;
117 	if (!request_region(OAKNET_IO_BASE, OAKNET_IO_SIZE, name))
118 		goto out_unmap;
119 
120 	/* Quick register check to see if the device is really there. */
121 
122 	ret = -ENODEV;
123 	if ((reg0 = ei_ibp(ioaddr)) == 0xFF)
124 		goto out_region;
125 
126 	/*
127 	 * That worked. Now a more thorough check, using the multicast
128 	 * address registers, that the device is definitely out there
129 	 * and semi-functional.
130 	 */
131 
132 	ei_obp(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
133 	regd = ei_ibp(ioaddr + 0x0D);
134 	ei_obp(0xFF, ioaddr + 0x0D);
135 	ei_obp(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
136 	ei_ibp(ioaddr + EN0_COUNTER0);
137 
138 	/* It's no good. Fix things back up and leave. */
139 
140 	ret = -ENODEV;
141 	if (ei_ibp(ioaddr + EN0_COUNTER0) != 0) {
142 		ei_obp(reg0, ioaddr);
143 		ei_obp(regd, ioaddr + 0x0D);
144 		goto out_region;
145 	}
146 
147 	/*
148 	 * We're not using the old-style probing API, so we have to allocate
149 	 * our own device structure.
150 	 */
151 
152 	dev = init_etherdev(NULL, 0);
153 	ret = -ENOMEM;
154 	if (!dev)
155 		goto out_region;
156 	SET_MODULE_OWNER(dev);
157 	oaknet_devs = dev;
158 
159 	/*
160 	 * This controller is on an embedded board, so the base address
161 	 * and interrupt assignments are pre-assigned and unchageable.
162 	 */
163 
164 	dev->base_addr = ioaddr;
165 	dev->irq = OAKNET_INT;
166 
167 	/* Allocate 8390-specific device-private area and fields. */
168 
169 	ret = -ENOMEM;
170 	if (ethdev_init(dev)) {
171 		printk(" unable to get memory for dev->priv.\n");
172 		goto out_dev;
173 	}
174 
175 	/*
176 	 * Disable all chip interrupts for now and ACK all pending
177 	 * interrupts.
178 	 */
179 
180 	ei_obp(0x0, ioaddr + EN0_IMR);
181 	ei_obp(0xFF, ioaddr + EN0_ISR);
182 
183 	/* Attempt to get the interrupt line */
184 
185 	ret = -EAGAIN;
186 	if (request_irq(dev->irq, ei_interrupt, 0, name, dev)) {
187 		printk("%s: unable to request interrupt %d.\n",
188 		       dev->name, dev->irq);
189 		goto out_priv;
190 	}
191 
192 	/* Tell the world about what and where we've found. */
193 
194 	printk("%s: %s at", dev->name, name);
195 	for (i = 0; i < ETHER_ADDR_LEN; ++i) {
196 		dev->dev_addr[i] = bip->bi_enetaddr[i];
197 		printk("%c%.2x", (i ? ':' : ' '), dev->dev_addr[i]);
198 	}
199 	printk(", found at %#lx, using IRQ %d.\n", dev->base_addr, dev->irq);
200 
201 	/* Set up some required driver fields and then we're done. */
202 
203 	ei_status.name		= name;
204 	ei_status.word16	= FALSE;
205 	ei_status.tx_start_page	= OAKNET_START_PG;
206 	ei_status.rx_start_page = OAKNET_START_PG + TX_PAGES;
207 	ei_status.stop_page	= OAKNET_STOP_PG;
208 
209 	ei_status.reset_8390	= &oaknet_reset_8390;
210 	ei_status.block_input	= &oaknet_block_input;
211 	ei_status.block_output	= &oaknet_block_output;
212 	ei_status.get_8390_hdr	= &oaknet_get_8390_hdr;
213 
214 	dev->open = oaknet_open;
215 	dev->stop = oaknet_close;
216 
217 	NS8390_init(dev, FALSE);
218 
219 	return (0);
220 out_priv:
221 	kfree(dev->priv);
222 out_dev:
223 	unregister_netdev(dev);
224 	kfree(dev);
225 out_region:
226 	release_region(OAKNET_IO_BASE, OAKNET_IO_SIZE);
227 out_unmap:
228 	iounmap(ioaddr);
229 	return ret;
230 }
231 
232 /*
233  * static int oaknet_open()
234  *
235  * Description:
236  *   This routine is a modest wrapper around ei_open, the 8390-generic,
237  *   driver open routine. This just increments the module usage count
238  *   and passes along the status from ei_open.
239  *
240  * Input(s):
241  *  *dev - Pointer to the device structure for this driver.
242  *
243  * Output(s):
244  *  *dev - Pointer to the device structure for this driver, potentially
245  *         modified by ei_open.
246  *
247  * Returns:
248  *   0 if OK, otherwise < 0 on error.
249  *
250  */
251 static int
oaknet_open(struct net_device * dev)252 oaknet_open(struct net_device *dev)
253 {
254 	int status = ei_open(dev);
255 	return (status);
256 }
257 
258 /*
259  * static int oaknet_close()
260  *
261  * Description:
262  *   This routine is a modest wrapper around ei_close, the 8390-generic,
263  *   driver close routine. This just decrements the module usage count
264  *   and passes along the status from ei_close.
265  *
266  * Input(s):
267  *  *dev - Pointer to the device structure for this driver.
268  *
269  * Output(s):
270  *  *dev - Pointer to the device structure for this driver, potentially
271  *         modified by ei_close.
272  *
273  * Returns:
274  *   0 if OK, otherwise < 0 on error.
275  *
276  */
277 static int
oaknet_close(struct net_device * dev)278 oaknet_close(struct net_device *dev)
279 {
280 	int status = ei_close(dev);
281 	return (status);
282 }
283 
284 /*
285  * static void oaknet_reset_8390()
286  *
287  * Description:
288  *   This routine resets the DP83902 chip.
289  *
290  * Input(s):
291  *  *dev - Pointer to the device structure for this driver.
292  *
293  * Output(s):
294  *   N/A
295  *
296  * Returns:
297  *   N/A
298  *
299  */
300 static void
oaknet_reset_8390(struct net_device * dev)301 oaknet_reset_8390(struct net_device *dev)
302 {
303 	int base = E8390_BASE;
304 
305 	/*
306 	 * We have no provision of reseting the controller as is done
307 	 * in other drivers, such as "ne.c". However, the following
308 	 * seems to work well enough in the TiVo driver.
309 	 */
310 
311 	printk("Resetting %s...\n", dev->name);
312 	ei_obp(E8390_STOP | E8390_NODMA | E8390_PAGE0, base + E8390_CMD);
313 	ei_status.txing = 0;
314 	ei_status.dmaing = 0;
315 }
316 
317 /*
318  * static void oaknet_get_8390_hdr()
319  *
320  * Description:
321  *   This routine grabs the 8390-specific header. It's similar to the
322  *   block input routine, but we don't need to be concerned with ring wrap
323  *   as the header will be at the start of a page, so we optimize accordingly.
324  *
325  * Input(s):
326  *  *dev       - Pointer to the device structure for this driver.
327  *  *hdr       - Pointer to storage for the 8390-specific packet header.
328  *   ring_page - ?
329  *
330  * Output(s):
331  *  *hdr       - Pointer to the 8390-specific packet header for the just-
332  *               received frame.
333  *
334  * Returns:
335  *   N/A
336  *
337  */
338 static void
oaknet_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)339 oaknet_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
340 		    int ring_page)
341 {
342 	int base = dev->base_addr;
343 
344 	/*
345 	 * This should NOT happen. If it does, it is the LAST thing you'll
346 	 * see.
347 	 */
348 
349 	if (ei_status.dmaing) {
350 		oaknet_dma_error(dev, "oaknet_get_8390_hdr");
351 		return;
352 	}
353 
354 	ei_status.dmaing |= 0x01;
355 	outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, base + OAKNET_CMD);
356 	outb_p(sizeof(struct e8390_pkt_hdr), base + EN0_RCNTLO);
357 	outb_p(0, base + EN0_RCNTHI);
358 	outb_p(0, base + EN0_RSARLO);		/* On page boundary */
359 	outb_p(ring_page, base + EN0_RSARHI);
360 	outb_p(E8390_RREAD + E8390_START, base + OAKNET_CMD);
361 
362 	if (ei_status.word16)
363 		insw(base + OAKNET_DATA, hdr,
364 		     sizeof(struct e8390_pkt_hdr) >> 1);
365 	else
366 		insb(base + OAKNET_DATA, hdr,
367 		     sizeof(struct e8390_pkt_hdr));
368 
369 	/* Byte-swap the packet byte count */
370 
371 	hdr->count = le16_to_cpu(hdr->count);
372 
373 	outb_p(ENISR_RDC, base + EN0_ISR);	/* ACK Remote DMA interrupt */
374 	ei_status.dmaing &= ~0x01;
375 }
376 
377 /*
378  * XXX - Document me.
379  */
380 static void
oaknet_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)381 oaknet_block_input(struct net_device *dev, int count, struct sk_buff *skb,
382 		   int ring_offset)
383 {
384 	int base = OAKNET_BASE;
385 	char *buf = skb->data;
386 
387 	/*
388 	 * This should NOT happen. If it does, it is the LAST thing you'll
389 	 * see.
390 	 */
391 
392 	if (ei_status.dmaing) {
393 		oaknet_dma_error(dev, "oaknet_block_input");
394 		return;
395 	}
396 
397 #ifdef OAKNET_DISINT
398 	save_flags(flags);
399 	cli();
400 #endif
401 
402 	ei_status.dmaing |= 0x01;
403 	ei_obp(E8390_NODMA + E8390_PAGE0 + E8390_START, base + E8390_CMD);
404 	ei_obp(count & 0xff, base + EN0_RCNTLO);
405 	ei_obp(count >> 8, base + EN0_RCNTHI);
406 	ei_obp(ring_offset & 0xff, base + EN0_RSARLO);
407 	ei_obp(ring_offset >> 8, base + EN0_RSARHI);
408 	ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
409 	if (ei_status.word16) {
410 		ei_isw(base + E8390_DATA, buf, count >> 1);
411 		if (count & 0x01) {
412 			buf[count - 1] = ei_ib(base + E8390_DATA);
413 #ifdef OAKNET_HEADCHECK
414 			bytes++;
415 #endif
416 		}
417 	} else {
418 		ei_isb(base + E8390_DATA, buf, count);
419 	}
420 #ifdef OAKNET_HEADCHECK
421 	/*
422 	 * This was for the ALPHA version only, but enough people have
423 	 * been encountering problems so it is still here.  If you see
424 	 * this message you either 1) have a slightly incompatible clone
425 	 * or 2) have noise/speed problems with your bus.
426 	 */
427 
428 	/* DMA termination address check... */
429 	{
430 		int addr, tries = 20;
431 		do {
432 			/* DON'T check for 'ei_ibp(EN0_ISR) & ENISR_RDC' here
433 			   -- it's broken for Rx on some cards! */
434 			int high = ei_ibp(base + EN0_RSARHI);
435 			int low = ei_ibp(base + EN0_RSARLO);
436 			addr = (high << 8) + low;
437 			if (((ring_offset + bytes) & 0xff) == low)
438 				break;
439 		} while (--tries > 0);
440 	 	if (tries <= 0)
441 			printk("%s: RX transfer address mismatch,"
442 			       "%#4.4x (expected) vs. %#4.4x (actual).\n",
443 			       dev->name, ring_offset + bytes, addr);
444 	}
445 #endif
446 	ei_obp(ENISR_RDC, base + EN0_ISR);	/* ACK Remote DMA interrupt */
447 	ei_status.dmaing &= ~0x01;
448 
449 #ifdef OAKNET_DISINT
450 	restore_flags(flags);
451 #endif
452 }
453 
454 /*
455  * static void oaknet_block_output()
456  *
457  * Description:
458  *   This routine...
459  *
460  * Input(s):
461  *  *dev        - Pointer to the device structure for this driver.
462  *   count      - Number of bytes to be transferred.
463  *  *buf        -
464  *   start_page -
465  *
466  * Output(s):
467  *   N/A
468  *
469  * Returns:
470  *   N/A
471  *
472  */
473 static void
oaknet_block_output(struct net_device * dev,int count,const unsigned char * buf,int start_page)474 oaknet_block_output(struct net_device *dev, int count,
475 		    const unsigned char *buf, int start_page)
476 {
477 	int base = E8390_BASE;
478 #if 0
479 	int bug;
480 #endif
481 	unsigned long start;
482 #ifdef OAKNET_DISINT
483 	unsigned long flags;
484 #endif
485 #ifdef OAKNET_HEADCHECK
486 	int retries = 0;
487 #endif
488 
489 	/* Round the count up for word writes. */
490 
491 	if (ei_status.word16 && (count & 0x1))
492 		count++;
493 
494 	/*
495 	 * This should NOT happen. If it does, it is the LAST thing you'll
496 	 * see.
497 	 */
498 
499 	if (ei_status.dmaing) {
500 		oaknet_dma_error(dev, "oaknet_block_output");
501 		return;
502 	}
503 
504 #ifdef OAKNET_DISINT
505 	save_flags(flags);
506 	cli();
507 #endif
508 
509 	ei_status.dmaing |= 0x01;
510 
511 	/* Make sure we are in page 0. */
512 
513 	ei_obp(E8390_PAGE0 + E8390_START + E8390_NODMA, base + E8390_CMD);
514 
515 #ifdef OAKNET_HEADCHECK
516 retry:
517 #endif
518 
519 #if 0
520 	/*
521 	 * The 83902 documentation states that the processor needs to
522 	 * do a "dummy read" before doing the remote write to work
523 	 * around a chip bug they don't feel like fixing.
524 	 */
525 
526 	bug = 0;
527 	while (1) {
528 		unsigned int rdhi;
529 		unsigned int rdlo;
530 
531 		/* Now the normal output. */
532 		ei_obp(ENISR_RDC, base + EN0_ISR);
533 		ei_obp(count & 0xff, base + EN0_RCNTLO);
534 		ei_obp(count >> 8,   base + EN0_RCNTHI);
535 		ei_obp(0x00, base + EN0_RSARLO);
536 		ei_obp(start_page, base + EN0_RSARHI);
537 
538 		if (bug++)
539 			break;
540 
541 		/* Perform the dummy read */
542 		rdhi = ei_ibp(base + EN0_CRDAHI);
543 		rdlo = ei_ibp(base + EN0_CRDALO);
544 		ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
545 
546 		while (1) {
547 			unsigned int nrdhi;
548 			unsigned int nrdlo;
549 			nrdhi = ei_ibp(base + EN0_CRDAHI);
550 			nrdlo = ei_ibp(base + EN0_CRDALO);
551 			if ((rdhi != nrdhi) || (rdlo != nrdlo))
552 				break;
553 		}
554 	}
555 #else
556 #ifdef OAKNET_RWFIX
557 	/*
558 	 * Handle the read-before-write bug the same way as the
559 	 * Crynwr packet driver -- the Nat'l Semi. method doesn't work.
560 	 * Actually this doesn't always work either, but if you have
561 	 * problems with your 83902 this is better than nothing!
562 	 */
563 
564 	ei_obp(0x42, base + EN0_RCNTLO);
565 	ei_obp(0x00, base + EN0_RCNTHI);
566 	ei_obp(0x42, base + EN0_RSARLO);
567 	ei_obp(0x00, base + EN0_RSARHI);
568 	ei_obp(E8390_RREAD + E8390_START, base + E8390_CMD);
569 	/* Make certain that the dummy read has occurred. */
570 	udelay(6);
571 #endif
572 
573 	ei_obp(ENISR_RDC, base + EN0_ISR);
574 
575 	/* Now the normal output. */
576 	ei_obp(count & 0xff, base + EN0_RCNTLO);
577 	ei_obp(count >> 8,   base + EN0_RCNTHI);
578 	ei_obp(0x00, base + EN0_RSARLO);
579 	ei_obp(start_page, base + EN0_RSARHI);
580 #endif /* 0/1 */
581 
582 	ei_obp(E8390_RWRITE + E8390_START, base + E8390_CMD);
583 	if (ei_status.word16) {
584 		ei_osw(E8390_BASE + E8390_DATA, buf, count >> 1);
585 	} else {
586 		ei_osb(E8390_BASE + E8390_DATA, buf, count);
587 	}
588 
589 #ifdef OAKNET_DISINT
590 	restore_flags(flags);
591 #endif
592 
593 	start = jiffies;
594 
595 #ifdef OAKNET_HEADCHECK
596 	/*
597 	 * This was for the ALPHA version only, but enough people have
598 	 * been encountering problems so it is still here.
599 	 */
600 
601 	{
602 		/* DMA termination address check... */
603 		int addr, tries = 20;
604 		do {
605 			int high = ei_ibp(base + EN0_RSARHI);
606 			int low = ei_ibp(base + EN0_RSARLO);
607 			addr = (high << 8) + low;
608 			if ((start_page << 8) + count == addr)
609 				break;
610 		} while (--tries > 0);
611 
612 		if (tries <= 0) {
613 			printk("%s: Tx packet transfer address mismatch,"
614 			       "%#4.4x (expected) vs. %#4.4x (actual).\n",
615 			       dev->name, (start_page << 8) + count, addr);
616 			if (retries++ == 0)
617 				goto retry;
618 		}
619 	}
620 #endif
621 
622 	while ((ei_ibp(base + EN0_ISR) & ENISR_RDC) == 0) {
623 		if (jiffies - start > OAKNET_WAIT) {
624 			printk("%s: timeout waiting for Tx RDC.\n", dev->name);
625 			oaknet_reset_8390(dev);
626 			NS8390_init(dev, TRUE);
627 			break;
628 		}
629 	}
630 
631 	ei_obp(ENISR_RDC, base + EN0_ISR);	/* Ack intr. */
632 	ei_status.dmaing &= ~0x01;
633 }
634 
635 /*
636  * static void oaknet_dma_error()
637  *
638  * Description:
639  *   This routine prints out a last-ditch informative message to the console
640  *   indicating that a DMA error occurred. If you see this, it's the last
641  *   thing you'll see.
642  *
643  * Input(s):
644  *  *dev  - Pointer to the device structure for this driver.
645  *  *name - Informative text (e.g. function name) indicating where the
646  *          DMA error occurred.
647  *
648  * Output(s):
649  *   N/A
650  *
651  * Returns:
652  *   N/A
653  *
654  */
655 static void
oaknet_dma_error(struct net_device * dev,const char * name)656 oaknet_dma_error(struct net_device *dev, const char *name)
657 {
658 	printk(KERN_EMERG "%s: DMAing conflict in %s."
659 	       "[DMAstat:%d][irqlock:%d][intr:%ld]\n",
660 	       dev->name, name, ei_status.dmaing, ei_status.irqlock,
661 	       dev->interrupt);
662 }
663 
664 /*
665  * Oak Ethernet module load interface.
666  */
oaknet_init_module(void)667 static int __init oaknet_init_module (void)
668 {
669 	if (oaknet_devs != NULL)
670 		return (-EBUSY);
671 
672 	return (oaknet_init());
673 }
674 
675 /*
676  * Oak Ethernet module unload interface.
677  */
oaknet_cleanup_module(void)678 static void __exit oaknet_cleanup_module (void)
679 {
680 	if (oaknet_devs == NULL)
681 		return;
682 
683 	if (oaknet_devs->priv != NULL) {
684 		int ioaddr = oaknet_devs->base_addr;
685 		void *priv = oaknet_devs->priv;
686 		free_irq(oaknet_devs->irq, oaknet_devs);
687 		release_region(ioaddr, OAKNET_IO_SIZE);
688 		iounmap(ioaddr);
689 		unregister_netdev(oaknet_dev);
690 		kfree(priv);
691 	}
692 
693 	/* Convert to loop once driver supports multiple devices. */
694 	kfree(oaknet_devs);
695 }
696 
697 module_init(oaknet_init_module);
698 module_exit(oaknet_cleanup_module);
699 MODULE_LICENSE("GPL");
700