sirf-dma.c 20 KB

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