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