16260fb04SPeter Tyser /* 26260fb04SPeter Tyser * (C) Copyright 2007 Michal Simek 36260fb04SPeter Tyser * 46260fb04SPeter Tyser * Michal SIMEK <monstr@monstr.eu> 56260fb04SPeter Tyser * 66260fb04SPeter Tyser * See file CREDITS for list of people who contributed to this 76260fb04SPeter Tyser * project. 86260fb04SPeter Tyser * 96260fb04SPeter Tyser * This program is free software; you can redistribute it and/or 106260fb04SPeter Tyser * modify it under the terms of the GNU General Public License as 116260fb04SPeter Tyser * published by the Free Software Foundation; either version 2 of 126260fb04SPeter Tyser * the License, or (at your option) any later version. 136260fb04SPeter Tyser * 146260fb04SPeter Tyser * This program is distributed in the hope that it will be useful, 156260fb04SPeter Tyser * but WITHOUT ANY WARRANTY; without even the implied warranty of 166260fb04SPeter Tyser * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 176260fb04SPeter Tyser * GNU General Public License for more details. 186260fb04SPeter Tyser * 196260fb04SPeter Tyser * You should have received a copy of the GNU General Public License 206260fb04SPeter Tyser * along with this program; if not, write to the Free Software 216260fb04SPeter Tyser * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 226260fb04SPeter Tyser * MA 02111-1307 USA 236260fb04SPeter Tyser */ 246260fb04SPeter Tyser 256260fb04SPeter Tyser #include <common.h> 266260fb04SPeter Tyser #include <asm/microblaze_timer.h> 276260fb04SPeter Tyser #include <asm/microblaze_intc.h> 286260fb04SPeter Tyser 296260fb04SPeter Tyser volatile int timestamp = 0; 30*bcbb046bSMichal Simek microblaze_timer_t *tmr; 316260fb04SPeter Tyser 326260fb04SPeter Tyser ulong get_timer (ulong base) 336260fb04SPeter Tyser { 34*bcbb046bSMichal Simek if (tmr) 35*bcbb046bSMichal Simek return timestamp - base; 36*bcbb046bSMichal Simek return timestamp++ - base; 376260fb04SPeter Tyser } 386260fb04SPeter Tyser 39779bf42cSMichal Simek void __udelay(unsigned long usec) 40779bf42cSMichal Simek { 41*bcbb046bSMichal Simek u32 i; 42779bf42cSMichal Simek 43*bcbb046bSMichal Simek if (tmr) { 44779bf42cSMichal Simek i = get_timer(0); 45779bf42cSMichal Simek while ((get_timer(0) - i) < (usec / 1000)) 46779bf42cSMichal Simek ; 47*bcbb046bSMichal Simek } else { 48*bcbb046bSMichal Simek for (i = 0; i < (usec * XILINX_CLOCK_FREQ / 10000000); i++) 49779bf42cSMichal Simek ; 50779bf42cSMichal Simek } 51*bcbb046bSMichal Simek } 52779bf42cSMichal Simek 53*bcbb046bSMichal Simek static void timer_isr(void *arg) 546260fb04SPeter Tyser { 556260fb04SPeter Tyser timestamp++; 566260fb04SPeter Tyser tmr->control = tmr->control | TIMER_INTERRUPT; 576260fb04SPeter Tyser } 586260fb04SPeter Tyser 595bbcb6cfSMichal Simek int timer_init (void) 606260fb04SPeter Tyser { 61*bcbb046bSMichal Simek int irq = -1; 62*bcbb046bSMichal Simek u32 preload = 0; 63*bcbb046bSMichal Simek u32 ret = 0; 64*bcbb046bSMichal Simek 65*bcbb046bSMichal Simek #if defined(CONFIG_SYS_TIMER_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM) 66*bcbb046bSMichal Simek preload = XILINX_CLOCK_FREQ / CONFIG_SYS_HZ; 67*bcbb046bSMichal Simek irq = CONFIG_SYS_TIMER_0_IRQ; 68*bcbb046bSMichal Simek tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR); 69*bcbb046bSMichal Simek #endif 70*bcbb046bSMichal Simek 71*bcbb046bSMichal Simek if (tmr && preload && irq >= 0) { 72*bcbb046bSMichal Simek tmr->loadreg = preload; 736260fb04SPeter Tyser tmr->control = TIMER_INTERRUPT | TIMER_RESET; 74*bcbb046bSMichal Simek tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\ 75*bcbb046bSMichal Simek TIMER_RELOAD | TIMER_DOWN_COUNT; 764769be21SGraeme Russ timestamp = 0; 77*bcbb046bSMichal Simek ret = install_interrupt_handler (irq, timer_isr, (void *)tmr); 78*bcbb046bSMichal Simek if (ret) 79*bcbb046bSMichal Simek tmr = NULL; 80*bcbb046bSMichal Simek } 81*bcbb046bSMichal Simek 82*bcbb046bSMichal Simek /* No problem if timer is not found/initialized */ 835bbcb6cfSMichal Simek return 0; 846260fb04SPeter Tyser } 85b9f0b730SStephan Linz 86b9f0b730SStephan Linz /* 87b9f0b730SStephan Linz * This function is derived from PowerPC code (read timebase as long long). 88b9f0b730SStephan Linz * On Microblaze it just returns the timer value. 89b9f0b730SStephan Linz */ 90b9f0b730SStephan Linz unsigned long long get_ticks(void) 91b9f0b730SStephan Linz { 92b9f0b730SStephan Linz return get_timer(0); 93b9f0b730SStephan Linz } 94b9f0b730SStephan Linz 95b9f0b730SStephan Linz /* 96b9f0b730SStephan Linz * This function is derived from PowerPC code (timebase clock frequency). 97b9f0b730SStephan Linz * On Microblaze it returns the number of timer ticks per second. 98b9f0b730SStephan Linz */ 99b9f0b730SStephan Linz ulong get_tbclk(void) 100b9f0b730SStephan Linz { 101b9f0b730SStephan Linz return CONFIG_SYS_HZ; 102b9f0b730SStephan Linz } 103