mac8390.c 23.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/* mac8390.c: New driver for 8390-based Nubus (or Nubus-alike)
   Ethernet cards on Linux */
/* Based on the former daynaport.c driver, by Alan Cox.  Some code
   taken from or inspired by skeleton.c by Donald Becker, acenic.c by
   Jes Sorensen, and ne2k-pci.c by Donald Becker and Paul Gortmaker.

   This software may be used and distributed according to the terms of
   the GNU Public License, incorporated herein by reference.  */

10
/* 2000-02-28: support added for Dayna and Kinetics cards by
L
Linus Torvalds 已提交
11 12 13 14
   A.G.deWijn@phys.uu.nl */
/* 2000-04-04: support added for Dayna2 by bart@etpmod.phys.tue.nl */
/* 2001-04-18: support for DaynaPort E/LC-M by rayk@knightsmanor.org */
/* 2001-05-15: support for Cabletron ported from old daynaport driver
15
 * and fixed access to Sonic Sys card which masquerades as a Farallon
L
Linus Torvalds 已提交
16
 * by rayk@knightsmanor.org */
F
Finn Thain 已提交
17 18
/* 2002-12-30: Try to support more cards, some clues from NetBSD driver */
/* 2003-12-26: Make sure Asante cards always work. */
L
Linus Torvalds 已提交
19

20 21
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/nubus.h>
#include <linux/in.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/bitops.h>
38
#include <linux/io.h>
L
Linus Torvalds 已提交
39 40 41 42 43

#include <asm/dma.h>
#include <asm/hwtest.h>
#include <asm/macints.h>

A
Al Viro 已提交
44
static char version[] =
45
	"v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
A
Al Viro 已提交
46 47

#define EI_SHIFT(x)	(ei_local->reg_offset[x])
48 49 50 51
#define ei_inb(port)	in_8(port)
#define ei_outb(val, port)	out_8(port, val)
#define ei_inb_p(port)	in_8(port)
#define ei_outb_p(val, port)	out_8(port, val)
A
Al Viro 已提交
52 53

#include "lib8390.c"
L
Linus Torvalds 已提交
54 55 56 57

#define WD_START_PG			0x00	/* First page of TX buffer */
#define CABLETRON_RX_START_PG		0x00    /* First page of RX buffer */
#define CABLETRON_RX_STOP_PG		0x30    /* Last page +1 of RX ring */
58 59
#define CABLETRON_TX_START_PG		CABLETRON_RX_STOP_PG
						/* First page of TX buffer */
L
Linus Torvalds 已提交
60

61 62 63 64 65 66
/*
 * Unfortunately it seems we have to hardcode these for the moment
 * 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?
 */
L
Linus Torvalds 已提交
67 68 69 70

#define DAYNA_8390_BASE		0x80000
#define DAYNA_8390_MEM		0x00000

71
#define CABLETRON_8390_BASE	0x90000
L
Linus Torvalds 已提交
72 73
#define CABLETRON_8390_MEM	0x00000

F
Finn Thain 已提交
74 75 76
#define INTERLAN_8390_BASE	0xE0000
#define INTERLAN_8390_MEM	0xD0000

L
Linus Torvalds 已提交
77 78 79 80
enum mac8390_type {
	MAC8390_NONE = -1,
	MAC8390_APPLE,
	MAC8390_ASANTE,
F
Finn Thain 已提交
81
	MAC8390_FARALLON,
L
Linus Torvalds 已提交
82 83 84 85 86 87
	MAC8390_CABLETRON,
	MAC8390_DAYNA,
	MAC8390_INTERLAN,
	MAC8390_KINETICS,
};

88
static const char *cardname[] = {
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97
	"apple",
	"asante",
	"farallon",
	"cabletron",
	"dayna",
	"interlan",
	"kinetics",
};

98
static const int word16[] = {
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108
	1, /* apple */
	1, /* asante */
	1, /* farallon */
	1, /* cabletron */
	0, /* dayna */
	1, /* interlan */
	0, /* kinetics */
};

/* on which cards do we use NuBus resources? */
109
static const int useresources[] = {
L
Linus Torvalds 已提交
110 111 112 113 114 115 116
	1, /* apple */
	1, /* asante */
	1, /* farallon */
	0, /* cabletron */
	0, /* dayna */
	0, /* interlan */
	0, /* kinetics */
F
Finn Thain 已提交
117 118 119 120 121 122
};

