More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / ariadne2.c
blob7678e510a7f654491acbfa24febf41150094c3ec
1 /*
2 * Amiga Linux/m68k and Linux/PPC Ariadne II and X-Surf Ethernet Driver
4 * (C) Copyright 1998-2000 by some Elitist 680x0 Users(TM)
6 * ---------------------------------------------------------------------------
8 * This program is based on all the other NE2000 drivers for Linux
10 * ---------------------------------------------------------------------------
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file COPYING in the main directory of the Linux
14 * distribution for more details.
16 * ---------------------------------------------------------------------------
18 * The Ariadne II and X-Surf are Zorro-II boards containing Realtek RTL8019AS
19 * Ethernet Controllers.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/zorro.h>
31 #include <asm/system.h>
32 #include <asm/irq.h>
33 #include <asm/amigaints.h>
34 #include <asm/amigahw.h>
36 #include "8390.h"
39 #define NE_BASE (dev->base_addr)
40 #define NE_CMD (0x00*2)
41 #define NE_DATAPORT (0x10*2) /* NatSemi-defined port window offset. */
42 #define NE_RESET (0x1f*2) /* Issue a read to reset, a write to clear. */
43 #define NE_IO_EXTENT (0x20*2)
45 #define NE_EN0_ISR (0x07*2)
46 #define NE_EN0_DCFG (0x0e*2)
48 #define NE_EN0_RSARLO (0x08*2)
49 #define NE_EN0_RSARHI (0x09*2)
50 #define NE_EN0_RCNTLO (0x0a*2)
51 #define NE_EN0_RXCR (0x0c*2)
52 #define NE_EN0_TXCR (0x0d*2)
53 #define NE_EN0_RCNTHI (0x0b*2)
54 #define NE_EN0_IMR (0x0f*2)
56 #define NESM_START_PG 0x40 /* First page of TX buffer */
57 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
60 #define WORDSWAP(a) ((((a)>>8)&0xff) | ((a)<<8))
62 #ifdef MODULE
63 static struct net_device *root_ariadne2_dev;
64 #endif
66 static const struct card_info {
67 zorro_id id;
68 const char *name;
69 unsigned int offset;
70 } cards[] __initdata = {
71 { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, "Ariadne II", 0x0600 },
72 { ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, "X-Surf", 0x8600 },
75 static int __init ariadne2_probe(void);
76 static int __init ariadne2_init(struct net_device *dev, unsigned long board,
77 const char *name, unsigned long ioaddr);
78 static int ariadne2_open(struct net_device *dev);
79 static int ariadne2_close(struct net_device *dev);
80 static void ariadne2_reset_8390(struct net_device *dev);
81 static void ariadne2_get_8390_hdr(struct net_device *dev,
82 struct e8390_pkt_hdr *hdr, int ring_page);
83 static void ariadne2_block_input(struct net_device *dev, int count,
84 struct sk_buff *skb, int ring_offset);
85 static void ariadne2_block_output(struct net_device *dev, const int count,
86 const unsigned char *buf,
87 const int start_page);
88 static void __exit ariadne2_cleanup(void);
90 static int __init ariadne2_probe(void)
92 struct net_device *dev;
93 struct zorro_dev *z = NULL;
94 unsigned long board, ioaddr;
95 int err = -ENODEV;
96 int i;
98 while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
99 for (i = ARRAY_SIZE(cards)-1; i >= 0; i--)
100 if (z->id == cards[i].id)
101 break;
102 if (i < 0)
103 continue;
104 board = z->resource.start;
105 ioaddr = board+cards[i].offset;
106 dev = init_etherdev(0, 0);
107 SET_MODULE_OWNER(dev);
108 if (!dev)
109 return -ENOMEM;
110 if (!request_mem_region(ioaddr, NE_IO_EXTENT*2, dev->name)) {
111 kfree(dev);
112 continue;
114 if ((err = ariadne2_init(dev, board, cards[i].name,
115 ZTWO_VADDR(ioaddr)))) {
116 release_mem_region(ioaddr, NE_IO_EXTENT*2);
117 kfree(dev);
118 return err;
120 err = 0;
123 if (err == -ENODEV)
124 printk("No Ariadne II or X-Surf ethernet card found.\n");
125 return err;
128 static int __init ariadne2_init(struct net_device *dev, unsigned long board,
129 const char *name, unsigned long ioaddr)
131 int i;
132 unsigned char SA_prom[32];
133 int start_page, stop_page;
134 static u32 ariadne2_offsets[16] = {
135 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
136 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
139 /* Reset card. Who knows what dain-bramaged state it was left in. */
141 unsigned long reset_start_time = jiffies;
143 z_writeb(z_readb(ioaddr + NE_RESET), ioaddr + NE_RESET);
145 while ((z_readb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
146 if (jiffies - reset_start_time > 2*HZ/100) {
147 printk(" not found (no reset ack).\n");
148 return -ENODEV;
151 z_writeb(0xff, ioaddr + NE_EN0_ISR); /* Ack all intr. */
154 /* Read the 16 bytes of station address PROM.
155 We must first initialize registers, similar to NS8390_init(eifdev, 0).
156 We can't reliably read the SAPROM address without this.
157 (I learned the hard way!). */
159 struct {
160 u32 value;
161 u32 offset;
162 } program_seq[] = {
163 {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
164 {0x48, NE_EN0_DCFG}, /* Set byte-wide (0x48) access. */
165 {0x00, NE_EN0_RCNTLO}, /* Clear the count regs. */
166 {0x00, NE_EN0_RCNTHI},
167 {0x00, NE_EN0_IMR}, /* Mask completion irq. */
168 {0xFF, NE_EN0_ISR},
169 {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
170 {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode. */
171 {32, NE_EN0_RCNTLO},
172 {0x00, NE_EN0_RCNTHI},
173 {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
174 {0x00, NE_EN0_RSARHI},
175 {E8390_RREAD+E8390_START, NE_CMD},
177 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
178 z_writeb(program_seq[i].value, ioaddr + program_seq[i].offset);
181 for (i = 0; i < 16; i++) {
182 SA_prom[i] = z_readb(ioaddr + NE_DATAPORT);
183 (void)z_readb(ioaddr + NE_DATAPORT);
186 /* We must set the 8390 for word mode. */
187 z_writeb(0x49, ioaddr + NE_EN0_DCFG);
188 start_page = NESM_START_PG;
189 stop_page = NESM_STOP_PG;
191 dev->base_addr = ioaddr;
192 dev->irq = IRQ_AMIGA_PORTS;
194 /* Install the Interrupt handler */
195 i = request_irq(IRQ_AMIGA_PORTS, ei_interrupt, SA_SHIRQ, dev->name, dev);
196 if (i) return i;
198 /* Allocate dev->priv and fill in 8390 specific dev fields. */
199 if (ethdev_init(dev)) {
200 printk("Unable to get memory for dev->priv.\n");
201 return -ENOMEM;
204 for(i = 0; i < ETHER_ADDR_LEN; i++) {
205 #ifdef DEBUG
206 printk(" %2.2x", SA_prom[i]);
207 #endif
208 dev->dev_addr[i] = SA_prom[i];
211 printk("%s: %s at 0x%08lx, Ethernet Address "
212 "%02x:%02x:%02x:%02x:%02x:%02x\n", dev->name, name, board,
213 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
214 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
216 ei_status.name = name;
217 ei_status.tx_start_page = start_page;
218 ei_status.stop_page = stop_page;
219 ei_status.word16 = 1;
221 ei_status.rx_start_page = start_page + TX_PAGES;
223 ei_status.reset_8390 = &ariadne2_reset_8390;
224 ei_status.block_input = &ariadne2_block_input;
225 ei_status.block_output = &ariadne2_block_output;
226 ei_status.get_8390_hdr = &ariadne2_get_8390_hdr;
227 ei_status.reg_offset = ariadne2_offsets;
228 dev->open = &ariadne2_open;
229 dev->stop = &ariadne2_close;
230 #ifdef MODULE
231 ei_status.priv = (unsigned long)root_ariadne2_dev;
232 root_ariadne2_dev = dev;
233 #endif
234 NS8390_init(dev, 0);
235 return 0;
238 static int ariadne2_open(struct net_device *dev)
240 ei_open(dev);
241 return 0;
244 static int ariadne2_close(struct net_device *dev)
246 if (ei_debug > 1)
247 printk("%s: Shutting down ethercard.\n", dev->name);
248 ei_close(dev);
249 return 0;
252 /* Hard reset the card. This used to pause for the same period that a
253 8390 reset command required, but that shouldn't be necessary. */
254 static void ariadne2_reset_8390(struct net_device *dev)
256 unsigned long reset_start_time = jiffies;
258 if (ei_debug > 1)
259 printk("resetting the 8390 t=%ld...", jiffies);
261 z_writeb(z_readb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
263 ei_status.txing = 0;
264 ei_status.dmaing = 0;
266 /* This check _should_not_ be necessary, omit eventually. */
267 while ((z_readb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
268 if (jiffies - reset_start_time > 2*HZ/100) {
269 printk("%s: ne_reset_8390() did not complete.\n", dev->name);
270 break;
272 z_writeb(ENISR_RESET, NE_BASE + NE_EN0_ISR); /* Ack intr. */
275 /* Grab the 8390 specific header. Similar to the block_input routine, but
276 we don't need to be concerned with ring wrap as the header will be at
277 the start of a page, so we optimize accordingly. */
279 static void ariadne2_get_8390_hdr(struct net_device *dev,
280 struct e8390_pkt_hdr *hdr, int ring_page)
282 int nic_base = dev->base_addr;
283 int cnt;
284 short *ptrs;
286 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
287 if (ei_status.dmaing) {
288 printk("%s: DMAing conflict in ne_get_8390_hdr "
289 "[DMAstat:%d][irqlock:%d].\n", dev->name, ei_status.dmaing,
290 ei_status.irqlock);
291 return;
294 ei_status.dmaing |= 0x01;
295 z_writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
296 z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
297 z_writeb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
298 z_writeb(0, nic_base + NE_EN0_RCNTHI);
299 z_writeb(0, nic_base + NE_EN0_RSARLO); /* On page boundary */
300 z_writeb(ring_page, nic_base + NE_EN0_RSARHI);
301 z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
303 ptrs = (short*)hdr;
304 for (cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
305 *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
307 z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
309 hdr->count = WORDSWAP(hdr->count);
311 ei_status.dmaing &= ~0x01;
314 /* Block input and output, similar to the Crynwr packet driver. If you
315 are porting to a new ethercard, look at the packet driver source for hints.
316 The NEx000 doesn't share the on-board packet memory -- you have to put
317 the packet out through the "remote DMA" dataport using z_writeb. */
319 static void ariadne2_block_input(struct net_device *dev, int count,
320 struct sk_buff *skb, int ring_offset)
322 int nic_base = dev->base_addr;
323 char *buf = skb->data;
324 short *ptrs;
325 int cnt;
327 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
328 if (ei_status.dmaing) {
329 printk("%s: DMAing conflict in ne_block_input "
330 "[DMAstat:%d][irqlock:%d].\n",
331 dev->name, ei_status.dmaing, ei_status.irqlock);
332 return;
334 ei_status.dmaing |= 0x01;
335 z_writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
336 z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
337 z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
338 z_writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
339 z_writeb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
340 z_writeb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
341 z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
342 ptrs = (short*)buf;
343 for (cnt = 0; cnt < (count>>1); cnt++)
344 *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
345 if (count & 0x01)
346 buf[count-1] = z_readb(NE_BASE + NE_DATAPORT);
348 z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
349 ei_status.dmaing &= ~0x01;
352 static void ariadne2_block_output(struct net_device *dev, int count,
353 const unsigned char *buf,
354 const int start_page)
356 int nic_base = NE_BASE;
357 unsigned long dma_start;
358 short *ptrs;
359 int cnt;
361 /* Round the count up for word writes. Do we need to do this?
362 What effect will an odd byte count have on the 8390?
363 I should check someday. */
364 if (count & 0x01)
365 count++;
367 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
368 if (ei_status.dmaing) {
369 printk("%s: DMAing conflict in ne_block_output."
370 "[DMAstat:%d][irqlock:%d]\n", dev->name, ei_status.dmaing,
371 ei_status.irqlock);
372 return;
374 ei_status.dmaing |= 0x01;
375 /* We should already be in page 0, but to be safe... */
376 z_writeb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
378 z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
380 /* Now the normal output. */
381 z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
382 z_writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
383 z_writeb(0x00, nic_base + NE_EN0_RSARLO);
384 z_writeb(start_page, nic_base + NE_EN0_RSARHI);
386 z_writeb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
387 ptrs = (short*)buf;
388 for (cnt = 0; cnt < count>>1; cnt++)
389 z_writew(*ptrs++, NE_BASE+NE_DATAPORT);
391 dma_start = jiffies;
393 while ((z_readb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
394 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
395 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
396 ariadne2_reset_8390(dev);
397 NS8390_init(dev,1);
398 break;
401 z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
402 ei_status.dmaing &= ~0x01;
403 return;
406 static void __exit ariadne2_cleanup(void)
408 #ifdef MODULE
409 struct net_device *dev, *next;
411 while ((dev = root_ariadne2_dev)) {
412 next = (struct net_device *)(ei_status.priv);
413 unregister_netdev(dev);
414 free_irq(IRQ_AMIGA_PORTS, dev);
415 release_mem_region(ZTWO_PADDR(dev->base_addr), NE_IO_EXTENT*2);
416 kfree(dev);
417 root_ariadne2_dev = next;
419 #endif
422 module_init(ariadne2_probe);
423 module_exit(ariadne2_cleanup);
425 MODULE_LICENSE("GPL");