ip27-timer.c 5.24 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2
/*
3
 * Copytight (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds's avatar
Linus Torvalds committed
4 5 6
 * Copytight (C) 1999, 2000 Silicon Graphics, Inc.
 */
#include <linux/bcd.h>
7
#include <linux/clockchips.h>
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
11
#include <linux/sched_clock.h>
Linus Torvalds's avatar
Linus Torvalds committed
12 13 14
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
#include <linux/param.h>
15
#include <linux/smp.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18
#include <linux/time.h>
#include <linux/timex.h>
#include <linux/mm.h>
19
#include <linux/platform_device.h>
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22 23 24 25 26 27

#include <asm/time.h>
#include <asm/pgtable.h>
#include <asm/sgialib.h>
#include <asm/sn/ioc3.h>
#include <asm/sn/klconfig.h>
#include <asm/sn/arch.h>
#include <asm/sn/addrs.h>
28
#include <asm/sn/agent.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35 36
#include <asm/sn/sn_private.h>

#define TICK_SIZE (tick_nsec / 1000)

/* Includes for ioc3_init().  */
#include <asm/sn/types.h>
#include <asm/pci/bridge.h>

37 38
#include "ip27-common.h"

39
static int rt_next_event(unsigned long delta, struct clock_event_device *evt)
40 41
{
	unsigned int cpu = smp_processor_id();
Ralf Baechle's avatar
Ralf Baechle committed
42
	int slice = cputoslice(cpu);
43 44 45 46
	unsigned long cnt;

	cnt = LOCAL_HUB_L(PI_RT_COUNT);
	cnt += delta;
47
	LOCAL_HUB_S(PI_RT_COMPARE_A + PI_COUNT_OFFSET * slice, cnt);
48 49 50 51

	return LOCAL_HUB_L(PI_RT_COUNT) >= cnt ? -ETIME : 0;
}

52 53 54
static DEFINE_PER_CPU(struct clock_event_device, hub_rt_clockevent);
static DEFINE_PER_CPU(char [11], hub_rt_name);

55
static irqreturn_t hub_rt_counter_handler(int irq, void *dev_id)
56 57
{
	unsigned int cpu = smp_processor_id();
58
	struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
59
	int slice = cputoslice(cpu);
60

61 62 63
	/*
	 * Ack
	 */
Ralf Baechle's avatar
Ralf Baechle committed
64
	LOCAL_HUB_S(PI_RT_PEND_A + PI_COUNT_OFFSET * slice, 0);
65 66 67 68 69
	cd->event_handler(cd);

	return IRQ_HANDLED;
}

70 71
struct irqaction hub_rt_irqaction = {
	.handler	= hub_rt_counter_handler,
72
	.percpu_dev_id	= &hub_rt_clockevent,
Yong Zhang's avatar
Yong Zhang committed
73
	.flags		= IRQF_PERCPU | IRQF_TIMER,
74
	.name		= "hub-rt",
75 76
};

77 78 79 80 81 82 83 84 85 86 87
/*
 * This is a hack; we really need to figure these values out dynamically
 *
 * Since 800 ns works very well with various HUB frequencies, such as
 * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
 *
 * Ralf: which clock rate is used to feed the counter?
 */
#define NSEC_PER_CYCLE		800
#define CYCLES_PER_SEC		(NSEC_PER_SEC / NSEC_PER_CYCLE)

88
void hub_rt_clock_event_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
89
{
90
	unsigned int cpu = smp_processor_id();
91 92 93 94
	struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
	unsigned char *name = per_cpu(hub_rt_name, cpu);

	sprintf(name, "hub-rt %d", cpu);
95 96
	cd->name		= name;
	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
97
	clockevent_set_clock(cd, CYCLES_PER_SEC);
Ralf Baechle's avatar
Ralf Baechle committed
98
	cd->max_delta_ns	= clockevent_delta2ns(0xfffffffffffff, cd);
99
	cd->max_delta_ticks	= 0xfffffffffffff;
Ralf Baechle's avatar
Ralf Baechle committed
100
	cd->min_delta_ns	= clockevent_delta2ns(0x300, cd);
101
	cd->min_delta_ticks	= 0x300;
102
	cd->rating		= 200;
103
	cd->irq			= IP27_RT_TIMER_IRQ;
104
	cd->cpumask		= cpumask_of(cpu);
105
	cd->set_next_event	= rt_next_event;
106
	clockevents_register_device(cd);
107 108

	enable_percpu_irq(IP27_RT_TIMER_IRQ, IRQ_TYPE_NONE);
109 110 111 112
}

