pl330.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /* linux/drivers/dma/pl330.c
  2. *
  3. * Copyright (C) 2010 Samsung Electronics Co. Ltd.
  4. * Jaswinder Singh <jassi.brar@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/io.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/amba/bus.h>
  18. #include <linux/amba/pl330.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/scatterlist.h>
  21. #define NR_DEFAULT_DESC 16
  22. enum desc_status {
  23. /* In the DMAC pool */
  24. FREE,
  25. /*
  26. * Allocted to some channel during prep_xxx
  27. * Also may be sitting on the work_list.
  28. */
  29. PREP,
  30. /*
  31. * Sitting on the work_list and already submitted
  32. * to the PL330 core. Not more than two descriptors
  33. * of a channel can be BUSY at any time.
  34. */
  35. BUSY,
  36. /*
  37. * Sitting on the channel work_list but xfer done
  38. * by PL330 core
  39. */
  40. DONE,
  41. };
  42. struct dma_pl330_chan {
  43. /* Schedule desc completion */
  44. struct tasklet_struct task;
  45. /* DMA-Engine Channel */
  46. struct dma_chan chan;
  47. /* Last completed cookie */
  48. dma_cookie_t completed;
  49. /* List of to be xfered descriptors */
  50. struct list_head work_list;
  51. /* Pointer to the DMAC that manages this channel,
  52. * NULL if the channel is available to be acquired.
  53. * As the parent, this DMAC also provides descriptors
  54. * to the channel.
  55. */
  56. struct dma_pl330_dmac *dmac;
  57. /* To protect channel manipulation */
  58. spinlock_t lock;
  59. /* Token of a hardware channel thread of PL330 DMAC
  60. * NULL if the channel is available to be acquired.
  61. */
  62. void *pl330_chid;
  63. /* For D-to-M and M-to-D channels */
  64. int burst_sz; /* the peripheral fifo width */
  65. int burst_len; /* the number of burst */
  66. dma_addr_t fifo_addr;
  67. /* for cyclic capability */
  68. bool cyclic;
  69. };
  70. struct dma_pl330_dmac {
  71. struct pl330_info pif;
  72. /* DMA-Engine Device */
  73. struct dma_device ddma;
  74. /* Pool of descriptors available for the DMAC's channels */
  75. struct list_head desc_pool;
  76. /* To protect desc_pool manipulation */
  77. spinlock_t pool_lock;
  78. /* Peripheral channels connected to this DMAC */
  79. struct dma_pl330_chan *peripherals; /* keep at end */
  80. struct clk *clk;
  81. };
  82. struct dma_pl330_desc {
  83. /* To attach to a queue as child */
  84. struct list_head node;
  85. /* Descriptor for the DMA Engine API */
  86. struct dma_async_tx_descriptor txd;
  87. /* Xfer for PL330 core */
  88. struct pl330_xfer px;
  89. struct pl330_reqcfg rqcfg;
  90. struct pl330_req req;
  91. enum desc_status status;
  92. /* The channel which currently holds this desc */
  93. struct dma_pl330_chan *pchan;
  94. };
  95. static inline struct dma_pl330_chan *
  96. to_pchan(struct dma_chan *ch)
  97. {
  98. if (!ch)
  99. return NULL;
  100. return container_of(ch, struct dma_pl330_chan, chan);
  101. }
  102. static inline struct dma_pl330_desc *
  103. to_desc(struct dma_async_tx_descriptor *tx)
  104. {
  105. return container_of(tx, struct dma_pl330_desc, txd);
  106. }
  107. static inline void free_desc_list(struct list_head *list)
  108. {
  109. struct dma_pl330_dmac *pdmac;
  110. struct dma_pl330_desc *desc;
  111. struct dma_pl330_chan *pch;
  112. unsigned long flags;
  113. if (list_empty(list))
  114. return;
  115. /* Finish off the work list */
  116. list_for_each_entry(desc, list, node) {
  117. dma_async_tx_callback callback;
  118. void *param;
  119. /* All desc in a list belong to same channel */
  120. pch = desc->pchan;
  121. callback = desc->txd.callback;
  122. param = desc->txd.callback_param;
  123. if (callback)
  124. callback(param);
  125. desc->pchan = NULL;
  126. }
  127. pdmac = pch->dmac;
  128. spin_lock_irqsave(&pdmac->pool_lock, flags);
  129. list_splice_tail_init(list, &pdmac->desc_pool);
  130. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  131. }
  132. static inline void handle_cyclic_desc_list(struct list_head *list)
  133. {
  134. struct dma_pl330_desc *desc;
  135. struct dma_pl330_chan *pch;
  136. unsigned long flags;
  137. if (list_empty(list))
  138. return;
  139. list_for_each_entry(desc, list, node) {
  140. dma_async_tx_callback callback;
  141. /* Change status to reload it */
  142. desc->status = PREP;
  143. pch = desc->pchan;
  144. callback = desc->txd.callback;
  145. if (callback)
  146. callback(desc->txd.callback_param);
  147. }
  148. spin_lock_irqsave(&pch->lock, flags);
  149. list_splice_tail_init(list, &pch->work_list);
  150. spin_unlock_irqrestore(&pch->lock, flags);
  151. }
  152. static inline void fill_queue(struct dma_pl330_chan *pch)
  153. {
  154. struct dma_pl330_desc *desc;
  155. int ret;
  156. list_for_each_entry(desc, &pch->work_list, node) {
  157. /* If already submitted */
  158. if (desc->status == BUSY)
  159. break;
  160. ret = pl330_submit_req(pch->pl330_chid,
  161. &desc->req);
  162. if (!ret) {
  163. desc->status = BUSY;
  164. break;
  165. } else if (ret == -EAGAIN) {
  166. /* QFull or DMAC Dying */
  167. break;
  168. } else {
  169. /* Unacceptable request */
  170. desc->status = DONE;
  171. dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n",
  172. __func__, __LINE__, desc->txd.cookie);
  173. tasklet_schedule(&pch->task);
  174. }
  175. }
  176. }
  177. static void pl330_tasklet(unsigned long data)
  178. {
  179. struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data;
  180. struct dma_pl330_desc *desc, *_dt;
  181. unsigned long flags;
  182. LIST_HEAD(list);
  183. spin_lock_irqsave(&pch->lock, flags);
  184. /* Pick up ripe tomatoes */
  185. list_for_each_entry_safe(desc, _dt, &pch->work_list, node)
  186. if (desc->status == DONE) {
  187. pch->completed = desc->txd.cookie;
  188. list_move_tail(&desc->node, &list);
  189. }
  190. /* Try to submit a req imm. next to the last completed cookie */
  191. fill_queue(pch);
  192. /* Make sure the PL330 Channel thread is active */
  193. pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START);
  194. spin_unlock_irqrestore(&pch->lock, flags);
  195. if (pch->cyclic)
  196. handle_cyclic_desc_list(&list);
  197. else
  198. free_desc_list(&list);
  199. }
  200. static void dma_pl330_rqcb(void *token, enum pl330_op_err err)
  201. {
  202. struct dma_pl330_desc *desc = token;
  203. struct dma_pl330_chan *pch = desc->pchan;
  204. unsigned long flags;
  205. /* If desc aborted */
  206. if (!pch)
  207. return;
  208. spin_lock_irqsave(&pch->lock, flags);
  209. desc->status = DONE;
  210. spin_unlock_irqrestore(&pch->lock, flags);
  211. tasklet_schedule(&pch->task);
  212. }
  213. static int pl330_alloc_chan_resources(struct dma_chan *chan)
  214. {
  215. struct dma_pl330_chan *pch = to_pchan(chan);
  216. struct dma_pl330_dmac *pdmac = pch->dmac;
  217. unsigned long flags;
  218. spin_lock_irqsave(&pch->lock, flags);
  219. pch->completed = chan->cookie = 1;
  220. pch->cyclic = false;
  221. pch->pl330_chid = pl330_request_channel(&pdmac->pif);
  222. if (!pch->pl330_chid) {
  223. spin_unlock_irqrestore(&pch->lock, flags);
  224. return 0;
  225. }
  226. tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
  227. spin_unlock_irqrestore(&pch->lock, flags);
  228. return 1;
  229. }
  230. static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
  231. {
  232. struct dma_pl330_chan *pch = to_pchan(chan);
  233. struct dma_pl330_desc *desc, *_dt;
  234. unsigned long flags;
  235. struct dma_pl330_dmac *pdmac = pch->dmac;
  236. struct dma_slave_config *slave_config;
  237. LIST_HEAD(list);
  238. switch (cmd) {
  239. case DMA_TERMINATE_ALL:
  240. spin_lock_irqsave(&pch->lock, flags);
  241. /* FLUSH the PL330 Channel thread */
  242. pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
  243. /* Mark all desc done */
  244. list_for_each_entry_safe(desc, _dt, &pch->work_list , node) {
  245. desc->status = DONE;
  246. pch->completed = desc->txd.cookie;
  247. list_move_tail(&desc->node, &list);
  248. }
  249. list_splice_tail_init(&list, &pdmac->desc_pool);
  250. spin_unlock_irqrestore(&pch->lock, flags);
  251. break;
  252. case DMA_SLAVE_CONFIG:
  253. slave_config = (struct dma_slave_config *)arg;
  254. if (slave_config->direction == DMA_TO_DEVICE) {
  255. if (slave_config->dst_addr)
  256. pch->fifo_addr = slave_config->dst_addr;
  257. if (slave_config->dst_addr_width)
  258. pch->burst_sz = __ffs(slave_config->dst_addr_width);
  259. if (slave_config->dst_maxburst)
  260. pch->burst_len = slave_config->dst_maxburst;
  261. } else if (slave_config->direction == DMA_FROM_DEVICE) {
  262. if (slave_config->src_addr)
  263. pch->fifo_addr = slave_config->src_addr;
  264. if (slave_config->src_addr_width)
  265. pch->burst_sz = __ffs(slave_config->src_addr_width);
  266. if (slave_config->src_maxburst)
  267. pch->burst_len = slave_config->src_maxburst;
  268. }
  269. break;
  270. default:
  271. dev_err(pch->dmac->pif.dev, "Not supported command.\n");
  272. return -ENXIO;
  273. }
  274. return 0;
  275. }
  276. static void pl330_free_chan_resources(struct dma_chan *chan)
  277. {
  278. struct dma_pl330_chan *pch = to_pchan(chan);
  279. unsigned long flags;
  280. spin_lock_irqsave(&pch->lock, flags);
  281. tasklet_kill(&pch->task);
  282. pl330_release_channel(pch->pl330_chid);
  283. pch->pl330_chid = NULL;
  284. if (pch->cyclic)
  285. list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
  286. spin_unlock_irqrestore(&pch->lock, flags);
  287. }
  288. static enum dma_status
  289. pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  290. struct dma_tx_state *txstate)
  291. {
  292. struct dma_pl330_chan *pch = to_pchan(chan);
  293. dma_cookie_t last_done, last_used;
  294. int ret;
  295. last_done = pch->completed;
  296. last_used = chan->cookie;
  297. ret = dma_async_is_complete(cookie, last_done, last_used);
  298. dma_set_tx_state(txstate, last_done, last_used, 0);
  299. return ret;
  300. }
  301. static void pl330_issue_pending(struct dma_chan *chan)
  302. {
  303. pl330_tasklet((unsigned long) to_pchan(chan));
  304. }
  305. /*
  306. * We returned the last one of the circular list of descriptor(s)
  307. * from prep_xxx, so the argument to submit corresponds to the last
  308. * descriptor of the list.
  309. */
  310. static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
  311. {
  312. struct dma_pl330_desc *desc, *last = to_desc(tx);
  313. struct dma_pl330_chan *pch = to_pchan(tx->chan);
  314. dma_cookie_t cookie;
  315. unsigned long flags;
  316. spin_lock_irqsave(&pch->lock, flags);
  317. /* Assign cookies to all nodes */
  318. cookie = tx->chan->cookie;
  319. while (!list_empty(&last->node)) {
  320. desc = list_entry(last->node.next, struct dma_pl330_desc, node);
  321. if (++cookie < 0)
  322. cookie = 1;
  323. desc->txd.cookie = cookie;
  324. list_move_tail(&desc->node, &pch->work_list);
  325. }
  326. if (++cookie < 0)
  327. cookie = 1;
  328. last->txd.cookie = cookie;
  329. list_add_tail(&last->node, &pch->work_list);
  330. tx->chan->cookie = cookie;
  331. spin_unlock_irqrestore(&pch->lock, flags);
  332. return cookie;
  333. }
  334. static inline void _init_desc(struct dma_pl330_desc *desc)
  335. {
  336. desc->pchan = NULL;
  337. desc->req.x = &desc->px;
  338. desc->req.token = desc;
  339. desc->rqcfg.swap = SWAP_NO;
  340. desc->rqcfg.privileged = 0;
  341. desc->rqcfg.insnaccess = 0;
  342. desc->rqcfg.scctl = SCCTRL0;
  343. desc->rqcfg.dcctl = DCCTRL0;
  344. desc->req.cfg = &desc->rqcfg;
  345. desc->req.xfer_cb = dma_pl330_rqcb;
  346. desc->txd.tx_submit = pl330_tx_submit;
  347. INIT_LIST_HEAD(&desc->node);
  348. }
  349. /* Returns the number of descriptors added to the DMAC pool */
  350. int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
  351. {
  352. struct dma_pl330_desc *desc;
  353. unsigned long flags;
  354. int i;
  355. if (!pdmac)
  356. return 0;
  357. desc = kmalloc(count * sizeof(*desc), flg);
  358. if (!desc)
  359. return 0;
  360. spin_lock_irqsave(&pdmac->pool_lock, flags);
  361. for (i = 0; i < count; i++) {
  362. _init_desc(&desc[i]);
  363. list_add_tail(&desc[i].node, &pdmac->desc_pool);
  364. }
  365. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  366. return count;
  367. }
  368. static struct dma_pl330_desc *
  369. pluck_desc(struct dma_pl330_dmac *pdmac)
  370. {
  371. struct dma_pl330_desc *desc = NULL;
  372. unsigned long flags;
  373. if (!pdmac)
  374. return NULL;
  375. spin_lock_irqsave(&pdmac->pool_lock, flags);
  376. if (!list_empty(&pdmac->desc_pool)) {
  377. desc = list_entry(pdmac->desc_pool.next,
  378. struct dma_pl330_desc, node);
  379. list_del_init(&desc->node);
  380. desc->status = PREP;
  381. desc->txd.callback = NULL;
  382. }
  383. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  384. return desc;
  385. }
  386. static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
  387. {
  388. struct dma_pl330_dmac *pdmac = pch->dmac;
  389. struct dma_pl330_peri *peri = pch->chan.private;
  390. struct dma_pl330_desc *desc;
  391. /* Pluck one desc from the pool of DMAC */
  392. desc = pluck_desc(pdmac);
  393. /* If the DMAC pool is empty, alloc new */
  394. if (!desc) {
  395. if (!add_desc(pdmac, GFP_ATOMIC, 1))
  396. return NULL;
  397. /* Try again */
  398. desc = pluck_desc(pdmac);
  399. if (!desc) {
  400. dev_err(pch->dmac->pif.dev,
  401. "%s:%d ALERT!\n", __func__, __LINE__);
  402. return NULL;
  403. }
  404. }
  405. /* Initialize the descriptor */
  406. desc->pchan = pch;
  407. desc->txd.cookie = 0;
  408. async_tx_ack(&desc->txd);
  409. if (peri) {
  410. desc->req.rqtype = peri->rqtype;
  411. desc->req.peri = pch->chan.chan_id;
  412. } else {
  413. desc->req.rqtype = MEMTOMEM;
  414. desc->req.peri = 0;
  415. }
  416. dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
  417. return desc;
  418. }
  419. static inline void fill_px(struct pl330_xfer *px,
  420. dma_addr_t dst, dma_addr_t src, size_t len)
  421. {
  422. px->next = NULL;
  423. px->bytes = len;
  424. px->dst_addr = dst;
  425. px->src_addr = src;
  426. }
  427. static struct dma_pl330_desc *
  428. __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
  429. dma_addr_t src, size_t len)
  430. {
  431. struct dma_pl330_desc *desc = pl330_get_desc(pch);
  432. if (!desc) {
  433. dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
  434. __func__, __LINE__);
  435. return NULL;
  436. }
  437. /*
  438. * Ideally we should lookout for reqs bigger than
  439. * those that can be programmed with 256 bytes of
  440. * MC buffer, but considering a req size is seldom
  441. * going to be word-unaligned and more than 200MB,
  442. * we take it easy.
  443. * Also, should the limit is reached we'd rather
  444. * have the platform increase MC buffer size than
  445. * complicating this API driver.
  446. */
  447. fill_px(&desc->px, dst, src, len);
  448. return desc;
  449. }
  450. /* Call after fixing burst size */
  451. static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
  452. {
  453. struct dma_pl330_chan *pch = desc->pchan;
  454. struct pl330_info *pi = &pch->dmac->pif;
  455. int burst_len;
  456. burst_len = pi->pcfg.data_bus_width / 8;
  457. burst_len *= pi->pcfg.data_buf_dep;
  458. burst_len >>= desc->rqcfg.brst_size;
  459. /* src/dst_burst_len can't be more than 16 */
  460. if (burst_len > 16)
  461. burst_len = 16;
  462. while (burst_len > 1) {
  463. if (!(len % (burst_len << desc->rqcfg.brst_size)))
  464. break;
  465. burst_len--;
  466. }
  467. return burst_len;
  468. }
  469. static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
  470. struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
  471. size_t period_len, enum dma_data_direction direction)
  472. {
  473. struct dma_pl330_desc *desc;
  474. struct dma_pl330_chan *pch = to_pchan(chan);
  475. dma_addr_t dst;
  476. dma_addr_t src;
  477. desc = pl330_get_desc(pch);
  478. if (!desc) {
  479. dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
  480. __func__, __LINE__);
  481. return NULL;
  482. }
  483. switch (direction) {
  484. case DMA_TO_DEVICE:
  485. desc->rqcfg.src_inc = 1;
  486. desc->rqcfg.dst_inc = 0;
  487. src = dma_addr;
  488. dst = pch->fifo_addr;
  489. break;
  490. case DMA_FROM_DEVICE:
  491. desc->rqcfg.src_inc = 0;
  492. desc->rqcfg.dst_inc = 1;
  493. src = pch->fifo_addr;
  494. dst = dma_addr;
  495. break;
  496. default:
  497. dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
  498. __func__, __LINE__);
  499. return NULL;
  500. }
  501. desc->rqcfg.brst_size = pch->burst_sz;
  502. desc->rqcfg.brst_len = 1;
  503. pch->cyclic = true;
  504. fill_px(&desc->px, dst, src, period_len);
  505. return &desc->txd;
  506. }
  507. static struct dma_async_tx_descriptor *
  508. pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
  509. dma_addr_t src, size_t len, unsigned long flags)
  510. {
  511. struct dma_pl330_desc *desc;
  512. struct dma_pl330_chan *pch = to_pchan(chan);
  513. struct dma_pl330_peri *peri = chan->private;
  514. struct pl330_info *pi;
  515. int burst;
  516. if (unlikely(!pch || !len))
  517. return NULL;
  518. if (peri && peri->rqtype != MEMTOMEM)
  519. return NULL;
  520. pi = &pch->dmac->pif;
  521. desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
  522. if (!desc)
  523. return NULL;
  524. desc->rqcfg.src_inc = 1;
  525. desc->rqcfg.dst_inc = 1;
  526. /* Select max possible burst size */
  527. burst = pi->pcfg.data_bus_width / 8;
  528. while (burst > 1) {
  529. if (!(len % burst))
  530. break;
  531. burst /= 2;
  532. }
  533. desc->rqcfg.brst_size = 0;
  534. while (burst != (1 << desc->rqcfg.brst_size))
  535. desc->rqcfg.brst_size++;
  536. desc->rqcfg.brst_len = get_burst_len(desc, len);
  537. desc->txd.flags = flags;
  538. return &desc->txd;
  539. }
  540. static struct dma_async_tx_descriptor *
  541. pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  542. unsigned int sg_len, enum dma_data_direction direction,
  543. unsigned long flg)
  544. {
  545. struct dma_pl330_desc *first, *desc = NULL;
  546. struct dma_pl330_chan *pch = to_pchan(chan);
  547. struct dma_pl330_peri *peri = chan->private;
  548. struct scatterlist *sg;
  549. unsigned long flags;
  550. int i;
  551. dma_addr_t addr;
  552. if (unlikely(!pch || !sgl || !sg_len || !peri))
  553. return NULL;
  554. /* Make sure the direction is consistent */
  555. if ((direction == DMA_TO_DEVICE &&
  556. peri->rqtype != MEMTODEV) ||
  557. (direction == DMA_FROM_DEVICE &&
  558. peri->rqtype != DEVTOMEM)) {
  559. dev_err(pch->dmac->pif.dev, "%s:%d Invalid Direction\n",
  560. __func__, __LINE__);
  561. return NULL;
  562. }
  563. addr = pch->fifo_addr;
  564. first = NULL;
  565. for_each_sg(sgl, sg, sg_len, i) {
  566. desc = pl330_get_desc(pch);
  567. if (!desc) {
  568. struct dma_pl330_dmac *pdmac = pch->dmac;
  569. dev_err(pch->dmac->pif.dev,
  570. "%s:%d Unable to fetch desc\n",
  571. __func__, __LINE__);
  572. if (!first)
  573. return NULL;
  574. spin_lock_irqsave(&pdmac->pool_lock, flags);
  575. while (!list_empty(&first->node)) {
  576. desc = list_entry(first->node.next,
  577. struct dma_pl330_desc, node);
  578. list_move_tail(&desc->node, &pdmac->desc_pool);
  579. }
  580. list_move_tail(&first->node, &pdmac->desc_pool);
  581. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  582. return NULL;
  583. }
  584. if (!first)
  585. first = desc;
  586. else
  587. list_add_tail(&desc->node, &first->node);
  588. if (direction == DMA_TO_DEVICE) {
  589. desc->rqcfg.src_inc = 1;
  590. desc->rqcfg.dst_inc = 0;
  591. fill_px(&desc->px,
  592. addr, sg_dma_address(sg), sg_dma_len(sg));
  593. } else {
  594. desc->rqcfg.src_inc = 0;
  595. desc->rqcfg.dst_inc = 1;
  596. fill_px(&desc->px,
  597. sg_dma_address(sg), addr, sg_dma_len(sg));
  598. }
  599. desc->rqcfg.brst_size = pch->burst_sz;
  600. desc->rqcfg.brst_len = 1;
  601. }
  602. /* Return the last desc in the chain */
  603. desc->txd.flags = flg;
  604. return &desc->txd;
  605. }
  606. static irqreturn_t pl330_irq_handler(int irq, void *data)
  607. {
  608. if (pl330_update(data))
  609. return IRQ_HANDLED;
  610. else
  611. return IRQ_NONE;
  612. }
  613. static int __devinit
  614. pl330_probe(struct amba_device *adev, const struct amba_id *id)
  615. {
  616. struct dma_pl330_platdata *pdat;
  617. struct dma_pl330_dmac *pdmac;
  618. struct dma_pl330_chan *pch;
  619. struct pl330_info *pi;
  620. struct dma_device *pd;
  621. struct resource *res;
  622. int i, ret, irq;
  623. int num_chan;
  624. pdat = adev->dev.platform_data;
  625. /* Allocate a new DMAC and its Channels */
  626. pdmac = kzalloc(sizeof(*pdmac), GFP_KERNEL);
  627. if (!pdmac) {
  628. dev_err(&adev->dev, "unable to allocate mem\n");
  629. return -ENOMEM;
  630. }
  631. pi = &pdmac->pif;
  632. pi->dev = &adev->dev;
  633. pi->pl330_data = NULL;
  634. pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
  635. res = &adev->res;
  636. request_mem_region(res->start, resource_size(res), "dma-pl330");
  637. pi->base = ioremap(res->start, resource_size(res));
  638. if (!pi->base) {
  639. ret = -ENXIO;
  640. goto probe_err1;
  641. }
  642. pdmac->clk = clk_get(&adev->dev, "dma");
  643. if (IS_ERR(pdmac->clk)) {
  644. dev_err(&adev->dev, "Cannot get operation clock.\n");
  645. ret = -EINVAL;
  646. goto probe_err1;
  647. }
  648. amba_set_drvdata(adev, pdmac);
  649. #ifdef CONFIG_PM_RUNTIME
  650. /* to use the runtime PM helper functions */
  651. pm_runtime_enable(&adev->dev);
  652. /* enable the power domain */
  653. if (pm_runtime_get_sync(&adev->dev)) {
  654. dev_err(&adev->dev, "failed to get runtime pm\n");
  655. ret = -ENODEV;
  656. goto probe_err1;
  657. }
  658. #else
  659. /* enable dma clk */
  660. clk_enable(pdmac->clk);
  661. #endif
  662. irq = adev->irq[0];
  663. ret = request_irq(irq, pl330_irq_handler, 0,
  664. dev_name(&adev->dev), pi);
  665. if (ret)
  666. goto probe_err2;
  667. ret = pl330_add(pi);
  668. if (ret)
  669. goto probe_err3;
  670. INIT_LIST_HEAD(&pdmac->desc_pool);
  671. spin_lock_init(&pdmac->pool_lock);
  672. /* Create a descriptor pool of default size */
  673. if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
  674. dev_warn(&adev->dev, "unable to allocate desc\n");
  675. pd = &pdmac->ddma;
  676. INIT_LIST_HEAD(&pd->channels);
  677. /* Initialize channel parameters */
  678. num_chan = max(pdat ? pdat->nr_valid_peri : 0, (u8)pi->pcfg.num_chan);
  679. pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
  680. for (i = 0; i < num_chan; i++) {
  681. pch = &pdmac->peripherals[i];
  682. if (pdat) {
  683. struct dma_pl330_peri *peri = &pdat->peri[i];
  684. switch (peri->rqtype) {
  685. case MEMTOMEM:
  686. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  687. break;
  688. case MEMTODEV:
  689. case DEVTOMEM:
  690. dma_cap_set(DMA_SLAVE, pd->cap_mask);
  691. dma_cap_set(DMA_CYCLIC, pd->cap_mask);
  692. break;
  693. default:
  694. dev_err(&adev->dev, "DEVTODEV Not Supported\n");
  695. continue;
  696. }
  697. pch->chan.private = peri;
  698. } else {
  699. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  700. pch->chan.private = NULL;
  701. }
  702. INIT_LIST_HEAD(&pch->work_list);
  703. spin_lock_init(&pch->lock);
  704. pch->pl330_chid = NULL;
  705. pch->chan.device = pd;
  706. pch->dmac = pdmac;
  707. /* Add the channel to the DMAC list */
  708. list_add_tail(&pch->chan.device_node, &pd->channels);
  709. }
  710. pd->dev = &adev->dev;
  711. pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
  712. pd->device_free_chan_resources = pl330_free_chan_resources;
  713. pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
  714. pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
  715. pd->device_tx_status = pl330_tx_status;
  716. pd->device_prep_slave_sg = pl330_prep_slave_sg;
  717. pd->device_control = pl330_control;
  718. pd->device_issue_pending = pl330_issue_pending;
  719. ret = dma_async_device_register(pd);
  720. if (ret) {
  721. dev_err(&adev->dev, "unable to register DMAC\n");
  722. goto probe_err4;
  723. }
  724. dev_info(&adev->dev,
  725. "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
  726. dev_info(&adev->dev,
  727. "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
  728. pi->pcfg.data_buf_dep,
  729. pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
  730. pi->pcfg.num_peri, pi->pcfg.num_events);
  731. return 0;
  732. probe_err4:
  733. pl330_del(pi);
  734. probe_err3:
  735. free_irq(irq, pi);
  736. probe_err2:
  737. iounmap(pi->base);
  738. probe_err1:
  739. release_mem_region(res->start, resource_size(res));
  740. kfree(pdmac);
  741. return ret;
  742. }
  743. static int __devexit pl330_remove(struct amba_device *adev)
  744. {
  745. struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
  746. struct dma_pl330_chan *pch, *_p;
  747. struct pl330_info *pi;
  748. struct resource *res;
  749. int irq;
  750. if (!pdmac)
  751. return 0;
  752. amba_set_drvdata(adev, NULL);
  753. /* Idle the DMAC */
  754. list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
  755. chan.device_node) {
  756. /* Remove the channel */
  757. list_del(&pch->chan.device_node);
  758. /* Flush the channel */
  759. pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
  760. pl330_free_chan_resources(&pch->chan);
  761. }
  762. pi = &pdmac->pif;
  763. pl330_del(pi);
  764. irq = adev->irq[0];
  765. free_irq(irq, pi);
  766. iounmap(pi->base);
  767. res = &adev->res;
  768. release_mem_region(res->start, resource_size(res));
  769. #ifdef CONFIG_PM_RUNTIME
  770. pm_runtime_put(&adev->dev);
  771. pm_runtime_disable(&adev->dev);
  772. #else
  773. clk_disable(pdmac->clk);
  774. #endif
  775. kfree(pdmac);
  776. return 0;
  777. }
  778. static struct amba_id pl330_ids[] = {
  779. {
  780. .id = 0x00041330,
  781. .mask = 0x000fffff,
  782. },
  783. { 0, 0 },
  784. };
  785. #ifdef CONFIG_PM_RUNTIME
  786. static int pl330_runtime_suspend(struct device *dev)
  787. {
  788. struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
  789. if (!pdmac) {
  790. dev_err(dev, "failed to get dmac\n");
  791. return -ENODEV;
  792. }
  793. clk_disable(pdmac->clk);
  794. return 0;
  795. }
  796. static int pl330_runtime_resume(struct device *dev)
  797. {
  798. struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
  799. if (!pdmac) {
  800. dev_err(dev, "failed to get dmac\n");
  801. return -ENODEV;
  802. }
  803. clk_enable(pdmac->clk);
  804. return 0;
  805. }
  806. #else
  807. #define pl330_runtime_suspend NULL
  808. #define pl330_runtime_resume NULL
  809. #endif /* CONFIG_PM_RUNTIME */
  810. static const struct dev_pm_ops pl330_pm_ops = {
  811. .runtime_suspend = pl330_runtime_suspend,
  812. .runtime_resume = pl330_runtime_resume,
  813. };
  814. static struct amba_driver pl330_driver = {
  815. .drv = {
  816. .owner = THIS_MODULE,
  817. .name = "dma-pl330",
  818. .pm = &pl330_pm_ops,
  819. },
  820. .id_table = pl330_ids,
  821. .probe = pl330_probe,
  822. .remove = pl330_remove,
  823. };
  824. static int __init pl330_init(void)
  825. {
  826. return amba_driver_register(&pl330_driver);
  827. }
  828. module_init(pl330_init);
  829. static void __exit pl330_exit(void)
  830. {
  831. amba_driver_unregister(&pl330_driver);
  832. return;
  833. }
  834. module_exit(pl330_exit);
  835. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  836. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  837. MODULE_LICENSE("GPL");