18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Corporation Limited
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute	it and/or modify it
68c2ecf20Sopenharmony_ci * under  the terms of	the GNU General	 Public License as published by the
78c2ecf20Sopenharmony_ci * Free Software Foundation;  either version 2 of the  License, or (at your
88c2ecf20Sopenharmony_ci * option) any later version.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci#ifndef _ASM_TIME_H
118c2ecf20Sopenharmony_ci#define _ASM_TIME_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
148c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
158c2ecf20Sopenharmony_ci#include <asm/loongarchregs.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciextern u64 cpu_clock_freq;
188c2ecf20Sopenharmony_ciextern u64 const_clock_freq;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciextern void save_counter(void);
218c2ecf20Sopenharmony_ciextern void sync_counter(void);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic inline unsigned int calc_const_freq(void)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	unsigned int res;
268c2ecf20Sopenharmony_ci	unsigned int base_freq;
278c2ecf20Sopenharmony_ci	unsigned int cfm, cfd;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	res = read_cpucfg(LOONGARCH_CPUCFG2);
308c2ecf20Sopenharmony_ci	if (!(res & CPUCFG2_LLFTP))
318c2ecf20Sopenharmony_ci		return 0;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	base_freq = read_cpucfg(LOONGARCH_CPUCFG4);
348c2ecf20Sopenharmony_ci	res = read_cpucfg(LOONGARCH_CPUCFG5);
358c2ecf20Sopenharmony_ci	cfm = res & 0xffff;
368c2ecf20Sopenharmony_ci	cfd = (res >> 16) & 0xffff;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	if (!base_freq || !cfm || !cfd)
398c2ecf20Sopenharmony_ci		return 0;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	return (base_freq * cfm / cfd);
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/*
458c2ecf20Sopenharmony_ci * Initialize the calling CPU's timer interrupt as clockevent device
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ciextern int constant_clockevent_init(void);
488c2ecf20Sopenharmony_ciextern int constant_clocksource_init(void);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic inline void clockevent_set_clock(struct clock_event_device *cd,
518c2ecf20Sopenharmony_ci					unsigned int clock)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	clockevents_calc_mult_shift(cd, clock, 4);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#endif /* _ASM_TIME_H */
57