diff options
Diffstat (limited to 'firmware/target/arm')
-rw-r--r-- | firmware/target/arm/s5l8700/timer-s5l8700.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/firmware/target/arm/s5l8700/timer-s5l8700.c b/firmware/target/arm/s5l8700/timer-s5l8700.c new file mode 100644 index 0000000000..f9bef1c956 --- /dev/null +++ b/firmware/target/arm/s5l8700/timer-s5l8700.c | |||
@@ -0,0 +1,104 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2008 Rafaël Carré | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "config.h" | ||
23 | |||
24 | #include "inttypes.h" | ||
25 | #include "s5l8700.h" | ||
26 | #include "system.h" | ||
27 | #include "timer.h" | ||
28 | |||
29 | /* Timer driver for the S5L8700 | ||
30 | |||
31 | The S5L8700 timer resolution is only 16-bit. Larger counts are done by using | ||
32 | both the clock-select and the clock prescaler to bring the count down into | ||
33 | the range of the 16-bit counter. | ||
34 | |||
35 | TODO: investigate why the timer seems to count twice as fast as expected | ||
36 | */ | ||
37 | |||
38 | void INT_TIMERD(void) | ||
39 | { | ||
40 | /* clear interrupt */ | ||
41 | TDCON = TDCON; | ||
42 | |||
43 | if (pfn_timer != NULL) { | ||
44 | pfn_timer(); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | bool timer_set(long cycles, bool start) | ||
49 | { | ||
50 | static const int cs_table[] = {1, 2, 4, 6}; | ||
51 | int prescale, cs; | ||
52 | long count; | ||
53 | |||
54 | /* stop and clear timer */ | ||
55 | TDCMD = (1 << 1); /* TD_CLR */ | ||
56 | |||
57 | /* optionally unregister any previously registered timer user */ | ||
58 | if (start) { | ||
59 | if (pfn_unregister != NULL) { | ||
60 | pfn_unregister(); | ||
61 | pfn_unregister = NULL; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | /* scale the count down with the clock select */ | ||
66 | for (cs = 0; cs < 4; cs++) { | ||
67 | count = cycles >> cs_table[cs]; | ||
68 | if ((count < 65536) || (cs == 3)) { | ||
69 | break; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | /* scale the count down with the prescaler */ | ||
74 | prescale = 1; | ||
75 | while (count >= 65536) { | ||
76 | count >>= 1; | ||
77 | prescale <<= 1; | ||
78 | } | ||
79 | |||
80 | /* configure timer */ | ||
81 | TDCON = (1 << 12) | /* TD_INT0_EN */ | ||
82 | (cs << 8) | /* TS_CS */ | ||
83 | (0 << 4); /* TD_MODE_SEL, 0 = interval mode */ | ||
84 | TDPRE = prescale - 1; | ||
85 | TDDATA0 = count; | ||
86 | TDCMD = (1 << 0); /* TD_ENABLE */ | ||
87 | |||
88 | /* enable interrupt */ | ||
89 | INTMSK |= (1 << 9); | ||
90 | |||
91 | return true; | ||
92 | } | ||
93 | |||
94 | bool timer_start(void) | ||
95 | { | ||
96 | TDCMD = (1 << 0); /* TD_ENABLE */ | ||
97 | return true; | ||
98 | } | ||
99 | |||
100 | void timer_stop(void) | ||
101 | { | ||
102 | TDCMD = (0 << 0); /* TD_ENABLE */ | ||
103 | } | ||
104 | |||