enum mac8390_access {
	ACCESS_UNKNOWN = 0,
	ACCESS_32,
	ACCESS_16,
L
Linus Torvalds 已提交
123 124
};

125 126
extern int mac8390_memtest(struct net_device *dev);
static int mac8390_initdev(struct net_device *dev, struct nubus_dev *ndev,
L
Linus Torvalds 已提交
127 128
			   enum mac8390_type type);

129 130
static int mac8390_open(struct net_device *dev);
static int mac8390_close(struct net_device *dev);
L
Linus Torvalds 已提交
131
static void mac8390_no_reset(struct net_device *dev);
F
Finn Thain 已提交
132
static void interlan_reset(struct net_device *dev);
L
Linus Torvalds 已提交
133

F
Finn Thain 已提交
134
/* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
L
Linus Torvalds 已提交
135 136
static void sane_get_8390_hdr(struct net_device *dev,
			      struct e8390_pkt_hdr *hdr, int ring_page);
137 138 139 140
static void sane_block_input(struct net_device *dev, int count,
			     struct sk_buff *skb, int ring_offset);
static void sane_block_output(struct net_device *dev, int count,
			      const unsigned char *buf, const int start_page);
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

/* dayna_memcpy to and from card */
static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
				int from, int count);
static void dayna_memcpy_tocard(struct net_device *dev, int to,
			      const void *from, int count);

/* Dayna - Dayna/Kinetics use this */
static void dayna_get_8390_hdr(struct net_device *dev,
			       struct e8390_pkt_hdr *hdr, int ring_page);
static void dayna_block_input(struct net_device *dev, int count,
			      struct sk_buff *skb, int ring_offset);
static void dayna_block_output(struct net_device *dev, int count,
			       const unsigned char *buf, int start_page);

156 157
#define memcpy_fromio(a, b, c)	memcpy((a), (void *)(b), (c))
#define memcpy_toio(a, b, c)	memcpy((void *)(a), (b), (c))
L
Linus Torvalds 已提交
158

159 160
#define memcmp_withio(a, b, c)	memcmp((a), (void *)(b), (c))

L
Linus Torvalds 已提交
161 162 163 164 165 166 167
/* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
static void slow_sane_get_8390_hdr(struct net_device *dev,
				   struct e8390_pkt_hdr *hdr, int ring_page);
static void slow_sane_block_input(struct net_device *dev, int count,
				  struct sk_buff *skb, int ring_offset);
static void slow_sane_block_output(struct net_device *dev, int count,
				   const unsigned char *buf, int start_page);
168 169
static void word_memcpy_tocard(unsigned long tp, const void *fp, int count);
static void word_memcpy_fromcard(void *tp, unsigned long fp, int count);
170
static u32 mac8390_msg_enable;
L
Linus Torvalds 已提交
171

172
static enum mac8390_type __init mac8390_ident(struct nubus_dev *dev)
L
Linus Torvalds 已提交
173
{
F
Finn Thain 已提交
174
	switch (dev->dr_sw) {
175 176 177 178 179 180
	case NUBUS_DRSW_3COM:
		switch (dev->dr_hw) {
		case NUBUS_DRHW_APPLE_SONIC_NB:
		case NUBUS_DRHW_APPLE_SONIC_LC:
		case NUBUS_DRHW_SONNET:
			return MAC8390_NONE;
F
Finn Thain 已提交
181
			break;
182 183
		default:
			return MAC8390_APPLE;
F
Finn Thain 已提交
184
			break;
185 186
		}
		break;
F
Finn Thain 已提交
187

188 189 190 191
	case NUBUS_DRSW_APPLE:
		switch (dev->dr_hw) {
		case NUBUS_DRHW_ASANTE_LC:
			return MAC8390_NONE;
F
Finn Thain 已提交
192
			break;
193 194
		case NUBUS_DRHW_CABLETRON:
			return MAC8390_CABLETRON;
F
Finn Thain 已提交
195
			break;
196 197
		default:
			return MAC8390_APPLE;
F
Finn Thain 已提交
198
			break;
199 200
		}
		break;
F
Finn Thain 已提交
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	case NUBUS_DRSW_ASANTE:
		return MAC8390_ASANTE;
		break;

	case NUBUS_DRSW_TECHWORKS:
	case NUBUS_DRSW_DAYNA2:
	case NUBUS_DRSW_DAYNA_LC:
		if (dev->dr_hw == NUBUS_DRHW_CABLETRON)
			return MAC8390_CABLETRON;
		else
			return MAC8390_APPLE;
		break;

	case NUBUS_DRSW_FARALLON:
		return MAC8390_FARALLON;
		break;
F
Finn Thain 已提交
218

219 220 221 222
	case NUBUS_DRSW_KINETICS:
		switch (dev->dr_hw) {
		case NUBUS_DRHW_INTERLAN:
			return MAC8390_INTERLAN;
F
Finn Thain 已提交
223
			break;
224 225 226 227 228 229 230
		default:
			return MAC8390_KINETICS;
			break;
		}
		break;

	case NUBUS_DRSW_DAYNA:
231 232 233 234
		/*
		 * These correspond to Dayna Sonic cards
		 * which use the macsonic driver
		 */
