1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include "qemu/osdep.h"
18#include "libqtest-single.h"
19
20
21
22
23
24#define TIMER_BASE 0x48000000
25
26
27#define PERIPHNSPPC0 (0x50080000 + 0x70)
28
29
30#define COUNTER_BASE 0x58100000
31
32
33#define CNTCR 0
34#define CNTSR 0x4
35#define CNTCV_LO 0x8
36#define CNTCV_HI 0xc
37#define CNTSCR 0x10
38
39
40#define CNTPCT_LO 0
41#define CNTPCT_HI 4
42#define CNTFRQ 0x10
43#define CNTP_CVAL_LO 0x20
44#define CNTP_CVAL_HI 0x24
45#define CNTP_TVAL 0x28
46#define CNTP_CTL 0x2c
47#define CNTP_AIVAL_LO 0x40
48#define CNTP_AIVAL_HI 0x44
49#define CNTP_AIVAL_RELOAD 0x48
50#define CNTP_AIVAL_CTL 0x4c
51
52
53#define FOUR_TICKS 125
54
55static void clock_step_ticks(uint64_t ticks)
56{
57
58
59
60
61
62 assert(!(ticks & 3));
63 clock_step(FOUR_TICKS * (ticks >> 2));
64}
65
66static void reset_counter_and_timer(void)
67{
68
69
70
71
72 writel(COUNTER_BASE + CNTCR, 0);
73 writel(TIMER_BASE + CNTP_CTL, 0);
74 writel(TIMER_BASE + CNTP_AIVAL_CTL, 0);
75 writel(COUNTER_BASE + CNTCV_LO, 0);
76 writel(COUNTER_BASE + CNTCV_HI, 0);
77}
78
79static void test_counter(void)
80{
81
82
83 reset_counter_and_timer();
84
85 clock_step_ticks(100);
86 g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_LO), ==, 0);
87 g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_HI), ==, 0);
88
89 writel(COUNTER_BASE + CNTCR, 1);
90 clock_step_ticks(100);
91 g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_LO), ==, 100);
92 g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_HI), ==, 0);
93
94 writel(COUNTER_BASE + CNTCR, 0);
95 writel(COUNTER_BASE + CNTSCR, 0x00100000);
96 writel(COUNTER_BASE + CNTCR, 5);
97 clock_step_ticks(160);
98 g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_LO), ==, 110);
99 g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_HI), ==, 0);
100}
101
102static void test_timer(void)
103{
104
105
106 reset_counter_and_timer();
107
108
109
110
111
112
113 writel(PERIPHNSPPC0, 1);
114
115 writel(COUNTER_BASE + CNTCR, 1);
116
117
118 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 0);
119 g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_LO), ==, 0);
120 g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_HI), ==, 0);
121
122
123 writel(TIMER_BASE + CNTP_CTL, 1);
124
125
126 clock_step_ticks(100);
127 g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_LO), ==, 100);
128 g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_HI), ==, 0);
129
130
131 writel(TIMER_BASE + CNTP_CVAL_LO, 4000);
132 writel(TIMER_BASE + CNTP_CVAL_HI, 0);
133
134
135 g_assert_cmpuint(readl(TIMER_BASE + CNTP_TVAL), ==, 3900);
136
137
138 clock_step_ticks(3900);
139 g_assert_cmpuint(readl(TIMER_BASE + CNTP_TVAL), ==, 0);
140 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5);
141
142
143 writel(TIMER_BASE + CNTP_AIVAL_RELOAD, 200);
144 writel(TIMER_BASE + CNTP_AIVAL_CTL, 1);
145
146
147 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4200);
148 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0);
149 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1);
150
151
152
153
154
155 clock_step_ticks(100);
156 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1);
157 clock_step_ticks(100);
158 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5);
159 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4400);
160 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0);
161
162 clock_step_ticks(100);
163 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5);
164
165 writel(TIMER_BASE + CNTP_AIVAL_CTL, 1);
166 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1);
167
168 clock_step_ticks(100);
169 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5);
170 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4600);
171 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0);
172
173
174
175
176
177
178
179
180
181 writel(TIMER_BASE + CNTP_AIVAL_CTL, 0);
182 clock_step_ticks(0x42ULL << 32);
183 g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_LO), ==, 4400);
184 g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_HI), ==, 0x42);
185
186
187 writel(TIMER_BASE + CNTP_AIVAL_CTL, 1);
188 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4600);
189 g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0x42);
190}
191
192static void test_timer_scale_change(void)
193{
194
195
196
197
198 reset_counter_and_timer();
199
200 writel(PERIPHNSPPC0, 1);
201 writel(COUNTER_BASE + CNTCR, 1);
202 writel(TIMER_BASE + CNTP_CTL, 1);
203
204 writel(TIMER_BASE + CNTP_CVAL_LO, 4000);
205 writel(TIMER_BASE + CNTP_CVAL_HI, 0);
206
207 clock_step_ticks(2000);
208 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1);
209
210 writel(COUNTER_BASE + CNTCR, 0);
211 writel(COUNTER_BASE + CNTSCR, 0x00100000);
212 writel(COUNTER_BASE + CNTCR, 5);
213
214 clock_step_ticks(2000);
215 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1);
216
217 clock_step_ticks(29996);
218 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1);
219 clock_step_ticks(4);
220 g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5);
221}
222
223int main(int argc, char **argv)
224{
225 int r;
226
227 g_test_init(&argc, &argv, NULL);
228
229 qtest_start("-machine mps3-an547");
230
231 qtest_add_func("/sse-timer/counter", test_counter);
232 qtest_add_func("/sse-timer/timer", test_timer);
233 qtest_add_func("/sse-timer/timer-scale-change", test_timer_scale_change);
234
235 r = g_test_run();
236
237 qtest_end();
238
239 return r;
240}
241