blob: 41fee2ef499374edeb71ca50248a92c7177e5e4c [file] [log] [blame]
/*
* Copyright (C) 2018 Synaptics Incorporated. All rights reserved.
* (C) Copyright 2010 Marvell Semiconductor <www.marvell.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS," AND
* SYNAPTICS EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE, AND ANY WARRANTIES OF NON-INFRINGEMENT OF ANY
* INTELLECTUAL PROPERTY RIGHTS. IN NO EVENT SHALL SYNAPTICS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, PUNITIVE, OR
* CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION WITH THE USE
* OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED AND
* BASED ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. IF A TRIBUNAL OF
* COMPETENT JURISDICTION DOES NOT PERMIT THE DISCLAIMER OF DIRECT
* DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS' TOTAL CUMULATIVE LIABILITY
* TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S. DOLLARS.
*/
#include "apb_timer.h"
#include "timer.h"
unsigned int timer_clk_freq;
static inline unsigned long read_timer(void)
{
return TIMERx_GetCurrentValue(GALOIS_UBOOT_TIMER) \
/ (timer_clk_freq/ 1000);
}
static unsigned long timestamp;
static unsigned long lastdec;
void reset_timer_masked (void)
{
/* reset time */
lastdec = read_timer(); /* capure current decrementer value time */
timestamp = 0; /* start "advancing" time stamp from 0 */
}
unsigned long get_timer_masked (void)
{
unsigned long now = read_timer();
if (lastdec >= now) {
/* normal mode */
timestamp += lastdec - now;
} else {
/* we have an overflow ... */
timestamp += lastdec +
(TIMER_LOAD_VAL / (timer_clk_freq/ 1000)) - now;
}
lastdec = now;
return timestamp;
}
/*
* timer without interrupts
*/
void reset_timer (void)
{
reset_timer_masked ();
}
unsigned long get_timer (unsigned long base)
{
return get_timer_masked () - base;
}
void set_timer (unsigned long t)
{
timestamp = t;
}
static inline unsigned long uboot_cntr_val(void)
{
return TIMERx_GetCurrentValue(GALOIS_UBOOT_TIMER);
}
void __udelay(unsigned long usec)
{
unsigned int current;
unsigned long delayticks;
current = uboot_cntr_val();
delayticks = (usec * (timer_clk_freq/ 1000000));
if (current < delayticks) {
delayticks -= current;
while (uboot_cntr_val() < current)
;
while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
;
} else {
while (uboot_cntr_val() > (current - delayticks))
;
}
}
#ifndef CONFIG_WD_PERIOD
# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default*/
#endif
void udelay(unsigned long usec)
{
unsigned long kv;
do {
kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
__udelay (kv);
usec -= kv;
} while(usec);
}
void delay_us(unsigned int us)
{
udelay(us);
}
void mdelay(unsigned int ms)
{
const unsigned int step_ms = 1000000;
const unsigned int step_us = 1000 * step_ms;
while (ms > step_ms) {
udelay(step_us);
ms -= step_ms;
}
if (ms)
udelay(1000 * ms);
}
void delay_ms(unsigned int ms)
{
mdelay(ms);
}
#define INT_UNMASK_BIT 0
#define INT_MASK_BIT 1
#define INT_ENABLE_BIT 1
#define INT_DISABLE_BIT 0
int timer_init (void)
{
/* call low level code to init timer, TODO: don't include low level code like this */
TIMERx_SetControlReg(GALOIS_UBOOT_TIMER, INT_MASK_BIT, INT_DISABLE_BIT, TIMER_USR_DEFINED_MODE);
TIMERx_SetLoadCount(GALOIS_UBOOT_TIMER, TIMER_LOAD_VAL);
/* INT_ENABLE_BIT is mis-name of ENABLE_BIT */
TIMERx_SetIntEn(GALOIS_UBOOT_TIMER, INT_ENABLE_BIT);
/* init the timestamp and lastdec value */
reset_timer_masked();
#if 0 //(ASIC_PLATFORM)
timer_clk_freq = 1000000*GaloisGetFrequency(SOC_FREQ_CFG);
printf("timer_clk_freq = 0x%x \n",timer_clk_freq);
#else
timer_clk_freq = CONFIG_SYS_HZ;
#endif
//printf("timer_clk_freq = 0x%08x \n",timer_clk_freq);
return 0;
}
int timer_stop(void)
{
TIMERx_SetIntEn(GALOIS_UBOOT_TIMER, INT_DISABLE_BIT);
return 0;
}