blob: c63f78f6823cee5432e395bc8a7550564a6dee58 [file] [log] [blame]
Stefano Babica521a772010-01-20 18:19:32 +01001/*
2 * (C) Copyright 2007
3 * Sascha Hauer, Pengutronix
4 *
5 * (C) Copyright 2009 Freescale Semiconductor, Inc.
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Stefano Babica521a772010-01-20 18:19:32 +01008 */
9
10#include <common.h>
11#include <asm/io.h>
Stefano Babicc37b7f72012-02-06 12:52:36 +010012#include <div64.h>
Stefano Babica521a772010-01-20 18:19:32 +010013#include <asm/arch/imx-regs.h>
Benoît Thébaudeauafac1652012-09-27 10:19:58 +000014#include <asm/arch/clock.h>
Stefano Babica521a772010-01-20 18:19:32 +010015
16/* General purpose timers registers */
17struct mxc_gpt {
18 unsigned int control;
19 unsigned int prescaler;
20 unsigned int status;
21 unsigned int nouse[6];
22 unsigned int counter;
23};
24
25static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
26
27/* General purpose timers bitfields */
Jason Liu83aa8fe2011-11-25 00:18:01 +000028#define GPTCR_SWR (1 << 15) /* Software reset */
29#define GPTCR_FRR (1 << 9) /* Freerun / restart */
30#define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
31#define GPTCR_TEN 1 /* Timer enable */
Stefano Babica521a772010-01-20 18:19:32 +010032
Stefano Babic19c53b42011-01-21 21:16:15 +010033DECLARE_GLOBAL_DATA_PTR;
34
Stefano Babicc37b7f72012-02-06 12:52:36 +010035static inline unsigned long long tick_to_time(unsigned long long tick)
36{
37 tick *= CONFIG_SYS_HZ;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +000038 do_div(tick, MXC_CLK32);
Stefano Babicc37b7f72012-02-06 12:52:36 +010039
40 return tick;
41}
42
43static inline unsigned long long us_to_tick(unsigned long long usec)
44{
Benoît Thébaudeauafac1652012-09-27 10:19:58 +000045 usec = usec * MXC_CLK32 + 999999;
Stefano Babicc37b7f72012-02-06 12:52:36 +010046 do_div(usec, 1000000);
47
48 return usec;
49}
50
Stefano Babica521a772010-01-20 18:19:32 +010051int timer_init(void)
52{
53 int i;
54
55 /* setup GP Timer 1 */
56 __raw_writel(GPTCR_SWR, &cur_gpt->control);
57
58 /* We have no udelay by now */
59 for (i = 0; i < 100; i++)
60 __raw_writel(0, &cur_gpt->control);
61
62 __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */
63
64 /* Freerun Mode, PERCLK1 input */
65 i = __raw_readl(&cur_gpt->control);
66 __raw_writel(i | GPTCR_CLKSOURCE_32 | GPTCR_TEN, &cur_gpt->control);
Stefano Babica521a772010-01-20 18:19:32 +010067
Knut Wohlrabd2481f22013-03-04 04:16:02 +000068 gd->arch.tbl = __raw_readl(&cur_gpt->counter);
69 gd->arch.tbu = 0;
Graeme Russ944a7fe2011-07-15 02:21:14 +000070
71 return 0;
Stefano Babica521a772010-01-20 18:19:32 +010072}
73
Stefano Babicc37b7f72012-02-06 12:52:36 +010074unsigned long long get_ticks(void)
Stefano Babica521a772010-01-20 18:19:32 +010075{
Stefano Babicc37b7f72012-02-06 12:52:36 +010076 ulong now = __raw_readl(&cur_gpt->counter); /* current tick value */
77
Knut Wohlrabd2481f22013-03-04 04:16:02 +000078 /* increment tbu if tbl has rolled over */
79 if (now < gd->arch.tbl)
80 gd->arch.tbu++;
81 gd->arch.tbl = now;
82 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
Stefano Babica521a772010-01-20 18:19:32 +010083}
84
Stefano Babicc37b7f72012-02-06 12:52:36 +010085ulong get_timer_masked(void)
86{
87 /*
88 * get_ticks() returns a long long (64 bit), it wraps in
Benoît Thébaudeauafac1652012-09-27 10:19:58 +000089 * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
Stefano Babicc37b7f72012-02-06 12:52:36 +010090 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
91 * 5 * 10^6 days - long enough.
92 */
93 return tick_to_time(get_ticks());
94}
95
Stefano Babica521a772010-01-20 18:19:32 +010096ulong get_timer(ulong base)
97{
98 return get_timer_masked() - base;
99}
100
Stefano Babicc37b7f72012-02-06 12:52:36 +0100101/* delay x useconds AND preserve advance timstamp value */
Stefano Babica521a772010-01-20 18:19:32 +0100102void __udelay(unsigned long usec)
103{
Stefano Babicc37b7f72012-02-06 12:52:36 +0100104 unsigned long long tmp;
105 ulong tmo;
Stefano Babica521a772010-01-20 18:19:32 +0100106
Stefano Babicc37b7f72012-02-06 12:52:36 +0100107 tmo = us_to_tick(usec);
108 tmp = get_ticks() + tmo; /* get current timestamp */
Stefano Babica521a772010-01-20 18:19:32 +0100109
Stefano Babicc37b7f72012-02-06 12:52:36 +0100110 while (get_ticks() < tmp) /* loop till event */
111 /*NOP*/;
112}
Stefano Babica521a772010-01-20 18:19:32 +0100113
Stefano Babicc37b7f72012-02-06 12:52:36 +0100114/*
115 * This function is derived from PowerPC code (timebase clock frequency).
116 * On ARM it returns the number of timer ticks per second.
117 */
118ulong get_tbclk(void)
119{
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000120 return MXC_CLK32;
Stefano Babica521a772010-01-20 18:19:32 +0100121}