Po-Yu Chuang | 5614a4d | 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 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <common.h> |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 21 | #include <div64.h> |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 22 | #include <asm/io.h> |
Po-Yu Chuang | 758898d | 2011-02-17 19:35:23 +0000 | [diff] [blame] | 23 | #include <faraday/ftpmu010.h> |
Macpaul Lin | 0d0783e | 2011-03-21 01:45:43 +0000 | [diff] [blame] | 24 | #include <faraday/fttmr010.h> |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 25 | |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 27 | |
| 28 | #define TIMER_CLOCK 32768 |
| 29 | #define TIMER_LOAD_VAL 0xffffffff |
| 30 | |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 31 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 32 | { |
| 33 | tick *= CONFIG_SYS_HZ; |
| 34 | do_div(tick, gd->timer_rate_hz); |
| 35 | |
| 36 | return tick; |
| 37 | } |
| 38 | |
| 39 | static inline unsigned long long usec_to_tick(unsigned long long usec) |
| 40 | { |
| 41 | usec *= gd->timer_rate_hz; |
| 42 | do_div(usec, 1000000); |
| 43 | |
| 44 | return usec; |
| 45 | } |
| 46 | |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 47 | int timer_init(void) |
| 48 | { |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 49 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 50 | unsigned int cr; |
| 51 | |
| 52 | debug("%s()\n", __func__); |
| 53 | |
| 54 | /* disable timers */ |
| 55 | writel(0, &tmr->cr); |
| 56 | |
Po-Yu Chuang | 758898d | 2011-02-17 19:35:23 +0000 | [diff] [blame] | 57 | /* use 32768Hz oscillator for RTC, WDT, TIMER */ |
| 58 | ftpmu010_32768osc_enable(); |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 59 | |
| 60 | /* setup timer */ |
| 61 | writel(TIMER_LOAD_VAL, &tmr->timer3_load); |
| 62 | writel(TIMER_LOAD_VAL, &tmr->timer3_counter); |
| 63 | writel(0, &tmr->timer3_match1); |
| 64 | writel(0, &tmr->timer3_match2); |
| 65 | |
| 66 | /* we don't want timer to issue interrupts */ |
| 67 | writel(FTTMR010_TM3_MATCH1 | |
| 68 | FTTMR010_TM3_MATCH2 | |
| 69 | FTTMR010_TM3_OVERFLOW, |
| 70 | &tmr->interrupt_mask); |
| 71 | |
| 72 | cr = readl(&tmr->cr); |
| 73 | cr |= FTTMR010_TM3_CLOCK; /* use external clock */ |
| 74 | cr |= FTTMR010_TM3_ENABLE; |
| 75 | writel(cr, &tmr->cr); |
| 76 | |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 77 | gd->timer_rate_hz = TIMER_CLOCK; |
| 78 | gd->tbu = gd->tbl = 0; |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /* |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 84 | * Get the current 64 bit timer tick count |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 85 | */ |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 86 | unsigned long long get_ticks(void) |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 87 | { |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 88 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
| 89 | ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter); |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 90 | |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 91 | /* increment tbu if tbl has rolled over */ |
| 92 | if (now < gd->tbl) |
| 93 | gd->tbu++; |
| 94 | gd->tbl = now; |
| 95 | return (((unsigned long long)gd->tbu) << 32) | gd->tbl; |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 96 | } |
| 97 | |
Wolfgang Denk | 0160884 | 2010-05-21 23:14:53 +0200 | [diff] [blame] | 98 | void __udelay(unsigned long usec) |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 99 | { |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 100 | unsigned long long start; |
| 101 | ulong tmo; |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 102 | |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 103 | start = get_ticks(); /* get current timestamp */ |
| 104 | tmo = usec_to_tick(usec); /* convert usecs to ticks */ |
| 105 | while ((get_ticks() - start) < tmo) |
| 106 | ; /* loop till time has passed */ |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 110 | * get_timer(base) can be used to check for timeouts or |
| 111 | * to measure elasped time relative to an event: |
| 112 | * |
| 113 | * ulong start_time = get_timer(0) sets start_time to the current |
| 114 | * time value. |
| 115 | * get_timer(start_time) returns the time elapsed since then. |
| 116 | * |
| 117 | * The time is used in CONFIG_SYS_HZ units! |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 118 | */ |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 119 | ulong get_timer(ulong base) |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 120 | { |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 121 | return tick_to_time(get_ticks()) - base; |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 125 | * Return the number of timer ticks per second. |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 126 | */ |
| 127 | ulong get_tbclk(void) |
| 128 | { |
Po-Yu Chuang | bf7a526 | 2011-08-10 17:44:21 +0000 | [diff] [blame] | 129 | return gd->timer_rate_hz; |
Po-Yu Chuang | 5614a4d | 2009-11-11 17:27:30 +0800 | [diff] [blame] | 130 | } |