fsldma.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. /*
  2. * Freescale MPC85xx, MPC83xx DMA Engine support
  3. *
  4. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author:
  7. * Zhang Wei <wei.zhang@freescale.com>, Jul 2007
  8. * Ebony Zhu <ebony.zhu@freescale.com>, May 2007
  9. *
  10. * Description:
  11. * DMA engine driver for Freescale MPC8540 DMA controller, which is
  12. * also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
  13. * The support for MPC8349 DMA contorller is also added.
  14. *
  15. * This driver instructs the DMA controller to issue the PCI Read Multiple
  16. * command for PCI read operations, instead of using the default PCI Read Line
  17. * command. Please be aware that this setting may result in read pre-fetching
  18. * on some platforms.
  19. *
  20. * This is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/dmapool.h>
  34. #include <linux/of_platform.h>
  35. #include <asm/fsldma.h>
  36. #include "fsldma.h"
  37. static void dma_init(struct fsldma_chan *chan)
  38. {
  39. /* Reset the channel */
  40. DMA_OUT(chan, &chan->regs->mr, 0, 32);
  41. switch (chan->feature & FSL_DMA_IP_MASK) {
  42. case FSL_DMA_IP_85XX:
  43. /* Set the channel to below modes:
  44. * EIE - Error interrupt enable
  45. * EOSIE - End of segments interrupt enable (basic mode)
  46. * EOLNIE - End of links interrupt enable
  47. */
  48. DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EIE
  49. | FSL_DMA_MR_EOLNIE | FSL_DMA_MR_EOSIE, 32);
  50. break;
  51. case FSL_DMA_IP_83XX:
  52. /* Set the channel to below modes:
  53. * EOTIE - End-of-transfer interrupt enable
  54. * PRC_RM - PCI read multiple
  55. */
  56. DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EOTIE
  57. | FSL_DMA_MR_PRC_RM, 32);
  58. break;
  59. }
  60. }
  61. static void set_sr(struct fsldma_chan *chan, u32 val)
  62. {
  63. DMA_OUT(chan, &chan->regs->sr, val, 32);
  64. }
  65. static u32 get_sr(struct fsldma_chan *chan)
  66. {
  67. return DMA_IN(chan, &chan->regs->sr, 32);
  68. }
  69. static void set_desc_cnt(struct fsldma_chan *chan,
  70. struct fsl_dma_ld_hw *hw, u32 count)
  71. {
  72. hw->count = CPU_TO_DMA(chan, count, 32);
  73. }
  74. static void set_desc_src(struct fsldma_chan *chan,
  75. struct fsl_dma_ld_hw *hw, dma_addr_t src)
  76. {
  77. u64 snoop_bits;
  78. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  79. ? ((u64)FSL_DMA_SATR_SREADTYPE_SNOOP_READ << 32) : 0;
  80. hw->src_addr = CPU_TO_DMA(chan, snoop_bits | src, 64);
  81. }
  82. static void set_desc_dst(struct fsldma_chan *chan,
  83. struct fsl_dma_ld_hw *hw, dma_addr_t dst)
  84. {
  85. u64 snoop_bits;
  86. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  87. ? ((u64)FSL_DMA_DATR_DWRITETYPE_SNOOP_WRITE << 32) : 0;
  88. hw->dst_addr = CPU_TO_DMA(chan, snoop_bits | dst, 64);
  89. }
  90. static void set_desc_next(struct fsldma_chan *chan,
  91. struct fsl_dma_ld_hw *hw, dma_addr_t next)
  92. {
  93. u64 snoop_bits;
  94. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  95. ? FSL_DMA_SNEN : 0;
  96. hw->next_ln_addr = CPU_TO_DMA(chan, snoop_bits | next, 64);
  97. }
  98. static void set_cdar(struct fsldma_chan *chan, dma_addr_t addr)
  99. {
  100. DMA_OUT(chan, &chan->regs->cdar, addr | FSL_DMA_SNEN, 64);
  101. }
  102. static dma_addr_t get_cdar(struct fsldma_chan *chan)
  103. {
  104. return DMA_IN(chan, &chan->regs->cdar, 64) & ~FSL_DMA_SNEN;
  105. }
  106. static dma_addr_t get_ndar(struct fsldma_chan *chan)
  107. {
  108. return DMA_IN(chan, &chan->regs->ndar, 64);
  109. }
  110. static u32 get_bcr(struct fsldma_chan *chan)
  111. {
  112. return DMA_IN(chan, &chan->regs->bcr, 32);
  113. }
  114. static int dma_is_idle(struct fsldma_chan *chan)
  115. {
  116. u32 sr = get_sr(chan);
  117. return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
  118. }
  119. static void dma_start(struct fsldma_chan *chan)
  120. {
  121. u32 mode;
  122. mode = DMA_IN(chan, &chan->regs->mr, 32);
  123. if ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX) {
  124. if (chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
  125. DMA_OUT(chan, &chan->regs->bcr, 0, 32);
  126. mode |= FSL_DMA_MR_EMP_EN;
  127. } else {
  128. mode &= ~FSL_DMA_MR_EMP_EN;
  129. }
  130. }
  131. if (chan->feature & FSL_DMA_CHAN_START_EXT)
  132. mode |= FSL_DMA_MR_EMS_EN;
  133. else
  134. mode |= FSL_DMA_MR_CS;
  135. DMA_OUT(chan, &chan->regs->mr, mode, 32);
  136. }
  137. static void dma_halt(struct fsldma_chan *chan)
  138. {
  139. u32 mode;
  140. int i;
  141. mode = DMA_IN(chan, &chan->regs->mr, 32);
  142. mode |= FSL_DMA_MR_CA;
  143. DMA_OUT(chan, &chan->regs->mr, mode, 32);
  144. mode &= ~(FSL_DMA_MR_CS | FSL_DMA_MR_EMS_EN | FSL_DMA_MR_CA);
  145. DMA_OUT(chan, &chan->regs->mr, mode, 32);
  146. for (i = 0; i < 100; i++) {
  147. if (dma_is_idle(chan))
  148. return;
  149. udelay(10);
  150. }
  151. if (!dma_is_idle(chan))
  152. dev_err(chan->dev, "DMA halt timeout!\n");
  153. }
  154. static void set_ld_eol(struct fsldma_chan *chan,
  155. struct fsl_desc_sw *desc)
  156. {
  157. u64 snoop_bits;
  158. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  159. ? FSL_DMA_SNEN : 0;
  160. desc->hw.next_ln_addr = CPU_TO_DMA(chan,
  161. DMA_TO_CPU(chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
  162. | snoop_bits, 64);
  163. }
  164. /**
  165. * fsl_chan_set_src_loop_size - Set source address hold transfer size
  166. * @chan : Freescale DMA channel
  167. * @size : Address loop size, 0 for disable loop
  168. *
  169. * The set source address hold transfer size. The source
  170. * address hold or loop transfer size is when the DMA transfer
  171. * data from source address (SA), if the loop size is 4, the DMA will
  172. * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
  173. * SA + 1 ... and so on.
  174. */
  175. static void fsl_chan_set_src_loop_size(struct fsldma_chan *chan, int size)
  176. {
  177. u32 mode;
  178. mode = DMA_IN(chan, &chan->regs->mr, 32);
  179. switch (size) {
  180. case 0:
  181. mode &= ~FSL_DMA_MR_SAHE;
  182. break;
  183. case 1:
  184. case 2:
  185. case 4:
  186. case 8:
  187. mode |= FSL_DMA_MR_SAHE | (__ilog2(size) << 14);
  188. break;
  189. }
  190. DMA_OUT(chan, &chan->regs->mr, mode, 32);
  191. }
  192. /**
  193. * fsl_chan_set_dst_loop_size - Set destination address hold transfer size
  194. * @chan : Freescale DMA channel
  195. * @size : Address loop size, 0 for disable loop
  196. *
  197. * The set destination address hold transfer size. The destination
  198. * address hold or loop transfer size is when the DMA transfer
  199. * data to destination address (TA), if the loop size is 4, the DMA will
  200. * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
  201. * TA + 1 ... and so on.
  202. */
  203. static void fsl_chan_set_dst_loop_size(struct fsldma_chan *chan, int size)
  204. {
  205. u32 mode;
  206. mode = DMA_IN(chan, &chan->regs->mr, 32);
  207. switch (size) {
  208. case 0:
  209. mode &= ~FSL_DMA_MR_DAHE;
  210. break;
  211. case 1:
  212. case 2:
  213. case 4:
  214. case 8:
  215. mode |= FSL_DMA_MR_DAHE | (__ilog2(size) << 16);
  216. break;
  217. }
  218. DMA_OUT(chan, &chan->regs->mr, mode, 32);
  219. }
  220. /**
  221. * fsl_chan_set_request_count - Set DMA Request Count for external control
  222. * @chan : Freescale DMA channel
  223. * @size : Number of bytes to transfer in a single request
  224. *
  225. * The Freescale DMA channel can be controlled by the external signal DREQ#.
  226. * The DMA request count is how many bytes are allowed to transfer before
  227. * pausing the channel, after which a new assertion of DREQ# resumes channel
  228. * operation.
  229. *
  230. * A size of 0 disables external pause control. The maximum size is 1024.
  231. */
  232. static void fsl_chan_set_request_count(struct fsldma_chan *chan, int size)
  233. {
  234. u32 mode;
  235. BUG_ON(size > 1024);
  236. mode = DMA_IN(chan, &chan->regs->mr, 32);
  237. mode |= (__ilog2(size) << 24) & 0x0f000000;
  238. DMA_OUT(chan, &chan->regs->mr, mode, 32);
  239. }
  240. /**
  241. * fsl_chan_toggle_ext_pause - Toggle channel external pause status
  242. * @chan : Freescale DMA channel
  243. * @enable : 0 is disabled, 1 is enabled.
  244. *
  245. * The Freescale DMA channel can be controlled by the external signal DREQ#.
  246. * The DMA Request Count feature should be used in addition to this feature
  247. * to set the number of bytes to transfer before pausing the channel.
  248. */
  249. static void fsl_chan_toggle_ext_pause(struct fsldma_chan *chan, int enable)
  250. {
  251. if (enable)
  252. chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
  253. else
  254. chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
  255. }
  256. /**
  257. * fsl_chan_toggle_ext_start - Toggle channel external start status
  258. * @chan : Freescale DMA channel
  259. * @enable : 0 is disabled, 1 is enabled.
  260. *
  261. * If enable the external start, the channel can be started by an
  262. * external DMA start pin. So the dma_start() does not start the
  263. * transfer immediately. The DMA channel will wait for the
  264. * control pin asserted.
  265. */
  266. static void fsl_chan_toggle_ext_start(struct fsldma_chan *chan, int enable)
  267. {
  268. if (enable)
  269. chan->feature |= FSL_DMA_CHAN_START_EXT;
  270. else
  271. chan->feature &= ~FSL_DMA_CHAN_START_EXT;
  272. }
  273. static void append_ld_queue(struct fsldma_chan *chan,
  274. struct fsl_desc_sw *desc)
  275. {
  276. struct fsl_desc_sw *tail = to_fsl_desc(chan->ld_pending.prev);
  277. if (list_empty(&chan->ld_pending))
  278. goto out_splice;
  279. /*
  280. * Add the hardware descriptor to the chain of hardware descriptors
  281. * that already exists in memory.
  282. *
  283. * This will un-set the EOL bit of the existing transaction, and the
  284. * last link in this transaction will become the EOL descriptor.
  285. */
  286. set_desc_next(chan, &tail->hw, desc->async_tx.phys);
  287. /*
  288. * Add the software descriptor and all children to the list
  289. * of pending transactions
  290. */
  291. out_splice:
  292. list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
  293. }
  294. static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  295. {
  296. struct fsldma_chan *chan = to_fsl_chan(tx->chan);
  297. struct fsl_desc_sw *desc = tx_to_fsl_desc(tx);
  298. struct fsl_desc_sw *child;
  299. unsigned long flags;
  300. dma_cookie_t cookie;
  301. spin_lock_irqsave(&chan->desc_lock, flags);
  302. /*
  303. * assign cookies to all of the software descriptors
  304. * that make up this transaction
  305. */
  306. cookie = chan->common.cookie;
  307. list_for_each_entry(child, &desc->tx_list, node) {
  308. cookie++;
  309. if (cookie < 0)
  310. cookie = 1;
  311. child->async_tx.cookie = cookie;
  312. }
  313. chan->common.cookie = cookie;
  314. /* put this transaction onto the tail of the pending queue */
  315. append_ld_queue(chan, desc);
  316. spin_unlock_irqrestore(&chan->desc_lock, flags);
  317. return cookie;
  318. }
  319. /**
  320. * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
  321. * @chan : Freescale DMA channel
  322. *
  323. * Return - The descriptor allocated. NULL for failed.
  324. */
  325. static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
  326. struct fsldma_chan *chan)
  327. {
  328. struct fsl_desc_sw *desc;
  329. dma_addr_t pdesc;
  330. desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  331. if (!desc) {
  332. dev_dbg(chan->dev, "out of memory for link desc\n");
  333. return NULL;
  334. }
  335. memset(desc, 0, sizeof(*desc));
  336. INIT_LIST_HEAD(&desc->tx_list);
  337. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  338. desc->async_tx.tx_submit = fsl_dma_tx_submit;
  339. desc->async_tx.phys = pdesc;
  340. return desc;
  341. }
  342. /**
  343. * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
  344. * @chan : Freescale DMA channel
  345. *
  346. * This function will create a dma pool for descriptor allocation.
  347. *
  348. * Return - The number of descriptors allocated.
  349. */
  350. static int fsl_dma_alloc_chan_resources(struct dma_chan *dchan)
  351. {
  352. struct fsldma_chan *chan = to_fsl_chan(dchan);
  353. /* Has this channel already been allocated? */
  354. if (chan->desc_pool)
  355. return 1;
  356. /*
  357. * We need the descriptor to be aligned to 32bytes
  358. * for meeting FSL DMA specification requirement.
  359. */
  360. chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
  361. chan->dev,
  362. sizeof(struct fsl_desc_sw),
  363. __alignof__(struct fsl_desc_sw), 0);
  364. if (!chan->desc_pool) {
  365. dev_err(chan->dev, "unable to allocate channel %d "
  366. "descriptor pool\n", chan->id);
  367. return -ENOMEM;
  368. }
  369. /* there is at least one descriptor free to be allocated */
  370. return 1;
  371. }
  372. /**
  373. * fsldma_free_desc_list - Free all descriptors in a queue
  374. * @chan: Freescae DMA channel
  375. * @list: the list to free
  376. *
  377. * LOCKING: must hold chan->desc_lock
  378. */
  379. static void fsldma_free_desc_list(struct fsldma_chan *chan,
  380. struct list_head *list)
  381. {
  382. struct fsl_desc_sw *desc, *_desc;
  383. list_for_each_entry_safe(desc, _desc, list, node) {
  384. list_del(&desc->node);
  385. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  386. }
  387. }
  388. static void fsldma_free_desc_list_reverse(struct fsldma_chan *chan,
  389. struct list_head *list)
  390. {
  391. struct fsl_desc_sw *desc, *_desc;
  392. list_for_each_entry_safe_reverse(desc, _desc, list, node) {
  393. list_del(&desc->node);
  394. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  395. }
  396. }
  397. /**
  398. * fsl_dma_free_chan_resources - Free all resources of the channel.
  399. * @chan : Freescale DMA channel
  400. */
  401. static void fsl_dma_free_chan_resources(struct dma_chan *dchan)
  402. {
  403. struct fsldma_chan *chan = to_fsl_chan(dchan);
  404. unsigned long flags;
  405. dev_dbg(chan->dev, "Free all channel resources.\n");
  406. spin_lock_irqsave(&chan->desc_lock, flags);
  407. fsldma_free_desc_list(chan, &chan->ld_pending);
  408. fsldma_free_desc_list(chan, &chan->ld_running);
  409. spin_unlock_irqrestore(&chan->desc_lock, flags);
  410. dma_pool_destroy(chan->desc_pool);
  411. chan->desc_pool = NULL;
  412. }
  413. static struct dma_async_tx_descriptor *
  414. fsl_dma_prep_interrupt(struct dma_chan *dchan, unsigned long flags)
  415. {
  416. struct fsldma_chan *chan;
  417. struct fsl_desc_sw *new;
  418. if (!dchan)
  419. return NULL;
  420. chan = to_fsl_chan(dchan);
  421. new = fsl_dma_alloc_descriptor(chan);
  422. if (!new) {
  423. dev_err(chan->dev, "No free memory for link descriptor\n");
  424. return NULL;
  425. }
  426. new->async_tx.cookie = -EBUSY;
  427. new->async_tx.flags = flags;
  428. /* Insert the link descriptor to the LD ring */
  429. list_add_tail(&new->node, &new->tx_list);
  430. /* Set End-of-link to the last link descriptor of new list*/
  431. set_ld_eol(chan, new);
  432. return &new->async_tx;
  433. }
  434. static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
  435. struct dma_chan *dchan, dma_addr_t dma_dst, dma_addr_t dma_src,
  436. size_t len, unsigned long flags)
  437. {
  438. struct fsldma_chan *chan;
  439. struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
  440. size_t copy;
  441. if (!dchan)
  442. return NULL;
  443. if (!len)
  444. return NULL;
  445. chan = to_fsl_chan(dchan);
  446. do {
  447. /* Allocate the link descriptor from DMA pool */
  448. new = fsl_dma_alloc_descriptor(chan);
  449. if (!new) {
  450. dev_err(chan->dev,
  451. "No free memory for link descriptor\n");
  452. goto fail;
  453. }
  454. #ifdef FSL_DMA_LD_DEBUG
  455. dev_dbg(chan->dev, "new link desc alloc %p\n", new);
  456. #endif
  457. copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
  458. set_desc_cnt(chan, &new->hw, copy);
  459. set_desc_src(chan, &new->hw, dma_src);
  460. set_desc_dst(chan, &new->hw, dma_dst);
  461. if (!first)
  462. first = new;
  463. else
  464. set_desc_next(chan, &prev->hw, new->async_tx.phys);
  465. new->async_tx.cookie = 0;
  466. async_tx_ack(&new->async_tx);
  467. prev = new;
  468. len -= copy;
  469. dma_src += copy;
  470. dma_dst += copy;
  471. /* Insert the link descriptor to the LD ring */
  472. list_add_tail(&new->node, &first->tx_list);
  473. } while (len);
  474. new->async_tx.flags = flags; /* client is in control of this ack */
  475. new->async_tx.cookie = -EBUSY;
  476. /* Set End-of-link to the last link descriptor of new list*/
  477. set_ld_eol(chan, new);
  478. return &first->async_tx;
  479. fail:
  480. if (!first)
  481. return NULL;
  482. fsldma_free_desc_list_reverse(chan, &first->tx_list);
  483. return NULL;
  484. }
  485. /**
  486. * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  487. * @chan: DMA channel
  488. * @sgl: scatterlist to transfer to/from
  489. * @sg_len: number of entries in @scatterlist
  490. * @direction: DMA direction
  491. * @flags: DMAEngine flags
  492. *
  493. * Prepare a set of descriptors for a DMA_SLAVE transaction. Following the
  494. * DMA_SLAVE API, this gets the device-specific information from the
  495. * chan->private variable.
  496. */
  497. static struct dma_async_tx_descriptor *fsl_dma_prep_slave_sg(
  498. struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
  499. enum dma_data_direction direction, unsigned long flags)
  500. {
  501. struct fsldma_chan *chan;
  502. struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  503. struct fsl_dma_slave *slave;
  504. size_t copy;
  505. int i;
  506. struct scatterlist *sg;
  507. size_t sg_used;
  508. size_t hw_used;
  509. struct fsl_dma_hw_addr *hw;
  510. dma_addr_t dma_dst, dma_src;
  511. if (!dchan)
  512. return NULL;
  513. if (!dchan->private)
  514. return NULL;
  515. chan = to_fsl_chan(dchan);
  516. slave = dchan->private;
  517. if (list_empty(&slave->addresses))
  518. return NULL;
  519. hw = list_first_entry(&slave->addresses, struct fsl_dma_hw_addr, entry);
  520. hw_used = 0;
  521. /*
  522. * Build the hardware transaction to copy from the scatterlist to
  523. * the hardware, or from the hardware to the scatterlist
  524. *
  525. * If you are copying from the hardware to the scatterlist and it
  526. * takes two hardware entries to fill an entire page, then both
  527. * hardware entries will be coalesced into the same page
  528. *
  529. * If you are copying from the scatterlist to the hardware and a
  530. * single page can fill two hardware entries, then the data will
  531. * be read out of the page into the first hardware entry, and so on
  532. */
  533. for_each_sg(sgl, sg, sg_len, i) {
  534. sg_used = 0;
  535. /* Loop until the entire scatterlist entry is used */
  536. while (sg_used < sg_dma_len(sg)) {
  537. /*
  538. * If we've used up the current hardware address/length
  539. * pair, we need to load a new one
  540. *
  541. * This is done in a while loop so that descriptors with
  542. * length == 0 will be skipped
  543. */
  544. while (hw_used >= hw->length) {
  545. /*
  546. * If the current hardware entry is the last
  547. * entry in the list, we're finished
  548. */
  549. if (list_is_last(&hw->entry, &slave->addresses))
  550. goto finished;
  551. /* Get the next hardware address/length pair */
  552. hw = list_entry(hw->entry.next,
  553. struct fsl_dma_hw_addr, entry);
  554. hw_used = 0;
  555. }
  556. /* Allocate the link descriptor from DMA pool */
  557. new = fsl_dma_alloc_descriptor(chan);
  558. if (!new) {
  559. dev_err(chan->dev, "No free memory for "
  560. "link descriptor\n");
  561. goto fail;
  562. }
  563. #ifdef FSL_DMA_LD_DEBUG
  564. dev_dbg(chan->dev, "new link desc alloc %p\n", new);
  565. #endif
  566. /*
  567. * Calculate the maximum number of bytes to transfer,
  568. * making sure it is less than the DMA controller limit
  569. */
  570. copy = min_t(size_t, sg_dma_len(sg) - sg_used,
  571. hw->length - hw_used);
  572. copy = min_t(size_t, copy, FSL_DMA_BCR_MAX_CNT);
  573. /*
  574. * DMA_FROM_DEVICE
  575. * from the hardware to the scatterlist
  576. *
  577. * DMA_TO_DEVICE
  578. * from the scatterlist to the hardware
  579. */
  580. if (direction == DMA_FROM_DEVICE) {
  581. dma_src = hw->address + hw_used;
  582. dma_dst = sg_dma_address(sg) + sg_used;
  583. } else {
  584. dma_src = sg_dma_address(sg) + sg_used;
  585. dma_dst = hw->address + hw_used;
  586. }
  587. /* Fill in the descriptor */
  588. set_desc_cnt(chan, &new->hw, copy);
  589. set_desc_src(chan, &new->hw, dma_src);
  590. set_desc_dst(chan, &new->hw, dma_dst);
  591. /*
  592. * If this is not the first descriptor, chain the
  593. * current descriptor after the previous descriptor
  594. */
  595. if (!first) {
  596. first = new;
  597. } else {
  598. set_desc_next(chan, &prev->hw,
  599. new->async_tx.phys);
  600. }
  601. new->async_tx.cookie = 0;
  602. async_tx_ack(&new->async_tx);
  603. prev = new;
  604. sg_used += copy;
  605. hw_used += copy;
  606. /* Insert the link descriptor into the LD ring */
  607. list_add_tail(&new->node, &first->tx_list);
  608. }
  609. }
  610. finished:
  611. /* All of the hardware address/length pairs had length == 0 */
  612. if (!first || !new)
  613. return NULL;
  614. new->async_tx.flags = flags;
  615. new->async_tx.cookie = -EBUSY;
  616. /* Set End-of-link to the last link descriptor of new list */
  617. set_ld_eol(chan, new);
  618. /* Enable extra controller features */
  619. if (chan->set_src_loop_size)
  620. chan->set_src_loop_size(chan, slave->src_loop_size);
  621. if (chan->set_dst_loop_size)
  622. chan->set_dst_loop_size(chan, slave->dst_loop_size);
  623. if (chan->toggle_ext_start)
  624. chan->toggle_ext_start(chan, slave->external_start);
  625. if (chan->toggle_ext_pause)
  626. chan->toggle_ext_pause(chan, slave->external_pause);
  627. if (chan->set_request_count)
  628. chan->set_request_count(chan, slave->request_count);
  629. return &first->async_tx;
  630. fail:
  631. /* If first was not set, then we failed to allocate the very first
  632. * descriptor, and we're done */
  633. if (!first)
  634. return NULL;
  635. /*
  636. * First is set, so all of the descriptors we allocated have been added
  637. * to first->tx_list, INCLUDING "first" itself. Therefore we
  638. * must traverse the list backwards freeing each descriptor in turn
  639. *
  640. * We're re-using variables for the loop, oh well
  641. */
  642. fsldma_free_desc_list_reverse(chan, &first->tx_list);
  643. return NULL;
  644. }
  645. static void fsl_dma_device_terminate_all(struct dma_chan *dchan)
  646. {
  647. struct fsldma_chan *chan;
  648. unsigned long flags;
  649. if (!dchan)
  650. return;
  651. chan = to_fsl_chan(dchan);
  652. /* Halt the DMA engine */
  653. dma_halt(chan);
  654. spin_lock_irqsave(&chan->desc_lock, flags);
  655. /* Remove and free all of the descriptors in the LD queue */
  656. fsldma_free_desc_list(chan, &chan->ld_pending);
  657. fsldma_free_desc_list(chan, &chan->ld_running);
  658. spin_unlock_irqrestore(&chan->desc_lock, flags);
  659. }
  660. /**
  661. * fsl_dma_update_completed_cookie - Update the completed cookie.
  662. * @chan : Freescale DMA channel
  663. *
  664. * CONTEXT: hardirq
  665. */
  666. static void fsl_dma_update_completed_cookie(struct fsldma_chan *chan)
  667. {
  668. struct fsl_desc_sw *desc;
  669. unsigned long flags;
  670. dma_cookie_t cookie;
  671. spin_lock_irqsave(&chan->desc_lock, flags);
  672. if (list_empty(&chan->ld_running)) {
  673. dev_dbg(chan->dev, "no running descriptors\n");
  674. goto out_unlock;
  675. }
  676. /* Get the last descriptor, update the cookie to that */
  677. desc = to_fsl_desc(chan->ld_running.prev);
  678. if (dma_is_idle(chan))
  679. cookie = desc->async_tx.cookie;
  680. else {
  681. cookie = desc->async_tx.cookie - 1;
  682. if (unlikely(cookie < DMA_MIN_COOKIE))
  683. cookie = DMA_MAX_COOKIE;
  684. }
  685. chan->completed_cookie = cookie;
  686. out_unlock:
  687. spin_unlock_irqrestore(&chan->desc_lock, flags);
  688. }
  689. /**
  690. * fsldma_desc_status - Check the status of a descriptor
  691. * @chan: Freescale DMA channel
  692. * @desc: DMA SW descriptor
  693. *
  694. * This function will return the status of the given descriptor
  695. */
  696. static enum dma_status fsldma_desc_status(struct fsldma_chan *chan,
  697. struct fsl_desc_sw *desc)
  698. {
  699. return dma_async_is_complete(desc->async_tx.cookie,
  700. chan->completed_cookie,
  701. chan->common.cookie);
  702. }
  703. /**
  704. * fsl_chan_ld_cleanup - Clean up link descriptors
  705. * @chan : Freescale DMA channel
  706. *
  707. * This function clean up the ld_queue of DMA channel.
  708. */
  709. static void fsl_chan_ld_cleanup(struct fsldma_chan *chan)
  710. {
  711. struct fsl_desc_sw *desc, *_desc;
  712. unsigned long flags;
  713. spin_lock_irqsave(&chan->desc_lock, flags);
  714. dev_dbg(chan->dev, "chan completed_cookie = %d\n", chan->completed_cookie);
  715. list_for_each_entry_safe(desc, _desc, &chan->ld_running, node) {
  716. dma_async_tx_callback callback;
  717. void *callback_param;
  718. if (fsldma_desc_status(chan, desc) == DMA_IN_PROGRESS)
  719. break;
  720. /* Remove from the list of running transactions */
  721. list_del(&desc->node);
  722. /* Run the link descriptor callback function */
  723. callback = desc->async_tx.callback;
  724. callback_param = desc->async_tx.callback_param;
  725. if (callback) {
  726. spin_unlock_irqrestore(&chan->desc_lock, flags);
  727. dev_dbg(chan->dev, "LD %p callback\n", desc);
  728. callback(callback_param);
  729. spin_lock_irqsave(&chan->desc_lock, flags);
  730. }
  731. /* Run any dependencies, then free the descriptor */
  732. dma_run_dependencies(&desc->async_tx);
  733. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  734. }
  735. spin_unlock_irqrestore(&chan->desc_lock, flags);
  736. }
  737. /**
  738. * fsl_chan_xfer_ld_queue - transfer any pending transactions
  739. * @chan : Freescale DMA channel
  740. *
  741. * This will make sure that any pending transactions will be run.
  742. * If the DMA controller is idle, it will be started. Otherwise,
  743. * the DMA controller's interrupt handler will start any pending
  744. * transactions when it becomes idle.
  745. */
  746. static void fsl_chan_xfer_ld_queue(struct fsldma_chan *chan)
  747. {
  748. struct fsl_desc_sw *desc;
  749. unsigned long flags;
  750. spin_lock_irqsave(&chan->desc_lock, flags);
  751. /*
  752. * If the list of pending descriptors is empty, then we
  753. * don't need to do any work at all
  754. */
  755. if (list_empty(&chan->ld_pending)) {
  756. dev_dbg(chan->dev, "no pending LDs\n");
  757. goto out_unlock;
  758. }
  759. /*
  760. * The DMA controller is not idle, which means the interrupt
  761. * handler will start any queued transactions when it runs
  762. * at the end of the current transaction
  763. */
  764. if (!dma_is_idle(chan)) {
  765. dev_dbg(chan->dev, "DMA controller still busy\n");
  766. goto out_unlock;
  767. }
  768. /*
  769. * TODO:
  770. * make sure the dma_halt() function really un-wedges the
  771. * controller as much as possible
  772. */
  773. dma_halt(chan);
  774. /*
  775. * If there are some link descriptors which have not been
  776. * transferred, we need to start the controller
  777. */
  778. /*
  779. * Move all elements from the queue of pending transactions
  780. * onto the list of running transactions
  781. */
  782. desc = list_first_entry(&chan->ld_pending, struct fsl_desc_sw, node);
  783. list_splice_tail_init(&chan->ld_pending, &chan->ld_running);
  784. /*
  785. * Program the descriptor's address into the DMA controller,
  786. * then start the DMA transaction
  787. */
  788. set_cdar(chan, desc->async_tx.phys);
  789. dma_start(chan);
  790. out_unlock:
  791. spin_unlock_irqrestore(&chan->desc_lock, flags);
  792. }
  793. /**
  794. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  795. * @chan : Freescale DMA channel
  796. */
  797. static void fsl_dma_memcpy_issue_pending(struct dma_chan *dchan)
  798. {
  799. struct fsldma_chan *chan = to_fsl_chan(dchan);
  800. fsl_chan_xfer_ld_queue(chan);
  801. }
  802. /**
  803. * fsl_dma_is_complete - Determine the DMA status
  804. * @chan : Freescale DMA channel
  805. */
  806. static enum dma_status fsl_dma_is_complete(struct dma_chan *dchan,
  807. dma_cookie_t cookie,
  808. dma_cookie_t *done,
  809. dma_cookie_t *used)
  810. {
  811. struct fsldma_chan *chan = to_fsl_chan(dchan);
  812. dma_cookie_t last_used;
  813. dma_cookie_t last_complete;
  814. fsl_chan_ld_cleanup(chan);
  815. last_used = dchan->cookie;
  816. last_complete = chan->completed_cookie;
  817. if (done)
  818. *done = last_complete;
  819. if (used)
  820. *used = last_used;
  821. return dma_async_is_complete(cookie, last_complete, last_used);
  822. }
  823. /*----------------------------------------------------------------------------*/
  824. /* Interrupt Handling */
  825. /*----------------------------------------------------------------------------*/
  826. static irqreturn_t fsldma_chan_irq(int irq, void *data)
  827. {
  828. struct fsldma_chan *chan = data;
  829. int update_cookie = 0;
  830. int xfer_ld_q = 0;
  831. u32 stat;
  832. /* save and clear the status register */
  833. stat = get_sr(chan);
  834. set_sr(chan, stat);
  835. dev_dbg(chan->dev, "irq: channel %d, stat = 0x%x\n", chan->id, stat);
  836. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  837. if (!stat)
  838. return IRQ_NONE;
  839. if (stat & FSL_DMA_SR_TE)
  840. dev_err(chan->dev, "Transfer Error!\n");
  841. /*
  842. * Programming Error
  843. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  844. * triger a PE interrupt.
  845. */
  846. if (stat & FSL_DMA_SR_PE) {
  847. dev_dbg(chan->dev, "irq: Programming Error INT\n");
  848. if (get_bcr(chan) == 0) {
  849. /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
  850. * Now, update the completed cookie, and continue the
  851. * next uncompleted transfer.
  852. */
  853. update_cookie = 1;
  854. xfer_ld_q = 1;
  855. }
  856. stat &= ~FSL_DMA_SR_PE;
  857. }
  858. /*
  859. * If the link descriptor segment transfer finishes,
  860. * we will recycle the used descriptor.
  861. */
  862. if (stat & FSL_DMA_SR_EOSI) {
  863. dev_dbg(chan->dev, "irq: End-of-segments INT\n");
  864. dev_dbg(chan->dev, "irq: clndar 0x%llx, nlndar 0x%llx\n",
  865. (unsigned long long)get_cdar(chan),
  866. (unsigned long long)get_ndar(chan));
  867. stat &= ~FSL_DMA_SR_EOSI;
  868. update_cookie = 1;
  869. }
  870. /*
  871. * For MPC8349, EOCDI event need to update cookie
  872. * and start the next transfer if it exist.
  873. */
  874. if (stat & FSL_DMA_SR_EOCDI) {
  875. dev_dbg(chan->dev, "irq: End-of-Chain link INT\n");
  876. stat &= ~FSL_DMA_SR_EOCDI;
  877. update_cookie = 1;
  878. xfer_ld_q = 1;
  879. }
  880. /*
  881. * If it current transfer is the end-of-transfer,
  882. * we should clear the Channel Start bit for
  883. * prepare next transfer.
  884. */
  885. if (stat & FSL_DMA_SR_EOLNI) {
  886. dev_dbg(chan->dev, "irq: End-of-link INT\n");
  887. stat &= ~FSL_DMA_SR_EOLNI;
  888. xfer_ld_q = 1;
  889. }
  890. if (update_cookie)
  891. fsl_dma_update_completed_cookie(chan);
  892. if (xfer_ld_q)
  893. fsl_chan_xfer_ld_queue(chan);
  894. if (stat)
  895. dev_dbg(chan->dev, "irq: unhandled sr 0x%02x\n", stat);
  896. dev_dbg(chan->dev, "irq: Exit\n");
  897. tasklet_schedule(&chan->tasklet);
  898. return IRQ_HANDLED;
  899. }
  900. static void dma_do_tasklet(unsigned long data)
  901. {
  902. struct fsldma_chan *chan = (struct fsldma_chan *)data;
  903. fsl_chan_ld_cleanup(chan);
  904. }
  905. static irqreturn_t fsldma_ctrl_irq(int irq, void *data)
  906. {
  907. struct fsldma_device *fdev = data;
  908. struct fsldma_chan *chan;
  909. unsigned int handled = 0;
  910. u32 gsr, mask;
  911. int i;
  912. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->regs)
  913. : in_le32(fdev->regs);
  914. mask = 0xff000000;
  915. dev_dbg(fdev->dev, "IRQ: gsr 0x%.8x\n", gsr);
  916. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  917. chan = fdev->chan[i];
  918. if (!chan)
  919. continue;
  920. if (gsr & mask) {
  921. dev_dbg(fdev->dev, "IRQ: chan %d\n", chan->id);
  922. fsldma_chan_irq(irq, chan);
  923. handled++;
  924. }
  925. gsr &= ~mask;
  926. mask >>= 8;
  927. }
  928. return IRQ_RETVAL(handled);
  929. }
  930. static void fsldma_free_irqs(struct fsldma_device *fdev)
  931. {
  932. struct fsldma_chan *chan;
  933. int i;
  934. if (fdev->irq != NO_IRQ) {
  935. dev_dbg(fdev->dev, "free per-controller IRQ\n");
  936. free_irq(fdev->irq, fdev);
  937. return;
  938. }
  939. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  940. chan = fdev->chan[i];
  941. if (chan && chan->irq != NO_IRQ) {
  942. dev_dbg(fdev->dev, "free channel %d IRQ\n", chan->id);
  943. free_irq(chan->irq, chan);
  944. }
  945. }
  946. }
  947. static int fsldma_request_irqs(struct fsldma_device *fdev)
  948. {
  949. struct fsldma_chan *chan;
  950. int ret;
  951. int i;
  952. /* if we have a per-controller IRQ, use that */
  953. if (fdev->irq != NO_IRQ) {
  954. dev_dbg(fdev->dev, "request per-controller IRQ\n");
  955. ret = request_irq(fdev->irq, fsldma_ctrl_irq, IRQF_SHARED,
  956. "fsldma-controller", fdev);
  957. return ret;
  958. }
  959. /* no per-controller IRQ, use the per-channel IRQs */
  960. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  961. chan = fdev->chan[i];
  962. if (!chan)
  963. continue;
  964. if (chan->irq == NO_IRQ) {
  965. dev_err(fdev->dev, "no interrupts property defined for "
  966. "DMA channel %d. Please fix your "
  967. "device tree\n", chan->id);
  968. ret = -ENODEV;
  969. goto out_unwind;
  970. }
  971. dev_dbg(fdev->dev, "request channel %d IRQ\n", chan->id);
  972. ret = request_irq(chan->irq, fsldma_chan_irq, IRQF_SHARED,
  973. "fsldma-chan", chan);
  974. if (ret) {
  975. dev_err(fdev->dev, "unable to request IRQ for DMA "
  976. "channel %d\n", chan->id);
  977. goto out_unwind;
  978. }
  979. }
  980. return 0;
  981. out_unwind:
  982. for (/* none */; i >= 0; i--) {
  983. chan = fdev->chan[i];
  984. if (!chan)
  985. continue;
  986. if (chan->irq == NO_IRQ)
  987. continue;
  988. free_irq(chan->irq, chan);
  989. }
  990. return ret;
  991. }
  992. /*----------------------------------------------------------------------------*/
  993. /* OpenFirmware Subsystem */
  994. /*----------------------------------------------------------------------------*/
  995. static int __devinit fsl_dma_chan_probe(struct fsldma_device *fdev,
  996. struct device_node *node, u32 feature, const char *compatible)
  997. {
  998. struct fsldma_chan *chan;
  999. struct resource res;
  1000. int err;
  1001. /* alloc channel */
  1002. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  1003. if (!chan) {
  1004. dev_err(fdev->dev, "no free memory for DMA channels!\n");
  1005. err = -ENOMEM;
  1006. goto out_return;
  1007. }
  1008. /* ioremap registers for use */
  1009. chan->regs = of_iomap(node, 0);
  1010. if (!chan->regs) {
  1011. dev_err(fdev->dev, "unable to ioremap registers\n");
  1012. err = -ENOMEM;
  1013. goto out_free_chan;
  1014. }
  1015. err = of_address_to_resource(node, 0, &res);
  1016. if (err) {
  1017. dev_err(fdev->dev, "unable to find 'reg' property\n");
  1018. goto out_iounmap_regs;
  1019. }
  1020. chan->feature = feature;
  1021. if (!fdev->feature)
  1022. fdev->feature = chan->feature;
  1023. /*
  1024. * If the DMA device's feature is different than the feature
  1025. * of its channels, report the bug
  1026. */
  1027. WARN_ON(fdev->feature != chan->feature);
  1028. chan->dev = fdev->dev;
  1029. chan->id = ((res.start - 0x100) & 0xfff) >> 7;
  1030. if (chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
  1031. dev_err(fdev->dev, "too many channels for device\n");
  1032. err = -EINVAL;
  1033. goto out_iounmap_regs;
  1034. }
  1035. fdev->chan[chan->id] = chan;
  1036. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  1037. /* Initialize the channel */
  1038. dma_init(chan);
  1039. /* Clear cdar registers */
  1040. set_cdar(chan, 0);
  1041. switch (chan->feature & FSL_DMA_IP_MASK) {
  1042. case FSL_DMA_IP_85XX:
  1043. chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  1044. case FSL_DMA_IP_83XX:
  1045. chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  1046. chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  1047. chan->set_dst_loop_size = fsl_chan_set_dst_loop_size;
  1048. chan->set_request_count = fsl_chan_set_request_count;
  1049. }
  1050. spin_lock_init(&chan->desc_lock);
  1051. INIT_LIST_HEAD(&chan->ld_pending);
  1052. INIT_LIST_HEAD(&chan->ld_running);
  1053. chan->common.device = &fdev->common;
  1054. /* find the IRQ line, if it exists in the device tree */
  1055. chan->irq = irq_of_parse_and_map(node, 0);
  1056. /* Add the channel to DMA device channel list */
  1057. list_add_tail(&chan->common.device_node, &fdev->common.channels);
  1058. fdev->common.chancnt++;
  1059. dev_info(fdev->dev, "#%d (%s), irq %d\n", chan->id, compatible,
  1060. chan->irq != NO_IRQ ? chan->irq : fdev->irq);
  1061. return 0;
  1062. out_iounmap_regs:
  1063. iounmap(chan->regs);
  1064. out_free_chan:
  1065. kfree(chan);
  1066. out_return:
  1067. return err;
  1068. }
  1069. static void fsl_dma_chan_remove(struct fsldma_chan *chan)
  1070. {
  1071. irq_dispose_mapping(chan->irq);
  1072. list_del(&chan->common.device_node);
  1073. iounmap(chan->regs);
  1074. kfree(chan);
  1075. }
  1076. static int __devinit fsldma_of_probe(struct of_device *op,
  1077. const struct of_device_id *match)
  1078. {
  1079. struct fsldma_device *fdev;
  1080. struct device_node *child;
  1081. int err;
  1082. fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
  1083. if (!fdev) {
  1084. dev_err(&op->dev, "No enough memory for 'priv'\n");
  1085. err = -ENOMEM;
  1086. goto out_return;
  1087. }
  1088. fdev->dev = &op->dev;
  1089. INIT_LIST_HEAD(&fdev->common.channels);
  1090. /* ioremap the registers for use */
  1091. fdev->regs = of_iomap(op->node, 0);
  1092. if (!fdev->regs) {
  1093. dev_err(&op->dev, "unable to ioremap registers\n");
  1094. err = -ENOMEM;
  1095. goto out_free_fdev;
  1096. }
  1097. /* map the channel IRQ if it exists, but don't hookup the handler yet */
  1098. fdev->irq = irq_of_parse_and_map(op->node, 0);
  1099. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  1100. dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
  1101. dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
  1102. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  1103. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  1104. fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
  1105. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  1106. fdev->common.device_is_tx_complete = fsl_dma_is_complete;
  1107. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  1108. fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
  1109. fdev->common.device_terminate_all = fsl_dma_device_terminate_all;
  1110. fdev->common.dev = &op->dev;
  1111. dev_set_drvdata(&op->dev, fdev);
  1112. /*
  1113. * We cannot use of_platform_bus_probe() because there is no
  1114. * of_platform_bus_remove(). Instead, we manually instantiate every DMA
  1115. * channel object.
  1116. */
  1117. for_each_child_of_node(op->node, child) {
  1118. if (of_device_is_compatible(child, "fsl,eloplus-dma-channel")) {
  1119. fsl_dma_chan_probe(fdev, child,
  1120. FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
  1121. "fsl,eloplus-dma-channel");
  1122. }
  1123. if (of_device_is_compatible(child, "fsl,elo-dma-channel")) {
  1124. fsl_dma_chan_probe(fdev, child,
  1125. FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
  1126. "fsl,elo-dma-channel");
  1127. }
  1128. }
  1129. /*
  1130. * Hookup the IRQ handler(s)
  1131. *
  1132. * If we have a per-controller interrupt, we prefer that to the
  1133. * per-channel interrupts to reduce the number of shared interrupt
  1134. * handlers on the same IRQ line
  1135. */
  1136. err = fsldma_request_irqs(fdev);
  1137. if (err) {
  1138. dev_err(fdev->dev, "unable to request IRQs\n");
  1139. goto out_free_fdev;
  1140. }
  1141. dma_async_device_register(&fdev->common);
  1142. return 0;
  1143. out_free_fdev:
  1144. irq_dispose_mapping(fdev->irq);
  1145. kfree(fdev);
  1146. out_return:
  1147. return err;
  1148. }
  1149. static int fsldma_of_remove(struct of_device *op)
  1150. {
  1151. struct fsldma_device *fdev;
  1152. unsigned int i;
  1153. fdev = dev_get_drvdata(&op->dev);
  1154. dma_async_device_unregister(&fdev->common);
  1155. fsldma_free_irqs(fdev);
  1156. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1157. if (fdev->chan[i])
  1158. fsl_dma_chan_remove(fdev->chan[i]);
  1159. }
  1160. iounmap(fdev->regs);
  1161. dev_set_drvdata(&op->dev, NULL);
  1162. kfree(fdev);
  1163. return 0;
  1164. }
  1165. static const struct of_device_id fsldma_of_ids[] = {
  1166. { .compatible = "fsl,eloplus-dma", },
  1167. { .compatible = "fsl,elo-dma", },
  1168. {}
  1169. };
  1170. static struct of_platform_driver fsldma_of_driver = {
  1171. .name = "fsl-elo-dma",
  1172. .match_table = fsldma_of_ids,
  1173. .probe = fsldma_of_probe,
  1174. .remove = fsldma_of_remove,
  1175. };
  1176. /*----------------------------------------------------------------------------*/
  1177. /* Module Init / Exit */
  1178. /*----------------------------------------------------------------------------*/
  1179. static __init int fsldma_init(void)
  1180. {
  1181. int ret;
  1182. pr_info("Freescale Elo / Elo Plus DMA driver\n");
  1183. ret = of_register_platform_driver(&fsldma_of_driver);
  1184. if (ret)
  1185. pr_err("fsldma: failed to register platform driver\n");
  1186. return ret;
  1187. }
  1188. static void __exit fsldma_exit(void)
  1189. {
  1190. of_unregister_platform_driver(&fsldma_of_driver);
  1191. }
  1192. subsys_initcall(fsldma_init);
  1193. module_exit(fsldma_exit);
  1194. MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
  1195. MODULE_LICENSE("GPL");