blob: ab37d641ece97c82f230ad9e6a89511fea8fddff [file] [log] [blame]
Stefano Babic64fdf452010-01-20 18:19:32 +01001/*
2 * (C) Copyright 2007
3 * Sascha Hauer, Pengutronix
4 *
5 * (C) Copyright 2009 Freescale Semiconductor, Inc.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27#include <asm/io.h>
Stefano Babic782bb0d2012-02-06 12:52:36 +010028#include <div64.h>
Stefano Babic64fdf452010-01-20 18:19:32 +010029#include <asm/arch/imx-regs.h>
Benoît Thébaudeau833b6432012-09-27 10:19:58 +000030#include <asm/arch/clock.h>
Stefano Babic64fdf452010-01-20 18:19:32 +010031
32/* General purpose timers registers */
33struct mxc_gpt {
34 unsigned int control;
35 unsigned int prescaler;
36 unsigned int status;
37 unsigned int nouse[6];
38 unsigned int counter;
39};
40
41static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
42
43/* General purpose timers bitfields */
Jason Liu18936ee2011-11-25 00:18:01 +000044#define GPTCR_SWR (1 << 15) /* Software reset */
45#define GPTCR_FRR (1 << 9) /* Freerun / restart */
46#define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
47#define GPTCR_TEN 1 /* Timer enable */
Stefano Babic64fdf452010-01-20 18:19:32 +010048
Stefano Babicdb106ef2011-01-21 21:16:15 +010049DECLARE_GLOBAL_DATA_PTR;
50
Simon Glass66ee6922012-12-13 20:48:34 +000051#define timestamp (gd->arch.tbl)
Simon Glass582601d2012-12-13 20:48:35 +000052#define lastinc (gd->arch.lastinc)
Stefano Babic64fdf452010-01-20 18:19:32 +010053
Stefano Babic782bb0d2012-02-06 12:52:36 +010054static inline unsigned long long tick_to_time(unsigned long long tick)
55{
56 tick *= CONFIG_SYS_HZ;
Benoît Thébaudeau833b6432012-09-27 10:19:58 +000057 do_div(tick, MXC_CLK32);
Stefano Babic782bb0d2012-02-06 12:52:36 +010058
59 return tick;
60}
61
62static inline unsigned long long us_to_tick(unsigned long long usec)
63{
Benoît Thébaudeau833b6432012-09-27 10:19:58 +000064 usec = usec * MXC_CLK32 + 999999;
Stefano Babic782bb0d2012-02-06 12:52:36 +010065 do_div(usec, 1000000);
66
67 return usec;
68}
69
Stefano Babic64fdf452010-01-20 18:19:32 +010070int timer_init(void)
71{
72 int i;
Graeme Russ17659d72011-07-15 02:21:14 +000073 ulong val;
Stefano Babic64fdf452010-01-20 18:19:32 +010074
75 /* setup GP Timer 1 */
76 __raw_writel(GPTCR_SWR, &cur_gpt->control);
77
78 /* We have no udelay by now */
79 for (i = 0; i < 100; i++)
80 __raw_writel(0, &cur_gpt->control);
81
82 __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */
83
84 /* Freerun Mode, PERCLK1 input */
85 i = __raw_readl(&cur_gpt->control);
86 __raw_writel(i | GPTCR_CLKSOURCE_32 | GPTCR_TEN, &cur_gpt->control);
Stefano Babic64fdf452010-01-20 18:19:32 +010087
Graeme Russ17659d72011-07-15 02:21:14 +000088 val = __raw_readl(&cur_gpt->counter);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +000089 lastinc = val / (MXC_CLK32 / CONFIG_SYS_HZ);
Stefano Babic64fdf452010-01-20 18:19:32 +010090 timestamp = 0;
Graeme Russ17659d72011-07-15 02:21:14 +000091
92 return 0;
Stefano Babic64fdf452010-01-20 18:19:32 +010093}
94
Stefano Babic782bb0d2012-02-06 12:52:36 +010095unsigned long long get_ticks(void)
96{
97 ulong now = __raw_readl(&cur_gpt->counter); /* current tick value */
98
99 if (now >= lastinc) {
100 /*
101 * normal mode (non roll)
102 * move stamp forward with absolut diff ticks
103 */
104 timestamp += (now - lastinc);
105 } else {
106 /* we have rollover of incrementer */
107 timestamp += (0xFFFFFFFF - lastinc) + now;
108 }
109 lastinc = now;
110 return timestamp;
111}
112
Stefano Babic64fdf452010-01-20 18:19:32 +0100113ulong get_timer_masked(void)
114{
Stefano Babic782bb0d2012-02-06 12:52:36 +0100115 /*
116 * get_ticks() returns a long long (64 bit), it wraps in
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000117 * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
Stefano Babic782bb0d2012-02-06 12:52:36 +0100118 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
119 * 5 * 10^6 days - long enough.
120 */
121 return tick_to_time(get_ticks());
Stefano Babic64fdf452010-01-20 18:19:32 +0100122}
123
124ulong get_timer(ulong base)
125{
126 return get_timer_masked() - base;
127}
128
Stefano Babic782bb0d2012-02-06 12:52:36 +0100129/* delay x useconds AND preserve advance timstamp value */
Stefano Babic64fdf452010-01-20 18:19:32 +0100130void __udelay(unsigned long usec)
131{
Stefano Babic782bb0d2012-02-06 12:52:36 +0100132 unsigned long long tmp;
133 ulong tmo;
Stefano Babic64fdf452010-01-20 18:19:32 +0100134
Stefano Babic782bb0d2012-02-06 12:52:36 +0100135 tmo = us_to_tick(usec);
136 tmp = get_ticks() + tmo; /* get current timestamp */
Stefano Babic64fdf452010-01-20 18:19:32 +0100137
Stefano Babic782bb0d2012-02-06 12:52:36 +0100138 while (get_ticks() < tmp) /* loop till event */
139 /*NOP*/;
140}
Stefano Babic64fdf452010-01-20 18:19:32 +0100141
Stefano Babic782bb0d2012-02-06 12:52:36 +0100142/*
143 * This function is derived from PowerPC code (timebase clock frequency).
144 * On ARM it returns the number of timer ticks per second.
145 */
146ulong get_tbclk(void)
147{
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000148 return MXC_CLK32;
Stefano Babic64fdf452010-01-20 18:19:32 +0100149}