No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ic / attimer.c
blob024636bbbb633d7376caf3b230347da93c1a556b
1 /* $NetBSD: attimer.c,v 1.9 2008/06/12 22:30:30 cegger Exp $ */
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation.
5 * All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * AT Timer
32 * This code only allows control over the pitch of the speaker (pcppi(4)).
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: attimer.c,v 1.9 2008/06/12 22:30:30 cegger Exp $");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
43 #include <sys/bus.h>
45 #include <dev/isa/isareg.h>
47 #include <dev/ic/attimervar.h>
48 #include <dev/ic/i8253reg.h>
50 extern struct cfdriver attimer_cd;
52 void
53 attimer_attach(struct attimer_softc *sc)
55 sc->sc_flags |= ATT_CONFIGURED;
57 if (!pmf_device_register(sc->sc_dev, NULL, NULL))
58 aprint_error_dev(sc->sc_dev, "couldn't establish power handler\n");
61 int
62 attimer_detach(device_t self, int flags)
64 struct attimer_softc *sc = device_private(self);
66 if ((sc->sc_flags & ATT_ATTACHED) != 0)
67 return EBUSY;
69 pmf_device_deregister(self);
70 sc->sc_flags &= ~ATT_CONFIGURED;
71 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
72 return 0;
76 * This is slightly artificial. While the code looks fine, I can't help
77 * but pray I'll get the attimer object associated to the pcppi object
78 * that calls the routine.
80 * There's not much at risk here, as there's about no chance to have a
81 * computer with more than one pcppi/attimer accessible.
84 device_t
85 attimer_attach_speaker(void)
87 int i;
88 struct attimer_softc *sc;
90 for (i = 0; i < attimer_cd.cd_ndevs; i++) {
91 sc = device_lookup_private(&attimer_cd, i);
92 if (sc == NULL)
93 continue;
94 if ((sc->sc_flags & ATT_CONFIGURED) &&
95 !(sc->sc_flags & ATT_ATTACHED)) {
96 sc->sc_flags |= ATT_ATTACHED;
97 return sc->sc_dev;
100 return NULL;
103 void
104 attimer_detach_speaker(device_t dev)
106 struct attimer_softc *sc = device_private(dev);
108 sc->sc_flags &= ~ATT_ATTACHED;
111 void
112 attimer_set_pitch(device_t dev, int pitch)
114 struct attimer_softc *sc = device_private(dev);
115 int s;
117 s = splhigh();
118 bus_space_write_1(sc->sc_iot, sc->sc_ioh, TIMER_MODE,
119 TIMER_SEL2 | TIMER_16BIT | TIMER_SQWAVE);
120 bus_space_write_1(sc->sc_iot, sc->sc_ioh, TIMER_CNTR2,
121 TIMER_DIV(pitch) % 256);
122 bus_space_write_1(sc->sc_iot, sc->sc_ioh, TIMER_CNTR2,
123 TIMER_DIV(pitch) / 256);
124 splx(s);