coh901318_lli.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * driver/dma/coh901318_lli.c
  3. *
  4. * Copyright (C) 2007-2009 ST-Ericsson
  5. * License terms: GNU General Public License (GPL) version 2
  6. * Support functions for handling lli for dma
  7. * Author: Per Friden <per.friden@stericsson.com>
  8. */
  9. #include <linux/dma-mapping.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/dmapool.h>
  12. #include <linux/memory.h>
  13. #include <linux/gfp.h>
  14. #include <mach/coh901318.h>
  15. #include "coh901318_lli.h"
  16. #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_U300_DEBUG))
  17. #define DEBUGFS_POOL_COUNTER_RESET(pool) (pool->debugfs_pool_counter = 0)
  18. #define DEBUGFS_POOL_COUNTER_ADD(pool, add) (pool->debugfs_pool_counter += add)
  19. #else
  20. #define DEBUGFS_POOL_COUNTER_RESET(pool)
  21. #define DEBUGFS_POOL_COUNTER_ADD(pool, add)
  22. #endif
  23. static struct coh901318_lli *
  24. coh901318_lli_next(struct coh901318_lli *data)
  25. {
  26. if (data == NULL || data->link_addr == 0)
  27. return NULL;
  28. return (struct coh901318_lli *) data->virt_link_addr;
  29. }
  30. int coh901318_pool_create(struct coh901318_pool *pool,
  31. struct device *dev,
  32. size_t size, size_t align)
  33. {
  34. spin_lock_init(&pool->lock);
  35. pool->dev = dev;
  36. pool->dmapool = dma_pool_create("lli_pool", dev, size, align, 0);
  37. DEBUGFS_POOL_COUNTER_RESET(pool);
  38. return 0;
  39. }
  40. int coh901318_pool_destroy(struct coh901318_pool *pool)
  41. {
  42. dma_pool_destroy(pool->dmapool);
  43. return 0;
  44. }
  45. struct coh901318_lli *
  46. coh901318_lli_alloc(struct coh901318_pool *pool, unsigned int len)
  47. {
  48. int i;
  49. struct coh901318_lli *head;
  50. struct coh901318_lli *lli;
  51. struct coh901318_lli *lli_prev;
  52. dma_addr_t phy;
  53. if (len == 0)
  54. goto err;
  55. spin_lock(&pool->lock);
  56. head = dma_pool_alloc(pool->dmapool, GFP_NOWAIT, &phy);
  57. if (head == NULL)
  58. goto err;
  59. DEBUGFS_POOL_COUNTER_ADD(pool, 1);
  60. lli = head;
  61. lli->phy_this = phy;
  62. lli->link_addr = 0x00000000;
  63. lli->virt_link_addr = 0x00000000U;
  64. for (i = 1; i < len; i++) {
  65. lli_prev = lli;
  66. lli = dma_pool_alloc(pool->dmapool, GFP_NOWAIT, &phy);
  67. if (lli == NULL)
  68. goto err_clean_up;
  69. DEBUGFS_POOL_COUNTER_ADD(pool, 1);
  70. lli->phy_this = phy;
  71. lli->link_addr = 0x00000000;
  72. lli->virt_link_addr = 0x00000000U;
  73. lli_prev->link_addr = phy;
  74. lli_prev->virt_link_addr = lli;
  75. }
  76. spin_unlock(&pool->lock);
  77. return head;
  78. err:
  79. spin_unlock(&pool->lock);
  80. return NULL;
  81. err_clean_up:
  82. lli_prev->link_addr = 0x00000000U;
  83. spin_unlock(&pool->lock);
  84. coh901318_lli_free(pool, &head);
  85. return NULL;
  86. }
  87. void coh901318_lli_free(struct coh901318_pool *pool,
  88. struct coh901318_lli **lli)
  89. {
  90. struct coh901318_lli *l;
  91. struct coh901318_lli *next;
  92. if (lli == NULL)
  93. return;
  94. l = *lli;
  95. if (l == NULL)
  96. return;
  97. spin_lock(&pool->lock);
  98. while (l->link_addr) {
  99. next = l->virt_link_addr;
  100. dma_pool_free(pool->dmapool, l, l->phy_this);
  101. DEBUGFS_POOL_COUNTER_ADD(pool, -1);
  102. l = next;
  103. }
  104. dma_pool_free(pool->dmapool, l, l->phy_this);
  105. DEBUGFS_POOL_COUNTER_ADD(pool, -1);
  106. spin_unlock(&pool->lock);
  107. *lli = NULL;
  108. }
  109. int
  110. coh901318_lli_fill_memcpy(struct coh901318_pool *pool,
  111. struct coh901318_lli *lli,
  112. dma_addr_t source, unsigned int size,
  113. dma_addr_t destination, u32 ctrl_chained,
  114. u32 ctrl_eom)
  115. {
  116. int s = size;
  117. dma_addr_t src = source;
  118. dma_addr_t dst = destination;
  119. lli->src_addr = src;
  120. lli->dst_addr = dst;
  121. while (lli->link_addr) {
  122. lli->control = ctrl_chained | MAX_DMA_PACKET_SIZE;
  123. lli->src_addr = src;
  124. lli->dst_addr = dst;
  125. s -= MAX_DMA_PACKET_SIZE;
  126. lli = coh901318_lli_next(lli);
  127. src += MAX_DMA_PACKET_SIZE;
  128. dst += MAX_DMA_PACKET_SIZE;
  129. }
  130. lli->control = ctrl_eom | s;
  131. lli->src_addr = src;
  132. lli->dst_addr = dst;
  133. return 0;
  134. }
  135. int
  136. coh901318_lli_fill_single(struct coh901318_pool *pool,
  137. struct coh901318_lli *lli,
  138. dma_addr_t buf, unsigned int size,
  139. dma_addr_t dev_addr, u32 ctrl_chained, u32 ctrl_eom,
  140. enum dma_data_direction dir)
  141. {
  142. int s = size;
  143. dma_addr_t src;
  144. dma_addr_t dst;
  145. if (dir == DMA_TO_DEVICE) {
  146. src = buf;
  147. dst = dev_addr;
  148. } else if (dir == DMA_FROM_DEVICE) {
  149. src = dev_addr;
  150. dst = buf;
  151. } else {
  152. return -EINVAL;
  153. }
  154. while (lli->link_addr) {
  155. size_t block_size = MAX_DMA_PACKET_SIZE;
  156. lli->control = ctrl_chained | MAX_DMA_PACKET_SIZE;
  157. /* If we are on the next-to-final block and there will
  158. * be less than half a DMA packet left for the last
  159. * block, then we want to make this block a little
  160. * smaller to balance the sizes. This is meant to
  161. * avoid too small transfers if the buffer size is
  162. * (MAX_DMA_PACKET_SIZE*N + 1) */
  163. if (s < (MAX_DMA_PACKET_SIZE + MAX_DMA_PACKET_SIZE/2))
  164. block_size = MAX_DMA_PACKET_SIZE/2;
  165. s -= block_size;
  166. lli->src_addr = src;
  167. lli->dst_addr = dst;
  168. lli = coh901318_lli_next(lli);
  169. if (dir == DMA_TO_DEVICE)
  170. src += block_size;
  171. else if (dir == DMA_FROM_DEVICE)
  172. dst += block_size;
  173. }
  174. lli->control = ctrl_eom | s;
  175. lli->src_addr = src;
  176. lli->dst_addr = dst;
  177. return 0;
  178. }
  179. int
  180. coh901318_lli_fill_sg(struct coh901318_pool *pool,
  181. struct coh901318_lli *lli,
  182. struct scatterlist *sgl, unsigned int nents,
  183. dma_addr_t dev_addr, u32 ctrl_chained, u32 ctrl,
  184. u32 ctrl_last,
  185. enum dma_data_direction dir, u32 ctrl_irq_mask)
  186. {
  187. int i;
  188. struct scatterlist *sg;
  189. u32 ctrl_sg;
  190. dma_addr_t src = 0;
  191. dma_addr_t dst = 0;
  192. u32 bytes_to_transfer;
  193. u32 elem_size;
  194. if (lli == NULL)
  195. goto err;
  196. spin_lock(&pool->lock);
  197. if (dir == DMA_TO_DEVICE)
  198. dst = dev_addr;
  199. else if (dir == DMA_FROM_DEVICE)
  200. src = dev_addr;
  201. else
  202. goto err;
  203. for_each_sg(sgl, sg, nents, i) {
  204. if (sg_is_chain(sg)) {
  205. /* sg continues to the next sg-element don't
  206. * send ctrl_finish until the last
  207. * sg-element in the chain
  208. */
  209. ctrl_sg = ctrl_chained;
  210. } else if (i == nents - 1)
  211. ctrl_sg = ctrl_last;
  212. else
  213. ctrl_sg = ctrl ? ctrl : ctrl_last;
  214. if (dir == DMA_TO_DEVICE)
  215. /* increment source address */
  216. src = sg_phys(sg);
  217. else
  218. /* increment destination address */
  219. dst = sg_phys(sg);
  220. bytes_to_transfer = sg_dma_len(sg);
  221. while (bytes_to_transfer) {
  222. u32 val;
  223. if (bytes_to_transfer > MAX_DMA_PACKET_SIZE) {
  224. elem_size = MAX_DMA_PACKET_SIZE;
  225. val = ctrl_chained;
  226. } else {
  227. elem_size = bytes_to_transfer;
  228. val = ctrl_sg;
  229. }
  230. lli->control = val | elem_size;
  231. lli->src_addr = src;
  232. lli->dst_addr = dst;
  233. if (dir == DMA_FROM_DEVICE)
  234. dst += elem_size;
  235. else
  236. src += elem_size;
  237. BUG_ON(lli->link_addr & 3);
  238. bytes_to_transfer -= elem_size;
  239. lli = coh901318_lli_next(lli);
  240. }
  241. }
  242. spin_unlock(&pool->lock);
  243. return 0;
  244. err:
  245. spin_unlock(&pool->lock);
  246. return -EINVAL;
  247. }