x86, delay: tsc based udelay should have rdtsc_barrier
[linux-2.6/mini2440.git] / drivers / net / wan / sealevel.c
blob23b26902745310b5d93fca475467e25a6487fb23
1 /*
2 * Sealevel Systems 4021 driver.
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.
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>
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 <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"
35 struct slvl_device
37 struct z8530_channel *chan;
38 int channel;
42 struct slvl_board
44 struct slvl_device dev[2];
45 struct z8530_dev board;
46 int iobase;
50 * Network driver support routines
53 static inline struct slvl_device* dev_to_chan(struct net_device *dev)
55 return (struct slvl_device *)dev_to_hdlc(dev)->priv;
59 * Frame receive. Simple for our card as we do HDLC and there
60 * is no funny garbage involved
63 static void sealevel_input(struct z8530_channel *c, struct sk_buff *skb)
65 /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
66 skb_trim(skb, skb->len - 2);
67 skb->protocol = hdlc_type_trans(skb, c->netdevice);
68 skb_reset_mac_header(skb);
69 skb->dev = c->netdevice;
70 netif_rx(skb);
74 * We've been placed in the UP state
77 static int sealevel_open(struct net_device *d)
79 struct slvl_device *slvl = dev_to_chan(d);
80 int err = -1;
81 int unit = slvl->channel;
84 * Link layer up.
87 switch (unit)
89 case 0:
90 err = z8530_sync_dma_open(d, slvl->chan);
91 break;
92 case 1:
93 err = z8530_sync_open(d, slvl->chan);
94 break;
97 if (err)
98 return err;
100 err = hdlc_open(d);
101 if (err) {
102 switch (unit) {
103 case 0:
104 z8530_sync_dma_close(d, slvl->chan);
105 break;
106 case 1:
107 z8530_sync_close(d, slvl->chan);
108 break;
110 return err;
113 slvl->chan->rx_function = sealevel_input;
116 * Go go go
118 netif_start_queue(d);
119 return 0;
122 static int sealevel_close(struct net_device *d)
124 struct slvl_device *slvl = dev_to_chan(d);
125 int unit = slvl->channel;
128 * Discard new frames
131 slvl->chan->rx_function = z8530_null_rx;
133 hdlc_close(d);
134 netif_stop_queue(d);
136 switch (unit)
138 case 0:
139 z8530_sync_dma_close(d, slvl->chan);
140 break;
141 case 1:
142 z8530_sync_close(d, slvl->chan);
143 break;
145 return 0;
148 static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
150 /* struct slvl_device *slvl=dev_to_chan(d);
151 z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
152 return hdlc_ioctl(d, ifr, cmd);
156 * Passed network frames, fire them downwind.
159 static int sealevel_queue_xmit(struct sk_buff *skb, struct net_device *d)
161 return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
164 static int sealevel_attach(struct net_device *dev, unsigned short encoding,
165 unsigned short parity)
167 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
168 return 0;
169 return -EINVAL;
172 static const struct net_device_ops sealevel_ops = {
173 .ndo_open = sealevel_open,
174 .ndo_stop = sealevel_close,
175 .ndo_change_mtu = hdlc_change_mtu,
176 .ndo_start_xmit = hdlc_start_xmit,
177 .ndo_do_ioctl = sealevel_ioctl,
180 static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
182 struct net_device *dev = alloc_hdlcdev(sv);
183 if (!dev)
184 return -1;
186 dev_to_hdlc(dev)->attach = sealevel_attach;
187 dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
188 dev->netdev_ops = &sealevel_ops;
189 dev->base_addr = iobase;
190 dev->irq = irq;
192 if (register_hdlc_device(dev)) {
193 printk(KERN_ERR "sealevel: unable to register HDLC device\n");
194 free_netdev(dev);
195 return -1;
198 sv->chan->netdevice = dev;
199 return 0;
204 * Allocate and setup Sealevel board.
207 static __init struct slvl_board *slvl_init(int iobase, int irq,
208 int txdma, int rxdma, int slow)
210 struct z8530_dev *dev;
211 struct slvl_board *b;
214 * Get the needed I/O space
217 if (!request_region(iobase, 8, "Sealevel 4021")) {
218 printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n",
219 iobase);
220 return NULL;
223 b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
224 if (!b)
225 goto err_kzalloc;
227 b->dev[0].chan = &b->board.chanA;
228 b->dev[0].channel = 0;
230 b->dev[1].chan = &b->board.chanB;
231 b->dev[1].channel = 1;
233 dev = &b->board;
236 * Stuff in the I/O addressing
239 dev->active = 0;
241 b->iobase = iobase;
244 * Select 8530 delays for the old board
247 if (slow)
248 iobase |= Z8530_PORT_SLEEP;
250 dev->chanA.ctrlio = iobase + 1;
251 dev->chanA.dataio = iobase;
252 dev->chanB.ctrlio = iobase + 3;
253 dev->chanB.dataio = iobase + 2;
255 dev->chanA.irqs = &z8530_nop;
256 dev->chanB.irqs = &z8530_nop;
259 * Assert DTR enable DMA
262 outb(3 | (1 << 7), b->iobase + 4);
265 /* We want a fast IRQ for this device. Actually we'd like an even faster
266 IRQ ;) - This is one driver RtLinux is made for */
268 if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
269 "SeaLevel", dev) < 0) {
270 printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
271 goto err_request_irq;
274 dev->irq = irq;
275 dev->chanA.private = &b->dev[0];
276 dev->chanB.private = &b->dev[1];
277 dev->chanA.dev = dev;
278 dev->chanB.dev = dev;
280 dev->chanA.txdma = 3;
281 dev->chanA.rxdma = 1;
282 if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
283 goto err_dma_tx;
285 if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
286 goto err_dma_rx;
288 disable_irq(irq);
291 * Begin normal initialise
294 if (z8530_init(dev) != 0) {
295 printk(KERN_ERR "Z8530 series device not found.\n");
296 enable_irq(irq);
297 goto free_hw;
299 if (dev->type == Z85C30) {
300 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
301 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
302 } else {
303 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
304 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
308 * Now we can take the IRQ
311 enable_irq(irq);
313 if (slvl_setup(&b->dev[0], iobase, irq))
314 goto free_hw;
315 if (slvl_setup(&b->dev[1], iobase, irq))
316 goto free_netdev0;
318 z8530_describe(dev, "I/O", iobase);
319 dev->active = 1;
320 return b;
322 free_netdev0:
323 unregister_hdlc_device(b->dev[0].chan->netdevice);
324 free_netdev(b->dev[0].chan->netdevice);
325 free_hw:
326 free_dma(dev->chanA.rxdma);
327 err_dma_rx:
328 free_dma(dev->chanA.txdma);
329 err_dma_tx:
330 free_irq(irq, dev);
331 err_request_irq:
332 kfree(b);
333 err_kzalloc:
334 release_region(iobase, 8);
335 return NULL;
338 static void __exit slvl_shutdown(struct slvl_board *b)
340 int u;
342 z8530_shutdown(&b->board);
344 for (u = 0; u < 2; u++)
346 struct net_device *d = b->dev[u].chan->netdevice;
347 unregister_hdlc_device(d);
348 free_netdev(d);
351 free_irq(b->board.irq, &b->board);
352 free_dma(b->board.chanA.rxdma);
353 free_dma(b->board.chanA.txdma);
354 /* DMA off on the card, drop DTR */
355 outb(0, b->iobase);
356 release_region(b->iobase, 8);
357 kfree(b);
361 static int io=0x238;
362 static int txdma=1;
363 static int rxdma=3;
364 static int irq=5;
365 static int slow=0;
367 module_param(io, int, 0);
368 MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
369 module_param(txdma, int, 0);
370 MODULE_PARM_DESC(txdma, "Transmit DMA channel");
371 module_param(rxdma, int, 0);
372 MODULE_PARM_DESC(rxdma, "Receive DMA channel");
373 module_param(irq, int, 0);
374 MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
375 module_param(slow, bool, 0);
376 MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
378 MODULE_AUTHOR("Alan Cox");
379 MODULE_LICENSE("GPL");
380 MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
382 static struct slvl_board *slvl_unit;
384 static int __init slvl_init_module(void)
386 slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
388 return slvl_unit ? 0 : -ENODEV;
391 static void __exit slvl_cleanup_module(void)
393 if(slvl_unit)
394 slvl_shutdown(slvl_unit);
397 module_init(slvl_init_module);
398 module_exit(slvl_cleanup_module);