mpc512x_dma.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
  3. * Copyright (C) Semihalf 2009
  4. * Copyright (C) Ilya Yanok, Emcraft Systems 2010
  5. * Copyright (C) Alexander Popov, Promcontroller 2014
  6. * Copyright (C) Mario Six, Guntermann & Drunck GmbH, 2016
  7. *
  8. * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
  9. * (defines, structures and comments) was taken from MPC5121 DMA driver
  10. * written by Hongjun Chen <hong-jun.chen@freescale.com>.
  11. *
  12. * Approved as OSADL project by a majority of OSADL members and funded
  13. * by OSADL membership fees in 2009; for details see www.osadl.org.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 of the License, or (at your option)
  18. * any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but WITHOUT
  21. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  22. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  23. * more details.
  24. *
  25. * The full GNU General Public License is included in this distribution in the
  26. * file called COPYING.
  27. */
  28. /*
  29. * MPC512x and MPC8308 DMA driver. It supports memory to memory data transfers
  30. * (tested using dmatest module) and data transfers between memory and
  31. * peripheral I/O memory by means of slave scatter/gather with these
  32. * limitations:
  33. * - chunked transfers (described by s/g lists with more than one item) are
  34. * refused as long as proper support for scatter/gather is missing
  35. * - transfers on MPC8308 always start from software as this SoC does not have
  36. * external request lines for peripheral flow control
  37. * - memory <-> I/O memory transfer chunks of sizes of 1, 2, 4, 16 (for
  38. * MPC512x), and 32 bytes are supported, and, consequently, source
  39. * addresses and destination addresses must be aligned accordingly;
  40. * furthermore, for MPC512x SoCs, the transfer size must be aligned on
  41. * (chunk size * maxburst)
  42. */
  43. #include <linux/module.h>
  44. #include <linux/dmaengine.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/io.h>
  48. #include <linux/slab.h>
  49. #include <linux/of_address.h>
  50. #include <linux/of_device.h>
  51. #include <linux/of_irq.h>
  52. #include <linux/of_dma.h>
  53. #include <linux/of_platform.h>
  54. #include <linux/random.h>
  55. #include "dmaengine.h"
  56. /* Number of DMA Transfer descriptors allocated per channel */
  57. #define MPC_DMA_DESCRIPTORS 64
  58. /* Macro definitions */
  59. #define MPC_DMA_TCD_OFFSET 0x1000
  60. /*
  61. * Maximum channel counts for individual hardware variants
  62. * and the maximum channel count over all supported controllers,
  63. * used for data structure size
  64. */
  65. #define MPC8308_DMACHAN_MAX 16
  66. #define MPC512x_DMACHAN_MAX 64
  67. #define MPC_DMA_CHANNELS 64
  68. /* Arbitration mode of group and channel */
  69. #define MPC_DMA_DMACR_EDCG (1 << 31)
  70. #define MPC_DMA_DMACR_ERGA (1 << 3)
  71. #define MPC_DMA_DMACR_ERCA (1 << 2)
  72. /* Error codes */
  73. #define MPC_DMA_DMAES_VLD (1 << 31)
  74. #define MPC_DMA_DMAES_GPE (1 << 15)
  75. #define MPC_DMA_DMAES_CPE (1 << 14)
  76. #define MPC_DMA_DMAES_ERRCHN(err) \
  77. (((err) >> 8) & 0x3f)
  78. #define MPC_DMA_DMAES_SAE (1 << 7)
  79. #define MPC_DMA_DMAES_SOE (1 << 6)
  80. #define MPC_DMA_DMAES_DAE (1 << 5)
  81. #define MPC_DMA_DMAES_DOE (1 << 4)
  82. #define MPC_DMA_DMAES_NCE (1 << 3)
  83. #define MPC_DMA_DMAES_SGE (1 << 2)
  84. #define MPC_DMA_DMAES_SBE (1 << 1)
  85. #define MPC_DMA_DMAES_DBE (1 << 0)
  86. #define MPC_DMA_DMAGPOR_SNOOP_ENABLE (1 << 6)
  87. #define MPC_DMA_TSIZE_1 0x00
  88. #define MPC_DMA_TSIZE_2 0x01
  89. #define MPC_DMA_TSIZE_4 0x02
  90. #define MPC_DMA_TSIZE_16 0x04
  91. #define MPC_DMA_TSIZE_32 0x05
  92. /* MPC5121 DMA engine registers */
  93. struct __attribute__ ((__packed__)) mpc_dma_regs {
  94. /* 0x00 */
  95. u32 dmacr; /* DMA control register */
  96. u32 dmaes; /* DMA error status */
  97. /* 0x08 */
  98. u32 dmaerqh; /* DMA enable request high(channels 63~32) */
  99. u32 dmaerql; /* DMA enable request low(channels 31~0) */
  100. u32 dmaeeih; /* DMA enable error interrupt high(ch63~32) */
  101. u32 dmaeeil; /* DMA enable error interrupt low(ch31~0) */
  102. /* 0x18 */
  103. u8 dmaserq; /* DMA set enable request */
  104. u8 dmacerq; /* DMA clear enable request */
  105. u8 dmaseei; /* DMA set enable error interrupt */
  106. u8 dmaceei; /* DMA clear enable error interrupt */
  107. /* 0x1c */
  108. u8 dmacint; /* DMA clear interrupt request */
  109. u8 dmacerr; /* DMA clear error */
  110. u8 dmassrt; /* DMA set start bit */
  111. u8 dmacdne; /* DMA clear DONE status bit */
  112. /* 0x20 */
  113. u32 dmainth; /* DMA interrupt request high(ch63~32) */
  114. u32 dmaintl; /* DMA interrupt request low(ch31~0) */
  115. u32 dmaerrh; /* DMA error high(ch63~32) */
  116. u32 dmaerrl; /* DMA error low(ch31~0) */
  117. /* 0x30 */
  118. u32 dmahrsh; /* DMA hw request status high(ch63~32) */
  119. u32 dmahrsl; /* DMA hardware request status low(ch31~0) */
  120. union {
  121. u32 dmaihsa; /* DMA interrupt high select AXE(ch63~32) */
  122. u32 dmagpor; /* (General purpose register on MPC8308) */
  123. };
  124. u32 dmailsa; /* DMA interrupt low select AXE(ch31~0) */
  125. /* 0x40 ~ 0xff */
  126. u32 reserve0[48]; /* Reserved */
  127. /* 0x100 */
  128. u8 dchpri[MPC_DMA_CHANNELS];
  129. /* DMA channels(0~63) priority */
  130. };
  131. struct __attribute__ ((__packed__)) mpc_dma_tcd {
  132. /* 0x00 */
  133. u32 saddr; /* Source address */
  134. u32 smod:5; /* Source address modulo */
  135. u32 ssize:3; /* Source data transfer size */
  136. u32 dmod:5; /* Destination address modulo */
  137. u32 dsize:3; /* Destination data transfer size */
  138. u32 soff:16; /* Signed source address offset */
  139. /* 0x08 */
  140. u32 nbytes; /* Inner "minor" byte count */
  141. u32 slast; /* Last source address adjustment */
  142. u32 daddr; /* Destination address */
  143. /* 0x14 */
  144. u32 citer_elink:1; /* Enable channel-to-channel linking on
  145. * minor loop complete
  146. */
  147. u32 citer_linkch:6; /* Link channel for minor loop complete */
  148. u32 citer:9; /* Current "major" iteration count */
  149. u32 doff:16; /* Signed destination address offset */
  150. /* 0x18 */
  151. u32 dlast_sga; /* Last Destination address adjustment/scatter
  152. * gather address
  153. */
  154. /* 0x1c */
  155. u32 biter_elink:1; /* Enable channel-to-channel linking on major
  156. * loop complete
  157. */
  158. u32 biter_linkch:6;
  159. u32 biter:9; /* Beginning "major" iteration count */
  160. u32 bwc:2; /* Bandwidth control */
  161. u32 major_linkch:6; /* Link channel number */
  162. u32 done:1; /* Channel done */
  163. u32 active:1; /* Channel active */
  164. u32 major_elink:1; /* Enable channel-to-channel linking on major
  165. * loop complete
  166. */
  167. u32 e_sg:1; /* Enable scatter/gather processing */
  168. u32 d_req:1; /* Disable request */
  169. u32 int_half:1; /* Enable an interrupt when major counter is
  170. * half complete
  171. */
  172. u32 int_maj:1; /* Enable an interrupt when major iteration
  173. * count completes
  174. */
  175. u32 start:1; /* Channel start */
  176. };
  177. struct mpc_dma_desc {
  178. struct dma_async_tx_descriptor desc;
  179. struct mpc_dma_tcd *tcd;
  180. dma_addr_t tcd_paddr;
  181. int error;
  182. struct list_head node;
  183. int will_access_peripheral;
  184. };
  185. struct mpc_dma_chan {
  186. struct dma_chan chan;
  187. struct list_head free;
  188. struct list_head prepared;
  189. struct list_head queued;
  190. struct list_head active;
  191. struct list_head completed;
  192. struct mpc_dma_tcd *tcd;
  193. dma_addr_t tcd_paddr;
  194. /* Settings for access to peripheral FIFO */
  195. dma_addr_t src_per_paddr;
  196. u32 src_tcd_nunits;
  197. u8 swidth;
  198. dma_addr_t dst_per_paddr;
  199. u32 dst_tcd_nunits;
  200. u8 dwidth;
  201. /* Lock for this structure */
  202. spinlock_t lock;
  203. };
  204. struct mpc_dma {
  205. struct dma_device dma;
  206. struct tasklet_struct tasklet;
  207. struct mpc_dma_chan channels[MPC_DMA_CHANNELS];
  208. struct mpc_dma_regs __iomem *regs;
  209. struct mpc_dma_tcd __iomem *tcd;
  210. int irq;
  211. int irq2;
  212. uint error_status;
  213. int is_mpc8308;
  214. /* Lock for error_status field in this structure */
  215. spinlock_t error_status_lock;
  216. };
  217. #define DRV_NAME "mpc512x_dma"
  218. /* Convert struct dma_chan to struct mpc_dma_chan */
  219. static inline struct mpc_dma_chan *dma_chan_to_mpc_dma_chan(struct dma_chan *c)
  220. {
  221. return container_of(c, struct mpc_dma_chan, chan);
  222. }
  223. /* Convert struct dma_chan to struct mpc_dma */
  224. static inline struct mpc_dma *dma_chan_to_mpc_dma(struct dma_chan *c)
  225. {
  226. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(c);
  227. return container_of(mchan, struct mpc_dma, channels[c->chan_id]);
  228. }
  229. /*
  230. * Execute all queued DMA descriptors.
  231. *
  232. * Following requirements must be met while calling mpc_dma_execute():
  233. * a) mchan->lock is acquired,
  234. * b) mchan->active list is empty,
  235. * c) mchan->queued list contains at least one entry.
  236. */
  237. static void mpc_dma_execute(struct mpc_dma_chan *mchan)
  238. {
  239. struct mpc_dma *mdma = dma_chan_to_mpc_dma(&mchan->chan);
  240. struct mpc_dma_desc *first = NULL;
  241. struct mpc_dma_desc *prev = NULL;
  242. struct mpc_dma_desc *mdesc;
  243. int cid = mchan->chan.chan_id;
  244. while (!list_empty(&mchan->queued)) {
  245. mdesc = list_first_entry(&mchan->queued,
  246. struct mpc_dma_desc, node);
  247. /*
  248. * Grab either several mem-to-mem transfer descriptors
  249. * or one peripheral transfer descriptor,
  250. * don't mix mem-to-mem and peripheral transfer descriptors
  251. * within the same 'active' list.
  252. */
  253. if (mdesc->will_access_peripheral) {
  254. if (list_empty(&mchan->active))
  255. list_move_tail(&mdesc->node, &mchan->active);
  256. break;
  257. } else {
  258. list_move_tail(&mdesc->node, &mchan->active);
  259. }
  260. }
  261. /* Chain descriptors into one transaction */
  262. list_for_each_entry(mdesc, &mchan->active, node) {
  263. if (!first)
  264. first = mdesc;
  265. if (!prev) {
  266. prev = mdesc;
  267. continue;
  268. }
  269. prev->tcd->dlast_sga = mdesc->tcd_paddr;
  270. prev->tcd->e_sg = 1;
  271. mdesc->tcd->start = 1;
  272. prev = mdesc;
  273. }
  274. prev->tcd->int_maj = 1;
  275. /* Send first descriptor in chain into hardware */
  276. memcpy_toio(&mdma->tcd[cid], first->tcd, sizeof(struct mpc_dma_tcd));
  277. if (first != prev)
  278. mdma->tcd[cid].e_sg = 1;
  279. if (mdma->is_mpc8308) {
  280. /* MPC8308, no request lines, software initiated start */
  281. out_8(&mdma->regs->dmassrt, cid);
  282. } else if (first->will_access_peripheral) {
  283. /* Peripherals involved, start by external request signal */
  284. out_8(&mdma->regs->dmaserq, cid);
  285. } else {
  286. /* Memory to memory transfer, software initiated start */
  287. out_8(&mdma->regs->dmassrt, cid);
  288. }
  289. }
  290. /* Handle interrupt on one half of DMA controller (32 channels) */
  291. static void mpc_dma_irq_process(struct mpc_dma *mdma, u32 is, u32 es, int off)
  292. {
  293. struct mpc_dma_chan *mchan;
  294. struct mpc_dma_desc *mdesc;
  295. u32 status = is | es;
  296. int ch;
  297. while ((ch = fls(status) - 1) >= 0) {
  298. status &= ~(1 << ch);
  299. mchan = &mdma->channels[ch + off];
  300. spin_lock(&mchan->lock);
  301. out_8(&mdma->regs->dmacint, ch + off);
  302. out_8(&mdma->regs->dmacerr, ch + off);
  303. /* Check error status */
  304. if (es & (1 << ch))
  305. list_for_each_entry(mdesc, &mchan->active, node)
  306. mdesc->error = -EIO;
  307. /* Execute queued descriptors */
  308. list_splice_tail_init(&mchan->active, &mchan->completed);
  309. if (!list_empty(&mchan->queued))
  310. mpc_dma_execute(mchan);
  311. spin_unlock(&mchan->lock);
  312. }
  313. }
  314. /* Interrupt handler */
  315. static irqreturn_t mpc_dma_irq(int irq, void *data)
  316. {
  317. struct mpc_dma *mdma = data;
  318. uint es;
  319. /* Save error status register */
  320. es = in_be32(&mdma->regs->dmaes);
  321. spin_lock(&mdma->error_status_lock);
  322. if ((es & MPC_DMA_DMAES_VLD) && mdma->error_status == 0)
  323. mdma->error_status = es;
  324. spin_unlock(&mdma->error_status_lock);
  325. /* Handle interrupt on each channel */
  326. if (mdma->dma.chancnt > 32) {
  327. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmainth),
  328. in_be32(&mdma->regs->dmaerrh), 32);
  329. }
  330. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmaintl),
  331. in_be32(&mdma->regs->dmaerrl), 0);
  332. /* Schedule tasklet */
  333. tasklet_schedule(&mdma->tasklet);
  334. return IRQ_HANDLED;
  335. }
  336. /* process completed descriptors */
  337. static void mpc_dma_process_completed(struct mpc_dma *mdma)
  338. {
  339. dma_cookie_t last_cookie = 0;
  340. struct mpc_dma_chan *mchan;
  341. struct mpc_dma_desc *mdesc;
  342. struct dma_async_tx_descriptor *desc;
  343. unsigned long flags;
  344. LIST_HEAD(list);
  345. int i;
  346. for (i = 0; i < mdma->dma.chancnt; i++) {
  347. mchan = &mdma->channels[i];
  348. /* Get all completed descriptors */
  349. spin_lock_irqsave(&mchan->lock, flags);
  350. if (!list_empty(&mchan->completed))
  351. list_splice_tail_init(&mchan->completed, &list);
  352. spin_unlock_irqrestore(&mchan->lock, flags);
  353. if (list_empty(&list))
  354. continue;
  355. /* Execute callbacks and run dependencies */
  356. list_for_each_entry(mdesc, &list, node) {
  357. desc = &mdesc->desc;
  358. dmaengine_desc_get_callback_invoke(desc, NULL);
  359. last_cookie = desc->cookie;
  360. dma_run_dependencies(desc);
  361. }
  362. /* Free descriptors */
  363. spin_lock_irqsave(&mchan->lock, flags);
  364. list_splice_tail_init(&list, &mchan->free);
  365. mchan->chan.completed_cookie = last_cookie;
  366. spin_unlock_irqrestore(&mchan->lock, flags);
  367. }
  368. }
  369. /* DMA Tasklet */
  370. static void mpc_dma_tasklet(unsigned long data)
  371. {
  372. struct mpc_dma *mdma = (void *)data;
  373. unsigned long flags;
  374. uint es;
  375. spin_lock_irqsave(&mdma->error_status_lock, flags);
  376. es = mdma->error_status;
  377. mdma->error_status = 0;
  378. spin_unlock_irqrestore(&mdma->error_status_lock, flags);
  379. /* Print nice error report */
  380. if (es) {
  381. dev_err(mdma->dma.dev,
  382. "Hardware reported following error(s) on channel %u:\n",
  383. MPC_DMA_DMAES_ERRCHN(es));
  384. if (es & MPC_DMA_DMAES_GPE)
  385. dev_err(mdma->dma.dev, "- Group Priority Error\n");
  386. if (es & MPC_DMA_DMAES_CPE)
  387. dev_err(mdma->dma.dev, "- Channel Priority Error\n");
  388. if (es & MPC_DMA_DMAES_SAE)
  389. dev_err(mdma->dma.dev, "- Source Address Error\n");
  390. if (es & MPC_DMA_DMAES_SOE)
  391. dev_err(mdma->dma.dev, "- Source Offset Configuration Error\n");
  392. if (es & MPC_DMA_DMAES_DAE)
  393. dev_err(mdma->dma.dev, "- Destination Address Error\n");
  394. if (es & MPC_DMA_DMAES_DOE)
  395. dev_err(mdma->dma.dev, "- Destination Offset Configuration Error\n");
  396. if (es & MPC_DMA_DMAES_NCE)
  397. dev_err(mdma->dma.dev, "- NBytes/Citter Configuration Error\n");
  398. if (es & MPC_DMA_DMAES_SGE)
  399. dev_err(mdma->dma.dev, "- Scatter/Gather Configuration Error\n");
  400. if (es & MPC_DMA_DMAES_SBE)
  401. dev_err(mdma->dma.dev, "- Source Bus Error\n");
  402. if (es & MPC_DMA_DMAES_DBE)
  403. dev_err(mdma->dma.dev, "- Destination Bus Error\n");
  404. }
  405. mpc_dma_process_completed(mdma);
  406. }
  407. /* Submit descriptor to hardware */
  408. static dma_cookie_t mpc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  409. {
  410. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(txd->chan);
  411. struct mpc_dma_desc *mdesc;
  412. unsigned long flags;
  413. dma_cookie_t cookie;
  414. mdesc = container_of(txd, struct mpc_dma_desc, desc);
  415. spin_lock_irqsave(&mchan->lock, flags);
  416. /* Move descriptor to queue */
  417. list_move_tail(&mdesc->node, &mchan->queued);
  418. /* If channel is idle, execute all queued descriptors */
  419. if (list_empty(&mchan->active))
  420. mpc_dma_execute(mchan);
  421. /* Update cookie */
  422. cookie = dma_cookie_assign(txd);
  423. spin_unlock_irqrestore(&mchan->lock, flags);
  424. return cookie;
  425. }
  426. /* Alloc channel resources */
  427. static int mpc_dma_alloc_chan_resources(struct dma_chan *chan)
  428. {
  429. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  430. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  431. struct mpc_dma_desc *mdesc;
  432. struct mpc_dma_tcd *tcd;
  433. dma_addr_t tcd_paddr;
  434. unsigned long flags;
  435. LIST_HEAD(descs);
  436. int i;
  437. /* Alloc DMA memory for Transfer Control Descriptors */
  438. tcd = dma_alloc_coherent(mdma->dma.dev,
  439. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  440. &tcd_paddr, GFP_KERNEL);
  441. if (!tcd)
  442. return -ENOMEM;
  443. /* Alloc descriptors for this channel */
  444. for (i = 0; i < MPC_DMA_DESCRIPTORS; i++) {
  445. mdesc = kzalloc(sizeof(struct mpc_dma_desc), GFP_KERNEL);
  446. if (!mdesc) {
  447. dev_notice(mdma->dma.dev,
  448. "Memory allocation error. Allocated only %u descriptors\n", i);
  449. break;
  450. }
  451. dma_async_tx_descriptor_init(&mdesc->desc, chan);
  452. mdesc->desc.flags = DMA_CTRL_ACK;
  453. mdesc->desc.tx_submit = mpc_dma_tx_submit;
  454. mdesc->tcd = &tcd[i];
  455. mdesc->tcd_paddr = tcd_paddr + (i * sizeof(struct mpc_dma_tcd));
  456. list_add_tail(&mdesc->node, &descs);
  457. }
  458. /* Return error only if no descriptors were allocated */
  459. if (i == 0) {
  460. dma_free_coherent(mdma->dma.dev,
  461. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  462. tcd, tcd_paddr);
  463. return -ENOMEM;
  464. }
  465. spin_lock_irqsave(&mchan->lock, flags);
  466. mchan->tcd = tcd;
  467. mchan->tcd_paddr = tcd_paddr;
  468. list_splice_tail_init(&descs, &mchan->free);
  469. spin_unlock_irqrestore(&mchan->lock, flags);
  470. /* Enable Error Interrupt */
  471. out_8(&mdma->regs->dmaseei, chan->chan_id);
  472. return 0;
  473. }
  474. /* Free channel resources */
  475. static void mpc_dma_free_chan_resources(struct dma_chan *chan)
  476. {
  477. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  478. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  479. struct mpc_dma_desc *mdesc, *tmp;
  480. struct mpc_dma_tcd *tcd;
  481. dma_addr_t tcd_paddr;
  482. unsigned long flags;
  483. LIST_HEAD(descs);
  484. spin_lock_irqsave(&mchan->lock, flags);
  485. /* Channel must be idle */
  486. BUG_ON(!list_empty(&mchan->prepared));
  487. BUG_ON(!list_empty(&mchan->queued));
  488. BUG_ON(!list_empty(&mchan->active));
  489. BUG_ON(!list_empty(&mchan->completed));
  490. /* Move data */
  491. list_splice_tail_init(&mchan->free, &descs);
  492. tcd = mchan->tcd;
  493. tcd_paddr = mchan->tcd_paddr;
  494. spin_unlock_irqrestore(&mchan->lock, flags);
  495. /* Free DMA memory used by descriptors */
  496. dma_free_coherent(mdma->dma.dev,
  497. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  498. tcd, tcd_paddr);
  499. /* Free descriptors */
  500. list_for_each_entry_safe(mdesc, tmp, &descs, node)
  501. kfree(mdesc);
  502. /* Disable Error Interrupt */
  503. out_8(&mdma->regs->dmaceei, chan->chan_id);
  504. }
  505. /* Send all pending descriptor to hardware */
  506. static void mpc_dma_issue_pending(struct dma_chan *chan)
  507. {
  508. /*
  509. * We are posting descriptors to the hardware as soon as
  510. * they are ready, so this function does nothing.
  511. */
  512. }
  513. /* Check request completion status */
  514. static enum dma_status
  515. mpc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  516. struct dma_tx_state *txstate)
  517. {
  518. return dma_cookie_status(chan, cookie, txstate);
  519. }
  520. /* Prepare descriptor for memory to memory copy */
  521. static struct dma_async_tx_descriptor *
  522. mpc_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  523. size_t len, unsigned long flags)
  524. {
  525. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  526. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  527. struct mpc_dma_desc *mdesc = NULL;
  528. struct mpc_dma_tcd *tcd;
  529. unsigned long iflags;
  530. /* Get free descriptor */
  531. spin_lock_irqsave(&mchan->lock, iflags);
  532. if (!list_empty(&mchan->free)) {
  533. mdesc = list_first_entry(&mchan->free, struct mpc_dma_desc,
  534. node);
  535. list_del(&mdesc->node);
  536. }
  537. spin_unlock_irqrestore(&mchan->lock, iflags);
  538. if (!mdesc) {
  539. /* try to free completed descriptors */
  540. mpc_dma_process_completed(mdma);
  541. return NULL;
  542. }
  543. mdesc->error = 0;
  544. mdesc->will_access_peripheral = 0;
  545. tcd = mdesc->tcd;
  546. /* Prepare Transfer Control Descriptor for this transaction */
  547. memset(tcd, 0, sizeof(struct mpc_dma_tcd));
  548. if (IS_ALIGNED(src | dst | len, 32)) {
  549. tcd->ssize = MPC_DMA_TSIZE_32;
  550. tcd->dsize = MPC_DMA_TSIZE_32;
  551. tcd->soff = 32;
  552. tcd->doff = 32;
  553. } else if (!mdma->is_mpc8308 && IS_ALIGNED(src | dst | len, 16)) {
  554. /* MPC8308 doesn't support 16 byte transfers */
  555. tcd->ssize = MPC_DMA_TSIZE_16;
  556. tcd->dsize = MPC_DMA_TSIZE_16;
  557. tcd->soff = 16;
  558. tcd->doff = 16;
  559. } else if (IS_ALIGNED(src | dst | len, 4)) {
  560. tcd->ssize = MPC_DMA_TSIZE_4;
  561. tcd->dsize = MPC_DMA_TSIZE_4;
  562. tcd->soff = 4;
  563. tcd->doff = 4;
  564. } else if (IS_ALIGNED(src | dst | len, 2)) {
  565. tcd->ssize = MPC_DMA_TSIZE_2;
  566. tcd->dsize = MPC_DMA_TSIZE_2;
  567. tcd->soff = 2;
  568. tcd->doff = 2;
  569. } else {
  570. tcd->ssize = MPC_DMA_TSIZE_1;
  571. tcd->dsize = MPC_DMA_TSIZE_1;
  572. tcd->soff = 1;
  573. tcd->doff = 1;
  574. }
  575. tcd->saddr = src;
  576. tcd->daddr = dst;
  577. tcd->nbytes = len;
  578. tcd->biter = 1;
  579. tcd->citer = 1;
  580. /* Place descriptor in prepared list */
  581. spin_lock_irqsave(&mchan->lock, iflags);
  582. list_add_tail(&mdesc->node, &mchan->prepared);
  583. spin_unlock_irqrestore(&mchan->lock, iflags);
  584. return &mdesc->desc;
  585. }
  586. inline u8 buswidth_to_dmatsize(u8 buswidth)
  587. {
  588. u8 res;
  589. for (res = 0; buswidth > 1; buswidth /= 2)
  590. res++;
  591. return res;
  592. }
  593. static struct dma_async_tx_descriptor *
  594. mpc_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  595. unsigned int sg_len, enum dma_transfer_direction direction,
  596. unsigned long flags, void *context)
  597. {
  598. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  599. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  600. struct mpc_dma_desc *mdesc = NULL;
  601. dma_addr_t per_paddr;
  602. u32 tcd_nunits;
  603. struct mpc_dma_tcd *tcd;
  604. unsigned long iflags;
  605. struct scatterlist *sg;
  606. size_t len;
  607. int iter, i;
  608. /* Currently there is no proper support for scatter/gather */
  609. if (sg_len != 1)
  610. return NULL;
  611. if (!is_slave_direction(direction))
  612. return NULL;
  613. for_each_sg(sgl, sg, sg_len, i) {
  614. spin_lock_irqsave(&mchan->lock, iflags);
  615. mdesc = list_first_entry(&mchan->free,
  616. struct mpc_dma_desc, node);
  617. if (!mdesc) {
  618. spin_unlock_irqrestore(&mchan->lock, iflags);
  619. /* Try to free completed descriptors */
  620. mpc_dma_process_completed(mdma);
  621. return NULL;
  622. }
  623. list_del(&mdesc->node);
  624. if (direction == DMA_DEV_TO_MEM) {
  625. per_paddr = mchan->src_per_paddr;
  626. tcd_nunits = mchan->src_tcd_nunits;
  627. } else {
  628. per_paddr = mchan->dst_per_paddr;
  629. tcd_nunits = mchan->dst_tcd_nunits;
  630. }
  631. spin_unlock_irqrestore(&mchan->lock, iflags);
  632. if (per_paddr == 0 || tcd_nunits == 0)
  633. goto err_prep;
  634. mdesc->error = 0;
  635. mdesc->will_access_peripheral = 1;
  636. /* Prepare Transfer Control Descriptor for this transaction */
  637. tcd = mdesc->tcd;
  638. memset(tcd, 0, sizeof(struct mpc_dma_tcd));
  639. if (direction == DMA_DEV_TO_MEM) {
  640. tcd->saddr = per_paddr;
  641. tcd->daddr = sg_dma_address(sg);
  642. if (!IS_ALIGNED(sg_dma_address(sg), mchan->dwidth))
  643. goto err_prep;
  644. tcd->soff = 0;
  645. tcd->doff = mchan->dwidth;
  646. } else {
  647. tcd->saddr = sg_dma_address(sg);
  648. tcd->daddr = per_paddr;
  649. if (!IS_ALIGNED(sg_dma_address(sg), mchan->swidth))
  650. goto err_prep;
  651. tcd->soff = mchan->swidth;
  652. tcd->doff = 0;
  653. }
  654. tcd->ssize = buswidth_to_dmatsize(mchan->swidth);
  655. tcd->dsize = buswidth_to_dmatsize(mchan->dwidth);
  656. if (mdma->is_mpc8308) {
  657. tcd->nbytes = sg_dma_len(sg);
  658. if (!IS_ALIGNED(tcd->nbytes, mchan->swidth))
  659. goto err_prep;
  660. /* No major loops for MPC8303 */
  661. tcd->biter = 1;
  662. tcd->citer = 1;
  663. } else {
  664. len = sg_dma_len(sg);
  665. tcd->nbytes = tcd_nunits * tcd->ssize;
  666. if (!IS_ALIGNED(len, tcd->nbytes))
  667. goto err_prep;
  668. iter = len / tcd->nbytes;
  669. if (iter >= 1 << 15) {
  670. /* len is too big */
  671. goto err_prep;
  672. }
  673. /* citer_linkch contains the high bits of iter */
  674. tcd->biter = iter & 0x1ff;
  675. tcd->biter_linkch = iter >> 9;
  676. tcd->citer = tcd->biter;
  677. tcd->citer_linkch = tcd->biter_linkch;
  678. }
  679. tcd->e_sg = 0;
  680. tcd->d_req = 1;
  681. /* Place descriptor in prepared list */
  682. spin_lock_irqsave(&mchan->lock, iflags);
  683. list_add_tail(&mdesc->node, &mchan->prepared);
  684. spin_unlock_irqrestore(&mchan->lock, iflags);
  685. }
  686. return &mdesc->desc;
  687. err_prep:
  688. /* Put the descriptor back */
  689. spin_lock_irqsave(&mchan->lock, iflags);
  690. list_add_tail(&mdesc->node, &mchan->free);
  691. spin_unlock_irqrestore(&mchan->lock, iflags);
  692. return NULL;
  693. }
  694. inline bool is_buswidth_valid(u8 buswidth, bool is_mpc8308)
  695. {
  696. switch (buswidth) {
  697. case 16:
  698. if (is_mpc8308)
  699. return false;
  700. case 1:
  701. case 2:
  702. case 4:
  703. case 32:
  704. break;
  705. default:
  706. return false;
  707. }
  708. return true;
  709. }
  710. static int mpc_dma_device_config(struct dma_chan *chan,
  711. struct dma_slave_config *cfg)
  712. {
  713. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  714. struct mpc_dma *mdma = dma_chan_to_mpc_dma(&mchan->chan);
  715. unsigned long flags;
  716. /*
  717. * Software constraints:
  718. * - only transfers between a peripheral device and memory are
  719. * supported
  720. * - transfer chunk sizes of 1, 2, 4, 16 (for MPC512x), and 32 bytes
  721. * are supported, and, consequently, source addresses and
  722. * destination addresses; must be aligned accordingly; furthermore,
  723. * for MPC512x SoCs, the transfer size must be aligned on (chunk
  724. * size * maxburst)
  725. * - during the transfer, the RAM address is incremented by the size
  726. * of transfer chunk
  727. * - the peripheral port's address is constant during the transfer.
  728. */
  729. if (!IS_ALIGNED(cfg->src_addr, cfg->src_addr_width) ||
  730. !IS_ALIGNED(cfg->dst_addr, cfg->dst_addr_width)) {
  731. return -EINVAL;
  732. }
  733. if (!is_buswidth_valid(cfg->src_addr_width, mdma->is_mpc8308) ||
  734. !is_buswidth_valid(cfg->dst_addr_width, mdma->is_mpc8308))
  735. return -EINVAL;
  736. spin_lock_irqsave(&mchan->lock, flags);
  737. mchan->src_per_paddr = cfg->src_addr;
  738. mchan->src_tcd_nunits = cfg->src_maxburst;
  739. mchan->swidth = cfg->src_addr_width;
  740. mchan->dst_per_paddr = cfg->dst_addr;
  741. mchan->dst_tcd_nunits = cfg->dst_maxburst;
  742. mchan->dwidth = cfg->dst_addr_width;
  743. /* Apply defaults */
  744. if (mchan->src_tcd_nunits == 0)
  745. mchan->src_tcd_nunits = 1;
  746. if (mchan->dst_tcd_nunits == 0)
  747. mchan->dst_tcd_nunits = 1;
  748. spin_unlock_irqrestore(&mchan->lock, flags);
  749. return 0;
  750. }
  751. static int mpc_dma_device_terminate_all(struct dma_chan *chan)
  752. {
  753. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  754. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  755. unsigned long flags;
  756. /* Disable channel requests */
  757. spin_lock_irqsave(&mchan->lock, flags);
  758. out_8(&mdma->regs->dmacerq, chan->chan_id);
  759. list_splice_tail_init(&mchan->prepared, &mchan->free);
  760. list_splice_tail_init(&mchan->queued, &mchan->free);
  761. list_splice_tail_init(&mchan->active, &mchan->free);
  762. spin_unlock_irqrestore(&mchan->lock, flags);
  763. return 0;
  764. }
  765. static int mpc_dma_probe(struct platform_device *op)
  766. {
  767. struct device_node *dn = op->dev.of_node;
  768. struct device *dev = &op->dev;
  769. struct dma_device *dma;
  770. struct mpc_dma *mdma;
  771. struct mpc_dma_chan *mchan;
  772. struct resource res;
  773. ulong regs_start, regs_size;
  774. int retval, i;
  775. u8 chancnt;
  776. mdma = devm_kzalloc(dev, sizeof(struct mpc_dma), GFP_KERNEL);
  777. if (!mdma) {
  778. retval = -ENOMEM;
  779. goto err;
  780. }
  781. mdma->irq = irq_of_parse_and_map(dn, 0);
  782. if (!mdma->irq) {
  783. dev_err(dev, "Error mapping IRQ!\n");
  784. retval = -EINVAL;
  785. goto err;
  786. }
  787. if (of_device_is_compatible(dn, "fsl,mpc8308-dma")) {
  788. mdma->is_mpc8308 = 1;
  789. mdma->irq2 = irq_of_parse_and_map(dn, 1);
  790. if (!mdma->irq2) {
  791. dev_err(dev, "Error mapping IRQ!\n");
  792. retval = -EINVAL;
  793. goto err_dispose1;
  794. }
  795. }
  796. retval = of_address_to_resource(dn, 0, &res);
  797. if (retval) {
  798. dev_err(dev, "Error parsing memory region!\n");
  799. goto err_dispose2;
  800. }
  801. regs_start = res.start;
  802. regs_size = resource_size(&res);
  803. if (!devm_request_mem_region(dev, regs_start, regs_size, DRV_NAME)) {
  804. dev_err(dev, "Error requesting memory region!\n");
  805. retval = -EBUSY;
  806. goto err_dispose2;
  807. }
  808. mdma->regs = devm_ioremap(dev, regs_start, regs_size);
  809. if (!mdma->regs) {
  810. dev_err(dev, "Error mapping memory region!\n");
  811. retval = -ENOMEM;
  812. goto err_dispose2;
  813. }
  814. mdma->tcd = (struct mpc_dma_tcd *)((u8 *)(mdma->regs)
  815. + MPC_DMA_TCD_OFFSET);
  816. retval = request_irq(mdma->irq, &mpc_dma_irq, 0, DRV_NAME, mdma);
  817. if (retval) {
  818. dev_err(dev, "Error requesting IRQ!\n");
  819. retval = -EINVAL;
  820. goto err_dispose2;
  821. }
  822. if (mdma->is_mpc8308) {
  823. retval = request_irq(mdma->irq2, &mpc_dma_irq, 0,
  824. DRV_NAME, mdma);
  825. if (retval) {
  826. dev_err(dev, "Error requesting IRQ2!\n");
  827. retval = -EINVAL;
  828. goto err_free1;
  829. }
  830. }
  831. spin_lock_init(&mdma->error_status_lock);
  832. dma = &mdma->dma;
  833. dma->dev = dev;
  834. dma->device_alloc_chan_resources = mpc_dma_alloc_chan_resources;
  835. dma->device_free_chan_resources = mpc_dma_free_chan_resources;
  836. dma->device_issue_pending = mpc_dma_issue_pending;
  837. dma->device_tx_status = mpc_dma_tx_status;
  838. dma->device_prep_dma_memcpy = mpc_dma_prep_memcpy;
  839. dma->device_prep_slave_sg = mpc_dma_prep_slave_sg;
  840. dma->device_config = mpc_dma_device_config;
  841. dma->device_terminate_all = mpc_dma_device_terminate_all;
  842. INIT_LIST_HEAD(&dma->channels);
  843. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  844. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  845. if (mdma->is_mpc8308)
  846. chancnt = MPC8308_DMACHAN_MAX;
  847. else
  848. chancnt = MPC512x_DMACHAN_MAX;
  849. for (i = 0; i < chancnt; i++) {
  850. mchan = &mdma->channels[i];
  851. mchan->chan.device = dma;
  852. dma_cookie_init(&mchan->chan);
  853. INIT_LIST_HEAD(&mchan->free);
  854. INIT_LIST_HEAD(&mchan->prepared);
  855. INIT_LIST_HEAD(&mchan->queued);
  856. INIT_LIST_HEAD(&mchan->active);
  857. INIT_LIST_HEAD(&mchan->completed);
  858. spin_lock_init(&mchan->lock);
  859. list_add_tail(&mchan->chan.device_node, &dma->channels);
  860. }
  861. tasklet_init(&mdma->tasklet, mpc_dma_tasklet, (unsigned long)mdma);
  862. /*
  863. * Configure DMA Engine:
  864. * - Dynamic clock,
  865. * - Round-robin group arbitration,
  866. * - Round-robin channel arbitration.
  867. */
  868. if (mdma->is_mpc8308) {
  869. /* MPC8308 has 16 channels and lacks some registers */
  870. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_ERCA);
  871. /* enable snooping */
  872. out_be32(&mdma->regs->dmagpor, MPC_DMA_DMAGPOR_SNOOP_ENABLE);
  873. /* Disable error interrupts */
  874. out_be32(&mdma->regs->dmaeeil, 0);
  875. /* Clear interrupts status */
  876. out_be32(&mdma->regs->dmaintl, 0xFFFF);
  877. out_be32(&mdma->regs->dmaerrl, 0xFFFF);
  878. } else {
  879. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_EDCG |
  880. MPC_DMA_DMACR_ERGA |
  881. MPC_DMA_DMACR_ERCA);
  882. /* Disable hardware DMA requests */
  883. out_be32(&mdma->regs->dmaerqh, 0);
  884. out_be32(&mdma->regs->dmaerql, 0);
  885. /* Disable error interrupts */
  886. out_be32(&mdma->regs->dmaeeih, 0);
  887. out_be32(&mdma->regs->dmaeeil, 0);
  888. /* Clear interrupts status */
  889. out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
  890. out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
  891. out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
  892. out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
  893. /* Route interrupts to IPIC */
  894. out_be32(&mdma->regs->dmaihsa, 0);
  895. out_be32(&mdma->regs->dmailsa, 0);
  896. }
  897. /* Register DMA engine */
  898. dev_set_drvdata(dev, mdma);
  899. retval = dma_async_device_register(dma);
  900. if (retval)
  901. goto err_free2;
  902. /* Register with OF helpers for DMA lookups (nonfatal) */
  903. if (dev->of_node) {
  904. retval = of_dma_controller_register(dev->of_node,
  905. of_dma_xlate_by_chan_id, mdma);
  906. if (retval)
  907. dev_warn(dev, "Could not register for OF lookup\n");
  908. }
  909. return 0;
  910. err_free2:
  911. if (mdma->is_mpc8308)
  912. free_irq(mdma->irq2, mdma);
  913. err_free1:
  914. free_irq(mdma->irq, mdma);
  915. err_dispose2:
  916. if (mdma->is_mpc8308)
  917. irq_dispose_mapping(mdma->irq2);
  918. err_dispose1:
  919. irq_dispose_mapping(mdma->irq);
  920. err:
  921. return retval;
  922. }
  923. static int mpc_dma_remove(struct platform_device *op)
  924. {
  925. struct device *dev = &op->dev;
  926. struct mpc_dma *mdma = dev_get_drvdata(dev);
  927. if (dev->of_node)
  928. of_dma_controller_free(dev->of_node);
  929. dma_async_device_unregister(&mdma->dma);
  930. if (mdma->is_mpc8308) {
  931. free_irq(mdma->irq2, mdma);
  932. irq_dispose_mapping(mdma->irq2);
  933. }
  934. free_irq(mdma->irq, mdma);
  935. irq_dispose_mapping(mdma->irq);
  936. tasklet_kill(&mdma->tasklet);
  937. return 0;
  938. }
  939. static const struct of_device_id mpc_dma_match[] = {
  940. { .compatible = "fsl,mpc5121-dma", },
  941. { .compatible = "fsl,mpc8308-dma", },
  942. {},
  943. };
  944. MODULE_DEVICE_TABLE(of, mpc_dma_match);
  945. static struct platform_driver mpc_dma_driver = {
  946. .probe = mpc_dma_probe,
  947. .remove = mpc_dma_remove,
  948. .driver = {
  949. .name = DRV_NAME,
  950. .of_match_table = mpc_dma_match,
  951. },
  952. };
  953. module_platform_driver(mpc_dma_driver);
  954. MODULE_LICENSE("GPL");
  955. MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");