blob: c56f2c252113a965845b1cc55c91fe412b672a85 [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
Joe Perches23efcb72011-06-26 19:01:35 +000015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/delay.h>
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020025#include <linux/hdlc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/ioport.h>
27#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/arp.h>
30
Al Viro82729972005-12-06 05:53:04 -050031#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/io.h>
33#include <asm/dma.h>
34#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "z85230.h"
36
37
38struct slvl_device
39{
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct z8530_channel *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 int channel;
42};
43
44
45struct slvl_board
46{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020047 struct slvl_device dev[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct z8530_dev board;
49 int iobase;
50};
51
52/*
53 * Network driver support routines
54 */
55
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020056static inline struct slvl_device* dev_to_chan(struct net_device *dev)
57{
58 return (struct slvl_device *)dev_to_hdlc(dev)->priv;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020062 * Frame receive. Simple for our card as we do HDLC and there
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * is no funny garbage involved
64 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void sealevel_input(struct z8530_channel *c, struct sk_buff *skb)
67{
68 /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020069 skb_trim(skb, skb->len - 2);
70 skb->protocol = hdlc_type_trans(skb, c->netdevice);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070071 skb_reset_mac_header(skb);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020072 skb->dev = c->netdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * We've been placed in the UP state
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020078 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int sealevel_open(struct net_device *d)
81{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020082 struct slvl_device *slvl = dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int err = -1;
84 int unit = slvl->channel;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 /*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020087 * Link layer up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89
Rudy Matela614c12a2009-12-02 01:26:01 -080090 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020092 err = z8530_sync_dma_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
94 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020095 err = z8530_sync_open(d, slvl->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
97 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020098
99 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return err;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200101
102 err = hdlc_open(d);
103 if (err) {
104 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 case 0:
106 z8530_sync_dma_close(d, slvl->chan);
107 break;
108 case 1:
109 z8530_sync_close(d, slvl->chan);
110 break;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return err;
113 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200114
115 slvl->chan->rx_function = sealevel_input;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /*
118 * Go go go
119 */
120 netif_start_queue(d);
121 return 0;
122}
123
124static int sealevel_close(struct net_device *d)
125{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200126 struct slvl_device *slvl = dev_to_chan(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int unit = slvl->channel;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
130 * Discard new frames
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200133 slvl->chan->rx_function = z8530_null_rx;
134
135 hdlc_close(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 netif_stop_queue(d);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200137
Rudy Matela614c12a2009-12-02 01:26:01 -0800138 switch (unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 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
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000160static netdev_tx_t sealevel_queue_xmit(struct sk_buff *skb,
161 struct net_device *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200163 return z8530_queue_xmit(dev_to_chan(d)->chan, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200166static int sealevel_attach(struct net_device *dev, unsigned short encoding,
167 unsigned short parity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200169 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
170 return 0;
171 return -EINVAL;
172}
173
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100174static const struct net_device_ops sealevel_ops = {
175 .ndo_open = sealevel_open,
176 .ndo_stop = sealevel_close,
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100177 .ndo_start_xmit = hdlc_start_xmit,
178 .ndo_do_ioctl = sealevel_ioctl,
179};
180
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200181static int slvl_setup(struct slvl_device *sv, int iobase, int irq)
182{
183 struct net_device *dev = alloc_hdlcdev(sv);
184 if (!dev)
185 return -1;
186
187 dev_to_hdlc(dev)->attach = sealevel_attach;
188 dev_to_hdlc(dev)->xmit = sealevel_queue_xmit;
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100189 dev->netdev_ops = &sealevel_ops;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200190 dev->base_addr = iobase;
191 dev->irq = irq;
192
193 if (register_hdlc_device(dev)) {
Joe Perches23efcb72011-06-26 19:01:35 +0000194 pr_err("unable to register HDLC device\n");
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200195 free_netdev(dev);
196 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200198
199 sv->chan->netdevice = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/*
205 * Allocate and setup Sealevel board.
206 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200207
208static __init struct slvl_board *slvl_init(int iobase, int irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int txdma, int rxdma, int slow)
210{
211 struct z8530_dev *dev;
212 struct slvl_board *b;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /*
215 * Get the needed I/O space
216 */
217
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200218 if (!request_region(iobase, 8, "Sealevel 4021")) {
Joe Perches23efcb72011-06-26 19:01:35 +0000219 pr_warn("I/O 0x%X already in use\n", iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return NULL;
221 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200222
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700223 b = kzalloc(sizeof(struct slvl_board), GFP_KERNEL);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200224 if (!b)
225 goto err_kzalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200227 b->dev[0].chan = &b->board.chanA;
228 b->dev[0].channel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200230 b->dev[1].chan = &b->board.chanB;
231 b->dev[1].channel = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 dev = &b->board;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
236 * Stuff in the I/O addressing
237 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 dev->active = 0;
240
241 b->iobase = iobase;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /*
244 * Select 8530 delays for the old board
245 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200246
247 if (slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 iobase |= Z8530_PORT_SLEEP;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200249
250 dev->chanA.ctrlio = iobase + 1;
251 dev->chanA.dataio = iobase;
252 dev->chanB.ctrlio = iobase + 3;
253 dev->chanB.dataio = iobase + 2;
254
255 dev->chanA.irqs = &z8530_nop;
256 dev->chanB.irqs = &z8530_nop;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /*
259 * Assert DTR enable DMA
260 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200261
262 outb(3 | (1 << 7), b->iobase + 4);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Michael Opdenacker0ca45202013-10-05 06:45:30 +0200268 if (request_irq(irq, z8530_interrupt, 0,
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200269 "SeaLevel", dev) < 0) {
Joe Perches23efcb72011-06-26 19:01:35 +0000270 pr_warn("IRQ %d already in use\n", irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200271 goto err_request_irq;
272 }
273
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;
279
280 dev->chanA.txdma = 3;
281 dev->chanA.rxdma = 1;
282 if (request_dma(dev->chanA.txdma, "SeaLevel (TX)"))
283 goto err_dma_tx;
284
285 if (request_dma(dev->chanA.rxdma, "SeaLevel (RX)"))
286 goto err_dma_rx;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 disable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /*
291 * Begin normal initialise
292 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200293
294 if (z8530_init(dev) != 0) {
Joe Perches23efcb72011-06-26 19:01:35 +0000295 pr_err("Z8530 series device not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 enable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200297 goto free_hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200299 if (dev->type == Z85C30) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
301 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200302 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
304 z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
305 }
306
307 /*
308 * Now we can take the IRQ
309 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 enable_irq(irq);
312
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200313 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 z8530_describe(dev, "I/O", iobase);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200319 dev->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return b;
321
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200322free_netdev0:
323 unregister_hdlc_device(b->dev[0].chan->netdevice);
324 free_netdev(b->dev[0].chan->netdevice);
325free_hw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 free_dma(dev->chanA.rxdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200327err_dma_rx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 free_dma(dev->chanA.txdma);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200329err_dma_tx:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 free_irq(irq, dev);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200331err_request_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 kfree(b);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200333err_kzalloc:
334 release_region(iobase, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return NULL;
336}
337
338static void __exit slvl_shutdown(struct slvl_board *b)
339{
340 int u;
341
342 z8530_shutdown(&b->board);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200343
Rudy Matela614c12a2009-12-02 01:26:01 -0800344 for (u = 0; u < 2; u++) {
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200345 struct net_device *d = b->dev[u].chan->netdevice;
346 unregister_hdlc_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 free_netdev(d);
348 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 free_irq(b->board.irq, &b->board);
351 free_dma(b->board.chanA.rxdma);
352 free_dma(b->board.chanA.txdma);
353 /* DMA off on the card, drop DTR */
354 outb(0, b->iobase);
355 release_region(b->iobase, 8);
356 kfree(b);
357}
358
359
360static int io=0x238;
361static int txdma=1;
362static int rxdma=3;
363static int irq=5;
Rusty Russelleb939922011-12-19 14:08:01 +0000364static bool slow=false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
David Howellsaf28a032017-04-04 16:54:27 +0100366module_param_hw(io, int, ioport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
David Howellsaf28a032017-04-04 16:54:27 +0100368module_param_hw(txdma, int, dma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369MODULE_PARM_DESC(txdma, "Transmit DMA channel");
David Howellsaf28a032017-04-04 16:54:27 +0100370module_param_hw(rxdma, int, dma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371MODULE_PARM_DESC(rxdma, "Receive DMA channel");
David Howellsaf28a032017-04-04 16:54:27 +0100372module_param_hw(irq, int, irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
374module_param(slow, bool, 0);
375MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
376
377MODULE_AUTHOR("Alan Cox");
378MODULE_LICENSE("GPL");
379MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
380
381static struct slvl_board *slvl_unit;
382
383static int __init slvl_init_module(void)
384{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 slvl_unit = slvl_init(io, irq, txdma, rxdma, slow);
386
387 return slvl_unit ? 0 : -ENODEV;
388}
389
390static void __exit slvl_cleanup_module(void)
391{
Rudy Matela614c12a2009-12-02 01:26:01 -0800392 if (slvl_unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 slvl_shutdown(slvl_unit);
394}
395
396module_init(slvl_init_module);
397module_exit(slvl_cleanup_module);