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 | |
| 7 | #include <common.h> |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 8 | #include <console.h> |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 10 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 12 | #include <rtc.h> |
| 13 | #include <asm/io.h> |
Rasmus Villemoes | 034b93b | 2020-07-06 22:01:18 +0200 | [diff] [blame] | 14 | #include <asm/rtc.h> |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 15 | #include <asm/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | #include <dm/test.h> |
| 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 | } |
| 30 | DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 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; |
| 62 | long offset, old_offset, old_base_time; |
| 63 | |
| 64 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 65 | ut_assertok(dm_rtc_get(dev, &now)); |
| 66 | |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 67 | ut_assertok(i2c_emul_find(dev, &emul)); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 68 | ut_assert(emul != NULL); |
| 69 | |
| 70 | /* Tell the RTC to go into manual mode */ |
| 71 | old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 72 | old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1); |
| 73 | |
| 74 | memset(&time, '\0', sizeof(time)); |
Rasmus Villemoes | acffbea | 2020-07-06 22:01:16 +0200 | [diff] [blame] | 75 | time.tm_mday = 3; |
| 76 | time.tm_mon = 6; |
| 77 | time.tm_year = 2004; |
| 78 | time.tm_sec = 0; |
| 79 | time.tm_min = 18; |
| 80 | time.tm_hour = 18; |
| 81 | ut_assertok(dm_rtc_set(dev, &time)); |
| 82 | |
| 83 | memset(&cmp, '\0', sizeof(cmp)); |
| 84 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 85 | ut_assertok(cmp_times(&time, &cmp, true)); |
| 86 | |
| 87 | memset(&time, '\0', sizeof(time)); |
| 88 | time.tm_mday = 31; |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 89 | time.tm_mon = 8; |
| 90 | time.tm_year = 2004; |
| 91 | time.tm_sec = 0; |
| 92 | time.tm_min = 18; |
| 93 | time.tm_hour = 18; |
| 94 | ut_assertok(dm_rtc_set(dev, &time)); |
| 95 | |
| 96 | memset(&cmp, '\0', sizeof(cmp)); |
| 97 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 98 | ut_assertok(cmp_times(&time, &cmp, true)); |
| 99 | |
| 100 | /* Increment by 1 second */ |
| 101 | offset = sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 102 | sandbox_i2c_rtc_set_offset(emul, false, offset + 1); |
| 103 | |
| 104 | memset(&cmp, '\0', sizeof(cmp)); |
| 105 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 106 | ut_asserteq(1, cmp.tm_sec); |
| 107 | |
| 108 | /* Check against original offset */ |
| 109 | sandbox_i2c_rtc_set_offset(emul, false, old_offset); |
| 110 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 111 | ut_assertok(cmp_times(&now, &cmp, true)); |
| 112 | |
| 113 | /* Back to the original offset */ |
| 114 | sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 115 | memset(&cmp, '\0', sizeof(cmp)); |
| 116 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 117 | ut_assertok(cmp_times(&now, &cmp, true)); |
| 118 | |
| 119 | /* Increment the base time by 1 emul */ |
| 120 | sandbox_i2c_rtc_get_set_base_time(emul, old_base_time + 1); |
| 121 | memset(&cmp, '\0', sizeof(cmp)); |
| 122 | ut_assertok(dm_rtc_get(dev, &cmp)); |
| 123 | if (now.tm_sec == 59) { |
| 124 | ut_asserteq(0, cmp.tm_sec); |
| 125 | } else { |
| 126 | ut_asserteq(now.tm_sec + 1, cmp.tm_sec); |
| 127 | } |
| 128 | |
| 129 | old_offset = sandbox_i2c_rtc_set_offset(emul, true, 0); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 134 | |
Rasmus Villemoes | 034b93b | 2020-07-06 22:01:18 +0200 | [diff] [blame] | 135 | static int dm_test_rtc_read_write(struct unit_test_state *uts) |
| 136 | { |
| 137 | struct rtc_time time; |
| 138 | struct udevice *dev, *emul; |
| 139 | long old_offset; |
| 140 | u8 buf[4], reg; |
| 141 | |
| 142 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 143 | |
| 144 | memcpy(buf, "car", 4); |
| 145 | ut_assertok(dm_rtc_write(dev, REG_AUX0, buf, 4)); |
| 146 | memset(buf, '\0', sizeof(buf)); |
| 147 | ut_assertok(dm_rtc_read(dev, REG_AUX0, buf, 4)); |
| 148 | ut_asserteq(memcmp(buf, "car", 4), 0); |
| 149 | |
| 150 | reg = 'b'; |
| 151 | ut_assertok(dm_rtc_write(dev, REG_AUX0, ®, 1)); |
| 152 | memset(buf, '\0', sizeof(buf)); |
| 153 | ut_assertok(dm_rtc_read(dev, REG_AUX0, buf, 4)); |
| 154 | ut_asserteq(memcmp(buf, "bar", 4), 0); |
| 155 | |
| 156 | reg = 't'; |
| 157 | ut_assertok(dm_rtc_write(dev, REG_AUX2, ®, 1)); |
| 158 | memset(buf, '\0', sizeof(buf)); |
| 159 | ut_assertok(dm_rtc_read(dev, REG_AUX1, buf, 3)); |
| 160 | ut_asserteq(memcmp(buf, "at", 3), 0); |
| 161 | |
| 162 | ut_assertok(i2c_emul_find(dev, &emul)); |
| 163 | ut_assert(emul != NULL); |
| 164 | |
| 165 | old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0); |
| 166 | ut_assertok(dm_rtc_get(dev, &time)); |
| 167 | |
| 168 | ut_assertok(dm_rtc_read(dev, REG_SEC, ®, 1)); |
| 169 | ut_asserteq(time.tm_sec, reg); |
| 170 | ut_assertok(dm_rtc_read(dev, REG_MDAY, ®, 1)); |
| 171 | ut_asserteq(time.tm_mday, reg); |
| 172 | |
| 173 | sandbox_i2c_rtc_set_offset(emul, true, old_offset); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | DM_TEST(dm_test_rtc_read_write, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 178 | |
Rasmus Villemoes | 956c18f | 2020-07-06 22:01:20 +0200 | [diff] [blame] | 179 | /* Test 'rtc list' command */ |
| 180 | static int dm_test_rtc_cmd_list(struct unit_test_state *uts) |
| 181 | { |
| 182 | console_record_reset(); |
| 183 | |
| 184 | run_command("rtc list", 0); |
| 185 | ut_assert_nextline("RTC #0 - rtc@43"); |
| 186 | ut_assert_nextline("RTC #1 - rtc@61"); |
| 187 | ut_assert_console_end(); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | DM_TEST(dm_test_rtc_cmd_list, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 192 | |
| 193 | /* Test 'rtc read' and 'rtc write' commands */ |
| 194 | static int dm_test_rtc_cmd_rw(struct unit_test_state *uts) |
| 195 | { |
| 196 | console_record_reset(); |
| 197 | |
| 198 | run_command("rtc dev 0", 0); |
| 199 | ut_assert_nextline("RTC #0 - rtc@43"); |
| 200 | ut_assert_console_end(); |
| 201 | |
| 202 | run_command("rtc write 0x30 aabb", 0); |
| 203 | ut_assert_console_end(); |
| 204 | |
| 205 | run_command("rtc read 0x30 2", 0); |
| 206 | ut_assert_nextline("00000030: aa bb .."); |
| 207 | ut_assert_console_end(); |
| 208 | |
| 209 | run_command("rtc dev 1", 0); |
| 210 | ut_assert_nextline("RTC #1 - rtc@61"); |
| 211 | ut_assert_console_end(); |
| 212 | |
| 213 | run_command("rtc write 0x30 ccdd", 0); |
| 214 | ut_assert_console_end(); |
| 215 | |
| 216 | run_command("rtc read 0x30 2", 0); |
| 217 | ut_assert_nextline("00000030: cc dd .."); |
| 218 | ut_assert_console_end(); |
| 219 | |
| 220 | /* |
| 221 | * Switch back to device #0, check that its aux registers |
| 222 | * still have the same values. |
| 223 | */ |
| 224 | run_command("rtc dev 0", 0); |
| 225 | ut_assert_nextline("RTC #0 - rtc@43"); |
| 226 | ut_assert_console_end(); |
| 227 | |
| 228 | run_command("rtc read 0x30 2", 0); |
| 229 | ut_assert_nextline("00000030: aa bb .."); |
| 230 | ut_assert_console_end(); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | DM_TEST(dm_test_rtc_cmd_rw, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 235 | |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 236 | /* Reset the time */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 237 | static int dm_test_rtc_reset(struct unit_test_state *uts) |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 238 | { |
| 239 | struct rtc_time now; |
| 240 | struct udevice *dev, *emul; |
| 241 | long old_base_time, base_time; |
| 242 | |
| 243 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 244 | ut_assertok(dm_rtc_get(dev, &now)); |
| 245 | |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 246 | ut_assertok(i2c_emul_find(dev, &emul)); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 247 | ut_assert(emul != NULL); |
| 248 | |
| 249 | old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0); |
| 250 | |
| 251 | ut_asserteq(0, sandbox_i2c_rtc_get_set_base_time(emul, -1)); |
| 252 | |
| 253 | /* Resetting the RTC should put he base time back to normal */ |
| 254 | ut_assertok(dm_rtc_reset(dev)); |
| 255 | base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1); |
| 256 | ut_asserteq(old_base_time, base_time); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | DM_TEST(dm_test_rtc_reset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 261 | |
| 262 | /* Check that two RTC devices can be used independently */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 263 | static int dm_test_rtc_dual(struct unit_test_state *uts) |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 264 | { |
| 265 | struct rtc_time now1, now2, cmp; |
| 266 | struct udevice *dev1, *dev2; |
| 267 | struct udevice *emul1, *emul2; |
| 268 | long offset; |
| 269 | |
| 270 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev1)); |
| 271 | ut_assertok(dm_rtc_get(dev1, &now1)); |
| 272 | ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev2)); |
| 273 | ut_assertok(dm_rtc_get(dev2, &now2)); |
| 274 | |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 275 | ut_assertok(i2c_emul_find(dev1, &emul1)); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 276 | ut_assert(emul1 != NULL); |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 277 | ut_assertok(i2c_emul_find(dev2, &emul2)); |
Simon Glass | c83c4b9 | 2015-04-20 12:37:31 -0600 | [diff] [blame] | 278 | ut_assert(emul2 != NULL); |
| 279 | |
| 280 | offset = sandbox_i2c_rtc_set_offset(emul1, false, -1); |
| 281 | sandbox_i2c_rtc_set_offset(emul2, false, offset + 1); |
| 282 | memset(&cmp, '\0', sizeof(cmp)); |
| 283 | ut_assertok(dm_rtc_get(dev2, &cmp)); |
| 284 | ut_asserteq(-EINVAL, cmp_times(&now1, &cmp, false)); |
| 285 | |
| 286 | memset(&cmp, '\0', sizeof(cmp)); |
| 287 | ut_assertok(dm_rtc_get(dev1, &cmp)); |
| 288 | ut_assertok(cmp_times(&now1, &cmp, true)); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |