Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Marvell International Ltd. and its affiliates |
| 3 | * Written-by: Prafulla Wadaskar <prafulla@marvell.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., 51 Franklin Street, Fifth Floor, Boston, |
| 21 | * MA 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Lei Wen | 298ae91 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 25 | #include <asm/io.h> |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 26 | #include <asm/arch/kirkwood.h> |
| 27 | |
| 28 | #define UBOOT_CNTR 0 /* counter to use for uboot timer */ |
| 29 | |
| 30 | /* Timer reload and current value registers */ |
| 31 | struct kwtmr_val { |
| 32 | u32 reload; /* Timer reload reg */ |
| 33 | u32 val; /* Timer value reg */ |
| 34 | }; |
| 35 | |
| 36 | /* Timer registers */ |
| 37 | struct kwtmr_registers { |
| 38 | u32 ctrl; /* Timer control reg */ |
| 39 | u32 pad[3]; |
| 40 | struct kwtmr_val tmr[2]; |
| 41 | u32 wdt_reload; |
| 42 | u32 wdt_val; |
| 43 | }; |
| 44 | |
| 45 | struct kwtmr_registers *kwtmr_regs = (struct kwtmr_registers *)KW_TIMER_BASE; |
| 46 | |
| 47 | /* |
| 48 | * ARM Timers Registers Map |
| 49 | */ |
| 50 | #define CNTMR_CTRL_REG &kwtmr_regs->ctrl |
| 51 | #define CNTMR_RELOAD_REG(tmrnum) &kwtmr_regs->tmr[tmrnum].reload |
| 52 | #define CNTMR_VAL_REG(tmrnum) &kwtmr_regs->tmr[tmrnum].val |
| 53 | |
| 54 | /* |
| 55 | * ARM Timers Control Register |
| 56 | * CPU_TIMERS_CTRL_REG (CTCR) |
| 57 | */ |
| 58 | #define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2) |
| 59 | #define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS) |
| 60 | #define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr)) |
| 61 | #define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr)) |
| 62 | |
| 63 | #define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1) |
| 64 | #define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1) |
| 65 | #define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr)) |
| 66 | #define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr)) |
| 67 | |
| 68 | /* |
| 69 | * ARM Timer\Watchdog Reload Register |
| 70 | * CNTMR_RELOAD_REG (TRR) |
| 71 | */ |
| 72 | #define TRG_ARM_TIMER_REL_OFFS 0 |
| 73 | #define TRG_ARM_TIMER_REL_MASK 0xffffffff |
| 74 | |
| 75 | /* |
| 76 | * ARM Timer\Watchdog Register |
| 77 | * CNTMR_VAL_REG (TVRG) |
| 78 | */ |
| 79 | #define TVR_ARM_TIMER_OFFS 0 |
| 80 | #define TVR_ARM_TIMER_MASK 0xffffffff |
| 81 | #define TVR_ARM_TIMER_MAX 0xffffffff |
| 82 | #define TIMER_LOAD_VAL 0xffffffff |
| 83 | |
| 84 | #define READ_TIMER (readl(CNTMR_VAL_REG(UBOOT_CNTR)) / \ |
| 85 | (CONFIG_SYS_TCLK / 1000)) |
| 86 | |
Heiko Schocher | 5504dab | 2011-01-20 22:56:39 +0000 | [diff] [blame] | 87 | DECLARE_GLOBAL_DATA_PTR; |
| 88 | |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 89 | #define timestamp gd->arch.tbl |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 90 | #define lastdec gd->arch.lastinc |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 91 | |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 92 | ulong get_timer_masked(void) |
| 93 | { |
| 94 | ulong now = READ_TIMER; |
| 95 | |
| 96 | if (lastdec >= now) { |
| 97 | /* normal mode */ |
| 98 | timestamp += lastdec - now; |
| 99 | } else { |
| 100 | /* we have an overflow ... */ |
| 101 | timestamp += lastdec + |
| 102 | (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now; |
| 103 | } |
| 104 | lastdec = now; |
| 105 | |
| 106 | return timestamp; |
| 107 | } |
| 108 | |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 109 | ulong get_timer(ulong base) |
| 110 | { |
| 111 | return get_timer_masked() - base; |
| 112 | } |
| 113 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 114 | void __udelay(unsigned long usec) |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 115 | { |
| 116 | uint current; |
| 117 | ulong delayticks; |
| 118 | |
| 119 | current = readl(CNTMR_VAL_REG(UBOOT_CNTR)); |
| 120 | delayticks = (usec * (CONFIG_SYS_TCLK / 1000000)); |
| 121 | |
| 122 | if (current < delayticks) { |
| 123 | delayticks -= current; |
| 124 | while (readl(CNTMR_VAL_REG(UBOOT_CNTR)) < current) ; |
| 125 | while ((TIMER_LOAD_VAL - delayticks) < |
| 126 | readl(CNTMR_VAL_REG(UBOOT_CNTR))) ; |
| 127 | } else { |
| 128 | while (readl(CNTMR_VAL_REG(UBOOT_CNTR)) > |
| 129 | (current - delayticks)) ; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * init the counter |
| 135 | */ |
| 136 | int timer_init(void) |
| 137 | { |
| 138 | unsigned int cntmrctrl; |
| 139 | |
| 140 | /* load value into timer */ |
| 141 | writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR)); |
| 142 | writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR)); |
| 143 | |
| 144 | /* enable timer in auto reload mode */ |
| 145 | cntmrctrl = readl(CNTMR_CTRL_REG); |
| 146 | cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR); |
| 147 | cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR); |
| 148 | writel(cntmrctrl, CNTMR_CTRL_REG); |
| 149 | |
| 150 | /* init the timestamp and lastdec value */ |
Graeme Russ | 944a7fe | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 151 | lastdec = READ_TIMER; |
| 152 | timestamp = 0; |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 153 | |
| 154 | return 0; |
| 155 | } |
Prafulla Wadaskar | f4ce6ad | 2012-02-08 14:15:53 +0530 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * This function is derived from PowerPC code (read timebase as long long). |
| 159 | * On ARM it just returns the timer value. |
| 160 | */ |
| 161 | unsigned long long get_ticks(void) |
| 162 | { |
| 163 | return get_timer(0); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * This function is derived from PowerPC code (timebase clock frequency). |
| 168 | * On ARM it returns the number of timer ticks per second. |
| 169 | */ |
| 170 | ulong get_tbclk (void) |
| 171 | { |
| 172 | return (ulong)CONFIG_SYS_HZ; |
| 173 | } |