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.

169 lines
4.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * edma.c
  3. *
  4. * User space driver for edma
  5. *
  6. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * Neither the name of Texas Instruments Incorporated nor the names of
  22. * its contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /*
  39. * ============================================================================
  40. * Copyright (c) Texas Instruments Inc 2010-11
  41. *
  42. * Use of this software is controlled by the terms and conditions found in the
  43. * license agreement under which this software has been supplied or provided.
  44. * ============================================================================
  45. */
  46. #include <stdio.h>
  47. #include <unistd.h>
  48. #include <sys/mman.h>
  49. #include <sys/stat.h>
  50. #include <sys/ioctl.h>
  51. #include <fcntl.h>
  52. #include <pthread.h>
  53. #include <stdlib.h>
  54. #include <strings.h>
  55. #include "../module/edmautils.h"
  56. #include "edma.h"
  57. #define EDMA_DEVICE "/dev/edma"
  58. #ifdef __DEBUG
  59. #define __D(fmt, args...) fprintf(stderr, "edma Debug: " fmt, ## args)
  60. #else
  61. #define __D(fmt, args...)
  62. #endif
  63. #ifdef __DEBUG
  64. #define __E(fmt, args...) fprintf(stderr, "edma Error: " fmt, ## args)
  65. #else
  66. #define __E(fmt, args...)
  67. #endif
  68. static int refcount, edma_fd = -1;
  69. int
  70. edma_memcpy(int channel, unsigned int dst, unsigned int src,
  71. unsigned int length)
  72. {
  73. memcpyparams_t memcpyparams;
  74. if (edma_fd == -1) {
  75. __E("You must initialize EDMA before making API calls\n");
  76. return -1;
  77. }
  78. memcpyparams.channel = channel;
  79. memcpyparams.srcaddr = src;
  80. memcpyparams.dstaddr = dst;
  81. memcpyparams.len = length;
  82. if (ioctl(edma_fd, EDMADRV_IOCMEMCPY_EDMA, &memcpyparams) == -1) {
  83. __E("%s could not do memcpy using EDMA\n", EDMA_DEVICE);
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. int
  89. edma_memset(int channel, unsigned int dst, unsigned int patternaddr,
  90. unsigned int length)
  91. {
  92. memsetparams_t memsetparams;
  93. if (edma_fd == -1) {
  94. __E("You must initialize EDMA before making API calls\n");
  95. return -1;
  96. }
  97. memsetparams.channel = channel;
  98. memsetparams.patternaddr = patternaddr;
  99. memsetparams.dstaddr = dst;
  100. memsetparams.len = length;
  101. if (ioctl(edma_fd, EDMADRV_IOCMEMSET_EDMA, &memsetparams) == -1) {
  102. __E("%s could not do memcpy using EDMA\n", EDMA_DEVICE);
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. int edma_allocate_channel(int *channel)
  108. {
  109. if (refcount == 0) {
  110. edma_fd = open(EDMA_DEVICE, O_RDWR | O_SYNC); //O_SYNC is essential
  111. if (edma_fd == -1) {
  112. __E("init: Failed to open %s\n", EDMA_DEVICE);
  113. return -1;
  114. }
  115. }
  116. refcount++;
  117. if (ioctl(edma_fd, EDMADRV_IOCALLOC_EDMA, channel) == -1) {
  118. __E("%s could not alloc EDMA channel:%d\n", EDMA_DEVICE, *channel);
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. int edma_free_channel(int channel)
  124. {
  125. int result = 0;
  126. if (edma_fd == -1) {
  127. __E("exit: You must initialize EDMA before making API calls.\n");
  128. result = -1;
  129. goto end;
  130. }
  131. refcount--;
  132. if (ioctl(edma_fd, EDMADRV_IOCFREE_EDMA, &channel) == -1) {
  133. __E("%s could not free EDMA channel: %d\n", EDMA_DEVICE, channel);
  134. result = -1;
  135. }
  136. if (refcount == 0) {
  137. result = close(edma_fd);
  138. edma_fd = -1;
  139. }
  140. end:
  141. return result;
  142. }