Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Jagan Teki | ab127ba | 2019-03-05 19:42:44 +0530 | [diff] [blame] | 7 | #include <clk.h> |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 11 | #include <asm/clk.h> |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 12 | #include <dm/test.h> |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 14 | #include <linux/err.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 15 | #include <test/test.h> |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 16 | #include <test/ut.h> |
| 17 | |
Jagan Teki | ab127ba | 2019-03-05 19:42:44 +0530 | [diff] [blame] | 18 | /* Base test of the clk uclass */ |
| 19 | static int dm_test_clk_base(struct unit_test_state *uts) |
| 20 | { |
| 21 | struct udevice *dev; |
| 22 | struct clk clk_method1; |
| 23 | struct clk clk_method2; |
| 24 | |
| 25 | /* Get the device using the clk device */ |
| 26 | ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &dev)); |
| 27 | |
| 28 | /* Get the same clk port in 2 different ways and compare */ |
| 29 | ut_assertok(clk_get_by_index(dev, 1, &clk_method1)); |
| 30 | ut_assertok(clk_get_by_index_nodev(dev_ofnode(dev), 1, &clk_method2)); |
Sekhar Nori | cf3119d | 2019-08-01 19:12:55 +0530 | [diff] [blame] | 31 | ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true); |
Jagan Teki | ab127ba | 2019-03-05 19:42:44 +0530 | [diff] [blame] | 32 | ut_asserteq(clk_method1.id, clk_method2.id); |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 37 | DM_TEST(dm_test_clk_base, UT_TESTF_SCAN_FDT); |
Jagan Teki | ab127ba | 2019-03-05 19:42:44 +0530 | [diff] [blame] | 38 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 39 | static int dm_test_clk(struct unit_test_state *uts) |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 40 | { |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 41 | struct udevice *dev_fixed, *dev_fixed_factor, *dev_clk, *dev_test; |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 42 | ulong rate; |
| 43 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 44 | ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed", |
| 45 | &dev_fixed)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 46 | |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 47 | ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed-factor", |
| 48 | &dev_fixed_factor)); |
| 49 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 50 | ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox", |
| 51 | &dev_clk)); |
| 52 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 53 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 54 | ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 55 | ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 56 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 57 | ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", |
| 58 | &dev_test)); |
| 59 | ut_assertok(sandbox_clk_test_get(dev_test)); |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 60 | ut_assertok(sandbox_clk_test_devm_get(dev_test)); |
Fabrice Gasnier | 1119271 | 2018-07-24 16:31:28 +0200 | [diff] [blame] | 61 | ut_assertok(sandbox_clk_test_valid(dev_test)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 62 | |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 63 | ut_asserteq(0, sandbox_clk_test_get_rate(dev_test, |
| 64 | SANDBOX_CLK_TEST_ID_DEVM_NULL)); |
| 65 | ut_asserteq(0, sandbox_clk_test_set_rate(dev_test, |
| 66 | SANDBOX_CLK_TEST_ID_DEVM_NULL, |
| 67 | 0)); |
| 68 | ut_asserteq(0, sandbox_clk_test_enable(dev_test, |
| 69 | SANDBOX_CLK_TEST_ID_DEVM_NULL)); |
| 70 | ut_asserteq(0, sandbox_clk_test_disable(dev_test, |
| 71 | SANDBOX_CLK_TEST_ID_DEVM_NULL)); |
| 72 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 73 | ut_asserteq(1234, |
| 74 | sandbox_clk_test_get_rate(dev_test, |
| 75 | SANDBOX_CLK_TEST_ID_FIXED)); |
| 76 | ut_asserteq(0, sandbox_clk_test_get_rate(dev_test, |
| 77 | SANDBOX_CLK_TEST_ID_SPI)); |
| 78 | ut_asserteq(0, sandbox_clk_test_get_rate(dev_test, |
| 79 | SANDBOX_CLK_TEST_ID_I2C)); |
Jean-Jacques Hiblot | c1e9c94 | 2019-10-22 14:00:07 +0200 | [diff] [blame] | 80 | ut_asserteq(321, sandbox_clk_test_get_rate(dev_test, |
| 81 | SANDBOX_CLK_TEST_ID_DEVM1)); |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 82 | ut_asserteq(0, sandbox_clk_test_get_rate(dev_test, |
| 83 | SANDBOX_CLK_TEST_ID_DEVM2)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 84 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 85 | rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED, |
| 86 | 12345); |
| 87 | ut_assert(IS_ERR_VALUE(rate)); |
| 88 | rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED); |
| 89 | ut_asserteq(1234, rate); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 90 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 91 | ut_asserteq(0, sandbox_clk_test_set_rate(dev_test, |
| 92 | SANDBOX_CLK_TEST_ID_SPI, |
| 93 | 1000)); |
| 94 | ut_asserteq(0, sandbox_clk_test_set_rate(dev_test, |
| 95 | SANDBOX_CLK_TEST_ID_I2C, |
| 96 | 2000)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 97 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 98 | ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test, |
| 99 | SANDBOX_CLK_TEST_ID_SPI)); |
| 100 | ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test, |
| 101 | SANDBOX_CLK_TEST_ID_I2C)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 102 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 103 | ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test, |
| 104 | SANDBOX_CLK_TEST_ID_SPI, |
| 105 | 10000)); |
| 106 | ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test, |
| 107 | SANDBOX_CLK_TEST_ID_I2C, |
| 108 | 20000)); |
| 109 | |
| 110 | rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0); |
| 111 | ut_assert(IS_ERR_VALUE(rate)); |
| 112 | rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0); |
| 113 | ut_assert(IS_ERR_VALUE(rate)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 114 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 115 | ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test, |
| 116 | SANDBOX_CLK_TEST_ID_SPI)); |
| 117 | ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test, |
| 118 | SANDBOX_CLK_TEST_ID_I2C)); |
| 119 | |
| 120 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 121 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 122 | ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 123 | ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 124 | |
| 125 | ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI)); |
| 126 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 127 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 128 | |
| 129 | ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C)); |
| 130 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 131 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 132 | |
| 133 | ut_assertok(sandbox_clk_test_disable(dev_test, |
| 134 | SANDBOX_CLK_TEST_ID_SPI)); |
| 135 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 136 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 137 | |
| 138 | ut_assertok(sandbox_clk_test_disable(dev_test, |
| 139 | SANDBOX_CLK_TEST_ID_I2C)); |
| 140 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 141 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 142 | |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 143 | ut_asserteq(1, sandbox_clk_query_requested(dev_clk, |
| 144 | SANDBOX_CLK_ID_SPI)); |
| 145 | ut_asserteq(1, sandbox_clk_query_requested(dev_clk, |
| 146 | SANDBOX_CLK_ID_I2C)); |
| 147 | ut_asserteq(1, sandbox_clk_query_requested(dev_clk, |
| 148 | SANDBOX_CLK_ID_UART2)); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 149 | ut_assertok(sandbox_clk_test_free(dev_test)); |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 150 | ut_asserteq(0, sandbox_clk_query_requested(dev_clk, |
| 151 | SANDBOX_CLK_ID_SPI)); |
| 152 | ut_asserteq(0, sandbox_clk_query_requested(dev_clk, |
| 153 | SANDBOX_CLK_ID_I2C)); |
| 154 | ut_asserteq(0, sandbox_clk_query_requested(dev_clk, |
| 155 | SANDBOX_CLK_ID_UART2)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 156 | |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 157 | ut_asserteq(1, sandbox_clk_query_requested(dev_clk, |
| 158 | SANDBOX_CLK_ID_UART1)); |
| 159 | ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL)); |
| 160 | ut_asserteq(0, sandbox_clk_query_requested(dev_clk, |
| 161 | SANDBOX_CLK_ID_UART1)); |
Simon Glass | 8cc4d82 | 2015-07-06 12:54:24 -0600 | [diff] [blame] | 162 | return 0; |
| 163 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 164 | DM_TEST(dm_test_clk, UT_TESTF_SCAN_FDT); |
Neil Armstrong | 567a38b | 2018-04-03 11:44:19 +0200 | [diff] [blame] | 165 | |
| 166 | static int dm_test_clk_bulk(struct unit_test_state *uts) |
| 167 | { |
| 168 | struct udevice *dev_clk, *dev_test; |
| 169 | |
| 170 | ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox", |
| 171 | &dev_clk)); |
| 172 | ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", |
| 173 | &dev_test)); |
| 174 | ut_assertok(sandbox_clk_test_get_bulk(dev_test)); |
| 175 | |
| 176 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 177 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 178 | |
| 179 | /* Fixed clock does not support enable, thus should not fail */ |
| 180 | ut_assertok(sandbox_clk_test_enable_bulk(dev_test)); |
| 181 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 182 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 183 | |
| 184 | /* Fixed clock does not support disable, thus should not fail */ |
| 185 | ut_assertok(sandbox_clk_test_disable_bulk(dev_test)); |
| 186 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 187 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 188 | |
| 189 | /* Fixed clock does not support enable, thus should not fail */ |
| 190 | ut_assertok(sandbox_clk_test_enable_bulk(dev_test)); |
| 191 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 192 | ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
| 193 | |
| 194 | /* Fixed clock does not support disable, thus should not fail */ |
| 195 | ut_assertok(sandbox_clk_test_release_bulk(dev_test)); |
| 196 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); |
| 197 | ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); |
Jean-Jacques Hiblot | 98e8418 | 2019-10-22 14:00:05 +0200 | [diff] [blame] | 198 | ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL)); |
Neil Armstrong | 567a38b | 2018-04-03 11:44:19 +0200 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 202 | DM_TEST(dm_test_clk_bulk, UT_TESTF_SCAN_FDT); |