0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
0029
0030 #include <linux/module.h>
0031 #include <linux/moduleparam.h>
0032 #include <linux/init.h>
0033 #include <linux/interrupt.h>
0034 #include <linux/ioport.h>
0035 #include <linux/delay.h>
0036 #include <linux/netdevice.h>
0037 #include <linux/slab.h>
0038 #include <linux/io.h>
0039
0040 #include "arcdevice.h"
0041 #include "com9026.h"
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 #undef FAST_PROBE
0056
0057
0058 static int com90xx_found(int ioaddr, int airq, u_long shmem, void __iomem *);
0059 static void com90xx_command(struct net_device *dev, int command);
0060 static int com90xx_status(struct net_device *dev);
0061 static void com90xx_setmask(struct net_device *dev, int mask);
0062 static int com90xx_reset(struct net_device *dev, int really_reset);
0063 static void com90xx_copy_to_card(struct net_device *dev, int bufnum, int offset,
0064 void *buf, int count);
0065 static void com90xx_copy_from_card(struct net_device *dev, int bufnum,
0066 int offset, void *buf, int count);
0067
0068
0069
0070 static struct net_device *cards[16];
0071 static int numcards;
0072
0073
0074
0075
0076 #define ARCNET_TOTAL_SIZE 16
0077
0078
0079 #define BUFFER_SIZE (512)
0080 #define MIRROR_SIZE (BUFFER_SIZE * 4)
0081
0082 static int com90xx_skip_probe __initdata = 0;
0083
0084
0085
0086 static int io;
0087 static int irq;
0088 static int shmem;
0089 static char device[9];
0090
0091 module_param_hw(io, int, ioport, 0);
0092 module_param_hw(irq, int, irq, 0);
0093 module_param(shmem, int, 0);
0094 module_param_string(device, device, sizeof(device), 0);
0095
0096 static void __init com90xx_probe(void)
0097 {
0098 int count, status, ioaddr, numprint, airq, openparen = 0;
0099 unsigned long airqmask;
0100 int ports[(0x3f0 - 0x200) / 16 + 1] = { 0 };
0101 unsigned long *shmems;
0102 void __iomem **iomem;
0103 int numports, numshmems, *port;
0104 u_long *p;
0105 int index;
0106
0107 if (!io && !irq && !shmem && !*device && com90xx_skip_probe)
0108 return;
0109
0110 shmems = kzalloc(((0x100000 - 0xa0000) / 0x800) * sizeof(unsigned long),
0111 GFP_KERNEL);
0112 if (!shmems)
0113 return;
0114 iomem = kzalloc(((0x100000 - 0xa0000) / 0x800) * sizeof(void __iomem *),
0115 GFP_KERNEL);
0116 if (!iomem) {
0117 kfree(shmems);
0118 return;
0119 }
0120
0121 if (BUGLVL(D_NORMAL))
0122 pr_info("%s\n", "COM90xx chipset support");
0123
0124
0125 numports = numshmems = 0;
0126 if (io)
0127 ports[numports++] = io;
0128 else
0129 for (count = 0x200; count <= 0x3f0; count += 16)
0130 ports[numports++] = count;
0131 if (shmem)
0132 shmems[numshmems++] = shmem;
0133 else
0134 for (count = 0xA0000; count <= 0xFF800; count += 2048)
0135 shmems[numshmems++] = count;
0136
0137
0138
0139
0140 numprint = -1;
0141 for (port = &ports[0]; port - ports < numports; port++) {
0142 numprint++;
0143 numprint %= 8;
0144 if (!numprint) {
0145 arc_cont(D_INIT, "\n");
0146 arc_cont(D_INIT, "S1: ");
0147 }
0148 arc_cont(D_INIT, "%Xh ", *port);
0149
0150 ioaddr = *port;
0151
0152 if (!request_region(*port, ARCNET_TOTAL_SIZE,
0153 "arcnet (90xx)")) {
0154 arc_cont(D_INIT_REASONS, "(request_region)\n");
0155 arc_cont(D_INIT_REASONS, "S1: ");
0156 if (BUGLVL(D_INIT_REASONS))
0157 numprint = 0;
0158 *port-- = ports[--numports];
0159 continue;
0160 }
0161 if (arcnet_inb(ioaddr, COM9026_REG_R_STATUS) == 0xFF) {
0162 arc_cont(D_INIT_REASONS, "(empty)\n");
0163 arc_cont(D_INIT_REASONS, "S1: ");
0164 if (BUGLVL(D_INIT_REASONS))
0165 numprint = 0;
0166 release_region(*port, ARCNET_TOTAL_SIZE);
0167 *port-- = ports[--numports];
0168 continue;
0169 }
0170
0171 arcnet_inb(ioaddr, COM9026_REG_R_RESET);
0172
0173 arc_cont(D_INIT_REASONS, "\n");
0174 arc_cont(D_INIT_REASONS, "S1: ");
0175 if (BUGLVL(D_INIT_REASONS))
0176 numprint = 0;
0177 }
0178 arc_cont(D_INIT, "\n");
0179
0180 if (!numports) {
0181 arc_cont(D_NORMAL, "S1: No ARCnet cards found.\n");
0182 kfree(shmems);
0183 kfree(iomem);
0184 return;
0185 }
0186
0187
0188
0189
0190 numprint = -1;
0191 for (port = &ports[0]; port < ports + numports; port++) {
0192 numprint++;
0193 numprint %= 8;
0194 if (!numprint) {
0195 arc_cont(D_INIT, "\n");
0196 arc_cont(D_INIT, "S2: ");
0197 }
0198 arc_cont(D_INIT, "%Xh ", *port);
0199 }
0200 arc_cont(D_INIT, "\n");
0201 mdelay(RESETtime);
0202
0203
0204
0205
0206 numprint = -1;
0207 for (index = 0, p = &shmems[0]; index < numshmems; p++, index++) {
0208 void __iomem *base;
0209
0210 numprint++;
0211 numprint %= 8;
0212 if (!numprint) {
0213 arc_cont(D_INIT, "\n");
0214 arc_cont(D_INIT, "S3: ");
0215 }
0216 arc_cont(D_INIT, "%lXh ", *p);
0217
0218 if (!request_mem_region(*p, MIRROR_SIZE, "arcnet (90xx)")) {
0219 arc_cont(D_INIT_REASONS, "(request_mem_region)\n");
0220 arc_cont(D_INIT_REASONS, "Stage 3: ");
0221 if (BUGLVL(D_INIT_REASONS))
0222 numprint = 0;
0223 goto out;
0224 }
0225 base = ioremap(*p, MIRROR_SIZE);
0226 if (!base) {
0227 arc_cont(D_INIT_REASONS, "(ioremap)\n");
0228 arc_cont(D_INIT_REASONS, "Stage 3: ");
0229 if (BUGLVL(D_INIT_REASONS))
0230 numprint = 0;
0231 goto out1;
0232 }
0233 if (arcnet_readb(base, COM9026_REG_R_STATUS) != TESTvalue) {
0234 arc_cont(D_INIT_REASONS, "(%02Xh != %02Xh)\n",
0235 arcnet_readb(base, COM9026_REG_R_STATUS),
0236 TESTvalue);
0237 arc_cont(D_INIT_REASONS, "S3: ");
0238 if (BUGLVL(D_INIT_REASONS))
0239 numprint = 0;
0240 goto out2;
0241 }
0242
0243
0244
0245
0246
0247 arcnet_writeb(0x42, base, COM9026_REG_W_INTMASK);
0248 if (arcnet_readb(base, COM9026_REG_R_STATUS) != 0x42) {
0249 arc_cont(D_INIT_REASONS, "(read only)\n");
0250 arc_cont(D_INIT_REASONS, "S3: ");
0251 goto out2;
0252 }
0253 arc_cont(D_INIT_REASONS, "\n");
0254 arc_cont(D_INIT_REASONS, "S3: ");
0255 if (BUGLVL(D_INIT_REASONS))
0256 numprint = 0;
0257 iomem[index] = base;
0258 continue;
0259 out2:
0260 iounmap(base);
0261 out1:
0262 release_mem_region(*p, MIRROR_SIZE);
0263 out:
0264 *p-- = shmems[--numshmems];
0265 index--;
0266 }
0267 arc_cont(D_INIT, "\n");
0268
0269 if (!numshmems) {
0270 arc_cont(D_NORMAL, "S3: No ARCnet cards found.\n");
0271 for (port = &ports[0]; port < ports + numports; port++)
0272 release_region(*port, ARCNET_TOTAL_SIZE);
0273 kfree(shmems);
0274 kfree(iomem);
0275 return;
0276 }
0277
0278
0279
0280 numprint = -1;
0281 for (p = &shmems[0]; p < shmems + numshmems; p++) {
0282 numprint++;
0283 numprint %= 8;
0284 if (!numprint) {
0285 arc_cont(D_INIT, "\n");
0286 arc_cont(D_INIT, "S4: ");
0287 }
0288 arc_cont(D_INIT, "%lXh ", *p);
0289 }
0290 arc_cont(D_INIT, "\n");
0291
0292
0293
0294
0295
0296
0297
0298
0299 numprint = -1;
0300 for (port = &ports[0]; port < ports + numports; port++) {
0301 int found = 0;
0302
0303 numprint++;
0304 numprint %= 8;
0305 if (!numprint) {
0306 arc_cont(D_INIT, "\n");
0307 arc_cont(D_INIT, "S5: ");
0308 }
0309 arc_cont(D_INIT, "%Xh ", *port);
0310
0311 ioaddr = *port;
0312 status = arcnet_inb(ioaddr, COM9026_REG_R_STATUS);
0313
0314 if ((status & 0x9D)
0315 != (NORXflag | RECONflag | TXFREEflag | RESETflag)) {
0316 arc_cont(D_INIT_REASONS, "(status=%Xh)\n", status);
0317 arc_cont(D_INIT_REASONS, "S5: ");
0318 if (BUGLVL(D_INIT_REASONS))
0319 numprint = 0;
0320 release_region(*port, ARCNET_TOTAL_SIZE);
0321 *port-- = ports[--numports];
0322 continue;
0323 }
0324 arcnet_outb(CFLAGScmd | RESETclear | CONFIGclear,
0325 ioaddr, COM9026_REG_W_COMMAND);
0326 status = arcnet_inb(ioaddr, COM9026_REG_R_STATUS);
0327 if (status & RESETflag) {
0328 arc_cont(D_INIT_REASONS, " (eternal reset, status=%Xh)\n",
0329 status);
0330 arc_cont(D_INIT_REASONS, "S5: ");
0331 if (BUGLVL(D_INIT_REASONS))
0332 numprint = 0;
0333 release_region(*port, ARCNET_TOTAL_SIZE);
0334 *port-- = ports[--numports];
0335 continue;
0336 }
0337
0338
0339
0340 if (!irq) {
0341
0342
0343
0344
0345 airqmask = probe_irq_on();
0346 arcnet_outb(NORXflag, ioaddr, COM9026_REG_W_INTMASK);
0347 udelay(1);
0348 arcnet_outb(0, ioaddr, COM9026_REG_W_INTMASK);
0349 airq = probe_irq_off(airqmask);
0350
0351 if (airq <= 0) {
0352 arc_cont(D_INIT_REASONS, "(airq=%d)\n", airq);
0353 arc_cont(D_INIT_REASONS, "S5: ");
0354 if (BUGLVL(D_INIT_REASONS))
0355 numprint = 0;
0356 release_region(*port, ARCNET_TOTAL_SIZE);
0357 *port-- = ports[--numports];
0358 continue;
0359 }
0360 } else {
0361 airq = irq;
0362 }
0363
0364 arc_cont(D_INIT, "(%d,", airq);
0365 openparen = 1;
0366
0367
0368
0369
0370
0371
0372
0373 #ifdef FAST_PROBE
0374 if (numports > 1 || numshmems > 1) {
0375 arcnet_inb(ioaddr, COM9026_REG_R_RESET);
0376 mdelay(RESETtime);
0377 } else {
0378
0379 arcnet_writeb(TESTvalue, iomem[0],
0380 COM9026_REG_W_INTMASK);
0381 }
0382 #else
0383 arcnet_inb(ioaddr, COM9026_REG_R_RESET);
0384 mdelay(RESETtime);
0385 #endif
0386
0387 for (index = 0; index < numshmems; index++) {
0388 u_long ptr = shmems[index];
0389 void __iomem *base = iomem[index];
0390
0391 if (arcnet_readb(base, COM9026_REG_R_STATUS) == TESTvalue) {
0392 arc_cont(D_INIT, "%lXh)\n", *p);
0393 openparen = 0;
0394
0395
0396 if (com90xx_found(*port, airq, ptr, base) == 0)
0397 found = 1;
0398 numprint = -1;
0399
0400
0401 shmems[index] = shmems[--numshmems];
0402 iomem[index] = iomem[numshmems];
0403 break;
0404 } else {
0405 arc_cont(D_INIT_REASONS, "%Xh-",
0406 arcnet_readb(base, COM9026_REG_R_STATUS));
0407 }
0408 }
0409
0410 if (openparen) {
0411 if (BUGLVL(D_INIT))
0412 pr_cont("no matching shmem)\n");
0413 if (BUGLVL(D_INIT_REASONS)) {
0414 pr_cont("S5: ");
0415 numprint = 0;
0416 }
0417 }
0418 if (!found)
0419 release_region(*port, ARCNET_TOTAL_SIZE);
0420 *port-- = ports[--numports];
0421 }
0422
0423 if (BUGLVL(D_INIT_REASONS))
0424 pr_cont("\n");
0425
0426
0427 for (index = 0; index < numshmems; index++) {
0428 arcnet_writeb(TESTvalue, iomem[index], COM9026_REG_W_INTMASK);
0429 iounmap(iomem[index]);
0430 release_mem_region(shmems[index], MIRROR_SIZE);
0431 }
0432 kfree(shmems);
0433 kfree(iomem);
0434 }
0435
0436 static int __init check_mirror(unsigned long addr, size_t size)
0437 {
0438 void __iomem *p;
0439 int res = -1;
0440
0441 if (!request_mem_region(addr, size, "arcnet (90xx)"))
0442 return -1;
0443
0444 p = ioremap(addr, size);
0445 if (p) {
0446 if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue)
0447 res = 1;
0448 else
0449 res = 0;
0450 iounmap(p);
0451 }
0452
0453 release_mem_region(addr, size);
0454 return res;
0455 }
0456
0457
0458
0459
0460 static int __init com90xx_found(int ioaddr, int airq, u_long shmem,
0461 void __iomem *p)
0462 {
0463 struct net_device *dev = NULL;
0464 struct arcnet_local *lp;
0465 u_long first_mirror, last_mirror;
0466 int mirror_size;
0467
0468
0469 dev = alloc_arcdev(device);
0470 if (!dev) {
0471 arc_cont(D_NORMAL, "com90xx: Can't allocate device!\n");
0472 iounmap(p);
0473 release_mem_region(shmem, MIRROR_SIZE);
0474 return -ENOMEM;
0475 }
0476 lp = netdev_priv(dev);
0477
0478
0479
0480
0481
0482
0483 mirror_size = MIRROR_SIZE;
0484 if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue &&
0485 check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 &&
0486 check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1)
0487 mirror_size = 2 * MIRROR_SIZE;
0488
0489 first_mirror = shmem - mirror_size;
0490 while (check_mirror(first_mirror, mirror_size) == 1)
0491 first_mirror -= mirror_size;
0492 first_mirror += mirror_size;
0493
0494 last_mirror = shmem + mirror_size;
0495 while (check_mirror(last_mirror, mirror_size) == 1)
0496 last_mirror += mirror_size;
0497 last_mirror -= mirror_size;
0498
0499 dev->mem_start = first_mirror;
0500 dev->mem_end = last_mirror + MIRROR_SIZE - 1;
0501
0502 iounmap(p);
0503 release_mem_region(shmem, MIRROR_SIZE);
0504
0505 if (!request_mem_region(dev->mem_start,
0506 dev->mem_end - dev->mem_start + 1,
0507 "arcnet (90xx)"))
0508 goto err_free_dev;
0509
0510
0511 if (request_irq(airq, arcnet_interrupt, 0, "arcnet (90xx)", dev)) {
0512 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", airq);
0513 goto err_release_mem;
0514 }
0515 dev->irq = airq;
0516
0517
0518 lp->card_name = "COM90xx";
0519 lp->hw.command = com90xx_command;
0520 lp->hw.status = com90xx_status;
0521 lp->hw.intmask = com90xx_setmask;
0522 lp->hw.reset = com90xx_reset;
0523 lp->hw.owner = THIS_MODULE;
0524 lp->hw.copy_to_card = com90xx_copy_to_card;
0525 lp->hw.copy_from_card = com90xx_copy_from_card;
0526 lp->mem_start = ioremap(dev->mem_start,
0527 dev->mem_end - dev->mem_start + 1);
0528 if (!lp->mem_start) {
0529 arc_printk(D_NORMAL, dev, "Can't remap device memory!\n");
0530 goto err_free_irq;
0531 }
0532
0533
0534 arcnet_set_addr(dev, arcnet_readb(lp->mem_start,
0535 COM9026_REG_R_STATION));
0536
0537 dev->base_addr = ioaddr;
0538
0539 arc_printk(D_NORMAL, dev, "COM90xx station %02Xh found at %03lXh, IRQ %d, ShMem %lXh (%ld*%xh).\n",
0540 dev->dev_addr[0],
0541 dev->base_addr, dev->irq, dev->mem_start,
0542 (dev->mem_end - dev->mem_start + 1) / mirror_size,
0543 mirror_size);
0544
0545 if (register_netdev(dev))
0546 goto err_unmap;
0547
0548 cards[numcards++] = dev;
0549 return 0;
0550
0551 err_unmap:
0552 iounmap(lp->mem_start);
0553 err_free_irq:
0554 free_irq(dev->irq, dev);
0555 err_release_mem:
0556 release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
0557 err_free_dev:
0558 free_arcdev(dev);
0559 return -EIO;
0560 }
0561
0562 static void com90xx_command(struct net_device *dev, int cmd)
0563 {
0564 short ioaddr = dev->base_addr;
0565
0566 arcnet_outb(cmd, ioaddr, COM9026_REG_W_COMMAND);
0567 }
0568
0569 static int com90xx_status(struct net_device *dev)
0570 {
0571 short ioaddr = dev->base_addr;
0572
0573 return arcnet_inb(ioaddr, COM9026_REG_R_STATUS);
0574 }
0575
0576 static void com90xx_setmask(struct net_device *dev, int mask)
0577 {
0578 short ioaddr = dev->base_addr;
0579
0580 arcnet_outb(mask, ioaddr, COM9026_REG_W_INTMASK);
0581 }
0582
0583
0584
0585
0586
0587
0588
0589
0590 static int com90xx_reset(struct net_device *dev, int really_reset)
0591 {
0592 struct arcnet_local *lp = netdev_priv(dev);
0593 short ioaddr = dev->base_addr;
0594
0595 arc_printk(D_INIT, dev, "Resetting (status=%02Xh)\n",
0596 arcnet_inb(ioaddr, COM9026_REG_R_STATUS));
0597
0598 if (really_reset) {
0599
0600 arcnet_inb(ioaddr, COM9026_REG_R_RESET);
0601 mdelay(RESETtime);
0602 }
0603
0604 arcnet_outb(CFLAGScmd | RESETclear, ioaddr, COM9026_REG_W_COMMAND);
0605 arcnet_outb(CFLAGScmd | CONFIGclear, ioaddr, COM9026_REG_W_COMMAND);
0606
0607 #if 0
0608
0609 arcnet_outb(arcnet_inb(ioaddr, COM9026_REG_RW_CONFIG) | ENABLE16flag,
0610 ioaddr, COM9026_REG_RW_CONFIG);
0611 #endif
0612
0613
0614 if (arcnet_readb(lp->mem_start, COM9026_REG_R_STATUS) != TESTvalue) {
0615 if (really_reset)
0616 arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
0617 return 1;
0618 }
0619
0620 arcnet_outb(CONFIGcmd | EXTconf, ioaddr, COM9026_REG_W_COMMAND);
0621
0622
0623 if (BUGLVL(D_DURING))
0624 memset_io(lp->mem_start, 0x42, 2048);
0625
0626
0627 return 0;
0628 }
0629
0630 static void com90xx_copy_to_card(struct net_device *dev, int bufnum,
0631 int offset, void *buf, int count)
0632 {
0633 struct arcnet_local *lp = netdev_priv(dev);
0634 void __iomem *memaddr = lp->mem_start + bufnum * 512 + offset;
0635
0636 TIME(dev, "memcpy_toio", count, memcpy_toio(memaddr, buf, count));
0637 }
0638
0639 static void com90xx_copy_from_card(struct net_device *dev, int bufnum,
0640 int offset, void *buf, int count)
0641 {
0642 struct arcnet_local *lp = netdev_priv(dev);
0643 void __iomem *memaddr = lp->mem_start + bufnum * 512 + offset;
0644
0645 TIME(dev, "memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
0646 }
0647
0648 MODULE_LICENSE("GPL");
0649
0650 static int __init com90xx_init(void)
0651 {
0652 if (irq == 2)
0653 irq = 9;
0654 com90xx_probe();
0655 if (!numcards)
0656 return -EIO;
0657 return 0;
0658 }
0659
0660 static void __exit com90xx_exit(void)
0661 {
0662 struct net_device *dev;
0663 struct arcnet_local *lp;
0664 int count;
0665
0666 for (count = 0; count < numcards; count++) {
0667 dev = cards[count];
0668 lp = netdev_priv(dev);
0669
0670 unregister_netdev(dev);
0671 free_irq(dev->irq, dev);
0672 iounmap(lp->mem_start);
0673 release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
0674 release_mem_region(dev->mem_start,
0675 dev->mem_end - dev->mem_start + 1);
0676 free_arcdev(dev);
0677 }
0678 }
0679
0680 module_init(com90xx_init);
0681 module_exit(com90xx_exit);
0682
0683 #ifndef MODULE
0684 static int __init com90xx_setup(char *s)
0685 {
0686 int ints[8];
0687
0688 s = get_options(s, 8, ints);
0689 if (!ints[0] && !*s) {
0690 pr_notice("Disabled\n");
0691 return 1;
0692 }
0693
0694 switch (ints[0]) {
0695 default:
0696 pr_err("Too many arguments\n");
0697 fallthrough;
0698 case 3:
0699 shmem = ints[3];
0700 fallthrough;
0701 case 2:
0702 irq = ints[2];
0703 fallthrough;
0704 case 1:
0705 io = ints[1];
0706 }
0707
0708 if (*s)
0709 snprintf(device, sizeof(device), "%s", s);
0710
0711 return 1;
0712 }
0713
0714 __setup("com90xx=", com90xx_setup);
0715 #endif