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