com20020.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Linux ARCnet driver - COM20020 chipset support
  3. *
  4. * Written 1997 by David Woodhouse.
  5. * Written 1994-1999 by Avery Pennarun.
  6. * Written 1999 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/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/ioport.h>
  32. #include <linux/slab.h>
  33. #include <linux/errno.h>
  34. #include <linux/delay.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/init.h>
  37. #include <linux/arcdevice.h>
  38. #include <linux/com20020.h>
  39. #include <asm/io.h>
  40. #define VERSION "arcnet: COM20020 chipset support (by David Woodhouse et al.)\n"
  41. static char *clockrates[] =
  42. {"10 Mb/s", "Reserved", "5 Mb/s",
  43. "2.5 Mb/s", "1.25Mb/s", "625 Kb/s", "312.5 Kb/s",
  44. "156.25 Kb/s", "Reserved", "Reserved", "Reserved"};
  45. static void com20020_command(struct net_device *dev, int command);
  46. static int com20020_status(struct net_device *dev);
  47. static void com20020_setmask(struct net_device *dev, int mask);
  48. static int com20020_reset(struct net_device *dev, int really_reset);
  49. static void com20020_copy_to_card(struct net_device *dev, int bufnum,
  50. int offset, void *buf, int count);
  51. static void com20020_copy_from_card(struct net_device *dev, int bufnum,
  52. int offset, void *buf, int count);
  53. static void com20020_set_mc_list(struct net_device *dev);
  54. static void com20020_close(struct net_device *);
  55. static void com20020_copy_from_card(struct net_device *dev, int bufnum,
  56. int offset, void *buf, int count)
  57. {
  58. int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset;
  59. /* set up the address register */
  60. outb((ofs >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
  61. outb(ofs & 0xff, _ADDR_LO);
  62. /* copy the data */
  63. TIME("insb", count, insb(_MEMDATA, buf, count));
  64. }
  65. static void com20020_copy_to_card(struct net_device *dev, int bufnum,
  66. int offset, void *buf, int count)
  67. {
  68. int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset;
  69. /* set up the address register */
  70. outb((ofs >> 8) | AUTOINCflag, _ADDR_HI);
  71. outb(ofs & 0xff, _ADDR_LO);
  72. /* copy the data */
  73. TIME("outsb", count, outsb(_MEMDATA, buf, count));
  74. }
  75. /* Reset the card and check some basic stuff during the detection stage. */
  76. int com20020_check(struct net_device *dev)
  77. {
  78. int ioaddr = dev->base_addr, status;
  79. struct arcnet_local *lp = dev->priv;
  80. ARCRESET0;
  81. mdelay(RESETtime);
  82. lp->setup = lp->clockm ? 0 : (lp->clockp << 1);
  83. lp->setup2 = (lp->clockm << 4) | 8;
  84. /* CHECK: should we do this for SOHARD cards ? */
  85. /* Enable P1Mode for backplane mode */
  86. lp->setup = lp->setup | P1MODE;
  87. SET_SUBADR(SUB_SETUP1);
  88. outb(lp->setup, _XREG);
  89. if (lp->card_flags & ARC_CAN_10MBIT)
  90. {
  91. SET_SUBADR(SUB_SETUP2);
  92. outb(lp->setup2, _XREG);
  93. /* must now write the magic "restart operation" command */
  94. mdelay(1);
  95. outb(0x18, _COMMAND);
  96. }
  97. lp->config = 0x21 | (lp->timeout << 3) | (lp->backplane << 2);
  98. /* set node ID to 0x42 (but transmitter is disabled, so it's okay) */
  99. SETCONF;
  100. outb(0x42, ioaddr + BUS_ALIGN*7);
  101. status = ASTATUS();
  102. if ((status & 0x99) != (NORXflag | TXFREEflag | RESETflag)) {
  103. BUGMSG(D_NORMAL, "status invalid (%Xh).\n", status);
  104. return -ENODEV;
  105. }
  106. BUGMSG(D_INIT_REASONS, "status after reset: %X\n", status);
  107. /* Enable TX */
  108. outb(0x39, _CONFIG);
  109. outb(inb(ioaddr + BUS_ALIGN*8), ioaddr + BUS_ALIGN*7);
  110. ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
  111. status = ASTATUS();
  112. BUGMSG(D_INIT_REASONS, "status after reset acknowledged: %X\n",
  113. status);
  114. /* Read first location of memory */
  115. outb(0 | RDDATAflag | AUTOINCflag, _ADDR_HI);
  116. outb(0, _ADDR_LO);
  117. if ((status = inb(_MEMDATA)) != TESTvalue) {
  118. BUGMSG(D_NORMAL, "Signature byte not found (%02Xh != D1h).\n",
  119. status);
  120. return -ENODEV;
  121. }
  122. return 0;
  123. }
  124. /* Set up the struct net_device associated with this card. Called after
  125. * probing succeeds.
  126. */
  127. int com20020_found(struct net_device *dev, int shared)
  128. {
  129. struct arcnet_local *lp;
  130. int ioaddr = dev->base_addr;
  131. /* Initialize the rest of the device structure. */
  132. lp = dev->priv;
  133. lp->hw.owner = THIS_MODULE;
  134. lp->hw.command = com20020_command;
  135. lp->hw.status = com20020_status;
  136. lp->hw.intmask = com20020_setmask;
  137. lp->hw.reset = com20020_reset;
  138. lp->hw.copy_to_card = com20020_copy_to_card;
  139. lp->hw.copy_from_card = com20020_copy_from_card;
  140. lp->hw.close = com20020_close;
  141. dev->set_multicast_list = com20020_set_mc_list;
  142. if (!dev->dev_addr[0])
  143. dev->dev_addr[0] = inb(ioaddr + BUS_ALIGN*8); /* FIXME: do this some other way! */
  144. SET_SUBADR(SUB_SETUP1);
  145. outb(lp->setup, _XREG);
  146. if (lp->card_flags & ARC_CAN_10MBIT)
  147. {
  148. SET_SUBADR(SUB_SETUP2);
  149. outb(lp->setup2, _XREG);
  150. /* must now write the magic "restart operation" command */
  151. mdelay(1);
  152. outb(0x18, _COMMAND);
  153. }
  154. lp->config = 0x20 | (lp->timeout << 3) | (lp->backplane << 2) | 1;
  155. /* Default 0x38 + register: Node ID */
  156. SETCONF;
  157. outb(dev->dev_addr[0], _XREG);
  158. /* reserve the irq */
  159. if (request_irq(dev->irq, &arcnet_interrupt, shared,
  160. "arcnet (COM20020)", dev)) {
  161. BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
  162. return -ENODEV;
  163. }
  164. dev->base_addr = ioaddr;
  165. BUGMSG(D_NORMAL, "%s: station %02Xh found at %03lXh, IRQ %d.\n",
  166. lp->card_name, dev->dev_addr[0], dev->base_addr, dev->irq);
  167. if (lp->backplane)
  168. BUGMSG(D_NORMAL, "Using backplane mode.\n");
  169. if (lp->timeout != 3)
  170. BUGMSG(D_NORMAL, "Using extended timeout value of %d.\n", lp->timeout);
  171. BUGMSG(D_NORMAL, "Using CKP %d - data rate %s.\n",
  172. lp->setup >> 1,
  173. clockrates[3 - ((lp->setup2 & 0xF0) >> 4) + ((lp->setup & 0x0F) >> 1)]);
  174. if (register_netdev(dev)) {
  175. free_irq(dev->irq, dev);
  176. return -EIO;
  177. }
  178. return 0;
  179. }
  180. /*
  181. * Do a hardware reset on the card, and set up necessary registers.
  182. *
  183. * This should be called as little as possible, because it disrupts the
  184. * token on the network (causes a RECON) and requires a significant delay.
  185. *
  186. * However, it does make sure the card is in a defined state.
  187. */
  188. static int com20020_reset(struct net_device *dev, int really_reset)
  189. {
  190. struct arcnet_local *lp = dev->priv;
  191. u_int ioaddr = dev->base_addr;
  192. u_char inbyte;
  193. BUGMSG(D_DEBUG, "%s: %d: %s: dev: %p, lp: %p, dev->name: %s\n",
  194. __FILE__,__LINE__,__FUNCTION__,dev,lp,dev->name);
  195. BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n",
  196. dev->name, ASTATUS());
  197. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  198. lp->config = TXENcfg | (lp->timeout << 3) | (lp->backplane << 2);
  199. /* power-up defaults */
  200. SETCONF;
  201. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  202. if (really_reset) {
  203. /* reset the card */
  204. ARCRESET;
  205. mdelay(RESETtime * 2); /* COM20020 seems to be slower sometimes */
  206. }
  207. /* clear flags & end reset */
  208. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  209. ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
  210. /* verify that the ARCnet signature byte is present */
  211. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  212. com20020_copy_from_card(dev, 0, 0, &inbyte, 1);
  213. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  214. if (inbyte != TESTvalue) {
  215. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  216. BUGMSG(D_NORMAL, "reset failed: TESTvalue not present.\n");
  217. return 1;
  218. }
  219. /* enable extended (512-byte) packets */
  220. ACOMMAND(CONFIGcmd | EXTconf);
  221. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  222. /* done! return success. */
  223. return 0;
  224. }
  225. static void com20020_setmask(struct net_device *dev, int mask)
  226. {
  227. u_int ioaddr = dev->base_addr;
  228. BUGMSG(D_DURING, "Setting mask to %x at %x\n",mask,ioaddr);
  229. AINTMASK(mask);
  230. }
  231. static void com20020_command(struct net_device *dev, int cmd)
  232. {
  233. u_int ioaddr = dev->base_addr;
  234. ACOMMAND(cmd);
  235. }
  236. static int com20020_status(struct net_device *dev)
  237. {
  238. u_int ioaddr = dev->base_addr;
  239. return ASTATUS() + (ADIAGSTATUS()<<8);
  240. }
  241. static void com20020_close(struct net_device *dev)
  242. {
  243. struct arcnet_local *lp = dev->priv;
  244. int ioaddr = dev->base_addr;
  245. /* disable transmitter */
  246. lp->config &= ~TXENcfg;
  247. SETCONF;
  248. }
  249. /* Set or clear the multicast filter for this adaptor.
  250. * num_addrs == -1 Promiscuous mode, receive all packets
  251. * num_addrs == 0 Normal mode, clear multicast list
  252. * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  253. * best-effort filtering.
  254. * FIXME - do multicast stuff, not just promiscuous.
  255. */
  256. static void com20020_set_mc_list(struct net_device *dev)
  257. {
  258. struct arcnet_local *lp = dev->priv;
  259. int ioaddr = dev->base_addr;
  260. if ((dev->flags & IFF_PROMISC) && (dev->flags & IFF_UP)) { /* Enable promiscuous mode */
  261. if (!(lp->setup & PROMISCset))
  262. BUGMSG(D_NORMAL, "Setting promiscuous flag...\n");
  263. SET_SUBADR(SUB_SETUP1);
  264. lp->setup |= PROMISCset;
  265. outb(lp->setup, _XREG);
  266. } else
  267. /* Disable promiscuous mode, use normal mode */
  268. {
  269. if ((lp->setup & PROMISCset))
  270. BUGMSG(D_NORMAL, "Resetting promiscuous flag...\n");
  271. SET_SUBADR(SUB_SETUP1);
  272. lp->setup &= ~PROMISCset;
  273. outb(lp->setup, _XREG);
  274. }
  275. }
  276. #ifdef MODULE
  277. EXPORT_SYMBOL(com20020_check);
  278. EXPORT_SYMBOL(com20020_found);
  279. MODULE_LICENSE("GPL");
  280. int init_module(void)
  281. {
  282. BUGLVL(D_NORMAL) printk(VERSION);
  283. return 0;
  284. }
  285. void cleanup_module(void)
  286. {
  287. }
  288. #endif /* MODULE */