dma-jz4780.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. spin_lock_irqsave(&jzchan->vchan.lock, flags);
  475. status = dma_cookie_status(chan, cookie, txstate);
  476. if ((status == DMA_COMPLETE) || (txstate == NULL))
  477. goto out_unlock_irqrestore;
  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);
  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. out_unlock_irqrestore:
  492. spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
  493. return status;
  494. }
  495. static void jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
  496. struct jz4780_dma_chan *jzchan)
  497. {
  498. uint32_t dcs;
  499. spin_lock(&jzchan->vchan.lock);
  500. dcs = jz4780_dma_readl(jzdma, JZ_DMA_REG_DCS(jzchan->id));
  501. jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), 0);
  502. if (dcs & JZ_DMA_DCS_AR) {
  503. dev_warn(&jzchan->vchan.chan.dev->device,
  504. "address error (DCS=0x%x)\n", dcs);
  505. }
  506. if (dcs & JZ_DMA_DCS_HLT) {
  507. dev_warn(&jzchan->vchan.chan.dev->device,
  508. "channel halt (DCS=0x%x)\n", dcs);
  509. }
  510. if (jzchan->desc) {
  511. jzchan->desc->status = dcs;
  512. if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
  513. if (jzchan->desc->type == DMA_CYCLIC) {
  514. vchan_cyclic_callback(&jzchan->desc->vdesc);
  515. } else {
  516. vchan_cookie_complete(&jzchan->desc->vdesc);
  517. jzchan->desc = NULL;
  518. }
  519. jz4780_dma_begin(jzchan);
  520. }
  521. } else {
  522. dev_err(&jzchan->vchan.chan.dev->device,
  523. "channel IRQ with no active transfer\n");
  524. }
  525. spin_unlock(&jzchan->vchan.lock);
  526. }
  527. static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
  528. {
  529. struct jz4780_dma_dev *jzdma = data;
  530. uint32_t pending, dmac;
  531. int i;
  532. pending = jz4780_dma_readl(jzdma, JZ_DMA_REG_DIRQP);
  533. for (i = 0; i < JZ_DMA_NR_CHANNELS; i++) {
  534. if (!(pending & (1<<i)))
  535. continue;
  536. jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]);
  537. }
  538. /* Clear halt and address error status of all channels. */
  539. dmac = jz4780_dma_readl(jzdma, JZ_DMA_REG_DMAC);
  540. dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
  541. jz4780_dma_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
  542. /* Clear interrupt pending status. */
  543. jz4780_dma_writel(jzdma, JZ_DMA_REG_DIRQP, 0);
  544. return IRQ_HANDLED;
  545. }
  546. static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
  547. {
  548. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  549. jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
  550. chan->device->dev,
  551. JZ_DMA_DESC_BLOCK_SIZE,
  552. PAGE_SIZE, 0);
  553. if (!jzchan->desc_pool) {
  554. dev_err(&chan->dev->device,
  555. "failed to allocate descriptor pool\n");
  556. return -ENOMEM;
  557. }
  558. return 0;
  559. }
  560. static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
  561. {
  562. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  563. vchan_free_chan_resources(&jzchan->vchan);
  564. dma_pool_destroy(jzchan->desc_pool);
  565. jzchan->desc_pool = NULL;
  566. }
  567. static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
  568. {
  569. struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
  570. struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
  571. struct jz4780_dma_filter_data *data = param;
  572. if (jzdma->dma_device.dev->of_node != data->of_node)
  573. return false;
  574. if (data->channel > -1) {
  575. if (data->channel != jzchan->id)
  576. return false;
  577. } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
  578. return false;
  579. }
  580. jzchan->transfer_type = data->transfer_type;
  581. return true;
  582. }
  583. static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
  584. struct of_dma *ofdma)
  585. {
  586. struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
  587. dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
  588. struct jz4780_dma_filter_data data;
  589. if (dma_spec->args_count != 2)
  590. return NULL;
  591. data.of_node = ofdma->of_node;
  592. data.transfer_type = dma_spec->args[0];
  593. data.channel = dma_spec->args[1];
  594. if (data.channel > -1) {
  595. if (data.channel >= JZ_DMA_NR_CHANNELS) {
  596. dev_err(jzdma->dma_device.dev,
  597. "device requested non-existent channel %u\n",
  598. data.channel);
  599. return NULL;
  600. }
  601. /* Can only select a channel marked as reserved. */
  602. if (!(jzdma->chan_reserved & BIT(data.channel))) {
  603. dev_err(jzdma->dma_device.dev,
  604. "device requested unreserved channel %u\n",
  605. data.channel);
  606. return NULL;
  607. }
  608. jzdma->chan[data.channel].transfer_type = data.transfer_type;
  609. return dma_get_slave_channel(
  610. &jzdma->chan[data.channel].vchan.chan);
  611. } else {
  612. return dma_request_channel(mask, jz4780_dma_filter_fn, &data);
  613. }
  614. }
  615. static int jz4780_dma_probe(struct platform_device *pdev)
  616. {
  617. struct device *dev = &pdev->dev;
  618. struct jz4780_dma_dev *jzdma;
  619. struct jz4780_dma_chan *jzchan;
  620. struct dma_device *dd;
  621. struct resource *res;
  622. int i, ret;
  623. if (!dev->of_node) {
  624. dev_err(dev, "This driver must be probed from devicetree\n");
  625. return -EINVAL;
  626. }
  627. jzdma = devm_kzalloc(dev, sizeof(*jzdma), GFP_KERNEL);
  628. if (!jzdma)
  629. return -ENOMEM;
  630. platform_set_drvdata(pdev, jzdma);
  631. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  632. if (!res) {
  633. dev_err(dev, "failed to get I/O memory\n");
  634. return -EINVAL;
  635. }
  636. jzdma->base = devm_ioremap_resource(dev, res);
  637. if (IS_ERR(jzdma->base))
  638. return PTR_ERR(jzdma->base);
  639. ret = platform_get_irq(pdev, 0);
  640. if (ret < 0) {
  641. dev_err(dev, "failed to get IRQ: %d\n", ret);
  642. return ret;
  643. }
  644. jzdma->irq = ret;
  645. ret = request_irq(jzdma->irq, jz4780_dma_irq_handler, 0, dev_name(dev),
  646. jzdma);
  647. if (ret) {
  648. dev_err(dev, "failed to request IRQ %u!\n", jzdma->irq);
  649. return ret;
  650. }
  651. jzdma->clk = devm_clk_get(dev, NULL);
  652. if (IS_ERR(jzdma->clk)) {
  653. dev_err(dev, "failed to get clock\n");
  654. ret = PTR_ERR(jzdma->clk);
  655. goto err_free_irq;
  656. }
  657. clk_prepare_enable(jzdma->clk);
  658. /* Property is optional, if it doesn't exist the value will remain 0. */
  659. of_property_read_u32_index(dev->of_node, "ingenic,reserved-channels",
  660. 0, &jzdma->chan_reserved);
  661. dd = &jzdma->dma_device;
  662. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  663. dma_cap_set(DMA_SLAVE, dd->cap_mask);
  664. dma_cap_set(DMA_CYCLIC, dd->cap_mask);
  665. dd->dev = dev;
  666. dd->copy_align = DMAENGINE_ALIGN_4_BYTES;
  667. dd->device_alloc_chan_resources = jz4780_dma_alloc_chan_resources;
  668. dd->device_free_chan_resources = jz4780_dma_free_chan_resources;
  669. dd->device_prep_slave_sg = jz4780_dma_prep_slave_sg;
  670. dd->device_prep_dma_cyclic = jz4780_dma_prep_dma_cyclic;
  671. dd->device_prep_dma_memcpy = jz4780_dma_prep_dma_memcpy;
  672. dd->device_config = jz4780_dma_config;
  673. dd->device_terminate_all = jz4780_dma_terminate_all;
  674. dd->device_tx_status = jz4780_dma_tx_status;
  675. dd->device_issue_pending = jz4780_dma_issue_pending;
  676. dd->src_addr_widths = JZ_DMA_BUSWIDTHS;
  677. dd->dst_addr_widths = JZ_DMA_BUSWIDTHS;
  678. dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  679. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  680. /*
  681. * Enable DMA controller, mark all channels as not programmable.
  682. * Also set the FMSC bit - it increases MSC performance, so it makes
  683. * little sense not to enable it.
  684. */
  685. jz4780_dma_writel(jzdma, JZ_DMA_REG_DMAC,
  686. JZ_DMA_DMAC_DMAE | JZ_DMA_DMAC_FMSC);
  687. jz4780_dma_writel(jzdma, JZ_DMA_REG_DMACP, 0);
  688. INIT_LIST_HEAD(&dd->channels);
  689. for (i = 0; i < JZ_DMA_NR_CHANNELS; i++) {
  690. jzchan = &jzdma->chan[i];
  691. jzchan->id = i;
  692. vchan_init(&jzchan->vchan, dd);
  693. jzchan->vchan.desc_free = jz4780_dma_desc_free;
  694. }
  695. ret = dma_async_device_register(dd);
  696. if (ret) {
  697. dev_err(dev, "failed to register device\n");
  698. goto err_disable_clk;
  699. }
  700. /* Register with OF DMA helpers. */
  701. ret = of_dma_controller_register(dev->of_node, jz4780_of_dma_xlate,
  702. jzdma);
  703. if (ret) {
  704. dev_err(dev, "failed to register OF DMA controller\n");
  705. goto err_unregister_dev;
  706. }
  707. dev_info(dev, "JZ4780 DMA controller initialised\n");
  708. return 0;
  709. err_unregister_dev:
  710. dma_async_device_unregister(dd);
  711. err_disable_clk:
  712. clk_disable_unprepare(jzdma->clk);
  713. err_free_irq:
  714. free_irq(jzdma->irq, jzdma);
  715. return ret;
  716. }
  717. static int jz4780_dma_remove(struct platform_device *pdev)
  718. {
  719. struct jz4780_dma_dev *jzdma = platform_get_drvdata(pdev);
  720. int i;
  721. of_dma_controller_free(pdev->dev.of_node);
  722. free_irq(jzdma->irq, jzdma);
  723. for (i = 0; i < JZ_DMA_NR_CHANNELS; i++)
  724. tasklet_kill(&jzdma->chan[i].vchan.task);
  725. dma_async_device_unregister(&jzdma->dma_device);
  726. return 0;
  727. }
  728. static const struct of_device_id jz4780_dma_dt_match[] = {
  729. { .compatible = "ingenic,jz4780-dma", .data = NULL },
  730. {},
  731. };
  732. MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match);
  733. static struct platform_driver jz4780_dma_driver = {
  734. .probe = jz4780_dma_probe,
  735. .remove = jz4780_dma_remove,
  736. .driver = {
  737. .name = "jz4780-dma",
  738. .of_match_table = of_match_ptr(jz4780_dma_dt_match),
  739. },
  740. };
  741. static int __init jz4780_dma_init(void)
  742. {
  743. return platform_driver_register(&jz4780_dma_driver);
  744. }
  745. subsys_initcall(jz4780_dma_init);
  746. static void __exit jz4780_dma_exit(void)
  747. {
  748. platform_driver_unregister(&jz4780_dma_driver);
  749. }
  750. module_exit(jz4780_dma_exit);
  751. MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
  752. MODULE_DESCRIPTION("Ingenic JZ4780 DMA controller driver");
  753. MODULE_LICENSE("GPL");