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