PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/arcnet/arc-rimi.c

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