xref: /openbmc/u-boot/arch/microblaze/cpu/timer.c (revision 779bf42c2a278b2a2f8a4d9e4869c237d0a8742a)
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;
306260fb04SPeter Tyser 
316260fb04SPeter Tyser #ifdef CONFIG_SYS_TIMER_0
326260fb04SPeter Tyser ulong get_timer (ulong base)
336260fb04SPeter Tyser {
346260fb04SPeter Tyser 	return (timestamp - base);
356260fb04SPeter Tyser }
366260fb04SPeter Tyser #else
376260fb04SPeter Tyser ulong get_timer (ulong base)
386260fb04SPeter Tyser {
396260fb04SPeter Tyser 	return (timestamp++ - base);
406260fb04SPeter Tyser }
416260fb04SPeter Tyser #endif
426260fb04SPeter Tyser 
436260fb04SPeter Tyser #ifdef CONFIG_SYS_INTC_0
446260fb04SPeter Tyser #ifdef CONFIG_SYS_TIMER_0
45*779bf42cSMichal Simek void __udelay(unsigned long usec)
46*779bf42cSMichal Simek {
47*779bf42cSMichal Simek 	int i;
48*779bf42cSMichal Simek 
49*779bf42cSMichal Simek 	i = get_timer(0);
50*779bf42cSMichal Simek 	while ((get_timer(0) - i) < (usec / 1000))
51*779bf42cSMichal Simek 		;
52*779bf42cSMichal Simek }
53*779bf42cSMichal Simek #else
54*779bf42cSMichal Simek void __udelay(unsigned long usec)
55*779bf42cSMichal Simek {
56*779bf42cSMichal Simek 	unsigned int i;
57*779bf42cSMichal Simek 
58*779bf42cSMichal Simek 	for (i = 0; i < (usec * CONFIG_XILINX_CLOCK_FREQ / 10000000); i++)
59*779bf42cSMichal Simek 		;
60*779bf42cSMichal Simek }
61*779bf42cSMichal Simek #endif
62*779bf42cSMichal Simek 
63*779bf42cSMichal Simek #ifdef CONFIG_SYS_TIMER_0
646260fb04SPeter Tyser microblaze_timer_t *tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
656260fb04SPeter Tyser 
666260fb04SPeter Tyser void timer_isr (void *arg)
676260fb04SPeter Tyser {
686260fb04SPeter Tyser 	timestamp++;
696260fb04SPeter Tyser 	tmr->control = tmr->control | TIMER_INTERRUPT;
706260fb04SPeter Tyser }
716260fb04SPeter Tyser 
725bbcb6cfSMichal Simek int timer_init (void)
736260fb04SPeter Tyser {
746260fb04SPeter Tyser 	tmr->loadreg = CONFIG_SYS_TIMER_0_PRELOAD;
756260fb04SPeter Tyser 	tmr->control = TIMER_INTERRUPT | TIMER_RESET;
766260fb04SPeter Tyser 	tmr->control =
776260fb04SPeter Tyser 	    TIMER_ENABLE | TIMER_ENABLE_INTR | TIMER_RELOAD | TIMER_DOWN_COUNT;
784769be21SGraeme Russ 	timestamp = 0;
796260fb04SPeter Tyser 	install_interrupt_handler (CONFIG_SYS_TIMER_0_IRQ, timer_isr, (void *)tmr);
805bbcb6cfSMichal Simek 	return 0;
816260fb04SPeter Tyser }
826260fb04SPeter Tyser #endif
836260fb04SPeter Tyser #endif
84b9f0b730SStephan Linz 
85b9f0b730SStephan Linz /*
86b9f0b730SStephan Linz  * This function is derived from PowerPC code (read timebase as long long).
87b9f0b730SStephan Linz  * On Microblaze it just returns the timer value.
88b9f0b730SStephan Linz  */
89b9f0b730SStephan Linz unsigned long long get_ticks(void)
90b9f0b730SStephan Linz {
91b9f0b730SStephan Linz 	return get_timer(0);
92b9f0b730SStephan Linz }
93b9f0b730SStephan Linz 
94b9f0b730SStephan Linz /*
95b9f0b730SStephan Linz  * This function is derived from PowerPC code (timebase clock frequency).
96b9f0b730SStephan Linz  * On Microblaze it returns the number of timer ticks per second.
97b9f0b730SStephan Linz  */
98b9f0b730SStephan Linz ulong get_tbclk(void)
99b9f0b730SStephan Linz {
100b9f0b730SStephan Linz 	return CONFIG_SYS_HZ;
101b9f0b730SStephan Linz }
102