1 /* $NetBSD: pcppi.c,v 1.34 2009/04/07 22:30:09 dyoung Exp $ */
4 * Copyright (c) 1996 Carnegie-Mellon University.
7 * Author: Chris G. Demetriou
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.
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.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pcppi.c,v 1.34 2009/04/07 22:30:09 dyoung Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/callout.h>
38 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/errno.h>
45 #include <dev/ic/attimervar.h>
47 #include <dev/isa/isareg.h>
48 #include <dev/isa/isavar.h>
49 #include <dev/isa/pcppireg.h>
50 #include <dev/isa/pcppivar.h>
54 #include <dev/pckbport/pckbdvar.h>
56 void pcppi_pckbd_bell(void *, u_int
, u_int
, u_int
, int);
59 int pcppi_match(device_t
, cfdata_t
, void *);
60 void pcppi_isa_attach(device_t
, device_t
, void *);
61 void pcppi_childdet(device_t
, device_t
);
63 CFATTACH_DECL3_NEW(pcppi
, sizeof(struct pcppi_softc
),
64 pcppi_match
, pcppi_isa_attach
, pcppi_detach
, NULL
, NULL
, pcppi_childdet
,
67 static int pcppisearch(device_t
, cfdata_t
, const int *, void *);
68 static void pcppi_bell_stop(void*);
71 static void pcppi_attach_speaker(device_t
);
72 static void pcppi_detach_speaker(struct pcppi_softc
*);
75 #define PCPPIPRI (PZERO - 1)
78 pcppi_match(device_t parent
, cfdata_t match
, void *aux
)
80 struct isa_attach_args
*ia
= aux
;
81 bus_space_handle_t ppi_ioh
;
85 if (ISA_DIRECT_CONFIG(ia
))
88 /* If values are hardwired to something that they can't be, punt. */
90 (ia
->ia_io
[0].ir_addr
!= ISA_UNKNOWN_PORT
&&
91 ia
->ia_io
[0].ir_addr
!= IO_PPI
))
94 if (ia
->ia_niomem
> 0 &&
95 (ia
->ia_iomem
[0].ir_addr
!= ISA_UNKNOWN_IOMEM
))
98 if (ia
->ia_nirq
> 0 &&
99 (ia
->ia_irq
[0].ir_irq
!= ISA_UNKNOWN_IRQ
))
102 if (ia
->ia_ndrq
> 0 &&
103 (ia
->ia_drq
[0].ir_drq
!= ISA_UNKNOWN_DRQ
))
109 if (bus_space_map(ia
->ia_iot
, IO_PPI
, 1, 0, &ppi_ioh
))
114 * Check for existence of PPI. Realistically, this is either going to
115 * be here or nothing is going to be here.
117 * We don't want to have any chance of changing speaker output (which
118 * this test might, if it crashes in the middle, or something;
119 * normally it's be to quick to produce anthing audible), but
120 * many "combo chip" mock-PPI's don't seem to support the top bit
121 * of Port B as a settable bit. The bottom bit has to be settable,
122 * since the speaker driver hardware still uses it.
124 v
= bus_space_read_1(ia
->ia_iot
, ppi_ioh
, 0); /* XXX */
125 bus_space_write_1(ia
->ia_iot
, ppi_ioh
, 0, v
^ 0x01); /* XXX */
126 nv
= bus_space_read_1(ia
->ia_iot
, ppi_ioh
, 0); /* XXX */
127 if (((nv
^ v
) & 0x01) == 0x01)
129 bus_space_write_1(ia
->ia_iot
, ppi_ioh
, 0, v
); /* XXX */
130 nv
= bus_space_read_1(ia
->ia_iot
, ppi_ioh
, 0); /* XXX */
131 if (((nv
^ v
) & 0x01) != 0x00) {
137 * We assume that the programmable interval timer is there.
142 bus_space_unmap(ia
->ia_iot
, ppi_ioh
, 1);
144 ia
->ia_io
[0].ir_addr
= IO_PPI
;
145 ia
->ia_io
[0].ir_size
= 1;
156 pcppi_isa_attach(device_t parent
, device_t self
, void *aux
)
158 struct pcppi_softc
*sc
= device_private(self
);
159 struct isa_attach_args
*ia
= aux
;
163 sc
->sc_iot
= iot
= ia
->ia_iot
;
166 if (bus_space_map(iot
, IO_PPI
, sc
->sc_size
, 0, &sc
->sc_ppi_ioh
))
167 panic("pcppi_attach: couldn't map");
174 pcppi_childdet(device_t self
, device_t child
)
176 /* we hold no child references, so do nothing */
180 pcppi_detach(device_t self
, int flags
)
183 struct pcppi_softc
*sc
= device_private(self
);
186 pcppi_detach_speaker(sc
);
189 if ((rc
= config_detach_children(sc
->sc_dv
, flags
)) != 0)
192 pmf_device_deregister(self
);
195 pckbd_unhook_bell(pcppi_pckbd_bell
, sc
);
199 callout_stop(&sc
->sc_bell_ch
);
200 callout_destroy(&sc
->sc_bell_ch
);
201 bus_space_unmap(sc
->sc_iot
, sc
->sc_ppi_ioh
, sc
->sc_size
);
206 pcppi_attach(struct pcppi_softc
*sc
)
208 struct pcppi_attach_args pa
;
209 device_t self
= sc
->sc_dv
;
211 callout_init(&sc
->sc_bell_ch
, 0);
213 sc
->sc_bellactive
= sc
->sc_bellpitch
= sc
->sc_slp
= 0;
216 /* Provide a beeper for the PC Keyboard, if there isn't one already. */
217 pckbd_hookup_bell(pcppi_pckbd_bell
, sc
);
220 config_defer(sc
->sc_dv
, pcppi_attach_speaker
);
222 if (!device_pmf_is_registered(self
))
223 if (!pmf_device_register(self
, NULL
, NULL
))
224 aprint_error_dev(self
,
225 "couldn't establish power handler\n");
228 config_search_loc(pcppisearch
, sc
->sc_dv
, "pcppi", NULL
, &pa
);
232 pcppisearch(device_t parent
, cfdata_t cf
, const int *locs
, void *aux
)
235 if (config_match(parent
, cf
, aux
))
236 config_attach_loc(parent
, cf
, locs
, aux
, NULL
);
243 pcppi_detach_speaker(struct pcppi_softc
*sc
)
245 if (sc
->sc_timer
!= NULL
) {
246 attimer_detach_speaker(sc
->sc_timer
);
252 pcppi_attach_speaker(device_t self
)
254 struct pcppi_softc
*sc
= device_private(self
);
256 if ((sc
->sc_timer
= attimer_attach_speaker()) == NULL
)
257 aprint_error_dev(self
, "could not find any available timer\n");
259 aprint_normal_dev(sc
->sc_timer
, "attached to %s\n",
266 pcppi_bell(pcppi_tag_t self
, int pitch
, int period
, int slp
)
268 struct pcppi_softc
*sc
= self
;
271 s
= spltty(); /* ??? */
272 if (sc
->sc_bellactive
) {
273 if (sc
->sc_timeout
) {
275 callout_stop(&sc
->sc_bell_ch
);
278 wakeup(pcppi_bell_stop
);
280 if (pitch
== 0 || period
== 0) {
282 sc
->sc_bellpitch
= 0;
286 if (!sc
->sc_bellactive
|| sc
->sc_bellpitch
!= pitch
) {
288 if (sc
->sc_timer
!= NULL
)
289 attimer_set_pitch(sc
->sc_timer
, pitch
);
292 bus_space_write_1(sc
->sc_iot
, sc
->sc_ppi_ioh
, 0,
293 bus_space_read_1(sc
->sc_iot
, sc
->sc_ppi_ioh
, 0)
296 sc
->sc_bellpitch
= pitch
;
298 sc
->sc_bellactive
= 1;
299 if (slp
& PCPPI_BELL_POLL
) {
300 delay((period
* 1000000) / hz
);
304 callout_reset(&sc
->sc_bell_ch
, period
, pcppi_bell_stop
, sc
);
305 if (slp
& PCPPI_BELL_SLEEP
) {
307 tsleep(pcppi_bell_stop
, PCPPIPRI
| PCATCH
, "bell", 0);
315 pcppi_bell_stop(void *arg
)
317 struct pcppi_softc
*sc
= arg
;
320 s
= spltty(); /* ??? */
324 bus_space_write_1(sc
->sc_iot
, sc
->sc_ppi_ioh
, 0,
325 bus_space_read_1(sc
->sc_iot
, sc
->sc_ppi_ioh
, 0)
327 sc
->sc_bellactive
= 0;
329 wakeup(pcppi_bell_stop
);
335 pcppi_pckbd_bell(void *arg
, u_int pitch
, u_int period
, u_int volume
,
340 * Comes in as ms, goes out at ticks; volume ignored.
342 pcppi_bell(arg
, pitch
, (period
* hz
) / 1000,
343 poll
? PCPPI_BELL_POLL
: 0);
345 #endif /* NPCKBD > 0 */