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