blob: ce15d191104835d25c4b2b23706b8d6fcda7e8a4 [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>
Joe Perches26c6d282015-05-05 10:06:03 -070041
42#include "arcdevice.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* Internal function declarations */
45
46static int com90io_found(struct net_device *dev);
47static void com90io_command(struct net_device *dev, int command);
48static int com90io_status(struct net_device *dev);
49static void com90io_setmask(struct net_device *dev, int mask);
50static int com90io_reset(struct net_device *dev, int really_reset);
51static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
52 void *buf, int count);
Joe Perchesd6d7d3e2015-05-05 10:06:02 -070053static void com90io_copy_from_card(struct net_device *dev, int bufnum,
54 int offset, void *buf, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* Handy defines for ARCnet specific stuff */
57
58/* The number of low I/O ports used by the card. */
59#define ARCNET_TOTAL_SIZE 16
60
61/* COM 9026 controller chip --> ARCnet register addresses */
Joe Perchescb334642015-05-05 10:05:47 -070062#define _INTMASK (ioaddr + 0) /* writable */
63#define _STATUS (ioaddr + 0) /* readable */
64#define _COMMAND (ioaddr + 1) /* writable, returns random vals on read (?) */
65#define _RESET (ioaddr + 8) /* software reset (on read) */
66#define _MEMDATA (ioaddr + 12) /* Data port for IO-mapped memory */
67#define _ADDR_HI (ioaddr + 15) /* Control registers for said */
68#define _ADDR_LO (ioaddr + 14)
69#define _CONFIG (ioaddr + 2) /* Configuration register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#undef ASTATUS
72#undef ACOMMAND
73#undef AINTMASK
74
75#define ASTATUS() inb(_STATUS)
Joe Perchescb334642015-05-05 10:05:47 -070076#define ACOMMAND(cmd) outb((cmd), _COMMAND)
77#define AINTMASK(msk) outb((msk), _INTMASK)
78#define SETCONF() outb((lp->config), _CONFIG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/****************************************************************************
81 * *
82 * IO-mapped operation routines *
83 * *
84 ****************************************************************************/
85
86#undef ONE_AT_A_TIME_TX
87#undef ONE_AT_A_TIME_RX
88
89static u_char get_buffer_byte(struct net_device *dev, unsigned offset)
90{
91 int ioaddr = dev->base_addr;
92
93 outb(offset >> 8, _ADDR_HI);
94 outb(offset & 0xff, _ADDR_LO);
95
96 return inb(_MEMDATA);
97}
98
99#ifdef ONE_AT_A_TIME_TX
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700100static void put_buffer_byte(struct net_device *dev, unsigned offset,
101 u_char datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 int ioaddr = dev->base_addr;
104
105 outb(offset >> 8, _ADDR_HI);
106 outb(offset & 0xff, _ADDR_LO);
107
108 outb(datum, _MEMDATA);
109}
110
111#endif
112
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700113static void get_whole_buffer(struct net_device *dev, unsigned offset,
114 unsigned length, char *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 int ioaddr = dev->base_addr;
117
118 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
119 outb(offset & 0xff, _ADDR_LO);
120
121 while (length--)
122#ifdef ONE_AT_A_TIME_RX
123 *(dest++) = get_buffer_byte(dev, offset++);
124#else
125 *(dest++) = inb(_MEMDATA);
126#endif
127}
128
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700129static void put_whole_buffer(struct net_device *dev, unsigned offset,
130 unsigned length, char *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 int ioaddr = dev->base_addr;
133
134 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
135 outb(offset & 0xff, _ADDR_LO);
136
137 while (length--)
138#ifdef ONE_AT_A_TIME_TX
139 put_buffer_byte(dev, offset++, *(dest++));
140#else
141 outb(*(dest++), _MEMDATA);
142#endif
143}
144
Joe Perchesf2f0a162015-05-05 10:05:52 -0700145/* We cannot probe for an IO mapped card either, although we can check that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * it's where we were told it was, and even autoirq
147 */
148static int __init com90io_probe(struct net_device *dev)
149{
150 int ioaddr = dev->base_addr, status;
151 unsigned long airqmask;
152
Joe Perches72aeea42015-05-05 10:05:54 -0700153 if (BUGLVL(D_NORMAL)) {
Joe Perches05a24b22015-05-05 10:05:56 -0700154 pr_info("%s\n", "COM90xx IO-mapped mode support (by David Woodhouse et el.)");
155 pr_info("E-mail me if you actually test this driver, please!\n");
Joe Perches72aeea42015-05-05 10:05:54 -0700156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (!ioaddr) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700159 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 -0700160 return -ENODEV;
161 }
162 if (!request_region(ioaddr, ARCNET_TOTAL_SIZE, "com90io probe")) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700163 arc_printk(D_INIT_REASONS, dev, "IO request_region %x-%x failed\n",
164 ioaddr, ioaddr + ARCNET_TOTAL_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -ENXIO;
166 }
167 if (ASTATUS() == 0xFF) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700168 arc_printk(D_INIT_REASONS, dev, "IO address %x empty\n",
169 ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto err_out;
171 }
172 inb(_RESET);
173 mdelay(RESETtime);
174
175 status = ASTATUS();
176
177 if ((status & 0x9D) != (NORXflag | RECONflag | TXFREEflag | RESETflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700178 arc_printk(D_INIT_REASONS, dev, "Status invalid (%Xh)\n",
179 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto err_out;
181 }
Joe Perchesa34c0932015-05-05 10:05:55 -0700182 arc_printk(D_INIT_REASONS, dev, "Status after reset: %X\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
185
Joe Perchesa34c0932015-05-05 10:05:55 -0700186 arc_printk(D_INIT_REASONS, dev, "Status after reset acknowledged: %X\n",
187 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 status = ASTATUS();
190
191 if (status & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700192 arc_printk(D_INIT_REASONS, dev, "Eternal reset (status=%Xh)\n",
193 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto err_out;
195 }
196 outb((0x16 | IOMAPflag) & ~ENABLE16flag, _CONFIG);
197
198 /* Read first loc'n of memory */
199
200 outb(AUTOINCflag, _ADDR_HI);
201 outb(0, _ADDR_LO);
202
Joe Perches97464ed2015-05-05 10:05:59 -0700203 status = inb(_MEMDATA);
204 if (status != 0xd1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700205 arc_printk(D_INIT_REASONS, dev, "Signature byte not found (%Xh instead).\n",
206 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto err_out;
208 }
209 if (!dev->irq) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700210 /* if we do this, we're sure to get an IRQ since the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * card has just reset and the NORXflag is on until
212 * we tell it to start receiving.
213 */
214
215 airqmask = probe_irq_on();
216 outb(NORXflag, _INTMASK);
217 udelay(1);
218 outb(0, _INTMASK);
219 dev->irq = probe_irq_off(airqmask);
220
Dan Carpenter0a6efc72010-07-17 07:21:28 +0000221 if ((int)dev->irq <= 0) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700222 arc_printk(D_INIT_REASONS, dev, "Autoprobe IRQ failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 goto err_out;
224 }
225 }
226 release_region(ioaddr, ARCNET_TOTAL_SIZE); /* end of probing */
227 return com90io_found(dev);
228
229err_out:
230 release_region(ioaddr, ARCNET_TOTAL_SIZE);
231 return -ENODEV;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/* Set up the struct net_device associated with this card. Called after
235 * probing succeeds.
236 */
237static int __init com90io_found(struct net_device *dev)
238{
239 struct arcnet_local *lp;
240 int ioaddr = dev->base_addr;
241 int err;
242
243 /* Reserve the irq */
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700244 if (request_irq(dev->irq, arcnet_interrupt, 0,
245 "arcnet (COM90xx-IO)", dev)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700246 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -ENODEV;
248 }
Peter Osterlundfb911ee2005-09-13 01:25:15 -0700249 /* Reserve the I/O region */
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700250 if (!request_region(dev->base_addr, ARCNET_TOTAL_SIZE,
251 "arcnet (COM90xx-IO)")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 free_irq(dev->irq, dev);
253 return -EBUSY;
254 }
255
Wang Chen454d7c92008-11-12 23:37:49 -0800256 lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 lp->card_name = "COM90xx I/O";
258 lp->hw.command = com90io_command;
259 lp->hw.status = com90io_status;
260 lp->hw.intmask = com90io_setmask;
261 lp->hw.reset = com90io_reset;
262 lp->hw.owner = THIS_MODULE;
263 lp->hw.copy_to_card = com90io_copy_to_card;
264 lp->hw.copy_from_card = com90io_copy_from_card;
265
266 lp->config = (0x16 | IOMAPflag) & ~ENABLE16flag;
267 SETCONF();
268
269 /* get and check the station ID from offset 1 in shmem */
270
271 dev->dev_addr[0] = get_buffer_byte(dev, 1);
272
273 err = register_netdev(dev);
274 if (err) {
275 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
276 free_irq(dev->irq, dev);
277 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
278 return err;
279 }
280
Joe Perchesa34c0932015-05-05 10:05:55 -0700281 arc_printk(D_NORMAL, dev, "COM90IO: station %02Xh found at %03lXh, IRQ %d.\n",
282 dev->dev_addr[0], dev->base_addr, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 return 0;
285}
286
Joe Perchesf2f0a162015-05-05 10:05:52 -0700287/* Do a hardware reset on the card, and set up necessary registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
289 * This should be called as little as possible, because it disrupts the
290 * token on the network (causes a RECON) and requires a significant delay.
291 *
292 * However, it does make sure the card is in a defined state.
293 */
294static int com90io_reset(struct net_device *dev, int really_reset)
295{
Wang Chen454d7c92008-11-12 23:37:49 -0800296 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 short ioaddr = dev->base_addr;
298
Joe Perchesa34c0932015-05-05 10:05:55 -0700299 arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
300 dev->name, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (really_reset) {
303 /* reset the card */
304 inb(_RESET);
305 mdelay(RESETtime);
306 }
307 /* Set the thing to IO-mapped, 8-bit mode */
308 lp->config = (0x1C | IOMAPflag) & ~ENABLE16flag;
309 SETCONF();
310
311 ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */
312 ACOMMAND(CFLAGScmd | CONFIGclear);
313
314 /* verify that the ARCnet signature byte is present */
315 if (get_buffer_byte(dev, 0) != TESTvalue) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700316 arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 1;
318 }
319 /* enable extended (512-byte) packets */
320 ACOMMAND(CONFIGcmd | EXTconf);
321
322 /* done! return success. */
323 return 0;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static void com90io_command(struct net_device *dev, int cmd)
327{
328 short ioaddr = dev->base_addr;
329
330 ACOMMAND(cmd);
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static int com90io_status(struct net_device *dev)
334{
335 short ioaddr = dev->base_addr;
336
337 return ASTATUS();
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static void com90io_setmask(struct net_device *dev, int mask)
341{
342 short ioaddr = dev->base_addr;
343
344 AINTMASK(mask);
345}
346
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700347static void com90io_copy_to_card(struct net_device *dev, int bufnum,
348 int offset, void *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Joe Perchesa34c0932015-05-05 10:05:55 -0700350 TIME(dev, "put_whole_buffer", count,
351 put_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700354static void com90io_copy_from_card(struct net_device *dev, int bufnum,
355 int offset, void *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Joe Perchesa34c0932015-05-05 10:05:55 -0700357 TIME(dev, "get_whole_buffer", count,
358 get_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361static int io; /* use the insmod io= irq= shmem= options */
362static int irq;
363static char device[9]; /* use eg. device=arc1 to change name */
364
365module_param(io, int, 0);
366module_param(irq, int, 0);
367module_param_string(device, device, sizeof(device), 0);
368MODULE_LICENSE("GPL");
369
370#ifndef MODULE
371static int __init com90io_setup(char *s)
372{
373 int ints[4];
Joe Perches01a1d5a2015-05-05 10:05:48 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 s = get_options(s, 4, ints);
376 if (!ints[0])
377 return 0;
378 switch (ints[0]) {
379 default: /* ERROR */
Joe Perches05a24b22015-05-05 10:05:56 -0700380 pr_err("Too many arguments\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case 2: /* IRQ */
382 irq = ints[2];
383 case 1: /* IO address */
384 io = ints[1];
385 }
386 if (*s)
387 snprintf(device, sizeof(device), "%s", s);
388 return 1;
389}
390__setup("com90io=", com90io_setup);
391#endif
392
393static struct net_device *my_dev;
394
395static int __init com90io_init(void)
396{
397 struct net_device *dev;
398 int err;
399
400 dev = alloc_arcdev(device);
401 if (!dev)
402 return -ENOMEM;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dev->base_addr = io;
405 dev->irq = irq;
406 if (dev->irq == 2)
407 dev->irq = 9;
408
409 err = com90io_probe(dev);
410
411 if (err) {
412 free_netdev(dev);
413 return err;
414 }
415
416 my_dev = dev;
417 return 0;
418}
419
420static void __exit com90io_exit(void)
421{
422 struct net_device *dev = my_dev;
423 int ioaddr = dev->base_addr;
424
425 unregister_netdev(dev);
426
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700427 /* In case the old driver is loaded later,
428 * set the thing back to MMAP mode
429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
431
432 free_irq(dev->irq, dev);
433 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
434 free_netdev(dev);
435}
436
437module_init(com90io_init)
438module_exit(com90io_exit)