Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 2 | /* |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 3 | * (C) Copyright 2007, 2012 Freescale Semiconductor, Inc. |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 4 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | 9b61c7c | 2019-11-14 12:57:41 -0700 | [diff] [blame] | 9 | #include <irq_func.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame^] | 10 | #include <linux/delay.h> |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 11 | |
| 12 | #include <asm/timer.h> |
| 13 | #include <asm/immap.h> |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 14 | #include <asm/io.h> |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | static ulong timestamp; |
| 19 | |
| 20 | #if defined(CONFIG_SLTTMR) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 21 | #ifndef CONFIG_SYS_UDELAY_BASE |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 22 | # error "uDelay base not defined!" |
| 23 | #endif |
| 24 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 25 | #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK) |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 26 | # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!" |
| 27 | #endif |
| 28 | extern void dtimer_intr_setup(void); |
| 29 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 30 | void __udelay(unsigned long usec) |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 31 | { |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 32 | slt_t *timerp = (slt_t *) (CONFIG_SYS_UDELAY_BASE); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 33 | u32 now, freq; |
| 34 | |
| 35 | /* 1 us period */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 36 | freq = CONFIG_SYS_TIMER_PRESCALER; |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 37 | |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 38 | /* Disable */ |
| 39 | out_be32(&timerp->cr, 0); |
| 40 | out_be32(&timerp->tcnt, usec * freq); |
| 41 | out_be32(&timerp->cr, SLT_CR_TEN); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 42 | |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 43 | now = in_be32(&timerp->cnt); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 44 | while (now != 0) |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 45 | now = in_be32(&timerp->cnt); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 46 | |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 47 | setbits_be32(&timerp->sr, SLT_SR_ST); |
| 48 | out_be32(&timerp->cr, 0); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void dtimer_interrupt(void *not_used) |
| 52 | { |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 53 | slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 54 | |
| 55 | /* check for timer interrupt asserted */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) { |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 57 | setbits_be32(&timerp->sr, SLT_SR_ST); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 58 | timestamp++; |
| 59 | return; |
| 60 | } |
| 61 | } |
| 62 | |
Jason Jin | 1dd491e | 2011-08-19 10:02:32 +0800 | [diff] [blame] | 63 | int timer_init(void) |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 64 | { |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 65 | slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 66 | |
| 67 | timestamp = 0; |
| 68 | |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 69 | /* disable timer */ |
| 70 | out_be32(&timerp->cr, 0); |
| 71 | out_be32(&timerp->tcnt, 0); |
| 72 | /* clear status */ |
| 73 | out_be32(&timerp->sr, SLT_SR_BE | SLT_SR_ST); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 74 | |
| 75 | /* initialize and enable timer interrupt */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 76 | irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 77 | |
| 78 | /* Interrupt every ms */ |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 79 | out_be32(&timerp->tcnt, 1000 * CONFIG_SYS_TIMER_PRESCALER); |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 80 | |
| 81 | dtimer_intr_setup(); |
| 82 | |
| 83 | /* set a period of 1us, set timer mode to restart and |
| 84 | enable timer and interrupt */ |
Alison Wang | 027f76f | 2012-03-26 21:49:07 +0000 | [diff] [blame] | 85 | out_be32(&timerp->cr, SLT_CR_RUN | SLT_CR_IEN | SLT_CR_TEN); |
Jason Jin | 1dd491e | 2011-08-19 10:02:32 +0800 | [diff] [blame] | 86 | return 0; |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 87 | } |
| 88 | |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 89 | ulong get_timer(ulong base) |
| 90 | { |
| 91 | return (timestamp - base); |
| 92 | } |
| 93 | |
TsiChungLiew | 8999e6b | 2008-01-15 13:37:34 -0600 | [diff] [blame] | 94 | #endif /* CONFIG_SLTTMR */ |