blob: 88f86581cce77c446e98bef1e7c49f79a54e6982 [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 Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060011#include <rtc.h>
12#include <asm/io.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060013#include <asm/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050014#include <dm/test.h>
15#include <test/ut.h>
Simon Glassc83c4b92015-04-20 12:37:31 -060016
17/* Simple RTC sanity check */
Joe Hershberger3a77be52015-05-20 14:27:27 -050018static int dm_test_rtc_base(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -060019{
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}
28DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
29
30static 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
37static 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 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050056static int dm_test_rtc_set_get(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -060057{
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
Simon Glass17b56f62018-11-18 08:14:34 -070065 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glassc83c4b92015-04-20 12:37:31 -060066 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}
118DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
119
120/* Reset the time */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500121static int dm_test_rtc_reset(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600122{
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
Simon Glass17b56f62018-11-18 08:14:34 -0700130 ut_assertok(i2c_emul_find(dev, &emul));
Simon Glassc83c4b92015-04-20 12:37:31 -0600131 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}
144DM_TEST(dm_test_rtc_reset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
145
146/* Check that two RTC devices can be used independently */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500147static int dm_test_rtc_dual(struct unit_test_state *uts)
Simon Glassc83c4b92015-04-20 12:37:31 -0600148{
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
Simon Glass17b56f62018-11-18 08:14:34 -0700159 ut_assertok(i2c_emul_find(dev1, &emul1));
Simon Glassc83c4b92015-04-20 12:37:31 -0600160 ut_assert(emul1 != NULL);
Simon Glass17b56f62018-11-18 08:14:34 -0700161 ut_assertok(i2c_emul_find(dev2, &emul2));
Simon Glassc83c4b92015-04-20 12:37:31 -0600162 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}
176DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);