sirf-dma.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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_chan *schan;
  853. int ch;
  854. int ret;
  855. int count;
  856. u32 int_offset;
  857. /*
  858. * if we were runtime-suspended before, resume to enable clock
  859. * before accessing register
  860. */
  861. if (pm_runtime_status_suspended(dev)) {
  862. ret = sirfsoc_dma_runtime_resume(dev);
  863. if (ret < 0)
  864. return ret;
  865. }
  866. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  867. count = 1;
  868. int_offset = SIRFSOC_DMA_INT_EN_ATLAS7;
  869. } else {
  870. count = SIRFSOC_DMA_CHANNELS;
  871. int_offset = SIRFSOC_DMA_INT_EN;
  872. }
  873. /*
  874. * DMA controller will lose all registers while suspending
  875. * so we need to save registers for active channels
  876. */
  877. for (ch = 0; ch < count; ch++) {
  878. schan = &sdma->channels[ch];
  879. if (list_empty(&schan->active))
  880. continue;
  881. save->ctrl[ch] = readl_relaxed(sdma->base +
  882. ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  883. }
  884. save->interrupt_en = readl_relaxed(sdma->base + int_offset);
  885. /* Disable clock */
  886. sirfsoc_dma_runtime_suspend(dev);
  887. return 0;
  888. }
  889. static int __maybe_unused sirfsoc_dma_pm_resume(struct device *dev)
  890. {
  891. struct sirfsoc_dma *sdma = dev_get_drvdata(dev);
  892. struct sirfsoc_dma_regs *save = &sdma->regs_save;
  893. struct sirfsoc_dma_desc *sdesc;
  894. struct sirfsoc_dma_chan *schan;
  895. int ch;
  896. int ret;
  897. int count;
  898. u32 int_offset;
  899. u32 width_offset;
  900. /* Enable clock before accessing register */
  901. ret = sirfsoc_dma_runtime_resume(dev);
  902. if (ret < 0)
  903. return ret;
  904. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  905. count = 1;
  906. int_offset = SIRFSOC_DMA_INT_EN_ATLAS7;
  907. width_offset = SIRFSOC_DMA_WIDTH_ATLAS7;
  908. } else {
  909. count = SIRFSOC_DMA_CHANNELS;
  910. int_offset = SIRFSOC_DMA_INT_EN;
  911. width_offset = SIRFSOC_DMA_WIDTH_0;
  912. }
  913. writel_relaxed(save->interrupt_en, sdma->base + int_offset);
  914. for (ch = 0; ch < count; ch++) {
  915. schan = &sdma->channels[ch];
  916. if (list_empty(&schan->active))
  917. continue;
  918. sdesc = list_first_entry(&schan->active,
  919. struct sirfsoc_dma_desc,
  920. node);
  921. writel_relaxed(sdesc->width,
  922. sdma->base + width_offset + ch * 4);
  923. writel_relaxed(sdesc->xlen,
  924. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_XLEN);
  925. writel_relaxed(sdesc->ylen,
  926. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_YLEN);
  927. writel_relaxed(save->ctrl[ch],
  928. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_CTRL);
  929. if (sdma->type == SIRFSOC_DMA_VER_A7V2) {
  930. writel_relaxed(sdesc->addr,
  931. sdma->base + SIRFSOC_DMA_CH_ADDR);
  932. } else {
  933. writel_relaxed(sdesc->addr >> 2,
  934. sdma->base + ch * 0x10 + SIRFSOC_DMA_CH_ADDR);
  935. }
  936. }
  937. /* if we were runtime-suspended before, suspend again */
  938. if (pm_runtime_status_suspended(dev))
  939. sirfsoc_dma_runtime_suspend(dev);
  940. return 0;
  941. }
  942. static const struct dev_pm_ops sirfsoc_dma_pm_ops = {
  943. SET_RUNTIME_PM_OPS(sirfsoc_dma_runtime_suspend, sirfsoc_dma_runtime_resume, NULL)
  944. SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_dma_pm_suspend, sirfsoc_dma_pm_resume)
  945. };
  946. static struct sirfsoc_dmadata sirfsoc_dmadata_a6 = {
  947. .exec = sirfsoc_dma_execute_hw_a6,
  948. .type = SIRFSOC_DMA_VER_A6,
  949. };
  950. static struct sirfsoc_dmadata sirfsoc_dmadata_a7v1 = {
  951. .exec = sirfsoc_dma_execute_hw_a7v1,
  952. .type = SIRFSOC_DMA_VER_A7V1,
  953. };
  954. static struct sirfsoc_dmadata sirfsoc_dmadata_a7v2 = {
  955. .exec = sirfsoc_dma_execute_hw_a7v2,
  956. .type = SIRFSOC_DMA_VER_A7V2,
  957. };
  958. static const struct of_device_id sirfsoc_dma_match[] = {
  959. { .compatible = "sirf,prima2-dmac", .data = &sirfsoc_dmadata_a6,},
  960. { .compatible = "sirf,atlas7-dmac", .data = &sirfsoc_dmadata_a7v1,},
  961. { .compatible = "sirf,atlas7-dmac-v2", .data = &sirfsoc_dmadata_a7v2,},
  962. {},
  963. };
  964. MODULE_DEVICE_TABLE(of, sirfsoc_dma_match);
  965. static struct platform_driver sirfsoc_dma_driver = {
  966. .probe = sirfsoc_dma_probe,
  967. .remove = sirfsoc_dma_remove,
  968. .driver = {
  969. .name = DRV_NAME,
  970. .pm = &sirfsoc_dma_pm_ops,
  971. .of_match_table = sirfsoc_dma_match,
  972. },
  973. };
  974. static __init int sirfsoc_dma_init(void)
  975. {
  976. return platform_driver_register(&sirfsoc_dma_driver);
  977. }
  978. static void __exit sirfsoc_dma_exit(void)
  979. {
  980. platform_driver_unregister(&sirfsoc_dma_driver);
  981. }
  982. subsys_initcall(sirfsoc_dma_init);
  983. module_exit(sirfsoc_dma_exit);
  984. MODULE_AUTHOR("Rongjun Ying <rongjun.ying@csr.com>");
  985. MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
  986. MODULE_DESCRIPTION("SIRFSOC DMA control driver");
  987. MODULE_LICENSE("GPL v2");