bcm2835-dma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * BCM2835 DMA engine support
  3. *
  4. * This driver only supports cyclic DMA transfers
  5. * as needed for the I2S module.
  6. *
  7. * Author: Florian Meier <florian.meier@koalo.de>
  8. * Copyright 2013
  9. *
  10. * Based on
  11. * OMAP DMAengine support by Russell King
  12. *
  13. * BCM2708 DMA Driver
  14. * Copyright (C) 2010 Broadcom
  15. *
  16. * Raspberry Pi PCM I2S ALSA Driver
  17. * Copyright (c) by Phil Poole 2013
  18. *
  19. * MARVELL MMP Peripheral DMA Driver
  20. * Copyright 2012 Marvell International Ltd.
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. */
  32. #include <linux/dmaengine.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/dmapool.h>
  35. #include <linux/err.h>
  36. #include <linux/init.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/list.h>
  39. #include <linux/module.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/slab.h>
  42. #include <linux/io.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/of.h>
  45. #include <linux/of_dma.h>
  46. #include "virt-dma.h"
  47. #define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14
  48. #define BCM2835_DMA_CHAN_NAME_SIZE 8
  49. struct bcm2835_dmadev {
  50. struct dma_device ddev;
  51. spinlock_t lock;
  52. void __iomem *base;
  53. struct device_dma_parameters dma_parms;
  54. };
  55. struct bcm2835_dma_cb {
  56. uint32_t info;
  57. uint32_t src;
  58. uint32_t dst;
  59. uint32_t length;
  60. uint32_t stride;
  61. uint32_t next;
  62. uint32_t pad[2];
  63. };
  64. struct bcm2835_cb_entry {
  65. struct bcm2835_dma_cb *cb;
  66. dma_addr_t paddr;
  67. };
  68. struct bcm2835_chan {
  69. struct virt_dma_chan vc;
  70. struct list_head node;
  71. struct dma_slave_config cfg;
  72. unsigned int dreq;
  73. int ch;
  74. struct bcm2835_desc *desc;
  75. struct dma_pool *cb_pool;
  76. void __iomem *chan_base;
  77. int irq_number;
  78. unsigned int irq_flags;
  79. bool is_lite_channel;
  80. };
  81. struct bcm2835_desc {
  82. struct bcm2835_chan *c;
  83. struct virt_dma_desc vd;
  84. enum dma_transfer_direction dir;
  85. unsigned int frames;
  86. size_t size;
  87. bool cyclic;
  88. struct bcm2835_cb_entry cb_list[];
  89. };
  90. #define BCM2835_DMA_CS 0x00
  91. #define BCM2835_DMA_ADDR 0x04
  92. #define BCM2835_DMA_TI 0x08
  93. #define BCM2835_DMA_SOURCE_AD 0x0c
  94. #define BCM2835_DMA_DEST_AD 0x10
  95. #define BCM2835_DMA_LEN 0x14
  96. #define BCM2835_DMA_STRIDE 0x18
  97. #define BCM2835_DMA_NEXTCB 0x1c
  98. #define BCM2835_DMA_DEBUG 0x20
  99. /* DMA CS Control and Status bits */
  100. #define BCM2835_DMA_ACTIVE BIT(0) /* activate the DMA */
  101. #define BCM2835_DMA_END BIT(1) /* current CB has ended */
  102. #define BCM2835_DMA_INT BIT(2) /* interrupt status */
  103. #define BCM2835_DMA_DREQ BIT(3) /* DREQ state */
  104. #define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
  105. #define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
  106. #define BCM2835_DMA_WAITING_FOR_WRITES BIT(6) /* waiting for last
  107. * AXI-write to ack
  108. */
  109. #define BCM2835_DMA_ERR BIT(8)
  110. #define BCM2835_DMA_PRIORITY(x) ((x & 15) << 16) /* AXI priority */
  111. #define BCM2835_DMA_PANIC_PRIORITY(x) ((x & 15) << 20) /* panic priority */
  112. /* current value of TI.BCM2835_DMA_WAIT_RESP */
  113. #define BCM2835_DMA_WAIT_FOR_WRITES BIT(28)
  114. #define BCM2835_DMA_DIS_DEBUG BIT(29) /* disable debug pause signal */
  115. #define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
  116. #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
  117. /* Transfer information bits - also bcm2835_cb.info field */
  118. #define BCM2835_DMA_INT_EN BIT(0)
  119. #define BCM2835_DMA_TDMODE BIT(1) /* 2D-Mode */
  120. #define BCM2835_DMA_WAIT_RESP BIT(3) /* wait for AXI-write to be acked */
  121. #define BCM2835_DMA_D_INC BIT(4)
  122. #define BCM2835_DMA_D_WIDTH BIT(5) /* 128bit writes if set */
  123. #define BCM2835_DMA_D_DREQ BIT(6) /* enable DREQ for destination */
  124. #define BCM2835_DMA_D_IGNORE BIT(7) /* ignore destination writes */
  125. #define BCM2835_DMA_S_INC BIT(8)
  126. #define BCM2835_DMA_S_WIDTH BIT(9) /* 128bit writes if set */
  127. #define BCM2835_DMA_S_DREQ BIT(10) /* enable SREQ for source */
  128. #define BCM2835_DMA_S_IGNORE BIT(11) /* ignore source reads - read 0 */
  129. #define BCM2835_DMA_BURST_LENGTH(x) ((x & 15) << 12)
  130. #define BCM2835_DMA_PER_MAP(x) ((x & 31) << 16) /* REQ source */
  131. #define BCM2835_DMA_WAIT(x) ((x & 31) << 21) /* add DMA-wait cycles */
  132. #define BCM2835_DMA_NO_WIDE_BURSTS BIT(26) /* no 2 beat write bursts */
  133. /* debug register bits */
  134. #define BCM2835_DMA_DEBUG_LAST_NOT_SET_ERR BIT(0)
  135. #define BCM2835_DMA_DEBUG_FIFO_ERR BIT(1)
  136. #define BCM2835_DMA_DEBUG_READ_ERR BIT(2)
  137. #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_SHIFT 4
  138. #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_BITS 4
  139. #define BCM2835_DMA_DEBUG_ID_SHIFT 16
  140. #define BCM2835_DMA_DEBUG_ID_BITS 9
  141. #define BCM2835_DMA_DEBUG_STATE_SHIFT 16
  142. #define BCM2835_DMA_DEBUG_STATE_BITS 9
  143. #define BCM2835_DMA_DEBUG_VERSION_SHIFT 25
  144. #define BCM2835_DMA_DEBUG_VERSION_BITS 3
  145. #define BCM2835_DMA_DEBUG_LITE BIT(28)
  146. /* shared registers for all dma channels */
  147. #define BCM2835_DMA_INT_STATUS 0xfe0
  148. #define BCM2835_DMA_ENABLE 0xff0
  149. #define BCM2835_DMA_DATA_TYPE_S8 1
  150. #define BCM2835_DMA_DATA_TYPE_S16 2
  151. #define BCM2835_DMA_DATA_TYPE_S32 4
  152. #define BCM2835_DMA_DATA_TYPE_S128 16
  153. /* Valid only for channels 0 - 14, 15 has its own base address */
  154. #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
  155. #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
  156. /* the max dma length for different channels */
  157. #define MAX_DMA_LEN SZ_1G
  158. #define MAX_LITE_DMA_LEN (SZ_64K - 4)
  159. static inline size_t bcm2835_dma_max_frame_length(struct bcm2835_chan *c)
  160. {
  161. /* lite and normal channels have different max frame length */
  162. return c->is_lite_channel ? MAX_LITE_DMA_LEN : MAX_DMA_LEN;
  163. }
  164. /* how many frames of max_len size do we need to transfer len bytes */
  165. static inline size_t bcm2835_dma_frames_for_length(size_t len,
  166. size_t max_len)
  167. {
  168. return DIV_ROUND_UP(len, max_len);
  169. }
  170. static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
  171. {
  172. return container_of(d, struct bcm2835_dmadev, ddev);
  173. }
  174. static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
  175. {
  176. return container_of(c, struct bcm2835_chan, vc.chan);
  177. }
  178. static inline struct bcm2835_desc *to_bcm2835_dma_desc(
  179. struct dma_async_tx_descriptor *t)
  180. {
  181. return container_of(t, struct bcm2835_desc, vd.tx);
  182. }
  183. static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc)
  184. {
  185. size_t i;
  186. for (i = 0; i < desc->frames; i++)
  187. dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
  188. desc->cb_list[i].paddr);
  189. kfree(desc);
  190. }
  191. static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
  192. {
  193. bcm2835_dma_free_cb_chain(
  194. container_of(vd, struct bcm2835_desc, vd));
  195. }
  196. static void bcm2835_dma_create_cb_set_length(
  197. struct bcm2835_chan *chan,
  198. struct bcm2835_dma_cb *control_block,
  199. size_t len,
  200. size_t period_len,
  201. size_t *total_len,
  202. u32 finalextrainfo)
  203. {
  204. size_t max_len = bcm2835_dma_max_frame_length(chan);
  205. /* set the length taking lite-channel limitations into account */
  206. control_block->length = min_t(u32, len, max_len);
  207. /* finished if we have no period_length */
  208. if (!period_len)
  209. return;
  210. /*
  211. * period_len means: that we need to generate
  212. * transfers that are terminating at every
  213. * multiple of period_len - this is typically
  214. * used to set the interrupt flag in info
  215. * which is required during cyclic transfers
  216. */
  217. /* have we filled in period_length yet? */
  218. if (*total_len + control_block->length < period_len) {
  219. /* update number of bytes in this period so far */
  220. *total_len += control_block->length;
  221. return;
  222. }
  223. /* calculate the length that remains to reach period_length */
  224. control_block->length = period_len - *total_len;
  225. /* reset total_length for next period */
  226. *total_len = 0;
  227. /* add extrainfo bits in info */
  228. control_block->info |= finalextrainfo;
  229. }
  230. static inline size_t bcm2835_dma_count_frames_for_sg(
  231. struct bcm2835_chan *c,
  232. struct scatterlist *sgl,
  233. unsigned int sg_len)
  234. {
  235. size_t frames = 0;
  236. struct scatterlist *sgent;
  237. unsigned int i;
  238. size_t plength = bcm2835_dma_max_frame_length(c);
  239. for_each_sg(sgl, sgent, sg_len, i)
  240. frames += bcm2835_dma_frames_for_length(
  241. sg_dma_len(sgent), plength);
  242. return frames;
  243. }
  244. /**
  245. * bcm2835_dma_create_cb_chain - create a control block and fills data in
  246. *
  247. * @chan: the @dma_chan for which we run this
  248. * @direction: the direction in which we transfer
  249. * @cyclic: it is a cyclic transfer
  250. * @info: the default info bits to apply per controlblock
  251. * @frames: number of controlblocks to allocate
  252. * @src: the src address to assign (if the S_INC bit is set
  253. * in @info, then it gets incremented)
  254. * @dst: the dst address to assign (if the D_INC bit is set
  255. * in @info, then it gets incremented)
  256. * @buf_len: the full buffer length (may also be 0)
  257. * @period_len: the period length when to apply @finalextrainfo
  258. * in addition to the last transfer
  259. * this will also break some control-blocks early
  260. * @finalextrainfo: additional bits in last controlblock
  261. * (or when period_len is reached in case of cyclic)
  262. * @gfp: the GFP flag to use for allocation
  263. */
  264. static struct bcm2835_desc *bcm2835_dma_create_cb_chain(
  265. struct dma_chan *chan, enum dma_transfer_direction direction,
  266. bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
  267. dma_addr_t src, dma_addr_t dst, size_t buf_len,
  268. size_t period_len, gfp_t gfp)
  269. {
  270. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  271. size_t len = buf_len, total_len;
  272. size_t frame;
  273. struct bcm2835_desc *d;
  274. struct bcm2835_cb_entry *cb_entry;
  275. struct bcm2835_dma_cb *control_block;
  276. if (!frames)
  277. return NULL;
  278. /* allocate and setup the descriptor. */
  279. d = kzalloc(sizeof(*d) + frames * sizeof(struct bcm2835_cb_entry),
  280. gfp);
  281. if (!d)
  282. return NULL;
  283. d->c = c;
  284. d->dir = direction;
  285. d->cyclic = cyclic;
  286. /*
  287. * Iterate over all frames, create a control block
  288. * for each frame and link them together.
  289. */
  290. for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) {
  291. cb_entry = &d->cb_list[frame];
  292. cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp,
  293. &cb_entry->paddr);
  294. if (!cb_entry->cb)
  295. goto error_cb;
  296. /* fill in the control block */
  297. control_block = cb_entry->cb;
  298. control_block->info = info;
  299. control_block->src = src;
  300. control_block->dst = dst;
  301. control_block->stride = 0;
  302. control_block->next = 0;
  303. /* set up length in control_block if requested */
  304. if (buf_len) {
  305. /* calculate length honoring period_length */
  306. bcm2835_dma_create_cb_set_length(
  307. c, control_block,
  308. len, period_len, &total_len,
  309. cyclic ? finalextrainfo : 0);
  310. /* calculate new remaining length */
  311. len -= control_block->length;
  312. }
  313. /* link this the last controlblock */
  314. if (frame)
  315. d->cb_list[frame - 1].cb->next = cb_entry->paddr;
  316. /* update src and dst and length */
  317. if (src && (info & BCM2835_DMA_S_INC))
  318. src += control_block->length;
  319. if (dst && (info & BCM2835_DMA_D_INC))
  320. dst += control_block->length;
  321. /* Length of total transfer */
  322. d->size += control_block->length;
  323. }
  324. /* the last frame requires extra flags */
  325. d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
  326. /* detect a size missmatch */
  327. if (buf_len && (d->size != buf_len))
  328. goto error_cb;
  329. return d;
  330. error_cb:
  331. bcm2835_dma_free_cb_chain(d);
  332. return NULL;
  333. }
  334. static void bcm2835_dma_fill_cb_chain_with_sg(
  335. struct dma_chan *chan,
  336. enum dma_transfer_direction direction,
  337. struct bcm2835_cb_entry *cb,
  338. struct scatterlist *sgl,
  339. unsigned int sg_len)
  340. {
  341. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  342. size_t len, max_len;
  343. unsigned int i;
  344. dma_addr_t addr;
  345. struct scatterlist *sgent;
  346. max_len = bcm2835_dma_max_frame_length(c);
  347. for_each_sg(sgl, sgent, sg_len, i) {
  348. for (addr = sg_dma_address(sgent), len = sg_dma_len(sgent);
  349. len > 0;
  350. addr += cb->cb->length, len -= cb->cb->length, cb++) {
  351. if (direction == DMA_DEV_TO_MEM)
  352. cb->cb->dst = addr;
  353. else
  354. cb->cb->src = addr;
  355. cb->cb->length = min(len, max_len);
  356. }
  357. }
  358. }
  359. static int bcm2835_dma_abort(void __iomem *chan_base)
  360. {
  361. unsigned long cs;
  362. long int timeout = 10000;
  363. cs = readl(chan_base + BCM2835_DMA_CS);
  364. if (!(cs & BCM2835_DMA_ACTIVE))
  365. return 0;
  366. /* Write 0 to the active bit - Pause the DMA */
  367. writel(0, chan_base + BCM2835_DMA_CS);
  368. /* Wait for any current AXI transfer to complete */
  369. while ((cs & BCM2835_DMA_ISPAUSED) && --timeout) {
  370. cpu_relax();
  371. cs = readl(chan_base + BCM2835_DMA_CS);
  372. }
  373. /* We'll un-pause when we set of our next DMA */
  374. if (!timeout)
  375. return -ETIMEDOUT;
  376. if (!(cs & BCM2835_DMA_ACTIVE))
  377. return 0;
  378. /* Terminate the control block chain */
  379. writel(0, chan_base + BCM2835_DMA_NEXTCB);
  380. /* Abort the whole DMA */
  381. writel(BCM2835_DMA_ABORT | BCM2835_DMA_ACTIVE,
  382. chan_base + BCM2835_DMA_CS);
  383. return 0;
  384. }
  385. static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
  386. {
  387. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  388. struct bcm2835_desc *d;
  389. if (!vd) {
  390. c->desc = NULL;
  391. return;
  392. }
  393. list_del(&vd->node);
  394. c->desc = d = to_bcm2835_dma_desc(&vd->tx);
  395. writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
  396. writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  397. }
  398. static irqreturn_t bcm2835_dma_callback(int irq, void *data)
  399. {
  400. struct bcm2835_chan *c = data;
  401. struct bcm2835_desc *d;
  402. unsigned long flags;
  403. /* check the shared interrupt */
  404. if (c->irq_flags & IRQF_SHARED) {
  405. /* check if the interrupt is enabled */
  406. flags = readl(c->chan_base + BCM2835_DMA_CS);
  407. /* if not set then we are not the reason for the irq */
  408. if (!(flags & BCM2835_DMA_INT))
  409. return IRQ_NONE;
  410. }
  411. spin_lock_irqsave(&c->vc.lock, flags);
  412. /* Acknowledge interrupt */
  413. writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
  414. d = c->desc;
  415. if (d) {
  416. if (d->cyclic) {
  417. /* call the cyclic callback */
  418. vchan_cyclic_callback(&d->vd);
  419. /* Keep the DMA engine running */
  420. writel(BCM2835_DMA_ACTIVE,
  421. c->chan_base + BCM2835_DMA_CS);
  422. } else {
  423. vchan_cookie_complete(&c->desc->vd);
  424. bcm2835_dma_start_desc(c);
  425. }
  426. }
  427. spin_unlock_irqrestore(&c->vc.lock, flags);
  428. return IRQ_HANDLED;
  429. }
  430. static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
  431. {
  432. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  433. struct device *dev = c->vc.chan.device->dev;
  434. dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
  435. c->cb_pool = dma_pool_create(dev_name(dev), dev,
  436. sizeof(struct bcm2835_dma_cb), 0, 0);
  437. if (!c->cb_pool) {
  438. dev_err(dev, "unable to allocate descriptor pool\n");
  439. return -ENOMEM;
  440. }
  441. return request_irq(c->irq_number, bcm2835_dma_callback,
  442. c->irq_flags, "DMA IRQ", c);
  443. }
  444. static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
  445. {
  446. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  447. vchan_free_chan_resources(&c->vc);
  448. free_irq(c->irq_number, c);
  449. dma_pool_destroy(c->cb_pool);
  450. dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  451. }
  452. static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
  453. {
  454. return d->size;
  455. }
  456. static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
  457. {
  458. unsigned int i;
  459. size_t size;
  460. for (size = i = 0; i < d->frames; i++) {
  461. struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
  462. size_t this_size = control_block->length;
  463. dma_addr_t dma;
  464. if (d->dir == DMA_DEV_TO_MEM)
  465. dma = control_block->dst;
  466. else
  467. dma = control_block->src;
  468. if (size)
  469. size += this_size;
  470. else if (addr >= dma && addr < dma + this_size)
  471. size += dma + this_size - addr;
  472. }
  473. return size;
  474. }
  475. static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
  476. dma_cookie_t cookie, struct dma_tx_state *txstate)
  477. {
  478. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  479. struct virt_dma_desc *vd;
  480. enum dma_status ret;
  481. unsigned long flags;
  482. ret = dma_cookie_status(chan, cookie, txstate);
  483. if (ret == DMA_COMPLETE || !txstate)
  484. return ret;
  485. spin_lock_irqsave(&c->vc.lock, flags);
  486. vd = vchan_find_desc(&c->vc, cookie);
  487. if (vd) {
  488. txstate->residue =
  489. bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
  490. } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  491. struct bcm2835_desc *d = c->desc;
  492. dma_addr_t pos;
  493. if (d->dir == DMA_MEM_TO_DEV)
  494. pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
  495. else if (d->dir == DMA_DEV_TO_MEM)
  496. pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
  497. else
  498. pos = 0;
  499. txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
  500. } else {
  501. txstate->residue = 0;
  502. }
  503. spin_unlock_irqrestore(&c->vc.lock, flags);
  504. return ret;
  505. }
  506. static void bcm2835_dma_issue_pending(struct dma_chan *chan)
  507. {
  508. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  509. unsigned long flags;
  510. spin_lock_irqsave(&c->vc.lock, flags);
  511. if (vchan_issue_pending(&c->vc) && !c->desc)
  512. bcm2835_dma_start_desc(c);
  513. spin_unlock_irqrestore(&c->vc.lock, flags);
  514. }
  515. static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy(
  516. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  517. size_t len, unsigned long flags)
  518. {
  519. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  520. struct bcm2835_desc *d;
  521. u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC;
  522. u32 extra = BCM2835_DMA_INT_EN | BCM2835_DMA_WAIT_RESP;
  523. size_t max_len = bcm2835_dma_max_frame_length(c);
  524. size_t frames;
  525. /* if src, dst or len is not given return with an error */
  526. if (!src || !dst || !len)
  527. return NULL;
  528. /* calculate number of frames */
  529. frames = bcm2835_dma_frames_for_length(len, max_len);
  530. /* allocate the CB chain - this also fills in the pointers */
  531. d = bcm2835_dma_create_cb_chain(chan, DMA_MEM_TO_MEM, false,
  532. info, extra, frames,
  533. src, dst, len, 0, GFP_KERNEL);
  534. if (!d)
  535. return NULL;
  536. return vchan_tx_prep(&c->vc, &d->vd, flags);
  537. }
  538. static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
  539. struct dma_chan *chan,
  540. struct scatterlist *sgl, unsigned int sg_len,
  541. enum dma_transfer_direction direction,
  542. unsigned long flags, void *context)
  543. {
  544. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  545. struct bcm2835_desc *d;
  546. dma_addr_t src = 0, dst = 0;
  547. u32 info = BCM2835_DMA_WAIT_RESP;
  548. u32 extra = BCM2835_DMA_INT_EN;
  549. size_t frames;
  550. if (!is_slave_direction(direction)) {
  551. dev_err(chan->device->dev,
  552. "%s: bad direction?\n", __func__);
  553. return NULL;
  554. }
  555. if (c->dreq != 0)
  556. info |= BCM2835_DMA_PER_MAP(c->dreq);
  557. if (direction == DMA_DEV_TO_MEM) {
  558. if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  559. return NULL;
  560. src = c->cfg.src_addr;
  561. info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
  562. } else {
  563. if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  564. return NULL;
  565. dst = c->cfg.dst_addr;
  566. info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
  567. }
  568. /* count frames in sg list */
  569. frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
  570. /* allocate the CB chain */
  571. d = bcm2835_dma_create_cb_chain(chan, direction, false,
  572. info, extra,
  573. frames, src, dst, 0, 0,
  574. GFP_KERNEL);
  575. if (!d)
  576. return NULL;
  577. /* fill in frames with scatterlist pointers */
  578. bcm2835_dma_fill_cb_chain_with_sg(chan, direction, d->cb_list,
  579. sgl, sg_len);
  580. return vchan_tx_prep(&c->vc, &d->vd, flags);
  581. }
  582. static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
  583. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  584. size_t period_len, enum dma_transfer_direction direction,
  585. unsigned long flags)
  586. {
  587. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  588. struct bcm2835_desc *d;
  589. dma_addr_t src, dst;
  590. u32 info = BCM2835_DMA_WAIT_RESP;
  591. u32 extra = BCM2835_DMA_INT_EN;
  592. size_t max_len = bcm2835_dma_max_frame_length(c);
  593. size_t frames;
  594. /* Grab configuration */
  595. if (!is_slave_direction(direction)) {
  596. dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  597. return NULL;
  598. }
  599. if (!buf_len) {
  600. dev_err(chan->device->dev,
  601. "%s: bad buffer length (= 0)\n", __func__);
  602. return NULL;
  603. }
  604. /*
  605. * warn if buf_len is not a multiple of period_len - this may leed
  606. * to unexpected latencies for interrupts and thus audiable clicks
  607. */
  608. if (buf_len % period_len)
  609. dev_warn_once(chan->device->dev,
  610. "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
  611. __func__, buf_len, period_len);
  612. /* Setup DREQ channel */
  613. if (c->dreq != 0)
  614. info |= BCM2835_DMA_PER_MAP(c->dreq);
  615. if (direction == DMA_DEV_TO_MEM) {
  616. if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  617. return NULL;
  618. src = c->cfg.src_addr;
  619. dst = buf_addr;
  620. info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
  621. } else {
  622. if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  623. return NULL;
  624. dst = c->cfg.dst_addr;
  625. src = buf_addr;
  626. info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
  627. }
  628. /* calculate number of frames */
  629. frames = /* number of periods */
  630. DIV_ROUND_UP(buf_len, period_len) *
  631. /* number of frames per period */
  632. bcm2835_dma_frames_for_length(period_len, max_len);
  633. /*
  634. * allocate the CB chain
  635. * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
  636. * implementation calls prep_dma_cyclic with interrupts disabled.
  637. */
  638. d = bcm2835_dma_create_cb_chain(chan, direction, true,
  639. info, extra,
  640. frames, src, dst, buf_len,
  641. period_len, GFP_NOWAIT);
  642. if (!d)
  643. return NULL;
  644. /* wrap around into a loop */
  645. d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
  646. return vchan_tx_prep(&c->vc, &d->vd, flags);
  647. }
  648. static int bcm2835_dma_slave_config(struct dma_chan *chan,
  649. struct dma_slave_config *cfg)
  650. {
  651. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  652. if ((cfg->direction == DMA_DEV_TO_MEM &&
  653. cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  654. (cfg->direction == DMA_MEM_TO_DEV &&
  655. cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  656. !is_slave_direction(cfg->direction)) {
  657. return -EINVAL;
  658. }
  659. c->cfg = *cfg;
  660. return 0;
  661. }
  662. static int bcm2835_dma_terminate_all(struct dma_chan *chan)
  663. {
  664. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  665. struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
  666. unsigned long flags;
  667. int timeout = 10000;
  668. LIST_HEAD(head);
  669. spin_lock_irqsave(&c->vc.lock, flags);
  670. /* Prevent this channel being scheduled */
  671. spin_lock(&d->lock);
  672. list_del_init(&c->node);
  673. spin_unlock(&d->lock);
  674. /*
  675. * Stop DMA activity: we assume the callback will not be called
  676. * after bcm_dma_abort() returns (even if it does, it will see
  677. * c->desc is NULL and exit.)
  678. */
  679. if (c->desc) {
  680. bcm2835_dma_desc_free(&c->desc->vd);
  681. c->desc = NULL;
  682. bcm2835_dma_abort(c->chan_base);
  683. /* Wait for stopping */
  684. while (--timeout) {
  685. if (!(readl(c->chan_base + BCM2835_DMA_CS) &
  686. BCM2835_DMA_ACTIVE))
  687. break;
  688. cpu_relax();
  689. }
  690. if (!timeout)
  691. dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  692. }
  693. vchan_get_all_descriptors(&c->vc, &head);
  694. spin_unlock_irqrestore(&c->vc.lock, flags);
  695. vchan_dma_desc_free_list(&c->vc, &head);
  696. return 0;
  697. }
  698. static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
  699. int irq, unsigned int irq_flags)
  700. {
  701. struct bcm2835_chan *c;
  702. c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  703. if (!c)
  704. return -ENOMEM;
  705. c->vc.desc_free = bcm2835_dma_desc_free;
  706. vchan_init(&c->vc, &d->ddev);
  707. INIT_LIST_HEAD(&c->node);
  708. c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  709. c->ch = chan_id;
  710. c->irq_number = irq;
  711. c->irq_flags = irq_flags;
  712. /* check in DEBUG register if this is a LITE channel */
  713. if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
  714. BCM2835_DMA_DEBUG_LITE)
  715. c->is_lite_channel = true;
  716. return 0;
  717. }
  718. static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  719. {
  720. struct bcm2835_chan *c, *next;
  721. list_for_each_entry_safe(c, next, &od->ddev.channels,
  722. vc.chan.device_node) {
  723. list_del(&c->vc.chan.device_node);
  724. tasklet_kill(&c->vc.task);
  725. }
  726. }
  727. static const struct of_device_id bcm2835_dma_of_match[] = {
  728. { .compatible = "brcm,bcm2835-dma", },
  729. {},
  730. };
  731. MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  732. static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  733. struct of_dma *ofdma)
  734. {
  735. struct bcm2835_dmadev *d = ofdma->of_dma_data;
  736. struct dma_chan *chan;
  737. chan = dma_get_any_slave_channel(&d->ddev);
  738. if (!chan)
  739. return NULL;
  740. /* Set DREQ from param */
  741. to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  742. return chan;
  743. }
  744. static int bcm2835_dma_probe(struct platform_device *pdev)
  745. {
  746. struct bcm2835_dmadev *od;
  747. struct resource *res;
  748. void __iomem *base;
  749. int rc;
  750. int i, j;
  751. int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
  752. int irq_flags;
  753. uint32_t chans_available;
  754. char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
  755. if (!pdev->dev.dma_mask)
  756. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  757. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  758. if (rc)
  759. return rc;
  760. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  761. if (!od)
  762. return -ENOMEM;
  763. pdev->dev.dma_parms = &od->dma_parms;
  764. dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  765. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  766. base = devm_ioremap_resource(&pdev->dev, res);
  767. if (IS_ERR(base))
  768. return PTR_ERR(base);
  769. od->base = base;
  770. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  771. dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  772. dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  773. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  774. dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
  775. od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  776. od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  777. od->ddev.device_tx_status = bcm2835_dma_tx_status;
  778. od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  779. od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  780. od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  781. od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
  782. od->ddev.device_config = bcm2835_dma_slave_config;
  783. od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
  784. od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  785. od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  786. od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  787. BIT(DMA_MEM_TO_MEM);
  788. od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  789. od->ddev.dev = &pdev->dev;
  790. INIT_LIST_HEAD(&od->ddev.channels);
  791. spin_lock_init(&od->lock);
  792. platform_set_drvdata(pdev, od);
  793. /* Request DMA channel mask from device tree */
  794. if (of_property_read_u32(pdev->dev.of_node,
  795. "brcm,dma-channel-mask",
  796. &chans_available)) {
  797. dev_err(&pdev->dev, "Failed to get channel mask\n");
  798. rc = -EINVAL;
  799. goto err_no_dma;
  800. }
  801. /* get irqs for each channel that we support */
  802. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  803. /* skip masked out channels */
  804. if (!(chans_available & (1 << i))) {
  805. irq[i] = -1;
  806. continue;
  807. }
  808. /* get the named irq */
  809. snprintf(chan_name, sizeof(chan_name), "dma%i", i);
  810. irq[i] = platform_get_irq_byname(pdev, chan_name);
  811. if (irq[i] >= 0)
  812. continue;
  813. /* legacy device tree case handling */
  814. dev_warn_once(&pdev->dev,
  815. "missing interrupt-names property in device tree - legacy interpretation is used\n");
  816. /*
  817. * in case of channel >= 11
  818. * use the 11th interrupt and that is shared
  819. */
  820. irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
  821. }
  822. /* get irqs for each channel */
  823. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  824. /* skip channels without irq */
  825. if (irq[i] < 0)
  826. continue;
  827. /* check if there are other channels that also use this irq */
  828. irq_flags = 0;
  829. for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
  830. if ((i != j) && (irq[j] == irq[i])) {
  831. irq_flags = IRQF_SHARED;
  832. break;
  833. }
  834. /* initialize the channel */
  835. rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
  836. if (rc)
  837. goto err_no_dma;
  838. }
  839. dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  840. /* Device-tree DMA controller registration */
  841. rc = of_dma_controller_register(pdev->dev.of_node,
  842. bcm2835_dma_xlate, od);
  843. if (rc) {
  844. dev_err(&pdev->dev, "Failed to register DMA controller\n");
  845. goto err_no_dma;
  846. }
  847. rc = dma_async_device_register(&od->ddev);
  848. if (rc) {
  849. dev_err(&pdev->dev,
  850. "Failed to register slave DMA engine device: %d\n", rc);
  851. goto err_no_dma;
  852. }
  853. dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  854. return 0;
  855. err_no_dma:
  856. bcm2835_dma_free(od);
  857. return rc;
  858. }
  859. static int bcm2835_dma_remove(struct platform_device *pdev)
  860. {
  861. struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  862. dma_async_device_unregister(&od->ddev);
  863. bcm2835_dma_free(od);
  864. return 0;
  865. }
  866. static struct platform_driver bcm2835_dma_driver = {
  867. .probe = bcm2835_dma_probe,
  868. .remove = bcm2835_dma_remove,
  869. .driver = {
  870. .name = "bcm2835-dma",
  871. .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  872. },
  873. };
  874. module_platform_driver(bcm2835_dma_driver);
  875. MODULE_ALIAS("platform:bcm2835-dma");
  876. MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  877. MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  878. MODULE_LICENSE("GPL v2");