bcm2835-dma.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. return;
  220. /* calculate the length that remains to reach period_length */
  221. control_block->length = period_len - *total_len;
  222. /* reset total_length for next period */
  223. *total_len = 0;
  224. /* add extrainfo bits in info */
  225. control_block->info |= finalextrainfo;
  226. }
  227. static inline size_t bcm2835_dma_count_frames_for_sg(
  228. struct bcm2835_chan *c,
  229. struct scatterlist *sgl,
  230. unsigned int sg_len)
  231. {
  232. size_t frames = 0;
  233. struct scatterlist *sgent;
  234. unsigned int i;
  235. size_t plength = bcm2835_dma_max_frame_length(c);
  236. for_each_sg(sgl, sgent, sg_len, i)
  237. frames += bcm2835_dma_frames_for_length(
  238. sg_dma_len(sgent), plength);
  239. return frames;
  240. }
  241. /**
  242. * bcm2835_dma_create_cb_chain - create a control block and fills data in
  243. *
  244. * @chan: the @dma_chan for which we run this
  245. * @direction: the direction in which we transfer
  246. * @cyclic: it is a cyclic transfer
  247. * @info: the default info bits to apply per controlblock
  248. * @frames: number of controlblocks to allocate
  249. * @src: the src address to assign (if the S_INC bit is set
  250. * in @info, then it gets incremented)
  251. * @dst: the dst address to assign (if the D_INC bit is set
  252. * in @info, then it gets incremented)
  253. * @buf_len: the full buffer length (may also be 0)
  254. * @period_len: the period length when to apply @finalextrainfo
  255. * in addition to the last transfer
  256. * this will also break some control-blocks early
  257. * @finalextrainfo: additional bits in last controlblock
  258. * (or when period_len is reached in case of cyclic)
  259. * @gfp: the GFP flag to use for allocation
  260. */
  261. static struct bcm2835_desc *bcm2835_dma_create_cb_chain(
  262. struct dma_chan *chan, enum dma_transfer_direction direction,
  263. bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
  264. dma_addr_t src, dma_addr_t dst, size_t buf_len,
  265. size_t period_len, gfp_t gfp)
  266. {
  267. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  268. size_t len = buf_len, total_len;
  269. size_t frame;
  270. struct bcm2835_desc *d;
  271. struct bcm2835_cb_entry *cb_entry;
  272. struct bcm2835_dma_cb *control_block;
  273. if (!frames)
  274. return NULL;
  275. /* allocate and setup the descriptor. */
  276. d = kzalloc(sizeof(*d) + frames * sizeof(struct bcm2835_cb_entry),
  277. gfp);
  278. if (!d)
  279. return NULL;
  280. d->c = c;
  281. d->dir = direction;
  282. d->cyclic = cyclic;
  283. /*
  284. * Iterate over all frames, create a control block
  285. * for each frame and link them together.
  286. */
  287. for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) {
  288. cb_entry = &d->cb_list[frame];
  289. cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp,
  290. &cb_entry->paddr);
  291. if (!cb_entry->cb)
  292. goto error_cb;
  293. /* fill in the control block */
  294. control_block = cb_entry->cb;
  295. control_block->info = info;
  296. control_block->src = src;
  297. control_block->dst = dst;
  298. control_block->stride = 0;
  299. control_block->next = 0;
  300. /* set up length in control_block if requested */
  301. if (buf_len) {
  302. /* calculate length honoring period_length */
  303. bcm2835_dma_create_cb_set_length(
  304. c, control_block,
  305. len, period_len, &total_len,
  306. cyclic ? finalextrainfo : 0);
  307. /* calculate new remaining length */
  308. len -= control_block->length;
  309. }
  310. /* link this the last controlblock */
  311. if (frame)
  312. d->cb_list[frame - 1].cb->next = cb_entry->paddr;
  313. /* update src and dst and length */
  314. if (src && (info & BCM2835_DMA_S_INC))
  315. src += control_block->length;
  316. if (dst && (info & BCM2835_DMA_D_INC))
  317. dst += control_block->length;
  318. /* Length of total transfer */
  319. d->size += control_block->length;
  320. }
  321. /* the last frame requires extra flags */
  322. d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
  323. /* detect a size missmatch */
  324. if (buf_len && (d->size != buf_len))
  325. goto error_cb;
  326. return d;
  327. error_cb:
  328. bcm2835_dma_free_cb_chain(d);
  329. return NULL;
  330. }
  331. static void bcm2835_dma_fill_cb_chain_with_sg(
  332. struct dma_chan *chan,
  333. enum dma_transfer_direction direction,
  334. struct bcm2835_cb_entry *cb,
  335. struct scatterlist *sgl,
  336. unsigned int sg_len)
  337. {
  338. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  339. size_t len, max_len;
  340. unsigned int i;
  341. dma_addr_t addr;
  342. struct scatterlist *sgent;
  343. max_len = bcm2835_dma_max_frame_length(c);
  344. for_each_sg(sgl, sgent, sg_len, i) {
  345. for (addr = sg_dma_address(sgent), len = sg_dma_len(sgent);
  346. len > 0;
  347. addr += cb->cb->length, len -= cb->cb->length, cb++) {
  348. if (direction == DMA_DEV_TO_MEM)
  349. cb->cb->dst = addr;
  350. else
  351. cb->cb->src = addr;
  352. cb->cb->length = min(len, max_len);
  353. }
  354. }
  355. }
  356. static int bcm2835_dma_abort(void __iomem *chan_base)
  357. {
  358. unsigned long cs;
  359. long int timeout = 10000;
  360. cs = readl(chan_base + BCM2835_DMA_CS);
  361. if (!(cs & BCM2835_DMA_ACTIVE))
  362. return 0;
  363. /* Write 0 to the active bit - Pause the DMA */
  364. writel(0, chan_base + BCM2835_DMA_CS);
  365. /* Wait for any current AXI transfer to complete */
  366. while ((cs & BCM2835_DMA_ISPAUSED) && --timeout) {
  367. cpu_relax();
  368. cs = readl(chan_base + BCM2835_DMA_CS);
  369. }
  370. /* We'll un-pause when we set of our next DMA */
  371. if (!timeout)
  372. return -ETIMEDOUT;
  373. if (!(cs & BCM2835_DMA_ACTIVE))
  374. return 0;
  375. /* Terminate the control block chain */
  376. writel(0, chan_base + BCM2835_DMA_NEXTCB);
  377. /* Abort the whole DMA */
  378. writel(BCM2835_DMA_ABORT | BCM2835_DMA_ACTIVE,
  379. 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. /* Acknowledge interrupt */
  410. writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
  411. d = c->desc;
  412. if (d) {
  413. if (d->cyclic) {
  414. /* call the cyclic callback */
  415. vchan_cyclic_callback(&d->vd);
  416. /* Keep the DMA engine running */
  417. writel(BCM2835_DMA_ACTIVE,
  418. c->chan_base + BCM2835_DMA_CS);
  419. } else {
  420. vchan_cookie_complete(&c->desc->vd);
  421. bcm2835_dma_start_desc(c);
  422. }
  423. }
  424. spin_unlock_irqrestore(&c->vc.lock, flags);
  425. return IRQ_HANDLED;
  426. }
  427. static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
  428. {
  429. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  430. struct device *dev = c->vc.chan.device->dev;
  431. dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
  432. c->cb_pool = dma_pool_create(dev_name(dev), dev,
  433. sizeof(struct bcm2835_dma_cb), 0, 0);
  434. if (!c->cb_pool) {
  435. dev_err(dev, "unable to allocate descriptor pool\n");
  436. return -ENOMEM;
  437. }
  438. return request_irq(c->irq_number, bcm2835_dma_callback,
  439. c->irq_flags, "DMA IRQ", c);
  440. }
  441. static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
  442. {
  443. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  444. vchan_free_chan_resources(&c->vc);
  445. free_irq(c->irq_number, c);
  446. dma_pool_destroy(c->cb_pool);
  447. dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  448. }
  449. static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
  450. {
  451. return d->size;
  452. }
  453. static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
  454. {
  455. unsigned int i;
  456. size_t size;
  457. for (size = i = 0; i < d->frames; i++) {
  458. struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
  459. size_t this_size = control_block->length;
  460. dma_addr_t dma;
  461. if (d->dir == DMA_DEV_TO_MEM)
  462. dma = control_block->dst;
  463. else
  464. dma = control_block->src;
  465. if (size)
  466. size += this_size;
  467. else if (addr >= dma && addr < dma + this_size)
  468. size += dma + this_size - addr;
  469. }
  470. return size;
  471. }
  472. static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
  473. dma_cookie_t cookie, struct dma_tx_state *txstate)
  474. {
  475. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  476. struct virt_dma_desc *vd;
  477. enum dma_status ret;
  478. unsigned long flags;
  479. ret = dma_cookie_status(chan, cookie, txstate);
  480. if (ret == DMA_COMPLETE || !txstate)
  481. return ret;
  482. spin_lock_irqsave(&c->vc.lock, flags);
  483. vd = vchan_find_desc(&c->vc, cookie);
  484. if (vd) {
  485. txstate->residue =
  486. bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
  487. } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  488. struct bcm2835_desc *d = c->desc;
  489. dma_addr_t pos;
  490. if (d->dir == DMA_MEM_TO_DEV)
  491. pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
  492. else if (d->dir == DMA_DEV_TO_MEM)
  493. pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
  494. else
  495. pos = 0;
  496. txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
  497. } else {
  498. txstate->residue = 0;
  499. }
  500. spin_unlock_irqrestore(&c->vc.lock, flags);
  501. return ret;
  502. }
  503. static void bcm2835_dma_issue_pending(struct dma_chan *chan)
  504. {
  505. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  506. unsigned long flags;
  507. spin_lock_irqsave(&c->vc.lock, flags);
  508. if (vchan_issue_pending(&c->vc) && !c->desc)
  509. bcm2835_dma_start_desc(c);
  510. spin_unlock_irqrestore(&c->vc.lock, flags);
  511. }
  512. static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy(
  513. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  514. size_t len, unsigned long flags)
  515. {
  516. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  517. struct bcm2835_desc *d;
  518. u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC;
  519. u32 extra = BCM2835_DMA_INT_EN | BCM2835_DMA_WAIT_RESP;
  520. size_t max_len = bcm2835_dma_max_frame_length(c);
  521. size_t frames;
  522. /* if src, dst or len is not given return with an error */
  523. if (!src || !dst || !len)
  524. return NULL;
  525. /* calculate number of frames */
  526. frames = bcm2835_dma_frames_for_length(len, max_len);
  527. /* allocate the CB chain - this also fills in the pointers */
  528. d = bcm2835_dma_create_cb_chain(chan, DMA_MEM_TO_MEM, false,
  529. info, extra, frames,
  530. src, dst, len, 0, GFP_KERNEL);
  531. if (!d)
  532. return NULL;
  533. return vchan_tx_prep(&c->vc, &d->vd, flags);
  534. }
  535. static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
  536. struct dma_chan *chan,
  537. struct scatterlist *sgl, unsigned int sg_len,
  538. enum dma_transfer_direction direction,
  539. unsigned long flags, void *context)
  540. {
  541. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  542. struct bcm2835_desc *d;
  543. dma_addr_t src = 0, dst = 0;
  544. u32 info = BCM2835_DMA_WAIT_RESP;
  545. u32 extra = BCM2835_DMA_INT_EN;
  546. size_t frames;
  547. if (!is_slave_direction(direction)) {
  548. dev_err(chan->device->dev,
  549. "%s: bad direction?\n", __func__);
  550. return NULL;
  551. }
  552. if (c->dreq != 0)
  553. info |= BCM2835_DMA_PER_MAP(c->dreq);
  554. if (direction == DMA_DEV_TO_MEM) {
  555. if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  556. return NULL;
  557. src = c->cfg.src_addr;
  558. info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
  559. } else {
  560. if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  561. return NULL;
  562. dst = c->cfg.dst_addr;
  563. info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
  564. }
  565. /* count frames in sg list */
  566. frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
  567. /* allocate the CB chain */
  568. d = bcm2835_dma_create_cb_chain(chan, direction, false,
  569. info, extra,
  570. frames, src, dst, 0, 0,
  571. GFP_KERNEL);
  572. if (!d)
  573. return NULL;
  574. /* fill in frames with scatterlist pointers */
  575. bcm2835_dma_fill_cb_chain_with_sg(chan, direction, d->cb_list,
  576. sgl, sg_len);
  577. return vchan_tx_prep(&c->vc, &d->vd, flags);
  578. }
  579. static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
  580. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  581. size_t period_len, enum dma_transfer_direction direction,
  582. unsigned long flags)
  583. {
  584. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  585. struct bcm2835_desc *d;
  586. dma_addr_t src, dst;
  587. u32 info = BCM2835_DMA_WAIT_RESP;
  588. u32 extra = BCM2835_DMA_INT_EN;
  589. size_t max_len = bcm2835_dma_max_frame_length(c);
  590. size_t frames;
  591. /* Grab configuration */
  592. if (!is_slave_direction(direction)) {
  593. dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  594. return NULL;
  595. }
  596. if (!buf_len) {
  597. dev_err(chan->device->dev,
  598. "%s: bad buffer length (= 0)\n", __func__);
  599. return NULL;
  600. }
  601. /*
  602. * warn if buf_len is not a multiple of period_len - this may leed
  603. * to unexpected latencies for interrupts and thus audiable clicks
  604. */
  605. if (buf_len % period_len)
  606. dev_warn_once(chan->device->dev,
  607. "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
  608. __func__, buf_len, period_len);
  609. /* Setup DREQ channel */
  610. if (c->dreq != 0)
  611. info |= BCM2835_DMA_PER_MAP(c->dreq);
  612. if (direction == DMA_DEV_TO_MEM) {
  613. if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  614. return NULL;
  615. src = c->cfg.src_addr;
  616. dst = buf_addr;
  617. info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
  618. } else {
  619. if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
  620. return NULL;
  621. dst = c->cfg.dst_addr;
  622. src = buf_addr;
  623. info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
  624. }
  625. /* calculate number of frames */
  626. frames = /* number of periods */
  627. DIV_ROUND_UP(buf_len, period_len) *
  628. /* number of frames per period */
  629. bcm2835_dma_frames_for_length(period_len, max_len);
  630. /*
  631. * allocate the CB chain
  632. * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
  633. * implementation calls prep_dma_cyclic with interrupts disabled.
  634. */
  635. d = bcm2835_dma_create_cb_chain(chan, direction, true,
  636. info, extra,
  637. frames, src, dst, buf_len,
  638. period_len, GFP_NOWAIT);
  639. if (!d)
  640. return NULL;
  641. /* wrap around into a loop */
  642. d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
  643. return vchan_tx_prep(&c->vc, &d->vd, flags);
  644. }
  645. static int bcm2835_dma_slave_config(struct dma_chan *chan,
  646. struct dma_slave_config *cfg)
  647. {
  648. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  649. if ((cfg->direction == DMA_DEV_TO_MEM &&
  650. cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  651. (cfg->direction == DMA_MEM_TO_DEV &&
  652. cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  653. !is_slave_direction(cfg->direction)) {
  654. return -EINVAL;
  655. }
  656. c->cfg = *cfg;
  657. return 0;
  658. }
  659. static int bcm2835_dma_terminate_all(struct dma_chan *chan)
  660. {
  661. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  662. struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
  663. unsigned long flags;
  664. int timeout = 10000;
  665. LIST_HEAD(head);
  666. spin_lock_irqsave(&c->vc.lock, flags);
  667. /* Prevent this channel being scheduled */
  668. spin_lock(&d->lock);
  669. list_del_init(&c->node);
  670. spin_unlock(&d->lock);
  671. /*
  672. * Stop DMA activity: we assume the callback will not be called
  673. * after bcm_dma_abort() returns (even if it does, it will see
  674. * c->desc is NULL and exit.)
  675. */
  676. if (c->desc) {
  677. bcm2835_dma_desc_free(&c->desc->vd);
  678. c->desc = NULL;
  679. bcm2835_dma_abort(c->chan_base);
  680. /* Wait for stopping */
  681. while (--timeout) {
  682. if (!(readl(c->chan_base + BCM2835_DMA_CS) &
  683. BCM2835_DMA_ACTIVE))
  684. break;
  685. cpu_relax();
  686. }
  687. if (!timeout)
  688. dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  689. }
  690. vchan_get_all_descriptors(&c->vc, &head);
  691. spin_unlock_irqrestore(&c->vc.lock, flags);
  692. vchan_dma_desc_free_list(&c->vc, &head);
  693. return 0;
  694. }
  695. static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
  696. int irq, unsigned int irq_flags)
  697. {
  698. struct bcm2835_chan *c;
  699. c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  700. if (!c)
  701. return -ENOMEM;
  702. c->vc.desc_free = bcm2835_dma_desc_free;
  703. vchan_init(&c->vc, &d->ddev);
  704. INIT_LIST_HEAD(&c->node);
  705. c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  706. c->ch = chan_id;
  707. c->irq_number = irq;
  708. c->irq_flags = irq_flags;
  709. /* check in DEBUG register if this is a LITE channel */
  710. if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
  711. BCM2835_DMA_DEBUG_LITE)
  712. c->is_lite_channel = true;
  713. return 0;
  714. }
  715. static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  716. {
  717. struct bcm2835_chan *c, *next;
  718. list_for_each_entry_safe(c, next, &od->ddev.channels,
  719. vc.chan.device_node) {
  720. list_del(&c->vc.chan.device_node);
  721. tasklet_kill(&c->vc.task);
  722. }
  723. }
  724. static const struct of_device_id bcm2835_dma_of_match[] = {
  725. { .compatible = "brcm,bcm2835-dma", },
  726. {},
  727. };
  728. MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  729. static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  730. struct of_dma *ofdma)
  731. {
  732. struct bcm2835_dmadev *d = ofdma->of_dma_data;
  733. struct dma_chan *chan;
  734. chan = dma_get_any_slave_channel(&d->ddev);
  735. if (!chan)
  736. return NULL;
  737. /* Set DREQ from param */
  738. to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  739. return chan;
  740. }
  741. static int bcm2835_dma_probe(struct platform_device *pdev)
  742. {
  743. struct bcm2835_dmadev *od;
  744. struct resource *res;
  745. void __iomem *base;
  746. int rc;
  747. int i, j;
  748. int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
  749. int irq_flags;
  750. uint32_t chans_available;
  751. char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
  752. if (!pdev->dev.dma_mask)
  753. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  754. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  755. if (rc)
  756. return rc;
  757. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  758. if (!od)
  759. return -ENOMEM;
  760. pdev->dev.dma_parms = &od->dma_parms;
  761. dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  762. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  763. base = devm_ioremap_resource(&pdev->dev, res);
  764. if (IS_ERR(base))
  765. return PTR_ERR(base);
  766. od->base = base;
  767. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  768. dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  769. dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  770. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  771. dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
  772. od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  773. od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  774. od->ddev.device_tx_status = bcm2835_dma_tx_status;
  775. od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  776. od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  777. od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  778. od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
  779. od->ddev.device_config = bcm2835_dma_slave_config;
  780. od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
  781. od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  782. od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  783. od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  784. BIT(DMA_MEM_TO_MEM);
  785. od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  786. od->ddev.dev = &pdev->dev;
  787. INIT_LIST_HEAD(&od->ddev.channels);
  788. spin_lock_init(&od->lock);
  789. platform_set_drvdata(pdev, od);
  790. /* Request DMA channel mask from device tree */
  791. if (of_property_read_u32(pdev->dev.of_node,
  792. "brcm,dma-channel-mask",
  793. &chans_available)) {
  794. dev_err(&pdev->dev, "Failed to get channel mask\n");
  795. rc = -EINVAL;
  796. goto err_no_dma;
  797. }
  798. /* get irqs for each channel that we support */
  799. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  800. /* skip masked out channels */
  801. if (!(chans_available & (1 << i))) {
  802. irq[i] = -1;
  803. continue;
  804. }
  805. /* get the named irq */
  806. snprintf(chan_name, sizeof(chan_name), "dma%i", i);
  807. irq[i] = platform_get_irq_byname(pdev, chan_name);
  808. if (irq[i] >= 0)
  809. continue;
  810. /* legacy device tree case handling */
  811. dev_warn_once(&pdev->dev,
  812. "missing interrupt-names property in device tree - legacy interpretation is used\n");
  813. /*
  814. * in case of channel >= 11
  815. * use the 11th interrupt and that is shared
  816. */
  817. irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
  818. }
  819. /* get irqs for each channel */
  820. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  821. /* skip channels without irq */
  822. if (irq[i] < 0)
  823. continue;
  824. /* check if there are other channels that also use this irq */
  825. irq_flags = 0;
  826. for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
  827. if ((i != j) && (irq[j] == irq[i])) {
  828. irq_flags = IRQF_SHARED;
  829. break;
  830. }
  831. /* initialize the channel */
  832. rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
  833. if (rc)
  834. goto err_no_dma;
  835. }
  836. dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  837. /* Device-tree DMA controller registration */
  838. rc = of_dma_controller_register(pdev->dev.of_node,
  839. bcm2835_dma_xlate, od);
  840. if (rc) {
  841. dev_err(&pdev->dev, "Failed to register DMA controller\n");
  842. goto err_no_dma;
  843. }
  844. rc = dma_async_device_register(&od->ddev);
  845. if (rc) {
  846. dev_err(&pdev->dev,
  847. "Failed to register slave DMA engine device: %d\n", rc);
  848. goto err_no_dma;
  849. }
  850. dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  851. return 0;
  852. err_no_dma:
  853. bcm2835_dma_free(od);
  854. return rc;
  855. }
  856. static int bcm2835_dma_remove(struct platform_device *pdev)
  857. {
  858. struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  859. dma_async_device_unregister(&od->ddev);
  860. bcm2835_dma_free(od);
  861. return 0;
  862. }
  863. static struct platform_driver bcm2835_dma_driver = {
  864. .probe = bcm2835_dma_probe,
  865. .remove = bcm2835_dma_remove,
  866. .driver = {
  867. .name = "bcm2835-dma",
  868. .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  869. },
  870. };
  871. module_platform_driver(bcm2835_dma_driver);
  872. MODULE_ALIAS("platform:bcm2835-dma");
  873. MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  874. MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  875. MODULE_LICENSE("GPL v2");