arc-rimi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 <linux/interrupt.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. BUGLVL(D_NORMAL) printk("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. BUGLVL(D_NORMAL) printk("No autoprobe for RIM I; you "
  83. "must specify the shmem and irq!\n");
  84. return -ENODEV;
  85. }
  86. if (dev->dev_addr[0] == 0) {
  87. BUGLVL(D_NORMAL) printk("You need to specify your card's station "
  88. "ID!\n");
  89. return -ENODEV;
  90. }
  91. /*
  92. * Grab the memory region at mem_start for MIRROR_SIZE bytes.
  93. * Later in arcrimi_found() the real size will be determined
  94. * and this reserve will be released and the correct size
  95. * will be taken.
  96. */
  97. if (!request_mem_region(dev->mem_start, MIRROR_SIZE, "arcnet (90xx)")) {
  98. BUGLVL(D_NORMAL) printk("Card memory already allocated\n");
  99. return -ENODEV;
  100. }
  101. return arcrimi_found(dev);
  102. }
  103. static int check_mirror(unsigned long addr, size_t size)
  104. {
  105. void __iomem *p;
  106. int res = -1;
  107. if (!request_mem_region(addr, size, "arcnet (90xx)"))
  108. return -1;
  109. p = ioremap(addr, size);
  110. if (p) {
  111. if (readb(p) == TESTvalue)
  112. res = 1;
  113. else
  114. res = 0;
  115. iounmap(p);
  116. }
  117. release_mem_region(addr, size);
  118. return res;
  119. }
  120. /*
  121. * Set up the struct net_device associated with this card. Called after
  122. * probing succeeds.
  123. */
  124. static int __init arcrimi_found(struct net_device *dev)
  125. {
  126. struct arcnet_local *lp;
  127. unsigned long first_mirror, last_mirror, shmem;
  128. void __iomem *p;
  129. int mirror_size;
  130. int err;
  131. p = ioremap(dev->mem_start, MIRROR_SIZE);
  132. if (!p) {
  133. release_mem_region(dev->mem_start, MIRROR_SIZE);
  134. BUGMSG(D_NORMAL, "Can't ioremap\n");
  135. return -ENODEV;
  136. }
  137. /* reserve the irq */
  138. if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (RIM I)", dev)) {
  139. iounmap(p);
  140. release_mem_region(dev->mem_start, MIRROR_SIZE);
  141. BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
  142. return -ENODEV;
  143. }
  144. shmem = dev->mem_start;
  145. writeb(TESTvalue, p);
  146. writeb(dev->dev_addr[0], p + 1); /* actually the node ID */
  147. /* find the real shared memory start/end points, including mirrors */
  148. /* guess the actual size of one "memory mirror" - the number of
  149. * bytes between copies of the shared memory. On most cards, it's
  150. * 2k (or there are no mirrors at all) but on some, it's 4k.
  151. */
  152. mirror_size = MIRROR_SIZE;
  153. if (readb(p) == TESTvalue &&
  154. check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 &&
  155. check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1)
  156. mirror_size = 2 * MIRROR_SIZE;
  157. first_mirror = shmem - mirror_size;
  158. while (check_mirror(first_mirror, mirror_size) == 1)
  159. first_mirror -= mirror_size;
  160. first_mirror += mirror_size;
  161. last_mirror = shmem + mirror_size;
  162. while (check_mirror(last_mirror, mirror_size) == 1)
  163. last_mirror += mirror_size;
  164. last_mirror -= mirror_size;
  165. dev->mem_start = first_mirror;
  166. dev->mem_end = last_mirror + MIRROR_SIZE - 1;
  167. /* initialize the rest of the device structure. */
  168. lp = netdev_priv(dev);
  169. lp->card_name = "RIM I";
  170. lp->hw.command = arcrimi_command;
  171. lp->hw.status = arcrimi_status;
  172. lp->hw.intmask = arcrimi_setmask;
  173. lp->hw.reset = arcrimi_reset;
  174. lp->hw.owner = THIS_MODULE;
  175. lp->hw.copy_to_card = arcrimi_copy_to_card;
  176. lp->hw.copy_from_card = arcrimi_copy_from_card;
  177. /*
  178. * re-reserve the memory region - arcrimi_probe() alloced this reqion
  179. * but didn't know the real size. Free that region and then re-get
  180. * with the correct size. There is a VERY slim chance this could
  181. * fail.
  182. */
  183. iounmap(p);
  184. release_mem_region(shmem, MIRROR_SIZE);
  185. if (!request_mem_region(dev->mem_start,
  186. dev->mem_end - dev->mem_start + 1,
  187. "arcnet (90xx)")) {
  188. BUGMSG(D_NORMAL, "Card memory already allocated\n");
  189. goto err_free_irq;
  190. }
  191. lp->mem_start = ioremap(dev->mem_start, dev->mem_end - dev->mem_start + 1);
  192. if (!lp->mem_start) {
  193. BUGMSG(D_NORMAL, "Can't remap device memory!\n");
  194. goto err_release_mem;
  195. }
  196. /* get and check the station ID from offset 1 in shmem */
  197. dev->dev_addr[0] = readb(lp->mem_start + 1);
  198. BUGMSG(D_NORMAL, "ARCnet RIM I: station %02Xh found at IRQ %d, "
  199. "ShMem %lXh (%ld*%d bytes).\n",
  200. dev->dev_addr[0],
  201. dev->irq, dev->mem_start,
  202. (dev->mem_end - dev->mem_start + 1) / mirror_size, mirror_size);
  203. err = register_netdev(dev);
  204. if (err)
  205. goto err_unmap;
  206. return 0;
  207. err_unmap:
  208. iounmap(lp->mem_start);
  209. err_release_mem:
  210. release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
  211. err_free_irq:
  212. free_irq(dev->irq, dev);
  213. return -EIO;
  214. }
  215. /*
  216. * Do a hardware reset on the card, and set up necessary registers.
  217. *
  218. * This should be called as little as possible, because it disrupts the
  219. * token on the network (causes a RECON) and requires a significant delay.
  220. *
  221. * However, it does make sure the card is in a defined state.
  222. */
  223. static int arcrimi_reset(struct net_device *dev, int really_reset)
  224. {
  225. struct arcnet_local *lp = netdev_priv(dev);
  226. void __iomem *ioaddr = lp->mem_start + 0x800;
  227. BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS());
  228. if (really_reset) {
  229. writeb(TESTvalue, ioaddr - 0x800); /* fake reset */
  230. return 0;
  231. }
  232. ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */
  233. ACOMMAND(CFLAGScmd | CONFIGclear);
  234. /* enable extended (512-byte) packets */
  235. ACOMMAND(CONFIGcmd | EXTconf);
  236. /* done! return success. */
  237. return 0;
  238. }
  239. static void arcrimi_setmask(struct net_device *dev, int mask)
  240. {
  241. struct arcnet_local *lp = netdev_priv(dev);
  242. void __iomem *ioaddr = lp->mem_start + 0x800;
  243. AINTMASK(mask);
  244. }
  245. static int arcrimi_status(struct net_device *dev)
  246. {
  247. struct arcnet_local *lp = netdev_priv(dev);
  248. void __iomem *ioaddr = lp->mem_start + 0x800;
  249. return ASTATUS();
  250. }
  251. static void arcrimi_command(struct net_device *dev, int cmd)
  252. {
  253. struct arcnet_local *lp = netdev_priv(dev);
  254. void __iomem *ioaddr = lp->mem_start + 0x800;
  255. ACOMMAND(cmd);
  256. }
  257. static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
  258. void *buf, int count)
  259. {
  260. struct arcnet_local *lp = netdev_priv(dev);
  261. void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
  262. TIME("memcpy_toio", count, memcpy_toio(memaddr, buf, count));
  263. }
  264. static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, int offset,
  265. void *buf, int count)
  266. {
  267. struct arcnet_local *lp = netdev_priv(dev);
  268. void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
  269. TIME("memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
  270. }
  271. static int node;
  272. static int io; /* use the insmod io= irq= node= options */
  273. static int irq;
  274. static char device[9]; /* use eg. device=arc1 to change name */
  275. module_param(node, int, 0);
  276. module_param(io, int, 0);
  277. module_param(irq, int, 0);
  278. module_param_string(device, device, sizeof(device), 0);
  279. MODULE_LICENSE("GPL");
  280. static struct net_device *my_dev;
  281. static int __init arc_rimi_init(void)
  282. {
  283. struct net_device *dev;
  284. dev = alloc_arcdev(device);
  285. if (!dev)
  286. return -ENOMEM;
  287. if (node && node != 0xff)
  288. dev->dev_addr[0] = node;
  289. dev->mem_start = io;
  290. dev->irq = irq;
  291. if (dev->irq == 2)
  292. dev->irq = 9;
  293. if (arcrimi_probe(dev)) {
  294. free_netdev(dev);
  295. return -EIO;
  296. }
  297. my_dev = dev;
  298. return 0;
  299. }
  300. static void __exit arc_rimi_exit(void)
  301. {
  302. struct net_device *dev = my_dev;
  303. struct arcnet_local *lp = netdev_priv(dev);
  304. unregister_netdev(dev);
  305. iounmap(lp->mem_start);
  306. release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
  307. free_irq(dev->irq, dev);
  308. free_netdev(dev);
  309. }
  310. #ifndef MODULE
  311. static int __init arcrimi_setup(char *s)
  312. {
  313. int ints[8];
  314. s = get_options(s, 8, ints);
  315. if (!ints[0])
  316. return 1;
  317. switch (ints[0]) {
  318. default: /* ERROR */
  319. printk("arcrimi: Too many arguments.\n");
  320. case 3: /* Node ID */
  321. node = ints[3];
  322. case 2: /* IRQ */
  323. irq = ints[2];
  324. case 1: /* IO address */
  325. io = ints[1];
  326. }
  327. if (*s)
  328. snprintf(device, sizeof(device), "%s", s);
  329. return 1;
  330. }
  331. __setup("arcrimi=", arcrimi_setup);
  332. #endif /* MODULE */
  333. module_init(arc_rimi_init)
  334. module_exit(arc_rimi_exit)