1 /* 2 * (C) Copyright 2000-2009 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <watchdog.h> 10 #include <div64.h> 11 #include <asm/io.h> 12 13 #if CONFIG_SYS_HZ != 1000 14 #warning "CONFIG_SYS_HZ must be 1000 and should not be defined by platforms" 15 #endif 16 17 #ifndef CONFIG_WD_PERIOD 18 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ 19 #endif 20 21 DECLARE_GLOBAL_DATA_PTR; 22 23 #ifdef CONFIG_SYS_TIMER_RATE 24 /* Returns tick rate in ticks per second */ 25 ulong notrace get_tbclk(void) 26 { 27 return CONFIG_SYS_TIMER_RATE; 28 } 29 #endif 30 31 #ifdef CONFIG_SYS_TIMER_COUNTER 32 unsigned long notrace timer_read_counter(void) 33 { 34 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN 35 return ~readl(CONFIG_SYS_TIMER_COUNTER); 36 #else 37 return readl(CONFIG_SYS_TIMER_COUNTER); 38 #endif 39 } 40 #else 41 extern unsigned long __weak timer_read_counter(void); 42 #endif 43 44 unsigned long long __weak notrace get_ticks(void) 45 { 46 unsigned long now = timer_read_counter(); 47 48 /* increment tbu if tbl has rolled over */ 49 if (now < gd->timebase_l) 50 gd->timebase_h++; 51 gd->timebase_l = now; 52 return ((unsigned long long)gd->timebase_h << 32) | gd->timebase_l; 53 } 54 55 /* Returns time in milliseconds */ 56 static unsigned long long notrace tick_to_time(unsigned long long tick) 57 { 58 ulong div = get_tbclk(); 59 60 tick *= CONFIG_SYS_HZ; 61 do_div(tick, div); 62 return tick; 63 } 64 65 int __weak timer_init(void) 66 { 67 return 0; 68 } 69 70 /* Returns time in milliseconds */ 71 ulong __weak get_timer(ulong base) 72 { 73 return tick_to_time(get_ticks()) - base; 74 } 75 76 unsigned long __weak notrace timer_get_us(void) 77 { 78 return tick_to_time(get_ticks() * 1000); 79 } 80 81 static unsigned long long usec_to_tick(unsigned long usec) 82 { 83 unsigned long long tick = usec; 84 tick *= get_tbclk(); 85 do_div(tick, 1000000); 86 return tick; 87 } 88 89 void __weak __udelay(unsigned long usec) 90 { 91 unsigned long long tmp; 92 93 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ 94 95 while (get_ticks() < tmp+1) /* loop till event */ 96 /*NOP*/; 97 } 98 99 /* ------------------------------------------------------------------------- */ 100 101 void udelay(unsigned long usec) 102 { 103 ulong kv; 104 105 do { 106 WATCHDOG_RESET(); 107 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; 108 __udelay (kv); 109 usec -= kv; 110 } while(usec); 111 } 112 113 void mdelay(unsigned long msec) 114 { 115 while (msec--) 116 udelay(1000); 117 } 118