blob: 9e779a53035de4d8fc14b5761c97e416d2dec555 [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 */
Joe Perches05a24b22015-05-05 10:05:56 -070028
29#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h>
36#include <linux/netdevice.h>
37#include <linux/bootmem.h>
38#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000039#include <linux/interrupt.h>
Joe Perches5e7ef912015-05-05 10:05:51 -070040#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/arcdevice.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Internal function declarations */
44
45static int com90io_found(struct net_device *dev);
46static void com90io_command(struct net_device *dev, int command);
47static int com90io_status(struct net_device *dev);
48static void com90io_setmask(struct net_device *dev, int mask);
49static int com90io_reset(struct net_device *dev, int really_reset);
50static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
51 void *buf, int count);
Joe Perchesd6d7d3e2015-05-05 10:06:02 -070052static void com90io_copy_from_card(struct net_device *dev, int bufnum,
53 int offset, void *buf, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* Handy defines for ARCnet specific stuff */
56
57/* The number of low I/O ports used by the card. */
58#define ARCNET_TOTAL_SIZE 16
59
60/* COM 9026 controller chip --> ARCnet register addresses */
Joe Perchescb334642015-05-05 10:05:47 -070061#define _INTMASK (ioaddr + 0) /* writable */
62#define _STATUS (ioaddr + 0) /* readable */
63#define _COMMAND (ioaddr + 1) /* writable, returns random vals on read (?) */
64#define _RESET (ioaddr + 8) /* software reset (on read) */
65#define _MEMDATA (ioaddr + 12) /* Data port for IO-mapped memory */
66#define _ADDR_HI (ioaddr + 15) /* Control registers for said */
67#define _ADDR_LO (ioaddr + 14)
68#define _CONFIG (ioaddr + 2) /* Configuration register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#undef ASTATUS
71#undef ACOMMAND
72#undef AINTMASK
73
74#define ASTATUS() inb(_STATUS)
Joe Perchescb334642015-05-05 10:05:47 -070075#define ACOMMAND(cmd) outb((cmd), _COMMAND)
76#define AINTMASK(msk) outb((msk), _INTMASK)
77#define SETCONF() outb((lp->config), _CONFIG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/****************************************************************************
80 * *
81 * IO-mapped operation routines *
82 * *
83 ****************************************************************************/
84
85#undef ONE_AT_A_TIME_TX
86#undef ONE_AT_A_TIME_RX
87
88static u_char get_buffer_byte(struct net_device *dev, unsigned offset)
89{
90 int ioaddr = dev->base_addr;
91
92 outb(offset >> 8, _ADDR_HI);
93 outb(offset & 0xff, _ADDR_LO);
94
95 return inb(_MEMDATA);
96}
97
98#ifdef ONE_AT_A_TIME_TX
Joe Perchesd6d7d3e2015-05-05 10:06:02 -070099static void put_buffer_byte(struct net_device *dev, unsigned offset,
100 u_char datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 int ioaddr = dev->base_addr;
103
104 outb(offset >> 8, _ADDR_HI);
105 outb(offset & 0xff, _ADDR_LO);
106
107 outb(datum, _MEMDATA);
108}
109
110#endif
111
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700112static void get_whole_buffer(struct net_device *dev, unsigned offset,
113 unsigned length, char *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 int ioaddr = dev->base_addr;
116
117 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
118 outb(offset & 0xff, _ADDR_LO);
119
120 while (length--)
121#ifdef ONE_AT_A_TIME_RX
122 *(dest++) = get_buffer_byte(dev, offset++);
123#else
124 *(dest++) = inb(_MEMDATA);
125#endif
126}
127
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700128static void put_whole_buffer(struct net_device *dev, unsigned offset,
129 unsigned length, char *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 int ioaddr = dev->base_addr;
132
133 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
134 outb(offset & 0xff, _ADDR_LO);
135
136 while (length--)
137#ifdef ONE_AT_A_TIME_TX
138 put_buffer_byte(dev, offset++, *(dest++));
139#else
140 outb(*(dest++), _MEMDATA);
141#endif
142}
143
Joe Perchesf2f0a162015-05-05 10:05:52 -0700144/* We cannot probe for an IO mapped card either, although we can check that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * it's where we were told it was, and even autoirq
146 */
147static int __init com90io_probe(struct net_device *dev)
148{
149 int ioaddr = dev->base_addr, status;
150 unsigned long airqmask;
151
Joe Perches72aeea42015-05-05 10:05:54 -0700152 if (BUGLVL(D_NORMAL)) {
Joe Perches05a24b22015-05-05 10:05:56 -0700153 pr_info("%s\n", "COM90xx IO-mapped mode support (by David Woodhouse et el.)");
154 pr_info("E-mail me if you actually test this driver, please!\n");
Joe Perches72aeea42015-05-05 10:05:54 -0700155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (!ioaddr) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700158 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 -0700159 return -ENODEV;
160 }
161 if (!request_region(ioaddr, ARCNET_TOTAL_SIZE, "com90io probe")) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700162 arc_printk(D_INIT_REASONS, dev, "IO request_region %x-%x failed\n",
163 ioaddr, ioaddr + ARCNET_TOTAL_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENXIO;
165 }
166 if (ASTATUS() == 0xFF) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700167 arc_printk(D_INIT_REASONS, dev, "IO address %x empty\n",
168 ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto err_out;
170 }
171 inb(_RESET);
172 mdelay(RESETtime);
173
174 status = ASTATUS();
175
176 if ((status & 0x9D) != (NORXflag | RECONflag | TXFREEflag | RESETflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700177 arc_printk(D_INIT_REASONS, dev, "Status invalid (%Xh)\n",
178 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto err_out;
180 }
Joe Perchesa34c0932015-05-05 10:05:55 -0700181 arc_printk(D_INIT_REASONS, dev, "Status after reset: %X\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
184
Joe Perchesa34c0932015-05-05 10:05:55 -0700185 arc_printk(D_INIT_REASONS, dev, "Status after reset acknowledged: %X\n",
186 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 status = ASTATUS();
189
190 if (status & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700191 arc_printk(D_INIT_REASONS, dev, "Eternal reset (status=%Xh)\n",
192 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto err_out;
194 }
195 outb((0x16 | IOMAPflag) & ~ENABLE16flag, _CONFIG);
196
197 /* Read first loc'n of memory */
198
199 outb(AUTOINCflag, _ADDR_HI);
200 outb(0, _ADDR_LO);
201
Joe Perches97464ed2015-05-05 10:05:59 -0700202 status = inb(_MEMDATA);
203 if (status != 0xd1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700204 arc_printk(D_INIT_REASONS, dev, "Signature byte not found (%Xh instead).\n",
205 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto err_out;
207 }
208 if (!dev->irq) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700209 /* if we do this, we're sure to get an IRQ since the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * card has just reset and the NORXflag is on until
211 * we tell it to start receiving.
212 */
213
214 airqmask = probe_irq_on();
215 outb(NORXflag, _INTMASK);
216 udelay(1);
217 outb(0, _INTMASK);
218 dev->irq = probe_irq_off(airqmask);
219
Dan Carpenter0a6efc72010-07-17 07:21:28 +0000220 if ((int)dev->irq <= 0) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700221 arc_printk(D_INIT_REASONS, dev, "Autoprobe IRQ failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 goto err_out;
223 }
224 }
225 release_region(ioaddr, ARCNET_TOTAL_SIZE); /* end of probing */
226 return com90io_found(dev);
227
228err_out:
229 release_region(ioaddr, ARCNET_TOTAL_SIZE);
230 return -ENODEV;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/* Set up the struct net_device associated with this card. Called after
234 * probing succeeds.
235 */
236static int __init com90io_found(struct net_device *dev)
237{
238 struct arcnet_local *lp;
239 int ioaddr = dev->base_addr;
240 int err;
241
242 /* Reserve the irq */
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700243 if (request_irq(dev->irq, arcnet_interrupt, 0,
244 "arcnet (COM90xx-IO)", dev)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700245 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -ENODEV;
247 }
Peter Osterlundfb911ee2005-09-13 01:25:15 -0700248 /* Reserve the I/O region */
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700249 if (!request_region(dev->base_addr, ARCNET_TOTAL_SIZE,
250 "arcnet (COM90xx-IO)")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 free_irq(dev->irq, dev);
252 return -EBUSY;
253 }
254
Wang Chen454d7c92008-11-12 23:37:49 -0800255 lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 lp->card_name = "COM90xx I/O";
257 lp->hw.command = com90io_command;
258 lp->hw.status = com90io_status;
259 lp->hw.intmask = com90io_setmask;
260 lp->hw.reset = com90io_reset;
261 lp->hw.owner = THIS_MODULE;
262 lp->hw.copy_to_card = com90io_copy_to_card;
263 lp->hw.copy_from_card = com90io_copy_from_card;
264
265 lp->config = (0x16 | IOMAPflag) & ~ENABLE16flag;
266 SETCONF();
267
268 /* get and check the station ID from offset 1 in shmem */
269
270 dev->dev_addr[0] = get_buffer_byte(dev, 1);
271
272 err = register_netdev(dev);
273 if (err) {
274 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
275 free_irq(dev->irq, dev);
276 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
277 return err;
278 }
279
Joe Perchesa34c0932015-05-05 10:05:55 -0700280 arc_printk(D_NORMAL, dev, "COM90IO: station %02Xh found at %03lXh, IRQ %d.\n",
281 dev->dev_addr[0], dev->base_addr, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 return 0;
284}
285
Joe Perchesf2f0a162015-05-05 10:05:52 -0700286/* Do a hardware reset on the card, and set up necessary registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
288 * This should be called as little as possible, because it disrupts the
289 * token on the network (causes a RECON) and requires a significant delay.
290 *
291 * However, it does make sure the card is in a defined state.
292 */
293static int com90io_reset(struct net_device *dev, int really_reset)
294{
Wang Chen454d7c92008-11-12 23:37:49 -0800295 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 short ioaddr = dev->base_addr;
297
Joe Perchesa34c0932015-05-05 10:05:55 -0700298 arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
299 dev->name, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (really_reset) {
302 /* reset the card */
303 inb(_RESET);
304 mdelay(RESETtime);
305 }
306 /* Set the thing to IO-mapped, 8-bit mode */
307 lp->config = (0x1C | IOMAPflag) & ~ENABLE16flag;
308 SETCONF();
309
310 ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */
311 ACOMMAND(CFLAGScmd | CONFIGclear);
312
313 /* verify that the ARCnet signature byte is present */
314 if (get_buffer_byte(dev, 0) != TESTvalue) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700315 arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 1;
317 }
318 /* enable extended (512-byte) packets */
319 ACOMMAND(CONFIGcmd | EXTconf);
320
321 /* done! return success. */
322 return 0;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static void com90io_command(struct net_device *dev, int cmd)
326{
327 short ioaddr = dev->base_addr;
328
329 ACOMMAND(cmd);
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int com90io_status(struct net_device *dev)
333{
334 short ioaddr = dev->base_addr;
335
336 return ASTATUS();
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static void com90io_setmask(struct net_device *dev, int mask)
340{
341 short ioaddr = dev->base_addr;
342
343 AINTMASK(mask);
344}
345
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700346static void com90io_copy_to_card(struct net_device *dev, int bufnum,
347 int offset, void *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Joe Perchesa34c0932015-05-05 10:05:55 -0700349 TIME(dev, "put_whole_buffer", count,
350 put_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700353static void com90io_copy_from_card(struct net_device *dev, int bufnum,
354 int offset, void *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Joe Perchesa34c0932015-05-05 10:05:55 -0700356 TIME(dev, "get_whole_buffer", count,
357 get_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360static int io; /* use the insmod io= irq= shmem= options */
361static int irq;
362static char device[9]; /* use eg. device=arc1 to change name */
363
364module_param(io, int, 0);
365module_param(irq, int, 0);
366module_param_string(device, device, sizeof(device), 0);
367MODULE_LICENSE("GPL");
368
369#ifndef MODULE
370static int __init com90io_setup(char *s)
371{
372 int ints[4];
Joe Perches01a1d5a2015-05-05 10:05:48 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 s = get_options(s, 4, ints);
375 if (!ints[0])
376 return 0;
377 switch (ints[0]) {
378 default: /* ERROR */
Joe Perches05a24b22015-05-05 10:05:56 -0700379 pr_err("Too many arguments\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case 2: /* IRQ */
381 irq = ints[2];
382 case 1: /* IO address */
383 io = ints[1];
384 }
385 if (*s)
386 snprintf(device, sizeof(device), "%s", s);
387 return 1;
388}
389__setup("com90io=", com90io_setup);
390#endif
391
392static struct net_device *my_dev;
393
394static int __init com90io_init(void)
395{
396 struct net_device *dev;
397 int err;
398
399 dev = alloc_arcdev(device);
400 if (!dev)
401 return -ENOMEM;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dev->base_addr = io;
404 dev->irq = irq;
405 if (dev->irq == 2)
406 dev->irq = 9;
407
408 err = com90io_probe(dev);
409
410 if (err) {
411 free_netdev(dev);
412 return err;
413 }
414
415 my_dev = dev;
416 return 0;
417}
418
419static void __exit com90io_exit(void)
420{
421 struct net_device *dev = my_dev;
422 int ioaddr = dev->base_addr;
423
424 unregister_netdev(dev);
425
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700426 /* In case the old driver is loaded later,
427 * set the thing back to MMAP mode
428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
430
431 free_irq(dev->irq, dev);
432 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
433 free_netdev(dev);
434}
435
436module_init(com90io_init)
437module_exit(com90io_exit)