blob: 32abaa8d0dc419bf41772fd153b659c93df0d517 [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);
52static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
53 void *buf, int count);
54
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
99static void put_buffer_byte(struct net_device *dev, unsigned offset, u_char datum)
100{
101 int ioaddr = dev->base_addr;
102
103 outb(offset >> 8, _ADDR_HI);
104 outb(offset & 0xff, _ADDR_LO);
105
106 outb(datum, _MEMDATA);
107}
108
109#endif
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static void get_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
112{
113 int ioaddr = dev->base_addr;
114
115 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
116 outb(offset & 0xff, _ADDR_LO);
117
118 while (length--)
119#ifdef ONE_AT_A_TIME_RX
120 *(dest++) = get_buffer_byte(dev, offset++);
121#else
122 *(dest++) = inb(_MEMDATA);
123#endif
124}
125
126static void put_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
127{
128 int ioaddr = dev->base_addr;
129
130 outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
131 outb(offset & 0xff, _ADDR_LO);
132
133 while (length--)
134#ifdef ONE_AT_A_TIME_TX
135 put_buffer_byte(dev, offset++, *(dest++));
136#else
137 outb(*(dest++), _MEMDATA);
138#endif
139}
140
Joe Perchesf2f0a162015-05-05 10:05:52 -0700141/* We cannot probe for an IO mapped card either, although we can check that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * it's where we were told it was, and even autoirq
143 */
144static int __init com90io_probe(struct net_device *dev)
145{
146 int ioaddr = dev->base_addr, status;
147 unsigned long airqmask;
148
Joe Perches72aeea42015-05-05 10:05:54 -0700149 if (BUGLVL(D_NORMAL)) {
Joe Perches05a24b22015-05-05 10:05:56 -0700150 pr_info("%s\n", "COM90xx IO-mapped mode support (by David Woodhouse et el.)");
151 pr_info("E-mail me if you actually test this driver, please!\n");
Joe Perches72aeea42015-05-05 10:05:54 -0700152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (!ioaddr) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700155 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 -0700156 return -ENODEV;
157 }
158 if (!request_region(ioaddr, ARCNET_TOTAL_SIZE, "com90io probe")) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700159 arc_printk(D_INIT_REASONS, dev, "IO request_region %x-%x failed\n",
160 ioaddr, ioaddr + ARCNET_TOTAL_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -ENXIO;
162 }
163 if (ASTATUS() == 0xFF) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700164 arc_printk(D_INIT_REASONS, dev, "IO address %x empty\n",
165 ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto err_out;
167 }
168 inb(_RESET);
169 mdelay(RESETtime);
170
171 status = ASTATUS();
172
173 if ((status & 0x9D) != (NORXflag | RECONflag | TXFREEflag | RESETflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700174 arc_printk(D_INIT_REASONS, dev, "Status invalid (%Xh)\n",
175 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto err_out;
177 }
Joe Perchesa34c0932015-05-05 10:05:55 -0700178 arc_printk(D_INIT_REASONS, dev, "Status after reset: %X\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
181
Joe Perchesa34c0932015-05-05 10:05:55 -0700182 arc_printk(D_INIT_REASONS, dev, "Status after reset acknowledged: %X\n",
183 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 status = ASTATUS();
186
187 if (status & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700188 arc_printk(D_INIT_REASONS, dev, "Eternal reset (status=%Xh)\n",
189 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto err_out;
191 }
192 outb((0x16 | IOMAPflag) & ~ENABLE16flag, _CONFIG);
193
194 /* Read first loc'n of memory */
195
196 outb(AUTOINCflag, _ADDR_HI);
197 outb(0, _ADDR_LO);
198
199 if ((status = inb(_MEMDATA)) != 0xd1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700200 arc_printk(D_INIT_REASONS, dev, "Signature byte not found (%Xh instead).\n",
201 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto err_out;
203 }
204 if (!dev->irq) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700205 /* if we do this, we're sure to get an IRQ since the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * card has just reset and the NORXflag is on until
207 * we tell it to start receiving.
208 */
209
210 airqmask = probe_irq_on();
211 outb(NORXflag, _INTMASK);
212 udelay(1);
213 outb(0, _INTMASK);
214 dev->irq = probe_irq_off(airqmask);
215
Dan Carpenter0a6efc72010-07-17 07:21:28 +0000216 if ((int)dev->irq <= 0) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700217 arc_printk(D_INIT_REASONS, dev, "Autoprobe IRQ failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto err_out;
219 }
220 }
221 release_region(ioaddr, ARCNET_TOTAL_SIZE); /* end of probing */
222 return com90io_found(dev);
223
224err_out:
225 release_region(ioaddr, ARCNET_TOTAL_SIZE);
226 return -ENODEV;
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* Set up the struct net_device associated with this card. Called after
230 * probing succeeds.
231 */
232static int __init com90io_found(struct net_device *dev)
233{
234 struct arcnet_local *lp;
235 int ioaddr = dev->base_addr;
236 int err;
237
238 /* Reserve the irq */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800239 if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (COM90xx-IO)", dev)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700240 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -ENODEV;
242 }
Peter Osterlundfb911ee2005-09-13 01:25:15 -0700243 /* Reserve the I/O region */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!request_region(dev->base_addr, ARCNET_TOTAL_SIZE, "arcnet (COM90xx-IO)")) {
245 free_irq(dev->irq, dev);
246 return -EBUSY;
247 }
248
Wang Chen454d7c92008-11-12 23:37:49 -0800249 lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 lp->card_name = "COM90xx I/O";
251 lp->hw.command = com90io_command;
252 lp->hw.status = com90io_status;
253 lp->hw.intmask = com90io_setmask;
254 lp->hw.reset = com90io_reset;
255 lp->hw.owner = THIS_MODULE;
256 lp->hw.copy_to_card = com90io_copy_to_card;
257 lp->hw.copy_from_card = com90io_copy_from_card;
258
259 lp->config = (0x16 | IOMAPflag) & ~ENABLE16flag;
260 SETCONF();
261
262 /* get and check the station ID from offset 1 in shmem */
263
264 dev->dev_addr[0] = get_buffer_byte(dev, 1);
265
266 err = register_netdev(dev);
267 if (err) {
268 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
269 free_irq(dev->irq, dev);
270 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
271 return err;
272 }
273
Joe Perchesa34c0932015-05-05 10:05:55 -0700274 arc_printk(D_NORMAL, dev, "COM90IO: station %02Xh found at %03lXh, IRQ %d.\n",
275 dev->dev_addr[0], dev->base_addr, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 return 0;
278}
279
Joe Perchesf2f0a162015-05-05 10:05:52 -0700280/* Do a hardware reset on the card, and set up necessary registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *
282 * This should be called as little as possible, because it disrupts the
283 * token on the network (causes a RECON) and requires a significant delay.
284 *
285 * However, it does make sure the card is in a defined state.
286 */
287static int com90io_reset(struct net_device *dev, int really_reset)
288{
Wang Chen454d7c92008-11-12 23:37:49 -0800289 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 short ioaddr = dev->base_addr;
291
Joe Perchesa34c0932015-05-05 10:05:55 -0700292 arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
293 dev->name, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (really_reset) {
296 /* reset the card */
297 inb(_RESET);
298 mdelay(RESETtime);
299 }
300 /* Set the thing to IO-mapped, 8-bit mode */
301 lp->config = (0x1C | IOMAPflag) & ~ENABLE16flag;
302 SETCONF();
303
304 ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */
305 ACOMMAND(CFLAGScmd | CONFIGclear);
306
307 /* verify that the ARCnet signature byte is present */
308 if (get_buffer_byte(dev, 0) != TESTvalue) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700309 arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 1;
311 }
312 /* enable extended (512-byte) packets */
313 ACOMMAND(CONFIGcmd | EXTconf);
314
315 /* done! return success. */
316 return 0;
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static void com90io_command(struct net_device *dev, int cmd)
320{
321 short ioaddr = dev->base_addr;
322
323 ACOMMAND(cmd);
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int com90io_status(struct net_device *dev)
327{
328 short ioaddr = dev->base_addr;
329
330 return ASTATUS();
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static void com90io_setmask(struct net_device *dev, int mask)
334{
335 short ioaddr = dev->base_addr;
336
337 AINTMASK(mask);
338}
339
340static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
341 void *buf, int count)
342{
Joe Perchesa34c0932015-05-05 10:05:55 -0700343 TIME(dev, "put_whole_buffer", count,
344 put_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
348 void *buf, int count)
349{
Joe Perchesa34c0932015-05-05 10:05:55 -0700350 TIME(dev, "get_whole_buffer", count,
351 get_whole_buffer(dev, bufnum * 512 + offset, count, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354static int io; /* use the insmod io= irq= shmem= options */
355static int irq;
356static char device[9]; /* use eg. device=arc1 to change name */
357
358module_param(io, int, 0);
359module_param(irq, int, 0);
360module_param_string(device, device, sizeof(device), 0);
361MODULE_LICENSE("GPL");
362
363#ifndef MODULE
364static int __init com90io_setup(char *s)
365{
366 int ints[4];
Joe Perches01a1d5a2015-05-05 10:05:48 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 s = get_options(s, 4, ints);
369 if (!ints[0])
370 return 0;
371 switch (ints[0]) {
372 default: /* ERROR */
Joe Perches05a24b22015-05-05 10:05:56 -0700373 pr_err("Too many arguments\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 case 2: /* IRQ */
375 irq = ints[2];
376 case 1: /* IO address */
377 io = ints[1];
378 }
379 if (*s)
380 snprintf(device, sizeof(device), "%s", s);
381 return 1;
382}
383__setup("com90io=", com90io_setup);
384#endif
385
386static struct net_device *my_dev;
387
388static int __init com90io_init(void)
389{
390 struct net_device *dev;
391 int err;
392
393 dev = alloc_arcdev(device);
394 if (!dev)
395 return -ENOMEM;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 dev->base_addr = io;
398 dev->irq = irq;
399 if (dev->irq == 2)
400 dev->irq = 9;
401
402 err = com90io_probe(dev);
403
404 if (err) {
405 free_netdev(dev);
406 return err;
407 }
408
409 my_dev = dev;
410 return 0;
411}
412
413static void __exit com90io_exit(void)
414{
415 struct net_device *dev = my_dev;
416 int ioaddr = dev->base_addr;
417
418 unregister_netdev(dev);
419
420 /* Set the thing back to MMAP mode, in case the old driver is loaded later */
421 outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
422
423 free_irq(dev->irq, dev);
424 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
425 free_netdev(dev);
426}
427
428module_init(com90io_init)
429module_exit(com90io_exit)