xref: /OK3568_Linux_fs/kernel/arch/mips/alchemy/common/time.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2008-2009 Manuel Lauss <manuel.lauss@gmail.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Previous incarnations were:
6*4882a593Smuzhiyun  * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
7*4882a593Smuzhiyun  * Copied and modified Carsten Langgaard's time.c
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Carsten Langgaard, carstenl@mips.com
10*4882a593Smuzhiyun  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * ########################################################################
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * ########################################################################
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
17*4882a593Smuzhiyun  * databooks).  Firmware/Board init code must enable the counters in the
18*4882a593Smuzhiyun  * counter control register, otherwise the CP0 counter clocksource/event
19*4882a593Smuzhiyun  * will be installed instead (and use of 'wait' instruction is prohibited).
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/clockchips.h>
23*4882a593Smuzhiyun #include <linux/clocksource.h>
24*4882a593Smuzhiyun #include <linux/interrupt.h>
25*4882a593Smuzhiyun #include <linux/spinlock.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <asm/idle.h>
28*4882a593Smuzhiyun #include <asm/processor.h>
29*4882a593Smuzhiyun #include <asm/time.h>
30*4882a593Smuzhiyun #include <asm/mach-au1x00/au1000.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* 32kHz clock enabled and detected */
33*4882a593Smuzhiyun #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
34*4882a593Smuzhiyun 
au1x_counter1_read(struct clocksource * cs)35*4882a593Smuzhiyun static u64 au1x_counter1_read(struct clocksource *cs)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	return alchemy_rdsys(AU1000_SYS_RTCREAD);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static struct clocksource au1x_counter1_clocksource = {
41*4882a593Smuzhiyun 	.name		= "alchemy-counter1",
42*4882a593Smuzhiyun 	.read		= au1x_counter1_read,
43*4882a593Smuzhiyun 	.mask		= CLOCKSOURCE_MASK(32),
44*4882a593Smuzhiyun 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
45*4882a593Smuzhiyun 	.rating		= 1500,
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
au1x_rtcmatch2_set_next_event(unsigned long delta,struct clock_event_device * cd)48*4882a593Smuzhiyun static int au1x_rtcmatch2_set_next_event(unsigned long delta,
49*4882a593Smuzhiyun 					 struct clock_event_device *cd)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	delta += alchemy_rdsys(AU1000_SYS_RTCREAD);
52*4882a593Smuzhiyun 	/* wait for register access */
53*4882a593Smuzhiyun 	while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M21)
54*4882a593Smuzhiyun 		;
55*4882a593Smuzhiyun 	alchemy_wrsys(delta, AU1000_SYS_RTCMATCH2);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	return 0;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
au1x_rtcmatch2_irq(int irq,void * dev_id)60*4882a593Smuzhiyun static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct clock_event_device *cd = dev_id;
63*4882a593Smuzhiyun 	cd->event_handler(cd);
64*4882a593Smuzhiyun 	return IRQ_HANDLED;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static struct clock_event_device au1x_rtcmatch2_clockdev = {
68*4882a593Smuzhiyun 	.name		= "rtcmatch2",
69*4882a593Smuzhiyun 	.features	= CLOCK_EVT_FEAT_ONESHOT,
70*4882a593Smuzhiyun 	.rating		= 1500,
71*4882a593Smuzhiyun 	.set_next_event = au1x_rtcmatch2_set_next_event,
72*4882a593Smuzhiyun 	.cpumask	= cpu_possible_mask,
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun 
alchemy_time_init(unsigned int m2int)75*4882a593Smuzhiyun static int __init alchemy_time_init(unsigned int m2int)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
78*4882a593Smuzhiyun 	unsigned long t;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	au1x_rtcmatch2_clockdev.irq = m2int;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* Check if firmware (YAMON, ...) has enabled 32kHz and clock
83*4882a593Smuzhiyun 	 * has been detected.  If so install the rtcmatch2 clocksource,
84*4882a593Smuzhiyun 	 * otherwise don't bother.  Note that both bits being set is by
85*4882a593Smuzhiyun 	 * no means a definite guarantee that the counters actually work
86*4882a593Smuzhiyun 	 * (the 32S bit seems to be stuck set to 1 once a single clock-
87*4882a593Smuzhiyun 	 * edge is detected, hence the timeouts).
88*4882a593Smuzhiyun 	 */
89*4882a593Smuzhiyun 	if (CNTR_OK != (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & CNTR_OK))
90*4882a593Smuzhiyun 		goto cntr_err;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/*
93*4882a593Smuzhiyun 	 * setup counter 1 (RTC) to tick at full speed
94*4882a593Smuzhiyun 	 */
95*4882a593Smuzhiyun 	t = 0xffffff;
96*4882a593Smuzhiyun 	while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T1S) && --t)
97*4882a593Smuzhiyun 		asm volatile ("nop");
98*4882a593Smuzhiyun 	if (!t)
99*4882a593Smuzhiyun 		goto cntr_err;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	alchemy_wrsys(0, AU1000_SYS_RTCTRIM);	/* 32.768 kHz */
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	t = 0xffffff;
104*4882a593Smuzhiyun 	while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
105*4882a593Smuzhiyun 		asm volatile ("nop");
106*4882a593Smuzhiyun 	if (!t)
107*4882a593Smuzhiyun 		goto cntr_err;
108*4882a593Smuzhiyun 	alchemy_wrsys(0, AU1000_SYS_RTCWRITE);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	t = 0xffffff;
111*4882a593Smuzhiyun 	while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
112*4882a593Smuzhiyun 		asm volatile ("nop");
113*4882a593Smuzhiyun 	if (!t)
114*4882a593Smuzhiyun 		goto cntr_err;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/* register counter1 clocksource and event device */
117*4882a593Smuzhiyun 	clocksource_register_hz(&au1x_counter1_clocksource, 32768);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	cd->shift = 32;
120*4882a593Smuzhiyun 	cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
121*4882a593Smuzhiyun 	cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
122*4882a593Smuzhiyun 	cd->max_delta_ticks = 0xffffffff;
123*4882a593Smuzhiyun 	cd->min_delta_ns = clockevent_delta2ns(9, cd);
124*4882a593Smuzhiyun 	cd->min_delta_ticks = 9;	/* ~0.28ms */
125*4882a593Smuzhiyun 	clockevents_register_device(cd);
126*4882a593Smuzhiyun 	if (request_irq(m2int, au1x_rtcmatch2_irq, IRQF_TIMER, "timer",
127*4882a593Smuzhiyun 			&au1x_rtcmatch2_clockdev))
128*4882a593Smuzhiyun 		pr_err("Failed to register timer interrupt\n");
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	printk(KERN_INFO "Alchemy clocksource installed\n");
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	return 0;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun cntr_err:
135*4882a593Smuzhiyun 	return -1;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun static int alchemy_m2inttab[] __initdata = {
139*4882a593Smuzhiyun 	AU1000_RTC_MATCH2_INT,
140*4882a593Smuzhiyun 	AU1500_RTC_MATCH2_INT,
141*4882a593Smuzhiyun 	AU1100_RTC_MATCH2_INT,
142*4882a593Smuzhiyun 	AU1550_RTC_MATCH2_INT,
143*4882a593Smuzhiyun 	AU1200_RTC_MATCH2_INT,
144*4882a593Smuzhiyun 	AU1300_RTC_MATCH2_INT,
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun 
plat_time_init(void)147*4882a593Smuzhiyun void __init plat_time_init(void)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	int t;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	t = alchemy_get_cputype();
152*4882a593Smuzhiyun 	if (t == ALCHEMY_CPU_UNKNOWN ||
153*4882a593Smuzhiyun 	    alchemy_time_init(alchemy_m2inttab[t]))
154*4882a593Smuzhiyun 		cpu_wait = NULL;	/* wait doesn't work with r4k timer */
155*4882a593Smuzhiyun }
156