wdenk | 4e5ca3e | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 1 | /* |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2003 Josef Baumgartner <josef.baumgartner@telex.de> |
| 3 | * |
| 4 | * (C) Copyright 2000 |
wdenk | 4e5ca3e | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 4e5ca3e | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | |
TsiChungLiew | 52b0176 | 2007-07-05 23:36:16 -0500 | [diff] [blame] | 12 | #include <asm/timer.h> |
| 13 | #include <asm/immap.h> |
Richard Retanubun | 42a8376 | 2009-03-20 15:30:10 -0400 | [diff] [blame] | 14 | #include <watchdog.h> |
wdenk | 4e5ca3e | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 15 | |
TsiChungLiew | 99c03c1 | 2007-08-05 03:58:52 -0500 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Richard Retanubun | 42a8376 | 2009-03-20 15:30:10 -0400 | [diff] [blame] | 18 | static volatile ulong timestamp = 0; |
| 19 | |
| 20 | #ifndef CONFIG_SYS_WATCHDOG_FREQ |
| 21 | #define CONFIG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2) |
| 22 | #endif |
stroese | cd42dee | 2004-12-16 17:56:09 +0000 | [diff] [blame] | 23 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 24 | #if defined(CONFIG_MCFTMR) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 25 | #ifndef CONFIG_SYS_UDELAY_BASE |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 26 | # error "uDelay base not defined!" |
| 27 | #endif |
| 28 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 29 | #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 30 | # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!" |
| 31 | #endif |
TsiChungLiew | 52b0176 | 2007-07-05 23:36:16 -0500 | [diff] [blame] | 32 | extern void dtimer_intr_setup(void); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 33 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 34 | void __udelay(unsigned long usec) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 35 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 36 | volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_UDELAY_BASE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 37 | uint start, now, tmp; |
| 38 | |
| 39 | while (usec > 0) { |
| 40 | if (usec > 65000) |
| 41 | tmp = 65000; |
| 42 | else |
| 43 | tmp = usec; |
| 44 | usec = usec - tmp; |
| 45 | |
| 46 | /* Set up TIMER 3 as timebase clock */ |
| 47 | timerp->tmr = DTIM_DTMR_RST_RST; |
| 48 | timerp->tcn = 0; |
| 49 | /* set period to 1 us */ |
| 50 | timerp->tmr = |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 51 | CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR | |
TsiChungLiew | 52b0176 | 2007-07-05 23:36:16 -0500 | [diff] [blame] | 52 | DTIM_DTMR_RST_EN; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 53 | |
| 54 | start = now = timerp->tcn; |
| 55 | while (now < start + tmp) |
| 56 | now = timerp->tcn; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void dtimer_interrupt(void *not_used) |
| 61 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 63 | |
| 64 | /* check for timer interrupt asserted */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) { |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 66 | timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF); |
| 67 | timestamp++; |
Richard Retanubun | 42a8376 | 2009-03-20 15:30:10 -0400 | [diff] [blame] | 68 | |
| 69 | #if defined(CONFIG_WATCHDOG) || defined (CONFIG_HW_WATCHDOG) |
| 70 | if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0) { |
| 71 | WATCHDOG_RESET (); |
| 72 | } |
| 73 | #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */ |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | } |
| 77 | |
Jason Jin | 444ddfc | 2011-08-19 10:02:32 +0800 | [diff] [blame] | 78 | int timer_init(void) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 79 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 80 | volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 81 | |
| 82 | timestamp = 0; |
| 83 | |
| 84 | timerp->tcn = 0; |
| 85 | timerp->trr = 0; |
| 86 | |
| 87 | /* Set up TIMER 4 as clock */ |
| 88 | timerp->tmr = DTIM_DTMR_RST_RST; |
| 89 | |
TsiChungLiew | 52b0176 | 2007-07-05 23:36:16 -0500 | [diff] [blame] | 90 | /* initialize and enable timer interrupt */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 92 | |
| 93 | timerp->tcn = 0; |
| 94 | timerp->trr = 1000; /* Interrupt every ms */ |
| 95 | |
TsiChungLiew | 52b0176 | 2007-07-05 23:36:16 -0500 | [diff] [blame] | 96 | dtimer_intr_setup(); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 97 | |
| 98 | /* set a period of 1us, set timer mode to restart and enable timer and interrupt */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 99 | timerp->tmr = CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 100 | DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN; |
Jason Jin | 444ddfc | 2011-08-19 10:02:32 +0800 | [diff] [blame] | 101 | |
| 102 | return 0; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 103 | } |
| 104 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 105 | ulong get_timer(ulong base) |
| 106 | { |
| 107 | return (timestamp - base); |
| 108 | } |
| 109 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 110 | #endif /* CONFIG_MCFTMR */ |
| 111 | |
| 112 | #if defined(CONFIG_MCFPIT) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 113 | #if !defined(CONFIG_SYS_PIT_BASE) |
| 114 | # error "CONFIG_SYS_PIT_BASE not defined!" |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 115 | #endif |
| 116 | |
| 117 | static unsigned short lastinc; |
| 118 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 119 | void __udelay(unsigned long usec) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 120 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 121 | volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_UDELAY_BASE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 122 | uint tmp; |
| 123 | |
| 124 | while (usec > 0) { |
| 125 | if (usec > 65000) |
| 126 | tmp = 65000; |
| 127 | else |
| 128 | tmp = usec; |
| 129 | usec = usec - tmp; |
| 130 | |
| 131 | /* Set up TIMER 3 as timebase clock */ |
| 132 | timerp->pcsr = PIT_PCSR_OVW; |
| 133 | timerp->pmr = 0; |
| 134 | /* set period to 1 us */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 135 | timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 136 | |
| 137 | timerp->pmr = tmp; |
| 138 | while (timerp->pcntr > 0) ; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void timer_init(void) |
| 143 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 144 | volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 145 | timestamp = 0; |
| 146 | |
| 147 | /* Set up TIMER 4 as poll clock */ |
| 148 | timerp->pcsr = PIT_PCSR_OVW; |
| 149 | timerp->pmr = lastinc = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 150 | timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN; |
Jason Jin | 444ddfc | 2011-08-19 10:02:32 +0800 | [diff] [blame] | 151 | |
| 152 | return 0; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 153 | } |
| 154 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 155 | ulong get_timer(ulong base) |
| 156 | { |
| 157 | unsigned short now, diff; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 158 | volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 159 | |
| 160 | now = timerp->pcntr; |
| 161 | diff = -(now - lastinc); |
| 162 | |
| 163 | timestamp += diff; |
| 164 | lastinc = now; |
| 165 | return timestamp - base; |
| 166 | } |
| 167 | |
| 168 | void wait_ticks(unsigned long ticks) |
| 169 | { |
Graeme Russ | 5c8404a | 2011-07-15 02:18:12 +0000 | [diff] [blame] | 170 | u32 start = get_timer(0); |
| 171 | while (get_timer(start) < ticks) ; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 172 | } |
| 173 | #endif /* CONFIG_MCFPIT */ |
stroese | cd42dee | 2004-12-16 17:56:09 +0000 | [diff] [blame] | 174 | |
wdenk | 70f05ac | 2004-06-09 15:24:18 +0000 | [diff] [blame] | 175 | /* |
| 176 | * This function is derived from PowerPC code (read timebase as long long). |
| 177 | * On M68K it just returns the timer value. |
| 178 | */ |
| 179 | unsigned long long get_ticks(void) |
| 180 | { |
| 181 | return get_timer(0); |
| 182 | } |
| 183 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 184 | unsigned long usec2ticks(unsigned long usec) |
| 185 | { |
| 186 | return get_timer(usec); |
| 187 | } |
| 188 | |
wdenk | 70f05ac | 2004-06-09 15:24:18 +0000 | [diff] [blame] | 189 | /* |
| 190 | * This function is derived from PowerPC code (timebase clock frequency). |
| 191 | * On M68K it returns the number of timer ticks per second. |
| 192 | */ |
TsiChungLiew | 52b0176 | 2007-07-05 23:36:16 -0500 | [diff] [blame] | 193 | ulong get_tbclk(void) |
wdenk | 70f05ac | 2004-06-09 15:24:18 +0000 | [diff] [blame] | 194 | { |
| 195 | ulong tbclk; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 196 | tbclk = CONFIG_SYS_HZ; |
wdenk | 70f05ac | 2004-06-09 15:24:18 +0000 | [diff] [blame] | 197 | return tbclk; |
| 198 | } |