sirf-dma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. #include "dmaengine.h"
  20. #define SIRFSOC_DMA_DESCRIPTORS 16
  21. #define SIRFSOC_DMA_CHANNELS 16
  22. #define SIRFSOC_DMA_CH_ADDR 0x00
  23. #define SIRFSOC_DMA_CH_XLEN 0x04
  24. #define SIRFSOC_DMA_CH_YLEN 0x08
  25. #define SIRFSOC_DMA_CH_CTRL 0x0C
  26. #define SIRFSOC_DMA_WIDTH_0 0x100
  27. #define SIRFSOC_DMA_CH_VALID 0x140
  28. #define SIRFSOC_DMA_CH_INT 0x144
  29. #define SIRFSOC_DMA_INT_EN 0x148
  30. #define SIRFSOC_DMA_CH_LOOP_CTRL 0x150
  31. #define SIRFSOC_DMA_MODE_CTRL_BIT 4
  32. #define SIRFSOC_DMA_DIR_CTRL_BIT 5
  33. /* xlen and dma_width register is in 4 bytes boundary */
  34. #define SIRFSOC_DMA_WORD_LEN 4
  35. struct sirfsoc_dma_desc {
  36. struct dma_async_tx_descriptor desc;
  37. struct list_head node;
  38. /* SiRFprimaII 2D-DMA parameters */
  39. int xlen; /* DMA xlen */
  40. int ylen; /* DMA ylen */
  41. int width; /* DMA width */
  42. int dir;
  43. bool cyclic; /* is loop DMA? */
  44. u32 addr; /* DMA buffer address */
  45. };
  46. struct sirfsoc_dma_chan {
  47. struct dma_chan chan;
  48. struct list_head free;
  49. struct list_head prepared;
  50. struct list_head queued;
  51. struct list_head active;
  52. struct list_head completed;
  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(&sdesc->node, &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->chan.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. cookie = dma_cookie_assign(txd);
  216. spin_unlock_irqrestore(&schan->lock, flags);
  217. return cookie;
  218. }
  219. static int sirfsoc_dma_slave_config(struct sirfsoc_dma_chan *schan,
  220. struct dma_slave_config *config)
  221. {
  222. unsigned long flags;
  223. if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  224. (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
  225. return -EINVAL;
  226. spin_lock_irqsave(&schan->lock, flags);
  227. schan->mode = (config->src_maxburst == 4 ? 1 : 0);
  228. spin_unlock_irqrestore(&schan->lock, flags);
  229. return 0;
  230. }
  231. static int sirfsoc_dma_terminate_all(struct sirfsoc_dma_chan *schan)
  232. {
  233. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  234. int cid = schan->chan.chan_id;
  235. unsigned long flags;
  236. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
  237. ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
  238. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
  239. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
  240. & ~((1 << cid) | 1 << (cid + 16)),
  241. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  242. spin_lock_irqsave(&schan->lock, flags);
  243. list_splice_tail_init(&schan->active, &schan->free);
  244. list_splice_tail_init(&schan->queued, &schan->free);
  245. spin_unlock_irqrestore(&schan->lock, flags);
  246. return 0;
  247. }
  248. static int sirfsoc_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  249. unsigned long arg)
  250. {
  251. struct dma_slave_config *config;
  252. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  253. switch (cmd) {
  254. case DMA_TERMINATE_ALL:
  255. return sirfsoc_dma_terminate_all(schan);
  256. case DMA_SLAVE_CONFIG:
  257. config = (struct dma_slave_config *)arg;
  258. return sirfsoc_dma_slave_config(schan, config);
  259. default:
  260. break;
  261. }
  262. return -ENOSYS;
  263. }
  264. /* Alloc channel resources */
  265. static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
  266. {
  267. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  268. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  269. struct sirfsoc_dma_desc *sdesc;
  270. unsigned long flags;
  271. LIST_HEAD(descs);
  272. int i;
  273. /* Alloc descriptors for this channel */
  274. for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
  275. sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
  276. if (!sdesc) {
  277. dev_notice(sdma->dma.dev, "Memory allocation error. "
  278. "Allocated only %u descriptors\n", i);
  279. break;
  280. }
  281. dma_async_tx_descriptor_init(&sdesc->desc, chan);
  282. sdesc->desc.flags = DMA_CTRL_ACK;
  283. sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
  284. list_add_tail(&sdesc->node, &descs);
  285. }
  286. /* Return error only if no descriptors were allocated */
  287. if (i == 0)
  288. return -ENOMEM;
  289. spin_lock_irqsave(&schan->lock, flags);
  290. list_splice_tail_init(&descs, &schan->free);
  291. spin_unlock_irqrestore(&schan->lock, flags);
  292. return i;
  293. }
  294. /* Free channel resources */
  295. static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
  296. {
  297. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  298. struct sirfsoc_dma_desc *sdesc, *tmp;
  299. unsigned long flags;
  300. LIST_HEAD(descs);
  301. spin_lock_irqsave(&schan->lock, flags);
  302. /* Channel must be idle */
  303. BUG_ON(!list_empty(&schan->prepared));
  304. BUG_ON(!list_empty(&schan->queued));
  305. BUG_ON(!list_empty(&schan->active));
  306. BUG_ON(!list_empty(&schan->completed));
  307. /* Move data */
  308. list_splice_tail_init(&schan->free, &descs);
  309. spin_unlock_irqrestore(&schan->lock, flags);
  310. /* Free descriptors */
  311. list_for_each_entry_safe(sdesc, tmp, &descs, node)
  312. kfree(sdesc);
  313. }
  314. /* Send pending descriptor to hardware */
  315. static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
  316. {
  317. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  318. unsigned long flags;
  319. spin_lock_irqsave(&schan->lock, flags);
  320. if (list_empty(&schan->active) && !list_empty(&schan->queued))
  321. sirfsoc_dma_execute(schan);
  322. spin_unlock_irqrestore(&schan->lock, flags);
  323. }
  324. /* Check request completion status */
  325. static enum dma_status
  326. sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  327. struct dma_tx_state *txstate)
  328. {
  329. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  330. unsigned long flags;
  331. enum dma_status ret;
  332. spin_lock_irqsave(&schan->lock, flags);
  333. ret = dma_cookie_status(chan, cookie, txstate);
  334. spin_unlock_irqrestore(&schan->lock, flags);
  335. return ret;
  336. }
  337. static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
  338. struct dma_chan *chan, struct dma_interleaved_template *xt,
  339. unsigned long flags)
  340. {
  341. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  342. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  343. struct sirfsoc_dma_desc *sdesc = NULL;
  344. unsigned long iflags;
  345. int ret;
  346. if ((xt->dir != DMA_MEM_TO_DEV) && (xt->dir != DMA_DEV_TO_MEM)) {
  347. ret = -EINVAL;
  348. goto err_dir;
  349. }
  350. /* Get free descriptor */
  351. spin_lock_irqsave(&schan->lock, iflags);
  352. if (!list_empty(&schan->free)) {
  353. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  354. node);
  355. list_del(&sdesc->node);
  356. }
  357. spin_unlock_irqrestore(&schan->lock, iflags);
  358. if (!sdesc) {
  359. /* try to free completed descriptors */
  360. sirfsoc_dma_process_completed(sdma);
  361. ret = 0;
  362. goto no_desc;
  363. }
  364. /* Place descriptor in prepared list */
  365. spin_lock_irqsave(&schan->lock, iflags);
  366. /*
  367. * Number of chunks in a frame can only be 1 for prima2
  368. * and ylen (number of frame - 1) must be at least 0
  369. */
  370. if ((xt->frame_size == 1) && (xt->numf > 0)) {
  371. sdesc->cyclic = 0;
  372. sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
  373. sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
  374. SIRFSOC_DMA_WORD_LEN;
  375. sdesc->ylen = xt->numf - 1;
  376. if (xt->dir == DMA_MEM_TO_DEV) {
  377. sdesc->addr = xt->src_start;
  378. sdesc->dir = 1;
  379. } else {
  380. sdesc->addr = xt->dst_start;
  381. sdesc->dir = 0;
  382. }
  383. list_add_tail(&sdesc->node, &schan->prepared);
  384. } else {
  385. pr_err("sirfsoc DMA Invalid xfer\n");
  386. ret = -EINVAL;
  387. goto err_xfer;
  388. }
  389. spin_unlock_irqrestore(&schan->lock, iflags);
  390. return &sdesc->desc;
  391. err_xfer:
  392. spin_unlock_irqrestore(&schan->lock, iflags);
  393. no_desc:
  394. err_dir:
  395. return ERR_PTR(ret);
  396. }
  397. static struct dma_async_tx_descriptor *
  398. sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
  399. size_t buf_len, size_t period_len,
  400. enum dma_transfer_direction direction, void *context)
  401. {
  402. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  403. struct sirfsoc_dma_desc *sdesc = NULL;
  404. unsigned long iflags;
  405. /*
  406. * we only support cycle transfer with 2 period
  407. * If the X-length is set to 0, it would be the loop mode.
  408. * The DMA address keeps increasing until reaching the end of a loop
  409. * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
  410. * the DMA address goes back to the beginning of this area.
  411. * In loop mode, the DMA data region is divided into two parts, BUFA
  412. * and BUFB. DMA controller generates interrupts twice in each loop:
  413. * when the DMA address reaches the end of BUFA or the end of the
  414. * BUFB
  415. */
  416. if (buf_len != 2 * period_len)
  417. return ERR_PTR(-EINVAL);
  418. /* Get free descriptor */
  419. spin_lock_irqsave(&schan->lock, iflags);
  420. if (!list_empty(&schan->free)) {
  421. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  422. node);
  423. list_del(&sdesc->node);
  424. }
  425. spin_unlock_irqrestore(&schan->lock, iflags);
  426. if (!sdesc)
  427. return 0;
  428. /* Place descriptor in prepared list */
  429. spin_lock_irqsave(&schan->lock, iflags);
  430. sdesc->addr = addr;
  431. sdesc->cyclic = 1;
  432. sdesc->xlen = 0;
  433. sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
  434. sdesc->width = 1;
  435. list_add_tail(&sdesc->node, &schan->prepared);
  436. spin_unlock_irqrestore(&schan->lock, iflags);
  437. return &sdesc->desc;
  438. }
  439. /*
  440. * The DMA controller consists of 16 independent DMA channels.
  441. * Each channel is allocated to a different function
  442. */
  443. bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
  444. {
  445. unsigned int ch_nr = (unsigned int) chan_id;
  446. if (ch_nr == chan->chan_id +
  447. chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
  448. return true;
  449. return false;
  450. }
  451. EXPORT_SYMBOL(sirfsoc_dma_filter_id);
  452. static int __devinit sirfsoc_dma_probe(struct platform_device *op)
  453. {
  454. struct device_node *dn = op->dev.of_node;
  455. struct device *dev = &op->dev;
  456. struct dma_device *dma;
  457. struct sirfsoc_dma *sdma;
  458. struct sirfsoc_dma_chan *schan;
  459. struct resource res;
  460. ulong regs_start, regs_size;
  461. u32 id;
  462. int ret, i;
  463. sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
  464. if (!sdma) {
  465. dev_err(dev, "Memory exhausted!\n");
  466. return -ENOMEM;
  467. }
  468. if (of_property_read_u32(dn, "cell-index", &id)) {
  469. dev_err(dev, "Fail to get DMAC index\n");
  470. ret = -ENODEV;
  471. goto free_mem;
  472. }
  473. sdma->irq = irq_of_parse_and_map(dn, 0);
  474. if (sdma->irq == NO_IRQ) {
  475. dev_err(dev, "Error mapping IRQ!\n");
  476. ret = -EINVAL;
  477. goto free_mem;
  478. }
  479. ret = of_address_to_resource(dn, 0, &res);
  480. if (ret) {
  481. dev_err(dev, "Error parsing memory region!\n");
  482. goto free_mem;
  483. }
  484. regs_start = res.start;
  485. regs_size = resource_size(&res);
  486. sdma->base = devm_ioremap(dev, regs_start, regs_size);
  487. if (!sdma->base) {
  488. dev_err(dev, "Error mapping memory region!\n");
  489. ret = -ENOMEM;
  490. goto irq_dispose;
  491. }
  492. ret = devm_request_irq(dev, sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME,
  493. sdma);
  494. if (ret) {
  495. dev_err(dev, "Error requesting IRQ!\n");
  496. ret = -EINVAL;
  497. goto unmap_mem;
  498. }
  499. dma = &sdma->dma;
  500. dma->dev = dev;
  501. dma->chancnt = SIRFSOC_DMA_CHANNELS;
  502. dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
  503. dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
  504. dma->device_issue_pending = sirfsoc_dma_issue_pending;
  505. dma->device_control = sirfsoc_dma_control;
  506. dma->device_tx_status = sirfsoc_dma_tx_status;
  507. dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
  508. dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
  509. INIT_LIST_HEAD(&dma->channels);
  510. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  511. dma_cap_set(DMA_CYCLIC, dma->cap_mask);
  512. dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
  513. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  514. for (i = 0; i < dma->chancnt; i++) {
  515. schan = &sdma->channels[i];
  516. schan->chan.device = dma;
  517. dma_cookie_init(&schan->chan);
  518. INIT_LIST_HEAD(&schan->free);
  519. INIT_LIST_HEAD(&schan->prepared);
  520. INIT_LIST_HEAD(&schan->queued);
  521. INIT_LIST_HEAD(&schan->active);
  522. INIT_LIST_HEAD(&schan->completed);
  523. spin_lock_init(&schan->lock);
  524. list_add_tail(&schan->chan.device_node, &dma->channels);
  525. }
  526. tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
  527. /* Register DMA engine */
  528. dev_set_drvdata(dev, sdma);
  529. ret = dma_async_device_register(dma);
  530. if (ret)
  531. goto free_irq;
  532. dev_info(dev, "initialized SIRFSOC DMAC driver\n");
  533. return 0;
  534. free_irq:
  535. devm_free_irq(dev, sdma->irq, sdma);
  536. irq_dispose:
  537. irq_dispose_mapping(sdma->irq);
  538. unmap_mem:
  539. iounmap(sdma->base);
  540. free_mem:
  541. devm_kfree(dev, sdma);
  542. return ret;
  543. }
  544. static int __devexit sirfsoc_dma_remove(struct platform_device *op)
  545. {
  546. struct device *dev = &op->dev;
  547. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  548. dma_async_device_unregister(&sdma->dma);
  549. devm_free_irq(dev, sdma->irq, sdma);
  550. irq_dispose_mapping(sdma->irq);
  551. iounmap(sdma->base);
  552. devm_kfree(dev, sdma);
  553. return 0;
  554. }
  555. static struct of_device_id sirfsoc_dma_match[] = {
  556. { .compatible = "sirf,prima2-dmac", },
  557. {},
  558. };
  559. static struct platform_driver sirfsoc_dma_driver = {
  560. .probe = sirfsoc_dma_probe,
  561. .remove = __devexit_p(sirfsoc_dma_remove),
  562. .driver = {
  563. .name = DRV_NAME,
  564. .owner = THIS_MODULE,
  565. .of_match_table = sirfsoc_dma_match,
  566. },
  567. };
  568. module_platform_driver(sirfsoc_dma_driver);
  569. MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
  570. "Barry Song <baohua.song@csr.com>");
  571. MODULE_DESCRIPTION("SIRFSOC DMA control driver");
  572. MODULE_LICENSE("GPL v2");