2010-01-20 17:19:32 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2007
|
|
|
|
* Sascha Hauer, Pengutronix
|
|
|
|
*
|
|
|
|
* (C) Copyright 2009 Freescale Semiconductor, Inc.
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2010-01-20 17:19:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/io.h>
|
2012-02-06 11:52:36 +00:00
|
|
|
#include <div64.h>
|
2010-01-20 17:19:32 +00:00
|
|
|
#include <asm/arch/imx-regs.h>
|
2012-09-27 10:19:58 +00:00
|
|
|
#include <asm/arch/clock.h>
|
2014-10-30 10:20:55 +00:00
|
|
|
#include <asm/arch/sys_proto.h>
|
2010-01-20 17:19:32 +00:00
|
|
|
|
|
|
|
/* General purpose timers registers */
|
|
|
|
struct mxc_gpt {
|
|
|
|
unsigned int control;
|
|
|
|
unsigned int prescaler;
|
|
|
|
unsigned int status;
|
|
|
|
unsigned int nouse[6];
|
|
|
|
unsigned int counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
|
|
|
|
|
|
|
|
/* General purpose timers bitfields */
|
2011-11-25 00:18:01 +00:00
|
|
|
#define GPTCR_SWR (1 << 15) /* Software reset */
|
2014-10-30 10:20:55 +00:00
|
|
|
#define GPTCR_24MEN (1 << 10) /* Enable 24MHz clock input */
|
2011-11-25 00:18:01 +00:00
|
|
|
#define GPTCR_FRR (1 << 9) /* Freerun / restart */
|
2014-10-30 10:20:55 +00:00
|
|
|
#define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source 32khz */
|
|
|
|
#define GPTCR_CLKSOURCE_OSC (5 << 6) /* Clock source OSC */
|
|
|
|
#define GPTCR_CLKSOURCE_PRE (1 << 6) /* Clock source PRECLK */
|
|
|
|
#define GPTCR_CLKSOURCE_MASK (0x7 << 6)
|
2011-11-25 00:18:01 +00:00
|
|
|
#define GPTCR_TEN 1 /* Timer enable */
|
2010-01-20 17:19:32 +00:00
|
|
|
|
2014-10-30 10:20:55 +00:00
|
|
|
#define GPTPR_PRESCALER24M_SHIFT 12
|
|
|
|
#define GPTPR_PRESCALER24M_MASK (0xF << GPTPR_PRESCALER24M_SHIFT)
|
|
|
|
|
2011-01-21 20:16:15 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2014-10-30 10:20:55 +00:00
|
|
|
static inline int gpt_has_clk_source_osc(void)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_MX6)
|
2016-05-23 10:35:56 +00:00
|
|
|
if (((is_mx6dq()) && (soc_rev() > CHIP_REV_1_0)) ||
|
2016-08-11 06:02:42 +00:00
|
|
|
is_mx6dqp() || is_mx6sdl() || is_mx6sx() || is_mx6ul() ||
|
2016-12-11 11:24:23 +00:00
|
|
|
is_mx6ull() || is_mx6sll())
|
2014-10-30 10:20:55 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ulong gpt_get_clk(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_MXC_GPT_HCLK
|
|
|
|
if (gpt_has_clk_source_osc())
|
|
|
|
return MXC_HCLK >> 3;
|
|
|
|
else
|
|
|
|
return mxc_get_clock(MXC_IPG_PERCLK);
|
|
|
|
#else
|
|
|
|
return MXC_CLK32;
|
|
|
|
#endif
|
|
|
|
}
|
2012-02-06 11:52:36 +00:00
|
|
|
|
2010-01-20 17:19:32 +00:00
|
|
|
int timer_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* setup GP Timer 1 */
|
|
|
|
__raw_writel(GPTCR_SWR, &cur_gpt->control);
|
|
|
|
|
|
|
|
/* We have no udelay by now */
|
|
|
|
for (i = 0; i < 100; i++)
|
|
|
|
__raw_writel(0, &cur_gpt->control);
|
|
|
|
|
|
|
|
i = __raw_readl(&cur_gpt->control);
|
2014-10-30 10:20:55 +00:00
|
|
|
i &= ~GPTCR_CLKSOURCE_MASK;
|
|
|
|
|
|
|
|
#ifdef CONFIG_MXC_GPT_HCLK
|
|
|
|
if (gpt_has_clk_source_osc()) {
|
|
|
|
i |= GPTCR_CLKSOURCE_OSC | GPTCR_TEN;
|
|
|
|
|
2016-12-11 11:24:23 +00:00
|
|
|
/*
|
|
|
|
* For DL/S, SX, UL, ULL, SLL set 24Mhz OSC
|
|
|
|
* Enable bit and prescaler
|
|
|
|
*/
|
|
|
|
if (is_mx6sdl() || is_mx6sx() || is_mx6ul() || is_mx6ull() ||
|
|
|
|
is_mx6sll()) {
|
2014-10-30 10:20:55 +00:00
|
|
|
i |= GPTCR_24MEN;
|
|
|
|
|
|
|
|
/* Produce 3Mhz clock */
|
|
|
|
__raw_writel((7 << GPTPR_PRESCALER24M_SHIFT),
|
|
|
|
&cur_gpt->prescaler);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i |= GPTCR_CLKSOURCE_PRE | GPTCR_TEN;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
__raw_writel(0, &cur_gpt->prescaler); /* 32Khz */
|
|
|
|
i |= GPTCR_CLKSOURCE_32 | GPTCR_TEN;
|
|
|
|
#endif
|
|
|
|
__raw_writel(i, &cur_gpt->control);
|
2010-01-20 17:19:32 +00:00
|
|
|
|
2013-03-04 04:16:02 +00:00
|
|
|
gd->arch.tbl = __raw_readl(&cur_gpt->counter);
|
|
|
|
gd->arch.tbu = 0;
|
2011-07-15 02:21:14 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-01-20 17:19:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 07:40:58 +00:00
|
|
|
unsigned long timer_read_counter(void)
|
2010-01-20 17:19:32 +00:00
|
|
|
{
|
2015-08-26 07:40:58 +00:00
|
|
|
return __raw_readl(&cur_gpt->counter); /* current tick value */
|
2012-02-06 11:52:36 +00:00
|
|
|
}
|
2010-01-20 17:19:32 +00:00
|
|
|
|
2012-02-06 11:52:36 +00:00
|
|
|
/*
|
|
|
|
* This function is derived from PowerPC code (timebase clock frequency).
|
|
|
|
* On ARM it returns the number of timer ticks per second.
|
|
|
|
*/
|
|
|
|
ulong get_tbclk(void)
|
|
|
|
{
|
2014-10-30 10:20:55 +00:00
|
|
|
return gpt_get_clk();
|
2010-01-20 17:19:32 +00:00
|
|
|
}
|
2016-08-25 17:03:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is intended for SHORT delays only.
|
|
|
|
* It will overflow at around 10 seconds @ 400MHz,
|
|
|
|
* or 20 seconds @ 200MHz.
|
|
|
|
*/
|
|
|
|
unsigned long usec2ticks(unsigned long _usec)
|
|
|
|
{
|
|
|
|
unsigned long long usec = _usec;
|
|
|
|
|
|
|
|
usec *= get_tbclk();
|
|
|
|
usec += 999999;
|
|
|
|
do_div(usec, 1000000);
|
|
|
|
|
|
|
|
return usec;
|
|
|
|
}
|