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