com20020.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/types.h>
  32. #include <linux/ioport.h>
  33. #include <linux/errno.h>
  34. #include <linux/delay.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/init.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/io.h>
  39. #include "arcdevice.h"
  40. #include "com20020.h"
  41. static const char * const clockrates[] = {
  42. "XXXXXXX", "XXXXXXXX", "XXXXXX", "2.5 Mb/s",
  43. "1.25Mb/s", "625 Kb/s", "312.5 Kb/s", "156.25 Kb/s",
  44. "Reserved", "Reserved", "Reserved"
  45. };
  46. static void com20020_command(struct net_device *dev, int command);
  47. static int com20020_status(struct net_device *dev);
  48. static void com20020_setmask(struct net_device *dev, int mask);
  49. static int com20020_reset(struct net_device *dev, int really_reset);
  50. static void com20020_copy_to_card(struct net_device *dev, int bufnum,
  51. int offset, void *buf, int count);
  52. static void com20020_copy_from_card(struct net_device *dev, int bufnum,
  53. int offset, void *buf, int count);
  54. static void com20020_set_mc_list(struct net_device *dev);
  55. static void com20020_close(struct net_device *);
  56. static void com20020_copy_from_card(struct net_device *dev, int bufnum,
  57. int offset, void *buf, int count)
  58. {
  59. int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset;
  60. /* set up the address register */
  61. arcnet_outb((ofs >> 8) | RDDATAflag | AUTOINCflag,
  62. ioaddr, COM20020_REG_W_ADDR_HI);
  63. arcnet_outb(ofs & 0xff, ioaddr, COM20020_REG_W_ADDR_LO);
  64. /* copy the data */
  65. TIME(dev, "insb", count,
  66. arcnet_insb(ioaddr, COM20020_REG_RW_MEMDATA, buf, count));
  67. }
  68. static void com20020_copy_to_card(struct net_device *dev, int bufnum,
  69. int offset, void *buf, int count)
  70. {
  71. int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset;
  72. /* set up the address register */
  73. arcnet_outb((ofs >> 8) | AUTOINCflag, ioaddr, COM20020_REG_W_ADDR_HI);
  74. arcnet_outb(ofs & 0xff, ioaddr, COM20020_REG_W_ADDR_LO);
  75. /* copy the data */
  76. TIME(dev, "outsb", count,
  77. arcnet_outsb(ioaddr, COM20020_REG_RW_MEMDATA, buf, count));
  78. }
  79. /* Reset the card and check some basic stuff during the detection stage. */
  80. int com20020_check(struct net_device *dev)
  81. {
  82. int ioaddr = dev->base_addr, status;
  83. struct arcnet_local *lp = netdev_priv(dev);
  84. arcnet_outb(XTOcfg(3) | RESETcfg, ioaddr, COM20020_REG_W_CONFIG);
  85. udelay(5);
  86. arcnet_outb(XTOcfg(3), ioaddr, COM20020_REG_W_CONFIG);
  87. mdelay(RESETtime);
  88. lp->setup = lp->clockm ? 0 : (lp->clockp << 1);
  89. lp->setup2 = (lp->clockm << 4) | 8;
  90. /* CHECK: should we do this for SOHARD cards ? */
  91. /* Enable P1Mode for backplane mode */
  92. lp->setup = lp->setup | P1MODE;
  93. com20020_set_subaddress(lp, ioaddr, SUB_SETUP1);
  94. arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG);
  95. if (lp->clockm != 0) {
  96. com20020_set_subaddress(lp, ioaddr, SUB_SETUP2);
  97. arcnet_outb(lp->setup2, ioaddr, COM20020_REG_W_XREG);
  98. /* must now write the magic "restart operation" command */
  99. mdelay(1);
  100. arcnet_outb(STARTIOcmd, ioaddr, COM20020_REG_W_COMMAND);
  101. }
  102. lp->config = (lp->timeout << 3) | (lp->backplane << 2) | SUB_NODE;
  103. /* set node ID to 0x42 (but transmitter is disabled, so it's okay) */
  104. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  105. arcnet_outb(0x42, ioaddr, COM20020_REG_W_XREG);
  106. status = arcnet_inb(ioaddr, COM20020_REG_R_STATUS);
  107. if ((status & 0x99) != (NORXflag | TXFREEflag | RESETflag)) {
  108. arc_printk(D_NORMAL, dev, "status invalid (%Xh).\n", status);
  109. return -ENODEV;
  110. }
  111. arc_printk(D_INIT_REASONS, dev, "status after reset: %X\n", status);
  112. arcnet_outb(CFLAGScmd | RESETclear | CONFIGclear,
  113. ioaddr, COM20020_REG_W_COMMAND);
  114. status = arcnet_inb(ioaddr, COM20020_REG_R_STATUS);
  115. arc_printk(D_INIT_REASONS, dev, "status after reset acknowledged: %X\n",
  116. status);
  117. /* Read first location of memory */
  118. arcnet_outb(0 | RDDATAflag | AUTOINCflag,
  119. ioaddr, COM20020_REG_W_ADDR_HI);
  120. arcnet_outb(0, ioaddr, COM20020_REG_W_ADDR_LO);
  121. status = arcnet_inb(ioaddr, COM20020_REG_RW_MEMDATA);
  122. if (status != TESTvalue) {
  123. arc_printk(D_NORMAL, dev, "Signature byte not found (%02Xh != D1h).\n",
  124. status);
  125. return -ENODEV;
  126. }
  127. return 0;
  128. }
  129. static int com20020_set_hwaddr(struct net_device *dev, void *addr)
  130. {
  131. int ioaddr = dev->base_addr;
  132. struct arcnet_local *lp = netdev_priv(dev);
  133. struct sockaddr *hwaddr = addr;
  134. memcpy(dev->dev_addr, hwaddr->sa_data, 1);
  135. com20020_set_subaddress(lp, ioaddr, SUB_NODE);
  136. arcnet_outb(dev->dev_addr[0], ioaddr, COM20020_REG_W_XREG);
  137. return 0;
  138. }
  139. static int com20020_netdev_open(struct net_device *dev)
  140. {
  141. int ioaddr = dev->base_addr;
  142. struct arcnet_local *lp = netdev_priv(dev);
  143. lp->config |= TXENcfg;
  144. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  145. return arcnet_open(dev);
  146. }
  147. static int com20020_netdev_close(struct net_device *dev)
  148. {
  149. int ioaddr = dev->base_addr;
  150. struct arcnet_local *lp = netdev_priv(dev);
  151. arcnet_close(dev);
  152. /* disable transmitter */
  153. lp->config &= ~TXENcfg;
  154. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  155. return 0;
  156. }
  157. const struct net_device_ops com20020_netdev_ops = {
  158. .ndo_open = com20020_netdev_open,
  159. .ndo_stop = com20020_netdev_close,
  160. .ndo_start_xmit = arcnet_send_packet,
  161. .ndo_tx_timeout = arcnet_timeout,
  162. .ndo_set_mac_address = com20020_set_hwaddr,
  163. .ndo_set_rx_mode = com20020_set_mc_list,
  164. };
  165. /* Set up the struct net_device associated with this card. Called after
  166. * probing succeeds.
  167. */
  168. int com20020_found(struct net_device *dev, int shared)
  169. {
  170. struct arcnet_local *lp;
  171. int ioaddr = dev->base_addr;
  172. /* Initialize the rest of the device structure. */
  173. lp = netdev_priv(dev);
  174. lp->hw.owner = THIS_MODULE;
  175. lp->hw.command = com20020_command;
  176. lp->hw.status = com20020_status;
  177. lp->hw.intmask = com20020_setmask;
  178. lp->hw.reset = com20020_reset;
  179. lp->hw.copy_to_card = com20020_copy_to_card;
  180. lp->hw.copy_from_card = com20020_copy_from_card;
  181. lp->hw.close = com20020_close;
  182. /* FIXME: do this some other way! */
  183. if (!dev->dev_addr[0])
  184. dev->dev_addr[0] = arcnet_inb(ioaddr, 8);
  185. com20020_set_subaddress(lp, ioaddr, SUB_SETUP1);
  186. arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG);
  187. if (lp->card_flags & ARC_CAN_10MBIT) {
  188. com20020_set_subaddress(lp, ioaddr, SUB_SETUP2);
  189. arcnet_outb(lp->setup2, ioaddr, COM20020_REG_W_XREG);
  190. /* must now write the magic "restart operation" command */
  191. mdelay(1);
  192. arcnet_outb(STARTIOcmd, ioaddr, COM20020_REG_W_COMMAND);
  193. }
  194. lp->config = (lp->timeout << 3) | (lp->backplane << 2) | SUB_NODE;
  195. /* Default 0x38 + register: Node ID */
  196. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  197. arcnet_outb(dev->dev_addr[0], ioaddr, COM20020_REG_W_XREG);
  198. /* reserve the irq */
  199. if (request_irq(dev->irq, arcnet_interrupt, shared,
  200. "arcnet (COM20020)", dev)) {
  201. arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
  202. return -ENODEV;
  203. }
  204. arc_printk(D_NORMAL, dev, "%s: station %02Xh found at %03lXh, IRQ %d.\n",
  205. lp->card_name, dev->dev_addr[0], dev->base_addr, dev->irq);
  206. if (lp->backplane)
  207. arc_printk(D_NORMAL, dev, "Using backplane mode.\n");
  208. if (lp->timeout != 3)
  209. arc_printk(D_NORMAL, dev, "Using extended timeout value of %d\n",
  210. lp->timeout);
  211. arc_printk(D_NORMAL, dev, "Using CKP %d - data rate %s\n",
  212. lp->setup >> 1,
  213. clockrates[3 -
  214. ((lp->setup2 & 0xF0) >> 4) +
  215. ((lp->setup & 0x0F) >> 1)]);
  216. /* The clockrates array index looks very fragile.
  217. * It seems like it could have negative indexing.
  218. */
  219. if (register_netdev(dev)) {
  220. free_irq(dev->irq, dev);
  221. return -EIO;
  222. }
  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 com20020_reset(struct net_device *dev, int really_reset)
  233. {
  234. struct arcnet_local *lp = netdev_priv(dev);
  235. u_int ioaddr = dev->base_addr;
  236. u_char inbyte;
  237. arc_printk(D_DEBUG, dev, "%s: %d: %s: dev: %p, lp: %p, dev->name: %s\n",
  238. __FILE__, __LINE__, __func__, dev, lp, dev->name);
  239. arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
  240. dev->name, arcnet_inb(ioaddr, COM20020_REG_R_STATUS));
  241. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  242. lp->config |= (lp->timeout << 3) | (lp->backplane << 2);
  243. /* power-up defaults */
  244. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  245. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  246. if (really_reset) {
  247. /* reset the card */
  248. arcnet_outb(lp->config | RESETcfg, ioaddr, COM20020_REG_W_CONFIG);
  249. udelay(5);
  250. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  251. mdelay(RESETtime * 2);
  252. /* COM20020 seems to be slower sometimes */
  253. }
  254. /* clear flags & end reset */
  255. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  256. arcnet_outb(CFLAGScmd | RESETclear | CONFIGclear,
  257. ioaddr, COM20020_REG_W_COMMAND);
  258. /* verify that the ARCnet signature byte is present */
  259. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  260. com20020_copy_from_card(dev, 0, 0, &inbyte, 1);
  261. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  262. if (inbyte != TESTvalue) {
  263. arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
  264. __FILE__, __LINE__, __func__);
  265. arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
  266. return 1;
  267. }
  268. /* enable extended (512-byte) packets */
  269. arcnet_outb(CONFIGcmd | EXTconf, ioaddr, COM20020_REG_W_COMMAND);
  270. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  271. /* done! return success. */
  272. return 0;
  273. }
  274. static void com20020_setmask(struct net_device *dev, int mask)
  275. {
  276. u_int ioaddr = dev->base_addr;
  277. arc_printk(D_DURING, dev, "Setting mask to %x at %x\n", mask, ioaddr);
  278. arcnet_outb(mask, ioaddr, COM20020_REG_W_INTMASK);
  279. }
  280. static void com20020_command(struct net_device *dev, int cmd)
  281. {
  282. u_int ioaddr = dev->base_addr;
  283. arcnet_outb(cmd, ioaddr, COM20020_REG_W_COMMAND);
  284. }
  285. static int com20020_status(struct net_device *dev)
  286. {
  287. u_int ioaddr = dev->base_addr;
  288. return arcnet_inb(ioaddr, COM20020_REG_R_STATUS) +
  289. (arcnet_inb(ioaddr, COM20020_REG_R_DIAGSTAT) << 8);
  290. }
  291. static void com20020_close(struct net_device *dev)
  292. {
  293. struct arcnet_local *lp = netdev_priv(dev);
  294. int ioaddr = dev->base_addr;
  295. /* disable transmitter */
  296. lp->config &= ~TXENcfg;
  297. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  298. }
  299. /* Set or clear the multicast filter for this adaptor.
  300. * num_addrs == -1 Promiscuous mode, receive all packets
  301. * num_addrs == 0 Normal mode, clear multicast list
  302. * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  303. * best-effort filtering.
  304. * FIXME - do multicast stuff, not just promiscuous.
  305. */
  306. static void com20020_set_mc_list(struct net_device *dev)
  307. {
  308. struct arcnet_local *lp = netdev_priv(dev);
  309. int ioaddr = dev->base_addr;
  310. if ((dev->flags & IFF_PROMISC) && (dev->flags & IFF_UP)) {
  311. /* Enable promiscuous mode */
  312. if (!(lp->setup & PROMISCset))
  313. arc_printk(D_NORMAL, dev, "Setting promiscuous flag...\n");
  314. com20020_set_subaddress(lp, ioaddr, SUB_SETUP1);
  315. lp->setup |= PROMISCset;
  316. arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG);
  317. } else {
  318. /* Disable promiscuous mode, use normal mode */
  319. if ((lp->setup & PROMISCset))
  320. arc_printk(D_NORMAL, dev, "Resetting promiscuous flag...\n");
  321. com20020_set_subaddress(lp, ioaddr, SUB_SETUP1);
  322. lp->setup &= ~PROMISCset;
  323. arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG);
  324. }
  325. }
  326. #if defined(CONFIG_ARCNET_COM20020_PCI_MODULE) || \
  327. defined(CONFIG_ARCNET_COM20020_ISA_MODULE) || \
  328. defined(CONFIG_ARCNET_COM20020_CS_MODULE)
  329. EXPORT_SYMBOL(com20020_check);
  330. EXPORT_SYMBOL(com20020_found);
  331. EXPORT_SYMBOL(com20020_netdev_ops);
  332. #endif
  333. MODULE_LICENSE("GPL");
  334. #ifdef MODULE
  335. static int __init com20020_module_init(void)
  336. {
  337. if (BUGLVL(D_NORMAL))
  338. pr_info("%s\n", "COM20020 chipset support (by David Woodhouse et al.)");
  339. return 0;
  340. }
  341. static void __exit com20020_module_exit(void)
  342. {
  343. }
  344. module_init(com20020_module_init);
  345. module_exit(com20020_module_exit);
  346. #endif /* MODULE */