Home | History | Annotate | Download | only in s5p-common
      1 // SPDX-License-Identifier: GPL-2.0+
      2 /*
      3  * Copyright (C) 2009 Samsung Electronics
      4  * Heungjun Kim <riverful.kim (at) samsung.com>
      5  * Inki Dae <inki.dae (at) samsung.com>
      6  * Minkyu Kang <mk7.kang (at) samsung.com>
      7  */
      8 
      9 #include <common.h>
     10 #include <div64.h>
     11 #include <asm/io.h>
     12 #include <asm/arch/pwm.h>
     13 #include <asm/arch/clk.h>
     14 
     15 /* Use the old PWM interface for now */
     16 #undef CONFIG_DM_PWM
     17 #include <pwm.h>
     18 
     19 DECLARE_GLOBAL_DATA_PTR;
     20 
     21 unsigned long get_current_tick(void);
     22 
     23 /* macro to read the 16 bit timer */
     24 static inline struct s5p_timer *s5p_get_base_timer(void)
     25 {
     26 	return (struct s5p_timer *)samsung_get_base_timer();
     27 }
     28 
     29 /**
     30  * Read the countdown timer.
     31  *
     32  * This operates at 1MHz and counts downwards. It will wrap about every
     33  * hour (2^32 microseconds).
     34  *
     35  * @return current value of timer
     36  */
     37 static unsigned long timer_get_us_down(void)
     38 {
     39 	struct s5p_timer *const timer = s5p_get_base_timer();
     40 
     41 	return readl(&timer->tcnto4);
     42 }
     43 
     44 int timer_init(void)
     45 {
     46 	/* PWM Timer 4 */
     47 	pwm_init(4, MUX_DIV_4, 0);
     48 	pwm_config(4, 100000, 100000);
     49 	pwm_enable(4);
     50 
     51 	/* Use this as the current monotonic time in us */
     52 	gd->arch.timer_reset_value = 0;
     53 
     54 	/* Use this as the last timer value we saw */
     55 	gd->arch.lastinc = timer_get_us_down();
     56 	reset_timer_masked();
     57 
     58 	return 0;
     59 }
     60 
     61 /*
     62  * timer without interrupts
     63  */
     64 unsigned long get_timer(unsigned long base)
     65 {
     66 	unsigned long long time_ms;
     67 
     68 	ulong now = timer_get_us_down();
     69 
     70 	/*
     71 	 * Increment the time by the amount elapsed since the last read.
     72 	 * The timer may have wrapped around, but it makes no difference to
     73 	 * our arithmetic here.
     74 	 */
     75 	gd->arch.timer_reset_value += gd->arch.lastinc - now;
     76 	gd->arch.lastinc = now;
     77 
     78 	/* Divide by 1000 to convert from us to ms */
     79 	time_ms = gd->arch.timer_reset_value;
     80 	do_div(time_ms, 1000);
     81 	return time_ms - base;
     82 }
     83 
     84 unsigned long __attribute__((no_instrument_function)) timer_get_us(void)
     85 {
     86 	static unsigned long base_time_us;
     87 
     88 	struct s5p_timer *const timer =
     89 		(struct s5p_timer *)samsung_get_base_timer();
     90 	unsigned long now_downward_us = readl(&timer->tcnto4);
     91 
     92 	if (!base_time_us)
     93 		base_time_us = now_downward_us;
     94 
     95 	/* Note that this timer counts downward. */
     96 	return base_time_us - now_downward_us;
     97 }
     98 
     99 /* delay x useconds */
    100 void __udelay(unsigned long usec)
    101 {
    102 	unsigned long count_value;
    103 
    104 	count_value = timer_get_us_down();
    105 	while ((int)(count_value - timer_get_us_down()) < (int)usec)
    106 		;
    107 }
    108 
    109 void reset_timer_masked(void)
    110 {
    111 	struct s5p_timer *const timer = s5p_get_base_timer();
    112 
    113 	/* reset time */
    114 	gd->arch.lastinc = readl(&timer->tcnto4);
    115 	gd->arch.tbl = 0;
    116 }
    117 
    118 /*
    119  * This function is derived from PowerPC code (read timebase as long long).
    120  * On ARM it just returns the timer value.
    121  */
    122 unsigned long long get_ticks(void)
    123 {
    124 	return get_timer(0);
    125 }
    126 
    127 /*
    128  * This function is derived from PowerPC code (timebase clock frequency).
    129  * On ARM it returns the number of timer ticks per second.
    130  */
    131 unsigned long get_tbclk(void)
    132 {
    133 	return CONFIG_SYS_HZ;
    134 }
    135