pcppi.c revision 1.34 1 /* $NetBSD: pcppi.c,v 1.34 2009/04/07 22:30:09 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pcppi.c,v 1.34 2009/04/07 22:30:09 dyoung Exp $");
32
33 #include "attimer.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/callout.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/device.h>
41 #include <sys/errno.h>
42
43 #include <sys/bus.h>
44
45 #include <dev/ic/attimervar.h>
46
47 #include <dev/isa/isareg.h>
48 #include <dev/isa/isavar.h>
49 #include <dev/isa/pcppireg.h>
50 #include <dev/isa/pcppivar.h>
51
52 #include "pckbd.h"
53 #if NPCKBD > 0
54 #include <dev/pckbport/pckbdvar.h>
55
56 void pcppi_pckbd_bell(void *, u_int, u_int, u_int, int);
57 #endif
58
59 int pcppi_match(device_t, cfdata_t, void *);
60 void pcppi_isa_attach(device_t, device_t, void *);
61 void pcppi_childdet(device_t, device_t);
62
63 CFATTACH_DECL2_NEW(pcppi, sizeof(struct pcppi_softc),
64 pcppi_match, pcppi_isa_attach, pcppi_detach, NULL, NULL, pcppi_childdet);
65
66 static int pcppisearch(device_t, cfdata_t, const int *, void *);
67 static void pcppi_bell_stop(void*);
68
69 #if NATTIMER > 0
70 static void pcppi_attach_speaker(device_t);
71 static void pcppi_detach_speaker(struct pcppi_softc *);
72 #endif
73
74 #define PCPPIPRI (PZERO - 1)
75
76 int
77 pcppi_match(device_t parent, cfdata_t match, void *aux)
78 {
79 struct isa_attach_args *ia = aux;
80 bus_space_handle_t ppi_ioh;
81 int have_ppi, rv;
82 u_int8_t v, nv;
83
84 if (ISA_DIRECT_CONFIG(ia))
85 return (0);
86
87 /* If values are hardwired to something that they can't be, punt. */
88 if (ia->ia_nio < 1 ||
89 (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
90 ia->ia_io[0].ir_addr != IO_PPI))
91 return (0);
92
93 if (ia->ia_niomem > 0 &&
94 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
95 return (0);
96
97 if (ia->ia_nirq > 0 &&
98 (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
99 return (0);
100
101 if (ia->ia_ndrq > 0 &&
102 (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
103 return (0);
104
105 rv = 0;
106 have_ppi = 0;
107
108 if (bus_space_map(ia->ia_iot, IO_PPI, 1, 0, &ppi_ioh))
109 goto lose;
110 have_ppi = 1;
111
112 /*
113 * Check for existence of PPI. Realistically, this is either going to
114 * be here or nothing is going to be here.
115 *
116 * We don't want to have any chance of changing speaker output (which
117 * this test might, if it crashes in the middle, or something;
118 * normally it's be to quick to produce anthing audible), but
119 * many "combo chip" mock-PPI's don't seem to support the top bit
120 * of Port B as a settable bit. The bottom bit has to be settable,
121 * since the speaker driver hardware still uses it.
122 */
123 v = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
124 bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v ^ 0x01); /* XXX */
125 nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
126 if (((nv ^ v) & 0x01) == 0x01)
127 rv = 1;
128 bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v); /* XXX */
129 nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
130 if (((nv ^ v) & 0x01) != 0x00) {
131 rv = 0;
132 goto lose;
133 }
134
135 /*
136 * We assume that the programmable interval timer is there.
137 */
138
139 lose:
140 if (have_ppi)
141 bus_space_unmap(ia->ia_iot, ppi_ioh, 1);
142 if (rv) {
143 ia->ia_io[0].ir_addr = IO_PPI;
144 ia->ia_io[0].ir_size = 1;
145 ia->ia_nio = 1;
146
147 ia->ia_niomem = 0;
148 ia->ia_nirq = 0;
149 ia->ia_ndrq = 0;
150 }
151 return (rv);
152 }
153
154 void
155 pcppi_isa_attach(device_t parent, device_t self, void *aux)
156 {
157 struct pcppi_softc *sc = device_private(self);
158 struct isa_attach_args *ia = aux;
159 bus_space_tag_t iot;
160
161 sc->sc_dv = self;
162 sc->sc_iot = iot = ia->ia_iot;
163
164 sc->sc_size = 1;
165 if (bus_space_map(iot, IO_PPI, sc->sc_size, 0, &sc->sc_ppi_ioh))
166 panic("pcppi_attach: couldn't map");
167
168 aprint_normal("\n");
169 pcppi_attach(sc);
170 }
171
172 void
173 pcppi_childdet(device_t self, device_t child)
174 {
175 /* we hold no child references, so do nothing */
176 }
177
178 int
179 pcppi_detach(device_t self, int flags)
180 {
181 int rc;
182 struct pcppi_softc *sc = device_private(self);
183
184 #if NATTIMER > 0
185 pcppi_detach_speaker(sc);
186 #endif
187
188 if ((rc = config_detach_children(sc->sc_dv, flags)) != 0)
189 return rc;
190
191 pmf_device_deregister(self);
192
193 #if NPCKBD > 0
194 pckbd_unhook_bell(pcppi_pckbd_bell, sc);
195 #endif
196 pcppi_bell_stop(sc);
197
198 callout_stop(&sc->sc_bell_ch);
199 callout_destroy(&sc->sc_bell_ch);
200 bus_space_unmap(sc->sc_iot, sc->sc_ppi_ioh, sc->sc_size);
201 return 0;
202 }
203
204 void
205 pcppi_attach(struct pcppi_softc *sc)
206 {
207 struct pcppi_attach_args pa;
208 device_t self = sc->sc_dv;
209
210 callout_init(&sc->sc_bell_ch, 0);
211
212 sc->sc_bellactive = sc->sc_bellpitch = sc->sc_slp = 0;
213
214 #if NPCKBD > 0
215 /* Provide a beeper for the PC Keyboard, if there isn't one already. */
216 pckbd_hookup_bell(pcppi_pckbd_bell, sc);
217 #endif
218 #if NATTIMER > 0
219 config_defer(sc->sc_dv, pcppi_attach_speaker);
220 #endif
221 if (!device_pmf_is_registered(self))
222 if (!pmf_device_register(self, NULL, NULL))
223 aprint_error_dev(self,
224 "couldn't establish power handler\n");
225
226 pa.pa_cookie = sc;
227 config_search_loc(pcppisearch, sc->sc_dv, "pcppi", NULL, &pa);
228 }
229
230 static int
231 pcppisearch(device_t parent, cfdata_t cf, const int *locs, void *aux)
232 {
233
234 if (config_match(parent, cf, aux))
235 config_attach_loc(parent, cf, locs, aux, NULL);
236
237 return 0;
238 }
239
240 #if NATTIMER > 0
241 static void
242 pcppi_detach_speaker(struct pcppi_softc *sc)
243 {
244 if (sc->sc_timer != NULL) {
245 attimer_detach_speaker(sc->sc_timer);
246 sc->sc_timer = NULL;
247 }
248 }
249
250 static void
251 pcppi_attach_speaker(device_t self)
252 {
253 struct pcppi_softc *sc = device_private(self);
254
255 if ((sc->sc_timer = attimer_attach_speaker()) == NULL)
256 aprint_error_dev(self, "could not find any available timer\n");
257 else {
258 aprint_normal_dev(sc->sc_timer, "attached to %s\n",
259 device_xname(self));
260 }
261 }
262 #endif
263
264 void
265 pcppi_bell(pcppi_tag_t self, int pitch, int period, int slp)
266 {
267 struct pcppi_softc *sc = self;
268 int s;
269
270 s = spltty(); /* ??? */
271 if (sc->sc_bellactive) {
272 if (sc->sc_timeout) {
273 sc->sc_timeout = 0;
274 callout_stop(&sc->sc_bell_ch);
275 }
276 if (sc->sc_slp)
277 wakeup(pcppi_bell_stop);
278 }
279 if (pitch == 0 || period == 0) {
280 pcppi_bell_stop(sc);
281 sc->sc_bellpitch = 0;
282 splx(s);
283 return;
284 }
285 if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
286 #if NATTIMER > 0
287 if (sc->sc_timer != NULL)
288 attimer_set_pitch(sc->sc_timer, pitch);
289 #endif
290 /* enable speaker */
291 bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
292 bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
293 | PIT_SPKR);
294 }
295 sc->sc_bellpitch = pitch;
296
297 sc->sc_bellactive = 1;
298 if (slp & PCPPI_BELL_POLL) {
299 delay((period * 1000000) / hz);
300 pcppi_bell_stop(sc);
301 } else {
302 sc->sc_timeout = 1;
303 callout_reset(&sc->sc_bell_ch, period, pcppi_bell_stop, sc);
304 if (slp & PCPPI_BELL_SLEEP) {
305 sc->sc_slp = 1;
306 tsleep(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", 0);
307 sc->sc_slp = 0;
308 }
309 }
310 splx(s);
311 }
312
313 static void
314 pcppi_bell_stop(void *arg)
315 {
316 struct pcppi_softc *sc = arg;
317 int s;
318
319 s = spltty(); /* ??? */
320 sc->sc_timeout = 0;
321
322 /* disable bell */
323 bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
324 bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
325 & ~PIT_SPKR);
326 sc->sc_bellactive = 0;
327 if (sc->sc_slp)
328 wakeup(pcppi_bell_stop);
329 splx(s);
330 }
331
332 #if NPCKBD > 0
333 void
334 pcppi_pckbd_bell(void *arg, u_int pitch, u_int period, u_int volume,
335 int poll)
336 {
337
338 /*
339 * Comes in as ms, goes out at ticks; volume ignored.
340 */
341 pcppi_bell(arg, pitch, (period * hz) / 1000,
342 poll ? PCPPI_BELL_POLL : 0);
343 }
344 #endif /* NPCKBD > 0 */
345