pcppi.c revision 1.1 1 1.1 drochner /* $NetBSD: pcppi.c,v 1.1 1998/04/15 20:26:18 drochner 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.1 drochner *
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.1 drochner *
15 1.1 drochner * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1 drochner * 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.1 drochner *
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.1 drochner
30 1.1 drochner #include <sys/param.h>
31 1.1 drochner #include <sys/systm.h>
32 1.1 drochner #include <sys/kernel.h>
33 1.1 drochner #include <sys/proc.h>
34 1.1 drochner #include <sys/device.h>
35 1.1 drochner #include <sys/errno.h>
36 1.1 drochner
37 1.1 drochner #include <machine/bus.h>
38 1.1 drochner
39 1.1 drochner #include <dev/isa/isareg.h>
40 1.1 drochner #include <dev/isa/isavar.h>
41 1.1 drochner #include <dev/isa/pcppireg.h>
42 1.1 drochner #include <dev/isa/pcppivar.h>
43 1.1 drochner
44 1.1 drochner #include <dev/ic/i8253reg.h>
45 1.1 drochner
46 1.1 drochner struct pcppi_softc {
47 1.1 drochner struct device sc_dv;
48 1.1 drochner
49 1.1 drochner bus_space_tag_t sc_iot;
50 1.1 drochner bus_space_handle_t sc_ppi_ioh, sc_pit1_ioh;
51 1.1 drochner
52 1.1 drochner int sc_bellactive, sc_bellpitch;
53 1.1 drochner int sc_slp;
54 1.1 drochner };
55 1.1 drochner
56 1.1 drochner int pcppi_match __P((struct device *, struct cfdata *, void *));
57 1.1 drochner void pcppi_attach __P((struct device *, struct device *, void *));
58 1.1 drochner
59 1.1 drochner struct cfattach pcppi_ca = {
60 1.1 drochner sizeof(struct pcppi_softc), pcppi_match, pcppi_attach,
61 1.1 drochner };
62 1.1 drochner
63 1.1 drochner static void pcppi_bell_stop __P((void*));
64 1.1 drochner
65 1.1 drochner #define PCPPIPRI (PZERO - 1)
66 1.1 drochner
67 1.1 drochner int
68 1.1 drochner pcppi_match(parent, match, aux)
69 1.1 drochner struct device *parent;
70 1.1 drochner struct cfdata *match;
71 1.1 drochner void *aux;
72 1.1 drochner {
73 1.1 drochner struct isa_attach_args *ia = aux;
74 1.1 drochner bus_space_handle_t ppi_ioh, pit1_ioh;
75 1.1 drochner int have_pit1, have_ppi, rv;
76 1.1 drochner u_int8_t v, nv;
77 1.1 drochner
78 1.1 drochner /* If values are hardwired to something that they can't be, punt. */
79 1.1 drochner if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_PPI) ||
80 1.1 drochner ia->ia_maddr != MADDRUNK || ia->ia_msize != 0 ||
81 1.1 drochner ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
82 1.1 drochner return (0);
83 1.1 drochner
84 1.1 drochner rv = 0;
85 1.1 drochner have_pit1 = have_ppi = 0;
86 1.1 drochner
87 1.1 drochner if (bus_space_map(ia->ia_iot, IO_TIMER1, 4, 0, &pit1_ioh))
88 1.1 drochner goto lose;
89 1.1 drochner have_pit1 = 1;
90 1.1 drochner if (bus_space_map(ia->ia_iot, IO_PPI, 1, 0, &ppi_ioh))
91 1.1 drochner goto lose;
92 1.1 drochner have_ppi = 1;
93 1.1 drochner
94 1.1 drochner /*
95 1.1 drochner * Check for existence of PPI. Realistically, this is either going to
96 1.1 drochner * be here or nothing is going to be here.
97 1.1 drochner *
98 1.1 drochner * We don't want to have any chance of changing speaker output (which
99 1.1 drochner * this test might, if it crashes in the middle, or something;
100 1.1 drochner * normally it's be to quick to produce anthing audible), but
101 1.1 drochner * many "combo chip" mock-PPI's don't seem to support the top bit
102 1.1 drochner * of Port B as a settable bit. The bottom bit has to be settable,
103 1.1 drochner * since the speaker driver hardware still uses it.
104 1.1 drochner */
105 1.1 drochner v = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
106 1.1 drochner bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v ^ 0x01); /* XXX */
107 1.1 drochner nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
108 1.1 drochner if (((nv ^ v) & 0x01) == 0x01)
109 1.1 drochner rv = 1;
110 1.1 drochner bus_space_write_1(ia->ia_iot, ppi_ioh, 0, v); /* XXX */
111 1.1 drochner nv = bus_space_read_1(ia->ia_iot, ppi_ioh, 0); /* XXX */
112 1.1 drochner if (((nv ^ v) & 0x01) != 0x00) {
113 1.1 drochner rv = 0;
114 1.1 drochner goto lose;
115 1.1 drochner }
116 1.1 drochner
117 1.1 drochner /*
118 1.1 drochner * We assume that the programmable interval timer is there.
119 1.1 drochner */
120 1.1 drochner
121 1.1 drochner lose:
122 1.1 drochner if (have_pit1)
123 1.1 drochner bus_space_unmap(ia->ia_iot, pit1_ioh, 4);
124 1.1 drochner if (have_ppi)
125 1.1 drochner bus_space_unmap(ia->ia_iot, ppi_ioh, 1);
126 1.1 drochner if (rv) {
127 1.1 drochner ia->ia_iobase = IO_PPI;
128 1.1 drochner ia->ia_iosize = 0x1;
129 1.1 drochner ia->ia_msize = 0x0;
130 1.1 drochner }
131 1.1 drochner return (rv);
132 1.1 drochner }
133 1.1 drochner
134 1.1 drochner void
135 1.1 drochner pcppi_attach(parent, self, aux)
136 1.1 drochner struct device *parent, *self;
137 1.1 drochner void *aux;
138 1.1 drochner {
139 1.1 drochner struct pcppi_softc *sc = (struct pcppi_softc *)self;
140 1.1 drochner struct isa_attach_args *ia = aux;
141 1.1 drochner bus_space_tag_t iot;
142 1.1 drochner struct pcppi_attach_args pa;
143 1.1 drochner
144 1.1 drochner sc->sc_iot = iot = ia->ia_iot;
145 1.1 drochner
146 1.1 drochner if (bus_space_map(iot, IO_TIMER1, 4, 0, &sc->sc_pit1_ioh) ||
147 1.1 drochner bus_space_map(iot, IO_PPI, 1, 0, &sc->sc_ppi_ioh))
148 1.1 drochner panic("pcppi_attach: couldn't map");
149 1.1 drochner
150 1.1 drochner printf("\n");
151 1.1 drochner
152 1.1 drochner sc->sc_bellactive = sc->sc_bellpitch = sc->sc_slp = 0;
153 1.1 drochner
154 1.1 drochner pa.pa_cookie = sc;
155 1.1 drochner while (config_found(self, &pa, 0));
156 1.1 drochner }
157 1.1 drochner
158 1.1 drochner void
159 1.1 drochner pcppi_bell(self, pitch, period, slp)
160 1.1 drochner pcppi_tag_t self;
161 1.1 drochner int pitch, period;
162 1.1 drochner int slp;
163 1.1 drochner {
164 1.1 drochner struct pcppi_softc *sc = self;
165 1.1 drochner int s1, s2;
166 1.1 drochner
167 1.1 drochner s1 = spltty(); /* ??? */
168 1.1 drochner if (sc->sc_bellactive) {
169 1.1 drochner untimeout(pcppi_bell_stop, sc);
170 1.1 drochner if (sc->sc_slp)
171 1.1 drochner wakeup(pcppi_bell_stop);
172 1.1 drochner }
173 1.1 drochner if (pitch == 0 || period == 0) {
174 1.1 drochner pcppi_bell_stop(sc);
175 1.1 drochner sc->sc_bellpitch = 0;
176 1.1 drochner splx(s1);
177 1.1 drochner return;
178 1.1 drochner }
179 1.1 drochner if (!sc->sc_bellactive || sc->sc_bellpitch != pitch) {
180 1.1 drochner s2 = splhigh();
181 1.1 drochner bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_MODE,
182 1.1 drochner TIMER_SEL2 | TIMER_16BIT | TIMER_SQWAVE);
183 1.1 drochner bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
184 1.1 drochner TIMER_DIV(pitch) % 256);
185 1.1 drochner bus_space_write_1(sc->sc_iot, sc->sc_pit1_ioh, TIMER_CNTR2,
186 1.1 drochner TIMER_DIV(pitch) / 256);
187 1.1 drochner splx(s2);
188 1.1 drochner /* enable speaker */
189 1.1 drochner bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
190 1.1 drochner bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
191 1.1 drochner | PIT_SPKR);
192 1.1 drochner }
193 1.1 drochner sc->sc_bellpitch = pitch;
194 1.1 drochner
195 1.1 drochner sc->sc_bellactive = 1;
196 1.1 drochner timeout(pcppi_bell_stop, sc, period);
197 1.1 drochner if (slp) {
198 1.1 drochner sc->sc_slp = 1;
199 1.1 drochner tsleep(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", 0);
200 1.1 drochner sc->sc_slp = 0;
201 1.1 drochner }
202 1.1 drochner splx(s1);
203 1.1 drochner }
204 1.1 drochner
205 1.1 drochner static void
206 1.1 drochner pcppi_bell_stop(arg)
207 1.1 drochner void *arg;
208 1.1 drochner {
209 1.1 drochner struct pcppi_softc *sc = arg;
210 1.1 drochner int s;
211 1.1 drochner
212 1.1 drochner s = spltty(); /* ??? */
213 1.1 drochner /* disable bell */
214 1.1 drochner bus_space_write_1(sc->sc_iot, sc->sc_ppi_ioh, 0,
215 1.1 drochner bus_space_read_1(sc->sc_iot, sc->sc_ppi_ioh, 0)
216 1.1 drochner & ~PIT_SPKR);
217 1.1 drochner sc->sc_bellactive = 0;
218 1.1 drochner if (sc->sc_slp)
219 1.1 drochner wakeup(pcppi_bell_stop);
220 1.1 drochner splx(s);
221 1.1 drochner }
222