Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 7 | #include <console.h> |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 9 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 11 | #include <rtc.h> |
| 12 | #include <asm/io.h> |
Rasmus Villemoes | 034b93b | 2020-07-06 22:01:18 +0200 | [diff] [blame] | 13 | #include <asm/rtc.h> |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 14 | #include <asm/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 15 | #include <dm/test.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 16 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 18 | |
| 19 | /* Simple RTC sanity check */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | static int dm_test_rtc_base(struct unit_test_state *uts) |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 21 | { |
| 22 | struct udevice *dev; |
| 23 | |
| 24 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_RTC, 2, &dev)); |
| 25 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 26 | ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev)); |
| 27 | |
| 28 | return 0; |
| 29 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 30 | DM_TEST(dm_test_rtc_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 31 | |
| 32 | static void show_time(const char *msg, struct rtc_time *time) |
| 33 | { |
| 34 | printf("%s: %02d/%02d/%04d %02d:%02d:%02d\n", msg, |
| 35 | time->tm_mday, time->tm_mon, time->tm_year, |
| 36 | time->tm_hour, time->tm_min, time->tm_sec); |
| 37 | } |
| 38 | |
| 39 | static int cmp_times(struct rtc_time *expect, struct rtc_time *time, bool show) |
| 40 | { |
| 41 | bool same; |
| 42 | |
| 43 | same = expect->tm_sec == time->tm_sec; |
| 44 | same &= expect->tm_min == time->tm_min; |
| 45 | same &= expect->tm_hour == time->tm_hour; |
| 46 | same &= expect->tm_mday == time->tm_mday; |
| 47 | same &= expect->tm_mon == time->tm_mon; |
| 48 | same &= expect->tm_year == time->tm_year; |
| 49 | if (!same && show) { |
| 50 | show_time("expected", expect); |
| 51 | show_time("actual", time); |
| 52 | } |
| 53 | |
| 54 | return same ? 0 : -EINVAL; |
| 55 | } |
| 56 | |
| 57 | /* Set and get the time */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 58 | static int dm_test_rtc_set_get(struct unit_test_state *uts) |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 59 | { |
| 60 | struct rtc_time now, time, cmp; |
| 61 | struct udevice *dev, *emul; |
Simon Glass | 277bab8 | 2022-08-01 07:58:47 -0600 | [diff] [blame] | 62 | long offset, check_offset, old_offset, old_base_time; |
| 63 | int i; |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 64 | |
| 65 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 66 | |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 67 | ut_assertok(i2c_emul_find(dev, &emul)); |
Simon Glass | 8980155 | 2022-08-01 07:58:44 -0600 | [diff] [blame] | 68 | ut_assertnonnull(emul); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 69 | |
Simon Glass | 277bab8 | 2022-08-01 07:58:47 -0600 | [diff] [blame] | 70 | /* Get the offset, putting the RTC into manual mode */ |
| 71 | i = 0; |
| 72 | do { |
| 73 | check_offset = sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 74 | ut_assertok(dm_rtc_get(dev, &now)); |
| 75 | |
| 76 | /* Tell the RTC to go into manual mode */ |
| 77 | old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 78 | |
| 79 | /* If the times changed in that period, read it again */ |
| 80 | } while (++i < 2 && check_offset != old_offset); |
| 81 | ut_asserteq(check_offset, old_offset); |
| 82 | |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 83 | old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1); |
| 84 | |
| 85 | memset(&time, '\0', sizeof(time)); |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 86 | time.tm_mday = 3; |
| 87 | time.tm_mon = 6; |
| 88 | time.tm_year = 2004; |
| 89 | time.tm_sec = 0; |
| 90 | time.tm_min = 18; |
| 91 | time.tm_hour = 18; |
| 92 | ut_assertok(dm_rtc_set(dev, &time)); |
| 93 | |
| 94 | memset(&cmp, '\0', sizeof(cmp)); |
| 95 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 96 | ut_assertok(cmp_times(&time, &cmp, true)); |
| 97 | |
| 98 | memset(&time, '\0', sizeof(time)); |
| 99 | time.tm_mday = 31; |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 100 | time.tm_mon = 8; |
| 101 | time.tm_year = 2004; |
| 102 | time.tm_sec = 0; |
| 103 | time.tm_min = 18; |
| 104 | time.tm_hour = 18; |
| 105 | ut_assertok(dm_rtc_set(dev, &time)); |
| 106 | |
| 107 | memset(&cmp, '\0', sizeof(cmp)); |
| 108 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 109 | ut_assertok(cmp_times(&time, &cmp, true)); |
| 110 | |
| 111 | /* Increment by 1 second */ |
| 112 | offset = sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 113 | sandbox_i2c_rtc_set_offset(emul, false, offset + 1); |
| 114 | |
| 115 | memset(&cmp, '\0', sizeof(cmp)); |
| 116 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 117 | ut_asserteq(1, cmp.tm_sec); |
| 118 | |
| 119 | /* Check against original offset */ |
| 120 | sandbox_i2c_rtc_set_offset(emul, false, old_offset); |
| 121 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 122 | ut_assertok(cmp_times(&now, &cmp, true)); |
| 123 | |
| 124 | /* Back to the original offset */ |
| 125 | sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 126 | memset(&cmp, '\0', sizeof(cmp)); |
| 127 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 128 | ut_assertok(cmp_times(&now, &cmp, true)); |
| 129 | |
| 130 | /* Increment the base time by 1 emul */ |
| 131 | sandbox_i2c_rtc_get_set_base_time(emul, old_base_time + 1); |
| 132 | memset(&cmp, '\0', sizeof(cmp)); |
| 133 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 134 | if (now.tm_sec == 59) { |
| 135 | ut_asserteq(0, cmp.tm_sec); |
| 136 | } else { |
| 137 | ut_asserteq(now.tm_sec + 1, cmp.tm_sec); |
| 138 | } |
| 139 | |
Simon Glass | 277bab8 | 2022-08-01 07:58:47 -0600 | [diff] [blame] | 140 | /* return RTC to normal mode */ |
| 141 | sandbox_i2c_rtc_set_offset(emul, true, 0); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 145 | DM_TEST(dm_test_rtc_set_get, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 146 | |
Rasmus Villemoes | 034b93b | 2020-07-06 22:01:18 +0200 | [diff] [blame] | 147 | static int dm_test_rtc_read_write(struct unit_test_state *uts) |
| 148 | { |
| 149 | struct rtc_time time; |
| 150 | struct udevice *dev, *emul; |
| 151 | long old_offset; |
| 152 | u8 buf[4], reg; |
| 153 | |
| 154 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 155 | |
| 156 | memcpy(buf, "car", 4); |
| 157 | ut_assertok(dm_rtc_write(dev, REG_AUX0, buf, 4)); |
| 158 | memset(buf, '\0', sizeof(buf)); |
| 159 | ut_assertok(dm_rtc_read(dev, REG_AUX0, buf, 4)); |
| 160 | ut_asserteq(memcmp(buf, "car", 4), 0); |
| 161 | |
| 162 | reg = 'b'; |
| 163 | ut_assertok(dm_rtc_write(dev, REG_AUX0, ®, 1)); |
| 164 | memset(buf, '\0', sizeof(buf)); |
| 165 | ut_assertok(dm_rtc_read(dev, REG_AUX0, buf, 4)); |
| 166 | ut_asserteq(memcmp(buf, "bar", 4), 0); |
| 167 | |
| 168 | reg = 't'; |
| 169 | ut_assertok(dm_rtc_write(dev, REG_AUX2, ®, 1)); |
| 170 | memset(buf, '\0', sizeof(buf)); |
| 171 | ut_assertok(dm_rtc_read(dev, REG_AUX1, buf, 3)); |
| 172 | ut_asserteq(memcmp(buf, "at", 3), 0); |
| 173 | |
| 174 | ut_assertok(i2c_emul_find(dev, &emul)); |
Simon Glass | 8980155 | 2022-08-01 07:58:44 -0600 | [diff] [blame] | 175 | ut_assertnonnull(emul); |
Rasmus Villemoes | 034b93b | 2020-07-06 22:01:18 +0200 | [diff] [blame] | 176 | |
| 177 | old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 178 | ut_assertok(dm_rtc_get(dev, &time)); |
| 179 | |
| 180 | ut_assertok(dm_rtc_read(dev, REG_SEC, ®, 1)); |
| 181 | ut_asserteq(time.tm_sec, reg); |
| 182 | ut_assertok(dm_rtc_read(dev, REG_MDAY, ®, 1)); |
| 183 | ut_asserteq(time.tm_mday, reg); |
| 184 | |
| 185 | sandbox_i2c_rtc_set_offset(emul, true, old_offset); |
| 186 | |
| 187 | return 0; |
| 188 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 189 | DM_TEST(dm_test_rtc_read_write, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Rasmus Villemoes | 034b93b | 2020-07-06 22:01:18 +0200 | [diff] [blame] | 190 | |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 191 | /* Test 'rtc list' command */ |
| 192 | static int dm_test_rtc_cmd_list(struct unit_test_state *uts) |
| 193 | { |
| 194 | console_record_reset(); |
| 195 | |
| 196 | run_command("rtc list", 0); |
| 197 | ut_assert_nextline("RTC #0 - rtc@43"); |
| 198 | ut_assert_nextline("RTC #1 - rtc@61"); |
| 199 | ut_assert_console_end(); |
| 200 | |
| 201 | return 0; |
| 202 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 203 | DM_TEST(dm_test_rtc_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 204 | |
| 205 | /* Test 'rtc read' and 'rtc write' commands */ |
| 206 | static int dm_test_rtc_cmd_rw(struct unit_test_state *uts) |
| 207 | { |
| 208 | console_record_reset(); |
| 209 | |
| 210 | run_command("rtc dev 0", 0); |
| 211 | ut_assert_nextline("RTC #0 - rtc@43"); |
| 212 | ut_assert_console_end(); |
| 213 | |
| 214 | run_command("rtc write 0x30 aabb", 0); |
| 215 | ut_assert_console_end(); |
| 216 | |
| 217 | run_command("rtc read 0x30 2", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 218 | ut_assert_nextline("00000030: aa bb .."); |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 219 | ut_assert_console_end(); |
| 220 | |
| 221 | run_command("rtc dev 1", 0); |
| 222 | ut_assert_nextline("RTC #1 - rtc@61"); |
| 223 | ut_assert_console_end(); |
| 224 | |
| 225 | run_command("rtc write 0x30 ccdd", 0); |
| 226 | ut_assert_console_end(); |
| 227 | |
| 228 | run_command("rtc read 0x30 2", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 229 | ut_assert_nextline("00000030: cc dd .."); |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 230 | ut_assert_console_end(); |
| 231 | |
| 232 | /* |
| 233 | * Switch back to device #0, check that its aux registers |
| 234 | * still have the same values. |
| 235 | */ |
| 236 | run_command("rtc dev 0", 0); |
| 237 | ut_assert_nextline("RTC #0 - rtc@43"); |
| 238 | ut_assert_console_end(); |
| 239 | |
| 240 | run_command("rtc read 0x30 2", 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 241 | ut_assert_nextline("00000030: aa bb .."); |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 242 | ut_assert_console_end(); |
| 243 | |
| 244 | return 0; |
| 245 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 246 | DM_TEST(dm_test_rtc_cmd_rw, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 247 | |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 248 | /* Reset the time */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 249 | static int dm_test_rtc_reset(struct unit_test_state *uts) |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 250 | { |
| 251 | struct rtc_time now; |
| 252 | struct udevice *dev, *emul; |
| 253 | long old_base_time, base_time; |
Simon Glass | b6e254d | 2022-08-01 07:58:48 -0600 | [diff] [blame] | 254 | int i; |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 255 | |
| 256 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 257 | ut_assertok(dm_rtc_get(dev, &now)); |
| 258 | |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 259 | ut_assertok(i2c_emul_find(dev, &emul)); |
Simon Glass | 8980155 | 2022-08-01 07:58:44 -0600 | [diff] [blame] | 260 | ut_assertnonnull(emul); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 261 | |
Simon Glass | b6e254d | 2022-08-01 07:58:48 -0600 | [diff] [blame] | 262 | i = 0; |
| 263 | do { |
| 264 | old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 265 | |
Simon Glass | b6e254d | 2022-08-01 07:58:48 -0600 | [diff] [blame] | 266 | ut_asserteq(0, sandbox_i2c_rtc_get_set_base_time(emul, -1)); |
| 267 | |
| 268 | ut_assertok(dm_rtc_reset(dev)); |
| 269 | base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 270 | |
Simon Glass | b6e254d | 2022-08-01 07:58:48 -0600 | [diff] [blame] | 271 | /* |
| 272 | * Resetting the RTC should put the base time back to normal. |
| 273 | * Allow for a one-timeadjustment in case the time flips over |
| 274 | * while this test process is pre-empted (either by a second |
| 275 | * or a daylight-saving change), since reset_time() in |
| 276 | * i2c_rtc_emul.c reads the time from the OS. |
| 277 | */ |
| 278 | } while (++i < 2 && base_time != old_base_time); |
| 279 | ut_asserteq(old_base_time, base_time); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 280 | |
| 281 | return 0; |
| 282 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 283 | DM_TEST(dm_test_rtc_reset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 284 | |
| 285 | /* Check that two RTC devices can be used independently */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 286 | static int dm_test_rtc_dual(struct unit_test_state *uts) |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 287 | { |
| 288 | struct rtc_time now1, now2, cmp; |
| 289 | struct udevice *dev1, *dev2; |
| 290 | struct udevice *emul1, *emul2; |
| 291 | long offset; |
| 292 | |
| 293 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev1)); |
| 294 | ut_assertok(dm_rtc_get(dev1, &now1)); |
| 295 | ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev2)); |
| 296 | ut_assertok(dm_rtc_get(dev2, &now2)); |
| 297 | |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 298 | ut_assertok(i2c_emul_find(dev1, &emul1)); |
Simon Glass | 8980155 | 2022-08-01 07:58:44 -0600 | [diff] [blame] | 299 | ut_assertnonnull(emul1); |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 300 | ut_assertok(i2c_emul_find(dev2, &emul2)); |
Simon Glass | 8980155 | 2022-08-01 07:58:44 -0600 | [diff] [blame] | 301 | ut_assertnonnull(emul2); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 302 | |
| 303 | offset = sandbox_i2c_rtc_set_offset(emul1, false, -1); |
| 304 | sandbox_i2c_rtc_set_offset(emul2, false, offset + 1); |
| 305 | memset(&cmp, '\0', sizeof(cmp)); |
| 306 | ut_assertok(dm_rtc_get(dev2, &cmp)); |
| 307 | ut_asserteq(-EINVAL, cmp_times(&now1, &cmp, false)); |
| 308 | |
| 309 | memset(&cmp, '\0', sizeof(cmp)); |
| 310 | ut_assertok(dm_rtc_get(dev1, &cmp)); |
| 311 | ut_assertok(cmp_times(&now1, &cmp, true)); |
| 312 | |
| 313 | return 0; |
| 314 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 315 | DM_TEST(dm_test_rtc_dual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |