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