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