235
		if (dev->dr_hw == NUBUS_DRHW_SMC9194 ||
236
		    dev->dr_hw == NUBUS_DRHW_INTERLAN)
237 238 239 240
			return MAC8390_NONE;
		else
			return MAC8390_DAYNA;
		break;
F
Finn Thain 已提交
241
	}
L
Linus Torvalds 已提交
242 243 244
	return MAC8390_NONE;
}

245
static enum mac8390_access __init mac8390_testio(volatile unsigned long membase)
F
Finn Thain 已提交
246 247 248 249
{
	unsigned long outdata = 0xA5A0B5B0;
	unsigned long indata =  0x00000000;
	/* Try writing 32 bits */
250
	memcpy_toio(membase, &outdata, 4);
F
Finn Thain 已提交
251
	/* Now compare them */
252
	if (memcmp_withio(&outdata, membase, 4) == 0)
F
Finn Thain 已提交
253 254
		return ACCESS_32;
	/* Write 16 bit output */
255
	word_memcpy_tocard(membase, &outdata, 4);
F
Finn Thain 已提交
256
	/* Now read it back */
257
	word_memcpy_fromcard(&indata, membase, 4);
F
Finn Thain 已提交
258 259 260 261 262
	if (outdata == indata)
		return ACCESS_16;
	return ACCESS_UNKNOWN;
}

263
static int __init mac8390_memsize(unsigned long membase)
L
Linus Torvalds 已提交
264 265 266
{
	unsigned long flags;
	int i, j;
267

L
Linus Torvalds 已提交
268 269 270
	local_irq_save(flags);
	/* Check up to 32K in 4K increments */
	for (i = 0; i < 8; i++) {
271
		volatile unsigned short *m = (unsigned short *)(membase + (i * 0x1000));
L
Linus Torvalds 已提交
272 273 274 275 276

		/* Unwriteable - we have a fully decoded card and the
		   RAM end located */
		if (hwreg_present(m) == 0)
			break;
277

L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285
		/* write a distinctive byte */
		*m = 0xA5A0 | i;
		/* check that we read back what we wrote */
		if (*m != (0xA5A0 | i))
			break;

		/* check for partial decode and wrap */
		for (j = 0; j < i; j++) {
286
			volatile unsigned short *p = (unsigned short *)(membase + (j * 0x1000));
L
Linus Torvalds 已提交
287 288
			if (*p != (0xA5A0 | j))
				break;
289 290
		}
	}
L
Linus Torvalds 已提交
291
	local_irq_restore(flags);
292 293 294 295 296
	/*
	 * in any case, we stopped once we tried one block too many,
	 * or once we reached 32K
	 */
	return i * 0x1000;
L
Linus Torvalds 已提交
297 298
}

299 300 301 302 303 304 305 306
static bool __init mac8390_init(struct net_device *dev, struct nubus_dev *ndev,
				enum mac8390_type cardtype)
{
	struct nubus_dir dir;
	struct nubus_dirent ent;
	int offset;
	volatile unsigned short *i;

G
Geert Uytterhoeven 已提交
307
	printk_once(KERN_INFO pr_fmt("%s"), version);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

	dev->irq = SLOT2IRQ(ndev->board->slot);
	/* This is getting to be a habit */
	dev->base_addr = (ndev->board->slot_addr |
			  ((ndev->board->slot & 0xf) << 20));

	/*
	 * Get some Nubus info - we will trust the card's idea
	 * of where its memory and registers are.
	 */

	if (nubus_get_func_dir(ndev, &dir) == -1) {
		pr_err("%s: Unable to get Nubus functional directory for slot %X!\n",
		       dev->name, ndev->board->slot);
		return false;
	}

