You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * edma.c
  3. *
  4. * EDMA kernel driver
  5. *
  6. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. /*
  18. * ============================================================================
  19. * Copyright (c) Texas Instruments Inc 2010-11
  20. *
  21. * Use of this software is controlled by the terms and conditions found in the
  22. * license agreement under which this software has been supplied or provided.
  23. * ============================================================================
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/fs.h>
  29. #include <linux/mm.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/sched.h>
  32. #include <linux/list.h>
  33. #include <linux/cdev.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/kernel.h>
  36. #include <linux/device.h>
  37. #include <asm/page.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/io.h>
  41. #include<asm/system.h>
  42. #include <asm/irq.h>
  43. #include <linux/completion.h>
  44. #include <mach/edma.h>
  45. #include "edmautils.h"
  46. #ifdef __DEBUG
  47. #define __D(fmt, args...) printk(KERN_DEBUG "edma Debug: " fmt, ## args)
  48. #else
  49. #define __D(fmt, args...)
  50. #endif
  51. #define __E(fmt, args...) printk(KERN_ERR "edma Error: " fmt, ## args)
  52. typedef struct _dmacompletion {
  53. int channel;
  54. struct completion completion_evt;
  55. } dmacompletion_t;
  56. static int major = 0, channels_allocated = 0;
  57. static dmacompletion_t edma_completion[MAX_EDMA_CHANNELS];
  58. #if (USE_UDEV==1)
  59. static struct class *edma_class;
  60. #endif //USE_UDEV
  61. /* Forward declaration of system calls */
  62. static int ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  63. unsigned long args);
  64. //static int mmap (struct file *filp, struct vm_area_struct *vma);
  65. static int open(struct inode *inode, struct file *filp);
  66. static int release(struct inode *inode, struct file *filp);
  67. static struct file_operations edma_fxns = {
  68. .owner = THIS_MODULE,
  69. ioctl:ioctl,
  70. //mmap:mmap,
  71. open:open,
  72. release:release
  73. };
  74. #if 0
  75. static int mmap(struct file *filp, struct vm_area_struct *vma)
  76. {
  77. __D("mmap: vma->vm_start = %#lx\n", vma->vm_start);
  78. __D("mmap: vma->vm_pgoff = %#lx\n", vma->vm_pgoff);
  79. __D("mmap: vma->vm_end = %#lx\n", vma->vm_end);
  80. __D("mmap: size = %#lx\n", vma->vm_end - vma->vm_start);
  81. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  82. if (remap_pfn_range(vma, vma->vm_start,
  83. vma->vm_pgoff,
  84. vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
  85. __E("mmap: failed remap_pfn_range\n");
  86. return -EAGAIN;
  87. }
  88. return 0;
  89. }
  90. #endif
  91. static void edma_callback(unsigned channel, u16 ch_status, void *data)
  92. {
  93. int i;
  94. edma_stop(channel);
  95. for (i = 0; i < MAX_EDMA_CHANNELS; i++) {
  96. if (edma_completion[i].channel == channel)
  97. break;
  98. }
  99. if (i < MAX_EDMA_CHANNELS)
  100. complete(&edma_completion[i].completion_evt);
  101. }
  102. int EDMAAlloc(void)
  103. {
  104. int channel, i;
  105. if (channels_allocated >= MAX_EDMA_CHANNELS)
  106. return -EFAULT;
  107. channel = edma_alloc_channel(EDMA_CHANNEL_ANY, &edma_callback, NULL,
  108. EVENTQ_1);
  109. edma_set_src_index(channel, 0, 0);
  110. edma_set_dest_index(channel, 0, 0);
  111. for (i = 0; i < MAX_EDMA_CHANNELS; i++) {
  112. if (edma_completion[i].channel == -1)
  113. break;
  114. }
  115. edma_completion[i].channel = channel;
  116. channels_allocated++;
  117. return channel;
  118. }
  119. int EDMAFree(unsigned int channel)
  120. {
  121. int i;
  122. edma_free_channel(channel);
  123. for (i = 0; i < MAX_EDMA_CHANNELS; i++) {
  124. if (edma_completion[i].channel == channel)
  125. break;
  126. }
  127. if (i == MAX_EDMA_CHANNELS)
  128. return -EFAULT;
  129. edma_completion[i].channel = -1;
  130. channels_allocated--;
  131. return 0;
  132. }
  133. int EDMAMemCpy(memcpyparams_t memcpyparams)
  134. {
  135. struct edmacc_param p_ram;
  136. int i;
  137. edma_set_src(memcpyparams.channel, memcpyparams.srcaddr, INCR, W8BIT);
  138. edma_set_dest(memcpyparams.channel, memcpyparams.dstaddr, INCR, W8BIT);
  139. edma_set_transfer_params(memcpyparams.channel, memcpyparams.len, 1,
  140. 1, 0, ASYNC);
  141. edma_read_slot(memcpyparams.channel, &p_ram);
  142. p_ram.opt |= TCINTEN | EDMA_TCC(EDMA_CHAN_SLOT(memcpyparams.channel));
  143. edma_write_slot(memcpyparams.channel, &p_ram);
  144. edma_start(memcpyparams.channel);
  145. for (i = 0; i < MAX_EDMA_CHANNELS; i++) {
  146. if (edma_completion[i].channel == memcpyparams.channel)
  147. break;
  148. }
  149. if (i == MAX_EDMA_CHANNELS)
  150. return -EFAULT;
  151. wait_for_completion_interruptible(&edma_completion[i].completion_evt);
  152. return 0;
  153. }
  154. int EDMAMemSet(memsetparams_t memsetparams)
  155. {
  156. struct edmacc_param p_ram;
  157. int i;
  158. edma_set_src(memsetparams.channel, memsetparams.patternaddr, FIFO,
  159. W8BIT);
  160. edma_set_dest(memsetparams.channel, memsetparams.dstaddr, INCR, W8BIT);
  161. edma_set_transfer_params(memsetparams.channel, memsetparams.len, 1,
  162. 1, 0, ASYNC);
  163. edma_read_slot(memsetparams.channel, &p_ram);
  164. p_ram.opt |= TCINTEN | EDMA_TCC(EDMA_CHAN_SLOT(memsetparams.channel));
  165. edma_write_slot(memsetparams.channel, &p_ram);
  166. edma_start(memsetparams.channel);
  167. for (i = 0; i < MAX_EDMA_CHANNELS; i++) {
  168. if (edma_completion[i].channel == memsetparams.channel)
  169. break;
  170. }
  171. if (i == MAX_EDMA_CHANNELS)
  172. return -EFAULT;
  173. wait_for_completion_interruptible(&edma_completion[i].completion_evt);
  174. return 0;
  175. }
  176. static int
  177. ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  178. unsigned long args)
  179. {
  180. unsigned int __user *argp = (unsigned int __user *) args;
  181. int channel;
  182. switch (cmd) {
  183. case EDMADRV_IOCMEMCPY_EDMA:
  184. {
  185. memcpyparams_t memcpy_params;
  186. if (copy_from_user
  187. (&memcpy_params, argp, sizeof(memcpy_params))) {
  188. __E("MEMCPY_EDMA failed\n");
  189. return -EFAULT;
  190. }
  191. return EDMAMemCpy(memcpy_params);
  192. }
  193. break;
  194. case EDMADRV_IOCMEMSET_EDMA:
  195. {
  196. memsetparams_t memset_params;
  197. if (copy_from_user
  198. (&memset_params, argp, sizeof(memset_params))) {
  199. __E("MEMSET_EDMA failed\n");
  200. return -EFAULT;
  201. }
  202. return EDMAMemSet(memset_params);
  203. }
  204. break;
  205. case EDMADRV_IOCALLOC_EDMA:
  206. {
  207. channel = EDMAAlloc();
  208. if (put_user(channel, argp)) {
  209. __E("ALLOC_ARMEDMA - put_user failed\n");
  210. return -EFAULT;
  211. }
  212. return 0;
  213. }
  214. break;
  215. case EDMADRV_IOCFREE_EDMA:
  216. {
  217. if (get_user(channel, argp)) {
  218. __E("FREE_ARMEDMA - get_user failed\n");
  219. return -EFAULT;
  220. }
  221. EDMAFree(channel);
  222. return 0;
  223. }
  224. break;
  225. default:
  226. __E("Unknown ioctl received = %d.\n", cmd);
  227. return -EINVAL;
  228. }
  229. return 0;
  230. }
  231. static int open(struct inode *inode, struct file *filp)
  232. {
  233. __D("open: called.\n");
  234. return 0;
  235. }
  236. static int release(struct inode *inode, struct file *filp)
  237. {
  238. __D("close: called.");
  239. return 0;
  240. }
  241. int __init edma_init(void)
  242. {
  243. int i;
  244. __D("** EDMA kernel module built: " __DATE__ " " __TIME__ "\n");
  245. major = register_chrdev(DRIVER_MAJOR, "edma", &edma_fxns);
  246. if (major < 0) {
  247. __E("Failed to allocate major number.\n");
  248. return -ENODEV;
  249. }
  250. #if (USE_UDEV==1)
  251. edma_class = class_create(THIS_MODULE, "edma");
  252. if (IS_ERR(edma_class)) {
  253. __E("Error creating edma device class.\n");
  254. return -EIO;
  255. }
  256. device_create(edma_class, NULL, MKDEV(major, 0), NULL, "edma");
  257. #endif //USE_UDEV
  258. for (i = 0; i < MAX_EDMA_CHANNELS; i++) {
  259. init_completion(&edma_completion[i].completion_evt);
  260. edma_completion[i].channel = -1;
  261. }
  262. return 0;
  263. }
  264. void __exit edma_exit(void)
  265. {
  266. #if (USE_UDEV==1)
  267. device_destroy(edma_class, MKDEV(major, 0));
  268. class_destroy(edma_class);
  269. #endif // USE_UDEV
  270. unregister_chrdev(major, "edma");
  271. }
  272. module_init(edma_init);
  273. module_exit(edma_exit);
  274. MODULE_LICENSE("GPL");
  275. MODULE_AUTHOR("Texas Instruments");
  276. MODULE_DESCRIPTION("EDMA usercopy driver");