blob: 544bfd20831481b5d9bc17808d5750941edf2ec4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChungLiew8999e6b2008-01-15 13:37:34 -06002/*
Alison Wang027f76f2012-03-26 21:49:07 +00003 * (C) Copyright 2007, 2012 Freescale Semiconductor, Inc.
TsiChungLiew8999e6b2008-01-15 13:37:34 -06004 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChungLiew8999e6b2008-01-15 13:37:34 -06005 */
6
7#include <common.h>
8
9#include <asm/timer.h>
10#include <asm/immap.h>
Alison Wang027f76f2012-03-26 21:49:07 +000011#include <asm/io.h>
TsiChungLiew8999e6b2008-01-15 13:37:34 -060012
13DECLARE_GLOBAL_DATA_PTR;
14
15static ulong timestamp;
16
17#if defined(CONFIG_SLTTMR)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020018#ifndef CONFIG_SYS_UDELAY_BASE
TsiChungLiew8999e6b2008-01-15 13:37:34 -060019# error "uDelay base not defined!"
20#endif
21
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020022#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 -060023# error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
24#endif
25extern void dtimer_intr_setup(void);
26
Ingo van Lilf0f778a2009-11-24 14:09:21 +010027void __udelay(unsigned long usec)
TsiChungLiew8999e6b2008-01-15 13:37:34 -060028{
Alison Wang027f76f2012-03-26 21:49:07 +000029 slt_t *timerp = (slt_t *) (CONFIG_SYS_UDELAY_BASE);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060030 u32 now, freq;
31
32 /* 1 us period */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020033 freq = CONFIG_SYS_TIMER_PRESCALER;
TsiChungLiew8999e6b2008-01-15 13:37:34 -060034
Alison Wang027f76f2012-03-26 21:49:07 +000035 /* Disable */
36 out_be32(&timerp->cr, 0);
37 out_be32(&timerp->tcnt, usec * freq);
38 out_be32(&timerp->cr, SLT_CR_TEN);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060039
Alison Wang027f76f2012-03-26 21:49:07 +000040 now = in_be32(&timerp->cnt);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060041 while (now != 0)
Alison Wang027f76f2012-03-26 21:49:07 +000042 now = in_be32(&timerp->cnt);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060043
Alison Wang027f76f2012-03-26 21:49:07 +000044 setbits_be32(&timerp->sr, SLT_SR_ST);
45 out_be32(&timerp->cr, 0);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060046}
47
48void dtimer_interrupt(void *not_used)
49{
Alison Wang027f76f2012-03-26 21:49:07 +000050 slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060051
52 /* check for timer interrupt asserted */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020053 if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
Alison Wang027f76f2012-03-26 21:49:07 +000054 setbits_be32(&timerp->sr, SLT_SR_ST);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060055 timestamp++;
56 return;
57 }
58}
59
Jason Jin1dd491e2011-08-19 10:02:32 +080060int timer_init(void)
TsiChungLiew8999e6b2008-01-15 13:37:34 -060061{
Alison Wang027f76f2012-03-26 21:49:07 +000062 slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060063
64 timestamp = 0;
65
Alison Wang027f76f2012-03-26 21:49:07 +000066 /* disable timer */
67 out_be32(&timerp->cr, 0);
68 out_be32(&timerp->tcnt, 0);
69 /* clear status */
70 out_be32(&timerp->sr, SLT_SR_BE | SLT_SR_ST);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060071
72 /* initialize and enable timer interrupt */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020073 irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060074
75 /* Interrupt every ms */
Alison Wang027f76f2012-03-26 21:49:07 +000076 out_be32(&timerp->tcnt, 1000 * CONFIG_SYS_TIMER_PRESCALER);
TsiChungLiew8999e6b2008-01-15 13:37:34 -060077
78 dtimer_intr_setup();
79
80 /* set a period of 1us, set timer mode to restart and
81 enable timer and interrupt */
Alison Wang027f76f2012-03-26 21:49:07 +000082 out_be32(&timerp->cr, SLT_CR_RUN | SLT_CR_IEN | SLT_CR_TEN);
Jason Jin1dd491e2011-08-19 10:02:32 +080083 return 0;
TsiChungLiew8999e6b2008-01-15 13:37:34 -060084}
85
TsiChungLiew8999e6b2008-01-15 13:37:34 -060086ulong get_timer(ulong base)
87{
88 return (timestamp - base);
89}
90
TsiChungLiew8999e6b2008-01-15 13:37:34 -060091#endif /* CONFIG_SLTTMR */