1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19static const char version1[] =
20"ne-h8300.c:v1.00 2004/04/11 ysato\n";
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/jiffies.h>
31
32#include <asm/system.h>
33#include <asm/io.h>
34#include <asm/irq.h>
35
36#define EI_SHIFT(x) (ei_local->reg_offset[x])
37
38#include "8390.h"
39
40#define DRV_NAME "ne-h8300"
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57static const char version[] =
58 "8390.c:v1.10cvs 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
59
60#include "lib8390.c"
61
62#define NE_BASE (dev->base_addr)
63#define NE_CMD 0x00
64#define NE_DATAPORT (ei_status.word16?0x20:0x10)
65#define NE_RESET (ei_status.word16?0x3f:0x1f)
66#define NE_IO_EXTENT (ei_status.word16?0x40:0x20)
67
68#define NESM_START_PG 0x40
69#define NESM_STOP_PG 0x80
70
71static int ne_probe1(struct net_device *dev, int ioaddr);
72
73static int ne_open(struct net_device *dev);
74static int ne_close(struct net_device *dev);
75
76static void ne_reset_8390(struct net_device *dev);
77static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
78 int ring_page);
79static void ne_block_input(struct net_device *dev, int count,
80 struct sk_buff *skb, int ring_offset);
81static void ne_block_output(struct net_device *dev, const int count,
82 const unsigned char *buf, const int start_page);
83
84
85static u32 reg_offset[16];
86
87static int __init init_reg_offset(struct net_device *dev,unsigned long base_addr)
88{
89 struct ei_device *ei_local = netdev_priv(dev);
90 int i;
91 unsigned char bus_width;
92
93 bus_width = *(volatile unsigned char *)ABWCR;
94 bus_width &= 1 << ((base_addr >> 21) & 7);
95
96 for (i = 0; i < ARRAY_SIZE(reg_offset); i++)
97 if (bus_width == 0)
98 reg_offset[i] = i * 2 + 1;
99 else
100 reg_offset[i] = i;
101
102 ei_local->reg_offset = reg_offset;
103 return 0;
104}
105
106static int __initdata h8300_ne_count = 0;
107#ifdef CONFIG_H8300H_H8MAX
108static unsigned long __initdata h8300_ne_base[] = { 0x800600 };
109static int h8300_ne_irq[] = {EXT_IRQ4};
110#endif
111#ifdef CONFIG_H8300H_AKI3068NET
112static unsigned long __initdata h8300_ne_base[] = { 0x200000 };
113static int h8300_ne_irq[] = {EXT_IRQ5};
114#endif
115
116static inline int init_dev(struct net_device *dev)
117{
118 if (h8300_ne_count < ARRAY_SIZE(h8300_ne_base)) {
119 dev->base_addr = h8300_ne_base[h8300_ne_count];
120 dev->irq = h8300_ne_irq[h8300_ne_count];
121 h8300_ne_count++;
122 return 0;
123 } else
124 return -ENODEV;
125}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148static int __init do_ne_probe(struct net_device *dev)
149{
150 unsigned int base_addr = dev->base_addr;
151
152
153 if (base_addr > 0x1ff)
154 return ne_probe1(dev, base_addr);
155 else if (base_addr != 0)
156 return -ENXIO;
157
158 return -ENODEV;
159}
160
161static void cleanup_card(struct net_device *dev)
162{
163 free_irq(dev->irq, dev);
164 release_region(dev->base_addr, NE_IO_EXTENT);
165}
166
167#ifndef MODULE
168struct net_device * __init ne_probe(int unit)
169{
170 struct net_device *dev = ____alloc_ei_netdev(0);
171 int err;
172
173 if (!dev)
174 return ERR_PTR(-ENOMEM);
175
176 if (init_dev(dev))
177 return ERR_PTR(-ENODEV);
178
179 sprintf(dev->name, "eth%d", unit);
180 netdev_boot_setup_check(dev);
181
182 err = init_reg_offset(dev, dev->base_addr);
183 if (err)
184 goto out;
185
186 err = do_ne_probe(dev);
187 if (err)
188 goto out;
189 return dev;
190out:
191 free_netdev(dev);
192 return ERR_PTR(err);
193}
194#endif
195
196static const struct net_device_ops ne_netdev_ops = {
197 .ndo_open = ne_open,
198 .ndo_stop = ne_close,
199
200 .ndo_start_xmit = __ei_start_xmit,
201 .ndo_tx_timeout = __ei_tx_timeout,
202 .ndo_get_stats = __ei_get_stats,
203 .ndo_set_multicast_list = __ei_set_multicast_list,
204 .ndo_validate_addr = eth_validate_addr,
205 .ndo_set_mac_address = eth_mac_addr,
206 .ndo_change_mtu = eth_change_mtu,
207#ifdef CONFIG_NET_POLL_CONTROLLER
208 .ndo_poll_controller = __ei_poll,
209#endif
210};
211
212static int __init ne_probe1(struct net_device *dev, int ioaddr)
213{
214 int i;
215 unsigned char SA_prom[16];
216 int wordlength = 2;
217 const char *name = NULL;
218 int start_page, stop_page;
219 int reg0, ret;
220 static unsigned version_printed;
221 struct ei_device *ei_local = netdev_priv(dev);
222 unsigned char bus_width;
223
224 if (!request_region(ioaddr, NE_IO_EXTENT, DRV_NAME))
225 return -EBUSY;
226
227 reg0 = inb_p(ioaddr);
228 if (reg0 == 0xFF) {
229 ret = -ENODEV;
230 goto err_out;
231 }
232
233
234 {
235 int regd;
236 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
237 regd = inb_p(ioaddr + EI_SHIFT(0x0d));
238 outb_p(0xff, ioaddr + EI_SHIFT(0x0d));
239 outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
240 inb_p(ioaddr + EN0_COUNTER0);
241 if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
242 outb_p(reg0, ioaddr + EI_SHIFT(0));
243 outb_p(regd, ioaddr + EI_SHIFT(0x0d));
244 ret = -ENODEV;
245 goto err_out;
246 }
247 }
248
249 if (ei_debug && version_printed++ == 0)
250 printk(KERN_INFO "%s", version1);
251
252 printk(KERN_INFO "NE*000 ethercard probe at %08x:", ioaddr);
253
254
255
256
257
258 {
259 struct {unsigned char value, offset; } program_seq[] =
260 {
261 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD},
262 {0x48, EN0_DCFG},
263 {0x00, EN0_RCNTLO},
264 {0x00, EN0_RCNTHI},
265 {0x00, EN0_IMR},
266 {0xFF, EN0_ISR},
267 {E8390_RXOFF, EN0_RXCR},
268 {E8390_TXOFF, EN0_TXCR},
269 {32, EN0_RCNTLO},
270 {0x00, EN0_RCNTHI},
271 {0x00, EN0_RSARLO},
272 {0x00, EN0_RSARHI},
273 {E8390_RREAD+E8390_START, E8390_CMD},
274 };
275
276 for (i = 0; i < ARRAY_SIZE(program_seq); i++)
277 outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
278
279 }
280 bus_width = *(volatile unsigned char *)ABWCR;
281 bus_width &= 1 << ((ioaddr >> 21) & 7);
282 ei_status.word16 = (bus_width == 0);
283 for(i = 0; i < 16 ; i++) {
284 SA_prom[i] = inb_p(ioaddr + NE_DATAPORT);
285 inb_p(ioaddr + NE_DATAPORT);
286 }
287
288 start_page = NESM_START_PG;
289 stop_page = NESM_STOP_PG;
290
291 if (bus_width)
292 wordlength = 1;
293 else
294 outb_p(0x49, ioaddr + EN0_DCFG);
295
296
297 name = (wordlength == 2) ? "NE2000" : "NE1000";
298
299 if (! dev->irq) {
300 printk(" failed to detect IRQ line.\n");
301 ret = -EAGAIN;
302 goto err_out;
303 }
304
305
306
307 ret = request_irq(dev->irq, __ei_interrupt, 0, name, dev);
308 if (ret) {
309 printk (" unable to get IRQ %d (errno=%d).\n", dev->irq, ret);
310 goto err_out;
311 }
312
313 dev->base_addr = ioaddr;
314
315 for(i = 0; i < ETHER_ADDR_LEN; i++)
316 dev->dev_addr[i] = SA_prom[i];
317 printk(" %pM\n", dev->dev_addr);
318
319 printk("%s: %s found at %#x, using IRQ %d.\n",
320 dev->name, name, ioaddr, dev->irq);
321
322 ei_status.name = name;
323 ei_status.tx_start_page = start_page;
324 ei_status.stop_page = stop_page;
325 ei_status.word16 = (wordlength == 2);
326
327 ei_status.rx_start_page = start_page + TX_PAGES;
328#ifdef PACKETBUF_MEMSIZE
329
330 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
331#endif
332
333 ei_status.reset_8390 = &ne_reset_8390;
334 ei_status.block_input = &ne_block_input;
335 ei_status.block_output = &ne_block_output;
336 ei_status.get_8390_hdr = &ne_get_8390_hdr;
337 ei_status.priv = 0;
338
339 dev->netdev_ops = &ne_netdev_ops;
340
341 __NS8390_init(dev, 0);
342
343 ret = register_netdev(dev);
344 if (ret)
345 goto out_irq;
346 return 0;
347out_irq:
348 free_irq(dev->irq, dev);
349err_out:
350 release_region(ioaddr, NE_IO_EXTENT);
351 return ret;
352}
353
354static int ne_open(struct net_device *dev)
355{
356 __ei_open(dev);
357 return 0;
358}
359
360static int ne_close(struct net_device *dev)
361{
362 if (ei_debug > 1)
363 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
364 __ei_close(dev);
365 return 0;
366}
367
368
369
370
371static void ne_reset_8390(struct net_device *dev)
372{
373 unsigned long reset_start_time = jiffies;
374 struct ei_device *ei_local = netdev_priv(dev);
375
376 if (ei_debug > 1)
377 printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
378
379
380 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
381
382 ei_status.txing = 0;
383 ei_status.dmaing = 0;
384
385
386 while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
387 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
388 printk(KERN_WARNING "%s: ne_reset_8390() did not complete.\n", dev->name);
389 break;
390 }
391 outb_p(ENISR_RESET, NE_BASE + EN0_ISR);
392}
393
394
395
396
397
398static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
399{
400 struct ei_device *ei_local = netdev_priv(dev);
401
402
403 if (ei_status.dmaing)
404 {
405 printk(KERN_EMERG "%s: DMAing conflict in ne_get_8390_hdr "
406 "[DMAstat:%d][irqlock:%d].\n",
407 dev->name, ei_status.dmaing, ei_status.irqlock);
408 return;
409 }
410
411 ei_status.dmaing |= 0x01;
412 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, NE_BASE + NE_CMD);
413 outb_p(sizeof(struct e8390_pkt_hdr), NE_BASE + EN0_RCNTLO);
414 outb_p(0, NE_BASE + EN0_RCNTHI);
415 outb_p(0, NE_BASE + EN0_RSARLO);
416 outb_p(ring_page, NE_BASE + EN0_RSARHI);
417 outb_p(E8390_RREAD+E8390_START, NE_BASE + NE_CMD);
418
419 if (ei_status.word16) {
420 int len;
421 unsigned short *p = (unsigned short *)hdr;
422 for (len = sizeof(struct e8390_pkt_hdr)>>1; len > 0; len--)
423 *p++ = inw(NE_BASE + NE_DATAPORT);
424 } else
425 insb(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
426
427 outb_p(ENISR_RDC, NE_BASE + EN0_ISR);
428 ei_status.dmaing &= ~0x01;
429
430 le16_to_cpus(&hdr->count);
431}
432
433
434
435
436
437
438static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
439{
440 struct ei_device *ei_local = netdev_priv(dev);
441#ifdef NE_SANITY_CHECK
442 int xfer_count = count;
443#endif
444 char *buf = skb->data;
445
446
447 if (ei_status.dmaing)
448 {
449 printk(KERN_EMERG "%s: DMAing conflict in ne_block_input "
450 "[DMAstat:%d][irqlock:%d].\n",
451 dev->name, ei_status.dmaing, ei_status.irqlock);
452 return;
453 }
454 ei_status.dmaing |= 0x01;
455 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, NE_BASE + NE_CMD);
456 outb_p(count & 0xff, NE_BASE + EN0_RCNTLO);
457 outb_p(count >> 8, NE_BASE + EN0_RCNTHI);
458 outb_p(ring_offset & 0xff, NE_BASE + EN0_RSARLO);
459 outb_p(ring_offset >> 8, NE_BASE + EN0_RSARHI);
460 outb_p(E8390_RREAD+E8390_START, NE_BASE + NE_CMD);
461 if (ei_status.word16)
462 {
463 int len;
464 unsigned short *p = (unsigned short *)buf;
465 for (len = count>>1; len > 0; len--)
466 *p++ = inw(NE_BASE + NE_DATAPORT);
467 if (count & 0x01)
468 {
469 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
470#ifdef NE_SANITY_CHECK
471 xfer_count++;
472#endif
473 }
474 } else {
475 insb(NE_BASE + NE_DATAPORT, buf, count);
476 }
477
478#ifdef NE_SANITY_CHECK
479
480
481
482
483
484 if (ei_debug > 1)
485 {
486
487 int addr, tries = 20;
488 do {
489
490
491 int high = inb_p(NE_BASE + EN0_RSARHI);
492 int low = inb_p(NE_BASE + EN0_RSARLO);
493 addr = (high << 8) + low;
494 if (((ring_offset + xfer_count) & 0xff) == low)
495 break;
496 } while (--tries > 0);
497 if (tries <= 0)
498 printk(KERN_WARNING "%s: RX transfer address mismatch,"
499 "%#4.4x (expected) vs. %#4.4x (actual).\n",
500 dev->name, ring_offset + xfer_count, addr);
501 }
502#endif
503 outb_p(ENISR_RDC, NE_BASE + EN0_ISR);
504 ei_status.dmaing &= ~0x01;
505}
506
507static void ne_block_output(struct net_device *dev, int count,
508 const unsigned char *buf, const int start_page)
509{
510 struct ei_device *ei_local = netdev_priv(dev);
511 unsigned long dma_start;
512#ifdef NE_SANITY_CHECK
513 int retries = 0;
514#endif
515
516
517
518
519
520 if (ei_status.word16 && (count & 0x01))
521 count++;
522
523
524 if (ei_status.dmaing)
525 {
526 printk(KERN_EMERG "%s: DMAing conflict in ne_block_output."
527 "[DMAstat:%d][irqlock:%d]\n",
528 dev->name, ei_status.dmaing, ei_status.irqlock);
529 return;
530 }
531 ei_status.dmaing |= 0x01;
532
533 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, NE_BASE + NE_CMD);
534
535#ifdef NE_SANITY_CHECK
536retry:
537#endif
538
539#ifdef NE8390_RW_BUGFIX
540
541
542
543
544
545 outb_p(0x42, NE_BASE + EN0_RCNTLO);
546 outb_p(0x00, NE_BASE + EN0_RCNTHI);
547 outb_p(0x42, NE_BASE + EN0_RSARLO);
548 outb_p(0x00, NE_BASE + EN0_RSARHI);
549 outb_p(E8390_RREAD+E8390_START, NE_BASE + NE_CMD);
550
551 udelay(6);
552#endif
553
554 outb_p(ENISR_RDC, NE_BASE + EN0_ISR);
555
556
557 outb_p(count & 0xff, NE_BASE + EN0_RCNTLO);
558 outb_p(count >> 8, NE_BASE + EN0_RCNTHI);
559 outb_p(0x00, NE_BASE + EN0_RSARLO);
560 outb_p(start_page, NE_BASE + EN0_RSARHI);
561
562 outb_p(E8390_RWRITE+E8390_START, NE_BASE + NE_CMD);
563 if (ei_status.word16) {
564 int len;
565 unsigned short *p = (unsigned short *)buf;
566 for (len = count>>1; len > 0; len--)
567 outw(*p++, NE_BASE + NE_DATAPORT);
568 } else {
569 outsb(NE_BASE + NE_DATAPORT, buf, count);
570 }
571
572 dma_start = jiffies;
573
574#ifdef NE_SANITY_CHECK
575
576
577
578 if (ei_debug > 1)
579 {
580
581 int addr, tries = 20;
582 do {
583 int high = inb_p(NE_BASE + EN0_RSARHI);
584 int low = inb_p(NE_BASE + EN0_RSARLO);
585 addr = (high << 8) + low;
586 if ((start_page << 8) + count == addr)
587 break;
588 } while (--tries > 0);
589
590 if (tries <= 0)
591 {
592 printk(KERN_WARNING "%s: Tx packet transfer address mismatch,"
593 "%#4.4x (expected) vs. %#4.4x (actual).\n",
594 dev->name, (start_page << 8) + count, addr);
595 if (retries++ == 0)
596 goto retry;
597 }
598 }
599#endif
600
601 while ((inb_p(NE_BASE + EN0_ISR) & ENISR_RDC) == 0)
602 if (time_after(jiffies, dma_start + 2*HZ/100)) {
603 printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
604 ne_reset_8390(dev);
605 __NS8390_init(dev,1);
606 break;
607 }
608
609 outb_p(ENISR_RDC, NE_BASE + EN0_ISR);
610 ei_status.dmaing &= ~0x01;
611}
612
613
614#ifdef MODULE
615#define MAX_NE_CARDS 1
616static struct net_device *dev_ne[MAX_NE_CARDS];
617static int io[MAX_NE_CARDS];
618static int irq[MAX_NE_CARDS];
619static int bad[MAX_NE_CARDS];
620
621module_param_array(io, int, NULL, 0);
622module_param_array(irq, int, NULL, 0);
623module_param_array(bad, int, NULL, 0);
624MODULE_PARM_DESC(io, "I/O base address(es)");
625MODULE_PARM_DESC(irq, "IRQ number(s)");
626MODULE_DESCRIPTION("H8/300 NE2000 Ethernet driver");
627MODULE_LICENSE("GPL");
628
629
630
631
632
633
634int init_module(void)
635{
636 int this_dev, found = 0;
637 int err;
638
639 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
640 struct net_device *dev = ____alloc_ei_netdev(0);
641 if (!dev)
642 break;
643 if (io[this_dev]) {
644 dev->irq = irq[this_dev];
645 dev->mem_end = bad[this_dev];
646 dev->base_addr = io[this_dev];
647 } else {
648 dev->base_addr = h8300_ne_base[this_dev];
649 dev->irq = h8300_ne_irq[this_dev];
650 }
651 err = init_reg_offset(dev, dev->base_addr);
652 if (!err) {
653 if (do_ne_probe(dev) == 0) {
654 dev_ne[found++] = dev;
655 continue;
656 }
657 }
658 free_netdev(dev);
659 if (found)
660 break;
661 if (io[this_dev] != 0)
662 printk(KERN_WARNING "ne.c: No NE*000 card found at i/o = %#x\n", dev->base_addr);
663 else
664 printk(KERN_NOTICE "ne.c: You must supply \"io=0xNNN\" value(s) for ISA cards.\n");
665 return -ENXIO;
666 }
667 if (found)
668 return 0;
669 return -ENODEV;
670}
671
672void cleanup_module(void)
673{
674 int this_dev;
675
676 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
677 struct net_device *dev = dev_ne[this_dev];
678 if (dev) {
679 unregister_netdev(dev);
680 cleanup_card(dev);
681 free_netdev(dev);
682 }
683 }
684}
685#endif
686