dsp56k.c 13 KB

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