1 /*
2  * Copyright (c) 2018 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <soc.h>
9 #include <kernel_arch_func.h>
10 #include <zephyr/device.h>
11 #include <zephyr/drivers/dma.h>
12 
13 #define DMA_BUFF_SIZE		1024
14 
15 enum dma_op_status {
16 	DMA_OP_STAT_NONE = 0,
17 	DMA_OP_STAT_ERR,
18 	DMA_OP_STAT_SUCCESS,
19 };
20 
21 static enum dma_op_status dma_stat;
22 
23 static char tx_data[DMA_BUFF_SIZE];
24 static char rx_data[DMA_BUFF_SIZE];
25 
26 static struct dma_config dma_cfg = {0};
27 static struct dma_block_config dma_block_cfg = {0};
28 
dma_user_callback(const struct device * dma_dev,void * arg,uint32_t id,int status)29 static void dma_user_callback(const struct device *dma_dev, void *arg,
30 			      uint32_t id, int status)
31 {
32 	if (status >= 0) {
33 		TC_PRINT("DMA completed successfully\n");
34 		dma_stat = DMA_OP_STAT_SUCCESS;
35 	} else {
36 		TC_PRINT("DMA error occurred!! (%d)\n", status);
37 		dma_stat = DMA_OP_STAT_ERR;
38 	}
39 }
40 
ZTEST(nios2_msgdma,test_msgdma)41 ZTEST(nios2_msgdma, test_msgdma)
42 {
43 	const struct device *dma;
44 	static uint32_t chan_id;
45 	int i;
46 
47 	dma = DEVICE_DT_GET(DT_NODELABEL(dma));
48 	__ASSERT_NO_MSG(device_is_ready(dma));
49 
50 	/* Init tx buffer */
51 	for (i = 0; i < DMA_BUFF_SIZE; i++) {
52 		tx_data[i] = i;
53 	}
54 
55 	/* Init DMA config info */
56 	dma_cfg.channel_direction = MEMORY_TO_MEMORY;
57 	dma_cfg.source_data_size = 1U;
58 	dma_cfg.dest_data_size = 1U;
59 	dma_cfg.source_burst_length = 1U;
60 	dma_cfg.dest_burst_length = 1U;
61 	dma_cfg.dma_callback = dma_user_callback;
62 	dma_cfg.block_count = 1U;
63 	dma_cfg.head_block = &dma_block_cfg;
64 
65 	/*
66 	 * Set channel id to 0 as Nios-II
67 	 * MSGDMA only supports one channel
68 	 */
69 	chan_id = 0U;
70 
71 	/* Init DMA descriptor info */
72 	dma_block_cfg.block_size = DMA_BUFF_SIZE;
73 	dma_block_cfg.source_address = (uint32_t)tx_data;
74 	dma_block_cfg.dest_address = (uint32_t)rx_data;
75 
76 	/* Configure DMA */
77 	zassert_true(dma_config(dma, chan_id, &dma_cfg) == 0,
78 						"DMA config error");
79 
80 	/* Make sure all the data is written out to memory */
81 	z_nios2_dcache_flush_all();
82 
83 	/* Start DMA operation */
84 	zassert_true(dma_start(dma, chan_id) == 0, "DMA start error");
85 
86 	while (dma_stat == DMA_OP_STAT_NONE) {
87 		k_busy_wait(10);
88 	}
89 
90 	/* Invalidate the data cache */
91 	z_nios2_dcache_flush_no_writeback(rx_data, DMA_BUFF_SIZE);
92 
93 	zassert_true(dma_stat == DMA_OP_STAT_SUCCESS,
94 			"Nios-II DMA operation failed!!");
95 
96 	zassert_true(!memcmp(&tx_data, &rx_data, DMA_BUFF_SIZE),
97 					"Nios-II DMA Test failed!!");
98 
99 }
100 
101 ZTEST_SUITE(nios2_msgdma, NULL, NULL, NULL, NULL, NULL);
102