timer.c 2.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
3
4
5
6
7
8
9
/*
 * Copyright (C) 2009 Samsung Electronics
 * Heungjun Kim <riverful.kim@samsung.com>
 * Inki Dae <inki.dae@samsung.com>
 * Minkyu Kang <mk7.kang@samsung.com>
 */

#include <common.h>
10
#include <div64.h>
11
#include <init.h>
12
#include <time.h>
13
#include <asm/global_data.h>
14
15
16
#include <asm/io.h>
#include <asm/arch/pwm.h>
#include <asm/arch/clk.h>
17
#include <linux/delay.h>
18

19
DECLARE_GLOBAL_DATA_PTR;
20

21
unsigned long get_current_tick(void);
22
static void reset_timer_masked(void);
23

24
/* macro to read the 16 bit timer */
25
static inline struct s5p_timer *s5p_get_base_timer(void)
26
{
27
	return (struct s5p_timer *)samsung_get_base_timer();
28
29
}

30
31
32
33
34
35
/**
 * Read the countdown timer.
 *
 * This operates at 1MHz and counts downwards. It will wrap about every
 * hour (2^32 microseconds).
 *
36
 * Return: current value of timer
37
38
39
40
41
42
43
44
 */
static unsigned long timer_get_us_down(void)
{
	struct s5p_timer *const timer = s5p_get_base_timer();

	return readl(&timer->tcnto4);
}

45
46
int timer_init(void)
{
Minkyu Kang's avatar
Minkyu Kang committed
47
	/* PWM Timer 4 */
Tom Rini's avatar
Tom Rini committed
48
49
50
	s5p_pwm_init(4, MUX_DIV_4, 0);
	s5p_pwm_config(4, 100000, 100000);
	s5p_pwm_enable(4);
51

52
53
54
55
56
	/* Use this as the current monotonic time in us */
	gd->arch.timer_reset_value = 0;

	/* Use this as the last timer value we saw */
	gd->arch.lastinc = timer_get_us_down();
57
58
	reset_timer_masked();

59
60
61
62
63
64
65
66
	return 0;
}

/*
 * timer without interrupts
 */
unsigned long get_timer(unsigned long base)
{
67
68
	unsigned long long time_ms;

69
70
71
72
73
74
75
76
77
78
79
	ulong now = timer_get_us_down();

	/*
	 * Increment the time by the amount elapsed since the last read.
	 * The timer may have wrapped around, but it makes no difference to
	 * our arithmetic here.
	 */
	gd->arch.timer_reset_value += gd->arch.lastinc - now;
	gd->arch.lastinc = now;

	/* Divide by 1000 to convert from us to ms */
80
81
82
	time_ms = gd->arch.timer_reset_value;
	do_div(time_ms, 1000);
	return time_ms - base;
83
84
}

Simon Glass's avatar
Simon Glass committed
85
unsigned long notrace timer_get_us(void)
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
	static unsigned long base_time_us;

	struct s5p_timer *const timer =
		(struct s5p_timer *)samsung_get_base_timer();
	unsigned long now_downward_us = readl(&timer->tcnto4);

	if (!base_time_us)
		base_time_us = now_downward_us;

	/* Note that this timer counts downward. */
	return base_time_us - now_downward_us;
}

100
/* delay x useconds */
101
void __udelay(unsigned long usec)
102
{
103
104
105
106
107
	unsigned long count_value;

	count_value = timer_get_us_down();
	while ((int)(count_value - timer_get_us_down()) < (int)usec)
		;
108
109
}

110
static void reset_timer_masked(void)
111
{
112
	struct s5p_timer *const timer = s5p_get_base_timer();
113
114

	/* reset time */
115
	gd->arch.lastinc = readl(&timer->tcnto4);
116
	gd->arch.tbl = 0;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
}

/*
 * This function is derived from PowerPC code (read timebase as long long).
 * On ARM it just returns the timer value.
 */
unsigned long long get_ticks(void)
{
	return get_timer(0);
}

/*
 * This function is derived from PowerPC code (timebase clock frequency).
 * On ARM it returns the number of timer ticks per second.
 */
unsigned long get_tbclk(void)
{
	return CONFIG_SYS_HZ;
}