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