blob: 61249f489e3782e5be8ea90a9f15b69b5dde1e6e [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
Rudy Matela614c12a2009-12-02 01:26:01 -080087 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020089 err = z8530_sync_dma_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
91 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020092 err = z8530_sync_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
94 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020095
96 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return err;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020098
99 err = hdlc_open(d);
100 if (err) {
101 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 case 0:
103 z8530_sync_dma_close(d, slvl->chan);
104 break;
105 case 1:
106 z8530_sync_close(d, slvl->chan);
107 break;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return err;
110 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200111
112 slvl->chan->rx_function = sealevel_input;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
115 * Go go go
116 */
117 netif_start_queue(d);
118 return 0;
119}
120
121static int sealevel_close(struct net_device *d)
122{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200123 struct slvl_device *slvl = dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int unit = slvl->channel;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * Discard new frames
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200130 slvl->chan->rx_function = z8530_null_rx;
131
132 hdlc_close(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 netif_stop_queue(d);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200134
Rudy Matela614c12a2009-12-02 01:26:01 -0800135 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 case 0:
137 z8530_sync_dma_close(d, slvl->chan);
138 break;
139 case 1:
140 z8530_sync_close(d, slvl->chan);
141 break;
142 }
143 return 0;
144}
145
146static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
147{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200148 /* struct slvl_device *slvl=dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200150 return hdlc_ioctl(d, ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200154 * Passed network frames, fire them downwind.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200156
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000157static netdev_tx_t sealevel_queue_xmit(struct sk_buff *skb,
158 struct net_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200160 return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200163static int sealevel_attach(struct net_device *dev, unsigned short encoding,
164 unsigned short parity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200166 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
167 return 0;
168 return -EINVAL;
169}
170
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100171static const struct net_device_ops sealevel_ops = {
172 .ndo_open = sealevel_open,
173 .ndo_stop = sealevel_close,
174 .ndo_change_mtu = hdlc_change_mtu,
175 .ndo_start_xmit = hdlc_start_xmit,
176 .ndo_do_ioctl = sealevel_ioctl,
177};
178
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200179static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
180{
181 struct net_device *dev = alloc_hdlcdev(sv);
182 if (!dev)
183 return -1;
184
185 dev_to_hdlc(dev)->attach = sealevel_attach;
186 dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100187 dev->netdev_ops = &sealevel_ops;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200188 dev->base_addr = iobase;
189 dev->irq = irq;
190
191 if (register_hdlc_device(dev)) {
192 printk(KERN_ERR "sealevel: unable to register HDLC device\n");
193 free_netdev(dev);
194 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200196
197 sv->chan->netdevice = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*
203 * Allocate and setup Sealevel board.
204 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200205
206static __init struct slvl_board *slvl_init(int iobase, int irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int txdma, int rxdma, int slow)
208{
209 struct z8530_dev *dev;
210 struct slvl_board *b;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
213 * Get the needed I/O space
214 */
215
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200216 if (!request_region(iobase, 8, "Sealevel 4021")) {
217 printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n",
218 iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return NULL;
220 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200221
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700222 b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200223 if (!b)
224 goto err_kzalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200226 b->dev[0].chan = &b->board.chanA;
227 b->dev[0].channel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200229 b->dev[1].chan = &b->board.chanB;
230 b->dev[1].channel = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 dev = &b->board;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
235 * Stuff in the I/O addressing
236 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 dev->active = 0;
239
240 b->iobase = iobase;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /*
243 * Select 8530 delays for the old board
244 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200245
246 if (slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 iobase |= Z8530_PORT_SLEEP;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200248
249 dev->chanA.ctrlio = iobase + 1;
250 dev->chanA.dataio = iobase;
251 dev->chanB.ctrlio = iobase + 3;
252 dev->chanB.dataio = iobase + 2;
253
254 dev->chanA.irqs = &z8530_nop;
255 dev->chanB.irqs = &z8530_nop;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /*
258 * Assert DTR enable DMA
259 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200260
261 outb(3 | (1 << 7), b->iobase + 4);
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* We want a fast IRQ for this device. Actually we'd like an even faster
265 IRQ ;) - This is one driver RtLinux is made for */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Joe Perchesa0607fd2009-11-18 23:29:17 -0800267 if (request_irq(irq, z8530_interrupt, IRQF_DISABLED,
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200268 "SeaLevel", dev) < 0) {
269 printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
270 goto err_request_irq;
271 }
272
273 dev->irq = irq;
274 dev->chanA.private = &b->dev[0];
275 dev->chanB.private = &b->dev[1];
276 dev->chanA.dev = dev;
277 dev->chanB.dev = dev;
278
279 dev->chanA.txdma = 3;
280 dev->chanA.rxdma = 1;
281 if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
282 goto err_dma_tx;
283
284 if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
285 goto err_dma_rx;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 disable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /*
290 * Begin normal initialise
291 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200292
293 if (z8530_init(dev) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 printk(KERN_ERR "Z8530 series device not found.\n");
295 enable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200296 goto free_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200298 if (dev->type == Z85C30) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
300 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200301 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
303 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
304 }
305
306 /*
307 * Now we can take the IRQ
308 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 enable_irq(irq);
311
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200312 if (slvl_setup(&b->dev[0], iobase, irq))
313 goto free_hw;
314 if (slvl_setup(&b->dev[1], iobase, irq))
315 goto free_netdev0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 z8530_describe(dev, "I/O", iobase);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200318 dev->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return b;
320
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200321free_netdev0:
322 unregister_hdlc_device(b->dev[0].chan->netdevice);
323 free_netdev(b->dev[0].chan->netdevice);
324free_hw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 free_dma(dev->chanA.rxdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200326err_dma_rx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 free_dma(dev->chanA.txdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200328err_dma_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 free_irq(irq, dev);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200330err_request_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 kfree(b);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200332err_kzalloc:
333 release_region(iobase, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return NULL;
335}
336
337static void __exit slvl_shutdown(struct slvl_board *b)
338{
339 int u;
340
341 z8530_shutdown(&b->board);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200342
Rudy Matela614c12a2009-12-02 01:26:01 -0800343 for (u = 0; u < 2; u++) {
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200344 struct net_device *d = b->dev[u].chan->netdevice;
345 unregister_hdlc_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 free_netdev(d);
347 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 free_irq(b->board.irq, &b->board);
350 free_dma(b->board.chanA.rxdma);
351 free_dma(b->board.chanA.txdma);
352 /* DMA off on the card, drop DTR */
353 outb(0, b->iobase);
354 release_region(b->iobase, 8);
355 kfree(b);
356}
357
358
359static int io=0x238;
360static int txdma=1;
361static int rxdma=3;
362static int irq=5;
363static int slow=0;
364
365module_param(io, int, 0);
366MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
367module_param(txdma, int, 0);
368MODULE_PARM_DESC(txdma, "Transmit DMA channel");
369module_param(rxdma, int, 0);
370MODULE_PARM_DESC(rxdma, "Receive DMA channel");
371module_param(irq, int, 0);
372MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
373module_param(slow, bool, 0);
374MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
375
376MODULE_AUTHOR("Alan Cox");
377MODULE_LICENSE("GPL");
378MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
379
380static struct slvl_board *slvl_unit;
381
382static int __init slvl_init_module(void)
383{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
385
386 return slvl_unit ? 0 : -ENODEV;
387}
388
389static void __exit slvl_cleanup_module(void)
390{
Rudy Matela614c12a2009-12-02 01:26:01 -0800391 if (slvl_unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 slvl_shutdown(slvl_unit);
393}
394
395module_init(slvl_init_module);
396module_exit(slvl_cleanup_module);