dsp56k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 struct class *dsp56k_class;
  93. static int dsp56k_reset(void)
  94. {
  95. u_char status;
  96. /* Power down the DSP */
  97. sound_ym.rd_data_reg_sel = 14;
  98. status = sound_ym.rd_data_reg_sel & 0xef;
  99. sound_ym.wd_data = status;
  100. sound_ym.wd_data = status | 0x10;
  101. udelay(10);
  102. /* Power up the DSP */
  103. sound_ym.rd_data_reg_sel = 14;
  104. sound_ym.wd_data = sound_ym.rd_data_reg_sel & 0xef;
  105. return 0;
  106. }
  107. static int dsp56k_upload(u_char __user *bin, int len)
  108. {
  109. struct platform_device *pdev;
  110. const struct firmware *fw;
  111. const char fw_name[] = "dsp56k/bootstrap.bin";
  112. int err;
  113. int i;
  114. dsp56k_reset();
  115. pdev = platform_device_register_simple("dsp56k", 0, NULL, 0);
  116. if (IS_ERR(pdev)) {
  117. printk(KERN_ERR "Failed to register device for \"%s\"\n",
  118. fw_name);
  119. return -EINVAL;
  120. }
  121. err = request_firmware(&fw, fw_name, &pdev->dev);
  122. platform_device_unregister(pdev);
  123. if (err) {
  124. printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
  125. fw_name, err);
  126. return err;
  127. }
  128. if (fw->size % 3) {
  129. printk(KERN_ERR "Bogus length %d in image \"%s\"\n",
  130. fw->size, fw_name);
  131. release_firmware(fw);
  132. return -EINVAL;
  133. }
  134. for (i = 0; i < fw->size; i = i + 3) {
  135. /* tx_wait(10); */
  136. dsp56k_host_interface.data.b[1] = fw->data[i];
  137. dsp56k_host_interface.data.b[2] = fw->data[i + 1];
  138. dsp56k_host_interface.data.b[3] = fw->data[i + 2];
  139. }
  140. release_firmware(fw);
  141. for (; i < 512; i++) {
  142. /* tx_wait(10); */
  143. dsp56k_host_interface.data.b[1] = 0;
  144. dsp56k_host_interface.data.b[2] = 0;
  145. dsp56k_host_interface.data.b[3] = 0;
  146. }
  147. for (i = 0; i < len; i++) {
  148. tx_wait(10);
  149. get_user(dsp56k_host_interface.data.b[1], bin++);
  150. get_user(dsp56k_host_interface.data.b[2], bin++);
  151. get_user(dsp56k_host_interface.data.b[3], bin++);
  152. }
  153. tx_wait(10);
  154. dsp56k_host_interface.data.l = 3; /* Magic execute */
  155. return 0;
  156. }
  157. static ssize_t dsp56k_read(struct file *file, char __user *buf, size_t count,
  158. loff_t *ppos)
  159. {
  160. struct inode *inode = file_inode(file);
  161. int dev = iminor(inode) & 0x0f;
  162. switch(dev)
  163. {
  164. case DSP56K_DEV_56001:
  165. {
  166. long n;
  167. /* Don't do anything if nothing is to be done */
  168. if (!count) return 0;
  169. n = 0;
  170. switch (dsp56k.rx_wsize) {
  171. case 1: /* 8 bit */
  172. {
  173. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  174. put_user(dsp56k_host_interface.data.b[3], buf+n++));
  175. return n;
  176. }
  177. case 2: /* 16 bit */
  178. {
  179. short __user *data;
  180. count /= 2;
  181. data = (short __user *) buf;
  182. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  183. put_user(dsp56k_host_interface.data.w[1], data+n++));
  184. return 2*n;
  185. }
  186. case 3: /* 24 bit */
  187. {
  188. count /= 3;
  189. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  190. put_user(dsp56k_host_interface.data.b[1], buf+n++);
  191. put_user(dsp56k_host_interface.data.b[2], buf+n++);
  192. put_user(dsp56k_host_interface.data.b[3], buf+n++));
  193. return 3*n;
  194. }
  195. case 4: /* 32 bit */
  196. {
  197. long __user *data;
  198. count /= 4;
  199. data = (long __user *) buf;
  200. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_RECEIVE,
  201. put_user(dsp56k_host_interface.data.l, data+n++));
  202. return 4*n;
  203. }
  204. }
  205. return -EFAULT;
  206. }
  207. default:
  208. printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
  209. return -ENXIO;
  210. }
  211. }
  212. static ssize_t dsp56k_write(struct file *file, const char __user *buf, size_t count,
  213. loff_t *ppos)
  214. {
  215. struct inode *inode = file_inode(file);
  216. int dev = iminor(inode) & 0x0f;
  217. switch(dev)
  218. {
  219. case DSP56K_DEV_56001:
  220. {
  221. long n;
  222. /* Don't do anything if nothing is to be done */
  223. if (!count) return 0;
  224. n = 0;
  225. switch (dsp56k.tx_wsize) {
  226. case 1: /* 8 bit */
  227. {
  228. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  229. get_user(dsp56k_host_interface.data.b[3], buf+n++));
  230. return n;
  231. }
  232. case 2: /* 16 bit */
  233. {
  234. const short __user *data;
  235. count /= 2;
  236. data = (const short __user *)buf;
  237. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  238. get_user(dsp56k_host_interface.data.w[1], data+n++));
  239. return 2*n;
  240. }
  241. case 3: /* 24 bit */
  242. {
  243. count /= 3;
  244. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  245. get_user(dsp56k_host_interface.data.b[1], buf+n++);
  246. get_user(dsp56k_host_interface.data.b[2], buf+n++);
  247. get_user(dsp56k_host_interface.data.b[3], buf+n++));
  248. return 3*n;
  249. }
  250. case 4: /* 32 bit */
  251. {
  252. const long __user *data;
  253. count /= 4;
  254. data = (const long __user *)buf;
  255. handshake(count, dsp56k.maxio, dsp56k.timeout, DSP56K_TRANSMIT,
  256. get_user(dsp56k_host_interface.data.l, data+n++));
  257. return 4*n;
  258. }
  259. }
  260. return -EFAULT;
  261. }
  262. default:
  263. printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
  264. return -ENXIO;
  265. }
  266. }
  267. static long dsp56k_ioctl(struct file *file, unsigned int cmd,
  268. unsigned long arg)
  269. {
  270. int dev = iminor(file_inode(file)) & 0x0f;
  271. void __user *argp = (void __user *)arg;
  272. switch(dev)
  273. {
  274. case DSP56K_DEV_56001:
  275. switch(cmd) {
  276. case DSP56K_UPLOAD:
  277. {
  278. char __user *bin;
  279. int r, len;
  280. struct dsp56k_upload __user *binary = argp;
  281. if(get_user(len, &binary->len) < 0)
  282. return -EFAULT;
  283. if(get_user(bin, &binary->bin) < 0)
  284. return -EFAULT;
  285. if (len <= 0) {
  286. return -EINVAL; /* nothing to upload?!? */
  287. }
  288. if (len > DSP56K_MAX_BINARY_LENGTH) {
  289. return -EINVAL;
  290. }
  291. mutex_lock(&dsp56k_mutex);
  292. r = dsp56k_upload(bin, len);
  293. mutex_unlock(&dsp56k_mutex);
  294. if (r < 0) {
  295. return r;
  296. }
  297. break;
  298. }
  299. case DSP56K_SET_TX_WSIZE:
  300. if (arg > 4 || arg < 1)
  301. return -EINVAL;
  302. mutex_lock(&dsp56k_mutex);
  303. dsp56k.tx_wsize = (int) arg;
  304. mutex_unlock(&dsp56k_mutex);
  305. break;
  306. case DSP56K_SET_RX_WSIZE:
  307. if (arg > 4 || arg < 1)
  308. return -EINVAL;
  309. mutex_lock(&dsp56k_mutex);
  310. dsp56k.rx_wsize = (int) arg;
  311. mutex_unlock(&dsp56k_mutex);
  312. break;
  313. case DSP56K_HOST_FLAGS:
  314. {
  315. int dir, out, status;
  316. struct dsp56k_host_flags __user *hf = argp;
  317. if(get_user(dir, &hf->dir) < 0)
  318. return -EFAULT;
  319. if(get_user(out, &hf->out) < 0)
  320. return -EFAULT;
  321. mutex_lock(&dsp56k_mutex);
  322. if ((dir & 0x1) && (out & 0x1))
  323. dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
  324. else if (dir & 0x1)
  325. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
  326. if ((dir & 0x2) && (out & 0x2))
  327. dsp56k_host_interface.icr |= DSP56K_ICR_HF1;
  328. else if (dir & 0x2)
  329. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
  330. status = 0;
  331. if (dsp56k_host_interface.icr & DSP56K_ICR_HF0) status |= 0x1;
  332. if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
  333. if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
  334. if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
  335. mutex_unlock(&dsp56k_mutex);
  336. return put_user(status, &hf->status);
  337. }
  338. case DSP56K_HOST_CMD:
  339. if (arg > 31)
  340. return -EINVAL;
  341. mutex_lock(&dsp56k_mutex);
  342. dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
  343. DSP56K_CVR_HC);
  344. mutex_unlock(&dsp56k_mutex);
  345. break;
  346. default:
  347. return -EINVAL;
  348. }
  349. return 0;
  350. default:
  351. printk(KERN_ERR "DSP56k driver: Unknown minor device: %d\n", dev);
  352. return -ENXIO;
  353. }
  354. }
  355. /* As of 2.1.26 this should be dsp56k_poll,
  356. * but how do I then check device minor number?
  357. * Do I need this function at all???
  358. */
  359. #if 0
  360. static __poll_t dsp56k_poll(struct file *file, poll_table *wait)
  361. {
  362. int dev = iminor(file_inode(file)) & 0x0f;
  363. switch(dev)
  364. {
  365. case DSP56K_DEV_56001:
  366. /* poll_wait(file, ???, wait); */
  367. return EPOLLIN | EPOLLRDNORM | EPOLLOUT;
  368. default:
  369. printk("DSP56k driver: Unknown minor device: %d\n", dev);
  370. return 0;
  371. }
  372. }
  373. #endif
  374. static int dsp56k_open(struct inode *inode, struct file *file)
  375. {
  376. int dev = iminor(inode) & 0x0f;
  377. int ret = 0;
  378. mutex_lock(&dsp56k_mutex);
  379. switch(dev)
  380. {
  381. case DSP56K_DEV_56001:
  382. if (test_and_set_bit(0, &dsp56k.in_use)) {
  383. ret = -EBUSY;
  384. goto out;
  385. }
  386. dsp56k.timeout = TIMEOUT;
  387. dsp56k.maxio = MAXIO;
  388. dsp56k.rx_wsize = dsp56k.tx_wsize = 4;
  389. DSP56K_TX_INT_OFF;
  390. DSP56K_RX_INT_OFF;
  391. /* Zero host flags */
  392. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF0;
  393. dsp56k_host_interface.icr &= ~DSP56K_ICR_HF1;
  394. break;
  395. default:
  396. ret = -ENODEV;
  397. }
  398. out:
  399. mutex_unlock(&dsp56k_mutex);
  400. return ret;
  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. .unlocked_ioctl = dsp56k_ioctl,
  421. .open = dsp56k_open,
  422. .release = dsp56k_release,
  423. .llseek = noop_llseek,
  424. };
  425. /****** Init and module functions ******/
  426. static const char banner[] __initconst = 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. 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_destroy(dsp56k_class);
  457. unregister_chrdev(DSP56K_MAJOR, "dsp56k");
  458. }
  459. module_exit(dsp56k_cleanup_driver);
  460. MODULE_LICENSE("GPL");
  461. MODULE_FIRMWARE("dsp56k/bootstrap.bin");