1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
16 /* Simple RTC sanity check */
17 static int dm_test_rtc_base(struct unit_test_state *uts)
21 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_RTC, 2, &dev));
22 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
23 ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev));
27 DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
29 static void show_time(const char *msg, struct rtc_time *time)
31 printf("%s: %02d/%02d/%04d %02d:%02d:%02d\n", msg,
32 time->tm_mday, time->tm_mon, time->tm_year,
33 time->tm_hour, time->tm_min, time->tm_sec);
36 static int cmp_times(struct rtc_time *expect, struct rtc_time *time, bool show)
40 same = expect->tm_sec == time->tm_sec;
41 same &= expect->tm_min == time->tm_min;
42 same &= expect->tm_hour == time->tm_hour;
43 same &= expect->tm_mday == time->tm_mday;
44 same &= expect->tm_mon == time->tm_mon;
45 same &= expect->tm_year == time->tm_year;
47 show_time("expected", expect);
48 show_time("actual", time);
51 return same ? 0 : -EINVAL;
54 /* Set and get the time */
55 static int dm_test_rtc_set_get(struct unit_test_state *uts)
57 struct rtc_time now, time, cmp;
58 struct udevice *dev, *emul;
59 long offset, old_offset, old_base_time;
61 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
62 ut_assertok(dm_rtc_get(dev, &now));
64 ut_assertok(i2c_emul_find(dev, &emul));
65 ut_assert(emul != NULL);
67 /* Tell the RTC to go into manual mode */
68 old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
69 old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
71 memset(&time, '\0', sizeof(time));
78 ut_assertok(dm_rtc_set(dev, &time));
80 memset(&cmp, '\0', sizeof(cmp));
81 ut_assertok(dm_rtc_get(dev, &cmp));
82 ut_assertok(cmp_times(&time, &cmp, true));
84 /* Increment by 1 second */
85 offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
86 sandbox_i2c_rtc_set_offset(emul, false, offset + 1);
88 memset(&cmp, '\0', sizeof(cmp));
89 ut_assertok(dm_rtc_get(dev, &cmp));
90 ut_asserteq(1, cmp.tm_sec);
92 /* Check against original offset */
93 sandbox_i2c_rtc_set_offset(emul, false, old_offset);
94 ut_assertok(dm_rtc_get(dev, &cmp));
95 ut_assertok(cmp_times(&now, &cmp, true));
97 /* Back to the original offset */
98 sandbox_i2c_rtc_set_offset(emul, false, 0);
99 memset(&cmp, '\0', sizeof(cmp));
100 ut_assertok(dm_rtc_get(dev, &cmp));
101 ut_assertok(cmp_times(&now, &cmp, true));
103 /* Increment the base time by 1 emul */
104 sandbox_i2c_rtc_get_set_base_time(emul, old_base_time + 1);
105 memset(&cmp, '\0', sizeof(cmp));
106 ut_assertok(dm_rtc_get(dev, &cmp));
107 if (now.tm_sec == 59) {
108 ut_asserteq(0, cmp.tm_sec);
110 ut_asserteq(now.tm_sec + 1, cmp.tm_sec);
113 old_offset = sandbox_i2c_rtc_set_offset(emul, true, 0);
117 DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
120 static int dm_test_rtc_reset(struct unit_test_state *uts)
123 struct udevice *dev, *emul;
124 long old_base_time, base_time;
126 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
127 ut_assertok(dm_rtc_get(dev, &now));
129 ut_assertok(i2c_emul_find(dev, &emul));
130 ut_assert(emul != NULL);
132 old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0);
134 ut_asserteq(0, sandbox_i2c_rtc_get_set_base_time(emul, -1));
136 /* Resetting the RTC should put he base time back to normal */
137 ut_assertok(dm_rtc_reset(dev));
138 base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
139 ut_asserteq(old_base_time, base_time);
143 DM_TEST(dm_test_rtc_reset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
145 /* Check that two RTC devices can be used independently */
146 static int dm_test_rtc_dual(struct unit_test_state *uts)
148 struct rtc_time now1, now2, cmp;
149 struct udevice *dev1, *dev2;
150 struct udevice *emul1, *emul2;
153 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev1));
154 ut_assertok(dm_rtc_get(dev1, &now1));
155 ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev2));
156 ut_assertok(dm_rtc_get(dev2, &now2));
158 ut_assertok(i2c_emul_find(dev1, &emul1));
159 ut_assert(emul1 != NULL);
160 ut_assertok(i2c_emul_find(dev2, &emul2));
161 ut_assert(emul2 != NULL);
163 offset = sandbox_i2c_rtc_set_offset(emul1, false, -1);
164 sandbox_i2c_rtc_set_offset(emul2, false, offset + 1);
165 memset(&cmp, '\0', sizeof(cmp));
166 ut_assertok(dm_rtc_get(dev2, &cmp));
167 ut_asserteq(-EINVAL, cmp_times(&now1, &cmp, false));
169 memset(&cmp, '\0', sizeof(cmp));
170 ut_assertok(dm_rtc_get(dev1, &cmp));
171 ut_assertok(cmp_times(&now1, &cmp, true));
175 DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);