sirf-dma.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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_VER_A7V1 1
  24. #define SIRFSOC_DMA_VER_A7V2 2
  25. #define SIRFSOC_DMA_VER_A6 4
  26. #define SIRFSOC_DMA_DESCRIPTORS 16
  27. #define SIRFSOC_DMA_CHANNELS 16
  28. #define SIRFSOC_DMA_TABLE_NUM 256
  29. #define SIRFSOC_DMA_CH_ADDR 0x00
  30. #define SIRFSOC_DMA_CH_XLEN 0x04
  31. #define SIRFSOC_DMA_CH_YLEN 0x08
  32. #define SIRFSOC_DMA_CH_CTRL 0x0C
  33. #define SIRFSOC_DMA_WIDTH_0 0x100
  34. #define SIRFSOC_DMA_CH_VALID 0x140
  35. #define SIRFSOC_DMA_CH_INT 0x144
  36. #define SIRFSOC_DMA_INT_EN 0x148
  37. #define SIRFSOC_DMA_INT_EN_CLR 0x14C
  38. #define SIRFSOC_DMA_CH_LOOP_CTRL 0x150
  39. #define SIRFSOC_DMA_CH_LOOP_CTRL_CLR 0x154
  40. #define SIRFSOC_DMA_WIDTH_ATLAS7 0x10
  41. #define SIRFSOC_DMA_VALID_ATLAS7 0x14
  42. #define SIRFSOC_DMA_INT_ATLAS7 0x18
  43. #define SIRFSOC_DMA_INT_EN_ATLAS7 0x1c
  44. #define SIRFSOC_DMA_LOOP_CTRL_ATLAS7 0x20
  45. #define SIRFSOC_DMA_CUR_DATA_ADDR 0x34
  46. #define SIRFSOC_DMA_MUL_ATLAS7 0x38
  47. #define SIRFSOC_DMA_CH_LOOP_CTRL_ATLAS7 0x158
  48. #define SIRFSOC_DMA_CH_LOOP_CTRL_CLR_ATLAS7 0x15C
  49. #define SIRFSOC_DMA_IOBG_SCMD_EN 0x800
  50. #define SIRFSOC_DMA_EARLY_RESP_SET 0x818
  51. #define SIRFSOC_DMA_EARLY_RESP_CLR 0x81C
  52. #define SIRFSOC_DMA_MODE_CTRL_BIT 4
  53. #define SIRFSOC_DMA_DIR_CTRL_BIT 5
  54. #define SIRFSOC_DMA_MODE_CTRL_BIT_ATLAS7 2
  55. #define SIRFSOC_DMA_CHAIN_CTRL_BIT_ATLAS7 3
  56. #define SIRFSOC_DMA_DIR_CTRL_BIT_ATLAS7 4
  57. #define SIRFSOC_DMA_TAB_NUM_ATLAS7 7
  58. #define SIRFSOC_DMA_CHAIN_INT_BIT_ATLAS7 5
  59. #define SIRFSOC_DMA_CHAIN_FLAG_SHIFT_ATLAS7 25
  60. #define SIRFSOC_DMA_CHAIN_ADDR_SHIFT 32
  61. #define SIRFSOC_DMA_INT_FINI_INT_ATLAS7 BIT(0)
  62. #define SIRFSOC_DMA_INT_CNT_INT_ATLAS7 BIT(1)
  63. #define SIRFSOC_DMA_INT_PAU_INT_ATLAS7 BIT(2)
  64. #define SIRFSOC_DMA_INT_LOOP_INT_ATLAS7 BIT(3)
  65. #define SIRFSOC_DMA_INT_INV_INT_ATLAS7 BIT(4)
  66. #define SIRFSOC_DMA_INT_END_INT_ATLAS7 BIT(5)
  67. #define SIRFSOC_DMA_INT_ALL_ATLAS7 0x3F
  68. /* xlen and dma_width register is in 4 bytes boundary */
  69. #define SIRFSOC_DMA_WORD_LEN 4
  70. #define SIRFSOC_DMA_XLEN_MAX_V1 0x800
  71. #define SIRFSOC_DMA_XLEN_MAX_V2 0x1000
  72. struct sirfsoc_dma_desc {
  73. struct dma_async_tx_descriptor desc;
  74. struct list_head node;
  75. /* SiRFprimaII 2D-DMA parameters */
  76. int xlen; /* DMA xlen */
  77. int ylen; /* DMA ylen */
  78. int width; /* DMA width */
  79. int dir;
  80. bool cyclic; /* is loop DMA? */
  81. bool chain; /* is chain DMA? */
  82. u32 addr; /* DMA buffer address */
  83. u64 chain_table[SIRFSOC_DMA_TABLE_NUM]; /* chain tbl */
  84. };
  85. struct sirfsoc_dma_chan {
  86. struct dma_chan chan;
  87. struct list_head free;
  88. struct list_head prepared;
  89. struct list_head queued;
  90. struct list_head active;
  91. struct list_head completed;
  92. unsigned long happened_cyclic;
  93. unsigned long completed_cyclic;
  94. /* Lock for this structure */
  95. spinlock_t lock;
  96. int mode;
  97. };
  98. struct sirfsoc_dma_regs {
  99. u32 ctrl[SIRFSOC_DMA_CHANNELS];
  100. u32 interrupt_en;
  101. };
  102. struct sirfsoc_dma {
  103. struct dma_device dma;
  104. struct tasklet_struct tasklet;
  105. struct sirfsoc_dma_chan channels[SIRFSOC_DMA_CHANNELS];
  106. void __iomem *base;
  107. int irq;
  108. struct clk *clk;
  109. int type;
  110. void (*exec_desc)(struct sirfsoc_dma_desc *sdesc,
  111. int cid, int burst_mode, void __iomem *base);
  112. struct sirfsoc_dma_regs regs_save;
  113. };
  114. struct sirfsoc_dmadata {
  115. void (*exec)(struct sirfsoc_dma_desc *sdesc,
  116. int cid, int burst_mode, void __iomem *base);
  117. int type;
  118. };
  119. enum sirfsoc_dma_chain_flag {
  120. SIRFSOC_DMA_CHAIN_NORMAL = 0x01,
  121. SIRFSOC_DMA_CHAIN_PAUSE = 0x02,
  122. SIRFSOC_DMA_CHAIN_LOOP = 0x03,
  123. SIRFSOC_DMA_CHAIN_END = 0x04
  124. };
  125. #define DRV_NAME "sirfsoc_dma"
  126. static int sirfsoc_dma_runtime_suspend(struct device *dev);
  127. /* Convert struct dma_chan to struct sirfsoc_dma_chan */
  128. static inline
  129. struct sirfsoc_dma_chan *dma_chan_to_sirfsoc_dma_chan(struct dma_chan *c)
  130. {
  131. return container_of(c, struct sirfsoc_dma_chan, chan);
  132. }
  133. /* Convert struct dma_chan to struct sirfsoc_dma */
  134. static inline struct sirfsoc_dma *dma_chan_to_sirfsoc_dma(struct dma_chan *c)
  135. {
  136. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(c);
  137. return container_of(schan, struct sirfsoc_dma, channels[c->chan_id]);
  138. }
  139. static void sirfsoc_dma_execute_hw_a7v2(struct sirfsoc_dma_desc *sdesc,
  140. int cid, int burst_mode, void __iomem *base)
  141. {
  142. if (sdesc->chain) {
  143. /* DMA v2 HW chain mode */
  144. writel_relaxed((sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT_ATLAS7) |
  145. (sdesc->chain <<
  146. SIRFSOC_DMA_CHAIN_CTRL_BIT_ATLAS7) |
  147. (0x8 << SIRFSOC_DMA_TAB_NUM_ATLAS7) | 0x3,
  148. base + SIRFSOC_DMA_CH_CTRL);
  149. } else {
  150. /* DMA v2 legacy mode */
  151. writel_relaxed(sdesc->xlen, base + SIRFSOC_DMA_CH_XLEN);
  152. writel_relaxed(sdesc->ylen, base + SIRFSOC_DMA_CH_YLEN);
  153. writel_relaxed(sdesc->width, base + SIRFSOC_DMA_WIDTH_ATLAS7);
  154. writel_relaxed((sdesc->width*((sdesc->ylen+1)>>1)),
  155. base + SIRFSOC_DMA_MUL_ATLAS7);
  156. writel_relaxed((sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT_ATLAS7) |
  157. (sdesc->chain <<
  158. SIRFSOC_DMA_CHAIN_CTRL_BIT_ATLAS7) |
  159. 0x3, base + SIRFSOC_DMA_CH_CTRL);
  160. }
  161. writel_relaxed(sdesc->chain ? SIRFSOC_DMA_INT_END_INT_ATLAS7 :
  162. (SIRFSOC_DMA_INT_FINI_INT_ATLAS7 |
  163. SIRFSOC_DMA_INT_LOOP_INT_ATLAS7),
  164. base + SIRFSOC_DMA_INT_EN_ATLAS7);
  165. writel(sdesc->addr, base + SIRFSOC_DMA_CH_ADDR);
  166. if (sdesc->cyclic)
  167. writel(0x10001, base + SIRFSOC_DMA_LOOP_CTRL_ATLAS7);
  168. }
  169. static void sirfsoc_dma_execute_hw_a7v1(struct sirfsoc_dma_desc *sdesc,
  170. int cid, int burst_mode, void __iomem *base)
  171. {
  172. writel_relaxed(1, base + SIRFSOC_DMA_IOBG_SCMD_EN);
  173. writel_relaxed((1 << cid), base + SIRFSOC_DMA_EARLY_RESP_SET);
  174. writel_relaxed(sdesc->width, base + SIRFSOC_DMA_WIDTH_0 + cid * 4);
  175. writel_relaxed(cid | (burst_mode << SIRFSOC_DMA_MODE_CTRL_BIT) |
  176. (sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT),
  177. base + cid * 0x10 + SIRFSOC_DMA_CH_CTRL);
  178. writel_relaxed(sdesc->xlen, base + cid * 0x10 + SIRFSOC_DMA_CH_XLEN);
  179. writel_relaxed(sdesc->ylen, base + cid * 0x10 + SIRFSOC_DMA_CH_YLEN);
  180. writel_relaxed(readl_relaxed(base + SIRFSOC_DMA_INT_EN) |
  181. (1 << cid), base + SIRFSOC_DMA_INT_EN);
  182. writel(sdesc->addr >> 2, base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR);
  183. if (sdesc->cyclic) {
  184. writel((1 << cid) | 1 << (cid + 16) |
  185. readl_relaxed(base + SIRFSOC_DMA_CH_LOOP_CTRL_ATLAS7),
  186. base + SIRFSOC_DMA_CH_LOOP_CTRL_ATLAS7);
  187. }
  188. }
  189. static void sirfsoc_dma_execute_hw_a6(struct sirfsoc_dma_desc *sdesc,
  190. int cid, int burst_mode, void __iomem *base)
  191. {
  192. writel_relaxed(sdesc->width, base + SIRFSOC_DMA_WIDTH_0 + cid * 4);
  193. writel_relaxed(cid | (burst_mode << SIRFSOC_DMA_MODE_CTRL_BIT) |
  194. (sdesc->dir << SIRFSOC_DMA_DIR_CTRL_BIT),
  195. base + cid * 0x10 + SIRFSOC_DMA_CH_CTRL);
  196. writel_relaxed(sdesc->xlen, base + cid * 0x10 + SIRFSOC_DMA_CH_XLEN);
  197. writel_relaxed(sdesc->ylen, base + cid * 0x10 + SIRFSOC_DMA_CH_YLEN);
  198. writel_relaxed(readl_relaxed(base + SIRFSOC_DMA_INT_EN) |
  199. (1 << cid), base + SIRFSOC_DMA_INT_EN);
  200. writel(sdesc->addr >> 2, base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR);
  201. if (sdesc->cyclic) {
  202. writel((1 << cid) | 1 << (cid + 16) |
  203. readl_relaxed(base + SIRFSOC_DMA_CH_LOOP_CTRL),
  204. base + SIRFSOC_DMA_CH_LOOP_CTRL);
  205. }
  206. }
  207. /* Execute all queued DMA descriptors */
  208. static void sirfsoc_dma_execute(struct sirfsoc_dma_chan *schan)
  209. {
  210. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  211. int cid = schan->chan.chan_id;
  212. struct sirfsoc_dma_desc *sdesc = NULL;
  213. void __iomem *base;
  214. /*
  215. * lock has been held by functions calling this, so we don't hold
  216. * lock again
  217. */
  218. base = sdma->base;
  219. sdesc = list_first_entry(&schan->queued, struct sirfsoc_dma_desc,
  220. node);
  221. /* Move the first queued descriptor to active list */
  222. list_move_tail(&sdesc->node, &schan->active);
  223. if (sdma->type == SIRFSOC_DMA_VER_A7V2)
  224. cid = 0;
  225. /* Start the DMA transfer */
  226. sdma->exec_desc(sdesc, cid, schan->mode, base);
  227. if (sdesc->cyclic)
  228. schan->happened_cyclic = schan->completed_cyclic = 0;
  229. }
  230. /* Interrupt handler */
  231. static irqreturn_t sirfsoc_dma_irq(int irq, void *data)
  232. {
  233. struct sirfsoc_dma *sdma = data;
  234. struct sirfsoc_dma_chan *schan;
  235. struct sirfsoc_dma_desc *sdesc = NULL;
  236. u32 is;
  237. bool chain;
  238. int ch;
  239. void __iomem *reg;
  240. switch (sdma->type) {
  241. case SIRFSOC_DMA_VER_A6:
  242. case SIRFSOC_DMA_VER_A7V1:
  243. is = readl(sdma->base + SIRFSOC_DMA_CH_INT);
  244. reg = sdma->base + SIRFSOC_DMA_CH_INT;
  245. while ((ch = fls(is) - 1) >= 0) {
  246. is &= ~(1 << ch);
  247. writel_relaxed(1 << ch, reg);
  248. schan = &sdma->channels[ch];
  249. spin_lock(&schan->lock);
  250. sdesc = list_first_entry(&schan->active,
  251. struct sirfsoc_dma_desc, node);
  252. if (!sdesc->cyclic) {
  253. /* Execute queued descriptors */
  254. list_splice_tail_init(&schan->active,
  255. &schan->completed);
  256. dma_cookie_complete(&sdesc->desc);
  257. if (!list_empty(&schan->queued))
  258. sirfsoc_dma_execute(schan);
  259. } else
  260. schan->happened_cyclic++;
  261. spin_unlock(&schan->lock);
  262. }
  263. break;
  264. case SIRFSOC_DMA_VER_A7V2:
  265. is = readl(sdma->base + SIRFSOC_DMA_INT_ATLAS7);
  266. reg = sdma->base + SIRFSOC_DMA_INT_ATLAS7;
  267. writel_relaxed(SIRFSOC_DMA_INT_ALL_ATLAS7, reg);
  268. schan = &sdma->channels[0];
  269. spin_lock(&schan->lock);
  270. sdesc = list_first_entry(&schan->active,
  271. struct sirfsoc_dma_desc, node);
  272. if (!sdesc->cyclic) {
  273. chain = sdesc->chain;
  274. if ((chain && (is & SIRFSOC_DMA_INT_END_INT_ATLAS7)) ||
  275. (!chain &&
  276. (is & SIRFSOC_DMA_INT_FINI_INT_ATLAS7))) {
  277. /* Execute queued descriptors */
  278. list_splice_tail_init(&schan->active,
  279. &schan->completed);
  280. dma_cookie_complete(&sdesc->desc);
  281. if (!list_empty(&schan->queued))
  282. sirfsoc_dma_execute(schan);
  283. }
  284. } else if (sdesc->cyclic && (is &
  285. SIRFSOC_DMA_INT_LOOP_INT_ATLAS7))
  286. schan->happened_cyclic++;
  287. spin_unlock(&schan->lock);
  288. break;
  289. default:
  290. break;
  291. }
  292. /* Schedule tasklet */
  293. tasklet_schedule(&sdma->tasklet);
  294. return IRQ_HANDLED;
  295. }
  296. /* process completed descriptors */
  297. static void sirfsoc_dma_process_completed(struct sirfsoc_dma *sdma)
  298. {
  299. dma_cookie_t last_cookie = 0;
  300. struct sirfsoc_dma_chan *schan;
  301. struct sirfsoc_dma_desc *sdesc;
  302. struct dma_async_tx_descriptor *desc;
  303. unsigned long flags;
  304. unsigned long happened_cyclic;
  305. LIST_HEAD(list);
  306. int i;
  307. for (i = 0; i < sdma->dma.chancnt; i++) {
  308. schan = &sdma->channels[i];
  309. /* Get all completed descriptors */
  310. spin_lock_irqsave(&schan->lock, flags);
  311. if (!list_empty(&schan->completed)) {
  312. list_splice_tail_init(&schan->completed, &list);
  313. spin_unlock_irqrestore(&schan->lock, flags);
  314. /* Execute callbacks and run dependencies */
  315. list_for_each_entry(sdesc, &list, node) {
  316. desc = &sdesc->desc;
  317. if (desc->callback)
  318. desc->callback(desc->callback_param);
  319. last_cookie = desc->cookie;
  320. dma_run_dependencies(desc);
  321. }
  322. /* Free descriptors */
  323. spin_lock_irqsave(&schan->lock, flags);
  324. list_splice_tail_init(&list, &schan->free);
  325. schan->chan.completed_cookie = last_cookie;
  326. spin_unlock_irqrestore(&schan->lock, flags);
  327. } else {
  328. if (list_empty(&schan->active)) {
  329. spin_unlock_irqrestore(&schan->lock, flags);
  330. continue;
  331. }
  332. /* for cyclic channel, desc is always in active list */
  333. sdesc = list_first_entry(&schan->active,
  334. struct sirfsoc_dma_desc, node);
  335. /* cyclic DMA */
  336. happened_cyclic = schan->happened_cyclic;
  337. spin_unlock_irqrestore(&schan->lock, flags);
  338. desc = &sdesc->desc;
  339. while (happened_cyclic != schan->completed_cyclic) {
  340. if (desc->callback)
  341. desc->callback(desc->callback_param);
  342. schan->completed_cyclic++;
  343. }
  344. }
  345. }
  346. }
  347. /* DMA Tasklet */
  348. static void sirfsoc_dma_tasklet(unsigned long data)
  349. {
  350. struct sirfsoc_dma *sdma = (void *)data;
  351. sirfsoc_dma_process_completed(sdma);
  352. }
  353. /* Submit descriptor to hardware */
  354. static dma_cookie_t sirfsoc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  355. {
  356. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(txd->chan);
  357. struct sirfsoc_dma_desc *sdesc;
  358. unsigned long flags;
  359. dma_cookie_t cookie;
  360. sdesc = container_of(txd, struct sirfsoc_dma_desc, desc);
  361. spin_lock_irqsave(&schan->lock, flags);
  362. /* Move descriptor to queue */
  363. list_move_tail(&sdesc->node, &schan->queued);
  364. cookie = dma_cookie_assign(txd);
  365. spin_unlock_irqrestore(&schan->lock, flags);
  366. return cookie;
  367. }
  368. static int sirfsoc_dma_slave_config(struct dma_chan *chan,
  369. struct dma_slave_config *config)
  370. {
  371. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  372. unsigned long flags;
  373. if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  374. (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
  375. return -EINVAL;
  376. spin_lock_irqsave(&schan->lock, flags);
  377. schan->mode = (config->src_maxburst == 4 ? 1 : 0);
  378. spin_unlock_irqrestore(&schan->lock, flags);
  379. return 0;
  380. }
  381. static int sirfsoc_dma_terminate_all(struct dma_chan *chan)
  382. {
  383. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  384. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  385. int cid = schan->chan.chan_id;
  386. unsigned long flags;
  387. spin_lock_irqsave(&schan->lock, flags);
  388. switch (sdma->type) {
  389. case SIRFSOC_DMA_VER_A7V1:
  390. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_INT_EN_CLR);
  391. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_INT);
  392. writel_relaxed((1 << cid) | 1 << (cid + 16),
  393. sdma->base +
  394. SIRFSOC_DMA_CH_LOOP_CTRL_CLR_ATLAS7);
  395. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
  396. break;
  397. case SIRFSOC_DMA_VER_A7V2:
  398. writel_relaxed(0, sdma->base + SIRFSOC_DMA_INT_EN_ATLAS7);
  399. writel_relaxed(SIRFSOC_DMA_INT_ALL_ATLAS7,
  400. sdma->base + SIRFSOC_DMA_INT_ATLAS7);
  401. writel_relaxed(0, sdma->base + SIRFSOC_DMA_LOOP_CTRL_ATLAS7);
  402. writel_relaxed(0, sdma->base + SIRFSOC_DMA_VALID_ATLAS7);
  403. break;
  404. case SIRFSOC_DMA_VER_A6:
  405. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
  406. ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
  407. writel_relaxed(readl_relaxed(sdma->base +
  408. SIRFSOC_DMA_CH_LOOP_CTRL) &
  409. ~((1 << cid) | 1 << (cid + 16)),
  410. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  411. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
  412. break;
  413. default:
  414. break;
  415. }
  416. list_splice_tail_init(&schan->active, &schan->free);
  417. list_splice_tail_init(&schan->queued, &schan->free);
  418. spin_unlock_irqrestore(&schan->lock, flags);
  419. return 0;
  420. }
  421. static int sirfsoc_dma_pause_chan(struct dma_chan *chan)
  422. {
  423. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  424. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  425. int cid = schan->chan.chan_id;
  426. unsigned long flags;
  427. spin_lock_irqsave(&schan->lock, flags);
  428. switch (sdma->type) {
  429. case SIRFSOC_DMA_VER_A7V1:
  430. writel_relaxed((1 << cid) | 1 << (cid + 16),
  431. sdma->base +
  432. SIRFSOC_DMA_CH_LOOP_CTRL_CLR_ATLAS7);
  433. break;
  434. case SIRFSOC_DMA_VER_A7V2:
  435. writel_relaxed(0, sdma->base + SIRFSOC_DMA_LOOP_CTRL_ATLAS7);
  436. break;
  437. case SIRFSOC_DMA_VER_A6:
  438. writel_relaxed(readl_relaxed(sdma->base +
  439. SIRFSOC_DMA_CH_LOOP_CTRL) &
  440. ~((1 << cid) | 1 << (cid + 16)),
  441. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  442. break;
  443. default:
  444. break;
  445. }
  446. spin_unlock_irqrestore(&schan->lock, flags);
  447. return 0;
  448. }
  449. static int sirfsoc_dma_resume_chan(struct dma_chan *chan)
  450. {
  451. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  452. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  453. int cid = schan->chan.chan_id;
  454. unsigned long flags;
  455. spin_lock_irqsave(&schan->lock, flags);
  456. switch (sdma->type) {
  457. case SIRFSOC_DMA_VER_A7V1:
  458. writel_relaxed((1 << cid) | 1 << (cid + 16),
  459. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_ATLAS7);
  460. break;
  461. case SIRFSOC_DMA_VER_A7V2:
  462. writel_relaxed(0x10001,
  463. sdma->base + SIRFSOC_DMA_LOOP_CTRL_ATLAS7);
  464. break;
  465. case SIRFSOC_DMA_VER_A6:
  466. writel_relaxed(readl_relaxed(sdma->base +
  467. SIRFSOC_DMA_CH_LOOP_CTRL) |
  468. ((1 << cid) | 1 << (cid + 16)),
  469. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  470. break;
  471. default:
  472. break;
  473. }
  474. spin_unlock_irqrestore(&schan->lock, flags);
  475. return 0;
  476. }
  477. /* Alloc channel resources */
  478. static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
  479. {
  480. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  481. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  482. struct sirfsoc_dma_desc *sdesc;
  483. unsigned long flags;
  484. LIST_HEAD(descs);
  485. int i;
  486. pm_runtime_get_sync(sdma->dma.dev);
  487. /* Alloc descriptors for this channel */
  488. for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
  489. sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
  490. if (!sdesc) {
  491. dev_notice(sdma->dma.dev, "Memory allocation error. "
  492. "Allocated only %u descriptors\n", i);
  493. break;
  494. }
  495. dma_async_tx_descriptor_init(&sdesc->desc, chan);
  496. sdesc->desc.flags = DMA_CTRL_ACK;
  497. sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
  498. list_add_tail(&sdesc->node, &descs);
  499. }
  500. /* Return error only if no descriptors were allocated */
  501. if (i == 0)
  502. return -ENOMEM;
  503. spin_lock_irqsave(&schan->lock, flags);
  504. list_splice_tail_init(&descs, &schan->free);
  505. spin_unlock_irqrestore(&schan->lock, flags);
  506. return i;
  507. }
  508. /* Free channel resources */
  509. static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
  510. {
  511. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  512. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  513. struct sirfsoc_dma_desc *sdesc, *tmp;
  514. unsigned long flags;
  515. LIST_HEAD(descs);
  516. spin_lock_irqsave(&schan->lock, flags);
  517. /* Channel must be idle */
  518. BUG_ON(!list_empty(&schan->prepared));
  519. BUG_ON(!list_empty(&schan->queued));
  520. BUG_ON(!list_empty(&schan->active));
  521. BUG_ON(!list_empty(&schan->completed));
  522. /* Move data */
  523. list_splice_tail_init(&schan->free, &descs);
  524. spin_unlock_irqrestore(&schan->lock, flags);
  525. /* Free descriptors */
  526. list_for_each_entry_safe(sdesc, tmp, &descs, node)
  527. kfree(sdesc);
  528. pm_runtime_put(sdma->dma.dev);
  529. }
  530. /* Send pending descriptor to hardware */
  531. static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
  532. {
  533. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  534. unsigned long flags;
  535. spin_lock_irqsave(&schan->lock, flags);
  536. if (list_empty(&schan->active) && !list_empty(&schan->queued))
  537. sirfsoc_dma_execute(schan);
  538. spin_unlock_irqrestore(&schan->lock, flags);
  539. }
  540. /* Check request completion status */
  541. static enum dma_status
  542. sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  543. struct dma_tx_state *txstate)
  544. {
  545. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  546. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  547. unsigned long flags;
  548. enum dma_status ret;
  549. struct sirfsoc_dma_desc *sdesc;
  550. int cid = schan->chan.chan_id;
  551. unsigned long dma_pos;
  552. unsigned long dma_request_bytes;
  553. unsigned long residue;
  554. spin_lock_irqsave(&schan->lock, flags);
  555. if (list_empty(&schan->active)) {
  556. ret = dma_cookie_status(chan, cookie, txstate);
  557. dma_set_residue(txstate, 0);
  558. spin_unlock_irqrestore(&schan->lock, flags);
  559. return ret;
  560. }
  561. sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc, node);
  562. if (sdesc->cyclic)
  563. dma_request_bytes = (sdesc->xlen + 1) * (sdesc->ylen + 1) *
  564. (sdesc->width * SIRFSOC_DMA_WORD_LEN);
  565. else
  566. dma_request_bytes = sdesc->xlen * SIRFSOC_DMA_WORD_LEN;
  567. ret = dma_cookie_status(chan, cookie, txstate);
  568. if (sdma->type == SIRFSOC_DMA_VER_A7V2)
  569. cid = 0;
  570. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  571. dma_pos = readl_relaxed(sdma->base + SIRFSOC_DMA_CUR_DATA_ADDR);
  572. } else {
  573. dma_pos = readl_relaxed(
  574. sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR) << 2;
  575. }
  576. residue = dma_request_bytes - (dma_pos - sdesc->addr);
  577. dma_set_residue(txstate, residue);
  578. spin_unlock_irqrestore(&schan->lock, flags);
  579. return ret;
  580. }
  581. static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
  582. struct dma_chan *chan, struct dma_interleaved_template *xt,
  583. unsigned long flags)
  584. {
  585. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  586. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  587. struct sirfsoc_dma_desc *sdesc = NULL;
  588. unsigned long iflags;
  589. int ret;
  590. if ((xt->dir != DMA_MEM_TO_DEV) && (xt->dir != DMA_DEV_TO_MEM)) {
  591. ret = -EINVAL;
  592. goto err_dir;
  593. }
  594. /* Get free descriptor */
  595. spin_lock_irqsave(&schan->lock, iflags);
  596. if (!list_empty(&schan->free)) {
  597. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  598. node);
  599. list_del(&sdesc->node);
  600. }
  601. spin_unlock_irqrestore(&schan->lock, iflags);
  602. if (!sdesc) {
  603. /* try to free completed descriptors */
  604. sirfsoc_dma_process_completed(sdma);
  605. ret = 0;
  606. goto no_desc;
  607. }
  608. /* Place descriptor in prepared list */
  609. spin_lock_irqsave(&schan->lock, iflags);
  610. /*
  611. * Number of chunks in a frame can only be 1 for prima2
  612. * and ylen (number of frame - 1) must be at least 0
  613. */
  614. if ((xt->frame_size == 1) && (xt->numf > 0)) {
  615. sdesc->cyclic = 0;
  616. sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
  617. sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
  618. SIRFSOC_DMA_WORD_LEN;
  619. sdesc->ylen = xt->numf - 1;
  620. if (xt->dir == DMA_MEM_TO_DEV) {
  621. sdesc->addr = xt->src_start;
  622. sdesc->dir = 1;
  623. } else {
  624. sdesc->addr = xt->dst_start;
  625. sdesc->dir = 0;
  626. }
  627. list_add_tail(&sdesc->node, &schan->prepared);
  628. } else {
  629. pr_err("sirfsoc DMA Invalid xfer\n");
  630. ret = -EINVAL;
  631. goto err_xfer;
  632. }
  633. spin_unlock_irqrestore(&schan->lock, iflags);
  634. return &sdesc->desc;
  635. err_xfer:
  636. spin_unlock_irqrestore(&schan->lock, iflags);
  637. no_desc:
  638. err_dir:
  639. return ERR_PTR(ret);
  640. }
  641. static struct dma_async_tx_descriptor *
  642. sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
  643. size_t buf_len, size_t period_len,
  644. enum dma_transfer_direction direction, unsigned long flags)
  645. {
  646. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  647. struct sirfsoc_dma_desc *sdesc = NULL;
  648. unsigned long iflags;
  649. /*
  650. * we only support cycle transfer with 2 period
  651. * If the X-length is set to 0, it would be the loop mode.
  652. * The DMA address keeps increasing until reaching the end of a loop
  653. * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
  654. * the DMA address goes back to the beginning of this area.
  655. * In loop mode, the DMA data region is divided into two parts, BUFA
  656. * and BUFB. DMA controller generates interrupts twice in each loop:
  657. * when the DMA address reaches the end of BUFA or the end of the
  658. * BUFB
  659. */
  660. if (buf_len != 2 * period_len)
  661. return ERR_PTR(-EINVAL);
  662. /* Get free descriptor */
  663. spin_lock_irqsave(&schan->lock, iflags);
  664. if (!list_empty(&schan->free)) {
  665. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  666. node);
  667. list_del(&sdesc->node);
  668. }
  669. spin_unlock_irqrestore(&schan->lock, iflags);
  670. if (!sdesc)
  671. return NULL;
  672. /* Place descriptor in prepared list */
  673. spin_lock_irqsave(&schan->lock, iflags);
  674. sdesc->addr = addr;
  675. sdesc->cyclic = 1;
  676. sdesc->xlen = 0;
  677. sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
  678. sdesc->width = 1;
  679. list_add_tail(&sdesc->node, &schan->prepared);
  680. spin_unlock_irqrestore(&schan->lock, iflags);
  681. return &sdesc->desc;
  682. }
  683. /*
  684. * The DMA controller consists of 16 independent DMA channels.
  685. * Each channel is allocated to a different function
  686. */
  687. bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
  688. {
  689. unsigned int ch_nr = (unsigned int) chan_id;
  690. if (ch_nr == chan->chan_id +
  691. chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
  692. return true;
  693. return false;
  694. }
  695. EXPORT_SYMBOL(sirfsoc_dma_filter_id);
  696. #define SIRFSOC_DMA_BUSWIDTHS \
  697. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  698. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  699. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  700. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  701. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  702. static struct dma_chan *of_dma_sirfsoc_xlate(struct of_phandle_args *dma_spec,
  703. struct of_dma *ofdma)
  704. {
  705. struct sirfsoc_dma *sdma = ofdma->of_dma_data;
  706. unsigned int request = dma_spec->args[0];
  707. if (request >= SIRFSOC_DMA_CHANNELS)
  708. return NULL;
  709. return dma_get_slave_channel(&sdma->channels[request].chan);
  710. }
  711. static int sirfsoc_dma_probe(struct platform_device *op)
  712. {
  713. struct device_node *dn = op->dev.of_node;
  714. struct device *dev = &op->dev;
  715. struct dma_device *dma;
  716. struct sirfsoc_dma *sdma;
  717. struct sirfsoc_dma_chan *schan;
  718. struct sirfsoc_dmadata *data;
  719. struct resource res;
  720. ulong regs_start, regs_size;
  721. u32 id;
  722. int ret, i;
  723. sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
  724. if (!sdma) {
  725. dev_err(dev, "Memory exhausted!\n");
  726. return -ENOMEM;
  727. }
  728. data = (struct sirfsoc_dmadata *)
  729. (of_match_device(op->dev.driver->of_match_table,
  730. &op->dev)->data);
  731. sdma->exec_desc = data->exec;
  732. sdma->type = data->type;
  733. if (of_property_read_u32(dn, "cell-index", &id)) {
  734. dev_err(dev, "Fail to get DMAC index\n");
  735. return -ENODEV;
  736. }
  737. sdma->irq = irq_of_parse_and_map(dn, 0);
  738. if (sdma->irq == NO_IRQ) {
  739. dev_err(dev, "Error mapping IRQ!\n");
  740. return -EINVAL;
  741. }
  742. sdma->clk = devm_clk_get(dev, NULL);
  743. if (IS_ERR(sdma->clk)) {
  744. dev_err(dev, "failed to get a clock.\n");
  745. return PTR_ERR(sdma->clk);
  746. }
  747. ret = of_address_to_resource(dn, 0, &res);
  748. if (ret) {
  749. dev_err(dev, "Error parsing memory region!\n");
  750. goto irq_dispose;
  751. }
  752. regs_start = res.start;
  753. regs_size = resource_size(&res);
  754. sdma->base = devm_ioremap(dev, regs_start, regs_size);
  755. if (!sdma->base) {
  756. dev_err(dev, "Error mapping memory region!\n");
  757. ret = -ENOMEM;
  758. goto irq_dispose;
  759. }
  760. ret = request_irq(sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME, sdma);
  761. if (ret) {
  762. dev_err(dev, "Error requesting IRQ!\n");
  763. ret = -EINVAL;
  764. goto irq_dispose;
  765. }
  766. dma = &sdma->dma;
  767. dma->dev = dev;
  768. dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
  769. dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
  770. dma->device_issue_pending = sirfsoc_dma_issue_pending;
  771. dma->device_config = sirfsoc_dma_slave_config;
  772. dma->device_pause = sirfsoc_dma_pause_chan;
  773. dma->device_resume = sirfsoc_dma_resume_chan;
  774. dma->device_terminate_all = sirfsoc_dma_terminate_all;
  775. dma->device_tx_status = sirfsoc_dma_tx_status;
  776. dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
  777. dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
  778. dma->src_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
  779. dma->dst_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
  780. dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  781. INIT_LIST_HEAD(&dma->channels);
  782. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  783. dma_cap_set(DMA_CYCLIC, dma->cap_mask);
  784. dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
  785. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  786. for (i = 0; i < SIRFSOC_DMA_CHANNELS; i++) {
  787. schan = &sdma->channels[i];
  788. schan->chan.device = dma;
  789. dma_cookie_init(&schan->chan);
  790. INIT_LIST_HEAD(&schan->free);
  791. INIT_LIST_HEAD(&schan->prepared);
  792. INIT_LIST_HEAD(&schan->queued);
  793. INIT_LIST_HEAD(&schan->active);
  794. INIT_LIST_HEAD(&schan->completed);
  795. spin_lock_init(&schan->lock);
  796. list_add_tail(&schan->chan.device_node, &dma->channels);
  797. }
  798. tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
  799. /* Register DMA engine */
  800. dev_set_drvdata(dev, sdma);
  801. ret = dma_async_device_register(dma);
  802. if (ret)
  803. goto free_irq;
  804. /* Device-tree DMA controller registration */
  805. ret = of_dma_controller_register(dn, of_dma_sirfsoc_xlate, sdma);
  806. if (ret) {
  807. dev_err(dev, "failed to register DMA controller\n");
  808. goto unreg_dma_dev;
  809. }
  810. pm_runtime_enable(&op->dev);
  811. dev_info(dev, "initialized SIRFSOC DMAC driver\n");
  812. return 0;
  813. unreg_dma_dev:
  814. dma_async_device_unregister(dma);
  815. free_irq:
  816. free_irq(sdma->irq, sdma);
  817. irq_dispose:
  818. irq_dispose_mapping(sdma->irq);
  819. return ret;
  820. }
  821. static int sirfsoc_dma_remove(struct platform_device *op)
  822. {
  823. struct device *dev = &op->dev;
  824. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  825. of_dma_controller_free(op->dev.of_node);
  826. dma_async_device_unregister(&sdma->dma);
  827. free_irq(sdma->irq, sdma);
  828. irq_dispose_mapping(sdma->irq);
  829. pm_runtime_disable(&op->dev);
  830. if (!pm_runtime_status_suspended(&op->dev))
  831. sirfsoc_dma_runtime_suspend(&op->dev);
  832. return 0;
  833. }
  834. static int sirfsoc_dma_runtime_suspend(struct device *dev)
  835. {
  836. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  837. clk_disable_unprepare(sdma->clk);
  838. return 0;
  839. }
  840. static int sirfsoc_dma_runtime_resume(struct device *dev)
  841. {
  842. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  843. int ret;
  844. ret = clk_prepare_enable(sdma->clk);
  845. if (ret < 0) {
  846. dev_err(dev, "clk_enable failed: %d\n", ret);
  847. return ret;
  848. }
  849. return 0;
  850. }
  851. #ifdef CONFIG_PM_SLEEP
  852. static int sirfsoc_dma_pm_suspend(struct device *dev)
  853. {
  854. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  855. struct sirfsoc_dma_regs *save = &sdma->regs_save;
  856. struct sirfsoc_dma_desc *sdesc;
  857. struct sirfsoc_dma_chan *schan;
  858. int ch;
  859. int ret;
  860. int count;
  861. u32 int_offset;
  862. /*
  863. * if we were runtime-suspended before, resume to enable clock
  864. * before accessing register
  865. */
  866. if (pm_runtime_status_suspended(dev)) {
  867. ret = sirfsoc_dma_runtime_resume(dev);
  868. if (ret < 0)
  869. return ret;
  870. }
  871. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  872. count = 1;
  873. int_offset = SIRFSOC_DMA_INT_EN_ATLAS7;
  874. } else {
  875. count = SIRFSOC_DMA_CHANNELS;
  876. int_offset = SIRFSOC_DMA_INT_EN;
  877. }
  878. /*
  879. * DMA controller will lose all registers while suspending
  880. * so we need to save registers for active channels
  881. */
  882. for (ch = 0; ch < count; ch++) {
  883. schan = &sdma->channels[ch];
  884. if (list_empty(&schan->active))
  885. continue;
  886. sdesc = list_first_entry(&schan->active,
  887. struct sirfsoc_dma_desc,
  888. node);
  889. save->ctrl[ch] = readl_relaxed(sdma->base +
  890. ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  891. }
  892. save->interrupt_en = readl_relaxed(sdma->base + int_offset);
  893. /* Disable clock */
  894. sirfsoc_dma_runtime_suspend(dev);
  895. return 0;
  896. }
  897. static int sirfsoc_dma_pm_resume(struct device *dev)
  898. {
  899. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  900. struct sirfsoc_dma_regs *save = &sdma->regs_save;
  901. struct sirfsoc_dma_desc *sdesc;
  902. struct sirfsoc_dma_chan *schan;
  903. int ch;
  904. int ret;
  905. int count;
  906. u32 int_offset;
  907. u32 width_offset;
  908. /* Enable clock before accessing register */
  909. ret = sirfsoc_dma_runtime_resume(dev);
  910. if (ret < 0)
  911. return ret;
  912. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  913. count = 1;
  914. int_offset = SIRFSOC_DMA_INT_EN_ATLAS7;
  915. width_offset = SIRFSOC_DMA_WIDTH_ATLAS7;
  916. } else {
  917. count = SIRFSOC_DMA_CHANNELS;
  918. int_offset = SIRFSOC_DMA_INT_EN;
  919. width_offset = SIRFSOC_DMA_WIDTH_0;
  920. }
  921. writel_relaxed(save->interrupt_en, sdma->base + int_offset);
  922. for (ch = 0; ch < count; ch++) {
  923. schan = &sdma->channels[ch];
  924. if (list_empty(&schan->active))
  925. continue;
  926. sdesc = list_first_entry(&schan->active,
  927. struct sirfsoc_dma_desc,
  928. node);
  929. writel_relaxed(sdesc->width,
  930. sdma->base + width_offset + ch * 4);
  931. writel_relaxed(sdesc->xlen,
  932. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_XLEN);
  933. writel_relaxed(sdesc->ylen,
  934. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_YLEN);
  935. writel_relaxed(save->ctrl[ch],
  936. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  937. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  938. writel_relaxed(sdesc->addr,
  939. sdma->base + SIRFSOC_DMA_CH_ADDR);
  940. } else {
  941. writel_relaxed(sdesc->addr >> 2,
  942. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_ADDR);
  943. }
  944. }
  945. /* if we were runtime-suspended before, suspend again */
  946. if (pm_runtime_status_suspended(dev))
  947. sirfsoc_dma_runtime_suspend(dev);
  948. return 0;
  949. }
  950. #endif
  951. static const struct dev_pm_ops sirfsoc_dma_pm_ops = {
  952. SET_RUNTIME_PM_OPS(sirfsoc_dma_runtime_suspend, sirfsoc_dma_runtime_resume, NULL)
  953. SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_dma_pm_suspend, sirfsoc_dma_pm_resume)
  954. };
  955. struct sirfsoc_dmadata sirfsoc_dmadata_a6 = {
  956. .exec = sirfsoc_dma_execute_hw_a6,
  957. .type = SIRFSOC_DMA_VER_A6,
  958. };
  959. struct sirfsoc_dmadata sirfsoc_dmadata_a7v1 = {
  960. .exec = sirfsoc_dma_execute_hw_a7v1,
  961. .type = SIRFSOC_DMA_VER_A7V1,
  962. };
  963. struct sirfsoc_dmadata sirfsoc_dmadata_a7v2 = {
  964. .exec = sirfsoc_dma_execute_hw_a7v2,
  965. .type = SIRFSOC_DMA_VER_A7V2,
  966. };
  967. static const struct of_device_id sirfsoc_dma_match[] = {
  968. { .compatible = "sirf,prima2-dmac", .data = &sirfsoc_dmadata_a6,},
  969. { .compatible = "sirf,atlas7-dmac", .data = &sirfsoc_dmadata_a7v1,},
  970. { .compatible = "sirf,atlas7-dmac-v2", .data = &sirfsoc_dmadata_a7v2,},
  971. {},
  972. };
  973. MODULE_DEVICE_TABLE(of, sirfsoc_dma_match);
  974. static struct platform_driver sirfsoc_dma_driver = {
  975. .probe = sirfsoc_dma_probe,
  976. .remove = sirfsoc_dma_remove,
  977. .driver = {
  978. .name = DRV_NAME,
  979. .pm = &sirfsoc_dma_pm_ops,
  980. .of_match_table = sirfsoc_dma_match,
  981. },
  982. };
  983. static __init int sirfsoc_dma_init(void)
  984. {
  985. return platform_driver_register(&sirfsoc_dma_driver);
  986. }
  987. static void __exit sirfsoc_dma_exit(void)
  988. {
  989. platform_driver_unregister(&sirfsoc_dma_driver);
  990. }
  991. subsys_initcall(sirfsoc_dma_init);
  992. module_exit(sirfsoc_dma_exit);
  993. MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>");
  994. MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
  995. MODULE_DESCRIPTION("SIRFSOC DMA control driver");
  996. MODULE_LICENSE("GPL v2");