blob: 3dff3d768d1ca8355ed1cd88267a5df3f21f452f [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>
Simon Glass97589732020-05-10 11:40:02 -060013#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070014#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Marek Vasutc140e982011-11-08 23:18:08 +000016#include <asm/io.h>
17#include <asm/arch/imx-regs.h>
18#include <asm/arch/sys_proto.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Marek Vasutc140e982011-11-08 23:18:08 +000020
21/* Maximum fixed count */
Fadil Berishad608f6e2013-02-27 17:00:07 +000022#if defined(CONFIG_MX23)
23#define TIMER_LOAD_VAL 0xffff
24#elif defined(CONFIG_MX28)
25#define TIMER_LOAD_VAL 0xffffffff
26#endif
Marek Vasutc140e982011-11-08 23:18:08 +000027
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glass2655ee12012-12-13 20:48:34 +000030#define timestamp (gd->arch.tbl)
Simon Glassa848da52012-12-13 20:48:35 +000031#define lastdec (gd->arch.lastinc)
Marek Vasutc140e982011-11-08 23:18:08 +000032
33/*
34 * This driver uses 1kHz clock source.
35 */
Fadil Berisha69d8ce02013-02-28 10:03:26 -050036#define MXS_INCREMENTER_HZ 1000
Marek Vasutc140e982011-11-08 23:18:08 +000037
38static inline unsigned long tick_to_time(unsigned long tick)
39{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050040 return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasutc140e982011-11-08 23:18:08 +000041}
42
43static inline unsigned long time_to_tick(unsigned long time)
44{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050045 return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
Marek Vasutc140e982011-11-08 23:18:08 +000046}
47
48/* Calculate how many ticks happen in "us" microseconds */
49static inline unsigned long us_to_tick(unsigned long us)
50{
Fadil Berisha69d8ce02013-02-28 10:03:26 -050051 return (us * MXS_INCREMENTER_HZ) / 1000000;
Marek Vasutc140e982011-11-08 23:18:08 +000052}
53
54int timer_init(void)
55{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000056 struct mxs_timrot_regs *timrot_regs =
57 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +000058
59 /* Reset Timers and Rotary Encoder module */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +000060 mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
Marek Vasutc140e982011-11-08 23:18:08 +000061
62 /* Set fixed_count to 0 */
Fadil Berishad608f6e2013-02-27 17:00:07 +000063#if defined(CONFIG_MX23)
64 writel(0, &timrot_regs->hw_timrot_timcount0);
65#elif defined(CONFIG_MX28)
Marek Vasutc140e982011-11-08 23:18:08 +000066 writel(0, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berishad608f6e2013-02-27 17:00:07 +000067#endif
Marek Vasutc140e982011-11-08 23:18:08 +000068
69 /* Set UPDATE bit and 1Khz frequency */
70 writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
71 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
72 &timrot_regs->hw_timrot_timctrl0);
73
74 /* Set fixed_count to maximal value */
Fadil Berishad608f6e2013-02-27 17:00:07 +000075#if defined(CONFIG_MX23)
76 writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
77#elif defined(CONFIG_MX28)
Marek Vasutc140e982011-11-08 23:18:08 +000078 writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
Fadil Berishad608f6e2013-02-27 17:00:07 +000079#endif
Marek Vasutc140e982011-11-08 23:18:08 +000080
81 return 0;
82}
83
Marek Vasutc142b672012-02-07 06:47:31 +000084unsigned long long get_ticks(void)
Marek Vasutc140e982011-11-08 23:18:08 +000085{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000086 struct mxs_timrot_regs *timrot_regs =
87 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
Fadil Berishad608f6e2013-02-27 17:00:07 +000088 uint32_t now;
Marek Vasutc140e982011-11-08 23:18:08 +000089
90 /* Current tick value */
Fadil Berishad608f6e2013-02-27 17:00:07 +000091#if defined(CONFIG_MX23)
92 /* Upper bits are the valid ones. */
93 now = readl(&timrot_regs->hw_timrot_timcount0) >>
94 TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
95#elif defined(CONFIG_MX28)
96 now = readl(&timrot_regs->hw_timrot_running_count0);
Wolfgang Denk1fc42342014-11-06 14:03:04 +010097#else
98#error "Don't know how to read timrot_regs"
Fadil Berishad608f6e2013-02-27 17:00:07 +000099#endif
Marek Vasutc140e982011-11-08 23:18:08 +0000100
101 if (lastdec >= now) {
102 /*
103 * normal mode (non roll)
104 * move stamp forward with absolut diff ticks
105 */
106 timestamp += (lastdec - now);
107 } else {
108 /* we have rollover of decrementer */
109 timestamp += (TIMER_LOAD_VAL - now) + lastdec;
110
111 }
112 lastdec = now;
113
Marek Vasutc142b672012-02-07 06:47:31 +0000114 return timestamp;
115}
116
Marek Vasutc142b672012-02-07 06:47:31 +0000117ulong get_timer(ulong base)
118{
Patrick Delaunay9858a602018-10-05 11:33:52 +0200119 return tick_to_time(get_ticks()) - base;
Marek Vasutc142b672012-02-07 06:47:31 +0000120}
121
Marek Vasutc140e982011-11-08 23:18:08 +0000122/* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500123#define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
Marek Vasutc140e982011-11-08 23:18:08 +0000124
125void __udelay(unsigned long usec)
126{
127 uint32_t old, new, incr;
128 uint32_t counter = 0;
129
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500130 old = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasutc140e982011-11-08 23:18:08 +0000131
132 while (counter < usec) {
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500133 new = readl(MXS_HW_DIGCTL_MICROSECONDS);
Marek Vasutc140e982011-11-08 23:18:08 +0000134
135 /* Check if the timer wrapped. */
136 if (new < old) {
137 incr = 0xffffffff - old;
138 incr += new;
139 } else {
140 incr = new - old;
141 }
142
143 /*
144 * Check if we are close to the maximum time and the counter
145 * would wrap if incremented. If that's the case, break out
146 * from the loop as the requested delay time passed.
147 */
148 if (counter + incr < counter)
149 break;
150
151 counter += incr;
152 old = new;
153 }
154}
Marek Vasutc142b672012-02-07 06:47:31 +0000155
156ulong get_tbclk(void)
157{
Fadil Berisha69d8ce02013-02-28 10:03:26 -0500158 return MXS_INCREMENTER_HZ;
Marek Vasutc142b672012-02-07 06:47:31 +0000159}