clock_pcctwo.c revision 1.17 1 1.17 chs /* $NetBSD: clock_pcctwo.c,v 1.17 2012/10/27 17:18:27 chs Exp $ */
2 1.1 scw
3 1.1 scw /*-
4 1.1 scw * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5 1.1 scw * All rights reserved.
6 1.1 scw *
7 1.1 scw * This code is derived from software contributed to The NetBSD Foundation
8 1.1 scw * by Steve C. Woodford.
9 1.1 scw *
10 1.1 scw * Redistribution and use in source and binary forms, with or without
11 1.1 scw * modification, are permitted provided that the following conditions
12 1.1 scw * are met:
13 1.1 scw * 1. Redistributions of source code must retain the above copyright
14 1.1 scw * notice, this list of conditions and the following disclaimer.
15 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 scw * notice, this list of conditions and the following disclaimer in the
17 1.1 scw * documentation and/or other materials provided with the distribution.
18 1.1 scw *
19 1.1 scw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 scw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 scw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 scw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 scw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 scw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 scw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 scw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 scw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 scw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 scw * POSSIBILITY OF SUCH DAMAGE.
30 1.1 scw */
31 1.1 scw
32 1.1 scw /*
33 1.1 scw * Glue for the Peripheral Channel Controller Two (PCCChip2) timers,
34 1.1 scw * the Memory Controller ASIC (MCchip, and the Mostek clock chip found
35 1.1 scw * on the MVME-1[67]7, MVME-1[67]2 and MVME-187 series of boards.
36 1.1 scw */
37 1.5 lukem
38 1.5 lukem #include <sys/cdefs.h>
39 1.17 chs __KERNEL_RCSID(0, "$NetBSD: clock_pcctwo.c,v 1.17 2012/10/27 17:18:27 chs Exp $");
40 1.1 scw
41 1.1 scw #include <sys/param.h>
42 1.1 scw #include <sys/kernel.h>
43 1.1 scw #include <sys/systm.h>
44 1.1 scw #include <sys/device.h>
45 1.12 tsutsui #include <sys/timetc.h>
46 1.1 scw
47 1.1 scw #include <machine/psl.h>
48 1.11 ad #include <sys/bus.h>
49 1.1 scw
50 1.1 scw #include <dev/mvme/clockvar.h>
51 1.1 scw #include <dev/mvme/pcctwovar.h>
52 1.1 scw #include <dev/mvme/pcctworeg.h>
53 1.1 scw
54 1.1 scw
55 1.16 cegger int clock_pcctwo_match(device_t, cfdata_t, void *);
56 1.16 cegger void clock_pcctwo_attach(device_t, device_t, void *);
57 1.1 scw
58 1.1 scw struct clock_pcctwo_softc {
59 1.1 scw struct clock_attach_args sc_clock_args;
60 1.1 scw u_char sc_clock_lvl;
61 1.12 tsutsui struct timecounter sc_tc;
62 1.1 scw };
63 1.1 scw
64 1.17 chs CFATTACH_DECL_NEW(clock_pcctwo, sizeof(struct clock_pcctwo_softc),
65 1.4 thorpej clock_pcctwo_match, clock_pcctwo_attach, NULL, NULL);
66 1.1 scw
67 1.1 scw extern struct cfdriver clock_cd;
68 1.1 scw
69 1.6 perry static int clock_pcctwo_profintr(void *);
70 1.6 perry static int clock_pcctwo_statintr(void *);
71 1.6 perry static void clock_pcctwo_initclocks(void *, int, int);
72 1.12 tsutsui static u_int clock_pcctwo_getcount(struct timecounter *);
73 1.6 perry static void clock_pcctwo_shutdown(void *);
74 1.1 scw
75 1.1 scw static struct clock_pcctwo_softc *clock_pcctwo_sc;
76 1.12 tsutsui static uint32_t clock_pcctwo_count;
77 1.1 scw
78 1.1 scw /* ARGSUSED */
79 1.1 scw int
80 1.16 cegger clock_pcctwo_match(device_t parent, cfdata_t cf, void *aux)
81 1.1 scw {
82 1.1 scw struct pcctwo_attach_args *pa = aux;
83 1.1 scw
84 1.1 scw /* Only one clock, please. */
85 1.1 scw if (clock_pcctwo_sc)
86 1.1 scw return (0);
87 1.1 scw
88 1.1 scw if (strcmp(pa->pa_name, clock_cd.cd_name))
89 1.1 scw return (0);
90 1.1 scw
91 1.1 scw pa->pa_ipl = cf->pcctwocf_ipl;
92 1.1 scw
93 1.1 scw return (1);
94 1.1 scw }
95 1.1 scw
96 1.1 scw /* ARGSUSED */
97 1.1 scw void
98 1.16 cegger clock_pcctwo_attach(device_t parent, device_t self, void *aux)
99 1.1 scw {
100 1.1 scw struct clock_pcctwo_softc *sc;
101 1.1 scw struct pcctwo_attach_args *pa;
102 1.1 scw
103 1.10 thorpej sc = clock_pcctwo_sc = device_private(self);
104 1.1 scw pa = aux;
105 1.1 scw
106 1.1 scw if (pa->pa_ipl != CLOCK_LEVEL)
107 1.1 scw panic("clock_pcctwo_attach: wrong interrupt level");
108 1.1 scw
109 1.1 scw sc->sc_clock_args.ca_arg = sc;
110 1.1 scw sc->sc_clock_args.ca_initfunc = clock_pcctwo_initclocks;
111 1.1 scw
112 1.1 scw /* Do common portions of clock config. */
113 1.1 scw clock_config(self, &sc->sc_clock_args, pcctwointr_evcnt(pa->pa_ipl));
114 1.1 scw
115 1.1 scw /* Ensure our interrupts get disabled at shutdown time. */
116 1.1 scw (void) shutdownhook_establish(clock_pcctwo_shutdown, NULL);
117 1.1 scw
118 1.1 scw sc->sc_clock_lvl = (pa->pa_ipl & PCCTWO_ICR_LEVEL_MASK) |
119 1.1 scw PCCTWO_ICR_ICLR | PCCTWO_ICR_IEN;
120 1.1 scw
121 1.1 scw /* Attach the interrupt handlers. */
122 1.1 scw pcctwointr_establish(PCCTWOV_TIMER1, clock_pcctwo_profintr,
123 1.1 scw pa->pa_ipl, NULL, &clock_profcnt);
124 1.1 scw pcctwointr_establish(PCCTWOV_TIMER2, clock_pcctwo_statintr,
125 1.1 scw pa->pa_ipl, NULL, &clock_statcnt);
126 1.1 scw }
127 1.1 scw
128 1.1 scw void
129 1.14 dsl clock_pcctwo_initclocks(void *arg, int prof_us, int stat_us)
130 1.1 scw {
131 1.1 scw struct clock_pcctwo_softc *sc;
132 1.1 scw
133 1.1 scw sc = arg;
134 1.1 scw
135 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL, PCCTWO_TT_CTRL_COVF);
136 1.1 scw pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER1_COUNTER, 0);
137 1.1 scw pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER1_COMPARE,
138 1.8 scw PCCTWO_US2LIM(prof_us));
139 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL,
140 1.1 scw PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
141 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_ICSR, sc->sc_clock_lvl);
142 1.1 scw
143 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL, PCCTWO_TT_CTRL_COVF);
144 1.1 scw pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COUNTER, 0);
145 1.1 scw pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COMPARE,
146 1.8 scw PCCTWO_US2LIM(stat_us));
147 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL,
148 1.1 scw PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
149 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR, sc->sc_clock_lvl);
150 1.12 tsutsui
151 1.12 tsutsui sc->sc_tc.tc_get_timecount = clock_pcctwo_getcount;
152 1.12 tsutsui sc->sc_tc.tc_name = "pcctwo_count";
153 1.12 tsutsui sc->sc_tc.tc_frequency = PCCTWO_TIMERFREQ;
154 1.12 tsutsui sc->sc_tc.tc_quality = 100;
155 1.12 tsutsui sc->sc_tc.tc_counter_mask = ~0;
156 1.12 tsutsui tc_init(&sc->sc_tc);
157 1.1 scw }
158 1.1 scw
159 1.1 scw /* ARGSUSED */
160 1.12 tsutsui u_int
161 1.12 tsutsui clock_pcctwo_getcount(struct timecounter *tc)
162 1.1 scw {
163 1.12 tsutsui u_int cnt;
164 1.12 tsutsui uint32_t tc1, tc2;
165 1.12 tsutsui uint8_t cr;
166 1.12 tsutsui int s;
167 1.12 tsutsui
168 1.12 tsutsui s = splhigh();
169 1.1 scw
170 1.1 scw /*
171 1.1 scw * There's no way to latch the counter and overflow registers
172 1.1 scw * without pausing the clock, so compensate for the possible
173 1.1 scw * race by checking for counter wrap-around and re-reading the
174 1.1 scw * overflow counter if necessary.
175 1.1 scw *
176 1.12 tsutsui * Note: This only works because we're at splhigh().
177 1.1 scw */
178 1.12 tsutsui tc1 = pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER);
179 1.1 scw cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
180 1.12 tsutsui tc2 = pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER);
181 1.12 tsutsui if (tc1 > tc2) {
182 1.1 scw cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
183 1.12 tsutsui tc1 = tc2;
184 1.1 scw }
185 1.12 tsutsui cnt = clock_pcctwo_count;
186 1.12 tsutsui splx(s);
187 1.12 tsutsui /* XXX assume HZ == 100 */
188 1.12 tsutsui cnt += tc1 + (PCCTWO_TIMERFREQ / 100) * PCCTWO_TT_CTRL_OVF(cr);
189 1.1 scw
190 1.12 tsutsui return cnt;
191 1.1 scw }
192 1.1 scw
193 1.1 scw int
194 1.14 dsl clock_pcctwo_profintr(void *frame)
195 1.1 scw {
196 1.1 scw u_int8_t cr;
197 1.1 scw u_int32_t tc;
198 1.1 scw int s;
199 1.1 scw
200 1.1 scw s = splhigh();
201 1.1 scw tc = pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER);
202 1.1 scw cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
203 1.1 scw if (tc > pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER))
204 1.1 scw cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
205 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL,
206 1.1 scw PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
207 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_ICSR,
208 1.1 scw clock_pcctwo_sc->sc_clock_lvl);
209 1.1 scw splx(s);
210 1.1 scw
211 1.12 tsutsui for (cr = PCCTWO_TT_CTRL_OVF(cr); cr; cr--) {
212 1.12 tsutsui /* XXX assume HZ == 100 */
213 1.12 tsutsui clock_pcctwo_count += PCCTWO_TIMERFREQ / 100;
214 1.1 scw hardclock(frame);
215 1.12 tsutsui }
216 1.1 scw
217 1.1 scw return (1);
218 1.1 scw }
219 1.1 scw
220 1.1 scw int
221 1.14 dsl clock_pcctwo_statintr(void *frame)
222 1.1 scw {
223 1.1 scw
224 1.1 scw /* Disable the timer interrupt while we handle it. */
225 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR, 0);
226 1.1 scw
227 1.1 scw statclock((struct clockframe *) frame);
228 1.1 scw
229 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL, PCCTWO_TT_CTRL_COVF);
230 1.1 scw pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COUNTER, 0);
231 1.1 scw pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COMPARE,
232 1.1 scw PCCTWO_US2LIM(CLOCK_NEWINT(clock_statvar, clock_statmin)));
233 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL,
234 1.1 scw PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
235 1.1 scw
236 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR,
237 1.1 scw clock_pcctwo_sc->sc_clock_lvl);
238 1.1 scw
239 1.1 scw return (1);
240 1.1 scw }
241 1.1 scw
242 1.1 scw /* ARGSUSED */
243 1.1 scw void
244 1.14 dsl clock_pcctwo_shutdown(void *arg)
245 1.1 scw {
246 1.1 scw
247 1.1 scw /* Make sure the timer interrupts are turned off. */
248 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL, PCCTWO_TT_CTRL_COVF);
249 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_ICSR, 0);
250 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL, PCCTWO_TT_CTRL_COVF);
251 1.1 scw pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR, 0);
252 1.1 scw }
253