18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copytight (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
48c2ecf20Sopenharmony_ci * Copytight (C) 1999, 2000 Silicon Graphics, Inc.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include <linux/bcd.h>
78c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/sched.h>
118c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel_stat.h>
148c2ecf20Sopenharmony_ci#include <linux/param.h>
158c2ecf20Sopenharmony_ci#include <linux/smp.h>
168c2ecf20Sopenharmony_ci#include <linux/time.h>
178c2ecf20Sopenharmony_ci#include <linux/timex.h>
188c2ecf20Sopenharmony_ci#include <linux/mm.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <asm/time.h>
228c2ecf20Sopenharmony_ci#include <asm/sgialib.h>
238c2ecf20Sopenharmony_ci#include <asm/sn/klconfig.h>
248c2ecf20Sopenharmony_ci#include <asm/sn/arch.h>
258c2ecf20Sopenharmony_ci#include <asm/sn/addrs.h>
268c2ecf20Sopenharmony_ci#include <asm/sn/agent.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "ip27-common.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int rt_next_event(unsigned long delta, struct clock_event_device *evt)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	unsigned int cpu = smp_processor_id();
338c2ecf20Sopenharmony_ci	int slice = cputoslice(cpu);
348c2ecf20Sopenharmony_ci	unsigned long cnt;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	cnt = LOCAL_HUB_L(PI_RT_COUNT);
378c2ecf20Sopenharmony_ci	cnt += delta;
388c2ecf20Sopenharmony_ci	LOCAL_HUB_S(PI_RT_COMPARE_A + PI_COUNT_OFFSET * slice, cnt);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	return LOCAL_HUB_L(PI_RT_COUNT) >= cnt ? -ETIME : 0;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct clock_event_device, hub_rt_clockevent);
448c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(char [11], hub_rt_name);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic irqreturn_t hub_rt_counter_handler(int irq, void *dev_id)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	unsigned int cpu = smp_processor_id();
498c2ecf20Sopenharmony_ci	struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
508c2ecf20Sopenharmony_ci	int slice = cputoslice(cpu);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	/*
538c2ecf20Sopenharmony_ci	 * Ack
548c2ecf20Sopenharmony_ci	 */
558c2ecf20Sopenharmony_ci	LOCAL_HUB_S(PI_RT_PEND_A + PI_COUNT_OFFSET * slice, 0);
568c2ecf20Sopenharmony_ci	cd->event_handler(cd);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistruct irqaction hub_rt_irqaction = {
628c2ecf20Sopenharmony_ci	.handler	= hub_rt_counter_handler,
638c2ecf20Sopenharmony_ci	.percpu_dev_id	= &hub_rt_clockevent,
648c2ecf20Sopenharmony_ci	.flags		= IRQF_PERCPU | IRQF_TIMER,
658c2ecf20Sopenharmony_ci	.name		= "hub-rt",
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * This is a hack; we really need to figure these values out dynamically
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * Since 800 ns works very well with various HUB frequencies, such as
728c2ecf20Sopenharmony_ci * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * Ralf: which clock rate is used to feed the counter?
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_ci#define NSEC_PER_CYCLE		800
778c2ecf20Sopenharmony_ci#define CYCLES_PER_SEC		(NSEC_PER_SEC / NSEC_PER_CYCLE)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_civoid hub_rt_clock_event_init(void)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	unsigned int cpu = smp_processor_id();
828c2ecf20Sopenharmony_ci	struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
838c2ecf20Sopenharmony_ci	unsigned char *name = per_cpu(hub_rt_name, cpu);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	sprintf(name, "hub-rt %d", cpu);
868c2ecf20Sopenharmony_ci	cd->name		= name;
878c2ecf20Sopenharmony_ci	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
888c2ecf20Sopenharmony_ci	clockevent_set_clock(cd, CYCLES_PER_SEC);
898c2ecf20Sopenharmony_ci	cd->max_delta_ns	= clockevent_delta2ns(0xfffffffffffff, cd);
908c2ecf20Sopenharmony_ci	cd->max_delta_ticks	= 0xfffffffffffff;
918c2ecf20Sopenharmony_ci	cd->min_delta_ns	= clockevent_delta2ns(0x300, cd);
928c2ecf20Sopenharmony_ci	cd->min_delta_ticks	= 0x300;
938c2ecf20Sopenharmony_ci	cd->rating		= 200;
948c2ecf20Sopenharmony_ci	cd->irq			= IP27_RT_TIMER_IRQ;
958c2ecf20Sopenharmony_ci	cd->cpumask		= cpumask_of(cpu);
968c2ecf20Sopenharmony_ci	cd->set_next_event	= rt_next_event;
978c2ecf20Sopenharmony_ci	clockevents_register_device(cd);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	enable_percpu_irq(IP27_RT_TIMER_IRQ, IRQ_TYPE_NONE);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic void __init hub_rt_clock_event_global_init(void)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	irq_set_handler(IP27_RT_TIMER_IRQ, handle_percpu_devid_irq);
1058c2ecf20Sopenharmony_ci	irq_set_percpu_devid(IP27_RT_TIMER_IRQ);
1068c2ecf20Sopenharmony_ci	setup_percpu_irq(IP27_RT_TIMER_IRQ, &hub_rt_irqaction);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic u64 hub_rt_read(struct clocksource *cs)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistruct clocksource hub_rt_clocksource = {
1158c2ecf20Sopenharmony_ci	.name	= "HUB-RT",
1168c2ecf20Sopenharmony_ci	.rating = 200,
1178c2ecf20Sopenharmony_ci	.read	= hub_rt_read,
1188c2ecf20Sopenharmony_ci	.mask	= CLOCKSOURCE_MASK(52),
1198c2ecf20Sopenharmony_ci	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic u64 notrace hub_rt_read_sched_clock(void)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void __init hub_rt_clocksource_init(void)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct clocksource *cs = &hub_rt_clocksource;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	clocksource_register_hz(cs, CYCLES_PER_SEC);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	sched_clock_register(hub_rt_read_sched_clock, 52, CYCLES_PER_SEC);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_civoid __init plat_time_init(void)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	hub_rt_clocksource_init();
1398c2ecf20Sopenharmony_ci	hub_rt_clock_event_global_init();
1408c2ecf20Sopenharmony_ci	hub_rt_clock_event_init();
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_civoid hub_rtc_init(nasid_t nasid)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/*
1478c2ecf20Sopenharmony_ci	 * We only need to initialize the current node.
1488c2ecf20Sopenharmony_ci	 * If this is not the current node then it is a cpuless
1498c2ecf20Sopenharmony_ci	 * node and timeouts will not happen there.
1508c2ecf20Sopenharmony_ci	 */
1518c2ecf20Sopenharmony_ci	if (get_nasid() == nasid) {
1528c2ecf20Sopenharmony_ci		LOCAL_HUB_S(PI_RT_EN_A, 1);
1538c2ecf20Sopenharmony_ci		LOCAL_HUB_S(PI_RT_EN_B, 1);
1548c2ecf20Sopenharmony_ci		LOCAL_HUB_S(PI_PROF_EN_A, 0);
1558c2ecf20Sopenharmony_ci		LOCAL_HUB_S(PI_PROF_EN_B, 0);
1568c2ecf20Sopenharmony_ci		LOCAL_HUB_S(PI_RT_COUNT, 0);
1578c2ecf20Sopenharmony_ci		LOCAL_HUB_S(PI_RT_PEND_A, 0);
1588c2ecf20Sopenharmony_ci		LOCAL_HUB_S(PI_RT_PEND_B, 0);
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci}
161