blob: ec2db852786254be3d960d7a03c50802326b534f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - COM90xx chipset (IO-mapped buffers)
Joe Perchescb334642015-05-05 10:05:47 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written 1997 by David Woodhouse.
5 * Written 1994-1999 by Avery Pennarun.
6 * Written 1999-2000 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/kernel.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33#include <linux/netdevice.h>
34#include <linux/bootmem.h>
35#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000036#include <linux/interrupt.h>
Joe Perches5e7ef912015-05-05 10:05:51 -070037#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/arcdevice.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define VERSION "arcnet: COM90xx IO-mapped mode support (by David Woodhouse et el.)\n"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* Internal function declarations */
43
44static int com90io_found(struct net_device *dev);
45static void com90io_command(struct net_device *dev, int command);
46static int com90io_status(struct net_device *dev);
47static void com90io_setmask(struct net_device *dev, int mask);
48static int com90io_reset(struct net_device *dev, int really_reset);
49static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
50 void *buf, int count);
51static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
52 void *buf, int count);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Handy defines for ARCnet specific stuff */
55
56/* The number of low I/O ports used by the card. */
57#define ARCNET_TOTAL_SIZE 16
58
59/* COM 9026 controller chip --> ARCnet register addresses */
Joe Perchescb334642015-05-05 10:05:47 -070060#define _INTMASK (ioaddr + 0) /* writable */
61#define _STATUS (ioaddr + 0) /* readable */
62#define _COMMAND (ioaddr + 1) /* writable, returns random vals on read (?) */
63#define _RESET (ioaddr + 8) /* software reset (on read) */
64#define _MEMDATA (ioaddr + 12) /* Data port for IO-mapped memory */
65#define _ADDR_HI (ioaddr + 15) /* Control registers for said */
66#define _ADDR_LO (ioaddr + 14)
67#define _CONFIG (ioaddr + 2) /* Configuration register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#undef ASTATUS
70#undef ACOMMAND
71#undef AINTMASK
72
73#define ASTATUS() inb(_STATUS)
Joe Perchescb334642015-05-05 10:05:47 -070074#define ACOMMAND(cmd) outb((cmd), _COMMAND)
75#define AINTMASK(msk) outb((msk), _INTMASK)
76#define SETCONF() outb((lp->config), _CONFIG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/****************************************************************************
79 * *
80 * IO-mapped operation routines *
81 * *
82 ****************************************************************************/
83
84#undef ONE_AT_A_TIME_TX
85#undef ONE_AT_A_TIME_RX
86
87static u_char get_buffer_byte(struct net_device *dev, unsigned offset)
88{
89 int ioaddr = dev->base_addr;
90
91 outb(offset >> 8, _ADDR_HI);
92 outb(offset & 0xff, _ADDR_LO);
93
94 return inb(_MEMDATA);
95}
96
97#ifdef ONE_AT_A_TIME_TX
98static void put_buffer_byte(struct net_device *dev, unsigned offset, u_char datum)
99{
100 int ioaddr = dev->base_addr;
101
102 outb(offset >> 8, _ADDR_HI);
103 outb(offset & 0xff, _ADDR_LO);
104
105 outb(datum, _MEMDATA);
106}
107
108#endif
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static void get_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
111{
112 int ioaddr = dev->base_addr;
113
114 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
115 outb(offset & 0xff, _ADDR_LO);
116
117 while (length--)
118#ifdef ONE_AT_A_TIME_RX
119 *(dest++) = get_buffer_byte(dev, offset++);
120#else
121 *(dest++) = inb(_MEMDATA);
122#endif
123}
124
125static void put_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
126{
127 int ioaddr = dev->base_addr;
128
129 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
130 outb(offset & 0xff, _ADDR_LO);
131
132 while (length--)
133#ifdef ONE_AT_A_TIME_TX
134 put_buffer_byte(dev, offset++, *(dest++));
135#else
136 outb(*(dest++), _MEMDATA);
137#endif
138}
139
Joe Perchesf2f0a162015-05-05 10:05:52 -0700140/* We cannot probe for an IO mapped card either, although we can check that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * it's where we were told it was, and even autoirq
142 */
143static int __init com90io_probe(struct net_device *dev)
144{
145 int ioaddr = dev->base_addr, status;
146 unsigned long airqmask;
147
Joe Perches72aeea42015-05-05 10:05:54 -0700148 if (BUGLVL(D_NORMAL)) {
149 printk(VERSION);
150 printk("E-mail me if you actually test this driver, please!\n");
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (!ioaddr) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700154 arc_printk(D_NORMAL, dev, "No autoprobe for IO mapped cards; you must specify the base address!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -ENODEV;
156 }
157 if (!request_region(ioaddr, ARCNET_TOTAL_SIZE, "com90io probe")) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700158 arc_printk(D_INIT_REASONS, dev, "IO request_region %x-%x failed\n",
159 ioaddr, ioaddr + ARCNET_TOTAL_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -ENXIO;
161 }
162 if (ASTATUS() == 0xFF) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700163 arc_printk(D_INIT_REASONS, dev, "IO address %x empty\n",
164 ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 goto err_out;
166 }
167 inb(_RESET);
168 mdelay(RESETtime);
169
170 status = ASTATUS();
171
172 if ((status & 0x9D) != (NORXflag | RECONflag | TXFREEflag | RESETflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700173 arc_printk(D_INIT_REASONS, dev, "Status invalid (%Xh)\n",
174 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto err_out;
176 }
Joe Perchesa34c0932015-05-05 10:05:55 -0700177 arc_printk(D_INIT_REASONS, dev, "Status after reset: %X\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
180
Joe Perchesa34c0932015-05-05 10:05:55 -0700181 arc_printk(D_INIT_REASONS, dev, "Status after reset acknowledged: %X\n",
182 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 status = ASTATUS();
185
186 if (status & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700187 arc_printk(D_INIT_REASONS, dev, "Eternal reset (status=%Xh)\n",
188 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto err_out;
190 }
191 outb((0x16 | IOMAPflag) & ~ENABLE16flag, _CONFIG);
192
193 /* Read first loc'n of memory */
194
195 outb(AUTOINCflag, _ADDR_HI);
196 outb(0, _ADDR_LO);
197
198 if ((status = inb(_MEMDATA)) != 0xd1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700199 arc_printk(D_INIT_REASONS, dev, "Signature byte not found (%Xh instead).\n",
200 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto err_out;
202 }
203 if (!dev->irq) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700204 /* if we do this, we're sure to get an IRQ since the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * card has just reset and the NORXflag is on until
206 * we tell it to start receiving.
207 */
208
209 airqmask = probe_irq_on();
210 outb(NORXflag, _INTMASK);
211 udelay(1);
212 outb(0, _INTMASK);
213 dev->irq = probe_irq_off(airqmask);
214
Dan Carpenter0a6efc72010-07-17 07:21:28 +0000215 if ((int)dev->irq <= 0) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700216 arc_printk(D_INIT_REASONS, dev, "Autoprobe IRQ failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto err_out;
218 }
219 }
220 release_region(ioaddr, ARCNET_TOTAL_SIZE); /* end of probing */
221 return com90io_found(dev);
222
223err_out:
224 release_region(ioaddr, ARCNET_TOTAL_SIZE);
225 return -ENODEV;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* Set up the struct net_device associated with this card. Called after
229 * probing succeeds.
230 */
231static int __init com90io_found(struct net_device *dev)
232{
233 struct arcnet_local *lp;
234 int ioaddr = dev->base_addr;
235 int err;
236
237 /* Reserve the irq */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800238 if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (COM90xx-IO)", dev)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700239 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -ENODEV;
241 }
Peter Osterlundfb911ee2005-09-13 01:25:15 -0700242 /* Reserve the I/O region */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!request_region(dev->base_addr, ARCNET_TOTAL_SIZE, "arcnet (COM90xx-IO)")) {
244 free_irq(dev->irq, dev);
245 return -EBUSY;
246 }
247
Wang Chen454d7c92008-11-12 23:37:49 -0800248 lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 lp->card_name = "COM90xx I/O";
250 lp->hw.command = com90io_command;
251 lp->hw.status = com90io_status;
252 lp->hw.intmask = com90io_setmask;
253 lp->hw.reset = com90io_reset;
254 lp->hw.owner = THIS_MODULE;
255 lp->hw.copy_to_card = com90io_copy_to_card;
256 lp->hw.copy_from_card = com90io_copy_from_card;
257
258 lp->config = (0x16 | IOMAPflag) & ~ENABLE16flag;
259 SETCONF();
260
261 /* get and check the station ID from offset 1 in shmem */
262
263 dev->dev_addr[0] = get_buffer_byte(dev, 1);
264
265 err = register_netdev(dev);
266 if (err) {
267 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
268 free_irq(dev->irq, dev);
269 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
270 return err;
271 }
272
Joe Perchesa34c0932015-05-05 10:05:55 -0700273 arc_printk(D_NORMAL, dev, "COM90IO: station %02Xh found at %03lXh, IRQ %d.\n",
274 dev->dev_addr[0], dev->base_addr, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return 0;
277}
278
Joe Perchesf2f0a162015-05-05 10:05:52 -0700279/* Do a hardware reset on the card, and set up necessary registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * This should be called as little as possible, because it disrupts the
282 * token on the network (causes a RECON) and requires a significant delay.
283 *
284 * However, it does make sure the card is in a defined state.
285 */
286static int com90io_reset(struct net_device *dev, int really_reset)
287{
Wang Chen454d7c92008-11-12 23:37:49 -0800288 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 short ioaddr = dev->base_addr;
290
Joe Perchesa34c0932015-05-05 10:05:55 -0700291 arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
292 dev->name, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (really_reset) {
295 /* reset the card */
296 inb(_RESET);
297 mdelay(RESETtime);
298 }
299 /* Set the thing to IO-mapped, 8-bit mode */
300 lp->config = (0x1C | IOMAPflag) & ~ENABLE16flag;
301 SETCONF();
302
303 ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */
304 ACOMMAND(CFLAGScmd | CONFIGclear);
305
306 /* verify that the ARCnet signature byte is present */
307 if (get_buffer_byte(dev, 0) != TESTvalue) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700308 arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 1;
310 }
311 /* enable extended (512-byte) packets */
312 ACOMMAND(CONFIGcmd | EXTconf);
313
314 /* done! return success. */
315 return 0;
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static void com90io_command(struct net_device *dev, int cmd)
319{
320 short ioaddr = dev->base_addr;
321
322 ACOMMAND(cmd);
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static int com90io_status(struct net_device *dev)
326{
327 short ioaddr = dev->base_addr;
328
329 return ASTATUS();
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static void com90io_setmask(struct net_device *dev, int mask)
333{
334 short ioaddr = dev->base_addr;
335
336 AINTMASK(mask);
337}
338
339static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
340 void *buf, int count)
341{
Joe Perchesa34c0932015-05-05 10:05:55 -0700342 TIME(dev, "put_whole_buffer", count,
343 put_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
347 void *buf, int count)
348{
Joe Perchesa34c0932015-05-05 10:05:55 -0700349 TIME(dev, "get_whole_buffer", count,
350 get_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353static int io; /* use the insmod io= irq= shmem= options */
354static int irq;
355static char device[9]; /* use eg. device=arc1 to change name */
356
357module_param(io, int, 0);
358module_param(irq, int, 0);
359module_param_string(device, device, sizeof(device), 0);
360MODULE_LICENSE("GPL");
361
362#ifndef MODULE
363static int __init com90io_setup(char *s)
364{
365 int ints[4];
Joe Perches01a1d5a2015-05-05 10:05:48 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 s = get_options(s, 4, ints);
368 if (!ints[0])
369 return 0;
370 switch (ints[0]) {
371 default: /* ERROR */
372 printk("com90io: Too many arguments.\n");
373 case 2: /* IRQ */
374 irq = ints[2];
375 case 1: /* IO address */
376 io = ints[1];
377 }
378 if (*s)
379 snprintf(device, sizeof(device), "%s", s);
380 return 1;
381}
382__setup("com90io=", com90io_setup);
383#endif
384
385static struct net_device *my_dev;
386
387static int __init com90io_init(void)
388{
389 struct net_device *dev;
390 int err;
391
392 dev = alloc_arcdev(device);
393 if (!dev)
394 return -ENOMEM;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 dev->base_addr = io;
397 dev->irq = irq;
398 if (dev->irq == 2)
399 dev->irq = 9;
400
401 err = com90io_probe(dev);
402
403 if (err) {
404 free_netdev(dev);
405 return err;
406 }
407
408 my_dev = dev;
409 return 0;
410}
411
412static void __exit com90io_exit(void)
413{
414 struct net_device *dev = my_dev;
415 int ioaddr = dev->base_addr;
416
417 unregister_netdev(dev);
418
419 /* Set the thing back to MMAP mode, in case the old driver is loaded later */
420 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
421
422 free_irq(dev->irq, dev);
423 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
424 free_netdev(dev);
425}
426
427module_init(com90io_init)
428module_exit(com90io_exit)