blob: 72659f172e85930e7f8852ba65e8b0a9b3411b9a [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 <debug.h>
27#include <platform.h>
28#include <platform/interrupts.h>
29#include <platform/timer.h>
30#include <platform/omap5912.h>
31#include "platform_p.h"
32
33static time_t system_time = 0;
34
35static time_t tick_interval;
36static uint32_t ticks_per_interval;
37static platform_timer_callback t_callback;
38static void *callback_arg;
39
40status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval)
41{
42 enter_critical_section();
43
44 t_callback = callback;
45 callback_arg = arg;
46 tick_interval = interval;
47 ticks_per_interval = interval * 32768 / 1000; // interval is in ms
48
49 OS_TIMER_CTRL_REG = 0; // stop it
50 OS_TIMER_TICK_VALUE_REG = ticks_per_interval;
51 OS_TIMER_CTRL_REG = (1<<3) | (1<<2) | (1<<1) | (1<<0);
52
53 exit_critical_section();
54
55 return NO_ERROR;
56}
57
58time_t current_time(void)
59{
60 time_t t;
61 uint32_t delta_ticks;
62 uint32_t delta_ticks2;
63
64retry:
65 delta_ticks = OS_TIMER_TICK_COUNTER_REG;
66 t = system_time;
67 delta_ticks2 = OS_TIMER_TICK_COUNTER_REG;
68 if (delta_ticks2 > delta_ticks)
69 goto retry;
70
71 t += ((ticks_per_interval - delta_ticks2) * tick_interval) / ticks_per_interval;
72
73 return t;
74}
75
76bigtime_t current_time_hires(void)
77{
78 time_t t;
79 uint32_t delta_ticks;
80 uint32_t delta_ticks2;
81
82retry:
83 delta_ticks = OS_TIMER_TICK_COUNTER_REG;
84 t = system_time;
85 delta_ticks2 = OS_TIMER_TICK_COUNTER_REG;
86 if (delta_ticks2 > delta_ticks)
87 goto retry;
88
89 t += ((ticks_per_interval - delta_ticks2) * tick_interval) / ticks_per_interval;
90
91 return t * 1000ULL;
92}
93
94
95static enum handler_return os_timer_tick(void *arg)
96{
97 system_time += tick_interval;
98// dprintf("os_timer_tick %d\n", system_time);
99
100 return t_callback(callback_arg, system_time);
101}
102
103void platform_init_timer(void)
104{
105 OS_TIMER_CTRL_REG = 0; // stop the timer if it's already running
106
107 register_int_handler(IRQ_OS_TIMER, &os_timer_tick, NULL);
108 unmask_interrupt(IRQ_OS_TIMER, NULL);
109}
110