1/*	$NetBSD: timer_hb.c,v 1.21 2024/01/15 20:21:50 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: timer_hb.c,v 1.21 2024/01/15 20:21:50 thorpej Exp $");
34
35#include <sys/param.h>
36#include <sys/kernel.h>
37#include <sys/systm.h>
38#include <sys/device.h>
39#include <sys/intr.h>
40
41#include <uvm/uvm_extern.h>
42
43#include <machine/cpu.h>
44#include <machine/bus.h>
45#include <machine/vectors.h>
46
47#include <dev/clock_subr.h>
48
49#include <news68k/news68k/isr.h>
50#include <news68k/news68k/clockvar.h>
51
52#include <news68k/dev/hbvar.h>
53
54#include "ioconf.h"
55
56/*
57 * interrupt level for clock
58 */
59
60#define TIMER_LEVEL 6
61#define TIMER_SIZE 8	/* XXX */
62
63static int timer_hb_match(device_t, cfdata_t, void *);
64static void timer_hb_attach(device_t, device_t, void *);
65static void timer_hb_initclocks(int, int);
66void clock_intr(struct clockframe);
67
68static inline void leds_intr(void);
69
70extern void _isr_clock(void);	/* locore.s */
71
72CFATTACH_DECL_NEW(timer_hb, 0,
73    timer_hb_match, timer_hb_attach, NULL, NULL);
74
75static volatile uint8_t *ctrl_timer; /* XXX */
76
77extern volatile u_char *ctrl_led; /* XXX */
78
79static int
80timer_hb_match(device_t parent, cfdata_t cf, void *aux)
81{
82	struct hb_attach_args *ha = aux;
83	static int timer_hb_matched;
84
85	/* Only one timer, please. */
86	if (timer_hb_matched)
87		return 0;
88
89	if (strcmp(ha->ha_name, timer_cd.cd_name))
90		return 0;
91
92	if (ha->ha_ipl == -1)
93		ha->ha_ipl = TIMER_LEVEL;
94
95	ha->ha_size = TIMER_SIZE;
96
97	timer_hb_matched = 1;
98
99	return 1;
100}
101
102static void
103timer_hb_attach(device_t parent, device_t self, void *aux)
104{
105	struct hb_attach_args *ha = aux;
106
107	if (ha->ha_ipl != TIMER_LEVEL)
108		panic("clock_hb_attach: wrong interrupt level");
109
110	ctrl_timer = (uint8_t *)(ha->ha_address); /* XXX needs bus_space */
111
112	printf("\n");
113
114	timer_config(timer_hb_initclocks);
115
116#if 0 /* XXX this will be done in timer_hb_initclocks() */
117	vec_set_entry(VECI_INTRAV0 + ha->ha_ipl, _isr_clock);
118#endif
119}
120
121/*
122 * Set up the interval timer (enable clock interrupts).
123 * Leave stathz 0 since there is no secondary clock available.
124 * Note that clock interrupts MUST STAY DISABLED until here.
125 */
126static void
127timer_hb_initclocks(int prof, int stat)
128{
129	int s;
130
131	s = splhigh();
132
133	/* Install isr (in locore.s) that calls clock_intr(). */
134	vec_set_entry(VECI_INTRAV0 + TIMER_LEVEL, _isr_clock);
135
136	/* enable the clock */
137	*ctrl_timer = 1;
138	splx(s);
139}
140
141/*
142 * Clock interrupt handler.
143 * This is called by the "custom" interrupt handler.
144 *
145 * from sun3/sun3x/clock.c -tsutsui
146 */
147void
148clock_intr(struct clockframe cf)
149{
150#ifdef	LED_IDLE_CHECK
151	extern char _Idle[];	/* locore.s */
152#endif
153
154	/* Pulse the clock intr. enable low. */
155	*ctrl_timer = 0;
156	*ctrl_timer = 1;
157
158	m68k_count_intr(TIMER_LEVEL);
159
160	/* Entertainment! */
161#ifdef	LED_IDLE_CHECK
162	if (cf.cf_pc == (long)_Idle)
163#endif
164		leds_intr();
165
166	/* Call common clock interrupt handler. */
167	hardclock(&cf);
168}
169
170/* heartbeat LED */
171#define LED0	0x01
172#define LED1	0x02
173
174static inline void
175leds_intr(void)
176{
177	static u_char led_countdown, led_stat;
178	u_char i;
179
180	if (led_countdown) {
181		led_countdown--;
182		return;
183	}
184
185	i = led_stat ^ LED0;
186	*ctrl_led = i;
187	led_stat = i;
188	led_countdown = hz;
189}
190
191