com20020.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. dev->base_addr = ioaddr;
  205. arc_printk(D_NORMAL, dev, "%s: station %02Xh found at %03lXh, IRQ %d.\n",
  206. lp->card_name, dev->dev_addr[0], dev->base_addr, dev->irq);
  207. if (lp->backplane)
  208. arc_printk(D_NORMAL, dev, "Using backplane mode.\n");
  209. if (lp->timeout != 3)
  210. arc_printk(D_NORMAL, dev, "Using extended timeout value of %d\n",
  211. lp->timeout);
  212. arc_printk(D_NORMAL, dev, "Using CKP %d - data rate %s\n",
  213. lp->setup >> 1,
  214. clockrates[3 -
  215. ((lp->setup2 & 0xF0) >> 4) +
  216. ((lp->setup & 0x0F) >> 1)]);
  217. /* The clockrates array index looks very fragile.
  218. * It seems like it could have negative indexing.
  219. */
  220. if (register_netdev(dev)) {
  221. free_irq(dev->irq, dev);
  222. return -EIO;
  223. }
  224. return 0;
  225. }
  226. /* Do a hardware reset on the card, and set up necessary registers.
  227. *
  228. * This should be called as little as possible, because it disrupts the
  229. * token on the network (causes a RECON) and requires a significant delay.
  230. *
  231. * However, it does make sure the card is in a defined state.
  232. */
  233. static int com20020_reset(struct net_device *dev, int really_reset)
  234. {
  235. struct arcnet_local *lp = netdev_priv(dev);
  236. u_int ioaddr = dev->base_addr;
  237. u_char inbyte;
  238. arc_printk(D_DEBUG, dev, "%s: %d: %s: dev: %p, lp: %p, dev->name: %s\n",
  239. __FILE__, __LINE__, __func__, dev, lp, dev->name);
  240. arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
  241. dev->name, arcnet_inb(ioaddr, COM20020_REG_R_STATUS));
  242. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  243. lp->config |= (lp->timeout << 3) | (lp->backplane << 2);
  244. /* power-up defaults */
  245. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  246. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  247. if (really_reset) {
  248. /* reset the card */
  249. arcnet_outb(lp->config | RESETcfg, ioaddr, COM20020_REG_W_CONFIG);
  250. udelay(5);
  251. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  252. mdelay(RESETtime * 2);
  253. /* COM20020 seems to be slower sometimes */
  254. }
  255. /* clear flags & end reset */
  256. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  257. arcnet_outb(CFLAGScmd | RESETclear | CONFIGclear,
  258. ioaddr, COM20020_REG_W_COMMAND);
  259. /* verify that the ARCnet signature byte is present */
  260. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  261. com20020_copy_from_card(dev, 0, 0, &inbyte, 1);
  262. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  263. if (inbyte != TESTvalue) {
  264. arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
  265. __FILE__, __LINE__, __func__);
  266. arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
  267. return 1;
  268. }
  269. /* enable extended (512-byte) packets */
  270. arcnet_outb(CONFIGcmd | EXTconf, ioaddr, COM20020_REG_W_COMMAND);
  271. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  272. /* done! return success. */
  273. return 0;
  274. }
  275. static void com20020_setmask(struct net_device *dev, int mask)
  276. {
  277. u_int ioaddr = dev->base_addr;
  278. arc_printk(D_DURING, dev, "Setting mask to %x at %x\n", mask, ioaddr);
  279. arcnet_outb(mask, ioaddr, COM20020_REG_W_INTMASK);
  280. }
  281. static void com20020_command(struct net_device *dev, int cmd)
  282. {
  283. u_int ioaddr = dev->base_addr;
  284. arcnet_outb(cmd, ioaddr, COM20020_REG_W_COMMAND);
  285. }
  286. static int com20020_status(struct net_device *dev)
  287. {
  288. u_int ioaddr = dev->base_addr;
  289. return arcnet_inb(ioaddr, COM20020_REG_R_STATUS) +
  290. (arcnet_inb(ioaddr, COM20020_REG_R_DIAGSTAT) << 8);
  291. }
  292. static void com20020_close(struct net_device *dev)
  293. {
  294. struct arcnet_local *lp = netdev_priv(dev);
  295. int ioaddr = dev->base_addr;
  296. /* disable transmitter */
  297. lp->config &= ~TXENcfg;
  298. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  299. }
  300. /* Set or clear the multicast filter for this adaptor.
  301. * num_addrs == -1 Promiscuous mode, receive all packets
  302. * num_addrs == 0 Normal mode, clear multicast list
  303. * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  304. * best-effort filtering.
  305. * FIXME - do multicast stuff, not just promiscuous.
  306. */
  307. static void com20020_set_mc_list(struct net_device *dev)
  308. {
  309. struct arcnet_local *lp = netdev_priv(dev);
  310. int ioaddr = dev->base_addr;
  311. if ((dev->flags & IFF_PROMISC) && (dev->flags & IFF_UP)) {
  312. /* Enable promiscuous mode */
  313. if (!(lp->setup & PROMISCset))
  314. arc_printk(D_NORMAL, dev, "Setting promiscuous flag...\n");
  315. com20020_set_subaddress(lp, ioaddr, SUB_SETUP1);
  316. lp->setup |= PROMISCset;
  317. arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG);
  318. } else {
  319. /* Disable promiscuous mode, use normal mode */
  320. if ((lp->setup & PROMISCset))
  321. arc_printk(D_NORMAL, dev, "Resetting promiscuous flag...\n");
  322. com20020_set_subaddress(lp, ioaddr, SUB_SETUP1);
  323. lp->setup &= ~PROMISCset;
  324. arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG);
  325. }
  326. }
  327. #if defined(CONFIG_ARCNET_COM20020_PCI_MODULE) || \
  328. defined(CONFIG_ARCNET_COM20020_ISA_MODULE) || \
  329. defined(CONFIG_ARCNET_COM20020_CS_MODULE)
  330. EXPORT_SYMBOL(com20020_check);
  331. EXPORT_SYMBOL(com20020_found);
  332. EXPORT_SYMBOL(com20020_netdev_ops);
  333. #endif
  334. MODULE_LICENSE("GPL");
  335. #ifdef MODULE
  336. static int __init com20020_module_init(void)
  337. {
  338. if (BUGLVL(D_NORMAL))
  339. pr_info("%s\n", "COM20020 chipset support (by David Woodhouse et al.)");
  340. return 0;
  341. }
  342. static void __exit com20020_module_exit(void)
  343. {
  344. }
  345. module_init(com20020_module_init);
  346. module_exit(com20020_module_exit);
  347. #endif /* MODULE */