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