blob: 112d5cbbc91c133ae4518b4881cd4731b2da0ced [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8cc4d822015-07-06 12:54:24 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glass8cc4d822015-07-06 12:54:24 -06004 */
5
6#include <common.h>
Simon Glass8cc4d822015-07-06 12:54:24 -06007#include <dm.h>
Stephen Warrena9622432016-06-17 09:44:00 -06008#include <asm/clk.h>
Simon Glass8cc4d822015-07-06 12:54:24 -06009#include <dm/test.h>
10#include <linux/err.h>
11#include <test/ut.h>
12
Stephen Warrena9622432016-06-17 09:44:00 -060013static int dm_test_clk(struct unit_test_state *uts)
Simon Glass8cc4d822015-07-06 12:54:24 -060014{
Anup Patel8d28c3c2019-02-25 08:14:55 +000015 struct udevice *dev_fixed, *dev_fixed_factor, *dev_clk, *dev_test;
Simon Glass8cc4d822015-07-06 12:54:24 -060016 ulong rate;
17
Stephen Warrena9622432016-06-17 09:44:00 -060018 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed",
19 &dev_fixed));
Simon Glass8cc4d822015-07-06 12:54:24 -060020
Anup Patel8d28c3c2019-02-25 08:14:55 +000021 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed-factor",
22 &dev_fixed_factor));
23
Stephen Warrena9622432016-06-17 09:44:00 -060024 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));
Simon Glass8cc4d822015-07-06 12:54:24 -060030
Stephen Warrena9622432016-06-17 09:44:00 -060031 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
32 &dev_test));
33 ut_assertok(sandbox_clk_test_get(dev_test));
Fabrice Gasnier11192712018-07-24 16:31:28 +020034 ut_assertok(sandbox_clk_test_valid(dev_test));
Simon Glass8cc4d822015-07-06 12:54:24 -060035
Stephen Warrena9622432016-06-17 09:44:00 -060036 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));
Simon Glass8cc4d822015-07-06 12:54:24 -060043
Stephen Warrena9622432016-06-17 09:44:00 -060044 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);
Simon Glass8cc4d822015-07-06 12:54:24 -060049
Stephen Warrena9622432016-06-17 09:44:00 -060050 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));
Simon Glass8cc4d822015-07-06 12:54:24 -060056
Stephen Warrena9622432016-06-17 09:44:00 -060057 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));
Simon Glass8cc4d822015-07-06 12:54:24 -060061
Stephen Warrena9622432016-06-17 09:44:00 -060062 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));
Simon Glass8cc4d822015-07-06 12:54:24 -060073
Stephen Warrena9622432016-06-17 09:44:00 -060074 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));
Simon Glass8cc4d822015-07-06 12:54:24 -0600103
104 return 0;
105}
Stephen Warrena9622432016-06-17 09:44:00 -0600106DM_TEST(dm_test_clk, DM_TESTF_SCAN_FDT);
Neil Armstrong567a38b2018-04-03 11:44:19 +0200107
108static 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}
143DM_TEST(dm_test_clk_bulk, DM_TESTF_SCAN_FDT);