blob: 8bd09e280c9eaa67965610ee7e8df68a60d02d85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010/* 2000-02-28: support added for Dayna and Kinetics cards by
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 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
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015 * and fixed access to Sonic Sys card which masquerades as a Farallon
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * by rayk@knightsmanor.org */
Finn Thain2964db02007-05-01 22:32:54 +020017/* 2002-12-30: Try to support more cards, some clues from NetBSD driver */
18/* 2003-12-26: Make sure Asante cards always work. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Joe Perches18c00192010-01-04 11:53:01 +000020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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/slab.h>
32#include <linux/string.h>
33#include <linux/errno.h>
34#include <linux/init.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/skbuff.h>
38#include <linux/bitops.h>
39
40#include <asm/system.h>
41#include <asm/io.h>
42#include <asm/dma.h>
43#include <asm/hwtest.h>
44#include <asm/macints.h>
45
Al Viro8c6270f2006-10-10 00:19:36 +010046static char version[] =
Joe Perches18c00192010-01-04 11:53:01 +000047 "v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
Al Viro8c6270f2006-10-10 00:19:36 +010048
49#define EI_SHIFT(x) (ei_local->reg_offset[x])
50#define ei_inb(port) in_8(port)
51#define ei_outb(val,port) out_8(port,val)
52#define ei_inb_p(port) in_8(port)
53#define ei_outb_p(val,port) out_8(port,val)
54
55#include "lib8390.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define WD_START_PG 0x00 /* First page of TX buffer */
58#define CABLETRON_RX_START_PG 0x00 /* First page of RX buffer */
59#define CABLETRON_RX_STOP_PG 0x30 /* Last page +1 of RX ring */
60#define CABLETRON_TX_START_PG CABLETRON_RX_STOP_PG /* First page of TX buffer */
61
62/* Unfortunately it seems we have to hardcode these for the moment */
63/* Shouldn't the card know about this? Does anyone know where to read it off the card? Do we trust the data provided by the card? */
64
65#define DAYNA_8390_BASE 0x80000
66#define DAYNA_8390_MEM 0x00000
67
Jeff Garzik6aa20a22006-09-13 13:24:59 -040068#define CABLETRON_8390_BASE 0x90000
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define CABLETRON_8390_MEM 0x00000
70
Finn Thain2964db02007-05-01 22:32:54 +020071#define INTERLAN_8390_BASE 0xE0000
72#define INTERLAN_8390_MEM 0xD0000
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074enum mac8390_type {
75 MAC8390_NONE = -1,
76 MAC8390_APPLE,
77 MAC8390_ASANTE,
Finn Thain2964db02007-05-01 22:32:54 +020078 MAC8390_FARALLON,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 MAC8390_CABLETRON,
80 MAC8390_DAYNA,
81 MAC8390_INTERLAN,
82 MAC8390_KINETICS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
85static const char * cardname[] = {
86 "apple",
87 "asante",
88 "farallon",
89 "cabletron",
90 "dayna",
91 "interlan",
92 "kinetics",
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95static int word16[] = {
96 1, /* apple */
97 1, /* asante */
98 1, /* farallon */
99 1, /* cabletron */
100 0, /* dayna */
101 1, /* interlan */
102 0, /* kinetics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105/* on which cards do we use NuBus resources? */
106static int useresources[] = {
107 1, /* apple */
108 1, /* asante */
109 1, /* farallon */
110 0, /* cabletron */
111 0, /* dayna */
112 0, /* interlan */
113 0, /* kinetics */
Finn Thain2964db02007-05-01 22:32:54 +0200114};
115
116enum mac8390_access {
117 ACCESS_UNKNOWN = 0,
118 ACCESS_32,
119 ACCESS_16,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122extern int mac8390_memtest(struct net_device * dev);
123static int mac8390_initdev(struct net_device * dev, struct nubus_dev * ndev,
124 enum mac8390_type type);
125
126static int mac8390_open(struct net_device * dev);
127static int mac8390_close(struct net_device * dev);
128static void mac8390_no_reset(struct net_device *dev);
Finn Thain2964db02007-05-01 22:32:54 +0200129static void interlan_reset(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Finn Thain2964db02007-05-01 22:32:54 +0200131/* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void sane_get_8390_hdr(struct net_device *dev,
133 struct e8390_pkt_hdr *hdr, int ring_page);
134static void sane_block_input(struct net_device * dev, int count,
135 struct sk_buff * skb, int ring_offset);
136static void sane_block_output(struct net_device * dev, int count,
137 const unsigned char * buf, const int start_page);
138
139/* dayna_memcpy to and from card */
140static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
141 int from, int count);
142static void dayna_memcpy_tocard(struct net_device *dev, int to,
143 const void *from, int count);
144
145/* Dayna - Dayna/Kinetics use this */
146static void dayna_get_8390_hdr(struct net_device *dev,
147 struct e8390_pkt_hdr *hdr, int ring_page);
148static void dayna_block_input(struct net_device *dev, int count,
149 struct sk_buff *skb, int ring_offset);
150static void dayna_block_output(struct net_device *dev, int count,
151 const unsigned char *buf, int start_page);
152
153#define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
154#define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
155
156/* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
157static void slow_sane_get_8390_hdr(struct net_device *dev,
158 struct e8390_pkt_hdr *hdr, int ring_page);
159static void slow_sane_block_input(struct net_device *dev, int count,
160 struct sk_buff *skb, int ring_offset);
161static void slow_sane_block_output(struct net_device *dev, int count,
162 const unsigned char *buf, int start_page);
163static void word_memcpy_tocard(void *tp, const void *fp, int count);
164static void word_memcpy_fromcard(void *tp, const void *fp, int count);
165
Adrian Bunk909fa882008-06-10 01:23:39 +0300166static enum mac8390_type __init mac8390_ident(struct nubus_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Finn Thain2964db02007-05-01 22:32:54 +0200168 switch (dev->dr_sw) {
Joe Perches35076402010-01-04 11:52:59 +0000169 case NUBUS_DRSW_3COM:
170 switch (dev->dr_hw) {
171 case NUBUS_DRHW_APPLE_SONIC_NB:
172 case NUBUS_DRHW_APPLE_SONIC_LC:
173 case NUBUS_DRHW_SONNET:
174 return MAC8390_NONE;
Finn Thain2964db02007-05-01 22:32:54 +0200175 break;
Joe Perches35076402010-01-04 11:52:59 +0000176 default:
177 return MAC8390_APPLE;
178 break;
179 }
180 break;
Finn Thain2964db02007-05-01 22:32:54 +0200181
Joe Perches35076402010-01-04 11:52:59 +0000182 case NUBUS_DRSW_APPLE:
183 switch (dev->dr_hw) {
184 case NUBUS_DRHW_ASANTE_LC:
185 return MAC8390_NONE;
Finn Thain2964db02007-05-01 22:32:54 +0200186 break;
Joe Perches35076402010-01-04 11:52:59 +0000187 case NUBUS_DRHW_CABLETRON:
188 return MAC8390_CABLETRON;
189 break;
190 default:
191 return MAC8390_APPLE;
192 break;
193 }
194 break;
Finn Thain2964db02007-05-01 22:32:54 +0200195
Joe Perches35076402010-01-04 11:52:59 +0000196 case NUBUS_DRSW_ASANTE:
197 return MAC8390_ASANTE;
198 break;
Finn Thain2964db02007-05-01 22:32:54 +0200199
Joe Perches35076402010-01-04 11:52:59 +0000200 case NUBUS_DRSW_TECHWORKS:
201 case NUBUS_DRSW_DAYNA2:
202 case NUBUS_DRSW_DAYNA_LC:
203 if (dev->dr_hw == NUBUS_DRHW_CABLETRON)
204 return MAC8390_CABLETRON;
205 else
206 return MAC8390_APPLE;
207 break;
Finn Thain2964db02007-05-01 22:32:54 +0200208
Joe Perches35076402010-01-04 11:52:59 +0000209 case NUBUS_DRSW_FARALLON:
210 return MAC8390_FARALLON;
211 break;
Finn Thain2964db02007-05-01 22:32:54 +0200212
Joe Perches35076402010-01-04 11:52:59 +0000213 case NUBUS_DRSW_KINETICS:
214 switch (dev->dr_hw) {
215 case NUBUS_DRHW_INTERLAN:
216 return MAC8390_INTERLAN;
Finn Thain2964db02007-05-01 22:32:54 +0200217 break;
Joe Perches35076402010-01-04 11:52:59 +0000218 default:
219 return MAC8390_KINETICS;
220 break;
221 }
222 break;
Finn Thain2964db02007-05-01 22:32:54 +0200223
Joe Perches35076402010-01-04 11:52:59 +0000224 case NUBUS_DRSW_DAYNA:
225 // These correspond to Dayna Sonic cards
226 // which use the macsonic driver
227 if (dev->dr_hw == NUBUS_DRHW_SMC9194 ||
228 dev->dr_hw == NUBUS_DRHW_INTERLAN )
229 return MAC8390_NONE;
230 else
231 return MAC8390_DAYNA;
232 break;
Finn Thain2964db02007-05-01 22:32:54 +0200233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return MAC8390_NONE;
235}
236
Adrian Bunk909fa882008-06-10 01:23:39 +0300237static enum mac8390_access __init mac8390_testio(volatile unsigned long membase)
Finn Thain2964db02007-05-01 22:32:54 +0200238{
239 unsigned long outdata = 0xA5A0B5B0;
240 unsigned long indata = 0x00000000;
241 /* Try writing 32 bits */
Joe Perches5c7fffd2010-01-04 11:53:00 +0000242 memcpy(membase, &outdata, 4);
Finn Thain2964db02007-05-01 22:32:54 +0200243 /* Now compare them */
244 if (memcmp((char *)&outdata, (char *)membase, 4) == 0)
245 return ACCESS_32;
246 /* Write 16 bit output */
Joe Perches5c7fffd2010-01-04 11:53:00 +0000247 word_memcpy_tocard(membase, &outdata, 4);
Finn Thain2964db02007-05-01 22:32:54 +0200248 /* Now read it back */
Joe Perches5c7fffd2010-01-04 11:53:00 +0000249 word_memcpy_fromcard(&indata, membase, 4);
Finn Thain2964db02007-05-01 22:32:54 +0200250 if (outdata == indata)
251 return ACCESS_16;
252 return ACCESS_UNKNOWN;
253}
254
Adrian Bunk909fa882008-06-10 01:23:39 +0300255static int __init mac8390_memsize(unsigned long membase)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 unsigned long flags;
258 int i, j;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 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;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* 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 /* in any case, we stopped once we tried one block too many,
285 or once we reached 32K */
286 return i * 0x1000;
287}
288
289struct net_device * __init mac8390_probe(int unit)
290{
291 struct net_device *dev;
292 volatile unsigned short *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct nubus_dev * ndev = NULL;
294 int err = -ENODEV;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct nubus_dir dir;
297 struct nubus_dirent ent;
298 int offset;
299 static unsigned int slots;
300
301 enum mac8390_type cardtype;
302
303 /* probably should check for Nubus instead */
304
305 if (!MACH_IS_MAC)
306 return ERR_PTR(-ENODEV);
307
Finn Thain217cbfa2009-05-25 22:43:49 -0700308 dev = ____alloc_ei_netdev(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!dev)
310 return ERR_PTR(-ENOMEM);
311
312 if (unit >= 0)
313 sprintf(dev->name, "eth%d", unit);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK, NUBUS_TYPE_ETHERNET, ndev))) {
316 /* Have we seen it already? */
317 if (slots & (1<<ndev->board->slot))
318 continue;
319 slots |= 1<<ndev->board->slot;
320
321 if ((cardtype = mac8390_ident(ndev)) == MAC8390_NONE)
322 continue;
323
Joe Perches18c00192010-01-04 11:53:01 +0000324 printk_once(KERN_INFO pr_fmt(version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 dev->irq = SLOT2IRQ(ndev->board->slot);
327 /* This is getting to be a habit */
328 dev->base_addr = ndev->board->slot_addr | ((ndev->board->slot&0xf) << 20);
329
330 /* Get some Nubus info - we will trust the card's idea
331 of where its memory and registers are. */
332
333 if (nubus_get_func_dir(ndev, &dir) == -1) {
Joe Perches18c00192010-01-04 11:53:01 +0000334 pr_err("%s: Unable to get Nubus functional directory for slot %X!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 dev->name, ndev->board->slot);
336 continue;
337 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* Get the MAC address */
340 if ((nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent)) == -1) {
Joe Perches18c00192010-01-04 11:53:01 +0000341 pr_info("%s: Couldn't get MAC address!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 continue;
343 } else {
344 nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (useresources[cardtype] == 1) {
348 nubus_rewinddir(&dir);
349 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS, &ent) == -1) {
Joe Perches18c00192010-01-04 11:53:01 +0000350 pr_err("%s: Memory offset resource for slot %X not found!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dev->name, ndev->board->slot);
352 continue;
353 }
354 nubus_get_rsrc_mem(&offset, &ent, 4);
355 dev->mem_start = dev->base_addr + offset;
356 /* yes, this is how the Apple driver does it */
357 dev->base_addr = dev->mem_start + 0x10000;
358 nubus_rewinddir(&dir);
359 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH, &ent) == -1) {
Joe Perches18c00192010-01-04 11:53:01 +0000360 pr_info("%s: Memory length resource for slot %X not found, probing\n",
361 dev->name, ndev->board->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 offset = mac8390_memsize(dev->mem_start);
363 } else {
364 nubus_get_rsrc_mem(&offset, &ent, 4);
365 }
366 dev->mem_end = dev->mem_start + offset;
367 } else {
368 switch (cardtype) {
Joe Perches35076402010-01-04 11:52:59 +0000369 case MAC8390_KINETICS:
370 case MAC8390_DAYNA: /* it's the same */
371 dev->base_addr =
372 (int)(ndev->board->slot_addr +
373 DAYNA_8390_BASE);
374 dev->mem_start =
375 (int)(ndev->board->slot_addr +
376 DAYNA_8390_MEM);
377 dev->mem_end =
378 dev->mem_start +
379 mac8390_memsize(dev->mem_start);
380 break;
381 case MAC8390_INTERLAN:
382 dev->base_addr =
383 (int)(ndev->board->slot_addr +
384 INTERLAN_8390_BASE);
385 dev->mem_start =
386 (int)(ndev->board->slot_addr +
387 INTERLAN_8390_MEM);
388 dev->mem_end =
389 dev->mem_start +
390 mac8390_memsize(dev->mem_start);
391 break;
392 case MAC8390_CABLETRON:
393 dev->base_addr =
394 (int)(ndev->board->slot_addr +
395 CABLETRON_8390_BASE);
396 dev->mem_start =
397 (int)(ndev->board->slot_addr +
398 CABLETRON_8390_MEM);
399 /* The base address is unreadable if 0x00
400 * has been written to the command register
401 * Reset the chip by writing E8390_NODMA +
402 * E8390_PAGE0 + E8390_STOP just to be
403 * sure
404 */
405 i = (void *)dev->base_addr;
406 *i = 0x21;
407 dev->mem_end =
408 dev->mem_start +
409 mac8390_memsize(dev->mem_start);
410 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400411
Joe Perches35076402010-01-04 11:52:59 +0000412 default:
Joe Perches18c00192010-01-04 11:53:01 +0000413 pr_err("Card type %s is unsupported, sorry\n",
Joe Perches35076402010-01-04 11:52:59 +0000414 ndev->board->name);
415 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 }
418
419 /* Do the nasty 8390 stuff */
420 if (!mac8390_initdev(dev, ndev, cardtype))
421 break;
422 }
423
424 if (!ndev)
425 goto out;
426 err = register_netdev(dev);
427 if (err)
428 goto out;
429 return dev;
430
431out:
432 free_netdev(dev);
433 return ERR_PTR(err);
434}
435
436#ifdef MODULE
437MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
438MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
439MODULE_LICENSE("GPL");
440
441/* overkill, of course */
442static struct net_device *dev_mac8390[15];
443int init_module(void)
444{
445 int i;
446 for (i = 0; i < 15; i++) {
447 struct net_device *dev = mac8390_probe(-1);
448 if (IS_ERR(dev))
449 break;
450 dev_mac890[i] = dev;
451 }
452 if (!i) {
Joe Perches18c00192010-01-04 11:53:01 +0000453 pr_notice("No useable cards found, driver NOT installed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -ENODEV;
455 }
456 return 0;
457}
458
459void cleanup_module(void)
460{
461 int i;
462 for (i = 0; i < 15; i++) {
463 struct net_device *dev = dev_mac890[i];
464 if (dev) {
465 unregister_netdev(dev);
466 free_netdev(dev);
467 }
468 }
469}
470
471#endif /* MODULE */
472
Stephen Hemmingerca175842008-12-02 15:00:28 -0800473static const struct net_device_ops mac8390_netdev_ops = {
474 .ndo_open = mac8390_open,
475 .ndo_stop = mac8390_close,
Finn Thain217cbfa2009-05-25 22:43:49 -0700476 .ndo_start_xmit = __ei_start_xmit,
477 .ndo_tx_timeout = __ei_tx_timeout,
478 .ndo_get_stats = __ei_get_stats,
479 .ndo_set_multicast_list = __ei_set_multicast_list,
Stephen Hemmingerca175842008-12-02 15:00:28 -0800480 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerfe96aaa2009-01-09 11:13:14 +0000481 .ndo_set_mac_address = eth_mac_addr,
Stephen Hemmingerca175842008-12-02 15:00:28 -0800482 .ndo_change_mtu = eth_change_mtu,
483#ifdef CONFIG_NET_POLL_CONTROLLER
Finn Thain4e0168f2009-05-28 02:05:53 +0000484 .ndo_poll_controller = __ei_poll,
Stephen Hemmingerca175842008-12-02 15:00:28 -0800485#endif
486};
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static int __init mac8390_initdev(struct net_device * dev, struct nubus_dev * ndev,
489 enum mac8390_type type)
490{
491 static u32 fwrd4_offsets[16]={
492 0, 4, 8, 12,
493 16, 20, 24, 28,
494 32, 36, 40, 44,
495 48, 52, 56, 60
496 };
497 static u32 back4_offsets[16]={
498 60, 56, 52, 48,
499 44, 40, 36, 32,
500 28, 24, 20, 16,
501 12, 8, 4, 0
502 };
503 static u32 fwrd2_offsets[16]={
504 0, 2, 4, 6,
505 8, 10, 12, 14,
506 16, 18, 20, 22,
507 24, 26, 28, 30
508 };
509
Finn Thain2964db02007-05-01 22:32:54 +0200510 int access_bitmode = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Now fill in our stuff */
Stephen Hemmingerca175842008-12-02 15:00:28 -0800513 dev->netdev_ops = &mac8390_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* GAR, ei_status is actually a macro even though it looks global */
516 ei_status.name = cardname[type];
517 ei_status.word16 = word16[type];
518
519 /* Cabletron's TX/RX buffers are backwards */
520 if (type == MAC8390_CABLETRON) {
521 ei_status.tx_start_page = CABLETRON_TX_START_PG;
522 ei_status.rx_start_page = CABLETRON_RX_START_PG;
523 ei_status.stop_page = CABLETRON_RX_STOP_PG;
524 ei_status.rmem_start = dev->mem_start;
525 ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
526 } else {
527 ei_status.tx_start_page = WD_START_PG;
528 ei_status.rx_start_page = WD_START_PG + TX_PAGES;
529 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
530 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
531 ei_status.rmem_end = dev->mem_end;
532 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* Fill in model-specific information and functions */
Joe Perches35076402010-01-04 11:52:59 +0000535 switch (type) {
Finn Thain2964db02007-05-01 22:32:54 +0200536 case MAC8390_FARALLON:
537 case MAC8390_APPLE:
Joe Perches35076402010-01-04 11:52:59 +0000538 switch (mac8390_testio(dev->mem_start)) {
539 case ACCESS_UNKNOWN:
Joe Perches18c00192010-01-04 11:53:01 +0000540 pr_info("Don't know how to access card memory!\n");
Joe Perches35076402010-01-04 11:52:59 +0000541 return -ENODEV;
542 break;
Finn Thain2964db02007-05-01 22:32:54 +0200543
Joe Perches35076402010-01-04 11:52:59 +0000544 case ACCESS_16:
545 /* 16 bit card, register map is reversed */
546 ei_status.reset_8390 = &mac8390_no_reset;
547 ei_status.block_input = &slow_sane_block_input;
548 ei_status.block_output = &slow_sane_block_output;
549 ei_status.get_8390_hdr = &slow_sane_get_8390_hdr;
550 ei_status.reg_offset = back4_offsets;
551 break;
Finn Thain2964db02007-05-01 22:32:54 +0200552
Joe Perches35076402010-01-04 11:52:59 +0000553 case ACCESS_32:
554 /* 32 bit card, register map is reversed */
555 ei_status.reset_8390 = &mac8390_no_reset;
556 ei_status.block_input = &sane_block_input;
557 ei_status.block_output = &sane_block_output;
558 ei_status.get_8390_hdr = &sane_get_8390_hdr;
559 ei_status.reg_offset = back4_offsets;
560 access_bitmode = 1;
561 break;
Finn Thain2964db02007-05-01 22:32:54 +0200562 }
563 break;
564
565 case MAC8390_ASANTE:
566 /* Some Asante cards pass the 32 bit test
567 * but overwrite system memory when run at 32 bit.
568 * so we run them all at 16 bit.
569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
Finn Thain2964db02007-05-01 22:32:54 +0200576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 case MAC8390_CABLETRON:
578 /* 16 bit card, register map is short forward */
579 ei_status.reset_8390 = &mac8390_no_reset;
580 ei_status.block_input = &slow_sane_block_input;
581 ei_status.block_output = &slow_sane_block_output;
582 ei_status.get_8390_hdr = &slow_sane_get_8390_hdr;
583 ei_status.reg_offset = fwrd2_offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 break;
Finn Thain2964db02007-05-01 22:32:54 +0200585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 case MAC8390_DAYNA:
587 case MAC8390_KINETICS:
Finn Thain2964db02007-05-01 22:32:54 +0200588 /* 16 bit memory, register map is forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* dayna and similar */
590 ei_status.reset_8390 = &mac8390_no_reset;
591 ei_status.block_input = &dayna_block_input;
592 ei_status.block_output = &dayna_block_output;
593 ei_status.get_8390_hdr = &dayna_get_8390_hdr;
594 ei_status.reg_offset = fwrd4_offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
Finn Thain2964db02007-05-01 22:32:54 +0200596
597 case MAC8390_INTERLAN:
598 /* 16 bit memory, register map is forward */
599 ei_status.reset_8390 = &interlan_reset;
600 ei_status.block_input = &slow_sane_block_input;
601 ei_status.block_output = &slow_sane_block_output;
602 ei_status.get_8390_hdr = &slow_sane_get_8390_hdr;
603 ei_status.reg_offset = fwrd4_offsets;
604 break;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 default:
Joe Perches18c00192010-01-04 11:53:01 +0000607 pr_err("Card type %s is unsupported, sorry\n",
608 ndev->board->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -ENODEV;
610 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400611
Al Viro8c6270f2006-10-10 00:19:36 +0100612 __NS8390_init(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* Good, done, now spit out some messages */
Joe Perches18c00192010-01-04 11:53:01 +0000615 pr_info("%s: %s in slot %X (type %s)\n",
616 dev->name, ndev->board->name, ndev->board->slot,
617 cardname[type]);
618 pr_info("MAC %pM IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
619 dev->dev_addr, dev->irq,
620 (unsigned int)(dev->mem_end - dev->mem_start) >> 10,
621 dev->mem_start, access_bitmode ? 32 : 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
624
625static int mac8390_open(struct net_device *dev)
626{
Al Viro8c6270f2006-10-10 00:19:36 +0100627 __ei_open(dev);
628 if (request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev)) {
Joe Perches18c00192010-01-04 11:53:01 +0000629 pr_info("%s: unable to get IRQ %d.\n", dev->name, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -EAGAIN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
633}
634
635static int mac8390_close(struct net_device *dev)
636{
637 free_irq(dev->irq, dev);
Al Viro8c6270f2006-10-10 00:19:36 +0100638 __ei_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640}
641
642static void mac8390_no_reset(struct net_device *dev)
643{
644 ei_status.txing = 0;
645 if (ei_debug > 1)
Joe Perches18c00192010-01-04 11:53:01 +0000646 pr_info("reset not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return;
648}
649
Finn Thain2964db02007-05-01 22:32:54 +0200650static void interlan_reset(struct net_device *dev)
651{
652 unsigned char *target=nubus_slot_addr(IRQ2SLOT(dev->irq));
653 if (ei_debug > 1)
Joe Perches18c00192010-01-04 11:53:01 +0000654 pr_info("Need to reset the NS8390 t=%lu...", jiffies);
Finn Thain2964db02007-05-01 22:32:54 +0200655 ei_status.txing = 0;
656 target[0xC0000] = 0;
657 if (ei_debug > 1)
Joe Perches18c00192010-01-04 11:53:01 +0000658 pr_cont("reset complete\n");
Finn Thain2964db02007-05-01 22:32:54 +0200659 return;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/* dayna_memcpy_fromio/dayna_memcpy_toio */
663/* directly from daynaport.c by Alan Cox */
664static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from, int count)
665{
Al Viro09cc07a2006-01-12 01:06:21 -0800666 volatile unsigned char *ptr;
667 unsigned char *target=to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 from<<=1; /* word, skip overhead */
Al Viro09cc07a2006-01-12 01:06:21 -0800669 ptr=(unsigned char *)(dev->mem_start+from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* Leading byte? */
671 if (from&2) {
Al Viro09cc07a2006-01-12 01:06:21 -0800672 *target++ = ptr[-1];
673 ptr += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 count--;
675 }
676 while(count>=2)
677 {
Al Viro09cc07a2006-01-12 01:06:21 -0800678 *(unsigned short *)target = *(unsigned short volatile *)ptr;
679 ptr += 4; /* skip cruft */
680 target += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 count-=2;
682 }
683 /* Trailing byte? */
684 if(count)
Al Viro09cc07a2006-01-12 01:06:21 -0800685 *target = *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688static void dayna_memcpy_tocard(struct net_device *dev, int to, const void *from, int count)
689{
690 volatile unsigned short *ptr;
Al Viro09cc07a2006-01-12 01:06:21 -0800691 const unsigned char *src=from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 to<<=1; /* word, skip overhead */
693 ptr=(unsigned short *)(dev->mem_start+to);
694 /* Leading byte? */
695 if (to&2) { /* avoid a byte write (stomps on other data) */
Al Viro09cc07a2006-01-12 01:06:21 -0800696 ptr[-1] = (ptr[-1]&0xFF00)|*src++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ptr++;
698 count--;
699 }
700 while(count>=2)
701 {
Al Viro09cc07a2006-01-12 01:06:21 -0800702 *ptr++=*(unsigned short *)src; /* Copy and */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ptr++; /* skip cruft */
Al Viro09cc07a2006-01-12 01:06:21 -0800704 src += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 count-=2;
706 }
707 /* Trailing byte? */
708 if(count)
709 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* card doesn't like byte writes */
Al Viro09cc07a2006-01-12 01:06:21 -0800711 *ptr=(*ptr&0x00FF)|(*src << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713}
714
715/* sane block input/output */
716static void sane_get_8390_hdr(struct net_device *dev,
717 struct e8390_pkt_hdr *hdr, int ring_page)
718{
719 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
720 memcpy_fromio((void *)hdr, (char *)dev->mem_start + hdr_start, 4);
721 /* Fix endianness */
722 hdr->count = swab16(hdr->count);
723}
724
725static void sane_block_input(struct net_device *dev, int count,
726 struct sk_buff *skb, int ring_offset)
727{
728 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
729 unsigned long xfer_start = xfer_base + dev->mem_start;
730
731 if (xfer_start + count > ei_status.rmem_end) {
732 /* We must wrap the input move. */
733 int semi_count = ei_status.rmem_end - xfer_start;
734 memcpy_fromio(skb->data, (char *)dev->mem_start + xfer_base, semi_count);
735 count -= semi_count;
736 memcpy_toio(skb->data + semi_count, (char *)ei_status.rmem_start, count);
737 } else {
738 memcpy_fromio(skb->data, (char *)dev->mem_start + xfer_base, count);
739 }
740}
741
742static void sane_block_output(struct net_device *dev, int count,
743 const unsigned char *buf, int start_page)
744{
745 long shmem = (start_page - WD_START_PG)<<8;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 memcpy_toio((char *)dev->mem_start + shmem, buf, count);
748}
749
750/* dayna block input/output */
751static void dayna_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
752{
753 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
754
Joe Perches5c7fffd2010-01-04 11:53:00 +0000755 dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* Fix endianness */
757 hdr->count=(hdr->count&0xFF)<<8|(hdr->count>>8);
758}
759
760static void dayna_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
761{
762 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
763 unsigned long xfer_start = xfer_base+dev->mem_start;
764
765 /* Note the offset math is done in card memory space which is word
766 per long onto our space. */
767
768 if (xfer_start + count > ei_status.rmem_end)
769 {
770 /* We must wrap the input move. */
771 int semi_count = ei_status.rmem_end - xfer_start;
772 dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
773 count -= semi_count;
774 dayna_memcpy_fromcard(dev, skb->data + semi_count,
775 ei_status.rmem_start - dev->mem_start,
776 count);
777 }
778 else
779 {
780 dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
781 }
782}
783
784static void dayna_block_output(struct net_device *dev, int count, const unsigned char *buf,
785 int start_page)
786{
787 long shmem = (start_page - WD_START_PG)<<8;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 dayna_memcpy_tocard(dev, shmem, buf, count);
790}
791
792/* Cabletron block I/O */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400793static void slow_sane_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int ring_page)
795{
796 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
Joe Perches5c7fffd2010-01-04 11:53:00 +0000797 word_memcpy_fromcard(hdr, (char *)dev->mem_start + hdr_start, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* Register endianism - fix here rather than 8390.c */
799 hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
800}
801
802static void slow_sane_block_input(struct net_device *dev, int count, struct sk_buff *skb,
803 int ring_offset)
804{
805 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
806 unsigned long xfer_start = xfer_base+dev->mem_start;
807
808 if (xfer_start + count > ei_status.rmem_end)
809 {
810 /* We must wrap the input move. */
811 int semi_count = ei_status.rmem_end - xfer_start;
Joe Perches5c7fffd2010-01-04 11:53:00 +0000812 word_memcpy_fromcard(skb->data,
813 (char *)dev->mem_start + xfer_base,
814 semi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 count -= semi_count;
816 word_memcpy_fromcard(skb->data + semi_count,
817 (char *)ei_status.rmem_start, count);
818 }
819 else
820 {
Joe Perches5c7fffd2010-01-04 11:53:00 +0000821 word_memcpy_fromcard(skb->data,
822 (char *)dev->mem_start + xfer_base, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824}
825
826static void slow_sane_block_output(struct net_device *dev, int count, const unsigned char *buf,
827 int start_page)
828{
829 long shmem = (start_page - WD_START_PG)<<8;
830
831 word_memcpy_tocard((char *)dev->mem_start + shmem, buf, count);
832}
833
834static void word_memcpy_tocard(void *tp, const void *fp, int count)
835{
836 volatile unsigned short *to = tp;
837 const unsigned short *from = fp;
838
839 count++;
840 count/=2;
841
842 while(count--)
843 *to++=*from++;
844}
845
846static void word_memcpy_fromcard(void *tp, const void *fp, int count)
847{
848 unsigned short *to = tp;
849 const volatile unsigned short *from = fp;
850
851 count++;
852 count/=2;
853
854 while(count--)
855 *to++=*from++;
856}
857
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400858