xref: /linux/arch/mips/sgi-ip27/ip27-timer.c (revision 7f92b583382a1eb4aaafed90d181464969e41656)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
4  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
5  */
6 #include <linux/bcd.h>
7 #include <linux/clockchips.h>
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/sched_clock.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/param.h>
15 #include <linux/smp.h>
16 #include <linux/time.h>
17 #include <linux/timex.h>
18 #include <linux/mm.h>
19 #include <linux/platform_device.h>
20 
21 #include <asm/time.h>
22 #include <asm/sgialib.h>
23 #include <asm/sn/klconfig.h>
24 #include <asm/sn/arch.h>
25 #include <asm/sn/addrs.h>
26 #include <asm/sn/agent.h>
27 
28 #include "ip27-common.h"
29 
30 static int rt_next_event(unsigned long delta, struct clock_event_device *evt)
31 {
32 	unsigned int cpu = smp_processor_id();
33 	int slice = cputoslice(cpu);
34 	unsigned long cnt;
35 
36 	cnt = LOCAL_HUB_L(PI_RT_COUNT);
37 	cnt += delta;
38 	LOCAL_HUB_S(PI_RT_COMPARE_A + PI_COUNT_OFFSET * slice, cnt);
39 
40 	return LOCAL_HUB_L(PI_RT_COUNT) >= cnt ? -ETIME : 0;
41 }
42 
43 static DEFINE_PER_CPU(struct clock_event_device, hub_rt_clockevent);
44 static DEFINE_PER_CPU(char [11], hub_rt_name);
45 
46 static irqreturn_t hub_rt_counter_handler(int irq, void *dev_id)
47 {
48 	unsigned int cpu = smp_processor_id();
49 	struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
50 	int slice = cputoslice(cpu);
51 
52 	/*
53 	 * Ack
54 	 */
55 	LOCAL_HUB_S(PI_RT_PEND_A + PI_COUNT_OFFSET * slice, 0);
56 	cd->event_handler(cd);
57 
58 	return IRQ_HANDLED;
59 }
60 
61 /*
62  * This is a hack; we really need to figure these values out dynamically
63  *
64  * Since 800 ns works very well with various HUB frequencies, such as
65  * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
66  *
67  * Ralf: which clock rate is used to feed the counter?
68  */
69 #define NSEC_PER_CYCLE		800
70 #define CYCLES_PER_SEC		(NSEC_PER_SEC / NSEC_PER_CYCLE)
71 
72 void hub_rt_clock_event_init(void)
73 {
74 	unsigned int cpu = smp_processor_id();
75 	struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
76 	unsigned char *name = per_cpu(hub_rt_name, cpu);
77 
78 	sprintf(name, "hub-rt %d", cpu);
79 	cd->name		= name;
80 	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
81 	clockevent_set_clock(cd, CYCLES_PER_SEC);
82 	cd->max_delta_ns	= clockevent_delta2ns(0xfffffffffffff, cd);
83 	cd->max_delta_ticks	= 0xfffffffffffff;
84 	cd->min_delta_ns	= clockevent_delta2ns(0x300, cd);
85 	cd->min_delta_ticks	= 0x300;
86 	cd->rating		= 200;
87 	cd->irq			= IP27_RT_TIMER_IRQ;
88 	cd->cpumask		= cpumask_of(cpu);
89 	cd->set_next_event	= rt_next_event;
90 	clockevents_register_device(cd);
91 
92 	enable_percpu_irq(IP27_RT_TIMER_IRQ, IRQ_TYPE_NONE);
93 }
94 
95 static void __init hub_rt_clock_event_global_init(void)
96 {
97 	irq_set_handler(IP27_RT_TIMER_IRQ, handle_percpu_devid_irq);
98 	irq_set_percpu_devid(IP27_RT_TIMER_IRQ);
99 	WARN_ON(request_percpu_irq(IP27_RT_TIMER_IRQ, hub_rt_counter_handler,
100 				   "hub-rt", &hub_rt_clockevent));
101 }
102 
103 static u64 hub_rt_read(struct clocksource *cs)
104 {
105 	return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
106 }
107 
108 struct clocksource hub_rt_clocksource = {
109 	.name	= "HUB-RT",
110 	.rating = 200,
111 	.read	= hub_rt_read,
112 	.mask	= CLOCKSOURCE_MASK(52),
113 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
114 };
115 
116 static u64 notrace hub_rt_read_sched_clock(void)
117 {
118 	return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
119 }
120 
121 static void __init hub_rt_clocksource_init(void)
122 {
123 	struct clocksource *cs = &hub_rt_clocksource;
124 
125 	clocksource_register_hz(cs, CYCLES_PER_SEC);
126 
127 	sched_clock_register(hub_rt_read_sched_clock, 52, CYCLES_PER_SEC);
128 }
129 
130 void __init plat_time_init(void)
131 {
132 	hub_rt_clocksource_init();
133 	hub_rt_clock_event_global_init();
134 	hub_rt_clock_event_init();
135 }
136 
137 void hub_rtc_init(nasid_t nasid)
138 {
139 
140 	/*
141 	 * We only need to initialize the current node.
142 	 * If this is not the current node then it is a cpuless
143 	 * node and timeouts will not happen there.
144 	 */
145 	if (get_nasid() == nasid) {
146 		LOCAL_HUB_S(PI_RT_EN_A, 1);
147 		LOCAL_HUB_S(PI_RT_EN_B, 1);
148 		LOCAL_HUB_S(PI_PROF_EN_A, 0);
149 		LOCAL_HUB_S(PI_PROF_EN_B, 0);
150 		LOCAL_HUB_S(PI_RT_COUNT, 0);
151 		LOCAL_HUB_S(PI_RT_PEND_A, 0);
152 		LOCAL_HUB_S(PI_RT_PEND_B, 0);
153 	}
154 }
155