1 /* 2 * Copyright (C) 2009 Samsung Electronics 3 * Heungjun Kim <riverful.kim@samsung.com> 4 * Inki Dae <inki.dae@samsung.com> 5 * Minkyu Kang <mk7.kang@samsung.com> 6 * 7 * See file CREDITS for list of people who contributed to this 8 * project. 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of 13 * the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 23 * MA 02111-1307 USA 24 */ 25 26 #include <common.h> 27 #include <asm/io.h> 28 #include <asm/arch/pwm.h> 29 #include <asm/arch/clk.h> 30 31 #define PRESCALER_1 (16 - 1) /* prescaler of timer 2, 3, 4 */ 32 #define MUX_DIV_2 1 /* 1/2 period */ 33 #define MUX_DIV_4 2 /* 1/4 period */ 34 #define MUX_DIV_8 3 /* 1/8 period */ 35 #define MUX_DIV_16 4 /* 1/16 period */ 36 #define MUX4_DIV_SHIFT 16 37 38 #define TCON_TIMER4_SHIFT 20 39 40 static unsigned long count_value; 41 42 /* Internal tick units */ 43 static unsigned long long timestamp; /* Monotonic incrementing timer */ 44 static unsigned long lastdec; /* Last decremneter snapshot */ 45 46 /* macro to read the 16 bit timer */ 47 static inline struct s5p_timer *s5p_get_base_timer(void) 48 { 49 return (struct s5p_timer *)samsung_get_base_timer(); 50 } 51 52 int timer_init(void) 53 { 54 struct s5p_timer *const timer = s5p_get_base_timer(); 55 u32 val; 56 57 /* 58 * @ PWM Timer 4 59 * Timer Freq(HZ) = 60 * PWM_CLK / { (prescaler_value + 1) * (divider_value) } 61 */ 62 63 /* set prescaler : 16 */ 64 /* set divider : 2 */ 65 writel((PRESCALER_1 & 0xff) << 8, &timer->tcfg0); 66 writel((MUX_DIV_2 & 0xf) << MUX4_DIV_SHIFT, &timer->tcfg1); 67 68 if (count_value == 0) { 69 /* reset initial value */ 70 /* count_value = 2085937.5(HZ) (per 1 sec)*/ 71 count_value = get_pwm_clk() / ((PRESCALER_1 + 1) * 72 (MUX_DIV_2 + 1)); 73 74 /* count_value / 100 = 20859.375(HZ) (per 10 msec) */ 75 count_value = count_value / 100; 76 } 77 78 /* set count value */ 79 writel(count_value, &timer->tcntb4); 80 lastdec = count_value; 81 82 val = (readl(&timer->tcon) & ~(0x07 << TCON_TIMER4_SHIFT)) | 83 TCON4_AUTO_RELOAD; 84 85 /* auto reload & manual update */ 86 writel(val | TCON4_UPDATE, &timer->tcon); 87 88 /* start PWM timer 4 */ 89 writel(val | TCON4_START, &timer->tcon); 90 91 timestamp = 0; 92 93 return 0; 94 } 95 96 /* 97 * timer without interrupts 98 */ 99 void reset_timer(void) 100 { 101 reset_timer_masked(); 102 } 103 104 unsigned long get_timer(unsigned long base) 105 { 106 return get_timer_masked() - base; 107 } 108 109 void set_timer(unsigned long t) 110 { 111 timestamp = t; 112 } 113 114 /* delay x useconds */ 115 void __udelay(unsigned long usec) 116 { 117 unsigned long tmo, tmp; 118 119 if (usec >= 1000) { 120 /* 121 * if "big" number, spread normalization 122 * to seconds 123 * 1. start to normalize for usec to ticks per sec 124 * 2. find number of "ticks" to wait to achieve target 125 * 3. finish normalize. 126 */ 127 tmo = usec / 1000; 128 tmo *= (CONFIG_SYS_HZ * count_value / 10); 129 tmo /= 1000; 130 } else { 131 /* else small number, don't kill it prior to HZ multiply */ 132 tmo = usec * CONFIG_SYS_HZ * count_value / 10; 133 tmo /= (1000 * 1000); 134 } 135 136 /* get current timestamp */ 137 tmp = get_timer(0); 138 139 /* if setting this fordward will roll time stamp */ 140 /* reset "advancing" timestamp to 0, set lastdec value */ 141 /* else, set advancing stamp wake up time */ 142 if ((tmo + tmp + 1) < tmp) 143 reset_timer_masked(); 144 else 145 tmo += tmp; 146 147 /* loop till event */ 148 while (get_timer_masked() < tmo) 149 ; /* nop */ 150 } 151 152 void reset_timer_masked(void) 153 { 154 struct s5p_timer *const timer = s5p_get_base_timer(); 155 156 /* reset time */ 157 lastdec = readl(&timer->tcnto4); 158 timestamp = 0; 159 } 160 161 unsigned long get_timer_masked(void) 162 { 163 struct s5p_timer *const timer = s5p_get_base_timer(); 164 unsigned long now = readl(&timer->tcnto4); 165 166 if (lastdec >= now) 167 timestamp += lastdec - now; 168 else 169 timestamp += lastdec + count_value - now; 170 171 lastdec = now; 172 173 return timestamp; 174 } 175 176 /* 177 * This function is derived from PowerPC code (read timebase as long long). 178 * On ARM it just returns the timer value. 179 */ 180 unsigned long long get_ticks(void) 181 { 182 return get_timer(0); 183 } 184 185 /* 186 * This function is derived from PowerPC code (timebase clock frequency). 187 * On ARM it returns the number of timer ticks per second. 188 */ 189 unsigned long get_tbclk(void) 190 { 191 return CONFIG_SYS_HZ; 192 } 193