arc-rimi.c 10 KB

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