xref: /openbmc/linux/arch/mips/alchemy/common/time.c (revision ac8fd122)
141173abcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e8c7c482SRalf Baechle /*
378814465SManuel Lauss  * Copyright (C) 2008-2009 Manuel Lauss <manuel.lauss@gmail.com>
4e8c7c482SRalf Baechle  *
50c694de1SManuel Lauss  * Previous incarnations were:
6e8c7c482SRalf Baechle  * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
7e8c7c482SRalf Baechle  * Copied and modified Carsten Langgaard's time.c
8e8c7c482SRalf Baechle  *
9e8c7c482SRalf Baechle  * Carsten Langgaard, carstenl@mips.com
10e8c7c482SRalf Baechle  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
11e8c7c482SRalf Baechle  *
12e8c7c482SRalf Baechle  * ########################################################################
13e8c7c482SRalf Baechle  *
14e8c7c482SRalf Baechle  * ########################################################################
15e8c7c482SRalf Baechle  *
160c694de1SManuel Lauss  * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
170c694de1SManuel Lauss  * databooks).  Firmware/Board init code must enable the counters in the
180c694de1SManuel Lauss  * counter control register, otherwise the CP0 counter clocksource/event
190c694de1SManuel Lauss  * will be installed instead (and use of 'wait' instruction is prohibited).
20e8c7c482SRalf Baechle  */
21e8c7c482SRalf Baechle 
220c694de1SManuel Lauss #include <linux/clockchips.h>
230c694de1SManuel Lauss #include <linux/clocksource.h>
240c694de1SManuel Lauss #include <linux/interrupt.h>
25e8c7c482SRalf Baechle #include <linux/spinlock.h>
26e8c7c482SRalf Baechle 
27bdc92d74SRalf Baechle #include <asm/idle.h>
282882b0c6SManuel Lauss #include <asm/processor.h>
29e8c7c482SRalf Baechle #include <asm/time.h>
30e8c7c482SRalf Baechle #include <asm/mach-au1x00/au1000.h>
31e8c7c482SRalf Baechle 
320c694de1SManuel Lauss /* 32kHz clock enabled and detected */
330c694de1SManuel Lauss #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
340c694de1SManuel Lauss 
au1x_counter1_read(struct clocksource * cs)35a5a1d1c2SThomas Gleixner static u64 au1x_counter1_read(struct clocksource *cs)
360c694de1SManuel Lauss {
371d09de7dSManuel Lauss 	return alchemy_rdsys(AU1000_SYS_RTCREAD);
380c694de1SManuel Lauss }
390c694de1SManuel Lauss 
400c694de1SManuel Lauss static struct clocksource au1x_counter1_clocksource = {
410c694de1SManuel Lauss 	.name		= "alchemy-counter1",
420c694de1SManuel Lauss 	.read		= au1x_counter1_read,
430c694de1SManuel Lauss 	.mask		= CLOCKSOURCE_MASK(32),
440c694de1SManuel Lauss 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
458e0d7372SManuel Lauss 	.rating		= 1500,
460c694de1SManuel Lauss };
470c694de1SManuel Lauss 
au1x_rtcmatch2_set_next_event(unsigned long delta,struct clock_event_device * cd)480c694de1SManuel Lauss static int au1x_rtcmatch2_set_next_event(unsigned long delta,
490c694de1SManuel Lauss 					 struct clock_event_device *cd)
500c694de1SManuel Lauss {
511d09de7dSManuel Lauss 	delta += alchemy_rdsys(AU1000_SYS_RTCREAD);
520c694de1SManuel Lauss 	/* wait for register access */
531d09de7dSManuel Lauss 	while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M21)
540c694de1SManuel Lauss 		;
551d09de7dSManuel Lauss 	alchemy_wrsys(delta, AU1000_SYS_RTCMATCH2);
560c694de1SManuel Lauss 
570c694de1SManuel Lauss 	return 0;
580c694de1SManuel Lauss }
590c694de1SManuel Lauss 
au1x_rtcmatch2_irq(int irq,void * dev_id)600c694de1SManuel Lauss static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
610c694de1SManuel Lauss {
620c694de1SManuel Lauss 	struct clock_event_device *cd = dev_id;
630c694de1SManuel Lauss 	cd->event_handler(cd);
640c694de1SManuel Lauss 	return IRQ_HANDLED;
650c694de1SManuel Lauss }
660c694de1SManuel Lauss 
670c694de1SManuel Lauss static struct clock_event_device au1x_rtcmatch2_clockdev = {
680c694de1SManuel Lauss 	.name		= "rtcmatch2",
690c694de1SManuel Lauss 	.features	= CLOCK_EVT_FEAT_ONESHOT,
708e0d7372SManuel Lauss 	.rating		= 1500,
710c694de1SManuel Lauss 	.set_next_event = au1x_rtcmatch2_set_next_event,
72bbb8a752SManuel Lauss 	.cpumask	= cpu_possible_mask,
730c694de1SManuel Lauss };
740c694de1SManuel Lauss 
alchemy_time_init(unsigned int m2int)7578814465SManuel Lauss static int __init alchemy_time_init(unsigned int m2int)
76e8c7c482SRalf Baechle {
770c694de1SManuel Lauss 	struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
780c694de1SManuel Lauss 	unsigned long t;
79e8c7c482SRalf Baechle 
8078814465SManuel Lauss 	au1x_rtcmatch2_clockdev.irq = m2int;
8178814465SManuel Lauss 
820c694de1SManuel Lauss 	/* Check if firmware (YAMON, ...) has enabled 32kHz and clock
830c694de1SManuel Lauss 	 * has been detected.  If so install the rtcmatch2 clocksource,
840c694de1SManuel Lauss 	 * otherwise don't bother.  Note that both bits being set is by
850c694de1SManuel Lauss 	 * no means a definite guarantee that the counters actually work
860c694de1SManuel Lauss 	 * (the 32S bit seems to be stuck set to 1 once a single clock-
870c694de1SManuel Lauss 	 * edge is detected, hence the timeouts).
88e8c7c482SRalf Baechle 	 */
891d09de7dSManuel Lauss 	if (CNTR_OK != (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & CNTR_OK))
900c694de1SManuel Lauss 		goto cntr_err;
91e8c7c482SRalf Baechle 
920c694de1SManuel Lauss 	/*
930c694de1SManuel Lauss 	 * setup counter 1 (RTC) to tick at full speed
940c694de1SManuel Lauss 	 */
950c694de1SManuel Lauss 	t = 0xffffff;
961d09de7dSManuel Lauss 	while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T1S) && --t)
970c694de1SManuel Lauss 		asm volatile ("nop");
980c694de1SManuel Lauss 	if (!t)
990c694de1SManuel Lauss 		goto cntr_err;
1000c694de1SManuel Lauss 
1011d09de7dSManuel Lauss 	alchemy_wrsys(0, AU1000_SYS_RTCTRIM);	/* 32.768 kHz */
102e8c7c482SRalf Baechle 
1030c694de1SManuel Lauss 	t = 0xffffff;
1041d09de7dSManuel Lauss 	while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
1050c694de1SManuel Lauss 		asm volatile ("nop");
1060c694de1SManuel Lauss 	if (!t)
1070c694de1SManuel Lauss 		goto cntr_err;
1081d09de7dSManuel Lauss 	alchemy_wrsys(0, AU1000_SYS_RTCWRITE);
109e8c7c482SRalf Baechle 
1100c694de1SManuel Lauss 	t = 0xffffff;
1111d09de7dSManuel Lauss 	while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
1120c694de1SManuel Lauss 		asm volatile ("nop");
1130c694de1SManuel Lauss 	if (!t)
1140c694de1SManuel Lauss 		goto cntr_err;
1150c694de1SManuel Lauss 
1160c694de1SManuel Lauss 	/* register counter1 clocksource and event device */
11775c4fd8cSJohn Stultz 	clocksource_register_hz(&au1x_counter1_clocksource, 32768);
1180c694de1SManuel Lauss 
1190c694de1SManuel Lauss 	cd->shift = 32;
1200c694de1SManuel Lauss 	cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
1210c694de1SManuel Lauss 	cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
122e4db9253SNicolai Stange 	cd->max_delta_ticks = 0xffffffff;
123e4db9253SNicolai Stange 	cd->min_delta_ns = clockevent_delta2ns(9, cd);
124e4db9253SNicolai Stange 	cd->min_delta_ticks = 9;	/* ~0.28ms */
1250c694de1SManuel Lauss 	clockevents_register_device(cd);
126ac8fd122Safzal mohammed 	if (request_irq(m2int, au1x_rtcmatch2_irq, IRQF_TIMER, "timer",
127ac8fd122Safzal mohammed 			&au1x_rtcmatch2_clockdev))
128ac8fd122Safzal mohammed 		pr_err("Failed to register timer interrupt\n");
1290c694de1SManuel Lauss 
1300c694de1SManuel Lauss 	printk(KERN_INFO "Alchemy clocksource installed\n");
1310c694de1SManuel Lauss 
13278814465SManuel Lauss 	return 0;
133e8c7c482SRalf Baechle 
1340c694de1SManuel Lauss cntr_err:
13578814465SManuel Lauss 	return -1;
13678814465SManuel Lauss }
13778814465SManuel Lauss 
13878814465SManuel Lauss static int alchemy_m2inttab[] __initdata = {
13978814465SManuel Lauss 	AU1000_RTC_MATCH2_INT,
14078814465SManuel Lauss 	AU1500_RTC_MATCH2_INT,
14178814465SManuel Lauss 	AU1100_RTC_MATCH2_INT,
14278814465SManuel Lauss 	AU1550_RTC_MATCH2_INT,
14378814465SManuel Lauss 	AU1200_RTC_MATCH2_INT,
144809f36c6SManuel Lauss 	AU1300_RTC_MATCH2_INT,
14578814465SManuel Lauss };
14678814465SManuel Lauss 
plat_time_init(void)14778814465SManuel Lauss void __init plat_time_init(void)
14878814465SManuel Lauss {
14978814465SManuel Lauss 	int t;
15078814465SManuel Lauss 
15178814465SManuel Lauss 	t = alchemy_get_cputype();
1528e0d7372SManuel Lauss 	if (t == ALCHEMY_CPU_UNKNOWN ||
1538e0d7372SManuel Lauss 	    alchemy_time_init(alchemy_m2inttab[t]))
1548e0d7372SManuel Lauss 		cpu_wait = NULL;	/* wait doesn't work with r4k timer */
15578814465SManuel Lauss }
156