bcm2835-dma.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. struct bcm2835_dmadev {
  48. struct dma_device ddev;
  49. spinlock_t lock;
  50. void __iomem *base;
  51. struct device_dma_parameters dma_parms;
  52. };
  53. struct bcm2835_dma_cb {
  54. uint32_t info;
  55. uint32_t src;
  56. uint32_t dst;
  57. uint32_t length;
  58. uint32_t stride;
  59. uint32_t next;
  60. uint32_t pad[2];
  61. };
  62. struct bcm2835_cb_entry {
  63. struct bcm2835_dma_cb *cb;
  64. dma_addr_t paddr;
  65. };
  66. struct bcm2835_chan {
  67. struct virt_dma_chan vc;
  68. struct list_head node;
  69. struct dma_slave_config cfg;
  70. bool cyclic;
  71. unsigned int dreq;
  72. int ch;
  73. struct bcm2835_desc *desc;
  74. struct dma_pool *cb_pool;
  75. void __iomem *chan_base;
  76. int irq_number;
  77. };
  78. struct bcm2835_desc {
  79. struct bcm2835_chan *c;
  80. struct virt_dma_desc vd;
  81. enum dma_transfer_direction dir;
  82. struct bcm2835_cb_entry *cb_list;
  83. unsigned int frames;
  84. size_t size;
  85. };
  86. #define BCM2835_DMA_CS 0x00
  87. #define BCM2835_DMA_ADDR 0x04
  88. #define BCM2835_DMA_SOURCE_AD 0x0c
  89. #define BCM2835_DMA_DEST_AD 0x10
  90. #define BCM2835_DMA_NEXTCB 0x1C
  91. /* DMA CS Control and Status bits */
  92. #define BCM2835_DMA_ACTIVE BIT(0)
  93. #define BCM2835_DMA_INT BIT(2)
  94. #define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
  95. #define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
  96. #define BCM2835_DMA_ERR BIT(8)
  97. #define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
  98. #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
  99. #define BCM2835_DMA_INT_EN BIT(0)
  100. #define BCM2835_DMA_D_INC BIT(4)
  101. #define BCM2835_DMA_D_DREQ BIT(6)
  102. #define BCM2835_DMA_S_INC BIT(8)
  103. #define BCM2835_DMA_S_DREQ BIT(10)
  104. #define BCM2835_DMA_PER_MAP(x) ((x) << 16)
  105. #define BCM2835_DMA_DATA_TYPE_S8 1
  106. #define BCM2835_DMA_DATA_TYPE_S16 2
  107. #define BCM2835_DMA_DATA_TYPE_S32 4
  108. #define BCM2835_DMA_DATA_TYPE_S128 16
  109. #define BCM2835_DMA_BULK_MASK BIT(0)
  110. #define BCM2835_DMA_FIQ_MASK (BIT(2) | BIT(3))
  111. /* Valid only for channels 0 - 14, 15 has its own base address */
  112. #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
  113. #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
  114. static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
  115. {
  116. return container_of(d, struct bcm2835_dmadev, ddev);
  117. }
  118. static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
  119. {
  120. return container_of(c, struct bcm2835_chan, vc.chan);
  121. }
  122. static inline struct bcm2835_desc *to_bcm2835_dma_desc(
  123. struct dma_async_tx_descriptor *t)
  124. {
  125. return container_of(t, struct bcm2835_desc, vd.tx);
  126. }
  127. static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
  128. {
  129. struct bcm2835_desc *desc = container_of(vd, struct bcm2835_desc, vd);
  130. int i;
  131. for (i = 0; i < desc->frames; i++)
  132. dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
  133. desc->cb_list[i].paddr);
  134. kfree(desc->cb_list);
  135. kfree(desc);
  136. }
  137. static int bcm2835_dma_abort(void __iomem *chan_base)
  138. {
  139. unsigned long cs;
  140. long int timeout = 10000;
  141. cs = readl(chan_base + BCM2835_DMA_CS);
  142. if (!(cs & BCM2835_DMA_ACTIVE))
  143. return 0;
  144. /* Write 0 to the active bit - Pause the DMA */
  145. writel(0, chan_base + BCM2835_DMA_CS);
  146. /* Wait for any current AXI transfer to complete */
  147. while ((cs & BCM2835_DMA_ISPAUSED) && --timeout) {
  148. cpu_relax();
  149. cs = readl(chan_base + BCM2835_DMA_CS);
  150. }
  151. /* We'll un-pause when we set of our next DMA */
  152. if (!timeout)
  153. return -ETIMEDOUT;
  154. if (!(cs & BCM2835_DMA_ACTIVE))
  155. return 0;
  156. /* Terminate the control block chain */
  157. writel(0, chan_base + BCM2835_DMA_NEXTCB);
  158. /* Abort the whole DMA */
  159. writel(BCM2835_DMA_ABORT | BCM2835_DMA_ACTIVE,
  160. chan_base + BCM2835_DMA_CS);
  161. return 0;
  162. }
  163. static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
  164. {
  165. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  166. struct bcm2835_desc *d;
  167. if (!vd) {
  168. c->desc = NULL;
  169. return;
  170. }
  171. list_del(&vd->node);
  172. c->desc = d = to_bcm2835_dma_desc(&vd->tx);
  173. writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
  174. writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  175. }
  176. static irqreturn_t bcm2835_dma_callback(int irq, void *data)
  177. {
  178. struct bcm2835_chan *c = data;
  179. struct bcm2835_desc *d;
  180. unsigned long flags;
  181. spin_lock_irqsave(&c->vc.lock, flags);
  182. /* Acknowledge interrupt */
  183. writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
  184. d = c->desc;
  185. if (d) {
  186. /* TODO Only works for cyclic DMA */
  187. vchan_cyclic_callback(&d->vd);
  188. }
  189. /* Keep the DMA engine running */
  190. writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  191. spin_unlock_irqrestore(&c->vc.lock, flags);
  192. return IRQ_HANDLED;
  193. }
  194. static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
  195. {
  196. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  197. struct device *dev = c->vc.chan.device->dev;
  198. dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
  199. c->cb_pool = dma_pool_create(dev_name(dev), dev,
  200. sizeof(struct bcm2835_dma_cb), 0, 0);
  201. if (!c->cb_pool) {
  202. dev_err(dev, "unable to allocate descriptor pool\n");
  203. return -ENOMEM;
  204. }
  205. return request_irq(c->irq_number,
  206. bcm2835_dma_callback, 0, "DMA IRQ", c);
  207. }
  208. static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
  209. {
  210. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  211. vchan_free_chan_resources(&c->vc);
  212. free_irq(c->irq_number, c);
  213. dma_pool_destroy(c->cb_pool);
  214. dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  215. }
  216. static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
  217. {
  218. return d->size;
  219. }
  220. static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
  221. {
  222. unsigned int i;
  223. size_t size;
  224. for (size = i = 0; i < d->frames; i++) {
  225. struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
  226. size_t this_size = control_block->length;
  227. dma_addr_t dma;
  228. if (d->dir == DMA_DEV_TO_MEM)
  229. dma = control_block->dst;
  230. else
  231. dma = control_block->src;
  232. if (size)
  233. size += this_size;
  234. else if (addr >= dma && addr < dma + this_size)
  235. size += dma + this_size - addr;
  236. }
  237. return size;
  238. }
  239. static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
  240. dma_cookie_t cookie, struct dma_tx_state *txstate)
  241. {
  242. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  243. struct virt_dma_desc *vd;
  244. enum dma_status ret;
  245. unsigned long flags;
  246. ret = dma_cookie_status(chan, cookie, txstate);
  247. if (ret == DMA_COMPLETE || !txstate)
  248. return ret;
  249. spin_lock_irqsave(&c->vc.lock, flags);
  250. vd = vchan_find_desc(&c->vc, cookie);
  251. if (vd) {
  252. txstate->residue =
  253. bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
  254. } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  255. struct bcm2835_desc *d = c->desc;
  256. dma_addr_t pos;
  257. if (d->dir == DMA_MEM_TO_DEV)
  258. pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
  259. else if (d->dir == DMA_DEV_TO_MEM)
  260. pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
  261. else
  262. pos = 0;
  263. txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
  264. } else {
  265. txstate->residue = 0;
  266. }
  267. spin_unlock_irqrestore(&c->vc.lock, flags);
  268. return ret;
  269. }
  270. static void bcm2835_dma_issue_pending(struct dma_chan *chan)
  271. {
  272. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  273. unsigned long flags;
  274. c->cyclic = true; /* Nothing else is implemented */
  275. spin_lock_irqsave(&c->vc.lock, flags);
  276. if (vchan_issue_pending(&c->vc) && !c->desc)
  277. bcm2835_dma_start_desc(c);
  278. spin_unlock_irqrestore(&c->vc.lock, flags);
  279. }
  280. static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
  281. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  282. size_t period_len, enum dma_transfer_direction direction,
  283. unsigned long flags)
  284. {
  285. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  286. enum dma_slave_buswidth dev_width;
  287. struct bcm2835_desc *d;
  288. dma_addr_t dev_addr;
  289. unsigned int es, sync_type;
  290. unsigned int frame;
  291. int i;
  292. /* Grab configuration */
  293. if (!is_slave_direction(direction)) {
  294. dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  295. return NULL;
  296. }
  297. if (direction == DMA_DEV_TO_MEM) {
  298. dev_addr = c->cfg.src_addr;
  299. dev_width = c->cfg.src_addr_width;
  300. sync_type = BCM2835_DMA_S_DREQ;
  301. } else {
  302. dev_addr = c->cfg.dst_addr;
  303. dev_width = c->cfg.dst_addr_width;
  304. sync_type = BCM2835_DMA_D_DREQ;
  305. }
  306. /* Bus width translates to the element size (ES) */
  307. switch (dev_width) {
  308. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  309. es = BCM2835_DMA_DATA_TYPE_S32;
  310. break;
  311. default:
  312. return NULL;
  313. }
  314. /* Now allocate and setup the descriptor. */
  315. d = kzalloc(sizeof(*d), GFP_NOWAIT);
  316. if (!d)
  317. return NULL;
  318. d->c = c;
  319. d->dir = direction;
  320. d->frames = buf_len / period_len;
  321. d->cb_list = kcalloc(d->frames, sizeof(*d->cb_list), GFP_KERNEL);
  322. if (!d->cb_list) {
  323. kfree(d);
  324. return NULL;
  325. }
  326. /* Allocate memory for control blocks */
  327. for (i = 0; i < d->frames; i++) {
  328. struct bcm2835_cb_entry *cb_entry = &d->cb_list[i];
  329. cb_entry->cb = dma_pool_zalloc(c->cb_pool, GFP_ATOMIC,
  330. &cb_entry->paddr);
  331. if (!cb_entry->cb)
  332. goto error_cb;
  333. }
  334. /*
  335. * Iterate over all frames, create a control block
  336. * for each frame and link them together.
  337. */
  338. for (frame = 0; frame < d->frames; frame++) {
  339. struct bcm2835_dma_cb *control_block = d->cb_list[frame].cb;
  340. /* Setup adresses */
  341. if (d->dir == DMA_DEV_TO_MEM) {
  342. control_block->info = BCM2835_DMA_D_INC;
  343. control_block->src = dev_addr;
  344. control_block->dst = buf_addr + frame * period_len;
  345. } else {
  346. control_block->info = BCM2835_DMA_S_INC;
  347. control_block->src = buf_addr + frame * period_len;
  348. control_block->dst = dev_addr;
  349. }
  350. /* Enable interrupt */
  351. control_block->info |= BCM2835_DMA_INT_EN;
  352. /* Setup synchronization */
  353. if (sync_type != 0)
  354. control_block->info |= sync_type;
  355. /* Setup DREQ channel */
  356. if (c->dreq != 0)
  357. control_block->info |=
  358. BCM2835_DMA_PER_MAP(c->dreq);
  359. /* Length of a frame */
  360. control_block->length = period_len;
  361. d->size += control_block->length;
  362. /*
  363. * Next block is the next frame.
  364. * This DMA engine driver currently only supports cyclic DMA.
  365. * Therefore, wrap around at number of frames.
  366. */
  367. control_block->next = d->cb_list[((frame + 1) % d->frames)].paddr;
  368. }
  369. return vchan_tx_prep(&c->vc, &d->vd, flags);
  370. error_cb:
  371. i--;
  372. for (; i >= 0; i--) {
  373. struct bcm2835_cb_entry *cb_entry = &d->cb_list[i];
  374. dma_pool_free(c->cb_pool, cb_entry->cb, cb_entry->paddr);
  375. }
  376. kfree(d->cb_list);
  377. kfree(d);
  378. return NULL;
  379. }
  380. static int bcm2835_dma_slave_config(struct dma_chan *chan,
  381. struct dma_slave_config *cfg)
  382. {
  383. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  384. if ((cfg->direction == DMA_DEV_TO_MEM &&
  385. cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  386. (cfg->direction == DMA_MEM_TO_DEV &&
  387. cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  388. !is_slave_direction(cfg->direction)) {
  389. return -EINVAL;
  390. }
  391. c->cfg = *cfg;
  392. return 0;
  393. }
  394. static int bcm2835_dma_terminate_all(struct dma_chan *chan)
  395. {
  396. struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  397. struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
  398. unsigned long flags;
  399. int timeout = 10000;
  400. LIST_HEAD(head);
  401. spin_lock_irqsave(&c->vc.lock, flags);
  402. /* Prevent this channel being scheduled */
  403. spin_lock(&d->lock);
  404. list_del_init(&c->node);
  405. spin_unlock(&d->lock);
  406. /*
  407. * Stop DMA activity: we assume the callback will not be called
  408. * after bcm_dma_abort() returns (even if it does, it will see
  409. * c->desc is NULL and exit.)
  410. */
  411. if (c->desc) {
  412. bcm2835_dma_desc_free(&c->desc->vd);
  413. c->desc = NULL;
  414. bcm2835_dma_abort(c->chan_base);
  415. /* Wait for stopping */
  416. while (--timeout) {
  417. if (!(readl(c->chan_base + BCM2835_DMA_CS) &
  418. BCM2835_DMA_ACTIVE))
  419. break;
  420. cpu_relax();
  421. }
  422. if (!timeout)
  423. dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  424. }
  425. vchan_get_all_descriptors(&c->vc, &head);
  426. spin_unlock_irqrestore(&c->vc.lock, flags);
  427. vchan_dma_desc_free_list(&c->vc, &head);
  428. return 0;
  429. }
  430. static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id, int irq)
  431. {
  432. struct bcm2835_chan *c;
  433. c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  434. if (!c)
  435. return -ENOMEM;
  436. c->vc.desc_free = bcm2835_dma_desc_free;
  437. vchan_init(&c->vc, &d->ddev);
  438. INIT_LIST_HEAD(&c->node);
  439. c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  440. c->ch = chan_id;
  441. c->irq_number = irq;
  442. return 0;
  443. }
  444. static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  445. {
  446. struct bcm2835_chan *c, *next;
  447. list_for_each_entry_safe(c, next, &od->ddev.channels,
  448. vc.chan.device_node) {
  449. list_del(&c->vc.chan.device_node);
  450. tasklet_kill(&c->vc.task);
  451. }
  452. }
  453. static const struct of_device_id bcm2835_dma_of_match[] = {
  454. { .compatible = "brcm,bcm2835-dma", },
  455. {},
  456. };
  457. MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  458. static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  459. struct of_dma *ofdma)
  460. {
  461. struct bcm2835_dmadev *d = ofdma->of_dma_data;
  462. struct dma_chan *chan;
  463. chan = dma_get_any_slave_channel(&d->ddev);
  464. if (!chan)
  465. return NULL;
  466. /* Set DREQ from param */
  467. to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  468. return chan;
  469. }
  470. static int bcm2835_dma_probe(struct platform_device *pdev)
  471. {
  472. struct bcm2835_dmadev *od;
  473. struct resource *res;
  474. void __iomem *base;
  475. int rc;
  476. int i;
  477. int irq;
  478. uint32_t chans_available;
  479. if (!pdev->dev.dma_mask)
  480. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  481. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  482. if (rc)
  483. return rc;
  484. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  485. if (!od)
  486. return -ENOMEM;
  487. pdev->dev.dma_parms = &od->dma_parms;
  488. dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  489. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  490. base = devm_ioremap_resource(&pdev->dev, res);
  491. if (IS_ERR(base))
  492. return PTR_ERR(base);
  493. od->base = base;
  494. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  495. dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  496. dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  497. od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  498. od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  499. od->ddev.device_tx_status = bcm2835_dma_tx_status;
  500. od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  501. od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  502. od->ddev.device_config = bcm2835_dma_slave_config;
  503. od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
  504. od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  505. od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  506. od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  507. od->ddev.dev = &pdev->dev;
  508. INIT_LIST_HEAD(&od->ddev.channels);
  509. spin_lock_init(&od->lock);
  510. platform_set_drvdata(pdev, od);
  511. /* Request DMA channel mask from device tree */
  512. if (of_property_read_u32(pdev->dev.of_node,
  513. "brcm,dma-channel-mask",
  514. &chans_available)) {
  515. dev_err(&pdev->dev, "Failed to get channel mask\n");
  516. rc = -EINVAL;
  517. goto err_no_dma;
  518. }
  519. /*
  520. * Do not use the FIQ and BULK channels,
  521. * because they are used by the GPU.
  522. */
  523. chans_available &= ~(BCM2835_DMA_FIQ_MASK | BCM2835_DMA_BULK_MASK);
  524. for (i = 0; i < pdev->num_resources; i++) {
  525. irq = platform_get_irq(pdev, i);
  526. if (irq < 0)
  527. break;
  528. if (chans_available & (1 << i)) {
  529. rc = bcm2835_dma_chan_init(od, i, irq);
  530. if (rc)
  531. goto err_no_dma;
  532. }
  533. }
  534. dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  535. /* Device-tree DMA controller registration */
  536. rc = of_dma_controller_register(pdev->dev.of_node,
  537. bcm2835_dma_xlate, od);
  538. if (rc) {
  539. dev_err(&pdev->dev, "Failed to register DMA controller\n");
  540. goto err_no_dma;
  541. }
  542. rc = dma_async_device_register(&od->ddev);
  543. if (rc) {
  544. dev_err(&pdev->dev,
  545. "Failed to register slave DMA engine device: %d\n", rc);
  546. goto err_no_dma;
  547. }
  548. dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  549. return 0;
  550. err_no_dma:
  551. bcm2835_dma_free(od);
  552. return rc;
  553. }
  554. static int bcm2835_dma_remove(struct platform_device *pdev)
  555. {
  556. struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  557. dma_async_device_unregister(&od->ddev);
  558. bcm2835_dma_free(od);
  559. return 0;
  560. }
  561. static struct platform_driver bcm2835_dma_driver = {
  562. .probe = bcm2835_dma_probe,
  563. .remove = bcm2835_dma_remove,
  564. .driver = {
  565. .name = "bcm2835-dma",
  566. .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  567. },
  568. };
  569. module_platform_driver(bcm2835_dma_driver);
  570. MODULE_ALIAS("platform:bcm2835-dma");
  571. MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  572. MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  573. MODULE_LICENSE("GPL v2");