blob: 1d28631a21079dbbaa349b7870b5e12489dd6323 [file] [log] [blame]
Patrick Williams864cc432023-02-09 14:54:44 -06001From 2bb9fb8414b8ad35ed5fc6c91a34c21cef285a01 Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: 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
Patrick Williams92b42cb2022-09-03 06:53:57 -050023
24Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
Brad Bishopbec4ebc2022-08-03 09:55:16 -040025---
26 arch/arm/cpu/armv7/Makefile | 1 +
27 arch/arm/cpu/armv7/mmio_timer.c | 75 +++++++++++++++++++++++++++++++++
28 scripts/config_whitelist.txt | 1 +
29 3 files changed, 77 insertions(+)
30 create mode 100644 arch/arm/cpu/armv7/mmio_timer.c
31
32diff --git a/arch/arm/cpu/armv7/Makefile b/arch/arm/cpu/armv7/Makefile
Patrick Williams92b42cb2022-09-03 06:53:57 -050033index bfbd85ae64ef..1a0a24e53110 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040034--- a/arch/arm/cpu/armv7/Makefile
35+++ b/arch/arm/cpu/armv7/Makefile
36@@ -28,6 +28,7 @@ obj-$(CONFIG_ARMV7_PSCI) += psci.o psci-common.o
37 obj-$(CONFIG_IPROC) += iproc-common/
38 obj-$(CONFIG_KONA) += kona-common/
39 obj-$(CONFIG_SYS_ARCH_TIMER) += arch_timer.o
40+obj-$(CONFIG_SYS_MMIO_TIMER) += mmio_timer.o
41
42 ifneq (,$(filter s5pc1xx exynos,$(SOC)))
43 obj-y += s5p-common/
44diff --git a/arch/arm/cpu/armv7/mmio_timer.c b/arch/arm/cpu/armv7/mmio_timer.c
45new file mode 100644
Patrick Williams92b42cb2022-09-03 06:53:57 -050046index 000000000000..edd806e06e42
Brad Bishopbec4ebc2022-08-03 09:55:16 -040047--- /dev/null
48+++ b/arch/arm/cpu/armv7/mmio_timer.c
49@@ -0,0 +1,75 @@
50+// SPDX-License-Identifier: GPL-2.0+
51+/*
52+ * Copyright (c) 2019, Arm Limited. All rights reserved.
53+ *
54+ */
55+
56+#include <common.h>
57+#include <asm/io.h>
58+#include <div64.h>
59+#include <bootstage.h>
60+#include <asm/global_data.h>
61+
62+DECLARE_GLOBAL_DATA_PTR;
63+
64+#define CNTCTLBASE 0x1a020000UL
65+#define CNTREADBASE 0x1a030000UL
66+#define CNTEN (1 << 0)
67+#define CNTFCREQ (1 << 8)
68+
69+static inline uint32_t mmio_read32(uintptr_t addr)
70+{
71+ return *(volatile uint32_t*)addr;
72+}
73+
74+static inline void mmio_write32(uintptr_t addr, uint32_t data)
75+{
76+ *(volatile uint32_t*)addr = data;
77+}
78+
79+int timer_init(void)
80+{
81+ /* calculate the frequency in ms */
82+ gd->arch.timer_rate_hz = COUNTER_FREQUENCY / CONFIG_SYS_HZ;
83+
84+ /* configure CNTFID0 register: set the base frequency */
85+ mmio_write32(CNTCTLBASE + 0x20, COUNTER_FREQUENCY);
86+
87+ /*
88+ * configure CNTCR register:
89+ * enable the generic counter and;
90+ * select the first frequency entry
91+ */
92+ mmio_write32(CNTCTLBASE, CNTFCREQ | CNTEN);
93+
94+ return 0;
95+}
96+
97+unsigned long long get_ticks(void)
98+{
99+ return (((u64)(mmio_read32(CNTREADBASE + 0x4)) << 32) |
100+ mmio_read32(CNTREADBASE));
101+}
102+
103+ulong get_timer(ulong base)
104+{
105+ return lldiv(get_ticks(), gd->arch.timer_rate_hz) - base;
106+}
107+
108+void __udelay(unsigned long usec)
109+{
110+ unsigned long endtime;
111+
112+ endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
113+ 1000UL);
114+
115+ endtime += get_ticks();
116+
117+ while (get_ticks() < endtime)
118+ ;
119+}
120+
121+ulong get_tbclk(void)
122+{
123+ return gd->arch.timer_rate_hz;
124+}
125diff --git a/scripts/config_whitelist.txt b/scripts/config_whitelist.txt
Patrick Williams864cc432023-02-09 14:54:44 -0600126index ea71f9d23449..1496d9b88233 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400127--- a/scripts/config_whitelist.txt
128+++ b/scripts/config_whitelist.txt
Patrick Williams864cc432023-02-09 14:54:44 -0600129@@ -610,6 +610,7 @@ CONFIG_SYS_MMC_U_BOOT_DST
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400130 CONFIG_SYS_MMC_U_BOOT_OFFS
131 CONFIG_SYS_MMC_U_BOOT_SIZE
132 CONFIG_SYS_MMC_U_BOOT_START
133+CONFIG_SYS_MMIO_TIMER
Patrick Williams92b42cb2022-09-03 06:53:57 -0500134 CONFIG_SYS_MOR_VAL
Patrick Williams864cc432023-02-09 14:54:44 -0600135 CONFIG_SYS_MRAM_BASE
136 CONFIG_SYS_NAND_AMASK
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400137--
Patrick Williams864cc432023-02-09 14:54:44 -06001382.39.1
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400139