2 * IPE405 (IBM IAP 405 chip evaluation board) Debug Support Board
4 * (C) Copyright 2001 by S.nishino (jl04348@jp.ibm.com) IBM-Japan
6 * ---------- Strategy ----------
8 * This NIC is RTL8019AS, simply connected to External Bus Controller
9 * of IAP 405 chip. As many folks of 8390 based NIC, 8390 core driver
10 * is usable. luckily, the following driver is already available for
11 * Amiga zorro bus (however I don't know this architecture beyond
12 * below), this is modified based on this driver (ariadne2).
14 * ---------- original header ----------
15 * Amiga Linux/m68k Ariadne II Ethernet Driver
17 * (C) Copyright 1998 by some Elitist 680x0 Users(TM)
19 * ---------------------------------------------------------------------------
21 * This program is based on all the other NE2000 drivers for Linux
23 * ---------------------------------------------------------------------------
25 * This file is subject to the terms and conditions of the GNU General Public
26 * License. See the file COPYING in the main directory of the Linux
27 * distribution for more details. */
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/irq.h>
40 #include <asm/system.h>
43 #include <asm/ppc4xx_pic.h>
44 #if defined(CONFIG_ARCTIC2)
45 #include <platforms/arctic2.h>
47 #error The driver only works on Arctic
53 #define ARCTIC_ENET_BASE8 (ARCTIC2_FPGA8_PADDR + 256*1024)
54 #define ARCTIC_ENET_BASE16 (ARCTIC2_FPGA16_PADDR + 0)
56 #define ARCTIC_ENET_IOBASE 0x0300 /* io base offset from NIC region */
58 #define ARCTIC_ENET_IRQ 29 /* irq number in UIC */
59 #define ARCTIC_ENET_IRQ_MASK (0x80000000 >> ARCTIC_ENET_IRQ)
61 #define NE_BASE (ARCTIC_ENET_BASE8 + ARCTIC_ENET_IOBASE)
62 #define NE_BASE16 (ARCTIC_ENET_BASE16 + ARCTIC_ENET_IOBASE)
64 /* 8390 register address */
66 #define NE_DATAPORT (0x10) /* NatSemi-defined port window offset. */
67 #define NE_DATAPORT16 (NE_DATAPORT / sizeof(u16))
68 #define NE_RESET (0x1f) /* Issue a read to reset, a write to clear. */
69 #define NE_IO_EXTENT (0x20) /* region extent */
71 #define NE_EN0_ISR (0x07)
72 #define NE_EN0_DCFG (0x0e)
74 #define NE_EN0_RSARLO (0x08)
75 #define NE_EN0_RSARHI (0x09)
76 #define NE_EN0_RCNTLO (0x0a)
77 #define NE_EN0_RXCR (0x0c)
78 #define NE_EN0_TXCR (0x0d)
79 #define NE_EN0_RCNTHI (0x0b)
80 #define NE_EN0_IMR (0x0f)
82 /* 8390 packet buffer page number */
83 #define NESM_START_PG 0x40 /* First page of TX buffer */
84 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
89 static int arctic_enet_probe(struct net_device *dev);
90 static int arctic_enet_init(struct net_device *dev);
92 static int arctic_enet_open(struct net_device *dev);
93 static int arctic_enet_close(struct net_device *dev);
95 static void arctic_enet_reset_8390(struct net_device *dev);
96 static void arctic_enet_get_8390_hdr(struct net_device *dev,
97 struct e8390_pkt_hdr *hdr,
99 static void arctic_enet_block_input(struct net_device *dev, int count,
100 struct sk_buff *skb, int ring_offset);
101 static void arctic_enet_block_output(struct net_device *dev,
103 const unsigned char *buf,
104 const int start_page);
106 /* These macros will do something on Arctic-I if we ever add support
108 #define switch_16bit_bank() do { } while (0)
109 #define switch_8bit_bank() do { } while (0)
111 void p_dump(unsigned char *p, int sz)
118 printk("------ PACKET START : %d Bytes ------ \n", sz);
120 for (i = 0; i < sz; i++) {
122 printk("\n %04X: %02X ", i, *wp);
123 } else if (i % 16 == 15) {
126 printk("%02X ", *wp);
131 printk("------ PACKET END ------ \n");
134 /* Code for reading the MAC address from the Arctic ethernet based on
135 * similar code in PIBS */
137 static void __init writereg_9346(volatile u8 *iobase, u8 value)
139 /* Switch to register page 3 */
140 writeb(readb(iobase + NE_CMD) | 0xc0, iobase + NE_CMD);
141 writeb(value, iobase + 0x01);
144 static u8 __init readreg_9346(volatile u8 *iobase)
146 /* Switch to register page 3 */
147 writeb(readb(iobase + NE_CMD) | 0xc0, iobase + NE_CMD);
148 return readb(iobase + 0x01);
151 static void __init write_bit_9346(volatile u8 *iobase, u8 bit)
155 writereg_9346(iobase, (readreg_9346(iobase) & mask) | bit);
157 writereg_9346(iobase, (readreg_9346(iobase) & mask) | bit | 0x04);
161 static u8 __init read_bit_9346(volatile u8 *iobase)
167 writereg_9346(iobase, readreg_9346(iobase) & mask);
169 writereg_9346(iobase, (readreg_9346(iobase) & mask) | 0x04);
170 bit = readreg_9346(iobase) & 0x01;
176 static u16 __init arctic_read_9346(volatile u8 *iobase, unsigned long addr)
182 local_irq_save(flags);
184 /* Put the chip into 8390 programming mode */
185 writereg_9346(iobase, (readreg_9346(iobase) & ~0xc0) | 0x80);
188 /* Send command (read 16-bit value) to EEPROM */
190 writereg_9346(iobase, readreg_9346(iobase) & ~0x0f);
193 writereg_9346(iobase, (readreg_9346(iobase) & ~0x0f) | 0x08);
197 write_bit_9346(iobase, 0x02);
198 /* Send opcode 0b10 */
199 write_bit_9346(iobase, 0x02);
200 write_bit_9346(iobase, 0x00);
201 /* Send address to read */
202 for (i = 0; i < 6; i++) {
204 write_bit_9346(iobase, 0x02);
206 write_bit_9346(iobase, 0x00);
210 /* Read the value back, bit by bit */
212 for (i = 0; i < 16; i++) {
214 if (read_bit_9346(iobase))
219 writereg_9346(iobase, readreg_9346(iobase) & ~0x0f);
221 /* Bring the chip out of 8390 programming mode */
222 writereg_9346(iobase, readreg_9346(iobase) & ~0xc0);
225 /* Return to register page 0 */
226 writeb(readb(iobase + NE_CMD) & ~0xc0, iobase + NE_CMD);
229 local_irq_restore(flags);
234 static void __init arctic_get_macaddr(struct net_device *dev)
236 u16 t0, t1, t2, v0, v1;
238 t0 = arctic_read_9346(iobase8, 0);
239 t1 = arctic_read_9346(iobase8, 2);
240 t2 = arctic_read_9346(iobase8, 4);
241 v0 = arctic_read_9346(iobase8, 6);
242 v1 = arctic_read_9346(iobase8, 8);
244 if ( (v0 != 0x4d50) || (v1 != 0x5400) ) {
245 printk(KERN_WARNING "%s: MAC address is not set in EEPROM\n", dev->name);
249 dev->dev_addr[0] = t0 >> 8;
250 dev->dev_addr[1] = t0 & 0xff;
251 dev->dev_addr[2] = t1 >> 8;
252 dev->dev_addr[3] = t1 & 0xff;
253 dev->dev_addr[4] = t2 >> 8;
254 dev->dev_addr[5] = t2 & 0xff;
257 int __init arctic_enet_probe(struct net_device *dev)
259 unsigned long reset_start_time;
262 /* Reset card. Who knows what dain-bramaged state it was left in. */
263 reset_start_time = jiffies;
265 writeb(readb(iobase8 + NE_RESET), iobase8 + NE_RESET);
267 while ((readb(iobase8 + NE_EN0_ISR) & ENISR_RESET) == 0)
268 if (jiffies - reset_start_time > 2 * HZ / 100) {
269 printk("arctic_enet: not found (no reset ack).\n");
273 writeb(0xff, iobase8 + NE_EN0_ISR); /* Ack all intr. */
275 arctic_get_macaddr(dev);
277 printk("arctic_enet: found at 0x%08x/0x%08x, MAC address "
278 "%02x:%02x:%02x:%02x:%02x:%02x\n",
280 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
281 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
283 /* Hack to let 8390.c work properly - it assumes IO space
285 dev->base_addr = (unsigned long)iobase8 - _IO_BASE;
286 dev->irq = ARCTIC_ENET_IRQ;
291 static int __init arctic_enet_init(struct net_device *dev)
293 static u32 arctic_enet_offsets[16] = {
294 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
295 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
298 /* Since this irq is connected to uic as edge interrupt, its pending must be cleared. */
299 /* FIXME: it would be nice to get rid of the direct reference
300 * to the 4xx irq structure */
301 ppc4xx_pic->ack(dev->irq);
303 /* Install the Interrupt handler */
304 if (request_irq(dev->irq, ei_interrupt, SA_SHIRQ, dev->name, dev))
307 /* Allocate dev->priv and fill in 8390 specific dev fields. */
308 if (ethdev_init(dev)) {
309 printk(" Unable to get memory for dev->priv.\n");
314 * Fill 8390 specific member for 8390 core driver
316 ei_status.name = "RTL8019AS";
317 ei_status.tx_start_page = NESM_START_PG;
318 ei_status.stop_page = NESM_STOP_PG;
319 ei_status.word16 = 1;
320 ei_status.rx_start_page = NESM_START_PG + TX_PAGES;
322 ei_status.reset_8390 = &arctic_enet_reset_8390;
323 ei_status.block_input = &arctic_enet_block_input;
324 ei_status.block_output = &arctic_enet_block_output;
325 ei_status.get_8390_hdr = &arctic_enet_get_8390_hdr;
326 ei_status.reg_offset = arctic_enet_offsets;
332 static int arctic_enet_open(struct net_device *dev)
343 static int arctic_enet_close(struct net_device *dev)
355 /* Hard reset the card. This used to pause for the same period that a
356 8390 reset command required, but that shouldn't be necessary. */
357 static void arctic_enet_reset_8390(struct net_device *dev)
359 unsigned long reset_start_time = jiffies;
362 printk("resetting the 8390 t=%ld...", jiffies);
364 writeb(readb(iobase8 + NE_RESET), iobase8 + NE_RESET);
367 ei_status.dmaing = 0;
369 /* This check _should_not_ be necessary, omit eventually. */
370 while ((readb(iobase8 + NE_EN0_ISR) & ENISR_RESET) == 0)
371 if (jiffies - reset_start_time > 2 * HZ / 100) {
372 printk("%s: ne_reset_8390() did not complete.\n",
376 writeb(ENISR_RESET, iobase8 + NE_EN0_ISR); /* Ack intr. */
379 /* Grab the 8390 specific header. Similar to the block_input routine, but
380 we don't need to be concerned with ring wrap as the header will be at
381 the start of a page, so we optimize accordingly. */
383 static void arctic_enet_get_8390_hdr(struct net_device *dev,
384 struct e8390_pkt_hdr *hdr,
391 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
392 if (ei_status.dmaing) {
393 printk("%s: DMAing conflict in ne_get_8390_hdr "
394 "[DMAstat:%d][irqlock:%d].\n", dev->name,
395 ei_status.dmaing, ei_status.irqlock);
399 ei_status.dmaing |= 0x01;
400 writeb(E8390_NODMA + E8390_PAGE0 + E8390_START, iobase8 + NE_CMD);
401 writeb(ENISR_RDC, iobase8 + NE_EN0_ISR);
402 writeb(sizeof(struct e8390_pkt_hdr), iobase8 + NE_EN0_RCNTLO);
403 writeb(0, iobase8 + NE_EN0_RCNTHI);
404 writeb(0, iobase8 + NE_EN0_RSARLO); /* On page boundary */
405 writeb(ring_page, iobase8 + NE_EN0_RSARHI);
406 writeb(E8390_RREAD + E8390_START, iobase8 + NE_CMD);
408 if (ei_status.word16) {
411 for (cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr) >> 1);
413 *ptrs++ = in_be16((u16 *) (iobase16 + NE_DATAPORT16));
417 ptrc = (unsigned char *) hdr;
418 for (cnt = 0; cnt < sizeof(struct e8390_pkt_hdr); cnt++)
419 *ptrc++ = readb(iobase8 + NE_DATAPORT);
423 writeb(ENISR_RDC, iobase8 + NE_EN0_ISR); /* Ack intr. */
425 /* I am Big Endian, but received byte count is Little Endian. */
426 hdr->count = le16_to_cpu(hdr->count);
428 ei_status.dmaing &= ~0x01;
431 /* Block input and output, similar to the Crynwr packet driver. If you
432 are porting to a new ethercard, look at the packet driver source for hints.
433 The NEx000 doesn't share the on-board packet memory -- you have to put
434 the packet out through the "remote DMA" dataport using writeb. */
436 static void arctic_enet_block_input(struct net_device *dev, int count,
437 struct sk_buff *skb, int ring_offset)
439 char *buf = skb->data;
445 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
446 if (ei_status.dmaing) {
447 printk("%s: DMAing conflict in ne_block_input "
448 "[DMAstat:%d][irqlock:%d].\n",
449 dev->name, ei_status.dmaing, ei_status.irqlock);
452 ei_status.dmaing |= 0x01;
453 writeb(E8390_NODMA + E8390_PAGE0 + E8390_START, iobase8 + NE_CMD);
454 writeb(ENISR_RDC, iobase8 + NE_EN0_ISR);
455 writeb(count & 0xff, iobase8 + NE_EN0_RCNTLO);
456 writeb(count >> 8, iobase8 + NE_EN0_RCNTHI);
457 writeb(ring_offset & 0xff, iobase8 + NE_EN0_RSARLO);
458 writeb(ring_offset >> 8, iobase8 + NE_EN0_RSARHI);
459 writeb(E8390_RREAD + E8390_START, iobase8 + NE_CMD);
462 if (ei_status.word16) {
467 for (cnt = 0; cnt < (count >> 1); cnt++)
468 /* At 16 bits mode, bus acts as Little Endian mode
469 That's swap is needed ??? */
470 *ptrs++ = in_be16((u16 *) (iobase16 + NE_DATAPORT16));
474 buf[count - 1] = readb(iobase8 + NE_DATAPORT);
479 ptrc = (unsigned char *) buf;
480 for (cnt = 0; cnt < count; cnt++)
481 *ptrc++ = readb(iobase8 + NE_DATAPORT);
484 writeb(ENISR_RDC, iobase8 + NE_EN0_ISR); /* Ack intr. */
485 ei_status.dmaing &= ~0x01;
488 static void arctic_enet_block_output(struct net_device *dev, int count,
489 const unsigned char *buf,
490 const int start_page)
492 unsigned long dma_start;
497 /* Round the count up for word writes. Do we need to do this?
498 What effect will an odd byte count have on the 8390?
499 I should check someday. */
503 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
504 if (ei_status.dmaing) {
505 printk("%s: DMAing conflict in ne_block_output."
506 "[DMAstat:%d][irqlock:%d]\n", dev->name,
507 ei_status.dmaing, ei_status.irqlock);
510 ei_status.dmaing |= 0x01;
512 #if 1 /* FIXME: not sure what this is for -dwg */
513 writeb(0x42, iobase8 + EN0_RCNTLO);
514 writeb(0x00, iobase8 + EN0_RCNTHI);
515 writeb(0x42, iobase8 + EN0_RSARLO);
516 writeb(0x00, iobase8 + EN0_RSARHI);
518 /* We should already be in page 0, but to be safe... */
519 writeb(E8390_PAGE0 + E8390_START + E8390_NODMA, iobase8 + NE_CMD);
521 writeb(ENISR_RDC, iobase8 + NE_EN0_ISR);
523 /* Now the normal output. */
524 writeb(count & 0xff, iobase8 + NE_EN0_RCNTLO);
525 writeb(count >> 8, iobase8 + NE_EN0_RCNTHI);
526 writeb(0x00, iobase8 + NE_EN0_RSARLO);
527 writeb(start_page, iobase8 + NE_EN0_RSARHI);
529 writeb(E8390_RWRITE + E8390_START, iobase8 + NE_CMD);
531 if (ei_status.word16) {
535 for (cnt = 0; cnt < count >> 1; cnt++) {
536 /* At 16 bits mode, bus acts as Little Endian mode
537 That's swap is needed ??? */
538 out_be16((u16 *) (iobase16 + NE_DATAPORT16),
546 ptrc = (unsigned char *) buf;
547 for (cnt = 0; cnt < count; cnt++)
548 writeb(*ptrc++, iobase8 + NE_DATAPORT);
553 while ((readb(iobase8 + NE_EN0_ISR) & ENISR_RDC) == 0)
554 if (jiffies - dma_start > 2 * HZ / 100) { /* 20ms */
555 printk("%s: timeout waiting for Tx RDC.\n",
557 arctic_enet_reset_8390(dev);
562 writeb(ENISR_RDC, iobase8 + NE_EN0_ISR); /* Ack intr. */
563 ei_status.dmaing &= ~0x01;
567 static struct net_device arctic_enet_dev = {
568 .init = arctic_enet_init,
569 .open = arctic_enet_open,
570 .stop = arctic_enet_close,
573 int init_arctic_enet(void)
575 struct net_device *dev = &arctic_enet_dev;
580 /* First set up our IO regions */
581 if (! request_mem_region(NE_BASE, NE_IO_EXTENT, "arctic_enet"))
585 iobase8 = ioremap(NE_BASE, NE_IO_EXTENT);
591 if (NE_BASE16 != NE_BASE) {
592 if (! request_mem_region(NE_BASE16, NE_IO_EXTENT, "arctic_enet"))
597 iobase16 = ioremap(NE_BASE16, NE_IO_EXTENT);
605 mtdcr(DCRN_UIC0_TR, mfdcr(DCRN_UIC0_TR) | ARCTIC_ENET_IRQ_MASK);
606 mtdcr(DCRN_UIC0_PR, mfdcr(DCRN_UIC0_PR) | ARCTIC_ENET_IRQ_MASK);
607 mtdcr(DCRN_UIC0_SR, ARCTIC_ENET_IRQ_MASK);
610 err = arctic_enet_probe(dev);
612 printk(KERN_ERR "arctic_enet: No Arctic ethernet card found.\n");
616 err = register_netdev(dev);
626 release_mem_region(NE_BASE16, NE_IO_EXTENT);
630 release_mem_region(NE_BASE, NE_IO_EXTENT);
636 void remove_arctic_enet(void)
638 unregister_netdev(&arctic_enet_dev);
639 free_irq(ARCTIC_ENET_IRQ, &arctic_enet_dev);
643 release_mem_region(NE_BASE16, NE_IO_EXTENT);
647 release_mem_region(NE_BASE, NE_IO_EXTENT);
651 module_init(init_arctic_enet);
652 module_exit(remove_arctic_enet);