com90io.c 11 KB

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