blob: 9a6f6c06d8c70bf21da8fe64fc09c007c7ecba40 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Ian Campbell49aeca32014-05-05 11:52:23 +010012#include <asm/io.h>
Andre Przywarae42015b2022-07-03 00:14:24 +010013#include <asm/arch/cpu.h>
Ian Campbell49aeca32014-05-05 11:52:23 +010014#include <asm/arch/timer.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Ian Campbell49aeca32014-05-05 11:52:23 +010016
17DECLARE_GLOBAL_DATA_PTR;
18
19#define TIMER_MODE (0x0 << 7) /* continuous mode */
20#define TIMER_DIV (0x0 << 4) /* pre scale 1 */
21#define TIMER_SRC (0x1 << 2) /* osc24m */
22#define TIMER_RELOAD (0x1 << 1) /* reload internal value */
23#define TIMER_EN (0x1 << 0) /* enable timer */
24
25#define TIMER_CLOCK (24 * 1000 * 1000)
26#define COUNT_TO_USEC(x) ((x) / 24)
27#define USEC_TO_COUNT(x) ((x) * 24)
28#define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ)
29#define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ)
30
31#define TIMER_LOAD_VAL 0xffffffff
32
33#define TIMER_NUM 0 /* we use timer 0 */
34
35/* read the 32-bit timer */
36static ulong read_timer(void)
37{
38 struct sunxi_timer_reg *timers =
39 (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
40 struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
41
42 /*
43 * The hardware timer counts down, therefore we invert to
44 * produce an incrementing timer.
45 */
46 return ~readl(&timer->val);
47}
48
49/* init timer register */
50int timer_init(void)
51{
52 struct sunxi_timer_reg *timers =
53 (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
54 struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
Jesse Taubec3390982022-01-29 10:23:01 -050055
Ian Campbell49aeca32014-05-05 11:52:23 +010056 writel(TIMER_LOAD_VAL, &timer->inter);
57 writel(TIMER_MODE | TIMER_DIV | TIMER_SRC | TIMER_RELOAD | TIMER_EN,
58 &timer->ctl);
59
60 return 0;
61}
62
Patrick Delaunay9858a602018-10-05 11:33:52 +020063static ulong get_timer_masked(void)
Ian Campbell49aeca32014-05-05 11:52:23 +010064{
65 /* current tick value */
66 ulong now = TICKS_TO_HZ(read_timer());
67
Jesse Taubec3390982022-01-29 10:23:01 -050068 if (now >= gd->arch.lastinc) { /* normal (non rollover) */
Ian Campbell49aeca32014-05-05 11:52:23 +010069 gd->arch.tbl += (now - gd->arch.lastinc);
Jesse Taubec3390982022-01-29 10:23:01 -050070 } else {
Ian Campbell49aeca32014-05-05 11:52:23 +010071 /* rollover */
72 gd->arch.tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL)
73 - gd->arch.lastinc) + now;
74 }
75 gd->arch.lastinc = now;
76
77 return gd->arch.tbl;
78}
79
Jesse Taubec3390982022-01-29 10:23:01 -050080/* timer without interrupts */
Patrick Delaunay9858a602018-10-05 11:33:52 +020081ulong get_timer(ulong base)
82{
83 return get_timer_masked() - base;
84}
85
Ian Campbell49aeca32014-05-05 11:52:23 +010086/* delay x useconds */
87void __udelay(unsigned long usec)
88{
89 long tmo = USEC_TO_COUNT(usec);
90 ulong now, last = read_timer();
91
92 while (tmo > 0) {
93 now = read_timer();
94 if (now > last) /* normal (non rollover) */
95 tmo -= now - last;
96 else /* rollover */
97 tmo -= TIMER_LOAD_VAL - last + now;
98 last = now;
99 }
100}
101
102/*
103 * This function is derived from PowerPC code (read timebase as long long).
104 * On ARM it just returns the timer value.
105 */
106unsigned long long get_ticks(void)
107{
108 return get_timer(0);
109}
110
111/*
112 * This function is derived from PowerPC code (timebase clock frequency).
113 * On ARM it returns the number of timer ticks per second.
114 */
115ulong get_tbclk(void)
116{
117 return CONFIG_SYS_HZ;
118}