pcppi.c revision 1.9.6.6 1 /* $NetBSD: pcppi.c,v 1.9.6.6 2005/04/01 14:29:52 skrll 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.9.6.6 2005/04/01 14:29:52 skrll 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 <machine/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(struct device *, struct cfdata *, void *);
60 void pcppi_isa_attach(struct device *, struct device *, void *);
61
62 CFATTACH_DECL(pcppi, sizeof(struct pcppi_softc),
63 pcppi_match, pcppi_isa_attach, NULL, NULL);
64
65 static void pcppi_bell_stop(void*);
66
67 #if NATTIMER > 0
68 static void pcppi_attach_speaker(struct device *);
69 #endif
70
71 #define PCPPIPRI (PZERO - 1)
72
73 int
74 pcppi_match(struct device *parent, struct cfdata *match, void *aux)
75 {
76 struct isa_attach_args *ia = aux;
77 bus_space_handle_t ppi_ioh;
78 int have_ppi, rv;
79 u_int8_t v, nv;
80
81 if (ISA_DIRECT_CONFIG(ia))
82 return (0);
83
84 /* If values are hardwired to something that they can't be, punt. */
85 if (ia->ia_nio < 1 ||
86 (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
87 ia->ia_io[0].ir_addr != IO_PPI))
88 return (0);
89
90 if (ia->ia_niomem > 0 &&
91 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
92 return (0);
93
94 if (ia->ia_nirq > 0 &&
95 (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
96 return (0);
97
98 if (ia->ia_ndrq > 0 &&
99 (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
100 return (0);
101
102 rv = 0;
103 have_ppi = 0;
104
105 if (bus_space_map(ia->ia_iot, IO_PPI, 1, 0, &ppi_ioh))
106 goto lose;
107 have_ppi = 1;
108
109 /*
110 * Check for existence of PPI. Realistically, this is either going to
111 * be here or nothing is going to be here.
112 *
113 * We don't want to have any chance of changing speaker output (which
114 * this test might, if it crashes in the middle, or something;
115 * normally it's be to quick to produce anthing audible), but
116 * many "combo chip" mock-PPI's don't seem to support the top bit
117 * of Port B as a settable bit. The bottom bit has to be settable,
118 * since the speaker driver hardware still uses it.
119 */
120 v = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
121 bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v ^ 0x01); /* XXX */
122 nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
123 if (((nv ^ v) & 0x01) == 0x01)
124 rv = 1;
125 bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v); /* XXX */
126 nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
127 if (((nv ^ v) & 0x01) != 0x00) {
128 rv = 0;
129 goto lose;
130 }
131
132 /*
133 * We assume that the programmable interval timer is there.
134 */
135
136 lose:
137 if (have_ppi)
138 bus_space_unmap(ia->ia_iot, ppi_ioh, 1);
139 if (rv) {
140 ia->ia_io[0].ir_addr = IO_PPI;
141 ia->ia_io[0].ir_size = 1;
142 ia->ia_nio = 1;
143
144 ia->ia_niomem = 0;
145 ia->ia_nirq = 0;
146 ia->ia_ndrq = 0;
147 }
148 return (rv);
149 }
150
151 void
152 pcppi_isa_attach(struct device *parent, struct device *self, void *aux)
153 {
154 struct pcppi_softc *sc = (struct pcppi_softc *)self;
155 struct isa_attach_args *ia = aux;
156 bus_space_tag_t iot;
157
158 sc->sc_iot = iot = ia->ia_iot;
159
160 if (bus_space_map(iot, IO_PPI, 1, 0, &sc->sc_ppi_ioh))
161 panic("pcppi_attach: couldn't map");
162
163 printf("\n");
164
165 pcppi_attach(sc);
166 }
167
168 void
169 pcppi_attach(struct pcppi_softc *sc)
170 {
171 struct pcppi_attach_args pa;
172
173 callout_init(&sc->sc_bell_ch);
174
175 sc->sc_bellactive = sc->sc_bellpitch = sc->sc_slp = 0;
176
177 #if NPCKBD > 0
178 /* Provide a beeper for the PC Keyboard, if there isn't one already. */
179 pckbd_hookup_bell(pcppi_pckbd_bell, sc);
180 #endif
181 #if NATTIMER > 0
182 config_defer((struct device *)sc, pcppi_attach_speaker);
183 #endif
184
185 pa.pa_cookie = sc;
186 while (config_found((struct device *)sc, &pa, 0));
187 }
188
189 #if NATTIMER > 0
190 static void
191 pcppi_attach_speaker(struct device *self)
192 {
193 struct pcppi_softc *sc = (struct pcppi_softc *)self;
194
195 if ((sc->sc_timer = attimer_attach_speaker()) == NULL)
196 aprint_error("%s: could not find any available timer\n",
197 sc->sc_dv.dv_xname);
198 else
199 aprint_normal("%s: attached to %s\n", sc->sc_dv.dv_xname,
200 sc->sc_timer->sc_dev.dv_xname);
201 }
202 #endif
203
204 void
205 pcppi_bell(pcppi_tag_t self, int pitch, int period, int slp)
206 {
207 struct pcppi_softc *sc = self;
208 int s;
209
210 s = spltty(); /* ??? */
211 if (sc->sc_bellactive) {
212 if (sc->sc_timeout) {
213 sc->sc_timeout = 0;
214 callout_stop(&sc->sc_bell_ch);
215 }
216 if (sc->sc_slp)
217 wakeup(pcppi_bell_stop);
218 }
219 if (pitch == 0 || period == 0) {
220 pcppi_bell_stop(sc);
221 sc->sc_bellpitch = 0;
222 splx(s);
223 return;
224 }
225 if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
226 #if NATTIMER > 0
227 if (sc->sc_timer != NULL)
228 attimer_set_pitch(sc->sc_timer, pitch);
229 #endif
230 /* enable speaker */
231 bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
232 bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
233 | PIT_SPKR);
234 }
235 sc->sc_bellpitch = pitch;
236
237 sc->sc_bellactive = 1;
238 if (slp & PCPPI_BELL_POLL) {
239 delay((period * 1000000) / hz);
240 pcppi_bell_stop(sc);
241 } else {
242 sc->sc_timeout = 1;
243 callout_reset(&sc->sc_bell_ch, period, pcppi_bell_stop, sc);
244 if (slp & PCPPI_BELL_SLEEP) {
245 sc->sc_slp = 1;
246 tsleep(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", 0);
247 sc->sc_slp = 0;
248 }
249 }
250 splx(s);
251 }
252
253 static void
254 pcppi_bell_stop(void *arg)
255 {
256 struct pcppi_softc *sc = arg;
257 int s;
258
259 s = spltty(); /* ??? */
260 sc->sc_timeout = 0;
261
262 /* disable bell */
263 bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
264 bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
265 & ~PIT_SPKR);
266 sc->sc_bellactive = 0;
267 if (sc->sc_slp)
268 wakeup(pcppi_bell_stop);
269 splx(s);
270 }
271
272 #if NPCKBD > 0
273 void
274 pcppi_pckbd_bell(void *arg, u_int pitch, u_int period, u_int volume, int poll)
275 {
276
277 /*
278 * Comes in as ms, goes out at ticks; volume ignored.
279 */
280 pcppi_bell(arg, pitch, (period * hz) / 1000,
281 poll ? PCPPI_BELL_POLL : 0);
282 }
283 #endif /* NPCKBD > 0 */
284