blob: 386f45a1b062014fa2f9d6aa15d8a8d8ba5f16db [file] [log] [blame]
wdenkc0218802003-03-27 12:09:35 +00001/*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc0218802003-03-27 12:09:35 +00006 */
7
8#include <common.h>
Shinya Kuribayashic7e38e42008-06-05 22:28:59 +09009#include <asm/mipsregs.h>
wdenkc0218802003-03-27 12:09:35 +000010
Shinya Kuribayashia55d4812008-06-05 22:29:00 +090011static unsigned long timestamp;
12
13/* how many counter cycles in a jiffy */
Gabor Juhosc3e49012013-06-12 18:02:43 +020014#define CYCLES_PER_JIFFY \
15 (CONFIG_SYS_MIPS_TIMER_FREQ + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ
Shinya Kuribayashia55d4812008-06-05 22:29:00 +090016
wdenkc0218802003-03-27 12:09:35 +000017/*
18 * timer without interrupts
19 */
20
21int timer_init(void)
22{
Shinya Kuribayashia55d4812008-06-05 22:29:00 +090023 /* Set up the timer for the first expiration. */
Shinya Kuribayashia55d4812008-06-05 22:29:00 +090024 write_c0_compare(read_c0_count() + CYCLES_PER_JIFFY);
wdenkc0218802003-03-27 12:09:35 +000025
26 return 0;
27}
28
wdenkc0218802003-03-27 12:09:35 +000029ulong get_timer(ulong base)
30{
Shinya Kuribayashia55d4812008-06-05 22:29:00 +090031 unsigned int count;
32 unsigned int expirelo = read_c0_compare();
33
34 /* Check to see if we have missed any timestamps. */
35 count = read_c0_count();
36 while ((count - expirelo) < 0x7fffffff) {
37 expirelo += CYCLES_PER_JIFFY;
38 timestamp++;
39 }
40 write_c0_compare(expirelo);
41
Gabor Juhosc3e49012013-06-12 18:02:43 +020042 return timestamp - base;
wdenkc0218802003-03-27 12:09:35 +000043}
44
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010045void __udelay(unsigned long usec)
wdenkc0218802003-03-27 12:09:35 +000046{
Shinya Kuribayashi199e4f62008-06-05 22:29:00 +090047 unsigned int tmo;
wdenkc0218802003-03-27 12:09:35 +000048
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020049 tmo = read_c0_count() + (usec * (CONFIG_SYS_MIPS_TIMER_FREQ / 1000000));
Shinya Kuribayashi199e4f62008-06-05 22:29:00 +090050 while ((tmo - read_c0_count()) < 0x7fffffff)
wdenkc0218802003-03-27 12:09:35 +000051 /*NOP*/;
52}
53
54/*
55 * This function is derived from PowerPC code (read timebase as long long).
56 * On MIPS it just returns the timer value.
57 */
58unsigned long long get_ticks(void)
59{
Shinya Kuribayashia55d4812008-06-05 22:29:00 +090060 return get_timer(0);
wdenkc0218802003-03-27 12:09:35 +000061}
62
63/*
64 * This function is derived from PowerPC code (timebase clock frequency).
65 * On MIPS it returns the number of timer ticks per second.
66 */
67ulong get_tbclk(void)
68{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020069 return CONFIG_SYS_HZ;
wdenkc0218802003-03-27 12:09:35 +000070}