2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-11-08 23:18:08 +00:00
|
|
|
/*
|
|
|
|
* Freescale i.MX28 timer driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
|
|
|
|
* on behalf of DENX Software Engineering GmbH
|
|
|
|
*
|
|
|
|
* Based on code from LTIB:
|
|
|
|
* (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:02 +00:00
|
|
|
#include <init.h>
|
2019-11-14 19:57:30 +00:00
|
|
|
#include <time.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2011-11-08 23:18:08 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/arch/imx-regs.h>
|
|
|
|
#include <asm/arch/sys_proto.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
/* Maximum fixed count */
|
2013-02-27 17:00:07 +00:00
|
|
|
#if defined(CONFIG_MX23)
|
|
|
|
#define TIMER_LOAD_VAL 0xffff
|
|
|
|
#elif defined(CONFIG_MX28)
|
|
|
|
#define TIMER_LOAD_VAL 0xffffffff
|
|
|
|
#endif
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2012-12-13 20:48:34 +00:00
|
|
|
#define timestamp (gd->arch.tbl)
|
2012-12-13 20:48:35 +00:00
|
|
|
#define lastdec (gd->arch.lastinc)
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This driver uses 1kHz clock source.
|
|
|
|
*/
|
2013-02-28 15:03:26 +00:00
|
|
|
#define MXS_INCREMENTER_HZ 1000
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
static inline unsigned long tick_to_time(unsigned long tick)
|
|
|
|
{
|
2013-02-28 15:03:26 +00:00
|
|
|
return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
|
2011-11-08 23:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long time_to_tick(unsigned long time)
|
|
|
|
{
|
2013-02-28 15:03:26 +00:00
|
|
|
return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
|
2011-11-08 23:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate how many ticks happen in "us" microseconds */
|
|
|
|
static inline unsigned long us_to_tick(unsigned long us)
|
|
|
|
{
|
2013-02-28 15:03:26 +00:00
|
|
|
return (us * MXS_INCREMENTER_HZ) / 1000000;
|
2011-11-08 23:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int timer_init(void)
|
|
|
|
{
|
2012-08-05 09:05:31 +00:00
|
|
|
struct mxs_timrot_regs *timrot_regs =
|
|
|
|
(struct mxs_timrot_regs *)MXS_TIMROT_BASE;
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
/* Reset Timers and Rotary Encoder module */
|
2012-08-13 09:53:12 +00:00
|
|
|
mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
/* Set fixed_count to 0 */
|
2013-02-27 17:00:07 +00:00
|
|
|
#if defined(CONFIG_MX23)
|
|
|
|
writel(0, &timrot_regs->hw_timrot_timcount0);
|
|
|
|
#elif defined(CONFIG_MX28)
|
2011-11-08 23:18:08 +00:00
|
|
|
writel(0, &timrot_regs->hw_timrot_fixed_count0);
|
2013-02-27 17:00:07 +00:00
|
|
|
#endif
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
/* Set UPDATE bit and 1Khz frequency */
|
|
|
|
writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
|
|
|
|
TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
|
|
|
|
&timrot_regs->hw_timrot_timctrl0);
|
|
|
|
|
|
|
|
/* Set fixed_count to maximal value */
|
2013-02-27 17:00:07 +00:00
|
|
|
#if defined(CONFIG_MX23)
|
|
|
|
writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
|
|
|
|
#elif defined(CONFIG_MX28)
|
2011-11-08 23:18:08 +00:00
|
|
|
writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
|
2013-02-27 17:00:07 +00:00
|
|
|
#endif
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-07 06:47:31 +00:00
|
|
|
unsigned long long get_ticks(void)
|
2011-11-08 23:18:08 +00:00
|
|
|
{
|
2012-08-05 09:05:31 +00:00
|
|
|
struct mxs_timrot_regs *timrot_regs =
|
|
|
|
(struct mxs_timrot_regs *)MXS_TIMROT_BASE;
|
2013-02-27 17:00:07 +00:00
|
|
|
uint32_t now;
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
/* Current tick value */
|
2013-02-27 17:00:07 +00:00
|
|
|
#if defined(CONFIG_MX23)
|
|
|
|
/* Upper bits are the valid ones. */
|
|
|
|
now = readl(&timrot_regs->hw_timrot_timcount0) >>
|
|
|
|
TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
|
|
|
|
#elif defined(CONFIG_MX28)
|
|
|
|
now = readl(&timrot_regs->hw_timrot_running_count0);
|
2014-11-06 13:03:04 +00:00
|
|
|
#else
|
|
|
|
#error "Don't know how to read timrot_regs"
|
2013-02-27 17:00:07 +00:00
|
|
|
#endif
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
if (lastdec >= now) {
|
|
|
|
/*
|
|
|
|
* normal mode (non roll)
|
|
|
|
* move stamp forward with absolut diff ticks
|
|
|
|
*/
|
|
|
|
timestamp += (lastdec - now);
|
|
|
|
} else {
|
|
|
|
/* we have rollover of decrementer */
|
|
|
|
timestamp += (TIMER_LOAD_VAL - now) + lastdec;
|
|
|
|
|
|
|
|
}
|
|
|
|
lastdec = now;
|
|
|
|
|
2012-02-07 06:47:31 +00:00
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong get_timer(ulong base)
|
|
|
|
{
|
2018-10-05 09:33:52 +00:00
|
|
|
return tick_to_time(get_ticks()) - base;
|
2011-11-08 23:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
|
2013-02-28 15:03:26 +00:00
|
|
|
#define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
void __udelay(unsigned long usec)
|
|
|
|
{
|
|
|
|
uint32_t old, new, incr;
|
|
|
|
uint32_t counter = 0;
|
|
|
|
|
2013-02-28 15:03:26 +00:00
|
|
|
old = readl(MXS_HW_DIGCTL_MICROSECONDS);
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
while (counter < usec) {
|
2013-02-28 15:03:26 +00:00
|
|
|
new = readl(MXS_HW_DIGCTL_MICROSECONDS);
|
2011-11-08 23:18:08 +00:00
|
|
|
|
|
|
|
/* Check if the timer wrapped. */
|
|
|
|
if (new < old) {
|
|
|
|
incr = 0xffffffff - old;
|
|
|
|
incr += new;
|
|
|
|
} else {
|
|
|
|
incr = new - old;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if we are close to the maximum time and the counter
|
|
|
|
* would wrap if incremented. If that's the case, break out
|
|
|
|
* from the loop as the requested delay time passed.
|
|
|
|
*/
|
|
|
|
if (counter + incr < counter)
|
|
|
|
break;
|
|
|
|
|
|
|
|
counter += incr;
|
|
|
|
old = new;
|
|
|
|
}
|
|
|
|
}
|
2012-02-07 06:47:31 +00:00
|
|
|
|
|
|
|
ulong get_tbclk(void)
|
|
|
|
{
|
2013-02-28 15:03:26 +00:00
|
|
|
return MXS_INCREMENTER_HZ;
|
2012-02-07 06:47:31 +00:00
|
|
|
}
|