bcm2835-dma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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(struct bcm2835_chan *c)
  360. {
  361. void __iomem *chan_base = c->chan_base;
  362. long int timeout = 10000;
  363. /*
  364. * A zero control block address means the channel is idle.
  365. * (The ACTIVE flag in the CS register is not a reliable indicator.)
  366. */
  367. if (!readl(chan_base + BCM2835_DMA_ADDR))
  368. return 0;
  369. /* Write 0 to the active bit - Pause the DMA */
  370. writel(0, chan_base + BCM2835_DMA_CS);
  371. /* Wait for any current AXI transfer to complete */
  372. while ((readl(chan_base + BCM2835_DMA_CS) &
  373. BCM2835_DMA_WAITING_FOR_WRITES) && --timeout)
  374. cpu_relax();
  375. /* Peripheral might be stuck and fail to signal AXI write responses */
  376. if (!timeout)
  377. dev_err(c->vc.chan.device->dev,
  378. "failed to complete outstanding writes\n");
  379. writel(BCM2835_DMA_RESET, chan_base + BCM2835_DMA_CS);
  380. return 0;
  381. }
  382. static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
  383. {
  384. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  385. struct bcm2835_desc *d;
  386. if (!vd) {
  387. c->desc = NULL;
  388. return;
  389. }
  390. list_del(&vd->node);
  391. c->desc = d = to_bcm2835_dma_desc(&vd->tx);
  392. writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
  393. writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  394. }
  395. static irqreturn_t bcm2835_dma_callback(int irq, void *data)
  396. {
  397. struct bcm2835_chan *c = data;
  398. struct bcm2835_desc *d;
  399. unsigned long flags;
  400. /* check the shared interrupt */
  401. if (c->irq_flags & IRQF_SHARED) {
  402. /* check if the interrupt is enabled */
  403. flags = readl(c->chan_base + BCM2835_DMA_CS);
  404. /* if not set then we are not the reason for the irq */
  405. if (!(flags & BCM2835_DMA_INT))
  406. return IRQ_NONE;
  407. }
  408. spin_lock_irqsave(&c->vc.lock, flags);
  409. /*
  410. * Clear the INT flag to receive further interrupts. Keep the channel
  411. * active in case the descriptor is cyclic or in case the client has
  412. * already terminated the descriptor and issued a new one. (May happen
  413. * if this IRQ handler is threaded.) If the channel is finished, it
  414. * will remain idle despite the ACTIVE flag being set.
  415. */
  416. writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE,
  417. c->chan_base + BCM2835_DMA_CS);
  418. d = c->desc;
  419. if (d) {
  420. if (d->cyclic) {
  421. /* call the cyclic callback */
  422. vchan_cyclic_callback(&d->vd);
  423. } else if (!readl(c->chan_base + BCM2835_DMA_ADDR)) {
  424. vchan_cookie_complete(&c->desc->vd);
  425. bcm2835_dma_start_desc(c);
  426. }
  427. }
  428. spin_unlock_irqrestore(&c->vc.lock, flags);
  429. return IRQ_HANDLED;
  430. }
  431. static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
  432. {
  433. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  434. struct device *dev = c->vc.chan.device->dev;
  435. dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
  436. c->cb_pool = dma_pool_create(dev_name(dev), dev,
  437. sizeof(struct bcm2835_dma_cb), 0, 0);
  438. if (!c->cb_pool) {
  439. dev_err(dev, "unable to allocate descriptor pool\n");
  440. return -ENOMEM;
  441. }
  442. return request_irq(c->irq_number, bcm2835_dma_callback,
  443. c->irq_flags, "DMA IRQ", c);
  444. }
  445. static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
  446. {
  447. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  448. vchan_free_chan_resources(&c->vc);
  449. free_irq(c->irq_number, c);
  450. dma_pool_destroy(c->cb_pool);
  451. dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  452. }
  453. static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
  454. {
  455. return d->size;
  456. }
  457. static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
  458. {
  459. unsigned int i;
  460. size_t size;
  461. for (size = i = 0; i < d->frames; i++) {
  462. struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
  463. size_t this_size = control_block->length;
  464. dma_addr_t dma;
  465. if (d->dir == DMA_DEV_TO_MEM)
  466. dma = control_block->dst;
  467. else
  468. dma = control_block->src;
  469. if (size)
  470. size += this_size;
  471. else if (addr >= dma && addr < dma + this_size)
  472. size += dma + this_size - addr;
  473. }
  474. return size;
  475. }
  476. static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
  477. dma_cookie_t cookie, struct dma_tx_state *txstate)
  478. {
  479. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  480. struct virt_dma_desc *vd;
  481. enum dma_status ret;
  482. unsigned long flags;
  483. ret = dma_cookie_status(chan, cookie, txstate);
  484. if (ret == DMA_COMPLETE || !txstate)
  485. return ret;
  486. spin_lock_irqsave(&c->vc.lock, flags);
  487. vd = vchan_find_desc(&c->vc, cookie);
  488. if (vd) {
  489. txstate->residue =
  490. bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
  491. } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  492. struct bcm2835_desc *d = c->desc;
  493. dma_addr_t pos;
  494. if (d->dir == DMA_MEM_TO_DEV)
  495. pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
  496. else if (d->dir == DMA_DEV_TO_MEM)
  497. pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
  498. else
  499. pos = 0;
  500. txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
  501. } else {
  502. txstate->residue = 0;
  503. }
  504. spin_unlock_irqrestore(&c->vc.lock, flags);
  505. return ret;
  506. }
  507. static void bcm2835_dma_issue_pending(struct dma_chan *chan)
  508. {
  509. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  510. unsigned long flags;
  511. spin_lock_irqsave(&c->vc.lock, flags);
  512. if (vchan_issue_pending(&c->vc) && !c->desc)
  513. bcm2835_dma_start_desc(c);
  514. spin_unlock_irqrestore(&c->vc.lock, flags);
  515. }
  516. static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy(
  517. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  518. size_t len, unsigned long flags)
  519. {
  520. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  521. struct bcm2835_desc *d;
  522. u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC;
  523. u32 extra = BCM2835_DMA_INT_EN | BCM2835_DMA_WAIT_RESP;
  524. size_t max_len = bcm2835_dma_max_frame_length(c);
  525. size_t frames;
  526. /* if src, dst or len is not given return with an error */
  527. if (!src || !dst || !len)
  528. return NULL;
  529. /* calculate number of frames */
  530. frames = bcm2835_dma_frames_for_length(len, max_len);
  531. /* allocate the CB chain - this also fills in the pointers */
  532. d = bcm2835_dma_create_cb_chain(chan, DMA_MEM_TO_MEM, false,
  533. info, extra, frames,
  534. src, dst, len, 0, GFP_KERNEL);
  535. if (!d)
  536. return NULL;
  537. return vchan_tx_prep(&c->vc, &d->vd, flags);
  538. }
  539. static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
  540. struct dma_chan *chan,
  541. struct scatterlist *sgl, unsigned int sg_len,
  542. enum dma_transfer_direction direction,
  543. unsigned long flags, void *context)
  544. {
  545. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  546. struct bcm2835_desc *d;
  547. dma_addr_t src = 0, dst = 0;
  548. u32 info = BCM2835_DMA_WAIT_RESP;
  549. u32 extra = BCM2835_DMA_INT_EN;
  550. size_t frames;
  551. if (!is_slave_direction(direction)) {
  552. dev_err(chan->device->dev,
  553. "%s: bad direction?\n", __func__);
  554. return NULL;
  555. }
  556. if (c->dreq != 0)
  557. info |= BCM2835_DMA_PER_MAP(c->dreq);
  558. if (direction == DMA_DEV_TO_MEM) {
  559. if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  560. return NULL;
  561. src = c->cfg.src_addr;
  562. info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
  563. } else {
  564. if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  565. return NULL;
  566. dst = c->cfg.dst_addr;
  567. info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
  568. }
  569. /* count frames in sg list */
  570. frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
  571. /* allocate the CB chain */
  572. d = bcm2835_dma_create_cb_chain(chan, direction, false,
  573. info, extra,
  574. frames, src, dst, 0, 0,
  575. GFP_KERNEL);
  576. if (!d)
  577. return NULL;
  578. /* fill in frames with scatterlist pointers */
  579. bcm2835_dma_fill_cb_chain_with_sg(chan, direction, d->cb_list,
  580. sgl, sg_len);
  581. return vchan_tx_prep(&c->vc, &d->vd, flags);
  582. }
  583. static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
  584. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  585. size_t period_len, enum dma_transfer_direction direction,
  586. unsigned long flags)
  587. {
  588. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  589. struct bcm2835_desc *d;
  590. dma_addr_t src, dst;
  591. u32 info = BCM2835_DMA_WAIT_RESP;
  592. u32 extra = BCM2835_DMA_INT_EN;
  593. size_t max_len = bcm2835_dma_max_frame_length(c);
  594. size_t frames;
  595. /* Grab configuration */
  596. if (!is_slave_direction(direction)) {
  597. dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  598. return NULL;
  599. }
  600. if (!buf_len) {
  601. dev_err(chan->device->dev,
  602. "%s: bad buffer length (= 0)\n", __func__);
  603. return NULL;
  604. }
  605. /*
  606. * warn if buf_len is not a multiple of period_len - this may leed
  607. * to unexpected latencies for interrupts and thus audiable clicks
  608. */
  609. if (buf_len % period_len)
  610. dev_warn_once(chan->device->dev,
  611. "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
  612. __func__, buf_len, period_len);
  613. /* Setup DREQ channel */
  614. if (c->dreq != 0)
  615. info |= BCM2835_DMA_PER_MAP(c->dreq);
  616. if (direction == DMA_DEV_TO_MEM) {
  617. if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  618. return NULL;
  619. src = c->cfg.src_addr;
  620. dst = buf_addr;
  621. info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
  622. } else {
  623. if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  624. return NULL;
  625. dst = c->cfg.dst_addr;
  626. src = buf_addr;
  627. info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
  628. }
  629. /* calculate number of frames */
  630. frames = /* number of periods */
  631. DIV_ROUND_UP(buf_len, period_len) *
  632. /* number of frames per period */
  633. bcm2835_dma_frames_for_length(period_len, max_len);
  634. /*
  635. * allocate the CB chain
  636. * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
  637. * implementation calls prep_dma_cyclic with interrupts disabled.
  638. */
  639. d = bcm2835_dma_create_cb_chain(chan, direction, true,
  640. info, extra,
  641. frames, src, dst, buf_len,
  642. period_len, GFP_NOWAIT);
  643. if (!d)
  644. return NULL;
  645. /* wrap around into a loop */
  646. d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
  647. return vchan_tx_prep(&c->vc, &d->vd, flags);
  648. }
  649. static int bcm2835_dma_slave_config(struct dma_chan *chan,
  650. struct dma_slave_config *cfg)
  651. {
  652. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  653. if ((cfg->direction == DMA_DEV_TO_MEM &&
  654. cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  655. (cfg->direction == DMA_MEM_TO_DEV &&
  656. cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  657. !is_slave_direction(cfg->direction)) {
  658. return -EINVAL;
  659. }
  660. c->cfg = *cfg;
  661. return 0;
  662. }
  663. static int bcm2835_dma_terminate_all(struct dma_chan *chan)
  664. {
  665. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  666. struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
  667. unsigned long flags;
  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. /* stop DMA activity */
  675. if (c->desc) {
  676. bcm2835_dma_desc_free(&c->desc->vd);
  677. c->desc = NULL;
  678. bcm2835_dma_abort(c);
  679. }
  680. vchan_get_all_descriptors(&c->vc, &head);
  681. spin_unlock_irqrestore(&c->vc.lock, flags);
  682. vchan_dma_desc_free_list(&c->vc, &head);
  683. return 0;
  684. }
  685. static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
  686. int irq, unsigned int irq_flags)
  687. {
  688. struct bcm2835_chan *c;
  689. c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  690. if (!c)
  691. return -ENOMEM;
  692. c->vc.desc_free = bcm2835_dma_desc_free;
  693. vchan_init(&c->vc, &d->ddev);
  694. INIT_LIST_HEAD(&c->node);
  695. c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  696. c->ch = chan_id;
  697. c->irq_number = irq;
  698. c->irq_flags = irq_flags;
  699. /* check in DEBUG register if this is a LITE channel */
  700. if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
  701. BCM2835_DMA_DEBUG_LITE)
  702. c->is_lite_channel = true;
  703. return 0;
  704. }
  705. static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  706. {
  707. struct bcm2835_chan *c, *next;
  708. list_for_each_entry_safe(c, next, &od->ddev.channels,
  709. vc.chan.device_node) {
  710. list_del(&c->vc.chan.device_node);
  711. tasklet_kill(&c->vc.task);
  712. }
  713. }
  714. static const struct of_device_id bcm2835_dma_of_match[] = {
  715. { .compatible = "brcm,bcm2835-dma", },
  716. {},
  717. };
  718. MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  719. static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  720. struct of_dma *ofdma)
  721. {
  722. struct bcm2835_dmadev *d = ofdma->of_dma_data;
  723. struct dma_chan *chan;
  724. chan = dma_get_any_slave_channel(&d->ddev);
  725. if (!chan)
  726. return NULL;
  727. /* Set DREQ from param */
  728. to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  729. return chan;
  730. }
  731. static int bcm2835_dma_probe(struct platform_device *pdev)
  732. {
  733. struct bcm2835_dmadev *od;
  734. struct resource *res;
  735. void __iomem *base;
  736. int rc;
  737. int i, j;
  738. int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
  739. int irq_flags;
  740. uint32_t chans_available;
  741. char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
  742. if (!pdev->dev.dma_mask)
  743. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  744. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  745. if (rc) {
  746. dev_err(&pdev->dev, "Unable to set DMA mask\n");
  747. return rc;
  748. }
  749. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  750. if (!od)
  751. return -ENOMEM;
  752. pdev->dev.dma_parms = &od->dma_parms;
  753. dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  754. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  755. base = devm_ioremap_resource(&pdev->dev, res);
  756. if (IS_ERR(base))
  757. return PTR_ERR(base);
  758. od->base = base;
  759. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  760. dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  761. dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  762. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  763. dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
  764. od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  765. od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  766. od->ddev.device_tx_status = bcm2835_dma_tx_status;
  767. od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  768. od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  769. od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  770. od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
  771. od->ddev.device_config = bcm2835_dma_slave_config;
  772. od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
  773. od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  774. od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  775. od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  776. BIT(DMA_MEM_TO_MEM);
  777. od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  778. od->ddev.dev = &pdev->dev;
  779. INIT_LIST_HEAD(&od->ddev.channels);
  780. spin_lock_init(&od->lock);
  781. platform_set_drvdata(pdev, od);
  782. /* Request DMA channel mask from device tree */
  783. if (of_property_read_u32(pdev->dev.of_node,
  784. "brcm,dma-channel-mask",
  785. &chans_available)) {
  786. dev_err(&pdev->dev, "Failed to get channel mask\n");
  787. rc = -EINVAL;
  788. goto err_no_dma;
  789. }
  790. /* get irqs for each channel that we support */
  791. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  792. /* skip masked out channels */
  793. if (!(chans_available & (1 << i))) {
  794. irq[i] = -1;
  795. continue;
  796. }
  797. /* get the named irq */
  798. snprintf(chan_name, sizeof(chan_name), "dma%i", i);
  799. irq[i] = platform_get_irq_byname(pdev, chan_name);
  800. if (irq[i] >= 0)
  801. continue;
  802. /* legacy device tree case handling */
  803. dev_warn_once(&pdev->dev,
  804. "missing interrupt-names property in device tree - legacy interpretation is used\n");
  805. /*
  806. * in case of channel >= 11
  807. * use the 11th interrupt and that is shared
  808. */
  809. irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
  810. }
  811. /* get irqs for each channel */
  812. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  813. /* skip channels without irq */
  814. if (irq[i] < 0)
  815. continue;
  816. /* check if there are other channels that also use this irq */
  817. irq_flags = 0;
  818. for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
  819. if ((i != j) && (irq[j] == irq[i])) {
  820. irq_flags = IRQF_SHARED;
  821. break;
  822. }
  823. /* initialize the channel */
  824. rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
  825. if (rc)
  826. goto err_no_dma;
  827. }
  828. dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  829. /* Device-tree DMA controller registration */
  830. rc = of_dma_controller_register(pdev->dev.of_node,
  831. bcm2835_dma_xlate, od);
  832. if (rc) {
  833. dev_err(&pdev->dev, "Failed to register DMA controller\n");
  834. goto err_no_dma;
  835. }
  836. rc = dma_async_device_register(&od->ddev);
  837. if (rc) {
  838. dev_err(&pdev->dev,
  839. "Failed to register slave DMA engine device: %d\n", rc);
  840. goto err_no_dma;
  841. }
  842. dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  843. return 0;
  844. err_no_dma:
  845. bcm2835_dma_free(od);
  846. return rc;
  847. }
  848. static int bcm2835_dma_remove(struct platform_device *pdev)
  849. {
  850. struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  851. dma_async_device_unregister(&od->ddev);
  852. bcm2835_dma_free(od);
  853. return 0;
  854. }
  855. static struct platform_driver bcm2835_dma_driver = {
  856. .probe = bcm2835_dma_probe,
  857. .remove = bcm2835_dma_remove,
  858. .driver = {
  859. .name = "bcm2835-dma",
  860. .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  861. },
  862. };
  863. module_platform_driver(bcm2835_dma_driver);
  864. MODULE_ALIAS("platform:bcm2835-dma");
  865. MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  866. MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  867. MODULE_LICENSE("GPL v2");