altera-msgdma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 <[email protected]>
  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: assosiated 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_add_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. * oder (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. list_for_each_entry_safe(desc, next, &mdev->done_list, node) {
  499. struct dmaengine_desc_callback cb;
  500. list_del(&desc->node);
  501. dmaengine_desc_get_callback(&desc->async_tx, &cb);
  502. if (dmaengine_desc_callback_valid(&cb)) {
  503. spin_unlock(&mdev->lock);
  504. dmaengine_desc_callback_invoke(&cb, NULL);
  505. spin_lock(&mdev->lock);
  506. }
  507. /* Run any dependencies, then free the descriptor */
  508. msgdma_free_descriptor(mdev, desc);
  509. }
  510. }
  511. /**
  512. * msgdma_complete_descriptor - Mark the active descriptor as complete
  513. * @mdev: Pointer to the Altera mSGDMA device structure
  514. */
  515. static void msgdma_complete_descriptor(struct msgdma_device *mdev)
  516. {
  517. struct msgdma_sw_desc *desc;
  518. desc = list_first_entry_or_null(&mdev->active_list,
  519. struct msgdma_sw_desc, node);
  520. if (!desc)
  521. return;
  522. list_del(&desc->node);
  523. dma_cookie_complete(&desc->async_tx);
  524. list_add_tail(&desc->node, &mdev->done_list);
  525. }
  526. /**
  527. * msgdma_free_descriptors - Free channel descriptors
  528. * @mdev: Pointer to the Altera mSGDMA device structure
  529. */
  530. static void msgdma_free_descriptors(struct msgdma_device *mdev)
  531. {
  532. msgdma_free_desc_list(mdev, &mdev->active_list);
  533. msgdma_free_desc_list(mdev, &mdev->pending_list);
  534. msgdma_free_desc_list(mdev, &mdev->done_list);
  535. }
  536. /**
  537. * msgdma_free_chan_resources - Free channel resources
  538. * @dchan: DMA channel pointer
  539. */
  540. static void msgdma_free_chan_resources(struct dma_chan *dchan)
  541. {
  542. struct msgdma_device *mdev = to_mdev(dchan);
  543. unsigned long flags;
  544. spin_lock_irqsave(&mdev->lock, flags);
  545. msgdma_free_descriptors(mdev);
  546. spin_unlock_irqrestore(&mdev->lock, flags);
  547. kfree(mdev->sw_desq);
  548. }
  549. /**
  550. * msgdma_alloc_chan_resources - Allocate channel resources
  551. * @dchan: DMA channel
  552. *
  553. * Return: Number of descriptors on success and failure value on error
  554. */
  555. static int msgdma_alloc_chan_resources(struct dma_chan *dchan)
  556. {
  557. struct msgdma_device *mdev = to_mdev(dchan);
  558. struct msgdma_sw_desc *desc;
  559. int i;
  560. mdev->sw_desq = kcalloc(MSGDMA_DESC_NUM, sizeof(*desc), GFP_NOWAIT);
  561. if (!mdev->sw_desq)
  562. return -ENOMEM;
  563. mdev->idle = true;
  564. mdev->desc_free_cnt = MSGDMA_DESC_NUM;
  565. INIT_LIST_HEAD(&mdev->free_list);
  566. for (i = 0; i < MSGDMA_DESC_NUM; i++) {
  567. desc = mdev->sw_desq + i;
  568. dma_async_tx_descriptor_init(&desc->async_tx, &mdev->dmachan);
  569. desc->async_tx.tx_submit = msgdma_tx_submit;
  570. list_add_tail(&desc->node, &mdev->free_list);
  571. }
  572. return MSGDMA_DESC_NUM;
  573. }
  574. /**
  575. * msgdma_tasklet - Schedule completion tasklet
  576. * @t: Pointer to the Altera sSGDMA channel structure
  577. */
  578. static void msgdma_tasklet(struct tasklet_struct *t)
  579. {
  580. struct msgdma_device *mdev = from_tasklet(mdev, t, irq_tasklet);
  581. u32 count;
  582. u32 __maybe_unused size;
  583. u32 __maybe_unused status;
  584. unsigned long flags;
  585. spin_lock_irqsave(&mdev->lock, flags);
  586. if (mdev->resp) {
  587. /* Read number of responses that are available */
  588. count = ioread32(mdev->csr + MSGDMA_CSR_RESP_FILL_LEVEL);
  589. dev_dbg(mdev->dev, "%s (%d): response count=%d\n",
  590. __func__, __LINE__, count);
  591. } else {
  592. count = 1;
  593. }
  594. while (count--) {
  595. /*
  596. * Read both longwords to purge this response from the FIFO
  597. * On Avalon-MM implementations, size and status do not
  598. * have any real values, like transferred bytes or error
  599. * bits. So we need to just drop these values.
  600. */
  601. if (mdev->resp) {
  602. size = ioread32(mdev->resp +
  603. MSGDMA_RESP_BYTES_TRANSFERRED);
  604. status = ioread32(mdev->resp +
  605. MSGDMA_RESP_STATUS);
  606. }
  607. msgdma_complete_descriptor(mdev);
  608. msgdma_chan_desc_cleanup(mdev);
  609. }
  610. spin_unlock_irqrestore(&mdev->lock, flags);
  611. }
  612. /**
  613. * msgdma_irq_handler - Altera mSGDMA Interrupt handler
  614. * @irq: IRQ number
  615. * @data: Pointer to the Altera mSGDMA device structure
  616. *
  617. * Return: IRQ_HANDLED/IRQ_NONE
  618. */
  619. static irqreturn_t msgdma_irq_handler(int irq, void *data)
  620. {
  621. struct msgdma_device *mdev = data;
  622. u32 status;
  623. status = ioread32(mdev->csr + MSGDMA_CSR_STATUS);
  624. if ((status & MSGDMA_CSR_STAT_BUSY) == 0) {
  625. /* Start next transfer if the DMA controller is idle */
  626. spin_lock(&mdev->lock);
  627. mdev->idle = true;
  628. msgdma_start_transfer(mdev);
  629. spin_unlock(&mdev->lock);
  630. }
  631. tasklet_schedule(&mdev->irq_tasklet);
  632. /* Clear interrupt in mSGDMA controller */
  633. iowrite32(MSGDMA_CSR_STAT_IRQ, mdev->csr + MSGDMA_CSR_STATUS);
  634. return IRQ_HANDLED;
  635. }
  636. /**
  637. * msgdma_dev_remove() - Device remove function
  638. * @mdev: Pointer to the Altera mSGDMA device structure
  639. */
  640. static void msgdma_dev_remove(struct msgdma_device *mdev)
  641. {
  642. if (!mdev)
  643. return;
  644. devm_free_irq(mdev->dev, mdev->irq, mdev);
  645. tasklet_kill(&mdev->irq_tasklet);
  646. list_del(&mdev->dmachan.device_node);
  647. }
  648. static int request_and_map(struct platform_device *pdev, const char *name,
  649. struct resource **res, void __iomem **ptr,
  650. bool optional)
  651. {
  652. struct resource *region;
  653. struct device *device = &pdev->dev;
  654. *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  655. if (*res == NULL) {
  656. if (optional) {
  657. *ptr = NULL;
  658. dev_info(device, "optional resource %s not defined\n",
  659. name);
  660. return 0;
  661. }
  662. dev_err(device, "mandatory resource %s not defined\n", name);
  663. return -ENODEV;
  664. }
  665. region = devm_request_mem_region(device, (*res)->start,
  666. resource_size(*res), dev_name(device));
  667. if (region == NULL) {
  668. dev_err(device, "unable to request %s\n", name);
  669. return -EBUSY;
  670. }
  671. *ptr = devm_ioremap(device, region->start,
  672. resource_size(region));
  673. if (*ptr == NULL) {
  674. dev_err(device, "ioremap of %s failed!", name);
  675. return -ENOMEM;
  676. }
  677. return 0;
  678. }
  679. /**
  680. * msgdma_probe - Driver probe function
  681. * @pdev: Pointer to the platform_device structure
  682. *
  683. * Return: '0' on success and failure value on error
  684. */
  685. static int msgdma_probe(struct platform_device *pdev)
  686. {
  687. struct msgdma_device *mdev;
  688. struct dma_device *dma_dev;
  689. struct resource *dma_res;
  690. int ret;
  691. mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_NOWAIT);
  692. if (!mdev)
  693. return -ENOMEM;
  694. mdev->dev = &pdev->dev;
  695. /* Map CSR space */
  696. ret = request_and_map(pdev, "csr", &dma_res, &mdev->csr, false);
  697. if (ret)
  698. return ret;
  699. /* Map (extended) descriptor space */
  700. ret = request_and_map(pdev, "desc", &dma_res, &mdev->desc, false);
  701. if (ret)
  702. return ret;
  703. /* Map response space */
  704. ret = request_and_map(pdev, "resp", &dma_res, &mdev->resp, true);
  705. if (ret)
  706. return ret;
  707. platform_set_drvdata(pdev, mdev);
  708. /* Get interrupt nr from platform data */
  709. mdev->irq = platform_get_irq(pdev, 0);
  710. if (mdev->irq < 0)
  711. return -ENXIO;
  712. ret = devm_request_irq(&pdev->dev, mdev->irq, msgdma_irq_handler,
  713. 0, dev_name(&pdev->dev), mdev);
  714. if (ret)
  715. return ret;
  716. tasklet_setup(&mdev->irq_tasklet, msgdma_tasklet);
  717. dma_cookie_init(&mdev->dmachan);
  718. spin_lock_init(&mdev->lock);
  719. INIT_LIST_HEAD(&mdev->active_list);
  720. INIT_LIST_HEAD(&mdev->pending_list);
  721. INIT_LIST_HEAD(&mdev->done_list);
  722. INIT_LIST_HEAD(&mdev->free_list);
  723. dma_dev = &mdev->dmadev;
  724. /* Set DMA capabilities */
  725. dma_cap_zero(dma_dev->cap_mask);
  726. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  727. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  728. dma_dev->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  729. dma_dev->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  730. dma_dev->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM) |
  731. BIT(DMA_MEM_TO_MEM);
  732. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  733. /* Init DMA link list */
  734. INIT_LIST_HEAD(&dma_dev->channels);
  735. /* Set base routines */
  736. dma_dev->device_tx_status = dma_cookie_status;
  737. dma_dev->device_issue_pending = msgdma_issue_pending;
  738. dma_dev->dev = &pdev->dev;
  739. dma_dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
  740. dma_dev->device_prep_dma_memcpy = msgdma_prep_memcpy;
  741. dma_dev->device_prep_slave_sg = msgdma_prep_slave_sg;
  742. dma_dev->device_config = msgdma_dma_config;
  743. dma_dev->device_alloc_chan_resources = msgdma_alloc_chan_resources;
  744. dma_dev->device_free_chan_resources = msgdma_free_chan_resources;
  745. mdev->dmachan.device = dma_dev;
  746. list_add_tail(&mdev->dmachan.device_node, &dma_dev->channels);
  747. /* Set DMA mask to 64 bits */
  748. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  749. if (ret) {
  750. dev_warn(&pdev->dev, "unable to set coherent mask to 64");
  751. goto fail;
  752. }
  753. msgdma_reset(mdev);
  754. ret = dma_async_device_register(dma_dev);
  755. if (ret)
  756. goto fail;
  757. ret = of_dma_controller_register(pdev->dev.of_node,
  758. of_dma_xlate_by_chan_id, dma_dev);
  759. if (ret == -EINVAL)
  760. dev_warn(&pdev->dev, "device was not probed from DT");
  761. else if (ret && ret != -ENODEV)
  762. goto fail;
  763. dev_notice(&pdev->dev, "Altera mSGDMA driver probe success\n");
  764. return 0;
  765. fail:
  766. msgdma_dev_remove(mdev);
  767. return ret;
  768. }
  769. /**
  770. * msgdma_remove() - Driver remove function
  771. * @pdev: Pointer to the platform_device structure
  772. *
  773. * Return: Always '0'
  774. */
  775. static int msgdma_remove(struct platform_device *pdev)
  776. {
  777. struct msgdma_device *mdev = platform_get_drvdata(pdev);
  778. if (pdev->dev.of_node)
  779. of_dma_controller_free(pdev->dev.of_node);
  780. dma_async_device_unregister(&mdev->dmadev);
  781. msgdma_dev_remove(mdev);
  782. dev_notice(&pdev->dev, "Altera mSGDMA driver removed\n");
  783. return 0;
  784. }
  785. #ifdef CONFIG_OF
  786. static const struct of_device_id msgdma_match[] = {
  787. { .compatible = "altr,socfpga-msgdma", },
  788. { }
  789. };
  790. MODULE_DEVICE_TABLE(of, msgdma_match);
  791. #endif
  792. static struct platform_driver msgdma_driver = {
  793. .driver = {
  794. .name = "altera-msgdma",
  795. .of_match_table = of_match_ptr(msgdma_match),
  796. },
  797. .probe = msgdma_probe,
  798. .remove = msgdma_remove,
  799. };
  800. module_platform_driver(msgdma_driver);
  801. MODULE_ALIAS("platform:altera-msgdma");
  802. MODULE_DESCRIPTION("Altera mSGDMA driver");
  803. MODULE_AUTHOR("Stefan Roese <[email protected]>");
  804. MODULE_LICENSE("GPL");