blob: c21ca3f9833b83d319ab2f4d653bc345fa7f2563 [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>
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000030#include <asm/arch/crm_regs.h>
Stefano Babicb36049c2012-02-04 12:56:50 +010031#include <asm/arch/clock.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
Simon Glass2655ee12012-12-13 20:48:34 +000035#define timestamp (gd->arch.tbl)
Stefano Babicb36049c2012-02-04 12:56:50 +010036#define lastinc (gd->lastinc)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000037
38/* General purpose timers bitfields */
39#define GPTCR_SWR (1<<15) /* Software reset */
40#define GPTCR_FRR (1<<9) /* Freerun / restart */
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000041#define GPTCR_CLKSOURCE_32 (4<<6) /* Clock source */
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000042#define GPTCR_TEN (1) /* Timer enable */
Stefano Babicb36049c2012-02-04 12:56:50 +010043
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000044/*
45 * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
46 * "tick" is internal timer period
47 */
48/* ~0.4% error - measured with stop-watch on 100s boot-delay */
Stefano Babicb36049c2012-02-04 12:56:50 +010049static inline unsigned long long tick_to_time(unsigned long long tick)
50{
51 tick *= CONFIG_SYS_HZ;
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000052 do_div(tick, MXC_CLK32);
Stefano Babicb36049c2012-02-04 12:56:50 +010053
54 return tick;
55}
56
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000057static inline unsigned long long us_to_tick(unsigned long long us)
Stefano Babicb36049c2012-02-04 12:56:50 +010058{
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000059 us = us * MXC_CLK32 + 999999;
60 do_div(us, 1000000);
Stefano Babicb36049c2012-02-04 12:56:50 +010061
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000062 return us;
Stefano Babicb36049c2012-02-04 12:56:50 +010063}
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000064
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000065/*
66 * nothing really to do with interrupts, just starts up a counter.
67 * The 32KHz 32-bit timer overruns in 134217 seconds
68 */
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000069int timer_init(void)
70{
71 int i;
72 struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000073 struct ccm_regs *ccm = (struct ccm_regs *)CCM_BASE_ADDR;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000074
75 /* setup GP Timer 1 */
76 writel(GPTCR_SWR, &gpt->ctrl);
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000077
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +000078 writel(readl(&ccm->cgr1) | 3 << MXC_CCM_CGR1_GPT_OFFSET, &ccm->cgr1);
79
80 for (i = 0; i < 100; i++)
81 writel(0, &gpt->ctrl); /* We have no udelay by now */
82 writel(0, &gpt->pre); /* prescaler = 1 */
83 /* Freerun Mode, 32KHz input */
84 writel(readl(&gpt->ctrl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
85 &gpt->ctrl);
86 writel(readl(&gpt->ctrl) | GPTCR_TEN, &gpt->ctrl);
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000087
88 return 0;
89}
90
Stefano Babicb36049c2012-02-04 12:56:50 +010091unsigned long long get_ticks(void)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000092{
93 struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
Stefano Babicb36049c2012-02-04 12:56:50 +010094 ulong now = readl(&gpt->counter); /* current tick value */
Stefano Babic1c2b3ac2011-01-20 07:49:52 +000095
Stefano Babicb36049c2012-02-04 12:56:50 +010096 if (now >= lastinc) {
97 /*
98 * normal mode (non roll)
99 * move stamp forward with absolut diff ticks
100 */
101 timestamp += (now - lastinc);
102 } else {
103 /* we have rollover of incrementer */
104 timestamp += (0xFFFFFFFF - lastinc) + now;
105 }
106 lastinc = now;
107 return timestamp;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000108}
109
Stefano Babicb36049c2012-02-04 12:56:50 +0100110ulong get_timer_masked(void)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000111{
Stefano Babicb36049c2012-02-04 12:56:50 +0100112 /*
113 * get_ticks() returns a long long (64 bit), it wraps in
Benoît Thébaudeaue4528342012-08-21 11:07:20 +0000114 * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
Stefano Babicb36049c2012-02-04 12:56:50 +0100115 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
116 * 5 * 10^6 days - long enough.
117 */
118 return tick_to_time(get_ticks());
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000119}
120
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000121ulong get_timer(ulong base)
122{
Stefano Babicb36049c2012-02-04 12:56:50 +0100123 return get_timer_masked() - base;
124}
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000125
Stefano Babicb36049c2012-02-04 12:56:50 +0100126/* delay x useconds AND preserve advance timstamp value */
127void __udelay(unsigned long usec)
128{
129 unsigned long long tmp;
130 ulong tmo;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000131
Stefano Babicb36049c2012-02-04 12:56:50 +0100132 tmo = us_to_tick(usec);
133 tmp = get_ticks() + tmo; /* get current timestamp */
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000134
Stefano Babicb36049c2012-02-04 12:56:50 +0100135 while (get_ticks() < tmp) /* loop till event */
136 /*NOP*/;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000137}
138
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000139/*
Stefano Babicb36049c2012-02-04 12:56:50 +0100140 * This function is derived from PowerPC code (timebase clock frequency).
141 * On ARM it returns the number of timer ticks per second.
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000142 */
Stefano Babicb36049c2012-02-04 12:56:50 +0100143ulong get_tbclk(void)
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000144{
Benoît Thébaudeau6ce2db02012-08-21 11:07:54 +0000145 return MXC_CLK32;
Stefano Babic1c2b3ac2011-01-20 07:49:52 +0000146}