fsldma.c 36 KB

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