0020-dmaengine-bcm2835-Add-slave-dma-support.patch 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. From d19a0acbd44ad6cd7b6deb75c48f610bbbc44c94 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= <noralf@tronnes.org>
  3. Date: Thu, 9 Apr 2015 12:34:11 +0200
  4. Subject: [PATCH] dmaengine: bcm2835: Add slave dma support
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Add slave transfer capability to BCM2835 dmaengine driver.
  9. This patch is pulled from the bcm2708-dmaengine driver in the
  10. Raspberry Pi repo. The work was done by Gellert Weisz.
  11. Tested using the bcm2835-mmc driver from the same repo.
  12. Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
  13. ---
  14. drivers/dma/bcm2835-dma.c | 206 ++++++++++++++++++++++++++++++++++++++++++----
  15. 1 file changed, 192 insertions(+), 14 deletions(-)
  16. --- a/drivers/dma/bcm2835-dma.c
  17. +++ b/drivers/dma/bcm2835-dma.c
  18. @@ -1,11 +1,10 @@
  19. /*
  20. * BCM2835 DMA engine support
  21. *
  22. - * This driver only supports cyclic DMA transfers
  23. - * as needed for the I2S module.
  24. - *
  25. * Author: Florian Meier <florian.meier@koalo.de>
  26. * Copyright 2013
  27. + * Gellert Weisz <gellert@raspberrypi.org>
  28. + * Copyright 2013-2014
  29. *
  30. * Based on
  31. * OMAP DMAengine support by Russell King
  32. @@ -95,6 +94,8 @@ struct bcm2835_desc {
  33. size_t size;
  34. };
  35. +#define BCM2835_DMA_WAIT_CYCLES 0 /* Slow down DMA transfers: 0-31 */
  36. +
  37. #define BCM2835_DMA_CS 0x00
  38. #define BCM2835_DMA_ADDR 0x04
  39. #define BCM2835_DMA_SOURCE_AD 0x0c
  40. @@ -111,12 +112,16 @@ struct bcm2835_desc {
  41. #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
  42. #define BCM2835_DMA_INT_EN BIT(0)
  43. +#define BCM2835_DMA_WAIT_RESP BIT(3)
  44. #define BCM2835_DMA_D_INC BIT(4)
  45. +#define BCM2835_DMA_D_WIDTH BIT(5)
  46. #define BCM2835_DMA_D_DREQ BIT(6)
  47. #define BCM2835_DMA_S_INC BIT(8)
  48. +#define BCM2835_DMA_S_WIDTH BIT(9)
  49. #define BCM2835_DMA_S_DREQ BIT(10)
  50. #define BCM2835_DMA_PER_MAP(x) ((x) << 16)
  51. +#define BCM2835_DMA_WAITS(x) (((x) & 0x1f) << 21)
  52. #define BCM2835_DMA_DATA_TYPE_S8 1
  53. #define BCM2835_DMA_DATA_TYPE_S16 2
  54. @@ -130,6 +135,14 @@ struct bcm2835_desc {
  55. #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
  56. #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
  57. +#define MAX_NORMAL_TRANSFER SZ_1G
  58. +/*
  59. + * Max length on a Lite channel is 65535 bytes.
  60. + * DMA handles byte-enables on SDRAM reads and writes even on 128-bit accesses,
  61. + * but byte-enables don't exist on peripheral addresses, so align to 32-bit.
  62. + */
  63. +#define MAX_LITE_TRANSFER (SZ_64K - 4)
  64. +
  65. static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
  66. {
  67. return container_of(d, struct bcm2835_dmadev, ddev);
  68. @@ -226,12 +239,18 @@ static irqreturn_t bcm2835_dma_callback(
  69. d = c->desc;
  70. if (d) {
  71. - /* TODO Only works for cyclic DMA */
  72. - vchan_cyclic_callback(&d->vd);
  73. - }
  74. + if (c->cyclic) {
  75. + vchan_cyclic_callback(&d->vd);
  76. - /* Keep the DMA engine running */
  77. - writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  78. + /* Keep the DMA engine running */
  79. + writel(BCM2835_DMA_ACTIVE,
  80. + c->chan_base + BCM2835_DMA_CS);
  81. +
  82. + } else {
  83. + vchan_cookie_complete(&c->desc->vd);
  84. + bcm2835_dma_start_desc(c);
  85. + }
  86. + }
  87. spin_unlock_irqrestore(&c->vc.lock, flags);
  88. @@ -339,8 +358,6 @@ static void bcm2835_dma_issue_pending(st
  89. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  90. unsigned long flags;
  91. - c->cyclic = true; /* Nothing else is implemented */
  92. -
  93. spin_lock_irqsave(&c->vc.lock, flags);
  94. if (vchan_issue_pending(&c->vc) && !c->desc)
  95. bcm2835_dma_start_desc(c);
  96. @@ -358,7 +375,7 @@ static struct dma_async_tx_descriptor *b
  97. struct bcm2835_desc *d;
  98. dma_addr_t dev_addr;
  99. unsigned int es, sync_type;
  100. - unsigned int frame;
  101. + unsigned int frame, max_size;
  102. int i;
  103. /* Grab configuration */
  104. @@ -393,7 +410,12 @@ static struct dma_async_tx_descriptor *b
  105. d->c = c;
  106. d->dir = direction;
  107. - d->frames = buf_len / period_len;
  108. + if (c->ch >= 8) /* LITE channel */
  109. + max_size = MAX_LITE_TRANSFER;
  110. + else
  111. + max_size = MAX_NORMAL_TRANSFER;
  112. + period_len = min(period_len, max_size);
  113. + d->frames = (buf_len - 1) / (period_len + 1);
  114. d->cb_list = kcalloc(d->frames, sizeof(*d->cb_list), GFP_KERNEL);
  115. if (!d->cb_list) {
  116. @@ -441,17 +463,171 @@ static struct dma_async_tx_descriptor *b
  117. BCM2835_DMA_PER_MAP(c->dreq);
  118. /* Length of a frame */
  119. - control_block->length = period_len;
  120. + if (frame != d->frames - 1)
  121. + control_block->length = period_len;
  122. + else
  123. + control_block->length = buf_len - (d->frames - 1) *
  124. + period_len;
  125. d->size += control_block->length;
  126. /*
  127. * Next block is the next frame.
  128. - * This DMA engine driver currently only supports cyclic DMA.
  129. + * This function is called on cyclic DMA transfers.
  130. * Therefore, wrap around at number of frames.
  131. */
  132. control_block->next = d->cb_list[((frame + 1) % d->frames)].paddr;
  133. }
  134. + c->cyclic = true;
  135. +
  136. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  137. +}
  138. +
  139. +static struct dma_async_tx_descriptor *
  140. +bcm2835_dma_prep_slave_sg(struct dma_chan *chan,
  141. + struct scatterlist *sgl,
  142. + unsigned int sg_len,
  143. + enum dma_transfer_direction direction,
  144. + unsigned long flags, void *context)
  145. +{
  146. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  147. + enum dma_slave_buswidth dev_width;
  148. + struct bcm2835_desc *d;
  149. + dma_addr_t dev_addr;
  150. + struct scatterlist *sgent;
  151. + unsigned int i, sync_type, split_cnt, max_size;
  152. +
  153. + if (!is_slave_direction(direction)) {
  154. + dev_err(chan->device->dev, "direction not supported\n");
  155. + return NULL;
  156. + }
  157. +
  158. + if (direction == DMA_DEV_TO_MEM) {
  159. + dev_addr = c->cfg.src_addr;
  160. + dev_width = c->cfg.src_addr_width;
  161. + sync_type = BCM2835_DMA_S_DREQ;
  162. + } else {
  163. + dev_addr = c->cfg.dst_addr;
  164. + dev_width = c->cfg.dst_addr_width;
  165. + sync_type = BCM2835_DMA_D_DREQ;
  166. + }
  167. +
  168. + /* Bus width translates to the element size (ES) */
  169. + switch (dev_width) {
  170. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  171. + break;
  172. + default:
  173. + dev_err(chan->device->dev, "buswidth not supported: %i\n",
  174. + dev_width);
  175. + return NULL;
  176. + }
  177. +
  178. + /* Allocate and setup the descriptor. */
  179. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  180. + if (!d)
  181. + return NULL;
  182. +
  183. + d->dir = direction;
  184. +
  185. + if (c->ch >= 8) /* LITE channel */
  186. + max_size = MAX_LITE_TRANSFER;
  187. + else
  188. + max_size = MAX_NORMAL_TRANSFER;
  189. +
  190. + /*
  191. + * Store the length of the SG list in d->frames
  192. + * taking care to account for splitting up transfers
  193. + * too large for a LITE channel
  194. + */
  195. + d->frames = 0;
  196. + for_each_sg(sgl, sgent, sg_len, i) {
  197. + unsigned int len = sg_dma_len(sgent);
  198. +
  199. + d->frames += len / max_size + 1;
  200. + }
  201. +
  202. + /* Allocate memory for control blocks */
  203. + d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
  204. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  205. + d->control_block_size, &d->control_block_base_phys,
  206. + GFP_NOWAIT);
  207. + if (!d->control_block_base) {
  208. + kfree(d);
  209. + return NULL;
  210. + }
  211. +
  212. + /*
  213. + * Iterate over all SG entries, create a control block
  214. + * for each frame and link them together.
  215. + * Count the number of times an SG entry had to be split
  216. + * as a result of using a LITE channel
  217. + */
  218. + split_cnt = 0;
  219. +
  220. + for_each_sg(sgl, sgent, sg_len, i) {
  221. + unsigned int j;
  222. + dma_addr_t addr = sg_dma_address(sgent);
  223. + unsigned int len = sg_dma_len(sgent);
  224. +
  225. + for (j = 0; j < len; j += max_size) {
  226. + struct bcm2835_dma_cb *control_block =
  227. + &d->control_block_base[i + split_cnt];
  228. +
  229. + /* Setup addresses */
  230. + if (d->dir == DMA_DEV_TO_MEM) {
  231. + control_block->info = BCM2835_DMA_D_INC |
  232. + BCM2835_DMA_D_WIDTH |
  233. + BCM2835_DMA_S_DREQ;
  234. + control_block->src = dev_addr;
  235. + control_block->dst = addr + (dma_addr_t)j;
  236. + } else {
  237. + control_block->info = BCM2835_DMA_S_INC |
  238. + BCM2835_DMA_S_WIDTH |
  239. + BCM2835_DMA_D_DREQ;
  240. + control_block->src = addr + (dma_addr_t)j;
  241. + control_block->dst = dev_addr;
  242. + }
  243. +
  244. + /* Common part */
  245. + control_block->info |=
  246. + BCM2835_DMA_WAITS(BCM2835_DMA_WAIT_CYCLES);
  247. + control_block->info |= BCM2835_DMA_WAIT_RESP;
  248. +
  249. + /* Enable */
  250. + if (i == sg_len - 1 && len - j <= max_size)
  251. + control_block->info |= BCM2835_DMA_INT_EN;
  252. +
  253. + /* Setup synchronization */
  254. + if (sync_type)
  255. + control_block->info |= sync_type;
  256. +
  257. + /* Setup DREQ channel */
  258. + if (c->dreq)
  259. + control_block->info |=
  260. + BCM2835_DMA_PER_MAP(c->dreq);
  261. +
  262. + /* Length of a frame */
  263. + control_block->length = min(len - j, max_size);
  264. + d->size += control_block->length;
  265. +
  266. + if (i < sg_len - 1 || len - j > max_size) {
  267. + /* Next block is the next frame. */
  268. + control_block->next =
  269. + d->control_block_base_phys +
  270. + sizeof(struct bcm2835_dma_cb) *
  271. + (i + split_cnt + 1);
  272. + } else {
  273. + /* Next block is empty. */
  274. + control_block->next = 0;
  275. + }
  276. +
  277. + if (len - j > max_size)
  278. + split_cnt++;
  279. + }
  280. + }
  281. +
  282. + c->cyclic = false;
  283. +
  284. return vchan_tx_prep(&c->vc, &d->vd, flags);
  285. error_cb:
  286. i--;
  287. @@ -620,6 +796,7 @@ static int bcm2835_dma_probe(struct plat
  288. od->ddev.device_tx_status = bcm2835_dma_tx_status;
  289. od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  290. od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  291. + od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  292. od->ddev.device_config = bcm2835_dma_slave_config;
  293. od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
  294. od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  295. @@ -708,4 +885,5 @@ module_platform_driver(bcm2835_dma_drive
  296. MODULE_ALIAS("platform:bcm2835-dma");
  297. MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  298. MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  299. +MODULE_AUTHOR("Gellert Weisz <gellert@raspberrypi.org>");
  300. MODULE_LICENSE("GPL v2");