cppi41.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. #include <linux/delay.h>
  2. #include <linux/dmaengine.h>
  3. #include <linux/dma-mapping.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/module.h>
  6. #include <linux/of.h>
  7. #include <linux/slab.h>
  8. #include <linux/of_dma.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/dmapool.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/of_address.h>
  13. #include <linux/pm_runtime.h>
  14. #include "../dmaengine.h"
  15. #define DESC_TYPE 27
  16. #define DESC_TYPE_HOST 0x10
  17. #define DESC_TYPE_TEARD 0x13
  18. #define TD_DESC_IS_RX (1 << 16)
  19. #define TD_DESC_DMA_NUM 10
  20. #define DESC_LENGTH_BITS_NUM 21
  21. #define DESC_TYPE_USB (5 << 26)
  22. #define DESC_PD_COMPLETE (1 << 31)
  23. /* DMA engine */
  24. #define DMA_TDFDQ 4
  25. #define DMA_TXGCR(x) (0x800 + (x) * 0x20)
  26. #define DMA_RXGCR(x) (0x808 + (x) * 0x20)
  27. #define RXHPCRA0 4
  28. #define GCR_CHAN_ENABLE (1 << 31)
  29. #define GCR_TEARDOWN (1 << 30)
  30. #define GCR_STARV_RETRY (1 << 24)
  31. #define GCR_DESC_TYPE_HOST (1 << 14)
  32. /* DMA scheduler */
  33. #define DMA_SCHED_CTRL 0
  34. #define DMA_SCHED_CTRL_EN (1 << 31)
  35. #define DMA_SCHED_WORD(x) ((x) * 4 + 0x800)
  36. #define SCHED_ENTRY0_CHAN(x) ((x) << 0)
  37. #define SCHED_ENTRY0_IS_RX (1 << 7)
  38. #define SCHED_ENTRY1_CHAN(x) ((x) << 8)
  39. #define SCHED_ENTRY1_IS_RX (1 << 15)
  40. #define SCHED_ENTRY2_CHAN(x) ((x) << 16)
  41. #define SCHED_ENTRY2_IS_RX (1 << 23)
  42. #define SCHED_ENTRY3_CHAN(x) ((x) << 24)
  43. #define SCHED_ENTRY3_IS_RX (1 << 31)
  44. /* Queue manager */
  45. /* 4 KiB of memory for descriptors, 2 for each endpoint */
  46. #define ALLOC_DECS_NUM 128
  47. #define DESCS_AREAS 1
  48. #define TOTAL_DESCS_NUM (ALLOC_DECS_NUM * DESCS_AREAS)
  49. #define QMGR_SCRATCH_SIZE (TOTAL_DESCS_NUM * 4)
  50. #define QMGR_LRAM0_BASE 0x80
  51. #define QMGR_LRAM_SIZE 0x84
  52. #define QMGR_LRAM1_BASE 0x88
  53. #define QMGR_MEMBASE(x) (0x1000 + (x) * 0x10)
  54. #define QMGR_MEMCTRL(x) (0x1004 + (x) * 0x10)
  55. #define QMGR_MEMCTRL_IDX_SH 16
  56. #define QMGR_MEMCTRL_DESC_SH 8
  57. #define QMGR_PEND(x) (0x90 + (x) * 4)
  58. #define QMGR_PENDING_SLOT_Q(x) (x / 32)
  59. #define QMGR_PENDING_BIT_Q(x) (x % 32)
  60. #define QMGR_QUEUE_A(n) (0x2000 + (n) * 0x10)
  61. #define QMGR_QUEUE_B(n) (0x2004 + (n) * 0x10)
  62. #define QMGR_QUEUE_C(n) (0x2008 + (n) * 0x10)
  63. #define QMGR_QUEUE_D(n) (0x200c + (n) * 0x10)
  64. /* Packet Descriptor */
  65. #define PD2_ZERO_LENGTH (1 << 19)
  66. struct cppi41_channel {
  67. struct dma_chan chan;
  68. struct dma_async_tx_descriptor txd;
  69. struct cppi41_dd *cdd;
  70. struct cppi41_desc *desc;
  71. dma_addr_t desc_phys;
  72. void __iomem *gcr_reg;
  73. int is_tx;
  74. u32 residue;
  75. unsigned int q_num;
  76. unsigned int q_comp_num;
  77. unsigned int port_num;
  78. unsigned td_retry;
  79. unsigned td_queued:1;
  80. unsigned td_seen:1;
  81. unsigned td_desc_seen:1;
  82. struct list_head node; /* Node for pending list */
  83. };
  84. struct cppi41_desc {
  85. u32 pd0;
  86. u32 pd1;
  87. u32 pd2;
  88. u32 pd3;
  89. u32 pd4;
  90. u32 pd5;
  91. u32 pd6;
  92. u32 pd7;
  93. } __aligned(32);
  94. struct chan_queues {
  95. u16 submit;
  96. u16 complete;
  97. };
  98. struct cppi41_dd {
  99. struct dma_device ddev;
  100. void *qmgr_scratch;
  101. dma_addr_t scratch_phys;
  102. struct cppi41_desc *cd;
  103. dma_addr_t descs_phys;
  104. u32 first_td_desc;
  105. struct cppi41_channel *chan_busy[ALLOC_DECS_NUM];
  106. void __iomem *ctrl_mem;
  107. void __iomem *sched_mem;
  108. void __iomem *qmgr_mem;
  109. unsigned int irq;
  110. const struct chan_queues *queues_rx;
  111. const struct chan_queues *queues_tx;
  112. struct chan_queues td_queue;
  113. u16 first_completion_queue;
  114. u16 qmgr_num_pend;
  115. u32 n_chans;
  116. u8 platform;
  117. struct list_head pending; /* Pending queued transfers */
  118. spinlock_t lock; /* Lock for pending list */
  119. /* context for suspend/resume */
  120. unsigned int dma_tdfdq;
  121. bool is_suspended;
  122. };
  123. static struct chan_queues am335x_usb_queues_tx[] = {
  124. /* USB0 ENDP 1 */
  125. [ 0] = { .submit = 32, .complete = 93},
  126. [ 1] = { .submit = 34, .complete = 94},
  127. [ 2] = { .submit = 36, .complete = 95},
  128. [ 3] = { .submit = 38, .complete = 96},
  129. [ 4] = { .submit = 40, .complete = 97},
  130. [ 5] = { .submit = 42, .complete = 98},
  131. [ 6] = { .submit = 44, .complete = 99},
  132. [ 7] = { .submit = 46, .complete = 100},
  133. [ 8] = { .submit = 48, .complete = 101},
  134. [ 9] = { .submit = 50, .complete = 102},
  135. [10] = { .submit = 52, .complete = 103},
  136. [11] = { .submit = 54, .complete = 104},
  137. [12] = { .submit = 56, .complete = 105},
  138. [13] = { .submit = 58, .complete = 106},
  139. [14] = { .submit = 60, .complete = 107},
  140. /* USB1 ENDP1 */
  141. [15] = { .submit = 62, .complete = 125},
  142. [16] = { .submit = 64, .complete = 126},
  143. [17] = { .submit = 66, .complete = 127},
  144. [18] = { .submit = 68, .complete = 128},
  145. [19] = { .submit = 70, .complete = 129},
  146. [20] = { .submit = 72, .complete = 130},
  147. [21] = { .submit = 74, .complete = 131},
  148. [22] = { .submit = 76, .complete = 132},
  149. [23] = { .submit = 78, .complete = 133},
  150. [24] = { .submit = 80, .complete = 134},
  151. [25] = { .submit = 82, .complete = 135},
  152. [26] = { .submit = 84, .complete = 136},
  153. [27] = { .submit = 86, .complete = 137},
  154. [28] = { .submit = 88, .complete = 138},
  155. [29] = { .submit = 90, .complete = 139},
  156. };
  157. static const struct chan_queues am335x_usb_queues_rx[] = {
  158. /* USB0 ENDP 1 */
  159. [ 0] = { .submit = 1, .complete = 109},
  160. [ 1] = { .submit = 2, .complete = 110},
  161. [ 2] = { .submit = 3, .complete = 111},
  162. [ 3] = { .submit = 4, .complete = 112},
  163. [ 4] = { .submit = 5, .complete = 113},
  164. [ 5] = { .submit = 6, .complete = 114},
  165. [ 6] = { .submit = 7, .complete = 115},
  166. [ 7] = { .submit = 8, .complete = 116},
  167. [ 8] = { .submit = 9, .complete = 117},
  168. [ 9] = { .submit = 10, .complete = 118},
  169. [10] = { .submit = 11, .complete = 119},
  170. [11] = { .submit = 12, .complete = 120},
  171. [12] = { .submit = 13, .complete = 121},
  172. [13] = { .submit = 14, .complete = 122},
  173. [14] = { .submit = 15, .complete = 123},
  174. /* USB1 ENDP 1 */
  175. [15] = { .submit = 16, .complete = 141},
  176. [16] = { .submit = 17, .complete = 142},
  177. [17] = { .submit = 18, .complete = 143},
  178. [18] = { .submit = 19, .complete = 144},
  179. [19] = { .submit = 20, .complete = 145},
  180. [20] = { .submit = 21, .complete = 146},
  181. [21] = { .submit = 22, .complete = 147},
  182. [22] = { .submit = 23, .complete = 148},
  183. [23] = { .submit = 24, .complete = 149},
  184. [24] = { .submit = 25, .complete = 150},
  185. [25] = { .submit = 26, .complete = 151},
  186. [26] = { .submit = 27, .complete = 152},
  187. [27] = { .submit = 28, .complete = 153},
  188. [28] = { .submit = 29, .complete = 154},
  189. [29] = { .submit = 30, .complete = 155},
  190. };
  191. static const struct chan_queues da8xx_usb_queues_tx[] = {
  192. [0] = { .submit = 16, .complete = 24},
  193. [1] = { .submit = 18, .complete = 24},
  194. [2] = { .submit = 20, .complete = 24},
  195. [3] = { .submit = 22, .complete = 24},
  196. };
  197. static const struct chan_queues da8xx_usb_queues_rx[] = {
  198. [0] = { .submit = 1, .complete = 26},
  199. [1] = { .submit = 3, .complete = 26},
  200. [2] = { .submit = 5, .complete = 26},
  201. [3] = { .submit = 7, .complete = 26},
  202. };
  203. struct cppi_glue_infos {
  204. const struct chan_queues *queues_rx;
  205. const struct chan_queues *queues_tx;
  206. struct chan_queues td_queue;
  207. u16 first_completion_queue;
  208. u16 qmgr_num_pend;
  209. };
  210. static struct cppi41_channel *to_cpp41_chan(struct dma_chan *c)
  211. {
  212. return container_of(c, struct cppi41_channel, chan);
  213. }
  214. static struct cppi41_channel *desc_to_chan(struct cppi41_dd *cdd, u32 desc)
  215. {
  216. struct cppi41_channel *c;
  217. u32 descs_size;
  218. u32 desc_num;
  219. descs_size = sizeof(struct cppi41_desc) * ALLOC_DECS_NUM;
  220. if (!((desc >= cdd->descs_phys) &&
  221. (desc < (cdd->descs_phys + descs_size)))) {
  222. return NULL;
  223. }
  224. desc_num = (desc - cdd->descs_phys) / sizeof(struct cppi41_desc);
  225. BUG_ON(desc_num >= ALLOC_DECS_NUM);
  226. c = cdd->chan_busy[desc_num];
  227. cdd->chan_busy[desc_num] = NULL;
  228. /* Usecount for chan_busy[], paired with push_desc_queue() */
  229. pm_runtime_put(cdd->ddev.dev);
  230. return c;
  231. }
  232. static void cppi_writel(u32 val, void *__iomem *mem)
  233. {
  234. __raw_writel(val, mem);
  235. }
  236. static u32 cppi_readl(void *__iomem *mem)
  237. {
  238. return __raw_readl(mem);
  239. }
  240. static u32 pd_trans_len(u32 val)
  241. {
  242. return val & ((1 << (DESC_LENGTH_BITS_NUM + 1)) - 1);
  243. }
  244. static u32 cppi41_pop_desc(struct cppi41_dd *cdd, unsigned queue_num)
  245. {
  246. u32 desc;
  247. desc = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(queue_num));
  248. desc &= ~0x1f;
  249. return desc;
  250. }
  251. static irqreturn_t cppi41_irq(int irq, void *data)
  252. {
  253. struct cppi41_dd *cdd = data;
  254. u16 first_completion_queue = cdd->first_completion_queue;
  255. u16 qmgr_num_pend = cdd->qmgr_num_pend;
  256. struct cppi41_channel *c;
  257. int i;
  258. for (i = QMGR_PENDING_SLOT_Q(first_completion_queue); i < qmgr_num_pend;
  259. i++) {
  260. u32 val;
  261. u32 q_num;
  262. val = cppi_readl(cdd->qmgr_mem + QMGR_PEND(i));
  263. if (i == QMGR_PENDING_SLOT_Q(first_completion_queue) && val) {
  264. u32 mask;
  265. /* set corresponding bit for completetion Q 93 */
  266. mask = 1 << QMGR_PENDING_BIT_Q(first_completion_queue);
  267. /* not set all bits for queues less than Q 93 */
  268. mask--;
  269. /* now invert and keep only Q 93+ set */
  270. val &= ~mask;
  271. }
  272. if (val)
  273. __iormb();
  274. while (val) {
  275. u32 desc, len;
  276. /*
  277. * This should never trigger, see the comments in
  278. * push_desc_queue()
  279. */
  280. WARN_ON(cdd->is_suspended);
  281. q_num = __fls(val);
  282. val &= ~(1 << q_num);
  283. q_num += 32 * i;
  284. desc = cppi41_pop_desc(cdd, q_num);
  285. c = desc_to_chan(cdd, desc);
  286. if (WARN_ON(!c)) {
  287. pr_err("%s() q %d desc %08x\n", __func__,
  288. q_num, desc);
  289. continue;
  290. }
  291. if (c->desc->pd2 & PD2_ZERO_LENGTH)
  292. len = 0;
  293. else
  294. len = pd_trans_len(c->desc->pd0);
  295. c->residue = pd_trans_len(c->desc->pd6) - len;
  296. dma_cookie_complete(&c->txd);
  297. dmaengine_desc_get_callback_invoke(&c->txd, NULL);
  298. }
  299. }
  300. return IRQ_HANDLED;
  301. }
  302. static dma_cookie_t cppi41_tx_submit(struct dma_async_tx_descriptor *tx)
  303. {
  304. dma_cookie_t cookie;
  305. cookie = dma_cookie_assign(tx);
  306. return cookie;
  307. }
  308. static int cppi41_dma_alloc_chan_resources(struct dma_chan *chan)
  309. {
  310. struct cppi41_channel *c = to_cpp41_chan(chan);
  311. struct cppi41_dd *cdd = c->cdd;
  312. int error;
  313. error = pm_runtime_get_sync(cdd->ddev.dev);
  314. if (error < 0) {
  315. dev_err(cdd->ddev.dev, "%s pm runtime get: %i\n",
  316. __func__, error);
  317. pm_runtime_put_noidle(cdd->ddev.dev);
  318. return error;
  319. }
  320. dma_cookie_init(chan);
  321. dma_async_tx_descriptor_init(&c->txd, chan);
  322. c->txd.tx_submit = cppi41_tx_submit;
  323. if (!c->is_tx)
  324. cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
  325. pm_runtime_mark_last_busy(cdd->ddev.dev);
  326. pm_runtime_put_autosuspend(cdd->ddev.dev);
  327. return 0;
  328. }
  329. static void cppi41_dma_free_chan_resources(struct dma_chan *chan)
  330. {
  331. struct cppi41_channel *c = to_cpp41_chan(chan);
  332. struct cppi41_dd *cdd = c->cdd;
  333. int error;
  334. error = pm_runtime_get_sync(cdd->ddev.dev);
  335. if (error < 0) {
  336. pm_runtime_put_noidle(cdd->ddev.dev);
  337. return;
  338. }
  339. WARN_ON(!list_empty(&cdd->pending));
  340. pm_runtime_mark_last_busy(cdd->ddev.dev);
  341. pm_runtime_put_autosuspend(cdd->ddev.dev);
  342. }
  343. static enum dma_status cppi41_dma_tx_status(struct dma_chan *chan,
  344. dma_cookie_t cookie, struct dma_tx_state *txstate)
  345. {
  346. struct cppi41_channel *c = to_cpp41_chan(chan);
  347. enum dma_status ret;
  348. ret = dma_cookie_status(chan, cookie, txstate);
  349. dma_set_residue(txstate, c->residue);
  350. return ret;
  351. }
  352. static void push_desc_queue(struct cppi41_channel *c)
  353. {
  354. struct cppi41_dd *cdd = c->cdd;
  355. u32 desc_num;
  356. u32 desc_phys;
  357. u32 reg;
  358. c->residue = 0;
  359. reg = GCR_CHAN_ENABLE;
  360. if (!c->is_tx) {
  361. reg |= GCR_STARV_RETRY;
  362. reg |= GCR_DESC_TYPE_HOST;
  363. reg |= c->q_comp_num;
  364. }
  365. cppi_writel(reg, c->gcr_reg);
  366. /*
  367. * We don't use writel() but __raw_writel() so we have to make sure
  368. * that the DMA descriptor in coherent memory made to the main memory
  369. * before starting the dma engine.
  370. */
  371. __iowmb();
  372. /*
  373. * DMA transfers can take at least 200ms to complete with USB mass
  374. * storage connected. To prevent autosuspend timeouts, we must use
  375. * pm_runtime_get/put() when chan_busy[] is modified. This will get
  376. * cleared in desc_to_chan() or cppi41_stop_chan() depending on the
  377. * outcome of the transfer.
  378. */
  379. pm_runtime_get(cdd->ddev.dev);
  380. desc_phys = lower_32_bits(c->desc_phys);
  381. desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
  382. WARN_ON(cdd->chan_busy[desc_num]);
  383. cdd->chan_busy[desc_num] = c;
  384. reg = (sizeof(struct cppi41_desc) - 24) / 4;
  385. reg |= desc_phys;
  386. cppi_writel(reg, cdd->qmgr_mem + QMGR_QUEUE_D(c->q_num));
  387. }
  388. /*
  389. * Caller must hold cdd->lock to prevent push_desc_queue()
  390. * getting called out of order. We have both cppi41_dma_issue_pending()
  391. * and cppi41_runtime_resume() call this function.
  392. */
  393. static void cppi41_run_queue(struct cppi41_dd *cdd)
  394. {
  395. struct cppi41_channel *c, *_c;
  396. list_for_each_entry_safe(c, _c, &cdd->pending, node) {
  397. push_desc_queue(c);
  398. list_del(&c->node);
  399. }
  400. }
  401. static void cppi41_dma_issue_pending(struct dma_chan *chan)
  402. {
  403. struct cppi41_channel *c = to_cpp41_chan(chan);
  404. struct cppi41_dd *cdd = c->cdd;
  405. unsigned long flags;
  406. int error;
  407. error = pm_runtime_get(cdd->ddev.dev);
  408. if ((error != -EINPROGRESS) && error < 0) {
  409. pm_runtime_put_noidle(cdd->ddev.dev);
  410. dev_err(cdd->ddev.dev, "Failed to pm_runtime_get: %i\n",
  411. error);
  412. return;
  413. }
  414. spin_lock_irqsave(&cdd->lock, flags);
  415. list_add_tail(&c->node, &cdd->pending);
  416. if (!cdd->is_suspended)
  417. cppi41_run_queue(cdd);
  418. spin_unlock_irqrestore(&cdd->lock, flags);
  419. pm_runtime_mark_last_busy(cdd->ddev.dev);
  420. pm_runtime_put_autosuspend(cdd->ddev.dev);
  421. }
  422. static u32 get_host_pd0(u32 length)
  423. {
  424. u32 reg;
  425. reg = DESC_TYPE_HOST << DESC_TYPE;
  426. reg |= length;
  427. return reg;
  428. }
  429. static u32 get_host_pd1(struct cppi41_channel *c)
  430. {
  431. u32 reg;
  432. reg = 0;
  433. return reg;
  434. }
  435. static u32 get_host_pd2(struct cppi41_channel *c)
  436. {
  437. u32 reg;
  438. reg = DESC_TYPE_USB;
  439. reg |= c->q_comp_num;
  440. return reg;
  441. }
  442. static u32 get_host_pd3(u32 length)
  443. {
  444. u32 reg;
  445. /* PD3 = packet size */
  446. reg = length;
  447. return reg;
  448. }
  449. static u32 get_host_pd6(u32 length)
  450. {
  451. u32 reg;
  452. /* PD6 buffer size */
  453. reg = DESC_PD_COMPLETE;
  454. reg |= length;
  455. return reg;
  456. }
  457. static u32 get_host_pd4_or_7(u32 addr)
  458. {
  459. u32 reg;
  460. reg = addr;
  461. return reg;
  462. }
  463. static u32 get_host_pd5(void)
  464. {
  465. u32 reg;
  466. reg = 0;
  467. return reg;
  468. }
  469. static struct dma_async_tx_descriptor *cppi41_dma_prep_slave_sg(
  470. struct dma_chan *chan, struct scatterlist *sgl, unsigned sg_len,
  471. enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
  472. {
  473. struct cppi41_channel *c = to_cpp41_chan(chan);
  474. struct dma_async_tx_descriptor *txd = NULL;
  475. struct cppi41_dd *cdd = c->cdd;
  476. struct cppi41_desc *d;
  477. struct scatterlist *sg;
  478. unsigned int i;
  479. int error;
  480. error = pm_runtime_get(cdd->ddev.dev);
  481. if (error < 0) {
  482. pm_runtime_put_noidle(cdd->ddev.dev);
  483. return NULL;
  484. }
  485. if (cdd->is_suspended)
  486. goto err_out_not_ready;
  487. d = c->desc;
  488. for_each_sg(sgl, sg, sg_len, i) {
  489. u32 addr;
  490. u32 len;
  491. /* We need to use more than one desc once musb supports sg */
  492. addr = lower_32_bits(sg_dma_address(sg));
  493. len = sg_dma_len(sg);
  494. d->pd0 = get_host_pd0(len);
  495. d->pd1 = get_host_pd1(c);
  496. d->pd2 = get_host_pd2(c);
  497. d->pd3 = get_host_pd3(len);
  498. d->pd4 = get_host_pd4_or_7(addr);
  499. d->pd5 = get_host_pd5();
  500. d->pd6 = get_host_pd6(len);
  501. d->pd7 = get_host_pd4_or_7(addr);
  502. d++;
  503. }
  504. txd = &c->txd;
  505. err_out_not_ready:
  506. pm_runtime_mark_last_busy(cdd->ddev.dev);
  507. pm_runtime_put_autosuspend(cdd->ddev.dev);
  508. return txd;
  509. }
  510. static void cppi41_compute_td_desc(struct cppi41_desc *d)
  511. {
  512. d->pd0 = DESC_TYPE_TEARD << DESC_TYPE;
  513. }
  514. static int cppi41_tear_down_chan(struct cppi41_channel *c)
  515. {
  516. struct dmaengine_result abort_result;
  517. struct cppi41_dd *cdd = c->cdd;
  518. struct cppi41_desc *td;
  519. u32 reg;
  520. u32 desc_phys;
  521. u32 td_desc_phys;
  522. td = cdd->cd;
  523. td += cdd->first_td_desc;
  524. td_desc_phys = cdd->descs_phys;
  525. td_desc_phys += cdd->first_td_desc * sizeof(struct cppi41_desc);
  526. if (!c->td_queued) {
  527. cppi41_compute_td_desc(td);
  528. __iowmb();
  529. reg = (sizeof(struct cppi41_desc) - 24) / 4;
  530. reg |= td_desc_phys;
  531. cppi_writel(reg, cdd->qmgr_mem +
  532. QMGR_QUEUE_D(cdd->td_queue.submit));
  533. reg = GCR_CHAN_ENABLE;
  534. if (!c->is_tx) {
  535. reg |= GCR_STARV_RETRY;
  536. reg |= GCR_DESC_TYPE_HOST;
  537. reg |= cdd->td_queue.complete;
  538. }
  539. reg |= GCR_TEARDOWN;
  540. cppi_writel(reg, c->gcr_reg);
  541. c->td_queued = 1;
  542. c->td_retry = 500;
  543. }
  544. if (!c->td_seen || !c->td_desc_seen) {
  545. desc_phys = cppi41_pop_desc(cdd, cdd->td_queue.complete);
  546. if (!desc_phys && c->is_tx)
  547. desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
  548. if (desc_phys == c->desc_phys) {
  549. c->td_desc_seen = 1;
  550. } else if (desc_phys == td_desc_phys) {
  551. u32 pd0;
  552. __iormb();
  553. pd0 = td->pd0;
  554. WARN_ON((pd0 >> DESC_TYPE) != DESC_TYPE_TEARD);
  555. WARN_ON(!c->is_tx && !(pd0 & TD_DESC_IS_RX));
  556. WARN_ON((pd0 & 0x1f) != c->port_num);
  557. c->td_seen = 1;
  558. } else if (desc_phys) {
  559. WARN_ON_ONCE(1);
  560. }
  561. }
  562. c->td_retry--;
  563. /*
  564. * If the TX descriptor / channel is in use, the caller needs to poke
  565. * his TD bit multiple times. After that he hardware releases the
  566. * transfer descriptor followed by TD descriptor. Waiting seems not to
  567. * cause any difference.
  568. * RX seems to be thrown out right away. However once the TearDown
  569. * descriptor gets through we are done. If we have seens the transfer
  570. * descriptor before the TD we fetch it from enqueue, it has to be
  571. * there waiting for us.
  572. */
  573. if (!c->td_seen && c->td_retry) {
  574. udelay(1);
  575. return -EAGAIN;
  576. }
  577. WARN_ON(!c->td_retry);
  578. if (!c->td_desc_seen) {
  579. desc_phys = cppi41_pop_desc(cdd, c->q_num);
  580. if (!desc_phys)
  581. desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
  582. WARN_ON(!desc_phys);
  583. }
  584. c->td_queued = 0;
  585. c->td_seen = 0;
  586. c->td_desc_seen = 0;
  587. cppi_writel(0, c->gcr_reg);
  588. /* Invoke the callback to do the necessary clean-up */
  589. abort_result.result = DMA_TRANS_ABORTED;
  590. dma_cookie_complete(&c->txd);
  591. dmaengine_desc_get_callback_invoke(&c->txd, &abort_result);
  592. return 0;
  593. }
  594. static int cppi41_stop_chan(struct dma_chan *chan)
  595. {
  596. struct cppi41_channel *c = to_cpp41_chan(chan);
  597. struct cppi41_dd *cdd = c->cdd;
  598. u32 desc_num;
  599. u32 desc_phys;
  600. int ret;
  601. desc_phys = lower_32_bits(c->desc_phys);
  602. desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
  603. if (!cdd->chan_busy[desc_num]) {
  604. struct cppi41_channel *cc, *_ct;
  605. /*
  606. * channels might still be in the pendling list if
  607. * cppi41_dma_issue_pending() is called after
  608. * cppi41_runtime_suspend() is called
  609. */
  610. list_for_each_entry_safe(cc, _ct, &cdd->pending, node) {
  611. if (cc != c)
  612. continue;
  613. list_del(&cc->node);
  614. break;
  615. }
  616. return 0;
  617. }
  618. ret = cppi41_tear_down_chan(c);
  619. if (ret)
  620. return ret;
  621. WARN_ON(!cdd->chan_busy[desc_num]);
  622. cdd->chan_busy[desc_num] = NULL;
  623. /* Usecount for chan_busy[], paired with push_desc_queue() */
  624. pm_runtime_put(cdd->ddev.dev);
  625. return 0;
  626. }
  627. static int cppi41_add_chans(struct device *dev, struct cppi41_dd *cdd)
  628. {
  629. struct cppi41_channel *cchan, *chans;
  630. int i;
  631. u32 n_chans = cdd->n_chans;
  632. /*
  633. * The channels can only be used as TX or as RX. So we add twice
  634. * that much dma channels because USB can only do RX or TX.
  635. */
  636. n_chans *= 2;
  637. chans = devm_kcalloc(dev, n_chans, sizeof(*chans), GFP_KERNEL);
  638. if (!chans)
  639. return -ENOMEM;
  640. for (i = 0; i < n_chans; i++) {
  641. cchan = &chans[i];
  642. cchan->cdd = cdd;
  643. if (i & 1) {
  644. cchan->gcr_reg = cdd->ctrl_mem + DMA_TXGCR(i >> 1);
  645. cchan->is_tx = 1;
  646. } else {
  647. cchan->gcr_reg = cdd->ctrl_mem + DMA_RXGCR(i >> 1);
  648. cchan->is_tx = 0;
  649. }
  650. cchan->port_num = i >> 1;
  651. cchan->desc = &cdd->cd[i];
  652. cchan->desc_phys = cdd->descs_phys;
  653. cchan->desc_phys += i * sizeof(struct cppi41_desc);
  654. cchan->chan.device = &cdd->ddev;
  655. list_add_tail(&cchan->chan.device_node, &cdd->ddev.channels);
  656. }
  657. cdd->first_td_desc = n_chans;
  658. return 0;
  659. }
  660. static void purge_descs(struct device *dev, struct cppi41_dd *cdd)
  661. {
  662. unsigned int mem_decs;
  663. int i;
  664. mem_decs = ALLOC_DECS_NUM * sizeof(struct cppi41_desc);
  665. for (i = 0; i < DESCS_AREAS; i++) {
  666. cppi_writel(0, cdd->qmgr_mem + QMGR_MEMBASE(i));
  667. cppi_writel(0, cdd->qmgr_mem + QMGR_MEMCTRL(i));
  668. dma_free_coherent(dev, mem_decs, cdd->cd,
  669. cdd->descs_phys);
  670. }
  671. }
  672. static void disable_sched(struct cppi41_dd *cdd)
  673. {
  674. cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
  675. }
  676. static void deinit_cppi41(struct device *dev, struct cppi41_dd *cdd)
  677. {
  678. disable_sched(cdd);
  679. purge_descs(dev, cdd);
  680. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  681. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  682. dma_free_coherent(dev, QMGR_SCRATCH_SIZE, cdd->qmgr_scratch,
  683. cdd->scratch_phys);
  684. }
  685. static int init_descs(struct device *dev, struct cppi41_dd *cdd)
  686. {
  687. unsigned int desc_size;
  688. unsigned int mem_decs;
  689. int i;
  690. u32 reg;
  691. u32 idx;
  692. BUILD_BUG_ON(sizeof(struct cppi41_desc) &
  693. (sizeof(struct cppi41_desc) - 1));
  694. BUILD_BUG_ON(sizeof(struct cppi41_desc) < 32);
  695. BUILD_BUG_ON(ALLOC_DECS_NUM < 32);
  696. desc_size = sizeof(struct cppi41_desc);
  697. mem_decs = ALLOC_DECS_NUM * desc_size;
  698. idx = 0;
  699. for (i = 0; i < DESCS_AREAS; i++) {
  700. reg = idx << QMGR_MEMCTRL_IDX_SH;
  701. reg |= (ilog2(desc_size) - 5) << QMGR_MEMCTRL_DESC_SH;
  702. reg |= ilog2(ALLOC_DECS_NUM) - 5;
  703. BUILD_BUG_ON(DESCS_AREAS != 1);
  704. cdd->cd = dma_alloc_coherent(dev, mem_decs,
  705. &cdd->descs_phys, GFP_KERNEL);
  706. if (!cdd->cd)
  707. return -ENOMEM;
  708. cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
  709. cppi_writel(reg, cdd->qmgr_mem + QMGR_MEMCTRL(i));
  710. idx += ALLOC_DECS_NUM;
  711. }
  712. return 0;
  713. }
  714. static void init_sched(struct cppi41_dd *cdd)
  715. {
  716. unsigned ch;
  717. unsigned word;
  718. u32 reg;
  719. word = 0;
  720. cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
  721. for (ch = 0; ch < cdd->n_chans; ch += 2) {
  722. reg = SCHED_ENTRY0_CHAN(ch);
  723. reg |= SCHED_ENTRY1_CHAN(ch) | SCHED_ENTRY1_IS_RX;
  724. reg |= SCHED_ENTRY2_CHAN(ch + 1);
  725. reg |= SCHED_ENTRY3_CHAN(ch + 1) | SCHED_ENTRY3_IS_RX;
  726. cppi_writel(reg, cdd->sched_mem + DMA_SCHED_WORD(word));
  727. word++;
  728. }
  729. reg = cdd->n_chans * 2 - 1;
  730. reg |= DMA_SCHED_CTRL_EN;
  731. cppi_writel(reg, cdd->sched_mem + DMA_SCHED_CTRL);
  732. }
  733. static int init_cppi41(struct device *dev, struct cppi41_dd *cdd)
  734. {
  735. int ret;
  736. BUILD_BUG_ON(QMGR_SCRATCH_SIZE > ((1 << 14) - 1));
  737. cdd->qmgr_scratch = dma_alloc_coherent(dev, QMGR_SCRATCH_SIZE,
  738. &cdd->scratch_phys, GFP_KERNEL);
  739. if (!cdd->qmgr_scratch)
  740. return -ENOMEM;
  741. cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  742. cppi_writel(TOTAL_DESCS_NUM, cdd->qmgr_mem + QMGR_LRAM_SIZE);
  743. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
  744. ret = init_descs(dev, cdd);
  745. if (ret)
  746. goto err_td;
  747. cppi_writel(cdd->td_queue.submit, cdd->ctrl_mem + DMA_TDFDQ);
  748. init_sched(cdd);
  749. return 0;
  750. err_td:
  751. deinit_cppi41(dev, cdd);
  752. return ret;
  753. }
  754. static struct platform_driver cpp41_dma_driver;
  755. /*
  756. * The param format is:
  757. * X Y
  758. * X: Port
  759. * Y: 0 = RX else TX
  760. */
  761. #define INFO_PORT 0
  762. #define INFO_IS_TX 1
  763. static bool cpp41_dma_filter_fn(struct dma_chan *chan, void *param)
  764. {
  765. struct cppi41_channel *cchan;
  766. struct cppi41_dd *cdd;
  767. const struct chan_queues *queues;
  768. u32 *num = param;
  769. if (chan->device->dev->driver != &cpp41_dma_driver.driver)
  770. return false;
  771. cchan = to_cpp41_chan(chan);
  772. if (cchan->port_num != num[INFO_PORT])
  773. return false;
  774. if (cchan->is_tx && !num[INFO_IS_TX])
  775. return false;
  776. cdd = cchan->cdd;
  777. if (cchan->is_tx)
  778. queues = cdd->queues_tx;
  779. else
  780. queues = cdd->queues_rx;
  781. BUILD_BUG_ON(ARRAY_SIZE(am335x_usb_queues_rx) !=
  782. ARRAY_SIZE(am335x_usb_queues_tx));
  783. if (WARN_ON(cchan->port_num >= ARRAY_SIZE(am335x_usb_queues_rx)))
  784. return false;
  785. cchan->q_num = queues[cchan->port_num].submit;
  786. cchan->q_comp_num = queues[cchan->port_num].complete;
  787. return true;
  788. }
  789. static struct of_dma_filter_info cpp41_dma_info = {
  790. .filter_fn = cpp41_dma_filter_fn,
  791. };
  792. static struct dma_chan *cppi41_dma_xlate(struct of_phandle_args *dma_spec,
  793. struct of_dma *ofdma)
  794. {
  795. int count = dma_spec->args_count;
  796. struct of_dma_filter_info *info = ofdma->of_dma_data;
  797. if (!info || !info->filter_fn)
  798. return NULL;
  799. if (count != 2)
  800. return NULL;
  801. return dma_request_channel(info->dma_cap, info->filter_fn,
  802. &dma_spec->args[0]);
  803. }
  804. static const struct cppi_glue_infos am335x_usb_infos = {
  805. .queues_rx = am335x_usb_queues_rx,
  806. .queues_tx = am335x_usb_queues_tx,
  807. .td_queue = { .submit = 31, .complete = 0 },
  808. .first_completion_queue = 93,
  809. .qmgr_num_pend = 5,
  810. };
  811. static const struct cppi_glue_infos da8xx_usb_infos = {
  812. .queues_rx = da8xx_usb_queues_rx,
  813. .queues_tx = da8xx_usb_queues_tx,
  814. .td_queue = { .submit = 31, .complete = 0 },
  815. .first_completion_queue = 24,
  816. .qmgr_num_pend = 2,
  817. };
  818. static const struct of_device_id cppi41_dma_ids[] = {
  819. { .compatible = "ti,am3359-cppi41", .data = &am335x_usb_infos},
  820. { .compatible = "ti,da830-cppi41", .data = &da8xx_usb_infos},
  821. {},
  822. };
  823. MODULE_DEVICE_TABLE(of, cppi41_dma_ids);
  824. static const struct cppi_glue_infos *get_glue_info(struct device *dev)
  825. {
  826. const struct of_device_id *of_id;
  827. of_id = of_match_node(cppi41_dma_ids, dev->of_node);
  828. if (!of_id)
  829. return NULL;
  830. return of_id->data;
  831. }
  832. #define CPPI41_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  833. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  834. BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
  835. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  836. static int cppi41_dma_probe(struct platform_device *pdev)
  837. {
  838. struct cppi41_dd *cdd;
  839. struct device *dev = &pdev->dev;
  840. const struct cppi_glue_infos *glue_info;
  841. struct resource *mem;
  842. int index;
  843. int irq;
  844. int ret;
  845. glue_info = get_glue_info(dev);
  846. if (!glue_info)
  847. return -EINVAL;
  848. cdd = devm_kzalloc(&pdev->dev, sizeof(*cdd), GFP_KERNEL);
  849. if (!cdd)
  850. return -ENOMEM;
  851. dma_cap_set(DMA_SLAVE, cdd->ddev.cap_mask);
  852. cdd->ddev.device_alloc_chan_resources = cppi41_dma_alloc_chan_resources;
  853. cdd->ddev.device_free_chan_resources = cppi41_dma_free_chan_resources;
  854. cdd->ddev.device_tx_status = cppi41_dma_tx_status;
  855. cdd->ddev.device_issue_pending = cppi41_dma_issue_pending;
  856. cdd->ddev.device_prep_slave_sg = cppi41_dma_prep_slave_sg;
  857. cdd->ddev.device_terminate_all = cppi41_stop_chan;
  858. cdd->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  859. cdd->ddev.src_addr_widths = CPPI41_DMA_BUSWIDTHS;
  860. cdd->ddev.dst_addr_widths = CPPI41_DMA_BUSWIDTHS;
  861. cdd->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  862. cdd->ddev.dev = dev;
  863. INIT_LIST_HEAD(&cdd->ddev.channels);
  864. cpp41_dma_info.dma_cap = cdd->ddev.cap_mask;
  865. index = of_property_match_string(dev->of_node,
  866. "reg-names", "controller");
  867. if (index < 0)
  868. return index;
  869. mem = platform_get_resource(pdev, IORESOURCE_MEM, index);
  870. cdd->ctrl_mem = devm_ioremap_resource(dev, mem);
  871. if (IS_ERR(cdd->ctrl_mem))
  872. return PTR_ERR(cdd->ctrl_mem);
  873. mem = platform_get_resource(pdev, IORESOURCE_MEM, index + 1);
  874. cdd->sched_mem = devm_ioremap_resource(dev, mem);
  875. if (IS_ERR(cdd->sched_mem))
  876. return PTR_ERR(cdd->sched_mem);
  877. mem = platform_get_resource(pdev, IORESOURCE_MEM, index + 2);
  878. cdd->qmgr_mem = devm_ioremap_resource(dev, mem);
  879. if (IS_ERR(cdd->qmgr_mem))
  880. return PTR_ERR(cdd->qmgr_mem);
  881. spin_lock_init(&cdd->lock);
  882. INIT_LIST_HEAD(&cdd->pending);
  883. platform_set_drvdata(pdev, cdd);
  884. pm_runtime_enable(dev);
  885. pm_runtime_set_autosuspend_delay(dev, 100);
  886. pm_runtime_use_autosuspend(dev);
  887. ret = pm_runtime_get_sync(dev);
  888. if (ret < 0)
  889. goto err_get_sync;
  890. cdd->queues_rx = glue_info->queues_rx;
  891. cdd->queues_tx = glue_info->queues_tx;
  892. cdd->td_queue = glue_info->td_queue;
  893. cdd->qmgr_num_pend = glue_info->qmgr_num_pend;
  894. cdd->first_completion_queue = glue_info->first_completion_queue;
  895. ret = of_property_read_u32(dev->of_node,
  896. "#dma-channels", &cdd->n_chans);
  897. if (ret)
  898. goto err_get_n_chans;
  899. ret = init_cppi41(dev, cdd);
  900. if (ret)
  901. goto err_init_cppi;
  902. ret = cppi41_add_chans(dev, cdd);
  903. if (ret)
  904. goto err_chans;
  905. irq = irq_of_parse_and_map(dev->of_node, 0);
  906. if (!irq) {
  907. ret = -EINVAL;
  908. goto err_chans;
  909. }
  910. ret = devm_request_irq(&pdev->dev, irq, cppi41_irq, IRQF_SHARED,
  911. dev_name(dev), cdd);
  912. if (ret)
  913. goto err_chans;
  914. cdd->irq = irq;
  915. ret = dma_async_device_register(&cdd->ddev);
  916. if (ret)
  917. goto err_chans;
  918. ret = of_dma_controller_register(dev->of_node,
  919. cppi41_dma_xlate, &cpp41_dma_info);
  920. if (ret)
  921. goto err_of;
  922. pm_runtime_mark_last_busy(dev);
  923. pm_runtime_put_autosuspend(dev);
  924. return 0;
  925. err_of:
  926. dma_async_device_unregister(&cdd->ddev);
  927. err_chans:
  928. deinit_cppi41(dev, cdd);
  929. err_init_cppi:
  930. pm_runtime_dont_use_autosuspend(dev);
  931. err_get_n_chans:
  932. err_get_sync:
  933. pm_runtime_put_sync(dev);
  934. pm_runtime_disable(dev);
  935. return ret;
  936. }
  937. static int cppi41_dma_remove(struct platform_device *pdev)
  938. {
  939. struct cppi41_dd *cdd = platform_get_drvdata(pdev);
  940. int error;
  941. error = pm_runtime_get_sync(&pdev->dev);
  942. if (error < 0)
  943. dev_err(&pdev->dev, "%s could not pm_runtime_get: %i\n",
  944. __func__, error);
  945. of_dma_controller_free(pdev->dev.of_node);
  946. dma_async_device_unregister(&cdd->ddev);
  947. devm_free_irq(&pdev->dev, cdd->irq, cdd);
  948. deinit_cppi41(&pdev->dev, cdd);
  949. pm_runtime_dont_use_autosuspend(&pdev->dev);
  950. pm_runtime_put_sync(&pdev->dev);
  951. pm_runtime_disable(&pdev->dev);
  952. return 0;
  953. }
  954. static int __maybe_unused cppi41_suspend(struct device *dev)
  955. {
  956. struct cppi41_dd *cdd = dev_get_drvdata(dev);
  957. cdd->dma_tdfdq = cppi_readl(cdd->ctrl_mem + DMA_TDFDQ);
  958. disable_sched(cdd);
  959. return 0;
  960. }
  961. static int __maybe_unused cppi41_resume(struct device *dev)
  962. {
  963. struct cppi41_dd *cdd = dev_get_drvdata(dev);
  964. struct cppi41_channel *c;
  965. int i;
  966. for (i = 0; i < DESCS_AREAS; i++)
  967. cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
  968. list_for_each_entry(c, &cdd->ddev.channels, chan.device_node)
  969. if (!c->is_tx)
  970. cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
  971. init_sched(cdd);
  972. cppi_writel(cdd->dma_tdfdq, cdd->ctrl_mem + DMA_TDFDQ);
  973. cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  974. cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
  975. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
  976. return 0;
  977. }
  978. static int __maybe_unused cppi41_runtime_suspend(struct device *dev)
  979. {
  980. struct cppi41_dd *cdd = dev_get_drvdata(dev);
  981. unsigned long flags;
  982. spin_lock_irqsave(&cdd->lock, flags);
  983. cdd->is_suspended = true;
  984. WARN_ON(!list_empty(&cdd->pending));
  985. spin_unlock_irqrestore(&cdd->lock, flags);
  986. return 0;
  987. }
  988. static int __maybe_unused cppi41_runtime_resume(struct device *dev)
  989. {
  990. struct cppi41_dd *cdd = dev_get_drvdata(dev);
  991. unsigned long flags;
  992. spin_lock_irqsave(&cdd->lock, flags);
  993. cdd->is_suspended = false;
  994. cppi41_run_queue(cdd);
  995. spin_unlock_irqrestore(&cdd->lock, flags);
  996. return 0;
  997. }
  998. static const struct dev_pm_ops cppi41_pm_ops = {
  999. SET_LATE_SYSTEM_SLEEP_PM_OPS(cppi41_suspend, cppi41_resume)
  1000. SET_RUNTIME_PM_OPS(cppi41_runtime_suspend,
  1001. cppi41_runtime_resume,
  1002. NULL)
  1003. };
  1004. static struct platform_driver cpp41_dma_driver = {
  1005. .probe = cppi41_dma_probe,
  1006. .remove = cppi41_dma_remove,
  1007. .driver = {
  1008. .name = "cppi41-dma-engine",
  1009. .pm = &cppi41_pm_ops,
  1010. .of_match_table = of_match_ptr(cppi41_dma_ids),
  1011. },
  1012. };
  1013. module_platform_driver(cpp41_dma_driver);
  1014. MODULE_LICENSE("GPL");
  1015. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");