timer.c revision 1.8.6.2 1 1.8.6.2 tls /* $NetBSD: timer.c,v 1.8.6.2 2014/08/20 00:03:18 tls Exp $ */
2 1.8.6.2 tls
3 1.8.6.2 tls /*-
4 1.8.6.2 tls * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.8.6.2 tls * All rights reserved.
6 1.8.6.2 tls *
7 1.8.6.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.8.6.2 tls * by UCHIYAMA Yasushi.
9 1.8.6.2 tls *
10 1.8.6.2 tls * Redistribution and use in source and binary forms, with or without
11 1.8.6.2 tls * modification, are permitted provided that the following conditions
12 1.8.6.2 tls * are met:
13 1.8.6.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.8.6.2 tls * notice, this list of conditions and the following disclaimer.
15 1.8.6.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.8.6.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.8.6.2 tls * documentation and/or other materials provided with the distribution.
18 1.8.6.2 tls *
19 1.8.6.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.8.6.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.8.6.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.8.6.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.8.6.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.8.6.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.8.6.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.8.6.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.8.6.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.8.6.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.8.6.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.8.6.2 tls */
31 1.8.6.2 tls
32 1.8.6.2 tls #include <sys/cdefs.h>
33 1.8.6.2 tls __KERNEL_RCSID(0, "$NetBSD: timer.c,v 1.8.6.2 2014/08/20 00:03:18 tls Exp $");
34 1.8.6.2 tls
35 1.8.6.2 tls #include "debug_playstation2.h"
36 1.8.6.2 tls
37 1.8.6.2 tls #include <sys/param.h>
38 1.8.6.2 tls #include <sys/systm.h>
39 1.8.6.2 tls
40 1.8.6.2 tls #include <playstation2/playstation2/interrupt.h>
41 1.8.6.2 tls
42 1.8.6.2 tls #include <playstation2/ee/eevar.h>
43 1.8.6.2 tls #include <playstation2/ee/intcvar.h>
44 1.8.6.2 tls #include <playstation2/ee/timervar.h>
45 1.8.6.2 tls #include <playstation2/ee/timerreg.h>
46 1.8.6.2 tls
47 1.8.6.2 tls
48 1.8.6.2 tls #ifdef DEBUG
49 1.8.6.2 tls #define STATIC
50 1.8.6.2 tls #else
51 1.8.6.2 tls #define STATIC static
52 1.8.6.2 tls #endif
53 1.8.6.2 tls
54 1.8.6.2 tls STATIC int timer0_intr(void *);
55 1.8.6.2 tls
56 1.8.6.2 tls /*
57 1.8.6.2 tls * EE timer usage
58 1.8.6.2 tls * 0 ... 100 Hz clock interrupt.
59 1.8.6.2 tls * 1 ... one shot interrupt for software interrupt for IPL_SOFT
60 1.8.6.2 tls * 2 ... for IPL_SOFTCLOCK
61 1.8.6.2 tls * 3 ... for IPL_SOFTNET, IPL_SOFTSERIAL
62 1.8.6.2 tls */
63 1.8.6.2 tls
64 1.8.6.2 tls void
65 1.8.6.2 tls timer_init(void)
66 1.8.6.2 tls {
67 1.8.6.2 tls
68 1.8.6.2 tls _reg_write_4(T0_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
69 1.8.6.2 tls _reg_write_4(T1_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
70 1.8.6.2 tls _reg_write_4(T2_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
71 1.8.6.2 tls _reg_write_4(T3_MODE_REG, (T_MODE_EQUF | T_MODE_OVFF));
72 1.8.6.2 tls }
73 1.8.6.2 tls
74 1.8.6.2 tls void
75 1.8.6.2 tls timer_clock_init(void)
76 1.8.6.2 tls {
77 1.8.6.2 tls /* clock interrupt (296.912MHz / 2 / 256) * 5760 = 100Hz */
78 1.8.6.2 tls intc_intr_establish(I_CH9_TIMER0, IPL_CLOCK, timer0_intr, 0);
79 1.8.6.2 tls _reg_write_4(T0_COUNT_REG, 0);
80 1.8.6.2 tls _reg_write_4(T0_COMP_REG, 5760);
81 1.8.6.2 tls _reg_write_4(T0_MODE_REG, T_MODE_CLKS_BUSCLK256 | T_MODE_ZRET |
82 1.8.6.2 tls T_MODE_CUE | T_MODE_CMPE);
83 1.8.6.2 tls }
84 1.8.6.2 tls
85 1.8.6.2 tls void
86 1.8.6.2 tls timer_one_shot(int timer)
87 1.8.6.2 tls {
88 1.8.6.2 tls KDASSERT(LEGAL_TIMER(timer) && timer != 0);
89 1.8.6.2 tls
90 1.8.6.2 tls _reg_write_4(T_COUNT_REG(timer), 0);
91 1.8.6.2 tls _reg_write_4(T_COMP_REG(timer), 1);
92 1.8.6.2 tls _reg_write_4(T_MODE_REG(timer), T_MODE_CUE | T_MODE_CMPE);
93 1.8.6.2 tls }
94 1.8.6.2 tls
95 1.8.6.2 tls /*
96 1.8.6.2 tls * interrupt handler for clock interrupt (100Hz)
97 1.8.6.2 tls */
98 1.8.6.2 tls int
99 1.8.6.2 tls timer0_intr(void *arg)
100 1.8.6.2 tls {
101 1.8.6.2 tls
102 1.8.6.2 tls _reg_write_4(T0_MODE_REG, _reg_read_4(T0_MODE_REG) | T_MODE_EQUF);
103 1.8.6.2 tls
104 1.8.6.2 tls _playstation2_evcnt.clock.ev_count++;
105 1.8.6.2 tls
106 1.8.6.2 tls hardclock(&playstation2_clockframe);
107 1.8.6.2 tls
108 1.8.6.2 tls return (1);
109 1.8.6.2 tls }
110 1.8.6.2 tls
111 1.8.6.2 tls /* one shot timer interrupt for software interrupt */
112 1.8.6.2 tls int
113 1.8.6.2 tls timer1_intr(void *arg)
114 1.8.6.2 tls {
115 1.8.6.2 tls
116 1.8.6.2 tls _reg_write_4(T1_MODE_REG, T_MODE_EQUF | T_MODE_OVFF);
117 1.8.6.2 tls
118 1.8.6.2 tls #ifdef __HAVE_FAST_SOFTINTS
119 1.8.6.2 tls softintr_dispatch(0); /* IPL_SOFT */
120 1.8.6.2 tls #endif
121 1.8.6.2 tls
122 1.8.6.2 tls return (1);
123 1.8.6.2 tls }
124 1.8.6.2 tls
125 1.8.6.2 tls int
126 1.8.6.2 tls timer2_intr(void *arg)
127 1.8.6.2 tls {
128 1.8.6.2 tls
129 1.8.6.2 tls _reg_write_4(T2_MODE_REG, T_MODE_EQUF | T_MODE_OVFF);
130 1.8.6.2 tls
131 1.8.6.2 tls #ifdef __HAVE_FAST_SOFTINTS
132 1.8.6.2 tls softintr_dispatch(1); /* IPL_SOFTCLOCK */
133 1.8.6.2 tls #endif
134 1.8.6.2 tls return (1);
135 1.8.6.2 tls }
136 1.8.6.2 tls
137 1.8.6.2 tls int
138 1.8.6.2 tls timer3_intr(void *arg)
139 1.8.6.2 tls {
140 1.8.6.2 tls
141 1.8.6.2 tls _reg_write_4(T3_MODE_REG, T_MODE_EQUF | T_MODE_OVFF);
142 1.8.6.2 tls
143 1.8.6.2 tls #ifdef __HAVE_FAST_SOFTINTS
144 1.8.6.2 tls softintr_dispatch(3); /* IPL_SOFTSERIAL */
145 1.8.6.2 tls softintr_dispatch(2); /* IPL_SOFTNET */
146 1.8.6.2 tls #endif
147 1.8.6.2 tls
148 1.8.6.2 tls return (1);
149 1.8.6.2 tls }
150