xref: /openbmc/u-boot/arch/microblaze/cpu/timer.c (revision b9f0b7305278feba7dc295e58c58f22cfcfe8009)
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
456260fb04SPeter Tyser microblaze_timer_t *tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
466260fb04SPeter Tyser 
476260fb04SPeter Tyser void timer_isr (void *arg)
486260fb04SPeter Tyser {
496260fb04SPeter Tyser 	timestamp++;
506260fb04SPeter Tyser 	tmr->control = tmr->control | TIMER_INTERRUPT;
516260fb04SPeter Tyser }
526260fb04SPeter Tyser 
535bbcb6cfSMichal Simek int timer_init (void)
546260fb04SPeter Tyser {
556260fb04SPeter Tyser 	tmr->loadreg = CONFIG_SYS_TIMER_0_PRELOAD;
566260fb04SPeter Tyser 	tmr->control = TIMER_INTERRUPT | TIMER_RESET;
576260fb04SPeter Tyser 	tmr->control =
586260fb04SPeter Tyser 	    TIMER_ENABLE | TIMER_ENABLE_INTR | TIMER_RELOAD | TIMER_DOWN_COUNT;
594769be21SGraeme Russ 	timestamp = 0;
606260fb04SPeter Tyser 	install_interrupt_handler (CONFIG_SYS_TIMER_0_IRQ, timer_isr, (void *)tmr);
615bbcb6cfSMichal Simek 	return 0;
626260fb04SPeter Tyser }
636260fb04SPeter Tyser #endif
646260fb04SPeter Tyser #endif
65*b9f0b730SStephan Linz 
66*b9f0b730SStephan Linz /*
67*b9f0b730SStephan Linz  * This function is derived from PowerPC code (read timebase as long long).
68*b9f0b730SStephan Linz  * On Microblaze it just returns the timer value.
69*b9f0b730SStephan Linz  */
70*b9f0b730SStephan Linz unsigned long long get_ticks(void)
71*b9f0b730SStephan Linz {
72*b9f0b730SStephan Linz 	return get_timer(0);
73*b9f0b730SStephan Linz }
74*b9f0b730SStephan Linz 
75*b9f0b730SStephan Linz /*
76*b9f0b730SStephan Linz  * This function is derived from PowerPC code (timebase clock frequency).
77*b9f0b730SStephan Linz  * On Microblaze it returns the number of timer ticks per second.
78*b9f0b730SStephan Linz  */
79*b9f0b730SStephan Linz ulong get_tbclk(void)
80*b9f0b730SStephan Linz {
81*b9f0b730SStephan Linz 	return CONFIG_SYS_HZ;
82*b9f0b730SStephan Linz }
83