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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/net.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/delay.h>
  23. #include <linux/hdlc.h>
  24. #include <linux/ioport.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <net/arp.h>
  28. #include <asm/irq.h>
  29. #include <asm/io.h>
  30. #include <asm/dma.h>
  31. #include <asm/byteorder.h>
  32. #include "z85230.h"
  33. struct slvl_device
  34. {
  35. struct z8530_channel *chan;
  36. int channel;
  37. };
  38. struct slvl_board
  39. {
  40. struct slvl_device dev[2];
  41. struct z8530_dev board;
  42. int iobase;
  43. };
  44. /*
  45. * Network driver support routines
  46. */
  47. static inline struct slvl_device* dev_to_chan(struct net_device *dev)
  48. {
  49. return (struct slvl_device *)dev_to_hdlc(dev)->priv;
  50. }
  51. /*
  52. * Frame receive. Simple for our card as we do HDLC and there
  53. * is no funny garbage involved
  54. */
  55. static void sealevel_input(struct z8530_channel *c, struct sk_buff *skb)
  56. {
  57. /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
  58. skb_trim(skb, skb->len - 2);
  59. skb->protocol = hdlc_type_trans(skb, c->netdevice);
  60. skb_reset_mac_header(skb);
  61. skb->dev = c->netdevice;
  62. netif_rx(skb);
  63. }
  64. /*
  65. * We've been placed in the UP state
  66. */
  67. static int sealevel_open(struct net_device *d)
  68. {
  69. struct slvl_device *slvl = dev_to_chan(d);
  70. int err = -1;
  71. int unit = slvl->channel;
  72. /*
  73. * Link layer up.
  74. */
  75. switch (unit) {
  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. 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. pr_err("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. pr_warn("I/O 0x%X already in use\n", iobase);
  183. return NULL;
  184. }
  185. b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
  186. if (!b)
  187. goto err_kzalloc;
  188. b->dev[0].chan = &b->board.chanA;
  189. b->dev[0].channel = 0;
  190. b->dev[1].chan = &b->board.chanB;
  191. b->dev[1].channel = 1;
  192. dev = &b->board;
  193. /*
  194. * Stuff in the I/O addressing
  195. */
  196. dev->active = 0;
  197. b->iobase = iobase;
  198. /*
  199. * Select 8530 delays for the old board
  200. */
  201. if (slow)
  202. iobase |= Z8530_PORT_SLEEP;
  203. dev->chanA.ctrlio = iobase + 1;
  204. dev->chanA.dataio = iobase;
  205. dev->chanB.ctrlio = iobase + 3;
  206. dev->chanB.dataio = iobase + 2;
  207. dev->chanA.irqs = &z8530_nop;
  208. dev->chanB.irqs = &z8530_nop;
  209. /*
  210. * Assert DTR enable DMA
  211. */
  212. outb(3 | (1 << 7), b->iobase + 4);
  213. /* We want a fast IRQ for this device. Actually we'd like an even faster
  214. IRQ ;) - This is one driver RtLinux is made for */
  215. if (request_irq(irq, z8530_interrupt, IRQF_DISABLED,
  216. "SeaLevel", dev) < 0) {
  217. pr_warn("IRQ %d already in use\n", irq);
  218. goto err_request_irq;
  219. }
  220. dev->irq = irq;
  221. dev->chanA.private = &b->dev[0];
  222. dev->chanB.private = &b->dev[1];
  223. dev->chanA.dev = dev;
  224. dev->chanB.dev = dev;
  225. dev->chanA.txdma = 3;
  226. dev->chanA.rxdma = 1;
  227. if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
  228. goto err_dma_tx;
  229. if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
  230. goto err_dma_rx;
  231. disable_irq(irq);
  232. /*
  233. * Begin normal initialise
  234. */
  235. if (z8530_init(dev) != 0) {
  236. pr_err("Z8530 series device not found\n");
  237. enable_irq(irq);
  238. goto free_hw;
  239. }
  240. if (dev->type == Z85C30) {
  241. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
  242. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
  243. } else {
  244. z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
  245. z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
  246. }
  247. /*
  248. * Now we can take the IRQ
  249. */
  250. enable_irq(irq);
  251. if (slvl_setup(&b->dev[0], iobase, irq))
  252. goto free_hw;
  253. if (slvl_setup(&b->dev[1], iobase, irq))
  254. goto free_netdev0;
  255. z8530_describe(dev, "I/O", iobase);
  256. dev->active = 1;
  257. return b;
  258. free_netdev0:
  259. unregister_hdlc_device(b->dev[0].chan->netdevice);
  260. free_netdev(b->dev[0].chan->netdevice);
  261. free_hw:
  262. free_dma(dev->chanA.rxdma);
  263. err_dma_rx:
  264. free_dma(dev->chanA.txdma);
  265. err_dma_tx:
  266. free_irq(irq, dev);
  267. err_request_irq:
  268. kfree(b);
  269. err_kzalloc:
  270. release_region(iobase, 8);
  271. return NULL;
  272. }
  273. static void __exit slvl_shutdown(struct slvl_board *b)
  274. {
  275. int u;
  276. z8530_shutdown(&b->board);
  277. for (u = 0; u < 2; u++) {
  278. struct net_device *d = b->dev[u].chan->netdevice;
  279. unregister_hdlc_device(d);
  280. free_netdev(d);
  281. }
  282. free_irq(b->board.irq, &b->board);
  283. free_dma(b->board.chanA.rxdma);
  284. free_dma(b->board.chanA.txdma);
  285. /* DMA off on the card, drop DTR */
  286. outb(0, b->iobase);
  287. release_region(b->iobase, 8);
  288. kfree(b);
  289. }
  290. static int io=0x238;
  291. static int txdma=1;
  292. static int rxdma=3;
  293. static int irq=5;
  294. static bool slow=false;
  295. module_param(io, int, 0);
  296. MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
  297. module_param(txdma, int, 0);
  298. MODULE_PARM_DESC(txdma, "Transmit DMA channel");
  299. module_param(rxdma, int, 0);
  300. MODULE_PARM_DESC(rxdma, "Receive DMA channel");
  301. module_param(irq, int, 0);
  302. MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
  303. module_param(slow, bool, 0);
  304. MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
  305. MODULE_AUTHOR("Alan Cox");
  306. MODULE_LICENSE("GPL");
  307. MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
  308. static struct slvl_board *slvl_unit;
  309. static int __init slvl_init_module(void)
  310. {
  311. slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
  312. return slvl_unit ? 0 : -ENODEV;
  313. }
  314. static void __exit slvl_cleanup_module(void)
  315. {
  316. if (slvl_unit)
  317. slvl_shutdown(slvl_unit);
  318. }
  319. module_init(slvl_init_module);
  320. module_exit(slvl_cleanup_module);