blob: 37cac3a208fde4015af8eac59812209961eb048a [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 <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 lk_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, lk_time_t interval)
46{
47 spin_lock_saved_state_t statep;
48 arch_interrupt_save(&statep, SPIN_LOCK_FLAG_IRQ);
49
50 t_callback = callback;
51 callback_arg = arg;
52 tick_interval = interval;
53 uint32_t ticks_per_interval = (uint64_t)interval * TIMER_TICK_RATE / 1000; // interval is in ms
54
55 TIMER_REG(TCLR) = 0; // stop the timer
56 TIMER_REG(TLDR) = -ticks_per_interval;
57 TIMER_REG(TTGR) = 1;
58 TIMER_REG(TIER) = 0x2;
59 TIMER_REG(TCLR) = 0x3; // autoreload, start
60
61 unmask_interrupt(GPT2_IRQ);
62
63 arch_interrupt_restore(statep, SPIN_LOCK_FLAG_IRQ);
64
65 return NO_ERROR;
66}
67
68lk_time_t current_time(void)
69{
70 uint32_t delta_ticks;
71 uint32_t delta_ticks2;
72
73retry:
74 delta_ticks = *REG32(TIMER32K_CR);
75 delta_ticks2 = *REG32(TIMER32K_CR);
76 if (delta_ticks2 != delta_ticks)
77 goto retry;
78
79 uint64_t longtime = delta_ticks * 1000ULL / 32768ULL;
80
81 return (lk_time_t)longtime;
82}
83
84lk_bigtime_t current_time_hires(void)
85{
86 uint32_t delta_ticks;
87 uint32_t delta_ticks2;
88
89retry:
90 delta_ticks = *REG32(TIMER32K_CR);
91 delta_ticks2 = *REG32(TIMER32K_CR);
92 if (delta_ticks2 != delta_ticks)
93 goto retry;
94
95 uint64_t longtime = delta_ticks * 1000000ULL / 32768ULL;
96
97 return (lk_bigtime_t)longtime;
98}
99static enum handler_return os_timer_tick(void *arg)
100{
101 TIMER_REG(TISR) = TIMER_REG(TISR);
102
103 return t_callback(callback_arg, current_time());
104}
105
106void platform_init_timer(void)
107{
108 /* GPT2 */
109 RMWREG32(CM_CLKSEL_PER, 0, 1, 1);
110 RMWREG32(CM_ICLKEN_PER, 3, 1, 1);
111 RMWREG32(CM_FCLKEN_PER, 3, 1, 1);
112
113 // reset the GP timer
114 TIMER_REG(TIOCP_CFG) = 0x2;
115 while ((TIMER_REG(TISTAT) & 1) == 0)
116 ;
117
118 // set GPT2-9 clock inputs over to 32k
119 *REG32(CM_CLKSEL_PER) = 0;
120
121 // disable ints
122 TIMER_REG(TIER) = 0;
123 TIMER_REG(TISR) = 0x7; // clear any pending bits
124
125 // XXX make sure 32K timer is running
126
127 register_int_handler(GPT2_IRQ, &os_timer_tick, NULL);
128}
129
130void platform_halt_timers(void)
131{
132 TIMER_REG(TCLR) = 0;
133}
134