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