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