Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Linux ARCnet driver - "RIM I" (entirely mem-mapped) cards
0003  *
0004  * Written 1994-1999 by Avery Pennarun.
0005  * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
0006  * Derived from skeleton.c by Donald Becker.
0007  *
0008  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
0009  *  for sponsoring the further development of this driver.
0010  *
0011  * **********************
0012  *
0013  * The original copyright of skeleton.c was as follows:
0014  *
0015  * skeleton.c Written 1993 by Donald Becker.
0016  * Copyright 1993 United States Government as represented by the
0017  * Director, National Security Agency.  This software may only be used
0018  * and distributed according to the terms of the GNU General Public License as
0019  * modified by SRC, incorporated herein by reference.
0020  *
0021  * **********************
0022  *
0023  * For more details, see drivers/net/arcnet.c
0024  *
0025  * **********************
0026  */
0027 
0028 #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
0029 
0030 #include <linux/kernel.h>
0031 #include <linux/module.h>
0032 #include <linux/moduleparam.h>
0033 #include <linux/ioport.h>
0034 #include <linux/delay.h>
0035 #include <linux/netdevice.h>
0036 #include <linux/memblock.h>
0037 #include <linux/init.h>
0038 #include <linux/interrupt.h>
0039 #include <linux/io.h>
0040 
0041 #include "arcdevice.h"
0042 #include "com9026.h"
0043 
0044 /* Internal function declarations */
0045 
0046 static int arcrimi_probe(struct net_device *dev);
0047 static int arcrimi_found(struct net_device *dev);
0048 static void arcrimi_command(struct net_device *dev, int command);
0049 static int arcrimi_status(struct net_device *dev);
0050 static void arcrimi_setmask(struct net_device *dev, int mask);
0051 static int arcrimi_reset(struct net_device *dev, int really_reset);
0052 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
0053                  void *buf, int count);
0054 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum,
0055                    int offset, void *buf, int count);
0056 
0057 /* Handy defines for ARCnet specific stuff */
0058 
0059 /* Amount of I/O memory used by the card */
0060 #define BUFFER_SIZE (512)
0061 #define MIRROR_SIZE (BUFFER_SIZE * 4)
0062 
0063 /* We cannot probe for a RIM I card; one reason is I don't know how to reset
0064  * them.  In fact, we can't even get their node ID automatically.  So, we
0065  * need to be passed a specific shmem address, IRQ, and node ID.
0066  */
0067 static int __init arcrimi_probe(struct net_device *dev)
0068 {
0069     if (BUGLVL(D_NORMAL)) {
0070         pr_info("%s\n", "RIM I (entirely mem-mapped) support");
0071         pr_info("E-mail me if you actually test the RIM I driver, please!\n");
0072         pr_info("Given: node %02Xh, shmem %lXh, irq %d\n",
0073             dev->dev_addr[0], dev->mem_start, dev->irq);
0074     }
0075 
0076     if (dev->mem_start <= 0 || dev->irq <= 0) {
0077         if (BUGLVL(D_NORMAL))
0078             pr_err("No autoprobe for RIM I; you must specify the shmem and irq!\n");
0079         return -ENODEV;
0080     }
0081     if (dev->dev_addr[0] == 0) {
0082         if (BUGLVL(D_NORMAL))
0083             pr_err("You need to specify your card's station ID!\n");
0084         return -ENODEV;
0085     }
0086     /* Grab the memory region at mem_start for MIRROR_SIZE bytes.
0087      * Later in arcrimi_found() the real size will be determined
0088      * and this reserve will be released and the correct size
0089      * will be taken.
0090      */
0091     if (!request_mem_region(dev->mem_start, MIRROR_SIZE, "arcnet (90xx)")) {
0092         if (BUGLVL(D_NORMAL))
0093             pr_notice("Card memory already allocated\n");
0094         return -ENODEV;
0095     }
0096     return arcrimi_found(dev);
0097 }
0098 
0099 static int check_mirror(unsigned long addr, size_t size)
0100 {
0101     void __iomem *p;
0102     int res = -1;
0103 
0104     if (!request_mem_region(addr, size, "arcnet (90xx)"))
0105         return -1;
0106 
0107     p = ioremap(addr, size);
0108     if (p) {
0109         if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue)
0110             res = 1;
0111         else
0112             res = 0;
0113         iounmap(p);
0114     }
0115 
0116     release_mem_region(addr, size);
0117     return res;
0118 }
0119 
0120 /* Set up the struct net_device associated with this card.
0121  * Called after probing succeeds.
0122  */
0123 static int __init arcrimi_found(struct net_device *dev)
0124 {
0125     struct arcnet_local *lp;
0126     unsigned long first_mirror, last_mirror, shmem;
0127     void __iomem *p;
0128     int mirror_size;
0129     int err;
0130 
0131     p = ioremap(dev->mem_start, MIRROR_SIZE);
0132     if (!p) {
0133         release_mem_region(dev->mem_start, MIRROR_SIZE);
0134         arc_printk(D_NORMAL, dev, "Can't ioremap\n");
0135         return -ENODEV;
0136     }
0137 
0138     /* reserve the irq */
0139     if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (RIM I)", dev)) {
0140         iounmap(p);
0141         release_mem_region(dev->mem_start, MIRROR_SIZE);
0142         arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
0143         return -ENODEV;
0144     }
0145 
0146     shmem = dev->mem_start;
0147     arcnet_writeb(TESTvalue, p, COM9026_REG_W_INTMASK);
0148     arcnet_writeb(TESTvalue, p, COM9026_REG_W_COMMAND);
0149                     /* actually the station/node ID */
0150 
0151     /* find the real shared memory start/end points, including mirrors */
0152 
0153     /* guess the actual size of one "memory mirror" - the number of
0154      * bytes between copies of the shared memory.  On most cards, it's
0155      * 2k (or there are no mirrors at all) but on some, it's 4k.
0156      */
0157     mirror_size = MIRROR_SIZE;
0158     if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue &&
0159         check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 &&
0160         check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1)
0161         mirror_size = 2 * MIRROR_SIZE;
0162 
0163     first_mirror = shmem - mirror_size;
0164     while (check_mirror(first_mirror, mirror_size) == 1)
0165         first_mirror -= mirror_size;
0166     first_mirror += mirror_size;
0167 
0168     last_mirror = shmem + mirror_size;
0169     while (check_mirror(last_mirror, mirror_size) == 1)
0170         last_mirror += mirror_size;
0171     last_mirror -= mirror_size;
0172 
0173     dev->mem_start = first_mirror;
0174     dev->mem_end = last_mirror + MIRROR_SIZE - 1;
0175 
0176     /* initialize the rest of the device structure. */
0177 
0178     lp = netdev_priv(dev);
0179     lp->card_name = "RIM I";
0180     lp->hw.command = arcrimi_command;
0181     lp->hw.status = arcrimi_status;
0182     lp->hw.intmask = arcrimi_setmask;
0183     lp->hw.reset = arcrimi_reset;
0184     lp->hw.owner = THIS_MODULE;
0185     lp->hw.copy_to_card = arcrimi_copy_to_card;
0186     lp->hw.copy_from_card = arcrimi_copy_from_card;
0187 
0188     /* re-reserve the memory region - arcrimi_probe() alloced this reqion
0189      * but didn't know the real size.  Free that region and then re-get
0190      * with the correct size.  There is a VERY slim chance this could
0191      * fail.
0192      */
0193     iounmap(p);
0194     release_mem_region(shmem, MIRROR_SIZE);
0195     if (!request_mem_region(dev->mem_start,
0196                 dev->mem_end - dev->mem_start + 1,
0197                 "arcnet (90xx)")) {
0198         arc_printk(D_NORMAL, dev, "Card memory already allocated\n");
0199         goto err_free_irq;
0200     }
0201 
0202     lp->mem_start = ioremap(dev->mem_start,
0203                 dev->mem_end - dev->mem_start + 1);
0204     if (!lp->mem_start) {
0205         arc_printk(D_NORMAL, dev, "Can't remap device memory!\n");
0206         goto err_release_mem;
0207     }
0208 
0209     /* get and check the station ID from offset 1 in shmem */
0210     arcnet_set_addr(dev, arcnet_readb(lp->mem_start,
0211                       COM9026_REG_R_STATION));
0212 
0213     arc_printk(D_NORMAL, dev, "ARCnet RIM I: station %02Xh found at IRQ %d, ShMem %lXh (%ld*%d bytes)\n",
0214            dev->dev_addr[0],
0215            dev->irq, dev->mem_start,
0216            (dev->mem_end - dev->mem_start + 1) / mirror_size,
0217            mirror_size);
0218 
0219     err = register_netdev(dev);
0220     if (err)
0221         goto err_unmap;
0222 
0223     return 0;
0224 
0225 err_unmap:
0226     iounmap(lp->mem_start);
0227 err_release_mem:
0228     release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
0229 err_free_irq:
0230     free_irq(dev->irq, dev);
0231     return -EIO;
0232 }
0233 
0234 /* Do a hardware reset on the card, and set up necessary registers.
0235  *
0236  * This should be called as little as possible, because it disrupts the
0237  * token on the network (causes a RECON) and requires a significant delay.
0238  *
0239  * However, it does make sure the card is in a defined state.
0240  */
0241 static int arcrimi_reset(struct net_device *dev, int really_reset)
0242 {
0243     struct arcnet_local *lp = netdev_priv(dev);
0244     void __iomem *ioaddr = lp->mem_start + 0x800;
0245 
0246     arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
0247            dev->name, arcnet_readb(ioaddr, COM9026_REG_R_STATUS));
0248 
0249     if (really_reset) {
0250         arcnet_writeb(TESTvalue, ioaddr, -0x800);   /* fake reset */
0251         return 0;
0252     }
0253     /* clear flags & end reset */
0254     arcnet_writeb(CFLAGScmd | RESETclear, ioaddr, COM9026_REG_W_COMMAND);
0255     arcnet_writeb(CFLAGScmd | CONFIGclear, ioaddr, COM9026_REG_W_COMMAND);
0256 
0257     /* enable extended (512-byte) packets */
0258     arcnet_writeb(CONFIGcmd | EXTconf, ioaddr, COM9026_REG_W_COMMAND);
0259 
0260     /* done!  return success. */
0261     return 0;
0262 }
0263 
0264 static void arcrimi_setmask(struct net_device *dev, int mask)
0265 {
0266     struct arcnet_local *lp = netdev_priv(dev);
0267     void __iomem *ioaddr = lp->mem_start + 0x800;
0268 
0269     arcnet_writeb(mask, ioaddr, COM9026_REG_W_INTMASK);
0270 }
0271 
0272 static int arcrimi_status(struct net_device *dev)
0273 {
0274     struct arcnet_local *lp = netdev_priv(dev);
0275     void __iomem *ioaddr = lp->mem_start + 0x800;
0276 
0277     return arcnet_readb(ioaddr, COM9026_REG_R_STATUS);
0278 }
0279 
0280 static void arcrimi_command(struct net_device *dev, int cmd)
0281 {
0282     struct arcnet_local *lp = netdev_priv(dev);
0283     void __iomem *ioaddr = lp->mem_start + 0x800;
0284 
0285     arcnet_writeb(cmd, ioaddr, COM9026_REG_W_COMMAND);
0286 }
0287 
0288 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
0289                  void *buf, int count)
0290 {
0291     struct arcnet_local *lp = netdev_priv(dev);
0292     void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
0293 
0294     TIME(dev, "memcpy_toio", count, memcpy_toio(memaddr, buf, count));
0295 }
0296 
0297 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum,
0298                    int offset, void *buf, int count)
0299 {
0300     struct arcnet_local *lp = netdev_priv(dev);
0301     void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
0302 
0303     TIME(dev, "memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
0304 }
0305 
0306 static int node;
0307 static int io;          /* use the insmod io= irq= node= options */
0308 static int irq;
0309 static char device[9];      /* use eg. device=arc1 to change name */
0310 
0311 module_param(node, int, 0);
0312 module_param(io, int, 0);
0313 module_param(irq, int, 0);
0314 module_param_string(device, device, sizeof(device), 0);
0315 MODULE_LICENSE("GPL");
0316 
0317 static struct net_device *my_dev;
0318 
0319 static int __init arc_rimi_init(void)
0320 {
0321     struct net_device *dev;
0322 
0323     dev = alloc_arcdev(device);
0324     if (!dev)
0325         return -ENOMEM;
0326 
0327     if (node && node != 0xff)
0328         arcnet_set_addr(dev, node);
0329 
0330     dev->mem_start = io;
0331     dev->irq = irq;
0332     if (dev->irq == 2)
0333         dev->irq = 9;
0334 
0335     if (arcrimi_probe(dev)) {
0336         free_arcdev(dev);
0337         return -EIO;
0338     }
0339 
0340     my_dev = dev;
0341     return 0;
0342 }
0343 
0344 static void __exit arc_rimi_exit(void)
0345 {
0346     struct net_device *dev = my_dev;
0347     struct arcnet_local *lp = netdev_priv(dev);
0348 
0349     unregister_netdev(dev);
0350     iounmap(lp->mem_start);
0351     release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
0352     free_irq(dev->irq, dev);
0353     free_arcdev(dev);
0354 }
0355 
0356 #ifndef MODULE
0357 static int __init arcrimi_setup(char *s)
0358 {
0359     int ints[8];
0360 
0361     s = get_options(s, 8, ints);
0362     if (!ints[0])
0363         return 1;
0364     switch (ints[0]) {
0365     default:        /* ERROR */
0366         pr_err("Too many arguments\n");
0367         fallthrough;
0368     case 3:     /* Node ID */
0369         node = ints[3];
0370         fallthrough;
0371     case 2:     /* IRQ */
0372         irq = ints[2];
0373         fallthrough;
0374     case 1:     /* IO address */
0375         io = ints[1];
0376     }
0377     if (*s)
0378         snprintf(device, sizeof(device), "%s", s);
0379     return 1;
0380 }
0381 __setup("arcrimi=", arcrimi_setup);
0382 #endif              /* MODULE */
0383 
0384 module_init(arc_rimi_init)
0385 module_exit(arc_rimi_exit)