sealevel.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Sealevel Systems 4021 driver.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * (c) Copyright 1999, 2001 Alan Cox
  10. * (c) Copyright 2001 Red Hat Inc.
  11. * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/net.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/delay.h>
  22. #include <linux/hdlc.h>
  23. #include <linux/ioport.h>
  24. #include <linux/init.h>
  25. #include <net/arp.h>
  26. #include <asm/irq.h>
  27. #include <asm/io.h>
  28. #include <asm/dma.h>
  29. #include <asm/byteorder.h>
  30. #include "z85230.h"
  31. struct slvl_device
  32. {
  33. struct z8530_channel *chan;
  34. int channel;
  35. };
  36. struct slvl_board
  37. {
  38. struct slvl_device dev[2];
  39. struct z8530_dev board;
  40. int iobase;
  41. };
  42. /*
  43. * Network driver support routines
  44. */
  45. static inline struct slvl_device* dev_to_chan(struct net_device *dev)
  46. {
  47. return (struct slvl_device *)dev_to_hdlc(dev)->priv;
  48. }
  49. /*
  50. * Frame receive. Simple for our card as we do HDLC and there
  51. * is no funny garbage involved
  52. */
  53. static void sealevel_input(struct z8530_channel *c, struct sk_buff *skb)
  54. {
  55. /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
  56. skb_trim(skb, skb->len - 2);
  57. skb->protocol = hdlc_type_trans(skb, c->netdevice);
  58. skb_reset_mac_header(skb);
  59. skb->dev = c->netdevice;
  60. netif_rx(skb);
  61. c->netdevice->last_rx = jiffies;
  62. }
  63. /*
  64. * We've been placed in the UP state
  65. */
  66. static int sealevel_open(struct net_device *d)
  67. {
  68. struct slvl_device *slvl = dev_to_chan(d);
  69. int err = -1;
  70. int unit = slvl->channel;
  71. /*
  72. * Link layer up.
  73. */
  74. switch (unit)
  75. {
  76. case 0:
  77. err = z8530_sync_dma_open(d, slvl->chan);
  78. break;
  79. case 1:
  80. err = z8530_sync_open(d, slvl->chan);
  81. break;
  82. }
  83. if (err)
  84. return err;
  85. err = hdlc_open(d);
  86. if (err) {
  87. switch (unit) {
  88. case 0:
  89. z8530_sync_dma_close(d, slvl->chan);
  90. break;
  91. case 1:
  92. z8530_sync_close(d, slvl->chan);
  93. break;
  94. }
  95. return err;
  96. }
  97. slvl->chan->rx_function = sealevel_input;
  98. /*
  99. * Go go go
  100. */
  101. netif_start_queue(d);
  102. return 0;
  103. }
  104. static int sealevel_close(struct net_device *d)
  105. {
  106. struct slvl_device *slvl = dev_to_chan(d);
  107. int unit = slvl->channel;
  108. /*
  109. * Discard new frames
  110. */
  111. slvl->chan->rx_function = z8530_null_rx;
  112. hdlc_close(d);
  113. netif_stop_queue(d);
  114. switch (unit)
  115. {
  116. case 0:
  117. z8530_sync_dma_close(d, slvl->chan);
  118. break;
  119. case 1:
  120. z8530_sync_close(d, slvl->chan);
  121. break;
  122. }
  123. return 0;
  124. }
  125. static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
  126. {
  127. /* struct slvl_device *slvl=dev_to_chan(d);
  128. z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
  129. return hdlc_ioctl(d, ifr, cmd);
  130. }
  131. /*
  132. * Passed network frames, fire them downwind.
  133. */
  134. static int sealevel_queue_xmit(struct sk_buff *skb, struct net_device *d)
  135. {
  136. return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
  137. }
  138. static int sealevel_attach(struct net_device *dev, unsigned short encoding,
  139. unsigned short parity)
  140. {
  141. if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
  142. return 0;
  143. return -EINVAL;
  144. }
  145. static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
  146. {
  147. struct net_device *dev = alloc_hdlcdev(sv);
  148. if (!dev)
  149. return -1;
  150. dev_to_hdlc(dev)->attach = sealevel_attach;
  151. dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
  152. dev->open = sealevel_open;
  153. dev->stop = sealevel_close;
  154. dev->do_ioctl = sealevel_ioctl;
  155. dev->base_addr = iobase;
  156. dev->irq = irq;
  157. if (register_hdlc_device(dev)) {
  158. printk(KERN_ERR "sealevel: unable to register HDLC device\n");
  159. free_netdev(dev);
  160. return -1;
  161. }
  162. sv->chan->netdevice = dev;
  163. return 0;
  164. }
  165. /*
  166. * Allocate and setup Sealevel board.
  167. */
  168. static __init struct slvl_board *slvl_init(int iobase, int irq,
  169. int txdma, int rxdma, int slow)
  170. {
  171. struct z8530_dev *dev;
  172. struct slvl_board *b;
  173. /*
  174. * Get the needed I/O space
  175. */
  176. if (!request_region(iobase, 8, "Sealevel 4021")) {
  177. printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n",
  178. iobase);
  179. return NULL;
  180. }
  181. b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
  182. if (!b)
  183. goto err_kzalloc;
  184. b->dev[0].chan = &b->board.chanA;
  185. b->dev[0].channel = 0;
  186. b->dev[1].chan = &b->board.chanB;
  187. b->dev[1].channel = 1;
  188. dev = &b->board;
  189. /*
  190. * Stuff in the I/O addressing
  191. */
  192. dev->active = 0;
  193. b->iobase = iobase;
  194. /*
  195. * Select 8530 delays for the old board
  196. */
  197. if (slow)
  198. iobase |= Z8530_PORT_SLEEP;
  199. dev->chanA.ctrlio = iobase + 1;
  200. dev->chanA.dataio = iobase;
  201. dev->chanB.ctrlio = iobase + 3;
  202. dev->chanB.dataio = iobase + 2;
  203. dev->chanA.irqs = &z8530_nop;
  204. dev->chanB.irqs = &z8530_nop;
  205. /*
  206. * Assert DTR enable DMA
  207. */
  208. outb(3 | (1 << 7), b->iobase + 4);
  209. /* We want a fast IRQ for this device. Actually we'd like an even faster
  210. IRQ ;) - This is one driver RtLinux is made for */
  211. if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
  212. "SeaLevel", dev) < 0) {
  213. printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
  214. goto err_request_irq;
  215. }
  216. dev->irq = irq;
  217. dev->chanA.private = &b->dev[0];
  218. dev->chanB.private = &b->dev[1];
  219. dev->chanA.dev = dev;
  220. dev->chanB.dev = dev;
  221. dev->chanA.txdma = 3;
  222. dev->chanA.rxdma = 1;
  223. if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
  224. goto err_dma_tx;
  225. if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
  226. goto err_dma_rx;
  227. disable_irq(irq);
  228. /*
  229. * Begin normal initialise
  230. */
  231. if (z8530_init(dev) != 0) {
  232. printk(KERN_ERR "Z8530 series device not found.\n");
  233. enable_irq(irq);
  234. goto free_hw;
  235. }
  236. if (dev->type == Z85C30) {
  237. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
  238. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
  239. } else {
  240. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
  241. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
  242. }
  243. /*
  244. * Now we can take the IRQ
  245. */
  246. enable_irq(irq);
  247. if (slvl_setup(&b->dev[0], iobase, irq))
  248. goto free_hw;
  249. if (slvl_setup(&b->dev[1], iobase, irq))
  250. goto free_netdev0;
  251. z8530_describe(dev, "I/O", iobase);
  252. dev->active = 1;
  253. return b;
  254. free_netdev0:
  255. unregister_hdlc_device(b->dev[0].chan->netdevice);
  256. free_netdev(b->dev[0].chan->netdevice);
  257. free_hw:
  258. free_dma(dev->chanA.rxdma);
  259. err_dma_rx:
  260. free_dma(dev->chanA.txdma);
  261. err_dma_tx:
  262. free_irq(irq, dev);
  263. err_request_irq:
  264. kfree(b);
  265. err_kzalloc:
  266. release_region(iobase, 8);
  267. return NULL;
  268. }
  269. static void __exit slvl_shutdown(struct slvl_board *b)
  270. {
  271. int u;
  272. z8530_shutdown(&b->board);
  273. for (u = 0; u < 2; u++)
  274. {
  275. struct net_device *d = b->dev[u].chan->netdevice;
  276. unregister_hdlc_device(d);
  277. free_netdev(d);
  278. }
  279. free_irq(b->board.irq, &b->board);
  280. free_dma(b->board.chanA.rxdma);
  281. free_dma(b->board.chanA.txdma);
  282. /* DMA off on the card, drop DTR */
  283. outb(0, b->iobase);
  284. release_region(b->iobase, 8);
  285. kfree(b);
  286. }
  287. static int io=0x238;
  288. static int txdma=1;
  289. static int rxdma=3;
  290. static int irq=5;
  291. static int slow=0;
  292. module_param(io, int, 0);
  293. MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
  294. module_param(txdma, int, 0);
  295. MODULE_PARM_DESC(txdma, "Transmit DMA channel");
  296. module_param(rxdma, int, 0);
  297. MODULE_PARM_DESC(rxdma, "Receive DMA channel");
  298. module_param(irq, int, 0);
  299. MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
  300. module_param(slow, bool, 0);
  301. MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
  302. MODULE_AUTHOR("Alan Cox");
  303. MODULE_LICENSE("GPL");
  304. MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
  305. static struct slvl_board *slvl_unit;
  306. static int __init slvl_init_module(void)
  307. {
  308. slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
  309. return slvl_unit ? 0 : -ENODEV;
  310. }
  311. static void __exit slvl_cleanup_module(void)
  312. {
  313. if(slvl_unit)
  314. slvl_shutdown(slvl_unit);
  315. }
  316. module_init(slvl_init_module);
  317. module_exit(slvl_cleanup_module);