sirf-dma.c 21 KB

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