blob: 42b6076c1f9154e886281f35022acd3ca37fc389 [file] [log] [blame]
John Rigby9c146032010-01-25 23:12:56 -07001/*
2 * (C) Copyright 2002
3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Alex Zuepke <azu@sysgo.de>
9 *
10 * (C) Copyright 2002
11 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
12 *
13 * (C) Copyright 2009
14 * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
15 *
16 * (C) Copyright 2009 DENX Software Engineering
17 * Author: John Rigby <jrigby@gmail.com>
Fabio Estevamf231efb2011-10-13 05:34:59 +000018 * Add support for MX25
John Rigby9c146032010-01-25 23:12:56 -070019 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020020 * SPDX-License-Identifier: GPL-2.0+
John Rigby9c146032010-01-25 23:12:56 -070021 */
22
23#include <common.h>
24#include <div64.h>
25#include <asm/io.h>
26#include <asm/arch/imx-regs.h>
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000027#include <asm/arch/clock.h>
John Rigby9c146032010-01-25 23:12:56 -070028
Heiko Schocher5504dab2011-01-20 22:56:39 +000029DECLARE_GLOBAL_DATA_PTR;
30
Simon Glass2655ee12012-12-13 20:48:34 +000031#define timestamp (gd->arch.tbl)
Simon Glassa848da52012-12-13 20:48:35 +000032#define lastinc (gd->arch.lastinc)
John Rigby9c146032010-01-25 23:12:56 -070033
34/*
35 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
36 * "tick" is internal timer period
37 */
38#ifdef CONFIG_MX25_TIMER_HIGH_PRECISION
39/* ~0.4% error - measured with stop-watch on 100s boot-delay */
40static inline unsigned long long tick_to_time(unsigned long long tick)
41{
42 tick *= CONFIG_SYS_HZ;
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000043 do_div(tick, MXC_CLK32);
John Rigby9c146032010-01-25 23:12:56 -070044 return tick;
45}
46
47static inline unsigned long long time_to_tick(unsigned long long time)
48{
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000049 time *= MXC_CLK32;
John Rigby9c146032010-01-25 23:12:56 -070050 do_div(time, CONFIG_SYS_HZ);
51 return time;
52}
53
54static inline unsigned long long us_to_tick(unsigned long long us)
55{
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000056 us = us * MXC_CLK32 + 999999;
John Rigby9c146032010-01-25 23:12:56 -070057 do_div(us, 1000000);
58 return us;
59}
60#else
61/* ~2% error */
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000062#define TICK_PER_TIME ((MXC_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
63#define US_PER_TICK (1000000 / MXC_CLK32)
John Rigby9c146032010-01-25 23:12:56 -070064
65static inline unsigned long long tick_to_time(unsigned long long tick)
66{
67 do_div(tick, TICK_PER_TIME);
68 return tick;
69}
70
71static inline unsigned long long time_to_tick(unsigned long long time)
72{
73 return time * TICK_PER_TIME;
74}
75
76static inline unsigned long long us_to_tick(unsigned long long us)
77{
78 us += US_PER_TICK - 1;
79 do_div(us, US_PER_TICK);
80 return us;
81}
82#endif
83
84/* nothing really to do with interrupts, just starts up a counter. */
85/* The 32KHz 32-bit timer overruns in 134217 seconds */
86int timer_init(void)
87{
88 int i;
89 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
90 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
91
92 /* setup GP Timer 1 */
93 writel(GPT_CTRL_SWR, &gpt->ctrl);
94
95 writel(readl(&ccm->cgr1) | CCM_CGR1_GPT1, &ccm->cgr1);
96
97 for (i = 0; i < 100; i++)
98 writel(0, &gpt->ctrl); /* We have no udelay by now */
99 writel(0, &gpt->pre); /* prescaler = 1 */
100 /* Freerun Mode, 32KHz input */
101 writel(readl(&gpt->ctrl) | GPT_CTRL_CLKSOURCE_32 | GPT_CTRL_FRR,
102 &gpt->ctrl);
103 writel(readl(&gpt->ctrl) | GPT_CTRL_TEN, &gpt->ctrl);
104
105 return 0;
106}
107
Fabio Estevamf231efb2011-10-13 05:34:59 +0000108unsigned long long get_ticks(void)
John Rigby9c146032010-01-25 23:12:56 -0700109{
110 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE;
111 ulong now = readl(&gpt->counter); /* current tick value */
112
113 if (now >= lastinc) {
114 /*
115 * normal mode (non roll)
116 * move stamp forward with absolut diff ticks
117 */
118 timestamp += (now - lastinc);
119 } else {
120 /* we have rollover of incrementer */
121 timestamp += (0xFFFFFFFF - lastinc) + now;
122 }
123 lastinc = now;
124 return timestamp;
125}
126
Fabio Estevamf231efb2011-10-13 05:34:59 +0000127ulong get_timer_masked(void)
John Rigby9c146032010-01-25 23:12:56 -0700128{
129 /*
130 * get_ticks() returns a long long (64 bit), it wraps in
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +0000131 * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
John Rigby9c146032010-01-25 23:12:56 -0700132 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
133 * 5 * 10^6 days - long enough.
134 */
135 return tick_to_time(get_ticks());
136}
137
Fabio Estevamf231efb2011-10-13 05:34:59 +0000138ulong get_timer(ulong base)
John Rigby9c146032010-01-25 23:12:56 -0700139{
Fabio Estevamf231efb2011-10-13 05:34:59 +0000140 return get_timer_masked() - base;
John Rigby9c146032010-01-25 23:12:56 -0700141}
142
John Rigby9c146032010-01-25 23:12:56 -0700143/* delay x useconds AND preserve advance timstamp value */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000144void __udelay(unsigned long usec)
John Rigby9c146032010-01-25 23:12:56 -0700145{
146 unsigned long long tmp;
147 ulong tmo;
148
149 tmo = us_to_tick(usec);
150 tmp = get_ticks() + tmo; /* get current timestamp */
151
152 while (get_ticks() < tmp) /* loop till event */
153 /*NOP*/;
154}
Matthias Weisser5e0e67d2011-07-06 00:28:28 +0000155
156/*
157 * This function is derived from PowerPC code (timebase clock frequency).
158 * On ARM it returns the number of timer ticks per second.
159 */
160ulong get_tbclk(void)
161{
162 ulong tbclk;
163
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +0000164 tbclk = MXC_CLK32;
Matthias Weisser5e0e67d2011-07-06 00:28:28 +0000165 return tbclk;
166}