dma-jz4780.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Ingenic JZ4780 DMA controller
  3. *
  4. * Copyright (c) 2015 Imagination Technologies
  5. * Author: Alex Smith <alex@alex-smith.me.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_dma.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include "dmaengine.h"
  22. #include "virt-dma.h"
  23. #define JZ_DMA_NR_CHANNELS 32
  24. /* Global registers. */
  25. #define JZ_DMA_REG_DMAC 0x1000
  26. #define JZ_DMA_REG_DIRQP 0x1004
  27. #define JZ_DMA_REG_DDR 0x1008
  28. #define JZ_DMA_REG_DDRS 0x100c
  29. #define JZ_DMA_REG_DMACP 0x101c
  30. #define JZ_DMA_REG_DSIRQP 0x1020
  31. #define JZ_DMA_REG_DSIRQM 0x1024
  32. #define JZ_DMA_REG_DCIRQP 0x1028
  33. #define JZ_DMA_REG_DCIRQM 0x102c
  34. /* Per-channel registers. */
  35. #define JZ_DMA_REG_CHAN(n) (n * 0x20)
  36. #define JZ_DMA_REG_DSA(n) (0x00 + JZ_DMA_REG_CHAN(n))
  37. #define JZ_DMA_REG_DTA(n) (0x04 + JZ_DMA_REG_CHAN(n))
  38. #define JZ_DMA_REG_DTC(n) (0x08 + JZ_DMA_REG_CHAN(n))
  39. #define JZ_DMA_REG_DRT(n) (0x0c + JZ_DMA_REG_CHAN(n))
  40. #define JZ_DMA_REG_DCS(n) (0x10 + JZ_DMA_REG_CHAN(n))
  41. #define JZ_DMA_REG_DCM(n) (0x14 + JZ_DMA_REG_CHAN(n))
  42. #define JZ_DMA_REG_DDA(n) (0x18 + JZ_DMA_REG_CHAN(n))
  43. #define JZ_DMA_REG_DSD(n) (0x1c + JZ_DMA_REG_CHAN(n))
  44. #define JZ_DMA_DMAC_DMAE BIT(0)
  45. #define JZ_DMA_DMAC_AR BIT(2)
  46. #define JZ_DMA_DMAC_HLT BIT(3)
  47. #define JZ_DMA_DMAC_FMSC BIT(31)
  48. #define JZ_DMA_DRT_AUTO 0x8
  49. #define JZ_DMA_DCS_CTE BIT(0)
  50. #define JZ_DMA_DCS_HLT BIT(2)
  51. #define JZ_DMA_DCS_TT BIT(3)
  52. #define JZ_DMA_DCS_AR BIT(4)
  53. #define JZ_DMA_DCS_DES8 BIT(30)
  54. #define JZ_DMA_DCM_LINK BIT(0)
  55. #define JZ_DMA_DCM_TIE BIT(1)
  56. #define JZ_DMA_DCM_STDE BIT(2)
  57. #define JZ_DMA_DCM_TSZ_SHIFT 8
  58. #define JZ_DMA_DCM_TSZ_MASK (0x7 << JZ_DMA_DCM_TSZ_SHIFT)
  59. #define JZ_DMA_DCM_DP_SHIFT 12
  60. #define JZ_DMA_DCM_SP_SHIFT 14
  61. #define JZ_DMA_DCM_DAI BIT(22)
  62. #define JZ_DMA_DCM_SAI BIT(23)
  63. #define JZ_DMA_SIZE_4_BYTE 0x0
  64. #define JZ_DMA_SIZE_1_BYTE 0x1
  65. #define JZ_DMA_SIZE_2_BYTE 0x2
  66. #define JZ_DMA_SIZE_16_BYTE 0x3
  67. #define JZ_DMA_SIZE_32_BYTE 0x4
  68. #define JZ_DMA_SIZE_64_BYTE 0x5
  69. #define JZ_DMA_SIZE_128_BYTE 0x6
  70. #define JZ_DMA_WIDTH_32_BIT 0x0
  71. #define JZ_DMA_WIDTH_8_BIT 0x1
  72. #define JZ_DMA_WIDTH_16_BIT 0x2
  73. #define JZ_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  74. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  75. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  76. /**
  77. * struct jz4780_dma_hwdesc - descriptor structure read by the DMA controller.
  78. * @dcm: value for the DCM (channel command) register
  79. * @dsa: source address
  80. * @dta: target address
  81. * @dtc: transfer count (number of blocks of the transfer size specified in DCM
  82. * to transfer) in the low 24 bits, offset of the next descriptor from the
  83. * descriptor base address in the upper 8 bits.
  84. * @sd: target/source stride difference (in stride transfer mode).
  85. * @drt: request type
  86. */
  87. struct jz4780_dma_hwdesc {
  88. uint32_t dcm;
  89. uint32_t dsa;
  90. uint32_t dta;
  91. uint32_t dtc;
  92. uint32_t sd;
  93. uint32_t drt;
  94. uint32_t reserved[2];
  95. };
  96. /* Size of allocations for hardware descriptor blocks. */
  97. #define JZ_DMA_DESC_BLOCK_SIZE PAGE_SIZE
  98. #define JZ_DMA_MAX_DESC \
  99. (JZ_DMA_DESC_BLOCK_SIZE / sizeof(struct jz4780_dma_hwdesc))
  100. struct jz4780_dma_desc {
  101. struct virt_dma_desc vdesc;
  102. struct jz4780_dma_hwdesc *desc;
  103. dma_addr_t desc_phys;
  104. unsigned int count;
  105. enum dma_transaction_type type;
  106. uint32_t status;
  107. };
  108. struct jz4780_dma_chan {
  109. struct virt_dma_chan vchan;
  110. unsigned int id;
  111. struct dma_pool *desc_pool;
  112. uint32_t transfer_type;
  113. uint32_t transfer_shift;
  114. struct dma_slave_config config;
  115. struct jz4780_dma_desc *desc;
  116. unsigned int curr_hwdesc;
  117. };
  118. struct jz4780_dma_dev {
  119. struct dma_device dma_device;
  120. void __iomem *base;
  121. struct clk *clk;
  122. unsigned int irq;
  123. uint32_t chan_reserved;
  124. struct jz4780_dma_chan chan[JZ_DMA_NR_CHANNELS];
  125. };
  126. struct jz4780_dma_filter_data {
  127. struct device_node *of_node;
  128. uint32_t transfer_type;
  129. int channel;
  130. };
  131. static inline struct jz4780_dma_chan *to_jz4780_dma_chan(struct dma_chan *chan)
  132. {
  133. return container_of(chan, struct jz4780_dma_chan, vchan.chan);
  134. }
  135. static inline struct jz4780_dma_desc *to_jz4780_dma_desc(
  136. struct virt_dma_desc *vdesc)
  137. {
  138. return container_of(vdesc, struct jz4780_dma_desc, vdesc);
  139. }
  140. static inline struct jz4780_dma_dev *jz4780_dma_chan_parent(
  141. struct jz4780_dma_chan *jzchan)
  142. {
  143. return container_of(jzchan->vchan.chan.device, struct jz4780_dma_dev,
  144. dma_device);
  145. }
  146. static inline uint32_t jz4780_dma_readl(struct jz4780_dma_dev *jzdma,
  147. unsigned int reg)
  148. {
  149. return readl(jzdma->base + reg);
  150. }
  151. static inline void jz4780_dma_writel(struct jz4780_dma_dev *jzdma,
  152. unsigned int reg, uint32_t val)
  153. {
  154. writel(val, jzdma->base + reg);
  155. }
  156. static struct jz4780_dma_desc *jz4780_dma_desc_alloc(
  157. struct jz4780_dma_chan *jzchan, unsigned int count,
  158. enum dma_transaction_type type)
  159. {
  160. struct jz4780_dma_desc *desc;
  161. if (count > JZ_DMA_MAX_DESC)
  162. return NULL;
  163. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  164. if (!desc)
  165. return NULL;
  166. desc->desc = dma_pool_alloc(jzchan->desc_pool, GFP_NOWAIT,
  167. &desc->desc_phys);
  168. if (!desc->desc) {
  169. kfree(desc);
  170. return NULL;
  171. }
  172. desc->count = count;
  173. desc->type = type;
  174. return desc;
  175. }
  176. static void jz4780_dma_desc_free(struct virt_dma_desc *vdesc)
  177. {
  178. struct jz4780_dma_desc *desc = to_jz4780_dma_desc(vdesc);
  179. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(vdesc->tx.chan);
  180. dma_pool_free(jzchan->desc_pool, desc->desc, desc->desc_phys);
  181. kfree(desc);
  182. }
  183. static uint32_t jz4780_dma_transfer_size(unsigned long val, uint32_t *shift)
  184. {
  185. int ord = ffs(val) - 1;
  186. /*
  187. * 8 byte transfer sizes unsupported so fall back on 4. If it's larger
  188. * than the maximum, just limit it. It is perfectly safe to fall back
  189. * in this way since we won't exceed the maximum burst size supported
  190. * by the device, the only effect is reduced efficiency. This is better
  191. * than refusing to perform the request at all.
  192. */
  193. if (ord == 3)
  194. ord = 2;
  195. else if (ord > 7)
  196. ord = 7;
  197. *shift = ord;
  198. switch (ord) {
  199. case 0:
  200. return JZ_DMA_SIZE_1_BYTE;
  201. case 1:
  202. return JZ_DMA_SIZE_2_BYTE;
  203. case 2:
  204. return JZ_DMA_SIZE_4_BYTE;
  205. case 4:
  206. return JZ_DMA_SIZE_16_BYTE;
  207. case 5:
  208. return JZ_DMA_SIZE_32_BYTE;
  209. case 6:
  210. return JZ_DMA_SIZE_64_BYTE;
  211. default:
  212. return JZ_DMA_SIZE_128_BYTE;
  213. }
  214. }
  215. static int jz4780_dma_setup_hwdesc(struct jz4780_dma_chan *jzchan,
  216. struct jz4780_dma_hwdesc *desc, dma_addr_t addr, size_t len,
  217. enum dma_transfer_direction direction)
  218. {
  219. struct dma_slave_config *config = &jzchan->config;
  220. uint32_t width, maxburst, tsz;
  221. if (direction == DMA_MEM_TO_DEV) {
  222. desc->dcm = JZ_DMA_DCM_SAI;
  223. desc->dsa = addr;
  224. desc->dta = config->dst_addr;
  225. desc->drt = jzchan->transfer_type;
  226. width = config->dst_addr_width;
  227. maxburst = config->dst_maxburst;
  228. } else {
  229. desc->dcm = JZ_DMA_DCM_DAI;
  230. desc->dsa = config->src_addr;
  231. desc->dta = addr;
  232. desc->drt = jzchan->transfer_type;
  233. width = config->src_addr_width;
  234. maxburst = config->src_maxburst;
  235. }
  236. /*
  237. * This calculates the maximum transfer size that can be used with the
  238. * given address, length, width and maximum burst size. The address
  239. * must be aligned to the transfer size, the total length must be
  240. * divisible by the transfer size, and we must not use more than the
  241. * maximum burst specified by the user.
  242. */
  243. tsz = jz4780_dma_transfer_size(addr | len | (width * maxburst),
  244. &jzchan->transfer_shift);
  245. switch (width) {
  246. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  247. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  248. break;
  249. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  250. width = JZ_DMA_WIDTH_32_BIT;
  251. break;
  252. default:
  253. return -EINVAL;
  254. }
  255. desc->dcm |= tsz << JZ_DMA_DCM_TSZ_SHIFT;
  256. desc->dcm |= width << JZ_DMA_DCM_SP_SHIFT;
  257. desc->dcm |= width << JZ_DMA_DCM_DP_SHIFT;
  258. desc->dtc = len >> jzchan->transfer_shift;
  259. return 0;
  260. }
  261. static struct dma_async_tx_descriptor *jz4780_dma_prep_slave_sg(
  262. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  263. enum dma_transfer_direction direction, unsigned long flags,
  264. void *context)
  265. {
  266. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  267. struct jz4780_dma_desc *desc;
  268. unsigned int i;
  269. int err;
  270. desc = jz4780_dma_desc_alloc(jzchan, sg_len, DMA_SLAVE);
  271. if (!desc)
  272. return NULL;
  273. for (i = 0; i < sg_len; i++) {
  274. err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i],
  275. sg_dma_address(&sgl[i]),
  276. sg_dma_len(&sgl[i]),
  277. direction);
  278. if (err < 0) {
  279. jz4780_dma_desc_free(&jzchan->desc->vdesc);
  280. return NULL;
  281. }
  282. desc->desc[i].dcm |= JZ_DMA_DCM_TIE;
  283. if (i != (sg_len - 1)) {
  284. /* Automatically proceeed to the next descriptor. */
  285. desc->desc[i].dcm |= JZ_DMA_DCM_LINK;
  286. /*
  287. * The upper 8 bits of the DTC field in the descriptor
  288. * must be set to (offset from descriptor base of next
  289. * descriptor >> 4).
  290. */
  291. desc->desc[i].dtc |=
  292. (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
  293. }
  294. }
  295. return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
  296. }
  297. static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic(
  298. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  299. size_t period_len, enum dma_transfer_direction direction,
  300. unsigned long flags)
  301. {
  302. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  303. struct jz4780_dma_desc *desc;
  304. unsigned int periods, i;
  305. int err;
  306. if (buf_len % period_len)
  307. return NULL;
  308. periods = buf_len / period_len;
  309. desc = jz4780_dma_desc_alloc(jzchan, periods, DMA_CYCLIC);
  310. if (!desc)
  311. return NULL;
  312. for (i = 0; i < periods; i++) {
  313. err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr,
  314. period_len, direction);
  315. if (err < 0) {
  316. jz4780_dma_desc_free(&jzchan->desc->vdesc);
  317. return NULL;
  318. }
  319. buf_addr += period_len;
  320. /*
  321. * Set the link bit to indicate that the controller should
  322. * automatically proceed to the next descriptor. In
  323. * jz4780_dma_begin(), this will be cleared if we need to issue
  324. * an interrupt after each period.
  325. */
  326. desc->desc[i].dcm |= JZ_DMA_DCM_TIE | JZ_DMA_DCM_LINK;
  327. /*
  328. * The upper 8 bits of the DTC field in the descriptor must be
  329. * set to (offset from descriptor base of next descriptor >> 4).
  330. * If this is the last descriptor, link it back to the first,
  331. * i.e. leave offset set to 0, otherwise point to the next one.
  332. */
  333. if (i != (periods - 1)) {
  334. desc->desc[i].dtc |=
  335. (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
  336. }
  337. }
  338. return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
  339. }
  340. static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_memcpy(
  341. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  342. size_t len, unsigned long flags)
  343. {
  344. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  345. struct jz4780_dma_desc *desc;
  346. uint32_t tsz;
  347. desc = jz4780_dma_desc_alloc(jzchan, 1, DMA_MEMCPY);
  348. if (!desc)
  349. return NULL;
  350. tsz = jz4780_dma_transfer_size(dest | src | len,
  351. &jzchan->transfer_shift);
  352. desc->desc[0].dsa = src;
  353. desc->desc[0].dta = dest;
  354. desc->desc[0].drt = JZ_DMA_DRT_AUTO;
  355. desc->desc[0].dcm = JZ_DMA_DCM_TIE | JZ_DMA_DCM_SAI | JZ_DMA_DCM_DAI |
  356. tsz << JZ_DMA_DCM_TSZ_SHIFT |
  357. JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_SP_SHIFT |
  358. JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_DP_SHIFT;
  359. desc->desc[0].dtc = len >> jzchan->transfer_shift;
  360. return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
  361. }
  362. static void jz4780_dma_begin(struct jz4780_dma_chan *jzchan)
  363. {
  364. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  365. struct virt_dma_desc *vdesc;
  366. unsigned int i;
  367. dma_addr_t desc_phys;
  368. if (!jzchan->desc) {
  369. vdesc = vchan_next_desc(&jzchan->vchan);
  370. if (!vdesc)
  371. return;
  372. list_del(&vdesc->node);
  373. jzchan->desc = to_jz4780_dma_desc(vdesc);
  374. jzchan->curr_hwdesc = 0;
  375. if (jzchan->desc->type == DMA_CYCLIC && vdesc->tx.callback) {
  376. /*
  377. * The DMA controller doesn't support triggering an
  378. * interrupt after processing each descriptor, only
  379. * after processing an entire terminated list of
  380. * descriptors. For a cyclic DMA setup the list of
  381. * descriptors is not terminated so we can never get an
  382. * interrupt.
  383. *
  384. * If the user requested a callback for a cyclic DMA
  385. * setup then we workaround this hardware limitation
  386. * here by degrading to a set of unlinked descriptors
  387. * which we will submit in sequence in response to the
  388. * completion of processing the previous descriptor.
  389. */
  390. for (i = 0; i < jzchan->desc->count; i++)
  391. jzchan->desc->desc[i].dcm &= ~JZ_DMA_DCM_LINK;
  392. }
  393. } else {
  394. /*
  395. * There is an existing transfer, therefore this must be one
  396. * for which we unlinked the descriptors above. Advance to the
  397. * next one in the list.
  398. */
  399. jzchan->curr_hwdesc =
  400. (jzchan->curr_hwdesc + 1) % jzchan->desc->count;
  401. }
  402. /* Use 8-word descriptors. */
  403. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), JZ_DMA_DCS_DES8);
  404. /* Write descriptor address and initiate descriptor fetch. */
  405. desc_phys = jzchan->desc->desc_phys +
  406. (jzchan->curr_hwdesc * sizeof(*jzchan->desc->desc));
  407. jz4780_dma_writel(jzdma, JZ_DMA_REG_DDA(jzchan->id), desc_phys);
  408. jz4780_dma_writel(jzdma, JZ_DMA_REG_DDRS, BIT(jzchan->id));
  409. /* Enable the channel. */
  410. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id),
  411. JZ_DMA_DCS_DES8 | JZ_DMA_DCS_CTE);
  412. }
  413. static void jz4780_dma_issue_pending(struct dma_chan *chan)
  414. {
  415. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  416. unsigned long flags;
  417. spin_lock_irqsave(&jzchan->vchan.lock, flags);
  418. if (vchan_issue_pending(&jzchan->vchan) && !jzchan->desc)
  419. jz4780_dma_begin(jzchan);
  420. spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
  421. }
  422. static int jz4780_dma_terminate_all(struct dma_chan *chan)
  423. {
  424. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  425. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  426. unsigned long flags;
  427. LIST_HEAD(head);
  428. spin_lock_irqsave(&jzchan->vchan.lock, flags);
  429. /* Clear the DMA status and stop the transfer. */
  430. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), 0);
  431. if (jzchan->desc) {
  432. jz4780_dma_desc_free(&jzchan->desc->vdesc);
  433. jzchan->desc = NULL;
  434. }
  435. vchan_get_all_descriptors(&jzchan->vchan, &head);
  436. spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
  437. vchan_dma_desc_free_list(&jzchan->vchan, &head);
  438. return 0;
  439. }
  440. static int jz4780_dma_config(struct dma_chan *chan,
  441. struct dma_slave_config *config)
  442. {
  443. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  444. if ((config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  445. || (config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES))
  446. return -EINVAL;
  447. /* Copy the reset of the slave configuration, it is used later. */
  448. memcpy(&jzchan->config, config, sizeof(jzchan->config));
  449. return 0;
  450. }
  451. static size_t jz4780_dma_desc_residue(struct jz4780_dma_chan *jzchan,
  452. struct jz4780_dma_desc *desc, unsigned int next_sg)
  453. {
  454. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  455. unsigned int residue, count;
  456. unsigned int i;
  457. residue = 0;
  458. for (i = next_sg; i < desc->count; i++)
  459. residue += desc->desc[i].dtc << jzchan->transfer_shift;
  460. if (next_sg != 0) {
  461. count = jz4780_dma_readl(jzdma,
  462. JZ_DMA_REG_DTC(jzchan->id));
  463. residue += count << jzchan->transfer_shift;
  464. }
  465. return residue;
  466. }
  467. static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan,
  468. dma_cookie_t cookie, struct dma_tx_state *txstate)
  469. {
  470. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  471. struct virt_dma_desc *vdesc;
  472. enum dma_status status;
  473. unsigned long flags;
  474. status = dma_cookie_status(chan, cookie, txstate);
  475. if ((status == DMA_COMPLETE) || (txstate == NULL))
  476. return status;
  477. spin_lock_irqsave(&jzchan->vchan.lock, flags);
  478. vdesc = vchan_find_desc(&jzchan->vchan, cookie);
  479. if (vdesc) {
  480. /* On the issued list, so hasn't been processed yet */
  481. txstate->residue = jz4780_dma_desc_residue(jzchan,
  482. to_jz4780_dma_desc(vdesc), 0);
  483. } else if (cookie == jzchan->desc->vdesc.tx.cookie) {
  484. txstate->residue = jz4780_dma_desc_residue(jzchan, jzchan->desc,
  485. (jzchan->curr_hwdesc + 1) % jzchan->desc->count);
  486. } else
  487. txstate->residue = 0;
  488. if (vdesc && jzchan->desc && vdesc == &jzchan->desc->vdesc
  489. && jzchan->desc->status & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT))
  490. status = DMA_ERROR;
  491. spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
  492. return status;
  493. }
  494. static void jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
  495. struct jz4780_dma_chan *jzchan)
  496. {
  497. uint32_t dcs;
  498. spin_lock(&jzchan->vchan.lock);
  499. dcs = jz4780_dma_readl(jzdma, JZ_DMA_REG_DCS(jzchan->id));
  500. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), 0);
  501. if (dcs & JZ_DMA_DCS_AR) {
  502. dev_warn(&jzchan->vchan.chan.dev->device,
  503. "address error (DCS=0x%x)\n", dcs);
  504. }
  505. if (dcs & JZ_DMA_DCS_HLT) {
  506. dev_warn(&jzchan->vchan.chan.dev->device,
  507. "channel halt (DCS=0x%x)\n", dcs);
  508. }
  509. if (jzchan->desc) {
  510. jzchan->desc->status = dcs;
  511. if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
  512. if (jzchan->desc->type == DMA_CYCLIC) {
  513. vchan_cyclic_callback(&jzchan->desc->vdesc);
  514. } else {
  515. vchan_cookie_complete(&jzchan->desc->vdesc);
  516. jzchan->desc = NULL;
  517. }
  518. jz4780_dma_begin(jzchan);
  519. }
  520. } else {
  521. dev_err(&jzchan->vchan.chan.dev->device,
  522. "channel IRQ with no active transfer\n");
  523. }
  524. spin_unlock(&jzchan->vchan.lock);
  525. }
  526. static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
  527. {
  528. struct jz4780_dma_dev *jzdma = data;
  529. uint32_t pending, dmac;
  530. int i;
  531. pending = jz4780_dma_readl(jzdma, JZ_DMA_REG_DIRQP);
  532. for (i = 0; i < JZ_DMA_NR_CHANNELS; i++) {
  533. if (!(pending & (1<<i)))
  534. continue;
  535. jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]);
  536. }
  537. /* Clear halt and address error status of all channels. */
  538. dmac = jz4780_dma_readl(jzdma, JZ_DMA_REG_DMAC);
  539. dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
  540. jz4780_dma_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
  541. /* Clear interrupt pending status. */
  542. jz4780_dma_writel(jzdma, JZ_DMA_REG_DIRQP, 0);
  543. return IRQ_HANDLED;
  544. }
  545. static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
  546. {
  547. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  548. jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
  549. chan->device->dev,
  550. JZ_DMA_DESC_BLOCK_SIZE,
  551. PAGE_SIZE, 0);
  552. if (!jzchan->desc_pool) {
  553. dev_err(&chan->dev->device,
  554. "failed to allocate descriptor pool\n");
  555. return -ENOMEM;
  556. }
  557. return 0;
  558. }
  559. static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
  560. {
  561. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  562. vchan_free_chan_resources(&jzchan->vchan);
  563. dma_pool_destroy(jzchan->desc_pool);
  564. jzchan->desc_pool = NULL;
  565. }
  566. static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
  567. {
  568. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  569. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  570. struct jz4780_dma_filter_data *data = param;
  571. if (jzdma->dma_device.dev->of_node != data->of_node)
  572. return false;
  573. if (data->channel > -1) {
  574. if (data->channel != jzchan->id)
  575. return false;
  576. } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
  577. return false;
  578. }
  579. jzchan->transfer_type = data->transfer_type;
  580. return true;
  581. }
  582. static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
  583. struct of_dma *ofdma)
  584. {
  585. struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
  586. dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
  587. struct jz4780_dma_filter_data data;
  588. if (dma_spec->args_count != 2)
  589. return NULL;
  590. data.of_node = ofdma->of_node;
  591. data.transfer_type = dma_spec->args[0];
  592. data.channel = dma_spec->args[1];
  593. if (data.channel > -1) {
  594. if (data.channel >= JZ_DMA_NR_CHANNELS) {
  595. dev_err(jzdma->dma_device.dev,
  596. "device requested non-existent channel %u\n",
  597. data.channel);
  598. return NULL;
  599. }
  600. /* Can only select a channel marked as reserved. */
  601. if (!(jzdma->chan_reserved & BIT(data.channel))) {
  602. dev_err(jzdma->dma_device.dev,
  603. "device requested unreserved channel %u\n",
  604. data.channel);
  605. return NULL;
  606. }
  607. jzdma->chan[data.channel].transfer_type = data.transfer_type;
  608. return dma_get_slave_channel(
  609. &jzdma->chan[data.channel].vchan.chan);
  610. } else {
  611. return dma_request_channel(mask, jz4780_dma_filter_fn, &data);
  612. }
  613. }
  614. static int jz4780_dma_probe(struct platform_device *pdev)
  615. {
  616. struct device *dev = &pdev->dev;
  617. struct jz4780_dma_dev *jzdma;
  618. struct jz4780_dma_chan *jzchan;
  619. struct dma_device *dd;
  620. struct resource *res;
  621. int i, ret;
  622. jzdma = devm_kzalloc(dev, sizeof(*jzdma), GFP_KERNEL);
  623. if (!jzdma)
  624. return -ENOMEM;
  625. platform_set_drvdata(pdev, jzdma);
  626. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  627. if (!res) {
  628. dev_err(dev, "failed to get I/O memory\n");
  629. return -EINVAL;
  630. }
  631. jzdma->base = devm_ioremap_resource(dev, res);
  632. if (IS_ERR(jzdma->base))
  633. return PTR_ERR(jzdma->base);
  634. ret = platform_get_irq(pdev, 0);
  635. if (ret < 0) {
  636. dev_err(dev, "failed to get IRQ: %d\n", ret);
  637. return ret;
  638. }
  639. jzdma->irq = ret;
  640. ret = request_irq(jzdma->irq, jz4780_dma_irq_handler, 0, dev_name(dev),
  641. jzdma);
  642. if (ret) {
  643. dev_err(dev, "failed to request IRQ %u!\n", jzdma->irq);
  644. return ret;
  645. }
  646. jzdma->clk = devm_clk_get(dev, NULL);
  647. if (IS_ERR(jzdma->clk)) {
  648. dev_err(dev, "failed to get clock\n");
  649. ret = PTR_ERR(jzdma->clk);
  650. goto err_free_irq;
  651. }
  652. clk_prepare_enable(jzdma->clk);
  653. /* Property is optional, if it doesn't exist the value will remain 0. */
  654. of_property_read_u32_index(dev->of_node, "ingenic,reserved-channels",
  655. 0, &jzdma->chan_reserved);
  656. dd = &jzdma->dma_device;
  657. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  658. dma_cap_set(DMA_SLAVE, dd->cap_mask);
  659. dma_cap_set(DMA_CYCLIC, dd->cap_mask);
  660. dd->dev = dev;
  661. dd->copy_align = DMAENGINE_ALIGN_4_BYTES;
  662. dd->device_alloc_chan_resources = jz4780_dma_alloc_chan_resources;
  663. dd->device_free_chan_resources = jz4780_dma_free_chan_resources;
  664. dd->device_prep_slave_sg = jz4780_dma_prep_slave_sg;
  665. dd->device_prep_dma_cyclic = jz4780_dma_prep_dma_cyclic;
  666. dd->device_prep_dma_memcpy = jz4780_dma_prep_dma_memcpy;
  667. dd->device_config = jz4780_dma_config;
  668. dd->device_terminate_all = jz4780_dma_terminate_all;
  669. dd->device_tx_status = jz4780_dma_tx_status;
  670. dd->device_issue_pending = jz4780_dma_issue_pending;
  671. dd->src_addr_widths = JZ_DMA_BUSWIDTHS;
  672. dd->dst_addr_widths = JZ_DMA_BUSWIDTHS;
  673. dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  674. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  675. /*
  676. * Enable DMA controller, mark all channels as not programmable.
  677. * Also set the FMSC bit - it increases MSC performance, so it makes
  678. * little sense not to enable it.
  679. */
  680. jz4780_dma_writel(jzdma, JZ_DMA_REG_DMAC,
  681. JZ_DMA_DMAC_DMAE | JZ_DMA_DMAC_FMSC);
  682. jz4780_dma_writel(jzdma, JZ_DMA_REG_DMACP, 0);
  683. INIT_LIST_HEAD(&dd->channels);
  684. for (i = 0; i < JZ_DMA_NR_CHANNELS; i++) {
  685. jzchan = &jzdma->chan[i];
  686. jzchan->id = i;
  687. vchan_init(&jzchan->vchan, dd);
  688. jzchan->vchan.desc_free = jz4780_dma_desc_free;
  689. }
  690. ret = dma_async_device_register(dd);
  691. if (ret) {
  692. dev_err(dev, "failed to register device\n");
  693. goto err_disable_clk;
  694. }
  695. /* Register with OF DMA helpers. */
  696. ret = of_dma_controller_register(dev->of_node, jz4780_of_dma_xlate,
  697. jzdma);
  698. if (ret) {
  699. dev_err(dev, "failed to register OF DMA controller\n");
  700. goto err_unregister_dev;
  701. }
  702. dev_info(dev, "JZ4780 DMA controller initialised\n");
  703. return 0;
  704. err_unregister_dev:
  705. dma_async_device_unregister(dd);
  706. err_disable_clk:
  707. clk_disable_unprepare(jzdma->clk);
  708. err_free_irq:
  709. free_irq(jzdma->irq, jzdma);
  710. return ret;
  711. }
  712. static int jz4780_dma_remove(struct platform_device *pdev)
  713. {
  714. struct jz4780_dma_dev *jzdma = platform_get_drvdata(pdev);
  715. int i;
  716. of_dma_controller_free(pdev->dev.of_node);
  717. free_irq(jzdma->irq, jzdma);
  718. for (i = 0; i < JZ_DMA_NR_CHANNELS; i++)
  719. tasklet_kill(&jzdma->chan[i].vchan.task);
  720. dma_async_device_unregister(&jzdma->dma_device);
  721. return 0;
  722. }
  723. static const struct of_device_id jz4780_dma_dt_match[] = {
  724. { .compatible = "ingenic,jz4780-dma", .data = NULL },
  725. {},
  726. };
  727. MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match);
  728. static struct platform_driver jz4780_dma_driver = {
  729. .probe = jz4780_dma_probe,
  730. .remove = jz4780_dma_remove,
  731. .driver = {
  732. .name = "jz4780-dma",
  733. .of_match_table = of_match_ptr(jz4780_dma_dt_match),
  734. },
  735. };
  736. static int __init jz4780_dma_init(void)
  737. {
  738. return platform_driver_register(&jz4780_dma_driver);
  739. }
  740. subsys_initcall(jz4780_dma_init);
  741. static void __exit jz4780_dma_exit(void)
  742. {
  743. platform_driver_unregister(&jz4780_dma_driver);
  744. }
  745. module_exit(jz4780_dma_exit);
  746. MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
  747. MODULE_DESCRIPTION("Ingenic JZ4780 DMA controller driver");
  748. MODULE_LICENSE("GPL");