1 /* 2 * Linux ARCnet driver - "RIM I" (entirely mem-mapped) cards 3 * 4 * Written 1994-1999 by Avery Pennarun. 5 * Written 1999-2000 by Martin Mares <mj@ucw.cz>. 6 * Derived from skeleton.c by Donald Becker. 7 * 8 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com) 9 * for sponsoring the further development of this driver. 10 * 11 * ********************** 12 * 13 * The original copyright of skeleton.c was as follows: 14 * 15 * skeleton.c Written 1993 by Donald Becker. 16 * Copyright 1993 United States Government as represented by the 17 * Director, National Security Agency. This software may only be used 18 * and distributed according to the terms of the GNU General Public License as 19 * modified by SRC, incorporated herein by reference. 20 * 21 * ********************** 22 * 23 * For more details, see drivers/net/arcnet.c 24 * 25 * ********************** 26 */ 27 #include <linux/kernel.h> 28 #include <linux/module.h> 29 #include <linux/moduleparam.h> 30 #include <linux/ioport.h> 31 #include <linux/delay.h> 32 #include <linux/netdevice.h> 33 #include <linux/bootmem.h> 34 #include <linux/init.h> 35 #include <asm/io.h> 36 #include <linux/arcdevice.h> 37 38 39 #define VERSION "arcnet: RIM I (entirely mem-mapped) support\n" 40 41 42 /* Internal function declarations */ 43 44 static int arcrimi_probe(struct net_device *dev); 45 static int arcrimi_found(struct net_device *dev); 46 static void arcrimi_command(struct net_device *dev, int command); 47 static int arcrimi_status(struct net_device *dev); 48 static void arcrimi_setmask(struct net_device *dev, int mask); 49 static int arcrimi_reset(struct net_device *dev, int really_reset); 50 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset, 51 void *buf, int count); 52 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset, 53 void *buf, int count); 54 55 /* Handy defines for ARCnet specific stuff */ 56 57 /* Amount of I/O memory used by the card */ 58 #define BUFFER_SIZE (512) 59 #define MIRROR_SIZE (BUFFER_SIZE*4) 60 61 /* COM 9026 controller chip --> ARCnet register addresses */ 62 #define _INTMASK (ioaddr+0) /* writable */ 63 #define _STATUS (ioaddr+0) /* readable */ 64 #define _COMMAND (ioaddr+1) /* writable, returns random vals on read (?) */ 65 #define _RESET (ioaddr+8) /* software reset (on read) */ 66 #define _MEMDATA (ioaddr+12) /* Data port for IO-mapped memory */ 67 #define _ADDR_HI (ioaddr+15) /* Control registers for said */ 68 #define _ADDR_LO (ioaddr+14) 69 #define _CONFIG (ioaddr+2) /* Configuration register */ 70 71 #undef ASTATUS 72 #undef ACOMMAND 73 #undef AINTMASK 74 75 #define ASTATUS() readb(_STATUS) 76 #define ACOMMAND(cmd) writeb((cmd),_COMMAND) 77 #define AINTMASK(msk) writeb((msk),_INTMASK) 78 #define SETCONF() writeb(lp->config,_CONFIG) 79 80 81 /* 82 * We cannot probe for a RIM I card; one reason is I don't know how to reset 83 * them. In fact, we can't even get their node ID automatically. So, we 84 * need to be passed a specific shmem address, IRQ, and node ID. 85 */ 86 static int __init arcrimi_probe(struct net_device *dev) 87 { 88 BUGLVL(D_NORMAL) printk(VERSION); 89 BUGLVL(D_NORMAL) printk("E-mail me if you actually test the RIM I driver, please!\n"); 90 91 BUGMSG(D_NORMAL, "Given: node %02Xh, shmem %lXh, irq %d\n", 92 dev->dev_addr[0], dev->mem_start, dev->irq); 93 94 if (dev->mem_start <= 0 || dev->irq <= 0) { 95 BUGMSG(D_NORMAL, "No autoprobe for RIM I; you " 96 "must specify the shmem and irq!\n"); 97 return -ENODEV; 98 } 99 if (dev->dev_addr[0] == 0) { 100 BUGMSG(D_NORMAL, "You need to specify your card's station " 101 "ID!\n"); 102 return -ENODEV; 103 } 104 /* 105 * Grab the memory region at mem_start for MIRROR_SIZE bytes. 106 * Later in arcrimi_found() the real size will be determined 107 * and this reserve will be released and the correct size 108 * will be taken. 109 */ 110 if (!request_mem_region(dev->mem_start, MIRROR_SIZE, "arcnet (90xx)")) { 111 BUGMSG(D_NORMAL, "Card memory already allocated\n"); 112 return -ENODEV; 113 } 114 return arcrimi_found(dev); 115 } 116 117 static int check_mirror(unsigned long addr, size_t size) 118 { 119 void __iomem *p; 120 int res = -1; 121 122 if (!request_mem_region(addr, size, "arcnet (90xx)")) 123 return -1; 124 125 p = ioremap(addr, size); 126 if (p) { 127 if (readb(p) == TESTvalue) 128 res = 1; 129 else 130 res = 0; 131 iounmap(p); 132 } 133 134 release_mem_region(addr, size); 135 return res; 136 } 137 138 /* 139 * Set up the struct net_device associated with this card. Called after 140 * probing succeeds. 141 */ 142 static int __init arcrimi_found(struct net_device *dev) 143 { 144 struct arcnet_local *lp; 145 unsigned long first_mirror, last_mirror, shmem; 146 void __iomem *p; 147 int mirror_size; 148 int err; 149 150 p = ioremap(dev->mem_start, MIRROR_SIZE); 151 if (!p) { 152 release_mem_region(dev->mem_start, MIRROR_SIZE); 153 BUGMSG(D_NORMAL, "Can't ioremap\n"); 154 return -ENODEV; 155 } 156 157 /* reserve the irq */ 158 if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (RIM I)", dev)) { 159 iounmap(p); 160 release_mem_region(dev->mem_start, MIRROR_SIZE); 161 BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq); 162 return -ENODEV; 163 } 164 165 shmem = dev->mem_start; 166 writeb(TESTvalue, p); 167 writeb(dev->dev_addr[0], p + 1); /* actually the node ID */ 168 169 /* find the real shared memory start/end points, including mirrors */ 170 171 /* guess the actual size of one "memory mirror" - the number of 172 * bytes between copies of the shared memory. On most cards, it's 173 * 2k (or there are no mirrors at all) but on some, it's 4k. 174 */ 175 mirror_size = MIRROR_SIZE; 176 if (readb(p) == TESTvalue && 177 check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 && 178 check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1) 179 mirror_size = 2 * MIRROR_SIZE; 180 181 first_mirror = shmem - mirror_size; 182 while (check_mirror(first_mirror, mirror_size) == 1) 183 first_mirror -= mirror_size; 184 first_mirror += mirror_size; 185 186 last_mirror = shmem + mirror_size; 187 while (check_mirror(last_mirror, mirror_size) == 1) 188 last_mirror += mirror_size; 189 last_mirror -= mirror_size; 190 191 dev->mem_start = first_mirror; 192 dev->mem_end = last_mirror + MIRROR_SIZE - 1; 193 194 /* initialize the rest of the device structure. */ 195 196 lp = netdev_priv(dev); 197 lp->card_name = "RIM I"; 198 lp->hw.command = arcrimi_command; 199 lp->hw.status = arcrimi_status; 200 lp->hw.intmask = arcrimi_setmask; 201 lp->hw.reset = arcrimi_reset; 202 lp->hw.owner = THIS_MODULE; 203 lp->hw.copy_to_card = arcrimi_copy_to_card; 204 lp->hw.copy_from_card = arcrimi_copy_from_card; 205 206 /* 207 * re-reserve the memory region - arcrimi_probe() alloced this reqion 208 * but didn't know the real size. Free that region and then re-get 209 * with the correct size. There is a VERY slim chance this could 210 * fail. 211 */ 212 iounmap(p); 213 release_mem_region(shmem, MIRROR_SIZE); 214 if (!request_mem_region(dev->mem_start, 215 dev->mem_end - dev->mem_start + 1, 216 "arcnet (90xx)")) { 217 BUGMSG(D_NORMAL, "Card memory already allocated\n"); 218 goto err_free_irq; 219 } 220 221 lp->mem_start = ioremap(dev->mem_start, dev->mem_end - dev->mem_start + 1); 222 if (!lp->mem_start) { 223 BUGMSG(D_NORMAL, "Can't remap device memory!\n"); 224 goto err_release_mem; 225 } 226 227 /* get and check the station ID from offset 1 in shmem */ 228 dev->dev_addr[0] = readb(lp->mem_start + 1); 229 230 BUGMSG(D_NORMAL, "ARCnet RIM I: station %02Xh found at IRQ %d, " 231 "ShMem %lXh (%ld*%d bytes).\n", 232 dev->dev_addr[0], 233 dev->irq, dev->mem_start, 234 (dev->mem_end - dev->mem_start + 1) / mirror_size, mirror_size); 235 236 err = register_netdev(dev); 237 if (err) 238 goto err_unmap; 239 240 return 0; 241 242 err_unmap: 243 iounmap(lp->mem_start); 244 err_release_mem: 245 release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1); 246 err_free_irq: 247 free_irq(dev->irq, dev); 248 return -EIO; 249 } 250 251 252 /* 253 * Do a hardware reset on the card, and set up necessary registers. 254 * 255 * This should be called as little as possible, because it disrupts the 256 * token on the network (causes a RECON) and requires a significant delay. 257 * 258 * However, it does make sure the card is in a defined state. 259 */ 260 static int arcrimi_reset(struct net_device *dev, int really_reset) 261 { 262 struct arcnet_local *lp = netdev_priv(dev); 263 void __iomem *ioaddr = lp->mem_start + 0x800; 264 265 BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS()); 266 267 if (really_reset) { 268 writeb(TESTvalue, ioaddr - 0x800); /* fake reset */ 269 return 0; 270 } 271 ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */ 272 ACOMMAND(CFLAGScmd | CONFIGclear); 273 274 /* enable extended (512-byte) packets */ 275 ACOMMAND(CONFIGcmd | EXTconf); 276 277 /* done! return success. */ 278 return 0; 279 } 280 281 static void arcrimi_setmask(struct net_device *dev, int mask) 282 { 283 struct arcnet_local *lp = netdev_priv(dev); 284 void __iomem *ioaddr = lp->mem_start + 0x800; 285 286 AINTMASK(mask); 287 } 288 289 static int arcrimi_status(struct net_device *dev) 290 { 291 struct arcnet_local *lp = netdev_priv(dev); 292 void __iomem *ioaddr = lp->mem_start + 0x800; 293 294 return ASTATUS(); 295 } 296 297 static void arcrimi_command(struct net_device *dev, int cmd) 298 { 299 struct arcnet_local *lp = netdev_priv(dev); 300 void __iomem *ioaddr = lp->mem_start + 0x800; 301 302 ACOMMAND(cmd); 303 } 304 305 static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset, 306 void *buf, int count) 307 { 308 struct arcnet_local *lp = netdev_priv(dev); 309 void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset; 310 TIME("memcpy_toio", count, memcpy_toio(memaddr, buf, count)); 311 } 312 313 314 static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset, 315 void *buf, int count) 316 { 317 struct arcnet_local *lp = netdev_priv(dev); 318 void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset; 319 TIME("memcpy_fromio", count, memcpy_fromio(buf, memaddr, count)); 320 } 321 322 static int node; 323 static int io; /* use the insmod io= irq= node= options */ 324 static int irq; 325 static char device[9]; /* use eg. device=arc1 to change name */ 326 327 module_param(node, int, 0); 328 module_param(io, int, 0); 329 module_param(irq, int, 0); 330 module_param_string(device, device, sizeof(device), 0); 331 MODULE_LICENSE("GPL"); 332 333 static struct net_device *my_dev; 334 335 static int __init arc_rimi_init(void) 336 { 337 struct net_device *dev; 338 339 dev = alloc_arcdev(device); 340 if (!dev) 341 return -ENOMEM; 342 343 if (node && node != 0xff) 344 dev->dev_addr[0] = node; 345 346 dev->mem_start = io; 347 dev->irq = irq; 348 if (dev->irq == 2) 349 dev->irq = 9; 350 351 if (arcrimi_probe(dev)) { 352 free_netdev(dev); 353 return -EIO; 354 } 355 356 my_dev = dev; 357 return 0; 358 } 359 360 static void __exit arc_rimi_exit(void) 361 { 362 struct net_device *dev = my_dev; 363 struct arcnet_local *lp = netdev_priv(dev); 364 365 unregister_netdev(dev); 366 iounmap(lp->mem_start); 367 release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1); 368 free_irq(dev->irq, dev); 369 free_netdev(dev); 370 } 371 372 #ifndef MODULE 373 static int __init arcrimi_setup(char *s) 374 { 375 int ints[8]; 376 s = get_options(s, 8, ints); 377 if (!ints[0]) 378 return 1; 379 switch (ints[0]) { 380 default: /* ERROR */ 381 printk("arcrimi: Too many arguments.\n"); 382 case 3: /* Node ID */ 383 node = ints[3]; 384 case 2: /* IRQ */ 385 irq = ints[2]; 386 case 1: /* IO address */ 387 io = ints[1]; 388 } 389 if (*s) 390 snprintf(device, sizeof(device), "%s", s); 391 return 1; 392 } 393 __setup("arcrimi=", arcrimi_setup); 394 #endif /* MODULE */ 395 396 module_init(arc_rimi_init) 397 module_exit(arc_rimi_exit) 398