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. return NULL;
  280. desc->desc[i].dcm |= JZ_DMA_DCM_TIE;
  281. if (i != (sg_len - 1)) {
  282. /* Automatically proceeed to the next descriptor. */
  283. desc->desc[i].dcm |= JZ_DMA_DCM_LINK;
  284. /*
  285. * The upper 8 bits of the DTC field in the descriptor
  286. * must be set to (offset from descriptor base of next
  287. * descriptor >> 4).
  288. */
  289. desc->desc[i].dtc |=
  290. (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
  291. }
  292. }
  293. return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
  294. }
  295. static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic(
  296. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  297. size_t period_len, enum dma_transfer_direction direction,
  298. unsigned long flags)
  299. {
  300. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  301. struct jz4780_dma_desc *desc;
  302. unsigned int periods, i;
  303. int err;
  304. if (buf_len % period_len)
  305. return NULL;
  306. periods = buf_len / period_len;
  307. desc = jz4780_dma_desc_alloc(jzchan, periods, DMA_CYCLIC);
  308. if (!desc)
  309. return NULL;
  310. for (i = 0; i < periods; i++) {
  311. err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr,
  312. period_len, direction);
  313. if (err < 0)
  314. return NULL;
  315. buf_addr += period_len;
  316. /*
  317. * Set the link bit to indicate that the controller should
  318. * automatically proceed to the next descriptor. In
  319. * jz4780_dma_begin(), this will be cleared if we need to issue
  320. * an interrupt after each period.
  321. */
  322. desc->desc[i].dcm |= JZ_DMA_DCM_TIE | JZ_DMA_DCM_LINK;
  323. /*
  324. * The upper 8 bits of the DTC field in the descriptor must be
  325. * set to (offset from descriptor base of next descriptor >> 4).
  326. * If this is the last descriptor, link it back to the first,
  327. * i.e. leave offset set to 0, otherwise point to the next one.
  328. */
  329. if (i != (periods - 1)) {
  330. desc->desc[i].dtc |=
  331. (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
  332. }
  333. }
  334. return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
  335. }
  336. struct dma_async_tx_descriptor *jz4780_dma_prep_dma_memcpy(
  337. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  338. size_t len, unsigned long flags)
  339. {
  340. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  341. struct jz4780_dma_desc *desc;
  342. uint32_t tsz;
  343. desc = jz4780_dma_desc_alloc(jzchan, 1, DMA_MEMCPY);
  344. if (!desc)
  345. return NULL;
  346. tsz = jz4780_dma_transfer_size(dest | src | len,
  347. &jzchan->transfer_shift);
  348. desc->desc[0].dsa = src;
  349. desc->desc[0].dta = dest;
  350. desc->desc[0].drt = JZ_DMA_DRT_AUTO;
  351. desc->desc[0].dcm = JZ_DMA_DCM_TIE | JZ_DMA_DCM_SAI | JZ_DMA_DCM_DAI |
  352. tsz << JZ_DMA_DCM_TSZ_SHIFT |
  353. JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_SP_SHIFT |
  354. JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_DP_SHIFT;
  355. desc->desc[0].dtc = len >> jzchan->transfer_shift;
  356. return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
  357. }
  358. static void jz4780_dma_begin(struct jz4780_dma_chan *jzchan)
  359. {
  360. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  361. struct virt_dma_desc *vdesc;
  362. unsigned int i;
  363. dma_addr_t desc_phys;
  364. if (!jzchan->desc) {
  365. vdesc = vchan_next_desc(&jzchan->vchan);
  366. if (!vdesc)
  367. return;
  368. list_del(&vdesc->node);
  369. jzchan->desc = to_jz4780_dma_desc(vdesc);
  370. jzchan->curr_hwdesc = 0;
  371. if (jzchan->desc->type == DMA_CYCLIC && vdesc->tx.callback) {
  372. /*
  373. * The DMA controller doesn't support triggering an
  374. * interrupt after processing each descriptor, only
  375. * after processing an entire terminated list of
  376. * descriptors. For a cyclic DMA setup the list of
  377. * descriptors is not terminated so we can never get an
  378. * interrupt.
  379. *
  380. * If the user requested a callback for a cyclic DMA
  381. * setup then we workaround this hardware limitation
  382. * here by degrading to a set of unlinked descriptors
  383. * which we will submit in sequence in response to the
  384. * completion of processing the previous descriptor.
  385. */
  386. for (i = 0; i < jzchan->desc->count; i++)
  387. jzchan->desc->desc[i].dcm &= ~JZ_DMA_DCM_LINK;
  388. }
  389. } else {
  390. /*
  391. * There is an existing transfer, therefore this must be one
  392. * for which we unlinked the descriptors above. Advance to the
  393. * next one in the list.
  394. */
  395. jzchan->curr_hwdesc =
  396. (jzchan->curr_hwdesc + 1) % jzchan->desc->count;
  397. }
  398. /* Use 8-word descriptors. */
  399. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), JZ_DMA_DCS_DES8);
  400. /* Write descriptor address and initiate descriptor fetch. */
  401. desc_phys = jzchan->desc->desc_phys +
  402. (jzchan->curr_hwdesc * sizeof(*jzchan->desc->desc));
  403. jz4780_dma_writel(jzdma, JZ_DMA_REG_DDA(jzchan->id), desc_phys);
  404. jz4780_dma_writel(jzdma, JZ_DMA_REG_DDRS, BIT(jzchan->id));
  405. /* Enable the channel. */
  406. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id),
  407. JZ_DMA_DCS_DES8 | JZ_DMA_DCS_CTE);
  408. }
  409. static void jz4780_dma_issue_pending(struct dma_chan *chan)
  410. {
  411. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  412. unsigned long flags;
  413. spin_lock_irqsave(&jzchan->vchan.lock, flags);
  414. if (vchan_issue_pending(&jzchan->vchan) && !jzchan->desc)
  415. jz4780_dma_begin(jzchan);
  416. spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
  417. }
  418. static int jz4780_dma_terminate_all(struct dma_chan *chan)
  419. {
  420. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  421. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  422. unsigned long flags;
  423. LIST_HEAD(head);
  424. spin_lock_irqsave(&jzchan->vchan.lock, flags);
  425. /* Clear the DMA status and stop the transfer. */
  426. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), 0);
  427. if (jzchan->desc) {
  428. jz4780_dma_desc_free(&jzchan->desc->vdesc);
  429. jzchan->desc = NULL;
  430. }
  431. vchan_get_all_descriptors(&jzchan->vchan, &head);
  432. spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
  433. vchan_dma_desc_free_list(&jzchan->vchan, &head);
  434. return 0;
  435. }
  436. static int jz4780_dma_config(struct dma_chan *chan,
  437. struct dma_slave_config *config)
  438. {
  439. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  440. if ((config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  441. || (config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES))
  442. return -EINVAL;
  443. /* Copy the reset of the slave configuration, it is used later. */
  444. memcpy(&jzchan->config, config, sizeof(jzchan->config));
  445. return 0;
  446. }
  447. static size_t jz4780_dma_desc_residue(struct jz4780_dma_chan *jzchan,
  448. struct jz4780_dma_desc *desc, unsigned int next_sg)
  449. {
  450. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  451. unsigned int residue, count;
  452. unsigned int i;
  453. residue = 0;
  454. for (i = next_sg; i < desc->count; i++)
  455. residue += desc->desc[i].dtc << jzchan->transfer_shift;
  456. if (next_sg != 0) {
  457. count = jz4780_dma_readl(jzdma,
  458. JZ_DMA_REG_DTC(jzchan->id));
  459. residue += count << jzchan->transfer_shift;
  460. }
  461. return residue;
  462. }
  463. static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan,
  464. dma_cookie_t cookie, struct dma_tx_state *txstate)
  465. {
  466. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  467. struct virt_dma_desc *vdesc;
  468. enum dma_status status;
  469. unsigned long flags;
  470. status = dma_cookie_status(chan, cookie, txstate);
  471. if ((status == DMA_COMPLETE) || (txstate == NULL))
  472. return status;
  473. spin_lock_irqsave(&jzchan->vchan.lock, flags);
  474. vdesc = vchan_find_desc(&jzchan->vchan, cookie);
  475. if (vdesc) {
  476. /* On the issued list, so hasn't been processed yet */
  477. txstate->residue = jz4780_dma_desc_residue(jzchan,
  478. to_jz4780_dma_desc(vdesc), 0);
  479. } else if (cookie == jzchan->desc->vdesc.tx.cookie) {
  480. txstate->residue = jz4780_dma_desc_residue(jzchan, jzchan->desc,
  481. (jzchan->curr_hwdesc + 1) % jzchan->desc->count);
  482. } else
  483. txstate->residue = 0;
  484. if (vdesc && jzchan->desc && vdesc == &jzchan->desc->vdesc
  485. && jzchan->desc->status & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT))
  486. status = DMA_ERROR;
  487. spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
  488. return status;
  489. }
  490. static void jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
  491. struct jz4780_dma_chan *jzchan)
  492. {
  493. uint32_t dcs;
  494. spin_lock(&jzchan->vchan.lock);
  495. dcs = jz4780_dma_readl(jzdma, JZ_DMA_REG_DCS(jzchan->id));
  496. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), 0);
  497. if (dcs & JZ_DMA_DCS_AR) {
  498. dev_warn(&jzchan->vchan.chan.dev->device,
  499. "address error (DCS=0x%x)\n", dcs);
  500. }
  501. if (dcs & JZ_DMA_DCS_HLT) {
  502. dev_warn(&jzchan->vchan.chan.dev->device,
  503. "channel halt (DCS=0x%x)\n", dcs);
  504. }
  505. if (jzchan->desc) {
  506. jzchan->desc->status = dcs;
  507. if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
  508. if (jzchan->desc->type == DMA_CYCLIC) {
  509. vchan_cyclic_callback(&jzchan->desc->vdesc);
  510. } else {
  511. vchan_cookie_complete(&jzchan->desc->vdesc);
  512. jzchan->desc = NULL;
  513. }
  514. jz4780_dma_begin(jzchan);
  515. }
  516. } else {
  517. dev_err(&jzchan->vchan.chan.dev->device,
  518. "channel IRQ with no active transfer\n");
  519. }
  520. spin_unlock(&jzchan->vchan.lock);
  521. }
  522. static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
  523. {
  524. struct jz4780_dma_dev *jzdma = data;
  525. uint32_t pending, dmac;
  526. int i;
  527. pending = jz4780_dma_readl(jzdma, JZ_DMA_REG_DIRQP);
  528. for (i = 0; i < JZ_DMA_NR_CHANNELS; i++) {
  529. if (!(pending & (1<<i)))
  530. continue;
  531. jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]);
  532. }
  533. /* Clear halt and address error status of all channels. */
  534. dmac = jz4780_dma_readl(jzdma, JZ_DMA_REG_DMAC);
  535. dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
  536. jz4780_dma_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
  537. /* Clear interrupt pending status. */
  538. jz4780_dma_writel(jzdma, JZ_DMA_REG_DIRQP, 0);
  539. return IRQ_HANDLED;
  540. }
  541. static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
  542. {
  543. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  544. jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
  545. chan->device->dev,
  546. JZ_DMA_DESC_BLOCK_SIZE,
  547. PAGE_SIZE, 0);
  548. if (!jzchan->desc_pool) {
  549. dev_err(&chan->dev->device,
  550. "failed to allocate descriptor pool\n");
  551. return -ENOMEM;
  552. }
  553. return 0;
  554. }
  555. static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
  556. {
  557. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  558. vchan_free_chan_resources(&jzchan->vchan);
  559. dma_pool_destroy(jzchan->desc_pool);
  560. jzchan->desc_pool = NULL;
  561. }
  562. static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
  563. {
  564. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  565. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  566. struct jz4780_dma_filter_data *data = param;
  567. if (jzdma->dma_device.dev->of_node != data->of_node)
  568. return false;
  569. if (data->channel > -1) {
  570. if (data->channel != jzchan->id)
  571. return false;
  572. } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
  573. return false;
  574. }
  575. jzchan->transfer_type = data->transfer_type;
  576. return true;
  577. }
  578. static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
  579. struct of_dma *ofdma)
  580. {
  581. struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
  582. dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
  583. struct jz4780_dma_filter_data data;
  584. if (dma_spec->args_count != 2)
  585. return NULL;
  586. data.of_node = ofdma->of_node;
  587. data.transfer_type = dma_spec->args[0];
  588. data.channel = dma_spec->args[1];
  589. if (data.channel > -1) {
  590. if (data.channel >= JZ_DMA_NR_CHANNELS) {
  591. dev_err(jzdma->dma_device.dev,
  592. "device requested non-existent channel %u\n",
  593. data.channel);
  594. return NULL;
  595. }
  596. /* Can only select a channel marked as reserved. */
  597. if (!(jzdma->chan_reserved & BIT(data.channel))) {
  598. dev_err(jzdma->dma_device.dev,
  599. "device requested unreserved channel %u\n",
  600. data.channel);
  601. return NULL;
  602. }
  603. jzdma->chan[data.channel].transfer_type = data.transfer_type;
  604. return dma_get_slave_channel(
  605. &jzdma->chan[data.channel].vchan.chan);
  606. } else {
  607. return dma_request_channel(mask, jz4780_dma_filter_fn, &data);
  608. }
  609. }
  610. static int jz4780_dma_probe(struct platform_device *pdev)
  611. {
  612. struct device *dev = &pdev->dev;
  613. struct jz4780_dma_dev *jzdma;
  614. struct jz4780_dma_chan *jzchan;
  615. struct dma_device *dd;
  616. struct resource *res;
  617. int i, ret;
  618. if (!dev->of_node) {
  619. dev_err(dev, "This driver must be probed from devicetree\n");
  620. return -EINVAL;
  621. }
  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");