blob: f101f503ee9d6d1556336ba18ab4caa4c4abe9ca [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 <reg.h>
26#include <debug.h>
27#include <kernel/thread.h>
28#include <platform.h>
29#include <platform/interrupts.h>
30#include <platform/timer.h>
31#include <platform/omap3.h>
32#include "platform_p.h"
33
34static time_t tick_interval;
35static platform_timer_callback t_callback;
36static void *callback_arg;
37
38/* timer 2 */
39static const ulong timer_base = OMAP34XX_GPT2;
40
41#define TIMER_TICK_RATE 32768
42
43#define TIMER_REG(reg) *REG32(timer_base + (reg))
44
45status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval)
46{
47 enter_critical_section();
48
49 t_callback = callback;
50 callback_arg = arg;
51 tick_interval = interval;
52 uint32_t ticks_per_interval = (uint64_t)interval * TIMER_TICK_RATE / 1000; // interval is in ms
53
54 TIMER_REG(TCLR) = 0; // stop the timer
55 TIMER_REG(TLDR) = -ticks_per_interval;
56 TIMER_REG(TTGR) = 1;
57 TIMER_REG(TIER) = 0x2;
58 TIMER_REG(TCLR) = 0x3; // autoreload, start
59
Travis Geiselbrechtf1cdea32008-09-13 14:13:54 -070060 unmask_interrupt(GPT2_IRQ);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070061
62 exit_critical_section();
63
64 return NO_ERROR;
65}
66
67time_t current_time(void)
68{
69 uint32_t delta_ticks;
70 uint32_t delta_ticks2;
71
72retry:
73 delta_ticks = *REG32(TIMER32K_CR);
74 delta_ticks2 = *REG32(TIMER32K_CR);
75 if (delta_ticks2 != delta_ticks)
76 goto retry;
77
78 uint64_t longtime = delta_ticks * 1000ULL / 32768ULL;
79
80 return (time_t)longtime;
81}
82
83bigtime_t current_time_hires(void)
84{
85 uint32_t delta_ticks;
86 uint32_t delta_ticks2;
87
88retry:
89 delta_ticks = *REG32(TIMER32K_CR);
90 delta_ticks2 = *REG32(TIMER32K_CR);
91 if (delta_ticks2 != delta_ticks)
92 goto retry;
93
94 uint64_t longtime = delta_ticks * 1000000ULL / 32768ULL;
95
96 return (bigtime_t)longtime;
97}
98static enum handler_return os_timer_tick(void *arg)
99{
100 TIMER_REG(TISR) = TIMER_REG(TISR);
101
102 return t_callback(callback_arg, current_time());
103}
104
105void platform_init_timer(void)
106{
Travis Geiselbrecht2c691e82008-09-04 02:41:01 -0700107 /* GPT2 */
108 RMWREG32(CM_CLKSEL_PER, 0, 1, 1);
109 RMWREG32(CM_ICLKEN_PER, 3, 1, 1);
110 RMWREG32(CM_FCLKEN_PER, 3, 1, 1);
111
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700112 // reset the GP timer
113 TIMER_REG(TIOCP_CFG) = 0x2;
114 while ((TIMER_REG(TISTAT) & 1) == 0)
115 ;
116
117 // set GPT2-9 clock inputs over to 32k
118 *REG32(CM_CLKSEL_PER) = 0;
119
120 // disable ints
121 TIMER_REG(TIER) = 0;
122 TIMER_REG(TISR) = 0x7; // clear any pending bits
123
124 // XXX make sure 32K timer is running
125
126 register_int_handler(GPT2_IRQ, &os_timer_tick, NULL);
127}
128
129void platform_halt_timers(void)
130{
131 TIMER_REG(TCLR) = 0;
132}
133