David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2013 |
| 3 | * David Feng <fenghua@phytium.com.cn> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <asm/system.h> |
| 11 | |
Andre Przywara | de223fd | 2016-11-03 00:56:25 +0000 | [diff] [blame] | 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 14 | /* |
| 15 | * Generic timer implementation of get_tbclk() |
| 16 | */ |
| 17 | unsigned long get_tbclk(void) |
| 18 | { |
| 19 | unsigned long cntfrq; |
| 20 | asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq)); |
| 21 | return cntfrq; |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * Generic timer implementation of timer_read_counter() |
| 26 | */ |
| 27 | unsigned long timer_read_counter(void) |
| 28 | { |
| 29 | unsigned long cntpct; |
York Sun | a7686cf | 2015-03-20 19:28:05 -0700 | [diff] [blame] | 30 | #ifdef CONFIG_SYS_FSL_ERRATUM_A008585 |
| 31 | /* This erratum number needs to be confirmed to match ARM document */ |
| 32 | unsigned long temp; |
| 33 | #endif |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 34 | isb(); |
| 35 | asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct)); |
York Sun | a7686cf | 2015-03-20 19:28:05 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_SYS_FSL_ERRATUM_A008585 |
| 37 | asm volatile("mrs %0, cntpct_el0" : "=r" (temp)); |
| 38 | while (temp != cntpct) { |
| 39 | asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct)); |
| 40 | asm volatile("mrs %0, cntpct_el0" : "=r" (temp)); |
| 41 | } |
| 42 | #endif |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 43 | return cntpct; |
| 44 | } |
Aneesh Bansal | d1074b4 | 2015-12-08 13:54:26 +0530 | [diff] [blame] | 45 | |
Andre Przywara | de223fd | 2016-11-03 00:56:25 +0000 | [diff] [blame] | 46 | unsigned long long get_ticks(void) |
| 47 | { |
| 48 | unsigned long ticks = timer_read_counter(); |
| 49 | |
| 50 | gd->arch.tbl = ticks; |
| 51 | |
| 52 | return ticks; |
| 53 | } |
| 54 | |
Aneesh Bansal | d1074b4 | 2015-12-08 13:54:26 +0530 | [diff] [blame] | 55 | unsigned long usec2ticks(unsigned long usec) |
| 56 | { |
| 57 | ulong ticks; |
| 58 | if (usec < 1000) |
| 59 | ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000; |
| 60 | else |
| 61 | ticks = ((usec / 10) * (get_tbclk() / 100000)); |
| 62 | |
| 63 | return ticks; |
| 64 | } |