1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-12-20 GuEe-GUI first version
* 2022-08-24 GuEe-GUI Add OFW support
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
/* support registers access and timer registers in libcpu */
#include <cpu.h>
#include <cpuport.h>
typedef void (*timer_ctrl_handle)(rt_bool_t enable);
typedef rt_uint64_t (*timer_value_handle)(rt_uint64_t val);
static volatile rt_uint64_t timer_step;
static int arm_arch_timer_irq = -1;
static timer_ctrl_handle arm_arch_timer_ctrl_handle = RT_NULL;
static timer_value_handle arm_arch_timer_value_handle = RT_NULL;
/* CTL */
static void mon_ptimer_ctrl(rt_bool_t enable)
{
rt_hw_sysreg_write(CNTPS_CTL, !!enable);
}
static void hyp_s_ptimer_ctrl(rt_bool_t enable)
{
#if ARCH_ARMV8_EXTENSIONS > 1
rt_hw_sysreg_write(CNTHPS_CTL, !!enable);
#endif
}
static void hyp_ns_ptimer_ctrl(rt_bool_t enable)
{
rt_hw_sysreg_write(CNTHP_CTL, !!enable);
}
static void hyp_s_vtimer_ctrl(rt_bool_t enable)
{
#if ARCH_ARMV8_EXTENSIONS > 1
rt_hw_sysreg_write(CNTHVS_CTL, !!enable);
#endif
}
static void hyp_ns_vtimer_ctrl(rt_bool_t enable)
{
#if ARCH_ARMV8_EXTENSIONS > 1
rt_hw_sysreg_write(CNTHV_CTL, !!enable);
#endif
}
static void os_ptimer_ctrl(rt_bool_t enable)
{
rt_hw_sysreg_write(CNTP_CTL, !!enable);
}
static void os_vtimer_ctrl(rt_bool_t enable)
{
rt_hw_sysreg_write(CNTV_CTL, !!enable);
}
/* TVAL */
static rt_uint64_t mon_ptimer_value(rt_uint64_t val)
{
if (val)
{
rt_hw_sysreg_write(CNTPS_TVAL, val);
}
else
{
rt_hw_sysreg_read(CNTPS_TVAL, val);
}
return val;
}
static rt_uint64_t hyp_s_ptimer_value(rt_uint64_t val)
{
#if ARCH_ARMV8_EXTENSIONS > 1
if (val)
{
rt_hw_sysreg_write(CNTHPS_TVAL, val);
}
else
{
rt_hw_sysreg_read(CNTHPS_TVAL, val);
}
return val;
#else
return 0;
#endif
}
static rt_uint64_t hyp_ns_ptimer_value(rt_uint64_t val)
{
if (val)
{
rt_hw_sysreg_write(CNTHP_TVAL, val);
}
else
{
rt_hw_sysreg_read(CNTHP_TVAL, val);
}
return val;
}
static rt_uint64_t hyp_s_vtimer_value(rt_uint64_t val)
{
#if ARCH_ARMV8_EXTENSIONS > 1
if (val)
{
rt_hw_sysreg_write(CNTHVS_TVAL, val);
}
else
{
rt_hw_sysreg_read(CNTHVS_TVAL, val);
}
return val;
#else
return 0;
#endif
}
static rt_uint64_t hyp_ns_vtimer_value(rt_uint64_t val)
{
#if ARCH_ARMV8_EXTENSIONS > 1
if (val)
{
rt_hw_sysreg_write(CNTHV_TVAL, val);
}
else
{
rt_hw_sysreg_read(CNTHV_TVAL, val);
}
return val;
#else
return 0;
#endif
}
static rt_uint64_t os_ptimer_value(rt_uint64_t val)
{
if (val)
{
rt_hw_sysreg_write(CNTP_TVAL, val);
}
else
{
rt_hw_sysreg_read(CNTP_TVAL, val);
}
return val;
}
static rt_uint64_t os_vtimer_value(rt_uint64_t val)
{
if (val)
{
rt_hw_sysreg_write(CNTV_TVAL, val);
}
else
{
rt_hw_sysreg_read(CNTV_TVAL, val);
}
return val;
}
static timer_ctrl_handle ctrl_handle[] =
{
mon_ptimer_ctrl,
hyp_s_ptimer_ctrl,
hyp_ns_ptimer_ctrl,
hyp_s_vtimer_ctrl,
hyp_ns_vtimer_ctrl,
os_ptimer_ctrl,
os_vtimer_ctrl,
};
static timer_value_handle value_handle[] =
{
mon_ptimer_value,
hyp_s_ptimer_value,
hyp_ns_ptimer_value,
hyp_s_vtimer_value,
hyp_ns_vtimer_value,
os_ptimer_value,
os_vtimer_value,
};
static rt_err_t arm_arch_timer_local_enable(void)
{
rt_err_t ret = RT_EOK;
if (arm_arch_timer_irq >= 0)
{
arm_arch_timer_ctrl_handle(RT_FALSE);
arm_arch_timer_value_handle(timer_step);
rt_hw_interrupt_umask(arm_arch_timer_irq);
arm_arch_timer_ctrl_handle(RT_TRUE);
}
else
{
ret = -RT_ENOSYS;
}
return ret;
}
rt_used
static rt_err_t arm_arch_timer_local_disable(void)
{
rt_err_t ret = RT_EOK;
if (arm_arch_timer_ctrl_handle)
{
arm_arch_timer_ctrl_handle(RT_FALSE);
rt_hw_interrupt_mask(arm_arch_timer_irq);
}
else
{
ret = -RT_ENOSYS;
}
return ret;
}
rt_used
static rt_err_t arm_arch_timer_set_frequency(rt_uint64_t frq)
{
rt_err_t ret = RT_EOK;
#ifdef ARCH_SUPPORT_TEE
rt_hw_isb();
rt_hw_sysreg_write(CNTFRQ, frq);
rt_hw_dsb();
#else
ret = -RT_ENOSYS;
#endif
return ret;
}
rt_used
static rt_uint64_t arm_arch_timer_get_frequency(void)
{
rt_uint64_t frq;
rt_hw_isb();
rt_hw_sysreg_read(CNTFRQ, frq);
rt_hw_isb();
return frq;
}
rt_used
static rt_err_t arm_arch_timer_set_value(rt_uint64_t val)
{
rt_err_t ret = RT_EOK;
if (arm_arch_timer_value_handle)
{
val = arm_arch_timer_value_handle(val);
}
else
{
ret = -RT_ENOSYS;
}
return ret;
}
rt_used
static rt_uint64_t arm_arch_timer_get_value(void)
{
rt_uint64_t val = 0;
if (arm_arch_timer_value_handle)
{
val = arm_arch_timer_value_handle(0);
}
return val;
}
static void arm_arch_timer_isr(int vector, void *param)
{
arm_arch_timer_set_value(timer_step);
rt_tick_increase();
}
static int arm_arch_timer_post_init(void)
{
arm_arch_timer_local_enable();
return 0;
}
INIT_SECONDARY_CPU_EXPORT(arm_arch_timer_post_init);
static rt_err_t arm_arch_timer_probe(struct rt_platform_device *pdev)
{
int mode_idx, irq_idx;
const char *irq_name[] =
{
"phys", /* Secure Phys IRQ */
"virt", /* Non-secure Phys IRQ */
"hyp-phys", /* Virt IRQ */
"hyp-virt", /* Hyp IRQ */
};
#if defined(ARCH_SUPPORT_TEE)
mode_idx = 0;
irq_idx = 0;
#elif defined(ARCH_SUPPORT_HYP)
mode_idx = 2;
irq_idx = 3;
#else
mode_idx = 5;
irq_idx = 1;
#endif
arm_arch_timer_irq = rt_dm_dev_get_irq_by_name(&pdev->parent, irq_name[irq_idx]);
if (arm_arch_timer_irq < 0)
{
arm_arch_timer_irq = rt_dm_dev_get_irq(&pdev->parent, irq_idx);
}
if (arm_arch_timer_irq < 0)
{
return -RT_EEMPTY;
}
arm_arch_timer_ctrl_handle = ctrl_handle[mode_idx];
arm_arch_timer_value_handle = value_handle[mode_idx];
rt_hw_interrupt_install(arm_arch_timer_irq, arm_arch_timer_isr, RT_NULL, "tick-arm-timer");
timer_step = arm_arch_timer_get_frequency() / RT_TICK_PER_SECOND;
arm_arch_timer_local_enable();
return RT_EOK;
}
static const struct rt_ofw_node_id arm_arch_timer_ofw_ids[] =
{
{ .compatible = "arm,armv7-timer", },
{ .compatible = "arm,armv8-timer", },
{ /* sentinel */ }
};
static struct rt_platform_driver arm_arch_timer_driver =
{
.name = "arm-arch-timer",
.ids = arm_arch_timer_ofw_ids,
.probe = arm_arch_timer_probe,
};
static int arm_arch_timer_drv_register(void)
{
rt_platform_driver_register(&arm_arch_timer_driver);
return 0;
}
INIT_SUBSYS_EXPORT(arm_arch_timer_drv_register);
|