pl330.c 23 KB

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