blob: e299313f828a74e20aee499e39739d1c79eef0fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 isnt wired (why - beats me)
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020019 *
20 * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/mm.h>
26#include <linux/net.h>
27#include <linux/skbuff.h>
28#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/delay.h>
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020031#include <linux/hdlc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/ioport.h>
33#include <net/arp.h>
34
Al Viro82729972005-12-06 05:53:04 -050035#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/io.h>
37#include <asm/dma.h>
38#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "z85230.h"
40
41static int dma;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Network driver support routines
45 */
46
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020047static inline struct z8530_dev* dev_to_sv(struct net_device *dev)
48{
49 return (struct z8530_dev *)dev_to_hdlc(dev)->priv;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020053 * Frame receive. Simple for our card as we do HDLC and there
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * is no funny garbage involved
55 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void hostess_input(struct z8530_channel *c, struct sk_buff *skb)
58{
59 /* Drop the CRC - it's not a good idea to try and negotiate it ;) */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020060 skb_trim(skb, skb->len - 2);
61 skb->protocol = hdlc_type_trans(skb, c->netdevice);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070062 skb_reset_mac_header(skb);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020063 skb->dev = c->netdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 /*
65 * Send it to the PPP layer. We don't have time to process
66 * it right now.
67 */
68 netif_rx(skb);
69 c->netdevice->last_rx = jiffies;
70}
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * We've been placed in the UP state
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020074 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int hostess_open(struct net_device *d)
77{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020078 struct z8530_dev *sv11 = dev_to_sv(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int err = -1;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /*
82 * Link layer up
83 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020084 switch (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020086 err = z8530_sync_open(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 break;
88 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020089 err = z8530_sync_dma_open(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
91 case 2:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020092 err = z8530_sync_txdma_open(d, &sv11->chanA);
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 (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200103 z8530_sync_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 break;
105 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200106 z8530_sync_dma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 break;
108 case 2:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200109 z8530_sync_txdma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 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 sv11->chanA.rx_function = hostess_input;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /*
117 * Go go go
118 */
119
120 netif_start_queue(d);
121 return 0;
122}
123
124static int hostess_close(struct net_device *d)
125{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200126 struct z8530_dev *sv11 = dev_to_sv(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * Discard new frames
129 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200130 sv11->chanA.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
135 switch (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200137 z8530_sync_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 break;
139 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200140 z8530_sync_dma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 case 2:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200143 z8530_sync_txdma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 break;
145 }
146 return 0;
147}
148
149static int hostess_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
150{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200151 /* struct z8530_dev *sv11=dev_to_sv(d);
152 z8530_ioctl(d,&sv11->chanA,ifr,cmd) */
153 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 hostess_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_sv(d)->chanA, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200165static int hostess_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/*
174 * Description block for a Comtrol Hostess SV11 card
175 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200176
177static struct z8530_dev *sv11_init(int iobase, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200179 struct z8530_dev *sv;
180 struct net_device *netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Get the needed I/O space
183 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200184
185 if (!request_region(iobase, 8, "Comtrol SV11")) {
186 printk(KERN_WARNING "hostess: I/O 0x%X already in use.\n",
187 iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return NULL;
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200191 sv = kzalloc(sizeof(struct z8530_dev), GFP_KERNEL);
192 if (!sv)
193 goto err_kzalloc;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /*
196 * Stuff in the I/O addressing
197 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200198
199 sv->active = 0;
200
201 sv->chanA.ctrlio = iobase + 1;
202 sv->chanA.dataio = iobase + 3;
203 sv->chanB.ctrlio = -1;
204 sv->chanB.dataio = -1;
205 sv->chanA.irqs = &z8530_nop;
206 sv->chanB.irqs = &z8530_nop;
207
208 outb(0, iobase + 4); /* DMA off */
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* We want a fast IRQ for this device. Actually we'd like an even faster
211 IRQ ;) - This is one driver RtLinux is made for */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200212
213 if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
214 "Hostess SV11", sv) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 printk(KERN_WARNING "hostess: IRQ %d already in use.\n", irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200216 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200218
219 sv->irq = irq;
220 sv->chanA.private = sv;
221 sv->chanA.dev = sv;
222 sv->chanB.dev = sv;
223
224 if (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /*
226 * You can have DMA off or 1 and 3 thats the lot
227 * on the Comtrol.
228 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200229 sv->chanA.txdma = 3;
230 sv->chanA.rxdma = 1;
231 outb(0x03 | 0x08, iobase + 4); /* DMA on */
232 if (request_dma(sv->chanA.txdma, "Hostess SV/11 (TX)"))
233 goto err_txdma;
234
235 if (dma == 1)
236 if (request_dma(sv->chanA.rxdma, "Hostess SV/11 (RX)"))
237 goto err_rxdma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
240 /* Kill our private IRQ line the hostess can end up chattering
241 until the configuration is set */
242 disable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /*
245 * Begin normal initialise
246 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200247
248 if (z8530_init(sv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 printk(KERN_ERR "Z8530 series device not found.\n");
250 enable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200251 goto free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200253 z8530_channel_load(&sv->chanB, z8530_dead_port);
254 if (sv->type == Z85C30)
255 z8530_channel_load(&sv->chanA, z8530_hdlc_kilostream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 else
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200257 z8530_channel_load(&sv->chanA, z8530_hdlc_kilostream_85230);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 enable_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
262 * Now we can take the IRQ
263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200265 sv->chanA.netdevice = netdev = alloc_hdlcdev(sv);
266 if (!netdev)
267 goto free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200269 dev_to_hdlc(netdev)->attach = hostess_attach;
270 dev_to_hdlc(netdev)->xmit = hostess_queue_xmit;
271 netdev->open = hostess_open;
272 netdev->stop = hostess_close;
273 netdev->do_ioctl = hostess_ioctl;
274 netdev->base_addr = iobase;
275 netdev->irq = irq;
276
277 if (register_hdlc_device(netdev)) {
278 printk(KERN_ERR "hostess: unable to register HDLC device.\n");
279 free_netdev(netdev);
280 goto free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200282
283 z8530_describe(sv, "I/O", iobase);
284 sv->active = 1;
285 return sv;
286
287free_dma:
288 if (dma == 1)
289 free_dma(sv->chanA.rxdma);
290err_rxdma:
291 if (dma)
292 free_dma(sv->chanA.txdma);
293err_txdma:
294 free_irq(irq, sv);
295err_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 kfree(sv);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200297err_kzalloc:
298 release_region(iobase, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return NULL;
300}
301
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200302static void sv11_shutdown(struct z8530_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200304 unregister_hdlc_device(dev->chanA.netdevice);
305 z8530_shutdown(dev);
306 free_irq(dev->irq, dev);
307 if (dma) {
308 if (dma == 1)
309 free_dma(dev->chanA.rxdma);
310 free_dma(dev->chanA.txdma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200312 release_region(dev->chanA.ctrlio - 1, 8);
313 free_netdev(dev->chanA.netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 kfree(dev);
315}
316
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200317static int io = 0x200;
318static int irq = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320module_param(io, int, 0);
321MODULE_PARM_DESC(io, "The I/O base of the Comtrol Hostess SV11 card");
322module_param(dma, int, 0);
323MODULE_PARM_DESC(dma, "Set this to 1 to use DMA1/DMA3 for TX/RX");
324module_param(irq, int, 0);
325MODULE_PARM_DESC(irq, "The interrupt line setting for the Comtrol Hostess SV11 card");
326
327MODULE_AUTHOR("Alan Cox");
328MODULE_LICENSE("GPL");
329MODULE_DESCRIPTION("Modular driver for the Comtrol Hostess SV11");
330
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200331static struct z8530_dev *sv11_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333int init_module(void)
334{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200335 if ((sv11_unit = sv11_init(io, irq)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -ENODEV;
337 return 0;
338}
339
340void cleanup_module(void)
341{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200342 if (sv11_unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 sv11_shutdown(sv11_unit);
344}