sirf-dma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * DMA controller driver for CSR SiRFprimaII
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/dmaengine.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/slab.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/sirfsoc_dma.h>
  19. #define SIRFSOC_DMA_DESCRIPTORS 16
  20. #define SIRFSOC_DMA_CHANNELS 16
  21. #define SIRFSOC_DMA_CH_ADDR 0x00
  22. #define SIRFSOC_DMA_CH_XLEN 0x04
  23. #define SIRFSOC_DMA_CH_YLEN 0x08
  24. #define SIRFSOC_DMA_CH_CTRL 0x0C
  25. #define SIRFSOC_DMA_WIDTH_0 0x100
  26. #define SIRFSOC_DMA_CH_VALID 0x140
  27. #define SIRFSOC_DMA_CH_INT 0x144
  28. #define SIRFSOC_DMA_INT_EN 0x148
  29. #define SIRFSOC_DMA_CH_LOOP_CTRL 0x150
  30. #define SIRFSOC_DMA_MODE_CTRL_BIT 4
  31. #define SIRFSOC_DMA_DIR_CTRL_BIT 5
  32. /* xlen and dma_width register is in 4 bytes boundary */
  33. #define SIRFSOC_DMA_WORD_LEN 4
  34. struct sirfsoc_dma_desc {
  35. struct dma_async_tx_descriptor desc;
  36. struct list_head node;
  37. /* SiRFprimaII 2D-DMA parameters */
  38. int xlen; /* DMA xlen */
  39. int ylen; /* DMA ylen */
  40. int width; /* DMA width */
  41. int dir;
  42. bool cyclic; /* is loop DMA? */
  43. u32 addr; /* DMA buffer address */
  44. };
  45. struct sirfsoc_dma_chan {
  46. struct dma_chan chan;
  47. struct list_head free;
  48. struct list_head prepared;
  49. struct list_head queued;
  50. struct list_head active;
  51. struct list_head completed;
  52. dma_cookie_t completed_cookie;
  53. unsigned long happened_cyclic;
  54. unsigned long completed_cyclic;
  55. /* Lock for this structure */
  56. spinlock_t lock;
  57. int mode;
  58. };
  59. struct sirfsoc_dma {
  60. struct dma_device dma;
  61. struct tasklet_struct tasklet;
  62. struct sirfsoc_dma_chan channels[SIRFSOC_DMA_CHANNELS];
  63. void __iomem *base;
  64. int irq;
  65. };
  66. #define DRV_NAME "sirfsoc_dma"
  67. /* Convert struct dma_chan to struct sirfsoc_dma_chan */
  68. static inline
  69. struct sirfsoc_dma_chan *dma_chan_to_sirfsoc_dma_chan(struct dma_chan *c)
  70. {
  71. return container_of(c, struct sirfsoc_dma_chan, chan);
  72. }
  73. /* Convert struct dma_chan to struct sirfsoc_dma */
  74. static inline struct sirfsoc_dma *dma_chan_to_sirfsoc_dma(struct dma_chan *c)
  75. {
  76. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(c);
  77. return container_of(schan, struct sirfsoc_dma, channels[c->chan_id]);
  78. }
  79. /* Execute all queued DMA descriptors */
  80. static void sirfsoc_dma_execute(struct sirfsoc_dma_chan *schan)
  81. {
  82. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  83. int cid = schan->chan.chan_id;
  84. struct sirfsoc_dma_desc *sdesc = NULL;
  85. /*
  86. * lock has been held by functions calling this, so we don't hold
  87. * lock again
  88. */
  89. sdesc = list_first_entry(&schan->queued, struct sirfsoc_dma_desc,
  90. node);
  91. /* Move the first queued descriptor to active list */
  92. list_move_tail(&schan->queued, &schan->active);
  93. /* Start the DMA transfer */
  94. writel_relaxed(sdesc->width, sdma->base + SIRFSOC_DMA_WIDTH_0 +
  95. cid * 4);
  96. writel_relaxed(cid | (schan->mode << SIRFSOC_DMA_MODE_CTRL_BIT) |
  97. (sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT),
  98. sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_CTRL);
  99. writel_relaxed(sdesc->xlen, sdma->base + cid * 0x10 +
  100. SIRFSOC_DMA_CH_XLEN);
  101. writel_relaxed(sdesc->ylen, sdma->base + cid * 0x10 +
  102. SIRFSOC_DMA_CH_YLEN);
  103. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) |
  104. (1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
  105. /*
  106. * writel has an implict memory write barrier to make sure data is
  107. * flushed into memory before starting DMA
  108. */
  109. writel(sdesc->addr >> 2, sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR);
  110. if (sdesc->cyclic) {
  111. writel((1 << cid) | 1 << (cid + 16) |
  112. readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL),
  113. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  114. schan->happened_cyclic = schan->completed_cyclic = 0;
  115. }
  116. }
  117. /* Interrupt handler */
  118. static irqreturn_t sirfsoc_dma_irq(int irq, void *data)
  119. {
  120. struct sirfsoc_dma *sdma = data;
  121. struct sirfsoc_dma_chan *schan;
  122. struct sirfsoc_dma_desc *sdesc = NULL;
  123. u32 is;
  124. int ch;
  125. is = readl(sdma->base + SIRFSOC_DMA_CH_INT);
  126. while ((ch = fls(is) - 1) >= 0) {
  127. is &= ~(1 << ch);
  128. writel_relaxed(1 << ch, sdma->base + SIRFSOC_DMA_CH_INT);
  129. schan = &sdma->channels[ch];
  130. spin_lock(&schan->lock);
  131. sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
  132. node);
  133. if (!sdesc->cyclic) {
  134. /* Execute queued descriptors */
  135. list_splice_tail_init(&schan->active, &schan->completed);
  136. if (!list_empty(&schan->queued))
  137. sirfsoc_dma_execute(schan);
  138. } else
  139. schan->happened_cyclic++;
  140. spin_unlock(&schan->lock);
  141. }
  142. /* Schedule tasklet */
  143. tasklet_schedule(&sdma->tasklet);
  144. return IRQ_HANDLED;
  145. }
  146. /* process completed descriptors */
  147. static void sirfsoc_dma_process_completed(struct sirfsoc_dma *sdma)
  148. {
  149. dma_cookie_t last_cookie = 0;
  150. struct sirfsoc_dma_chan *schan;
  151. struct sirfsoc_dma_desc *sdesc;
  152. struct dma_async_tx_descriptor *desc;
  153. unsigned long flags;
  154. unsigned long happened_cyclic;
  155. LIST_HEAD(list);
  156. int i;
  157. for (i = 0; i < sdma->dma.chancnt; i++) {
  158. schan = &sdma->channels[i];
  159. /* Get all completed descriptors */
  160. spin_lock_irqsave(&schan->lock, flags);
  161. if (!list_empty(&schan->completed)) {
  162. list_splice_tail_init(&schan->completed, &list);
  163. spin_unlock_irqrestore(&schan->lock, flags);
  164. /* Execute callbacks and run dependencies */
  165. list_for_each_entry(sdesc, &list, node) {
  166. desc = &sdesc->desc;
  167. if (desc->callback)
  168. desc->callback(desc->callback_param);
  169. last_cookie = desc->cookie;
  170. dma_run_dependencies(desc);
  171. }
  172. /* Free descriptors */
  173. spin_lock_irqsave(&schan->lock, flags);
  174. list_splice_tail_init(&list, &schan->free);
  175. schan->completed_cookie = last_cookie;
  176. spin_unlock_irqrestore(&schan->lock, flags);
  177. } else {
  178. /* for cyclic channel, desc is always in active list */
  179. sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
  180. node);
  181. if (!sdesc || (sdesc && !sdesc->cyclic)) {
  182. /* without active cyclic DMA */
  183. spin_unlock_irqrestore(&schan->lock, flags);
  184. continue;
  185. }
  186. /* cyclic DMA */
  187. happened_cyclic = schan->happened_cyclic;
  188. spin_unlock_irqrestore(&schan->lock, flags);
  189. desc = &sdesc->desc;
  190. while (happened_cyclic != schan->completed_cyclic) {
  191. if (desc->callback)
  192. desc->callback(desc->callback_param);
  193. schan->completed_cyclic++;
  194. }
  195. }
  196. }
  197. }
  198. /* DMA Tasklet */
  199. static void sirfsoc_dma_tasklet(unsigned long data)
  200. {
  201. struct sirfsoc_dma *sdma = (void *)data;
  202. sirfsoc_dma_process_completed(sdma);
  203. }
  204. /* Submit descriptor to hardware */
  205. static dma_cookie_t sirfsoc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  206. {
  207. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(txd->chan);
  208. struct sirfsoc_dma_desc *sdesc;
  209. unsigned long flags;
  210. dma_cookie_t cookie;
  211. sdesc = container_of(txd, struct sirfsoc_dma_desc, desc);
  212. spin_lock_irqsave(&schan->lock, flags);
  213. /* Move descriptor to queue */
  214. list_move_tail(&sdesc->node, &schan->queued);
  215. /* Update cookie */
  216. cookie = schan->chan.cookie + 1;
  217. if (cookie <= 0)
  218. cookie = 1;
  219. schan->chan.cookie = cookie;
  220. sdesc->desc.cookie = cookie;
  221. spin_unlock_irqrestore(&schan->lock, flags);
  222. return cookie;
  223. }
  224. static int sirfsoc_dma_slave_config(struct sirfsoc_dma_chan *schan,
  225. struct dma_slave_config *config)
  226. {
  227. unsigned long flags;
  228. if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  229. (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
  230. return -EINVAL;
  231. spin_lock_irqsave(&schan->lock, flags);
  232. schan->mode = (config->src_maxburst == 4 ? 1 : 0);
  233. spin_unlock_irqrestore(&schan->lock, flags);
  234. return 0;
  235. }
  236. static int sirfsoc_dma_terminate_all(struct sirfsoc_dma_chan *schan)
  237. {
  238. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  239. int cid = schan->chan.chan_id;
  240. unsigned long flags;
  241. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
  242. ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
  243. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
  244. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
  245. & ~((1 << cid) | 1 << (cid + 16)),
  246. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  247. spin_lock_irqsave(&schan->lock, flags);
  248. list_splice_tail_init(&schan->active, &schan->free);
  249. list_splice_tail_init(&schan->queued, &schan->free);
  250. spin_unlock_irqrestore(&schan->lock, flags);
  251. return 0;
  252. }
  253. static int sirfsoc_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  254. unsigned long arg)
  255. {
  256. struct dma_slave_config *config;
  257. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  258. switch (cmd) {
  259. case DMA_TERMINATE_ALL:
  260. return sirfsoc_dma_terminate_all(schan);
  261. case DMA_SLAVE_CONFIG:
  262. config = (struct dma_slave_config *)arg;
  263. return sirfsoc_dma_slave_config(schan, config);
  264. default:
  265. break;
  266. }
  267. return -ENOSYS;
  268. }
  269. /* Alloc channel resources */
  270. static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
  271. {
  272. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  273. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  274. struct sirfsoc_dma_desc *sdesc;
  275. unsigned long flags;
  276. LIST_HEAD(descs);
  277. int i;
  278. /* Alloc descriptors for this channel */
  279. for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
  280. sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
  281. if (!sdesc) {
  282. dev_notice(sdma->dma.dev, "Memory allocation error. "
  283. "Allocated only %u descriptors\n", i);
  284. break;
  285. }
  286. dma_async_tx_descriptor_init(&sdesc->desc, chan);
  287. sdesc->desc.flags = DMA_CTRL_ACK;
  288. sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
  289. list_add_tail(&sdesc->node, &descs);
  290. }
  291. /* Return error only if no descriptors were allocated */
  292. if (i == 0)
  293. return -ENOMEM;
  294. spin_lock_irqsave(&schan->lock, flags);
  295. list_splice_tail_init(&descs, &schan->free);
  296. spin_unlock_irqrestore(&schan->lock, flags);
  297. return i;
  298. }
  299. /* Free channel resources */
  300. static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
  301. {
  302. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  303. struct sirfsoc_dma_desc *sdesc, *tmp;
  304. unsigned long flags;
  305. LIST_HEAD(descs);
  306. spin_lock_irqsave(&schan->lock, flags);
  307. /* Channel must be idle */
  308. BUG_ON(!list_empty(&schan->prepared));
  309. BUG_ON(!list_empty(&schan->queued));
  310. BUG_ON(!list_empty(&schan->active));
  311. BUG_ON(!list_empty(&schan->completed));
  312. /* Move data */
  313. list_splice_tail_init(&schan->free, &descs);
  314. spin_unlock_irqrestore(&schan->lock, flags);
  315. /* Free descriptors */
  316. list_for_each_entry_safe(sdesc, tmp, &descs, node)
  317. kfree(sdesc);
  318. }
  319. /* Send pending descriptor to hardware */
  320. static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
  321. {
  322. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  323. unsigned long flags;
  324. spin_lock_irqsave(&schan->lock, flags);
  325. if (list_empty(&schan->active) && !list_empty(&schan->queued))
  326. sirfsoc_dma_execute(schan);
  327. spin_unlock_irqrestore(&schan->lock, flags);
  328. }
  329. /* Check request completion status */
  330. static enum dma_status
  331. sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  332. struct dma_tx_state *txstate)
  333. {
  334. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  335. unsigned long flags;
  336. dma_cookie_t last_used;
  337. dma_cookie_t last_complete;
  338. spin_lock_irqsave(&schan->lock, flags);
  339. last_used = schan->chan.cookie;
  340. last_complete = schan->completed_cookie;
  341. spin_unlock_irqrestore(&schan->lock, flags);
  342. dma_set_tx_state(txstate, last_complete, last_used, 0);
  343. return dma_async_is_complete(cookie, last_complete, last_used);
  344. }
  345. static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
  346. struct dma_chan *chan, struct dma_interleaved_template *xt,
  347. unsigned long flags)
  348. {
  349. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  350. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  351. struct sirfsoc_dma_desc *sdesc = NULL;
  352. unsigned long iflags;
  353. int ret;
  354. if ((xt->dir != DMA_MEM_TO_DEV) || (xt->dir != DMA_DEV_TO_MEM)) {
  355. ret = -EINVAL;
  356. goto err_dir;
  357. }
  358. /* Get free descriptor */
  359. spin_lock_irqsave(&schan->lock, iflags);
  360. if (!list_empty(&schan->free)) {
  361. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  362. node);
  363. list_del(&sdesc->node);
  364. }
  365. spin_unlock_irqrestore(&schan->lock, iflags);
  366. if (!sdesc) {
  367. /* try to free completed descriptors */
  368. sirfsoc_dma_process_completed(sdma);
  369. ret = 0;
  370. goto no_desc;
  371. }
  372. /* Place descriptor in prepared list */
  373. spin_lock_irqsave(&schan->lock, iflags);
  374. /*
  375. * Number of chunks in a frame can only be 1 for prima2
  376. * and ylen (number of frame - 1) must be at least 0
  377. */
  378. if ((xt->frame_size == 1) && (xt->numf > 0)) {
  379. sdesc->cyclic = 0;
  380. sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
  381. sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
  382. SIRFSOC_DMA_WORD_LEN;
  383. sdesc->ylen = xt->numf - 1;
  384. if (xt->dir == DMA_MEM_TO_DEV) {
  385. sdesc->addr = xt->src_start;
  386. sdesc->dir = 1;
  387. } else {
  388. sdesc->addr = xt->dst_start;
  389. sdesc->dir = 0;
  390. }
  391. list_add_tail(&sdesc->node, &schan->prepared);
  392. } else {
  393. pr_err("sirfsoc DMA Invalid xfer\n");
  394. ret = -EINVAL;
  395. goto err_xfer;
  396. }
  397. spin_unlock_irqrestore(&schan->lock, iflags);
  398. return &sdesc->desc;
  399. err_xfer:
  400. spin_unlock_irqrestore(&schan->lock, iflags);
  401. no_desc:
  402. err_dir:
  403. return ERR_PTR(ret);
  404. }
  405. static struct dma_async_tx_descriptor *
  406. sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
  407. size_t buf_len, size_t period_len,
  408. enum dma_transfer_direction direction)
  409. {
  410. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  411. struct sirfsoc_dma_desc *sdesc = NULL;
  412. unsigned long iflags;
  413. /*
  414. * we only support cycle transfer with 2 period
  415. * If the X-length is set to 0, it would be the loop mode.
  416. * The DMA address keeps increasing until reaching the end of a loop
  417. * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
  418. * the DMA address goes back to the beginning of this area.
  419. * In loop mode, the DMA data region is divided into two parts, BUFA
  420. * and BUFB. DMA controller generates interrupts twice in each loop:
  421. * when the DMA address reaches the end of BUFA or the end of the
  422. * BUFB
  423. */
  424. if (buf_len != 2 * period_len)
  425. return ERR_PTR(-EINVAL);
  426. /* Get free descriptor */
  427. spin_lock_irqsave(&schan->lock, iflags);
  428. if (!list_empty(&schan->free)) {
  429. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  430. node);
  431. list_del(&sdesc->node);
  432. }
  433. spin_unlock_irqrestore(&schan->lock, iflags);
  434. if (!sdesc)
  435. return 0;
  436. /* Place descriptor in prepared list */
  437. spin_lock_irqsave(&schan->lock, iflags);
  438. sdesc->addr = addr;
  439. sdesc->cyclic = 1;
  440. sdesc->xlen = 0;
  441. sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
  442. sdesc->width = 1;
  443. list_add_tail(&sdesc->node, &schan->prepared);
  444. spin_unlock_irqrestore(&schan->lock, iflags);
  445. return &sdesc->desc;
  446. }
  447. /*
  448. * The DMA controller consists of 16 independent DMA channels.
  449. * Each channel is allocated to a different function
  450. */
  451. bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
  452. {
  453. unsigned int ch_nr = (unsigned int) chan_id;
  454. if (ch_nr == chan->chan_id +
  455. chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
  456. return true;
  457. return false;
  458. }
  459. EXPORT_SYMBOL(sirfsoc_dma_filter_id);
  460. static int __devinit sirfsoc_dma_probe(struct platform_device *op)
  461. {
  462. struct device_node *dn = op->dev.of_node;
  463. struct device *dev = &op->dev;
  464. struct dma_device *dma;
  465. struct sirfsoc_dma *sdma;
  466. struct sirfsoc_dma_chan *schan;
  467. struct resource res;
  468. ulong regs_start, regs_size;
  469. u32 id;
  470. int ret, i;
  471. sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
  472. if (!sdma) {
  473. dev_err(dev, "Memory exhausted!\n");
  474. return -ENOMEM;
  475. }
  476. if (of_property_read_u32(dn, "cell-index", &id)) {
  477. dev_err(dev, "Fail to get DMAC index\n");
  478. ret = -ENODEV;
  479. goto free_mem;
  480. }
  481. sdma->irq = irq_of_parse_and_map(dn, 0);
  482. if (sdma->irq == NO_IRQ) {
  483. dev_err(dev, "Error mapping IRQ!\n");
  484. ret = -EINVAL;
  485. goto free_mem;
  486. }
  487. ret = of_address_to_resource(dn, 0, &res);
  488. if (ret) {
  489. dev_err(dev, "Error parsing memory region!\n");
  490. goto free_mem;
  491. }
  492. regs_start = res.start;
  493. regs_size = resource_size(&res);
  494. sdma->base = devm_ioremap(dev, regs_start, regs_size);
  495. if (!sdma->base) {
  496. dev_err(dev, "Error mapping memory region!\n");
  497. ret = -ENOMEM;
  498. goto irq_dispose;
  499. }
  500. ret = devm_request_irq(dev, sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME,
  501. sdma);
  502. if (ret) {
  503. dev_err(dev, "Error requesting IRQ!\n");
  504. ret = -EINVAL;
  505. goto unmap_mem;
  506. }
  507. dma = &sdma->dma;
  508. dma->dev = dev;
  509. dma->chancnt = SIRFSOC_DMA_CHANNELS;
  510. dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
  511. dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
  512. dma->device_issue_pending = sirfsoc_dma_issue_pending;
  513. dma->device_control = sirfsoc_dma_control;
  514. dma->device_tx_status = sirfsoc_dma_tx_status;
  515. dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
  516. dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
  517. INIT_LIST_HEAD(&dma->channels);
  518. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  519. dma_cap_set(DMA_CYCLIC, dma->cap_mask);
  520. dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
  521. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  522. for (i = 0; i < dma->chancnt; i++) {
  523. schan = &sdma->channels[i];
  524. schan->chan.device = dma;
  525. schan->chan.cookie = 1;
  526. schan->completed_cookie = schan->chan.cookie;
  527. INIT_LIST_HEAD(&schan->free);
  528. INIT_LIST_HEAD(&schan->prepared);
  529. INIT_LIST_HEAD(&schan->queued);
  530. INIT_LIST_HEAD(&schan->active);
  531. INIT_LIST_HEAD(&schan->completed);
  532. spin_lock_init(&schan->lock);
  533. list_add_tail(&schan->chan.device_node, &dma->channels);
  534. }
  535. tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
  536. /* Register DMA engine */
  537. dev_set_drvdata(dev, sdma);
  538. ret = dma_async_device_register(dma);
  539. if (ret)
  540. goto free_irq;
  541. dev_info(dev, "initialized SIRFSOC DMAC driver\n");
  542. return 0;
  543. free_irq:
  544. devm_free_irq(dev, sdma->irq, sdma);
  545. irq_dispose:
  546. irq_dispose_mapping(sdma->irq);
  547. unmap_mem:
  548. iounmap(sdma->base);
  549. free_mem:
  550. devm_kfree(dev, sdma);
  551. return ret;
  552. }
  553. static int __devexit sirfsoc_dma_remove(struct platform_device *op)
  554. {
  555. struct device *dev = &op->dev;
  556. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  557. dma_async_device_unregister(&sdma->dma);
  558. devm_free_irq(dev, sdma->irq, sdma);
  559. irq_dispose_mapping(sdma->irq);
  560. iounmap(sdma->base);
  561. devm_kfree(dev, sdma);
  562. return 0;
  563. }
  564. static struct of_device_id sirfsoc_dma_match[] = {
  565. { .compatible = "sirf,prima2-dmac", },
  566. {},
  567. };
  568. static struct platform_driver sirfsoc_dma_driver = {
  569. .probe = sirfsoc_dma_probe,
  570. .remove = __devexit_p(sirfsoc_dma_remove),
  571. .driver = {
  572. .name = DRV_NAME,
  573. .owner = THIS_MODULE,
  574. .of_match_table = sirfsoc_dma_match,
  575. },
  576. };
  577. module_platform_driver(sirfsoc_dma_driver);
  578. MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
  579. "Barry Song <baohua.song@csr.com>");
  580. MODULE_DESCRIPTION("SIRFSOC DMA control driver");
  581. MODULE_LICENSE("GPL v2");