	/* Get the MAC address */
	if (nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent) == -1) {
		pr_info("%s: Couldn't get MAC address!\n", dev->name);
		return false;
	}

	nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);

	if (useresources[cardtype] == 1) {
		nubus_rewinddir(&dir);
		if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS,
				    &ent) == -1) {
			pr_err("%s: Memory offset resource for slot %X not found!\n",
			       dev->name, ndev->board->slot);
			return false;
		}
		nubus_get_rsrc_mem(&offset, &ent, 4);
		dev->mem_start = dev->base_addr + offset;
		/* yes, this is how the Apple driver does it */
		dev->base_addr = dev->mem_start + 0x10000;
		nubus_rewinddir(&dir);
		if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH,
				    &ent) == -1) {
			pr_info("%s: Memory length resource for slot %X not found, probing\n",
				dev->name, ndev->board->slot);
			offset = mac8390_memsize(dev->mem_start);
		} else {
			nubus_get_rsrc_mem(&offset, &ent, 4);
		}
		dev->mem_end = dev->mem_start + offset;
	} else {
		switch (cardtype) {
		case MAC8390_KINETICS:
		case MAC8390_DAYNA: /* it's the same */
			dev->base_addr = (int)(ndev->board->slot_addr +
					       DAYNA_8390_BASE);
			dev->mem_start = (int)(ndev->board->slot_addr +
					       DAYNA_8390_MEM);
			dev->mem_end = dev->mem_start +
				       mac8390_memsize(dev->mem_start);
			break;
		case MAC8390_INTERLAN:
			dev->base_addr = (int)(ndev->board->slot_addr +
					       INTERLAN_8390_BASE);
			dev->mem_start = (int)(ndev->board->slot_addr +
					       INTERLAN_8390_MEM);
			dev->mem_end = dev->mem_start +
				       mac8390_memsize(dev->mem_start);
			break;
		case MAC8390_CABLETRON:
			dev->base_addr = (int)(ndev->board->slot_addr +
					       CABLETRON_8390_BASE);
			dev->mem_start = (int)(ndev->board->slot_addr +
					       CABLETRON_8390_MEM);
			/* The base address is unreadable if 0x00
			 * has been written to the command register
			 * Reset the chip by writing E8390_NODMA +
			 *   E8390_PAGE0 + E8390_STOP just to be
			 *   sure
			 */
			i = (void *)dev->base_addr;
			*i = 0x21;
			dev->mem_end = dev->mem_start +
				       mac8390_memsize(dev->mem_start);
			break;

		default:
			pr_err("Card type %s is unsupported, sorry\n",
			       ndev->board->name);
			return false;
		}
	}

	return true;
}

L
Linus Torvalds 已提交
401 402 403
struct net_device * __init mac8390_probe(int unit)
{
	struct net_device *dev;
404
	struct nubus_dev *ndev = NULL;
L
Linus Torvalds 已提交
405
	int err = -ENODEV;
406
	struct ei_device *ei_local;
407

L
Linus Torvalds 已提交
408 409 410 411 412 413 414 415 416
	static unsigned int slots;

	enum mac8390_type cardtype;

	/* probably should check for Nubus instead */

	if (!MACH_IS_MAC)
		return ERR_PTR(-ENODEV);

417
	dev = ____alloc_ei_netdev(0);
L
Linus Torvalds 已提交
418 419 420 421 422 423
	if (!dev)
		return ERR_PTR(-ENOMEM);

	if (unit >= 0)
		sprintf(dev->name, "eth%d", unit);

424 425
	while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK, NUBUS_TYPE_ETHERNET,
				       ndev))) {
L
Linus Torvalds 已提交
426
		/* Have we seen it already? */
427
		if (slots & (1 << ndev->board->slot))
L
Linus Torvalds 已提交
428
			continue;
429
		slots |= 1 << ndev->board->slot;
L
Linus Torvalds 已提交
430

431 432
		cardtype = mac8390_ident(ndev);
		if (cardtype == MAC8390_NONE)
L
Linus Torvalds 已提交
433 434
			continue;

435
		if (!mac8390_init(dev, ndev, cardtype))
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444
			continue;

		/* Do the nasty 8390 stuff */
		if (!mac8390_initdev(dev, ndev, cardtype))
			break;
	}

	if (!ndev)
		goto out;
445 446 447 448

	 ei_local = netdev_priv(dev);
	 ei_local->msg_enable = mac8390_msg_enable;

