ip27-timer.c 5.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
3
 * Copytight (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
L
Linus Torvalds 已提交
4 5 6
 * Copytight (C) 1999, 2000 Silicon Graphics, Inc.
 */
#include <linux/bcd.h>
7
#include <linux/clockchips.h>
L
Linus Torvalds 已提交
8 9 10
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
11
#include <linux/sched_clock.h>
L
Linus Torvalds 已提交
12 13 14
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
#include <linux/param.h>
15
#include <linux/smp.h>
L
Linus Torvalds 已提交
16 17 18
#include <linux/time.h>
#include <linux/timex.h>
#include <linux/mm.h>
19
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#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>
#include <asm/sn/sn_private.h>
#include <asm/sn/sn0/ip27.h>
#include <asm/sn/sn0/hub.h>

#define TICK_SIZE (tick_nsec / 1000)

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

41
static int rt_next_event(unsigned long delta, struct clock_event_device *evt)
42 43
{
	unsigned int cpu = smp_processor_id();
R
Ralf Baechle 已提交
44
	int slice = cputoslice(cpu);
45 46 47 48
	unsigned long cnt;

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

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

54 55 56
static DEFINE_PER_CPU(struct clock_event_device, hub_rt_clockevent);
static DEFINE_PER_CPU(char [11], hub_rt_name);

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

63 64 65
	/*
	 * Ack
	 */
R
Ralf Baechle 已提交
66
	LOCAL_HUB_S(PI_RT_PEND_A + PI_COUNT_OFFSET * slice, 0);
67 68 69 70 71
	cd->event_handler(cd);

	return IRQ_HANDLED;
}

72 73
struct irqaction hub_rt_irqaction = {
	.handler	= hub_rt_counter_handler,
74
	.percpu_dev_id	= &hub_rt_clockevent,
Y
Yong Zhang 已提交
75
	.flags		= IRQF_PERCPU | IRQF_TIMER,
76
	.name		= "hub-rt",
77 78
};

79 80 81 82 83 84 85 86 87 88 89
/*
 * 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)

90
void hub_rt_clock_event_init(void)
L
Linus Torvalds 已提交
91
{
92
	unsigned int cpu = smp_processor_id();
93 94 95 96
	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);
97 98
	cd->name		= name;
	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
99
	clockevent_set_clock(cd, CYCLES_PER_SEC);
R
Ralf Baechle 已提交
100
	cd->max_delta_ns	= clockevent_delta2ns(0xfffffffffffff, cd);
101
	cd->max_delta_ticks	= 0xfffffffffffff;
R
Ralf Baechle 已提交
102
	cd->min_delta_ns	= clockevent_delta2ns(0x300, cd);
103
	cd->min_delta_ticks	= 0x300;
104
	cd->rating		= 200;
105
	cd->irq			= IP27_RT_TIMER_IRQ;
106
	cd->cpumask		= cpumask_of(cpu);
107
	cd->set_next_event	= rt_next_event;
108
	clockevents_register_device(cd);
109 110

	enable_percpu_irq(IP27_RT_TIMER_IRQ, IRQ_TYPE_NONE);
111 112 113 114
}

static void __init hub_rt_clock_event_global_init(void)
{
115 116 117
	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);
L
Linus Torvalds 已提交
118 119
}

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

125
struct clocksource hub_rt_clocksource = {
126
	.name	= "HUB-RT",
R
Ralf Baechle 已提交
127
	.rating = 200,
128 129 130 131 132
	.read	= hub_rt_read,
	.mask	= CLOCKSOURCE_MASK(52),
	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
};

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

138
static void __init hub_rt_clocksource_init(void)
L
Linus Torvalds 已提交
139
{
140 141
	struct clocksource *cs = &hub_rt_clocksource;

142
	clocksource_register_hz(cs, CYCLES_PER_SEC);
143 144

	sched_clock_register(hub_rt_read_sched_clock, 52, CYCLES_PER_SEC);
L
Linus Torvalds 已提交
145 146
}

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

154
void cpu_time_init(void)
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
{
	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);
}

173
void hub_rtc_init(cnodeid_t cnode)
L
Linus Torvalds 已提交
174
{
175

L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	/*
	 * 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.
	 */
	if (get_compact_nodeid() == cnode) {
		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);
	}
}
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

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);