File: /usr/src/linux/drivers/net/arcnet/arc-rimi.c

1     /*
2      * Linux ARCnet driver - "RIM I" (entirely mem-mapped) cards
3      * 
4      * Written 1994-1999 by Avery Pennarun.
5      * Written 1999-2000 by Martin Mares <mj@suse.cz>.
6      * Derived from skeleton.c by Donald Becker.
7      *
8      * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
9      *  for sponsoring the further development of this driver.
10      *
11      * **********************
12      *
13      * The original copyright of skeleton.c was as follows:
14      *
15      * skeleton.c Written 1993 by Donald Becker.
16      * Copyright 1993 United States Government as represented by the
17      * Director, National Security Agency.  This software may only be used
18      * and distributed according to the terms of the GNU General Public License as
19      * modified by SRC, incorporated herein by reference.
20      *
21      * **********************
22      *
23      * For more details, see drivers/net/arcnet.c
24      *
25      * **********************
26      */
27     #include <linux/kernel.h>
28     #include <linux/module.h>
29     #include <linux/ioport.h>
30     #include <linux/slab.h>
31     #include <linux/delay.h>
32     #include <linux/netdevice.h>
33     #include <linux/bootmem.h>
34     #include <linux/init.h>
35     #include <asm/io.h>
36     #include <linux/arcdevice.h>
37     
38     
39     #define VERSION "arcnet: RIM I (entirely mem-mapped) support\n"
40     
41     
42     /* Internal function declarations */
43     
44     static int arcrimi_probe(struct net_device *dev);
45     static int arcrimi_found(struct net_device *dev);
46     static void arcrimi_command(struct net_device *dev, int command);
47     static int arcrimi_status(struct net_device *dev);
48     static void arcrimi_setmask(struct net_device *dev, int mask);
49     static int arcrimi_reset(struct net_device *dev, int really_reset);
50     static void arcrimi_openclose(struct net_device *dev, bool open);
51     static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
52     				 void *buf, int count);
53     static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
54     				   void *buf, int count);
55     
56     /* Handy defines for ARCnet specific stuff */
57     
58     /* Amount of I/O memory used by the card */
59     #define BUFFER_SIZE (512)
60     #define MIRROR_SIZE (BUFFER_SIZE*4)
61     
62     /* COM 9026 controller chip --> ARCnet register addresses */
63     #define _INTMASK (ioaddr+0)	/* writable */
64     #define _STATUS  (ioaddr+0)	/* readable */
65     #define _COMMAND (ioaddr+1)	/* writable, returns random vals on read (?) */
66     #define _RESET  (ioaddr+8)	/* software reset (on read) */
67     #define _MEMDATA  (ioaddr+12)	/* Data port for IO-mapped memory */
68     #define _ADDR_HI  (ioaddr+15)	/* Control registers for said */
69     #define _ADDR_LO  (ioaddr+14)
70     #define _CONFIG  (ioaddr+2)	/* Configuration register */
71     
72     #undef ASTATUS
73     #undef ACOMMAND
74     #undef AINTMASK
75     
76     #define ASTATUS()	readb(_STATUS)
77     #define ACOMMAND(cmd)	writeb((cmd),_COMMAND)
78     #define AINTMASK(msk)	writeb((msk),_INTMASK)
79     #define SETCONF()	writeb(lp->config,_CONFIG)
80     
81     
82     /*
83      * We cannot probe for a RIM I card; one reason is I don't know how to reset
84      * them.  In fact, we can't even get their node ID automatically.  So, we
85      * need to be passed a specific shmem address, IRQ, and node ID.
86      */
87     static int __init arcrimi_probe(struct net_device *dev)
88     {
89     	BUGLVL(D_NORMAL) printk(VERSION);
90     	BUGLVL(D_NORMAL) printk("E-mail me if you actually test the RIM I driver, please!\n");
91     
92     	BUGMSG(D_NORMAL, "Given: node %02Xh, shmem %lXh, irq %d\n",
93     	       dev->dev_addr[0], dev->mem_start, dev->irq);
94     
95     	if (dev->mem_start <= 0 || dev->irq <= 0) {
96     		BUGMSG(D_NORMAL, "No autoprobe for RIM I; you "
97     		       "must specify the shmem and irq!\n");
98     		return -ENODEV;
99     	}
100     	if (check_mem_region(dev->mem_start, BUFFER_SIZE)) {
101     		BUGMSG(D_NORMAL, "Card memory already allocated\n");
102     		return -ENODEV;
103     	}
104     	if (dev->dev_addr[0] == 0) {
105     		BUGMSG(D_NORMAL, "You need to specify your card's station "
106     		       "ID!\n");
107     		return -ENODEV;
108     	}
109     	return arcrimi_found(dev);
110     }
111     
112     
113     /*
114      * Set up the struct net_device associated with this card.  Called after
115      * probing succeeds.
116      */
117     static int __init arcrimi_found(struct net_device *dev)
118     {
119     	struct arcnet_local *lp;
120     	u_long first_mirror, last_mirror, shmem;
121     	int mirror_size;
122     
123     	/* reserve the irq */  {
124     		if (request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet (RIM I)", dev))
125     			BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
126     		return -ENODEV;
127     	}
128     
129     	shmem = dev->mem_start;
130     	isa_writeb(TESTvalue, shmem);
131     	isa_writeb(dev->dev_addr[0], shmem + 1);	/* actually the node ID */
132     
133     	/* find the real shared memory start/end points, including mirrors */
134     
135     	/* guess the actual size of one "memory mirror" - the number of
136     	 * bytes between copies of the shared memory.  On most cards, it's
137     	 * 2k (or there are no mirrors at all) but on some, it's 4k.
138     	 */
139     	mirror_size = MIRROR_SIZE;
140     	if (isa_readb(shmem) == TESTvalue
141     	    && isa_readb(shmem - mirror_size) != TESTvalue
142     	    && isa_readb(shmem - 2 * mirror_size) == TESTvalue)
143     		mirror_size *= 2;
144     
145     	first_mirror = last_mirror = shmem;
146     	while (isa_readb(first_mirror) == TESTvalue)
147     		first_mirror -= mirror_size;
148     	first_mirror += mirror_size;
149     
150     	while (isa_readb(last_mirror) == TESTvalue)
151     		last_mirror += mirror_size;
152     	last_mirror -= mirror_size;
153     
154     	dev->mem_start = first_mirror;
155     	dev->mem_end = last_mirror + MIRROR_SIZE - 1;
156     	dev->rmem_start = dev->mem_start + BUFFER_SIZE * 0;
157     	dev->rmem_end = dev->mem_start + BUFFER_SIZE * 2 - 1;
158     
159     	/* initialize the rest of the device structure. */
160     
161     	lp = dev->priv = kmalloc(sizeof(struct arcnet_local), GFP_KERNEL);
162     	if (!lp) {
163     		BUGMSG(D_NORMAL, "Can't allocate device data!\n");
164     		goto err_free_irq;
165     	}
166     	lp->card_name = "RIM I";
167     	lp->hw.command = arcrimi_command;
168     	lp->hw.status = arcrimi_status;
169     	lp->hw.intmask = arcrimi_setmask;
170     	lp->hw.reset = arcrimi_reset;
171     	lp->hw.open_close = arcrimi_openclose;
172     	lp->hw.copy_to_card = arcrimi_copy_to_card;
173     	lp->hw.copy_from_card = arcrimi_copy_from_card;
174     	lp->mem_start = ioremap(dev->mem_start, dev->mem_end - dev->mem_start + 1);
175     	if (!lp->mem_start) {
176     		BUGMSG(D_NORMAL, "Can't remap device memory!\n");
177     		goto err_free_dev_priv;
178     	}
179     	/* Fill in the fields of the device structure with generic
180     	 * values.
181     	 */
182     	arcdev_setup(dev);
183     
184     	/* get and check the station ID from offset 1 in shmem */
185     	dev->dev_addr[0] = readb(lp->mem_start + 1);
186     
187     	/* reserve the memory region - guaranteed to work by check_region */
188     	request_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1, "arcnet (90xx)");
189     
190     	BUGMSG(D_NORMAL, "ARCnet RIM I: station %02Xh found at IRQ %d, "
191     	       "ShMem %lXh (%ld*%d bytes).\n",
192     	       dev->dev_addr[0],
193     	       dev->irq, dev->mem_start,
194     	 (dev->mem_end - dev->mem_start + 1) / mirror_size, mirror_size);
195     
196     	return 0;
197     
198           err_free_dev_priv:
199     	kfree(dev->priv);
200           err_free_irq:
201     	free_irq(dev->irq, dev);
202     	return -EIO;
203     }
204     
205     
206     /*
207      * Do a hardware reset on the card, and set up necessary registers.
208      *
209      * This should be called as little as possible, because it disrupts the
210      * token on the network (causes a RECON) and requires a significant delay.
211      *
212      * However, it does make sure the card is in a defined state.
213      */
214     static int arcrimi_reset(struct net_device *dev, int really_reset)
215     {
216     	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
217     	void *ioaddr = lp->mem_start + 0x800;
218     
219     	BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS());
220     
221     	if (really_reset) {
222     		writeb(TESTvalue, ioaddr - 0x800);	/* fake reset */
223     		return 0;
224     	}
225     	ACOMMAND(CFLAGScmd | RESETclear);	/* clear flags & end reset */
226     	ACOMMAND(CFLAGScmd | CONFIGclear);
227     
228     	/* enable extended (512-byte) packets */
229     	ACOMMAND(CONFIGcmd | EXTconf);
230     
231     	/* done!  return success. */
232     	return 0;
233     }
234     
235     
236     static void arcrimi_openclose(struct net_device *dev, int open)
237     {
238     	if (open)
239     		MOD_INC_USE_COUNT;
240     	else
241     		MOD_DEC_USE_COUNT;
242     }
243     
244     static void arcrimi_setmask(struct net_device *dev, int mask)
245     {
246     	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
247     	void *ioaddr = lp->mem_start + 0x800;
248     
249     	AINTMASK(mask);
250     }
251     
252     static int arcrimi_status(struct net_device *dev)
253     {
254     	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
255     	void *ioaddr = lp->mem_start + 0x800;
256     
257     	return ASTATUS();
258     }
259     
260     static void arcrimi_command(struct net_device *dev, int cmd)
261     {
262     	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
263     	void *ioaddr = lp->mem_start + 0x800;
264     
265     	ACOMMAND(cmd);
266     }
267     
268     static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
269     				 void *buf, int count)
270     {
271     	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
272     	void *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
273     	TIME("memcpy_toio", count, memcpy_toio(memaddr, buf, count));
274     }
275     
276     
277     static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
278     				   void *buf, int count)
279     {
280     	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
281     	void *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
282     	TIME("memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
283     }
284     
285     #ifdef MODULE
286     
287     static struct net_device *my_dev;
288     
289     /* Module parameters */
290     
291     static int node = 0;
292     static int io = 0x0;		/* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
293     static int irq = 0;		/* or use the insmod io= irq= shmem= options */
294     static char *device;		/* use eg. device="arc1" to change name */
295     
296     MODULE_PARM(node, "i");
297     MODULE_PARM(io, "i");
298     MODULE_PARM(irq, "i");
299     MODULE_PARM(device, "s");
300     
301     int init_module(void)
302     {
303     	struct net_device *dev;
304     	int err;
305     
306     	dev = dev_alloc(device ? : "arc%d", &err);
307     	if (!dev)
308     		return err;
309     
310     	if (node && node != 0xff)
311     		dev->dev_addr[0] = node;
312     
313     	dev->base_addr = io;
314     	dev->irq = irq;
315     	if (dev->irq == 2)
316     		dev->irq = 9;
317     
318     	if (arcrimi_probe(dev))
319     		return -EIO;
320     
321     	my_dev = dev;
322     	return 0;
323     }
324     
325     void cleanup_module(void)
326     {
327     	struct net_device *dev = my_dev;
328     	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
329     
330     	unregister_netdev(dev);
331     	free_irq(dev->irq, dev);
332     	iounmap(lp->mem_start);
333     	release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
334     	kfree(dev->priv);
335     	kfree(dev);
336     }
337     
338     #else
339     
340     static int __init arcrimi_setup(char *s)
341     {
342     	struct net_device *dev;
343     	int ints[8];
344     
345     	s = get_options(s, 8, ints);
346     	if (!ints[0])
347     		return 1;
348     	dev = alloc_bootmem(sizeof(struct net_device));
349     	memset(dev, 0, sizeof(struct net_device));
350     	dev->init = arcrimi_probe;
351     
352     	switch (ints[0]) {
353     	default:		/* ERROR */
354     		printk("arcrimi: Too many arguments.\n");
355     	case 3:		/* Node ID */
356     		dev->dev_addr[0] = ints[3];
357     	case 2:		/* IRQ */
358     		dev->irq = ints[2];
359     	case 1:		/* IO address */
360     		dev->mem_start = ints[1];
361     	}
362     	if (*s)
363     		strncpy(dev->name, s, 9);
364     	else
365     		strcpy(dev->name, "arc%d");
366     	if (register_netdev(dev))
367     		printk(KERN_ERR "arc-rimi: Cannot register arcnet device\n");
368     
369     	return 1;
370     }
371     
372     __setup("arcrimi=", arcrimi_setup);
373     
374     #endif				/* MODULE */
375