wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com> |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <common.h> |
| 24 | #include <asm/arch/platform.h> |
| 25 | |
| 26 | /* |
Greg Ungerer | 37da45d | 2011-09-09 22:19:10 +1000 | [diff] [blame] | 27 | * Initial timer set constants. Nothing complicated, just set for a 1ms |
| 28 | * tick. |
| 29 | */ |
| 30 | #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_1) |
| 31 | #define TIMER_COUNT (TIMER_INTERVAL / 2) |
| 32 | #define TIMER_PULSE TIMER_COUNT |
| 33 | |
| 34 | /* |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 35 | * Handy KS8695 register access functions. |
| 36 | */ |
| 37 | #define ks8695_read(a) *((volatile ulong *) (KS8695_IO_BASE + (a))) |
| 38 | #define ks8695_write(a,v) *((volatile ulong *) (KS8695_IO_BASE + (a))) = (v) |
| 39 | |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 40 | ulong timer_ticks; |
| 41 | |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 42 | int timer_init (void) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 43 | { |
Graeme Russ | 944a7fe | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 44 | /* Set the hadware timer for 1ms */ |
| 45 | ks8695_write(KS8695_TIMER1, TIMER_COUNT); |
| 46 | ks8695_write(KS8695_TIMER1_PCOUNT, TIMER_PULSE); |
| 47 | ks8695_write(KS8695_TIMER_CTRL, 0x2); |
| 48 | timer_ticks = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 49 | |
| 50 | return 0; |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 51 | } |
| 52 | |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 53 | ulong get_timer_masked(void) |
| 54 | { |
| 55 | /* Check for timer wrap */ |
| 56 | if (ks8695_read(KS8695_INT_STATUS) & KS8695_INTMASK_TIMERINT1) { |
| 57 | /* Clear interrupt condition */ |
| 58 | ks8695_write(KS8695_INT_STATUS, KS8695_INTMASK_TIMERINT1); |
| 59 | timer_ticks++; |
| 60 | } |
| 61 | return timer_ticks; |
| 62 | } |
| 63 | |
| 64 | ulong get_timer(ulong base) |
| 65 | { |
| 66 | return (get_timer_masked() - base); |
| 67 | } |
| 68 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 69 | void __udelay(ulong usec) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 70 | { |
| 71 | ulong start = get_timer_masked(); |
| 72 | ulong end; |
| 73 | |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 74 | /* Only 1ms resolution :-( */ |
| 75 | end = usec / 1000; |
| 76 | while (get_timer(start) < end) |
| 77 | ; |
| 78 | } |
| 79 | |
| 80 | void reset_cpu (ulong ignored) |
| 81 | { |
| 82 | ulong tc; |
| 83 | |
| 84 | /* Set timer0 to watchdog, and let it timeout */ |
| 85 | tc = ks8695_read(KS8695_TIMER_CTRL) & 0x2; |
| 86 | ks8695_write(KS8695_TIMER_CTRL, tc); |
| 87 | ks8695_write(KS8695_TIMER0, ((10 << 8) | 0xff)); |
| 88 | ks8695_write(KS8695_TIMER_CTRL, (tc | 0x1)); |
| 89 | |
| 90 | /* Should only wait here till watchdog resets */ |
| 91 | for (;;) |
| 92 | ; |
| 93 | } |