blob: 25dd2aed56f5f028be4e78df46f4c897dbb5a0be [file] [log] [blame]
TsiChungLiew8999e6b2008-01-15 13:37:34 -06001/*
Alison Wang027f76f2012-03-26 21:49:07 +00002 * (C) Copyright 2007, 2012 Freescale Semiconductor, Inc.
TsiChungLiew8999e6b2008-01-15 13:37:34 -06003 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25
26#include <asm/timer.h>
27#include <asm/immap.h>
Alison Wang027f76f2012-03-26 21:49:07 +000028#include <asm/io.h>
TsiChungLiew8999e6b2008-01-15 13:37:34 -060029
30DECLARE_GLOBAL_DATA_PTR;
31
32static ulong timestamp;
33
34#if defined(CONFIG_SLTTMR)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020035#ifndef CONFIG_SYS_UDELAY_BASE
TsiChungLiew8999e6b2008-01-15 13:37:34 -060036# error "uDelay base not defined!"
37#endif
38
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020039#if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
TsiChungLiew8999e6b2008-01-15 13:37:34 -060040# error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
41#endif
42extern void dtimer_intr_setup(void);
43
Ingo van Lilf0f778a2009-11-24 14:09:21 +010044void __udelay(unsigned long usec)
TsiChungLiew8999e6b2008-01-15 13:37:34 -060045{
Alison Wang027f76f2012-03-26 21:49:07 +000046 slt_t *timerp = (slt_t *) (CONFIG_SYS_UDELAY_BASE);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060047 u32 now, freq;
48
49 /* 1 us period */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020050 freq = CONFIG_SYS_TIMER_PRESCALER;
TsiChungLiew8999e6b2008-01-15 13:37:34 -060051
Alison Wang027f76f2012-03-26 21:49:07 +000052 /* Disable */
53 out_be32(&timerp->cr, 0);
54 out_be32(&timerp->tcnt, usec * freq);
55 out_be32(&timerp->cr, SLT_CR_TEN);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060056
Alison Wang027f76f2012-03-26 21:49:07 +000057 now = in_be32(&timerp->cnt);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060058 while (now != 0)
Alison Wang027f76f2012-03-26 21:49:07 +000059 now = in_be32(&timerp->cnt);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060060
Alison Wang027f76f2012-03-26 21:49:07 +000061 setbits_be32(&timerp->sr, SLT_SR_ST);
62 out_be32(&timerp->cr, 0);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060063}
64
65void dtimer_interrupt(void *not_used)
66{
Alison Wang027f76f2012-03-26 21:49:07 +000067 slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060068
69 /* check for timer interrupt asserted */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070 if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
Alison Wang027f76f2012-03-26 21:49:07 +000071 setbits_be32(&timerp->sr, SLT_SR_ST);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060072 timestamp++;
73 return;
74 }
75}
76
Jason Jin1dd491e2011-08-19 10:02:32 +080077int timer_init(void)
TsiChungLiew8999e6b2008-01-15 13:37:34 -060078{
Alison Wang027f76f2012-03-26 21:49:07 +000079 slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060080
81 timestamp = 0;
82
Alison Wang027f76f2012-03-26 21:49:07 +000083 /* disable timer */
84 out_be32(&timerp->cr, 0);
85 out_be32(&timerp->tcnt, 0);
86 /* clear status */
87 out_be32(&timerp->sr, SLT_SR_BE | SLT_SR_ST);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060088
89 /* initialize and enable timer interrupt */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020090 irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060091
92 /* Interrupt every ms */
Alison Wang027f76f2012-03-26 21:49:07 +000093 out_be32(&timerp->tcnt, 1000 * CONFIG_SYS_TIMER_PRESCALER);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060094
95 dtimer_intr_setup();
96
97 /* set a period of 1us, set timer mode to restart and
98 enable timer and interrupt */
Alison Wang027f76f2012-03-26 21:49:07 +000099 out_be32(&timerp->cr, SLT_CR_RUN | SLT_CR_IEN | SLT_CR_TEN);
Jason Jin1dd491e2011-08-19 10:02:32 +0800100 return 0;
TsiChungLiew8999e6b2008-01-15 13:37:34 -0600101}
102
TsiChungLiew8999e6b2008-01-15 13:37:34 -0600103ulong get_timer(ulong base)
104{
105 return (timestamp - base);
106}
107
TsiChungLiew8999e6b2008-01-15 13:37:34 -0600108#endif /* CONFIG_SLTTMR */