Loading...
1/* mac8390.c: New driver for 8390-based Nubus (or Nubus-alike)
2 Ethernet cards on Linux */
3/* Based on the former daynaport.c driver, by Alan Cox. Some code
4 taken from or inspired by skeleton.c by Donald Becker, acenic.c by
5 Jes Sorensen, and ne2k-pci.c by Donald Becker and Paul Gortmaker.
6
7 This software may be used and distributed according to the terms of
8 the GNU Public License, incorporated herein by reference. */
9
10/* 2000-02-28: support added for Dayna and Kinetics cards by
11 A.G.deWijn@phys.uu.nl */
12/* 2000-04-04: support added for Dayna2 by bart@etpmod.phys.tue.nl */
13/* 2001-04-18: support for DaynaPort E/LC-M by rayk@knightsmanor.org */
14/* 2001-05-15: support for Cabletron ported from old daynaport driver
15 * and fixed access to Sonic Sys card which masquerades as a Farallon
16 * by rayk@knightsmanor.org */
17/* 2002-12-30: Try to support more cards, some clues from NetBSD driver */
18/* 2003-12-26: Make sure Asante cards always work. */
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/types.h>
25#include <linux/fcntl.h>
26#include <linux/interrupt.h>
27#include <linux/ptrace.h>
28#include <linux/ioport.h>
29#include <linux/nubus.h>
30#include <linux/in.h>
31#include <linux/string.h>
32#include <linux/errno.h>
33#include <linux/init.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
37#include <linux/bitops.h>
38#include <linux/io.h>
39
40#include <asm/dma.h>
41#include <asm/hwtest.h>
42#include <asm/macints.h>
43
44static char version[] =
45 "v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
46
47#define EI_SHIFT(x) (ei_local->reg_offset[x])
48#define ei_inb(port) in_8(port)
49#define ei_outb(val, port) out_8(port, val)
50#define ei_inb_p(port) in_8(port)
51#define ei_outb_p(val, port) out_8(port, val)
52
53#include "lib8390.c"
54
55#define WD_START_PG 0x00 /* First page of TX buffer */
56#define CABLETRON_RX_START_PG 0x00 /* First page of RX buffer */
57#define CABLETRON_RX_STOP_PG 0x30 /* Last page +1 of RX ring */
58#define CABLETRON_TX_START_PG CABLETRON_RX_STOP_PG
59 /* First page of TX buffer */
60
61/*
62 * Unfortunately it seems we have to hardcode these for the moment
63 * Shouldn't the card know about this?
64 * Does anyone know where to read it off the card?
65 * Do we trust the data provided by the card?
66 */
67
68#define DAYNA_8390_BASE 0x80000
69#define DAYNA_8390_MEM 0x00000
70
71#define CABLETRON_8390_BASE 0x90000
72#define CABLETRON_8390_MEM 0x00000
73
74#define INTERLAN_8390_BASE 0xE0000
75#define INTERLAN_8390_MEM 0xD0000
76
77enum mac8390_type {
78 MAC8390_NONE = -1,
79 MAC8390_APPLE,
80 MAC8390_ASANTE,
81 MAC8390_FARALLON,
82 MAC8390_CABLETRON,
83 MAC8390_DAYNA,
84 MAC8390_INTERLAN,
85 MAC8390_KINETICS,
86};
87
88static const char *cardname[] = {
89 "apple",
90 "asante",
91 "farallon",
92 "cabletron",
93 "dayna",
94 "interlan",
95 "kinetics",
96};
97
98static const int word16[] = {
99 1, /* apple */
100 1, /* asante */
101 1, /* farallon */
102 1, /* cabletron */
103 0, /* dayna */
104 1, /* interlan */
105 0, /* kinetics */
106};
107
108/* on which cards do we use NuBus resources? */
109static const int useresources[] = {
110 1, /* apple */
111 1, /* asante */
112 1, /* farallon */
113 0, /* cabletron */
114 0, /* dayna */
115 0, /* interlan */
116 0, /* kinetics */
117};
118
119enum mac8390_access {
120 ACCESS_UNKNOWN = 0,
121 ACCESS_32,
122 ACCESS_16,
123};
124
125extern int mac8390_memtest(struct net_device *dev);
126static int mac8390_initdev(struct net_device *dev, struct nubus_dev *ndev,
127 enum mac8390_type type);
128
129static int mac8390_open(struct net_device *dev);
130static int mac8390_close(struct net_device *dev);
131static void mac8390_no_reset(struct net_device *dev);
132static void interlan_reset(struct net_device *dev);
133
134/* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
135static void sane_get_8390_hdr(struct net_device *dev,
136 struct e8390_pkt_hdr *hdr, int ring_page);
137static void sane_block_input(struct net_device *dev, int count,
138 struct sk_buff *skb, int ring_offset);
139static void sane_block_output(struct net_device *dev, int count,
140 const unsigned char *buf, const int start_page);
141
142/* dayna_memcpy to and from card */
143static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
144 int from, int count);
145static void dayna_memcpy_tocard(struct net_device *dev, int to,
146 const void *from, int count);
147
148/* Dayna - Dayna/Kinetics use this */
149static void dayna_get_8390_hdr(struct net_device *dev,
150 struct e8390_pkt_hdr *hdr, int ring_page);
151static void dayna_block_input(struct net_device *dev, int count,
152 struct sk_buff *skb, int ring_offset);
153static void dayna_block_output(struct net_device *dev, int count,
154 const unsigned char *buf, int start_page);
155
156#define memcpy_fromio(a, b, c) memcpy((a), (void *)(b), (c))
157#define memcpy_toio(a, b, c) memcpy((void *)(a), (b), (c))
158
159#define memcmp_withio(a, b, c) memcmp((a), (void *)(b), (c))
160
161/* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
162static void slow_sane_get_8390_hdr(struct net_device *dev,
163 struct e8390_pkt_hdr *hdr, int ring_page);
164static void slow_sane_block_input(struct net_device *dev, int count,
165 struct sk_buff *skb, int ring_offset);
166static void slow_sane_block_output(struct net_device *dev, int count,
167 const unsigned char *buf, int start_page);
168static void word_memcpy_tocard(unsigned long tp, const void *fp, int count);
169static void word_memcpy_fromcard(void *tp, unsigned long fp, int count);
170static u32 mac8390_msg_enable;
171
172static enum mac8390_type __init mac8390_ident(struct nubus_dev *dev)
173{
174 switch (dev->dr_sw) {
175 case NUBUS_DRSW_3COM:
176 switch (dev->dr_hw) {
177 case NUBUS_DRHW_APPLE_SONIC_NB:
178 case NUBUS_DRHW_APPLE_SONIC_LC:
179 case NUBUS_DRHW_SONNET:
180 return MAC8390_NONE;
181 break;
182 default:
183 return MAC8390_APPLE;
184 break;
185 }
186 break;
187
188 case NUBUS_DRSW_APPLE:
189 switch (dev->dr_hw) {
190 case NUBUS_DRHW_ASANTE_LC:
191 return MAC8390_NONE;
192 break;
193 case NUBUS_DRHW_CABLETRON:
194 return MAC8390_CABLETRON;
195 break;
196 default:
197 return MAC8390_APPLE;
198 break;
199 }
200 break;
201
202 case NUBUS_DRSW_ASANTE:
203 return MAC8390_ASANTE;
204 break;
205
206 case NUBUS_DRSW_TECHWORKS:
207 case NUBUS_DRSW_DAYNA2:
208 case NUBUS_DRSW_DAYNA_LC:
209 if (dev->dr_hw == NUBUS_DRHW_CABLETRON)
210 return MAC8390_CABLETRON;
211 else
212 return MAC8390_APPLE;
213 break;
214
215 case NUBUS_DRSW_FARALLON:
216 return MAC8390_FARALLON;
217 break;
218
219 case NUBUS_DRSW_KINETICS:
220 switch (dev->dr_hw) {
221 case NUBUS_DRHW_INTERLAN:
222 return MAC8390_INTERLAN;
223 break;
224 default:
225 return MAC8390_KINETICS;
226 break;
227 }
228 break;
229
230 case NUBUS_DRSW_DAYNA:
231 /*
232 * These correspond to Dayna Sonic cards
233 * which use the macsonic driver
234 */
235 if (dev->dr_hw == NUBUS_DRHW_SMC9194 ||
236 dev->dr_hw == NUBUS_DRHW_INTERLAN)
237 return MAC8390_NONE;
238 else
239 return MAC8390_DAYNA;
240 break;
241 }
242 return MAC8390_NONE;
243}
244
245static enum mac8390_access __init mac8390_testio(volatile unsigned long membase)
246{
247 unsigned long outdata = 0xA5A0B5B0;
248 unsigned long indata = 0x00000000;
249 /* Try writing 32 bits */
250 memcpy_toio(membase, &outdata, 4);
251 /* Now compare them */
252 if (memcmp_withio(&outdata, membase, 4) == 0)
253 return ACCESS_32;
254 /* Write 16 bit output */
255 word_memcpy_tocard(membase, &outdata, 4);
256 /* Now read it back */
257 word_memcpy_fromcard(&indata, membase, 4);
258 if (outdata == indata)
259 return ACCESS_16;
260 return ACCESS_UNKNOWN;
261}
262
263static int __init mac8390_memsize(unsigned long membase)
264{
265 unsigned long flags;
266 int i, j;
267
268 local_irq_save(flags);
269 /* Check up to 32K in 4K increments */
270 for (i = 0; i < 8; i++) {
271 volatile unsigned short *m = (unsigned short *)(membase + (i * 0x1000));
272
273 /* Unwriteable - we have a fully decoded card and the
274 RAM end located */
275 if (hwreg_present(m) == 0)
276 break;
277
278 /* write a distinctive byte */
279 *m = 0xA5A0 | i;
280 /* check that we read back what we wrote */
281 if (*m != (0xA5A0 | i))
282 break;
283
284 /* check for partial decode and wrap */
285 for (j = 0; j < i; j++) {
286 volatile unsigned short *p = (unsigned short *)(membase + (j * 0x1000));
287 if (*p != (0xA5A0 | j))
288 break;
289 }
290 }
291 local_irq_restore(flags);
292 /*
293 * in any case, we stopped once we tried one block too many,
294 * or once we reached 32K
295 */
296 return i * 0x1000;
297}
298
299static bool __init mac8390_init(struct net_device *dev, struct nubus_dev *ndev,
300 enum mac8390_type cardtype)
301{
302 struct nubus_dir dir;
303 struct nubus_dirent ent;
304 int offset;
305 volatile unsigned short *i;
306
307 printk_once(KERN_INFO pr_fmt("%s"), version);
308
309 dev->irq = SLOT2IRQ(ndev->board->slot);
310 /* This is getting to be a habit */
311 dev->base_addr = (ndev->board->slot_addr |
312 ((ndev->board->slot & 0xf) << 20));
313
314 /*
315 * Get some Nubus info - we will trust the card's idea
316 * of where its memory and registers are.
317 */
318
319 if (nubus_get_func_dir(ndev, &dir) == -1) {
320 pr_err("%s: Unable to get Nubus functional directory for slot %X!\n",
321 dev->name, ndev->board->slot);
322 return false;
323 }
324
325 /* Get the MAC address */
326 if (nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent) == -1) {
327 pr_info("%s: Couldn't get MAC address!\n", dev->name);
328 return false;
329 }
330
331 nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);
332
333 if (useresources[cardtype] == 1) {
334 nubus_rewinddir(&dir);
335 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS,
336 &ent) == -1) {
337 pr_err("%s: Memory offset resource for slot %X not found!\n",
338 dev->name, ndev->board->slot);
339 return false;
340 }
341 nubus_get_rsrc_mem(&offset, &ent, 4);
342 dev->mem_start = dev->base_addr + offset;
343 /* yes, this is how the Apple driver does it */
344 dev->base_addr = dev->mem_start + 0x10000;
345 nubus_rewinddir(&dir);
346 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH,
347 &ent) == -1) {
348 pr_info("%s: Memory length resource for slot %X not found, probing\n",
349 dev->name, ndev->board->slot);
350 offset = mac8390_memsize(dev->mem_start);
351 } else {
352 nubus_get_rsrc_mem(&offset, &ent, 4);
353 }
354 dev->mem_end = dev->mem_start + offset;
355 } else {
356 switch (cardtype) {
357 case MAC8390_KINETICS:
358 case MAC8390_DAYNA: /* it's the same */
359 dev->base_addr = (int)(ndev->board->slot_addr +
360 DAYNA_8390_BASE);
361 dev->mem_start = (int)(ndev->board->slot_addr +
362 DAYNA_8390_MEM);
363 dev->mem_end = dev->mem_start +
364 mac8390_memsize(dev->mem_start);
365 break;
366 case MAC8390_INTERLAN:
367 dev->base_addr = (int)(ndev->board->slot_addr +
368 INTERLAN_8390_BASE);
369 dev->mem_start = (int)(ndev->board->slot_addr +
370 INTERLAN_8390_MEM);
371 dev->mem_end = dev->mem_start +
372 mac8390_memsize(dev->mem_start);
373 break;
374 case MAC8390_CABLETRON:
375 dev->base_addr = (int)(ndev->board->slot_addr +
376 CABLETRON_8390_BASE);
377 dev->mem_start = (int)(ndev->board->slot_addr +
378 CABLETRON_8390_MEM);
379 /* The base address is unreadable if 0x00
380 * has been written to the command register
381 * Reset the chip by writing E8390_NODMA +
382 * E8390_PAGE0 + E8390_STOP just to be
383 * sure
384 */
385 i = (void *)dev->base_addr;
386 *i = 0x21;
387 dev->mem_end = dev->mem_start +
388 mac8390_memsize(dev->mem_start);
389 break;
390
391 default:
392 pr_err("Card type %s is unsupported, sorry\n",
393 ndev->board->name);
394 return false;
395 }
396 }
397
398 return true;
399}
400
401struct net_device * __init mac8390_probe(int unit)
402{
403 struct net_device *dev;
404 struct nubus_dev *ndev = NULL;
405 int err = -ENODEV;
406 struct ei_device *ei_local;
407
408 static unsigned int slots;
409
410 enum mac8390_type cardtype;
411
412 /* probably should check for Nubus instead */
413
414 if (!MACH_IS_MAC)
415 return ERR_PTR(-ENODEV);
416
417 dev = ____alloc_ei_netdev(0);
418 if (!dev)
419 return ERR_PTR(-ENOMEM);
420
421 if (unit >= 0)
422 sprintf(dev->name, "eth%d", unit);
423
424 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK, NUBUS_TYPE_ETHERNET,
425 ndev))) {
426 /* Have we seen it already? */
427 if (slots & (1 << ndev->board->slot))
428 continue;
429 slots |= 1 << ndev->board->slot;
430
431 cardtype = mac8390_ident(ndev);
432 if (cardtype == MAC8390_NONE)
433 continue;
434
435 if (!mac8390_init(dev, ndev, cardtype))
436 continue;
437
438 /* Do the nasty 8390 stuff */
439 if (!mac8390_initdev(dev, ndev, cardtype))
440 break;
441 }
442
443 if (!ndev)
444 goto out;
445
446 ei_local = netdev_priv(dev);
447 ei_local->msg_enable = mac8390_msg_enable;
448
449 err = register_netdev(dev);
450 if (err)
451 goto out;
452 return dev;
453
454out:
455 free_netdev(dev);
456 return ERR_PTR(err);
457}
458
459#ifdef MODULE
460MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
461MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
462MODULE_LICENSE("GPL");
463
464/* overkill, of course */
465static struct net_device *dev_mac8390[15];
466int init_module(void)
467{
468 int i;
469 for (i = 0; i < 15; i++) {
470 struct net_device *dev = mac8390_probe(-1);
471 if (IS_ERR(dev))
472 break;
473 dev_mac890[i] = dev;
474 }
475 if (!i) {
476 pr_notice("No useable cards found, driver NOT installed.\n");
477 return -ENODEV;
478 }
479 return 0;
480}
481
482void cleanup_module(void)
483{
484 int i;
485 for (i = 0; i < 15; i++) {
486 struct net_device *dev = dev_mac890[i];
487 if (dev) {
488 unregister_netdev(dev);
489 free_netdev(dev);
490 }
491 }
492}
493
494#endif /* MODULE */
495
496static const struct net_device_ops mac8390_netdev_ops = {
497 .ndo_open = mac8390_open,
498 .ndo_stop = mac8390_close,
499 .ndo_start_xmit = __ei_start_xmit,
500 .ndo_tx_timeout = __ei_tx_timeout,
501 .ndo_get_stats = __ei_get_stats,
502 .ndo_set_rx_mode = __ei_set_multicast_list,
503 .ndo_validate_addr = eth_validate_addr,
504 .ndo_set_mac_address = eth_mac_addr,
505 .ndo_change_mtu = eth_change_mtu,
506#ifdef CONFIG_NET_POLL_CONTROLLER
507 .ndo_poll_controller = __ei_poll,
508#endif
509};
510
511static int __init mac8390_initdev(struct net_device *dev,
512 struct nubus_dev *ndev,
513 enum mac8390_type type)
514{
515 static u32 fwrd4_offsets[16] = {
516 0, 4, 8, 12,
517 16, 20, 24, 28,
518 32, 36, 40, 44,
519 48, 52, 56, 60
520 };
521 static u32 back4_offsets[16] = {
522 60, 56, 52, 48,
523 44, 40, 36, 32,
524 28, 24, 20, 16,
525 12, 8, 4, 0
526 };
527 static u32 fwrd2_offsets[16] = {
528 0, 2, 4, 6,
529 8, 10, 12, 14,
530 16, 18, 20, 22,
531 24, 26, 28, 30
532 };
533
534 int access_bitmode = 0;
535
536 /* Now fill in our stuff */
537 dev->netdev_ops = &mac8390_netdev_ops;
538
539 /* GAR, ei_status is actually a macro even though it looks global */
540 ei_status.name = cardname[type];
541 ei_status.word16 = word16[type];
542
543 /* Cabletron's TX/RX buffers are backwards */
544 if (type == MAC8390_CABLETRON) {
545 ei_status.tx_start_page = CABLETRON_TX_START_PG;
546 ei_status.rx_start_page = CABLETRON_RX_START_PG;
547 ei_status.stop_page = CABLETRON_RX_STOP_PG;
548 ei_status.rmem_start = dev->mem_start;
549 ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
550 } else {
551 ei_status.tx_start_page = WD_START_PG;
552 ei_status.rx_start_page = WD_START_PG + TX_PAGES;
553 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
554 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
555 ei_status.rmem_end = dev->mem_end;
556 }
557
558 /* Fill in model-specific information and functions */
559 switch (type) {
560 case MAC8390_FARALLON:
561 case MAC8390_APPLE:
562 switch (mac8390_testio(dev->mem_start)) {
563 case ACCESS_UNKNOWN:
564 pr_err("Don't know how to access card memory!\n");
565 return -ENODEV;
566 break;
567
568 case ACCESS_16:
569 /* 16 bit card, register map is reversed */
570 ei_status.reset_8390 = mac8390_no_reset;
571 ei_status.block_input = slow_sane_block_input;
572 ei_status.block_output = slow_sane_block_output;
573 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
574 ei_status.reg_offset = back4_offsets;
575 break;
576
577 case ACCESS_32:
578 /* 32 bit card, register map is reversed */
579 ei_status.reset_8390 = mac8390_no_reset;
580 ei_status.block_input = sane_block_input;
581 ei_status.block_output = sane_block_output;
582 ei_status.get_8390_hdr = sane_get_8390_hdr;
583 ei_status.reg_offset = back4_offsets;
584 access_bitmode = 1;
585 break;
586 }
587 break;
588
589 case MAC8390_ASANTE:
590 /* Some Asante cards pass the 32 bit test
591 * but overwrite system memory when run at 32 bit.
592 * so we run them all at 16 bit.
593 */
594 ei_status.reset_8390 = mac8390_no_reset;
595 ei_status.block_input = slow_sane_block_input;
596 ei_status.block_output = slow_sane_block_output;
597 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
598 ei_status.reg_offset = back4_offsets;
599 break;
600
601 case MAC8390_CABLETRON:
602 /* 16 bit card, register map is short forward */
603 ei_status.reset_8390 = mac8390_no_reset;
604 ei_status.block_input = slow_sane_block_input;
605 ei_status.block_output = slow_sane_block_output;
606 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
607 ei_status.reg_offset = fwrd2_offsets;
608 break;
609
610 case MAC8390_DAYNA:
611 case MAC8390_KINETICS:
612 /* 16 bit memory, register map is forward */
613 /* dayna and similar */
614 ei_status.reset_8390 = mac8390_no_reset;
615 ei_status.block_input = dayna_block_input;
616 ei_status.block_output = dayna_block_output;
617 ei_status.get_8390_hdr = dayna_get_8390_hdr;
618 ei_status.reg_offset = fwrd4_offsets;
619 break;
620
621 case MAC8390_INTERLAN:
622 /* 16 bit memory, register map is forward */
623 ei_status.reset_8390 = interlan_reset;
624 ei_status.block_input = slow_sane_block_input;
625 ei_status.block_output = slow_sane_block_output;
626 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
627 ei_status.reg_offset = fwrd4_offsets;
628 break;
629
630 default:
631 pr_err("Card type %s is unsupported, sorry\n",
632 ndev->board->name);
633 return -ENODEV;
634 }
635
636 __NS8390_init(dev, 0);
637
638 /* Good, done, now spit out some messages */
639 pr_info("%s: %s in slot %X (type %s)\n",
640 dev->name, ndev->board->name, ndev->board->slot,
641 cardname[type]);
642 pr_info("MAC %pM IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
643 dev->dev_addr, dev->irq,
644 (unsigned int)(dev->mem_end - dev->mem_start) >> 10,
645 dev->mem_start, access_bitmode ? 32 : 16);
646 return 0;
647}
648
649static int mac8390_open(struct net_device *dev)
650{
651 int err;
652
653 __ei_open(dev);
654 err = request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev);
655 if (err)
656 pr_err("%s: unable to get IRQ %d\n", dev->name, dev->irq);
657 return err;
658}
659
660static int mac8390_close(struct net_device *dev)
661{
662 free_irq(dev->irq, dev);
663 __ei_close(dev);
664 return 0;
665}
666
667static void mac8390_no_reset(struct net_device *dev)
668{
669 struct ei_device *ei_local = netdev_priv(dev);
670
671 ei_status.txing = 0;
672 netif_info(ei_local, hw, dev, "reset not supported\n");
673}
674
675static void interlan_reset(struct net_device *dev)
676{
677 unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq));
678 struct ei_device *ei_local = netdev_priv(dev);
679
680 netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...",
681 jiffies);
682 ei_status.txing = 0;
683 target[0xC0000] = 0;
684 if (netif_msg_hw(ei_local))
685 pr_cont("reset complete\n");
686}
687
688/* dayna_memcpy_fromio/dayna_memcpy_toio */
689/* directly from daynaport.c by Alan Cox */
690static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from,
691 int count)
692{
693 volatile unsigned char *ptr;
694 unsigned char *target = to;
695 from <<= 1; /* word, skip overhead */
696 ptr = (unsigned char *)(dev->mem_start+from);
697 /* Leading byte? */
698 if (from & 2) {
699 *target++ = ptr[-1];
700 ptr += 2;
701 count--;
702 }
703 while (count >= 2) {
704 *(unsigned short *)target = *(unsigned short volatile *)ptr;
705 ptr += 4; /* skip cruft */
706 target += 2;
707 count -= 2;
708 }
709 /* Trailing byte? */
710 if (count)
711 *target = *ptr;
712}
713
714static void dayna_memcpy_tocard(struct net_device *dev, int to,
715 const void *from, int count)
716{
717 volatile unsigned short *ptr;
718 const unsigned char *src = from;
719 to <<= 1; /* word, skip overhead */
720 ptr = (unsigned short *)(dev->mem_start+to);
721 /* Leading byte? */
722 if (to & 2) { /* avoid a byte write (stomps on other data) */
723 ptr[-1] = (ptr[-1]&0xFF00)|*src++;
724 ptr++;
725 count--;
726 }
727 while (count >= 2) {
728 *ptr++ = *(unsigned short *)src; /* Copy and */
729 ptr++; /* skip cruft */
730 src += 2;
731 count -= 2;
732 }
733 /* Trailing byte? */
734 if (count) {
735 /* card doesn't like byte writes */
736 *ptr = (*ptr & 0x00FF) | (*src << 8);
737 }
738}
739
740/* sane block input/output */
741static void sane_get_8390_hdr(struct net_device *dev,
742 struct e8390_pkt_hdr *hdr, int ring_page)
743{
744 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
745 memcpy_fromio(hdr, dev->mem_start + hdr_start, 4);
746 /* Fix endianness */
747 hdr->count = swab16(hdr->count);
748}
749
750static void sane_block_input(struct net_device *dev, int count,
751 struct sk_buff *skb, int ring_offset)
752{
753 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
754 unsigned long xfer_start = xfer_base + dev->mem_start;
755
756 if (xfer_start + count > ei_status.rmem_end) {
757 /* We must wrap the input move. */
758 int semi_count = ei_status.rmem_end - xfer_start;
759 memcpy_fromio(skb->data, dev->mem_start + xfer_base,
760 semi_count);
761 count -= semi_count;
762 memcpy_fromio(skb->data + semi_count, ei_status.rmem_start,
763 count);
764 } else {
765 memcpy_fromio(skb->data, dev->mem_start + xfer_base, count);
766 }
767}
768
769static void sane_block_output(struct net_device *dev, int count,
770 const unsigned char *buf, int start_page)
771{
772 long shmem = (start_page - WD_START_PG)<<8;
773
774 memcpy_toio(dev->mem_start + shmem, buf, count);
775}
776
777/* dayna block input/output */
778static void dayna_get_8390_hdr(struct net_device *dev,
779 struct e8390_pkt_hdr *hdr, int ring_page)
780{
781 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
782
783 dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
784 /* Fix endianness */
785 hdr->count = (hdr->count & 0xFF) << 8 | (hdr->count >> 8);
786}
787
788static void dayna_block_input(struct net_device *dev, int count,
789 struct sk_buff *skb, int ring_offset)
790{
791 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
792 unsigned long xfer_start = xfer_base+dev->mem_start;
793
794 /* Note the offset math is done in card memory space which is word
795 per long onto our space. */
796
797 if (xfer_start + count > ei_status.rmem_end) {
798 /* We must wrap the input move. */
799 int semi_count = ei_status.rmem_end - xfer_start;
800 dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
801 count -= semi_count;
802 dayna_memcpy_fromcard(dev, skb->data + semi_count,
803 ei_status.rmem_start - dev->mem_start,
804 count);
805 } else {
806 dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
807 }
808}
809
810static void dayna_block_output(struct net_device *dev, int count,
811 const unsigned char *buf,
812 int start_page)
813{
814 long shmem = (start_page - WD_START_PG)<<8;
815
816 dayna_memcpy_tocard(dev, shmem, buf, count);
817}
818
819/* Cabletron block I/O */
820static void slow_sane_get_8390_hdr(struct net_device *dev,
821 struct e8390_pkt_hdr *hdr,
822 int ring_page)
823{
824 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
825 word_memcpy_fromcard(hdr, dev->mem_start + hdr_start, 4);
826 /* Register endianism - fix here rather than 8390.c */
827 hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
828}
829
830static void slow_sane_block_input(struct net_device *dev, int count,
831 struct sk_buff *skb, int ring_offset)
832{
833 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
834 unsigned long xfer_start = xfer_base+dev->mem_start;
835
836 if (xfer_start + count > ei_status.rmem_end) {
837 /* We must wrap the input move. */
838 int semi_count = ei_status.rmem_end - xfer_start;
839 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
840 semi_count);
841 count -= semi_count;
842 word_memcpy_fromcard(skb->data + semi_count,
843 ei_status.rmem_start, count);
844 } else {
845 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
846 count);
847 }
848}
849
850static void slow_sane_block_output(struct net_device *dev, int count,
851 const unsigned char *buf, int start_page)
852{
853 long shmem = (start_page - WD_START_PG)<<8;
854
855 word_memcpy_tocard(dev->mem_start + shmem, buf, count);
856}
857
858static void word_memcpy_tocard(unsigned long tp, const void *fp, int count)
859{
860 volatile unsigned short *to = (void *)tp;
861 const unsigned short *from = fp;
862
863 count++;
864 count /= 2;
865
866 while (count--)
867 *to++ = *from++;
868}
869
870static void word_memcpy_fromcard(void *tp, unsigned long fp, int count)
871{
872 unsigned short *to = tp;
873 const volatile unsigned short *from = (const void *)fp;
874
875 count++;
876 count /= 2;
877
878 while (count--)
879 *to++ = *from++;
880}
881
882
1/* mac8390.c: New driver for 8390-based Nubus (or Nubus-alike)
2 Ethernet cards on Linux */
3/* Based on the former daynaport.c driver, by Alan Cox. Some code
4 taken from or inspired by skeleton.c by Donald Becker, acenic.c by
5 Jes Sorensen, and ne2k-pci.c by Donald Becker and Paul Gortmaker.
6
7 This software may be used and distributed according to the terms of
8 the GNU Public License, incorporated herein by reference. */
9
10/* 2000-02-28: support added for Dayna and Kinetics cards by
11 A.G.deWijn@phys.uu.nl */
12/* 2000-04-04: support added for Dayna2 by bart@etpmod.phys.tue.nl */
13/* 2001-04-18: support for DaynaPort E/LC-M by rayk@knightsmanor.org */
14/* 2001-05-15: support for Cabletron ported from old daynaport driver
15 * and fixed access to Sonic Sys card which masquerades as a Farallon
16 * by rayk@knightsmanor.org */
17/* 2002-12-30: Try to support more cards, some clues from NetBSD driver */
18/* 2003-12-26: Make sure Asante cards always work. */
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/types.h>
25#include <linux/fcntl.h>
26#include <linux/interrupt.h>
27#include <linux/ptrace.h>
28#include <linux/ioport.h>
29#include <linux/nubus.h>
30#include <linux/in.h>
31#include <linux/string.h>
32#include <linux/errno.h>
33#include <linux/init.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
37#include <linux/bitops.h>
38#include <linux/io.h>
39
40#include <asm/dma.h>
41#include <asm/hwtest.h>
42#include <asm/macints.h>
43
44static char version[] =
45 "v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
46
47#define EI_SHIFT(x) (ei_local->reg_offset[x])
48#define ei_inb(port) in_8(port)
49#define ei_outb(val, port) out_8(port, val)
50#define ei_inb_p(port) in_8(port)
51#define ei_outb_p(val, port) out_8(port, val)
52
53#include "lib8390.c"
54
55#define WD_START_PG 0x00 /* First page of TX buffer */
56#define CABLETRON_RX_START_PG 0x00 /* First page of RX buffer */
57#define CABLETRON_RX_STOP_PG 0x30 /* Last page +1 of RX ring */
58#define CABLETRON_TX_START_PG CABLETRON_RX_STOP_PG
59 /* First page of TX buffer */
60
61/*
62 * Unfortunately it seems we have to hardcode these for the moment
63 * Shouldn't the card know about this?
64 * Does anyone know where to read it off the card?
65 * Do we trust the data provided by the card?
66 */
67
68#define DAYNA_8390_BASE 0x80000
69#define DAYNA_8390_MEM 0x00000
70
71#define CABLETRON_8390_BASE 0x90000
72#define CABLETRON_8390_MEM 0x00000
73
74#define INTERLAN_8390_BASE 0xE0000
75#define INTERLAN_8390_MEM 0xD0000
76
77enum mac8390_type {
78 MAC8390_NONE = -1,
79 MAC8390_APPLE,
80 MAC8390_ASANTE,
81 MAC8390_FARALLON,
82 MAC8390_CABLETRON,
83 MAC8390_DAYNA,
84 MAC8390_INTERLAN,
85 MAC8390_KINETICS,
86};
87
88static const char *cardname[] = {
89 "apple",
90 "asante",
91 "farallon",
92 "cabletron",
93 "dayna",
94 "interlan",
95 "kinetics",
96};
97
98static const int word16[] = {
99 1, /* apple */
100 1, /* asante */
101 1, /* farallon */
102 1, /* cabletron */
103 0, /* dayna */
104 1, /* interlan */
105 0, /* kinetics */
106};
107
108/* on which cards do we use NuBus resources? */
109static const int useresources[] = {
110 1, /* apple */
111 1, /* asante */
112 1, /* farallon */
113 0, /* cabletron */
114 0, /* dayna */
115 0, /* interlan */
116 0, /* kinetics */
117};
118
119enum mac8390_access {
120 ACCESS_UNKNOWN = 0,
121 ACCESS_32,
122 ACCESS_16,
123};
124
125extern int mac8390_memtest(struct net_device *dev);
126static int mac8390_initdev(struct net_device *dev, struct nubus_dev *ndev,
127 enum mac8390_type type);
128
129static int mac8390_open(struct net_device *dev);
130static int mac8390_close(struct net_device *dev);
131static void mac8390_no_reset(struct net_device *dev);
132static void interlan_reset(struct net_device *dev);
133
134/* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
135static void sane_get_8390_hdr(struct net_device *dev,
136 struct e8390_pkt_hdr *hdr, int ring_page);
137static void sane_block_input(struct net_device *dev, int count,
138 struct sk_buff *skb, int ring_offset);
139static void sane_block_output(struct net_device *dev, int count,
140 const unsigned char *buf, const int start_page);
141
142/* dayna_memcpy to and from card */
143static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
144 int from, int count);
145static void dayna_memcpy_tocard(struct net_device *dev, int to,
146 const void *from, int count);
147
148/* Dayna - Dayna/Kinetics use this */
149static void dayna_get_8390_hdr(struct net_device *dev,
150 struct e8390_pkt_hdr *hdr, int ring_page);
151static void dayna_block_input(struct net_device *dev, int count,
152 struct sk_buff *skb, int ring_offset);
153static void dayna_block_output(struct net_device *dev, int count,
154 const unsigned char *buf, int start_page);
155
156#define memcpy_fromio(a, b, c) memcpy((a), (void *)(b), (c))
157#define memcpy_toio(a, b, c) memcpy((void *)(a), (b), (c))
158
159#define memcmp_withio(a, b, c) memcmp((a), (void *)(b), (c))
160
161/* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
162static void slow_sane_get_8390_hdr(struct net_device *dev,
163 struct e8390_pkt_hdr *hdr, int ring_page);
164static void slow_sane_block_input(struct net_device *dev, int count,
165 struct sk_buff *skb, int ring_offset);
166static void slow_sane_block_output(struct net_device *dev, int count,
167 const unsigned char *buf, int start_page);
168static void word_memcpy_tocard(unsigned long tp, const void *fp, int count);
169static void word_memcpy_fromcard(void *tp, unsigned long fp, int count);
170static u32 mac8390_msg_enable;
171
172static enum mac8390_type __init mac8390_ident(struct nubus_dev *dev)
173{
174 switch (dev->dr_sw) {
175 case NUBUS_DRSW_3COM:
176 switch (dev->dr_hw) {
177 case NUBUS_DRHW_APPLE_SONIC_NB:
178 case NUBUS_DRHW_APPLE_SONIC_LC:
179 case NUBUS_DRHW_SONNET:
180 return MAC8390_NONE;
181 default:
182 return MAC8390_APPLE;
183 }
184 break;
185
186 case NUBUS_DRSW_APPLE:
187 switch (dev->dr_hw) {
188 case NUBUS_DRHW_ASANTE_LC:
189 return MAC8390_NONE;
190 case NUBUS_DRHW_CABLETRON:
191 return MAC8390_CABLETRON;
192 default:
193 return MAC8390_APPLE;
194 }
195 break;
196
197 case NUBUS_DRSW_ASANTE:
198 return MAC8390_ASANTE;
199 break;
200
201 case NUBUS_DRSW_TECHWORKS:
202 case NUBUS_DRSW_DAYNA2:
203 case NUBUS_DRSW_DAYNA_LC:
204 if (dev->dr_hw == NUBUS_DRHW_CABLETRON)
205 return MAC8390_CABLETRON;
206 else
207 return MAC8390_APPLE;
208 break;
209
210 case NUBUS_DRSW_FARALLON:
211 return MAC8390_FARALLON;
212 break;
213
214 case NUBUS_DRSW_KINETICS:
215 switch (dev->dr_hw) {
216 case NUBUS_DRHW_INTERLAN:
217 return MAC8390_INTERLAN;
218 default:
219 return MAC8390_KINETICS;
220 }
221 break;
222
223 case NUBUS_DRSW_DAYNA:
224 /*
225 * These correspond to Dayna Sonic cards
226 * which use the macsonic driver
227 */
228 if (dev->dr_hw == NUBUS_DRHW_SMC9194 ||
229 dev->dr_hw == NUBUS_DRHW_INTERLAN)
230 return MAC8390_NONE;
231 else
232 return MAC8390_DAYNA;
233 break;
234 }
235 return MAC8390_NONE;
236}
237
238static enum mac8390_access __init mac8390_testio(volatile unsigned long membase)
239{
240 unsigned long outdata = 0xA5A0B5B0;
241 unsigned long indata = 0x00000000;
242 /* Try writing 32 bits */
243 memcpy_toio(membase, &outdata, 4);
244 /* Now compare them */
245 if (memcmp_withio(&outdata, membase, 4) == 0)
246 return ACCESS_32;
247 /* Write 16 bit output */
248 word_memcpy_tocard(membase, &outdata, 4);
249 /* Now read it back */
250 word_memcpy_fromcard(&indata, membase, 4);
251 if (outdata == indata)
252 return ACCESS_16;
253 return ACCESS_UNKNOWN;
254}
255
256static int __init mac8390_memsize(unsigned long membase)
257{
258 unsigned long flags;
259 int i, j;
260
261 local_irq_save(flags);
262 /* Check up to 32K in 4K increments */
263 for (i = 0; i < 8; i++) {
264 volatile unsigned short *m = (unsigned short *)(membase + (i * 0x1000));
265
266 /* Unwriteable - we have a fully decoded card and the
267 RAM end located */
268 if (hwreg_present(m) == 0)
269 break;
270
271 /* write a distinctive byte */
272 *m = 0xA5A0 | i;
273 /* check that we read back what we wrote */
274 if (*m != (0xA5A0 | i))
275 break;
276
277 /* check for partial decode and wrap */
278 for (j = 0; j < i; j++) {
279 volatile unsigned short *p = (unsigned short *)(membase + (j * 0x1000));
280 if (*p != (0xA5A0 | j))
281 break;
282 }
283 }
284 local_irq_restore(flags);
285 /*
286 * in any case, we stopped once we tried one block too many,
287 * or once we reached 32K
288 */
289 return i * 0x1000;
290}
291
292static bool __init mac8390_init(struct net_device *dev, struct nubus_dev *ndev,
293 enum mac8390_type cardtype)
294{
295 struct nubus_dir dir;
296 struct nubus_dirent ent;
297 int offset;
298 volatile unsigned short *i;
299
300 printk_once(KERN_INFO pr_fmt("%s"), version);
301
302 dev->irq = SLOT2IRQ(ndev->board->slot);
303 /* This is getting to be a habit */
304 dev->base_addr = (ndev->board->slot_addr |
305 ((ndev->board->slot & 0xf) << 20));
306
307 /*
308 * Get some Nubus info - we will trust the card's idea
309 * of where its memory and registers are.
310 */
311
312 if (nubus_get_func_dir(ndev, &dir) == -1) {
313 pr_err("%s: Unable to get Nubus functional directory for slot %X!\n",
314 dev->name, ndev->board->slot);
315 return false;
316 }
317
318 /* Get the MAC address */
319 if (nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent) == -1) {
320 pr_info("%s: Couldn't get MAC address!\n", dev->name);
321 return false;
322 }
323
324 nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);
325
326 if (useresources[cardtype] == 1) {
327 nubus_rewinddir(&dir);
328 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS,
329 &ent) == -1) {
330 pr_err("%s: Memory offset resource for slot %X not found!\n",
331 dev->name, ndev->board->slot);
332 return false;
333 }
334 nubus_get_rsrc_mem(&offset, &ent, 4);
335 dev->mem_start = dev->base_addr + offset;
336 /* yes, this is how the Apple driver does it */
337 dev->base_addr = dev->mem_start + 0x10000;
338 nubus_rewinddir(&dir);
339 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH,
340 &ent) == -1) {
341 pr_info("%s: Memory length resource for slot %X not found, probing\n",
342 dev->name, ndev->board->slot);
343 offset = mac8390_memsize(dev->mem_start);
344 } else {
345 nubus_get_rsrc_mem(&offset, &ent, 4);
346 }
347 dev->mem_end = dev->mem_start + offset;
348 } else {
349 switch (cardtype) {
350 case MAC8390_KINETICS:
351 case MAC8390_DAYNA: /* it's the same */
352 dev->base_addr = (int)(ndev->board->slot_addr +
353 DAYNA_8390_BASE);
354 dev->mem_start = (int)(ndev->board->slot_addr +
355 DAYNA_8390_MEM);
356 dev->mem_end = dev->mem_start +
357 mac8390_memsize(dev->mem_start);
358 break;
359 case MAC8390_INTERLAN:
360 dev->base_addr = (int)(ndev->board->slot_addr +
361 INTERLAN_8390_BASE);
362 dev->mem_start = (int)(ndev->board->slot_addr +
363 INTERLAN_8390_MEM);
364 dev->mem_end = dev->mem_start +
365 mac8390_memsize(dev->mem_start);
366 break;
367 case MAC8390_CABLETRON:
368 dev->base_addr = (int)(ndev->board->slot_addr +
369 CABLETRON_8390_BASE);
370 dev->mem_start = (int)(ndev->board->slot_addr +
371 CABLETRON_8390_MEM);
372 /* The base address is unreadable if 0x00
373 * has been written to the command register
374 * Reset the chip by writing E8390_NODMA +
375 * E8390_PAGE0 + E8390_STOP just to be
376 * sure
377 */
378 i = (void *)dev->base_addr;
379 *i = 0x21;
380 dev->mem_end = dev->mem_start +
381 mac8390_memsize(dev->mem_start);
382 break;
383
384 default:
385 pr_err("Card type %s is unsupported, sorry\n",
386 ndev->board->name);
387 return false;
388 }
389 }
390
391 return true;
392}
393
394struct net_device * __init mac8390_probe(int unit)
395{
396 struct net_device *dev;
397 struct nubus_dev *ndev = NULL;
398 int err = -ENODEV;
399 struct ei_device *ei_local;
400
401 static unsigned int slots;
402
403 enum mac8390_type cardtype;
404
405 /* probably should check for Nubus instead */
406
407 if (!MACH_IS_MAC)
408 return ERR_PTR(-ENODEV);
409
410 dev = ____alloc_ei_netdev(0);
411 if (!dev)
412 return ERR_PTR(-ENOMEM);
413
414 if (unit >= 0)
415 sprintf(dev->name, "eth%d", unit);
416
417 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK, NUBUS_TYPE_ETHERNET,
418 ndev))) {
419 /* Have we seen it already? */
420 if (slots & (1 << ndev->board->slot))
421 continue;
422 slots |= 1 << ndev->board->slot;
423
424 cardtype = mac8390_ident(ndev);
425 if (cardtype == MAC8390_NONE)
426 continue;
427
428 if (!mac8390_init(dev, ndev, cardtype))
429 continue;
430
431 /* Do the nasty 8390 stuff */
432 if (!mac8390_initdev(dev, ndev, cardtype))
433 break;
434 }
435
436 if (!ndev)
437 goto out;
438
439 ei_local = netdev_priv(dev);
440 ei_local->msg_enable = mac8390_msg_enable;
441
442 err = register_netdev(dev);
443 if (err)
444 goto out;
445 return dev;
446
447out:
448 free_netdev(dev);
449 return ERR_PTR(err);
450}
451
452#ifdef MODULE
453MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
454MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
455MODULE_LICENSE("GPL");
456
457static struct net_device *dev_mac8390;
458
459int __init init_module(void)
460{
461 dev_mac8390 = mac8390_probe(-1);
462 if (IS_ERR(dev_mac8390)) {
463 pr_warn("mac8390: No card found\n");
464 return PTR_ERR(dev_mac8390);
465 }
466 return 0;
467}
468
469void __exit cleanup_module(void)
470{
471 unregister_netdev(dev_mac8390);
472 free_netdev(dev_mac8390);
473}
474
475#endif /* MODULE */
476
477static const struct net_device_ops mac8390_netdev_ops = {
478 .ndo_open = mac8390_open,
479 .ndo_stop = mac8390_close,
480 .ndo_start_xmit = __ei_start_xmit,
481 .ndo_tx_timeout = __ei_tx_timeout,
482 .ndo_get_stats = __ei_get_stats,
483 .ndo_set_rx_mode = __ei_set_multicast_list,
484 .ndo_validate_addr = eth_validate_addr,
485 .ndo_set_mac_address = eth_mac_addr,
486#ifdef CONFIG_NET_POLL_CONTROLLER
487 .ndo_poll_controller = __ei_poll,
488#endif
489};
490
491static int __init mac8390_initdev(struct net_device *dev,
492 struct nubus_dev *ndev,
493 enum mac8390_type type)
494{
495 static u32 fwrd4_offsets[16] = {
496 0, 4, 8, 12,
497 16, 20, 24, 28,
498 32, 36, 40, 44,
499 48, 52, 56, 60
500 };
501 static u32 back4_offsets[16] = {
502 60, 56, 52, 48,
503 44, 40, 36, 32,
504 28, 24, 20, 16,
505 12, 8, 4, 0
506 };
507 static u32 fwrd2_offsets[16] = {
508 0, 2, 4, 6,
509 8, 10, 12, 14,
510 16, 18, 20, 22,
511 24, 26, 28, 30
512 };
513
514 int access_bitmode = 0;
515
516 /* Now fill in our stuff */
517 dev->netdev_ops = &mac8390_netdev_ops;
518
519 /* GAR, ei_status is actually a macro even though it looks global */
520 ei_status.name = cardname[type];
521 ei_status.word16 = word16[type];
522
523 /* Cabletron's TX/RX buffers are backwards */
524 if (type == MAC8390_CABLETRON) {
525 ei_status.tx_start_page = CABLETRON_TX_START_PG;
526 ei_status.rx_start_page = CABLETRON_RX_START_PG;
527 ei_status.stop_page = CABLETRON_RX_STOP_PG;
528 ei_status.rmem_start = dev->mem_start;
529 ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
530 } else {
531 ei_status.tx_start_page = WD_START_PG;
532 ei_status.rx_start_page = WD_START_PG + TX_PAGES;
533 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
534 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
535 ei_status.rmem_end = dev->mem_end;
536 }
537
538 /* Fill in model-specific information and functions */
539 switch (type) {
540 case MAC8390_FARALLON:
541 case MAC8390_APPLE:
542 switch (mac8390_testio(dev->mem_start)) {
543 case ACCESS_UNKNOWN:
544 pr_err("Don't know how to access card memory!\n");
545 return -ENODEV;
546
547 case ACCESS_16:
548 /* 16 bit card, register map is reversed */
549 ei_status.reset_8390 = mac8390_no_reset;
550 ei_status.block_input = slow_sane_block_input;
551 ei_status.block_output = slow_sane_block_output;
552 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
553 ei_status.reg_offset = back4_offsets;
554 break;
555
556 case ACCESS_32:
557 /* 32 bit card, register map is reversed */
558 ei_status.reset_8390 = mac8390_no_reset;
559 ei_status.block_input = sane_block_input;
560 ei_status.block_output = sane_block_output;
561 ei_status.get_8390_hdr = sane_get_8390_hdr;
562 ei_status.reg_offset = back4_offsets;
563 access_bitmode = 1;
564 break;
565 }
566 break;
567
568 case MAC8390_ASANTE:
569 /* Some Asante cards pass the 32 bit test
570 * but overwrite system memory when run at 32 bit.
571 * so we run them all at 16 bit.
572 */
573 ei_status.reset_8390 = mac8390_no_reset;
574 ei_status.block_input = slow_sane_block_input;
575 ei_status.block_output = slow_sane_block_output;
576 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
577 ei_status.reg_offset = back4_offsets;
578 break;
579
580 case MAC8390_CABLETRON:
581 /* 16 bit card, register map is short forward */
582 ei_status.reset_8390 = mac8390_no_reset;
583 ei_status.block_input = slow_sane_block_input;
584 ei_status.block_output = slow_sane_block_output;
585 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
586 ei_status.reg_offset = fwrd2_offsets;
587 break;
588
589 case MAC8390_DAYNA:
590 case MAC8390_KINETICS:
591 /* 16 bit memory, register map is forward */
592 /* dayna and similar */
593 ei_status.reset_8390 = mac8390_no_reset;
594 ei_status.block_input = dayna_block_input;
595 ei_status.block_output = dayna_block_output;
596 ei_status.get_8390_hdr = dayna_get_8390_hdr;
597 ei_status.reg_offset = fwrd4_offsets;
598 break;
599
600 case MAC8390_INTERLAN:
601 /* 16 bit memory, register map is forward */
602 ei_status.reset_8390 = interlan_reset;
603 ei_status.block_input = slow_sane_block_input;
604 ei_status.block_output = slow_sane_block_output;
605 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
606 ei_status.reg_offset = fwrd4_offsets;
607 break;
608
609 default:
610 pr_err("Card type %s is unsupported, sorry\n",
611 ndev->board->name);
612 return -ENODEV;
613 }
614
615 __NS8390_init(dev, 0);
616
617 /* Good, done, now spit out some messages */
618 pr_info("%s: %s in slot %X (type %s)\n",
619 dev->name, ndev->board->name, ndev->board->slot,
620 cardname[type]);
621 pr_info("MAC %pM IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
622 dev->dev_addr, dev->irq,
623 (unsigned int)(dev->mem_end - dev->mem_start) >> 10,
624 dev->mem_start, access_bitmode ? 32 : 16);
625 return 0;
626}
627
628static int mac8390_open(struct net_device *dev)
629{
630 int err;
631
632 __ei_open(dev);
633 err = request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev);
634 if (err)
635 pr_err("%s: unable to get IRQ %d\n", dev->name, dev->irq);
636 return err;
637}
638
639static int mac8390_close(struct net_device *dev)
640{
641 free_irq(dev->irq, dev);
642 __ei_close(dev);
643 return 0;
644}
645
646static void mac8390_no_reset(struct net_device *dev)
647{
648 struct ei_device *ei_local = netdev_priv(dev);
649
650 ei_status.txing = 0;
651 netif_info(ei_local, hw, dev, "reset not supported\n");
652}
653
654static void interlan_reset(struct net_device *dev)
655{
656 unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq));
657 struct ei_device *ei_local = netdev_priv(dev);
658
659 netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...",
660 jiffies);
661 ei_status.txing = 0;
662 target[0xC0000] = 0;
663 if (netif_msg_hw(ei_local))
664 pr_cont("reset complete\n");
665}
666
667/* dayna_memcpy_fromio/dayna_memcpy_toio */
668/* directly from daynaport.c by Alan Cox */
669static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from,
670 int count)
671{
672 volatile unsigned char *ptr;
673 unsigned char *target = to;
674 from <<= 1; /* word, skip overhead */
675 ptr = (unsigned char *)(dev->mem_start+from);
676 /* Leading byte? */
677 if (from & 2) {
678 *target++ = ptr[-1];
679 ptr += 2;
680 count--;
681 }
682 while (count >= 2) {
683 *(unsigned short *)target = *(unsigned short volatile *)ptr;
684 ptr += 4; /* skip cruft */
685 target += 2;
686 count -= 2;
687 }
688 /* Trailing byte? */
689 if (count)
690 *target = *ptr;
691}
692
693static void dayna_memcpy_tocard(struct net_device *dev, int to,
694 const void *from, int count)
695{
696 volatile unsigned short *ptr;
697 const unsigned char *src = from;
698 to <<= 1; /* word, skip overhead */
699 ptr = (unsigned short *)(dev->mem_start+to);
700 /* Leading byte? */
701 if (to & 2) { /* avoid a byte write (stomps on other data) */
702 ptr[-1] = (ptr[-1]&0xFF00)|*src++;
703 ptr++;
704 count--;
705 }
706 while (count >= 2) {
707 *ptr++ = *(unsigned short *)src; /* Copy and */
708 ptr++; /* skip cruft */
709 src += 2;
710 count -= 2;
711 }
712 /* Trailing byte? */
713 if (count) {
714 /* card doesn't like byte writes */
715 *ptr = (*ptr & 0x00FF) | (*src << 8);
716 }
717}
718
719/* sane block input/output */
720static void sane_get_8390_hdr(struct net_device *dev,
721 struct e8390_pkt_hdr *hdr, int ring_page)
722{
723 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
724 memcpy_fromio(hdr, dev->mem_start + hdr_start, 4);
725 /* Fix endianness */
726 hdr->count = swab16(hdr->count);
727}
728
729static void sane_block_input(struct net_device *dev, int count,
730 struct sk_buff *skb, int ring_offset)
731{
732 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
733 unsigned long xfer_start = xfer_base + dev->mem_start;
734
735 if (xfer_start + count > ei_status.rmem_end) {
736 /* We must wrap the input move. */
737 int semi_count = ei_status.rmem_end - xfer_start;
738 memcpy_fromio(skb->data, dev->mem_start + xfer_base,
739 semi_count);
740 count -= semi_count;
741 memcpy_fromio(skb->data + semi_count, ei_status.rmem_start,
742 count);
743 } else {
744 memcpy_fromio(skb->data, dev->mem_start + xfer_base, count);
745 }
746}
747
748static void sane_block_output(struct net_device *dev, int count,
749 const unsigned char *buf, int start_page)
750{
751 long shmem = (start_page - WD_START_PG)<<8;
752
753 memcpy_toio(dev->mem_start + shmem, buf, count);
754}
755
756/* dayna block input/output */
757static void dayna_get_8390_hdr(struct net_device *dev,
758 struct e8390_pkt_hdr *hdr, int ring_page)
759{
760 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
761
762 dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
763 /* Fix endianness */
764 hdr->count = (hdr->count & 0xFF) << 8 | (hdr->count >> 8);
765}
766
767static void dayna_block_input(struct net_device *dev, int count,
768 struct sk_buff *skb, int ring_offset)
769{
770 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
771 unsigned long xfer_start = xfer_base+dev->mem_start;
772
773 /* Note the offset math is done in card memory space which is word
774 per long onto our space. */
775
776 if (xfer_start + count > ei_status.rmem_end) {
777 /* We must wrap the input move. */
778 int semi_count = ei_status.rmem_end - xfer_start;
779 dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
780 count -= semi_count;
781 dayna_memcpy_fromcard(dev, skb->data + semi_count,
782 ei_status.rmem_start - dev->mem_start,
783 count);
784 } else {
785 dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
786 }
787}
788
789static void dayna_block_output(struct net_device *dev, int count,
790 const unsigned char *buf,
791 int start_page)
792{
793 long shmem = (start_page - WD_START_PG)<<8;
794
795 dayna_memcpy_tocard(dev, shmem, buf, count);
796}
797
798/* Cabletron block I/O */
799static void slow_sane_get_8390_hdr(struct net_device *dev,
800 struct e8390_pkt_hdr *hdr,
801 int ring_page)
802{
803 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
804 word_memcpy_fromcard(hdr, dev->mem_start + hdr_start, 4);
805 /* Register endianism - fix here rather than 8390.c */
806 hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
807}
808
809static void slow_sane_block_input(struct net_device *dev, int count,
810 struct sk_buff *skb, int ring_offset)
811{
812 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
813 unsigned long xfer_start = xfer_base+dev->mem_start;
814
815 if (xfer_start + count > ei_status.rmem_end) {
816 /* We must wrap the input move. */
817 int semi_count = ei_status.rmem_end - xfer_start;
818 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
819 semi_count);
820 count -= semi_count;
821 word_memcpy_fromcard(skb->data + semi_count,
822 ei_status.rmem_start, count);
823 } else {
824 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
825 count);
826 }
827}
828
829static void slow_sane_block_output(struct net_device *dev, int count,
830 const unsigned char *buf, int start_page)
831{
832 long shmem = (start_page - WD_START_PG)<<8;
833
834 word_memcpy_tocard(dev->mem_start + shmem, buf, count);
835}
836
837static void word_memcpy_tocard(unsigned long tp, const void *fp, int count)
838{
839 volatile unsigned short *to = (void *)tp;
840 const unsigned short *from = fp;
841
842 count++;
843 count /= 2;
844
845 while (count--)
846 *to++ = *from++;
847}
848
849static void word_memcpy_fromcard(void *tp, unsigned long fp, int count)
850{
851 unsigned short *to = tp;
852 const volatile unsigned short *from = (const void *)fp;
853
854 count++;
855 count /= 2;
856
857 while (count--)
858 *to++ = *from++;
859}
860
861