summaryrefslogtreecommitdiff
path: root/test/dm/rtc.c
blob: dd037a6e17abad1b1ab3bf156b5900f05ed28f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015 Google, Inc
 * Written by Simon Glass <sjg@chromium.org>
 */

#include <common.h>
#include <console.h>
#include <dm.h>
#include <i2c.h>
#include <log.h>
#include <rtc.h>
#include <asm/io.h>
#include <asm/rtc.h>
#include <asm/test.h>
#include <dm/test.h>
#include <test/ut.h>

/* Simple RTC sanity check */
static int dm_test_rtc_base(struct unit_test_state *uts)
{
	struct udevice *dev;

	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_RTC, 2, &dev));
	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
	ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev));

	return 0;
}
DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

static void show_time(const char *msg, struct rtc_time *time)
{
	printf("%s: %02d/%02d/%04d %02d:%02d:%02d\n", msg,
	       time->tm_mday, time->tm_mon, time->tm_year,
	       time->tm_hour, time->tm_min, time->tm_sec);
}

static int cmp_times(struct rtc_time *expect, struct rtc_time *time, bool show)
{
	bool same;

	same = expect->tm_sec == time->tm_sec;
	same &= expect->tm_min == time->tm_min;
	same &= expect->tm_hour == time->tm_hour;
	same &= expect->tm_mday == time->tm_mday;
	same &= expect->tm_mon == time->tm_mon;
	same &= expect->tm_year == time->tm_year;
	if (!same && show) {
		show_time("expected", expect);
		show_time("actual", time);
	}

	return same ? 0 : -EINVAL;
}

/* Set and get the time */
static int dm_test_rtc_set_get(struct unit_test_state *uts)
{
	struct rtc_time now, time, cmp;
	struct udevice *dev, *emul;
	long offset, old_offset, old_base_time;

	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
	ut_assertok(dm_rtc_get(dev, &now));

	ut_assertok(i2c_emul_find(dev, &emul));
	ut_assert(emul != NULL);

	/* Tell the RTC to go into manual mode */
	old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
	old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);

	memset(&time, '\0', sizeof(time));
	time.tm_mday = 3;
	time.tm_mon = 6;
	time.tm_year = 2004;
	time.tm_sec = 0;
	time.tm_min = 18;
	time.tm_hour = 18;
	ut_assertok(dm_rtc_set(dev, &time));

	memset(&cmp, '\0', sizeof(cmp));
	ut_assertok(dm_rtc_get(dev, &cmp));
	ut_assertok(cmp_times(&time, &cmp, true));

	memset(&time, '\0', sizeof(time));
	time.tm_mday = 31;
	time.tm_mon = 8;
	time.tm_year = 2004;
	time.tm_sec = 0;
	time.tm_min = 18;
	time.tm_hour = 18;
	ut_assertok(dm_rtc_set(dev, &time));

	memset(&cmp, '\0', sizeof(cmp));
	ut_assertok(dm_rtc_get(dev, &cmp));
	ut_assertok(cmp_times(&time, &cmp, true));

	/* Increment by 1 second */
	offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
	sandbox_i2c_rtc_set_offset(emul, false, offset + 1);

	memset(&cmp, '\0', sizeof(cmp));
	ut_assertok(dm_rtc_get(dev, &cmp));
	ut_asserteq(1, cmp.tm_sec);

	/* Check against original offset */
	sandbox_i2c_rtc_set_offset(emul, false, old_offset);
	ut_assertok(dm_rtc_get(dev, &cmp));
	ut_assertok(cmp_times(&now, &cmp, true));

	/* Back to the original offset */
	sandbox_i2c_rtc_set_offset(emul, false, 0);
	memset(&cmp, '\0', sizeof(cmp));
	ut_assertok(dm_rtc_get(dev, &cmp));
	ut_assertok(cmp_times(&now, &cmp, true));

	/* Increment the base time by 1 emul */
	sandbox_i2c_rtc_get_set_base_time(emul, old_base_time + 1);
	memset(&cmp, '\0', sizeof(cmp));
	ut_assertok(dm_rtc_get(dev, &cmp));
	if (now.tm_sec == 59) {
		ut_asserteq(0, cmp.tm_sec);
	} else {
		ut_asserteq(now.tm_sec + 1, cmp.tm_sec);
	}

	old_offset = sandbox_i2c_rtc_set_offset(emul, true, 0);

	return 0;
}
DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

