com20020.c 10 KB

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