pl330.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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; /* 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. if (peri) {
  347. desc->req.rqtype = peri->rqtype;
  348. desc->req.peri = peri->peri_id;
  349. } else {
  350. desc->req.rqtype = MEMTOMEM;
  351. desc->req.peri = 0;
  352. }
  353. dma_async_tx_descriptor_init(&desc->txd, &pch->chan);
  354. return desc;
  355. }
  356. static inline void fill_px(struct pl330_xfer *px,
  357. dma_addr_t dst, dma_addr_t src, size_t len)
  358. {
  359. px->next = NULL;
  360. px->bytes = len;
  361. px->dst_addr = dst;
  362. px->src_addr = src;
  363. }
  364. static struct dma_pl330_desc *
  365. __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst,
  366. dma_addr_t src, size_t len)
  367. {
  368. struct dma_pl330_desc *desc = pl330_get_desc(pch);
  369. if (!desc) {
  370. dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n",
  371. __func__, __LINE__);
  372. return NULL;
  373. }
  374. /*
  375. * Ideally we should lookout for reqs bigger than
  376. * those that can be programmed with 256 bytes of
  377. * MC buffer, but considering a req size is seldom
  378. * going to be word-unaligned and more than 200MB,
  379. * we take it easy.
  380. * Also, should the limit is reached we'd rather
  381. * have the platform increase MC buffer size than
  382. * complicating this API driver.
  383. */
  384. fill_px(&desc->px, dst, src, len);
  385. return desc;
  386. }
  387. /* Call after fixing burst size */
  388. static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len)
  389. {
  390. struct dma_pl330_chan *pch = desc->pchan;
  391. struct pl330_info *pi = &pch->dmac->pif;
  392. int burst_len;
  393. burst_len = pi->pcfg.data_bus_width / 8;
  394. burst_len *= pi->pcfg.data_buf_dep;
  395. burst_len >>= desc->rqcfg.brst_size;
  396. /* src/dst_burst_len can't be more than 16 */
  397. if (burst_len > 16)
  398. burst_len = 16;
  399. while (burst_len > 1) {
  400. if (!(len % (burst_len << desc->rqcfg.brst_size)))
  401. break;
  402. burst_len--;
  403. }
  404. return burst_len;
  405. }
  406. static struct dma_async_tx_descriptor *
  407. pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst,
  408. dma_addr_t src, size_t len, unsigned long flags)
  409. {
  410. struct dma_pl330_desc *desc;
  411. struct dma_pl330_chan *pch = to_pchan(chan);
  412. struct dma_pl330_peri *peri = chan->private;
  413. struct pl330_info *pi;
  414. int burst;
  415. if (unlikely(!pch || !len))
  416. return NULL;
  417. if (peri && peri->rqtype != MEMTOMEM)
  418. return NULL;
  419. pi = &pch->dmac->pif;
  420. desc = __pl330_prep_dma_memcpy(pch, dst, src, len);
  421. if (!desc)
  422. return NULL;
  423. desc->rqcfg.src_inc = 1;
  424. desc->rqcfg.dst_inc = 1;
  425. /* Select max possible burst size */
  426. burst = pi->pcfg.data_bus_width / 8;
  427. while (burst > 1) {
  428. if (!(len % burst))
  429. break;
  430. burst /= 2;
  431. }
  432. desc->rqcfg.brst_size = 0;
  433. while (burst != (1 << desc->rqcfg.brst_size))
  434. desc->rqcfg.brst_size++;
  435. desc->rqcfg.brst_len = get_burst_len(desc, len);
  436. desc->txd.flags = flags;
  437. return &desc->txd;
  438. }
  439. static struct dma_async_tx_descriptor *
  440. pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  441. unsigned int sg_len, enum dma_data_direction direction,
  442. unsigned long flg)
  443. {
  444. struct dma_pl330_desc *first, *desc = NULL;
  445. struct dma_pl330_chan *pch = to_pchan(chan);
  446. struct dma_pl330_peri *peri = chan->private;
  447. struct scatterlist *sg;
  448. unsigned long flags;
  449. int i, burst_size;
  450. dma_addr_t addr;
  451. if (unlikely(!pch || !sgl || !sg_len || !peri))
  452. return NULL;
  453. /* Make sure the direction is consistent */
  454. if ((direction == DMA_TO_DEVICE &&
  455. peri->rqtype != MEMTODEV) ||
  456. (direction == DMA_FROM_DEVICE &&
  457. peri->rqtype != DEVTOMEM)) {
  458. dev_err(pch->dmac->pif.dev, "%s:%d Invalid Direction\n",
  459. __func__, __LINE__);
  460. return NULL;
  461. }
  462. addr = peri->fifo_addr;
  463. burst_size = peri->burst_sz;
  464. first = NULL;
  465. for_each_sg(sgl, sg, sg_len, i) {
  466. desc = pl330_get_desc(pch);
  467. if (!desc) {
  468. struct dma_pl330_dmac *pdmac = pch->dmac;
  469. dev_err(pch->dmac->pif.dev,
  470. "%s:%d Unable to fetch desc\n",
  471. __func__, __LINE__);
  472. if (!first)
  473. return NULL;
  474. spin_lock_irqsave(&pdmac->pool_lock, flags);
  475. while (!list_empty(&first->node)) {
  476. desc = list_entry(first->node.next,
  477. struct dma_pl330_desc, node);
  478. list_move_tail(&desc->node, &pdmac->desc_pool);
  479. }
  480. list_move_tail(&first->node, &pdmac->desc_pool);
  481. spin_unlock_irqrestore(&pdmac->pool_lock, flags);
  482. return NULL;
  483. }
  484. if (!first)
  485. first = desc;
  486. else
  487. list_add_tail(&desc->node, &first->node);
  488. if (direction == DMA_TO_DEVICE) {
  489. desc->rqcfg.src_inc = 1;
  490. desc->rqcfg.dst_inc = 0;
  491. fill_px(&desc->px,
  492. addr, sg_dma_address(sg), sg_dma_len(sg));
  493. } else {
  494. desc->rqcfg.src_inc = 0;
  495. desc->rqcfg.dst_inc = 1;
  496. fill_px(&desc->px,
  497. sg_dma_address(sg), addr, sg_dma_len(sg));
  498. }
  499. desc->rqcfg.brst_size = burst_size;
  500. desc->rqcfg.brst_len = 1;
  501. }
  502. /* Return the last desc in the chain */
  503. desc->txd.flags = flg;
  504. return &desc->txd;
  505. }
  506. static irqreturn_t pl330_irq_handler(int irq, void *data)
  507. {
  508. if (pl330_update(data))
  509. return IRQ_HANDLED;
  510. else
  511. return IRQ_NONE;
  512. }
  513. static int __devinit
  514. pl330_probe(struct amba_device *adev, const struct amba_id *id)
  515. {
  516. struct dma_pl330_platdata *pdat;
  517. struct dma_pl330_dmac *pdmac;
  518. struct dma_pl330_chan *pch;
  519. struct pl330_info *pi;
  520. struct dma_device *pd;
  521. struct resource *res;
  522. int i, ret, irq;
  523. int num_chan;
  524. pdat = adev->dev.platform_data;
  525. /* Allocate a new DMAC and its Channels */
  526. pdmac = kzalloc(sizeof(*pdmac), GFP_KERNEL);
  527. if (!pdmac) {
  528. dev_err(&adev->dev, "unable to allocate mem\n");
  529. return -ENOMEM;
  530. }
  531. pi = &pdmac->pif;
  532. pi->dev = &adev->dev;
  533. pi->pl330_data = NULL;
  534. pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
  535. res = &adev->res;
  536. request_mem_region(res->start, resource_size(res), "dma-pl330");
  537. pi->base = ioremap(res->start, resource_size(res));
  538. if (!pi->base) {
  539. ret = -ENXIO;
  540. goto probe_err1;
  541. }
  542. irq = adev->irq[0];
  543. ret = request_irq(irq, pl330_irq_handler, 0,
  544. dev_name(&adev->dev), pi);
  545. if (ret)
  546. goto probe_err2;
  547. ret = pl330_add(pi);
  548. if (ret)
  549. goto probe_err3;
  550. INIT_LIST_HEAD(&pdmac->desc_pool);
  551. spin_lock_init(&pdmac->pool_lock);
  552. /* Create a descriptor pool of default size */
  553. if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC))
  554. dev_warn(&adev->dev, "unable to allocate desc\n");
  555. pd = &pdmac->ddma;
  556. INIT_LIST_HEAD(&pd->channels);
  557. /* Initialize channel parameters */
  558. num_chan = max(pdat ? pdat->nr_valid_peri : 0, (u8)pi->pcfg.num_chan);
  559. pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL);
  560. for (i = 0; i < num_chan; i++) {
  561. pch = &pdmac->peripherals[i];
  562. if (pdat) {
  563. struct dma_pl330_peri *peri = &pdat->peri[i];
  564. switch (peri->rqtype) {
  565. case MEMTOMEM:
  566. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  567. break;
  568. case MEMTODEV:
  569. case DEVTOMEM:
  570. dma_cap_set(DMA_SLAVE, pd->cap_mask);
  571. break;
  572. default:
  573. dev_err(&adev->dev, "DEVTODEV Not Supported\n");
  574. continue;
  575. }
  576. pch->chan.private = peri;
  577. } else {
  578. dma_cap_set(DMA_MEMCPY, pd->cap_mask);
  579. pch->chan.private = NULL;
  580. }
  581. INIT_LIST_HEAD(&pch->work_list);
  582. spin_lock_init(&pch->lock);
  583. pch->pl330_chid = NULL;
  584. pch->chan.device = pd;
  585. pch->chan.chan_id = i;
  586. pch->dmac = pdmac;
  587. /* Add the channel to the DMAC list */
  588. pd->chancnt++;
  589. list_add_tail(&pch->chan.device_node, &pd->channels);
  590. }
  591. pd->dev = &adev->dev;
  592. pd->device_alloc_chan_resources = pl330_alloc_chan_resources;
  593. pd->device_free_chan_resources = pl330_free_chan_resources;
  594. pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy;
  595. pd->device_tx_status = pl330_tx_status;
  596. pd->device_prep_slave_sg = pl330_prep_slave_sg;
  597. pd->device_control = pl330_control;
  598. pd->device_issue_pending = pl330_issue_pending;
  599. ret = dma_async_device_register(pd);
  600. if (ret) {
  601. dev_err(&adev->dev, "unable to register DMAC\n");
  602. goto probe_err4;
  603. }
  604. amba_set_drvdata(adev, pdmac);
  605. dev_info(&adev->dev,
  606. "Loaded driver for PL330 DMAC-%d\n", adev->periphid);
  607. dev_info(&adev->dev,
  608. "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n",
  609. pi->pcfg.data_buf_dep,
  610. pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan,
  611. pi->pcfg.num_peri, pi->pcfg.num_events);
  612. return 0;
  613. probe_err4:
  614. pl330_del(pi);
  615. probe_err3:
  616. free_irq(irq, pi);
  617. probe_err2:
  618. iounmap(pi->base);
  619. probe_err1:
  620. release_mem_region(res->start, resource_size(res));
  621. kfree(pdmac);
  622. return ret;
  623. }
  624. static int __devexit pl330_remove(struct amba_device *adev)
  625. {
  626. struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev);
  627. struct dma_pl330_chan *pch, *_p;
  628. struct pl330_info *pi;
  629. struct resource *res;
  630. int irq;
  631. if (!pdmac)
  632. return 0;
  633. amba_set_drvdata(adev, NULL);
  634. /* Idle the DMAC */
  635. list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels,
  636. chan.device_node) {
  637. /* Remove the channel */
  638. list_del(&pch->chan.device_node);
  639. /* Flush the channel */
  640. pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0);
  641. pl330_free_chan_resources(&pch->chan);
  642. }
  643. pi = &pdmac->pif;
  644. pl330_del(pi);
  645. irq = adev->irq[0];
  646. free_irq(irq, pi);
  647. iounmap(pi->base);
  648. res = &adev->res;
  649. release_mem_region(res->start, resource_size(res));
  650. kfree(pdmac);
  651. return 0;
  652. }
  653. static struct amba_id pl330_ids[] = {
  654. {
  655. .id = 0x00041330,
  656. .mask = 0x000fffff,
  657. },
  658. { 0, 0 },
  659. };
  660. static struct amba_driver pl330_driver = {
  661. .drv = {
  662. .owner = THIS_MODULE,
  663. .name = "dma-pl330",
  664. },
  665. .id_table = pl330_ids,
  666. .probe = pl330_probe,
  667. .remove = pl330_remove,
  668. };
  669. static int __init pl330_init(void)
  670. {
  671. return amba_driver_register(&pl330_driver);
  672. }
  673. module_init(pl330_init);
  674. static void __exit pl330_exit(void)
  675. {
  676. amba_driver_unregister(&pl330_driver);
  677. return;
  678. }
  679. module_exit(pl330_exit);
  680. MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
  681. MODULE_DESCRIPTION("API Driver for PL330 DMAC");
  682. MODULE_LICENSE("GPL");