bcm2835-dma.c 28 KB

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