File: /usr/src/linux/drivers/acorn/net/etherh.c
1 /*
2 * linux/drivers/acorn/net/etherh.c
3 *
4 * Copyright (C) 2000 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * NS8390 I-cubed EtherH and ANT EtherM specific driver
11 * Thanks to I-Cubed for information on their cards.
12 * EtherM conversion (C) 1999 Chris Kemp and Tim Watterton
13 * EtherM integration (C) 2000 Aleph One Ltd (Tak-Shing Chan)
14 * EtherM integration re-engineered by Russell King.
15 *
16 * Changelog:
17 * 08-12-1996 RMK 1.00 Created
18 * RMK 1.03 Added support for EtherLan500 cards
19 * 23-11-1997 RMK 1.04 Added media autodetection
20 * 16-04-1998 RMK 1.05 Improved media autodetection
21 * 10-02-2000 RMK 1.06 Updated for 2.3.43
22 * 13-05-2000 RMK 1.07 Updated for 2.3.99-pre8
23 * 12-10-1999 CK/TEW EtherM driver first release
24 * 21-12-2000 TTC EtherH/EtherM integration
25 * 25-12-2000 RMK 1.08 Clean integration of EtherM into this driver.
26 */
27
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/types.h>
32 #include <linux/fcntl.h>
33 #include <linux/interrupt.h>
34 #include <linux/ptrace.h>
35 #include <linux/ioport.h>
36 #include <linux/in.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/skbuff.h>
43 #include <linux/delay.h>
44 #include <linux/init.h>
45
46 #include <asm/system.h>
47 #include <asm/bitops.h>
48 #include <asm/ecard.h>
49 #include <asm/io.h>
50 #include <asm/irq.h>
51
52 #include "../../net/8390.h"
53
54 #define NET_DEBUG 0
55 #define DEBUG_INIT 2
56
57 static unsigned int net_debug = NET_DEBUG;
58
59 static const card_ids __init etherh_cids[] = {
60 { MANU_ANT, PROD_ANT_ETHERM },
61 { MANU_I3, PROD_I3_ETHERLAN500 },
62 { MANU_I3, PROD_I3_ETHERLAN600 },
63 { MANU_I3, PROD_I3_ETHERLAN600A },
64 { 0xffff, 0xffff }
65 };
66
67
68 MODULE_AUTHOR("Russell King");
69 MODULE_DESCRIPTION("EtherH/EtherM driver");
70 MODULE_LICENSE("GPL");
71
72 static char version[] __initdata =
73 "EtherH/EtherM Driver (c) 2000 Russell King v1.08\n";
74
75 #define ETHERH500_DATAPORT 0x200 /* MEMC */
76 #define ETHERH500_NS8390 0x000 /* MEMC */
77 #define ETHERH500_CTRLPORT 0x200 /* IOC */
78
79 #define ETHERH600_DATAPORT 16 /* MEMC */
80 #define ETHERH600_NS8390 0x200 /* MEMC */
81 #define ETHERH600_CTRLPORT 0x080 /* MEMC */
82
83 #define ETHERH_CP_IE 1
84 #define ETHERH_CP_IF 2
85 #define ETHERH_CP_HEARTBEAT 2
86
87 #define ETHERH_TX_START_PAGE 1
88 #define ETHERH_STOP_PAGE 127
89
90 /*
91 * These came from CK/TEW
92 */
93 #define ETHERM_DATAPORT 0x080 /* MEMC */
94 #define ETHERM_NS8390 0x200 /* MEMC */
95 #define ETHERM_CTRLPORT 0x08f /* MEMC */
96
97 #define ETHERM_TX_START_PAGE 64
98 #define ETHERM_STOP_PAGE 127
99
100 /* --------------------------------------------------------------------------- */
101
102 static void
103 etherh_setif(struct net_device *dev)
104 {
105 struct ei_device *ei_local = (struct ei_device *) dev->priv;
106 unsigned long addr, flags;
107
108 save_flags_cli(flags);
109
110 /* set the interface type */
111 switch (dev->mem_end) {
112 case PROD_I3_ETHERLAN600:
113 case PROD_I3_ETHERLAN600A:
114 addr = dev->base_addr + EN0_RCNTHI;
115
116 switch (dev->if_port) {
117 case IF_PORT_10BASE2:
118 outb((inb(addr) & 0xf8) | 1, addr);
119 break;
120 case IF_PORT_10BASET:
121 outb((inb(addr) & 0xf8), addr);
122 break;
123 }
124 break;
125
126 case PROD_I3_ETHERLAN500:
127 addr = dev->rmem_start;
128
129 switch (dev->if_port) {
130 case IF_PORT_10BASE2:
131 outb(inb(addr) & ~ETHERH_CP_IF, addr);
132 break;
133 case IF_PORT_10BASET:
134 outb(inb(addr) | ETHERH_CP_IF, addr);
135 break;
136 }
137 break;
138
139 default:
140 break;
141 }
142
143 restore_flags(flags);
144 }
145
146 static int
147 etherh_getifstat(struct net_device *dev)
148 {
149 struct ei_device *ei_local = (struct ei_device *) dev->priv;
150 int stat = 0;
151
152 switch (dev->mem_end) {
153 case PROD_I3_ETHERLAN600:
154 case PROD_I3_ETHERLAN600A:
155 switch (dev->if_port) {
156 case IF_PORT_10BASE2:
157 stat = 1;
158 break;
159 case IF_PORT_10BASET:
160 stat = inb(dev->base_addr+EN0_RCNTHI) & 4;
161 break;
162 }
163 break;
164
165 case PROD_I3_ETHERLAN500:
166 switch (dev->if_port) {
167 case IF_PORT_10BASE2:
168 stat = 1;
169 break;
170 case IF_PORT_10BASET:
171 stat = inb(dev->rmem_start) & ETHERH_CP_HEARTBEAT;
172 break;
173 }
174 break;
175
176 default:
177 stat = 0;
178 break;
179 }
180
181 return stat != 0;
182 }
183
184 /*
185 * Configure the interface. Note that we ignore the other
186 * parts of ifmap, since its mostly meaningless for this driver.
187 */
188 static int etherh_set_config(struct net_device *dev, struct ifmap *map)
189 {
190 switch (map->port) {
191 case IF_PORT_10BASE2:
192 case IF_PORT_10BASET:
193 /*
194 * If the user explicitly sets the interface
195 * media type, turn off automedia detection.
196 */
197 dev->flags &= ~IFF_AUTOMEDIA;
198 dev->if_port = map->port;
199 break;
200
201 default:
202 return -EINVAL;
203 }
204
205 etherh_setif(dev);
206
207 return 0;
208 }
209
210 /*
211 * Reset the 8390 (hard reset). Note that we can't actually do this.
212 */
213 static void
214 etherh_reset(struct net_device *dev)
215 {
216 struct ei_device *ei_local = (struct ei_device *) dev->priv;
217
218 outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, dev->base_addr);
219
220 /*
221 * See if we need to change the interface type.
222 * Note that we use 'interface_num' as a flag
223 * to indicate that we need to change the media.
224 */
225 if (dev->flags & IFF_AUTOMEDIA && ei_local->interface_num) {
226 ei_local->interface_num = 0;
227
228 if (dev->if_port == IF_PORT_10BASET)
229 dev->if_port = IF_PORT_10BASE2;
230 else
231 dev->if_port = IF_PORT_10BASET;
232
233 etherh_setif(dev);
234 }
235 }
236
237 /*
238 * Write a block of data out to the 8390
239 */
240 static void
241 etherh_block_output (struct net_device *dev, int count, const unsigned char *buf, int start_page)
242 {
243 struct ei_device *ei_local = (struct ei_device *) dev->priv;
244 unsigned int addr, dma_addr;
245 unsigned long dma_start;
246
247 if (ei_local->dmaing) {
248 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
249 " DMAstat %d irqlock %d\n", dev->name,
250 ei_local->dmaing, ei_local->irqlock);
251 return;
252 }
253
254 ei_local->dmaing |= 1;
255
256 addr = dev->base_addr;
257 dma_addr = dev->mem_start;
258
259 count = (count + 1) & ~1;
260 outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
261
262 outb (0x42, addr + EN0_RCNTLO);
263 outb (0x00, addr + EN0_RCNTHI);
264 outb (0x42, addr + EN0_RSARLO);
265 outb (0x00, addr + EN0_RSARHI);
266 outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
267
268 udelay (1);
269
270 outb (ENISR_RDC, addr + EN0_ISR);
271 outb (count, addr + EN0_RCNTLO);
272 outb (count >> 8, addr + EN0_RCNTHI);
273 outb (0, addr + EN0_RSARLO);
274 outb (start_page, addr + EN0_RSARHI);
275 outb (E8390_RWRITE | E8390_START, addr + E8390_CMD);
276
277 if (ei_local->word16)
278 outsw (dma_addr, buf, count >> 1);
279 else
280 outsb (dma_addr, buf, count);
281
282 dma_start = jiffies;
283
284 while ((inb (addr + EN0_ISR) & ENISR_RDC) == 0)
285 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
286 printk(KERN_ERR "%s: timeout waiting for TX RDC\n",
287 dev->name);
288 etherh_reset (dev);
289 NS8390_init (dev, 1);
290 break;
291 }
292
293 outb (ENISR_RDC, addr + EN0_ISR);
294 ei_local->dmaing &= ~1;
295 }
296
297 /*
298 * Read a block of data from the 8390
299 */
300 static void
301 etherh_block_input (struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
302 {
303 struct ei_device *ei_local = (struct ei_device *) dev->priv;
304 unsigned int addr, dma_addr;
305 unsigned char *buf;
306
307 if (ei_local->dmaing) {
308 printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
309 " DMAstat %d irqlock %d\n", dev->name,
310 ei_local->dmaing, ei_local->irqlock);
311 return;
312 }
313
314 ei_local->dmaing |= 1;
315
316 addr = dev->base_addr;
317 dma_addr = dev->mem_start;
318
319 buf = skb->data;
320 outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
321 outb (count, addr + EN0_RCNTLO);
322 outb (count >> 8, addr + EN0_RCNTHI);
323 outb (ring_offset, addr + EN0_RSARLO);
324 outb (ring_offset >> 8, addr + EN0_RSARHI);
325 outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
326
327 if (ei_local->word16) {
328 insw (dma_addr, buf, count >> 1);
329 if (count & 1)
330 buf[count - 1] = inb (dma_addr);
331 } else
332 insb (dma_addr, buf, count);
333
334 outb (ENISR_RDC, addr + EN0_ISR);
335 ei_local->dmaing &= ~1;
336 }
337
338 /*
339 * Read a header from the 8390
340 */
341 static void
342 etherh_get_header (struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
343 {
344 struct ei_device *ei_local = (struct ei_device *) dev->priv;
345 unsigned int addr, dma_addr;
346
347 if (ei_local->dmaing) {
348 printk(KERN_ERR "%s: DMAing conflict in etherh_get_header: "
349 " DMAstat %d irqlock %d\n", dev->name,
350 ei_local->dmaing, ei_local->irqlock);
351 return;
352 }
353
354 ei_local->dmaing |= 1;
355
356 addr = dev->base_addr;
357 dma_addr = dev->mem_start;
358
359 outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
360 outb (sizeof (*hdr), addr + EN0_RCNTLO);
361 outb (0, addr + EN0_RCNTHI);
362 outb (0, addr + EN0_RSARLO);
363 outb (ring_page, addr + EN0_RSARHI);
364 outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
365
366 if (ei_local->word16)
367 insw (dma_addr, hdr, sizeof (*hdr) >> 1);
368 else
369 insb (dma_addr, hdr, sizeof (*hdr));
370
371 outb (ENISR_RDC, addr + EN0_ISR);
372 ei_local->dmaing &= ~1;
373 }
374
375 /*
376 * Open/initialize the board. This is called (in the current kernel)
377 * sometime after booting when the 'ifconfig' program is run.
378 *
379 * This routine should set everything up anew at each open, even
380 * registers that "should" only need to be set once at boot, so that
381 * there is non-reboot way to recover if something goes wrong.
382 */
383 static int
384 etherh_open(struct net_device *dev)
385 {
386 struct ei_device *ei_local = (struct ei_device *) dev->priv;
387
388 if (request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))
389 return -EAGAIN;
390
391 /*
392 * Make sure that we aren't going to change the
393 * media type on the next reset - we are about to
394 * do automedia manually now.
395 */
396 ei_local->interface_num = 0;
397
398 /*
399 * If we are doing automedia detection, do it now.
400 * This is more reliable than the 8390's detection.
401 */
402 if (dev->flags & IFF_AUTOMEDIA) {
403 dev->if_port = IF_PORT_10BASET;
404 etherh_setif(dev);
405 mdelay(1);
406 if (!etherh_getifstat(dev)) {
407 dev->if_port = IF_PORT_10BASE2;
408 etherh_setif(dev);
409 }
410 } else
411 etherh_setif(dev);
412
413 etherh_reset(dev);
414 ei_open(dev);
415
416 return 0;
417 }
418
419 /*
420 * The inverse routine to etherh_open().
421 */
422 static int
423 etherh_close(struct net_device *dev)
424 {
425 ei_close (dev);
426 free_irq (dev->irq, dev);
427 return 0;
428 }
429
430 static void etherh_irq_enable(ecard_t *ec, int irqnr)
431 {
432 unsigned int ctrl_addr = (unsigned int)ec->irq_data;
433 outb(inb(ctrl_addr) | ETHERH_CP_IE, ctrl_addr);
434 }
435
436 static void etherh_irq_disable(ecard_t *ec, int irqnr)
437 {
438 unsigned int ctrl_addr = (unsigned int)ec->irq_data;
439 outb(inb(ctrl_addr) & ~ETHERH_CP_IE, ctrl_addr);
440 }
441
442 static expansioncard_ops_t etherh_ops = {
443 irqenable: etherh_irq_enable,
444 irqdisable: etherh_irq_disable,
445 };
446
447 /*
448 * Initialisation
449 */
450
451 static void __init etherh_banner(void)
452 {
453 static int version_printed;
454
455 if (net_debug && version_printed++ == 0)
456 printk(KERN_INFO "%s", version);
457 }
458
459 /*
460 * Read the ethernet address string from the on board rom.
461 * This is an ascii string...
462 */
463 static int __init etherh_addr(char *addr, struct expansion_card *ec)
464 {
465 struct in_chunk_dir cd;
466 char *s;
467
468 if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
469 int i;
470 for (i = 0; i < 6; i++) {
471 addr[i] = simple_strtoul(s + 1, &s, 0x10);
472 if (*s != (i == 5? ')' : ':'))
473 break;
474 }
475 if (i == 6)
476 return 0;
477 }
478 return ENODEV;
479 }
480
481 /*
482 * Create an ethernet address from the system serial number.
483 */
484 static int __init etherm_addr(char *addr)
485 {
486 unsigned int serial;
487
488 if (system_serial_low == 0 && system_serial_high == 0)
489 return ENODEV;
490
491 serial = system_serial_low | system_serial_high;
492
493 addr[0] = 0;
494 addr[1] = 0;
495 addr[2] = 0xa4;
496 addr[3] = 0x10 + (serial >> 24);
497 addr[4] = serial >> 16;
498 addr[5] = serial >> 8;
499 return 0;
500 }
501
502 static u32 etherh_regoffsets[16];
503 static u32 etherm_regoffsets[16];
504
505 static struct net_device * __init etherh_init_one(struct expansion_card *ec)
506 {
507 struct ei_device *ei_local;
508 struct net_device *dev;
509 const char *dev_type;
510 int i, size;
511
512 etherh_banner();
513
514 ecard_claim(ec);
515
516 dev = init_etherdev(NULL, 0);
517 if (!dev)
518 goto out;
519
520 SET_MODULE_OWNER(dev);
521
522 dev->open = etherh_open;
523 dev->stop = etherh_close;
524 dev->set_config = etherh_set_config;
525 dev->irq = ec->irq;
526 dev->base_addr = ecard_address(ec, ECARD_MEMC, 0);
527 dev->mem_end = ec->cid.product;
528 ec->ops = ðerh_ops;
529
530 switch (ec->cid.product) {
531 case PROD_ANT_ETHERM:
532 if (etherm_addr(dev->dev_addr))
533 goto free;
534 dev->base_addr += ETHERM_NS8390;
535 dev->mem_start = dev->base_addr + ETHERM_DATAPORT;
536 ec->irq_data = (void *)(dev->base_addr + ETHERM_CTRLPORT);
537 break;
538
539 case PROD_I3_ETHERLAN500:
540 if (etherh_addr(dev->dev_addr, ec))
541 goto free;
542 dev->base_addr += ETHERH500_NS8390;
543 dev->mem_start = dev->base_addr + ETHERH500_DATAPORT;
544 dev->rmem_start = (unsigned long)
545 ec->irq_data = (void *)ecard_address (ec, ECARD_IOC, ECARD_FAST)
546 + ETHERH500_CTRLPORT;
547 break;
548
549 case PROD_I3_ETHERLAN600:
550 case PROD_I3_ETHERLAN600A:
551 if (etherh_addr(dev->dev_addr, ec))
552 goto free;
553 dev->base_addr += ETHERH600_NS8390;
554 dev->mem_start = dev->base_addr + ETHERH600_DATAPORT;
555 ec->irq_data = (void *)(dev->base_addr + ETHERH600_CTRLPORT);
556 break;
557
558 default:
559 printk(KERN_ERR "%s: unknown card type %x\n",
560 dev->name, ec->cid.product);
561 goto free;
562 }
563
564 size = 16;
565 if (ec->cid.product == PROD_ANT_ETHERM)
566 size <<= 3;
567
568 if (!request_region(dev->base_addr, size, dev->name))
569 goto free;
570
571 if (ethdev_init(dev))
572 goto release;
573
574 /*
575 * Unfortunately, ethdev_init eventually calls
576 * ether_setup, which re-writes dev->flags.
577 */
578 switch (ec->cid.product) {
579 case PROD_ANT_ETHERM:
580 dev_type = "ANT EtherM";
581 dev->if_port = IF_PORT_UNKNOWN;
582 break;
583
584 case PROD_I3_ETHERLAN500:
585 dev_type = "i3 EtherH 500";
586 dev->if_port = IF_PORT_UNKNOWN;
587 break;
588
589 case PROD_I3_ETHERLAN600:
590 dev_type = "i3 EtherH 600";
591 dev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
592 dev->if_port = IF_PORT_10BASET;
593 break;
594
595 case PROD_I3_ETHERLAN600A:
596 dev_type = "i3 EtherH 600A";
597 dev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
598 dev->if_port = IF_PORT_10BASET;
599 break;
600
601 default:
602 dev_type = "unknown";
603 break;
604 }
605
606 printk(KERN_INFO "%s: %s in slot %d, ",
607 dev->name, dev_type, ec->slot_no);
608
609 for (i = 0; i < 6; i++)
610 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
611
612 ei_local = (struct ei_device *) dev->priv;
613 if (ec->cid.product == PROD_ANT_ETHERM) {
614 ei_local->tx_start_page = ETHERM_TX_START_PAGE;
615 ei_local->stop_page = ETHERM_STOP_PAGE;
616 ei_local->reg_offset = etherm_regoffsets;
617 } else {
618 ei_local->tx_start_page = ETHERH_TX_START_PAGE;
619 ei_local->stop_page = ETHERH_STOP_PAGE;
620 ei_local->reg_offset = etherh_regoffsets;
621 }
622
623 ei_local->name = dev->name;
624 ei_local->word16 = 1;
625 ei_local->rx_start_page = ei_local->tx_start_page + TX_PAGES;
626 ei_local->reset_8390 = etherh_reset;
627 ei_local->block_input = etherh_block_input;
628 ei_local->block_output = etherh_block_output;
629 ei_local->get_8390_hdr = etherh_get_header;
630 ei_local->interface_num = 0;
631
632 etherh_reset(dev);
633 NS8390_init(dev, 0);
634 return dev;
635
636 release:
637 release_region(dev->base_addr, 16);
638 free:
639 unregister_netdev(dev);
640 kfree(dev);
641 out:
642 ecard_release(ec);
643 return NULL;
644 }
645
646 #define MAX_ETHERH_CARDS 2
647
648 static struct net_device *e_dev[MAX_ETHERH_CARDS];
649 static struct expansion_card *e_card[MAX_ETHERH_CARDS];
650
651 static int __init etherh_init(void)
652 {
653 int i, ret = -ENODEV;
654
655 for (i = 0; i < 16; i++) {
656 etherh_regoffsets[i] = i;
657 etherm_regoffsets[i] = i << 3;
658 }
659
660 ecard_startfind();
661
662 for (i = 0; i < MAX_ECARDS; i++) {
663 struct expansion_card *ec;
664 struct net_device *dev;
665
666 ec = ecard_find(0, etherh_cids);
667 if (!ec)
668 break;
669
670 dev = etherh_init_one(ec);
671 if (!dev)
672 break;
673
674 e_card[i] = ec;
675 e_dev[i] = dev;
676 ret = 0;
677 }
678
679 return ret;
680 }
681
682 static void __exit etherh_exit(void)
683 {
684 int i;
685
686 for (i = 0; i < MAX_ETHERH_CARDS; i++) {
687 if (e_dev[i]) {
688 int size;
689 unregister_netdev(e_dev[i]);
690 size = 16;
691 if (e_card[i]->cid.product == PROD_ANT_ETHERM)
692 size <<= 3;
693 release_region(e_dev[i]->base_addr, size);
694 kfree(e_dev[i]);
695 e_dev[i] = NULL;
696 }
697 if (e_card[i]) {
698 e_card[i]->ops = NULL;
699 ecard_release(e_card[i]);
700 e_card[i] = NULL;
701 }
702 }
703 }
704
705 module_init(etherh_init);
706 module_exit(etherh_exit);
707