pl330.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. dma_cookie_complete(&desc->txd);
  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. while (!list_empty(&last->node)) {
  342. desc = list_entry(last->node.next, struct dma_pl330_desc, node);
  343. dma_cookie_assign(&desc->txd);
  344. list_move_tail(&desc->node, &pch->work_list);
  345. }
  346. cookie = dma_cookie_assign(&last->txd);
  347. list_add_tail(&last->node, &pch->work_list);
  348. spin_unlock_irqrestore(&pch->lock, flags);
  349. return cookie;
  350. }
  351. static inline void _init_desc(struct dma_pl330_desc *desc)
  352. {
  353. desc->pchan = NULL;
  354. desc->req.x = &desc->px;
  355. desc->req.token = desc;
  356. desc->rqcfg.swap = SWAP_NO;
  357. desc->rqcfg.privileged = 0;
  358. desc->rqcfg.insnaccess = 0;
  359. desc->rqcfg.scctl = SCCTRL0;
  360. desc->rqcfg.dcctl = DCCTRL0;
  361. desc->req.cfg = &desc->rqcfg;
  362. desc->req.xfer_cb = dma_pl330_rqcb;
  363. desc->txd.tx_submit = pl330_tx_submit;
  364. INIT_LIST_HEAD(&desc->node);
  365. }
  366. /* Returns the number of descriptors added to the DMAC pool */
  367. int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count)
  368. {
  369. struct dma_pl330_desc *desc;
  370. unsigned long flags;
  371. int i;
  372. if (!pdmac)
  373. return 0;
  374. desc = kmalloc(count * sizeof(*desc), flg);
  375. if (!desc)
  376. return 0;
  377. spin_lock_irqsave(&pdmac->pool_lock, flags);
  378. for (i = 0; i < count; i++) {
  379. _init_desc(&desc[i]);
  380. list_add_tail(&desc[i].node, &pdmac->desc_pool);
  381. }
  382. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  383. return count;
  384. }
  385. static struct dma_pl330_desc *
  386. pluck_desc(struct dma_pl330_dmac *pdmac)
  387. {
  388. struct dma_pl330_desc *desc = NULL;
  389. unsigned long flags;
  390. if (!pdmac)
  391. return NULL;
  392. spin_lock_irqsave(&pdmac->pool_lock, flags);
  393. if (!list_empty(&pdmac->desc_pool)) {
  394. desc = list_entry(pdmac->desc_pool.next,
  395. struct dma_pl330_desc, node);
  396. list_del_init(&desc->node);
  397. desc->status = PREP;
  398. desc->txd.callback = NULL;
  399. }
  400. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  401. return desc;
  402. }
  403. static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch)
  404. {
  405. struct dma_pl330_dmac *pdmac = pch->dmac;
  406. u8 *peri_id = pch->chan.private;
  407. struct dma_pl330_desc *desc;
  408. /* Pluck one desc from the pool of DMAC */
  409. desc = pluck_desc(pdmac);
  410. /* If the DMAC pool is empty, alloc new */
  411. if (!desc) {
  412. if (!add_desc(pdmac, GFP_ATOMIC, 1))
  413. return NULL;
  414. /* Try again */
  415. desc = pluck_desc(pdmac);
  416. if (!desc) {
  417. dev_err(pch->dmac->pif.dev,
  418. "%s:%d ALERT!\n", __func__, __LINE__);
  419. return NULL;
  420. }
  421. }
  422. /* Initialize the descriptor */
  423. desc->pchan = pch;
  424. desc->txd.cookie = 0;
  425. async_tx_ack(&desc->txd);
  426. desc->req.peri = peri_id ? pch->chan.chan_id : 0;
  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_transfer_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_MEM_TO_DEV:
  496. desc->rqcfg.src_inc = 1;
  497. desc->rqcfg.dst_inc = 0;
  498. desc->req.rqtype = MEMTODEV;
  499. src = dma_addr;
  500. dst = pch->fifo_addr;
  501. break;
  502. case DMA_DEV_TO_MEM:
  503. desc->rqcfg.src_inc = 0;
  504. desc->rqcfg.dst_inc = 1;
  505. desc->req.rqtype = DEVTOMEM;
  506. src = pch->fifo_addr;
  507. dst = dma_addr;
  508. break;
  509. default:
  510. dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n",
  511. __func__, __LINE__);
  512. return NULL;
  513. }
  514. desc->rqcfg.brst_size = pch->burst_sz;
  515. desc->rqcfg.brst_len = 1;
  516. pch->cyclic = true;
  517. fill_px(&desc->px, dst, src, period_len);
  518. return &desc->txd;
  519. }
  520. static struct dma_async_tx_descriptor *
  521. pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
  522. dma_addr_t src, size_t len, unsigned long flags)
  523. {
  524. struct dma_pl330_desc *desc;
  525. struct dma_pl330_chan *pch = to_pchan(chan);
  526. struct pl330_info *pi;
  527. int burst;
  528. if (unlikely(!pch || !len))
  529. return NULL;
  530. pi = &pch->dmac->pif;
  531. desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
  532. if (!desc)
  533. return NULL;
  534. desc->rqcfg.src_inc = 1;
  535. desc->rqcfg.dst_inc = 1;
  536. desc->req.rqtype = MEMTOMEM;
  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_transfer_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 scatterlist *sg;
  559. unsigned long flags;
  560. int i;
  561. dma_addr_t addr;
  562. if (unlikely(!pch || !sgl || !sg_len))
  563. return NULL;
  564. addr = pch->fifo_addr;
  565. first = NULL;
  566. for_each_sg(sgl, sg, sg_len, i) {
  567. desc = pl330_get_desc(pch);
  568. if (!desc) {
  569. struct dma_pl330_dmac *pdmac = pch->dmac;
  570. dev_err(pch->dmac->pif.dev,
  571. "%s:%d Unable to fetch desc\n",
  572. __func__, __LINE__);
  573. if (!first)
  574. return NULL;
  575. spin_lock_irqsave(&pdmac->pool_lock, flags);
  576. while (!list_empty(&first->node)) {
  577. desc = list_entry(first->node.next,
  578. struct dma_pl330_desc, node);
  579. list_move_tail(&desc->node, &pdmac->desc_pool);
  580. }
  581. list_move_tail(&first->node, &pdmac->desc_pool);
  582. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  583. return NULL;
  584. }
  585. if (!first)
  586. first = desc;
  587. else
  588. list_add_tail(&desc->node, &first->node);
  589. if (direction == DMA_MEM_TO_DEV) {
  590. desc->rqcfg.src_inc = 1;
  591. desc->rqcfg.dst_inc = 0;
  592. desc->req.rqtype = MEMTODEV;
  593. fill_px(&desc->px,
  594. addr, sg_dma_address(sg), sg_dma_len(sg));
  595. } else {
  596. desc->rqcfg.src_inc = 0;
  597. desc->rqcfg.dst_inc = 1;
  598. desc->req.rqtype = DEVTOMEM;
  599. fill_px(&desc->px,
  600. sg_dma_address(sg), addr, sg_dma_len(sg));
  601. }
  602. desc->rqcfg.brst_size = pch->burst_sz;
  603. desc->rqcfg.brst_len = 1;
  604. }
  605. /* Return the last desc in the chain */
  606. desc->txd.flags = flg;
  607. return &desc->txd;
  608. }
  609. static irqreturn_t pl330_irq_handler(int irq, void *data)
  610. {
  611. if (pl330_update(data))
  612. return IRQ_HANDLED;
  613. else
  614. return IRQ_NONE;
  615. }
  616. static int __devinit
  617. pl330_probe(struct amba_device *adev, const struct amba_id *id)
  618. {
  619. struct dma_pl330_platdata *pdat;
  620. struct dma_pl330_dmac *pdmac;
  621. struct dma_pl330_chan *pch;
  622. struct pl330_info *pi;
  623. struct dma_device *pd;
  624. struct resource *res;
  625. int i, ret, irq;
  626. int num_chan;
  627. pdat = adev->dev.platform_data;
  628. /* Allocate a new DMAC and its Channels */
  629. pdmac = kzalloc(sizeof(*pdmac), GFP_KERNEL);
  630. if (!pdmac) {
  631. dev_err(&adev->dev, "unable to allocate mem\n");
  632. return -ENOMEM;
  633. }
  634. pi = &pdmac->pif;
  635. pi->dev = &adev->dev;
  636. pi->pl330_data = NULL;
  637. pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
  638. res = &adev->res;
  639. request_mem_region(res->start, resource_size(res), "dma-pl330");
  640. pi->base = ioremap(res->start, resource_size(res));
  641. if (!pi->base) {
  642. ret = -ENXIO;
  643. goto probe_err1;
  644. }
  645. pdmac->clk = clk_get(&adev->dev, "dma");
  646. if (IS_ERR(pdmac->clk)) {
  647. dev_err(&adev->dev, "Cannot get operation clock.\n");
  648. ret = -EINVAL;
  649. goto probe_err2;
  650. }
  651. amba_set_drvdata(adev, pdmac);
  652. #ifndef CONFIG_PM_RUNTIME
  653. /* enable dma clk */
  654. clk_enable(pdmac->clk);
  655. #endif
  656. irq = adev->irq[0];
  657. ret = request_irq(irq, pl330_irq_handler, 0,
  658. dev_name(&adev->dev), pi);
  659. if (ret)
  660. goto probe_err3;
  661. ret = pl330_add(pi);
  662. if (ret)
  663. goto probe_err4;
  664. INIT_LIST_HEAD(&pdmac->desc_pool);
  665. spin_lock_init(&pdmac->pool_lock);
  666. /* Create a descriptor pool of default size */
  667. if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
  668. dev_warn(&adev->dev, "unable to allocate desc\n");
  669. pd = &pdmac->ddma;
  670. INIT_LIST_HEAD(&pd->channels);
  671. /* Initialize channel parameters */
  672. num_chan = max(pdat ? pdat->nr_valid_peri : (u8)pi->pcfg.num_peri,
  673. (u8)pi->pcfg.num_chan);
  674. pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
  675. for (i = 0; i < num_chan; i++) {
  676. pch = &pdmac->peripherals[i];
  677. if (!adev->dev.of_node)
  678. pch->chan.private = pdat ? &pdat->peri_id[i] : NULL;
  679. else
  680. pch->chan.private = adev->dev.of_node;
  681. INIT_LIST_HEAD(&pch->work_list);
  682. spin_lock_init(&pch->lock);
  683. pch->pl330_chid = NULL;
  684. pch->chan.device = pd;
  685. pch->dmac = pdmac;
  686. /* Add the channel to the DMAC list */
  687. list_add_tail(&pch->chan.device_node, &pd->channels);
  688. }
  689. pd->dev = &adev->dev;
  690. if (pdat) {
  691. pd->cap_mask = pdat->cap_mask;
  692. } else {
  693. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  694. if (pi->pcfg.num_peri) {
  695. dma_cap_set(DMA_SLAVE, pd->cap_mask);
  696. dma_cap_set(DMA_CYCLIC, pd->cap_mask);
  697. }
  698. }
  699. pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
  700. pd->device_free_chan_resources = pl330_free_chan_resources;
  701. pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
  702. pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic;
  703. pd->device_tx_status = pl330_tx_status;
  704. pd->device_prep_slave_sg = pl330_prep_slave_sg;
  705. pd->device_control = pl330_control;
  706. pd->device_issue_pending = pl330_issue_pending;
  707. ret = dma_async_device_register(pd);
  708. if (ret) {
  709. dev_err(&adev->dev, "unable to register DMAC\n");
  710. goto probe_err5;
  711. }
  712. dev_info(&adev->dev,
  713. "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
  714. dev_info(&adev->dev,
  715. "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
  716. pi->pcfg.data_buf_dep,
  717. pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
  718. pi->pcfg.num_peri, pi->pcfg.num_events);
  719. return 0;
  720. probe_err5:
  721. pl330_del(pi);
  722. probe_err4:
  723. free_irq(irq, pi);
  724. probe_err3:
  725. #ifndef CONFIG_PM_RUNTIME
  726. clk_disable(pdmac->clk);
  727. #endif
  728. clk_put(pdmac->clk);
  729. probe_err2:
  730. iounmap(pi->base);
  731. probe_err1:
  732. release_mem_region(res->start, resource_size(res));
  733. kfree(pdmac);
  734. return ret;
  735. }
  736. static int __devexit pl330_remove(struct amba_device *adev)
  737. {
  738. struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
  739. struct dma_pl330_chan *pch, *_p;
  740. struct pl330_info *pi;
  741. struct resource *res;
  742. int irq;
  743. if (!pdmac)
  744. return 0;
  745. amba_set_drvdata(adev, NULL);
  746. /* Idle the DMAC */
  747. list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
  748. chan.device_node) {
  749. /* Remove the channel */
  750. list_del(&pch->chan.device_node);
  751. /* Flush the channel */
  752. pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
  753. pl330_free_chan_resources(&pch->chan);
  754. }
  755. pi = &pdmac->pif;
  756. pl330_del(pi);
  757. irq = adev->irq[0];
  758. free_irq(irq, pi);
  759. iounmap(pi->base);
  760. res = &adev->res;
  761. release_mem_region(res->start, resource_size(res));
  762. #ifndef CONFIG_PM_RUNTIME
  763. clk_disable(pdmac->clk);
  764. #endif
  765. kfree(pdmac);
  766. return 0;
  767. }
  768. static struct amba_id pl330_ids[] = {
  769. {
  770. .id = 0x00041330,
  771. .mask = 0x000fffff,
  772. },
  773. { 0, 0 },
  774. };
  775. MODULE_DEVICE_TABLE(amba, pl330_ids);
  776. #ifdef CONFIG_PM_RUNTIME
  777. static int pl330_runtime_suspend(struct device *dev)
  778. {
  779. struct dma_pl330_dmac *pdmac = dev_get_drvdata(dev);
  780. if (!pdmac) {
  781. dev_err(dev, "failed to get dmac\n");
  782. return -ENODEV;
  783. }
  784. clk_disable(pdmac->clk);
  785. return 0;
  786. }
  787. static int pl330_runtime_resume(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_enable(pdmac->clk);
  795. return 0;
  796. }
  797. #else
  798. #define pl330_runtime_suspend NULL
  799. #define pl330_runtime_resume NULL
  800. #endif /* CONFIG_PM_RUNTIME */
  801. static const struct dev_pm_ops pl330_pm_ops = {
  802. .runtime_suspend = pl330_runtime_suspend,
  803. .runtime_resume = pl330_runtime_resume,
  804. };
  805. static struct amba_driver pl330_driver = {
  806. .drv = {
  807. .owner = THIS_MODULE,
  808. .name = "dma-pl330",
  809. .pm = &pl330_pm_ops,
  810. },
  811. .id_table = pl330_ids,
  812. .probe = pl330_probe,
  813. .remove = pl330_remove,
  814. };
  815. static int __init pl330_init(void)
  816. {
  817. return amba_driver_register(&pl330_driver);
  818. }
  819. module_init(pl330_init);
  820. static void __exit pl330_exit(void)
  821. {
  822. amba_driver_unregister(&pl330_driver);
  823. return;
  824. }
  825. module_exit(pl330_exit);
  826. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  827. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  828. MODULE_LICENSE("GPL");