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