altera-msgdma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * DMA driver for Altera mSGDMA IP core
  3. *
  4. * Copyright (C) 2017 Stefan Roese <sr@denx.de>
  5. *
  6. * Based on drivers/dma/xilinx/zynqmp_dma.c, which is:
  7. * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/dmapool.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include "dmaengine.h"
  26. #define MSGDMA_MAX_TRANS_LEN U32_MAX
  27. #define MSGDMA_DESC_NUM 1024
  28. /**
  29. * struct msgdma_extended_desc - implements an extended descriptor
  30. * @read_addr_lo: data buffer source address low bits
  31. * @write_addr_lo: data buffer destination address low bits
  32. * @len: the number of bytes to transfer per descriptor
  33. * @burst_seq_num: bit 31:24 write burst
  34. * bit 23:16 read burst
  35. * bit 15:00 sequence number
  36. * @stride: bit 31:16 write stride
  37. * bit 15:00 read stride
  38. * @read_addr_hi: data buffer source address high bits
  39. * @write_addr_hi: data buffer destination address high bits
  40. * @control: characteristics of the transfer
  41. */
  42. struct msgdma_extended_desc {
  43. u32 read_addr_lo;
  44. u32 write_addr_lo;
  45. u32 len;
  46. u32 burst_seq_num;
  47. u32 stride;
  48. u32 read_addr_hi;
  49. u32 write_addr_hi;
  50. u32 control;
  51. };
  52. /* mSGDMA descriptor control field bit definitions */
  53. #define MSGDMA_DESC_CTL_SET_CH(x) ((x) & 0xff)
  54. #define MSGDMA_DESC_CTL_GEN_SOP BIT(8)
  55. #define MSGDMA_DESC_CTL_GEN_EOP BIT(9)
  56. #define MSGDMA_DESC_CTL_PARK_READS BIT(10)
  57. #define MSGDMA_DESC_CTL_PARK_WRITES BIT(11)
  58. #define MSGDMA_DESC_CTL_END_ON_EOP BIT(12)
  59. #define MSGDMA_DESC_CTL_END_ON_LEN BIT(13)
  60. #define MSGDMA_DESC_CTL_TR_COMP_IRQ BIT(14)
  61. #define MSGDMA_DESC_CTL_EARLY_IRQ BIT(15)
  62. #define MSGDMA_DESC_CTL_TR_ERR_IRQ GENMASK(23, 16)
  63. #define MSGDMA_DESC_CTL_EARLY_DONE BIT(24)
  64. /*
  65. * Writing "1" the "go" bit commits the entire descriptor into the
  66. * descriptor FIFO(s)
  67. */
  68. #define MSGDMA_DESC_CTL_GO BIT(31)
  69. /* Tx buffer control flags */
  70. #define MSGDMA_DESC_CTL_TX_FIRST (MSGDMA_DESC_CTL_GEN_SOP | \
  71. MSGDMA_DESC_CTL_TR_ERR_IRQ | \
  72. MSGDMA_DESC_CTL_GO)
  73. #define MSGDMA_DESC_CTL_TX_MIDDLE (MSGDMA_DESC_CTL_TR_ERR_IRQ | \
  74. MSGDMA_DESC_CTL_GO)
  75. #define MSGDMA_DESC_CTL_TX_LAST (MSGDMA_DESC_CTL_GEN_EOP | \
  76. MSGDMA_DESC_CTL_TR_COMP_IRQ | \
  77. MSGDMA_DESC_CTL_TR_ERR_IRQ | \
  78. MSGDMA_DESC_CTL_GO)
  79. #define MSGDMA_DESC_CTL_TX_SINGLE (MSGDMA_DESC_CTL_GEN_SOP | \
  80. MSGDMA_DESC_CTL_GEN_EOP | \
  81. MSGDMA_DESC_CTL_TR_COMP_IRQ | \
  82. MSGDMA_DESC_CTL_TR_ERR_IRQ | \
  83. MSGDMA_DESC_CTL_GO)
  84. #define MSGDMA_DESC_CTL_RX_SINGLE (MSGDMA_DESC_CTL_END_ON_EOP | \
  85. MSGDMA_DESC_CTL_END_ON_LEN | \
  86. MSGDMA_DESC_CTL_TR_COMP_IRQ | \
  87. MSGDMA_DESC_CTL_EARLY_IRQ | \
  88. MSGDMA_DESC_CTL_TR_ERR_IRQ | \
  89. MSGDMA_DESC_CTL_GO)
  90. /* mSGDMA extended descriptor stride definitions */
  91. #define MSGDMA_DESC_STRIDE_RD 0x00000001
  92. #define MSGDMA_DESC_STRIDE_WR 0x00010000
  93. #define MSGDMA_DESC_STRIDE_RW 0x00010001
  94. /* mSGDMA dispatcher control and status register map */
  95. #define MSGDMA_CSR_STATUS 0x00 /* Read / Clear */
  96. #define MSGDMA_CSR_CONTROL 0x04 /* Read / Write */
  97. #define MSGDMA_CSR_RW_FILL_LEVEL 0x08 /* 31:16 - write fill level */
  98. /* 15:00 - read fill level */
  99. #define MSGDMA_CSR_RESP_FILL_LEVEL 0x0c /* response FIFO fill level */
  100. #define MSGDMA_CSR_RW_SEQ_NUM 0x10 /* 31:16 - write seq number */
  101. /* 15:00 - read seq number */
  102. /* mSGDMA CSR status register bit definitions */
  103. #define MSGDMA_CSR_STAT_BUSY BIT(0)
  104. #define MSGDMA_CSR_STAT_DESC_BUF_EMPTY BIT(1)
  105. #define MSGDMA_CSR_STAT_DESC_BUF_FULL BIT(2)
  106. #define MSGDMA_CSR_STAT_RESP_BUF_EMPTY BIT(3)
  107. #define MSGDMA_CSR_STAT_RESP_BUF_FULL BIT(4)
  108. #define MSGDMA_CSR_STAT_STOPPED BIT(5)
  109. #define MSGDMA_CSR_STAT_RESETTING BIT(6)
  110. #define MSGDMA_CSR_STAT_STOPPED_ON_ERR BIT(7)
  111. #define MSGDMA_CSR_STAT_STOPPED_ON_EARLY BIT(8)
  112. #define MSGDMA_CSR_STAT_IRQ BIT(9)
  113. #define MSGDMA_CSR_STAT_MASK GENMASK(9, 0)
  114. #define MSGDMA_CSR_STAT_MASK_WITHOUT_IRQ GENMASK(8, 0)
  115. #define DESC_EMPTY (MSGDMA_CSR_STAT_DESC_BUF_EMPTY | \
  116. MSGDMA_CSR_STAT_RESP_BUF_EMPTY)
  117. /* mSGDMA CSR control register bit definitions */
  118. #define MSGDMA_CSR_CTL_STOP BIT(0)
  119. #define MSGDMA_CSR_CTL_RESET BIT(1)
  120. #define MSGDMA_CSR_CTL_STOP_ON_ERR BIT(2)
  121. #define MSGDMA_CSR_CTL_STOP_ON_EARLY BIT(3)
  122. #define MSGDMA_CSR_CTL_GLOBAL_INTR BIT(4)
  123. #define MSGDMA_CSR_CTL_STOP_DESCS BIT(5)
  124. /* mSGDMA CSR fill level bits */
  125. #define MSGDMA_CSR_WR_FILL_LEVEL_GET(v) (((v) & 0xffff0000) >> 16)
  126. #define MSGDMA_CSR_RD_FILL_LEVEL_GET(v) ((v) & 0x0000ffff)
  127. #define MSGDMA_CSR_RESP_FILL_LEVEL_GET(v) ((v) & 0x0000ffff)
  128. #define MSGDMA_CSR_SEQ_NUM_GET(v) (((v) & 0xffff0000) >> 16)
  129. /* mSGDMA response register map */
  130. #define MSGDMA_RESP_BYTES_TRANSFERRED 0x00
  131. #define MSGDMA_RESP_STATUS 0x04
  132. /* mSGDMA response register bit definitions */
  133. #define MSGDMA_RESP_EARLY_TERM BIT(8)
  134. #define MSGDMA_RESP_ERR_MASK 0xff
  135. /**
  136. * struct msgdma_sw_desc - implements a sw descriptor
  137. * @async_tx: support for the async_tx api
  138. * @hw_desc: assosiated HW descriptor
  139. * @free_list: node of the free SW descriprots list
  140. */
  141. struct msgdma_sw_desc {
  142. struct dma_async_tx_descriptor async_tx;
  143. struct msgdma_extended_desc hw_desc;
  144. struct list_head node;
  145. struct list_head tx_list;
  146. };
  147. /**
  148. * struct msgdma_device - DMA device structure
  149. */
  150. struct msgdma_device {
  151. spinlock_t lock;
  152. struct device *dev;
  153. struct tasklet_struct irq_tasklet;
  154. struct list_head pending_list;
  155. struct list_head free_list;
  156. struct list_head active_list;
  157. struct list_head done_list;
  158. u32 desc_free_cnt;
  159. bool idle;
  160. struct dma_device dmadev;
  161. struct dma_chan dmachan;
  162. dma_addr_t hw_desq;
  163. struct msgdma_sw_desc *sw_desq;
  164. unsigned int npendings;
  165. struct dma_slave_config slave_cfg;
  166. int irq;
  167. /* mSGDMA controller */
  168. void __iomem *csr;
  169. /* mSGDMA descriptors */
  170. void __iomem *desc;
  171. /* mSGDMA response */
  172. void __iomem *resp;
  173. };
  174. #define to_mdev(chan) container_of(chan, struct msgdma_device, dmachan)
  175. #define tx_to_desc(tx) container_of(tx, struct msgdma_sw_desc, async_tx)
  176. /**
  177. * msgdma_get_descriptor - Get the sw descriptor from the pool
  178. * @mdev: Pointer to the Altera mSGDMA device structure
  179. *
  180. * Return: The sw descriptor
  181. */
  182. static struct msgdma_sw_desc *msgdma_get_descriptor(struct msgdma_device *mdev)
  183. {
  184. struct msgdma_sw_desc *desc;
  185. spin_lock_bh(&mdev->lock);
  186. desc = list_first_entry(&mdev->free_list, struct msgdma_sw_desc, node);
  187. list_del(&desc->node);
  188. spin_unlock_bh(&mdev->lock);
  189. INIT_LIST_HEAD(&desc->tx_list);
  190. return desc;
  191. }
  192. /**
  193. * msgdma_free_descriptor - Issue pending transactions
  194. * @mdev: Pointer to the Altera mSGDMA device structure
  195. * @desc: Transaction descriptor pointer
  196. */
  197. static void msgdma_free_descriptor(struct msgdma_device *mdev,
  198. struct msgdma_sw_desc *desc)
  199. {
  200. struct msgdma_sw_desc *child, *next;
  201. mdev->desc_free_cnt++;
  202. list_add_tail(&desc->node, &mdev->free_list);
  203. list_for_each_entry_safe(child, next, &desc->tx_list, node) {
  204. mdev->desc_free_cnt++;
  205. list_move_tail(&child->node, &mdev->free_list);
  206. }
  207. }
  208. /**
  209. * msgdma_free_desc_list - Free descriptors list
  210. * @mdev: Pointer to the Altera mSGDMA device structure
  211. * @list: List to parse and delete the descriptor
  212. */
  213. static void msgdma_free_desc_list(struct msgdma_device *mdev,
  214. struct list_head *list)
  215. {
  216. struct msgdma_sw_desc *desc, *next;
  217. list_for_each_entry_safe(desc, next, list, node)
  218. msgdma_free_descriptor(mdev, desc);
  219. }
  220. /**
  221. * msgdma_desc_config - Configure the descriptor
  222. * @desc: Hw descriptor pointer
  223. * @dst: Destination buffer address
  224. * @src: Source buffer address
  225. * @len: Transfer length
  226. */
  227. static void msgdma_desc_config(struct msgdma_extended_desc *desc,
  228. dma_addr_t dst, dma_addr_t src, size_t len,
  229. u32 stride)
  230. {
  231. /* Set lower 32bits of src & dst addresses in the descriptor */
  232. desc->read_addr_lo = lower_32_bits(src);
  233. desc->write_addr_lo = lower_32_bits(dst);
  234. /* Set upper 32bits of src & dst addresses in the descriptor */
  235. desc->read_addr_hi = upper_32_bits(src);
  236. desc->write_addr_hi = upper_32_bits(dst);
  237. desc->len = len;
  238. desc->stride = stride;
  239. desc->burst_seq_num = 0; /* 0 will result in max burst length */
  240. /*
  241. * Don't set interrupt on xfer end yet, this will be done later
  242. * for the "last" descriptor
  243. */
  244. desc->control = MSGDMA_DESC_CTL_TR_ERR_IRQ | MSGDMA_DESC_CTL_GO |
  245. MSGDMA_DESC_CTL_END_ON_LEN;
  246. }
  247. /**
  248. * msgdma_desc_config_eod - Mark the descriptor as end descriptor
  249. * @desc: Hw descriptor pointer
  250. */
  251. static void msgdma_desc_config_eod(struct msgdma_extended_desc *desc)
  252. {
  253. desc->control |= MSGDMA_DESC_CTL_TR_COMP_IRQ;
  254. }
  255. /**
  256. * msgdma_tx_submit - Submit DMA transaction
  257. * @tx: Async transaction descriptor pointer
  258. *
  259. * Return: cookie value
  260. */
  261. static dma_cookie_t msgdma_tx_submit(struct dma_async_tx_descriptor *tx)
  262. {
  263. struct msgdma_device *mdev = to_mdev(tx->chan);
  264. struct msgdma_sw_desc *new;
  265. dma_cookie_t cookie;
  266. new = tx_to_desc(tx);
  267. spin_lock_bh(&mdev->lock);
  268. cookie = dma_cookie_assign(tx);
  269. list_add_tail(&new->node, &mdev->pending_list);
  270. spin_unlock_bh(&mdev->lock);
  271. return cookie;
  272. }
  273. /**
  274. * msgdma_prep_memcpy - prepare descriptors for memcpy transaction
  275. * @dchan: DMA channel
  276. * @dma_dst: Destination buffer address
  277. * @dma_src: Source buffer address
  278. * @len: Transfer length
  279. * @flags: transfer ack flags
  280. *
  281. * Return: Async transaction descriptor on success and NULL on failure
  282. */
  283. static struct dma_async_tx_descriptor *
  284. msgdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
  285. dma_addr_t dma_src, size_t len, ulong flags)
  286. {
  287. struct msgdma_device *mdev = to_mdev(dchan);
  288. struct msgdma_sw_desc *new, *first = NULL;
  289. struct msgdma_extended_desc *desc;
  290. size_t copy;
  291. u32 desc_cnt;
  292. desc_cnt = DIV_ROUND_UP(len, MSGDMA_MAX_TRANS_LEN);
  293. spin_lock_bh(&mdev->lock);
  294. if (desc_cnt > mdev->desc_free_cnt) {
  295. spin_unlock_bh(&mdev->lock);
  296. dev_dbg(mdev->dev, "mdev %p descs are not available\n", mdev);
  297. return NULL;
  298. }
  299. mdev->desc_free_cnt -= desc_cnt;
  300. spin_unlock_bh(&mdev->lock);
  301. do {
  302. /* Allocate and populate the descriptor */
  303. new = msgdma_get_descriptor(mdev);
  304. copy = min_t(size_t, len, MSGDMA_MAX_TRANS_LEN);
  305. desc = &new->hw_desc;
  306. msgdma_desc_config(desc, dma_dst, dma_src, copy,
  307. MSGDMA_DESC_STRIDE_RW);
  308. len -= copy;
  309. dma_src += copy;
  310. dma_dst += copy;
  311. if (!first)
  312. first = new;
  313. else
  314. list_add_tail(&new->node, &first->tx_list);
  315. } while (len);
  316. msgdma_desc_config_eod(desc);
  317. async_tx_ack(&first->async_tx);
  318. first->async_tx.flags = flags;
  319. return &first->async_tx;
  320. }
  321. /**
  322. * msgdma_prep_slave_sg - prepare descriptors for a slave sg transaction
  323. *
  324. * @dchan: DMA channel
  325. * @sgl: Destination scatter list
  326. * @sg_len: Number of entries in destination scatter list
  327. * @dir: DMA transfer direction
  328. * @flags: transfer ack flags
  329. * @context: transfer context (unused)
  330. */
  331. static struct dma_async_tx_descriptor *
  332. msgdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  333. unsigned int sg_len, enum dma_transfer_direction dir,
  334. unsigned long flags, void *context)
  335. {
  336. struct msgdma_device *mdev = to_mdev(dchan);
  337. struct dma_slave_config *cfg = &mdev->slave_cfg;
  338. struct msgdma_sw_desc *new, *first = NULL;
  339. void *desc = NULL;
  340. size_t len, avail;
  341. dma_addr_t dma_dst, dma_src;
  342. u32 desc_cnt = 0, i;
  343. struct scatterlist *sg;
  344. u32 stride;
  345. for_each_sg(sgl, sg, sg_len, i)
  346. desc_cnt += DIV_ROUND_UP(sg_dma_len(sg), MSGDMA_MAX_TRANS_LEN);
  347. spin_lock_bh(&mdev->lock);
  348. if (desc_cnt > mdev->desc_free_cnt) {
  349. spin_unlock_bh(&mdev->lock);
  350. dev_dbg(mdev->dev, "mdev %p descs are not available\n", mdev);
  351. return NULL;
  352. }
  353. mdev->desc_free_cnt -= desc_cnt;
  354. spin_unlock_bh(&mdev->lock);
  355. avail = sg_dma_len(sgl);
  356. /* Run until we are out of scatterlist entries */
  357. while (true) {
  358. /* Allocate and populate the descriptor */
  359. new = msgdma_get_descriptor(mdev);
  360. desc = &new->hw_desc;
  361. len = min_t(size_t, avail, MSGDMA_MAX_TRANS_LEN);
  362. if (dir == DMA_MEM_TO_DEV) {
  363. dma_src = sg_dma_address(sgl) + sg_dma_len(sgl) - avail;
  364. dma_dst = cfg->dst_addr;
  365. stride = MSGDMA_DESC_STRIDE_RD;
  366. } else {
  367. dma_src = cfg->src_addr;
  368. dma_dst = sg_dma_address(sgl) + sg_dma_len(sgl) - avail;
  369. stride = MSGDMA_DESC_STRIDE_WR;
  370. }
  371. msgdma_desc_config(desc, dma_dst, dma_src, len, stride);
  372. avail -= len;
  373. if (!first)
  374. first = new;
  375. else
  376. list_add_tail(&new->node, &first->tx_list);
  377. /* Fetch the next scatterlist entry */
  378. if (avail == 0) {
  379. if (sg_len == 0)
  380. break;
  381. sgl = sg_next(sgl);
  382. if (sgl == NULL)
  383. break;
  384. sg_len--;
  385. avail = sg_dma_len(sgl);
  386. }
  387. }
  388. msgdma_desc_config_eod(desc);
  389. first->async_tx.flags = flags;
  390. return &first->async_tx;
  391. }
  392. static int msgdma_dma_config(struct dma_chan *dchan,
  393. struct dma_slave_config *config)
  394. {
  395. struct msgdma_device *mdev = to_mdev(dchan);
  396. memcpy(&mdev->slave_cfg, config, sizeof(*config));
  397. return 0;
  398. }
  399. static void msgdma_reset(struct msgdma_device *mdev)
  400. {
  401. u32 val;
  402. int ret;
  403. /* Reset mSGDMA */
  404. iowrite32(MSGDMA_CSR_STAT_MASK, mdev->csr + MSGDMA_CSR_STATUS);
  405. iowrite32(MSGDMA_CSR_CTL_RESET, mdev->csr + MSGDMA_CSR_CONTROL);
  406. ret = readl_poll_timeout(mdev->csr + MSGDMA_CSR_STATUS, val,
  407. (val & MSGDMA_CSR_STAT_RESETTING) == 0,
  408. 1, 10000);
  409. if (ret)
  410. dev_err(mdev->dev, "DMA channel did not reset\n");
  411. /* Clear all status bits */
  412. iowrite32(MSGDMA_CSR_STAT_MASK, mdev->csr + MSGDMA_CSR_STATUS);
  413. /* Enable the DMA controller including interrupts */
  414. iowrite32(MSGDMA_CSR_CTL_STOP_ON_ERR | MSGDMA_CSR_CTL_STOP_ON_EARLY |
  415. MSGDMA_CSR_CTL_GLOBAL_INTR, mdev->csr + MSGDMA_CSR_CONTROL);
  416. mdev->idle = true;
  417. };
  418. static void msgdma_copy_one(struct msgdma_device *mdev,
  419. struct msgdma_sw_desc *desc)
  420. {
  421. void __iomem *hw_desc = mdev->desc;
  422. /*
  423. * Check if the DESC FIFO it not full. If its full, we need to wait
  424. * for at least one entry to become free again
  425. */
  426. while (ioread32(mdev->csr + MSGDMA_CSR_STATUS) &
  427. MSGDMA_CSR_STAT_DESC_BUF_FULL)
  428. mdelay(1);
  429. /*
  430. * The descriptor needs to get copied into the descriptor FIFO
  431. * of the DMA controller. The descriptor will get flushed to the
  432. * FIFO, once the last word (control word) is written. Since we
  433. * are not 100% sure that memcpy() writes all word in the "correct"
  434. * oder (address from low to high) on all architectures, we make
  435. * sure this control word is written last by single coding it and
  436. * adding some write-barriers here.
  437. */
  438. memcpy((void __force *)hw_desc, &desc->hw_desc,
  439. sizeof(desc->hw_desc) - sizeof(u32));
  440. /* Write control word last to flush this descriptor into the FIFO */
  441. mdev->idle = false;
  442. wmb();
  443. iowrite32(desc->hw_desc.control, hw_desc +
  444. offsetof(struct msgdma_extended_desc, control));
  445. wmb();
  446. }
  447. /**
  448. * msgdma_copy_desc_to_fifo - copy descriptor(s) into controller FIFO
  449. * @mdev: Pointer to the Altera mSGDMA device structure
  450. * @desc: Transaction descriptor pointer
  451. */
  452. static void msgdma_copy_desc_to_fifo(struct msgdma_device *mdev,
  453. struct msgdma_sw_desc *desc)
  454. {
  455. struct msgdma_sw_desc *sdesc, *next;
  456. msgdma_copy_one(mdev, desc);
  457. list_for_each_entry_safe(sdesc, next, &desc->tx_list, node)
  458. msgdma_copy_one(mdev, sdesc);
  459. }
  460. /**
  461. * msgdma_start_transfer - Initiate the new transfer
  462. * @mdev: Pointer to the Altera mSGDMA device structure
  463. */
  464. static void msgdma_start_transfer(struct msgdma_device *mdev)
  465. {
  466. struct msgdma_sw_desc *desc;
  467. if (!mdev->idle)
  468. return;
  469. desc = list_first_entry_or_null(&mdev->pending_list,
  470. struct msgdma_sw_desc, node);
  471. if (!desc)
  472. return;
  473. list_splice_tail_init(&mdev->pending_list, &mdev->active_list);
  474. msgdma_copy_desc_to_fifo(mdev, desc);
  475. }
  476. /**
  477. * msgdma_issue_pending - Issue pending transactions
  478. * @chan: DMA channel pointer
  479. */
  480. static void msgdma_issue_pending(struct dma_chan *chan)
  481. {
  482. struct msgdma_device *mdev = to_mdev(chan);
  483. spin_lock_bh(&mdev->lock);
  484. msgdma_start_transfer(mdev);
  485. spin_unlock_bh(&mdev->lock);
  486. }
  487. /**
  488. * msgdma_chan_desc_cleanup - Cleanup the completed descriptors
  489. * @mdev: Pointer to the Altera mSGDMA device structure
  490. */
  491. static void msgdma_chan_desc_cleanup(struct msgdma_device *mdev)
  492. {
  493. struct msgdma_sw_desc *desc, *next;
  494. list_for_each_entry_safe(desc, next, &mdev->done_list, node) {
  495. dma_async_tx_callback callback;
  496. void *callback_param;
  497. list_del(&desc->node);
  498. callback = desc->async_tx.callback;
  499. callback_param = desc->async_tx.callback_param;
  500. if (callback) {
  501. spin_unlock(&mdev->lock);
  502. callback(callback_param);
  503. spin_lock(&mdev->lock);
  504. }
  505. /* Run any dependencies, then free the descriptor */
  506. msgdma_free_descriptor(mdev, desc);
  507. }
  508. }
  509. /**
  510. * msgdma_complete_descriptor - Mark the active descriptor as complete
  511. * @mdev: Pointer to the Altera mSGDMA device structure
  512. */
  513. static void msgdma_complete_descriptor(struct msgdma_device *mdev)
  514. {
  515. struct msgdma_sw_desc *desc;
  516. desc = list_first_entry_or_null(&mdev->active_list,
  517. struct msgdma_sw_desc, node);
  518. if (!desc)
  519. return;
  520. list_del(&desc->node);
  521. dma_cookie_complete(&desc->async_tx);
  522. list_add_tail(&desc->node, &mdev->done_list);
  523. }
  524. /**
  525. * msgdma_free_descriptors - Free channel descriptors
  526. * @mdev: Pointer to the Altera mSGDMA device structure
  527. */
  528. static void msgdma_free_descriptors(struct msgdma_device *mdev)
  529. {
  530. msgdma_free_desc_list(mdev, &mdev->active_list);
  531. msgdma_free_desc_list(mdev, &mdev->pending_list);
  532. msgdma_free_desc_list(mdev, &mdev->done_list);
  533. }
  534. /**
  535. * msgdma_free_chan_resources - Free channel resources
  536. * @dchan: DMA channel pointer
  537. */
  538. static void msgdma_free_chan_resources(struct dma_chan *dchan)
  539. {
  540. struct msgdma_device *mdev = to_mdev(dchan);
  541. spin_lock_bh(&mdev->lock);
  542. msgdma_free_descriptors(mdev);
  543. spin_unlock_bh(&mdev->lock);
  544. kfree(mdev->sw_desq);
  545. }
  546. /**
  547. * msgdma_alloc_chan_resources - Allocate channel resources
  548. * @dchan: DMA channel
  549. *
  550. * Return: Number of descriptors on success and failure value on error
  551. */
  552. static int msgdma_alloc_chan_resources(struct dma_chan *dchan)
  553. {
  554. struct msgdma_device *mdev = to_mdev(dchan);
  555. struct msgdma_sw_desc *desc;
  556. int i;
  557. mdev->sw_desq = kcalloc(MSGDMA_DESC_NUM, sizeof(*desc), GFP_NOWAIT);
  558. if (!mdev->sw_desq)
  559. return -ENOMEM;
  560. mdev->idle = true;
  561. mdev->desc_free_cnt = MSGDMA_DESC_NUM;
  562. INIT_LIST_HEAD(&mdev->free_list);
  563. for (i = 0; i < MSGDMA_DESC_NUM; i++) {
  564. desc = mdev->sw_desq + i;
  565. dma_async_tx_descriptor_init(&desc->async_tx, &mdev->dmachan);
  566. desc->async_tx.tx_submit = msgdma_tx_submit;
  567. list_add_tail(&desc->node, &mdev->free_list);
  568. }
  569. return MSGDMA_DESC_NUM;
  570. }
  571. /**
  572. * msgdma_tasklet - Schedule completion tasklet
  573. * @data: Pointer to the Altera sSGDMA channel structure
  574. */
  575. static void msgdma_tasklet(unsigned long data)
  576. {
  577. struct msgdma_device *mdev = (struct msgdma_device *)data;
  578. u32 count;
  579. u32 __maybe_unused size;
  580. u32 __maybe_unused status;
  581. spin_lock(&mdev->lock);
  582. /* Read number of responses that are available */
  583. count = ioread32(mdev->csr + MSGDMA_CSR_RESP_FILL_LEVEL);
  584. dev_dbg(mdev->dev, "%s (%d): response count=%d\n",
  585. __func__, __LINE__, count);
  586. while (count--) {
  587. /*
  588. * Read both longwords to purge this response from the FIFO
  589. * On Avalon-MM implementations, size and status do not
  590. * have any real values, like transferred bytes or error
  591. * bits. So we need to just drop these values.
  592. */
  593. size = ioread32(mdev->resp + MSGDMA_RESP_BYTES_TRANSFERRED);
  594. status = ioread32(mdev->resp - MSGDMA_RESP_STATUS);
  595. msgdma_complete_descriptor(mdev);
  596. msgdma_chan_desc_cleanup(mdev);
  597. }
  598. spin_unlock(&mdev->lock);
  599. }
  600. /**
  601. * msgdma_irq_handler - Altera mSGDMA Interrupt handler
  602. * @irq: IRQ number
  603. * @data: Pointer to the Altera mSGDMA device structure
  604. *
  605. * Return: IRQ_HANDLED/IRQ_NONE
  606. */
  607. static irqreturn_t msgdma_irq_handler(int irq, void *data)
  608. {
  609. struct msgdma_device *mdev = data;
  610. u32 status;
  611. status = ioread32(mdev->csr + MSGDMA_CSR_STATUS);
  612. if ((status & MSGDMA_CSR_STAT_BUSY) == 0) {
  613. /* Start next transfer if the DMA controller is idle */
  614. spin_lock(&mdev->lock);
  615. mdev->idle = true;
  616. msgdma_start_transfer(mdev);
  617. spin_unlock(&mdev->lock);
  618. }
  619. tasklet_schedule(&mdev->irq_tasklet);
  620. /* Clear interrupt in mSGDMA controller */
  621. iowrite32(MSGDMA_CSR_STAT_IRQ, mdev->csr + MSGDMA_CSR_STATUS);
  622. return IRQ_HANDLED;
  623. }
  624. /**
  625. * msgdma_chan_remove - Channel remove function
  626. * @mdev: Pointer to the Altera mSGDMA device structure
  627. */
  628. static void msgdma_dev_remove(struct msgdma_device *mdev)
  629. {
  630. if (!mdev)
  631. return;
  632. devm_free_irq(mdev->dev, mdev->irq, mdev);
  633. tasklet_kill(&mdev->irq_tasklet);
  634. list_del(&mdev->dmachan.device_node);
  635. }
  636. static int request_and_map(struct platform_device *pdev, const char *name,
  637. struct resource **res, void __iomem **ptr)
  638. {
  639. struct resource *region;
  640. struct device *device = &pdev->dev;
  641. *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  642. if (*res == NULL) {
  643. dev_err(device, "resource %s not defined\n", name);
  644. return -ENODEV;
  645. }
  646. region = devm_request_mem_region(device, (*res)->start,
  647. resource_size(*res), dev_name(device));
  648. if (region == NULL) {
  649. dev_err(device, "unable to request %s\n", name);
  650. return -EBUSY;
  651. }
  652. *ptr = devm_ioremap_nocache(device, region->start,
  653. resource_size(region));
  654. if (*ptr == NULL) {
  655. dev_err(device, "ioremap_nocache of %s failed!", name);
  656. return -ENOMEM;
  657. }
  658. return 0;
  659. }
  660. /**
  661. * msgdma_probe - Driver probe function
  662. * @pdev: Pointer to the platform_device structure
  663. *
  664. * Return: '0' on success and failure value on error
  665. */
  666. static int msgdma_probe(struct platform_device *pdev)
  667. {
  668. struct msgdma_device *mdev;
  669. struct dma_device *dma_dev;
  670. struct resource *dma_res;
  671. int ret;
  672. mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_NOWAIT);
  673. if (!mdev)
  674. return -ENOMEM;
  675. mdev->dev = &pdev->dev;
  676. /* Map CSR space */
  677. ret = request_and_map(pdev, "csr", &dma_res, &mdev->csr);
  678. if (ret)
  679. return ret;
  680. /* Map (extended) descriptor space */
  681. ret = request_and_map(pdev, "desc", &dma_res, &mdev->desc);
  682. if (ret)
  683. return ret;
  684. /* Map response space */
  685. ret = request_and_map(pdev, "resp", &dma_res, &mdev->resp);
  686. if (ret)
  687. return ret;
  688. platform_set_drvdata(pdev, mdev);
  689. /* Get interrupt nr from platform data */
  690. mdev->irq = platform_get_irq(pdev, 0);
  691. if (mdev->irq < 0)
  692. return -ENXIO;
  693. ret = devm_request_irq(&pdev->dev, mdev->irq, msgdma_irq_handler,
  694. 0, dev_name(&pdev->dev), mdev);
  695. if (ret)
  696. return ret;
  697. tasklet_init(&mdev->irq_tasklet, msgdma_tasklet, (unsigned long)mdev);
  698. dma_cookie_init(&mdev->dmachan);
  699. spin_lock_init(&mdev->lock);
  700. INIT_LIST_HEAD(&mdev->active_list);
  701. INIT_LIST_HEAD(&mdev->pending_list);
  702. INIT_LIST_HEAD(&mdev->done_list);
  703. INIT_LIST_HEAD(&mdev->free_list);
  704. dma_dev = &mdev->dmadev;
  705. /* Set DMA capabilities */
  706. dma_cap_zero(dma_dev->cap_mask);
  707. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  708. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  709. dma_dev->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  710. dma_dev->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  711. dma_dev->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM) |
  712. BIT(DMA_MEM_TO_MEM);
  713. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  714. /* Init DMA link list */
  715. INIT_LIST_HEAD(&dma_dev->channels);
  716. /* Set base routines */
  717. dma_dev->device_tx_status = dma_cookie_status;
  718. dma_dev->device_issue_pending = msgdma_issue_pending;
  719. dma_dev->dev = &pdev->dev;
  720. dma_dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
  721. dma_dev->device_prep_dma_memcpy = msgdma_prep_memcpy;
  722. dma_dev->device_prep_slave_sg = msgdma_prep_slave_sg;
  723. dma_dev->device_config = msgdma_dma_config;
  724. dma_dev->device_alloc_chan_resources = msgdma_alloc_chan_resources;
  725. dma_dev->device_free_chan_resources = msgdma_free_chan_resources;
  726. mdev->dmachan.device = dma_dev;
  727. list_add_tail(&mdev->dmachan.device_node, &dma_dev->channels);
  728. /* Set DMA mask to 64 bits */
  729. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  730. if (ret) {
  731. dev_warn(&pdev->dev, "unable to set coherent mask to 64");
  732. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  733. if (ret)
  734. goto fail;
  735. }
  736. msgdma_reset(mdev);
  737. ret = dma_async_device_register(dma_dev);
  738. if (ret)
  739. goto fail;
  740. dev_notice(&pdev->dev, "Altera mSGDMA driver probe success\n");
  741. return 0;
  742. fail:
  743. msgdma_dev_remove(mdev);
  744. return ret;
  745. }
  746. /**
  747. * msgdma_dma_remove - Driver remove function
  748. * @pdev: Pointer to the platform_device structure
  749. *
  750. * Return: Always '0'
  751. */
  752. static int msgdma_remove(struct platform_device *pdev)
  753. {
  754. struct msgdma_device *mdev = platform_get_drvdata(pdev);
  755. dma_async_device_unregister(&mdev->dmadev);
  756. msgdma_dev_remove(mdev);
  757. dev_notice(&pdev->dev, "Altera mSGDMA driver removed\n");
  758. return 0;
  759. }
  760. static struct platform_driver msgdma_driver = {
  761. .driver = {
  762. .name = "altera-msgdma",
  763. },
  764. .probe = msgdma_probe,
  765. .remove = msgdma_remove,
  766. };
  767. module_platform_driver(msgdma_driver);
  768. MODULE_ALIAS("platform:altera-msgdma");
  769. MODULE_DESCRIPTION("Altera mSGDMA driver");
  770. MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
  771. MODULE_LICENSE("GPL");