xref: /openbmc/linux/arch/mips/kernel/csrc-ioasic.c (revision c942fddf)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24247417dSYoichi Yuasa /*
34247417dSYoichi Yuasa  *  DEC I/O ASIC's counter clocksource
44247417dSYoichi Yuasa  *
5ada8e951SYoichi Yuasa  *  Copyright (C) 2008	Yoichi Yuasa <yuasa@linux-mips.org>
64247417dSYoichi Yuasa  */
74247417dSYoichi Yuasa #include <linux/clocksource.h>
87cb24b70SDeng-Cheng Zhu #include <linux/sched_clock.h>
94247417dSYoichi Yuasa #include <linux/init.h>
104247417dSYoichi Yuasa 
114247417dSYoichi Yuasa #include <asm/ds1287.h>
124247417dSYoichi Yuasa #include <asm/time.h>
134247417dSYoichi Yuasa #include <asm/dec/ioasic.h>
144247417dSYoichi Yuasa #include <asm/dec/ioasic_addrs.h>
154247417dSYoichi Yuasa 
dec_ioasic_hpt_read(struct clocksource * cs)16a5a1d1c2SThomas Gleixner static u64 dec_ioasic_hpt_read(struct clocksource *cs)
174247417dSYoichi Yuasa {
184247417dSYoichi Yuasa 	return ioasic_read(IO_REG_FCTR);
194247417dSYoichi Yuasa }
204247417dSYoichi Yuasa 
214247417dSYoichi Yuasa static struct clocksource clocksource_dec = {
224247417dSYoichi Yuasa 	.name		= "dec-ioasic",
234247417dSYoichi Yuasa 	.read		= dec_ioasic_hpt_read,
244247417dSYoichi Yuasa 	.mask		= CLOCKSOURCE_MASK(32),
254247417dSYoichi Yuasa 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
264247417dSYoichi Yuasa };
274247417dSYoichi Yuasa 
dec_ioasic_read_sched_clock(void)287cb24b70SDeng-Cheng Zhu static u64 notrace dec_ioasic_read_sched_clock(void)
297cb24b70SDeng-Cheng Zhu {
307cb24b70SDeng-Cheng Zhu 	return ioasic_read(IO_REG_FCTR);
317cb24b70SDeng-Cheng Zhu }
327cb24b70SDeng-Cheng Zhu 
dec_ioasic_clocksource_init(void)33daed1285SMaciej W. Rozycki int __init dec_ioasic_clocksource_init(void)
344247417dSYoichi Yuasa {
354247417dSYoichi Yuasa 	unsigned int freq;
364247417dSYoichi Yuasa 	u32 start, end;
378533966aSMaciej W. Rozycki 	int i = HZ / 8;
384247417dSYoichi Yuasa 
398533966aSMaciej W. Rozycki 	ds1287_timer_state();
404247417dSYoichi Yuasa 	while (!ds1287_timer_state())
414247417dSYoichi Yuasa 		;
424247417dSYoichi Yuasa 
438e19608eSMagnus Damm 	start = dec_ioasic_hpt_read(&clocksource_dec);
444247417dSYoichi Yuasa 
454247417dSYoichi Yuasa 	while (i--)
464247417dSYoichi Yuasa 		while (!ds1287_timer_state())
474247417dSYoichi Yuasa 			;
484247417dSYoichi Yuasa 
498e19608eSMagnus Damm 	end = dec_ioasic_hpt_read(&clocksource_dec);
504247417dSYoichi Yuasa 
518533966aSMaciej W. Rozycki 	freq = (end - start) * 8;
52daed1285SMaciej W. Rozycki 
53daed1285SMaciej W. Rozycki 	/* An early revision of the I/O ASIC didn't have the counter.  */
54daed1285SMaciej W. Rozycki 	if (!freq)
55daed1285SMaciej W. Rozycki 		return -ENXIO;
56daed1285SMaciej W. Rozycki 
574247417dSYoichi Yuasa 	printk(KERN_INFO "I/O ASIC clock frequency %dHz\n", freq);
584247417dSYoichi Yuasa 
594247417dSYoichi Yuasa 	clocksource_dec.rating = 200 + freq / 10000000;
6075c4fd8cSJohn Stultz 	clocksource_register_hz(&clocksource_dec, freq);
617cb24b70SDeng-Cheng Zhu 
627cb24b70SDeng-Cheng Zhu 	sched_clock_register(dec_ioasic_read_sched_clock, 32, freq);
637cb24b70SDeng-Cheng Zhu 
64daed1285SMaciej W. Rozycki 	return 0;
654247417dSYoichi Yuasa }
66