blob: 8e2c6e38e84e6957e9ddb3fdc664b7bb3b2d3563 [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 <err.h>
Travis Geiselbrechteb946052008-09-07 22:32:49 -070024#include <debug.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070025#include <sys/types.h>
26
27#include <kernel/thread.h>
28#include <platform/timer.h>
29#include <platform/interrupts.h>
30#include <platform/debug.h>
31#include <platform/at91sam7.h>
32
33#define FIXED_1KHZ_TIMER 0
34
35static platform_timer_callback timer_func;
36
37static volatile time_t ticks = 0;
38
39#if FIXED_1KHZ_TIMER
40static volatile int timer_interval;
41static volatile int timer_downcount;
42#else
43static int timer_ms_per_tick;
44#endif
45
46time_t current_time(void)
47{
48 return ticks;
49}
50
Travis Geiselbrecht5eaef672009-12-03 23:56:23 -080051bigtime_t current_time_hires(void)
52{
53 return ticks * 1000ULL;
54}
55
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070056static enum handler_return pit_irq_handler(void *arg)
57{
58 AT91PIT *pit = AT91PIT_ADDR;
59 unsigned n = PIT_PICNT(pit->PIVR);
60
61#if FIXED_1KHZ_TIMER
62 ticks += n;
63 timer_downcount -= n;
64
65 if(timer_downcount <= 0) {
66 timer_downcount = timer_interval;
67 return timer_func(0, ticks);
68 } else {
69 return INT_NO_RESCHEDULE;
70 }
71#else
72 ticks += (n * timer_ms_per_tick);
73 return timer_func(0, ticks);
74#endif
75}
76
77status_t platform_set_periodic_timer(platform_timer_callback callback,
78 void *arg, time_t interval)
79{
80 unsigned n;
81
82 AT91PIT *pit = AT91PIT_ADDR;
83
84 n = AT91_MCK_MHZ / 16 / 1000;
Travis Geiselbrechteb946052008-09-07 22:32:49 -070085 dprintf(INFO, "timer: MCK=%dKHz, n=%d\n", AT91_MCK_MHZ / 1000, n);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070086
87 enter_critical_section();
88
89 timer_func = callback;
90
91#if FIXED_1KHZ_TIMER
92 timer_interval = interval;
93 timer_downcount = interval;
94#else
95 timer_ms_per_tick = interval;
96 n *= interval;
97#endif
98
99 pit->MR = PIT_PITEN | PIT_PITIEN | (n & 0xfffff);
100
101 register_int_handler(PID_SYSIRQ, pit_irq_handler, 0);
Travis Geiselbrechtf1cdea32008-09-13 14:13:54 -0700102 unmask_interrupt(PID_SYSIRQ);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700103
104 exit_critical_section();
105
106 return NO_ERROR;
107}
108