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