ste_dma40_ll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2007-2010
  3. * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
  4. * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
  5. * License terms: GNU General Public License (GPL) version 2
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/platform_data/dma-ste-dma40.h>
  9. #include "ste_dma40_ll.h"
  10. /* Sets up proper LCSP1 and LCSP3 register for a logical channel */
  11. void d40_log_cfg(struct stedma40_chan_cfg *cfg,
  12. u32 *lcsp1, u32 *lcsp3)
  13. {
  14. u32 l3 = 0; /* dst */
  15. u32 l1 = 0; /* src */
  16. /* src is mem? -> increase address pos */
  17. if (cfg->dir == STEDMA40_MEM_TO_PERIPH ||
  18. cfg->dir == STEDMA40_MEM_TO_MEM)
  19. l1 |= 1 << D40_MEM_LCSP1_SCFG_INCR_POS;
  20. /* dst is mem? -> increase address pos */
  21. if (cfg->dir == STEDMA40_PERIPH_TO_MEM ||
  22. cfg->dir == STEDMA40_MEM_TO_MEM)
  23. l3 |= 1 << D40_MEM_LCSP3_DCFG_INCR_POS;
  24. /* src is hw? -> master port 1 */
  25. if (cfg->dir == STEDMA40_PERIPH_TO_MEM ||
  26. cfg->dir == STEDMA40_PERIPH_TO_PERIPH)
  27. l1 |= 1 << D40_MEM_LCSP1_SCFG_MST_POS;
  28. /* dst is hw? -> master port 1 */
  29. if (cfg->dir == STEDMA40_MEM_TO_PERIPH ||
  30. cfg->dir == STEDMA40_PERIPH_TO_PERIPH)
  31. l3 |= 1 << D40_MEM_LCSP3_DCFG_MST_POS;
  32. l3 |= 1 << D40_MEM_LCSP3_DCFG_EIM_POS;
  33. l3 |= cfg->dst_info.psize << D40_MEM_LCSP3_DCFG_PSIZE_POS;
  34. l3 |= cfg->dst_info.data_width << D40_MEM_LCSP3_DCFG_ESIZE_POS;
  35. l1 |= 1 << D40_MEM_LCSP1_SCFG_EIM_POS;
  36. l1 |= cfg->src_info.psize << D40_MEM_LCSP1_SCFG_PSIZE_POS;
  37. l1 |= cfg->src_info.data_width << D40_MEM_LCSP1_SCFG_ESIZE_POS;
  38. *lcsp1 = l1;
  39. *lcsp3 = l3;
  40. }
  41. void d40_phy_cfg(struct stedma40_chan_cfg *cfg, u32 *src_cfg, u32 *dst_cfg)
  42. {
  43. u32 src = 0;
  44. u32 dst = 0;
  45. if ((cfg->dir == STEDMA40_PERIPH_TO_MEM) ||
  46. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  47. /* Set master port to 1 */
  48. src |= 1 << D40_SREG_CFG_MST_POS;
  49. src |= D40_TYPE_TO_EVENT(cfg->dev_type);
  50. if (cfg->src_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  51. src |= 1 << D40_SREG_CFG_PHY_TM_POS;
  52. else
  53. src |= 3 << D40_SREG_CFG_PHY_TM_POS;
  54. }
  55. if ((cfg->dir == STEDMA40_MEM_TO_PERIPH) ||
  56. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  57. /* Set master port to 1 */
  58. dst |= 1 << D40_SREG_CFG_MST_POS;
  59. dst |= D40_TYPE_TO_EVENT(cfg->dev_type);
  60. if (cfg->dst_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  61. dst |= 1 << D40_SREG_CFG_PHY_TM_POS;
  62. else
  63. dst |= 3 << D40_SREG_CFG_PHY_TM_POS;
  64. }
  65. /* Interrupt on end of transfer for destination */
  66. dst |= 1 << D40_SREG_CFG_TIM_POS;
  67. /* Generate interrupt on error */
  68. src |= 1 << D40_SREG_CFG_EIM_POS;
  69. dst |= 1 << D40_SREG_CFG_EIM_POS;
  70. /* PSIZE */
  71. if (cfg->src_info.psize != STEDMA40_PSIZE_PHY_1) {
  72. src |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  73. src |= cfg->src_info.psize << D40_SREG_CFG_PSIZE_POS;
  74. }
  75. if (cfg->dst_info.psize != STEDMA40_PSIZE_PHY_1) {
  76. dst |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  77. dst |= cfg->dst_info.psize << D40_SREG_CFG_PSIZE_POS;
  78. }
  79. /* Element size */
  80. src |= cfg->src_info.data_width << D40_SREG_CFG_ESIZE_POS;
  81. dst |= cfg->dst_info.data_width << D40_SREG_CFG_ESIZE_POS;
  82. /* Set the priority bit to high for the physical channel */
  83. if (cfg->high_priority) {
  84. src |= 1 << D40_SREG_CFG_PRI_POS;
  85. dst |= 1 << D40_SREG_CFG_PRI_POS;
  86. }
  87. if (cfg->src_info.big_endian)
  88. src |= 1 << D40_SREG_CFG_LBE_POS;
  89. if (cfg->dst_info.big_endian)
  90. dst |= 1 << D40_SREG_CFG_LBE_POS;
  91. *src_cfg = src;
  92. *dst_cfg = dst;
  93. }
  94. static int d40_phy_fill_lli(struct d40_phy_lli *lli,
  95. dma_addr_t data,
  96. u32 data_size,
  97. dma_addr_t next_lli,
  98. u32 reg_cfg,
  99. struct stedma40_half_channel_info *info,
  100. unsigned int flags)
  101. {
  102. bool addr_inc = flags & LLI_ADDR_INC;
  103. bool term_int = flags & LLI_TERM_INT;
  104. unsigned int data_width = info->data_width;
  105. int psize = info->psize;
  106. int num_elems;
  107. if (psize == STEDMA40_PSIZE_PHY_1)
  108. num_elems = 1;
  109. else
  110. num_elems = 2 << psize;
  111. /* Must be aligned */
  112. if (!IS_ALIGNED(data, 0x1 << data_width))
  113. return -EINVAL;
  114. /* Transfer size can't be smaller than (num_elms * elem_size) */
  115. if (data_size < num_elems * (0x1 << data_width))
  116. return -EINVAL;
  117. /* The number of elements. IE now many chunks */
  118. lli->reg_elt = (data_size >> data_width) << D40_SREG_ELEM_PHY_ECNT_POS;
  119. /*
  120. * Distance to next element sized entry.
  121. * Usually the size of the element unless you want gaps.
  122. */
  123. if (addr_inc)
  124. lli->reg_elt |= (0x1 << data_width) <<
  125. D40_SREG_ELEM_PHY_EIDX_POS;
  126. /* Where the data is */
  127. lli->reg_ptr = data;
  128. lli->reg_cfg = reg_cfg;
  129. /* If this scatter list entry is the last one, no next link */
  130. if (next_lli == 0)
  131. lli->reg_lnk = 0x1 << D40_SREG_LNK_PHY_TCP_POS;
  132. else
  133. lli->reg_lnk = next_lli;
  134. /* Set/clear interrupt generation on this link item.*/
  135. if (term_int)
  136. lli->reg_cfg |= 0x1 << D40_SREG_CFG_TIM_POS;
  137. else
  138. lli->reg_cfg &= ~(0x1 << D40_SREG_CFG_TIM_POS);
  139. /* Post link */
  140. lli->reg_lnk |= 0 << D40_SREG_LNK_PHY_PRE_POS;
  141. return 0;
  142. }
  143. static int d40_seg_size(int size, int data_width1, int data_width2)
  144. {
  145. u32 max_w = max(data_width1, data_width2);
  146. u32 min_w = min(data_width1, data_width2);
  147. u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
  148. if (seg_max > STEDMA40_MAX_SEG_SIZE)
  149. seg_max -= (1 << max_w);
  150. if (size <= seg_max)
  151. return size;
  152. if (size <= 2 * seg_max)
  153. return ALIGN(size / 2, 1 << max_w);
  154. return seg_max;
  155. }
  156. static struct d40_phy_lli *
  157. d40_phy_buf_to_lli(struct d40_phy_lli *lli, dma_addr_t addr, u32 size,
  158. dma_addr_t lli_phys, dma_addr_t first_phys, u32 reg_cfg,
  159. struct stedma40_half_channel_info *info,
  160. struct stedma40_half_channel_info *otherinfo,
  161. unsigned long flags)
  162. {
  163. bool lastlink = flags & LLI_LAST_LINK;
  164. bool addr_inc = flags & LLI_ADDR_INC;
  165. bool term_int = flags & LLI_TERM_INT;
  166. bool cyclic = flags & LLI_CYCLIC;
  167. int err;
  168. dma_addr_t next = lli_phys;
  169. int size_rest = size;
  170. int size_seg = 0;
  171. /*
  172. * This piece may be split up based on d40_seg_size(); we only want the
  173. * term int on the last part.
  174. */
  175. if (term_int)
  176. flags &= ~LLI_TERM_INT;
  177. do {
  178. size_seg = d40_seg_size(size_rest, info->data_width,
  179. otherinfo->data_width);
  180. size_rest -= size_seg;
  181. if (size_rest == 0 && term_int)
  182. flags |= LLI_TERM_INT;
  183. if (size_rest == 0 && lastlink)
  184. next = cyclic ? first_phys : 0;
  185. else
  186. next = ALIGN(next + sizeof(struct d40_phy_lli),
  187. D40_LLI_ALIGN);
  188. err = d40_phy_fill_lli(lli, addr, size_seg, next,
  189. reg_cfg, info, flags);
  190. if (err)
  191. goto err;
  192. lli++;
  193. if (addr_inc)
  194. addr += size_seg;
  195. } while (size_rest);
  196. return lli;
  197. err:
  198. return NULL;
  199. }
  200. int d40_phy_sg_to_lli(struct scatterlist *sg,
  201. int sg_len,
  202. dma_addr_t target,
  203. struct d40_phy_lli *lli_sg,
  204. dma_addr_t lli_phys,
  205. u32 reg_cfg,
  206. struct stedma40_half_channel_info *info,
  207. struct stedma40_half_channel_info *otherinfo,
  208. unsigned long flags)
  209. {
  210. int total_size = 0;
  211. int i;
  212. struct scatterlist *current_sg = sg;
  213. struct d40_phy_lli *lli = lli_sg;
  214. dma_addr_t l_phys = lli_phys;
  215. if (!target)
  216. flags |= LLI_ADDR_INC;
  217. for_each_sg(sg, current_sg, sg_len, i) {
  218. dma_addr_t sg_addr = sg_dma_address(current_sg);
  219. unsigned int len = sg_dma_len(current_sg);
  220. dma_addr_t dst = target ?: sg_addr;
  221. total_size += sg_dma_len(current_sg);
  222. if (i == sg_len - 1)
  223. flags |= LLI_TERM_INT | LLI_LAST_LINK;
  224. l_phys = ALIGN(lli_phys + (lli - lli_sg) *
  225. sizeof(struct d40_phy_lli), D40_LLI_ALIGN);
  226. lli = d40_phy_buf_to_lli(lli, dst, len, l_phys, lli_phys,
  227. reg_cfg, info, otherinfo, flags);
  228. if (lli == NULL)
  229. return -EINVAL;
  230. }
  231. return total_size;
  232. }
  233. /* DMA logical lli operations */
  234. static void d40_log_lli_link(struct d40_log_lli *lli_dst,
  235. struct d40_log_lli *lli_src,
  236. int next, unsigned int flags)
  237. {
  238. bool interrupt = flags & LLI_TERM_INT;
  239. u32 slos = 0;
  240. u32 dlos = 0;
  241. if (next != -EINVAL) {
  242. slos = next * 2;
  243. dlos = next * 2 + 1;
  244. }
  245. if (interrupt) {
  246. lli_dst->lcsp13 |= D40_MEM_LCSP1_SCFG_TIM_MASK;
  247. lli_dst->lcsp13 |= D40_MEM_LCSP3_DTCP_MASK;
  248. }
  249. lli_src->lcsp13 = (lli_src->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  250. (slos << D40_MEM_LCSP1_SLOS_POS);
  251. lli_dst->lcsp13 = (lli_dst->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  252. (dlos << D40_MEM_LCSP1_SLOS_POS);
  253. }
  254. void d40_log_lli_lcpa_write(struct d40_log_lli_full *lcpa,
  255. struct d40_log_lli *lli_dst,
  256. struct d40_log_lli *lli_src,
  257. int next, unsigned int flags)
  258. {
  259. d40_log_lli_link(lli_dst, lli_src, next, flags);
  260. writel_relaxed(lli_src->lcsp02, &lcpa[0].lcsp0);
  261. writel_relaxed(lli_src->lcsp13, &lcpa[0].lcsp1);
  262. writel_relaxed(lli_dst->lcsp02, &lcpa[0].lcsp2);
  263. writel_relaxed(lli_dst->lcsp13, &lcpa[0].lcsp3);
  264. }
  265. void d40_log_lli_lcla_write(struct d40_log_lli *lcla,
  266. struct d40_log_lli *lli_dst,
  267. struct d40_log_lli *lli_src,
  268. int next, unsigned int flags)
  269. {
  270. d40_log_lli_link(lli_dst, lli_src, next, flags);
  271. writel_relaxed(lli_src->lcsp02, &lcla[0].lcsp02);
  272. writel_relaxed(lli_src->lcsp13, &lcla[0].lcsp13);
  273. writel_relaxed(lli_dst->lcsp02, &lcla[1].lcsp02);
  274. writel_relaxed(lli_dst->lcsp13, &lcla[1].lcsp13);
  275. }
  276. static void d40_log_fill_lli(struct d40_log_lli *lli,
  277. dma_addr_t data, u32 data_size,
  278. u32 reg_cfg,
  279. u32 data_width,
  280. unsigned int flags)
  281. {
  282. bool addr_inc = flags & LLI_ADDR_INC;
  283. lli->lcsp13 = reg_cfg;
  284. /* The number of elements to transfer */
  285. lli->lcsp02 = ((data_size >> data_width) <<
  286. D40_MEM_LCSP0_ECNT_POS) & D40_MEM_LCSP0_ECNT_MASK;
  287. BUG_ON((data_size >> data_width) > STEDMA40_MAX_SEG_SIZE);
  288. /* 16 LSBs address of the current element */
  289. lli->lcsp02 |= data & D40_MEM_LCSP0_SPTR_MASK;
  290. /* 16 MSBs address of the current element */
  291. lli->lcsp13 |= data & D40_MEM_LCSP1_SPTR_MASK;
  292. if (addr_inc)
  293. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_INCR_MASK;
  294. }
  295. static struct d40_log_lli *d40_log_buf_to_lli(struct d40_log_lli *lli_sg,
  296. dma_addr_t addr,
  297. int size,
  298. u32 lcsp13, /* src or dst*/
  299. u32 data_width1,
  300. u32 data_width2,
  301. unsigned int flags)
  302. {
  303. bool addr_inc = flags & LLI_ADDR_INC;
  304. struct d40_log_lli *lli = lli_sg;
  305. int size_rest = size;
  306. int size_seg = 0;
  307. do {
  308. size_seg = d40_seg_size(size_rest, data_width1, data_width2);
  309. size_rest -= size_seg;
  310. d40_log_fill_lli(lli,
  311. addr,
  312. size_seg,
  313. lcsp13, data_width1,
  314. flags);
  315. if (addr_inc)
  316. addr += size_seg;
  317. lli++;
  318. } while (size_rest);
  319. return lli;
  320. }
  321. int d40_log_sg_to_lli(struct scatterlist *sg,
  322. int sg_len,
  323. dma_addr_t dev_addr,
  324. struct d40_log_lli *lli_sg,
  325. u32 lcsp13, /* src or dst*/
  326. u32 data_width1, u32 data_width2)
  327. {
  328. int total_size = 0;
  329. struct scatterlist *current_sg = sg;
  330. int i;
  331. struct d40_log_lli *lli = lli_sg;
  332. unsigned long flags = 0;
  333. if (!dev_addr)
  334. flags |= LLI_ADDR_INC;
  335. for_each_sg(sg, current_sg, sg_len, i) {
  336. dma_addr_t sg_addr = sg_dma_address(current_sg);
  337. unsigned int len = sg_dma_len(current_sg);
  338. dma_addr_t addr = dev_addr ?: sg_addr;
  339. total_size += sg_dma_len(current_sg);
  340. lli = d40_log_buf_to_lli(lli, addr, len,
  341. lcsp13,
  342. data_width1,
  343. data_width2,
  344. flags);
  345. }
  346. return total_size;
  347. }