altera-msgdma.c 26 KB

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