L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	err = register_netdev(dev);
	if (err)
		goto out;
	return dev;

out:
	free_netdev(dev);
	return ERR_PTR(err);
}

#ifdef MODULE
MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
MODULE_LICENSE("GPL");

/* overkill, of course */
static struct net_device *dev_mac8390[15];
int init_module(void)
{
	int i;
	for (i = 0; i < 15; i++) {
		struct net_device *dev = mac8390_probe(-1);
		if (IS_ERR(dev))
			break;
		dev_mac890[i] = dev;
	}
	if (!i) {
476
		pr_notice("No useable cards found, driver NOT installed.\n");
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
		return -ENODEV;
	}
	return 0;
}

void cleanup_module(void)
{
	int i;
	for (i = 0; i < 15; i++) {
		struct net_device *dev = dev_mac890[i];
		if (dev) {
			unregister_netdev(dev);
			free_netdev(dev);
		}
	}
}

#endif /* MODULE */

496 497 498
static const struct net_device_ops mac8390_netdev_ops = {
	.ndo_open 		= mac8390_open,
	.ndo_stop		= mac8390_close,
499 500 501
	.ndo_start_xmit		= __ei_start_xmit,
	.ndo_tx_timeout		= __ei_tx_timeout,
	.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 506
	.ndo_change_mtu		= eth_change_mtu,
#ifdef CONFIG_NET_POLL_CONTROLLER
507
	.ndo_poll_controller	= __ei_poll,
508 509 510
#endif
};

511 512 513
static int __init mac8390_initdev(struct net_device *dev,
				  struct nubus_dev *ndev,
				  enum mac8390_type type)
