blob: c0235844a4d594e5cee2b243d4105b54c0f47eb9 [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);
71 c->netdevice->last_rx = jiffies;
72}
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
75 * We've been placed in the UP state
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020076 */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int sealevel_open(struct net_device *d)
79{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020080 struct slvl_device *slvl = dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int err = -1;
82 int unit = slvl->channel;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020085 * Link layer up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020088 switch (unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 {
90 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020091 err = z8530_sync_dma_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 break;
93 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020094 err = z8530_sync_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
96 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020097
98 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return err;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200100
101 err = hdlc_open(d);
102 if (err) {
103 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 case 0:
105 z8530_sync_dma_close(d, slvl->chan);
106 break;
107 case 1:
108 z8530_sync_close(d, slvl->chan);
109 break;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return err;
112 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200113
114 slvl->chan->rx_function = sealevel_input;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /*
117 * Go go go
118 */
119 netif_start_queue(d);
120 return 0;
121}
122
123static int sealevel_close(struct net_device *d)
124{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200125 struct slvl_device *slvl = dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int unit = slvl->channel;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
129 * Discard new frames
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200132 slvl->chan->rx_function = z8530_null_rx;
133
134 hdlc_close(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 netif_stop_queue(d);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200136
137 switch (unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 {
139 case 0:
140 z8530_sync_dma_close(d, slvl->chan);
141 break;
142 case 1:
143 z8530_sync_close(d, slvl->chan);
144 break;
145 }
146 return 0;
147}
148
149static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
150{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200151 /* struct slvl_device *slvl=dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200153 return hdlc_ioctl(d, ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200157 * Passed network frames, fire them downwind.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int sealevel_queue_xmit(struct sk_buff *skb, struct net_device *d)
161{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200162 return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200165static int sealevel_attach(struct net_device *dev, unsigned short encoding,
166 unsigned short parity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200168 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
169 return 0;
170 return -EINVAL;
171}
172
173static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
174{
175 struct net_device *dev = alloc_hdlcdev(sv);
176 if (!dev)
177 return -1;
178
179 dev_to_hdlc(dev)->attach = sealevel_attach;
180 dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
181 dev->open = sealevel_open;
182 dev->stop = sealevel_close;
183 dev->do_ioctl = sealevel_ioctl;
184 dev->base_addr = iobase;
185 dev->irq = irq;
186
187 if (register_hdlc_device(dev)) {
188 printk(KERN_ERR "sealevel: unable to register HDLC device\n");
189 free_netdev(dev);
190 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200192
193 sv->chan->netdevice = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 0;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/*
199 * Allocate and setup Sealevel board.
200 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200201
202static __init struct slvl_board *slvl_init(int iobase, int irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int txdma, int rxdma, int slow)
204{
205 struct z8530_dev *dev;
206 struct slvl_board *b;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /*
209 * Get the needed I/O space
210 */
211
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200212 if (!request_region(iobase, 8, "Sealevel 4021")) {
213 printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n",
214 iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
216 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200217
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700218 b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200219 if (!b)
220 goto err_kzalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200222 b->dev[0].chan = &b->board.chanA;
223 b->dev[0].channel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200225 b->dev[1].chan = &b->board.chanB;
226 b->dev[1].channel = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 dev = &b->board;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
231 * Stuff in the I/O addressing
232 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dev->active = 0;
235
236 b->iobase = iobase;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /*
239 * Select 8530 delays for the old board
240 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200241
242 if (slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 iobase |= Z8530_PORT_SLEEP;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200244
245 dev->chanA.ctrlio = iobase + 1;
246 dev->chanA.dataio = iobase;
247 dev->chanB.ctrlio = iobase + 3;
248 dev->chanB.dataio = iobase + 2;
249
250 dev->chanA.irqs = &z8530_nop;
251 dev->chanB.irqs = &z8530_nop;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
254 * Assert DTR enable DMA
255 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200256
257 outb(3 | (1 << 7), b->iobase + 4);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* We want a fast IRQ for this device. Actually we'd like an even faster
261 IRQ ;) - This is one driver RtLinux is made for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200263 if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
264 "SeaLevel", dev) < 0) {
265 printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
266 goto err_request_irq;
267 }
268
269 dev->irq = irq;
270 dev->chanA.private = &b->dev[0];
271 dev->chanB.private = &b->dev[1];
272 dev->chanA.dev = dev;
273 dev->chanB.dev = dev;
274
275 dev->chanA.txdma = 3;
276 dev->chanA.rxdma = 1;
277 if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
278 goto err_dma_tx;
279
280 if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
281 goto err_dma_rx;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 disable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /*
286 * Begin normal initialise
287 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200288
289 if (z8530_init(dev) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 printk(KERN_ERR "Z8530 series device not found.\n");
291 enable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200292 goto free_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200294 if (dev->type == Z85C30) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
296 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200297 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
299 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
300 }
301
302 /*
303 * Now we can take the IRQ
304 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 enable_irq(irq);
307
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200308 if (slvl_setup(&b->dev[0], iobase, irq))
309 goto free_hw;
310 if (slvl_setup(&b->dev[1], iobase, irq))
311 goto free_netdev0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 z8530_describe(dev, "I/O", iobase);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200314 dev->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return b;
316
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200317free_netdev0:
318 unregister_hdlc_device(b->dev[0].chan->netdevice);
319 free_netdev(b->dev[0].chan->netdevice);
320free_hw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 free_dma(dev->chanA.rxdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200322err_dma_rx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 free_dma(dev->chanA.txdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200324err_dma_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 free_irq(irq, dev);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200326err_request_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 kfree(b);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200328err_kzalloc:
329 release_region(iobase, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return NULL;
331}
332
333static void __exit slvl_shutdown(struct slvl_board *b)
334{
335 int u;
336
337 z8530_shutdown(&b->board);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200338
339 for (u = 0; u < 2; u++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 {
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200341 struct net_device *d = b->dev[u].chan->netdevice;
342 unregister_hdlc_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 free_netdev(d);
344 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 free_irq(b->board.irq, &b->board);
347 free_dma(b->board.chanA.rxdma);
348 free_dma(b->board.chanA.txdma);
349 /* DMA off on the card, drop DTR */
350 outb(0, b->iobase);
351 release_region(b->iobase, 8);
352 kfree(b);
353}
354
355
356static int io=0x238;
357static int txdma=1;
358static int rxdma=3;
359static int irq=5;
360static int slow=0;
361
362module_param(io, int, 0);
363MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
364module_param(txdma, int, 0);
365MODULE_PARM_DESC(txdma, "Transmit DMA channel");
366module_param(rxdma, int, 0);
367MODULE_PARM_DESC(rxdma, "Receive DMA channel");
368module_param(irq, int, 0);
369MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
370module_param(slow, bool, 0);
371MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
372
373MODULE_AUTHOR("Alan Cox");
374MODULE_LICENSE("GPL");
375MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
376
377static struct slvl_board *slvl_unit;
378
379static int __init slvl_init_module(void)
380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
382
383 return slvl_unit ? 0 : -ENODEV;
384}
385
386static void __exit slvl_cleanup_module(void)
387{
388 if(slvl_unit)
389 slvl_shutdown(slvl_unit);
390}
391
392module_init(slvl_init_module);
393module_exit(slvl_cleanup_module);