summaryrefslogtreecommitdiff
path: root/firmware/target/arm/tms320dm320/timer-mr500.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/tms320dm320/timer-mr500.c')
-rw-r--r--firmware/target/arm/tms320dm320/timer-mr500.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/firmware/target/arm/tms320dm320/timer-mr500.c b/firmware/target/arm/tms320dm320/timer-mr500.c
new file mode 100644
index 0000000000..21449ed19f
--- /dev/null
+++ b/firmware/target/arm/tms320dm320/timer-mr500.c
@@ -0,0 +1,106 @@
1/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id$
9*
10* Copyright (C) 2007 by Karl Kurbjun
11*
12* All files in this archive are subject to the GNU General Public License.
13* See the file COPYING in the source tree root for full license agreement.
14*
15* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16* KIND, either express or implied.
17*
18****************************************************************************/
19
20#include "config.h"
21#include "cpu.h"
22#include "system.h"
23#include "timer.h"
24#include "logf.h"
25
26/* GPB0/TOUT0 should already have been configured as output so that pin
27 should not be a functional pin and TIMER0 output unseen there */
28void TIMER0(void)
29{
30 if (pfn_timer != NULL)
31 pfn_timer();
32 IO_INTC_IRQ0 |= 1<<IRQ_TIMER0;
33}
34
35static void stop_timer(void)
36{
37 IO_INTC_EINT0 &= ~(1<<IRQ_TIMER0);
38
39 IO_INTC_IRQ0 |= 1<<IRQ_TIMER0;
40
41 IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_STOP;
42}
43
44bool __timer_set(long cycles, bool start)
45{
46 int oldlevel;
47 unsigned int divider;
48 /* taken from linux/arch/arm/mach-itdm320-20/time.c and timer-meg-fx.c */
49
50 /* Turn off all timers */
51 IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_STOP;
52 IO_TIMER1_TMMD = CONFIG_TIMER1_TMMD_STOP;
53 IO_TIMER2_TMMD = CONFIG_TIMER2_TMMD_STOP;
54 IO_TIMER3_TMMD = CONFIG_TIMER3_TMMD_STOP;
55
56 /* Find the minimum factor that puts the counter in range 1-65535 */
57 unsigned int prescaler = (cycles + 65534) / 65535;
58
59 /* Test this by writing 1's to registers to see how many bits we have */
60 /* Maximum divider setting is x / 1024 / 65536 = x / 67108864 */
61 if (start && pfn_unregister != NULL)
62 {
63 pfn_unregister();
64 pfn_unregister = NULL;
65 }
66
67 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
68
69 /* Max prescale is 1023+1 */
70 for (divider = 0; prescaler > 1024; prescaler >>= 1, divider++);
71
72 /* Setup the Prescalar */
73 IO_TIMER0_TMPRSCL = prescaler;
74
75 /* Setup the Divisor */
76 IO_TIMER0_TMDIV = divider;
77
78 set_irq_level(oldlevel);
79
80 return true;
81}
82
83bool __timer_register(void)
84{
85 bool retval = true;
86
87 int oldstatus = set_interrupt_status(IRQ_FIQ_DISABLED, IRQ_FIQ_STATUS);
88
89 stop_timer();
90
91 /* Turn Timer0 to Free Run mode */
92 IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_FREE_RUN;
93
94 IO_INTC_EINT0 |= 1<<IRQ_TIMER0;
95
96 set_interrupt_status(oldstatus, IRQ_FIQ_STATUS);
97
98 return retval;
99}
100
101void __timer_unregister(void)
102{
103 int oldstatus = set_interrupt_status(IRQ_FIQ_DISABLED, IRQ_FIQ_STATUS);
104 stop_timer();
105 set_interrupt_status(oldstatus, IRQ_FIQ_STATUS);
106}