L
Linus Torvalds 已提交
514
{
515
	static u32 fwrd4_offsets[16] = {
L
Linus Torvalds 已提交
516 517 518 519 520
		0,      4,      8,      12,
		16,     20,     24,     28,
		32,     36,     40,     44,
		48,     52,     56,     60
	};
521
	static u32 back4_offsets[16] = {
L
Linus Torvalds 已提交
522 523 524 525 526
		60,     56,     52,     48,
		44,     40,     36,     32,
		28,     24,     20,     16,
		12,     8,      4,      0
	};
527
	static u32 fwrd2_offsets[16] = {
L
Linus Torvalds 已提交
528 529 530 531 532 533
		0,      2,      4,      6,
		8,     10,     12,     14,
		16,    18,     20,     22,
		24,    26,     28,     30
	};

F
Finn Thain 已提交
534
	int access_bitmode = 0;
535

L
Linus Torvalds 已提交
536
	/* Now fill in our stuff */
537
	dev->netdev_ops = &mac8390_netdev_ops;
L
Linus Torvalds 已提交
538 539 540 541 542 543 544

	/* GAR, ei_status is actually a macro even though it looks global */
	ei_status.name = cardname[type];
	ei_status.word16 = word16[type];

	/* Cabletron's TX/RX buffers are backwards */
	if (type == MAC8390_CABLETRON) {
545 546 547 548 549
		ei_status.tx_start_page = CABLETRON_TX_START_PG;
		ei_status.rx_start_page = CABLETRON_RX_START_PG;
		ei_status.stop_page = CABLETRON_RX_STOP_PG;
		ei_status.rmem_start = dev->mem_start;
		ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
L
Linus Torvalds 已提交
550
	} else {
551 552 553 554 555
		ei_status.tx_start_page = WD_START_PG;
		ei_status.rx_start_page = WD_START_PG + TX_PAGES;
		ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
		ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
		ei_status.rmem_end = dev->mem_end;
L
Linus Torvalds 已提交
556
	}
557

L
Linus Torvalds 已提交
558
	/* Fill in model-specific information and functions */
559
	switch (type) {
L
Linus Torvalds 已提交
560 561
	case MAC8390_FARALLON:
	case MAC8390_APPLE:
562 563
		switch (mac8390_testio(dev->mem_start)) {
		case ACCESS_UNKNOWN:
564
			pr_err("Don't know how to access card memory!\n");
565 566
			return -ENODEV;
			break;
F
Finn Thain 已提交
567

568 569
		case ACCESS_16:
			/* 16 bit card, register map is reversed */
570 571 572 573
			ei_status.reset_8390 = mac8390_no_reset;
			ei_status.block_input = slow_sane_block_input;
			ei_status.block_output = slow_sane_block_output;
			ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
574 575
			ei_status.reg_offset = back4_offsets;
			break;
F
Finn Thain 已提交
576

577 578
		case ACCESS_32:
			/* 32 bit card, register map is reversed */
579 580 581 582
			ei_status.reset_8390 = mac8390_no_reset;
			ei_status.block_input = sane_block_input;
			ei_status.block_output = sane_block_output;
			ei_status.get_8390_hdr = sane_get_8390_hdr;
583 584 585
			ei_status.reg_offset = back4_offsets;
			access_bitmode = 1;
			break;
F
Finn Thain 已提交
586 587 588
		}
		break;

L
Linus Torvalds 已提交
589
	case MAC8390_ASANTE:
F
Finn Thain 已提交
590 591 592 593
		/* Some Asante cards pass the 32 bit test
		 * but overwrite system memory when run at 32 bit.
		 * so we run them all at 16 bit.
		 */
594 595 596 597
		ei_status.reset_8390 = mac8390_no_reset;
		ei_status.block_input = slow_sane_block_input;
		ei_status.block_output = slow_sane_block_output;
		ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
L
Linus Torvalds 已提交
598 599
		ei_status.reg_offset = back4_offsets;
		break;
F
Finn Thain 已提交
600

L
Linus Torvalds 已提交
601 602
	case MAC8390_CABLETRON:
		/* 16 bit card, register map is short forward */
603 604 605 606
		ei_status.reset_8390 = mac8390_no_reset;
		ei_status.block_input = slow_sane_block_input;
		ei_status.block_output = slow_sane_block_output;
		ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
L
Linus Torvalds 已提交
607 608
		ei_status.reg_offset = fwrd2_offsets;
		break;
F
Finn Thain 已提交
609

L
Linus Torvalds 已提交
610 611
	case MAC8390_DAYNA:
	case MAC8390_KINETICS:
F
Finn Thain 已提交
612
		/* 16 bit memory, register map is forward */
L
Linus Torvalds 已提交
613
		/* dayna and similar */
614 615 616 617
		ei_status.reset_8390 = mac8390_no_reset;
		ei_status.block_input = dayna_block_input;
		ei_status.block_output = dayna_block_output;
		ei_status.get_8390_hdr = dayna_get_8390_hdr;
L
Linus Torvalds 已提交
618 619
		ei_status.reg_offset = fwrd4_offsets;
		break;
F
Finn Thain 已提交
620 621 622

	case MAC8390_INTERLAN:
		/* 16 bit memory, register map is forward */
623 624 625 626
		ei_status.reset_8390 = interlan_reset;
		ei_status.block_input = slow_sane_block_input;
		ei_status.block_output = slow_sane_block_output;
		ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
627 628
		ei_status.reg_offset = fwrd4_offsets;
		break;
F
Finn Thain 已提交
629

L
Linus Torvalds 已提交
630
	default:
631 632
		pr_err("Card type %s is unsupported, sorry\n",
		       ndev->board->name);
L
Linus Torvalds 已提交
633 634
		return -ENODEV;
	}
635

A
Al Viro 已提交
636
	__NS8390_init(dev, 0);
L
Linus Torvalds 已提交
637 638

	/* Good, done, now spit out some messages */
639 640 641 642 643 644 645
	pr_info("%s: %s in slot %X (type %s)\n",
		dev->name, ndev->board->name, ndev->board->slot,
		cardname[type]);
	pr_info("MAC %pM IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
		dev->dev_addr, dev->irq,
		(unsigned int)(dev->mem_end - dev->mem_start) >> 10,
		dev->mem_start, access_bitmode ? 32 : 16);
L
Linus Torvalds 已提交
646 647 648 649 650
	return 0;
}

static int mac8390_open(struct net_device *dev)
{
651 652
	int err;

A
Al Viro 已提交
653
	__ei_open(dev);
654 655
	err = request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev);
	if (err)
656
		pr_err("%s: unable to get IRQ %d\n", dev->name, dev->irq);
657
	return err;
L
Linus Torvalds 已提交
658 659 660 661 662
}

static int mac8390_close(struct net_device *dev)
{
	free_irq(dev->irq, dev);
A
Al Viro 已提交
663
	__ei_close(dev);
L
Linus Torvalds 已提交
664 665 666 667 668
	return 0;
}

static void mac8390_no_reset(struct net_device *dev)
{
669 670
	struct ei_device *ei_local = netdev_priv(dev);

L
Linus Torvalds 已提交
671
	ei_status.txing = 0;
672
	netif_info(ei_local, hw, dev, "reset not supported\n");
L
Linus Torvalds 已提交
673 674
}

