com90io.c 12 KB

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