Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wang Huan | 8ce6bec | 2014-09-05 13:52:34 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Freescale Semiconductor, Inc. |
Wang Huan | 8ce6bec | 2014-09-05 13:52:34 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 7 | #include <time.h> |
Wang Huan | 8ce6bec | 2014-09-05 13:52:34 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
| 9 | #include <div64.h> |
| 10 | #include <asm/arch/immap_ls102xa.h> |
| 11 | #include <asm/arch/clock.h> |
| 12 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
| 15 | /* |
| 16 | * This function is intended for SHORT delays only. |
| 17 | * It will overflow at around 10 seconds @ 400MHz, |
| 18 | * or 20 seconds @ 200MHz. |
| 19 | */ |
| 20 | unsigned long usec2ticks(unsigned long usec) |
| 21 | { |
| 22 | ulong ticks; |
| 23 | |
| 24 | if (usec < 1000) |
| 25 | ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000; |
| 26 | else |
| 27 | ticks = ((usec / 10) * (get_tbclk() / 100000)); |
| 28 | |
| 29 | return ticks; |
| 30 | } |
| 31 | |
| 32 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 33 | { |
| 34 | unsigned long freq; |
| 35 | |
| 36 | asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq)); |
| 37 | |
| 38 | tick *= CONFIG_SYS_HZ; |
| 39 | do_div(tick, freq); |
| 40 | |
| 41 | return tick; |
| 42 | } |
| 43 | |
| 44 | static inline unsigned long long us_to_tick(unsigned long long usec) |
| 45 | { |
| 46 | unsigned long freq; |
| 47 | |
| 48 | asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq)); |
| 49 | |
| 50 | usec = usec * freq + 999999; |
| 51 | do_div(usec, 1000000); |
| 52 | |
| 53 | return usec; |
| 54 | } |
| 55 | |
| 56 | int timer_init(void) |
| 57 | { |
| 58 | struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR; |
Alison Wang | 25c9dc5 | 2015-07-15 15:13:05 +0800 | [diff] [blame] | 59 | unsigned long ctrl, freq; |
| 60 | unsigned long long val; |
Wang Huan | 8ce6bec | 2014-09-05 13:52:34 +0800 | [diff] [blame] | 61 | |
| 62 | /* Enable System Counter */ |
| 63 | writel(SYS_COUNTER_CTRL_ENABLE, &sctr->cntcr); |
| 64 | |
Andre Przywara | 9d095c3 | 2017-02-16 01:20:20 +0000 | [diff] [blame] | 65 | freq = COUNTER_FREQUENCY; |
Wang Huan | 8ce6bec | 2014-09-05 13:52:34 +0800 | [diff] [blame] | 66 | asm("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq)); |
| 67 | |
| 68 | /* Set PL1 Physical Timer Ctrl */ |
| 69 | ctrl = ARCH_TIMER_CTRL_ENABLE; |
| 70 | asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl)); |
| 71 | |
| 72 | /* Set PL1 Physical Comp Value */ |
| 73 | val = TIMER_COMP_VAL; |
| 74 | asm("mcrr p15, 2, %Q0, %R0, c14" : : "r" (val)); |
| 75 | |
| 76 | gd->arch.tbl = 0; |
| 77 | gd->arch.tbu = 0; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | unsigned long long get_ticks(void) |
| 83 | { |
| 84 | unsigned long long now; |
| 85 | |
| 86 | asm("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now)); |
| 87 | |
| 88 | gd->arch.tbl = (unsigned long)(now & 0xffffffff); |
| 89 | gd->arch.tbu = (unsigned long)(now >> 32); |
| 90 | |
| 91 | return now; |
| 92 | } |
| 93 | |
Wang Huan | 8ce6bec | 2014-09-05 13:52:34 +0800 | [diff] [blame] | 94 | unsigned long get_timer(ulong base) |
| 95 | { |
Patrick Delaunay | 9858a60 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 96 | return tick_to_time(get_ticks()) - base; |
Wang Huan | 8ce6bec | 2014-09-05 13:52:34 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* delay x useconds and preserve advance timstamp value */ |
| 100 | void __udelay(unsigned long usec) |
| 101 | { |
| 102 | unsigned long long start; |
| 103 | unsigned long tmo; |
| 104 | |
| 105 | start = get_ticks(); /* get current timestamp */ |
| 106 | tmo = us_to_tick(usec); /* convert usecs to ticks */ |
| 107 | |
| 108 | while ((get_ticks() - start) < tmo) |
| 109 | ; /* loop till time has passed */ |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * This function is derived from PowerPC code (timebase clock frequency). |
| 114 | * On ARM it returns the number of timer ticks per second. |
| 115 | */ |
| 116 | unsigned long get_tbclk(void) |
| 117 | { |
| 118 | unsigned long freq; |
| 119 | |
| 120 | asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq)); |
| 121 | |
| 122 | return freq; |
| 123 | } |