F
Finn Thain 已提交
675 676
static void interlan_reset(struct net_device *dev)
{
677
	unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq));
678 679 680 681
	struct ei_device *ei_local = netdev_priv(dev);

	netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...",
		   jiffies);
F
Finn Thain 已提交
682 683
	ei_status.txing = 0;
	target[0xC0000] = 0;
684
	if (netif_msg_hw(ei_local))
685
		pr_cont("reset complete\n");
F
Finn Thain 已提交
686 687
}

L
Linus Torvalds 已提交
688 689
/* dayna_memcpy_fromio/dayna_memcpy_toio */
/* directly from daynaport.c by Alan Cox */
690 691
static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from,
				  int count)
L
Linus Torvalds 已提交
692
{
A
Al Viro 已提交
693
	volatile unsigned char *ptr;
694 695 696
	unsigned char *target = to;
	from <<= 1;	/* word, skip overhead */
	ptr = (unsigned char *)(dev->mem_start+from);
L
Linus Torvalds 已提交
697
	/* Leading byte? */
698
	if (from & 2) {
A
Al Viro 已提交
699 700
		*target++ = ptr[-1];
		ptr += 2;
L
Linus Torvalds 已提交
701 702
		count--;
	}
703
	while (count >= 2) {
A
Al Viro 已提交
704 705 706
		*(unsigned short *)target = *(unsigned short volatile *)ptr;
		ptr += 4;			/* skip cruft */
		target += 2;
707
		count -= 2;
L
Linus Torvalds 已提交
708 709
	}
	/* Trailing byte? */
710
	if (count)
A
Al Viro 已提交
711
		*target = *ptr;
L
Linus Torvalds 已提交
712 713
}

714 715
static void dayna_memcpy_tocard(struct net_device *dev, int to,
				const void *from, int count)
L
Linus Torvalds 已提交
716 717
{
	volatile unsigned short *ptr;
718 719 720
	const unsigned char *src = from;
	to <<= 1;	/* word, skip overhead */
	ptr = (unsigned short *)(dev->mem_start+to);
L
Linus Torvalds 已提交
721
	/* Leading byte? */
722
	if (to & 2) {		/* avoid a byte write (stomps on other data) */
A
Al Viro 已提交
723
		ptr[-1] = (ptr[-1]&0xFF00)|*src++;
L
Linus Torvalds 已提交
724 725 726
		ptr++;
		count--;
	}
727 728
	while (count >= 2) {
		*ptr++ = *(unsigned short *)src;	/* Copy and */
L
Linus Torvalds 已提交
729
		ptr++;			/* skip cruft */
A
Al Viro 已提交
730
		src += 2;
731
		count -= 2;
L
Linus Torvalds 已提交
732 733
	}
	/* Trailing byte? */
734
	if (count) {
L
Linus Torvalds 已提交
735
		/* card doesn't like byte writes */
736
		*ptr = (*ptr & 0x00FF) | (*src << 8);
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744
	}
}

/* sane block input/output */
static void sane_get_8390_hdr(struct net_device *dev,
			      struct e8390_pkt_hdr *hdr, int ring_page)
{
	unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
745
	memcpy_fromio(hdr, dev->mem_start + hdr_start, 4);
L
Linus Torvalds 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758
	/* Fix endianness */
	hdr->count = swab16(hdr->count);
}

static void sane_block_input(struct net_device *dev, int count,
			     struct sk_buff *skb, int ring_offset)
{
	unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
	unsigned long xfer_start = xfer_base + dev->mem_start;

	if (xfer_start + count > ei_status.rmem_end) {
		/* We must wrap the input move. */
		int semi_count = ei_status.rmem_end - xfer_start;
759
		memcpy_fromio(skb->data, dev->mem_start + xfer_base,
760
			      semi_count);
L
Linus Torvalds 已提交
761
		count -= semi_count;
762
		memcpy_fromio(skb->data + semi_count, ei_status.rmem_start,
763
			      count);
764 765
	} else {
		memcpy_fromio(skb->data, dev->mem_start + xfer_base, count);
L
Linus Torvalds 已提交
766 767 768 769 770 771 772
	}
}

static void sane_block_output(struct net_device *dev, int count,
			      const unsigned char *buf, int start_page)
{
	long shmem = (start_page - WD_START_PG)<<8;
773

774
	memcpy_toio(dev->mem_start + shmem, buf, count);
L
Linus Torvalds 已提交
775 776 777
}

