malta-time.c 6.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Carsten Langgaard, carstenl@mips.com
 * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 * Setting up the clock on the MIPS boards.
 */
#include <linux/types.h>
21
#include <linux/i8253.h>
L
Linus Torvalds 已提交
22 23
#include <linux/init.h>
#include <linux/kernel_stat.h>
24
#include <linux/libfdt.h>
25
#include <linux/math64.h>
L
Linus Torvalds 已提交
26 27 28
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
29
#include <linux/irqchip/mips-gic.h>
L
Linus Torvalds 已提交
30 31 32
#include <linux/timex.h>
#include <linux/mc146818rtc.h>

33
#include <asm/cpu.h>
L
Linus Torvalds 已提交
34
#include <asm/mipsregs.h>
35
#include <asm/mipsmtregs.h>
36 37
#include <asm/hardirq.h>
#include <asm/irq.h>
L
Linus Torvalds 已提交
38
#include <asm/div64.h>
39
#include <asm/setup.h>
L
Linus Torvalds 已提交
40 41
#include <asm/time.h>
#include <asm/mc146818-time.h>
42
#include <asm/msc01_ic.h>
L
Linus Torvalds 已提交
43 44

#include <asm/mips-boards/generic.h>
45
#include <asm/mips-boards/maltaint.h>
L
Linus Torvalds 已提交
46

47
static int mips_cpu_timer_irq;
48
static int mips_cpu_perf_irq;
49
extern int cp0_perfcount_irq;
L
Linus Torvalds 已提交
50

51 52
static unsigned int gic_frequency;

53
static void mips_timer_dispatch(void)
L
Linus Torvalds 已提交
54
{
55
	do_IRQ(mips_cpu_timer_irq);
56 57
}

58 59
static void mips_perf_dispatch(void)
{
60
	do_IRQ(mips_cpu_perf_irq);
61 62
}

S
Steven J. Hill 已提交
63 64 65 66 67 68 69
static unsigned int freqround(unsigned int freq, unsigned int amount)
{
	freq += amount;
	freq -= freq % (amount*2);
	return freq;
}

L
Linus Torvalds 已提交
70
/*
S
Steven J. Hill 已提交
71
 * Estimate CPU and GIC frequencies.
L
Linus Torvalds 已提交
72
 */
S
Steven J. Hill 已提交
73
static void __init estimate_frequencies(void)
L
Linus Torvalds 已提交
74
{
R
Ralf Baechle 已提交
75
	unsigned long flags;
S
Steven J. Hill 已提交
76
	unsigned int count, start;
77 78
	unsigned char secs1, secs2, ctrl;
	int secs;
79
	u64 giccount = 0, gicstart = 0;
L
Linus Torvalds 已提交
80

81 82
#if defined(CONFIG_KVM_GUEST) && CONFIG_KVM_GUEST_TIMER_FREQ
	mips_hpt_frequency = CONFIG_KVM_GUEST_TIMER_FREQ * 1000000;
83 84 85
	return;
#endif

L
Linus Torvalds 已提交
86 87
	local_irq_save(flags);

88 89 90
	if (gic_present)
		gic_start_count();

91 92 93 94
	/*
	 * Read counters exactly on rising edge of update flag.
	 * This helps get an accurate reading under virtualisation.
	 */
L
Linus Torvalds 已提交
95 96
	while (CMOS_READ(RTC_REG_A) & RTC_UIP);
	while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
97
	start = read_c0_count();
98
	if (gic_present)
99
		gicstart = gic_read_count();
L
Linus Torvalds 已提交
100

101
	/* Wait for falling edge before reading RTC. */
L
Linus Torvalds 已提交
102
	while (CMOS_READ(RTC_REG_A) & RTC_UIP);
103
	secs1 = CMOS_READ(RTC_SECONDS);
L
Linus Torvalds 已提交
104

105 106
	/* Read counters again exactly on rising edge of update flag. */
	while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
S
Steven J. Hill 已提交
107 108
	count = read_c0_count();
	if (gic_present)
109
		giccount = gic_read_count();
L
Linus Torvalds 已提交
110

111 112 113 114 115 116
	/* Wait for falling edge before reading RTC again. */
	while (CMOS_READ(RTC_REG_A) & RTC_UIP);
	secs2 = CMOS_READ(RTC_SECONDS);

	ctrl = CMOS_READ(RTC_CONTROL);

L
Linus Torvalds 已提交
117 118
	local_irq_restore(flags);

119 120 121 122 123 124 125 126
	if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
		secs1 = bcd2bin(secs1);
		secs2 = bcd2bin(secs2);
	}
	secs = secs2 - secs1;
	if (secs < 1)
		secs += 60;

S
Steven J. Hill 已提交
127
	count -= start;
128
	count /= secs;
S
Steven J. Hill 已提交
129
	mips_hpt_frequency = count;
130 131

	if (gic_present) {
132
		giccount = div_u64(giccount - gicstart, secs);
S
Steven J. Hill 已提交
133
		gic_frequency = giccount;
134
	}
L
Linus Torvalds 已提交
135 136
}

