xref: /openbmc/u-boot/test/dm/clk.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <asm/clk.h>
9 #include <dm/test.h>
10 #include <linux/err.h>
11 #include <test/ut.h>
12 
13 static int dm_test_clk(struct unit_test_state *uts)
14 {
15 	struct udevice *dev_fixed, *dev_clk, *dev_test;
16 	ulong rate;
17 
18 	ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed",
19 					      &dev_fixed));
20 
21 	ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
22 					      &dev_clk));
23 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
24 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
25 	ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
26 	ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
27 
28 	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
29 					      &dev_test));
30 	ut_assertok(sandbox_clk_test_get(dev_test));
31 
32 	ut_asserteq(1234,
33 		    sandbox_clk_test_get_rate(dev_test,
34 					      SANDBOX_CLK_TEST_ID_FIXED));
35 	ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
36 						 SANDBOX_CLK_TEST_ID_SPI));
37 	ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
38 						 SANDBOX_CLK_TEST_ID_I2C));
39 
40 	rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED,
41 					 12345);
42 	ut_assert(IS_ERR_VALUE(rate));
43 	rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED);
44 	ut_asserteq(1234, rate);
45 
46 	ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
47 						 SANDBOX_CLK_TEST_ID_SPI,
48 						 1000));
49 	ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
50 						 SANDBOX_CLK_TEST_ID_I2C,
51 						 2000));
52 
53 	ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test,
54 						    SANDBOX_CLK_TEST_ID_SPI));
55 	ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test,
56 						    SANDBOX_CLK_TEST_ID_I2C));
57 
58 	ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test,
59 						    SANDBOX_CLK_TEST_ID_SPI,
60 						    10000));
61 	ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test,
62 						    SANDBOX_CLK_TEST_ID_I2C,
63 						    20000));
64 
65 	rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
66 	ut_assert(IS_ERR_VALUE(rate));
67 	rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
68 	ut_assert(IS_ERR_VALUE(rate));
69 
70 	ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
71 						     SANDBOX_CLK_TEST_ID_SPI));
72 	ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
73 						     SANDBOX_CLK_TEST_ID_I2C));
74 
75 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
76 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
77 	ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
78 	ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
79 
80 	ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI));
81 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
82 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
83 
84 	ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C));
85 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
86 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
87 
88 	ut_assertok(sandbox_clk_test_disable(dev_test,
89 					     SANDBOX_CLK_TEST_ID_SPI));
90 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
91 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
92 
93 	ut_assertok(sandbox_clk_test_disable(dev_test,
94 					     SANDBOX_CLK_TEST_ID_I2C));
95 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
96 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
97 
98 	ut_assertok(sandbox_clk_test_free(dev_test));
99 
100 	return 0;
101 }
102 DM_TEST(dm_test_clk, DM_TESTF_SCAN_FDT);
103 
104 static int dm_test_clk_bulk(struct unit_test_state *uts)
105 {
106 	struct udevice *dev_clk, *dev_test;
107 
108 	ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
109 					      &dev_clk));
110 	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
111 					      &dev_test));
112 	ut_assertok(sandbox_clk_test_get_bulk(dev_test));
113 
114 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
115 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
116 
117 	/* Fixed clock does not support enable, thus should not fail */
118 	ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
119 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
120 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
121 
122 	/* Fixed clock does not support disable, thus should not fail */
123 	ut_assertok(sandbox_clk_test_disable_bulk(dev_test));
124 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
125 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
126 
127 	/* Fixed clock does not support enable, thus should not fail */
128 	ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
129 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
130 	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
131 
132 	/* Fixed clock does not support disable, thus should not fail */
133 	ut_assertok(sandbox_clk_test_release_bulk(dev_test));
134 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
135 	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
136 
137 	return 0;
138 }
139 DM_TEST(dm_test_clk_bulk, DM_TESTF_SCAN_FDT);
140