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