sealevel.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. }
  62. /*
  63. * We've been placed in the UP state
  64. */
  65. static int sealevel_open(struct net_device *d)
  66. {
  67. struct slvl_device *slvl = dev_to_chan(d);
  68. int err = -1;
  69. int unit = slvl->channel;
  70. /*
  71. * Link layer up.
  72. */
  73. switch (unit)
  74. {
  75. case 0:
  76. err = z8530_sync_dma_open(d, slvl->chan);
  77. break;
  78. case 1:
  79. err = z8530_sync_open(d, slvl->chan);
  80. break;
  81. }
  82. if (err)
  83. return err;
  84. err = hdlc_open(d);
  85. if (err) {
  86. switch (unit) {
  87. case 0:
  88. z8530_sync_dma_close(d, slvl->chan);
  89. break;
  90. case 1:
  91. z8530_sync_close(d, slvl->chan);
  92. break;
  93. }
  94. return err;
  95. }
  96. slvl->chan->rx_function = sealevel_input;
  97. /*
  98. * Go go go
  99. */
  100. netif_start_queue(d);
  101. return 0;
  102. }
  103. static int sealevel_close(struct net_device *d)
  104. {
  105. struct slvl_device *slvl = dev_to_chan(d);
  106. int unit = slvl->channel;
  107. /*
  108. * Discard new frames
  109. */
  110. slvl->chan->rx_function = z8530_null_rx;
  111. hdlc_close(d);
  112. netif_stop_queue(d);
  113. switch (unit)
  114. {
  115. case 0:
  116. z8530_sync_dma_close(d, slvl->chan);
  117. break;
  118. case 1:
  119. z8530_sync_close(d, slvl->chan);
  120. break;
  121. }
  122. return 0;
  123. }
  124. static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
  125. {
  126. /* struct slvl_device *slvl=dev_to_chan(d);
  127. z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
  128. return hdlc_ioctl(d, ifr, cmd);
  129. }
  130. /*
  131. * Passed network frames, fire them downwind.
  132. */
  133. static int sealevel_queue_xmit(struct sk_buff *skb, struct net_device *d)
  134. {
  135. return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
  136. }
  137. static int sealevel_attach(struct net_device *dev, unsigned short encoding,
  138. unsigned short parity)
  139. {
  140. if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
  141. return 0;
  142. return -EINVAL;
  143. }
  144. static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
  145. {
  146. struct net_device *dev = alloc_hdlcdev(sv);
  147. if (!dev)
  148. return -1;
  149. dev_to_hdlc(dev)->attach = sealevel_attach;
  150. dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
  151. dev->open = sealevel_open;
  152. dev->stop = sealevel_close;
  153. dev->do_ioctl = sealevel_ioctl;
  154. dev->base_addr = iobase;
  155. dev->irq = irq;
  156. if (register_hdlc_device(dev)) {
  157. printk(KERN_ERR "sealevel: unable to register HDLC device\n");
  158. free_netdev(dev);
  159. return -1;
  160. }
  161. sv->chan->netdevice = dev;
  162. return 0;
  163. }
  164. /*
  165. * Allocate and setup Sealevel board.
  166. */
  167. static __init struct slvl_board *slvl_init(int iobase, int irq,
  168. int txdma, int rxdma, int slow)
  169. {
  170. struct z8530_dev *dev;
  171. struct slvl_board *b;
  172. /*
  173. * Get the needed I/O space
  174. */
  175. if (!request_region(iobase, 8, "Sealevel 4021")) {
  176. printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n",
  177. iobase);
  178. return NULL;
  179. }
  180. b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
  181. if (!b)
  182. goto err_kzalloc;
  183. b->dev[0].chan = &b->board.chanA;
  184. b->dev[0].channel = 0;
  185. b->dev[1].chan = &b->board.chanB;
  186. b->dev[1].channel = 1;
  187. dev = &b->board;
  188. /*
  189. * Stuff in the I/O addressing
  190. */
  191. dev->active = 0;
  192. b->iobase = iobase;
  193. /*
  194. * Select 8530 delays for the old board
  195. */
  196. if (slow)
  197. iobase |= Z8530_PORT_SLEEP;
  198. dev->chanA.ctrlio = iobase + 1;
  199. dev->chanA.dataio = iobase;
  200. dev->chanB.ctrlio = iobase + 3;
  201. dev->chanB.dataio = iobase + 2;
  202. dev->chanA.irqs = &z8530_nop;
  203. dev->chanB.irqs = &z8530_nop;
  204. /*
  205. * Assert DTR enable DMA
  206. */
  207. outb(3 | (1 << 7), b->iobase + 4);
  208. /* We want a fast IRQ for this device. Actually we'd like an even faster
  209. IRQ ;) - This is one driver RtLinux is made for */
  210. if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
  211. "SeaLevel", dev) < 0) {
  212. printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
  213. goto err_request_irq;
  214. }
  215. dev->irq = irq;
  216. dev->chanA.private = &b->dev[0];
  217. dev->chanB.private = &b->dev[1];
  218. dev->chanA.dev = dev;
  219. dev->chanB.dev = dev;
  220. dev->chanA.txdma = 3;
  221. dev->chanA.rxdma = 1;
  222. if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
  223. goto err_dma_tx;
  224. if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
  225. goto err_dma_rx;
  226. disable_irq(irq);
  227. /*
  228. * Begin normal initialise
  229. */
  230. if (z8530_init(dev) != 0) {
  231. printk(KERN_ERR "Z8530 series device not found.\n");
  232. enable_irq(irq);
  233. goto free_hw;
  234. }
  235. if (dev->type == Z85C30) {
  236. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
  237. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
  238. } else {
  239. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
  240. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
  241. }
  242. /*
  243. * Now we can take the IRQ
  244. */
  245. enable_irq(irq);
  246. if (slvl_setup(&b->dev[0], iobase, irq))
  247. goto free_hw;
  248. if (slvl_setup(&b->dev[1], iobase, irq))
  249. goto free_netdev0;
  250. z8530_describe(dev, "I/O", iobase);
  251. dev->active = 1;
  252. return b;
  253. free_netdev0:
  254. unregister_hdlc_device(b->dev[0].chan->netdevice);
  255. free_netdev(b->dev[0].chan->netdevice);
  256. free_hw:
  257. free_dma(dev->chanA.rxdma);
  258. err_dma_rx:
  259. free_dma(dev->chanA.txdma);
  260. err_dma_tx:
  261. free_irq(irq, dev);
  262. err_request_irq:
  263. kfree(b);
  264. err_kzalloc:
  265. release_region(iobase, 8);
  266. return NULL;
  267. }
  268. static void __exit slvl_shutdown(struct slvl_board *b)
  269. {
  270. int u;
  271. z8530_shutdown(&b->board);
  272. for (u = 0; u < 2; u++)
  273. {
  274. struct net_device *d = b->dev[u].chan->netdevice;
  275. unregister_hdlc_device(d);
  276. free_netdev(d);
  277. }
  278. free_irq(b->board.irq, &b->board);
  279. free_dma(b->board.chanA.rxdma);
  280. free_dma(b->board.chanA.txdma);
  281. /* DMA off on the card, drop DTR */
  282. outb(0, b->iobase);
  283. release_region(b->iobase, 8);
  284. kfree(b);
  285. }
  286. static int io=0x238;
  287. static int txdma=1;
  288. static int rxdma=3;
  289. static int irq=5;
  290. static int slow=0;
  291. module_param(io, int, 0);
  292. MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
  293. module_param(txdma, int, 0);
  294. MODULE_PARM_DESC(txdma, "Transmit DMA channel");
  295. module_param(rxdma, int, 0);
  296. MODULE_PARM_DESC(rxdma, "Receive DMA channel");
  297. module_param(irq, int, 0);
  298. MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
  299. module_param(slow, bool, 0);
  300. MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
  301. MODULE_AUTHOR("Alan Cox");
  302. MODULE_LICENSE("GPL");
  303. MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
  304. static struct slvl_board *slvl_unit;
  305. static int __init slvl_init_module(void)
  306. {
  307. slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
  308. return slvl_unit ? 0 : -ENODEV;
  309. }
  310. static void __exit slvl_cleanup_module(void)
  311. {
  312. if(slvl_unit)
  313. slvl_shutdown(slvl_unit);
  314. }
  315. module_init(slvl_init_module);
  316. module_exit(slvl_cleanup_module);