Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Amiga Linux/68k 8390 based PCMCIA Ethernet Driver for the Amiga 1200
  4 *
  5 * (C) Copyright 1997 Alain Malek
  6 *                    (Alain.Malek@cryogen.com)
  7 *
  8 * ----------------------------------------------------------------------------
  9 *
 10 * This program is based on
 11 *
 12 * ne.c:       A general non-shared-memory NS8390 ethernet driver for linux
 13 *             Written 1992-94 by Donald Becker.
 14 *
 15 * 8390.c:     A general NS8390 ethernet driver core for linux.
 16 *             Written 1992-94 by Donald Becker.
 17 *
 18 * cnetdevice: A Sana-II ethernet driver for AmigaOS
 19 *             Written by Bruce Abbott (bhabbott@inhb.co.nz)
 20 *
 21 * ----------------------------------------------------------------------------
 22 *
 
 
 
 
 
 
 23 */
 24
 25
 26#include <linux/module.h>
 27#include <linux/kernel.h>
 28#include <linux/errno.h>
 29#include <linux/pci.h>
 30#include <linux/init.h>
 31#include <linux/delay.h>
 32#include <linux/netdevice.h>
 33#include <linux/etherdevice.h>
 34#include <linux/interrupt.h>
 35#include <linux/jiffies.h>
 36
 37#include <asm/io.h>
 38#include <asm/setup.h>
 39#include <asm/amigaints.h>
 40#include <asm/amigahw.h>
 41#include <asm/amigayle.h>
 42#include <asm/amipcmcia.h>
 43
 44#include "8390.h"
 45
 46/* ---- No user-serviceable parts below ---- */
 47
 48#define DRV_NAME "apne"
 49
 50#define NE_BASE	 (dev->base_addr)
 51#define NE_CMD	 		0x00
 52#define NE_DATAPORT		0x10            /* NatSemi-defined port window offset. */
 53#define NE_RESET		0x1f            /* Issue a read to reset, a write to clear. */
 54#define NE_IO_EXTENT	        0x20
 55
 56#define NE_EN0_ISR		0x07
 57#define NE_EN0_DCFG		0x0e
 58
 59#define NE_EN0_RSARLO	        0x08
 60#define NE_EN0_RSARHI	        0x09
 61#define NE_EN0_RCNTLO	        0x0a
 62#define NE_EN0_RXCR		0x0c
 63#define NE_EN0_TXCR		0x0d
 64#define NE_EN0_RCNTHI	        0x0b
 65#define NE_EN0_IMR		0x0f
 66
 67#define NE1SM_START_PG	0x20	/* First page of TX buffer */
 68#define NE1SM_STOP_PG 	0x40	/* Last page +1 of RX ring */
 69#define NESM_START_PG	0x40	/* First page of TX buffer */
 70#define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
 71
 72
 
 73static int apne_probe1(struct net_device *dev, int ioaddr);
 74
 75static void apne_reset_8390(struct net_device *dev);
 76static void apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
 77			  int ring_page);
 78static void apne_block_input(struct net_device *dev, int count,
 79								struct sk_buff *skb, int ring_offset);
 80static void apne_block_output(struct net_device *dev, const int count,
 81							const unsigned char *buf, const int start_page);
 82static irqreturn_t apne_interrupt(int irq, void *dev_id);
 83
 84static int init_pcmcia(void);
 85
 86/* IO base address used for nic */
 87
 88#define IOBASE 0x300
 89
 90/*
 91   use MANUAL_CONFIG and MANUAL_OFFSET for enabling IO by hand
 92   you can find the values to use by looking at the cnet.device
 93   config file example (the default values are for the CNET40BC card)
 94*/
 95
 96/*
 97#define MANUAL_CONFIG 0x20
 98#define MANUAL_OFFSET 0x3f8
 99
100#define MANUAL_HWADDR0 0x00
101#define MANUAL_HWADDR1 0x12
102#define MANUAL_HWADDR2 0x34
103#define MANUAL_HWADDR3 0x56
104#define MANUAL_HWADDR4 0x78
105#define MANUAL_HWADDR5 0x9a
106*/
107
108static const char version[] =
109    "apne.c:v1.1 7/10/98 Alain Malek (Alain.Malek@cryogen.ch)\n";
110
111static int apne_owned;	/* signal if card already owned */
112
113static u32 apne_msg_enable;
114module_param_named(msg_enable, apne_msg_enable, uint, 0444);
115MODULE_PARM_DESC(msg_enable, "Debug message level (see linux/netdevice.h for bitmap)");
116
117static struct net_device * __init apne_probe(void)
118{
119	struct net_device *dev;
120	struct ei_device *ei_local;
121
122#ifndef MANUAL_CONFIG
123	char tuple[8];
124#endif
125	int err;
126
127	if (!MACH_IS_AMIGA)
128		return ERR_PTR(-ENODEV);
129
130	if (apne_owned)
131		return ERR_PTR(-ENODEV);
132
133	if ( !(AMIGAHW_PRESENT(PCMCIA)) )
134		return ERR_PTR(-ENODEV);
135
136	pr_info("Looking for PCMCIA ethernet card : ");
137
138	/* check if a card is inserted */
139	if (!(PCMCIA_INSERTED)) {
140		pr_cont("NO PCMCIA card inserted\n");
141		return ERR_PTR(-ENODEV);
142	}
143
144	dev = alloc_ei_netdev();
145	if (!dev)
146		return ERR_PTR(-ENOMEM);
147	ei_local = netdev_priv(dev);
148	ei_local->msg_enable = apne_msg_enable;
 
 
149
150	/* disable pcmcia irq for readtuple */
151	pcmcia_disable_irq();
152
153#ifndef MANUAL_CONFIG
154	if ((pcmcia_copy_tuple(CISTPL_FUNCID, tuple, 8) < 3) ||
155		(tuple[2] != CISTPL_FUNCID_NETWORK)) {
156		pr_cont("not an ethernet card\n");
157		/* XXX: shouldn't we re-enable irq here? */
158		free_netdev(dev);
159		return ERR_PTR(-ENODEV);
160	}
161#endif
162
163	pr_cont("ethernet PCMCIA card inserted\n");
164
165	if (!init_pcmcia()) {
166		/* XXX: shouldn't we re-enable irq here? */
167		free_netdev(dev);
168		return ERR_PTR(-ENODEV);
169	}
170
171	if (!request_region(IOBASE, 0x20, DRV_NAME)) {
172		free_netdev(dev);
173		return ERR_PTR(-EBUSY);
174	}
175
176	err = apne_probe1(dev, IOBASE);
177	if (err) {
178		release_region(IOBASE, 0x20);
179		free_netdev(dev);
180		return ERR_PTR(err);
181	}
182	err = register_netdev(dev);
183	if (!err)
184		return dev;
185
186	pcmcia_disable_irq();
187	free_irq(IRQ_AMIGA_PORTS, dev);
188	pcmcia_reset();
189	release_region(IOBASE, 0x20);
190	free_netdev(dev);
191	return ERR_PTR(err);
192}
193
194static int __init apne_probe1(struct net_device *dev, int ioaddr)
195{
196    int i;
197    unsigned char SA_prom[32];
198    int wordlength = 2;
199    const char *name = NULL;
200    int start_page, stop_page;
201#ifndef MANUAL_HWADDR0
202    int neX000, ctron;
203#endif
204    static unsigned version_printed;
205
206    if ((apne_msg_enable & NETIF_MSG_DRV) && (version_printed++ == 0))
207		netdev_info(dev, version);
208
209    netdev_info(dev, "PCMCIA NE*000 ethercard probe");
210
211    /* Reset card. Who knows what dain-bramaged state it was left in. */
212    {	unsigned long reset_start_time = jiffies;
213
214	outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
215
216	while ((inb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
217		if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
218			pr_cont(" not found (no reset ack).\n");
219			return -ENODEV;
220		}
221
222	outb(0xff, ioaddr + NE_EN0_ISR);		/* Ack all intr. */
223    }
224
225#ifndef MANUAL_HWADDR0
226
227    /* Read the 16 bytes of station address PROM.
228       We must first initialize registers, similar to NS8390_init(eifdev, 0).
229       We can't reliably read the SAPROM address without this.
230       (I learned the hard way!). */
231    {
232	struct {unsigned long value, offset; } program_seq[] = {
233	    {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
234	    {0x48,	NE_EN0_DCFG},	/* Set byte-wide (0x48) access. */
235	    {0x00,	NE_EN0_RCNTLO},	/* Clear the count regs. */
236	    {0x00,	NE_EN0_RCNTHI},
237	    {0x00,	NE_EN0_IMR},	/* Mask completion irq. */
238	    {0xFF,	NE_EN0_ISR},
239	    {E8390_RXOFF, NE_EN0_RXCR},	/* 0x20  Set to monitor */
240	    {E8390_TXOFF, NE_EN0_TXCR},	/* 0x02  and loopback mode. */
241	    {32,	NE_EN0_RCNTLO},
242	    {0x00,	NE_EN0_RCNTHI},
243	    {0x00,	NE_EN0_RSARLO},	/* DMA starting at 0x0000. */
244	    {0x00,	NE_EN0_RSARHI},
245	    {E8390_RREAD+E8390_START, NE_CMD},
246	};
247	for (i = 0; i < ARRAY_SIZE(program_seq); i++) {
248	    outb(program_seq[i].value, ioaddr + program_seq[i].offset);
249	}
250
251    }
252    for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
253	SA_prom[i] = inb(ioaddr + NE_DATAPORT);
254	SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
255	if (SA_prom[i] != SA_prom[i+1])
256	    wordlength = 1;
257    }
258
259    /*	At this point, wordlength *only* tells us if the SA_prom is doubled
260	up or not because some broken PCI cards don't respect the byte-wide
261	request in program_seq above, and hence don't have doubled up values.
262	These broken cards would otherwise be detected as an ne1000.  */
263
264    if (wordlength == 2)
265	for (i = 0; i < 16; i++)
266		SA_prom[i] = SA_prom[i+i];
267
268    if (wordlength == 2) {
269	/* We must set the 8390 for word mode. */
270	outb(0x49, ioaddr + NE_EN0_DCFG);
271	start_page = NESM_START_PG;
272	stop_page = NESM_STOP_PG;
273    } else {
274	start_page = NE1SM_START_PG;
275	stop_page = NE1SM_STOP_PG;
276    }
277
278    neX000 = (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
279    ctron =  (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
280
281    /* Set up the rest of the parameters. */
282    if (neX000) {
283	name = (wordlength == 2) ? "NE2000" : "NE1000";
284    } else if (ctron) {
285	name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
286	start_page = 0x01;
287	stop_page = (wordlength == 2) ? 0x40 : 0x20;
288    } else {
289	pr_cont(" not found.\n");
290	return -ENXIO;
291
292    }
293
294#else
295    wordlength = 2;
296    /* We must set the 8390 for word mode. */
297    outb(0x49, ioaddr + NE_EN0_DCFG);
298    start_page = NESM_START_PG;
299    stop_page = NESM_STOP_PG;
300
301    SA_prom[0] = MANUAL_HWADDR0;
302    SA_prom[1] = MANUAL_HWADDR1;
303    SA_prom[2] = MANUAL_HWADDR2;
304    SA_prom[3] = MANUAL_HWADDR3;
305    SA_prom[4] = MANUAL_HWADDR4;
306    SA_prom[5] = MANUAL_HWADDR5;
307    name = "NE2000";
308#endif
309
310    dev->base_addr = ioaddr;
311    dev->irq = IRQ_AMIGA_PORTS;
312    dev->netdev_ops = &ei_netdev_ops;
313
314    /* Install the Interrupt handler */
315    i = request_irq(dev->irq, apne_interrupt, IRQF_SHARED, DRV_NAME, dev);
316    if (i) return i;
317
318    eth_hw_addr_set(dev, SA_prom);
 
319
320    pr_cont(" %pM\n", dev->dev_addr);
321
322    netdev_info(dev, "%s found.\n", name);
323
324    ei_status.name = name;
325    ei_status.tx_start_page = start_page;
326    ei_status.stop_page = stop_page;
327    ei_status.word16 = (wordlength == 2);
328
329    ei_status.rx_start_page = start_page + TX_PAGES;
330
331    ei_status.reset_8390 = &apne_reset_8390;
332    ei_status.block_input = &apne_block_input;
333    ei_status.block_output = &apne_block_output;
334    ei_status.get_8390_hdr = &apne_get_8390_hdr;
335
336    NS8390_init(dev, 0);
337
338    pcmcia_ack_int(pcmcia_get_intreq());		/* ack PCMCIA int req */
339    pcmcia_enable_irq();
340
341    apne_owned = 1;
342
343    return 0;
344}
345
346/* Hard reset the card.  This used to pause for the same period that a
347   8390 reset command required, but that shouldn't be necessary. */
348static void
349apne_reset_8390(struct net_device *dev)
350{
351    unsigned long reset_start_time = jiffies;
352    struct ei_device *ei_local = netdev_priv(dev);
353
354    init_pcmcia();
355
356    netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
357
358    outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
359
360    ei_status.txing = 0;
361    ei_status.dmaing = 0;
362
363    /* This check _should_not_ be necessary, omit eventually. */
364    while ((inb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
365	if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
366		netdev_err(dev, "ne_reset_8390() did not complete.\n");
367		break;
368	}
369    outb(ENISR_RESET, NE_BASE + NE_EN0_ISR);	/* Ack intr. */
370}
371
372/* Grab the 8390 specific header. Similar to the block_input routine, but
373   we don't need to be concerned with ring wrap as the header will be at
374   the start of a page, so we optimize accordingly. */
375
376static void
377apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
378{
379
380    int nic_base = dev->base_addr;
381    int cnt;
382    char *ptrc;
383    short *ptrs;
384
385    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
386    if (ei_status.dmaing) {
387	netdev_err(dev, "DMAing conflict in ne_get_8390_hdr "
388		   "[DMAstat:%d][irqlock:%d][intr:%d].\n",
389		   ei_status.dmaing, ei_status.irqlock, dev->irq);
390	return;
391    }
392
393    ei_status.dmaing |= 0x01;
394    outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
395    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
396    outb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
397    outb(0, nic_base + NE_EN0_RCNTHI);
398    outb(0, nic_base + NE_EN0_RSARLO);		/* On page boundary */
399    outb(ring_page, nic_base + NE_EN0_RSARHI);
400    outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
401
402    if (ei_status.word16) {
403        ptrs = (short*)hdr;
404        for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
405            *ptrs++ = inw(NE_BASE + NE_DATAPORT);
406    } else {
407        ptrc = (char*)hdr;
408        for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
409            *ptrc++ = inb(NE_BASE + NE_DATAPORT);
410    }
411
412    outb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
413    ei_status.dmaing &= ~0x01;
414
415    le16_to_cpus(&hdr->count);
416}
417
418/* Block input and output, similar to the Crynwr packet driver.  If you
419   are porting to a new ethercard, look at the packet driver source for hints.
420   The NEx000 doesn't share the on-board packet memory -- you have to put
421   the packet out through the "remote DMA" dataport using outb. */
422
423static void
424apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
425{
426    int nic_base = dev->base_addr;
427    char *buf = skb->data;
428    char *ptrc;
429    short *ptrs;
430    int cnt;
431
432    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
433    if (ei_status.dmaing) {
434		netdev_err(dev, "DMAing conflict in ne_block_input "
435			   "[DMAstat:%d][irqlock:%d][intr:%d].\n",
436			   ei_status.dmaing, ei_status.irqlock, dev->irq);
437	return;
438    }
439    ei_status.dmaing |= 0x01;
440    outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
441    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
442    outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
443    outb(count >> 8, nic_base + NE_EN0_RCNTHI);
444    outb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
445    outb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
446    outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
447    if (ei_status.word16) {
448      ptrs = (short*)buf;
449      for (cnt = 0; cnt < (count>>1); cnt++)
450        *ptrs++ = inw(NE_BASE + NE_DATAPORT);
451      if (count & 0x01) {
452	buf[count-1] = inb(NE_BASE + NE_DATAPORT);
453      }
454    } else {
455      ptrc = buf;
456      for (cnt = 0; cnt < count; cnt++)
457        *ptrc++ = inb(NE_BASE + NE_DATAPORT);
458    }
459
460    outb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
461    ei_status.dmaing &= ~0x01;
462}
463
464static void
465apne_block_output(struct net_device *dev, int count,
466		const unsigned char *buf, const int start_page)
467{
468    int nic_base = NE_BASE;
469    unsigned long dma_start;
470    char *ptrc;
471    short *ptrs;
472    int cnt;
473
474    /* Round the count up for word writes.  Do we need to do this?
475       What effect will an odd byte count have on the 8390?
476       I should check someday. */
477    if (ei_status.word16 && (count & 0x01))
478      count++;
479
480    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
481    if (ei_status.dmaing) {
482		netdev_err(dev, "DMAing conflict in ne_block_output."
483			   "[DMAstat:%d][irqlock:%d][intr:%d]\n",
484			   ei_status.dmaing, ei_status.irqlock, dev->irq);
485	return;
486    }
487    ei_status.dmaing |= 0x01;
488    /* We should already be in page 0, but to be safe... */
489    outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
490
491    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
492
493   /* Now the normal output. */
494    outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
495    outb(count >> 8,   nic_base + NE_EN0_RCNTHI);
496    outb(0x00, nic_base + NE_EN0_RSARLO);
497    outb(start_page, nic_base + NE_EN0_RSARHI);
498
499    outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
500    if (ei_status.word16) {
501        ptrs = (short*)buf;
502        for (cnt = 0; cnt < count>>1; cnt++)
503            outw(*ptrs++, NE_BASE+NE_DATAPORT);
504    } else {
505        ptrc = (char*)buf;
506        for (cnt = 0; cnt < count; cnt++)
507	    outb(*ptrc++, NE_BASE + NE_DATAPORT);
508    }
509
510    dma_start = jiffies;
511
512    while ((inb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
513	if (time_after(jiffies, dma_start + 2*HZ/100)) {	/* 20ms */
514		netdev_warn(dev, "timeout waiting for Tx RDC.\n");
515		apne_reset_8390(dev);
516		NS8390_init(dev,1);
517		break;
518	}
519
520    outb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
521    ei_status.dmaing &= ~0x01;
522}
523
524static irqreturn_t apne_interrupt(int irq, void *dev_id)
525{
526    unsigned char pcmcia_intreq;
527
528    if (!(gayle.inten & GAYLE_IRQ_IRQ))
529        return IRQ_NONE;
530
531    pcmcia_intreq = pcmcia_get_intreq();
532
533    if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
534        pcmcia_ack_int(pcmcia_intreq);
535        return IRQ_NONE;
536    }
537    if (apne_msg_enable & NETIF_MSG_INTR)
538	pr_debug("pcmcia intreq = %x\n", pcmcia_intreq);
539    pcmcia_disable_irq();			/* to get rid of the sti() within ei_interrupt */
540    ei_interrupt(irq, dev_id);
541    pcmcia_ack_int(pcmcia_get_intreq());
542    pcmcia_enable_irq();
543    return IRQ_HANDLED;
544}
545
 
546static struct net_device *apne_dev;
547
548static int __init apne_module_init(void)
549{
550	apne_dev = apne_probe();
551	return PTR_ERR_OR_ZERO(apne_dev);
 
 
552}
553
554static void __exit apne_module_exit(void)
555{
556	unregister_netdev(apne_dev);
557
558	pcmcia_disable_irq();
559
560	free_irq(IRQ_AMIGA_PORTS, apne_dev);
561
562	pcmcia_reset();
563
564	release_region(IOBASE, 0x20);
565
566	free_netdev(apne_dev);
567}
568module_init(apne_module_init);
569module_exit(apne_module_exit);
 
570
571static int init_pcmcia(void)
572{
573	u_char config;
574#ifndef MANUAL_CONFIG
575	u_char tuple[32];
576	int offset_len;
577#endif
578	u_long offset;
579
580	pcmcia_reset();
581	pcmcia_program_voltage(PCMCIA_0V);
582	pcmcia_access_speed(PCMCIA_SPEED_250NS);
583	pcmcia_write_enable();
584
585#ifdef MANUAL_CONFIG
586	config = MANUAL_CONFIG;
587#else
588	/* get and write config byte to enable IO port */
589
590	if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
591		return 0;
592
593	config = tuple[2] & 0x3f;
594#endif
595#ifdef MANUAL_OFFSET
596	offset = MANUAL_OFFSET;
597#else
598	if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
599		return 0;
600
601	offset_len = (tuple[2] & 0x3) + 1;
602	offset = 0;
603	while(offset_len--) {
604		offset = (offset << 8) | tuple[4+offset_len];
605	}
606#endif
607
608	out_8(GAYLE_ATTRIBUTE+offset, config);
609
610	return 1;
611}
612
613MODULE_DESCRIPTION("National Semiconductor 8390 Amiga PCMCIA ethernet driver");
614MODULE_LICENSE("GPL");
v3.5.6
 
  1/*
  2 * Amiga Linux/68k 8390 based PCMCIA Ethernet Driver for the Amiga 1200
  3 *
  4 * (C) Copyright 1997 Alain Malek
  5 *                    (Alain.Malek@cryogen.com)
  6 *
  7 * ----------------------------------------------------------------------------
  8 *
  9 * This program is based on
 10 *
 11 * ne.c:       A general non-shared-memory NS8390 ethernet driver for linux
 12 *             Written 1992-94 by Donald Becker.
 13 *
 14 * 8390.c:     A general NS8390 ethernet driver core for linux.
 15 *             Written 1992-94 by Donald Becker.
 16 *
 17 * cnetdevice: A Sana-II ethernet driver for AmigaOS
 18 *             Written by Bruce Abbott (bhabbott@inhb.co.nz)
 19 *
 20 * ----------------------------------------------------------------------------
 21 *
 22 * This file is subject to the terms and conditions of the GNU General Public
 23 * License.  See the file COPYING in the main directory of the Linux
 24 * distribution for more details.
 25 *
 26 * ----------------------------------------------------------------------------
 27 *
 28 */
 29
 30
 31#include <linux/module.h>
 32#include <linux/kernel.h>
 33#include <linux/errno.h>
 34#include <linux/pci.h>
 35#include <linux/init.h>
 36#include <linux/delay.h>
 37#include <linux/netdevice.h>
 38#include <linux/etherdevice.h>
 39#include <linux/interrupt.h>
 40#include <linux/jiffies.h>
 41
 42#include <asm/io.h>
 43#include <asm/setup.h>
 44#include <asm/amigaints.h>
 45#include <asm/amigahw.h>
 46#include <asm/amigayle.h>
 47#include <asm/amipcmcia.h>
 48
 49#include "8390.h"
 50
 51/* ---- No user-serviceable parts below ---- */
 52
 53#define DRV_NAME "apne"
 54
 55#define NE_BASE	 (dev->base_addr)
 56#define NE_CMD	 		0x00
 57#define NE_DATAPORT		0x10            /* NatSemi-defined port window offset. */
 58#define NE_RESET		0x1f            /* Issue a read to reset, a write to clear. */
 59#define NE_IO_EXTENT	        0x20
 60
 61#define NE_EN0_ISR		0x07
 62#define NE_EN0_DCFG		0x0e
 63
 64#define NE_EN0_RSARLO	        0x08
 65#define NE_EN0_RSARHI	        0x09
 66#define NE_EN0_RCNTLO	        0x0a
 67#define NE_EN0_RXCR		0x0c
 68#define NE_EN0_TXCR		0x0d
 69#define NE_EN0_RCNTHI	        0x0b
 70#define NE_EN0_IMR		0x0f
 71
 72#define NE1SM_START_PG	0x20	/* First page of TX buffer */
 73#define NE1SM_STOP_PG 	0x40	/* Last page +1 of RX ring */
 74#define NESM_START_PG	0x40	/* First page of TX buffer */
 75#define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
 76
 77
 78struct net_device * __init apne_probe(int unit);
 79static int apne_probe1(struct net_device *dev, int ioaddr);
 80
 81static void apne_reset_8390(struct net_device *dev);
 82static void apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
 83			  int ring_page);
 84static void apne_block_input(struct net_device *dev, int count,
 85								struct sk_buff *skb, int ring_offset);
 86static void apne_block_output(struct net_device *dev, const int count,
 87							const unsigned char *buf, const int start_page);
 88static irqreturn_t apne_interrupt(int irq, void *dev_id);
 89
 90static int init_pcmcia(void);
 91
 92/* IO base address used for nic */
 93
 94#define IOBASE 0x300
 95
 96/*
 97   use MANUAL_CONFIG and MANUAL_OFFSET for enabling IO by hand
 98   you can find the values to use by looking at the cnet.device
 99   config file example (the default values are for the CNET40BC card)
100*/
101
102/*
103#define MANUAL_CONFIG 0x20
104#define MANUAL_OFFSET 0x3f8
105
106#define MANUAL_HWADDR0 0x00
107#define MANUAL_HWADDR1 0x12
108#define MANUAL_HWADDR2 0x34
109#define MANUAL_HWADDR3 0x56
110#define MANUAL_HWADDR4 0x78
111#define MANUAL_HWADDR5 0x9a
112*/
113
114static const char version[] =
115    "apne.c:v1.1 7/10/98 Alain Malek (Alain.Malek@cryogen.ch)\n";
116
117static int apne_owned;	/* signal if card already owned */
118
119struct net_device * __init apne_probe(int unit)
 
 
 
 
120{
121	struct net_device *dev;
 
 
122#ifndef MANUAL_CONFIG
123	char tuple[8];
124#endif
125	int err;
126
127	if (!MACH_IS_AMIGA)
128		return ERR_PTR(-ENODEV);
129
130	if (apne_owned)
131		return ERR_PTR(-ENODEV);
132
133	if ( !(AMIGAHW_PRESENT(PCMCIA)) )
134		return ERR_PTR(-ENODEV);
135
136	printk("Looking for PCMCIA ethernet card : ");
137
138	/* check if a card is inserted */
139	if (!(PCMCIA_INSERTED)) {
140		printk("NO PCMCIA card inserted\n");
141		return ERR_PTR(-ENODEV);
142	}
143
144	dev = alloc_ei_netdev();
145	if (!dev)
146		return ERR_PTR(-ENOMEM);
147	if (unit >= 0) {
148		sprintf(dev->name, "eth%d", unit);
149		netdev_boot_setup_check(dev);
150	}
151
152	/* disable pcmcia irq for readtuple */
153	pcmcia_disable_irq();
154
155#ifndef MANUAL_CONFIG
156	if ((pcmcia_copy_tuple(CISTPL_FUNCID, tuple, 8) < 3) ||
157		(tuple[2] != CISTPL_FUNCID_NETWORK)) {
158		printk("not an ethernet card\n");
159		/* XXX: shouldn't we re-enable irq here? */
160		free_netdev(dev);
161		return ERR_PTR(-ENODEV);
162	}
163#endif
164
165	printk("ethernet PCMCIA card inserted\n");
166
167	if (!init_pcmcia()) {
168		/* XXX: shouldn't we re-enable irq here? */
169		free_netdev(dev);
170		return ERR_PTR(-ENODEV);
171	}
172
173	if (!request_region(IOBASE, 0x20, DRV_NAME)) {
174		free_netdev(dev);
175		return ERR_PTR(-EBUSY);
176	}
177
178	err = apne_probe1(dev, IOBASE);
179	if (err) {
180		release_region(IOBASE, 0x20);
181		free_netdev(dev);
182		return ERR_PTR(err);
183	}
184	err = register_netdev(dev);
185	if (!err)
186		return dev;
187
188	pcmcia_disable_irq();
189	free_irq(IRQ_AMIGA_PORTS, dev);
190	pcmcia_reset();
191	release_region(IOBASE, 0x20);
192	free_netdev(dev);
193	return ERR_PTR(err);
194}
195
196static int __init apne_probe1(struct net_device *dev, int ioaddr)
197{
198    int i;
199    unsigned char SA_prom[32];
200    int wordlength = 2;
201    const char *name = NULL;
202    int start_page, stop_page;
203#ifndef MANUAL_HWADDR0
204    int neX000, ctron;
205#endif
206    static unsigned version_printed;
207
208    if (ei_debug  &&  version_printed++ == 0)
209	printk(version);
210
211    printk("PCMCIA NE*000 ethercard probe");
212
213    /* Reset card. Who knows what dain-bramaged state it was left in. */
214    {	unsigned long reset_start_time = jiffies;
215
216	outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
217
218	while ((inb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
219		if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
220			printk(" not found (no reset ack).\n");
221			return -ENODEV;
222		}
223
224	outb(0xff, ioaddr + NE_EN0_ISR);		/* Ack all intr. */
225    }
226
227#ifndef MANUAL_HWADDR0
228
229    /* Read the 16 bytes of station address PROM.
230       We must first initialize registers, similar to NS8390_init(eifdev, 0).
231       We can't reliably read the SAPROM address without this.
232       (I learned the hard way!). */
233    {
234	struct {unsigned long value, offset; } program_seq[] = {
235	    {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
236	    {0x48,	NE_EN0_DCFG},	/* Set byte-wide (0x48) access. */
237	    {0x00,	NE_EN0_RCNTLO},	/* Clear the count regs. */
238	    {0x00,	NE_EN0_RCNTHI},
239	    {0x00,	NE_EN0_IMR},	/* Mask completion irq. */
240	    {0xFF,	NE_EN0_ISR},
241	    {E8390_RXOFF, NE_EN0_RXCR},	/* 0x20  Set to monitor */
242	    {E8390_TXOFF, NE_EN0_TXCR},	/* 0x02  and loopback mode. */
243	    {32,	NE_EN0_RCNTLO},
244	    {0x00,	NE_EN0_RCNTHI},
245	    {0x00,	NE_EN0_RSARLO},	/* DMA starting at 0x0000. */
246	    {0x00,	NE_EN0_RSARHI},
247	    {E8390_RREAD+E8390_START, NE_CMD},
248	};
249	for (i = 0; i < ARRAY_SIZE(program_seq); i++) {
250	    outb(program_seq[i].value, ioaddr + program_seq[i].offset);
251	}
252
253    }
254    for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
255	SA_prom[i] = inb(ioaddr + NE_DATAPORT);
256	SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
257	if (SA_prom[i] != SA_prom[i+1])
258	    wordlength = 1;
259    }
260
261    /*	At this point, wordlength *only* tells us if the SA_prom is doubled
262	up or not because some broken PCI cards don't respect the byte-wide
263	request in program_seq above, and hence don't have doubled up values.
264	These broken cards would otherwise be detected as an ne1000.  */
265
266    if (wordlength == 2)
267	for (i = 0; i < 16; i++)
268		SA_prom[i] = SA_prom[i+i];
269
270    if (wordlength == 2) {
271	/* We must set the 8390 for word mode. */
272	outb(0x49, ioaddr + NE_EN0_DCFG);
273	start_page = NESM_START_PG;
274	stop_page = NESM_STOP_PG;
275    } else {
276	start_page = NE1SM_START_PG;
277	stop_page = NE1SM_STOP_PG;
278    }
279
280    neX000 = (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
281    ctron =  (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
282
283    /* Set up the rest of the parameters. */
284    if (neX000) {
285	name = (wordlength == 2) ? "NE2000" : "NE1000";
286    } else if (ctron) {
287	name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
288	start_page = 0x01;
289	stop_page = (wordlength == 2) ? 0x40 : 0x20;
290    } else {
291	printk(" not found.\n");
292	return -ENXIO;
293
294    }
295
296#else
297    wordlength = 2;
298    /* We must set the 8390 for word mode. */
299    outb(0x49, ioaddr + NE_EN0_DCFG);
300    start_page = NESM_START_PG;
301    stop_page = NESM_STOP_PG;
302
303    SA_prom[0] = MANUAL_HWADDR0;
304    SA_prom[1] = MANUAL_HWADDR1;
305    SA_prom[2] = MANUAL_HWADDR2;
306    SA_prom[3] = MANUAL_HWADDR3;
307    SA_prom[4] = MANUAL_HWADDR4;
308    SA_prom[5] = MANUAL_HWADDR5;
309    name = "NE2000";
310#endif
311
312    dev->base_addr = ioaddr;
313    dev->irq = IRQ_AMIGA_PORTS;
314    dev->netdev_ops = &ei_netdev_ops;
315
316    /* Install the Interrupt handler */
317    i = request_irq(dev->irq, apne_interrupt, IRQF_SHARED, DRV_NAME, dev);
318    if (i) return i;
319
320    for (i = 0; i < ETH_ALEN; i++)
321	dev->dev_addr[i] = SA_prom[i];
322
323    printk(" %pM\n", dev->dev_addr);
324
325    printk("%s: %s found.\n", dev->name, name);
326
327    ei_status.name = name;
328    ei_status.tx_start_page = start_page;
329    ei_status.stop_page = stop_page;
330    ei_status.word16 = (wordlength == 2);
331
332    ei_status.rx_start_page = start_page + TX_PAGES;
333
334    ei_status.reset_8390 = &apne_reset_8390;
335    ei_status.block_input = &apne_block_input;
336    ei_status.block_output = &apne_block_output;
337    ei_status.get_8390_hdr = &apne_get_8390_hdr;
338
339    NS8390_init(dev, 0);
340
341    pcmcia_ack_int(pcmcia_get_intreq());		/* ack PCMCIA int req */
342    pcmcia_enable_irq();
343
344    apne_owned = 1;
345
346    return 0;
347}
348
349/* Hard reset the card.  This used to pause for the same period that a
350   8390 reset command required, but that shouldn't be necessary. */
351static void
352apne_reset_8390(struct net_device *dev)
353{
354    unsigned long reset_start_time = jiffies;
 
355
356    init_pcmcia();
357
358    if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
359
360    outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
361
362    ei_status.txing = 0;
363    ei_status.dmaing = 0;
364
365    /* This check _should_not_ be necessary, omit eventually. */
366    while ((inb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
367	if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
368	    printk("%s: ne_reset_8390() did not complete.\n", dev->name);
369	    break;
370	}
371    outb(ENISR_RESET, NE_BASE + NE_EN0_ISR);	/* Ack intr. */
372}
373
374/* Grab the 8390 specific header. Similar to the block_input routine, but
375   we don't need to be concerned with ring wrap as the header will be at
376   the start of a page, so we optimize accordingly. */
377
378static void
379apne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
380{
381
382    int nic_base = dev->base_addr;
383    int cnt;
384    char *ptrc;
385    short *ptrs;
386
387    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
388    if (ei_status.dmaing) {
389	printk("%s: DMAing conflict in ne_get_8390_hdr "
390	   "[DMAstat:%d][irqlock:%d][intr:%d].\n",
391	   dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
392	return;
393    }
394
395    ei_status.dmaing |= 0x01;
396    outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
397    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
398    outb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
399    outb(0, nic_base + NE_EN0_RCNTHI);
400    outb(0, nic_base + NE_EN0_RSARLO);		/* On page boundary */
401    outb(ring_page, nic_base + NE_EN0_RSARHI);
402    outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
403
404    if (ei_status.word16) {
405        ptrs = (short*)hdr;
406        for(cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
407            *ptrs++ = inw(NE_BASE + NE_DATAPORT);
408    } else {
409        ptrc = (char*)hdr;
410        for(cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
411            *ptrc++ = inb(NE_BASE + NE_DATAPORT);
412    }
413
414    outb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
415    ei_status.dmaing &= ~0x01;
416
417    le16_to_cpus(&hdr->count);
418}
419
420/* Block input and output, similar to the Crynwr packet driver.  If you
421   are porting to a new ethercard, look at the packet driver source for hints.
422   The NEx000 doesn't share the on-board packet memory -- you have to put
423   the packet out through the "remote DMA" dataport using outb. */
424
425static void
426apne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
427{
428    int nic_base = dev->base_addr;
429    char *buf = skb->data;
430    char *ptrc;
431    short *ptrs;
432    int cnt;
433
434    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
435    if (ei_status.dmaing) {
436	printk("%s: DMAing conflict in ne_block_input "
437	   "[DMAstat:%d][irqlock:%d][intr:%d].\n",
438	   dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
439	return;
440    }
441    ei_status.dmaing |= 0x01;
442    outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
443    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
444    outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
445    outb(count >> 8, nic_base + NE_EN0_RCNTHI);
446    outb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
447    outb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
448    outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
449    if (ei_status.word16) {
450      ptrs = (short*)buf;
451      for (cnt = 0; cnt < (count>>1); cnt++)
452        *ptrs++ = inw(NE_BASE + NE_DATAPORT);
453      if (count & 0x01) {
454	buf[count-1] = inb(NE_BASE + NE_DATAPORT);
455      }
456    } else {
457      ptrc = (char*)buf;
458      for (cnt = 0; cnt < count; cnt++)
459        *ptrc++ = inb(NE_BASE + NE_DATAPORT);
460    }
461
462    outb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
463    ei_status.dmaing &= ~0x01;
464}
465
466static void
467apne_block_output(struct net_device *dev, int count,
468		const unsigned char *buf, const int start_page)
469{
470    int nic_base = NE_BASE;
471    unsigned long dma_start;
472    char *ptrc;
473    short *ptrs;
474    int cnt;
475
476    /* Round the count up for word writes.  Do we need to do this?
477       What effect will an odd byte count have on the 8390?
478       I should check someday. */
479    if (ei_status.word16 && (count & 0x01))
480      count++;
481
482    /* This *shouldn't* happen. If it does, it's the last thing you'll see */
483    if (ei_status.dmaing) {
484	printk("%s: DMAing conflict in ne_block_output."
485	   "[DMAstat:%d][irqlock:%d][intr:%d]\n",
486	   dev->name, ei_status.dmaing, ei_status.irqlock, dev->irq);
487	return;
488    }
489    ei_status.dmaing |= 0x01;
490    /* We should already be in page 0, but to be safe... */
491    outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
492
493    outb(ENISR_RDC, nic_base + NE_EN0_ISR);
494
495   /* Now the normal output. */
496    outb(count & 0xff, nic_base + NE_EN0_RCNTLO);
497    outb(count >> 8,   nic_base + NE_EN0_RCNTHI);
498    outb(0x00, nic_base + NE_EN0_RSARLO);
499    outb(start_page, nic_base + NE_EN0_RSARHI);
500
501    outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
502    if (ei_status.word16) {
503        ptrs = (short*)buf;
504        for (cnt = 0; cnt < count>>1; cnt++)
505            outw(*ptrs++, NE_BASE+NE_DATAPORT);
506    } else {
507        ptrc = (char*)buf;
508        for (cnt = 0; cnt < count; cnt++)
509	    outb(*ptrc++, NE_BASE + NE_DATAPORT);
510    }
511
512    dma_start = jiffies;
513
514    while ((inb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
515	if (time_after(jiffies, dma_start + 2*HZ/100)) {	/* 20ms */
516		printk("%s: timeout waiting for Tx RDC.\n", dev->name);
517		apne_reset_8390(dev);
518		NS8390_init(dev,1);
519		break;
520	}
521
522    outb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr. */
523    ei_status.dmaing &= ~0x01;
524}
525
526static irqreturn_t apne_interrupt(int irq, void *dev_id)
527{
528    unsigned char pcmcia_intreq;
529
530    if (!(gayle.inten & GAYLE_IRQ_IRQ))
531        return IRQ_NONE;
532
533    pcmcia_intreq = pcmcia_get_intreq();
534
535    if (!(pcmcia_intreq & GAYLE_IRQ_IRQ)) {
536        pcmcia_ack_int(pcmcia_intreq);
537        return IRQ_NONE;
538    }
539    if (ei_debug > 3)
540        printk("pcmcia intreq = %x\n", pcmcia_intreq);
541    pcmcia_disable_irq();			/* to get rid of the sti() within ei_interrupt */
542    ei_interrupt(irq, dev_id);
543    pcmcia_ack_int(pcmcia_get_intreq());
544    pcmcia_enable_irq();
545    return IRQ_HANDLED;
546}
547
548#ifdef MODULE
549static struct net_device *apne_dev;
550
551static int __init apne_module_init(void)
552{
553	apne_dev = apne_probe(-1);
554	if (IS_ERR(apne_dev))
555		return PTR_ERR(apne_dev);
556	return 0;
557}
558
559static void __exit apne_module_exit(void)
560{
561	unregister_netdev(apne_dev);
562
563	pcmcia_disable_irq();
564
565	free_irq(IRQ_AMIGA_PORTS, apne_dev);
566
567	pcmcia_reset();
568
569	release_region(IOBASE, 0x20);
570
571	free_netdev(apne_dev);
572}
573module_init(apne_module_init);
574module_exit(apne_module_exit);
575#endif
576
577static int init_pcmcia(void)
578{
579	u_char config;
580#ifndef MANUAL_CONFIG
581	u_char tuple[32];
582	int offset_len;
583#endif
584	u_long offset;
585
586	pcmcia_reset();
587	pcmcia_program_voltage(PCMCIA_0V);
588	pcmcia_access_speed(PCMCIA_SPEED_250NS);
589	pcmcia_write_enable();
590
591#ifdef MANUAL_CONFIG
592	config = MANUAL_CONFIG;
593#else
594	/* get and write config byte to enable IO port */
595
596	if (pcmcia_copy_tuple(CISTPL_CFTABLE_ENTRY, tuple, 32) < 3)
597		return 0;
598
599	config = tuple[2] & 0x3f;
600#endif
601#ifdef MANUAL_OFFSET
602	offset = MANUAL_OFFSET;
603#else
604	if (pcmcia_copy_tuple(CISTPL_CONFIG, tuple, 32) < 6)
605		return 0;
606
607	offset_len = (tuple[2] & 0x3) + 1;
608	offset = 0;
609	while(offset_len--) {
610		offset = (offset << 8) | tuple[4+offset_len];
611	}
612#endif
613
614	out_8(GAYLE_ATTRIBUTE+offset, config);
615
616	return 1;
617}
618
 
619MODULE_LICENSE("GPL");