Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 Faraday Technology |
| 4 | * Po-Yu Chuang <ratbert@faraday-tech.com> |
| 5 | * |
| 6 | * Copyright (C) 2011 Andes Technology Corporation |
| 7 | * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com> |
| 8 | * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com> |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 9 | */ |
rick | 6d564b8 | 2017-05-17 10:59:20 +0800 | [diff] [blame] | 10 | #ifndef CONFIG_TIMER |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 12 | #include <init.h> |
Simon Glass | 9b61c7c | 2019-11-14 12:57:41 -0700 | [diff] [blame] | 13 | #include <irq_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 15 | #include <time.h> |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | #include <faraday/fttmr010.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame^] | 18 | #include <linux/delay.h> |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 19 | |
| 20 | static ulong timestamp; |
| 21 | static ulong lastdec; |
| 22 | |
| 23 | int timer_init(void) |
| 24 | { |
Macpaul Lin | f4a7b80 | 2011-11-23 13:56:50 +0800 | [diff] [blame] | 25 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 26 | unsigned int cr; |
| 27 | |
| 28 | debug("%s()\n", __func__); |
| 29 | |
| 30 | /* disable timers */ |
| 31 | writel(0, &tmr->cr); |
| 32 | |
| 33 | #ifdef CONFIG_FTTMR010_EXT_CLK |
| 34 | /* use 32768Hz oscillator for RTC, WDT, TIMER */ |
| 35 | ftpmu010_32768osc_enable(); |
| 36 | #endif |
| 37 | |
| 38 | /* setup timer */ |
| 39 | writel(TIMER_LOAD_VAL, &tmr->timer3_load); |
| 40 | writel(TIMER_LOAD_VAL, &tmr->timer3_counter); |
| 41 | writel(0, &tmr->timer3_match1); |
| 42 | writel(0, &tmr->timer3_match2); |
| 43 | |
| 44 | /* we don't want timer to issue interrupts */ |
| 45 | writel(FTTMR010_TM3_MATCH1 | |
| 46 | FTTMR010_TM3_MATCH2 | |
| 47 | FTTMR010_TM3_OVERFLOW, |
| 48 | &tmr->interrupt_mask); |
| 49 | |
| 50 | cr = readl(&tmr->cr); |
| 51 | #ifdef CONFIG_FTTMR010_EXT_CLK |
| 52 | cr |= FTTMR010_TM3_CLOCK; /* use external clock */ |
| 53 | #endif |
| 54 | cr |= FTTMR010_TM3_ENABLE; |
| 55 | writel(cr, &tmr->cr); |
| 56 | |
| 57 | /* init the timestamp and lastdec value */ |
| 58 | reset_timer_masked(); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * timer without interrupts |
| 65 | */ |
| 66 | |
| 67 | /* |
| 68 | * reset time |
| 69 | */ |
| 70 | void reset_timer_masked(void) |
| 71 | { |
Macpaul Lin | f4a7b80 | 2011-11-23 13:56:50 +0800 | [diff] [blame] | 72 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 73 | |
| 74 | /* capure current decrementer value time */ |
| 75 | #ifdef CONFIG_FTTMR010_EXT_CLK |
| 76 | lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ); |
| 77 | #else |
Axel Lin | 55edeb5 | 2013-07-08 14:29:52 +0800 | [diff] [blame] | 78 | lastdec = readl(&tmr->timer3_counter) / |
| 79 | (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ); |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 80 | #endif |
| 81 | timestamp = 0; /* start "advancing" time stamp from 0 */ |
| 82 | |
| 83 | debug("%s(): lastdec = %lx\n", __func__, lastdec); |
| 84 | } |
| 85 | |
| 86 | void reset_timer(void) |
| 87 | { |
| 88 | debug("%s()\n", __func__); |
| 89 | reset_timer_masked(); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * return timer ticks |
| 94 | */ |
| 95 | ulong get_timer_masked(void) |
| 96 | { |
Macpaul Lin | f4a7b80 | 2011-11-23 13:56:50 +0800 | [diff] [blame] | 97 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 98 | |
| 99 | /* current tick value */ |
| 100 | #ifdef CONFIG_FTTMR010_EXT_CLK |
| 101 | ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ); |
| 102 | #else |
Axel Lin | 55edeb5 | 2013-07-08 14:29:52 +0800 | [diff] [blame] | 103 | ulong now = readl(&tmr->timer3_counter) / |
| 104 | (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ); |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
| 107 | debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec); |
| 108 | |
| 109 | if (lastdec >= now) { |
| 110 | /* |
| 111 | * normal mode (non roll) |
| 112 | * move stamp fordward with absoulte diff ticks |
| 113 | */ |
| 114 | timestamp += lastdec - now; |
| 115 | } else { |
| 116 | /* |
| 117 | * we have overflow of the count down timer |
| 118 | * |
| 119 | * nts = ts + ld + (TLV - now) |
| 120 | * ts=old stamp, ld=time that passed before passing through -1 |
| 121 | * (TLV-now) amount of time after passing though -1 |
| 122 | * nts = new "advancing time stamp"...it could also roll and |
| 123 | * cause problems. |
| 124 | */ |
| 125 | timestamp += lastdec + TIMER_LOAD_VAL - now; |
| 126 | } |
| 127 | |
| 128 | lastdec = now; |
| 129 | |
| 130 | debug("%s() returns %lx\n", __func__, timestamp); |
| 131 | |
| 132 | return timestamp; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * return difference between timer ticks and base |
| 137 | */ |
| 138 | ulong get_timer(ulong base) |
| 139 | { |
| 140 | debug("%s(%lx)\n", __func__, base); |
| 141 | return get_timer_masked() - base; |
| 142 | } |
| 143 | |
| 144 | void set_timer(ulong t) |
| 145 | { |
| 146 | debug("%s(%lx)\n", __func__, t); |
| 147 | timestamp = t; |
| 148 | } |
| 149 | |
| 150 | /* delay x useconds AND preserve advance timestamp value */ |
| 151 | void __udelay(unsigned long usec) |
| 152 | { |
Macpaul Lin | f4a7b80 | 2011-11-23 13:56:50 +0800 | [diff] [blame] | 153 | struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE; |
Macpaul Lin | 83dbca7 | 2011-10-11 22:33:18 +0000 | [diff] [blame] | 154 | |
| 155 | #ifdef CONFIG_FTTMR010_EXT_CLK |
| 156 | long tmo = usec * (TIMER_CLOCK / 1000) / 1000; |
| 157 | #else |
| 158 | long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000; |
| 159 | #endif |
| 160 | unsigned long now, last = readl(&tmr->timer3_counter); |
| 161 | |
| 162 | debug("%s(%lu)\n", __func__, usec); |
| 163 | while (tmo > 0) { |
| 164 | now = readl(&tmr->timer3_counter); |
| 165 | if (now > last) /* count down timer overflow */ |
| 166 | tmo -= TIMER_LOAD_VAL + last - now; |
| 167 | else |
| 168 | tmo -= last - now; |
| 169 | last = now; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * This function is derived from PowerPC code (read timebase as long long). |
| 175 | * On ARM it just returns the timer value. |
| 176 | */ |
| 177 | unsigned long long get_ticks(void) |
| 178 | { |
| 179 | debug("%s()\n", __func__); |
| 180 | return get_timer(0); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * This function is derived from PowerPC code (timebase clock frequency). |
| 185 | * On ARM it returns the number of timer ticks per second. |
| 186 | */ |
| 187 | ulong get_tbclk(void) |
| 188 | { |
| 189 | debug("%s()\n", __func__); |
| 190 | #ifdef CONFIG_FTTMR010_EXT_CLK |
| 191 | return CONFIG_SYS_HZ; |
| 192 | #else |
| 193 | return CONFIG_SYS_CLK_FREQ; |
| 194 | #endif |
| 195 | } |
rick | 6d564b8 | 2017-05-17 10:59:20 +0800 | [diff] [blame] | 196 | #endif /* CONFIG_TIMER */ |