Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 |
| 4 | * Lukasz Majewski, DENX Software Engineering, lukma@denx.de |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <asm/clk.h> |
| 11 | #include <dm/test.h> |
| 12 | #include <dm/uclass.h> |
| 13 | #include <linux/err.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 14 | #include <test/test.h> |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 15 | #include <test/ut.h> |
| 16 | #include <sandbox-clk.h> |
| 17 | |
| 18 | /* Tests for Common Clock Framework driver */ |
| 19 | static int dm_test_clk_ccf(struct unit_test_state *uts) |
| 20 | { |
Yang Xiwen | 97d65b3 | 2023-12-16 04:21:11 +0800 | [diff] [blame] | 21 | struct clk *clk, *pclk; |
Yang Xiwen | d306182 | 2023-12-16 02:28:52 +0800 | [diff] [blame] | 22 | struct udevice *dev, *test_dev; |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 23 | long long rate; |
| 24 | int ret; |
Claudiu Beznea | 4d139f3 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 25 | #if CONFIG_IS_ENABLED(CLK_CCF) |
Yang Xiwen | 97d65b3 | 2023-12-16 04:21:11 +0800 | [diff] [blame] | 26 | struct clk clk_ccf; |
Claudiu Beznea | 4d139f3 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 27 | const char *clkname; |
Claudiu Beznea | 9a5d59d | 2020-09-07 17:46:35 +0300 | [diff] [blame] | 28 | int clkid, i; |
Claudiu Beznea | 4d139f3 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 29 | #endif |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 30 | |
| 31 | /* Get the device using the clk device */ |
| 32 | ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-ccf", &dev)); |
Yang Xiwen | d306182 | 2023-12-16 02:28:52 +0800 | [diff] [blame] | 33 | ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &test_dev)); |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 34 | |
| 35 | /* Test for clk_get_by_id() */ |
| 36 | ret = clk_get_by_id(SANDBOX_CLK_ECSPI_ROOT, &clk); |
| 37 | ut_assertok(ret); |
| 38 | ut_asserteq_str("ecspi_root", clk->dev->name); |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 39 | ut_asserteq(CLK_SET_RATE_PARENT, clk->flags); |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 40 | |
| 41 | /* Test for clk_get_parent_rate() */ |
| 42 | ret = clk_get_by_id(SANDBOX_CLK_ECSPI1, &clk); |
| 43 | ut_assertok(ret); |
| 44 | ut_asserteq_str("ecspi1", clk->dev->name); |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 45 | ut_asserteq(CLK_SET_RATE_PARENT, clk->flags); |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 46 | |
| 47 | rate = clk_get_parent_rate(clk); |
| 48 | ut_asserteq(rate, 20000000); |
| 49 | |
Dario Binacchi | cd16c57 | 2020-04-13 14:36:26 +0200 | [diff] [blame] | 50 | /* test the gate of CCF */ |
| 51 | ret = clk_get_by_id(SANDBOX_CLK_ECSPI0, &clk); |
| 52 | ut_assertok(ret); |
| 53 | ut_asserteq_str("ecspi0", clk->dev->name); |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 54 | ut_asserteq(CLK_SET_RATE_PARENT, clk->flags); |
Dario Binacchi | cd16c57 | 2020-04-13 14:36:26 +0200 | [diff] [blame] | 55 | |
| 56 | rate = clk_get_parent_rate(clk); |
| 57 | ut_asserteq(rate, 20000000); |
| 58 | |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 59 | /* Test the mux of CCF */ |
| 60 | ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk); |
| 61 | ut_assertok(ret); |
| 62 | ut_asserteq_str("usdhc1_sel", clk->dev->name); |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 63 | ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags); |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 64 | |
| 65 | rate = clk_get_parent_rate(clk); |
| 66 | ut_asserteq(rate, 60000000); |
| 67 | |
Igor Prusov | 9e02503 | 2023-12-06 02:23:34 +0300 | [diff] [blame] | 68 | rate = clk_set_rate(clk, 60000000); |
| 69 | ut_asserteq(rate, -ENOSYS); |
| 70 | |
Dario Binacchi | 12d1526 | 2020-06-03 15:36:25 +0200 | [diff] [blame] | 71 | rate = clk_get_rate(clk); |
| 72 | ut_asserteq(rate, 60000000); |
| 73 | |
| 74 | ret = clk_get_by_id(SANDBOX_CLK_PLL3_80M, &pclk); |
| 75 | ut_assertok(ret); |
| 76 | |
| 77 | ret = clk_set_parent(clk, pclk); |
| 78 | ut_assertok(ret); |
| 79 | |
| 80 | rate = clk_get_rate(clk); |
| 81 | ut_asserteq(rate, 80000000); |
| 82 | |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 83 | ret = clk_get_by_id(SANDBOX_CLK_USDHC2_SEL, &clk); |
| 84 | ut_assertok(ret); |
| 85 | ut_asserteq_str("usdhc2_sel", clk->dev->name); |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 86 | ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags); |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 87 | |
| 88 | rate = clk_get_parent_rate(clk); |
| 89 | ut_asserteq(rate, 80000000); |
| 90 | |
| 91 | pclk = clk_get_parent(clk); |
| 92 | ut_asserteq_str("pll3_80m", pclk->dev->name); |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 93 | ut_asserteq(CLK_SET_RATE_PARENT, pclk->flags); |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 94 | |
Igor Prusov | 9e02503 | 2023-12-06 02:23:34 +0300 | [diff] [blame] | 95 | rate = clk_set_rate(clk, 80000000); |
| 96 | ut_asserteq(rate, -ENOSYS); |
| 97 | |
Dario Binacchi | 12d1526 | 2020-06-03 15:36:25 +0200 | [diff] [blame] | 98 | rate = clk_get_rate(clk); |
| 99 | ut_asserteq(rate, 80000000); |
| 100 | |
| 101 | ret = clk_get_by_id(SANDBOX_CLK_PLL3_60M, &pclk); |
| 102 | ut_assertok(ret); |
| 103 | |
| 104 | ret = clk_set_parent(clk, pclk); |
| 105 | ut_assertok(ret); |
| 106 | |
| 107 | rate = clk_get_rate(clk); |
| 108 | ut_asserteq(rate, 60000000); |
| 109 | |
Peng Fan | 4f89598 | 2019-07-31 07:02:05 +0000 | [diff] [blame] | 110 | /* Test the composite of CCF */ |
| 111 | ret = clk_get_by_id(SANDBOX_CLK_I2C, &clk); |
| 112 | ut_assertok(ret); |
| 113 | ut_asserteq_str("i2c", clk->dev->name); |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 114 | ut_asserteq(CLK_SET_RATE_UNGATE, clk->flags); |
Peng Fan | 4f89598 | 2019-07-31 07:02:05 +0000 | [diff] [blame] | 115 | |
| 116 | rate = clk_get_rate(clk); |
| 117 | ut_asserteq(rate, 60000000); |
| 118 | |
Igor Prusov | 9e02503 | 2023-12-06 02:23:34 +0300 | [diff] [blame] | 119 | rate = clk_set_rate(clk, 60000000); |
| 120 | ut_asserteq(rate, 60000000); |
| 121 | |
Peng Fan | c66f4f5 | 2019-08-21 13:35:19 +0000 | [diff] [blame] | 122 | #if CONFIG_IS_ENABLED(CLK_CCF) |
| 123 | /* Test clk tree enable/disable */ |
Yang Xiwen | d306182 | 2023-12-16 02:28:52 +0800 | [diff] [blame] | 124 | |
| 125 | ret = clk_get_by_index(test_dev, SANDBOX_CLK_TEST_ID_I2C_ROOT, &clk_ccf); |
| 126 | ut_assertok(ret); |
| 127 | ut_asserteq_str("clk-ccf", clk_ccf.dev->name); |
| 128 | ut_asserteq(clk_ccf.id, SANDBOX_CLK_I2C_ROOT); |
| 129 | |
Peng Fan | c66f4f5 | 2019-08-21 13:35:19 +0000 | [diff] [blame] | 130 | ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk); |
| 131 | ut_assertok(ret); |
| 132 | ut_asserteq_str("i2c_root", clk->dev->name); |
Yang Xiwen | d306182 | 2023-12-16 02:28:52 +0800 | [diff] [blame] | 133 | ut_asserteq(clk->id, SANDBOX_CLK_I2C_ROOT); |
Peng Fan | c66f4f5 | 2019-08-21 13:35:19 +0000 | [diff] [blame] | 134 | |
Yang Xiwen | d306182 | 2023-12-16 02:28:52 +0800 | [diff] [blame] | 135 | ret = clk_enable(&clk_ccf); |
Peng Fan | c66f4f5 | 2019-08-21 13:35:19 +0000 | [diff] [blame] | 136 | ut_assertok(ret); |
| 137 | |
| 138 | ret = sandbox_clk_enable_count(clk); |
| 139 | ut_asserteq(ret, 1); |
| 140 | |
| 141 | ret = clk_get_by_id(SANDBOX_CLK_I2C, &pclk); |
| 142 | ut_assertok(ret); |
| 143 | |
| 144 | ret = sandbox_clk_enable_count(pclk); |
| 145 | ut_asserteq(ret, 1); |
| 146 | |
| 147 | ret = clk_disable(clk); |
| 148 | ut_assertok(ret); |
| 149 | |
| 150 | ret = sandbox_clk_enable_count(clk); |
| 151 | ut_asserteq(ret, 0); |
| 152 | |
| 153 | ret = sandbox_clk_enable_count(pclk); |
| 154 | ut_asserteq(ret, 0); |
Claudiu Beznea | 4d139f3 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 155 | |
| 156 | /* Test clock re-parenting. */ |
| 157 | ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk); |
| 158 | ut_assertok(ret); |
| 159 | ut_asserteq_str("usdhc1_sel", clk->dev->name); |
| 160 | |
| 161 | pclk = clk_get_parent(clk); |
| 162 | ut_assertok_ptr(pclk); |
| 163 | if (!strcmp(pclk->dev->name, "pll3_60m")) { |
| 164 | clkname = "pll3_80m"; |
| 165 | clkid = SANDBOX_CLK_PLL3_80M; |
| 166 | } else { |
| 167 | clkname = "pll3_60m"; |
| 168 | clkid = SANDBOX_CLK_PLL3_60M; |
| 169 | } |
| 170 | |
| 171 | ret = clk_get_by_id(clkid, &pclk); |
| 172 | ut_assertok(ret); |
| 173 | ret = clk_set_parent(clk, pclk); |
| 174 | ut_assertok(ret); |
| 175 | pclk = clk_get_parent(clk); |
| 176 | ut_assertok_ptr(pclk); |
| 177 | ut_asserteq_str(clkname, pclk->dev->name); |
Claudiu Beznea | 9a5d59d | 2020-09-07 17:46:35 +0300 | [diff] [blame] | 178 | |
| 179 | /* Test disabling critical clock. */ |
| 180 | ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk); |
| 181 | ut_assertok(ret); |
| 182 | ut_asserteq_str("i2c_root", clk->dev->name); |
| 183 | |
| 184 | /* Disable it, if any. */ |
| 185 | ret = sandbox_clk_enable_count(clk); |
| 186 | for (i = 0; i < ret; i++) { |
| 187 | ret = clk_disable(clk); |
| 188 | ut_assertok(ret); |
| 189 | } |
| 190 | |
| 191 | ret = sandbox_clk_enable_count(clk); |
| 192 | ut_asserteq(ret, 0); |
| 193 | |
| 194 | clk->flags = CLK_IS_CRITICAL; |
| 195 | ret = clk_enable(clk); |
| 196 | ut_assertok(ret); |
| 197 | |
| 198 | ret = clk_disable(clk); |
| 199 | ut_assertok(ret); |
| 200 | ret = sandbox_clk_enable_count(clk); |
| 201 | ut_asserteq(ret, 1); |
| 202 | clk->flags &= ~CLK_IS_CRITICAL; |
| 203 | |
| 204 | ret = clk_disable(clk); |
| 205 | ut_assertok(ret); |
| 206 | ret = sandbox_clk_enable_count(clk); |
| 207 | ut_asserteq(ret, 0); |
Peng Fan | c66f4f5 | 2019-08-21 13:35:19 +0000 | [diff] [blame] | 208 | #endif |
| 209 | |
Lukasz Majewski | 87e460c | 2019-06-24 15:50:50 +0200 | [diff] [blame] | 210 | return 1; |
| 211 | } |
| 212 | |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 213 | DM_TEST(dm_test_clk_ccf, UT_TESTF_SCAN_FDT); |