Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2009 Faraday Technology |
| 3 | * Po-Yu Chuang <ratbert@faraday-tech.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 9 | #include <div64.h> |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 10 | #include <asm/io.h> |
Po-Yu Chuang | 8dc667c | 2011-02-17 19:35:23 +0000 | [diff] [blame] | 11 | #include <faraday/ftpmu010.h> |
Macpaul Lin | 844c05a | 2011-03-21 01:45:43 +0000 | [diff] [blame] | 12 | #include <faraday/fttmr010.h> |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 13 | |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 15 | |
| 16 | #define TIMER_CLOCK 32768 |
| 17 | #define TIMER_LOAD_VAL 0xffffffff |
| 18 | |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 19 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 20 | { |
| 21 | tick *= CONFIG_SYS_HZ; |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 22 | do_div(tick, gd->arch.timer_rate_hz); |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 23 | |
| 24 | return tick; |
| 25 | } |
| 26 | |
| 27 | static inline unsigned long long usec_to_tick(unsigned long long usec) |
| 28 | { |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 29 | usec *= gd->arch.timer_rate_hz; |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 30 | do_div(usec, 1000000); |
| 31 | |
| 32 | return usec; |
| 33 | } |
| 34 | |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 35 | int timer_init(void) |
| 36 | { |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 37 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 38 | unsigned int cr; |
| 39 | |
| 40 | debug("%s()\n", __func__); |
| 41 | |
| 42 | /* disable timers */ |
| 43 | writel(0, &tmr->cr); |
| 44 | |
Po-Yu Chuang | 8dc667c | 2011-02-17 19:35:23 +0000 | [diff] [blame] | 45 | /* use 32768Hz oscillator for RTC, WDT, TIMER */ |
| 46 | ftpmu010_32768osc_enable(); |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 47 | |
| 48 | /* setup timer */ |
| 49 | writel(TIMER_LOAD_VAL, &tmr->timer3_load); |
| 50 | writel(TIMER_LOAD_VAL, &tmr->timer3_counter); |
| 51 | writel(0, &tmr->timer3_match1); |
| 52 | writel(0, &tmr->timer3_match2); |
| 53 | |
| 54 | /* we don't want timer to issue interrupts */ |
| 55 | writel(FTTMR010_TM3_MATCH1 | |
| 56 | FTTMR010_TM3_MATCH2 | |
| 57 | FTTMR010_TM3_OVERFLOW, |
| 58 | &tmr->interrupt_mask); |
| 59 | |
| 60 | cr = readl(&tmr->cr); |
| 61 | cr |= FTTMR010_TM3_CLOCK; /* use external clock */ |
| 62 | cr |= FTTMR010_TM3_ENABLE; |
| 63 | writel(cr, &tmr->cr); |
| 64 | |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 65 | gd->arch.timer_rate_hz = TIMER_CLOCK; |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 66 | gd->arch.tbu = gd->arch.tbl = 0; |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 72 | * Get the current 64 bit timer tick count |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 73 | */ |
| 74 | unsigned long long get_ticks(void) |
| 75 | { |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 76 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
| 77 | ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter); |
| 78 | |
| 79 | /* increment tbu if tbl has rolled over */ |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 80 | if (now < gd->arch.tbl) |
Simon Glass | 8ff43b0 | 2012-12-13 20:48:33 +0000 | [diff] [blame] | 81 | gd->arch.tbu++; |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 82 | gd->arch.tbl = now; |
| 83 | return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl; |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void __udelay(unsigned long usec) |
| 87 | { |
| 88 | unsigned long long start; |
| 89 | ulong tmo; |
| 90 | |
| 91 | start = get_ticks(); /* get current timestamp */ |
| 92 | tmo = usec_to_tick(usec); /* convert usecs to ticks */ |
| 93 | while ((get_ticks() - start) < tmo) |
| 94 | ; /* loop till time has passed */ |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
Po-Yu Chuang | 60d1ea9 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 98 | * get_timer(base) can be used to check for timeouts or |
| 99 | * to measure elasped time relative to an event: |
| 100 | * |
| 101 | * ulong start_time = get_timer(0) sets start_time to the current |
| 102 | * time value. |
| 103 | * get_timer(start_time) returns the time elapsed since then. |
| 104 | * |
| 105 | * The time is used in CONFIG_SYS_HZ units! |
| 106 | */ |
| 107 | ulong get_timer(ulong base) |
| 108 | { |
| 109 | return tick_to_time(get_ticks()) - base; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Return the number of timer ticks per second. |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 114 | */ |
| 115 | ulong get_tbclk(void) |
| 116 | { |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 117 | return gd->arch.timer_rate_hz; |
Po-Yu Chuang | 43a5f0d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 118 | } |