blob: 3fab54a264666572a76c96f5c86a8baed22baa6e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * skisa.c: A network driver for SK-NET TMS380-based ISA token ring cards.
3 *
4 * Based on tmspci written 1999 by Adam Fritzler
5 *
6 * Written 2000 by Jochen Friedrich
7 * Dedicated to my girlfriend Steffi Bopp
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 *
12 * This driver module supports the following cards:
13 * - SysKonnect TR4/16(+) ISA (SK-4190)
14 *
15 * Maintainer(s):
16 * AF Adam Fritzler mid@auk.cx
17 * JF Jochen Friedrich jochen@scram.de
18 *
19 * Modification History:
20 * 14-Jan-01 JF Created
21 * 28-Oct-02 JF Fixed probe of card for static compilation.
22 * Fixed module init to not make hotplug go wild.
23 * 09-Nov-02 JF Fixed early bail out on out of memory
24 * situations if multiple cards are found.
25 * Cleaned up some unnecessary console SPAM.
26 * 09-Dec-02 JF Fixed module reference counting.
27 * 02-Jan-03 JF Renamed to skisa.c
28 *
29 */
30static const char version[] = "skisa.c: v1.03 09/12/2002 by Jochen Friedrich\n";
31
32#include <linux/module.h>
33#include <linux/kernel.h>
34#include <linux/errno.h>
35#include <linux/pci.h>
36#include <linux/init.h>
37#include <linux/netdevice.h>
38#include <linux/trdevice.h>
39
40#include <asm/system.h>
41#include <asm/io.h>
42#include <asm/irq.h>
43#include <asm/pci.h>
44#include <asm/dma.h>
45
46#include "tms380tr.h"
47
48#define SK_ISA_IO_EXTENT 32
49
50/* A zero-terminated list of I/O addresses to be probed. */
51static unsigned int portlist[] __initdata = {
52 0x0A20, 0x1A20, 0x0B20, 0x1B20, 0x0980, 0x1980, 0x0900, 0x1900,// SK
53 0
54};
55
56/* A zero-terminated list of IRQs to be probed.
57 * Used again after initial probe for sktr_chipset_init, called from sktr_open.
58 */
59static const unsigned short irqlist[] = {
60 3, 5, 9, 10, 11, 12, 15,
61 0
62};
63
64/* A zero-terminated list of DMAs to be probed. */
65static int dmalist[] __initdata = {
66 5, 6, 7,
67 0
68};
69
70static char isa_cardname[] = "SK NET TR 4/16 ISA\0";
71
72struct net_device *sk_isa_probe(int unit);
73static int sk_isa_open(struct net_device *dev);
74static void sk_isa_read_eeprom(struct net_device *dev);
75static unsigned short sk_isa_setnselout_pins(struct net_device *dev);
76
77static unsigned short sk_isa_sifreadb(struct net_device *dev, unsigned short reg)
78{
79 return inb(dev->base_addr + reg);
80}
81
82static unsigned short sk_isa_sifreadw(struct net_device *dev, unsigned short reg)
83{
84 return inw(dev->base_addr + reg);
85}
86
87static void sk_isa_sifwriteb(struct net_device *dev, unsigned short val, unsigned short reg)
88{
89 outb(val, dev->base_addr + reg);
90}
91
92static void sk_isa_sifwritew(struct net_device *dev, unsigned short val, unsigned short reg)
93{
94 outw(val, dev->base_addr + reg);
95}
96
97
98static int __init sk_isa_probe1(struct net_device *dev, int ioaddr)
99{
100 unsigned char old, chk1, chk2;
101
102 if (!request_region(ioaddr, SK_ISA_IO_EXTENT, isa_cardname))
103 return -ENODEV;
104
105 old = inb(ioaddr + SIFADR); /* Get the old SIFADR value */
106
107 chk1 = 0; /* Begin with check value 0 */
108 do {
109 /* Write new SIFADR value */
110 outb(chk1, ioaddr + SIFADR);
111
112 /* Read, invert and write */
113 chk2 = inb(ioaddr + SIFADD);
114 chk2 ^= 0x0FE;
115 outb(chk2, ioaddr + SIFADR);
116
117 /* Read, invert and compare */
118 chk2 = inb(ioaddr + SIFADD);
119 chk2 ^= 0x0FE;
120
121 if(chk1 != chk2) {
122 release_region(ioaddr, SK_ISA_IO_EXTENT);
123 return -ENODEV;
124 }
125
126 chk1 -= 2;
127 } while(chk1 != 0); /* Repeat 128 times (all byte values) */
128
129 /* Restore the SIFADR value */
130 outb(old, ioaddr + SIFADR);
131
132 dev->base_addr = ioaddr;
133 return 0;
134}
135
136static int __init setup_card(struct net_device *dev)
137{
138 struct net_local *tp;
139 static int versionprinted;
140 const unsigned *port;
141 int j, err = 0;
142
143 if (!dev)
144 return -ENOMEM;
145
146 SET_MODULE_OWNER(dev);
147 if (dev->base_addr) /* probe specific location */
148 err = sk_isa_probe1(dev, dev->base_addr);
149 else {
150 for (port = portlist; *port; port++) {
151 err = sk_isa_probe1(dev, *port);
152 if (!err)
153 break;
154 }
155 }
156 if (err)
157 goto out4;
158
159 /* At this point we have found a valid card. */
160
161 if (versionprinted++ == 0)
162 printk(KERN_DEBUG "%s", version);
163
164 err = -EIO;
165 if (tmsdev_init(dev, ISA_MAX_ADDRESS, NULL))
166 goto out4;
167
168 dev->base_addr &= ~3;
169
170 sk_isa_read_eeprom(dev);
171
172 printk(KERN_DEBUG "%s: Ring Station Address: ", dev->name);
173 printk("%2.2x", dev->dev_addr[0]);
174 for (j = 1; j < 6; j++)
175 printk(":%2.2x", dev->dev_addr[j]);
176 printk("\n");
177
178 tp = netdev_priv(dev);
179 tp->setnselout = sk_isa_setnselout_pins;
180
181 tp->sifreadb = sk_isa_sifreadb;
182 tp->sifreadw = sk_isa_sifreadw;
183 tp->sifwriteb = sk_isa_sifwriteb;
184 tp->sifwritew = sk_isa_sifwritew;
185
186 memcpy(tp->ProductID, isa_cardname, PROD_ID_SIZE + 1);
187
188 tp->tmspriv = NULL;
189
190 dev->open = sk_isa_open;
191 dev->stop = tms380tr_close;
192
193 if (dev->irq == 0)
194 {
195 for(j = 0; irqlist[j] != 0; j++)
196 {
197 dev->irq = irqlist[j];
198 if (!request_irq(dev->irq, tms380tr_interrupt, 0,
199 isa_cardname, dev))
200 break;
201 }
202
203 if(irqlist[j] == 0)
204 {
205 printk(KERN_INFO "%s: AutoSelect no IRQ available\n", dev->name);
206 goto out3;
207 }
208 }
209 else
210 {
211 for(j = 0; irqlist[j] != 0; j++)
212 if (irqlist[j] == dev->irq)
213 break;
214 if (irqlist[j] == 0)
215 {
216 printk(KERN_INFO "%s: Illegal IRQ %d specified\n",
217 dev->name, dev->irq);
218 goto out3;
219 }
220 if (request_irq(dev->irq, tms380tr_interrupt, 0,
221 isa_cardname, dev))
222 {
223 printk(KERN_INFO "%s: Selected IRQ %d not available\n",
224 dev->name, dev->irq);
225 goto out3;
226 }
227 }
228
229 if (dev->dma == 0)
230 {
231 for(j = 0; dmalist[j] != 0; j++)
232 {
233 dev->dma = dmalist[j];
234 if (!request_dma(dev->dma, isa_cardname))
235 break;
236 }
237
238 if(dmalist[j] == 0)
239 {
240 printk(KERN_INFO "%s: AutoSelect no DMA available\n", dev->name);
241 goto out2;
242 }
243 }
244 else
245 {
246 for(j = 0; dmalist[j] != 0; j++)
247 if (dmalist[j] == dev->dma)
248 break;
249 if (dmalist[j] == 0)
250 {
251 printk(KERN_INFO "%s: Illegal DMA %d specified\n",
252 dev->name, dev->dma);
253 goto out2;
254 }
255 if (request_dma(dev->dma, isa_cardname))
256 {
257 printk(KERN_INFO "%s: Selected DMA %d not available\n",
258 dev->name, dev->dma);
259 goto out2;
260 }
261 }
262
263 printk(KERN_DEBUG "%s: IO: %#4lx IRQ: %d DMA: %d\n",
264 dev->name, dev->base_addr, dev->irq, dev->dma);
265
266 err = register_netdev(dev);
267 if (err)
268 goto out;
269
270 return 0;
271out:
272 free_dma(dev->dma);
273out2:
274 free_irq(dev->irq, dev);
275out3:
276 tmsdev_term(dev);
277out4:
278 release_region(dev->base_addr, SK_ISA_IO_EXTENT);
279 return err;
280}
281
282struct net_device * __init sk_isa_probe(int unit)
283{
284 struct net_device *dev = alloc_trdev(sizeof(struct net_local));
285 int err = 0;
286
287 if (!dev)
288 return ERR_PTR(-ENOMEM);
289
290 if (unit >= 0) {
291 sprintf(dev->name, "tr%d", unit);
292 netdev_boot_setup_check(dev);
293 }
294
295 err = setup_card(dev);
296 if (err)
297 goto out;
298
299 return dev;
300out:
301 free_netdev(dev);
302 return ERR_PTR(err);
303}
304
305/*
306 * Reads MAC address from adapter RAM, which should've read it from
307 * the onboard ROM.
308 *
309 * Calling this on a board that does not support it can be a very
310 * dangerous thing. The Madge board, for instance, will lock your
311 * machine hard when this is called. Luckily, its supported in a
312 * separate driver. --ASF
313 */
314static void sk_isa_read_eeprom(struct net_device *dev)
315{
316 int i;
317
318 /* Address: 0000:0000 */
319 sk_isa_sifwritew(dev, 0, SIFADX);
320 sk_isa_sifwritew(dev, 0, SIFADR);
321
322 /* Read six byte MAC address data */
323 dev->addr_len = 6;
324 for(i = 0; i < 6; i++)
325 dev->dev_addr[i] = sk_isa_sifreadw(dev, SIFINC) >> 8;
326}
327
328unsigned short sk_isa_setnselout_pins(struct net_device *dev)
329{
330 return 0;
331}
332
333static int sk_isa_open(struct net_device *dev)
334{
335 struct net_local *tp = netdev_priv(dev);
336 unsigned short val = 0;
337 unsigned short oldval;
338 int i;
339
340 val = 0;
341 for(i = 0; irqlist[i] != 0; i++)
342 {
343 if(irqlist[i] == dev->irq)
344 break;
345 }
346
347 val |= CYCLE_TIME << 2;
348 val |= i << 4;
349 i = dev->dma - 5;
350 val |= i;
351 if(tp->DataRate == SPEED_4)
352 val |= LINE_SPEED_BIT;
353 else
354 val &= ~LINE_SPEED_BIT;
355 oldval = sk_isa_sifreadb(dev, POSREG);
356 /* Leave cycle bits alone */
357 oldval |= 0xf3;
358 val &= oldval;
359 sk_isa_sifwriteb(dev, val, POSREG);
360
361 return tms380tr_open(dev);
362}
363
364#ifdef MODULE
365
366#define ISATR_MAX_ADAPTERS 3
367
368static int io[ISATR_MAX_ADAPTERS];
369static int irq[ISATR_MAX_ADAPTERS];
370static int dma[ISATR_MAX_ADAPTERS];
371
372MODULE_LICENSE("GPL");
373
374module_param_array(io, int, NULL, 0);
375module_param_array(irq, int, NULL, 0);
376module_param_array(dma, int, NULL, 0);
377
378static struct net_device *sk_isa_dev[ISATR_MAX_ADAPTERS];
379
380int init_module(void)
381{
382 struct net_device *dev;
383 int i, num = 0, err = 0;
384
385 for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) {
386 dev = alloc_trdev(sizeof(struct net_local));
387 if (!dev)
388 continue;
389
390 dev->base_addr = io[i];
391 dev->irq = irq[i];
392 dev->dma = dma[i];
393 err = setup_card(dev);
394
395 if (!err) {
396 sk_isa_dev[i] = dev;
397 ++num;
398 } else {
399 free_netdev(dev);
400 }
401 }
402
403 printk(KERN_NOTICE "skisa.c: %d cards found.\n", num);
404 /* Probe for cards. */
405 if (num == 0) {
406 printk(KERN_NOTICE "skisa.c: No cards found.\n");
407 return (-ENODEV);
408 }
409 return (0);
410}
411
412void cleanup_module(void)
413{
414 int i;
415
416 for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) {
417 struct net_device *dev = sk_isa_dev[i];
418
419 if (!dev)
420 continue;
421
422 unregister_netdev(dev);
423 release_region(dev->base_addr, SK_ISA_IO_EXTENT);
424 free_irq(dev->irq, dev);
425 free_dma(dev->dma);
426 tmsdev_term(dev);
427 free_netdev(dev);
428 }
429}
430#endif /* MODULE */
431
432
433/*
434 * Local variables:
435 * compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c skisa.c"
436 * alt-compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c skisa.c"
437 * c-set-style "K&R"
438 * c-indent-level: 8
439 * c-basic-offset: 8
440 * tab-width: 8
441 * End:
442 */