com20020.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/interrupt.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 = netdev_priv(dev);
  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->clockm != 0)
  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. const struct net_device_ops com20020_netdev_ops = {
  125. .ndo_open = arcnet_open,
  126. .ndo_stop = arcnet_close,
  127. .ndo_start_xmit = arcnet_send_packet,
  128. .ndo_tx_timeout = arcnet_timeout,
  129. .ndo_set_rx_mode = com20020_set_mc_list,
  130. };
  131. /* Set up the struct net_device associated with this card. Called after
  132. * probing succeeds.
  133. */
  134. int com20020_found(struct net_device *dev, int shared)
  135. {
  136. struct arcnet_local *lp;
  137. int ioaddr = dev->base_addr;
  138. /* Initialize the rest of the device structure. */
  139. lp = netdev_priv(dev);
  140. lp->hw.owner = THIS_MODULE;
  141. lp->hw.command = com20020_command;
  142. lp->hw.status = com20020_status;
  143. lp->hw.intmask = com20020_setmask;
  144. lp->hw.reset = com20020_reset;
  145. lp->hw.copy_to_card = com20020_copy_to_card;
  146. lp->hw.copy_from_card = com20020_copy_from_card;
  147. lp->hw.close = com20020_close;
  148. if (!dev->dev_addr[0])
  149. dev->dev_addr[0] = inb(ioaddr + BUS_ALIGN*8); /* FIXME: do this some other way! */
  150. SET_SUBADR(SUB_SETUP1);
  151. outb(lp->setup, _XREG);
  152. if (lp->card_flags & ARC_CAN_10MBIT)
  153. {
  154. SET_SUBADR(SUB_SETUP2);
  155. outb(lp->setup2, _XREG);
  156. /* must now write the magic "restart operation" command */
  157. mdelay(1);
  158. outb(0x18, _COMMAND);
  159. }
  160. lp->config = 0x20 | (lp->timeout << 3) | (lp->backplane << 2) | 1;
  161. /* Default 0x38 + register: Node ID */
  162. SETCONF;
  163. outb(dev->dev_addr[0], _XREG);
  164. /* reserve the irq */
  165. if (request_irq(dev->irq, arcnet_interrupt, shared,
  166. "arcnet (COM20020)", dev)) {
  167. BUGMSG(D_NORMAL, "Can't get IRQ %d!\n", dev->irq);
  168. return -ENODEV;
  169. }
  170. dev->base_addr = ioaddr;
  171. BUGMSG(D_NORMAL, "%s: station %02Xh found at %03lXh, IRQ %d.\n",
  172. lp->card_name, dev->dev_addr[0], dev->base_addr, dev->irq);
  173. if (lp->backplane)
  174. BUGMSG(D_NORMAL, "Using backplane mode.\n");
  175. if (lp->timeout != 3)
  176. BUGMSG(D_NORMAL, "Using extended timeout value of %d.\n", lp->timeout);
  177. BUGMSG(D_NORMAL, "Using CKP %d - data rate %s.\n",
  178. lp->setup >> 1,
  179. clockrates[3 - ((lp->setup2 & 0xF0) >> 4) + ((lp->setup & 0x0F) >> 1)]);
  180. if (register_netdev(dev)) {
  181. free_irq(dev->irq, dev);
  182. return -EIO;
  183. }
  184. return 0;
  185. }
  186. /*
  187. * Do a hardware reset on the card, and set up necessary registers.
  188. *
  189. * This should be called as little as possible, because it disrupts the
  190. * token on the network (causes a RECON) and requires a significant delay.
  191. *
  192. * However, it does make sure the card is in a defined state.
  193. */
  194. static int com20020_reset(struct net_device *dev, int really_reset)
  195. {
  196. struct arcnet_local *lp = netdev_priv(dev);
  197. u_int ioaddr = dev->base_addr;
  198. u_char inbyte;
  199. BUGMSG(D_DEBUG, "%s: %d: %s: dev: %p, lp: %p, dev->name: %s\n",
  200. __FILE__,__LINE__,__func__,dev,lp,dev->name);
  201. BUGMSG(D_INIT, "Resetting %s (status=%02Xh)\n",
  202. dev->name, ASTATUS());
  203. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  204. lp->config = TXENcfg | (lp->timeout << 3) | (lp->backplane << 2);
  205. /* power-up defaults */
  206. SETCONF;
  207. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  208. if (really_reset) {
  209. /* reset the card */
  210. ARCRESET;
  211. mdelay(RESETtime * 2); /* COM20020 seems to be slower sometimes */
  212. }
  213. /* clear flags & end reset */
  214. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  215. ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
  216. /* verify that the ARCnet signature byte is present */
  217. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  218. com20020_copy_from_card(dev, 0, 0, &inbyte, 1);
  219. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  220. if (inbyte != TESTvalue) {
  221. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  222. BUGMSG(D_NORMAL, "reset failed: TESTvalue not present.\n");
  223. return 1;
  224. }
  225. /* enable extended (512-byte) packets */
  226. ACOMMAND(CONFIGcmd | EXTconf);
  227. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  228. /* done! return success. */
  229. return 0;
  230. }
  231. static void com20020_setmask(struct net_device *dev, int mask)
  232. {
  233. u_int ioaddr = dev->base_addr;
  234. BUGMSG(D_DURING, "Setting mask to %x at %x\n",mask,ioaddr);
  235. AINTMASK(mask);
  236. }
  237. static void com20020_command(struct net_device *dev, int cmd)
  238. {
  239. u_int ioaddr = dev->base_addr;
  240. ACOMMAND(cmd);
  241. }
  242. static int com20020_status(struct net_device *dev)
  243. {
  244. u_int ioaddr = dev->base_addr;
  245. return ASTATUS() + (ADIAGSTATUS()<<8);
  246. }
  247. static void com20020_close(struct net_device *dev)
  248. {
  249. struct arcnet_local *lp = netdev_priv(dev);
  250. int ioaddr = dev->base_addr;
  251. /* disable transmitter */
  252. lp->config &= ~TXENcfg;
  253. SETCONF;
  254. }
  255. /* Set or clear the multicast filter for this adaptor.
  256. * num_addrs == -1 Promiscuous mode, receive all packets
  257. * num_addrs == 0 Normal mode, clear multicast list
  258. * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  259. * best-effort filtering.
  260. * FIXME - do multicast stuff, not just promiscuous.
  261. */
  262. static void com20020_set_mc_list(struct net_device *dev)
  263. {
  264. struct arcnet_local *lp = netdev_priv(dev);
  265. int ioaddr = dev->base_addr;
  266. if ((dev->flags & IFF_PROMISC) && (dev->flags & IFF_UP)) { /* Enable promiscuous mode */
  267. if (!(lp->setup & PROMISCset))
  268. BUGMSG(D_NORMAL, "Setting promiscuous flag...\n");
  269. SET_SUBADR(SUB_SETUP1);
  270. lp->setup |= PROMISCset;
  271. outb(lp->setup, _XREG);
  272. } else
  273. /* Disable promiscuous mode, use normal mode */
  274. {
  275. if ((lp->setup & PROMISCset))
  276. BUGMSG(D_NORMAL, "Resetting promiscuous flag...\n");
  277. SET_SUBADR(SUB_SETUP1);
  278. lp->setup &= ~PROMISCset;
  279. outb(lp->setup, _XREG);
  280. }
  281. }
  282. #if defined(CONFIG_ARCNET_COM20020_PCI_MODULE) || \
  283. defined(CONFIG_ARCNET_COM20020_ISA_MODULE) || \
  284. defined(CONFIG_ARCNET_COM20020_CS_MODULE)
  285. EXPORT_SYMBOL(com20020_check);
  286. EXPORT_SYMBOL(com20020_found);
  287. EXPORT_SYMBOL(com20020_netdev_ops);
  288. #endif
  289. MODULE_LICENSE("GPL");
  290. #ifdef MODULE
  291. static int __init com20020_module_init(void)
  292. {
  293. BUGLVL(D_NORMAL) printk(VERSION);
  294. return 0;
  295. }
  296. static void __exit com20020_module_exit(void)
  297. {
  298. }
  299. module_init(com20020_module_init);
  300. module_exit(com20020_module_exit);
  301. #endif /* MODULE */