dsp56k.c 12 KB

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