static void __init hub_rt_clock_event_global_init(void)
{
113 114 115
	irq_set_handler(IP27_RT_TIMER_IRQ, handle_percpu_devid_irq);
	irq_set_percpu_devid(IP27_RT_TIMER_IRQ);
	setup_percpu_irq(IP27_RT_TIMER_IRQ, &hub_rt_irqaction);
Linus Torvalds's avatar
Linus Torvalds committed
116 117
}

118
static u64 hub_rt_read(struct clocksource *cs)
119 120 121 122
{
	return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
}

123
struct clocksource hub_rt_clocksource = {
124
	.name	= "HUB-RT",
Ralf Baechle's avatar
Ralf Baechle committed
125
	.rating = 200,
126 127 128 129 130
	.read	= hub_rt_read,
	.mask	= CLOCKSOURCE_MASK(52),
	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
};

131 132 133 134 135
static u64 notrace hub_rt_read_sched_clock(void)
{
	return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
}

136
static void __init hub_rt_clocksource_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
137
{
138 139
	struct clocksource *cs = &hub_rt_clocksource;

140
	clocksource_register_hz(cs, CYCLES_PER_SEC);
141 142

	sched_clock_register(hub_rt_read_sched_clock, 52, CYCLES_PER_SEC);
Linus Torvalds's avatar
Linus Torvalds committed
143 144
}

145 146
void __init plat_time_init(void)
{
147 148
	hub_rt_clocksource_init();
	hub_rt_clock_event_global_init();
149
	hub_rt_clock_event_init();
150 151
}

152
void cpu_time_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
{
	lboard_t *board;
	klcpu_t *cpu;
	int cpuid;

	/* Don't use ARCS.  ARCS is fragile.  Klconfig is simple and sane.  */
	board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
	if (!board)
		panic("Can't find board info for myself.");

	cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
	cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
	if (!cpu)
		panic("No information about myself?");

	printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
}

171
void hub_rtc_init(nasid_t nasid)
Linus Torvalds's avatar
Linus Torvalds committed
172
{
173

Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177 178
	/*
	 * We only need to initialize the current node.
	 * If this is not the current node then it is a cpuless
	 * node and timeouts will not happen there.
	 */
179
	if (get_nasid() == nasid) {
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183 184 185 186 187 188
		LOCAL_HUB_S(PI_RT_EN_A, 1);
		LOCAL_HUB_S(PI_RT_EN_B, 1);
		LOCAL_HUB_S(PI_PROF_EN_A, 0);
		LOCAL_HUB_S(PI_PROF_EN_B, 0);
		LOCAL_HUB_S(PI_RT_COUNT, 0);
		LOCAL_HUB_S(PI_RT_PEND_A, 0);
		LOCAL_HUB_S(PI_RT_PEND_B, 0);
	}
}
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

static int __init sgi_ip27_rtc_devinit(void)
{
	struct resource res;

	memset(&res, 0, sizeof(res));
	res.start = XPHYSADDR(KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base +
			      IOC3_BYTEBUS_DEV0);
	res.end = res.start + 32767;
	res.flags = IORESOURCE_MEM;

	return IS_ERR(platform_device_register_simple("rtc-m48t35", -1,
						      &res, 1));
}

/*
 * kludge make this a device_initcall after ioc3 resource conflicts
 * are resolved
 */
late_initcall(sgi_ip27_rtc_devinit);