fsldma.c 35 KB

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