com90io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Linux ARCnet driver - COM90xx chipset (IO-mapped buffers)
  3. *
  4. * Written 1997 by David Woodhouse.
  5. * Written 1994-1999 by Avery Pennarun.
  6. * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
  7. * Derived from skeleton.c by Donald Becker.
  8. *
  9. * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
  10. * for sponsoring the further development of this driver.
  11. *
  12. * **********************
  13. *
  14. * The original copyright of skeleton.c was as follows:
  15. *
  16. * skeleton.c Written 1993 by Donald Becker.
  17. * Copyright 1993 United States Government as represented by the
  18. * Director, National Security Agency. This software may only be used
  19. * and distributed according to the terms of the GNU General Public License as
  20. * modified by SRC, incorporated herein by reference.
  21. *
  22. * **********************
  23. *
  24. * For more details, see drivers/net/arcnet.c
  25. *
  26. * **********************
  27. */
  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 <asm/io.h>
  38. #include <linux/arcdevice.h>
  39. #define VERSION "arcnet: COM90xx IO-mapped mode support (by David Woodhouse et el.)\n"
  40. /* Internal function declarations */
  41. static int com90io_found(struct net_device *dev);
  42. static void com90io_command(struct net_device *dev, int command);
  43. static int com90io_status(struct net_device *dev);
  44. static void com90io_setmask(struct net_device *dev, int mask);
  45. static int com90io_reset(struct net_device *dev, int really_reset);
  46. static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
  47. void *buf, int count);
  48. static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
  49. void *buf, int count);
  50. /* Handy defines for ARCnet specific stuff */
  51. /* The number of low I/O ports used by the card. */
  52. #define ARCNET_TOTAL_SIZE 16
  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() inb(_STATUS)
  66. #define ACOMMAND(cmd) outb((cmd),_COMMAND)
  67. #define AINTMASK(msk) outb((msk),_INTMASK)
  68. #define SETCONF() outb((lp->config),_CONFIG)
  69. /****************************************************************************
  70. * *
  71. * IO-mapped operation routines *
  72. * *
  73. ****************************************************************************/
  74. #undef ONE_AT_A_TIME_TX
  75. #undef ONE_AT_A_TIME_RX
  76. static u_char get_buffer_byte(struct net_device *dev, unsigned offset)
  77. {
  78. int ioaddr = dev->base_addr;
  79. outb(offset >> 8, _ADDR_HI);
  80. outb(offset & 0xff, _ADDR_LO);
  81. return inb(_MEMDATA);
  82. }
  83. #ifdef ONE_AT_A_TIME_TX
  84. static void put_buffer_byte(struct net_device *dev, unsigned offset, u_char datum)
  85. {
  86. int ioaddr = dev->base_addr;
  87. outb(offset >> 8, _ADDR_HI);
  88. outb(offset & 0xff, _ADDR_LO);
  89. outb(datum, _MEMDATA);
  90. }
  91. #endif
  92. static void get_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
  93. {
  94. int ioaddr = dev->base_addr;
  95. outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
  96. outb(offset & 0xff, _ADDR_LO);
  97. while (length--)
  98. #ifdef ONE_AT_A_TIME_RX
  99. *(dest++) = get_buffer_byte(dev, offset++);
  100. #else
  101. *(dest++) = inb(_MEMDATA);
  102. #endif
  103. }
  104. static void put_whole_buffer(struct net_device *dev, unsigned offset, unsigned length, char *dest)
  105. {
  106. int ioaddr = dev->base_addr;
  107. outb((offset >> 8) | AUTOINCflag, _ADDR_HI);
  108. outb(offset & 0xff, _ADDR_LO);
  109. while (length--)
  110. #ifdef ONE_AT_A_TIME_TX
  111. put_buffer_byte(dev, offset++, *(dest++));
  112. #else
  113. outb(*(dest++), _MEMDATA);
  114. #endif
  115. }
  116. /*
  117. * We cannot probe for an IO mapped card either, although we can check that
  118. * it's where we were told it was, and even autoirq
  119. */
  120. static int __init com90io_probe(struct net_device *dev)
  121. {
  122. int ioaddr = dev->base_addr, status;
  123. unsigned long airqmask;
  124. BUGLVL(D_NORMAL) printk(VERSION);
  125. BUGLVL(D_NORMAL) printk("E-mail me if you actually test this driver, please!\n");
  126. if (!ioaddr) {
  127. BUGMSG(D_NORMAL, "No autoprobe for IO mapped cards; you "
  128. "must specify the base address!\n");
  129. return -ENODEV;
  130. }
  131. if (!request_region(ioaddr, ARCNET_TOTAL_SIZE, "com90io probe")) {
  132. BUGMSG(D_INIT_REASONS, "IO request_region %x-%x failed.\n",
  133. ioaddr, ioaddr + ARCNET_TOTAL_SIZE - 1);
  134. return -ENXIO;
  135. }
  136. if (ASTATUS() == 0xFF) {
  137. BUGMSG(D_INIT_REASONS, "IO address %x empty\n", ioaddr);
  138. goto err_out;
  139. }
  140. inb(_RESET);
  141. mdelay(RESETtime);
  142. status = ASTATUS();
  143. if ((status & 0x9D) != (NORXflag | RECONflag | TXFREEflag | RESETflag)) {
  144. BUGMSG(D_INIT_REASONS, "Status invalid (%Xh).\n", status);
  145. goto err_out;
  146. }
  147. BUGMSG(D_INIT_REASONS, "Status after reset: %X\n", status);
  148. ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
  149. BUGMSG(D_INIT_REASONS, "Status after reset acknowledged: %X\n", status);
  150. status = ASTATUS();
  151. if (status & RESETflag) {
  152. BUGMSG(D_INIT_REASONS, "Eternal reset (status=%Xh)\n", status);
  153. goto err_out;
  154. }
  155. outb((0x16 | IOMAPflag) & ~ENABLE16flag, _CONFIG);
  156. /* Read first loc'n of memory */
  157. outb(AUTOINCflag, _ADDR_HI);
  158. outb(0, _ADDR_LO);
  159. if ((status = inb(_MEMDATA)) != 0xd1) {
  160. BUGMSG(D_INIT_REASONS, "Signature byte not found"
  161. " (%Xh instead).\n", status);
  162. goto err_out;
  163. }
  164. if (!dev->irq) {
  165. /*
  166. * if we do this, we're sure to get an IRQ since the
  167. * card has just reset and the NORXflag is on until
  168. * we tell it to start receiving.
  169. */
  170. airqmask = probe_irq_on();
  171. outb(NORXflag, _INTMASK);
  172. udelay(1);
  173. outb(0, _INTMASK);
  174. dev->irq = probe_irq_off(airqmask);
  175. if ((int)dev->irq <= 0) {
  176. BUGMSG(D_INIT_REASONS, "Autoprobe IRQ failed\n");
  177. goto err_out;
  178. }
  179. }
  180. release_region(ioaddr, ARCNET_TOTAL_SIZE); /* end of probing */
  181. return com90io_found(dev);
  182. err_out:
  183. release_region(ioaddr, ARCNET_TOTAL_SIZE);
  184. return -ENODEV;
  185. }
  186. /* Set up the struct net_device associated with this card. Called after
  187. * probing succeeds.
  188. */
  189. static int __init com90io_found(struct net_device *dev)
  190. {
  191. struct arcnet_local *lp;
  192. int ioaddr = dev->base_addr;
  193. int err;
  194. /* Reserve the irq */
  195. if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (COM90xx-IO)", dev)) {
  196. BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
  197. return -ENODEV;
  198. }
  199. /* Reserve the I/O region */
  200. if (!request_region(dev->base_addr, ARCNET_TOTAL_SIZE, "arcnet (COM90xx-IO)")) {
  201. free_irq(dev->irq, dev);
  202. return -EBUSY;
  203. }
  204. lp = netdev_priv(dev);
  205. lp->card_name = "COM90xx I/O";
  206. lp->hw.command = com90io_command;
  207. lp->hw.status = com90io_status;
  208. lp->hw.intmask = com90io_setmask;
  209. lp->hw.reset = com90io_reset;
  210. lp->hw.owner = THIS_MODULE;
  211. lp->hw.copy_to_card = com90io_copy_to_card;
  212. lp->hw.copy_from_card = com90io_copy_from_card;
  213. lp->config = (0x16 | IOMAPflag) & ~ENABLE16flag;
  214. SETCONF();
  215. /* get and check the station ID from offset 1 in shmem */
  216. dev->dev_addr[0] = get_buffer_byte(dev, 1);
  217. err = register_netdev(dev);
  218. if (err) {
  219. outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
  220. free_irq(dev->irq, dev);
  221. release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
  222. return err;
  223. }
  224. BUGMSG(D_NORMAL, "COM90IO: station %02Xh found at %03lXh, IRQ %d.\n",
  225. dev->dev_addr[0], dev->base_addr, dev->irq);
  226. return 0;
  227. }
  228. /*
  229. * Do a hardware reset on the card, and set up necessary registers.
  230. *
  231. * This should be called as little as possible, because it disrupts the
  232. * token on the network (causes a RECON) and requires a significant delay.
  233. *
  234. * However, it does make sure the card is in a defined state.
  235. */
  236. static int com90io_reset(struct net_device *dev, int really_reset)
  237. {
  238. struct arcnet_local *lp = netdev_priv(dev);
  239. short ioaddr = dev->base_addr;
  240. BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n", dev->name, ASTATUS());
  241. if (really_reset) {
  242. /* reset the card */
  243. inb(_RESET);
  244. mdelay(RESETtime);
  245. }
  246. /* Set the thing to IO-mapped, 8-bit mode */
  247. lp->config = (0x1C | IOMAPflag) & ~ENABLE16flag;
  248. SETCONF();
  249. ACOMMAND(CFLAGScmd | RESETclear); /* clear flags & end reset */
  250. ACOMMAND(CFLAGScmd | CONFIGclear);
  251. /* verify that the ARCnet signature byte is present */
  252. if (get_buffer_byte(dev, 0) != TESTvalue) {
  253. BUGMSG(D_NORMAL, "reset failed: TESTvalue not present.\n");
  254. return 1;
  255. }
  256. /* enable extended (512-byte) packets */
  257. ACOMMAND(CONFIGcmd | EXTconf);
  258. /* done! return success. */
  259. return 0;
  260. }
  261. static void com90io_command(struct net_device *dev, int cmd)
  262. {
  263. short ioaddr = dev->base_addr;
  264. ACOMMAND(cmd);
  265. }
  266. static int com90io_status(struct net_device *dev)
  267. {
  268. short ioaddr = dev->base_addr;
  269. return ASTATUS();
  270. }
  271. static void com90io_setmask(struct net_device *dev, int mask)
  272. {
  273. short ioaddr = dev->base_addr;
  274. AINTMASK(mask);
  275. }
  276. static void com90io_copy_to_card(struct net_device *dev, int bufnum, int offset,
  277. void *buf, int count)
  278. {
  279. TIME("put_whole_buffer", count, put_whole_buffer(dev, bufnum * 512 + offset, count, buf));
  280. }
  281. static void com90io_copy_from_card(struct net_device *dev, int bufnum, int offset,
  282. void *buf, int count)
  283. {
  284. TIME("get_whole_buffer", count, get_whole_buffer(dev, bufnum * 512 + offset, count, buf));
  285. }
  286. static int io; /* use the insmod io= irq= shmem= options */
  287. static int irq;
  288. static char device[9]; /* use eg. device=arc1 to change name */
  289. module_param(io, int, 0);
  290. module_param(irq, int, 0);
  291. module_param_string(device, device, sizeof(device), 0);
  292. MODULE_LICENSE("GPL");
  293. #ifndef MODULE
  294. static int __init com90io_setup(char *s)
  295. {
  296. int ints[4];
  297. s = get_options(s, 4, ints);
  298. if (!ints[0])
  299. return 0;
  300. switch (ints[0]) {
  301. default: /* ERROR */
  302. printk("com90io: Too many arguments.\n");
  303. case 2: /* IRQ */
  304. irq = ints[2];
  305. case 1: /* IO address */
  306. io = ints[1];
  307. }
  308. if (*s)
  309. snprintf(device, sizeof(device), "%s", s);
  310. return 1;
  311. }
  312. __setup("com90io=", com90io_setup);
  313. #endif
  314. static struct net_device *my_dev;
  315. static int __init com90io_init(void)
  316. {
  317. struct net_device *dev;
  318. int err;
  319. dev = alloc_arcdev(device);
  320. if (!dev)
  321. return -ENOMEM;
  322. dev->base_addr = io;
  323. dev->irq = irq;
  324. if (dev->irq == 2)
  325. dev->irq = 9;
  326. err = com90io_probe(dev);
  327. if (err) {
  328. free_netdev(dev);
  329. return err;
  330. }
  331. my_dev = dev;
  332. return 0;
  333. }
  334. static void __exit com90io_exit(void)
  335. {
  336. struct net_device *dev = my_dev;
  337. int ioaddr = dev->base_addr;
  338. unregister_netdev(dev);
  339. /* Set the thing back to MMAP mode, in case the old driver is loaded later */
  340. outb((inb(_CONFIG) & ~IOMAPflag), _CONFIG);
  341. free_irq(dev->irq, dev);
  342. release_region(dev->base_addr, ARCNET_TOTAL_SIZE);
  343. free_netdev(dev);
  344. }
  345. module_init(com90io_init)
  346. module_exit(com90io_exit)