blob: a4b88f3815c096aa1db369918f02585c1d0a3e31 [file] [log] [blame]
John Rigbybe72e0c2011-04-19 10:42:40 +00001/*
2 * Copyright (C) 2010 Linaro Limited
3 * John Rigby <john.rigby@linaro.org>
4 *
5 * Based on original from Linux kernel source and
6 * internal ST-Ericsson U-Boot source.
7 * (C) Copyright 2009 Alessandro Rubini
8 * (C) Copyright 2010 ST-Ericsson
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29#include <common.h>
30#include <asm/io.h>
31#include <asm/arch/hardware.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
35/*
36 * The MTU device has some interrupt control registers
37 * followed by 4 timers.
38 */
39
40/* The timers */
41struct u8500_mtu_timer {
42 u32 lr; /* Load value */
43 u32 cv; /* Current value */
44 u32 cr; /* Control reg */
45 u32 bglr; /* ??? */
46};
47
48/* The MTU that contains the timers */
49struct u8500_mtu {
50 u32 imsc; /* Interrupt mask set/clear */
51 u32 ris; /* Raw interrupt status */
52 u32 mis; /* Masked interrupt status */
53 u32 icr; /* Interrupt clear register */
54 struct u8500_mtu_timer pt[4];
55};
56
57/* bits for the control register */
58#define MTU_CR_ONESHOT 0x01 /* if 0 = wraps reloading from BGLR */
59#define MTU_CR_32BITS 0x02
60
61#define MTU_CR_PRESCALE_1 0x00
62#define MTU_CR_PRESCALE_16 0x04
63#define MTU_CR_PRESCALE_256 0x08
64#define MTU_CR_PRESCALE_MASK 0x0c
65
66#define MTU_CR_PERIODIC 0x40 /* if 0 = free-running */
67#define MTU_CR_ENA 0x80
68
69/*
70 * The MTU is clocked at 133 MHz by default. (V1 and later)
71 */
72#define TIMER_CLOCK (133 * 1000 * 1000 / 16)
73#define COUNT_TO_USEC(x) ((x) * 16 / 133)
74#define USEC_TO_COUNT(x) ((x) * 133 / 16)
75#define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ)
76#define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ)
77#define TIMER_LOAD_VAL 0xffffffff
78
79/*
80 * MTU timer to use (from 0 to 3).
81 */
82#define MTU_TIMER 2
83
84static struct u8500_mtu_timer *timer_base =
85 &((struct u8500_mtu *)U8500_MTU0_BASE_V1)->pt[MTU_TIMER];
86
87/* macro to read the 32 bit timer: since it decrements, we invert read value */
88#define READ_TIMER() (~readl(&timer_base->cv))
89
90/* Configure a free-running, auto-wrap counter with /16 prescaler */
91int timer_init(void)
92{
93 writel(MTU_CR_ENA | MTU_CR_PRESCALE_16 | MTU_CR_32BITS,
94 &timer_base->cr);
95 return 0;
96}
97
98ulong get_timer_masked(void)
99{
100 /* current tick value */
101 ulong now = TICKS_TO_HZ(READ_TIMER());
102
Simon Glass582601d2012-12-13 20:48:35 +0000103 if (now >= gd->arch.lastinc) { /* normal (non rollover) */
104 gd->arch.tbl += (now - gd->arch.lastinc);
Simon Glass66ee6922012-12-13 20:48:34 +0000105 } else { /* rollover */
Simon Glass582601d2012-12-13 20:48:35 +0000106 gd->arch.tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL) -
107 gd->arch.lastinc) + now;
Simon Glass66ee6922012-12-13 20:48:34 +0000108 }
Simon Glass582601d2012-12-13 20:48:35 +0000109 gd->arch.lastinc = now;
Simon Glass66ee6922012-12-13 20:48:34 +0000110 return gd->arch.tbl;
John Rigbybe72e0c2011-04-19 10:42:40 +0000111}
112
113/* Delay x useconds */
114void __udelay(ulong usec)
115{
116 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
117 ulong now, last = READ_TIMER();
118
119 while (tmo > 0) {
120 now = READ_TIMER();
121 if (now > last) /* normal (non rollover) */
122 tmo -= now - last;
123 else /* rollover */
124 tmo -= TIMER_LOAD_VAL - last + now;
125 last = now;
126 }
127}
128
129ulong get_timer(ulong base)
130{
131 return get_timer_masked() - base;
132}
133
John Rigbybe72e0c2011-04-19 10:42:40 +0000134/*
135 * Emulation of Power architecture long long timebase.
136 *
Simon Glass8ff43b02012-12-13 20:48:33 +0000137 * TODO: Support gd->arch.tbu for real long long timebase.
John Rigbybe72e0c2011-04-19 10:42:40 +0000138 */
139unsigned long long get_ticks(void)
140{
141 return get_timer(0);
142}
143
144/*
145 * Emulation of Power architecture timebase.
146 * NB: Low resolution compared to Power tbclk.
147 */
148ulong get_tbclk(void)
149{
150 return CONFIG_SYS_HZ;
151}