ste_dma40_ll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 <plat/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. /* Sets up SRC and DST CFG register for both logical and physical channels */
  42. void d40_phy_cfg(struct stedma40_chan_cfg *cfg,
  43. u32 *src_cfg, u32 *dst_cfg, bool is_log)
  44. {
  45. u32 src = 0;
  46. u32 dst = 0;
  47. if (!is_log) {
  48. /* Physical channel */
  49. if ((cfg->dir == STEDMA40_PERIPH_TO_MEM) ||
  50. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  51. /* Set master port to 1 */
  52. src |= 1 << D40_SREG_CFG_MST_POS;
  53. src |= D40_TYPE_TO_EVENT(cfg->src_dev_type);
  54. if (cfg->src_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  55. src |= 1 << D40_SREG_CFG_PHY_TM_POS;
  56. else
  57. src |= 3 << D40_SREG_CFG_PHY_TM_POS;
  58. }
  59. if ((cfg->dir == STEDMA40_MEM_TO_PERIPH) ||
  60. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  61. /* Set master port to 1 */
  62. dst |= 1 << D40_SREG_CFG_MST_POS;
  63. dst |= D40_TYPE_TO_EVENT(cfg->dst_dev_type);
  64. if (cfg->dst_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  65. dst |= 1 << D40_SREG_CFG_PHY_TM_POS;
  66. else
  67. dst |= 3 << D40_SREG_CFG_PHY_TM_POS;
  68. }
  69. /* Interrupt on end of transfer for destination */
  70. dst |= 1 << D40_SREG_CFG_TIM_POS;
  71. /* Generate interrupt on error */
  72. src |= 1 << D40_SREG_CFG_EIM_POS;
  73. dst |= 1 << D40_SREG_CFG_EIM_POS;
  74. /* PSIZE */
  75. if (cfg->src_info.psize != STEDMA40_PSIZE_PHY_1) {
  76. src |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  77. src |= cfg->src_info.psize << D40_SREG_CFG_PSIZE_POS;
  78. }
  79. if (cfg->dst_info.psize != STEDMA40_PSIZE_PHY_1) {
  80. dst |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  81. dst |= cfg->dst_info.psize << D40_SREG_CFG_PSIZE_POS;
  82. }
  83. /* Element size */
  84. src |= cfg->src_info.data_width << D40_SREG_CFG_ESIZE_POS;
  85. dst |= cfg->dst_info.data_width << D40_SREG_CFG_ESIZE_POS;
  86. } else {
  87. /* Logical channel */
  88. dst |= 1 << D40_SREG_CFG_LOG_GIM_POS;
  89. src |= 1 << D40_SREG_CFG_LOG_GIM_POS;
  90. }
  91. if (cfg->high_priority) {
  92. src |= 1 << D40_SREG_CFG_PRI_POS;
  93. dst |= 1 << D40_SREG_CFG_PRI_POS;
  94. }
  95. if (cfg->src_info.big_endian)
  96. src |= 1 << D40_SREG_CFG_LBE_POS;
  97. if (cfg->dst_info.big_endian)
  98. dst |= 1 << D40_SREG_CFG_LBE_POS;
  99. *src_cfg = src;
  100. *dst_cfg = dst;
  101. }
  102. static int d40_phy_fill_lli(struct d40_phy_lli *lli,
  103. dma_addr_t data,
  104. u32 data_size,
  105. int psize,
  106. dma_addr_t next_lli,
  107. u32 reg_cfg,
  108. bool term_int,
  109. u32 data_width,
  110. bool is_device)
  111. {
  112. int num_elems;
  113. if (psize == STEDMA40_PSIZE_PHY_1)
  114. num_elems = 1;
  115. else
  116. num_elems = 2 << psize;
  117. /* Must be aligned */
  118. if (!IS_ALIGNED(data, 0x1 << data_width))
  119. return -EINVAL;
  120. /* Transfer size can't be smaller than (num_elms * elem_size) */
  121. if (data_size < num_elems * (0x1 << data_width))
  122. return -EINVAL;
  123. /* The number of elements. IE now many chunks */
  124. lli->reg_elt = (data_size >> data_width) << D40_SREG_ELEM_PHY_ECNT_POS;
  125. /*
  126. * Distance to next element sized entry.
  127. * Usually the size of the element unless you want gaps.
  128. */
  129. if (!is_device)
  130. lli->reg_elt |= (0x1 << data_width) <<
  131. D40_SREG_ELEM_PHY_EIDX_POS;
  132. /* Where the data is */
  133. lli->reg_ptr = data;
  134. lli->reg_cfg = reg_cfg;
  135. /* If this scatter list entry is the last one, no next link */
  136. if (next_lli == 0)
  137. lli->reg_lnk = 0x1 << D40_SREG_LNK_PHY_TCP_POS;
  138. else
  139. lli->reg_lnk = next_lli;
  140. /* Set/clear interrupt generation on this link item.*/
  141. if (term_int)
  142. lli->reg_cfg |= 0x1 << D40_SREG_CFG_TIM_POS;
  143. else
  144. lli->reg_cfg &= ~(0x1 << D40_SREG_CFG_TIM_POS);
  145. /* Post link */
  146. lli->reg_lnk |= 0 << D40_SREG_LNK_PHY_PRE_POS;
  147. return 0;
  148. }
  149. static int d40_seg_size(int size, int data_width1, int data_width2)
  150. {
  151. u32 max_w = max(data_width1, data_width2);
  152. u32 min_w = min(data_width1, data_width2);
  153. u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
  154. if (seg_max > STEDMA40_MAX_SEG_SIZE)
  155. seg_max -= (1 << max_w);
  156. if (size <= seg_max)
  157. return size;
  158. if (size <= 2 * seg_max)
  159. return ALIGN(size / 2, 1 << max_w);
  160. return seg_max;
  161. }
  162. struct d40_phy_lli *d40_phy_buf_to_lli(struct d40_phy_lli *lli,
  163. dma_addr_t addr,
  164. u32 size,
  165. int psize,
  166. dma_addr_t lli_phys,
  167. u32 reg_cfg,
  168. bool term_int,
  169. u32 data_width1,
  170. u32 data_width2,
  171. bool is_device)
  172. {
  173. int err;
  174. dma_addr_t next = lli_phys;
  175. int size_rest = size;
  176. int size_seg = 0;
  177. do {
  178. size_seg = d40_seg_size(size_rest, data_width1, data_width2);
  179. size_rest -= size_seg;
  180. if (term_int && size_rest == 0)
  181. next = 0;
  182. else
  183. next = ALIGN(next + sizeof(struct d40_phy_lli),
  184. D40_LLI_ALIGN);
  185. err = d40_phy_fill_lli(lli,
  186. addr,
  187. size_seg,
  188. psize,
  189. next,
  190. reg_cfg,
  191. !next,
  192. data_width1,
  193. is_device);
  194. if (err)
  195. goto err;
  196. lli++;
  197. if (!is_device)
  198. addr += size_seg;
  199. } while (size_rest);
  200. return lli;
  201. err:
  202. return NULL;
  203. }
  204. int d40_phy_sg_to_lli(struct scatterlist *sg,
  205. int sg_len,
  206. dma_addr_t target,
  207. struct d40_phy_lli *lli_sg,
  208. dma_addr_t lli_phys,
  209. u32 reg_cfg,
  210. u32 data_width1,
  211. u32 data_width2,
  212. int psize)
  213. {
  214. int total_size = 0;
  215. int i;
  216. struct scatterlist *current_sg = sg;
  217. dma_addr_t dst;
  218. struct d40_phy_lli *lli = lli_sg;
  219. dma_addr_t l_phys = lli_phys;
  220. for_each_sg(sg, current_sg, sg_len, i) {
  221. total_size += sg_dma_len(current_sg);
  222. if (target)
  223. dst = target;
  224. else
  225. dst = sg_phys(current_sg);
  226. l_phys = ALIGN(lli_phys + (lli - lli_sg) *
  227. sizeof(struct d40_phy_lli), D40_LLI_ALIGN);
  228. lli = d40_phy_buf_to_lli(lli,
  229. dst,
  230. sg_dma_len(current_sg),
  231. psize,
  232. l_phys,
  233. reg_cfg,
  234. sg_len - 1 == i,
  235. data_width1,
  236. data_width2,
  237. target == dst);
  238. if (lli == NULL)
  239. return -EINVAL;
  240. }
  241. return total_size;
  242. }
  243. void d40_phy_lli_write(void __iomem *virtbase,
  244. u32 phy_chan_num,
  245. struct d40_phy_lli *lli_dst,
  246. struct d40_phy_lli *lli_src)
  247. {
  248. writel(lli_src->reg_cfg, virtbase + D40_DREG_PCBASE +
  249. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSCFG);
  250. writel(lli_src->reg_elt, virtbase + D40_DREG_PCBASE +
  251. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSELT);
  252. writel(lli_src->reg_ptr, virtbase + D40_DREG_PCBASE +
  253. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSPTR);
  254. writel(lli_src->reg_lnk, virtbase + D40_DREG_PCBASE +
  255. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSLNK);
  256. writel(lli_dst->reg_cfg, virtbase + D40_DREG_PCBASE +
  257. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDCFG);
  258. writel(lli_dst->reg_elt, virtbase + D40_DREG_PCBASE +
  259. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDELT);
  260. writel(lli_dst->reg_ptr, virtbase + D40_DREG_PCBASE +
  261. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDPTR);
  262. writel(lli_dst->reg_lnk, virtbase + D40_DREG_PCBASE +
  263. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDLNK);
  264. }
  265. /* DMA logical lli operations */
  266. static void d40_log_lli_link(struct d40_log_lli *lli_dst,
  267. struct d40_log_lli *lli_src,
  268. int next)
  269. {
  270. u32 slos = 0;
  271. u32 dlos = 0;
  272. if (next != -EINVAL) {
  273. slos = next * 2;
  274. dlos = next * 2 + 1;
  275. } else {
  276. lli_dst->lcsp13 |= D40_MEM_LCSP1_SCFG_TIM_MASK;
  277. lli_dst->lcsp13 |= D40_MEM_LCSP3_DTCP_MASK;
  278. }
  279. lli_src->lcsp13 = (lli_src->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  280. (slos << D40_MEM_LCSP1_SLOS_POS);
  281. lli_dst->lcsp13 = (lli_dst->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  282. (dlos << D40_MEM_LCSP1_SLOS_POS);
  283. }
  284. void d40_log_lli_lcpa_write(struct d40_log_lli_full *lcpa,
  285. struct d40_log_lli *lli_dst,
  286. struct d40_log_lli *lli_src,
  287. int next)
  288. {
  289. d40_log_lli_link(lli_dst, lli_src, next);
  290. writel(lli_src->lcsp02, &lcpa[0].lcsp0);
  291. writel(lli_src->lcsp13, &lcpa[0].lcsp1);
  292. writel(lli_dst->lcsp02, &lcpa[0].lcsp2);
  293. writel(lli_dst->lcsp13, &lcpa[0].lcsp3);
  294. }
  295. void d40_log_lli_lcla_write(struct d40_log_lli *lcla,
  296. struct d40_log_lli *lli_dst,
  297. struct d40_log_lli *lli_src,
  298. int next)
  299. {
  300. d40_log_lli_link(lli_dst, lli_src, next);
  301. writel(lli_src->lcsp02, &lcla[0].lcsp02);
  302. writel(lli_src->lcsp13, &lcla[0].lcsp13);
  303. writel(lli_dst->lcsp02, &lcla[1].lcsp02);
  304. writel(lli_dst->lcsp13, &lcla[1].lcsp13);
  305. }
  306. static void d40_log_fill_lli(struct d40_log_lli *lli,
  307. dma_addr_t data, u32 data_size,
  308. u32 reg_cfg,
  309. u32 data_width,
  310. bool addr_inc)
  311. {
  312. lli->lcsp13 = reg_cfg;
  313. /* The number of elements to transfer */
  314. lli->lcsp02 = ((data_size >> data_width) <<
  315. D40_MEM_LCSP0_ECNT_POS) & D40_MEM_LCSP0_ECNT_MASK;
  316. BUG_ON((data_size >> data_width) > STEDMA40_MAX_SEG_SIZE);
  317. /* 16 LSBs address of the current element */
  318. lli->lcsp02 |= data & D40_MEM_LCSP0_SPTR_MASK;
  319. /* 16 MSBs address of the current element */
  320. lli->lcsp13 |= data & D40_MEM_LCSP1_SPTR_MASK;
  321. if (addr_inc)
  322. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_INCR_MASK;
  323. }
  324. int d40_log_sg_to_dev(struct scatterlist *sg,
  325. int sg_len,
  326. struct d40_log_lli_bidir *lli,
  327. struct d40_def_lcsp *lcsp,
  328. u32 src_data_width,
  329. u32 dst_data_width,
  330. enum dma_data_direction direction,
  331. dma_addr_t dev_addr)
  332. {
  333. int total_size = 0;
  334. struct scatterlist *current_sg = sg;
  335. int i;
  336. struct d40_log_lli *lli_src = lli->src;
  337. struct d40_log_lli *lli_dst = lli->dst;
  338. for_each_sg(sg, current_sg, sg_len, i) {
  339. total_size += sg_dma_len(current_sg);
  340. if (direction == DMA_TO_DEVICE) {
  341. lli_src =
  342. d40_log_buf_to_lli(lli_src,
  343. sg_phys(current_sg),
  344. sg_dma_len(current_sg),
  345. lcsp->lcsp1, src_data_width,
  346. dst_data_width,
  347. true);
  348. lli_dst =
  349. d40_log_buf_to_lli(lli_dst,
  350. dev_addr,
  351. sg_dma_len(current_sg),
  352. lcsp->lcsp3, dst_data_width,
  353. src_data_width,
  354. false);
  355. } else {
  356. lli_dst =
  357. d40_log_buf_to_lli(lli_dst,
  358. sg_phys(current_sg),
  359. sg_dma_len(current_sg),
  360. lcsp->lcsp3, dst_data_width,
  361. src_data_width,
  362. true);
  363. lli_src =
  364. d40_log_buf_to_lli(lli_src,
  365. dev_addr,
  366. sg_dma_len(current_sg),
  367. lcsp->lcsp1, src_data_width,
  368. dst_data_width,
  369. false);
  370. }
  371. }
  372. return total_size;
  373. }
  374. struct d40_log_lli *d40_log_buf_to_lli(struct d40_log_lli *lli_sg,
  375. dma_addr_t addr,
  376. int size,
  377. u32 lcsp13, /* src or dst*/
  378. u32 data_width1,
  379. u32 data_width2,
  380. bool addr_inc)
  381. {
  382. struct d40_log_lli *lli = lli_sg;
  383. int size_rest = size;
  384. int size_seg = 0;
  385. do {
  386. size_seg = d40_seg_size(size_rest, data_width1, data_width2);
  387. size_rest -= size_seg;
  388. d40_log_fill_lli(lli,
  389. addr,
  390. size_seg,
  391. lcsp13, data_width1,
  392. addr_inc);
  393. if (addr_inc)
  394. addr += size_seg;
  395. lli++;
  396. } while (size_rest);
  397. return lli;
  398. }
  399. int d40_log_sg_to_lli(struct scatterlist *sg,
  400. int sg_len,
  401. struct d40_log_lli *lli_sg,
  402. u32 lcsp13, /* src or dst*/
  403. u32 data_width1, u32 data_width2)
  404. {
  405. int total_size = 0;
  406. struct scatterlist *current_sg = sg;
  407. int i;
  408. struct d40_log_lli *lli = lli_sg;
  409. for_each_sg(sg, current_sg, sg_len, i) {
  410. total_size += sg_dma_len(current_sg);
  411. lli = d40_log_buf_to_lli(lli,
  412. sg_phys(current_sg),
  413. sg_dma_len(current_sg),
  414. lcsp13,
  415. data_width1, data_width2, true);
  416. }
  417. return total_size;
  418. }