wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Shinya Kuribayashi | 5fcf6d3 | 2008-06-05 22:28:59 +0900 | [diff] [blame] | 9 | #include <asm/mipsregs.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 10 | |
Shinya Kuribayashi | 5d374e0 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 11 | static unsigned long timestamp; |
| 12 | |
| 13 | /* how many counter cycles in a jiffy */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 14 | #define CYCLES_PER_JIFFY (CONFIG_SYS_MIPS_TIMER_FREQ + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ |
Shinya Kuribayashi | 5d374e0 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 15 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 16 | /* |
| 17 | * timer without interrupts |
| 18 | */ |
| 19 | |
| 20 | int timer_init(void) |
| 21 | { |
Shinya Kuribayashi | 5d374e0 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 22 | /* Set up the timer for the first expiration. */ |
Shinya Kuribayashi | 5d374e0 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 23 | write_c0_compare(read_c0_count() + CYCLES_PER_JIFFY); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 24 | |
| 25 | return 0; |
| 26 | } |
| 27 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 28 | ulong get_timer(ulong base) |
| 29 | { |
Shinya Kuribayashi | 5d374e0 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 30 | unsigned int count; |
| 31 | unsigned int expirelo = read_c0_compare(); |
| 32 | |
| 33 | /* Check to see if we have missed any timestamps. */ |
| 34 | count = read_c0_count(); |
| 35 | while ((count - expirelo) < 0x7fffffff) { |
| 36 | expirelo += CYCLES_PER_JIFFY; |
| 37 | timestamp++; |
| 38 | } |
| 39 | write_c0_compare(expirelo); |
| 40 | |
| 41 | return (timestamp - base); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 44 | void __udelay(unsigned long usec) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 45 | { |
Shinya Kuribayashi | dbbaa22 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 46 | unsigned int tmo; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 47 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 48 | tmo = read_c0_count() + (usec * (CONFIG_SYS_MIPS_TIMER_FREQ / 1000000)); |
Shinya Kuribayashi | dbbaa22 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 49 | while ((tmo - read_c0_count()) < 0x7fffffff) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 50 | /*NOP*/; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * This function is derived from PowerPC code (read timebase as long long). |
| 55 | * On MIPS it just returns the timer value. |
| 56 | */ |
| 57 | unsigned long long get_ticks(void) |
| 58 | { |
Shinya Kuribayashi | 5d374e0 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 59 | return get_timer(0); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | * This function is derived from PowerPC code (timebase clock frequency). |
| 64 | * On MIPS it returns the number of timer ticks per second. |
| 65 | */ |
| 66 | ulong get_tbclk(void) |
| 67 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 68 | return CONFIG_SYS_HZ; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 69 | } |