pl330.c 19 KB

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