blob: 98e9f4ad61f978be533bce1ec996e3909028da75 [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>
28#include <asm/arch/imx-regs.h>
29
30/* General purpose timers registers */
31struct mxc_gpt {
32 unsigned int control;
33 unsigned int prescaler;
34 unsigned int status;
35 unsigned int nouse[6];
36 unsigned int counter;
37};
38
39static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
40
41/* General purpose timers bitfields */
Jason Liu18936ee2011-11-25 00:18:01 +000042#define GPTCR_SWR (1 << 15) /* Software reset */
43#define GPTCR_FRR (1 << 9) /* Freerun / restart */
44#define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
45#define GPTCR_TEN 1 /* Timer enable */
46#define CLK_32KHZ 32768 /* 32Khz input */
Stefano Babic64fdf452010-01-20 18:19:32 +010047
Stefano Babicdb106ef2011-01-21 21:16:15 +010048DECLARE_GLOBAL_DATA_PTR;
49
50#define timestamp (gd->tbl)
51#define lastinc (gd->lastinc)
Stefano Babic64fdf452010-01-20 18:19:32 +010052
53int timer_init(void)
54{
55 int i;
Graeme Russ17659d72011-07-15 02:21:14 +000056 ulong val;
Stefano Babic64fdf452010-01-20 18:19:32 +010057
58 /* setup GP Timer 1 */
59 __raw_writel(GPTCR_SWR, &cur_gpt->control);
60
61 /* We have no udelay by now */
62 for (i = 0; i < 100; i++)
63 __raw_writel(0, &cur_gpt->control);
64
65 __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */
66
67 /* Freerun Mode, PERCLK1 input */
68 i = __raw_readl(&cur_gpt->control);
69 __raw_writel(i | GPTCR_CLKSOURCE_32 | GPTCR_TEN, &cur_gpt->control);
Stefano Babic64fdf452010-01-20 18:19:32 +010070
Graeme Russ17659d72011-07-15 02:21:14 +000071 val = __raw_readl(&cur_gpt->counter);
Jason Liu18936ee2011-11-25 00:18:01 +000072 lastinc = val / (CLK_32KHZ / CONFIG_SYS_HZ);
Stefano Babic64fdf452010-01-20 18:19:32 +010073 timestamp = 0;
Graeme Russ17659d72011-07-15 02:21:14 +000074
75 return 0;
Stefano Babic64fdf452010-01-20 18:19:32 +010076}
77
78ulong get_timer_masked(void)
79{
80 ulong val = __raw_readl(&cur_gpt->counter);
Jason Liu18936ee2011-11-25 00:18:01 +000081 val /= (CLK_32KHZ / CONFIG_SYS_HZ);
Stefano Babic64fdf452010-01-20 18:19:32 +010082 if (val >= lastinc)
83 timestamp += (val - lastinc);
84 else
Jason Liu18936ee2011-11-25 00:18:01 +000085 timestamp += ((0xFFFFFFFF / (CLK_32KHZ / CONFIG_SYS_HZ))
Stefano Babic64fdf452010-01-20 18:19:32 +010086 - lastinc) + val;
87 lastinc = val;
Li Haibo51b58702010-08-10 14:18:38 +080088 return timestamp;
Stefano Babic64fdf452010-01-20 18:19:32 +010089}
90
91ulong get_timer(ulong base)
92{
93 return get_timer_masked() - base;
94}
95
Wolfgang Denk92381c42010-05-21 23:13:18 +020096/* delay x useconds AND preserve advance timestamp value */
Stefano Babic64fdf452010-01-20 18:19:32 +010097void __udelay(unsigned long usec)
98{
99 unsigned long now, start, tmo;
Jason Liu18936ee2011-11-25 00:18:01 +0000100 tmo = usec * (CLK_32KHZ / 1000) / 1000;
Stefano Babic64fdf452010-01-20 18:19:32 +0100101
102 if (!tmo)
103 tmo = 1;
104
105 now = start = readl(&cur_gpt->counter);
106
107 while ((now - start) < tmo)
108 now = readl(&cur_gpt->counter);
109
110}