bcm2835-dma.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. vchan_terminate_vdesc(&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 void bcm2835_dma_synchronize(struct dma_chan *chan)
  686. {
  687. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  688. vchan_synchronize(&c->vc);
  689. }
  690. static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
  691. int irq, unsigned int irq_flags)
  692. {
  693. struct bcm2835_chan *c;
  694. c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  695. if (!c)
  696. return -ENOMEM;
  697. c->vc.desc_free = bcm2835_dma_desc_free;
  698. vchan_init(&c->vc, &d->ddev);
  699. INIT_LIST_HEAD(&c->node);
  700. c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  701. c->ch = chan_id;
  702. c->irq_number = irq;
  703. c->irq_flags = irq_flags;
  704. /* check in DEBUG register if this is a LITE channel */
  705. if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
  706. BCM2835_DMA_DEBUG_LITE)
  707. c->is_lite_channel = true;
  708. return 0;
  709. }
  710. static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  711. {
  712. struct bcm2835_chan *c, *next;
  713. list_for_each_entry_safe(c, next, &od->ddev.channels,
  714. vc.chan.device_node) {
  715. list_del(&c->vc.chan.device_node);
  716. tasklet_kill(&c->vc.task);
  717. }
  718. }
  719. static const struct of_device_id bcm2835_dma_of_match[] = {
  720. { .compatible = "brcm,bcm2835-dma", },
  721. {},
  722. };
  723. MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  724. static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  725. struct of_dma *ofdma)
  726. {
  727. struct bcm2835_dmadev *d = ofdma->of_dma_data;
  728. struct dma_chan *chan;
  729. chan = dma_get_any_slave_channel(&d->ddev);
  730. if (!chan)
  731. return NULL;
  732. /* Set DREQ from param */
  733. to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  734. return chan;
  735. }
  736. static int bcm2835_dma_probe(struct platform_device *pdev)
  737. {
  738. struct bcm2835_dmadev *od;
  739. struct resource *res;
  740. void __iomem *base;
  741. int rc;
  742. int i, j;
  743. int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
  744. int irq_flags;
  745. uint32_t chans_available;
  746. char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
  747. if (!pdev->dev.dma_mask)
  748. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  749. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  750. if (rc) {
  751. dev_err(&pdev->dev, "Unable to set DMA mask\n");
  752. return rc;
  753. }
  754. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  755. if (!od)
  756. return -ENOMEM;
  757. pdev->dev.dma_parms = &od->dma_parms;
  758. dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  759. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  760. base = devm_ioremap_resource(&pdev->dev, res);
  761. if (IS_ERR(base))
  762. return PTR_ERR(base);
  763. od->base = base;
  764. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  765. dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  766. dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  767. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  768. dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
  769. od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  770. od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  771. od->ddev.device_tx_status = bcm2835_dma_tx_status;
  772. od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  773. od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  774. od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  775. od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
  776. od->ddev.device_config = bcm2835_dma_slave_config;
  777. od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
  778. od->ddev.device_synchronize = bcm2835_dma_synchronize;
  779. od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  780. od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  781. od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  782. BIT(DMA_MEM_TO_MEM);
  783. od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  784. od->ddev.dev = &pdev->dev;
  785. INIT_LIST_HEAD(&od->ddev.channels);
  786. spin_lock_init(&od->lock);
  787. platform_set_drvdata(pdev, od);
  788. /* Request DMA channel mask from device tree */
  789. if (of_property_read_u32(pdev->dev.of_node,
  790. "brcm,dma-channel-mask",
  791. &chans_available)) {
  792. dev_err(&pdev->dev, "Failed to get channel mask\n");
  793. rc = -EINVAL;
  794. goto err_no_dma;
  795. }
  796. /* get irqs for each channel that we support */
  797. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  798. /* skip masked out channels */
  799. if (!(chans_available & (1 << i))) {
  800. irq[i] = -1;
  801. continue;
  802. }
  803. /* get the named irq */
  804. snprintf(chan_name, sizeof(chan_name), "dma%i", i);
  805. irq[i] = platform_get_irq_byname(pdev, chan_name);
  806. if (irq[i] >= 0)
  807. continue;
  808. /* legacy device tree case handling */
  809. dev_warn_once(&pdev->dev,
  810. "missing interrupt-names property in device tree - legacy interpretation is used\n");
  811. /*
  812. * in case of channel >= 11
  813. * use the 11th interrupt and that is shared
  814. */
  815. irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
  816. }
  817. /* get irqs for each channel */
  818. for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
  819. /* skip channels without irq */
  820. if (irq[i] < 0)
  821. continue;
  822. /* check if there are other channels that also use this irq */
  823. irq_flags = 0;
  824. for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
  825. if ((i != j) && (irq[j] == irq[i])) {
  826. irq_flags = IRQF_SHARED;
  827. break;
  828. }
  829. /* initialize the channel */
  830. rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
  831. if (rc)
  832. goto err_no_dma;
  833. }
  834. dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  835. /* Device-tree DMA controller registration */
  836. rc = of_dma_controller_register(pdev->dev.of_node,
  837. bcm2835_dma_xlate, od);
  838. if (rc) {
  839. dev_err(&pdev->dev, "Failed to register DMA controller\n");
  840. goto err_no_dma;
  841. }
  842. rc = dma_async_device_register(&od->ddev);
  843. if (rc) {
  844. dev_err(&pdev->dev,
  845. "Failed to register slave DMA engine device: %d\n", rc);
  846. goto err_no_dma;
  847. }
  848. dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  849. return 0;
  850. err_no_dma:
  851. bcm2835_dma_free(od);
  852. return rc;
  853. }
  854. static int bcm2835_dma_remove(struct platform_device *pdev)
  855. {
  856. struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  857. dma_async_device_unregister(&od->ddev);
  858. bcm2835_dma_free(od);
  859. return 0;
  860. }
  861. static struct platform_driver bcm2835_dma_driver = {
  862. .probe = bcm2835_dma_probe,
  863. .remove = bcm2835_dma_remove,
  864. .driver = {
  865. .name = "bcm2835-dma",
  866. .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  867. },
  868. };
  869. module_platform_driver(bcm2835_dma_driver);
  870. MODULE_ALIAS("platform:bcm2835-dma");
  871. MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  872. MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  873. MODULE_LICENSE("GPL v2");