blob: 04937a1dfeb733ad342b0a1c9821dabf822e7207 [file] [log] [blame]
Stefano Babic1c2b3ac2011-01-20 07:49:52 +00001/*
2 * (C) Copyright 2007
3 * Sascha Hauer, Pengutronix
4 *
5 * (C) Copyright 2008-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 Babicb36049c2012-02-04 12:56:50 +010028#include <div64.h>
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000029#include <asm/arch/imx-regs.h>
Stefano Babicb36049c2012-02-04 12:56:50 +010030#include <asm/arch/clock.h>
31
32DECLARE_GLOBAL_DATA_PTR;
33
34#define timestamp (gd->tbl)
35#define lastinc (gd->lastinc)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000036
37/* General purpose timers bitfields */
38#define GPTCR_SWR (1<<15) /* Software reset */
39#define GPTCR_FRR (1<<9) /* Freerun / restart */
40#define GPTCR_CLKSOURCE_32 (0x100<<6) /* Clock source */
41#define GPTCR_CLKSOURCE_IPG (0x001<<6) /* Clock source */
42#define GPTCR_TEN (1) /* Timer enable */
Stefano Babicb36049c2012-02-04 12:56:50 +010043
44#define TIMER_FREQ_HZ mxc_get_clock(MXC_IPG_CLK)
45
46static inline unsigned long long tick_to_time(unsigned long long tick)
47{
48 tick *= CONFIG_SYS_HZ;
49 do_div(tick, TIMER_FREQ_HZ);
50
51 return tick;
52}
53
54static inline unsigned long long us_to_tick(unsigned long long usec)
55{
56 usec *= TIMER_FREQ_HZ;
57 do_div(usec, 1000000);
58
59 return usec;
60}
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000061
62int timer_init(void)
63{
64 int i;
65 struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
66
67 /* setup GP Timer 1 */
68 writel(GPTCR_SWR, &gpt->ctrl);
69 for (i = 0; i < 100; i++)
70 writel(0, &gpt->ctrl); /* We have no udelay by now */
71
Stefano Babicb36049c2012-02-04 12:56:50 +010072 writel(0, &gpt->pre);
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000073 /* Freerun Mode, PERCLK1 input */
74 writel(readl(&gpt->ctrl) |
75 GPTCR_CLKSOURCE_IPG | GPTCR_TEN,
76 &gpt->ctrl);
77
78 return 0;
79}
80
Stefano Babicb36049c2012-02-04 12:56:50 +010081unsigned long long get_ticks(void)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000082{
83 struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
Stefano Babicb36049c2012-02-04 12:56:50 +010084 ulong now = readl(&gpt->counter); /* current tick value */
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000085
Stefano Babicb36049c2012-02-04 12:56:50 +010086 if (now >= lastinc) {
87 /*
88 * normal mode (non roll)
89 * move stamp forward with absolut diff ticks
90 */
91 timestamp += (now - lastinc);
92 } else {
93 /* we have rollover of incrementer */
94 timestamp += (0xFFFFFFFF - lastinc) + now;
95 }
96 lastinc = now;
97 return timestamp;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000098}
99
Stefano Babicb36049c2012-02-04 12:56:50 +0100100ulong get_timer_masked(void)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000101{
Stefano Babicb36049c2012-02-04 12:56:50 +0100102 /*
103 * get_ticks() returns a long long (64 bit), it wraps in
104 * 2^64 / CONFIG_MX25_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
105 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
106 * 5 * 10^6 days - long enough.
107 */
108 return tick_to_time(get_ticks());
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000109}
110
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000111ulong get_timer(ulong base)
112{
Stefano Babicb36049c2012-02-04 12:56:50 +0100113 return get_timer_masked() - base;
114}
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000115
Stefano Babicb36049c2012-02-04 12:56:50 +0100116/* delay x useconds AND preserve advance timstamp value */
117void __udelay(unsigned long usec)
118{
119 unsigned long long tmp;
120 ulong tmo;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000121
Stefano Babicb36049c2012-02-04 12:56:50 +0100122 tmo = us_to_tick(usec);
123 tmp = get_ticks() + tmo; /* get current timestamp */
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000124
Stefano Babicb36049c2012-02-04 12:56:50 +0100125 while (get_ticks() < tmp) /* loop till event */
126 /*NOP*/;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000127}
128
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000129/*
Stefano Babicb36049c2012-02-04 12:56:50 +0100130 * This function is derived from PowerPC code (timebase clock frequency).
131 * On ARM it returns the number of timer ticks per second.
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000132 */
Stefano Babicb36049c2012-02-04 12:56:50 +0100133ulong get_tbclk(void)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000134{
Stefano Babicb36049c2012-02-04 12:56:50 +0100135 return TIMER_FREQ_HZ;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000136}