Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 - 2013 Tensilica Inc. |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
Tom Rini | 8c70baa | 2021-12-14 13:36:40 -0500 | [diff] [blame] | 6 | #include <clock_legacy.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 7 | #include <time.h> |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 9 | #include <linux/delay.h> |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 10 | #include <linux/stringify.h> |
| 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
| 14 | #if XCHAL_HAVE_CCOUNT |
| 15 | static ulong get_ccount(void) |
| 16 | { |
| 17 | ulong ccount; |
| 18 | asm volatile ("rsr %0,"__stringify(CCOUNT) : "=a" (ccount)); |
| 19 | return ccount; |
| 20 | } |
| 21 | #else |
| 22 | static ulong fake_ccount; |
| 23 | #define get_ccount() fake_ccount |
| 24 | #endif |
| 25 | |
| 26 | static void delay_cycles(unsigned cycles) |
| 27 | { |
| 28 | #if XCHAL_HAVE_CCOUNT |
| 29 | unsigned expiry = get_ccount() + cycles; |
| 30 | while ((signed)(expiry - get_ccount()) > 0) |
| 31 | ; |
| 32 | #else |
| 33 | #warning "Without Xtensa timer option, timing will not be accurate." |
| 34 | |
| 35 | /* |
| 36 | * Approximate the cycle count by a loop iteration count. |
| 37 | * This is highly dependent on config and optimization. |
| 38 | */ |
| 39 | |
| 40 | volatile unsigned i; |
| 41 | for (i = cycles >> 4U; i > 0; --i) |
| 42 | ; |
| 43 | fake_ccount += cycles; |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Delay (busy-wait) for a number of microseconds. |
| 49 | */ |
| 50 | |
| 51 | void __udelay(unsigned long usec) |
| 52 | { |
| 53 | ulong lo, hi, i; |
Tom Rini | 8c70baa | 2021-12-14 13:36:40 -0500 | [diff] [blame] | 54 | ulong mhz = get_board_sys_clk() / 1000000; |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 55 | |
| 56 | /* Scale to support full 32-bit usec range */ |
| 57 | |
| 58 | lo = usec & ((1<<22)-1); |
| 59 | hi = usec >> 22UL; |
| 60 | for (i = 0; i < hi; ++i) |
| 61 | delay_cycles(mhz << 22); |
| 62 | delay_cycles(mhz * lo); |
| 63 | } |
| 64 | |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 65 | /* |
| 66 | * Return the elapsed time (ticks) since 'base'. |
| 67 | */ |
| 68 | |
| 69 | ulong get_timer(ulong base) |
| 70 | { |
| 71 | /* Don't tie up a timer; use cycle counter if available (or fake it) */ |
| 72 | |
| 73 | #if XCHAL_HAVE_CCOUNT |
| 74 | register ulong ccount; |
| 75 | __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount)); |
Tom Rini | 8c70baa | 2021-12-14 13:36:40 -0500 | [diff] [blame] | 76 | return ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base; |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 77 | #else |
| 78 | /* |
| 79 | * Add at least the overhead of this call (in cycles). |
| 80 | * Avoids hanging in case caller doesn't use udelay(). |
| 81 | * Note that functions that don't call udelay() (such as |
| 82 | * the "sleep" command) will not get a significant delay |
| 83 | * because there is no time reference. |
| 84 | */ |
| 85 | |
| 86 | fake_ccount += 20; |
Tom Rini | 8c70baa | 2021-12-14 13:36:40 -0500 | [diff] [blame] | 87 | return fake_ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base; |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 88 | #endif |
| 89 | } |
| 90 | |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 91 | /* |
| 92 | * This function is derived from ARM/PowerPC code (read timebase as long long). |
| 93 | * On Xtensa it just returns the timer value. |
| 94 | */ |
| 95 | unsigned long long get_ticks(void) |
| 96 | { |
| 97 | return get_timer(0); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * This function is derived from ARM/PowerPC code (timebase clock frequency). |
| 102 | * On Xtensa it returns the number of timer ticks per second. |
| 103 | */ |
| 104 | ulong get_tbclk(void) |
| 105 | { |
Masahiro Yamada | 04cfea5 | 2016-09-06 22:17:38 +0900 | [diff] [blame] | 106 | return CONFIG_SYS_HZ; |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | #if XCHAL_HAVE_CCOUNT |
| 110 | unsigned long timer_get_us(void) |
| 111 | { |
| 112 | unsigned long ccount; |
| 113 | |
| 114 | __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount)); |
Tom Rini | 8c70baa | 2021-12-14 13:36:40 -0500 | [diff] [blame] | 115 | return ccount / (get_board_sys_clk() / 1000000); |
Chris Zankel | 1387dab | 2016-08-10 18:36:44 +0300 | [diff] [blame] | 116 | } |
| 117 | #endif |