1 /* 2 * (C) Copyright 2012-2014 3 * Texas Instruments Incorporated, <www.ti.com> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <asm/io.h> 10 #include <div64.h> 11 #include <bootstage.h> 12 13 DECLARE_GLOBAL_DATA_PTR; 14 15 int timer_init(void) 16 { 17 gd->arch.tbl = 0; 18 gd->arch.tbu = 0; 19 20 gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ; 21 return 0; 22 } 23 24 unsigned long long get_ticks(void) 25 { 26 ulong nowl, nowu; 27 28 asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (nowl), "=r" (nowu)); 29 30 gd->arch.tbl = nowl; 31 gd->arch.tbu = nowu; 32 33 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl; 34 } 35 36 37 ulong get_timer(ulong base) 38 { 39 return lldiv(get_ticks(), gd->arch.timer_rate_hz) - base; 40 } 41 42 ulong timer_get_boot_us(void) 43 { 44 return lldiv(get_ticks(), CONFIG_SYS_HZ_CLOCK / (CONFIG_SYS_HZ * 1000)); 45 } 46 47 void __udelay(unsigned long usec) 48 { 49 unsigned long long endtime; 50 51 endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz, 52 1000UL); 53 54 endtime += get_ticks(); 55 56 while (get_ticks() < endtime) 57 ; 58 } 59 60 ulong get_tbclk(void) 61 { 62 return gd->arch.timer_rate_hz; 63 } 64