com20020.c 10 KB

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