xref: /openbmc/u-boot/test/dm/dma.c (revision 5396b2a9)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Direct Memory Access U-Class tests
4  *
5  * Copyright (C) 2018 Texas Instruments Incorporated <www.ti.com>
6  * Grygorii Strashko <grygorii.strashko@ti.com>
7  */
8 
9 #include <common.h>
10 #include <dm.h>
11 #include <dm/test.h>
12 #include <dma.h>
13 #include <test/ut.h>
14 
15 static int dm_test_dma_m2m(struct unit_test_state *uts)
16 {
17 	struct udevice *dev;
18 	struct dma dma_m2m;
19 	u8 src_buf[512];
20 	u8 dst_buf[512];
21 	size_t len = 512;
22 	int i;
23 
24 	ut_assertok(uclass_get_device_by_name(UCLASS_DMA, "dma", &dev));
25 	ut_assertok(dma_get_by_name(dev, "m2m", &dma_m2m));
26 
27 	memset(dst_buf, 0, len);
28 	for (i = 0; i < len; i++)
29 		src_buf[i] = i;
30 
31 	ut_assertok(dma_memcpy(dst_buf, src_buf, len));
32 
33 	ut_assertok(memcmp(src_buf, dst_buf, len));
34 	return 0;
35 }
36 DM_TEST(dm_test_dma_m2m, DM_TESTF_SCAN_FDT);
37 
38 static int dm_test_dma(struct unit_test_state *uts)
39 {
40 	struct udevice *dev;
41 	struct dma dma_tx, dma_rx;
42 	u8 src_buf[512];
43 	u8 dst_buf[512];
44 	void *dst_ptr;
45 	size_t len = 512;
46 	u32 meta1, meta2;
47 	int i;
48 
49 	ut_assertok(uclass_get_device_by_name(UCLASS_DMA, "dma", &dev));
50 
51 	ut_assertok(dma_get_by_name(dev, "tx0", &dma_tx));
52 	ut_assertok(dma_get_by_name(dev, "rx0", &dma_rx));
53 
54 	ut_assertok(dma_enable(&dma_tx));
55 	ut_assertok(dma_enable(&dma_rx));
56 
57 	memset(dst_buf, 0, len);
58 	for (i = 0; i < len; i++)
59 		src_buf[i] = i;
60 	meta1 = 0xADADDEAD;
61 	meta2 = 0;
62 	dst_ptr = &dst_buf;
63 
64 	ut_assertok(dma_send(&dma_tx, src_buf, len, &meta1));
65 
66 	ut_asserteq(len, dma_receive(&dma_rx, &dst_ptr, &meta2));
67 	ut_asserteq(0xADADDEAD, meta2);
68 
69 	ut_assertok(dma_disable(&dma_tx));
70 	ut_assertok(dma_disable(&dma_rx));
71 
72 	ut_assertok(dma_free(&dma_tx));
73 	ut_assertok(dma_free(&dma_rx));
74 	ut_assertok(memcmp(src_buf, dst_buf, len));
75 
76 	return 0;
77 }
78 DM_TEST(dm_test_dma, DM_TESTF_SCAN_FDT);
79 
80 static int dm_test_dma_rx(struct unit_test_state *uts)
81 {
82 	struct udevice *dev;
83 	struct dma dma_tx, dma_rx;
84 	u8 src_buf[512];
85 	u8 dst_buf[512];
86 	void *dst_ptr;
87 	size_t len = 512;
88 	u32 meta1, meta2;
89 	int i;
90 
91 	ut_assertok(uclass_get_device_by_name(UCLASS_DMA, "dma", &dev));
92 
93 	ut_assertok(dma_get_by_name(dev, "tx0", &dma_tx));
94 	ut_assertok(dma_get_by_name(dev, "rx0", &dma_rx));
95 
96 	ut_assertok(dma_enable(&dma_tx));
97 	ut_assertok(dma_enable(&dma_rx));
98 
99 	memset(dst_buf, 0, len);
100 	for (i = 0; i < len; i++)
101 		src_buf[i] = i;
102 	meta1 = 0xADADDEAD;
103 	meta2 = 0;
104 	dst_ptr = NULL;
105 
106 	ut_assertok(dma_prepare_rcv_buf(&dma_tx, dst_buf, len));
107 
108 	ut_assertok(dma_send(&dma_tx, src_buf, len, &meta1));
109 
110 	ut_asserteq(len, dma_receive(&dma_rx, &dst_ptr, &meta2));
111 	ut_asserteq(0xADADDEAD, meta2);
112 	ut_asserteq_ptr(dst_buf, dst_ptr);
113 
114 	ut_assertok(dma_disable(&dma_tx));
115 	ut_assertok(dma_disable(&dma_rx));
116 
117 	ut_assertok(dma_free(&dma_tx));
118 	ut_assertok(dma_free(&dma_rx));
119 	ut_assertok(memcmp(src_buf, dst_buf, len));
120 
121 	return 0;
122 }
123 DM_TEST(dm_test_dma_rx, DM_TESTF_SCAN_FDT);
124