18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __ASMARM_ARCH_TIMER_H
38c2ecf20Sopenharmony_ci#define __ASMARM_ARCH_TIMER_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <asm/barrier.h>
68c2ecf20Sopenharmony_ci#include <asm/errno.h>
78c2ecf20Sopenharmony_ci#include <asm/hwcap.h>
88c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/types.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <clocksource/arm_arch_timer.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM_ARCH_TIMER
158c2ecf20Sopenharmony_ci/* 32bit ARM doesn't know anything about timer errata... */
168c2ecf20Sopenharmony_ci#define has_erratum_handler(h)		(false)
178c2ecf20Sopenharmony_ci#define erratum_handler(h)		(arch_timer_##h)
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciint arch_timer_arch_init(void);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * These register accessors are marked inline so the compiler can
238c2ecf20Sopenharmony_ci * nicely work out which register we want, and chuck away the rest of
248c2ecf20Sopenharmony_ci * the code. At least it does so with a recent GCC (4.6.3).
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_cistatic __always_inline
278c2ecf20Sopenharmony_civoid arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	if (access == ARCH_TIMER_PHYS_ACCESS) {
308c2ecf20Sopenharmony_ci		switch (reg) {
318c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
328c2ecf20Sopenharmony_ci			asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val));
338c2ecf20Sopenharmony_ci			break;
348c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
358c2ecf20Sopenharmony_ci			asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val));
368c2ecf20Sopenharmony_ci			break;
378c2ecf20Sopenharmony_ci		}
388c2ecf20Sopenharmony_ci	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
398c2ecf20Sopenharmony_ci		switch (reg) {
408c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
418c2ecf20Sopenharmony_ci			asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" (val));
428c2ecf20Sopenharmony_ci			break;
438c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
448c2ecf20Sopenharmony_ci			asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" (val));
458c2ecf20Sopenharmony_ci			break;
468c2ecf20Sopenharmony_ci		}
478c2ecf20Sopenharmony_ci	}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	isb();
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic __always_inline
538c2ecf20Sopenharmony_ciu32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	u32 val = 0;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (access == ARCH_TIMER_PHYS_ACCESS) {
588c2ecf20Sopenharmony_ci		switch (reg) {
598c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
608c2ecf20Sopenharmony_ci			asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
618c2ecf20Sopenharmony_ci			break;
628c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
638c2ecf20Sopenharmony_ci			asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val));
648c2ecf20Sopenharmony_ci			break;
658c2ecf20Sopenharmony_ci		}
668c2ecf20Sopenharmony_ci	} else if (access == ARCH_TIMER_VIRT_ACCESS) {
678c2ecf20Sopenharmony_ci		switch (reg) {
688c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_CTRL:
698c2ecf20Sopenharmony_ci			asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val));
708c2ecf20Sopenharmony_ci			break;
718c2ecf20Sopenharmony_ci		case ARCH_TIMER_REG_TVAL:
728c2ecf20Sopenharmony_ci			asm volatile("mrc p15, 0, %0, c14, c3, 0" : "=r" (val));
738c2ecf20Sopenharmony_ci			break;
748c2ecf20Sopenharmony_ci		}
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return val;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic inline u32 arch_timer_get_cntfrq(void)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	u32 val;
838c2ecf20Sopenharmony_ci	asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val));
848c2ecf20Sopenharmony_ci	return val;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic inline u64 __arch_counter_get_cntpct(void)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	u64 cval;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	isb();
928c2ecf20Sopenharmony_ci	asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
938c2ecf20Sopenharmony_ci	return cval;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic inline u64 __arch_counter_get_cntpct_stable(void)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	return __arch_counter_get_cntpct();
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic inline u64 __arch_counter_get_cntvct(void)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	u64 cval;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	isb();
1068c2ecf20Sopenharmony_ci	asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval));
1078c2ecf20Sopenharmony_ci	return cval;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic inline u64 __arch_counter_get_cntvct_stable(void)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	return __arch_counter_get_cntvct();
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic inline u32 arch_timer_get_cntkctl(void)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	u32 cntkctl;
1188c2ecf20Sopenharmony_ci	asm volatile("mrc p15, 0, %0, c14, c1, 0" : "=r" (cntkctl));
1198c2ecf20Sopenharmony_ci	return cntkctl;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic inline void arch_timer_set_cntkctl(u32 cntkctl)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	asm volatile("mcr p15, 0, %0, c14, c1, 0" : : "r" (cntkctl));
1258c2ecf20Sopenharmony_ci	isb();
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic inline void arch_timer_set_evtstrm_feature(void)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	elf_hwcap |= HWCAP_EVTSTRM;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic inline bool arch_timer_have_evtstrm_feature(void)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return elf_hwcap & HWCAP_EVTSTRM;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci#endif
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#endif
140