blob: 567d4f5062d6517bb397895093ebc560910ff023 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * We've been placed in the UP state
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020073 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int hostess_open(struct net_device *d)
76{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020077 struct z8530_dev *sv11 = dev_to_sv(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int err = -1;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /*
81 * Link layer up
82 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020083 switch (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020085 err = z8530_sync_open(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 break;
87 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020088 err = z8530_sync_dma_open(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 break;
90 case 2:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020091 err = z8530_sync_txdma_open(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 break;
93 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020094
95 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return err;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +020097
98 err = hdlc_open(d);
99 if (err) {
100 switch (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200102 z8530_sync_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 break;
104 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200105 z8530_sync_dma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 break;
107 case 2:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200108 z8530_sync_txdma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return err;
112 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200113 sv11->chanA.rx_function = hostess_input;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * Go go go
117 */
118
119 netif_start_queue(d);
120 return 0;
121}
122
123static int hostess_close(struct net_device *d)
124{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200125 struct z8530_dev *sv11 = dev_to_sv(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * Discard new frames
128 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200129 sv11->chanA.rx_function = z8530_null_rx;
130
131 hdlc_close(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 netif_stop_queue(d);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200133
134 switch (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 case 0:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200136 z8530_sync_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 break;
138 case 1:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200139 z8530_sync_dma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
141 case 2:
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200142 z8530_sync_txdma_close(d, &sv11->chanA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 }
145 return 0;
146}
147
148static int hostess_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
149{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200150 /* struct z8530_dev *sv11=dev_to_sv(d);
151 z8530_ioctl(d,&sv11->chanA,ifr,cmd) */
152 return hdlc_ioctl(d, ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/*
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200156 * Passed network frames, fire them downwind.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int hostess_queue_xmit(struct sk_buff *skb, struct net_device *d)
160{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200161 return z8530_queue_xmit(&dev_to_sv(d)->chanA, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200164static int hostess_attach(struct net_device *dev, unsigned short encoding,
165 unsigned short parity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200167 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
168 return 0;
169 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * Description block for a Comtrol Hostess SV11 card
174 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200175
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100176static const struct net_device_ops hostess_ops = {
177 .ndo_open = hostess_open,
178 .ndo_stop = hostess_close,
179 .ndo_change_mtu = hdlc_change_mtu,
180 .ndo_start_xmit = hdlc_start_xmit,
181 .ndo_do_ioctl = hostess_ioctl,
182};
183
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200184static struct z8530_dev *sv11_init(int iobase, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200186 struct z8530_dev *sv;
187 struct net_device *netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
189 * Get the needed I/O space
190 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200191
192 if (!request_region(iobase, 8, "Comtrol SV11")) {
193 printk(KERN_WARNING "hostess: I/O 0x%X already in use.\n",
194 iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return NULL;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200198 sv = kzalloc(sizeof(struct z8530_dev), GFP_KERNEL);
199 if (!sv)
200 goto err_kzalloc;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /*
203 * Stuff in the I/O addressing
204 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200205
206 sv->active = 0;
207
208 sv->chanA.ctrlio = iobase + 1;
209 sv->chanA.dataio = iobase + 3;
210 sv->chanB.ctrlio = -1;
211 sv->chanB.dataio = -1;
212 sv->chanA.irqs = &z8530_nop;
213 sv->chanB.irqs = &z8530_nop;
214
215 outb(0, iobase + 4); /* DMA off */
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* We want a fast IRQ for this device. Actually we'd like an even faster
218 IRQ ;) - This is one driver RtLinux is made for */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200219
220 if (request_irq(irq, &z8530_interrupt, IRQF_DISABLED,
221 "Hostess SV11", sv) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 printk(KERN_WARNING "hostess: IRQ %d already in use.\n", irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200223 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200225
226 sv->irq = irq;
227 sv->chanA.private = sv;
228 sv->chanA.dev = sv;
229 sv->chanB.dev = sv;
230
231 if (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /*
233 * You can have DMA off or 1 and 3 thats the lot
234 * on the Comtrol.
235 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200236 sv->chanA.txdma = 3;
237 sv->chanA.rxdma = 1;
238 outb(0x03 | 0x08, iobase + 4); /* DMA on */
239 if (request_dma(sv->chanA.txdma, "Hostess SV/11 (TX)"))
240 goto err_txdma;
241
242 if (dma == 1)
243 if (request_dma(sv->chanA.rxdma, "Hostess SV/11 (RX)"))
244 goto err_rxdma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 /* Kill our private IRQ line the hostess can end up chattering
248 until the configuration is set */
249 disable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /*
252 * Begin normal initialise
253 */
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200254
255 if (z8530_init(sv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 printk(KERN_ERR "Z8530 series device not found.\n");
257 enable_irq(irq);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200258 goto free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200260 z8530_channel_load(&sv->chanB, z8530_dead_port);
261 if (sv->type == Z85C30)
262 z8530_channel_load(&sv->chanA, z8530_hdlc_kilostream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 else
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200264 z8530_channel_load(&sv->chanA, z8530_hdlc_kilostream_85230);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 enable_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /*
269 * Now we can take the IRQ
270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200272 sv->chanA.netdevice = netdev = alloc_hdlcdev(sv);
273 if (!netdev)
274 goto free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200276 dev_to_hdlc(netdev)->attach = hostess_attach;
277 dev_to_hdlc(netdev)->xmit = hostess_queue_xmit;
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100278 netdev->netdev_ops = &hostess_ops;
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200279 netdev->base_addr = iobase;
280 netdev->irq = irq;
281
282 if (register_hdlc_device(netdev)) {
283 printk(KERN_ERR "hostess: unable to register HDLC device.\n");
284 free_netdev(netdev);
285 goto free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200287
288 z8530_describe(sv, "I/O", iobase);
289 sv->active = 1;
290 return sv;
291
292free_dma:
293 if (dma == 1)
294 free_dma(sv->chanA.rxdma);
295err_rxdma:
296 if (dma)
297 free_dma(sv->chanA.txdma);
298err_txdma:
299 free_irq(irq, sv);
300err_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 kfree(sv);
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200302err_kzalloc:
303 release_region(iobase, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return NULL;
305}
306
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200307static void sv11_shutdown(struct z8530_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200309 unregister_hdlc_device(dev->chanA.netdevice);
310 z8530_shutdown(dev);
311 free_irq(dev->irq, dev);
312 if (dma) {
313 if (dma == 1)
314 free_dma(dev->chanA.rxdma);
315 free_dma(dev->chanA.txdma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200317 release_region(dev->chanA.ctrlio - 1, 8);
318 free_netdev(dev->chanA.netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 kfree(dev);
320}
321
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200322static int io = 0x200;
323static int irq = 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325module_param(io, int, 0);
326MODULE_PARM_DESC(io, "The I/O base of the Comtrol Hostess SV11 card");
327module_param(dma, int, 0);
328MODULE_PARM_DESC(dma, "Set this to 1 to use DMA1/DMA3 for TX/RX");
329module_param(irq, int, 0);
330MODULE_PARM_DESC(irq, "The interrupt line setting for the Comtrol Hostess SV11 card");
331
332MODULE_AUTHOR("Alan Cox");
333MODULE_LICENSE("GPL");
334MODULE_DESCRIPTION("Modular driver for the Comtrol Hostess SV11");
335
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200336static struct z8530_dev *sv11_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338int init_module(void)
339{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200340 if ((sv11_unit = sv11_init(io, irq)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -ENODEV;
342 return 0;
343}
344
345void cleanup_module(void)
346{
Krzysztof Hałasa52e8a6a2008-07-02 17:47:52 +0200347 if (sv11_unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sv11_shutdown(sv11_unit);
349}