dsp56k.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * The DSP56001 Device Driver, saviour of the Free World(tm)
  3. *
  4. * Authors: Fredrik Noring <noring@nocrew.org>
  5. * lars brinkhoff <lars@nocrew.org>
  6. * Tomas Berndtsson <tomas@nocrew.org>
  7. *
  8. * First version May 1996
  9. *
  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
  15. *
  16. * BUGS:
  17. * Hmm... there must be something here :)
  18. *
  19. * Copyright (C) 1996,1997 Fredrik Noring, lars brinkhoff & Tomas Berndtsson
  20. *
  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.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/slab.h> /* for kmalloc() and kfree() */
  27. #include <linux/sched.h> /* for struct wait_queue etc */
  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/devfs_fs_kernel.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/device.h>
  38. #include <asm/atarihw.h>
  39. #include <asm/traps.h>
  40. #include <asm/uaccess.h> /* For put_user and get_user */
  41. #include <asm/dsp56k.h>
  42. /* minor devices */
  43. #define DSP56K_DEV_56001 0 /* The only device so far */
  44. #define TIMEOUT 10 /* Host port timeout in number of tries */
  45. #define MAXIO 2048 /* Maximum number of words before sleep */
  46. #define DSP56K_MAX_BINARY_LENGTH (3*64*1024)
  47. #define DSP56K_TX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_TREQ
  48. #define DSP56K_RX_INT_ON dsp56k_host_interface.icr |= DSP56K_ICR_RREQ
  49. #define DSP56K_TX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_TREQ
  50. #define DSP56K_RX_INT_OFF dsp56k_host_interface.icr &= ~DSP56K_ICR_RREQ
  51. #define DSP56K_TRANSMIT (dsp56k_host_interface.isr & DSP56K_ISR_TXDE)
  52. #define DSP56K_RECEIVE (dsp56k_host_interface.isr & DSP56K_ISR_RXDF)
  53. #define handshake(count, maxio, timeout, ENABLE, f) \
  54. { \
  55. long i, t, m; \
  56. while (count > 0) { \
  57. m = min_t(unsigned long, count, maxio); \
  58. for (i = 0; i < m; i++) { \
  59. for (t = 0; t < timeout && !ENABLE; t++) \
  60. msleep(20); \
  61. if(!ENABLE) \
  62. return -EIO; \
  63. f; \
  64. } \
  65. count -= m; \
  66. if (m == maxio) msleep(20); \
  67. } \
  68. }
  69. #define tx_wait(n) \
  70. { \
  71. int t; \
  72. for(t = 0; t < n && !DSP56K_TRANSMIT; t++) \
  73. msleep(10); \
  74. if(!DSP56K_TRANSMIT) { \
  75. return -EIO; \
  76. } \
  77. }
  78. #define rx_wait(n) \
  79. { \
  80. int t; \
  81. for(t = 0; t < n && !DSP56K_RECEIVE; t++) \
  82. msleep(10); \
  83. if(!DSP56K_RECEIVE) { \
  84. return -EIO; \
  85. } \
  86. }
  87. /* DSP56001 bootstrap code */
  88. static char bootstrap[] = {
  89. 0x0c, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 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, 0x60, 0xf4, 0x00, 0x00, 0x00, 0x4f, 0x61, 0xf4,
  109. 0x00, 0x00, 0x7e, 0xa9, 0x06, 0x2e, 0x80, 0x00, 0x00, 0x47,
  110. 0x07, 0xd8, 0x84, 0x07, 0x59, 0x84, 0x08, 0xf4, 0xa8, 0x00,
  111. 0x00, 0x04, 0x08, 0xf4, 0xbf, 0x00, 0x0c, 0x00, 0x00, 0xfe,
  112. 0xb8, 0x0a, 0xf0, 0x80, 0x00, 0x7e, 0xa9, 0x08, 0xf4, 0xa0,
  113. 0x00, 0x00, 0x01, 0x08, 0xf4, 0xbe, 0x00, 0x00, 0x00, 0x0a,
  114. 0xa9, 0x80, 0x00, 0x7e, 0xad, 0x08, 0x4e, 0x2b, 0x44, 0xf4,
  115. 0x00, 0x00, 0x00, 0x03, 0x44, 0xf4, 0x45, 0x00, 0x00, 0x01,
  116. 0x0e, 0xa0, 0x00, 0x0a, 0xa9, 0x80, 0x00, 0x7e, 0xb5, 0x08,
  117. 0x50, 0x2b, 0x0a, 0xa9, 0x80, 0x00, 0x7e, 0xb8, 0x08, 0x46,
  118. 0x2b, 0x44, 0xf4, 0x45, 0x00, 0x00, 0x02, 0x0a, 0xf0, 0xaa,
  119. 0x00, 0x7e, 0xc9, 0x20, 0x00, 0x45, 0x0a, 0xf0, 0xaa, 0x00,
  120. 0x7e, 0xd0, 0x06, 0xc6, 0x00, 0x00, 0x7e, 0xc6, 0x0a, 0xa9,
  121. 0x80, 0x00, 0x7e, 0xc4, 0x08, 0x58, 0x6b, 0x0a, 0xf0, 0x80,
  122. 0x00, 0x7e, 0xad, 0x06, 0xc6, 0x00, 0x00, 0x7e, 0xcd, 0x0a,
  123. 0xa9, 0x80, 0x00, 0x7e, 0xcb, 0x08, 0x58, 0xab, 0x0a, 0xf0,
  124. 0x80, 0x00, 0x7e, 0xad, 0x06, 0xc6, 0x00, 0x00, 0x7e, 0xd4,
  125. 0x0a, 0xa9, 0x80, 0x00, 0x7e, 0xd2, 0x08, 0x58, 0xeb, 0x0a,
  126. 0xf0, 0x80, 0x00, 0x7e, 0xad};
  127. static int sizeof_bootstrap = 375;
  128. static struct dsp56k_device {
  129. long in_use;
  130. long maxio, timeout;
  131. int tx_wsize, rx_wsize;
  132. } dsp56k;
  133. static struct class *dsp56k_class;
  134. static int dsp56k_reset(void)
  135. {
  136. u_char status;
  137. /* Power down the DSP */
  138. sound_ym.rd_data_reg_sel = 14;
  139. status = sound_ym.rd_data_reg_sel & 0xef;
  140. sound_ym.wd_data = status;
  141. sound_ym.wd_data = status | 0x10;
  142. udelay(10);
  143. /* Power up the DSP */
  144. sound_ym.rd_data_reg_sel = 14;
  145. sound_ym.wd_data = sound_ym.rd_data_reg_sel & 0xef;
  146. return 0;
  147. }
  148. static int dsp56k_upload(u_char __user *bin, int len)
  149. {
  150. int i;
  151. u_char *p;
  152. dsp56k_reset();
  153. p = bootstrap;
  154. for (i = 0; i < sizeof_bootstrap/3; i++) {
  155. /* tx_wait(10); */
  156. dsp56k_host_interface.data.b[1] = *p++;
  157. dsp56k_host_interface.data.b[2] = *p++;
  158. dsp56k_host_interface.data.b[3] = *p++;
  159. }
  160. for (; i < 512; i++) {
  161. /* tx_wait(10); */
  162. dsp56k_host_interface.data.b[1] = 0;
  163. dsp56k_host_interface.data.b[2] = 0;
  164. dsp56k_host_interface.data.b[3] = 0;
  165. }
  166. for (i = 0; i < len; i++) {
  167. tx_wait(10);
  168. get_user(dsp56k_host_interface.data.b[1], bin++);
  169. get_user(dsp56k_host_interface.data.b[2], bin++);
  170. get_user(dsp56k_host_interface.data.b[3], bin++);
  171. }
  172. tx_wait(10);
  173. dsp56k_host_interface.data.l = 3; /* Magic execute */
  174. return 0;
  175. }
  176. static ssize_t dsp56k_read(struct file *file, char __user *buf, size_t count,
  177. loff_t *ppos)
  178. {
  179. struct inode *inode = file->f_dentry->d_inode;
  180. int dev = iminor(inode) & 0x0f;
  181. switch(dev)
  182. {
  183. case DSP56K_DEV_56001:
  184. {
  185. long n;
  186. /* Don't do anything if nothing is to be done */
  187. if (!count) return 0;
  188. n = 0;
  189. switch (dsp56k.rx_wsize) {
  190. case 1: /* 8 bit */
  191. {
  192. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  193. put_user(dsp56k_host_interface.data.b[3], buf+n++));
  194. return n;
  195. }
  196. case 2: /* 16 bit */
  197. {
  198. short __user *data;
  199. count /= 2;
  200. data = (short __user *) buf;
  201. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  202. put_user(dsp56k_host_interface.data.w[1], data+n++));
  203. return 2*n;
  204. }
  205. case 3: /* 24 bit */
  206. {
  207. count /= 3;
  208. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  209. put_user(dsp56k_host_interface.data.b[1], buf+n++);
  210. put_user(dsp56k_host_interface.data.b[2], buf+n++);
  211. put_user(dsp56k_host_interface.data.b[3], buf+n++));
  212. return 3*n;
  213. }
  214. case 4: /* 32 bit */
  215. {
  216. long __user *data;
  217. count /= 4;
  218. data = (long __user *) buf;
  219. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  220. put_user(dsp56k_host_interface.data.l, data+n++));
  221. return 4*n;
  222. }
  223. }
  224. return -EFAULT;
  225. }
  226. default:
  227. printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
  228. return -ENXIO;
  229. }
  230. }
  231. static ssize_t dsp56k_write(struct file *file, const char __user *buf, size_t count,
  232. loff_t *ppos)
  233. {
  234. struct inode *inode = file->f_dentry->d_inode;
  235. int dev = iminor(inode) & 0x0f;
  236. switch(dev)
  237. {
  238. case DSP56K_DEV_56001:
  239. {
  240. long n;
  241. /* Don't do anything if nothing is to be done */
  242. if (!count) return 0;
  243. n = 0;
  244. switch (dsp56k.tx_wsize) {
  245. case 1: /* 8 bit */
  246. {
  247. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  248. get_user(dsp56k_host_interface.data.b[3], buf+n++));
  249. return n;
  250. }
  251. case 2: /* 16 bit */
  252. {
  253. const short __user *data;
  254. count /= 2;
  255. data = (const short __user *)buf;
  256. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  257. get_user(dsp56k_host_interface.data.w[1], data+n++));
  258. return 2*n;
  259. }
  260. case 3: /* 24 bit */
  261. {
  262. count /= 3;
  263. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  264. get_user(dsp56k_host_interface.data.b[1], buf+n++);
  265. get_user(dsp56k_host_interface.data.b[2], buf+n++);
  266. get_user(dsp56k_host_interface.data.b[3], buf+n++));
  267. return 3*n;
  268. }
  269. case 4: /* 32 bit */
  270. {
  271. const long __user *data;
  272. count /= 4;
  273. data = (const long __user *)buf;
  274. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  275. get_user(dsp56k_host_interface.data.l, data+n++));
  276. return 4*n;
  277. }
  278. }
  279. return -EFAULT;
  280. }
  281. default:
  282. printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
  283. return -ENXIO;
  284. }
  285. }
  286. static int dsp56k_ioctl(struct inode *inode, struct file *file,
  287. unsigned int cmd, unsigned long arg)
  288. {
  289. int dev = iminor(inode) & 0x0f;
  290. void __user *argp = (void __user *)arg;
  291. switch(dev)
  292. {
  293. case DSP56K_DEV_56001:
  294. switch(cmd) {
  295. case DSP56K_UPLOAD:
  296. {
  297. char __user *bin;
  298. int r, len;
  299. struct dsp56k_upload __user *binary = argp;
  300. if(get_user(len, &binary->len) < 0)
  301. return -EFAULT;
  302. if(get_user(bin, &binary->bin) < 0)
  303. return -EFAULT;
  304. if (len == 0) {
  305. return -EINVAL; /* nothing to upload?!? */
  306. }
  307. if (len > DSP56K_MAX_BINARY_LENGTH) {
  308. return -EINVAL;
  309. }
  310. r = dsp56k_upload(bin, len);
  311. if (r < 0) {
  312. return r;
  313. }
  314. break;
  315. }
  316. case DSP56K_SET_TX_WSIZE:
  317. if (arg > 4 || arg < 1)
  318. return -EINVAL;
  319. dsp56k.tx_wsize = (int) arg;
  320. break;
  321. case DSP56K_SET_RX_WSIZE:
  322. if (arg > 4 || arg < 1)
  323. return -EINVAL;
  324. dsp56k.rx_wsize = (int) arg;
  325. break;
  326. case DSP56K_HOST_FLAGS:
  327. {
  328. int dir, out, status;
  329. struct dsp56k_host_flags __user *hf = argp;
  330. if(get_user(dir, &hf->dir) < 0)
  331. return -EFAULT;
  332. if(get_user(out, &hf->out) < 0)
  333. return -EFAULT;
  334. if ((dir & 0x1) && (out & 0x1))
  335. dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
  336. else if (dir & 0x1)
  337. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
  338. if ((dir & 0x2) && (out & 0x2))
  339. dsp56k_host_interface.icr |= DSP56K_ICR_HF1;
  340. else if (dir & 0x2)
  341. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
  342. status = 0;
  343. if (dsp56k_host_interface.icr & DSP56K_ICR_HF0) status |= 0x1;
  344. if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
  345. if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
  346. if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
  347. return put_user(status, &hf->status);
  348. }
  349. case DSP56K_HOST_CMD:
  350. if (arg > 31 || arg < 0)
  351. return -EINVAL;
  352. dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
  353. DSP56K_CVR_HC);
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. default:
  360. printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
  361. return -ENXIO;
  362. }
  363. }
  364. /* As of 2.1.26 this should be dsp56k_poll,
  365. * but how do I then check device minor number?
  366. * Do I need this function at all???
  367. */
  368. #if 0
  369. static unsigned int dsp56k_poll(struct file *file, poll_table *wait)
  370. {
  371. int dev = iminor(file->f_dentry->d_inode) & 0x0f;
  372. switch(dev)
  373. {
  374. case DSP56K_DEV_56001:
  375. /* poll_wait(file, ???, wait); */
  376. return POLLIN | POLLRDNORM | POLLOUT;
  377. default:
  378. printk("DSP56k driver: Unknown minor device: %d\n", dev);
  379. return 0;
  380. }
  381. }
  382. #endif
  383. static int dsp56k_open(struct inode *inode, struct file *file)
  384. {
  385. int dev = iminor(inode) & 0x0f;
  386. switch(dev)
  387. {
  388. case DSP56K_DEV_56001:
  389. if (test_and_set_bit(0, &dsp56k.in_use))
  390. return -EBUSY;
  391. dsp56k.timeout = TIMEOUT;
  392. dsp56k.maxio = MAXIO;
  393. dsp56k.rx_wsize = dsp56k.tx_wsize = 4;
  394. DSP56K_TX_INT_OFF;
  395. DSP56K_RX_INT_OFF;
  396. /* Zero host flags */
  397. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
  398. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
  399. break;
  400. default:
  401. return -ENODEV;
  402. }
  403. return 0;
  404. }
  405. static int dsp56k_release(struct inode *inode, struct file *file)
  406. {
  407. int dev = iminor(inode) & 0x0f;
  408. switch(dev)
  409. {
  410. case DSP56K_DEV_56001:
  411. clear_bit(0, &dsp56k.in_use);
  412. break;
  413. default:
  414. printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
  415. return -ENXIO;
  416. }
  417. return 0;
  418. }
  419. static struct file_operations dsp56k_fops = {
  420. .owner = THIS_MODULE,
  421. .read = dsp56k_read,
  422. .write = dsp56k_write,
  423. .ioctl = dsp56k_ioctl,
  424. .open = dsp56k_open,
  425. .release = dsp56k_release,
  426. };
  427. /****** Init and module functions ******/
  428. static char banner[] __initdata = KERN_INFO "DSP56k driver installed\n";
  429. static int __init dsp56k_init_driver(void)
  430. {
  431. int err = 0;
  432. if(!MACH_IS_ATARI || !ATARIHW_PRESENT(DSP56K)) {
  433. printk("DSP56k driver: Hardware not present\n");
  434. return -ENODEV;
  435. }
  436. if(register_chrdev(DSP56K_MAJOR, "dsp56k", &dsp56k_fops)) {
  437. printk("DSP56k driver: Unable to register driver\n");
  438. return -ENODEV;
  439. }
  440. dsp56k_class = class_create(THIS_MODULE, "dsp56k");
  441. if (IS_ERR(dsp56k_class)) {
  442. err = PTR_ERR(dsp56k_class);
  443. goto out_chrdev;
  444. }
  445. class_device_create(dsp56k_class, NULL, MKDEV(DSP56K_MAJOR, 0), NULL, "dsp56k");
  446. err = devfs_mk_cdev(MKDEV(DSP56K_MAJOR, 0),
  447. S_IFCHR | S_IRUSR | S_IWUSR, "dsp56k");
  448. if(err)
  449. goto out_class;
  450. printk(banner);
  451. goto out;
  452. out_class:
  453. class_device_destroy(dsp56k_class, MKDEV(DSP56K_MAJOR, 0));
  454. class_destroy(dsp56k_class);
  455. out_chrdev:
  456. unregister_chrdev(DSP56K_MAJOR, "dsp56k");
  457. out:
  458. return err;
  459. }
  460. module_init(dsp56k_init_driver);
  461. static void __exit dsp56k_cleanup_driver(void)
  462. {
  463. class_device_destroy(dsp56k_class, MKDEV(DSP56K_MAJOR, 0));
  464. class_destroy(dsp56k_class);
  465. unregister_chrdev(DSP56K_MAJOR, "dsp56k");
  466. devfs_remove("dsp56k");
  467. }
  468. module_exit(dsp56k_cleanup_driver);
  469. MODULE_LICENSE("GPL");