asc.c revision 1.17 1 1.17 scottr /* $NetBSD: asc.c,v 1.17 1997/02/11 07:47:36 scottr Exp $ */
2 1.6 cgd
3 1.17 scottr /*
4 1.17 scottr * Copyright (C) 1997 Scott Reynolds
5 1.17 scottr * All rights reserved.
6 1.17 scottr *
7 1.17 scottr * Redistribution and use in source and binary forms, with or without
8 1.17 scottr * modification, are permitted provided that the following conditions
9 1.17 scottr * are met:
10 1.17 scottr * 1. Redistributions of source code must retain the above copyright
11 1.17 scottr * notice, this list of conditions and the following disclaimer.
12 1.17 scottr * 2. Redistributions in binary form must reproduce the above copyright
13 1.17 scottr * notice, this list of conditions and the following disclaimer in the
14 1.17 scottr * documentation and/or other materials provided with the distribution.
15 1.17 scottr * 3. All advertising materials mentioning features or use of this software
16 1.17 scottr * must display the following acknowledgement:
17 1.17 scottr * This product includes software developed by Scott Reynolds.
18 1.17 scottr * 4. The name of the author may not be used to endorse or promote products
19 1.17 scottr * derived from this software without specific prior written permission.
20 1.17 scottr *
21 1.17 scottr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.17 scottr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.17 scottr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.17 scottr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
25 1.17 scottr * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.17 scottr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.17 scottr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.17 scottr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.17 scottr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.17 scottr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.17 scottr * POSSIBILITY OF SUCH DAMAGE.
32 1.17 scottr */
33 1.1 briggs /*-
34 1.1 briggs * Copyright (C) 1993 Allen K. Briggs, Chris P. Caputo,
35 1.1 briggs * Michael L. Finch, Bradley A. Grantham, and
36 1.1 briggs * Lawrence A. Kesteloot
37 1.1 briggs * All rights reserved.
38 1.1 briggs *
39 1.1 briggs * Redistribution and use in source and binary forms, with or without
40 1.1 briggs * modification, are permitted provided that the following conditions
41 1.1 briggs * are met:
42 1.1 briggs * 1. Redistributions of source code must retain the above copyright
43 1.1 briggs * notice, this list of conditions and the following disclaimer.
44 1.1 briggs * 2. Redistributions in binary form must reproduce the above copyright
45 1.1 briggs * notice, this list of conditions and the following disclaimer in the
46 1.1 briggs * documentation and/or other materials provided with the distribution.
47 1.1 briggs * 3. All advertising materials mentioning features or use of this software
48 1.1 briggs * must display the following acknowledgement:
49 1.1 briggs * This product includes software developed by the Alice Group.
50 1.1 briggs * 4. The names of the Alice Group or any of its members may not be used
51 1.1 briggs * to endorse or promote products derived from this software without
52 1.1 briggs * specific prior written permission.
53 1.1 briggs *
54 1.1 briggs * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
55 1.1 briggs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 1.1 briggs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 1.1 briggs * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
58 1.1 briggs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59 1.1 briggs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 1.1 briggs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 1.1 briggs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 1.1 briggs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
63 1.1 briggs * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 1.1 briggs */
65 1.1 briggs
66 1.1 briggs /*
67 1.17 scottr * ASC driver code and console bell support
68 1.1 briggs */
69 1.1 briggs
70 1.1 briggs #include <sys/types.h>
71 1.8 briggs #include <sys/cdefs.h>
72 1.1 briggs #include <sys/errno.h>
73 1.3 briggs #include <sys/time.h>
74 1.1 briggs #include <sys/systm.h>
75 1.1 briggs #include <sys/param.h>
76 1.5 briggs #include <sys/device.h>
77 1.17 scottr #include <sys/poll.h>
78 1.17 scottr
79 1.17 scottr #include <vm/vm.h>
80 1.17 scottr #include <vm/pmap.h>
81 1.14 briggs
82 1.14 briggs #include <machine/autoconf.h>
83 1.5 briggs #include <machine/cpu.h>
84 1.16 scottr #include <machine/bus.h>
85 1.1 briggs
86 1.11 briggs #include "ascvar.h"
87 1.16 scottr #include "obiovar.h"
88 1.1 briggs
89 1.16 scottr #define MAC68K_ASC_BASE ((caddr_t) 0x50f14000)
90 1.16 scottr #define MAC68K_ASC_LEN 0x01000
91 1.1 briggs
92 1.17 scottr static u_int8_t asc_wave_tab[0x800];
93 1.17 scottr
94 1.17 scottr static int asc_ring_bell __P((void *, int, int, int));
95 1.17 scottr static void asc_stop_bell __P((void *));
96 1.1 briggs
97 1.17 scottr static int ascmatch __P((struct device *, struct cfdata *, void *));
98 1.17 scottr static void ascattach __P((struct device *, struct device *, void *));
99 1.5 briggs
100 1.10 thorpej struct cfattach asc_ca = {
101 1.17 scottr sizeof(struct asc_softc), ascmatch, ascattach
102 1.10 thorpej };
103 1.10 thorpej
104 1.10 thorpej struct cfdriver asc_cd = {
105 1.10 thorpej NULL, "asc", DV_DULL, NULL, 0
106 1.10 thorpej };
107 1.5 briggs
108 1.5 briggs static int
109 1.15 scottr ascmatch(parent, cf, aux)
110 1.15 scottr struct device *parent;
111 1.15 scottr struct cfdata *cf;
112 1.15 scottr void *aux;
113 1.5 briggs {
114 1.16 scottr struct obio_attach_args *oa = aux;
115 1.16 scottr bus_space_tag_t bst = MAC68K_BUS_SPACE_MEM;
116 1.16 scottr bus_space_handle_t bsh;
117 1.16 scottr bus_addr_t addr;
118 1.16 scottr int rval = 0;
119 1.16 scottr
120 1.16 scottr addr = (bus_addr_t) (oa->oa_addr ? oa->oa_addr : MAC68K_ASC_BASE);
121 1.16 scottr
122 1.16 scottr if (bus_space_map(bst, addr, MAC68K_ASC_LEN, 0, &bsh))
123 1.16 scottr return (0);
124 1.16 scottr
125 1.16 scottr if (bus_probe(bst, bsh, 0, 1))
126 1.16 scottr rval = 1;
127 1.16 scottr else
128 1.16 scottr rval = 0;
129 1.16 scottr
130 1.16 scottr bus_space_unmap(bst, bsh, MAC68K_ASC_LEN);
131 1.16 scottr
132 1.16 scottr return rval;
133 1.5 briggs }
134 1.1 briggs
135 1.5 briggs static void
136 1.16 scottr ascattach(parent, self, aux)
137 1.16 scottr struct device *parent, *self;
138 1.16 scottr void *aux;
139 1.1 briggs {
140 1.17 scottr struct asc_softc *sc = (struct asc_softc *) self;
141 1.16 scottr struct obio_attach_args *oa = aux;
142 1.16 scottr bus_addr_t addr;
143 1.17 scottr int i;
144 1.16 scottr
145 1.17 scottr sc->sc_tag = MAC68K_BUS_SPACE_MEM;
146 1.16 scottr addr = (bus_addr_t) (oa->oa_addr ? oa->oa_addr : MAC68K_ASC_BASE);
147 1.17 scottr if (bus_space_map(sc->sc_tag, addr, MAC68K_ASC_LEN, 0,
148 1.17 scottr &sc->sc_handle)) {
149 1.17 scottr printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
150 1.16 scottr return;
151 1.16 scottr }
152 1.17 scottr sc->sc_open = 0;
153 1.17 scottr sc->sc_ringing = 0;
154 1.17 scottr
155 1.17 scottr for (i = 0; i < 256; i++) { /* up part of wave, four voices? */
156 1.17 scottr asc_wave_tab[i] = i / 4;
157 1.17 scottr asc_wave_tab[i + 512] = i / 4;
158 1.17 scottr asc_wave_tab[i + 1024] = i / 4;
159 1.17 scottr asc_wave_tab[i + 1536] = i / 4;
160 1.17 scottr }
161 1.17 scottr for (i = 0; i < 256; i++) { /* down part of wave, four voices? */
162 1.17 scottr asc_wave_tab[i + 256] = 0x3f - (i / 4);
163 1.17 scottr asc_wave_tab[i + 768] = 0x3f - (i / 4);
164 1.17 scottr asc_wave_tab[i + 1280] = 0x3f - (i / 4);
165 1.17 scottr asc_wave_tab[i + 1792] = 0x3f - (i / 4);
166 1.17 scottr }
167 1.17 scottr
168 1.17 scottr printf(": Apple Sound Chip");
169 1.17 scottr if (oa->oa_addr)
170 1.17 scottr printf(" at %p", oa->oa_addr);
171 1.17 scottr printf("\n");
172 1.16 scottr
173 1.17 scottr mac68k_set_bell_callback(asc_ring_bell, sc);
174 1.1 briggs }
175 1.1 briggs
176 1.17 scottr int
177 1.17 scottr ascopen(dev, flag, mode, p)
178 1.17 scottr dev_t dev;
179 1.17 scottr int flag;
180 1.17 scottr int mode;
181 1.17 scottr struct proc *p;
182 1.16 scottr {
183 1.17 scottr struct asc_softc *sc;
184 1.17 scottr int unit;
185 1.16 scottr
186 1.17 scottr unit = ASCUNIT(dev);
187 1.17 scottr sc = asc_cd.cd_devs[unit];
188 1.17 scottr if (unit >= asc_cd.cd_ndevs)
189 1.17 scottr return (ENXIO);
190 1.17 scottr if (sc->sc_open)
191 1.17 scottr return (EBUSY);
192 1.17 scottr sc->sc_open = 1;
193 1.1 briggs
194 1.7 briggs return (0);
195 1.1 briggs }
196 1.1 briggs
197 1.17 scottr int
198 1.17 scottr ascclose(dev, flag, mode, p)
199 1.17 scottr dev_t dev;
200 1.17 scottr int flag;
201 1.17 scottr int mode;
202 1.17 scottr struct proc *p;
203 1.1 briggs {
204 1.17 scottr struct asc_softc *sc;
205 1.14 briggs
206 1.17 scottr sc = asc_cd.cd_devs[ASCUNIT(dev)];
207 1.17 scottr sc->sc_open = 0;
208 1.1 briggs
209 1.7 briggs return (0);
210 1.1 briggs }
211 1.1 briggs
212 1.17 scottr int
213 1.17 scottr ascread(dev, uio, ioflag)
214 1.17 scottr dev_t dev;
215 1.17 scottr struct uio *uio;
216 1.17 scottr int ioflag;
217 1.17 scottr {
218 1.17 scottr return (ENXIO);
219 1.17 scottr }
220 1.1 briggs
221 1.17 scottr int
222 1.17 scottr ascwrite(dev, uio, ioflag)
223 1.17 scottr dev_t dev;
224 1.17 scottr struct uio *uio;
225 1.17 scottr int ioflag;
226 1.1 briggs {
227 1.17 scottr return (ENXIO);
228 1.17 scottr }
229 1.14 briggs
230 1.17 scottr int
231 1.17 scottr ascioctl(dev, cmd, data, flag, p)
232 1.17 scottr dev_t dev;
233 1.17 scottr int cmd;
234 1.17 scottr caddr_t data;
235 1.17 scottr int flag;
236 1.17 scottr struct proc *p;
237 1.17 scottr {
238 1.17 scottr struct asc_softc *sc;
239 1.17 scottr int error;
240 1.17 scottr int unit = ASCUNIT(dev);
241 1.17 scottr
242 1.17 scottr sc = asc_cd.cd_devs[unit];
243 1.17 scottr error = 0;
244 1.17 scottr
245 1.17 scottr switch (cmd) {
246 1.17 scottr default:
247 1.17 scottr error = EINVAL;
248 1.17 scottr break;
249 1.17 scottr }
250 1.17 scottr return (error);
251 1.17 scottr }
252 1.16 scottr
253 1.17 scottr int
254 1.17 scottr ascpoll(dev, events, p)
255 1.17 scottr dev_t dev;
256 1.17 scottr int events;
257 1.17 scottr struct proc *p;
258 1.17 scottr {
259 1.17 scottr return (events & (POLLOUT | POLLWRNORM));
260 1.1 briggs }
261 1.1 briggs
262 1.17 scottr int
263 1.17 scottr ascmmap(dev, off, prot)
264 1.17 scottr dev_t dev;
265 1.17 scottr int off;
266 1.17 scottr int prot;
267 1.17 scottr {
268 1.17 scottr int unit = ASCUNIT(dev);
269 1.17 scottr struct asc_softc *sc;
270 1.17 scottr vm_offset_t pa;
271 1.17 scottr
272 1.17 scottr sc = asc_cd.cd_devs[unit];
273 1.17 scottr if (off < MAC68K_ASC_LEN) {
274 1.17 scottr pa = pmap_extract(pmap_kernel(), (vm_offset_t) sc->sc_handle);
275 1.17 scottr return mac68k_btop(pa + off);
276 1.17 scottr }
277 1.17 scottr
278 1.17 scottr return (-1);
279 1.17 scottr }
280 1.1 briggs
281 1.17 scottr static int
282 1.17 scottr asc_ring_bell(arg, freq, length, volume)
283 1.17 scottr void *arg;
284 1.17 scottr int freq, length, volume;
285 1.1 briggs {
286 1.17 scottr struct asc_softc *sc = (struct asc_softc *) arg;
287 1.17 scottr unsigned long cfreq;
288 1.17 scottr int i;
289 1.14 briggs
290 1.17 scottr if (!sc)
291 1.16 scottr return (ENODEV);
292 1.1 briggs
293 1.17 scottr if (sc->sc_ringing == 0) {
294 1.16 scottr
295 1.17 scottr bus_space_write_multi_1(sc->sc_tag, sc->sc_handle,
296 1.17 scottr 0, 0, 0x800);
297 1.17 scottr bus_space_write_region_1(sc->sc_tag, sc->sc_handle,
298 1.17 scottr 0, asc_wave_tab, 0x800);
299 1.1 briggs
300 1.7 briggs /* Fix this. Need to find exact ASC sampling freq */
301 1.17 scottr cfreq = 65536 * freq / 466;
302 1.1 briggs
303 1.13 christos /* printf("beep: from %d, %02x %02x %02x %02x\n",
304 1.17 scottr * cur_beep.freq, (cfreq >> 24) & 0xff, (cfreq >> 16) & 0xff,
305 1.17 scottr * (cfreq >> 8) & 0xff, (cfreq) & 0xff); */
306 1.7 briggs for (i = 0; i < 8; i++) {
307 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle,
308 1.17 scottr 0x814 + 8 * i, (cfreq >> 24) & 0xff);
309 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle,
310 1.17 scottr 0x815 + 8 * i, (cfreq >> 16) & 0xff);
311 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle,
312 1.17 scottr 0x816 + 8 * i, (cfreq >> 8) & 0xff);
313 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle,
314 1.17 scottr 0x817 + 8 * i, (cfreq) & 0xff);
315 1.7 briggs } /* frequency; should put cur_beep.freq in here
316 1.7 briggs * somewhere. */
317 1.7 briggs
318 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x807, 3); /* 44 ? */
319 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x806,
320 1.17 scottr 255 * volume / 100);
321 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x805, 0);
322 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80f, 0);
323 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x802, 2); /* sampled */
324 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 2); /* enable sampled */
325 1.1 briggs }
326 1.17 scottr sc->sc_ringing++;
327 1.17 scottr timeout((void *) asc_stop_bell, sc, length);
328 1.11 briggs
329 1.16 scottr return (0);
330 1.17 scottr }
331 1.17 scottr
332 1.17 scottr static void
333 1.17 scottr asc_stop_bell(arg)
334 1.17 scottr void *arg;
335 1.17 scottr {
336 1.17 scottr struct asc_softc *sc = (struct asc_softc *) arg;
337 1.17 scottr
338 1.17 scottr if (!sc)
339 1.17 scottr return;
340 1.17 scottr
341 1.17 scottr if (sc->sc_ringing > 1000 || sc->sc_ringing < 0)
342 1.17 scottr panic("bell got out of sync?");
343 1.17 scottr
344 1.17 scottr if (--sc->sc_ringing == 0) /* disable ASC */
345 1.17 scottr bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 0);
346 1.1 briggs }
347