sealevel.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 netdev_tx_t sealevel_queue_xmit(struct sk_buff *skb,
  134. 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 const struct net_device_ops sealevel_ops = {
  146. .ndo_open = sealevel_open,
  147. .ndo_stop = sealevel_close,
  148. .ndo_change_mtu = hdlc_change_mtu,
  149. .ndo_start_xmit = hdlc_start_xmit,
  150. .ndo_do_ioctl = sealevel_ioctl,
  151. };
  152. static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
  153. {
  154. struct net_device *dev = alloc_hdlcdev(sv);
  155. if (!dev)
  156. return -1;
  157. dev_to_hdlc(dev)->attach = sealevel_attach;
  158. dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
  159. dev->netdev_ops = &sealevel_ops;
  160. dev->base_addr = iobase;
  161. dev->irq = irq;
  162. if (register_hdlc_device(dev)) {
  163. printk(KERN_ERR "sealevel: unable to register HDLC device\n");
  164. free_netdev(dev);
  165. return -1;
  166. }
  167. sv->chan->netdevice = dev;
  168. return 0;
  169. }
  170. /*
  171. * Allocate and setup Sealevel board.
  172. */
  173. static __init struct slvl_board *slvl_init(int iobase, int irq,
  174. int txdma, int rxdma, int slow)
  175. {
  176. struct z8530_dev *dev;
  177. struct slvl_board *b;
  178. /*
  179. * Get the needed I/O space
  180. */
  181. if (!request_region(iobase, 8, "Sealevel 4021")) {
  182. printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n",
  183. iobase);
  184. return NULL;
  185. }
  186. b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
  187. if (!b)
  188. goto err_kzalloc;
  189. b->dev[0].chan = &b->board.chanA;
  190. b->dev[0].channel = 0;
  191. b->dev[1].chan = &b->board.chanB;
  192. b->dev[1].channel = 1;
  193. dev = &b->board;
  194. /*
  195. * Stuff in the I/O addressing
  196. */
  197. dev->active = 0;
  198. b->iobase = iobase;
  199. /*
  200. * Select 8530 delays for the old board
  201. */
  202. if (slow)
  203. iobase |= Z8530_PORT_SLEEP;
  204. dev->chanA.ctrlio = iobase + 1;
  205. dev->chanA.dataio = iobase;
  206. dev->chanB.ctrlio = iobase + 3;
  207. dev->chanB.dataio = iobase + 2;
  208. dev->chanA.irqs = &z8530_nop;
  209. dev->chanB.irqs = &z8530_nop;
  210. /*
  211. * Assert DTR enable DMA
  212. */
  213. outb(3 | (1 << 7), b->iobase + 4);
  214. /* We want a fast IRQ for this device. Actually we'd like an even faster
  215. IRQ ;) - This is one driver RtLinux is made for */
  216. if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
  217. "SeaLevel", dev) < 0) {
  218. printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
  219. goto err_request_irq;
  220. }
  221. dev->irq = irq;
  222. dev->chanA.private = &b->dev[0];
  223. dev->chanB.private = &b->dev[1];
  224. dev->chanA.dev = dev;
  225. dev->chanB.dev = dev;
  226. dev->chanA.txdma = 3;
  227. dev->chanA.rxdma = 1;
  228. if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
  229. goto err_dma_tx;
  230. if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
  231. goto err_dma_rx;
  232. disable_irq(irq);
  233. /*
  234. * Begin normal initialise
  235. */
  236. if (z8530_init(dev) != 0) {
  237. printk(KERN_ERR "Z8530 series device not found.\n");
  238. enable_irq(irq);
  239. goto free_hw;
  240. }
  241. if (dev->type == Z85C30) {
  242. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
  243. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
  244. } else {
  245. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
  246. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
  247. }
  248. /*
  249. * Now we can take the IRQ
  250. */
  251. enable_irq(irq);
  252. if (slvl_setup(&b->dev[0], iobase, irq))
  253. goto free_hw;
  254. if (slvl_setup(&b->dev[1], iobase, irq))
  255. goto free_netdev0;
  256. z8530_describe(dev, "I/O", iobase);
  257. dev->active = 1;
  258. return b;
  259. free_netdev0:
  260. unregister_hdlc_device(b->dev[0].chan->netdevice);
  261. free_netdev(b->dev[0].chan->netdevice);
  262. free_hw:
  263. free_dma(dev->chanA.rxdma);
  264. err_dma_rx:
  265. free_dma(dev->chanA.txdma);
  266. err_dma_tx:
  267. free_irq(irq, dev);
  268. err_request_irq:
  269. kfree(b);
  270. err_kzalloc:
  271. release_region(iobase, 8);
  272. return NULL;
  273. }
  274. static void __exit slvl_shutdown(struct slvl_board *b)
  275. {
  276. int u;
  277. z8530_shutdown(&b->board);
  278. for (u = 0; u < 2; u++)
  279. {
  280. struct net_device *d = b->dev[u].chan->netdevice;
  281. unregister_hdlc_device(d);
  282. free_netdev(d);
  283. }
  284. free_irq(b->board.irq, &b->board);
  285. free_dma(b->board.chanA.rxdma);
  286. free_dma(b->board.chanA.txdma);
  287. /* DMA off on the card, drop DTR */
  288. outb(0, b->iobase);
  289. release_region(b->iobase, 8);
  290. kfree(b);
  291. }
  292. static int io=0x238;
  293. static int txdma=1;
  294. static int rxdma=3;
  295. static int irq=5;
  296. static int slow=0;
  297. module_param(io, int, 0);
  298. MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
  299. module_param(txdma, int, 0);
  300. MODULE_PARM_DESC(txdma, "Transmit DMA channel");
  301. module_param(rxdma, int, 0);
  302. MODULE_PARM_DESC(rxdma, "Receive DMA channel");
  303. module_param(irq, int, 0);
  304. MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
  305. module_param(slow, bool, 0);
  306. MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
  307. MODULE_AUTHOR("Alan Cox");
  308. MODULE_LICENSE("GPL");
  309. MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
  310. static struct slvl_board *slvl_unit;
  311. static int __init slvl_init_module(void)
  312. {
  313. slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
  314. return slvl_unit ? 0 : -ENODEV;
  315. }
  316. static void __exit slvl_cleanup_module(void)
  317. {
  318. if(slvl_unit)
  319. slvl_shutdown(slvl_unit);
  320. }
  321. module_init(slvl_init_module);
  322. module_exit(slvl_cleanup_module);