John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 4 | * Marius Groeger <mgroeger@sysgo.de> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Alex Zuepke <azu@sysgo.de> |
| 9 | * |
| 10 | * (C) Copyright 2002 |
| 11 | * Gary Jennejohn, DENX Software Engineering, <gj@denx.de> |
| 12 | * |
| 13 | * (C) Copyright 2009 |
| 14 | * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com> |
| 15 | * |
| 16 | * (C) Copyright 2009 DENX Software Engineering |
| 17 | * Author: John Rigby <jrigby@gmail.com> |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 18 | * Add support for MX25 |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 19 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 20 | * SPDX-License-Identifier: GPL-2.0+ |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <common.h> |
| 24 | #include <div64.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/arch/imx-regs.h> |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 27 | #include <asm/arch/clock.h> |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 28 | |
Heiko Schocher | 5504dab | 2011-01-20 22:56:39 +0000 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 31 | #define timestamp (gd->arch.tbl) |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 32 | #define lastinc (gd->arch.lastinc) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * "time" is measured in 1 / CONFIG_SYS_HZ seconds, |
| 36 | * "tick" is internal timer period |
| 37 | */ |
| 38 | #ifdef CONFIG_MX25_TIMER_HIGH_PRECISION |
| 39 | /* ~0.4% error - measured with stop-watch on 100s boot-delay */ |
| 40 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 41 | { |
| 42 | tick *= CONFIG_SYS_HZ; |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 43 | do_div(tick, MXC_CLK32); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 44 | return tick; |
| 45 | } |
| 46 | |
| 47 | static inline unsigned long long time_to_tick(unsigned long long time) |
| 48 | { |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 49 | time *= MXC_CLK32; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 50 | do_div(time, CONFIG_SYS_HZ); |
| 51 | return time; |
| 52 | } |
| 53 | |
| 54 | static inline unsigned long long us_to_tick(unsigned long long us) |
| 55 | { |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 56 | us = us * MXC_CLK32 + 999999; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 57 | do_div(us, 1000000); |
| 58 | return us; |
| 59 | } |
| 60 | #else |
| 61 | /* ~2% error */ |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 62 | #define TICK_PER_TIME ((MXC_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ) |
| 63 | #define US_PER_TICK (1000000 / MXC_CLK32) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 64 | |
| 65 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 66 | { |
| 67 | do_div(tick, TICK_PER_TIME); |
| 68 | return tick; |
| 69 | } |
| 70 | |
| 71 | static inline unsigned long long time_to_tick(unsigned long long time) |
| 72 | { |
| 73 | return time * TICK_PER_TIME; |
| 74 | } |
| 75 | |
| 76 | static inline unsigned long long us_to_tick(unsigned long long us) |
| 77 | { |
| 78 | us += US_PER_TICK - 1; |
| 79 | do_div(us, US_PER_TICK); |
| 80 | return us; |
| 81 | } |
| 82 | #endif |
| 83 | |
| 84 | /* nothing really to do with interrupts, just starts up a counter. */ |
| 85 | /* The 32KHz 32-bit timer overruns in 134217 seconds */ |
| 86 | int timer_init(void) |
| 87 | { |
| 88 | int i; |
| 89 | struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE; |
| 90 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
| 91 | |
| 92 | /* setup GP Timer 1 */ |
| 93 | writel(GPT_CTRL_SWR, &gpt->ctrl); |
| 94 | |
| 95 | writel(readl(&ccm->cgr1) | CCM_CGR1_GPT1, &ccm->cgr1); |
| 96 | |
| 97 | for (i = 0; i < 100; i++) |
| 98 | writel(0, &gpt->ctrl); /* We have no udelay by now */ |
| 99 | writel(0, &gpt->pre); /* prescaler = 1 */ |
| 100 | /* Freerun Mode, 32KHz input */ |
| 101 | writel(readl(&gpt->ctrl) | GPT_CTRL_CLKSOURCE_32 | GPT_CTRL_FRR, |
| 102 | &gpt->ctrl); |
| 103 | writel(readl(&gpt->ctrl) | GPT_CTRL_TEN, &gpt->ctrl); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 108 | unsigned long long get_ticks(void) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 109 | { |
| 110 | struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE; |
| 111 | ulong now = readl(&gpt->counter); /* current tick value */ |
| 112 | |
| 113 | if (now >= lastinc) { |
| 114 | /* |
| 115 | * normal mode (non roll) |
| 116 | * move stamp forward with absolut diff ticks |
| 117 | */ |
| 118 | timestamp += (now - lastinc); |
| 119 | } else { |
| 120 | /* we have rollover of incrementer */ |
| 121 | timestamp += (0xFFFFFFFF - lastinc) + now; |
| 122 | } |
| 123 | lastinc = now; |
| 124 | return timestamp; |
| 125 | } |
| 126 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 127 | ulong get_timer_masked(void) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 128 | { |
| 129 | /* |
| 130 | * get_ticks() returns a long long (64 bit), it wraps in |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 131 | * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~ |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 132 | * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in |
| 133 | * 5 * 10^6 days - long enough. |
| 134 | */ |
| 135 | return tick_to_time(get_ticks()); |
| 136 | } |
| 137 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 138 | ulong get_timer(ulong base) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 139 | { |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 140 | return get_timer_masked() - base; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 141 | } |
| 142 | |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 143 | /* delay x useconds AND preserve advance timstamp value */ |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 144 | void __udelay(unsigned long usec) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 145 | { |
| 146 | unsigned long long tmp; |
| 147 | ulong tmo; |
| 148 | |
| 149 | tmo = us_to_tick(usec); |
| 150 | tmp = get_ticks() + tmo; /* get current timestamp */ |
| 151 | |
| 152 | while (get_ticks() < tmp) /* loop till event */ |
| 153 | /*NOP*/; |
| 154 | } |
Matthias Weisser | 5e0e67d | 2011-07-06 00:28:28 +0000 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * This function is derived from PowerPC code (timebase clock frequency). |
| 158 | * On ARM it returns the number of timer ticks per second. |
| 159 | */ |
| 160 | ulong get_tbclk(void) |
| 161 | { |
| 162 | ulong tbclk; |
| 163 | |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 164 | tbclk = MXC_CLK32; |
Matthias Weisser | 5e0e67d | 2011-07-06 00:28:28 +0000 | [diff] [blame] | 165 | return tbclk; |
| 166 | } |