1 /*
2 * Copyright (c) 2008-2012 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 <debug.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/vexpress-a15.h>
30 #include <sys/types.h>
31 #include "platform_p.h"
32
33 #define TIMREG(reg) (*REG32(TIMER0 + (reg)))
34
35 #define LOADVAL (0x00)
36 #define VAL (0x04)
37 #define CONTROL (0x08)
38 #define INTCLEAR (0x0c)
39 #define RAWINTSTAT (0x10)
40 #define MASKEDINTSTAT (0x14)
41
42 static platform_timer_callback t_callback;
43
44 static volatile uint ticks = 0;
45 static lk_time_t periodic_interval;
46
platform_set_periodic_timer(platform_timer_callback callback,void * arg,lk_time_t interval)47 status_t platform_set_periodic_timer(platform_timer_callback callback,
48 void* arg,
49 lk_time_t interval) {
50 enter_critical_section();
51
52 t_callback = callback;
53
54 periodic_interval = interval;
55 TIMREG(LOADVAL) = periodic_interval * 1000; /* timer is running at 1Mhz */
56
57 TIMREG(CONTROL) |= (1 << 7); // enable
58
59 unmask_interrupt(TIMER01_INT);
60
61 exit_critical_section();
62
63 return NO_ERROR;
64 }
65
current_time_hires(void)66 lk_bigtime_t current_time_hires(void) {
67 lk_bigtime_t time;
68
69 time = ticks * periodic_interval * 1000ULL;
70
71 return time;
72 }
73
current_time(void)74 lk_time_t current_time(void) {
75 lk_time_t time;
76
77 time = ticks * periodic_interval;
78
79 return time;
80 }
81
platform_tick(void * arg)82 static enum handler_return platform_tick(void* arg) {
83 ticks++;
84 TIMREG(INTCLEAR) = 1;
85 if (t_callback) {
86 return t_callback(arg, current_time());
87 } else {
88 return INT_NO_RESCHEDULE;
89 }
90 }
91
platform_init_timer(void)92 void platform_init_timer(void) {
93 /* disable timer */
94 TIMREG(CONTROL) = 0;
95
96 /* periodic mode, ints enabled, 32bit, wrapping */
97 TIMREG(CONTROL) = (1 << 6) | (1 << 5) | (1 << 1);
98
99 register_int_handler(TIMER01_INT, &platform_tick, NULL);
100 }
101