blob: dd037a6e17abad1b1ab3bf156b5900f05ed28f7e [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
7#include <common.h>
Rasmus Villemoes956c18f2020-07-06 22:01:20 +02008#include <console.h>
Simon Glassc83c4b92015-04-20 12:37:31 -06009#include <dm.h>
Simon Glass17b56f62018-11-18 08:14:34 -070010#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060012#include <rtc.h>
13#include <asm/io.h>
Rasmus Villemoes034b93b2020-07-06 22:01:18 +020014#include <asm/rtc.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060015#include <asm/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050016#include <dm/test.h>
17#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}
30DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
31
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;
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 Glass17b56f62018-11-18 08:14:34 -070067 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glassc83c4b92015-04-20 12:37:31 -060068 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 Villemoesacffbea2020-07-06 22:01:16 +020075 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 Glassc83c4b92015-04-20 12:37:31 -060089 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}
133DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
134
Rasmus Villemoes034b93b2020-07-06 22:01:18 +0200135static 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, &reg, 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, &reg, 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, &reg, 1));
169 ut_asserteq(time.tm_sec, reg);
170 ut_assertok(dm_rtc_read(dev, REG_MDAY, &reg, 1));
171 ut_asserteq(time.tm_mday, reg);
172
173 sandbox_i2c_rtc_set_offset(emul, true, old_offset);
174
175 return 0;
176}
177DM_TEST(dm_test_rtc_read_write, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
178
Rasmus Villemoes956c18f2020-07-06 22:01:20 +0200179/* Test 'rtc list' command */
180static 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}
191DM_TEST(dm_test_rtc_cmd_list, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
192
193/* Test 'rtc read' and 'rtc write' commands */
194static 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}
234DM_TEST(dm_test_rtc_cmd_rw, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
235
Simon Glassc83c4b92015-04-20 12:37:31 -0600236/* Reset the time */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500237static int dm_test_rtc_reset(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600238{
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 Glass17b56f62018-11-18 08:14:34 -0700246 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glassc83c4b92015-04-20 12:37:31 -0600247 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}
260DM_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 Hershberger3a77be52015-05-20 14:27:27 -0500263static int dm_test_rtc_dual(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600264{
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 Glass17b56f62018-11-18 08:14:34 -0700275 ut_assertok(i2c_emul_find(dev1, &emul1));
Simon Glassc83c4b92015-04-20 12:37:31 -0600276 ut_assert(emul1 != NULL);
Simon Glass17b56f62018-11-18 08:14:34 -0700277 ut_assertok(i2c_emul_find(dev2, &emul2));
Simon Glassc83c4b92015-04-20 12:37:31 -0600278 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}
292DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);