blob: f2e72257d15b5d77fb7d583152f2b19b1f32a95f [file] [log] [blame]
Marek Vasut6e9a0a32011-11-08 23:18:08 +00001/*
2 * Freescale i.MX28 timer driver
3 *
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
6 *
7 * Based on code from LTIB:
8 * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
9 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +020010 * SPDX-License-Identifier: GPL-2.0+
Marek Vasut6e9a0a32011-11-08 23:18:08 +000011 */
12
13#include <common.h>
14#include <asm/io.h>
15#include <asm/arch/imx-regs.h>
16#include <asm/arch/sys_proto.h>
17
18/* Maximum fixed count */
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000019#if defined(CONFIG_MX23)
20#define TIMER_LOAD_VAL 0xffff
21#elif defined(CONFIG_MX28)
22#define TIMER_LOAD_VAL 0xffffffff
23#endif
Marek Vasut6e9a0a32011-11-08 23:18:08 +000024
25DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass66ee6922012-12-13 20:48:34 +000027#define timestamp (gd->arch.tbl)
Simon Glass582601d2012-12-13 20:48:35 +000028#define lastdec (gd->arch.lastinc)
Marek Vasut6e9a0a32011-11-08 23:18:08 +000029
30/*
31 * This driver uses 1kHz clock source.
32 */
Fadil Berisha3e9dc932013-02-28 10:03:26 -050033#define MXS_INCREMENTER_HZ 1000
Marek Vasut6e9a0a32011-11-08 23:18:08 +000034
35static inline unsigned long tick_to_time(unsigned long tick)
36{
Fadil Berisha3e9dc932013-02-28 10:03:26 -050037 return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasut6e9a0a32011-11-08 23:18:08 +000038}
39
40static inline unsigned long time_to_tick(unsigned long time)
41{
Fadil Berisha3e9dc932013-02-28 10:03:26 -050042 return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasut6e9a0a32011-11-08 23:18:08 +000043}
44
45/* Calculate how many ticks happen in "us" microseconds */
46static inline unsigned long us_to_tick(unsigned long us)
47{
Fadil Berisha3e9dc932013-02-28 10:03:26 -050048 return (us * MXS_INCREMENTER_HZ) / 1000000;
Marek Vasut6e9a0a32011-11-08 23:18:08 +000049}
50
51int timer_init(void)
52{
Otavio Salvador9c471142012-08-05 09:05:31 +000053 struct mxs_timrot_regs *timrot_regs =
54 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Marek Vasut6e9a0a32011-11-08 23:18:08 +000055
56 /* Reset Timers and Rotary Encoder module */
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +000057 mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
Marek Vasut6e9a0a32011-11-08 23:18:08 +000058
59 /* Set fixed_count to 0 */
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000060#if defined(CONFIG_MX23)
61 writel(0, &timrot_regs->hw_timrot_timcount0);
62#elif defined(CONFIG_MX28)
Marek Vasut6e9a0a32011-11-08 23:18:08 +000063 writel(0, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000064#endif
Marek Vasut6e9a0a32011-11-08 23:18:08 +000065
66 /* Set UPDATE bit and 1Khz frequency */
67 writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
68 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
69 &timrot_regs->hw_timrot_timctrl0);
70
71 /* Set fixed_count to maximal value */
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000072#if defined(CONFIG_MX23)
73 writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
74#elif defined(CONFIG_MX28)
Marek Vasut6e9a0a32011-11-08 23:18:08 +000075 writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000076#endif
Marek Vasut6e9a0a32011-11-08 23:18:08 +000077
78 return 0;
79}
80
Marek Vasutff250f32012-02-07 06:47:31 +000081unsigned long long get_ticks(void)
Marek Vasut6e9a0a32011-11-08 23:18:08 +000082{
Otavio Salvador9c471142012-08-05 09:05:31 +000083 struct mxs_timrot_regs *timrot_regs =
84 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000085 uint32_t now;
Marek Vasut6e9a0a32011-11-08 23:18:08 +000086
87 /* Current tick value */
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000088#if defined(CONFIG_MX23)
89 /* Upper bits are the valid ones. */
90 now = readl(&timrot_regs->hw_timrot_timcount0) >>
91 TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
92#elif defined(CONFIG_MX28)
93 now = readl(&timrot_regs->hw_timrot_running_count0);
Wolfgang Denkfeb8cf42014-11-06 14:03:04 +010094#else
95#error "Don't know how to read timrot_regs"
Fadil Berisha6ecd05d2013-02-27 17:00:07 +000096#endif
Marek Vasut6e9a0a32011-11-08 23:18:08 +000097
98 if (lastdec >= now) {
99 /*
100 * normal mode (non roll)
101 * move stamp forward with absolut diff ticks
102 */
103 timestamp += (lastdec - now);
104 } else {
105 /* we have rollover of decrementer */
106 timestamp += (TIMER_LOAD_VAL - now) + lastdec;
107
108 }
109 lastdec = now;
110
Marek Vasutff250f32012-02-07 06:47:31 +0000111 return timestamp;
112}
113
114ulong get_timer_masked(void)
115{
116 return tick_to_time(get_ticks());
117}
118
119ulong get_timer(ulong base)
120{
121 return get_timer_masked() - base;
Marek Vasut6e9a0a32011-11-08 23:18:08 +0000122}
123
124/* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
Fadil Berisha3e9dc932013-02-28 10:03:26 -0500125#define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
Marek Vasut6e9a0a32011-11-08 23:18:08 +0000126
127void __udelay(unsigned long usec)
128{
129 uint32_t old, new, incr;
130 uint32_t counter = 0;
131
Fadil Berisha3e9dc932013-02-28 10:03:26 -0500132 old = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasut6e9a0a32011-11-08 23:18:08 +0000133
134 while (counter < usec) {
Fadil Berisha3e9dc932013-02-28 10:03:26 -0500135 new = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasut6e9a0a32011-11-08 23:18:08 +0000136
137 /* Check if the timer wrapped. */
138 if (new < old) {
139 incr = 0xffffffff - old;
140 incr += new;
141 } else {
142 incr = new - old;
143 }
144
145 /*
146 * Check if we are close to the maximum time and the counter
147 * would wrap if incremented. If that's the case, break out
148 * from the loop as the requested delay time passed.
149 */
150 if (counter + incr < counter)
151 break;
152
153 counter += incr;
154 old = new;
155 }
156}
Marek Vasutff250f32012-02-07 06:47:31 +0000157
158ulong get_tbclk(void)
159{
Fadil Berisha3e9dc932013-02-28 10:03:26 -0500160 return MXS_INCREMENTER_HZ;
Marek Vasutff250f32012-02-07 06:47:31 +0000161}