blob: 3f7f135ca944888e618ef8606cdcfb6ba8c6659e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <sys/types.h>
24#include <err.h>
25#include <kernel/thread.h>
26#include <platform.h>
27#include <platform/interrupts.h>
28#include <platform/timer.h>
29#include <platform/armemu.h>
30#include "platform_p.h"
31
32static platform_timer_callback t_callback;
33
34status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval)
35{
36 t_callback = callback;
37
38 *REG(PIT_CLEAR) = 1;
39 *REG(PIT_INTERVAL) = interval;
40 *REG(PIT_START_PERIODIC) = 1;
41
42 unmask_interrupt(INT_PIT);
43
44 return NO_ERROR;
45}
46
47lk_bigtime_t current_time_hires(void)
48{
49 lk_bigtime_t time;
50 *REG(SYSINFO_TIME_LATCH) = 1;
51 time = *REG(SYSINFO_TIME_SECS) * 1000000ULL;
52 time += *REG(SYSINFO_TIME_USECS);
53
54 return time;
55}
56
57lk_time_t current_time(void)
58{
59 lk_time_t time;
60 *REG(SYSINFO_TIME_LATCH) = 1;
61 time = *REG(SYSINFO_TIME_SECS) * 1000;
62 time += *REG(SYSINFO_TIME_USECS) / 1000;
63
64 return time;
65}
66
67static enum handler_return platform_tick(void *arg)
68{
69 *REG(PIT_CLEAR_INT) = 1;
70 if (t_callback) {
71 return t_callback(arg, current_time());
72 } else {
73 return INT_NO_RESCHEDULE;
74 }
75}
76
77void platform_init_timer(void)
78{
79 register_int_handler(INT_PIT, &platform_tick, NULL);
80}
81