blob: 88f080b64a8380e711ca271d7fed29393d277174 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc83c4b92015-04-20 12:37:31 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glassc83c4b92015-04-20 12:37:31 -06004 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Rasmus Villemoes956c18f2020-07-06 22:01:20 +02007#include <console.h>
Simon Glassc83c4b92015-04-20 12:37:31 -06008#include <dm.h>
Simon Glass17b56f62018-11-18 08:14:34 -07009#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060011#include <rtc.h>
12#include <asm/io.h>
Rasmus Villemoes034b93b2020-07-06 22:01:18 +020013#include <asm/rtc.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060014#include <asm/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050015#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060016#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050017#include <test/ut.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060018
19/* Simple RTC sanity check */
Joe Hershberger3a77be52015-05-20 14:27:27 -050020static int dm_test_rtc_base(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -060021{
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 Glass1a92f832024-08-22 07:57:48 -060030DM_TEST(dm_test_rtc_base, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassc83c4b92015-04-20 12:37:31 -060031
32static 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
39static 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 Hershberger3a77be52015-05-20 14:27:27 -050058static int dm_test_rtc_set_get(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -060059{
60 struct rtc_time now, time, cmp;
61 struct udevice *dev, *emul;
Simon Glass277bab82022-08-01 07:58:47 -060062 long offset, check_offset, old_offset, old_base_time;
63 int i;
Simon Glassc83c4b92015-04-20 12:37:31 -060064
65 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
Simon Glassc83c4b92015-04-20 12:37:31 -060066
Simon Glass17b56f62018-11-18 08:14:34 -070067 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glass89801552022-08-01 07:58:44 -060068 ut_assertnonnull(emul);
Simon Glassc83c4b92015-04-20 12:37:31 -060069
Simon Glass277bab82022-08-01 07:58:47 -060070 /* 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 Glassc83c4b92015-04-20 12:37:31 -060083 old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
84
85 memset(&time, '\0', sizeof(time));
Rasmus Villemoesacffbea2020-07-06 22:01:16 +020086 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 Glassc83c4b92015-04-20 12:37:31 -0600100 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 Glass277bab82022-08-01 07:58:47 -0600140 /* return RTC to normal mode */
141 sandbox_i2c_rtc_set_offset(emul, true, 0);
Simon Glassc83c4b92015-04-20 12:37:31 -0600142
143 return 0;
144}
Simon Glass1a92f832024-08-22 07:57:48 -0600145DM_TEST(dm_test_rtc_set_get, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassc83c4b92015-04-20 12:37:31 -0600146
Rasmus Villemoes034b93b2020-07-06 22:01:18 +0200147static 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, &reg, 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, &reg, 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 Glass89801552022-08-01 07:58:44 -0600175 ut_assertnonnull(emul);
Rasmus Villemoes034b93b2020-07-06 22:01:18 +0200176
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, &reg, 1));
181 ut_asserteq(time.tm_sec, reg);
182 ut_assertok(dm_rtc_read(dev, REG_MDAY, &reg, 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 Glass1a92f832024-08-22 07:57:48 -0600189DM_TEST(dm_test_rtc_read_write, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Rasmus Villemoes034b93b2020-07-06 22:01:18 +0200190
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200191/* Test 'rtc list' command */
192static int dm_test_rtc_cmd_list(struct unit_test_state *uts)
193{
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200194 run_command("rtc list", 0);
195 ut_assert_nextline("RTC #0 - rtc@43");
196 ut_assert_nextline("RTC #1 - rtc@61");
197 ut_assert_console_end();
198
199 return 0;
200}
Simon Glassf54458b2024-08-22 07:57:59 -0600201DM_TEST(dm_test_rtc_cmd_list, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200202
203/* Test 'rtc read' and 'rtc write' commands */
204static int dm_test_rtc_cmd_rw(struct unit_test_state *uts)
205{
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200206 run_command("rtc dev 0", 0);
207 ut_assert_nextline("RTC #0 - rtc@43");
208 ut_assert_console_end();
209
210 run_command("rtc write 0x30 aabb", 0);
211 ut_assert_console_end();
212
213 run_command("rtc read 0x30 2", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600214 ut_assert_nextline("00000030: aa bb ..");
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200215 ut_assert_console_end();
216
217 run_command("rtc dev 1", 0);
218 ut_assert_nextline("RTC #1 - rtc@61");
219 ut_assert_console_end();
220
221 run_command("rtc write 0x30 ccdd", 0);
222 ut_assert_console_end();
223
224 run_command("rtc read 0x30 2", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600225 ut_assert_nextline("00000030: cc dd ..");
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200226 ut_assert_console_end();
227
228 /*
229 * Switch back to device #0, check that its aux registers
230 * still have the same values.
231 */
232 run_command("rtc dev 0", 0);
233 ut_assert_nextline("RTC #0 - rtc@43");
234 ut_assert_console_end();
235
236 run_command("rtc read 0x30 2", 0);
Simon Glass85c8fc52021-05-08 07:00:00 -0600237 ut_assert_nextline("00000030: aa bb ..");
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200238 ut_assert_console_end();
239
240 return 0;
241}
Simon Glassf54458b2024-08-22 07:57:59 -0600242DM_TEST(dm_test_rtc_cmd_rw, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200243
Simon Glassc83c4b92015-04-20 12:37:31 -0600244/* Reset the time */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500245static int dm_test_rtc_reset(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600246{
247 struct rtc_time now;
248 struct udevice *dev, *emul;
249 long old_base_time, base_time;
Simon Glassb6e254d2022-08-01 07:58:48 -0600250 int i;
Simon Glassc83c4b92015-04-20 12:37:31 -0600251
252 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
253 ut_assertok(dm_rtc_get(dev, &now));
254
Simon Glass17b56f62018-11-18 08:14:34 -0700255 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glass89801552022-08-01 07:58:44 -0600256 ut_assertnonnull(emul);
Simon Glassc83c4b92015-04-20 12:37:31 -0600257
Simon Glassb6e254d2022-08-01 07:58:48 -0600258 i = 0;
259 do {
260 old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0);
Simon Glassc83c4b92015-04-20 12:37:31 -0600261
Simon Glassb6e254d2022-08-01 07:58:48 -0600262 ut_asserteq(0, sandbox_i2c_rtc_get_set_base_time(emul, -1));
263
264 ut_assertok(dm_rtc_reset(dev));
265 base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
Simon Glassc83c4b92015-04-20 12:37:31 -0600266
Simon Glassb6e254d2022-08-01 07:58:48 -0600267 /*
268 * Resetting the RTC should put the base time back to normal.
269 * Allow for a one-timeadjustment in case the time flips over
270 * while this test process is pre-empted (either by a second
271 * or a daylight-saving change), since reset_time() in
272 * i2c_rtc_emul.c reads the time from the OS.
273 */
274 } while (++i < 2 && base_time != old_base_time);
275 ut_asserteq(old_base_time, base_time);
Simon Glassc83c4b92015-04-20 12:37:31 -0600276
277 return 0;
278}
Simon Glass1a92f832024-08-22 07:57:48 -0600279DM_TEST(dm_test_rtc_reset, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassc83c4b92015-04-20 12:37:31 -0600280
281/* Check that two RTC devices can be used independently */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500282static int dm_test_rtc_dual(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600283{
284 struct rtc_time now1, now2, cmp;
285 struct udevice *dev1, *dev2;
286 struct udevice *emul1, *emul2;
287 long offset;
288
289 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev1));
290 ut_assertok(dm_rtc_get(dev1, &now1));
291 ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev2));
292 ut_assertok(dm_rtc_get(dev2, &now2));
293
Simon Glass17b56f62018-11-18 08:14:34 -0700294 ut_assertok(i2c_emul_find(dev1, &emul1));
Simon Glass89801552022-08-01 07:58:44 -0600295 ut_assertnonnull(emul1);
Simon Glass17b56f62018-11-18 08:14:34 -0700296 ut_assertok(i2c_emul_find(dev2, &emul2));
Simon Glass89801552022-08-01 07:58:44 -0600297 ut_assertnonnull(emul2);
Simon Glassc83c4b92015-04-20 12:37:31 -0600298
299 offset = sandbox_i2c_rtc_set_offset(emul1, false, -1);
300 sandbox_i2c_rtc_set_offset(emul2, false, offset + 1);
301 memset(&cmp, '\0', sizeof(cmp));
302 ut_assertok(dm_rtc_get(dev2, &cmp));
303 ut_asserteq(-EINVAL, cmp_times(&now1, &cmp, false));
304
305 memset(&cmp, '\0', sizeof(cmp));
306 ut_assertok(dm_rtc_get(dev1, &cmp));
307 ut_assertok(cmp_times(&now1, &cmp, true));
308
309 return 0;
310}
Simon Glass1a92f832024-08-22 07:57:48 -0600311DM_TEST(dm_test_rtc_dual, UTF_SCAN_PDATA | UTF_SCAN_FDT);