blob: 71887427647b1540ed6866ca32614a26c5a47ef1 [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>
8#include <dm.h>
Simon Glass17b56f62018-11-18 08:14:34 -07009#include <i2c.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060010#include <rtc.h>
11#include <asm/io.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060012#include <asm/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050013#include <dm/test.h>
14#include <test/ut.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060015
16/* Simple RTC sanity check */
Joe Hershberger3a77be52015-05-20 14:27:27 -050017static int dm_test_rtc_base(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -060018{
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}
27DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
28
29static 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
36static 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 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050055static int dm_test_rtc_set_get(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -060056{
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
Simon Glass17b56f62018-11-18 08:14:34 -070064 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glassc83c4b92015-04-20 12:37:31 -060065 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}
117DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
118
119/* Reset the time */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500120static int dm_test_rtc_reset(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600121{
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
Simon Glass17b56f62018-11-18 08:14:34 -0700129 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glassc83c4b92015-04-20 12:37:31 -0600130 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}
143DM_TEST(dm_test_rtc_reset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
144
145/* Check that two RTC devices can be used independently */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500146static int dm_test_rtc_dual(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600147{
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
Simon Glass17b56f62018-11-18 08:14:34 -0700158 ut_assertok(i2c_emul_find(dev1, &emul1));
Simon Glassc83c4b92015-04-20 12:37:31 -0600159 ut_assert(emul1 != NULL);
Simon Glass17b56f62018-11-18 08:14:34 -0700160 ut_assertok(i2c_emul_find(dev2, &emul2));
Simon Glassc83c4b92015-04-20 12:37:31 -0600161 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}
175DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);