sirf-dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 sirfsoc_dma_chan *schan,
  233. struct dma_slave_config *config)
  234. {
  235. unsigned long flags;
  236. if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  237. (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
  238. return -EINVAL;
  239. spin_lock_irqsave(&schan->lock, flags);
  240. schan->mode = (config->src_maxburst == 4 ? 1 : 0);
  241. spin_unlock_irqrestore(&schan->lock, flags);
  242. return 0;
  243. }
  244. static int sirfsoc_dma_terminate_all(struct sirfsoc_dma_chan *schan)
  245. {
  246. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  247. int cid = schan->chan.chan_id;
  248. unsigned long flags;
  249. spin_lock_irqsave(&schan->lock, flags);
  250. if (!sdma->is_marco) {
  251. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
  252. ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
  253. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
  254. & ~((1 << cid) | 1 << (cid + 16)),
  255. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  256. } else {
  257. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_INT_EN_CLR);
  258. writel_relaxed((1 << cid) | 1 << (cid + 16),
  259. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_CLR);
  260. }
  261. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
  262. list_splice_tail_init(&schan->active, &schan->free);
  263. list_splice_tail_init(&schan->queued, &schan->free);
  264. spin_unlock_irqrestore(&schan->lock, flags);
  265. return 0;
  266. }
  267. static int sirfsoc_dma_pause_chan(struct sirfsoc_dma_chan *schan)
  268. {
  269. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  270. int cid = schan->chan.chan_id;
  271. unsigned long flags;
  272. spin_lock_irqsave(&schan->lock, flags);
  273. if (!sdma->is_marco)
  274. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
  275. & ~((1 << cid) | 1 << (cid + 16)),
  276. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  277. else
  278. writel_relaxed((1 << cid) | 1 << (cid + 16),
  279. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_CLR);
  280. spin_unlock_irqrestore(&schan->lock, flags);
  281. return 0;
  282. }
  283. static int sirfsoc_dma_resume_chan(struct sirfsoc_dma_chan *schan)
  284. {
  285. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  286. int cid = schan->chan.chan_id;
  287. unsigned long flags;
  288. spin_lock_irqsave(&schan->lock, flags);
  289. if (!sdma->is_marco)
  290. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL)
  291. | ((1 << cid) | 1 << (cid + 16)),
  292. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  293. else
  294. writel_relaxed((1 << cid) | 1 << (cid + 16),
  295. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  296. spin_unlock_irqrestore(&schan->lock, flags);
  297. return 0;
  298. }
  299. static int sirfsoc_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  300. unsigned long arg)
  301. {
  302. struct dma_slave_config *config;
  303. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  304. switch (cmd) {
  305. case DMA_PAUSE:
  306. return sirfsoc_dma_pause_chan(schan);
  307. case DMA_RESUME:
  308. return sirfsoc_dma_resume_chan(schan);
  309. case DMA_TERMINATE_ALL:
  310. return sirfsoc_dma_terminate_all(schan);
  311. case DMA_SLAVE_CONFIG:
  312. config = (struct dma_slave_config *)arg;
  313. return sirfsoc_dma_slave_config(schan, config);
  314. default:
  315. break;
  316. }
  317. return -ENOSYS;
  318. }
  319. /* Alloc channel resources */
  320. static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
  321. {
  322. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  323. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  324. struct sirfsoc_dma_desc *sdesc;
  325. unsigned long flags;
  326. LIST_HEAD(descs);
  327. int i;
  328. pm_runtime_get_sync(sdma->dma.dev);
  329. /* Alloc descriptors for this channel */
  330. for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
  331. sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
  332. if (!sdesc) {
  333. dev_notice(sdma->dma.dev, "Memory allocation error. "
  334. "Allocated only %u descriptors\n", i);
  335. break;
  336. }
  337. dma_async_tx_descriptor_init(&sdesc->desc, chan);
  338. sdesc->desc.flags = DMA_CTRL_ACK;
  339. sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
  340. list_add_tail(&sdesc->node, &descs);
  341. }
  342. /* Return error only if no descriptors were allocated */
  343. if (i == 0)
  344. return -ENOMEM;
  345. spin_lock_irqsave(&schan->lock, flags);
  346. list_splice_tail_init(&descs, &schan->free);
  347. spin_unlock_irqrestore(&schan->lock, flags);
  348. return i;
  349. }
  350. /* Free channel resources */
  351. static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
  352. {
  353. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  354. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  355. struct sirfsoc_dma_desc *sdesc, *tmp;
  356. unsigned long flags;
  357. LIST_HEAD(descs);
  358. spin_lock_irqsave(&schan->lock, flags);
  359. /* Channel must be idle */
  360. BUG_ON(!list_empty(&schan->prepared));
  361. BUG_ON(!list_empty(&schan->queued));
  362. BUG_ON(!list_empty(&schan->active));
  363. BUG_ON(!list_empty(&schan->completed));
  364. /* Move data */
  365. list_splice_tail_init(&schan->free, &descs);
  366. spin_unlock_irqrestore(&schan->lock, flags);
  367. /* Free descriptors */
  368. list_for_each_entry_safe(sdesc, tmp, &descs, node)
  369. kfree(sdesc);
  370. pm_runtime_put(sdma->dma.dev);
  371. }
  372. /* Send pending descriptor to hardware */
  373. static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
  374. {
  375. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  376. unsigned long flags;
  377. spin_lock_irqsave(&schan->lock, flags);
  378. if (list_empty(&schan->active) && !list_empty(&schan->queued))
  379. sirfsoc_dma_execute(schan);
  380. spin_unlock_irqrestore(&schan->lock, flags);
  381. }
  382. /* Check request completion status */
  383. static enum dma_status
  384. sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  385. struct dma_tx_state *txstate)
  386. {
  387. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  388. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  389. unsigned long flags;
  390. enum dma_status ret;
  391. struct sirfsoc_dma_desc *sdesc;
  392. int cid = schan->chan.chan_id;
  393. unsigned long dma_pos;
  394. unsigned long dma_request_bytes;
  395. unsigned long residue;
  396. spin_lock_irqsave(&schan->lock, flags);
  397. sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc,
  398. node);
  399. dma_request_bytes = (sdesc->xlen + 1) * (sdesc->ylen + 1) *
  400. (sdesc->width * SIRFSOC_DMA_WORD_LEN);
  401. ret = dma_cookie_status(chan, cookie, txstate);
  402. dma_pos = readl_relaxed(sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR)
  403. << 2;
  404. residue = dma_request_bytes - (dma_pos - sdesc->addr);
  405. dma_set_residue(txstate, residue);
  406. spin_unlock_irqrestore(&schan->lock, flags);
  407. return ret;
  408. }
  409. static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
  410. struct dma_chan *chan, struct dma_interleaved_template *xt,
  411. unsigned long flags)
  412. {
  413. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  414. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  415. struct sirfsoc_dma_desc *sdesc = NULL;
  416. unsigned long iflags;
  417. int ret;
  418. if ((xt->dir != DMA_MEM_TO_DEV) && (xt->dir != DMA_DEV_TO_MEM)) {
  419. ret = -EINVAL;
  420. goto err_dir;
  421. }
  422. /* Get free descriptor */
  423. spin_lock_irqsave(&schan->lock, iflags);
  424. if (!list_empty(&schan->free)) {
  425. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  426. node);
  427. list_del(&sdesc->node);
  428. }
  429. spin_unlock_irqrestore(&schan->lock, iflags);
  430. if (!sdesc) {
  431. /* try to free completed descriptors */
  432. sirfsoc_dma_process_completed(sdma);
  433. ret = 0;
  434. goto no_desc;
  435. }
  436. /* Place descriptor in prepared list */
  437. spin_lock_irqsave(&schan->lock, iflags);
  438. /*
  439. * Number of chunks in a frame can only be 1 for prima2
  440. * and ylen (number of frame - 1) must be at least 0
  441. */
  442. if ((xt->frame_size == 1) && (xt->numf > 0)) {
  443. sdesc->cyclic = 0;
  444. sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
  445. sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
  446. SIRFSOC_DMA_WORD_LEN;
  447. sdesc->ylen = xt->numf - 1;
  448. if (xt->dir == DMA_MEM_TO_DEV) {
  449. sdesc->addr = xt->src_start;
  450. sdesc->dir = 1;
  451. } else {
  452. sdesc->addr = xt->dst_start;
  453. sdesc->dir = 0;
  454. }
  455. list_add_tail(&sdesc->node, &schan->prepared);
  456. } else {
  457. pr_err("sirfsoc DMA Invalid xfer\n");
  458. ret = -EINVAL;
  459. goto err_xfer;
  460. }
  461. spin_unlock_irqrestore(&schan->lock, iflags);
  462. return &sdesc->desc;
  463. err_xfer:
  464. spin_unlock_irqrestore(&schan->lock, iflags);
  465. no_desc:
  466. err_dir:
  467. return ERR_PTR(ret);
  468. }
  469. static struct dma_async_tx_descriptor *
  470. sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
  471. size_t buf_len, size_t period_len,
  472. enum dma_transfer_direction direction, unsigned long flags)
  473. {
  474. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  475. struct sirfsoc_dma_desc *sdesc = NULL;
  476. unsigned long iflags;
  477. /*
  478. * we only support cycle transfer with 2 period
  479. * If the X-length is set to 0, it would be the loop mode.
  480. * The DMA address keeps increasing until reaching the end of a loop
  481. * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
  482. * the DMA address goes back to the beginning of this area.
  483. * In loop mode, the DMA data region is divided into two parts, BUFA
  484. * and BUFB. DMA controller generates interrupts twice in each loop:
  485. * when the DMA address reaches the end of BUFA or the end of the
  486. * BUFB
  487. */
  488. if (buf_len != 2 * period_len)
  489. return ERR_PTR(-EINVAL);
  490. /* Get free descriptor */
  491. spin_lock_irqsave(&schan->lock, iflags);
  492. if (!list_empty(&schan->free)) {
  493. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  494. node);
  495. list_del(&sdesc->node);
  496. }
  497. spin_unlock_irqrestore(&schan->lock, iflags);
  498. if (!sdesc)
  499. return NULL;
  500. /* Place descriptor in prepared list */
  501. spin_lock_irqsave(&schan->lock, iflags);
  502. sdesc->addr = addr;
  503. sdesc->cyclic = 1;
  504. sdesc->xlen = 0;
  505. sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
  506. sdesc->width = 1;
  507. list_add_tail(&sdesc->node, &schan->prepared);
  508. spin_unlock_irqrestore(&schan->lock, iflags);
  509. return &sdesc->desc;
  510. }
  511. /*
  512. * The DMA controller consists of 16 independent DMA channels.
  513. * Each channel is allocated to a different function
  514. */
  515. bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
  516. {
  517. unsigned int ch_nr = (unsigned int) chan_id;
  518. if (ch_nr == chan->chan_id +
  519. chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
  520. return true;
  521. return false;
  522. }
  523. EXPORT_SYMBOL(sirfsoc_dma_filter_id);
  524. #define SIRFSOC_DMA_BUSWIDTHS \
  525. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  526. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  527. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  528. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  529. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  530. static int sirfsoc_dma_device_slave_caps(struct dma_chan *dchan,
  531. struct dma_slave_caps *caps)
  532. {
  533. caps->src_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
  534. caps->dstn_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
  535. caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  536. caps->cmd_pause = true;
  537. caps->cmd_terminate = true;
  538. return 0;
  539. }
  540. static struct dma_chan *of_dma_sirfsoc_xlate(struct of_phandle_args *dma_spec,
  541. struct of_dma *ofdma)
  542. {
  543. struct sirfsoc_dma *sdma = ofdma->of_dma_data;
  544. unsigned int request = dma_spec->args[0];
  545. if (request >= SIRFSOC_DMA_CHANNELS)
  546. return NULL;
  547. return dma_get_slave_channel(&sdma->channels[request].chan);
  548. }
  549. static int sirfsoc_dma_probe(struct platform_device *op)
  550. {
  551. struct device_node *dn = op->dev.of_node;
  552. struct device *dev = &op->dev;
  553. struct dma_device *dma;
  554. struct sirfsoc_dma *sdma;
  555. struct sirfsoc_dma_chan *schan;
  556. struct resource res;
  557. ulong regs_start, regs_size;
  558. u32 id;
  559. int ret, i;
  560. sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
  561. if (!sdma) {
  562. dev_err(dev, "Memory exhausted!\n");
  563. return -ENOMEM;
  564. }
  565. if (of_device_is_compatible(dn, "sirf,marco-dmac"))
  566. sdma->is_marco = true;
  567. if (of_property_read_u32(dn, "cell-index", &id)) {
  568. dev_err(dev, "Fail to get DMAC index\n");
  569. return -ENODEV;
  570. }
  571. sdma->irq = irq_of_parse_and_map(dn, 0);
  572. if (sdma->irq == NO_IRQ) {
  573. dev_err(dev, "Error mapping IRQ!\n");
  574. return -EINVAL;
  575. }
  576. sdma->clk = devm_clk_get(dev, NULL);
  577. if (IS_ERR(sdma->clk)) {
  578. dev_err(dev, "failed to get a clock.\n");
  579. return PTR_ERR(sdma->clk);
  580. }
  581. ret = of_address_to_resource(dn, 0, &res);
  582. if (ret) {
  583. dev_err(dev, "Error parsing memory region!\n");
  584. goto irq_dispose;
  585. }
  586. regs_start = res.start;
  587. regs_size = resource_size(&res);
  588. sdma->base = devm_ioremap(dev, regs_start, regs_size);
  589. if (!sdma->base) {
  590. dev_err(dev, "Error mapping memory region!\n");
  591. ret = -ENOMEM;
  592. goto irq_dispose;
  593. }
  594. ret = request_irq(sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME, sdma);
  595. if (ret) {
  596. dev_err(dev, "Error requesting IRQ!\n");
  597. ret = -EINVAL;
  598. goto irq_dispose;
  599. }
  600. dma = &sdma->dma;
  601. dma->dev = dev;
  602. dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
  603. dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
  604. dma->device_issue_pending = sirfsoc_dma_issue_pending;
  605. dma->device_control = sirfsoc_dma_control;
  606. dma->device_tx_status = sirfsoc_dma_tx_status;
  607. dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
  608. dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
  609. dma->device_slave_caps = sirfsoc_dma_device_slave_caps;
  610. INIT_LIST_HEAD(&dma->channels);
  611. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  612. dma_cap_set(DMA_CYCLIC, dma->cap_mask);
  613. dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
  614. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  615. for (i = 0; i < SIRFSOC_DMA_CHANNELS; i++) {
  616. schan = &sdma->channels[i];
  617. schan->chan.device = dma;
  618. dma_cookie_init(&schan->chan);
  619. INIT_LIST_HEAD(&schan->free);
  620. INIT_LIST_HEAD(&schan->prepared);
  621. INIT_LIST_HEAD(&schan->queued);
  622. INIT_LIST_HEAD(&schan->active);
  623. INIT_LIST_HEAD(&schan->completed);
  624. spin_lock_init(&schan->lock);
  625. list_add_tail(&schan->chan.device_node, &dma->channels);
  626. }
  627. tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
  628. /* Register DMA engine */
  629. dev_set_drvdata(dev, sdma);
  630. ret = dma_async_device_register(dma);
  631. if (ret)
  632. goto free_irq;
  633. /* Device-tree DMA controller registration */
  634. ret = of_dma_controller_register(dn, of_dma_sirfsoc_xlate, sdma);
  635. if (ret) {
  636. dev_err(dev, "failed to register DMA controller\n");
  637. goto unreg_dma_dev;
  638. }
  639. pm_runtime_enable(&op->dev);
  640. dev_info(dev, "initialized SIRFSOC DMAC driver\n");
  641. return 0;
  642. unreg_dma_dev:
  643. dma_async_device_unregister(dma);
  644. free_irq:
  645. free_irq(sdma->irq, sdma);
  646. irq_dispose:
  647. irq_dispose_mapping(sdma->irq);
  648. return ret;
  649. }
  650. static int sirfsoc_dma_remove(struct platform_device *op)
  651. {
  652. struct device *dev = &op->dev;
  653. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  654. of_dma_controller_free(op->dev.of_node);
  655. dma_async_device_unregister(&sdma->dma);
  656. free_irq(sdma->irq, sdma);
  657. irq_dispose_mapping(sdma->irq);
  658. pm_runtime_disable(&op->dev);
  659. if (!pm_runtime_status_suspended(&op->dev))
  660. sirfsoc_dma_runtime_suspend(&op->dev);
  661. return 0;
  662. }
  663. static int sirfsoc_dma_runtime_suspend(struct device *dev)
  664. {
  665. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  666. clk_disable_unprepare(sdma->clk);
  667. return 0;
  668. }
  669. static int sirfsoc_dma_runtime_resume(struct device *dev)
  670. {
  671. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  672. int ret;
  673. ret = clk_prepare_enable(sdma->clk);
  674. if (ret < 0) {
  675. dev_err(dev, "clk_enable failed: %d\n", ret);
  676. return ret;
  677. }
  678. return 0;
  679. }
  680. #ifdef CONFIG_PM_SLEEP
  681. static int sirfsoc_dma_pm_suspend(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. /*
  690. * if we were runtime-suspended before, resume to enable clock
  691. * before accessing register
  692. */
  693. if (pm_runtime_status_suspended(dev)) {
  694. ret = sirfsoc_dma_runtime_resume(dev);
  695. if (ret < 0)
  696. return ret;
  697. }
  698. /*
  699. * DMA controller will lose all registers while suspending
  700. * so we need to save registers for active channels
  701. */
  702. for (ch = 0; ch < SIRFSOC_DMA_CHANNELS; ch++) {
  703. schan = &sdma->channels[ch];
  704. if (list_empty(&schan->active))
  705. continue;
  706. sdesc = list_first_entry(&schan->active,
  707. struct sirfsoc_dma_desc,
  708. node);
  709. save->ctrl[ch] = readl_relaxed(sdma->base +
  710. ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  711. }
  712. save->interrupt_en = readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN);
  713. /* Disable clock */
  714. sirfsoc_dma_runtime_suspend(dev);
  715. return 0;
  716. }
  717. static int sirfsoc_dma_pm_resume(struct device *dev)
  718. {
  719. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  720. struct sirfsoc_dma_regs *save = &sdma->regs_save;
  721. struct sirfsoc_dma_desc *sdesc;
  722. struct sirfsoc_dma_chan *schan;
  723. int ch;
  724. int ret;
  725. /* Enable clock before accessing register */
  726. ret = sirfsoc_dma_runtime_resume(dev);
  727. if (ret < 0)
  728. return ret;
  729. writel_relaxed(save->interrupt_en, sdma->base + SIRFSOC_DMA_INT_EN);
  730. for (ch = 0; ch < SIRFSOC_DMA_CHANNELS; ch++) {
  731. schan = &sdma->channels[ch];
  732. if (list_empty(&schan->active))
  733. continue;
  734. sdesc = list_first_entry(&schan->active,
  735. struct sirfsoc_dma_desc,
  736. node);
  737. writel_relaxed(sdesc->width,
  738. sdma->base + SIRFSOC_DMA_WIDTH_0 + ch * 4);
  739. writel_relaxed(sdesc->xlen,
  740. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_XLEN);
  741. writel_relaxed(sdesc->ylen,
  742. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_YLEN);
  743. writel_relaxed(save->ctrl[ch],
  744. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  745. writel_relaxed(sdesc->addr >> 2,
  746. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_ADDR);
  747. }
  748. /* if we were runtime-suspended before, suspend again */
  749. if (pm_runtime_status_suspended(dev))
  750. sirfsoc_dma_runtime_suspend(dev);
  751. return 0;
  752. }
  753. #endif
  754. static const struct dev_pm_ops sirfsoc_dma_pm_ops = {
  755. SET_RUNTIME_PM_OPS(sirfsoc_dma_runtime_suspend, sirfsoc_dma_runtime_resume, NULL)
  756. SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_dma_pm_suspend, sirfsoc_dma_pm_resume)
  757. };
  758. static struct of_device_id sirfsoc_dma_match[] = {
  759. { .compatible = "sirf,prima2-dmac", },
  760. { .compatible = "sirf,marco-dmac", },
  761. {},
  762. };
  763. static struct platform_driver sirfsoc_dma_driver = {
  764. .probe = sirfsoc_dma_probe,
  765. .remove = sirfsoc_dma_remove,
  766. .driver = {
  767. .name = DRV_NAME,
  768. .owner = THIS_MODULE,
  769. .pm = &sirfsoc_dma_pm_ops,
  770. .of_match_table = sirfsoc_dma_match,
  771. },
  772. };
  773. static __init int sirfsoc_dma_init(void)
  774. {
  775. return platform_driver_register(&sirfsoc_dma_driver);
  776. }
  777. static void __exit sirfsoc_dma_exit(void)
  778. {
  779. platform_driver_unregister(&sirfsoc_dma_driver);
  780. }
  781. subsys_initcall(sirfsoc_dma_init);
  782. module_exit(sirfsoc_dma_exit);
  783. MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>, "
  784. "Barry Song <baohua.song@csr.com>");
  785. MODULE_DESCRIPTION("SIRFSOC DMA control driver");
  786. MODULE_LICENSE("GPL v2");