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. #include <linux/of.h>
  22. #include "dmaengine.h"
  23. #define NR_DEFAULT_DESC 16
  24. enum desc_status {
  25. /* In the DMAC pool */
  26. FREE,
  27. /*
  28. * Allocted to some channel during prep_xxx
  29. * Also may be sitting on the work_list.
  30. */
  31. PREP,
  32. /*
  33. * Sitting on the work_list and already submitted
  34. * to the PL330 core. Not more than two descriptors
  35. * of a channel can be BUSY at any time.
  36. */
  37. BUSY,
  38. /*
  39. * Sitting on the channel work_list but xfer done
  40. * by PL330 core
  41. */
  42. DONE,
  43. };
  44. struct dma_pl330_chan {
  45. /* Schedule desc completion */
  46. struct tasklet_struct task;
  47. /* DMA-Engine Channel */
  48. struct dma_chan chan;
  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->chan.completed_cookie = 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. u8 *peri_id;
  218. if (chan->device->dev->driver != &pl330_driver.drv)
  219. return false;
  220. #ifdef CONFIG_OF
  221. if (chan->device->dev->of_node) {
  222. const __be32 *prop_value;
  223. phandle phandle;
  224. struct device_node *node;
  225. prop_value = ((struct property *)param)->value;
  226. phandle = be32_to_cpup(prop_value++);
  227. node = of_find_node_by_phandle(phandle);
  228. return ((chan->private == node) &&
  229. (chan->chan_id == be32_to_cpup(prop_value)));
  230. }
  231. #endif
  232. peri_id = chan->private;
  233. return *peri_id == (unsigned)param;
  234. }
  235. EXPORT_SYMBOL(pl330_filter);
  236. static int pl330_alloc_chan_resources(struct dma_chan *chan)
  237. {
  238. struct dma_pl330_chan *pch = to_pchan(chan);
  239. struct dma_pl330_dmac *pdmac = pch->dmac;
  240. unsigned long flags;
  241. spin_lock_irqsave(&pch->lock, flags);
  242. chan->completed_cookie = chan->cookie = 1;
  243. pch->cyclic = false;
  244. pch->pl330_chid = pl330_request_channel(&pdmac->pif);
  245. if (!pch->pl330_chid) {
  246. spin_unlock_irqrestore(&pch->lock, flags);
  247. return 0;
  248. }
  249. tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch);
  250. spin_unlock_irqrestore(&pch->lock, flags);
  251. return 1;
  252. }
  253. static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg)
  254. {
  255. struct dma_pl330_chan *pch = to_pchan(chan);
  256. struct dma_pl330_desc *desc, *_dt;
  257. unsigned long flags;
  258. struct dma_pl330_dmac *pdmac = pch->dmac;
  259. struct dma_slave_config *slave_config;
  260. LIST_HEAD(list);
  261. switch (cmd) {
  262. case DMA_TERMINATE_ALL:
  263. spin_lock_irqsave(&pch->lock, flags);
  264. /* FLUSH the PL330 Channel thread */
  265. pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH);
  266. /* Mark all desc done */
  267. list_for_each_entry_safe(desc, _dt, &pch->work_list , node) {
  268. desc->status = DONE;
  269. pch->completed = desc->txd.cookie;
  270. list_move_tail(&desc->node, &list);
  271. }
  272. list_splice_tail_init(&list, &pdmac->desc_pool);
  273. spin_unlock_irqrestore(&pch->lock, flags);
  274. break;
  275. case DMA_SLAVE_CONFIG:
  276. slave_config = (struct dma_slave_config *)arg;
  277. if (slave_config->direction == DMA_MEM_TO_DEV) {
  278. if (slave_config->dst_addr)
  279. pch->fifo_addr = slave_config->dst_addr;
  280. if (slave_config->dst_addr_width)
  281. pch->burst_sz = __ffs(slave_config->dst_addr_width);
  282. if (slave_config->dst_maxburst)
  283. pch->burst_len = slave_config->dst_maxburst;
  284. } else if (slave_config->direction == DMA_DEV_TO_MEM) {
  285. if (slave_config->src_addr)
  286. pch->fifo_addr = slave_config->src_addr;
  287. if (slave_config->src_addr_width)
  288. pch->burst_sz = __ffs(slave_config->src_addr_width);
  289. if (slave_config->src_maxburst)
  290. pch->burst_len = slave_config->src_maxburst;
  291. }
  292. break;
  293. default:
  294. dev_err(pch->dmac->pif.dev, "Not supported command.\n");
  295. return -ENXIO;
  296. }
  297. return 0;
  298. }
  299. static void pl330_free_chan_resources(struct dma_chan *chan)
  300. {
  301. struct dma_pl330_chan *pch = to_pchan(chan);
  302. unsigned long flags;
  303. spin_lock_irqsave(&pch->lock, flags);
  304. tasklet_kill(&pch->task);
  305. pl330_release_channel(pch->pl330_chid);
  306. pch->pl330_chid = NULL;
  307. if (pch->cyclic)
  308. list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool);
  309. spin_unlock_irqrestore(&pch->lock, flags);
  310. }
  311. static enum dma_status
  312. pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  313. struct dma_tx_state *txstate)
  314. {
  315. struct dma_pl330_chan *pch = to_pchan(chan);
  316. dma_cookie_t last_done, last_used;
  317. int ret;
  318. last_done = chan->completed_cookie;
  319. last_used = chan->cookie;
  320. ret = dma_async_is_complete(cookie, last_done, last_used);
  321. dma_set_tx_state(txstate, last_done, last_used, 0);
  322. return ret;
  323. }
  324. static void pl330_issue_pending(struct dma_chan *chan)
  325. {
  326. pl330_tasklet((unsigned long) to_pchan(chan));
  327. }
  328. /*
  329. * We returned the last one of the circular list of descriptor(s)
  330. * from prep_xxx, so the argument to submit corresponds to the last
  331. * descriptor of the list.
  332. */
  333. static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx)
  334. {
  335. struct dma_pl330_desc *desc, *last = to_desc(tx);
  336. struct dma_pl330_chan *pch = to_pchan(tx->chan);
  337. dma_cookie_t cookie;
  338. unsigned long flags;
  339. spin_lock_irqsave(&pch->lock, flags);
  340. /* Assign cookies to all nodes */
  341. cookie = tx->chan->cookie;
  342. while (!list_empty(&last->node)) {
  343. desc = list_entry(last->node.next, struct dma_pl330_desc, node);
  344. if (++cookie < 0)
  345. cookie = 1;
  346. desc->txd.cookie = cookie;
  347. list_move_tail(&desc->node, &pch->work_list);
  348. }
  349. if (++cookie < 0)
  350. cookie = 1;
  351. last->txd.cookie = cookie;
  352. list_add_tail(&last->node, &pch->work_list);
  353. tx->chan->cookie = cookie;
  354. spin_unlock_irqrestore(&pch->lock, flags);
  355. return cookie;
  356. }
  357. static inline void _init_desc(struct dma_pl330_desc *desc)
  358. {
  359. desc->pchan = NULL;
  360. desc->req.x = &desc->px;
  361. desc->req.token = desc;
  362. desc->rqcfg.swap = SWAP_NO;
  363. desc->rqcfg.privileged = 0;
  364. desc->rqcfg.insnaccess = 0;
  365. desc->rqcfg.scctl = SCCTRL0;
  366. desc->rqcfg.dcctl = DCCTRL0;
  367. desc->req.cfg = &desc->rqcfg;
  368. desc->req.xfer_cb = dma_pl330_rqcb;
  369. desc->txd.tx_submit = pl330_tx_submit;
  370. INIT_LIST_HEAD(&desc->node);
  371. }
  372. /* Returns the number of descriptors added to the DMAC pool */
  373. int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
  374. {
  375. struct dma_pl330_desc *desc;
  376. unsigned long flags;
  377. int i;
  378. if (!pdmac)
  379. return 0;
  380. desc = kmalloc(count * sizeof(*desc), flg);
  381. if (!desc)
  382. return 0;
  383. spin_lock_irqsave(&pdmac->pool_lock, flags);
  384. for (i = 0; i < count; i++) {
  385. _init_desc(&desc[i]);
  386. list_add_tail(&desc[i].node, &pdmac->desc_pool);
  387. }
  388. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  389. return count;
  390. }
  391. static struct dma_pl330_desc *
  392. pluck_desc(struct dma_pl330_dmac *pdmac)
  393. {
  394. struct dma_pl330_desc *desc = NULL;
  395. unsigned long flags;
  396. if (!pdmac)
  397. return NULL;
  398. spin_lock_irqsave(&pdmac->pool_lock, flags);
  399. if (!list_empty(&pdmac->desc_pool)) {
  400. desc = list_entry(pdmac->desc_pool.next,
  401. struct dma_pl330_desc, node);
  402. list_del_init(&desc->node);
  403. desc->status = PREP;
  404. desc->txd.callback = NULL;
  405. }
  406. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  407. return desc;
  408. }
  409. static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
  410. {
  411. struct dma_pl330_dmac *pdmac = pch->dmac;
  412. u8 *peri_id = pch->chan.private;
  413. struct dma_pl330_desc *desc;
  414. /* Pluck one desc from the pool of DMAC */
  415. desc = pluck_desc(pdmac);
  416. /* If the DMAC pool is empty, alloc new */
  417. if (!desc) {
  418. if (!add_desc(pdmac, GFP_ATOMIC, 1))
  419. return NULL;
  420. /* Try again */
  421. desc = pluck_desc(pdmac);
  422. if (!desc) {
  423. dev_err(pch->dmac->pif.dev,
  424. "%s:%d ALERT!\n", __func__, __LINE__);
  425. return NULL;
  426. }
  427. }
  428. /* Initialize the descriptor */
  429. desc->pchan = pch;
  430. desc->txd.cookie = 0;
  431. async_tx_ack(&desc->txd);
  432. desc->req.peri = peri_id ? pch->chan.chan_id : 0;
  433. dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
  434. return desc;
  435. }
  436. static inline void fill_px(struct pl330_xfer *px,
  437. dma_addr_t dst, dma_addr_t src, size_t len)
  438. {
  439. px->next = NULL;
  440. px->bytes = len;
  441. px->dst_addr = dst;
  442. px->src_addr = src;
  443. }
  444. static struct dma_pl330_desc *
  445. __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
  446. dma_addr_t src, size_t len)
  447. {
  448. struct dma_pl330_desc *desc = pl330_get_desc(pch);
  449. if (!desc) {
  450. dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
  451. __func__, __LINE__);
  452. return NULL;
  453. }
  454. /*
  455. * Ideally we should lookout for reqs bigger than
  456. * those that can be programmed with 256 bytes of
  457. * MC buffer, but considering a req size is seldom
  458. * going to be word-unaligned and more than 200MB,
  459. * we take it easy.
  460. * Also, should the limit is reached we'd rather
  461. * have the platform increase MC buffer size than
  462. * complicating this API driver.
  463. */
  464. fill_px(&desc->px, dst, src, len);
  465. return desc;
  466. }
  467. /* Call after fixing burst size */
  468. static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
  469. {
  470. struct dma_pl330_chan *pch = desc->pchan;
  471. struct pl330_info *pi = &pch->dmac->pif;
  472. int burst_len;
  473. burst_len = pi->pcfg.data_bus_width / 8;
  474. burst_len *= pi->pcfg.data_buf_dep;
  475. burst_len >>= desc->rqcfg.brst_size;
  476. /* src/dst_burst_len can't be more than 16 */
  477. if (burst_len > 16)
  478. burst_len = 16;
  479. while (burst_len > 1) {
  480. if (!(len % (burst_len << desc->rqcfg.brst_size)))
  481. break;
  482. burst_len--;
  483. }
  484. return burst_len;
  485. }
  486. static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
  487. struct dma_chan *chan, dma_addr_t dma_addr, size_t len,
  488. size_t period_len, enum dma_transfer_direction direction)
  489. {
  490. struct dma_pl330_desc *desc;
  491. struct dma_pl330_chan *pch = to_pchan(chan);
  492. dma_addr_t dst;
  493. dma_addr_t src;
  494. desc = pl330_get_desc(pch);
  495. if (!desc) {
  496. dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
  497. __func__, __LINE__);
  498. return NULL;
  499. }
  500. switch (direction) {
  501. case DMA_MEM_TO_DEV:
  502. desc->rqcfg.src_inc = 1;
  503. desc->rqcfg.dst_inc = 0;
  504. desc->req.rqtype = MEMTODEV;
  505. src = dma_addr;
  506. dst = pch->fifo_addr;
  507. break;
  508. case DMA_DEV_TO_MEM:
  509. desc->rqcfg.src_inc = 0;
  510. desc->rqcfg.dst_inc = 1;
  511. desc->req.rqtype = DEVTOMEM;
  512. src = pch->fifo_addr;
  513. dst = dma_addr;
  514. break;
  515. default:
  516. dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
  517. __func__, __LINE__);
  518. return NULL;
  519. }
  520. desc->rqcfg.brst_size = pch->burst_sz;
  521. desc->rqcfg.brst_len = 1;
  522. pch->cyclic = true;
  523. fill_px(&desc->px, dst, src, period_len);
  524. return &desc->txd;
  525. }
  526. static struct dma_async_tx_descriptor *
  527. pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
  528. dma_addr_t src, size_t len, unsigned long flags)
  529. {
  530. struct dma_pl330_desc *desc;
  531. struct dma_pl330_chan *pch = to_pchan(chan);
  532. struct pl330_info *pi;
  533. int burst;
  534. if (unlikely(!pch || !len))
  535. return NULL;
  536. pi = &pch->dmac->pif;
  537. desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
  538. if (!desc)
  539. return NULL;
  540. desc->rqcfg.src_inc = 1;
  541. desc->rqcfg.dst_inc = 1;
  542. desc->req.rqtype = MEMTOMEM;
  543. /* Select max possible burst size */
  544. burst = pi->pcfg.data_bus_width / 8;
  545. while (burst > 1) {
  546. if (!(len % burst))
  547. break;
  548. burst /= 2;
  549. }
  550. desc->rqcfg.brst_size = 0;
  551. while (burst != (1 << desc->rqcfg.brst_size))
  552. desc->rqcfg.brst_size++;
  553. desc->rqcfg.brst_len = get_burst_len(desc, len);
  554. desc->txd.flags = flags;
  555. return &desc->txd;
  556. }
  557. static struct dma_async_tx_descriptor *
  558. pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  559. unsigned int sg_len, enum dma_transfer_direction direction,
  560. unsigned long flg)
  561. {
  562. struct dma_pl330_desc *first, *desc = NULL;
  563. struct dma_pl330_chan *pch = to_pchan(chan);
  564. struct scatterlist *sg;
  565. unsigned long flags;
  566. int i;
  567. dma_addr_t addr;
  568. if (unlikely(!pch || !sgl || !sg_len))
  569. return NULL;
  570. addr = pch->fifo_addr;
  571. first = NULL;
  572. for_each_sg(sgl, sg, sg_len, i) {
  573. desc = pl330_get_desc(pch);
  574. if (!desc) {
  575. struct dma_pl330_dmac *pdmac = pch->dmac;
  576. dev_err(pch->dmac->pif.dev,
  577. "%s:%d Unable to fetch desc\n",
  578. __func__, __LINE__);
  579. if (!first)
  580. return NULL;
  581. spin_lock_irqsave(&pdmac->pool_lock, flags);
  582. while (!list_empty(&first->node)) {
  583. desc = list_entry(first->node.next,
  584. struct dma_pl330_desc, node);
  585. list_move_tail(&desc->node, &pdmac->desc_pool);
  586. }
  587. list_move_tail(&first->node, &pdmac->desc_pool);
  588. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  589. return NULL;
  590. }
  591. if (!first)
  592. first = desc;
  593. else
  594. list_add_tail(&desc->node, &first->node);
  595. if (direction == DMA_MEM_TO_DEV) {
  596. desc->rqcfg.src_inc = 1;
  597. desc->rqcfg.dst_inc = 0;
  598. desc->req.rqtype = MEMTODEV;
  599. fill_px(&desc->px,
  600. addr, sg_dma_address(sg), sg_dma_len(sg));
  601. } else {
  602. desc->rqcfg.src_inc = 0;
  603. desc->rqcfg.dst_inc = 1;
  604. desc->req.rqtype = DEVTOMEM;
  605. fill_px(&desc->px,
  606. sg_dma_address(sg), addr, sg_dma_len(sg));
  607. }
  608. desc->rqcfg.brst_size = pch->burst_sz;
  609. desc->rqcfg.brst_len = 1;
  610. }
  611. /* Return the last desc in the chain */
  612. desc->txd.flags = flg;
  613. return &desc->txd;
  614. }
  615. static irqreturn_t pl330_irq_handler(int irq, void *data)
  616. {
  617. if (pl330_update(data))
  618. return IRQ_HANDLED;
  619. else
  620. return IRQ_NONE;
  621. }
  622. static int __devinit
  623. pl330_probe(struct amba_device *adev, const struct amba_id *id)
  624. {
  625. struct dma_pl330_platdata *pdat;
  626. struct dma_pl330_dmac *pdmac;
  627. struct dma_pl330_chan *pch;
  628. struct pl330_info *pi;
  629. struct dma_device *pd;
  630. struct resource *res;
  631. int i, ret, irq;
  632. int num_chan;
  633. pdat = adev->dev.platform_data;
  634. /* Allocate a new DMAC and its Channels */
  635. pdmac = kzalloc(sizeof(*pdmac), GFP_KERNEL);
  636. if (!pdmac) {
  637. dev_err(&adev->dev, "unable to allocate mem\n");
  638. return -ENOMEM;
  639. }
  640. pi = &pdmac->pif;
  641. pi->dev = &adev->dev;
  642. pi->pl330_data = NULL;
  643. pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
  644. res = &adev->res;
  645. request_mem_region(res->start, resource_size(res), "dma-pl330");
  646. pi->base = ioremap(res->start, resource_size(res));
  647. if (!pi->base) {
  648. ret = -ENXIO;
  649. goto probe_err1;
  650. }
  651. pdmac->clk = clk_get(&adev->dev, "dma");
  652. if (IS_ERR(pdmac->clk)) {
  653. dev_err(&adev->dev, "Cannot get operation clock.\n");
  654. ret = -EINVAL;
  655. goto probe_err2;
  656. }
  657. amba_set_drvdata(adev, pdmac);
  658. #ifndef CONFIG_PM_RUNTIME
  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_err3;
  667. ret = pl330_add(pi);
  668. if (ret)
  669. goto probe_err4;
  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 : (u8)pi->pcfg.num_peri,
  679. (u8)pi->pcfg.num_chan);
  680. pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
  681. for (i = 0; i < num_chan; i++) {
  682. pch = &pdmac->peripherals[i];
  683. if (!adev->dev.of_node)
  684. pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
  685. else
  686. pch->chan.private = adev->dev.of_node;
  687. INIT_LIST_HEAD(&pch->work_list);
  688. spin_lock_init(&pch->lock);
  689. pch->pl330_chid = NULL;
  690. pch->chan.device = pd;
  691. pch->dmac = pdmac;
  692. /* Add the channel to the DMAC list */
  693. list_add_tail(&pch->chan.device_node, &pd->channels);
  694. }
  695. pd->dev = &adev->dev;
  696. if (pdat) {
  697. pd->cap_mask = pdat->cap_mask;
  698. } else {
  699. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  700. if (pi->pcfg.num_peri) {
  701. dma_cap_set(DMA_SLAVE, pd->cap_mask);
  702. dma_cap_set(DMA_CYCLIC, pd->cap_mask);
  703. }
  704. }
  705. pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
  706. pd->device_free_chan_resources = pl330_free_chan_resources;
  707. pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
  708. pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
  709. pd->device_tx_status = pl330_tx_status;
  710. pd->device_prep_slave_sg = pl330_prep_slave_sg;
  711. pd->device_control = pl330_control;
  712. pd->device_issue_pending = pl330_issue_pending;
  713. ret = dma_async_device_register(pd);
  714. if (ret) {
  715. dev_err(&adev->dev, "unable to register DMAC\n");
  716. goto probe_err5;
  717. }
  718. dev_info(&adev->dev,
  719. "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
  720. dev_info(&adev->dev,
  721. "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
  722. pi->pcfg.data_buf_dep,
  723. pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
  724. pi->pcfg.num_peri, pi->pcfg.num_events);
  725. return 0;
  726. probe_err5:
  727. pl330_del(pi);
  728. probe_err4:
  729. free_irq(irq, pi);
  730. probe_err3:
  731. #ifndef CONFIG_PM_RUNTIME
  732. clk_disable(pdmac->clk);
  733. #endif
  734. clk_put(pdmac->clk);
  735. probe_err2:
  736. iounmap(pi->base);
  737. probe_err1:
  738. release_mem_region(res->start, resource_size(res));
  739. kfree(pdmac);
  740. return ret;
  741. }
  742. static int __devexit pl330_remove(struct amba_device *adev)
  743. {
  744. struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
  745. struct dma_pl330_chan *pch, *_p;
  746. struct pl330_info *pi;
  747. struct resource *res;
  748. int irq;
  749. if (!pdmac)
  750. return 0;
  751. amba_set_drvdata(adev, NULL);
  752. /* Idle the DMAC */
  753. list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
  754. chan.device_node) {
  755. /* Remove the channel */
  756. list_del(&pch->chan.device_node);
  757. /* Flush the channel */
  758. pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
  759. pl330_free_chan_resources(&pch->chan);
  760. }
  761. pi = &pdmac->pif;
  762. pl330_del(pi);
  763. irq = adev->irq[0];
  764. free_irq(irq, pi);
  765. iounmap(pi->base);
  766. res = &adev->res;
  767. release_mem_region(res->start, resource_size(res));
  768. #ifndef CONFIG_PM_RUNTIME
  769. clk_disable(pdmac->clk);
  770. #endif
  771. kfree(pdmac);
  772. return 0;
  773. }
  774. static struct amba_id pl330_ids[] = {
  775. {
  776. .id = 0x00041330,
  777. .mask = 0x000fffff,
  778. },
  779. { 0, 0 },
  780. };
  781. MODULE_DEVICE_TABLE(amba, pl330_ids);
  782. #ifdef CONFIG_PM_RUNTIME
  783. static int pl330_runtime_suspend(struct device *dev)
  784. {
  785. struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
  786. if (!pdmac) {
  787. dev_err(dev, "failed to get dmac\n");
  788. return -ENODEV;
  789. }
  790. clk_disable(pdmac->clk);
  791. return 0;
  792. }
  793. static int pl330_runtime_resume(struct device *dev)
  794. {
  795. struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
  796. if (!pdmac) {
  797. dev_err(dev, "failed to get dmac\n");
  798. return -ENODEV;
  799. }
  800. clk_enable(pdmac->clk);
  801. return 0;
  802. }
  803. #else
  804. #define pl330_runtime_suspend NULL
  805. #define pl330_runtime_resume NULL
  806. #endif /* CONFIG_PM_RUNTIME */
  807. static const struct dev_pm_ops pl330_pm_ops = {
  808. .runtime_suspend = pl330_runtime_suspend,
  809. .runtime_resume = pl330_runtime_resume,
  810. };
  811. static struct amba_driver pl330_driver = {
  812. .drv = {
  813. .owner = THIS_MODULE,
  814. .name = "dma-pl330",
  815. .pm = &pl330_pm_ops,
  816. },
  817. .id_table = pl330_ids,
  818. .probe = pl330_probe,
  819. .remove = pl330_remove,
  820. };
  821. static int __init pl330_init(void)
  822. {
  823. return amba_driver_register(&pl330_driver);
  824. }
  825. module_init(pl330_init);
  826. static void __exit pl330_exit(void)
  827. {
  828. amba_driver_unregister(&pl330_driver);
  829. return;
  830. }
  831. module_exit(pl330_exit);
  832. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  833. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  834. MODULE_LICENSE("GPL");