blob: 8a98f9dedcce4e0706534a6c9a2248a04570b45f [file] [log] [blame]
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001From fff63cfd7d9654dc9ed0c106f29d3a7ad01b0502 Mon Sep 17 00:00:00 2001
2From: Rui Miguel Silva <rui.silva@linaro.org>
3Date: Wed, 18 Dec 2019 21:52:34 +0000
4Subject: [PATCH 1/2] armv7: adding generic timer access through MMIO
5
6Upstream-Status: Pending [Not submitted to upstream yet]
7Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
8
9This driver enables the ARMv7 generic timer.
10
11The access to the timer registers is through memory mapping (MMIO).
12
13This driver can be used by u-boot to access to the timer through MMIO
14when arch_timer is not available in the core (access using system
15instructions not possible), for example, in case of Cortex-A5.
16
17This driver configures and enables the generic timer at
18the u-boot initcall level (timer_init) before u-boot relocation.
19
20Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
21Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
22
23%% original patch: 0001-armv7-adding-generic-timer-access-through-MMIO.patch
24---
25 arch/arm/cpu/armv7/Makefile | 1 +
26 arch/arm/cpu/armv7/mmio_timer.c | 75 +++++++++++++++++++++++++++++++++
27 scripts/config_whitelist.txt | 1 +
28 3 files changed, 77 insertions(+)
29 create mode 100644 arch/arm/cpu/armv7/mmio_timer.c
30
31diff --git a/arch/arm/cpu/armv7/Makefile b/arch/arm/cpu/armv7/Makefile
32index bfbd85ae64..1a0a24e531 100644
33--- a/arch/arm/cpu/armv7/Makefile
34+++ b/arch/arm/cpu/armv7/Makefile
35@@ -28,6 +28,7 @@ obj-$(CONFIG_ARMV7_PSCI) += psci.o psci-common.o
36 obj-$(CONFIG_IPROC) += iproc-common/
37 obj-$(CONFIG_KONA) += kona-common/
38 obj-$(CONFIG_SYS_ARCH_TIMER) += arch_timer.o
39+obj-$(CONFIG_SYS_MMIO_TIMER) += mmio_timer.o
40
41 ifneq (,$(filter s5pc1xx exynos,$(SOC)))
42 obj-y += s5p-common/
43diff --git a/arch/arm/cpu/armv7/mmio_timer.c b/arch/arm/cpu/armv7/mmio_timer.c
44new file mode 100644
45index 0000000000..edd806e06e
46--- /dev/null
47+++ b/arch/arm/cpu/armv7/mmio_timer.c
48@@ -0,0 +1,75 @@
49+// SPDX-License-Identifier: GPL-2.0+
50+/*
51+ * Copyright (c) 2019, Arm Limited. All rights reserved.
52+ *
53+ */
54+
55+#include <common.h>
56+#include <asm/io.h>
57+#include <div64.h>
58+#include <bootstage.h>
59+#include <asm/global_data.h>
60+
61+DECLARE_GLOBAL_DATA_PTR;
62+
63+#define CNTCTLBASE 0x1a020000UL
64+#define CNTREADBASE 0x1a030000UL
65+#define CNTEN (1 << 0)
66+#define CNTFCREQ (1 << 8)
67+
68+static inline uint32_t mmio_read32(uintptr_t addr)
69+{
70+ return *(volatile uint32_t*)addr;
71+}
72+
73+static inline void mmio_write32(uintptr_t addr, uint32_t data)
74+{
75+ *(volatile uint32_t*)addr = data;
76+}
77+
78+int timer_init(void)
79+{
80+ /* calculate the frequency in ms */
81+ gd->arch.timer_rate_hz = COUNTER_FREQUENCY / CONFIG_SYS_HZ;
82+
83+ /* configure CNTFID0 register: set the base frequency */
84+ mmio_write32(CNTCTLBASE + 0x20, COUNTER_FREQUENCY);
85+
86+ /*
87+ * configure CNTCR register:
88+ * enable the generic counter and;
89+ * select the first frequency entry
90+ */
91+ mmio_write32(CNTCTLBASE, CNTFCREQ | CNTEN);
92+
93+ return 0;
94+}
95+
96+unsigned long long get_ticks(void)
97+{
98+ return (((u64)(mmio_read32(CNTREADBASE + 0x4)) << 32) |
99+ mmio_read32(CNTREADBASE));
100+}
101+
102+ulong get_timer(ulong base)
103+{
104+ return lldiv(get_ticks(), gd->arch.timer_rate_hz) - base;
105+}
106+
107+void __udelay(unsigned long usec)
108+{
109+ unsigned long endtime;
110+
111+ endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
112+ 1000UL);
113+
114+ endtime += get_ticks();
115+
116+ while (get_ticks() < endtime)
117+ ;
118+}
119+
120+ulong get_tbclk(void)
121+{
122+ return gd->arch.timer_rate_hz;
123+}
124diff --git a/scripts/config_whitelist.txt b/scripts/config_whitelist.txt
125index a6bc234f51..8d5cd67ace 100644
126--- a/scripts/config_whitelist.txt
127+++ b/scripts/config_whitelist.txt
128@@ -1524,6 +1524,7 @@ CONFIG_SYS_MMC_U_BOOT_DST
129 CONFIG_SYS_MMC_U_BOOT_OFFS
130 CONFIG_SYS_MMC_U_BOOT_SIZE
131 CONFIG_SYS_MMC_U_BOOT_START
132+CONFIG_SYS_MMIO_TIMER
133 CONFIG_SYS_MONITOR_BASE
134 CONFIG_SYS_MONITOR_LEN
135 CONFIG_SYS_MONITOR_SEC
136--
1372.30.2
138