137
void read_persistent_clock(struct timespec *ts)
L
Linus Torvalds 已提交
138
{
139 140
	ts->tv_sec = mc146818_get_cmos_time();
	ts->tv_nsec = 0;
L
Linus Torvalds 已提交
141 142
}

143 144
int get_c0_fdc_int(void)
{
145 146 147 148 149 150 151 152 153
	/*
	 * Some cores claim the FDC is routable through the GIC, but it doesn't
	 * actually seem to be connected for those Malta bitstreams.
	 */
	switch (current_cpu_type()) {
	case CPU_INTERAPTIV:
	case CPU_PROAPTIV:
		return -1;
	};
154 155

	if (cpu_has_veic)
156
		return -1;
157
	else if (gic_present)
158
		return gic_get_c0_fdc_int();
159
	else if (cp0_fdc_irq >= 0)
160
		return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
161
	else
162
		return -1;
163 164
}

165
int get_c0_perfcount_int(void)
166
{
167
	if (cpu_has_veic) {
168
		set_vi_handler(MSC01E_INT_PERFCTR, mips_perf_dispatch);
169
		mips_cpu_perf_irq = MSC01E_INT_BASE + MSC01E_INT_PERFCTR;
170 171
	} else if (gic_present) {
		mips_cpu_perf_irq = gic_get_c0_perfcount_int();
172
	} else if (cp0_perfcount_irq >= 0) {
173
		mips_cpu_perf_irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
174 175
	} else {
		mips_cpu_perf_irq = -1;
176
	}
177 178

	return mips_cpu_perf_irq;
179
}
180
EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
181

182
unsigned int get_c0_compare_int(void)
183 184
{
	if (cpu_has_veic) {
185
		set_vi_handler(MSC01E_INT_CPUCTR, mips_timer_dispatch);
186
		mips_cpu_timer_irq = MSC01E_INT_BASE + MSC01E_INT_CPUCTR;
187 188 189
	} else if (gic_present) {
		mips_cpu_timer_irq = gic_get_c0_compare_int();
	} else {
190
		mips_cpu_timer_irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
191
	}
192

193 194 195
	return mips_cpu_timer_irq;
}

196 197
static void __init init_rtc(void)
{
198
	unsigned char freq, ctrl;
199

200 201 202 203
	/* Set 32KHz time base if not already set */
	freq = CMOS_READ(RTC_FREQ_SELECT);
	if ((freq & RTC_DIV_CTL) != RTC_REF_CLCK_32KHZ)
		CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_FREQ_SELECT);
204

205 206 207 208
	/* Ensure SET bit is clear so RTC can run */
	ctrl = CMOS_READ(RTC_CONTROL);
	if (ctrl & RTC_SET)
		CMOS_WRITE(ctrl & ~RTC_SET, RTC_CONTROL);
209 210
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#ifdef CONFIG_CLKSRC_MIPS_GIC
static u32 gic_frequency_dt;

static struct property gic_frequency_prop = {
	.name = "clock-frequency",
	.length = sizeof(u32),
	.value = &gic_frequency_dt,
};

static void update_gic_frequency_dt(void)
{
	struct device_node *node;

	gic_frequency_dt = cpu_to_be32(gic_frequency);

	node = of_find_compatible_node(NULL, NULL, "mti,gic-timer");
	if (!node) {
		pr_err("mti,gic-timer device node not found\n");
		return;
	}

	if (of_update_property(node, &gic_frequency_prop) < 0)
		pr_err("error updating gic frequency property\n");
}

#endif

238 239
void __init plat_time_init(void)
{
240
	unsigned int prid = read_c0_prid() & (PRID_COMP_MASK | PRID_IMP_MASK);
S
Steven J. Hill 已提交
241
	unsigned int freq;
242

243
	init_rtc();
S
Steven J. Hill 已提交
244
	estimate_frequencies();
245

S
Steven J. Hill 已提交
246 247 248 249 250
	freq = mips_hpt_frequency;
	if ((prid != (PRID_COMP_MIPS | PRID_IMP_20KC)) &&
	    (prid != (PRID_COMP_MIPS | PRID_IMP_25KF)))
		freq *= 2;
	freq = freqround(freq, 5000);
251
	printk("CPU frequency %d.%02d MHz\n", freq/1000000,
S
Steven J. Hill 已提交
252 253
	       (freq%1000000)*100/1000000);

254
	mips_scroll_message();
255

S
Steven J. Hill 已提交
256 257
#ifdef CONFIG_I8253
	/* Only Malta has a PIT. */
258
	setup_pit_timer();
R
Ralf Baechle 已提交
259
#endif
260

261
#ifdef CONFIG_MIPS_GIC
262 263 264 265
	if (gic_present) {
		freq = freqround(gic_frequency, 5000);
		printk("GIC frequency %d.%02d MHz\n", freq/1000000,
		       (freq%1000000)*100/1000000);
266
#ifdef CONFIG_CLKSRC_MIPS_GIC
267
		update_gic_frequency_dt();
268
		timer_probe();
269 270 271
#endif
	}
#endif
L
Linus Torvalds 已提交
272
}