sirf-dma.c 25 KB

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