/* dayna block input/output */
778 779
static void dayna_get_8390_hdr(struct net_device *dev,
			       struct e8390_pkt_hdr *hdr, int ring_page)
L
Linus Torvalds 已提交
780 781 782
{
	unsigned long hdr_start = (ring_page - WD_START_PG)<<8;

783
	dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
L
Linus Torvalds 已提交
784
	/* Fix endianness */
785
	hdr->count = (hdr->count & 0xFF) << 8 | (hdr->count >> 8);
L
Linus Torvalds 已提交
786 787
}

788 789
static void dayna_block_input(struct net_device *dev, int count,
			      struct sk_buff *skb, int ring_offset)
L
Linus Torvalds 已提交
790 791 792 793 794 795 796
{
	unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
	unsigned long xfer_start = xfer_base+dev->mem_start;

	/* Note the offset math is done in card memory space which is word
	   per long onto our space. */

797
	if (xfer_start + count > ei_status.rmem_end) {
L
Linus Torvalds 已提交
798 799 800 801 802 803 804
		/* We must wrap the input move. */
		int semi_count = ei_status.rmem_end - xfer_start;
		dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
		count -= semi_count;
		dayna_memcpy_fromcard(dev, skb->data + semi_count,
				      ei_status.rmem_start - dev->mem_start,
				      count);
805
	} else {
L
Linus Torvalds 已提交
806 807 808 809
		dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
	}
}

810 811 812
static void dayna_block_output(struct net_device *dev, int count,
			       const unsigned char *buf,
			       int start_page)
L
Linus Torvalds 已提交
813 814
{
	long shmem = (start_page - WD_START_PG)<<8;
815

L
Linus Torvalds 已提交
816 817 818 819
	dayna_memcpy_tocard(dev, shmem, buf, count);
}

/* Cabletron block I/O */
820 821 822
static void slow_sane_get_8390_hdr(struct net_device *dev,
				   struct e8390_pkt_hdr *hdr,
				   int ring_page)
L
Linus Torvalds 已提交
823 824
{
	unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
825
	word_memcpy_fromcard(hdr, dev->mem_start + hdr_start, 4);
L
Linus Torvalds 已提交
826 827 828 829
	/* Register endianism - fix here rather than 8390.c */
	hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
}

830 831
static void slow_sane_block_input(struct net_device *dev, int count,
				  struct sk_buff *skb, int ring_offset)
L
Linus Torvalds 已提交
832 833 834 835
{
	unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
	unsigned long xfer_start = xfer_base+dev->mem_start;

836
	if (xfer_start + count > ei_status.rmem_end) {
L
Linus Torvalds 已提交
837 838
		/* We must wrap the input move. */
		int semi_count = ei_status.rmem_end - xfer_start;
839
		word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
840
				     semi_count);
L
Linus Torvalds 已提交
841 842
		count -= semi_count;
		word_memcpy_fromcard(skb->data + semi_count,
843
				     ei_status.rmem_start, count);
844
	} else {
845 846
		word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
				     count);
L
Linus Torvalds 已提交
847 848 849
	}
}

850 851
static void slow_sane_block_output(struct net_device *dev, int count,
				   const unsigned char *buf, int start_page)
L
Linus Torvalds 已提交
852 853 854
{
	long shmem = (start_page - WD_START_PG)<<8;

855
	word_memcpy_tocard(dev->mem_start + shmem, buf, count);
L
Linus Torvalds 已提交
856 857
}

858
static void word_memcpy_tocard(unsigned long tp, const void *fp, int count)
L
Linus Torvalds 已提交
859
{
860
	volatile unsigned short *to = (void *)tp;
L
Linus Torvalds 已提交
861 862 863
	const unsigned short *from = fp;

	count++;
864
	count /= 2;
L
Linus Torvalds 已提交
865

866 867
	while (count--)
		*to++ = *from++;
L
Linus Torvalds 已提交
868 869
}

870
static void word_memcpy_fromcard(void *tp, unsigned long fp, int count)
L
Linus Torvalds 已提交
871 872
{
	unsigned short *to = tp;
873
	const volatile unsigned short *from = (const void *)fp;
L
Linus Torvalds 已提交
874 875

	count++;
876
	count /= 2;
L
Linus Torvalds 已提交
877

878 879
	while (count--)
		*to++ = *from++;
L
Linus Torvalds 已提交
880 881
}

882