blob: 4f6a66d8039ded186dfa5e1cb457c90b62209c8c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbell49aeca32014-05-05 11:52:23 +01002/*
3 * (C) Copyright 2007-2011
4 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5 * Tom Cubie <tangliang@allwinnertech.com>
Ian Campbell49aeca32014-05-05 11:52:23 +01006 */
7
8#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070010#include <time.h>
Ian Campbell49aeca32014-05-05 11:52:23 +010011#include <asm/io.h>
12#include <asm/arch/timer.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16#define TIMER_MODE (0x0 << 7) /* continuous mode */
17#define TIMER_DIV (0x0 << 4) /* pre scale 1 */
18#define TIMER_SRC (0x1 << 2) /* osc24m */
19#define TIMER_RELOAD (0x1 << 1) /* reload internal value */
20#define TIMER_EN (0x1 << 0) /* enable timer */
21
22#define TIMER_CLOCK (24 * 1000 * 1000)
23#define COUNT_TO_USEC(x) ((x) / 24)
24#define USEC_TO_COUNT(x) ((x) * 24)
25#define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ)
26#define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ)
27
28#define TIMER_LOAD_VAL 0xffffffff
29
30#define TIMER_NUM 0 /* we use timer 0 */
31
32/* read the 32-bit timer */
33static ulong read_timer(void)
34{
35 struct sunxi_timer_reg *timers =
36 (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
37 struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
38
39 /*
40 * The hardware timer counts down, therefore we invert to
41 * produce an incrementing timer.
42 */
43 return ~readl(&timer->val);
44}
45
46/* init timer register */
47int timer_init(void)
48{
49 struct sunxi_timer_reg *timers =
50 (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
51 struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
52 writel(TIMER_LOAD_VAL, &timer->inter);
53 writel(TIMER_MODE | TIMER_DIV | TIMER_SRC | TIMER_RELOAD | TIMER_EN,
54 &timer->ctl);
55
56 return 0;
57}
58
59/* timer without interrupts */
Patrick Delaunay9858a602018-10-05 11:33:52 +020060static ulong get_timer_masked(void)
Ian Campbell49aeca32014-05-05 11:52:23 +010061{
62 /* current tick value */
63 ulong now = TICKS_TO_HZ(read_timer());
64
65 if (now >= gd->arch.lastinc) /* normal (non rollover) */
66 gd->arch.tbl += (now - gd->arch.lastinc);
67 else {
68 /* rollover */
69 gd->arch.tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL)
70 - gd->arch.lastinc) + now;
71 }
72 gd->arch.lastinc = now;
73
74 return gd->arch.tbl;
75}
76
Patrick Delaunay9858a602018-10-05 11:33:52 +020077ulong get_timer(ulong base)
78{
79 return get_timer_masked() - base;
80}
81
Ian Campbell49aeca32014-05-05 11:52:23 +010082/* delay x useconds */
83void __udelay(unsigned long usec)
84{
85 long tmo = USEC_TO_COUNT(usec);
86 ulong now, last = read_timer();
87
88 while (tmo > 0) {
89 now = read_timer();
90 if (now > last) /* normal (non rollover) */
91 tmo -= now - last;
92 else /* rollover */
93 tmo -= TIMER_LOAD_VAL - last + now;
94 last = now;
95 }
96}
97
98/*
99 * This function is derived from PowerPC code (read timebase as long long).
100 * On ARM it just returns the timer value.
101 */
102unsigned long long get_ticks(void)
103{
104 return get_timer(0);
105}
106
107/*
108 * This function is derived from PowerPC code (timebase clock frequency).
109 * On ARM it returns the number of timer ticks per second.
110 */
111ulong get_tbclk(void)
112{
113 return CONFIG_SYS_HZ;
114}