blob: 0941a26f6e3f9b94d794bc2f43ecce49a35290cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020011 * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 */
14
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>
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020023#include <linux/hdlc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/ioport.h>
25#include <linux/init.h>
26#include <net/arp.h>
27
Al Viro82729972005-12-06 05:53:04 -050028#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/io.h>
30#include <asm/dma.h>
31#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "z85230.h"
33
34
35struct slvl_device
36{
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct z8530_channel *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 int channel;
39};
40
41
42struct slvl_board
43{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020044 struct slvl_device dev[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct z8530_dev board;
46 int iobase;
47};
48
49/*
50 * Network driver support routines
51 */
52
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020053static inline struct slvl_device* dev_to_chan(struct net_device *dev)
54{
55 return (struct slvl_device *)dev_to_hdlc(dev)->priv;
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020059 * Frame receive. Simple for our card as we do HDLC and there
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * is no funny garbage involved
61 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void sealevel_input(struct z8530_channel *c, struct sk_buff *skb)
64{
65 /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020066 skb_trim(skb, skb->len - 2);
67 skb->protocol = hdlc_type_trans(skb, c->netdevice);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070068 skb_reset_mac_header(skb);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020069 skb->dev = c->netdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
74 * We've been placed in the UP state
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020075 */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int sealevel_open(struct net_device *d)
78{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020079 struct slvl_device *slvl = dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int err = -1;
81 int unit = slvl->channel;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020084 * Link layer up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020087 switch (unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 {
89 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020090 err = z8530_sync_dma_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 break;
92 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020093 err = z8530_sync_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 break;
95 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020096
97 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return err;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020099
100 err = hdlc_open(d);
101 if (err) {
102 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 case 0:
104 z8530_sync_dma_close(d, slvl->chan);
105 break;
106 case 1:
107 z8530_sync_close(d, slvl->chan);
108 break;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return err;
111 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200112
113 slvl->chan->rx_function = sealevel_input;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * Go go go
117 */
118 netif_start_queue(d);
119 return 0;
120}
121
122static int sealevel_close(struct net_device *d)
123{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200124 struct slvl_device *slvl = dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int unit = slvl->channel;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * Discard new frames
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200131 slvl->chan->rx_function = z8530_null_rx;
132
133 hdlc_close(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 netif_stop_queue(d);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200135
136 switch (unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
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;
144 }
145 return 0;
146}
147
148static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
149{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200150 /* struct slvl_device *slvl=dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200152 return hdlc_ioctl(d, ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200156 * Passed network frames, fire them downwind.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int sealevel_queue_xmit(struct sk_buff *skb, struct net_device *d)
160{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200161 return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200164static int sealevel_attach(struct net_device *dev, unsigned short encoding,
165 unsigned short parity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200167 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
168 return 0;
169 return -EINVAL;
170}
171
172static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
173{
174 struct net_device *dev = alloc_hdlcdev(sv);
175 if (!dev)
176 return -1;
177
178 dev_to_hdlc(dev)->attach = sealevel_attach;
179 dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
180 dev->open = sealevel_open;
181 dev->stop = sealevel_close;
182 dev->do_ioctl = sealevel_ioctl;
183 dev->base_addr = iobase;
184 dev->irq = irq;
185
186 if (register_hdlc_device(dev)) {
187 printk(KERN_ERR "sealevel: unable to register HDLC device\n");
188 free_netdev(dev);
189 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200191
192 sv->chan->netdevice = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197/*
198 * Allocate and setup Sealevel board.
199 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200200
201static __init struct slvl_board *slvl_init(int iobase, int irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int txdma, int rxdma, int slow)
203{
204 struct z8530_dev *dev;
205 struct slvl_board *b;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /*
208 * Get the needed I/O space
209 */
210
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200211 if (!request_region(iobase, 8, "Sealevel 4021")) {
212 printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n",
213 iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return NULL;
215 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200216
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700217 b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200218 if (!b)
219 goto err_kzalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200221 b->dev[0].chan = &b->board.chanA;
222 b->dev[0].channel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200224 b->dev[1].chan = &b->board.chanB;
225 b->dev[1].channel = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 dev = &b->board;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
230 * Stuff in the I/O addressing
231 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 dev->active = 0;
234
235 b->iobase = iobase;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
238 * Select 8530 delays for the old board
239 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200240
241 if (slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 iobase |= Z8530_PORT_SLEEP;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200243
244 dev->chanA.ctrlio = iobase + 1;
245 dev->chanA.dataio = iobase;
246 dev->chanB.ctrlio = iobase + 3;
247 dev->chanB.dataio = iobase + 2;
248
249 dev->chanA.irqs = &z8530_nop;
250 dev->chanB.irqs = &z8530_nop;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /*
253 * Assert DTR enable DMA
254 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200255
256 outb(3 | (1 << 7), b->iobase + 4);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* We want a fast IRQ for this device. Actually we'd like an even faster
260 IRQ ;) - This is one driver RtLinux is made for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200262 if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
263 "SeaLevel", dev) < 0) {
264 printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
265 goto err_request_irq;
266 }
267
268 dev->irq = irq;
269 dev->chanA.private = &b->dev[0];
270 dev->chanB.private = &b->dev[1];
271 dev->chanA.dev = dev;
272 dev->chanB.dev = dev;
273
274 dev->chanA.txdma = 3;
275 dev->chanA.rxdma = 1;
276 if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
277 goto err_dma_tx;
278
279 if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
280 goto err_dma_rx;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 disable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /*
285 * Begin normal initialise
286 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200287
288 if (z8530_init(dev) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 printk(KERN_ERR "Z8530 series device not found.\n");
290 enable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200291 goto free_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200293 if (dev->type == Z85C30) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
295 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200296 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
298 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
299 }
300
301 /*
302 * Now we can take the IRQ
303 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 enable_irq(irq);
306
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200307 if (slvl_setup(&b->dev[0], iobase, irq))
308 goto free_hw;
309 if (slvl_setup(&b->dev[1], iobase, irq))
310 goto free_netdev0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 z8530_describe(dev, "I/O", iobase);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200313 dev->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return b;
315
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200316free_netdev0:
317 unregister_hdlc_device(b->dev[0].chan->netdevice);
318 free_netdev(b->dev[0].chan->netdevice);
319free_hw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 free_dma(dev->chanA.rxdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200321err_dma_rx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 free_dma(dev->chanA.txdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200323err_dma_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 free_irq(irq, dev);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200325err_request_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 kfree(b);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200327err_kzalloc:
328 release_region(iobase, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return NULL;
330}
331
332static void __exit slvl_shutdown(struct slvl_board *b)
333{
334 int u;
335
336 z8530_shutdown(&b->board);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200337
338 for (u = 0; u < 2; u++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 {
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200340 struct net_device *d = b->dev[u].chan->netdevice;
341 unregister_hdlc_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 free_netdev(d);
343 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 free_irq(b->board.irq, &b->board);
346 free_dma(b->board.chanA.rxdma);
347 free_dma(b->board.chanA.txdma);
348 /* DMA off on the card, drop DTR */
349 outb(0, b->iobase);
350 release_region(b->iobase, 8);
351 kfree(b);
352}
353
354
355static int io=0x238;
356static int txdma=1;
357static int rxdma=3;
358static int irq=5;
359static int slow=0;
360
361module_param(io, int, 0);
362MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
363module_param(txdma, int, 0);
364MODULE_PARM_DESC(txdma, "Transmit DMA channel");
365module_param(rxdma, int, 0);
366MODULE_PARM_DESC(rxdma, "Receive DMA channel");
367module_param(irq, int, 0);
368MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
369module_param(slow, bool, 0);
370MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
371
372MODULE_AUTHOR("Alan Cox");
373MODULE_LICENSE("GPL");
374MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
375
376static struct slvl_board *slvl_unit;
377
378static int __init slvl_init_module(void)
379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
381
382 return slvl_unit ? 0 : -ENODEV;
383}
384
385static void __exit slvl_cleanup_module(void)
386{
387 if(slvl_unit)
388 slvl_shutdown(slvl_unit);
389}
390
391module_init(slvl_init_module);
392module_exit(slvl_cleanup_module);