blob: a8aa41955c22e32557157d538b1f1f5debf93af9 [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 Glass974dccd2020-07-28 19:41:12 -060030DM_TEST(dm_test_rtc_base, UT_TESTF_SCAN_PDATA | UT_TESTF_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 Glass974dccd2020-07-28 19:41:12 -0600145DM_TEST(dm_test_rtc_set_get, UT_TESTF_SCAN_PDATA | UT_TESTF_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 Glass974dccd2020-07-28 19:41:12 -0600189DM_TEST(dm_test_rtc_read_write, UT_TESTF_SCAN_PDATA | UT_TESTF_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{
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 Glass974dccd2020-07-28 19:41:12 -0600203DM_TEST(dm_test_rtc_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200204
205/* Test 'rtc read' and 'rtc write' commands */
206static 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 Glass85c8fc52021-05-08 07:00:00 -0600218 ut_assert_nextline("00000030: aa bb ..");
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200219 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 Glass85c8fc52021-05-08 07:00:00 -0600229 ut_assert_nextline("00000030: cc dd ..");
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200230 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 Glass85c8fc52021-05-08 07:00:00 -0600241 ut_assert_nextline("00000030: aa bb ..");
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200242 ut_assert_console_end();
243
244 return 0;
245}
Simon Glass974dccd2020-07-28 19:41:12 -0600246DM_TEST(dm_test_rtc_cmd_rw, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200247
Simon Glassc83c4b92015-04-20 12:37:31 -0600248/* Reset the time */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500249static int dm_test_rtc_reset(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600250{
251 struct rtc_time now;
252 struct udevice *dev, *emul;
253 long old_base_time, base_time;
Simon Glassb6e254d2022-08-01 07:58:48 -0600254 int i;
Simon Glassc83c4b92015-04-20 12:37:31 -0600255
256 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
257 ut_assertok(dm_rtc_get(dev, &now));
258
Simon Glass17b56f62018-11-18 08:14:34 -0700259 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glass89801552022-08-01 07:58:44 -0600260 ut_assertnonnull(emul);
Simon Glassc83c4b92015-04-20 12:37:31 -0600261
Simon Glassb6e254d2022-08-01 07:58:48 -0600262 i = 0;
263 do {
264 old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0);
Simon Glassc83c4b92015-04-20 12:37:31 -0600265
Simon Glassb6e254d2022-08-01 07:58:48 -0600266 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 Glassc83c4b92015-04-20 12:37:31 -0600270
Simon Glassb6e254d2022-08-01 07:58:48 -0600271 /*
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 Glassc83c4b92015-04-20 12:37:31 -0600280
281 return 0;
282}
Simon Glass974dccd2020-07-28 19:41:12 -0600283DM_TEST(dm_test_rtc_reset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassc83c4b92015-04-20 12:37:31 -0600284
285/* Check that two RTC devices can be used independently */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500286static int dm_test_rtc_dual(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600287{
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 Glass17b56f62018-11-18 08:14:34 -0700298 ut_assertok(i2c_emul_find(dev1, &emul1));
Simon Glass89801552022-08-01 07:58:44 -0600299 ut_assertnonnull(emul1);
Simon Glass17b56f62018-11-18 08:14:34 -0700300 ut_assertok(i2c_emul_find(dev2, &emul2));
Simon Glass89801552022-08-01 07:58:44 -0600301 ut_assertnonnull(emul2);
Simon Glassc83c4b92015-04-20 12:37:31 -0600302
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 Glass974dccd2020-07-28 19:41:12 -0600315DM_TEST(dm_test_rtc_dual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);