xref: /netbsd/sys/arch/playstation2/ee/timer.c (revision 035a3ada)
1*035a3adaSchristos /*	$NetBSD: timer.c,v 1.9 2021/06/05 21:38:37 christos Exp $	*/
2768423b8Smartin 
3768423b8Smartin /*-
4768423b8Smartin  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5768423b8Smartin  * All rights reserved.
6768423b8Smartin  *
7768423b8Smartin  * This code is derived from software contributed to The NetBSD Foundation
8768423b8Smartin  * by UCHIYAMA Yasushi.
9768423b8Smartin  *
10768423b8Smartin  * Redistribution and use in source and binary forms, with or without
11768423b8Smartin  * modification, are permitted provided that the following conditions
12768423b8Smartin  * are met:
13768423b8Smartin  * 1. Redistributions of source code must retain the above copyright
14768423b8Smartin  *    notice, this list of conditions and the following disclaimer.
15768423b8Smartin  * 2. Redistributions in binary form must reproduce the above copyright
16768423b8Smartin  *    notice, this list of conditions and the following disclaimer in the
17768423b8Smartin  *    documentation and/or other materials provided with the distribution.
18768423b8Smartin  *
19768423b8Smartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20768423b8Smartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21768423b8Smartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22768423b8Smartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23768423b8Smartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24768423b8Smartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25768423b8Smartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26768423b8Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27768423b8Smartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28768423b8Smartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29768423b8Smartin  * POSSIBILITY OF SUCH DAMAGE.
30768423b8Smartin  */
31768423b8Smartin 
32768423b8Smartin #include <sys/cdefs.h>
33*035a3adaSchristos __KERNEL_RCSID(0, "$NetBSD: timer.c,v 1.9 2021/06/05 21:38:37 christos Exp $");
34768423b8Smartin 
35768423b8Smartin #include "debug_playstation2.h"
36768423b8Smartin 
37768423b8Smartin #include <sys/param.h>
38768423b8Smartin #include <sys/systm.h>
39768423b8Smartin 
40768423b8Smartin #include <playstation2/playstation2/interrupt.h>
41768423b8Smartin 
42768423b8Smartin #include <playstation2/ee/eevar.h>
43768423b8Smartin #include <playstation2/ee/intcvar.h>
44768423b8Smartin #include <playstation2/ee/timervar.h>
45768423b8Smartin #include <playstation2/ee/timerreg.h>
46768423b8Smartin 
47768423b8Smartin 
48768423b8Smartin #ifdef DEBUG
49768423b8Smartin #define STATIC
50768423b8Smartin #else
51768423b8Smartin #define STATIC static
52768423b8Smartin #endif
53768423b8Smartin 
54768423b8Smartin STATIC int timer0_intr(void *);
55768423b8Smartin 
56768423b8Smartin /*
57768423b8Smartin  * EE timer usage
58768423b8Smartin  *	0 ... 100 Hz clock interrupt.
59768423b8Smartin  *      1 ... one shot interrupt for software interrupt for IPL_SOFT
60768423b8Smartin  *	2 ... for IPL_SOFTCLOCK
61768423b8Smartin  *	3 ... for IPL_SOFTNET, IPL_SOFTSERIAL
62768423b8Smartin  */
63768423b8Smartin 
64768423b8Smartin void
timer_init(void)65768423b8Smartin timer_init(void)
66768423b8Smartin {
67768423b8Smartin 
68768423b8Smartin 	_reg_write_4(T0_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
69768423b8Smartin 	_reg_write_4(T1_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
70768423b8Smartin 	_reg_write_4(T2_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
71768423b8Smartin 	_reg_write_4(T3_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
72768423b8Smartin }
73768423b8Smartin 
74768423b8Smartin void
timer_clock_init(void)75768423b8Smartin timer_clock_init(void)
76768423b8Smartin {
77768423b8Smartin 	/* clock interrupt (296.912MHz / 2 / 256) * 5760 = 100Hz */
78768423b8Smartin 	intc_intr_establish(I_CH9_TIMER0, IPL_CLOCK, timer0_intr, 0);
79768423b8Smartin 	_reg_write_4(T0_COUNT_REG, 0);
80768423b8Smartin 	_reg_write_4(T0_COMP_REG, 5760);
81768423b8Smartin 	_reg_write_4(T0_MODE_REG, T_MODE_CLKS_BUSCLK256 | T_MODE_ZRET |
82768423b8Smartin 	    T_MODE_CUE | T_MODE_CMPE);
83768423b8Smartin }
84768423b8Smartin 
85768423b8Smartin void
timer_one_shot(int timer)86768423b8Smartin timer_one_shot(int timer)
87768423b8Smartin {
88768423b8Smartin 	KDASSERT(LEGAL_TIMER(timer) && timer != 0);
89768423b8Smartin 
90768423b8Smartin 	_reg_write_4(T_COUNT_REG(timer), 0);
91768423b8Smartin 	_reg_write_4(T_COMP_REG(timer), 1);
92768423b8Smartin 	_reg_write_4(T_MODE_REG(timer), T_MODE_CUE | T_MODE_CMPE);
93768423b8Smartin }
94768423b8Smartin 
95768423b8Smartin /*
96768423b8Smartin  * interrupt handler for clock interrupt (100Hz)
97768423b8Smartin  */
98768423b8Smartin int
timer0_intr(void * arg)99768423b8Smartin timer0_intr(void *arg)
100768423b8Smartin {
101768423b8Smartin 
102768423b8Smartin 	_reg_write_4(T0_MODE_REG, _reg_read_4(T0_MODE_REG) | T_MODE_EQUF);
103768423b8Smartin 
104768423b8Smartin 	_playstation2_evcnt.clock.ev_count++;
105768423b8Smartin 
106768423b8Smartin 	hardclock(&playstation2_clockframe);
107768423b8Smartin 
108768423b8Smartin 	return (1);
109768423b8Smartin }
110768423b8Smartin 
111768423b8Smartin /* one shot timer interrupt for software interrupt */
112768423b8Smartin int
timer1_intr(void * arg)113768423b8Smartin timer1_intr(void *arg)
114768423b8Smartin {
115768423b8Smartin 
116768423b8Smartin 	_reg_write_4(T1_MODE_REG, T_MODE_EQUF | T_MODE_OVFF);
117768423b8Smartin 
118768423b8Smartin #ifdef __HAVE_FAST_SOFTINTS
119*035a3adaSchristos 	softintr_dispatch(0); /* IPL_SOFTCLOCK */
120768423b8Smartin #endif
121768423b8Smartin 
122768423b8Smartin 	return (1);
123768423b8Smartin }
124768423b8Smartin 
125768423b8Smartin int
timer2_intr(void * arg)126768423b8Smartin timer2_intr(void *arg)
127768423b8Smartin {
128768423b8Smartin 
129768423b8Smartin 	_reg_write_4(T2_MODE_REG, T_MODE_EQUF | T_MODE_OVFF);
130768423b8Smartin 
131768423b8Smartin #ifdef __HAVE_FAST_SOFTINTS
132*035a3adaSchristos 	softintr_dispatch(1); /* IPL_SOFTBIO */
133768423b8Smartin #endif
134768423b8Smartin 	return (1);
135768423b8Smartin }
136768423b8Smartin 
137768423b8Smartin int
timer3_intr(void * arg)138768423b8Smartin timer3_intr(void *arg)
139768423b8Smartin {
140768423b8Smartin 
141768423b8Smartin 	_reg_write_4(T3_MODE_REG, T_MODE_EQUF | T_MODE_OVFF);
142768423b8Smartin 
143768423b8Smartin #ifdef __HAVE_FAST_SOFTINTS
144768423b8Smartin 	softintr_dispatch(3); /* IPL_SOFTSERIAL */
145768423b8Smartin 	softintr_dispatch(2); /* IPL_SOFTNET */
146768423b8Smartin #endif
147768423b8Smartin 
148768423b8Smartin 	return (1);
149768423b8Smartin }
150