blob: 770905b70c87fe40ca389d4e2e17e12357833790 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
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, time_t interval)
35{
36 enter_critical_section();
37
38 t_callback = callback;
39
40 *REG(PIT_CLEAR) = 1;
41 *REG(PIT_INTERVAL) = interval;
42 *REG(PIT_START_PERIODIC) = 1;
43
44 unmask_interrupt(INT_PIT, NULL);
45
46 exit_critical_section();
47
48 return NO_ERROR;
49}
50
51time_t current_time(void)
52{
53 time_t time;
54 *REG(SYSINFO_TIME_LATCH) = 1;
55 time = *REG(SYSINFO_TIME_SECS) * 1000;
56 time += *REG(SYSINFO_TIME_USECS) / 1000;
57
58 return time;
59}
60
61static enum handler_return platform_tick(void *arg)
62{
63 *REG(PIT_CLEAR_INT) = 1;
64 if (t_callback) {
65 return t_callback(arg, current_time());
66 } else {
67 return INT_NO_RESCHEDULE;
68 }
69}
70
71void platform_init_timer(void)
72{
73 register_int_handler(INT_PIT, &platform_tick, NULL);
74}
75