1/*
2 * Linux ARCnet driver - COM20020 chipset support
3 *
4 * Written 1997 by David Woodhouse.
5 * Written 1994-1999 by Avery Pennarun.
6 * Written 1999 by Martin Mares <mj@ucw.cz>.
7 * Derived from skeleton.c by Donald Becker.
8 *
9 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
10 *  for sponsoring the further development of this driver.
11 *
12 * **********************
13 *
14 * The original copyright of skeleton.c was as follows:
15 *
16 * skeleton.c Written 1993 by Donald Becker.
17 * Copyright 1993 United States Government as represented by the
18 * Director, National Security Agency.  This software may only be used
19 * and distributed according to the terms of the GNU General Public License as
20 * modified by SRC, incorporated herein by reference.
21 *
22 * **********************
23 *
24 * For more details, see drivers/net/arcnet.c
25 *
26 * **********************
27 */
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/types.h>
31#include <linux/ioport.h>
32#include <linux/slab.h>
33#include <linux/errno.h>
34#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/init.h>
37#include <linux/arcdevice.h>
38#include <linux/com20020.h>
39
40#include <asm/io.h>
41
42#define VERSION "arcnet: COM20020 chipset support (by David Woodhouse et al.)\n"
43
44static char *clockrates[] =
45{"10 Mb/s", "Reserved", "5 Mb/s",
46 "2.5 Mb/s", "1.25Mb/s", "625 Kb/s", "312.5 Kb/s",
47 "156.25 Kb/s", "Reserved", "Reserved", "Reserved"};
48
49static void com20020_command(struct net_device *dev, int command);
50static int com20020_status(struct net_device *dev);
51static void com20020_setmask(struct net_device *dev, int mask);
52static int com20020_reset(struct net_device *dev, int really_reset);
53static void com20020_openclose(struct net_device *dev, bool open);
54static void com20020_copy_to_card(struct net_device *dev, int bufnum,
55				  int offset, void *buf, int count);
56static void com20020_copy_from_card(struct net_device *dev, int bufnum,
57				    int offset, void *buf, int count);
58static void com20020_set_mc_list(struct net_device *dev);
59
60
61static void com20020_copy_from_card(struct net_device *dev, int bufnum,
62				    int offset, void *buf, int count)
63{
64	int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset;
65
66	/* set up the address register */
67	outb((ofs >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
68	outb(ofs & 0xff, _ADDR_LO);
69
70	/* copy the data */
71	TIME("insb", count, insb(_MEMDATA, buf, count));
72}
73
74
75static void com20020_copy_to_card(struct net_device *dev, int bufnum,
76				  int offset, void *buf, int count)
77{
78	int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset;
79
80	/* set up the address register */
81	outb((ofs >> 8) | AUTOINCflag, _ADDR_HI);
82	outb(ofs & 0xff, _ADDR_LO);
83
84	/* copy the data */
85	TIME("outsb", count, outsb(_MEMDATA, buf, count));
86}
87
88
89/* Reset the card and check some basic stuff during the detection stage. */
90int __devinit com20020_check(struct net_device *dev)
91{
92	int ioaddr = dev->base_addr, status;
93	struct arcnet_local *lp = dev->priv;
94
95	ARCRESET0;
96	mdelay(RESETtime);
97
98	lp->setup = lp->clockm ? 0 : (lp->clockp << 1);
99	lp->setup2 = (lp->clockm << 4) | 8;
100
101	SET_SUBADR(SUB_SETUP1);
102	outb(lp->setup, _XREG);
103
104	if (lp->card_flags & ARC_CAN_10MBIT)
105	{
106		SET_SUBADR(SUB_SETUP2);
107		outb(lp->setup2, _XREG);
108
109		/* must now write the magic "restart operation" command */
110		mdelay(1);
111		outb(0x18, _COMMAND);
112	}
113
114	lp->config = 0x21 | (lp->timeout << 3) | (lp->backplane << 2);
115	/* set node ID to 0x42 (but transmitter is disabled, so it's okay) */
116	SETCONF;
117	outb(0x42, ioaddr + 7);
118
119	status = ASTATUS();
120
121	if ((status & 0x99) != (NORXflag | TXFREEflag | RESETflag)) {
122		BUGMSG(D_NORMAL, "status invalid (%Xh).\n", status);
123		return -ENODEV;
124	}
125	BUGMSG(D_INIT_REASONS, "status after reset: %X\n", status);
126
127	/* Enable TX */
128	outb(0x39, _CONFIG);
129	outb(inb(ioaddr + 8), ioaddr + 7);
130
131	ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
132
133	status = ASTATUS();
134	BUGMSG(D_INIT_REASONS, "status after reset acknowledged: %X\n",
135	       status);
136
137	/* Read first location of memory */
138	outb(0 | RDDATAflag | AUTOINCflag, _ADDR_HI);
139	outb(0, _ADDR_LO);
140
141	if ((status = inb(_MEMDATA)) != TESTvalue) {
142		BUGMSG(D_NORMAL, "Signature byte not found (%02Xh != D1h).\n",
143		       status);
144		return -ENODEV;
145	}
146	return 0;
147}
148
149/* Set up the struct net_device associated with this card.  Called after
150 * probing succeeds.
151 */
152int __devinit com20020_found(struct net_device *dev, int shared)
153{
154	struct arcnet_local *lp;
155	int ioaddr = dev->base_addr;
156
157	/* Initialize the rest of the device structure. */
158
159	lp = (struct arcnet_local *) dev->priv;
160
161	lp->hw.command = com20020_command;
162	lp->hw.status = com20020_status;
163	lp->hw.intmask = com20020_setmask;
164	lp->hw.reset = com20020_reset;
165	lp->hw.open_close = com20020_openclose;
166	lp->hw.copy_to_card = com20020_copy_to_card;
167	lp->hw.copy_from_card = com20020_copy_from_card;
168
169	dev->set_multicast_list = com20020_set_mc_list;
170
171	/* Fill in the fields of the device structure with generic
172	 * values.
173	 */
174	arcdev_setup(dev);
175
176	if (!dev->dev_addr[0])
177		dev->dev_addr[0] = inb(ioaddr + 8);
178
179	SET_SUBADR(SUB_SETUP1);
180	outb(lp->setup, _XREG);
181
182	if (lp->card_flags & ARC_CAN_10MBIT)
183	{
184		SET_SUBADR(SUB_SETUP2);
185		outb(lp->setup2, _XREG);
186
187		/* must now write the magic "restart operation" command */
188		mdelay(1);
189		outb(0x18, _COMMAND);
190	}
191
192
193	lp->config = 0x20 | (lp->timeout << 3) | (lp->backplane << 2) | 1;
194	/* Default 0x38 + register: Node ID */
195	SETCONF;
196	outb(dev->dev_addr[0], _XREG);
197
198	/* reserve the irq */
199	if (request_irq(dev->irq, &arcnet_interrupt, shared,
200			"arcnet (COM20020)", dev)) {
201		BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
202		return -ENODEV;
203	}
204	/* reserve the I/O region */
205	if (request_region(ioaddr, ARCNET_TOTAL_SIZE, "arcnet (COM20020)")) {
206		free_irq(dev->irq, dev);
207		return -EBUSY;
208	}
209	dev->base_addr = ioaddr;
210
211	BUGMSG(D_NORMAL, "%s: station %02Xh found at %03lXh, IRQ %d.\n",
212	       lp->card_name, dev->dev_addr[0], dev->base_addr, dev->irq);
213
214	if (lp->backplane)
215		BUGMSG(D_NORMAL, "Using backplane mode.\n");
216
217	if (lp->timeout != 3)
218		BUGMSG(D_NORMAL, "Using extended timeout value of %d.\n", lp->timeout);
219
220	BUGMSG(D_NORMAL, "Using CKP %d - data rate %s.\n",
221	       lp->setup >> 1,
222	       clockrates[3 - ((lp->setup2 & 0xF0) >> 4) + ((lp->setup & 0x0F) >> 1)]);
223
224	if (!dev->init && register_netdev(dev)) {
225		free_irq(dev->irq, dev);
226		release_region(ioaddr, ARCNET_TOTAL_SIZE);
227		return -EIO;
228	}
229	return 0;
230}
231
232
233/*
234 * Do a hardware reset on the card, and set up necessary registers.
235 *
236 * This should be called as little as possible, because it disrupts the
237 * token on the network (causes a RECON) and requires a significant delay.
238 *
239 * However, it does make sure the card is in a defined state.
240 */
241static int com20020_reset(struct net_device *dev, int really_reset)
242{
243	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
244	short ioaddr = dev->base_addr;
245	u_char inbyte;
246
247	BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n",
248	       dev->name, ASTATUS());
249
250	lp->config = TXENcfg | (lp->timeout << 3) | (lp->backplane << 2);
251	/* power-up defaults */
252	SETCONF;
253
254	if (really_reset) {
255		/* reset the card */
256		ARCRESET;
257		mdelay(RESETtime * 2);	/* COM20020 seems to be slower sometimes */
258	}
259	/* clear flags & end reset */
260	ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
261
262	/* verify that the ARCnet signature byte is present */
263
264	com20020_copy_from_card(dev, 0, 0, &inbyte, 1);
265	if (inbyte != TESTvalue) {
266		BUGMSG(D_NORMAL, "reset failed: TESTvalue not present.\n");
267		return 1;
268	}
269	/* enable extended (512-byte) packets */
270	ACOMMAND(CONFIGcmd | EXTconf);
271
272	/* done!  return success. */
273	return 0;
274}
275
276
277static void com20020_setmask(struct net_device *dev, int mask)
278{
279	short ioaddr = dev->base_addr;
280	AINTMASK(mask);
281}
282
283
284static void com20020_command(struct net_device *dev, int cmd)
285{
286	short ioaddr = dev->base_addr;
287	ACOMMAND(cmd);
288}
289
290
291static int com20020_status(struct net_device *dev)
292{
293	short ioaddr = dev->base_addr;
294	return ASTATUS();
295}
296
297
298static void com20020_openclose(struct net_device *dev, bool open)
299{
300	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
301	int ioaddr = dev->base_addr;
302
303	if (open)
304		MOD_INC_USE_COUNT;
305	else {
306		/* disable transmitter */
307		lp->config &= ~TXENcfg;
308		SETCONF;
309		MOD_DEC_USE_COUNT;
310	}
311	lp->hw.open_close_ll(dev, open);
312}
313
314
315static void com20020_set_mc_list(struct net_device *dev)
316{
317	struct arcnet_local *lp = dev->priv;
318	int ioaddr = dev->base_addr;
319
320	if ((dev->flags & IFF_PROMISC) && (dev->flags & IFF_UP)) {	/* Enable promiscuous mode */
321		if (!(lp->setup & PROMISCset))
322			BUGMSG(D_NORMAL, "Setting promiscuous flag...\n");
323		SET_SUBADR(SUB_SETUP1);
324		lp->setup |= PROMISCset;
325		outb(lp->setup, _XREG);
326	} else
327		/* Disable promiscuous mode, use normal mode */
328	{
329		if ((lp->setup & PROMISCset))
330			BUGMSG(D_NORMAL, "Resetting promiscuous flag...\n");
331		SET_SUBADR(SUB_SETUP1);
332		lp->setup &= ~PROMISCset;
333		outb(lp->setup, _XREG);
334	}
335}
336
337void __devexit com20020_remove(struct net_device *dev)
338{
339	unregister_netdev(dev);
340	free_irq(dev->irq, dev);
341	release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
342	kfree(dev->priv);
343	kfree(dev);
344}
345
346#ifdef MODULE
347
348EXPORT_SYMBOL(com20020_check);
349EXPORT_SYMBOL(com20020_found);
350EXPORT_SYMBOL(com20020_remove);
351
352MODULE_LICENSE("GPL");
353
354int init_module(void)
355{
356	BUGLVL(D_NORMAL) printk(VERSION);
357	return 0;
358}
359
360void cleanup_module(void)
361{
362}
363
364#endif				/* MODULE */
365