xref: /openbmc/u-boot/test/dm/rtc.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <rtc.h>
10 #include <asm/io.h>
11 #include <asm/test.h>
12 #include <dm/test.h>
13 #include <test/ut.h>
14 
15 /* Simple RTC sanity check */
16 static int dm_test_rtc_base(struct unit_test_state *uts)
17 {
18 	struct udevice *dev;
19 
20 	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_RTC, 2, &dev));
21 	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
22 	ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev));
23 
24 	return 0;
25 }
26 DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
27 
28 static void show_time(const char *msg, struct rtc_time *time)
29 {
30 	printf("%s: %02d/%02d/%04d %02d:%02d:%02d\n", msg,
31 	       time->tm_mday, time->tm_mon, time->tm_year,
32 	       time->tm_hour, time->tm_min, time->tm_sec);
33 }
34 
35 static int cmp_times(struct rtc_time *expect, struct rtc_time *time, bool show)
36 {
37 	bool same;
38 
39 	same = expect->tm_sec == time->tm_sec;
40 	same &= expect->tm_min == time->tm_min;
41 	same &= expect->tm_hour == time->tm_hour;
42 	same &= expect->tm_mday == time->tm_mday;
43 	same &= expect->tm_mon == time->tm_mon;
44 	same &= expect->tm_year == time->tm_year;
45 	if (!same && show) {
46 		show_time("expected", expect);
47 		show_time("actual", time);
48 	}
49 
50 	return same ? 0 : -EINVAL;
51 }
52 
53 /* Set and get the time */
54 static int dm_test_rtc_set_get(struct unit_test_state *uts)
55 {
56 	struct rtc_time now, time, cmp;
57 	struct udevice *dev, *emul;
58 	long offset, old_offset, old_base_time;
59 
60 	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
61 	ut_assertok(dm_rtc_get(dev, &now));
62 
63 	ut_assertok(device_find_first_child(dev, &emul));
64 	ut_assert(emul != NULL);
65 
66 	/* Tell the RTC to go into manual mode */
67 	old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
68 	old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
69 
70 	memset(&time, '\0', sizeof(time));
71 	time.tm_mday = 25;
72 	time.tm_mon = 8;
73 	time.tm_year = 2004;
74 	time.tm_sec = 0;
75 	time.tm_min = 18;
76 	time.tm_hour = 18;
77 	ut_assertok(dm_rtc_set(dev, &time));
78 
79 	memset(&cmp, '\0', sizeof(cmp));
80 	ut_assertok(dm_rtc_get(dev, &cmp));
81 	ut_assertok(cmp_times(&time, &cmp, true));
82 
83 	/* Increment by 1 second */
84 	offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
85 	sandbox_i2c_rtc_set_offset(emul, false, offset + 1);
86 
87 	memset(&cmp, '\0', sizeof(cmp));
88 	ut_assertok(dm_rtc_get(dev, &cmp));
89 	ut_asserteq(1, cmp.tm_sec);
90 
91 	/* Check against original offset */
92 	sandbox_i2c_rtc_set_offset(emul, false, old_offset);
93 	ut_assertok(dm_rtc_get(dev, &cmp));
94 	ut_assertok(cmp_times(&now, &cmp, true));
95 
96 	/* Back to the original offset */
97 	sandbox_i2c_rtc_set_offset(emul, false, 0);
98 	memset(&cmp, '\0', sizeof(cmp));
99 	ut_assertok(dm_rtc_get(dev, &cmp));
100 	ut_assertok(cmp_times(&now, &cmp, true));
101 
102 	/* Increment the base time by 1 emul */
103 	sandbox_i2c_rtc_get_set_base_time(emul, old_base_time + 1);
104 	memset(&cmp, '\0', sizeof(cmp));
105 	ut_assertok(dm_rtc_get(dev, &cmp));
106 	if (now.tm_sec == 59) {
107 		ut_asserteq(0, cmp.tm_sec);
108 	} else {
109 		ut_asserteq(now.tm_sec + 1, cmp.tm_sec);
110 	}
111 
112 	old_offset = sandbox_i2c_rtc_set_offset(emul, true, 0);
113 
114 	return 0;
115 }
116 DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
117 
118 /* Reset the time */
119 static int dm_test_rtc_reset(struct unit_test_state *uts)
120 {
121 	struct rtc_time now;
122 	struct udevice *dev, *emul;
123 	long old_base_time, base_time;
124 
125 	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
126 	ut_assertok(dm_rtc_get(dev, &now));
127 
128 	ut_assertok(device_find_first_child(dev, &emul));
129 	ut_assert(emul != NULL);
130 
131 	old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0);
132 
133 	ut_asserteq(0, sandbox_i2c_rtc_get_set_base_time(emul, -1));
134 
135 	/* Resetting the RTC should put he base time back to normal */
136 	ut_assertok(dm_rtc_reset(dev));
137 	base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
138 	ut_asserteq(old_base_time, base_time);
139 
140 	return 0;
141 }
142 DM_TEST(dm_test_rtc_reset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
143 
144 /* Check that two RTC devices can be used independently */
145 static int dm_test_rtc_dual(struct unit_test_state *uts)
146 {
147 	struct rtc_time now1, now2, cmp;
148 	struct udevice *dev1, *dev2;
149 	struct udevice *emul1, *emul2;
150 	long offset;
151 
152 	ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev1));
153 	ut_assertok(dm_rtc_get(dev1, &now1));
154 	ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev2));
155 	ut_assertok(dm_rtc_get(dev2, &now2));
156 
157 	ut_assertok(device_find_first_child(dev1, &emul1));
158 	ut_assert(emul1 != NULL);
159 	ut_assertok(device_find_first_child(dev2, &emul2));
160 	ut_assert(emul2 != NULL);
161 
162 	offset = sandbox_i2c_rtc_set_offset(emul1, false, -1);
163 	sandbox_i2c_rtc_set_offset(emul2, false, offset + 1);
164 	memset(&cmp, '\0', sizeof(cmp));
165 	ut_assertok(dm_rtc_get(dev2, &cmp));
166 	ut_asserteq(-EINVAL, cmp_times(&now1, &cmp, false));
167 
168 	memset(&cmp, '\0', sizeof(cmp));
169 	ut_assertok(dm_rtc_get(dev1, &cmp));
170 	ut_assertok(cmp_times(&now1, &cmp, true));
171 
172 	return 0;
173 }
174 DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
175