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
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
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
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 */
34 #include <linux/init.h>
35 #include <linux/device.h>
36 #include <linux/smp_lock.h>
37 #include <linux/firmware.h>
38 #include <linux/platform_device.h>
40 #include <asm/atarihw.h>
41 #include <asm/traps.h>
42 #include <asm/uaccess.h> /* For put_user and get_user */
44 #include <asm/dsp56k.h>
47 #define DSP56K_DEV_56001 0 /* The only device so far */
49 #define TIMEOUT 10 /* Host port timeout in number of tries */
50 #define MAXIO 2048 /* Maximum number of words before sleep */
51 #define DSP56K_MAX_BINARY_LENGTH (3*64*1024)
53 #define DSP56K_TX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_TREQ
54 #define DSP56K_RX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_RREQ
55 #define DSP56K_TX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_TREQ
56 #define DSP56K_RX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_RREQ
58 #define DSP56K_TRANSMIT (dsp56k_host_interface.isr & DSP56K_ISR_TXDE)
59 #define DSP56K_RECEIVE (dsp56k_host_interface.isr & DSP56K_ISR_RXDF)
61 #define handshake(count, maxio, timeout, ENABLE, f) \
65 m = min_t(unsigned long, count, maxio); \
66 for (i = 0; i < m; i++) { \
67 for (t = 0; t < timeout && !ENABLE; t++) \
74 if (m == maxio) msleep(20); \
81 for(t = 0; t < n && !DSP56K_TRANSMIT; t++) \
83 if(!DSP56K_TRANSMIT) { \
91 for(t = 0; t < n && !DSP56K_RECEIVE; t++) \
93 if(!DSP56K_RECEIVE) { \
98 static struct dsp56k_device
{
101 int tx_wsize
, rx_wsize
;
104 static struct class *dsp56k_class
;
106 static int dsp56k_reset(void)
110 /* Power down the DSP */
111 sound_ym
.rd_data_reg_sel
= 14;
112 status
= sound_ym
.rd_data_reg_sel
& 0xef;
113 sound_ym
.wd_data
= status
;
114 sound_ym
.wd_data
= status
| 0x10;
118 /* Power up the DSP */
119 sound_ym
.rd_data_reg_sel
= 14;
120 sound_ym
.wd_data
= sound_ym
.rd_data_reg_sel
& 0xef;
125 static int dsp56k_upload(u_char __user
*bin
, int len
)
127 struct platform_device
*pdev
;
128 const struct firmware
*fw
;
129 const char fw_name
[] = "dsp56k/bootstrap.bin";
135 pdev
= platform_device_register_simple("dsp56k", 0, NULL
, 0);
137 printk(KERN_ERR
"Failed to register device for \"%s\"\n",
141 err
= request_firmware(&fw
, fw_name
, &pdev
->dev
);
142 platform_device_unregister(pdev
);
144 printk(KERN_ERR
"Failed to load image \"%s\" err %d\n",
149 printk(KERN_ERR
"Bogus length %d in image \"%s\"\n",
151 release_firmware(fw
);
154 for (i
= 0; i
< fw
->size
; i
= i
+ 3) {
156 dsp56k_host_interface
.data
.b
[1] = fw
->data
[i
];
157 dsp56k_host_interface
.data
.b
[2] = fw
->data
[i
+ 1];
158 dsp56k_host_interface
.data
.b
[3] = fw
->data
[i
+ 2];
160 release_firmware(fw
);
161 for (; i
< 512; i
++) {
163 dsp56k_host_interface
.data
.b
[1] = 0;
164 dsp56k_host_interface
.data
.b
[2] = 0;
165 dsp56k_host_interface
.data
.b
[3] = 0;
168 for (i
= 0; i
< len
; i
++) {
170 get_user(dsp56k_host_interface
.data
.b
[1], bin
++);
171 get_user(dsp56k_host_interface
.data
.b
[2], bin
++);
172 get_user(dsp56k_host_interface
.data
.b
[3], bin
++);
176 dsp56k_host_interface
.data
.l
= 3; /* Magic execute */
181 static ssize_t
dsp56k_read(struct file
*file
, char __user
*buf
, size_t count
,
184 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
185 int dev
= iminor(inode
) & 0x0f;
189 case DSP56K_DEV_56001
:
194 /* Don't do anything if nothing is to be done */
195 if (!count
) return 0;
198 switch (dsp56k
.rx_wsize
) {
201 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
202 put_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
210 data
= (short __user
*) buf
;
211 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
212 put_user(dsp56k_host_interface
.data
.w
[1], data
+n
++));
218 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
219 put_user(dsp56k_host_interface
.data
.b
[1], buf
+n
++);
220 put_user(dsp56k_host_interface
.data
.b
[2], buf
+n
++);
221 put_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
229 data
= (long __user
*) buf
;
230 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
231 put_user(dsp56k_host_interface
.data
.l
, data
+n
++));
239 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
244 static ssize_t
dsp56k_write(struct file
*file
, const char __user
*buf
, size_t count
,
247 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
248 int dev
= iminor(inode
) & 0x0f;
252 case DSP56K_DEV_56001
:
256 /* Don't do anything if nothing is to be done */
257 if (!count
) return 0;
260 switch (dsp56k
.tx_wsize
) {
263 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
264 get_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
269 const short __user
*data
;
272 data
= (const short __user
*)buf
;
273 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
274 get_user(dsp56k_host_interface
.data
.w
[1], data
+n
++));
280 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
281 get_user(dsp56k_host_interface
.data
.b
[1], buf
+n
++);
282 get_user(dsp56k_host_interface
.data
.b
[2], buf
+n
++);
283 get_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
288 const long __user
*data
;
291 data
= (const long __user
*)buf
;
292 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
293 get_user(dsp56k_host_interface
.data
.l
, data
+n
++));
301 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
306 static int dsp56k_ioctl(struct inode
*inode
, struct file
*file
,
307 unsigned int cmd
, unsigned long arg
)
309 int dev
= iminor(inode
) & 0x0f;
310 void __user
*argp
= (void __user
*)arg
;
314 case DSP56K_DEV_56001
:
321 struct dsp56k_upload __user
*binary
= argp
;
323 if(get_user(len
, &binary
->len
) < 0)
325 if(get_user(bin
, &binary
->bin
) < 0)
329 return -EINVAL
; /* nothing to upload?!? */
331 if (len
> DSP56K_MAX_BINARY_LENGTH
) {
335 r
= dsp56k_upload(bin
, len
);
342 case DSP56K_SET_TX_WSIZE
:
343 if (arg
> 4 || arg
< 1)
345 dsp56k
.tx_wsize
= (int) arg
;
347 case DSP56K_SET_RX_WSIZE
:
348 if (arg
> 4 || arg
< 1)
350 dsp56k
.rx_wsize
= (int) arg
;
352 case DSP56K_HOST_FLAGS
:
354 int dir
, out
, status
;
355 struct dsp56k_host_flags __user
*hf
= argp
;
357 if(get_user(dir
, &hf
->dir
) < 0)
359 if(get_user(out
, &hf
->out
) < 0)
362 if ((dir
& 0x1) && (out
& 0x1))
363 dsp56k_host_interface
.icr
|= DSP56K_ICR_HF0
;
365 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF0
;
366 if ((dir
& 0x2) && (out
& 0x2))
367 dsp56k_host_interface
.icr
|= DSP56K_ICR_HF1
;
369 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF1
;
372 if (dsp56k_host_interface
.icr
& DSP56K_ICR_HF0
) status
|= 0x1;
373 if (dsp56k_host_interface
.icr
& DSP56K_ICR_HF1
) status
|= 0x2;
374 if (dsp56k_host_interface
.isr
& DSP56K_ISR_HF2
) status
|= 0x4;
375 if (dsp56k_host_interface
.isr
& DSP56K_ISR_HF3
) status
|= 0x8;
377 return put_user(status
, &hf
->status
);
379 case DSP56K_HOST_CMD
:
380 if (arg
> 31 || arg
< 0)
382 dsp56k_host_interface
.cvr
= (u_char
)((arg
& DSP56K_CVR_HV_MASK
) |
391 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
396 /* As of 2.1.26 this should be dsp56k_poll,
397 * but how do I then check device minor number?
398 * Do I need this function at all???
401 static unsigned int dsp56k_poll(struct file
*file
, poll_table
*wait
)
403 int dev
= iminor(file
->f_path
.dentry
->d_inode
) & 0x0f;
407 case DSP56K_DEV_56001
:
408 /* poll_wait(file, ???, wait); */
409 return POLLIN
| POLLRDNORM
| POLLOUT
;
412 printk("DSP56k driver: Unknown minor device: %d\n", dev
);
418 static int dsp56k_open(struct inode
*inode
, struct file
*file
)
420 int dev
= iminor(inode
) & 0x0f;
426 case DSP56K_DEV_56001
:
428 if (test_and_set_bit(0, &dsp56k
.in_use
)) {
433 dsp56k
.timeout
= TIMEOUT
;
434 dsp56k
.maxio
= MAXIO
;
435 dsp56k
.rx_wsize
= dsp56k
.tx_wsize
= 4;
440 /* Zero host flags */
441 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF0
;
442 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF1
;
454 static int dsp56k_release(struct inode
*inode
, struct file
*file
)
456 int dev
= iminor(inode
) & 0x0f;
460 case DSP56K_DEV_56001
:
461 clear_bit(0, &dsp56k
.in_use
);
464 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
471 static const struct file_operations dsp56k_fops
= {
472 .owner
= THIS_MODULE
,
474 .write
= dsp56k_write
,
475 .ioctl
= dsp56k_ioctl
,
477 .release
= dsp56k_release
,
481 /****** Init and module functions ******/
483 static char banner
[] __initdata
= KERN_INFO
"DSP56k driver installed\n";
485 static int __init
dsp56k_init_driver(void)
489 if(!MACH_IS_ATARI
|| !ATARIHW_PRESENT(DSP56K
)) {
490 printk("DSP56k driver: Hardware not present\n");
494 if(register_chrdev(DSP56K_MAJOR
, "dsp56k", &dsp56k_fops
)) {
495 printk("DSP56k driver: Unable to register driver\n");
498 dsp56k_class
= class_create(THIS_MODULE
, "dsp56k");
499 if (IS_ERR(dsp56k_class
)) {
500 err
= PTR_ERR(dsp56k_class
);
503 device_create(dsp56k_class
, NULL
, MKDEV(DSP56K_MAJOR
, 0), "dsp56k");
509 unregister_chrdev(DSP56K_MAJOR
, "dsp56k");
513 module_init(dsp56k_init_driver
);
515 static void __exit
dsp56k_cleanup_driver(void)
517 device_destroy(dsp56k_class
, MKDEV(DSP56K_MAJOR
, 0));
518 class_destroy(dsp56k_class
);
519 unregister_chrdev(DSP56K_MAJOR
, "dsp56k");
521 module_exit(dsp56k_cleanup_driver
);
523 MODULE_LICENSE("GPL");
524 MODULE_FIRMWARE("dsp56k/bootstrap.bin");