dsp56k.c 13 KB

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