treewide: Add SPDX license identifier for more missed files
[linux-block.git] / drivers / net / wan / hostess_sv11.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * Comtrol SV11 card driver
4 *
5 * This is a slightly odd Z85230 synchronous driver. All you need to
6 * know basically is
7 *
8 * Its a genuine Z85230
9 *
10 * It supports DMA using two DMA channels in SYNC mode. The driver doesn't
11 * use these facilities
12 *
13 * The control port is at io+1, the data at io+3 and turning off the DMA
14 * is done by writing 0 to io+4
15 *
16 * The hardware does the bus handling to avoid the need for delays between
17 * touching control registers.
18 *
25985edc 19 * Port B isn't wired (why - beats me)
52e8a6a2
KH
20 *
21 * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
1da177e4
LT
22 */
23
23efcb73
JP
24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
1da177e4
LT
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/net.h>
30#include <linux/skbuff.h>
31#include <linux/netdevice.h>
32#include <linux/if_arp.h>
33#include <linux/delay.h>
52e8a6a2 34#include <linux/hdlc.h>
1da177e4 35#include <linux/ioport.h>
5a0e3ad6 36#include <linux/slab.h>
1da177e4
LT
37#include <net/arp.h>
38
82729971 39#include <asm/irq.h>
1da177e4
LT
40#include <asm/io.h>
41#include <asm/dma.h>
42#include <asm/byteorder.h>
1da177e4
LT
43#include "z85230.h"
44
45static int dma;
46
1da177e4
LT
47/*
48 * Network driver support routines
49 */
50
52e8a6a2
KH
51static inline struct z8530_dev* dev_to_sv(struct net_device *dev)
52{
53 return (struct z8530_dev *)dev_to_hdlc(dev)->priv;
54}
55
1da177e4 56/*
52e8a6a2 57 * Frame receive. Simple for our card as we do HDLC and there
1da177e4
LT
58 * is no funny garbage involved
59 */
52e8a6a2 60
1da177e4
LT
61static void hostess_input(struct z8530_channel *c, struct sk_buff *skb)
62{
63 /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
52e8a6a2
KH
64 skb_trim(skb, skb->len - 2);
65 skb->protocol = hdlc_type_trans(skb, c->netdevice);
98e399f8 66 skb_reset_mac_header(skb);
52e8a6a2 67 skb->dev = c->netdevice;
1da177e4
LT
68 /*
69 * Send it to the PPP layer. We don't have time to process
70 * it right now.
71 */
72 netif_rx(skb);
1da177e4 73}
52e8a6a2 74
1da177e4
LT
75/*
76 * We've been placed in the UP state
52e8a6a2
KH
77 */
78
1da177e4
LT
79static int hostess_open(struct net_device *d)
80{
52e8a6a2 81 struct z8530_dev *sv11 = dev_to_sv(d);
1da177e4 82 int err = -1;
52e8a6a2 83
1da177e4
LT
84 /*
85 * Link layer up
86 */
52e8a6a2 87 switch (dma) {
1da177e4 88 case 0:
52e8a6a2 89 err = z8530_sync_open(d, &sv11->chanA);
1da177e4
LT
90 break;
91 case 1:
52e8a6a2 92 err = z8530_sync_dma_open(d, &sv11->chanA);
1da177e4
LT
93 break;
94 case 2:
52e8a6a2 95 err = z8530_sync_txdma_open(d, &sv11->chanA);
1da177e4
LT
96 break;
97 }
52e8a6a2
KH
98
99 if (err)
1da177e4 100 return err;
52e8a6a2
KH
101
102 err = hdlc_open(d);
103 if (err) {
104 switch (dma) {
1da177e4 105 case 0:
52e8a6a2 106 z8530_sync_close(d, &sv11->chanA);
1da177e4
LT
107 break;
108 case 1:
52e8a6a2 109 z8530_sync_dma_close(d, &sv11->chanA);
1da177e4
LT
110 break;
111 case 2:
52e8a6a2 112 z8530_sync_txdma_close(d, &sv11->chanA);
1da177e4 113 break;
52e8a6a2 114 }
1da177e4
LT
115 return err;
116 }
52e8a6a2
KH
117 sv11->chanA.rx_function = hostess_input;
118
1da177e4
LT
119 /*
120 * Go go go
121 */
122
123 netif_start_queue(d);
124 return 0;
125}
126
127static int hostess_close(struct net_device *d)
128{
52e8a6a2 129 struct z8530_dev *sv11 = dev_to_sv(d);
1da177e4
LT
130 /*
131 * Discard new frames
132 */
52e8a6a2
KH
133 sv11->chanA.rx_function = z8530_null_rx;
134
135 hdlc_close(d);
1da177e4 136 netif_stop_queue(d);
52e8a6a2
KH
137
138 switch (dma) {
1da177e4 139 case 0:
52e8a6a2 140 z8530_sync_close(d, &sv11->chanA);
1da177e4
LT
141 break;
142 case 1:
52e8a6a2 143 z8530_sync_dma_close(d, &sv11->chanA);
1da177e4
LT
144 break;
145 case 2:
52e8a6a2 146 z8530_sync_txdma_close(d, &sv11->chanA);
1da177e4
LT
147 break;
148 }
149 return 0;
150}
151
152static int hostess_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
153{
52e8a6a2
KH
154 /* struct z8530_dev *sv11=dev_to_sv(d);
155 z8530_ioctl(d,&sv11->chanA,ifr,cmd) */
156 return hdlc_ioctl(d, ifr, cmd);
1da177e4
LT
157}
158
159/*
52e8a6a2 160 * Passed network frames, fire them downwind.
1da177e4 161 */
52e8a6a2 162
d71a6749
SH
163static netdev_tx_t hostess_queue_xmit(struct sk_buff *skb,
164 struct net_device *d)
1da177e4 165{
52e8a6a2 166 return z8530_queue_xmit(&dev_to_sv(d)->chanA, skb);
1da177e4
LT
167}
168
52e8a6a2
KH
169static int hostess_attach(struct net_device *dev, unsigned short encoding,
170 unsigned short parity)
1da177e4 171{
52e8a6a2
KH
172 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
173 return 0;
174 return -EINVAL;
1da177e4
LT
175}
176
177/*
178 * Description block for a Comtrol Hostess SV11 card
179 */
52e8a6a2 180
991990a1
KH
181static const struct net_device_ops hostess_ops = {
182 .ndo_open = hostess_open,
183 .ndo_stop = hostess_close,
991990a1
KH
184 .ndo_start_xmit = hdlc_start_xmit,
185 .ndo_do_ioctl = hostess_ioctl,
186};
187
52e8a6a2 188static struct z8530_dev *sv11_init(int iobase, int irq)
1da177e4 189{
52e8a6a2
KH
190 struct z8530_dev *sv;
191 struct net_device *netdev;
1da177e4
LT
192 /*
193 * Get the needed I/O space
194 */
52e8a6a2
KH
195
196 if (!request_region(iobase, 8, "Comtrol SV11")) {
23efcb73 197 pr_warn("I/O 0x%X already in use\n", iobase);
1da177e4
LT
198 return NULL;
199 }
52e8a6a2
KH
200
201 sv = kzalloc(sizeof(struct z8530_dev), GFP_KERNEL);
202 if (!sv)
203 goto err_kzalloc;
204
1da177e4
LT
205 /*
206 * Stuff in the I/O addressing
207 */
52e8a6a2
KH
208
209 sv->active = 0;
210
211 sv->chanA.ctrlio = iobase + 1;
212 sv->chanA.dataio = iobase + 3;
213 sv->chanB.ctrlio = -1;
214 sv->chanB.dataio = -1;
215 sv->chanA.irqs = &z8530_nop;
216 sv->chanB.irqs = &z8530_nop;
217
218 outb(0, iobase + 4); /* DMA off */
219
1da177e4
LT
220 /* We want a fast IRQ for this device. Actually we'd like an even faster
221 IRQ ;) - This is one driver RtLinux is made for */
52e8a6a2 222
0ca45208 223 if (request_irq(irq, z8530_interrupt, 0,
52e8a6a2 224 "Hostess SV11", sv) < 0) {
23efcb73 225 pr_warn("IRQ %d already in use\n", irq);
52e8a6a2 226 goto err_irq;
1da177e4 227 }
52e8a6a2
KH
228
229 sv->irq = irq;
230 sv->chanA.private = sv;
231 sv->chanA.dev = sv;
232 sv->chanB.dev = sv;
233
234 if (dma) {
1da177e4
LT
235 /*
236 * You can have DMA off or 1 and 3 thats the lot
237 * on the Comtrol.
238 */
52e8a6a2
KH
239 sv->chanA.txdma = 3;
240 sv->chanA.rxdma = 1;
241 outb(0x03 | 0x08, iobase + 4); /* DMA on */
242 if (request_dma(sv->chanA.txdma, "Hostess SV/11 (TX)"))
243 goto err_txdma;
244
245 if (dma == 1)
246 if (request_dma(sv->chanA.rxdma, "Hostess SV/11 (RX)"))
247 goto err_rxdma;
1da177e4
LT
248 }
249
250 /* Kill our private IRQ line the hostess can end up chattering
251 until the configuration is set */
252 disable_irq(irq);
52e8a6a2 253
1da177e4
LT
254 /*
255 * Begin normal initialise
256 */
52e8a6a2
KH
257
258 if (z8530_init(sv)) {
23efcb73 259 pr_err("Z8530 series device not found\n");
1da177e4 260 enable_irq(irq);
52e8a6a2 261 goto free_dma;
1da177e4 262 }
52e8a6a2
KH
263 z8530_channel_load(&sv->chanB, z8530_dead_port);
264 if (sv->type == Z85C30)
265 z8530_channel_load(&sv->chanA, z8530_hdlc_kilostream);
1da177e4 266 else
52e8a6a2
KH
267 z8530_channel_load(&sv->chanA, z8530_hdlc_kilostream_85230);
268
1da177e4 269 enable_irq(irq);
1da177e4
LT
270
271 /*
272 * Now we can take the IRQ
273 */
1da177e4 274
52e8a6a2
KH
275 sv->chanA.netdevice = netdev = alloc_hdlcdev(sv);
276 if (!netdev)
277 goto free_dma;
1da177e4 278
52e8a6a2
KH
279 dev_to_hdlc(netdev)->attach = hostess_attach;
280 dev_to_hdlc(netdev)->xmit = hostess_queue_xmit;
991990a1 281 netdev->netdev_ops = &hostess_ops;
52e8a6a2
KH
282 netdev->base_addr = iobase;
283 netdev->irq = irq;
284
285 if (register_hdlc_device(netdev)) {
23efcb73 286 pr_err("unable to register HDLC device\n");
52e8a6a2
KH
287 free_netdev(netdev);
288 goto free_dma;
1da177e4 289 }
52e8a6a2
KH
290
291 z8530_describe(sv, "I/O", iobase);
292 sv->active = 1;
293 return sv;
294
295free_dma:
296 if (dma == 1)
297 free_dma(sv->chanA.rxdma);
298err_rxdma:
299 if (dma)
300 free_dma(sv->chanA.txdma);
301err_txdma:
302 free_irq(irq, sv);
303err_irq:
1da177e4 304 kfree(sv);
52e8a6a2
KH
305err_kzalloc:
306 release_region(iobase, 8);
1da177e4
LT
307 return NULL;
308}
309
52e8a6a2 310static void sv11_shutdown(struct z8530_dev *dev)
1da177e4 311{
52e8a6a2
KH
312 unregister_hdlc_device(dev->chanA.netdevice);
313 z8530_shutdown(dev);
314 free_irq(dev->irq, dev);
315 if (dma) {
316 if (dma == 1)
317 free_dma(dev->chanA.rxdma);
318 free_dma(dev->chanA.txdma);
1da177e4 319 }
52e8a6a2
KH
320 release_region(dev->chanA.ctrlio - 1, 8);
321 free_netdev(dev->chanA.netdevice);
1da177e4
LT
322 kfree(dev);
323}
324
52e8a6a2
KH
325static int io = 0x200;
326static int irq = 9;
1da177e4 327
af28a03c 328module_param_hw(io, int, ioport, 0);
1da177e4 329MODULE_PARM_DESC(io, "The I/O base of the Comtrol Hostess SV11 card");
af28a03c 330module_param_hw(dma, int, dma, 0);
1da177e4 331MODULE_PARM_DESC(dma, "Set this to 1 to use DMA1/DMA3 for TX/RX");
af28a03c 332module_param_hw(irq, int, irq, 0);
1da177e4
LT
333MODULE_PARM_DESC(irq, "The interrupt line setting for the Comtrol Hostess SV11 card");
334
335MODULE_AUTHOR("Alan Cox");
336MODULE_LICENSE("GPL");
337MODULE_DESCRIPTION("Modular driver for the Comtrol Hostess SV11");
338
52e8a6a2 339static struct z8530_dev *sv11_unit;
1da177e4
LT
340
341int init_module(void)
342{
52e8a6a2 343 if ((sv11_unit = sv11_init(io, irq)) == NULL)
1da177e4
LT
344 return -ENODEV;
345 return 0;
346}
347
348void cleanup_module(void)
349{
52e8a6a2 350 if (sv11_unit)
1da177e4
LT
351 sv11_shutdown(sv11_unit);
352}