xref: /netbsd-src/sys/dev/ic/attimer.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: attimer.c,v 1.5 2007/12/09 20:27:58 jmcneill Exp $	*/
2 
3 /*
4  *  Copyright (c) 2005 The NetBSD Foundation.
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *  3. Neither the name of The NetBSD Foundation nor the names of its
16  *     contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * AT Timer
34  *
35  * This code only allows control over the pitch of the speaker (pcppi(4)).
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: attimer.c,v 1.5 2007/12/09 20:27:58 jmcneill Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 
46 #include <sys/bus.h>
47 
48 #include <dev/isa/isareg.h>
49 
50 #include <dev/ic/attimervar.h>
51 #include <dev/ic/i8253reg.h>
52 
53 extern struct cfdriver attimer_cd;
54 
55 void
56 attimer_attach(struct attimer_softc *sc)
57 {
58 	sc->sc_flags |= ATT_CONFIGURED;
59 
60 	if (!pmf_device_register(&sc->sc_dev, NULL, NULL))
61 		aprint_error_dev(&sc->sc_dev, "couldn't establish power handler\n");
62 }
63 
64 /*
65  * This is slightly artificial.  While the code looks fine, I can't help
66  * but pray I'll get the attimer object associated to the pcppi object
67  * that calls the routine.
68  *
69  * There's not much at risk here, as there's about no chance to have a
70  * computer with more than one pcppi/attimer accessible.
71  */
72 
73 struct attimer_softc *
74 attimer_attach_speaker()
75 {
76 	int i;
77 	struct attimer_softc *sc;
78 
79 	for (i = 0; i < attimer_cd.cd_ndevs; i++)
80 		if (attimer_cd.cd_devs[i] != NULL) {
81 			sc = (struct attimer_softc *)attimer_cd.cd_devs[i];
82 			if ((sc->sc_flags & ATT_CONFIGURED) &&
83 			    !(sc->sc_flags & ATT_ATTACHED)) {
84 				sc->sc_flags |= ATT_ATTACHED;
85 				return sc;
86 			}
87 		}
88 	return NULL;
89 }
90 
91 void
92 attimer_set_pitch(struct attimer_softc *sc, int pitch)
93 {
94 	int s;
95 	s = splhigh();
96 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, TIMER_MODE,
97 	    TIMER_SEL2 | TIMER_16BIT | TIMER_SQWAVE);
98 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, TIMER_CNTR2,
99 	    TIMER_DIV(pitch) % 256);
100 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, TIMER_CNTR2,
101 	    TIMER_DIV(pitch) / 256);
102 	splx(s);
103 }
104