1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * (C) Copyright 2007-2011 3*4882a593Smuzhiyun * Allwinner Technology Co., Ltd. <www.allwinnertech.com> 4*4882a593Smuzhiyun * Tom Cubie <tangliang@allwinnertech.com> 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+ 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun #include <common.h> 10*4882a593Smuzhiyun #include <asm/io.h> 11*4882a593Smuzhiyun #include <asm/arch/timer.h> 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR; 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun #define TIMER_MODE (0x0 << 7) /* continuous mode */ 16*4882a593Smuzhiyun #define TIMER_DIV (0x0 << 4) /* pre scale 1 */ 17*4882a593Smuzhiyun #define TIMER_SRC (0x1 << 2) /* osc24m */ 18*4882a593Smuzhiyun #define TIMER_RELOAD (0x1 << 1) /* reload internal value */ 19*4882a593Smuzhiyun #define TIMER_EN (0x1 << 0) /* enable timer */ 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun #define TIMER_CLOCK (24 * 1000 * 1000) 22*4882a593Smuzhiyun #define COUNT_TO_USEC(x) ((x) / 24) 23*4882a593Smuzhiyun #define USEC_TO_COUNT(x) ((x) * 24) 24*4882a593Smuzhiyun #define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ) 25*4882a593Smuzhiyun #define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ) 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun #define TIMER_LOAD_VAL 0xffffffff 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun #define TIMER_NUM 0 /* we use timer 0 */ 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun /* read the 32-bit timer */ read_timer(void)32*4882a593Smuzhiyunstatic ulong read_timer(void) 33*4882a593Smuzhiyun { 34*4882a593Smuzhiyun struct sunxi_timer_reg *timers = 35*4882a593Smuzhiyun (struct sunxi_timer_reg *)SUNXI_TIMER_BASE; 36*4882a593Smuzhiyun struct sunxi_timer *timer = &timers->timer[TIMER_NUM]; 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun /* 39*4882a593Smuzhiyun * The hardware timer counts down, therefore we invert to 40*4882a593Smuzhiyun * produce an incrementing timer. 41*4882a593Smuzhiyun */ 42*4882a593Smuzhiyun return ~readl(&timer->val); 43*4882a593Smuzhiyun } 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun /* init timer register */ timer_init(void)46*4882a593Smuzhiyunint timer_init(void) 47*4882a593Smuzhiyun { 48*4882a593Smuzhiyun struct sunxi_timer_reg *timers = 49*4882a593Smuzhiyun (struct sunxi_timer_reg *)SUNXI_TIMER_BASE; 50*4882a593Smuzhiyun struct sunxi_timer *timer = &timers->timer[TIMER_NUM]; 51*4882a593Smuzhiyun writel(TIMER_LOAD_VAL, &timer->inter); 52*4882a593Smuzhiyun writel(TIMER_MODE | TIMER_DIV | TIMER_SRC | TIMER_RELOAD | TIMER_EN, 53*4882a593Smuzhiyun &timer->ctl); 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun return 0; 56*4882a593Smuzhiyun } 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun /* timer without interrupts */ get_timer(ulong base)59*4882a593Smuzhiyunulong get_timer(ulong base) 60*4882a593Smuzhiyun { 61*4882a593Smuzhiyun return get_timer_masked() - base; 62*4882a593Smuzhiyun } 63*4882a593Smuzhiyun get_timer_masked(void)64*4882a593Smuzhiyunulong get_timer_masked(void) 65*4882a593Smuzhiyun { 66*4882a593Smuzhiyun /* current tick value */ 67*4882a593Smuzhiyun ulong now = TICKS_TO_HZ(read_timer()); 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun if (now >= gd->arch.lastinc) /* normal (non rollover) */ 70*4882a593Smuzhiyun gd->arch.tbl += (now - gd->arch.lastinc); 71*4882a593Smuzhiyun else { 72*4882a593Smuzhiyun /* rollover */ 73*4882a593Smuzhiyun gd->arch.tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL) 74*4882a593Smuzhiyun - gd->arch.lastinc) + now; 75*4882a593Smuzhiyun } 76*4882a593Smuzhiyun gd->arch.lastinc = now; 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun return gd->arch.tbl; 79*4882a593Smuzhiyun } 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun /* delay x useconds */ __udelay(unsigned long usec)82*4882a593Smuzhiyunvoid __udelay(unsigned long usec) 83*4882a593Smuzhiyun { 84*4882a593Smuzhiyun long tmo = USEC_TO_COUNT(usec); 85*4882a593Smuzhiyun ulong now, last = read_timer(); 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun while (tmo > 0) { 88*4882a593Smuzhiyun now = read_timer(); 89*4882a593Smuzhiyun if (now > last) /* normal (non rollover) */ 90*4882a593Smuzhiyun tmo -= now - last; 91*4882a593Smuzhiyun else /* rollover */ 92*4882a593Smuzhiyun tmo -= TIMER_LOAD_VAL - last + now; 93*4882a593Smuzhiyun last = now; 94*4882a593Smuzhiyun } 95*4882a593Smuzhiyun } 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun /* 98*4882a593Smuzhiyun * This function is derived from PowerPC code (read timebase as long long). 99*4882a593Smuzhiyun * On ARM it just returns the timer value. 100*4882a593Smuzhiyun */ get_ticks(void)101*4882a593Smuzhiyununsigned long long get_ticks(void) 102*4882a593Smuzhiyun { 103*4882a593Smuzhiyun return get_timer(0); 104*4882a593Smuzhiyun } 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun /* 107*4882a593Smuzhiyun * This function is derived from PowerPC code (timebase clock frequency). 108*4882a593Smuzhiyun * On ARM it returns the number of timer ticks per second. 109*4882a593Smuzhiyun */ get_tbclk(void)110*4882a593Smuzhiyunulong get_tbclk(void) 111*4882a593Smuzhiyun { 112*4882a593Smuzhiyun return CONFIG_SYS_HZ; 113*4882a593Smuzhiyun } 114