uboot/arch/arm/cpu/armv7/rmobile/timer.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
   3 * (C) Copyright 2012 Renesas Solutions Corp.
   4 *
   5 * SPDX-License-Identifier:     GPL-2.0+
   6 */
   7
   8#include <common.h>
   9#include <asm/io.h>
  10#include <asm/arch-armv7/globaltimer.h>
  11#include <asm/arch/rmobile.h>
  12
  13static struct globaltimer *global_timer = \
  14                (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
  15
  16#define CLK2MHZ(clk)    (clk / 1000 / 1000)
  17static u64 get_cpu_global_timer(void)
  18{
  19        u32 low, high;
  20        u64 timer;
  21
  22        u32 old = readl(&global_timer->cnt_h);
  23        while (1) {
  24                low = readl(&global_timer->cnt_l);
  25                high = readl(&global_timer->cnt_h);
  26                if (old == high)
  27                        break;
  28                else
  29                        old = high;
  30        }
  31
  32        timer = high;
  33        return (u64)((timer << 32) | low);
  34}
  35
  36static u64 get_time_us(void)
  37{
  38        u64 timer = get_cpu_global_timer();
  39
  40        timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
  41        timer /= (u64)CLK2MHZ(CONFIG_SYS_CPU_CLK);
  42        return timer;
  43}
  44
  45static ulong get_time_ms(void)
  46{
  47        return (ulong)(get_time_us() / 1000);
  48}
  49
  50int timer_init(void)
  51{
  52        writel(0x01, &global_timer->ctl);
  53        return 0;
  54}
  55
  56void __udelay(unsigned long usec)
  57{
  58        u64 start, current;
  59        u64 wait;
  60
  61        start = get_cpu_global_timer();
  62        wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
  63        do {
  64                current = get_cpu_global_timer();
  65        } while ((current - start) < wait);
  66}
  67
  68ulong get_timer(ulong base)
  69{
  70        return get_time_ms() - base;
  71}
  72
  73unsigned long long get_ticks(void)
  74{
  75        return get_cpu_global_timer();
  76}
  77
  78ulong get_tbclk(void)
  79{
  80        return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
  81}
  82