Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 30 | static 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. */ |
| 51 | static 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 | */ |
| 59 | static 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. */ |
| 65 | static int dmalist[] __initdata = { |
| 66 | 5, 6, 7, |
| 67 | 0 |
| 68 | }; |
| 69 | |
| 70 | static char isa_cardname[] = "SK NET TR 4/16 ISA\0"; |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 71 | static u64 dma_mask = ISA_MAX_ADDRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static int sk_isa_open(struct net_device *dev); |
| 73 | static void sk_isa_read_eeprom(struct net_device *dev); |
| 74 | static unsigned short sk_isa_setnselout_pins(struct net_device *dev); |
| 75 | |
| 76 | static unsigned short sk_isa_sifreadb(struct net_device *dev, unsigned short reg) |
| 77 | { |
| 78 | return inb(dev->base_addr + reg); |
| 79 | } |
| 80 | |
| 81 | static unsigned short sk_isa_sifreadw(struct net_device *dev, unsigned short reg) |
| 82 | { |
| 83 | return inw(dev->base_addr + reg); |
| 84 | } |
| 85 | |
| 86 | static void sk_isa_sifwriteb(struct net_device *dev, unsigned short val, unsigned short reg) |
| 87 | { |
| 88 | outb(val, dev->base_addr + reg); |
| 89 | } |
| 90 | |
| 91 | static void sk_isa_sifwritew(struct net_device *dev, unsigned short val, unsigned short reg) |
| 92 | { |
| 93 | outw(val, dev->base_addr + reg); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static int __init sk_isa_probe1(struct net_device *dev, int ioaddr) |
| 98 | { |
| 99 | unsigned char old, chk1, chk2; |
| 100 | |
| 101 | if (!request_region(ioaddr, SK_ISA_IO_EXTENT, isa_cardname)) |
| 102 | return -ENODEV; |
| 103 | |
| 104 | old = inb(ioaddr + SIFADR); /* Get the old SIFADR value */ |
| 105 | |
| 106 | chk1 = 0; /* Begin with check value 0 */ |
| 107 | do { |
| 108 | /* Write new SIFADR value */ |
| 109 | outb(chk1, ioaddr + SIFADR); |
| 110 | |
| 111 | /* Read, invert and write */ |
| 112 | chk2 = inb(ioaddr + SIFADD); |
| 113 | chk2 ^= 0x0FE; |
| 114 | outb(chk2, ioaddr + SIFADR); |
| 115 | |
| 116 | /* Read, invert and compare */ |
| 117 | chk2 = inb(ioaddr + SIFADD); |
| 118 | chk2 ^= 0x0FE; |
| 119 | |
| 120 | if(chk1 != chk2) { |
| 121 | release_region(ioaddr, SK_ISA_IO_EXTENT); |
| 122 | return -ENODEV; |
| 123 | } |
| 124 | |
| 125 | chk1 -= 2; |
| 126 | } while(chk1 != 0); /* Repeat 128 times (all byte values) */ |
| 127 | |
| 128 | /* Restore the SIFADR value */ |
| 129 | outb(old, ioaddr + SIFADR); |
| 130 | |
| 131 | dev->base_addr = ioaddr; |
| 132 | return 0; |
| 133 | } |
| 134 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 135 | static int __init setup_card(struct net_device *dev, struct device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | struct net_local *tp; |
| 138 | static int versionprinted; |
| 139 | const unsigned *port; |
| 140 | int j, err = 0; |
| 141 | |
| 142 | if (!dev) |
| 143 | return -ENOMEM; |
| 144 | |
| 145 | SET_MODULE_OWNER(dev); |
| 146 | if (dev->base_addr) /* probe specific location */ |
| 147 | err = sk_isa_probe1(dev, dev->base_addr); |
| 148 | else { |
| 149 | for (port = portlist; *port; port++) { |
| 150 | err = sk_isa_probe1(dev, *port); |
| 151 | if (!err) |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (err) |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 156 | goto out5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | /* At this point we have found a valid card. */ |
| 159 | |
| 160 | if (versionprinted++ == 0) |
| 161 | printk(KERN_DEBUG "%s", version); |
| 162 | |
| 163 | err = -EIO; |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 164 | pdev->dma_mask = &dma_mask; |
| 165 | if (tmsdev_init(dev, ISA_MAX_ADDRESS, pdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | goto out4; |
| 167 | |
| 168 | dev->base_addr &= ~3; |
| 169 | |
| 170 | sk_isa_read_eeprom(dev); |
| 171 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 172 | printk(KERN_DEBUG "skisa.c: Ring Station Address: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 205 | printk(KERN_INFO "skisa.c: AutoSelect no IRQ available\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 216 | printk(KERN_INFO "skisa.c: Illegal IRQ %d specified\n", |
| 217 | dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | goto out3; |
| 219 | } |
| 220 | if (request_irq(dev->irq, tms380tr_interrupt, 0, |
| 221 | isa_cardname, dev)) |
| 222 | { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 223 | printk(KERN_INFO "skisa.c: Selected IRQ %d not available\n", |
| 224 | dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 240 | printk(KERN_INFO "skisa.c: AutoSelect no DMA available\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 251 | printk(KERN_INFO "skisa.c: Illegal DMA %d specified\n", |
| 252 | dev->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | goto out2; |
| 254 | } |
| 255 | if (request_dma(dev->dma, isa_cardname)) |
| 256 | { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 257 | printk(KERN_INFO "skisa.c: Selected DMA %d not available\n", |
| 258 | dev->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | goto out2; |
| 260 | } |
| 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | err = register_netdev(dev); |
| 264 | if (err) |
| 265 | goto out; |
| 266 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 267 | printk(KERN_DEBUG "%s: IO: %#4lx IRQ: %d DMA: %d\n", |
| 268 | dev->name, dev->base_addr, dev->irq, dev->dma); |
| 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return 0; |
| 271 | out: |
| 272 | free_dma(dev->dma); |
| 273 | out2: |
| 274 | free_irq(dev->irq, dev); |
| 275 | out3: |
| 276 | tmsdev_term(dev); |
| 277 | out4: |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 278 | release_region(dev->base_addr, SK_ISA_IO_EXTENT); |
| 279 | out5: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return err; |
| 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* |
| 284 | * Reads MAC address from adapter RAM, which should've read it from |
| 285 | * the onboard ROM. |
| 286 | * |
| 287 | * Calling this on a board that does not support it can be a very |
| 288 | * dangerous thing. The Madge board, for instance, will lock your |
| 289 | * machine hard when this is called. Luckily, its supported in a |
| 290 | * separate driver. --ASF |
| 291 | */ |
| 292 | static void sk_isa_read_eeprom(struct net_device *dev) |
| 293 | { |
| 294 | int i; |
| 295 | |
| 296 | /* Address: 0000:0000 */ |
| 297 | sk_isa_sifwritew(dev, 0, SIFADX); |
| 298 | sk_isa_sifwritew(dev, 0, SIFADR); |
| 299 | |
| 300 | /* Read six byte MAC address data */ |
| 301 | dev->addr_len = 6; |
| 302 | for(i = 0; i < 6; i++) |
| 303 | dev->dev_addr[i] = sk_isa_sifreadw(dev, SIFINC) >> 8; |
| 304 | } |
| 305 | |
| 306 | unsigned short sk_isa_setnselout_pins(struct net_device *dev) |
| 307 | { |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int sk_isa_open(struct net_device *dev) |
| 312 | { |
| 313 | struct net_local *tp = netdev_priv(dev); |
| 314 | unsigned short val = 0; |
| 315 | unsigned short oldval; |
| 316 | int i; |
| 317 | |
| 318 | val = 0; |
| 319 | for(i = 0; irqlist[i] != 0; i++) |
| 320 | { |
| 321 | if(irqlist[i] == dev->irq) |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | val |= CYCLE_TIME << 2; |
| 326 | val |= i << 4; |
| 327 | i = dev->dma - 5; |
| 328 | val |= i; |
| 329 | if(tp->DataRate == SPEED_4) |
| 330 | val |= LINE_SPEED_BIT; |
| 331 | else |
| 332 | val &= ~LINE_SPEED_BIT; |
| 333 | oldval = sk_isa_sifreadb(dev, POSREG); |
| 334 | /* Leave cycle bits alone */ |
| 335 | oldval |= 0xf3; |
| 336 | val &= oldval; |
| 337 | sk_isa_sifwriteb(dev, val, POSREG); |
| 338 | |
| 339 | return tms380tr_open(dev); |
| 340 | } |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | #define ISATR_MAX_ADAPTERS 3 |
| 343 | |
| 344 | static int io[ISATR_MAX_ADAPTERS]; |
| 345 | static int irq[ISATR_MAX_ADAPTERS]; |
| 346 | static int dma[ISATR_MAX_ADAPTERS]; |
| 347 | |
| 348 | MODULE_LICENSE("GPL"); |
| 349 | |
| 350 | module_param_array(io, int, NULL, 0); |
| 351 | module_param_array(irq, int, NULL, 0); |
| 352 | module_param_array(dma, int, NULL, 0); |
| 353 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 354 | static struct platform_device *sk_isa_dev[ISATR_MAX_ADAPTERS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 356 | static struct device_driver sk_isa_driver = { |
| 357 | .name = "skisa", |
| 358 | .bus = &platform_bus_type, |
| 359 | }; |
| 360 | |
| 361 | static int __init sk_isa_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
| 363 | struct net_device *dev; |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 364 | struct platform_device *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | int i, num = 0, err = 0; |
| 366 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 367 | err = driver_register(&sk_isa_driver); |
| 368 | if (err) |
| 369 | return err; |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) { |
| 372 | dev = alloc_trdev(sizeof(struct net_local)); |
| 373 | if (!dev) |
| 374 | continue; |
| 375 | |
| 376 | dev->base_addr = io[i]; |
| 377 | dev->irq = irq[i]; |
| 378 | dev->dma = dma[i]; |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 379 | pdev = platform_device_register_simple("skisa", |
| 380 | i, NULL, 0); |
| 381 | err = setup_card(dev, &pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if (!err) { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 383 | sk_isa_dev[i] = pdev; |
| 384 | dev_set_drvdata(&sk_isa_dev[i]->dev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | ++num; |
| 386 | } else { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 387 | platform_device_unregister(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | free_netdev(dev); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | printk(KERN_NOTICE "skisa.c: %d cards found.\n", num); |
| 393 | /* Probe for cards. */ |
| 394 | if (num == 0) { |
| 395 | printk(KERN_NOTICE "skisa.c: No cards found.\n"); |
| 396 | return (-ENODEV); |
| 397 | } |
| 398 | return (0); |
| 399 | } |
| 400 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 401 | static void __exit sk_isa_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 403 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | int i; |
| 405 | |
| 406 | for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) { |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 407 | struct platform_device *pdev = sk_isa_dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 409 | if (!pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | continue; |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 411 | dev = dev_get_drvdata(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | unregister_netdev(dev); |
| 413 | release_region(dev->base_addr, SK_ISA_IO_EXTENT); |
| 414 | free_irq(dev->irq, dev); |
| 415 | free_dma(dev->dma); |
| 416 | tmsdev_term(dev); |
| 417 | free_netdev(dev); |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 418 | dev_set_drvdata(&pdev->dev, NULL); |
| 419 | platform_device_unregister(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 421 | driver_unregister(&sk_isa_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Jochen Friedrich | 504ff16 | 2005-07-27 01:14:50 -0700 | [diff] [blame] | 424 | module_init(sk_isa_init); |
| 425 | module_exit(sk_isa_cleanup); |