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/major.h>
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/delay.h> /* guess what */
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/mutex.h>
36 #include <linux/firmware.h>
37 #include <linux/platform_device.h>
38 #include <linux/uaccess.h> /* For put_user and get_user */
40 #include <asm/atarihw.h>
41 #include <asm/traps.h>
43 #include <asm/dsp56k.h>
46 #define DSP56K_DEV_56001 0 /* The only device so far */
48 #define TIMEOUT 10 /* Host port timeout in number of tries */
49 #define MAXIO 2048 /* Maximum number of words before sleep */
50 #define DSP56K_MAX_BINARY_LENGTH (3*64*1024)
52 #define DSP56K_TX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_TREQ
53 #define DSP56K_RX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_RREQ
54 #define DSP56K_TX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_TREQ
55 #define DSP56K_RX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_RREQ
57 #define DSP56K_TRANSMIT (dsp56k_host_interface.isr & DSP56K_ISR_TXDE)
58 #define DSP56K_RECEIVE (dsp56k_host_interface.isr & DSP56K_ISR_RXDF)
60 #define handshake(count, maxio, timeout, ENABLE, f) \
64 m = min_t(unsigned long, count, maxio); \
65 for (i = 0; i < m; i++) { \
66 for (t = 0; t < timeout && !ENABLE; t++) \
73 if (m == maxio) msleep(20); \
80 for(t = 0; t < n && !DSP56K_TRANSMIT; t++) \
82 if(!DSP56K_TRANSMIT) { \
90 for(t = 0; t < n && !DSP56K_RECEIVE; t++) \
92 if(!DSP56K_RECEIVE) { \
97 static DEFINE_MUTEX(dsp56k_mutex
);
98 static struct dsp56k_device
{
101 int tx_wsize
, rx_wsize
;
104 static const struct class dsp56k_class
= {
108 static int dsp56k_reset(void)
112 /* Power down the DSP */
113 sound_ym
.rd_data_reg_sel
= 14;
114 status
= sound_ym
.rd_data_reg_sel
& 0xef;
115 sound_ym
.wd_data
= status
;
116 sound_ym
.wd_data
= status
| 0x10;
120 /* Power up the DSP */
121 sound_ym
.rd_data_reg_sel
= 14;
122 sound_ym
.wd_data
= sound_ym
.rd_data_reg_sel
& 0xef;
127 static int dsp56k_upload(u_char __user
*bin
, int len
)
129 struct platform_device
*pdev
;
130 const struct firmware
*fw
;
131 const char fw_name
[] = "dsp56k/bootstrap.bin";
137 pdev
= platform_device_register_simple("dsp56k", 0, NULL
, 0);
139 printk(KERN_ERR
"Failed to register device for \"%s\"\n",
143 err
= request_firmware(&fw
, fw_name
, &pdev
->dev
);
144 platform_device_unregister(pdev
);
146 printk(KERN_ERR
"Failed to load image \"%s\" err %d\n",
151 printk(KERN_ERR
"Bogus length %d in image \"%s\"\n",
153 release_firmware(fw
);
156 for (i
= 0; i
< fw
->size
; i
= i
+ 3) {
158 dsp56k_host_interface
.data
.b
[1] = fw
->data
[i
];
159 dsp56k_host_interface
.data
.b
[2] = fw
->data
[i
+ 1];
160 dsp56k_host_interface
.data
.b
[3] = fw
->data
[i
+ 2];
162 release_firmware(fw
);
163 for (; i
< 512; i
++) {
165 dsp56k_host_interface
.data
.b
[1] = 0;
166 dsp56k_host_interface
.data
.b
[2] = 0;
167 dsp56k_host_interface
.data
.b
[3] = 0;
170 for (i
= 0; i
< len
; i
++) {
172 get_user(dsp56k_host_interface
.data
.b
[1], bin
++);
173 get_user(dsp56k_host_interface
.data
.b
[2], bin
++);
174 get_user(dsp56k_host_interface
.data
.b
[3], bin
++);
178 dsp56k_host_interface
.data
.l
= 3; /* Magic execute */
183 static ssize_t
dsp56k_read(struct file
*file
, char __user
*buf
, size_t count
,
186 struct inode
*inode
= file_inode(file
);
187 int dev
= iminor(inode
) & 0x0f;
191 case DSP56K_DEV_56001
:
196 /* Don't do anything if nothing is to be done */
197 if (!count
) return 0;
200 switch (dsp56k
.rx_wsize
) {
203 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
204 put_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
212 data
= (short __user
*) buf
;
213 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
214 put_user(dsp56k_host_interface
.data
.w
[1], data
+n
++));
220 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
221 put_user(dsp56k_host_interface
.data
.b
[1], buf
+n
++);
222 put_user(dsp56k_host_interface
.data
.b
[2], buf
+n
++);
223 put_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
231 data
= (long __user
*) buf
;
232 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_RECEIVE
,
233 put_user(dsp56k_host_interface
.data
.l
, data
+n
++));
241 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
246 static ssize_t
dsp56k_write(struct file
*file
, const char __user
*buf
, size_t count
,
249 struct inode
*inode
= file_inode(file
);
250 int dev
= iminor(inode
) & 0x0f;
254 case DSP56K_DEV_56001
:
258 /* Don't do anything if nothing is to be done */
259 if (!count
) return 0;
262 switch (dsp56k
.tx_wsize
) {
265 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
266 get_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
271 const short __user
*data
;
274 data
= (const short __user
*)buf
;
275 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
276 get_user(dsp56k_host_interface
.data
.w
[1], data
+n
++));
282 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
283 get_user(dsp56k_host_interface
.data
.b
[1], buf
+n
++);
284 get_user(dsp56k_host_interface
.data
.b
[2], buf
+n
++);
285 get_user(dsp56k_host_interface
.data
.b
[3], buf
+n
++));
290 const long __user
*data
;
293 data
= (const long __user
*)buf
;
294 handshake(count
, dsp56k
.maxio
, dsp56k
.timeout
, DSP56K_TRANSMIT
,
295 get_user(dsp56k_host_interface
.data
.l
, data
+n
++));
303 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
308 static long dsp56k_ioctl(struct file
*file
, unsigned int cmd
,
311 int dev
= iminor(file_inode(file
)) & 0x0f;
312 void __user
*argp
= (void __user
*)arg
;
316 case DSP56K_DEV_56001
:
323 struct dsp56k_upload __user
*binary
= argp
;
325 if(get_user(len
, &binary
->len
) < 0)
327 if(get_user(bin
, &binary
->bin
) < 0)
331 return -EINVAL
; /* nothing to upload?!? */
333 if (len
> DSP56K_MAX_BINARY_LENGTH
) {
336 mutex_lock(&dsp56k_mutex
);
337 r
= dsp56k_upload(bin
, len
);
338 mutex_unlock(&dsp56k_mutex
);
345 case DSP56K_SET_TX_WSIZE
:
346 if (arg
> 4 || arg
< 1)
348 mutex_lock(&dsp56k_mutex
);
349 dsp56k
.tx_wsize
= (int) arg
;
350 mutex_unlock(&dsp56k_mutex
);
352 case DSP56K_SET_RX_WSIZE
:
353 if (arg
> 4 || arg
< 1)
355 mutex_lock(&dsp56k_mutex
);
356 dsp56k
.rx_wsize
= (int) arg
;
357 mutex_unlock(&dsp56k_mutex
);
359 case DSP56K_HOST_FLAGS
:
361 int dir
, out
, status
;
362 struct dsp56k_host_flags __user
*hf
= argp
;
364 if(get_user(dir
, &hf
->dir
) < 0)
366 if(get_user(out
, &hf
->out
) < 0)
369 mutex_lock(&dsp56k_mutex
);
370 if ((dir
& 0x1) && (out
& 0x1))
371 dsp56k_host_interface
.icr
|= DSP56K_ICR_HF0
;
373 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF0
;
374 if ((dir
& 0x2) && (out
& 0x2))
375 dsp56k_host_interface
.icr
|= DSP56K_ICR_HF1
;
377 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF1
;
380 if (dsp56k_host_interface
.icr
& DSP56K_ICR_HF0
) status
|= 0x1;
381 if (dsp56k_host_interface
.icr
& DSP56K_ICR_HF1
) status
|= 0x2;
382 if (dsp56k_host_interface
.isr
& DSP56K_ISR_HF2
) status
|= 0x4;
383 if (dsp56k_host_interface
.isr
& DSP56K_ISR_HF3
) status
|= 0x8;
384 mutex_unlock(&dsp56k_mutex
);
385 return put_user(status
, &hf
->status
);
387 case DSP56K_HOST_CMD
:
390 mutex_lock(&dsp56k_mutex
);
391 dsp56k_host_interface
.cvr
= (u_char
)((arg
& DSP56K_CVR_HV_MASK
) |
393 mutex_unlock(&dsp56k_mutex
);
401 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
406 /* As of 2.1.26 this should be dsp56k_poll,
407 * but how do I then check device minor number?
408 * Do I need this function at all???
411 static __poll_t
dsp56k_poll(struct file
*file
, poll_table
*wait
)
413 int dev
= iminor(file_inode(file
)) & 0x0f;
417 case DSP56K_DEV_56001
:
418 /* poll_wait(file, ???, wait); */
419 return EPOLLIN
| EPOLLRDNORM
| EPOLLOUT
;
422 printk("DSP56k driver: Unknown minor device: %d\n", dev
);
428 static int dsp56k_open(struct inode
*inode
, struct file
*file
)
430 int dev
= iminor(inode
) & 0x0f;
433 mutex_lock(&dsp56k_mutex
);
436 case DSP56K_DEV_56001
:
438 if (test_and_set_bit(0, &dsp56k
.in_use
)) {
443 dsp56k
.timeout
= TIMEOUT
;
444 dsp56k
.maxio
= MAXIO
;
445 dsp56k
.rx_wsize
= dsp56k
.tx_wsize
= 4;
450 /* Zero host flags */
451 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF0
;
452 dsp56k_host_interface
.icr
&= ~DSP56K_ICR_HF1
;
460 mutex_unlock(&dsp56k_mutex
);
464 static int dsp56k_release(struct inode
*inode
, struct file
*file
)
466 int dev
= iminor(inode
) & 0x0f;
470 case DSP56K_DEV_56001
:
471 clear_bit(0, &dsp56k
.in_use
);
474 printk(KERN_ERR
"DSP56k driver: Unknown minor device: %d\n", dev
);
481 static const struct file_operations dsp56k_fops
= {
482 .owner
= THIS_MODULE
,
484 .write
= dsp56k_write
,
485 .unlocked_ioctl
= dsp56k_ioctl
,
487 .release
= dsp56k_release
,
488 .llseek
= noop_llseek
,
492 /****** Init and module functions ******/
494 static const char banner
[] __initconst
= KERN_INFO
"DSP56k driver installed\n";
496 static int __init
dsp56k_init_driver(void)
500 if(!MACH_IS_ATARI
|| !ATARIHW_PRESENT(DSP56K
)) {
501 printk("DSP56k driver: Hardware not present\n");
505 if(register_chrdev(DSP56K_MAJOR
, "dsp56k", &dsp56k_fops
)) {
506 printk("DSP56k driver: Unable to register driver\n");
509 err
= class_register(&dsp56k_class
);
512 device_create(&dsp56k_class
, NULL
, MKDEV(DSP56K_MAJOR
, 0), NULL
,
519 unregister_chrdev(DSP56K_MAJOR
, "dsp56k");
523 module_init(dsp56k_init_driver
);
525 static void __exit
dsp56k_cleanup_driver(void)
527 device_destroy(&dsp56k_class
, MKDEV(DSP56K_MAJOR
, 0));
528 class_unregister(&dsp56k_class
);
529 unregister_chrdev(DSP56K_MAJOR
, "dsp56k");
531 module_exit(dsp56k_cleanup_driver
);
533 MODULE_DESCRIPTION("Atari DSP56001 Device Driver");
534 MODULE_LICENSE("GPL");
535 MODULE_FIRMWARE("dsp56k/bootstrap.bin");