pl330.c 23 KB

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