sirf-dma.c 24 KB

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