static int dm_test_rtc_read_write(struct unit_test_state *uts)
{
	struct rtc_time time;
	struct udevice *dev, *emul;
	long old_offset;
	u8 buf[4], reg;

	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));

	memcpy(buf, "car", 4);
	ut_assertok(dm_rtc_write(dev, REG_AUX0, buf, 4));
	memset(buf, '\0', sizeof(buf));
	ut_assertok(dm_rtc_read(dev, REG_AUX0, buf, 4));
	ut_asserteq(memcmp(buf, "car", 4), 0);

	reg = 'b';
	ut_assertok(dm_rtc_write(dev, REG_AUX0, &reg, 1));
	memset(buf, '\0', sizeof(buf));
	ut_assertok(dm_rtc_read(dev, REG_AUX0, buf, 4));
	ut_asserteq(memcmp(buf, "bar", 4), 0);

	reg = 't';
	ut_assertok(dm_rtc_write(dev, REG_AUX2, &reg, 1));
	memset(buf, '\0', sizeof(buf));
	ut_assertok(dm_rtc_read(dev, REG_AUX1, buf, 3));
	ut_asserteq(memcmp(buf, "at", 3), 0);

	ut_assertok(i2c_emul_find(dev, &emul));
	ut_assert(emul != NULL);

	old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
	ut_assertok(dm_rtc_get(dev, &time));

	ut_assertok(dm_rtc_read(dev, REG_SEC, &reg, 1));
	ut_asserteq(time.tm_sec, reg);
	ut_assertok(dm_rtc_read(dev, REG_MDAY, &reg, 1));
	ut_asserteq(time.tm_mday, reg);

	sandbox_i2c_rtc_set_offset(emul, true, old_offset);

	return 0;
}
DM_TEST(dm_test_rtc_read_write, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

/* Test 'rtc list' command */
static int dm_test_rtc_cmd_list(struct unit_test_state *uts)
{
	console_record_reset();

	run_command("rtc list", 0);
	ut_assert_nextline("RTC #0 - rtc@43");
	ut_assert_nextline("RTC #1 - rtc@61");
	ut_assert_console_end();

	return 0;
}
DM_TEST(dm_test_rtc_cmd_list, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

/* Test 'rtc read' and 'rtc write' commands */
static int dm_test_rtc_cmd_rw(struct unit_test_state *uts)
{
	console_record_reset();

	run_command("rtc dev 0", 0);
	ut_assert_nextline("RTC #0 - rtc@43");
	ut_assert_console_end();

	run_command("rtc write 0x30 aabb", 0);
	ut_assert_console_end();

	run_command("rtc read 0x30 2", 0);
	ut_assert_nextline("00000030: aa bb                                              ..");
	ut_assert_console_end();

	run_command("rtc dev 1", 0);
	ut_assert_nextline("RTC #1 - rtc@61");
	ut_assert_console_end();

	run_command("rtc write 0x30 ccdd", 0);
	ut_assert_console_end();

	run_command("rtc read 0x30 2", 0);
	ut_assert_nextline("00000030: cc dd                                              ..");
	ut_assert_console_end();

	/*
	 * Switch back to device #0, check that its aux registers
	 * still have the same values.
	 */
	run_command("rtc dev 0", 0);
	ut_assert_nextline("RTC #0 - rtc@43");
	ut_assert_console_end();

	run_command("rtc read 0x30 2", 0);
	ut_assert_nextline("00000030: aa bb                                              ..");
	ut_assert_console_end();

	return 0;
}
DM_TEST(dm_test_rtc_cmd_rw, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

/* Reset the time */
static int dm_test_rtc_reset(struct unit_test_state *uts)
{
	struct rtc_time now;
	struct udevice *dev, *emul;
	long old_base_time, base_time;

	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
	ut_assertok(dm_rtc_get(dev, &now));

	ut_assertok(i2c_emul_find(dev, &emul));
	ut_assert(emul != NULL);

	old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0);

	ut_asserteq(0, sandbox_i2c_rtc_get_set_base_time(emul, -1));

	/* Resetting the RTC should put he base time back to normal */
	ut_assertok(dm_rtc_reset(dev));
	base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
	ut_asserteq(old_base_time, base_time);

	return 0;
}
DM_TEST(dm_test_rtc_reset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

/* Check that two RTC devices can be used independently */
static int dm_test_rtc_dual(struct unit_test_state *uts)
{
	struct rtc_time now1, now2, cmp;
	struct udevice *dev1, *dev2;
	struct udevice *emul1, *emul2;
	long offset;

	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev1));
	ut_assertok(dm_rtc_get(dev1, &now1));
	ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev2));
	ut_assertok(dm_rtc_get(dev2, &now2));

	ut_assertok(i2c_emul_find(dev1, &emul1));
	ut_assert(emul1 != NULL);
	ut_assertok(i2c_emul_find(dev2, &emul2));
	ut_assert(emul2 != NULL);

	offset = sandbox_i2c_rtc_set_offset(emul1, false, -1);
	sandbox_i2c_rtc_set_offset(emul2, false, offset + 1);
	memset(&cmp, '\0', sizeof(cmp));
	ut_assertok(dm_rtc_get(dev2, &cmp));
	ut_asserteq(-EINVAL, cmp_times(&now1, &cmp, false));

	memset(&cmp, '\0', sizeof(cmp));
	ut_assertok(dm_rtc_get(dev1, &cmp));
	ut_assertok(cmp_times(&now1, &cmp, true));

	return 0;
}
DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);