Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2008 |
Stelian Pop | cce9cfd | 2008-05-08 22:52:09 +0200 | [diff] [blame] | 3 | * Stelian Pop <stelian.pop@leadtechdesign.com> |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 4 | * Lead Tech Design <www.leadtechdesign.com> |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <common.h> |
| 26 | #include <asm/arch/hardware.h> |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 27 | #include <asm/arch/at91_pit.h> |
| 28 | #include <asm/arch/at91_pmc.h> |
| 29 | #include <asm/arch/at91_rstc.h> |
| 30 | #include <asm/arch/io.h> |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 31 | |
| 32 | /* |
Stelian Pop | a8a78f2 | 2008-03-26 20:52:28 +0100 | [diff] [blame] | 33 | * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 34 | * setting the 20 bit counter period to its maximum (0xfffff). |
| 35 | */ |
| 36 | #define TIMER_LOAD_VAL 0xfffff |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 37 | #define READ_RESET_TIMER at91_sys_read(AT91_PIT_PIVR) |
| 38 | #define READ_TIMER at91_sys_read(AT91_PIT_PIIR) |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 39 | #define TIMER_FREQ (AT91C_MASTER_CLOCK << 4) |
| 40 | #define TICKS_TO_USEC(ticks) ((ticks) / 6) |
| 41 | |
| 42 | ulong get_timer_masked(void); |
| 43 | ulong resettime; |
| 44 | |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 45 | /* nothing really to do with interrupts, just starts up a counter. */ |
Stelian Pop | 61106a5 | 2008-03-26 21:52:27 +0100 | [diff] [blame] | 46 | int timer_init(void) |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 47 | { |
| 48 | /* |
| 49 | * Enable PITC Clock |
| 50 | * The clock is already enabled for system controller in boot |
| 51 | */ |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 52 | at91_sys_write(AT91_PMC_PCER, 1 << AT91_ID_SYS); |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 53 | |
| 54 | /* Enable PITC */ |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 55 | at91_sys_write(AT91_PIT_MR, TIMER_LOAD_VAL | AT91_PIT_PITEN); |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 56 | |
| 57 | reset_timer_masked(); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * timer without interrupts |
| 64 | */ |
| 65 | |
| 66 | static inline ulong get_timer_raw(void) |
| 67 | { |
| 68 | ulong now = READ_TIMER; |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 69 | |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 70 | if (now >= resettime) |
| 71 | return now - resettime; |
| 72 | else |
| 73 | return 0xFFFFFFFFUL - (resettime - now) ; |
| 74 | } |
| 75 | |
| 76 | void reset_timer_masked(void) |
| 77 | { |
| 78 | resettime = READ_TIMER; |
| 79 | } |
| 80 | |
| 81 | ulong get_timer_masked(void) |
| 82 | { |
| 83 | return TICKS_TO_USEC(get_timer_raw()); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | void udelay_masked(unsigned long usec) |
| 88 | { |
| 89 | ulong tmp; |
| 90 | |
| 91 | tmp = get_timer(0); |
| 92 | while (get_timer(tmp) < usec) /* our timer works in usecs */ |
| 93 | ; /* NOP */ |
| 94 | } |
| 95 | |
| 96 | void reset_timer(void) |
| 97 | { |
| 98 | reset_timer_masked(); |
| 99 | } |
| 100 | |
| 101 | ulong get_timer(ulong base) |
| 102 | { |
| 103 | ulong now = get_timer_masked(); |
| 104 | |
| 105 | if (now >= base) |
| 106 | return now - base; |
| 107 | else |
| 108 | return TICKS_TO_USEC(0xFFFFFFFFUL) - (base - now) ; |
| 109 | } |
| 110 | |
| 111 | void udelay(unsigned long usec) |
| 112 | { |
| 113 | udelay_masked(usec); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * This function is derived from PowerPC code (read timebase as long long). |
| 118 | * On ARM it just returns the timer value. |
| 119 | */ |
| 120 | unsigned long long get_ticks(void) |
| 121 | { |
| 122 | return get_timer(0); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * This function is derived from PowerPC code (timebase clock frequency). |
| 127 | * On ARM it returns the number of timer ticks per second. |
| 128 | */ |
| 129 | ulong get_tbclk(void) |
| 130 | { |
| 131 | ulong tbclk; |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 132 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 133 | tbclk = CONFIG_SYS_HZ; |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 134 | return tbclk; |
| 135 | } |
| 136 | |
| 137 | /* |
Stelian Pop | a8a78f2 | 2008-03-26 20:52:28 +0100 | [diff] [blame] | 138 | * Reset the cpu by setting up the watchdog timer and let him time out. |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 139 | */ |
| 140 | void reset_cpu(ulong ignored) |
| 141 | { |
| 142 | /* this is the way Linux does it */ |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 143 | at91_sys_write(AT91_RSTC_CR, AT91_RSTC_KEY | |
| 144 | AT91_RSTC_PROCRST | |
| 145 | AT91_RSTC_PERRST); |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 146 | |
| 147 | while (1); |
| 148 | /* Never reached */ |
| 149 | } |