blob: f2e72257d15b5d77fb7d583152f2b19b1f32a95f [file] [log] [blame]
Marek Vasutc140e982011-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 Denkbd8ec7e2013-10-07 13:07:26 +020010 * SPDX-License-Identifier: GPL-2.0+
Marek Vasutc140e982011-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 Berishad608f6e2013-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 Vasutc140e982011-11-08 23:18:08 +000024
25DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass2655ee12012-12-13 20:48:34 +000027#define timestamp (gd->arch.tbl)
Simon Glassa848da52012-12-13 20:48:35 +000028#define lastdec (gd->arch.lastinc)
Marek Vasutc140e982011-11-08 23:18:08 +000029
30/*
31 * This driver uses 1kHz clock source.
32 */
Fadil Berisha69d8ce02013-02-28 10:03:26 -050033#define MXS_INCREMENTER_HZ 1000
Marek Vasutc140e982011-11-08 23:18:08 +000034
35static inline unsigned long tick_to_time(unsigned long tick)
36{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050037 return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasutc140e982011-11-08 23:18:08 +000038}
39
40static inline unsigned long time_to_tick(unsigned long time)
41{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050042 return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasutc140e982011-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 Berisha69d8ce02013-02-28 10:03:26 -050048 return (us * MXS_INCREMENTER_HZ) / 1000000;
Marek Vasutc140e982011-11-08 23:18:08 +000049}
50
51int timer_init(void)
52{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000053 struct mxs_timrot_regs *timrot_regs =
54 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +000055
56 /* Reset Timers and Rotary Encoder module */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +000057 mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
Marek Vasutc140e982011-11-08 23:18:08 +000058
59 /* Set fixed_count to 0 */
Fadil Berishad608f6e2013-02-27 17:00:07 +000060#if defined(CONFIG_MX23)
61 writel(0, &timrot_regs->hw_timrot_timcount0);
62#elif defined(CONFIG_MX28)
Marek Vasutc140e982011-11-08 23:18:08 +000063 writel(0, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berishad608f6e2013-02-27 17:00:07 +000064#endif
Marek Vasutc140e982011-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 Berishad608f6e2013-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 Vasutc140e982011-11-08 23:18:08 +000075 writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berishad608f6e2013-02-27 17:00:07 +000076#endif
Marek Vasutc140e982011-11-08 23:18:08 +000077
78 return 0;
79}
80
Marek Vasutc142b672012-02-07 06:47:31 +000081unsigned long long get_ticks(void)
Marek Vasutc140e982011-11-08 23:18:08 +000082{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000083 struct mxs_timrot_regs *timrot_regs =
84 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Fadil Berishad608f6e2013-02-27 17:00:07 +000085 uint32_t now;
Marek Vasutc140e982011-11-08 23:18:08 +000086
87 /* Current tick value */
Fadil Berishad608f6e2013-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 Denk1fc42342014-11-06 14:03:04 +010094#else
95#error "Don't know how to read timrot_regs"
Fadil Berishad608f6e2013-02-27 17:00:07 +000096#endif
Marek Vasutc140e982011-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 Vasutc142b672012-02-07 06:47:31 +0000111 return timestamp;
112}
113
114ulong get_timer_masked(void)
115{
116 return tick_to_time(get_ticks());
Marek Vasutc140e982011-11-08 23:18:08 +0000117}
118
Marek Vasutc142b672012-02-07 06:47:31 +0000119ulong get_timer(ulong base)
120{
121 return get_timer_masked() - base;
122}
123
Marek Vasutc140e982011-11-08 23:18:08 +0000124/* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500125#define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
Marek Vasutc140e982011-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 Berisha69d8ce02013-02-28 10:03:26 -0500132 old = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasutc140e982011-11-08 23:18:08 +0000133
134 while (counter < usec) {
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500135 new = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasutc140e982011-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 Vasutc142b672012-02-07 06:47:31 +0000157
158ulong get_tbclk(void)
159{
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500160 return MXS_INCREMENTER_HZ;
Marek Vasutc142b672012-02-07 06:47:31 +0000161}