sirf-dma.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. dmaengine_desc_get_callback_invoke(desc, NULL);
  318. last_cookie = desc->cookie;
  319. dma_run_dependencies(desc);
  320. }
  321. /* Free descriptors */
  322. spin_lock_irqsave(&schan->lock, flags);
  323. list_splice_tail_init(&list, &schan->free);
  324. schan->chan.completed_cookie = last_cookie;
  325. spin_unlock_irqrestore(&schan->lock, flags);
  326. } else {
  327. if (list_empty(&schan->active)) {
  328. spin_unlock_irqrestore(&schan->lock, flags);
  329. continue;
  330. }
  331. /* for cyclic channel, desc is always in active list */
  332. sdesc = list_first_entry(&schan->active,
  333. struct sirfsoc_dma_desc, node);
  334. /* cyclic DMA */
  335. happened_cyclic = schan->happened_cyclic;
  336. spin_unlock_irqrestore(&schan->lock, flags);
  337. desc = &sdesc->desc;
  338. while (happened_cyclic != schan->completed_cyclic) {
  339. dmaengine_desc_get_callback_invoke(desc, NULL);
  340. schan->completed_cyclic++;
  341. }
  342. }
  343. }
  344. }
  345. /* DMA Tasklet */
  346. static void sirfsoc_dma_tasklet(unsigned long data)
  347. {
  348. struct sirfsoc_dma *sdma = (void *)data;
  349. sirfsoc_dma_process_completed(sdma);
  350. }
  351. /* Submit descriptor to hardware */
  352. static dma_cookie_t sirfsoc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  353. {
  354. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(txd->chan);
  355. struct sirfsoc_dma_desc *sdesc;
  356. unsigned long flags;
  357. dma_cookie_t cookie;
  358. sdesc = container_of(txd, struct sirfsoc_dma_desc, desc);
  359. spin_lock_irqsave(&schan->lock, flags);
  360. /* Move descriptor to queue */
  361. list_move_tail(&sdesc->node, &schan->queued);
  362. cookie = dma_cookie_assign(txd);
  363. spin_unlock_irqrestore(&schan->lock, flags);
  364. return cookie;
  365. }
  366. static int sirfsoc_dma_slave_config(struct dma_chan *chan,
  367. struct dma_slave_config *config)
  368. {
  369. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  370. unsigned long flags;
  371. if ((config->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  372. (config->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES))
  373. return -EINVAL;
  374. spin_lock_irqsave(&schan->lock, flags);
  375. schan->mode = (config->src_maxburst == 4 ? 1 : 0);
  376. spin_unlock_irqrestore(&schan->lock, flags);
  377. return 0;
  378. }
  379. static int sirfsoc_dma_terminate_all(struct dma_chan *chan)
  380. {
  381. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  382. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  383. int cid = schan->chan.chan_id;
  384. unsigned long flags;
  385. spin_lock_irqsave(&schan->lock, flags);
  386. switch (sdma->type) {
  387. case SIRFSOC_DMA_VER_A7V1:
  388. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_INT_EN_CLR);
  389. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_INT);
  390. writel_relaxed((1 << cid) | 1 << (cid + 16),
  391. sdma->base +
  392. SIRFSOC_DMA_CH_LOOP_CTRL_CLR_ATLAS7);
  393. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
  394. break;
  395. case SIRFSOC_DMA_VER_A7V2:
  396. writel_relaxed(0, sdma->base + SIRFSOC_DMA_INT_EN_ATLAS7);
  397. writel_relaxed(SIRFSOC_DMA_INT_ALL_ATLAS7,
  398. sdma->base + SIRFSOC_DMA_INT_ATLAS7);
  399. writel_relaxed(0, sdma->base + SIRFSOC_DMA_LOOP_CTRL_ATLAS7);
  400. writel_relaxed(0, sdma->base + SIRFSOC_DMA_VALID_ATLAS7);
  401. break;
  402. case SIRFSOC_DMA_VER_A6:
  403. writel_relaxed(readl_relaxed(sdma->base + SIRFSOC_DMA_INT_EN) &
  404. ~(1 << cid), sdma->base + SIRFSOC_DMA_INT_EN);
  405. writel_relaxed(readl_relaxed(sdma->base +
  406. SIRFSOC_DMA_CH_LOOP_CTRL) &
  407. ~((1 << cid) | 1 << (cid + 16)),
  408. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  409. writel_relaxed(1 << cid, sdma->base + SIRFSOC_DMA_CH_VALID);
  410. break;
  411. default:
  412. break;
  413. }
  414. list_splice_tail_init(&schan->active, &schan->free);
  415. list_splice_tail_init(&schan->queued, &schan->free);
  416. spin_unlock_irqrestore(&schan->lock, flags);
  417. return 0;
  418. }
  419. static int sirfsoc_dma_pause_chan(struct dma_chan *chan)
  420. {
  421. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  422. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  423. int cid = schan->chan.chan_id;
  424. unsigned long flags;
  425. spin_lock_irqsave(&schan->lock, flags);
  426. switch (sdma->type) {
  427. case SIRFSOC_DMA_VER_A7V1:
  428. writel_relaxed((1 << cid) | 1 << (cid + 16),
  429. sdma->base +
  430. SIRFSOC_DMA_CH_LOOP_CTRL_CLR_ATLAS7);
  431. break;
  432. case SIRFSOC_DMA_VER_A7V2:
  433. writel_relaxed(0, sdma->base + SIRFSOC_DMA_LOOP_CTRL_ATLAS7);
  434. break;
  435. case SIRFSOC_DMA_VER_A6:
  436. writel_relaxed(readl_relaxed(sdma->base +
  437. SIRFSOC_DMA_CH_LOOP_CTRL) &
  438. ~((1 << cid) | 1 << (cid + 16)),
  439. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  440. break;
  441. default:
  442. break;
  443. }
  444. spin_unlock_irqrestore(&schan->lock, flags);
  445. return 0;
  446. }
  447. static int sirfsoc_dma_resume_chan(struct dma_chan *chan)
  448. {
  449. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  450. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(&schan->chan);
  451. int cid = schan->chan.chan_id;
  452. unsigned long flags;
  453. spin_lock_irqsave(&schan->lock, flags);
  454. switch (sdma->type) {
  455. case SIRFSOC_DMA_VER_A7V1:
  456. writel_relaxed((1 << cid) | 1 << (cid + 16),
  457. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL_ATLAS7);
  458. break;
  459. case SIRFSOC_DMA_VER_A7V2:
  460. writel_relaxed(0x10001,
  461. sdma->base + SIRFSOC_DMA_LOOP_CTRL_ATLAS7);
  462. break;
  463. case SIRFSOC_DMA_VER_A6:
  464. writel_relaxed(readl_relaxed(sdma->base +
  465. SIRFSOC_DMA_CH_LOOP_CTRL) |
  466. ((1 << cid) | 1 << (cid + 16)),
  467. sdma->base + SIRFSOC_DMA_CH_LOOP_CTRL);
  468. break;
  469. default:
  470. break;
  471. }
  472. spin_unlock_irqrestore(&schan->lock, flags);
  473. return 0;
  474. }
  475. /* Alloc channel resources */
  476. static int sirfsoc_dma_alloc_chan_resources(struct dma_chan *chan)
  477. {
  478. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  479. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  480. struct sirfsoc_dma_desc *sdesc;
  481. unsigned long flags;
  482. LIST_HEAD(descs);
  483. int i;
  484. pm_runtime_get_sync(sdma->dma.dev);
  485. /* Alloc descriptors for this channel */
  486. for (i = 0; i < SIRFSOC_DMA_DESCRIPTORS; i++) {
  487. sdesc = kzalloc(sizeof(*sdesc), GFP_KERNEL);
  488. if (!sdesc) {
  489. dev_notice(sdma->dma.dev, "Memory allocation error. "
  490. "Allocated only %u descriptors\n", i);
  491. break;
  492. }
  493. dma_async_tx_descriptor_init(&sdesc->desc, chan);
  494. sdesc->desc.flags = DMA_CTRL_ACK;
  495. sdesc->desc.tx_submit = sirfsoc_dma_tx_submit;
  496. list_add_tail(&sdesc->node, &descs);
  497. }
  498. /* Return error only if no descriptors were allocated */
  499. if (i == 0)
  500. return -ENOMEM;
  501. spin_lock_irqsave(&schan->lock, flags);
  502. list_splice_tail_init(&descs, &schan->free);
  503. spin_unlock_irqrestore(&schan->lock, flags);
  504. return i;
  505. }
  506. /* Free channel resources */
  507. static void sirfsoc_dma_free_chan_resources(struct dma_chan *chan)
  508. {
  509. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  510. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  511. struct sirfsoc_dma_desc *sdesc, *tmp;
  512. unsigned long flags;
  513. LIST_HEAD(descs);
  514. spin_lock_irqsave(&schan->lock, flags);
  515. /* Channel must be idle */
  516. BUG_ON(!list_empty(&schan->prepared));
  517. BUG_ON(!list_empty(&schan->queued));
  518. BUG_ON(!list_empty(&schan->active));
  519. BUG_ON(!list_empty(&schan->completed));
  520. /* Move data */
  521. list_splice_tail_init(&schan->free, &descs);
  522. spin_unlock_irqrestore(&schan->lock, flags);
  523. /* Free descriptors */
  524. list_for_each_entry_safe(sdesc, tmp, &descs, node)
  525. kfree(sdesc);
  526. pm_runtime_put(sdma->dma.dev);
  527. }
  528. /* Send pending descriptor to hardware */
  529. static void sirfsoc_dma_issue_pending(struct dma_chan *chan)
  530. {
  531. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  532. unsigned long flags;
  533. spin_lock_irqsave(&schan->lock, flags);
  534. if (list_empty(&schan->active) && !list_empty(&schan->queued))
  535. sirfsoc_dma_execute(schan);
  536. spin_unlock_irqrestore(&schan->lock, flags);
  537. }
  538. /* Check request completion status */
  539. static enum dma_status
  540. sirfsoc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  541. struct dma_tx_state *txstate)
  542. {
  543. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  544. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  545. unsigned long flags;
  546. enum dma_status ret;
  547. struct sirfsoc_dma_desc *sdesc;
  548. int cid = schan->chan.chan_id;
  549. unsigned long dma_pos;
  550. unsigned long dma_request_bytes;
  551. unsigned long residue;
  552. spin_lock_irqsave(&schan->lock, flags);
  553. if (list_empty(&schan->active)) {
  554. ret = dma_cookie_status(chan, cookie, txstate);
  555. dma_set_residue(txstate, 0);
  556. spin_unlock_irqrestore(&schan->lock, flags);
  557. return ret;
  558. }
  559. sdesc = list_first_entry(&schan->active, struct sirfsoc_dma_desc, node);
  560. if (sdesc->cyclic)
  561. dma_request_bytes = (sdesc->xlen + 1) * (sdesc->ylen + 1) *
  562. (sdesc->width * SIRFSOC_DMA_WORD_LEN);
  563. else
  564. dma_request_bytes = sdesc->xlen * SIRFSOC_DMA_WORD_LEN;
  565. ret = dma_cookie_status(chan, cookie, txstate);
  566. if (sdma->type == SIRFSOC_DMA_VER_A7V2)
  567. cid = 0;
  568. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  569. dma_pos = readl_relaxed(sdma->base + SIRFSOC_DMA_CUR_DATA_ADDR);
  570. } else {
  571. dma_pos = readl_relaxed(
  572. sdma->base + cid * 0x10 + SIRFSOC_DMA_CH_ADDR) << 2;
  573. }
  574. residue = dma_request_bytes - (dma_pos - sdesc->addr);
  575. dma_set_residue(txstate, residue);
  576. spin_unlock_irqrestore(&schan->lock, flags);
  577. return ret;
  578. }
  579. static struct dma_async_tx_descriptor *sirfsoc_dma_prep_interleaved(
  580. struct dma_chan *chan, struct dma_interleaved_template *xt,
  581. unsigned long flags)
  582. {
  583. struct sirfsoc_dma *sdma = dma_chan_to_sirfsoc_dma(chan);
  584. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  585. struct sirfsoc_dma_desc *sdesc = NULL;
  586. unsigned long iflags;
  587. int ret;
  588. if ((xt->dir != DMA_MEM_TO_DEV) && (xt->dir != DMA_DEV_TO_MEM)) {
  589. ret = -EINVAL;
  590. goto err_dir;
  591. }
  592. /* Get free descriptor */
  593. spin_lock_irqsave(&schan->lock, iflags);
  594. if (!list_empty(&schan->free)) {
  595. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  596. node);
  597. list_del(&sdesc->node);
  598. }
  599. spin_unlock_irqrestore(&schan->lock, iflags);
  600. if (!sdesc) {
  601. /* try to free completed descriptors */
  602. sirfsoc_dma_process_completed(sdma);
  603. ret = 0;
  604. goto no_desc;
  605. }
  606. /* Place descriptor in prepared list */
  607. spin_lock_irqsave(&schan->lock, iflags);
  608. /*
  609. * Number of chunks in a frame can only be 1 for prima2
  610. * and ylen (number of frame - 1) must be at least 0
  611. */
  612. if ((xt->frame_size == 1) && (xt->numf > 0)) {
  613. sdesc->cyclic = 0;
  614. sdesc->xlen = xt->sgl[0].size / SIRFSOC_DMA_WORD_LEN;
  615. sdesc->width = (xt->sgl[0].size + xt->sgl[0].icg) /
  616. SIRFSOC_DMA_WORD_LEN;
  617. sdesc->ylen = xt->numf - 1;
  618. if (xt->dir == DMA_MEM_TO_DEV) {
  619. sdesc->addr = xt->src_start;
  620. sdesc->dir = 1;
  621. } else {
  622. sdesc->addr = xt->dst_start;
  623. sdesc->dir = 0;
  624. }
  625. list_add_tail(&sdesc->node, &schan->prepared);
  626. } else {
  627. pr_err("sirfsoc DMA Invalid xfer\n");
  628. ret = -EINVAL;
  629. goto err_xfer;
  630. }
  631. spin_unlock_irqrestore(&schan->lock, iflags);
  632. return &sdesc->desc;
  633. err_xfer:
  634. spin_unlock_irqrestore(&schan->lock, iflags);
  635. no_desc:
  636. err_dir:
  637. return ERR_PTR(ret);
  638. }
  639. static struct dma_async_tx_descriptor *
  640. sirfsoc_dma_prep_cyclic(struct dma_chan *chan, dma_addr_t addr,
  641. size_t buf_len, size_t period_len,
  642. enum dma_transfer_direction direction, unsigned long flags)
  643. {
  644. struct sirfsoc_dma_chan *schan = dma_chan_to_sirfsoc_dma_chan(chan);
  645. struct sirfsoc_dma_desc *sdesc = NULL;
  646. unsigned long iflags;
  647. /*
  648. * we only support cycle transfer with 2 period
  649. * If the X-length is set to 0, it would be the loop mode.
  650. * The DMA address keeps increasing until reaching the end of a loop
  651. * area whose size is defined by (DMA_WIDTH x (Y_LENGTH + 1)). Then
  652. * the DMA address goes back to the beginning of this area.
  653. * In loop mode, the DMA data region is divided into two parts, BUFA
  654. * and BUFB. DMA controller generates interrupts twice in each loop:
  655. * when the DMA address reaches the end of BUFA or the end of the
  656. * BUFB
  657. */
  658. if (buf_len != 2 * period_len)
  659. return ERR_PTR(-EINVAL);
  660. /* Get free descriptor */
  661. spin_lock_irqsave(&schan->lock, iflags);
  662. if (!list_empty(&schan->free)) {
  663. sdesc = list_first_entry(&schan->free, struct sirfsoc_dma_desc,
  664. node);
  665. list_del(&sdesc->node);
  666. }
  667. spin_unlock_irqrestore(&schan->lock, iflags);
  668. if (!sdesc)
  669. return NULL;
  670. /* Place descriptor in prepared list */
  671. spin_lock_irqsave(&schan->lock, iflags);
  672. sdesc->addr = addr;
  673. sdesc->cyclic = 1;
  674. sdesc->xlen = 0;
  675. sdesc->ylen = buf_len / SIRFSOC_DMA_WORD_LEN - 1;
  676. sdesc->width = 1;
  677. list_add_tail(&sdesc->node, &schan->prepared);
  678. spin_unlock_irqrestore(&schan->lock, iflags);
  679. return &sdesc->desc;
  680. }
  681. /*
  682. * The DMA controller consists of 16 independent DMA channels.
  683. * Each channel is allocated to a different function
  684. */
  685. bool sirfsoc_dma_filter_id(struct dma_chan *chan, void *chan_id)
  686. {
  687. unsigned int ch_nr = (unsigned int) chan_id;
  688. if (ch_nr == chan->chan_id +
  689. chan->device->dev_id * SIRFSOC_DMA_CHANNELS)
  690. return true;
  691. return false;
  692. }
  693. EXPORT_SYMBOL(sirfsoc_dma_filter_id);
  694. #define SIRFSOC_DMA_BUSWIDTHS \
  695. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  696. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  697. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  698. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  699. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  700. static struct dma_chan *of_dma_sirfsoc_xlate(struct of_phandle_args *dma_spec,
  701. struct of_dma *ofdma)
  702. {
  703. struct sirfsoc_dma *sdma = ofdma->of_dma_data;
  704. unsigned int request = dma_spec->args[0];
  705. if (request >= SIRFSOC_DMA_CHANNELS)
  706. return NULL;
  707. return dma_get_slave_channel(&sdma->channels[request].chan);
  708. }
  709. static int sirfsoc_dma_probe(struct platform_device *op)
  710. {
  711. struct device_node *dn = op->dev.of_node;
  712. struct device *dev = &op->dev;
  713. struct dma_device *dma;
  714. struct sirfsoc_dma *sdma;
  715. struct sirfsoc_dma_chan *schan;
  716. struct sirfsoc_dmadata *data;
  717. struct resource res;
  718. ulong regs_start, regs_size;
  719. u32 id;
  720. int ret, i;
  721. sdma = devm_kzalloc(dev, sizeof(*sdma), GFP_KERNEL);
  722. if (!sdma)
  723. return -ENOMEM;
  724. data = (struct sirfsoc_dmadata *)
  725. (of_match_device(op->dev.driver->of_match_table,
  726. &op->dev)->data);
  727. sdma->exec_desc = data->exec;
  728. sdma->type = data->type;
  729. if (of_property_read_u32(dn, "cell-index", &id)) {
  730. dev_err(dev, "Fail to get DMAC index\n");
  731. return -ENODEV;
  732. }
  733. sdma->irq = irq_of_parse_and_map(dn, 0);
  734. if (!sdma->irq) {
  735. dev_err(dev, "Error mapping IRQ!\n");
  736. return -EINVAL;
  737. }
  738. sdma->clk = devm_clk_get(dev, NULL);
  739. if (IS_ERR(sdma->clk)) {
  740. dev_err(dev, "failed to get a clock.\n");
  741. return PTR_ERR(sdma->clk);
  742. }
  743. ret = of_address_to_resource(dn, 0, &res);
  744. if (ret) {
  745. dev_err(dev, "Error parsing memory region!\n");
  746. goto irq_dispose;
  747. }
  748. regs_start = res.start;
  749. regs_size = resource_size(&res);
  750. sdma->base = devm_ioremap(dev, regs_start, regs_size);
  751. if (!sdma->base) {
  752. dev_err(dev, "Error mapping memory region!\n");
  753. ret = -ENOMEM;
  754. goto irq_dispose;
  755. }
  756. ret = request_irq(sdma->irq, &sirfsoc_dma_irq, 0, DRV_NAME, sdma);
  757. if (ret) {
  758. dev_err(dev, "Error requesting IRQ!\n");
  759. ret = -EINVAL;
  760. goto irq_dispose;
  761. }
  762. dma = &sdma->dma;
  763. dma->dev = dev;
  764. dma->device_alloc_chan_resources = sirfsoc_dma_alloc_chan_resources;
  765. dma->device_free_chan_resources = sirfsoc_dma_free_chan_resources;
  766. dma->device_issue_pending = sirfsoc_dma_issue_pending;
  767. dma->device_config = sirfsoc_dma_slave_config;
  768. dma->device_pause = sirfsoc_dma_pause_chan;
  769. dma->device_resume = sirfsoc_dma_resume_chan;
  770. dma->device_terminate_all = sirfsoc_dma_terminate_all;
  771. dma->device_tx_status = sirfsoc_dma_tx_status;
  772. dma->device_prep_interleaved_dma = sirfsoc_dma_prep_interleaved;
  773. dma->device_prep_dma_cyclic = sirfsoc_dma_prep_cyclic;
  774. dma->src_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
  775. dma->dst_addr_widths = SIRFSOC_DMA_BUSWIDTHS;
  776. dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  777. INIT_LIST_HEAD(&dma->channels);
  778. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  779. dma_cap_set(DMA_CYCLIC, dma->cap_mask);
  780. dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
  781. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  782. for (i = 0; i < SIRFSOC_DMA_CHANNELS; i++) {
  783. schan = &sdma->channels[i];
  784. schan->chan.device = dma;
  785. dma_cookie_init(&schan->chan);
  786. INIT_LIST_HEAD(&schan->free);
  787. INIT_LIST_HEAD(&schan->prepared);
  788. INIT_LIST_HEAD(&schan->queued);
  789. INIT_LIST_HEAD(&schan->active);
  790. INIT_LIST_HEAD(&schan->completed);
  791. spin_lock_init(&schan->lock);
  792. list_add_tail(&schan->chan.device_node, &dma->channels);
  793. }
  794. tasklet_init(&sdma->tasklet, sirfsoc_dma_tasklet, (unsigned long)sdma);
  795. /* Register DMA engine */
  796. dev_set_drvdata(dev, sdma);
  797. ret = dma_async_device_register(dma);
  798. if (ret)
  799. goto free_irq;
  800. /* Device-tree DMA controller registration */
  801. ret = of_dma_controller_register(dn, of_dma_sirfsoc_xlate, sdma);
  802. if (ret) {
  803. dev_err(dev, "failed to register DMA controller\n");
  804. goto unreg_dma_dev;
  805. }
  806. pm_runtime_enable(&op->dev);
  807. dev_info(dev, "initialized SIRFSOC DMAC driver\n");
  808. return 0;
  809. unreg_dma_dev:
  810. dma_async_device_unregister(dma);
  811. free_irq:
  812. free_irq(sdma->irq, sdma);
  813. irq_dispose:
  814. irq_dispose_mapping(sdma->irq);
  815. return ret;
  816. }
  817. static int sirfsoc_dma_remove(struct platform_device *op)
  818. {
  819. struct device *dev = &op->dev;
  820. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  821. of_dma_controller_free(op->dev.of_node);
  822. dma_async_device_unregister(&sdma->dma);
  823. free_irq(sdma->irq, sdma);
  824. tasklet_kill(&sdma->tasklet);
  825. irq_dispose_mapping(sdma->irq);
  826. pm_runtime_disable(&op->dev);
  827. if (!pm_runtime_status_suspended(&op->dev))
  828. sirfsoc_dma_runtime_suspend(&op->dev);
  829. return 0;
  830. }
  831. static int __maybe_unused sirfsoc_dma_runtime_suspend(struct device *dev)
  832. {
  833. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  834. clk_disable_unprepare(sdma->clk);
  835. return 0;
  836. }
  837. static int __maybe_unused sirfsoc_dma_runtime_resume(struct device *dev)
  838. {
  839. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  840. int ret;
  841. ret = clk_prepare_enable(sdma->clk);
  842. if (ret < 0) {
  843. dev_err(dev, "clk_enable failed: %d\n", ret);
  844. return ret;
  845. }
  846. return 0;
  847. }
  848. static int __maybe_unused sirfsoc_dma_pm_suspend(struct device *dev)
  849. {
  850. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  851. struct sirfsoc_dma_regs *save = &sdma->regs_save;
  852. struct sirfsoc_dma_desc *sdesc;
  853. struct sirfsoc_dma_chan *schan;
  854. int ch;
  855. int ret;
  856. int count;
  857. u32 int_offset;
  858. /*
  859. * if we were runtime-suspended before, resume to enable clock
  860. * before accessing register
  861. */
  862. if (pm_runtime_status_suspended(dev)) {
  863. ret = sirfsoc_dma_runtime_resume(dev);
  864. if (ret < 0)
  865. return ret;
  866. }
  867. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  868. count = 1;
  869. int_offset = SIRFSOC_DMA_INT_EN_ATLAS7;
  870. } else {
  871. count = SIRFSOC_DMA_CHANNELS;
  872. int_offset = SIRFSOC_DMA_INT_EN;
  873. }
  874. /*
  875. * DMA controller will lose all registers while suspending
  876. * so we need to save registers for active channels
  877. */
  878. for (ch = 0; ch < count; ch++) {
  879. schan = &sdma->channels[ch];
  880. if (list_empty(&schan->active))
  881. continue;
  882. sdesc = list_first_entry(&schan->active,
  883. struct sirfsoc_dma_desc,
  884. node);
  885. save->ctrl[ch] = readl_relaxed(sdma->base +
  886. ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  887. }
  888. save->interrupt_en = readl_relaxed(sdma->base + int_offset);
  889. /* Disable clock */
  890. sirfsoc_dma_runtime_suspend(dev);
  891. return 0;
  892. }
  893. static int __maybe_unused sirfsoc_dma_pm_resume(struct device *dev)
  894. {
  895. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  896. struct sirfsoc_dma_regs *save = &sdma->regs_save;
  897. struct sirfsoc_dma_desc *sdesc;
  898. struct sirfsoc_dma_chan *schan;
  899. int ch;
  900. int ret;
  901. int count;
  902. u32 int_offset;
  903. u32 width_offset;
  904. /* Enable clock before accessing register */
  905. ret = sirfsoc_dma_runtime_resume(dev);
  906. if (ret < 0)
  907. return ret;
  908. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  909. count = 1;
  910. int_offset = SIRFSOC_DMA_INT_EN_ATLAS7;
  911. width_offset = SIRFSOC_DMA_WIDTH_ATLAS7;
  912. } else {
  913. count = SIRFSOC_DMA_CHANNELS;
  914. int_offset = SIRFSOC_DMA_INT_EN;
  915. width_offset = SIRFSOC_DMA_WIDTH_0;
  916. }
  917. writel_relaxed(save->interrupt_en, sdma->base + int_offset);
  918. for (ch = 0; ch < count; ch++) {
  919. schan = &sdma->channels[ch];
  920. if (list_empty(&schan->active))
  921. continue;
  922. sdesc = list_first_entry(&schan->active,
  923. struct sirfsoc_dma_desc,
  924. node);
  925. writel_relaxed(sdesc->width,
  926. sdma->base + width_offset + ch * 4);
  927. writel_relaxed(sdesc->xlen,
  928. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_XLEN);
  929. writel_relaxed(sdesc->ylen,
  930. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_YLEN);
  931. writel_relaxed(save->ctrl[ch],
  932. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  933. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  934. writel_relaxed(sdesc->addr,
  935. sdma->base + SIRFSOC_DMA_CH_ADDR);
  936. } else {
  937. writel_relaxed(sdesc->addr >> 2,
  938. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_ADDR);
  939. }
  940. }
  941. /* if we were runtime-suspended before, suspend again */
  942. if (pm_runtime_status_suspended(dev))
  943. sirfsoc_dma_runtime_suspend(dev);
  944. return 0;
  945. }
  946. static const struct dev_pm_ops sirfsoc_dma_pm_ops = {
  947. SET_RUNTIME_PM_OPS(sirfsoc_dma_runtime_suspend, sirfsoc_dma_runtime_resume, NULL)
  948. SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_dma_pm_suspend, sirfsoc_dma_pm_resume)
  949. };
  950. static struct sirfsoc_dmadata sirfsoc_dmadata_a6 = {
  951. .exec = sirfsoc_dma_execute_hw_a6,
  952. .type = SIRFSOC_DMA_VER_A6,
  953. };
  954. static struct sirfsoc_dmadata sirfsoc_dmadata_a7v1 = {
  955. .exec = sirfsoc_dma_execute_hw_a7v1,
  956. .type = SIRFSOC_DMA_VER_A7V1,
  957. };
  958. static struct sirfsoc_dmadata sirfsoc_dmadata_a7v2 = {
  959. .exec = sirfsoc_dma_execute_hw_a7v2,
  960. .type = SIRFSOC_DMA_VER_A7V2,
  961. };
  962. static const struct of_device_id sirfsoc_dma_match[] = {
  963. { .compatible = "sirf,prima2-dmac", .data = &sirfsoc_dmadata_a6,},
  964. { .compatible = "sirf,atlas7-dmac", .data = &sirfsoc_dmadata_a7v1,},
  965. { .compatible = "sirf,atlas7-dmac-v2", .data = &sirfsoc_dmadata_a7v2,},
  966. {},
  967. };
  968. MODULE_DEVICE_TABLE(of, sirfsoc_dma_match);
  969. static struct platform_driver sirfsoc_dma_driver = {
  970. .probe = sirfsoc_dma_probe,
  971. .remove = sirfsoc_dma_remove,
  972. .driver = {
  973. .name = DRV_NAME,
  974. .pm = &sirfsoc_dma_pm_ops,
  975. .of_match_table = sirfsoc_dma_match,
  976. },
  977. };
  978. static __init int sirfsoc_dma_init(void)
  979. {
  980. return platform_driver_register(&sirfsoc_dma_driver);
  981. }
  982. static void __exit sirfsoc_dma_exit(void)
  983. {
  984. platform_driver_unregister(&sirfsoc_dma_driver);
  985. }
  986. subsys_initcall(sirfsoc_dma_init);
  987. module_exit(sirfsoc_dma_exit);
  988. MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>");
  989. MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
  990. MODULE_DESCRIPTION("SIRFSOC DMA control driver");
  991. MODULE_LICENSE("GPL v2");