arc-rimi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/bootmem.h>
  35. #include <linux/init.h>
  36. #include <asm/io.h>
  37. #include <linux/arcdevice.h>
  38. #define VERSION "arcnet: RIM I (entirely mem-mapped) support\n"
  39. /* Internal function declarations */
  40. static int arcrimi_probe(struct net_device *dev);
  41. static int arcrimi_found(struct net_device *dev);
  42. static void arcrimi_command(struct net_device *dev, int command);
  43. static int arcrimi_status(struct net_device *dev);
  44. static void arcrimi_setmask(struct net_device *dev, int mask);
  45. static int arcrimi_reset(struct net_device *dev, int really_reset);
  46. static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
  47. void *buf, int count);
  48. static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
  49. void *buf, int count);
  50. /* Handy defines for ARCnet specific stuff */
  51. /* Amount of I/O memory used by the card */
  52. #define BUFFER_SIZE (512)
  53. #define MIRROR_SIZE (BUFFER_SIZE*4)
  54. /* COM 9026 controller chip --> ARCnet register addresses */
  55. #define _INTMASK (ioaddr+0) /* writable */
  56. #define _STATUS (ioaddr+0) /* readable */
  57. #define _COMMAND (ioaddr+1) /* writable, returns random vals on read (?) */
  58. #define _RESET (ioaddr+8) /* software reset (on read) */
  59. #define _MEMDATA (ioaddr+12) /* Data port for IO-mapped memory */
  60. #define _ADDR_HI (ioaddr+15) /* Control registers for said */
  61. #define _ADDR_LO (ioaddr+14)
  62. #define _CONFIG (ioaddr+2) /* Configuration register */
  63. #undef ASTATUS
  64. #undef ACOMMAND
  65. #undef AINTMASK
  66. #define ASTATUS() readb(_STATUS)
  67. #define ACOMMAND(cmd) writeb((cmd),_COMMAND)
  68. #define AINTMASK(msk) writeb((msk),_INTMASK)
  69. #define SETCONF() writeb(lp->config,_CONFIG)
  70. /*
  71. * We cannot probe for a RIM I card; one reason is I don't know how to reset
  72. * them. In fact, we can't even get their node ID automatically. So, we
  73. * need to be passed a specific shmem address, IRQ, and node ID.
  74. */
  75. static int __init arcrimi_probe(struct net_device *dev)
  76. {
  77. BUGLVL(D_NORMAL) printk(VERSION);
  78. BUGLVL(D_NORMAL) printk("E-mail me if you actually test the RIM I driver, please!\n");
  79. BUGMSG(D_NORMAL, "Given: node %02Xh, shmem %lXh, irq %d\n",
  80. dev->dev_addr[0], dev->mem_start, dev->irq);
  81. if (dev->mem_start <= 0 || dev->irq <= 0) {
  82. BUGMSG(D_NORMAL, "No autoprobe for RIM I; you "
  83. "must specify the shmem and irq!\n");
  84. return -ENODEV;
  85. }
  86. /*
  87. * Grab the memory region at mem_start for BUFFER_SIZE bytes.
  88. * Later in arcrimi_found() the real size will be determined
  89. * and this reserve will be released and the correct size
  90. * will be taken.
  91. */
  92. if (!request_mem_region(dev->mem_start, BUFFER_SIZE, "arcnet (90xx)")) {
  93. BUGMSG(D_NORMAL, "Card memory already allocated\n");
  94. return -ENODEV;
  95. }
  96. if (dev->dev_addr[0] == 0) {
  97. release_mem_region(dev->mem_start, BUFFER_SIZE);
  98. BUGMSG(D_NORMAL, "You need to specify your card's station "
  99. "ID!\n");
  100. return -ENODEV;
  101. }
  102. return arcrimi_found(dev);
  103. }
  104. /*
  105. * Set up the struct net_device associated with this card. Called after
  106. * probing succeeds.
  107. */
  108. static int __init arcrimi_found(struct net_device *dev)
  109. {
  110. struct arcnet_local *lp;
  111. unsigned long first_mirror, last_mirror, shmem;
  112. int mirror_size;
  113. int err;
  114. /* reserve the irq */
  115. if (request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet (RIM I)", dev)) {
  116. release_mem_region(dev->mem_start, BUFFER_SIZE);
  117. BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
  118. return -ENODEV;
  119. }
  120. shmem = dev->mem_start;
  121. isa_writeb(TESTvalue, shmem);
  122. isa_writeb(dev->dev_addr[0], shmem + 1); /* actually the node ID */
  123. /* find the real shared memory start/end points, including mirrors */
  124. /* guess the actual size of one "memory mirror" - the number of
  125. * bytes between copies of the shared memory. On most cards, it's
  126. * 2k (or there are no mirrors at all) but on some, it's 4k.
  127. */
  128. mirror_size = MIRROR_SIZE;
  129. if (isa_readb(shmem) == TESTvalue
  130. && isa_readb(shmem - mirror_size) != TESTvalue
  131. && isa_readb(shmem - 2 * mirror_size) == TESTvalue)
  132. mirror_size *= 2;
  133. first_mirror = last_mirror = shmem;
  134. while (isa_readb(first_mirror) == TESTvalue)
  135. first_mirror -= mirror_size;
  136. first_mirror += mirror_size;
  137. while (isa_readb(last_mirror) == TESTvalue)
  138. last_mirror += mirror_size;
  139. last_mirror -= mirror_size;
  140. dev->mem_start = first_mirror;
  141. dev->mem_end = last_mirror + MIRROR_SIZE - 1;
  142. /* initialize the rest of the device structure. */
  143. lp = dev->priv;
  144. lp->card_name = "RIM I";
  145. lp->hw.command = arcrimi_command;
  146. lp->hw.status = arcrimi_status;
  147. lp->hw.intmask = arcrimi_setmask;
  148. lp->hw.reset = arcrimi_reset;
  149. lp->hw.owner = THIS_MODULE;
  150. lp->hw.copy_to_card = arcrimi_copy_to_card;
  151. lp->hw.copy_from_card = arcrimi_copy_from_card;
  152. /*
  153. * re-reserve the memory region - arcrimi_probe() alloced this reqion
  154. * but didn't know the real size. Free that region and then re-get
  155. * with the correct size. There is a VERY slim chance this could
  156. * fail.
  157. */
  158. release_mem_region(shmem, BUFFER_SIZE);
  159. if (!request_mem_region(dev->mem_start,
  160. dev->mem_end - dev->mem_start + 1,
  161. "arcnet (90xx)")) {
  162. BUGMSG(D_NORMAL, "Card memory already allocated\n");
  163. goto err_free_irq;
  164. }
  165. lp->mem_start = ioremap(dev->mem_start, dev->mem_end - dev->mem_start + 1);
  166. if (!lp->mem_start) {
  167. BUGMSG(D_NORMAL, "Can't remap device memory!\n");
  168. goto err_release_mem;
  169. }
  170. /* get and check the station ID from offset 1 in shmem */
  171. dev->dev_addr[0] = readb(lp->mem_start + 1);
  172. BUGMSG(D_NORMAL, "ARCnet RIM I: station %02Xh found at IRQ %d, "
  173. "ShMem %lXh (%ld*%d bytes).\n",
  174. dev->dev_addr[0],
  175. dev->irq, dev->mem_start,
  176. (dev->mem_end - dev->mem_start + 1) / mirror_size, mirror_size);
  177. err = register_netdev(dev);
  178. if (err)
  179. goto err_unmap;
  180. return 0;
  181. err_unmap:
  182. iounmap(lp->mem_start);
  183. err_release_mem:
  184. release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
  185. err_free_irq:
  186. free_irq(dev->irq, dev);
  187. return -EIO;
  188. }
  189. /*
  190. * Do a hardware reset on the card, and set up necessary registers.
  191. *
  192. * This should be called as little as possible, because it disrupts the
  193. * token on the network (causes a RECON) and requires a significant delay.
  194. *
  195. * However, it does make sure the card is in a defined state.
  196. */
  197. static int arcrimi_reset(struct net_device *dev, int really_reset)
  198. {
  199. struct arcnet_local *lp = dev->priv;
  200. void __iomem *ioaddr = lp->mem_start + 0x800;
  201. BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS());
  202. if (really_reset) {
  203. writeb(TESTvalue, ioaddr - 0x800); /* fake reset */
  204. return 0;
  205. }
  206. ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */
  207. ACOMMAND(CFLAGScmd | CONFIGclear);
  208. /* enable extended (512-byte) packets */
  209. ACOMMAND(CONFIGcmd | EXTconf);
  210. /* done! return success. */
  211. return 0;
  212. }
  213. static void arcrimi_setmask(struct net_device *dev, int mask)
  214. {
  215. struct arcnet_local *lp = dev->priv;
  216. void __iomem *ioaddr = lp->mem_start + 0x800;
  217. AINTMASK(mask);
  218. }
  219. static int arcrimi_status(struct net_device *dev)
  220. {
  221. struct arcnet_local *lp = dev->priv;
  222. void __iomem *ioaddr = lp->mem_start + 0x800;
  223. return ASTATUS();
  224. }
  225. static void arcrimi_command(struct net_device *dev, int cmd)
  226. {
  227. struct arcnet_local *lp = dev->priv;
  228. void __iomem *ioaddr = lp->mem_start + 0x800;
  229. ACOMMAND(cmd);
  230. }
  231. static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
  232. void *buf, int count)
  233. {
  234. struct arcnet_local *lp = dev->priv;
  235. void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
  236. TIME("memcpy_toio", count, memcpy_toio(memaddr, buf, count));
  237. }
  238. static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
  239. void *buf, int count)
  240. {
  241. struct arcnet_local *lp = dev->priv;
  242. void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
  243. TIME("memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
  244. }
  245. static int node;
  246. static int io; /* use the insmod io= irq= node= options */
  247. static int irq;
  248. static char device[9]; /* use eg. device=arc1 to change name */
  249. module_param(node, int, 0);
  250. module_param(io, int, 0);
  251. module_param(irq, int, 0);
  252. module_param_string(device, device, sizeof(device), 0);
  253. MODULE_LICENSE("GPL");
  254. static struct net_device *my_dev;
  255. static int __init arc_rimi_init(void)
  256. {
  257. struct net_device *dev;
  258. dev = alloc_arcdev(device);
  259. if (!dev)
  260. return -ENOMEM;
  261. if (node && node != 0xff)
  262. dev->dev_addr[0] = node;
  263. dev->mem_start = io;
  264. dev->irq = irq;
  265. if (dev->irq == 2)
  266. dev->irq = 9;
  267. if (arcrimi_probe(dev)) {
  268. free_netdev(dev);
  269. return -EIO;
  270. }
  271. my_dev = dev;
  272. return 0;
  273. }
  274. static void __exit arc_rimi_exit(void)
  275. {
  276. struct net_device *dev = my_dev;
  277. struct arcnet_local *lp = dev->priv;
  278. unregister_netdev(dev);
  279. iounmap(lp->mem_start);
  280. release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
  281. free_irq(dev->irq, dev);
  282. free_netdev(dev);
  283. }
  284. #ifndef MODULE
  285. static int __init arcrimi_setup(char *s)
  286. {
  287. int ints[8];
  288. s = get_options(s, 8, ints);
  289. if (!ints[0])
  290. return 1;
  291. switch (ints[0]) {
  292. default: /* ERROR */
  293. printk("arcrimi: Too many arguments.\n");
  294. case 3: /* Node ID */
  295. node = ints[3];
  296. case 2: /* IRQ */
  297. irq = ints[2];
  298. case 1: /* IO address */
  299. io = ints[1];
  300. }
  301. if (*s)
  302. snprintf(device, sizeof(device), "%s", s);
  303. return 1;
  304. }
  305. __setup("arcrimi=", arcrimi_setup);
  306. #endif /* MODULE */
  307. module_init(arc_rimi_init)
  308. module_exit(arc_rimi_exit)