pl330.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. MODULE_DEVICE_TABLE(amba, pl330_ids);
  786. #ifdef CONFIG_PM_RUNTIME
  787. static int pl330_runtime_suspend(struct device *dev)
  788. {
  789. struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
  790. if (!pdmac) {
  791. dev_err(dev, "failed to get dmac\n");
  792. return -ENODEV;
  793. }
  794. clk_disable(pdmac->clk);
  795. return 0;
  796. }
  797. static int pl330_runtime_resume(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_enable(pdmac->clk);
  805. return 0;
  806. }
  807. #else
  808. #define pl330_runtime_suspend NULL
  809. #define pl330_runtime_resume NULL
  810. #endif /* CONFIG_PM_RUNTIME */
  811. static const struct dev_pm_ops pl330_pm_ops = {
  812. .runtime_suspend = pl330_runtime_suspend,
  813. .runtime_resume = pl330_runtime_resume,
  814. };
  815. static struct amba_driver pl330_driver = {
  816. .drv = {
  817. .owner = THIS_MODULE,
  818. .name = "dma-pl330",
  819. .pm = &pl330_pm_ops,
  820. },
  821. .id_table = pl330_ids,
  822. .probe = pl330_probe,
  823. .remove = pl330_remove,
  824. };
  825. static int __init pl330_init(void)
  826. {
  827. return amba_driver_register(&pl330_driver);
  828. }
  829. module_init(pl330_init);
  830. static void __exit pl330_exit(void)
  831. {
  832. amba_driver_unregister(&pl330_driver);
  833. return;
  834. }
  835. module_exit(pl330_exit);
  836. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  837. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  838. MODULE_LICENSE("GPL");