wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 4 | * Marius Groeger <mgroeger@sysgo.de> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Alex Zuepke <azu@sysgo.de> |
| 9 | * |
| 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | |
| 29 | #include <common.h> |
| 30 | #include <asm/arch/pxa-regs.h> |
Jean-Christophe PLAGNIOL-VILLARD | a922fdb | 2009-02-24 06:13:10 +0100 | [diff] [blame] | 31 | #include <div64.h> |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 32 | |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_USE_IRQ |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 34 | #error: interrupts not implemented yet |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Micha Kalfon | 94a3312 | 2009-02-11 19:50:11 +0200 | [diff] [blame] | 37 | #if defined(CONFIG_PXA27X) || defined(CONFIG_CPU_MONAHANS) |
| 38 | #define TIMER_FREQ_HZ 3250000 |
| 39 | #elif defined(CONFIG_PXA250) |
| 40 | #define TIMER_FREQ_HZ 3686400 |
| 41 | #else |
| 42 | #error "Timer frequency unknown - please config PXA CPU type" |
| 43 | #endif |
| 44 | |
Jean-Christophe PLAGNIOL-VILLARD | a922fdb | 2009-02-24 06:13:10 +0100 | [diff] [blame] | 45 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 46 | { |
| 47 | tick *= CONFIG_SYS_HZ; |
| 48 | do_div(tick, TIMER_FREQ_HZ); |
| 49 | return tick; |
| 50 | } |
| 51 | |
| 52 | static inline unsigned long long us_to_tick(unsigned long long us) |
| 53 | { |
| 54 | us = us * TIMER_FREQ_HZ + 999999; |
| 55 | do_div(us, 1000000); |
| 56 | return us; |
| 57 | } |
| 58 | |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 59 | int timer_init (void) |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 60 | { |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 61 | reset_timer(); |
| 62 | |
| 63 | return 0; |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void reset_timer (void) |
| 67 | { |
| 68 | reset_timer_masked (); |
| 69 | } |
| 70 | |
| 71 | ulong get_timer (ulong base) |
| 72 | { |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 73 | return get_timer_masked () - base; |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void set_timer (ulong t) |
| 77 | { |
| 78 | /* nop */ |
| 79 | } |
| 80 | |
| 81 | void udelay (unsigned long usec) |
| 82 | { |
| 83 | udelay_masked (usec); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void reset_timer_masked (void) |
| 88 | { |
| 89 | OSCR = 0; |
| 90 | } |
| 91 | |
| 92 | ulong get_timer_masked (void) |
| 93 | { |
Jean-Christophe PLAGNIOL-VILLARD | a922fdb | 2009-02-24 06:13:10 +0100 | [diff] [blame] | 94 | return tick_to_time(get_ticks()); |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void udelay_masked (unsigned long usec) |
| 98 | { |
Jean-Christophe PLAGNIOL-VILLARD | a922fdb | 2009-02-24 06:13:10 +0100 | [diff] [blame] | 99 | unsigned long long tmp; |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 100 | ulong tmo; |
| 101 | |
Jean-Christophe PLAGNIOL-VILLARD | a922fdb | 2009-02-24 06:13:10 +0100 | [diff] [blame] | 102 | tmo = us_to_tick(usec); |
| 103 | tmp = get_ticks() + tmo; /* get current timestamp */ |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 104 | |
Jean-Christophe PLAGNIOL-VILLARD | a922fdb | 2009-02-24 06:13:10 +0100 | [diff] [blame] | 105 | while (get_ticks() < tmp) /* loop till event */ |
| 106 | /*NOP*/; |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 107 | |
wdenk | 32fe287 | 2002-10-11 07:57:01 +0000 | [diff] [blame] | 108 | } |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * This function is derived from PowerPC code (read timebase as long long). |
| 112 | * On ARM it just returns the timer value. |
| 113 | */ |
| 114 | unsigned long long get_ticks(void) |
| 115 | { |
Micha Kalfon | 94a3312 | 2009-02-11 19:50:11 +0200 | [diff] [blame] | 116 | return OSCR; |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * This function is derived from PowerPC code (timebase clock frequency). |
| 121 | * On ARM it returns the number of timer ticks per second. |
| 122 | */ |
| 123 | ulong get_tbclk (void) |
| 124 | { |
| 125 | ulong tbclk; |
Micha Kalfon | 94a3312 | 2009-02-11 19:50:11 +0200 | [diff] [blame] | 126 | tbclk = TIMER_FREQ_HZ; |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 127 | return tbclk; |
| 128 | } |