18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/arch/m68k/hp300/time.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 1998 Philip Blundell <philb@gnu.org>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  This file contains the HP300-specific time handling code.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
118c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/sched.h>
158c2ecf20Sopenharmony_ci#include <linux/kernel_stat.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <asm/machdep.h>
188c2ecf20Sopenharmony_ci#include <asm/irq.h>
198c2ecf20Sopenharmony_ci#include <asm/io.h>
208c2ecf20Sopenharmony_ci#include <asm/traps.h>
218c2ecf20Sopenharmony_ci#include <asm/blinken.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic u64 hp300_read_clk(struct clocksource *cs);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic struct clocksource hp300_clk = {
268c2ecf20Sopenharmony_ci	.name   = "timer",
278c2ecf20Sopenharmony_ci	.rating = 250,
288c2ecf20Sopenharmony_ci	.read   = hp300_read_clk,
298c2ecf20Sopenharmony_ci	.mask   = CLOCKSOURCE_MASK(32),
308c2ecf20Sopenharmony_ci	.flags  = CLOCK_SOURCE_IS_CONTINUOUS,
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic u32 clk_total, clk_offset;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Clock hardware definitions */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define CLOCKBASE	0xf05f8000
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define	CLKCR1		0x1
408c2ecf20Sopenharmony_ci#define	CLKCR2		0x3
418c2ecf20Sopenharmony_ci#define	CLKCR3		CLKCR1
428c2ecf20Sopenharmony_ci#define	CLKSR		CLKCR2
438c2ecf20Sopenharmony_ci#define	CLKMSB1		0x5
448c2ecf20Sopenharmony_ci#define	CLKLSB1		0x7
458c2ecf20Sopenharmony_ci#define	CLKMSB2		0x9
468c2ecf20Sopenharmony_ci#define	CLKMSB3		0xD
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define	CLKSR_INT1	BIT(0)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* This is for machines which generate the exact clock. */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define HP300_TIMER_CLOCK_FREQ 250000
538c2ecf20Sopenharmony_ci#define HP300_TIMER_CYCLES     (HP300_TIMER_CLOCK_FREQ / HZ)
548c2ecf20Sopenharmony_ci#define INTVAL                 (HP300_TIMER_CYCLES - 1)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic irqreturn_t hp300_tick(int irq, void *dev_id)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	irq_handler_t timer_routine = dev_id;
598c2ecf20Sopenharmony_ci	unsigned long flags;
608c2ecf20Sopenharmony_ci	unsigned long tmp;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	local_irq_save(flags);
638c2ecf20Sopenharmony_ci	in_8(CLOCKBASE + CLKSR);
648c2ecf20Sopenharmony_ci	asm volatile ("movpw %1@(5),%0" : "=d" (tmp) : "a" (CLOCKBASE));
658c2ecf20Sopenharmony_ci	clk_total += INTVAL;
668c2ecf20Sopenharmony_ci	clk_offset = 0;
678c2ecf20Sopenharmony_ci	timer_routine(0, NULL);
688c2ecf20Sopenharmony_ci	local_irq_restore(flags);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/* Turn off the network and SCSI leds */
718c2ecf20Sopenharmony_ci	blinken_leds(0, 0xe0);
728c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic u64 hp300_read_clk(struct clocksource *cs)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	unsigned long flags;
788c2ecf20Sopenharmony_ci	unsigned char lsb, msb, msb_new;
798c2ecf20Sopenharmony_ci	u32 ticks;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	local_irq_save(flags);
828c2ecf20Sopenharmony_ci	/* Read current timer 1 value */
838c2ecf20Sopenharmony_ci	msb = in_8(CLOCKBASE + CLKMSB1);
848c2ecf20Sopenharmony_ciagain:
858c2ecf20Sopenharmony_ci	if ((in_8(CLOCKBASE + CLKSR) & CLKSR_INT1) && msb > 0)
868c2ecf20Sopenharmony_ci		clk_offset = INTVAL;
878c2ecf20Sopenharmony_ci	lsb = in_8(CLOCKBASE + CLKLSB1);
888c2ecf20Sopenharmony_ci	msb_new = in_8(CLOCKBASE + CLKMSB1);
898c2ecf20Sopenharmony_ci	if (msb_new != msb) {
908c2ecf20Sopenharmony_ci		msb = msb_new;
918c2ecf20Sopenharmony_ci		goto again;
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	ticks = INTVAL - ((msb << 8) | lsb);
958c2ecf20Sopenharmony_ci	ticks += clk_offset + clk_total;
968c2ecf20Sopenharmony_ci	local_irq_restore(flags);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	return ticks;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_civoid __init hp300_sched_init(irq_handler_t vector)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci  out_8(CLOCKBASE + CLKCR2, 0x1);		/* select CR1 */
1048c2ecf20Sopenharmony_ci  out_8(CLOCKBASE + CLKCR1, 0x1);		/* reset */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci  asm volatile(" movpw %0,%1@(5)" : : "d" (INTVAL), "a" (CLOCKBASE));
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci  if (request_irq(IRQ_AUTO_6, hp300_tick, IRQF_TIMER, "timer tick", vector))
1098c2ecf20Sopenharmony_ci    pr_err("Couldn't register timer interrupt\n");
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci  out_8(CLOCKBASE + CLKCR2, 0x1);		/* select CR1 */
1128c2ecf20Sopenharmony_ci  out_8(CLOCKBASE + CLKCR1, 0x40);		/* enable irq */
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci  clocksource_register_hz(&hp300_clk, HP300_TIMER_CLOCK_FREQ);
1158c2ecf20Sopenharmony_ci}
116