ste_dma40_ll.c 11 KB

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