blob: 7492ba46dc720619e5275073e72baa243d31b85d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutc140e982011-11-08 23:18:08 +00002/*
3 * Freescale i.MX28 timer driver
4 *
5 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * on behalf of DENX Software Engineering GmbH
7 *
8 * Based on code from LTIB:
9 * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
Marek Vasutc140e982011-11-08 23:18:08 +000010 */
11
12#include <common.h>
13#include <asm/io.h>
14#include <asm/arch/imx-regs.h>
15#include <asm/arch/sys_proto.h>
16
17/* Maximum fixed count */
Fadil Berishad608f6e2013-02-27 17:00:07 +000018#if defined(CONFIG_MX23)
19#define TIMER_LOAD_VAL 0xffff
20#elif defined(CONFIG_MX28)
21#define TIMER_LOAD_VAL 0xffffffff
22#endif
Marek Vasutc140e982011-11-08 23:18:08 +000023
24DECLARE_GLOBAL_DATA_PTR;
25
Simon Glass2655ee12012-12-13 20:48:34 +000026#define timestamp (gd->arch.tbl)
Simon Glassa848da52012-12-13 20:48:35 +000027#define lastdec (gd->arch.lastinc)
Marek Vasutc140e982011-11-08 23:18:08 +000028
29/*
30 * This driver uses 1kHz clock source.
31 */
Fadil Berisha69d8ce02013-02-28 10:03:26 -050032#define MXS_INCREMENTER_HZ 1000
Marek Vasutc140e982011-11-08 23:18:08 +000033
34static inline unsigned long tick_to_time(unsigned long tick)
35{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050036 return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasutc140e982011-11-08 23:18:08 +000037}
38
39static inline unsigned long time_to_tick(unsigned long time)
40{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050041 return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasutc140e982011-11-08 23:18:08 +000042}
43
44/* Calculate how many ticks happen in "us" microseconds */
45static inline unsigned long us_to_tick(unsigned long us)
46{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050047 return (us * MXS_INCREMENTER_HZ) / 1000000;
Marek Vasutc140e982011-11-08 23:18:08 +000048}
49
50int timer_init(void)
51{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000052 struct mxs_timrot_regs *timrot_regs =
53 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +000054
55 /* Reset Timers and Rotary Encoder module */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +000056 mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
Marek Vasutc140e982011-11-08 23:18:08 +000057
58 /* Set fixed_count to 0 */
Fadil Berishad608f6e2013-02-27 17:00:07 +000059#if defined(CONFIG_MX23)
60 writel(0, &timrot_regs->hw_timrot_timcount0);
61#elif defined(CONFIG_MX28)
Marek Vasutc140e982011-11-08 23:18:08 +000062 writel(0, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berishad608f6e2013-02-27 17:00:07 +000063#endif
Marek Vasutc140e982011-11-08 23:18:08 +000064
65 /* Set UPDATE bit and 1Khz frequency */
66 writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
67 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
68 &timrot_regs->hw_timrot_timctrl0);
69
70 /* Set fixed_count to maximal value */
Fadil Berishad608f6e2013-02-27 17:00:07 +000071#if defined(CONFIG_MX23)
72 writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
73#elif defined(CONFIG_MX28)
Marek Vasutc140e982011-11-08 23:18:08 +000074 writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berishad608f6e2013-02-27 17:00:07 +000075#endif
Marek Vasutc140e982011-11-08 23:18:08 +000076
77 return 0;
78}
79
Marek Vasutc142b672012-02-07 06:47:31 +000080unsigned long long get_ticks(void)
Marek Vasutc140e982011-11-08 23:18:08 +000081{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000082 struct mxs_timrot_regs *timrot_regs =
83 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Fadil Berishad608f6e2013-02-27 17:00:07 +000084 uint32_t now;
Marek Vasutc140e982011-11-08 23:18:08 +000085
86 /* Current tick value */
Fadil Berishad608f6e2013-02-27 17:00:07 +000087#if defined(CONFIG_MX23)
88 /* Upper bits are the valid ones. */
89 now = readl(&timrot_regs->hw_timrot_timcount0) >>
90 TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
91#elif defined(CONFIG_MX28)
92 now = readl(&timrot_regs->hw_timrot_running_count0);
Wolfgang Denk1fc42342014-11-06 14:03:04 +010093#else
94#error "Don't know how to read timrot_regs"
Fadil Berishad608f6e2013-02-27 17:00:07 +000095#endif
Marek Vasutc140e982011-11-08 23:18:08 +000096
97 if (lastdec >= now) {
98 /*
99 * normal mode (non roll)
100 * move stamp forward with absolut diff ticks
101 */
102 timestamp += (lastdec - now);
103 } else {
104 /* we have rollover of decrementer */
105 timestamp += (TIMER_LOAD_VAL - now) + lastdec;
106
107 }
108 lastdec = now;
109
Marek Vasutc142b672012-02-07 06:47:31 +0000110 return timestamp;
111}
112
Marek Vasutc142b672012-02-07 06:47:31 +0000113ulong get_timer(ulong base)
114{
Patrick Delaunay9858a602018-10-05 11:33:52 +0200115 return tick_to_time(get_ticks()) - base;
Marek Vasutc142b672012-02-07 06:47:31 +0000116}
117
Marek Vasutc140e982011-11-08 23:18:08 +0000118/* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500119#define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
Marek Vasutc140e982011-11-08 23:18:08 +0000120
121void __udelay(unsigned long usec)
122{
123 uint32_t old, new, incr;
124 uint32_t counter = 0;
125
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500126 old = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasutc140e982011-11-08 23:18:08 +0000127
128 while (counter < usec) {
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500129 new = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasutc140e982011-11-08 23:18:08 +0000130
131 /* Check if the timer wrapped. */
132 if (new < old) {
133 incr = 0xffffffff - old;
134 incr += new;
135 } else {
136 incr = new - old;
137 }
138
139 /*
140 * Check if we are close to the maximum time and the counter
141 * would wrap if incremented. If that's the case, break out
142 * from the loop as the requested delay time passed.
143 */
144 if (counter + incr < counter)
145 break;
146
147 counter += incr;
148 old = new;
149 }
150}
Marek Vasutc142b672012-02-07 06:47:31 +0000151
152ulong get_tbclk(void)
153{
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500154 return MXS_INCREMENTER_HZ;
Marek Vasutc142b672012-02-07 06:47:31 +0000155}