libertas: fixes for slow hardware
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / char / dsp56k.c
bloba69c6528326005694809005a546436a1af5cd5d1
1 /*
2 * The DSP56001 Device Driver, saviour of the Free World(tm)
4 * Authors: Fredrik Noring <noring@nocrew.org>
5 * lars brinkhoff <lars@nocrew.org>
6 * Tomas Berndtsson <tomas@nocrew.org>
8 * First version May 1996
10 * History:
11 * 97-01-29 Tomas Berndtsson,
12 * Integrated with Linux 2.1.21 kernel sources.
13 * 97-02-15 Tomas Berndtsson,
14 * Fixed for kernel 2.1.26
16 * BUGS:
17 * Hmm... there must be something here :)
19 * Copyright (C) 1996,1997 Fredrik Noring, lars brinkhoff & Tomas Berndtsson
21 * This file is subject to the terms and conditions of the GNU General Public
22 * License. See the file COPYING in the main directory of this archive
23 * for more details.
26 #include <linux/module.h>
27 #include <linux/slab.h> /* for kmalloc() and kfree() */
28 #include <linux/major.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/delay.h> /* guess what */
32 #include <linux/fs.h>
33 #include <linux/mm.h>
34 #include <linux/init.h>
35 #include <linux/device.h>
37 #include <asm/atarihw.h>
38 #include <asm/traps.h>
39 #include <asm/uaccess.h> /* For put_user and get_user */
41 #include <asm/dsp56k.h>
43 /* minor devices */
44 #define DSP56K_DEV_56001 0 /* The only device so far */
46 #define TIMEOUT 10 /* Host port timeout in number of tries */
47 #define MAXIO 2048 /* Maximum number of words before sleep */
48 #define DSP56K_MAX_BINARY_LENGTH (3*64*1024)
50 #define DSP56K_TX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_TREQ
51 #define DSP56K_RX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_RREQ
52 #define DSP56K_TX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_TREQ
53 #define DSP56K_RX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_RREQ
55 #define DSP56K_TRANSMIT (dsp56k_host_interface.isr & DSP56K_ISR_TXDE)
56 #define DSP56K_RECEIVE (dsp56k_host_interface.isr & DSP56K_ISR_RXDF)
58 #define handshake(count, maxio, timeout, ENABLE, f) \
59 { \
60 long i, t, m; \
61 while (count > 0) { \
62 m = min_t(unsigned long, count, maxio); \
63 for (i = 0; i < m; i++) { \
64 for (t = 0; t < timeout && !ENABLE; t++) \
65 msleep(20); \
66 if(!ENABLE) \
67 return -EIO; \
68 f; \
69 } \
70 count -= m; \
71 if (m == maxio) msleep(20); \
72 } \
75 #define tx_wait(n) \
76 { \
77 int t; \
78 for(t = 0; t < n && !DSP56K_TRANSMIT; t++) \
79 msleep(10); \
80 if(!DSP56K_TRANSMIT) { \
81 return -EIO; \
82 } \
85 #define rx_wait(n) \
86 { \
87 int t; \
88 for(t = 0; t < n && !DSP56K_RECEIVE; t++) \
89 msleep(10); \
90 if(!DSP56K_RECEIVE) { \
91 return -EIO; \
92 } \
95 /* DSP56001 bootstrap code */
96 static char bootstrap[] = {
97 0x0c, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116 0x00, 0x00, 0x60, 0xf4, 0x00, 0x00, 0x00, 0x4f, 0x61, 0xf4,
117 0x00, 0x00, 0x7e, 0xa9, 0x06, 0x2e, 0x80, 0x00, 0x00, 0x47,
118 0x07, 0xd8, 0x84, 0x07, 0x59, 0x84, 0x08, 0xf4, 0xa8, 0x00,
119 0x00, 0x04, 0x08, 0xf4, 0xbf, 0x00, 0x0c, 0x00, 0x00, 0xfe,
120 0xb8, 0x0a, 0xf0, 0x80, 0x00, 0x7e, 0xa9, 0x08, 0xf4, 0xa0,
121 0x00, 0x00, 0x01, 0x08, 0xf4, 0xbe, 0x00, 0x00, 0x00, 0x0a,
122 0xa9, 0x80, 0x00, 0x7e, 0xad, 0x08, 0x4e, 0x2b, 0x44, 0xf4,
123 0x00, 0x00, 0x00, 0x03, 0x44, 0xf4, 0x45, 0x00, 0x00, 0x01,
124 0x0e, 0xa0, 0x00, 0x0a, 0xa9, 0x80, 0x00, 0x7e, 0xb5, 0x08,
125 0x50, 0x2b, 0x0a, 0xa9, 0x80, 0x00, 0x7e, 0xb8, 0x08, 0x46,
126 0x2b, 0x44, 0xf4, 0x45, 0x00, 0x00, 0x02, 0x0a, 0xf0, 0xaa,
127 0x00, 0x7e, 0xc9, 0x20, 0x00, 0x45, 0x0a, 0xf0, 0xaa, 0x00,
128 0x7e, 0xd0, 0x06, 0xc6, 0x00, 0x00, 0x7e, 0xc6, 0x0a, 0xa9,
129 0x80, 0x00, 0x7e, 0xc4, 0x08, 0x58, 0x6b, 0x0a, 0xf0, 0x80,
130 0x00, 0x7e, 0xad, 0x06, 0xc6, 0x00, 0x00, 0x7e, 0xcd, 0x0a,
131 0xa9, 0x80, 0x00, 0x7e, 0xcb, 0x08, 0x58, 0xab, 0x0a, 0xf0,
132 0x80, 0x00, 0x7e, 0xad, 0x06, 0xc6, 0x00, 0x00, 0x7e, 0xd4,
133 0x0a, 0xa9, 0x80, 0x00, 0x7e, 0xd2, 0x08, 0x58, 0xeb, 0x0a,
134 0xf0, 0x80, 0x00, 0x7e, 0xad};
135 static int sizeof_bootstrap = 375;
138 static struct dsp56k_device {
139 unsigned long in_use;
140 long maxio, timeout;
141 int tx_wsize, rx_wsize;
142 } dsp56k;
144 static struct class *dsp56k_class;
146 static int dsp56k_reset(void)
148 u_char status;
150 /* Power down the DSP */
151 sound_ym.rd_data_reg_sel = 14;
152 status = sound_ym.rd_data_reg_sel & 0xef;
153 sound_ym.wd_data = status;
154 sound_ym.wd_data = status | 0x10;
156 udelay(10);
158 /* Power up the DSP */
159 sound_ym.rd_data_reg_sel = 14;
160 sound_ym.wd_data = sound_ym.rd_data_reg_sel & 0xef;
162 return 0;
165 static int dsp56k_upload(u_char __user *bin, int len)
167 int i;
168 u_char *p;
170 dsp56k_reset();
172 p = bootstrap;
173 for (i = 0; i < sizeof_bootstrap/3; i++) {
174 /* tx_wait(10); */
175 dsp56k_host_interface.data.b[1] = *p++;
176 dsp56k_host_interface.data.b[2] = *p++;
177 dsp56k_host_interface.data.b[3] = *p++;
179 for (; i < 512; i++) {
180 /* tx_wait(10); */
181 dsp56k_host_interface.data.b[1] = 0;
182 dsp56k_host_interface.data.b[2] = 0;
183 dsp56k_host_interface.data.b[3] = 0;
186 for (i = 0; i < len; i++) {
187 tx_wait(10);
188 get_user(dsp56k_host_interface.data.b[1], bin++);
189 get_user(dsp56k_host_interface.data.b[2], bin++);
190 get_user(dsp56k_host_interface.data.b[3], bin++);
193 tx_wait(10);
194 dsp56k_host_interface.data.l = 3; /* Magic execute */
196 return 0;
199 static ssize_t dsp56k_read(struct file *file, char __user *buf, size_t count,
200 loff_t *ppos)
202 struct inode *inode = file->f_path.dentry->d_inode;
203 int dev = iminor(inode) & 0x0f;
205 switch(dev)
207 case DSP56K_DEV_56001:
210 long n;
212 /* Don't do anything if nothing is to be done */
213 if (!count) return 0;
215 n = 0;
216 switch (dsp56k.rx_wsize) {
217 case 1: /* 8 bit */
219 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
220 put_user(dsp56k_host_interface.data.b[3], buf+n++));
221 return n;
223 case 2: /* 16 bit */
225 short __user *data;
227 count /= 2;
228 data = (short __user *) buf;
229 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
230 put_user(dsp56k_host_interface.data.w[1], data+n++));
231 return 2*n;
233 case 3: /* 24 bit */
235 count /= 3;
236 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
237 put_user(dsp56k_host_interface.data.b[1], buf+n++);
238 put_user(dsp56k_host_interface.data.b[2], buf+n++);
239 put_user(dsp56k_host_interface.data.b[3], buf+n++));
240 return 3*n;
242 case 4: /* 32 bit */
244 long __user *data;
246 count /= 4;
247 data = (long __user *) buf;
248 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
249 put_user(dsp56k_host_interface.data.l, data+n++));
250 return 4*n;
253 return -EFAULT;
256 default:
257 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
258 return -ENXIO;
262 static ssize_t dsp56k_write(struct file *file, const char __user *buf, size_t count,
263 loff_t *ppos)
265 struct inode *inode = file->f_path.dentry->d_inode;
266 int dev = iminor(inode) & 0x0f;
268 switch(dev)
270 case DSP56K_DEV_56001:
272 long n;
274 /* Don't do anything if nothing is to be done */
275 if (!count) return 0;
277 n = 0;
278 switch (dsp56k.tx_wsize) {
279 case 1: /* 8 bit */
281 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
282 get_user(dsp56k_host_interface.data.b[3], buf+n++));
283 return n;
285 case 2: /* 16 bit */
287 const short __user *data;
289 count /= 2;
290 data = (const short __user *)buf;
291 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
292 get_user(dsp56k_host_interface.data.w[1], data+n++));
293 return 2*n;
295 case 3: /* 24 bit */
297 count /= 3;
298 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
299 get_user(dsp56k_host_interface.data.b[1], buf+n++);
300 get_user(dsp56k_host_interface.data.b[2], buf+n++);
301 get_user(dsp56k_host_interface.data.b[3], buf+n++));
302 return 3*n;
304 case 4: /* 32 bit */
306 const long __user *data;
308 count /= 4;
309 data = (const long __user *)buf;
310 handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
311 get_user(dsp56k_host_interface.data.l, data+n++));
312 return 4*n;
316 return -EFAULT;
318 default:
319 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
320 return -ENXIO;
324 static int dsp56k_ioctl(struct inode *inode, struct file *file,
325 unsigned int cmd, unsigned long arg)
327 int dev = iminor(inode) & 0x0f;
328 void __user *argp = (void __user *)arg;
330 switch(dev)
332 case DSP56K_DEV_56001:
334 switch(cmd) {
335 case DSP56K_UPLOAD:
337 char __user *bin;
338 int r, len;
339 struct dsp56k_upload __user *binary = argp;
341 if(get_user(len, &binary->len) < 0)
342 return -EFAULT;
343 if(get_user(bin, &binary->bin) < 0)
344 return -EFAULT;
346 if (len == 0) {
347 return -EINVAL; /* nothing to upload?!? */
349 if (len > DSP56K_MAX_BINARY_LENGTH) {
350 return -EINVAL;
353 r = dsp56k_upload(bin, len);
354 if (r < 0) {
355 return r;
358 break;
360 case DSP56K_SET_TX_WSIZE:
361 if (arg > 4 || arg < 1)
362 return -EINVAL;
363 dsp56k.tx_wsize = (int) arg;
364 break;
365 case DSP56K_SET_RX_WSIZE:
366 if (arg > 4 || arg < 1)
367 return -EINVAL;
368 dsp56k.rx_wsize = (int) arg;
369 break;
370 case DSP56K_HOST_FLAGS:
372 int dir, out, status;
373 struct dsp56k_host_flags __user *hf = argp;
375 if(get_user(dir, &hf->dir) < 0)
376 return -EFAULT;
377 if(get_user(out, &hf->out) < 0)
378 return -EFAULT;
380 if ((dir & 0x1) && (out & 0x1))
381 dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
382 else if (dir & 0x1)
383 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
384 if ((dir & 0x2) && (out & 0x2))
385 dsp56k_host_interface.icr |= DSP56K_ICR_HF1;
386 else if (dir & 0x2)
387 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
389 status = 0;
390 if (dsp56k_host_interface.icr & DSP56K_ICR_HF0) status |= 0x1;
391 if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
392 if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
393 if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
395 return put_user(status, &hf->status);
397 case DSP56K_HOST_CMD:
398 if (arg > 31 || arg < 0)
399 return -EINVAL;
400 dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
401 DSP56K_CVR_HC);
402 break;
403 default:
404 return -EINVAL;
406 return 0;
408 default:
409 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
410 return -ENXIO;
414 /* As of 2.1.26 this should be dsp56k_poll,
415 * but how do I then check device minor number?
416 * Do I need this function at all???
418 #if 0
419 static unsigned int dsp56k_poll(struct file *file, poll_table *wait)
421 int dev = iminor(file->f_path.dentry->d_inode) & 0x0f;
423 switch(dev)
425 case DSP56K_DEV_56001:
426 /* poll_wait(file, ???, wait); */
427 return POLLIN | POLLRDNORM | POLLOUT;
429 default:
430 printk("DSP56k driver: Unknown minor device: %d\n", dev);
431 return 0;
434 #endif
436 static int dsp56k_open(struct inode *inode, struct file *file)
438 int dev = iminor(inode) & 0x0f;
440 switch(dev)
442 case DSP56K_DEV_56001:
444 if (test_and_set_bit(0, &dsp56k.in_use))
445 return -EBUSY;
447 dsp56k.timeout = TIMEOUT;
448 dsp56k.maxio = MAXIO;
449 dsp56k.rx_wsize = dsp56k.tx_wsize = 4;
451 DSP56K_TX_INT_OFF;
452 DSP56K_RX_INT_OFF;
454 /* Zero host flags */
455 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
456 dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
458 break;
460 default:
461 return -ENODEV;
464 return 0;
467 static int dsp56k_release(struct inode *inode, struct file *file)
469 int dev = iminor(inode) & 0x0f;
471 switch(dev)
473 case DSP56K_DEV_56001:
474 clear_bit(0, &dsp56k.in_use);
475 break;
476 default:
477 printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
478 return -ENXIO;
481 return 0;
484 static const struct file_operations dsp56k_fops = {
485 .owner = THIS_MODULE,
486 .read = dsp56k_read,
487 .write = dsp56k_write,
488 .ioctl = dsp56k_ioctl,
489 .open = dsp56k_open,
490 .release = dsp56k_release,
494 /****** Init and module functions ******/
496 static char banner[] __initdata = KERN_INFO "DSP56k driver installed\n";
498 static int __init dsp56k_init_driver(void)
500 int err = 0;
502 if(!MACH_IS_ATARI || !ATARIHW_PRESENT(DSP56K)) {
503 printk("DSP56k driver: Hardware not present\n");
504 return -ENODEV;
507 if(register_chrdev(DSP56K_MAJOR, "dsp56k", &dsp56k_fops)) {
508 printk("DSP56k driver: Unable to register driver\n");
509 return -ENODEV;
511 dsp56k_class = class_create(THIS_MODULE, "dsp56k");
512 if (IS_ERR(dsp56k_class)) {
513 err = PTR_ERR(dsp56k_class);
514 goto out_chrdev;
516 device_create(dsp56k_class, NULL, MKDEV(DSP56K_MAJOR, 0), "dsp56k");
518 printk(banner);
519 goto out;
521 out_chrdev:
522 unregister_chrdev(DSP56K_MAJOR, "dsp56k");
523 out:
524 return err;
526 module_init(dsp56k_init_driver);
528 static void __exit dsp56k_cleanup_driver(void)
530 device_destroy(dsp56k_class, MKDEV(DSP56K_MAJOR, 0));
531 class_destroy(dsp56k_class);
532 unregister_chrdev(DSP56K_MAJOR, "dsp56k");
534 module_exit(dsp56k_cleanup_driver);
536 MODULE_LICENSE("GPL");