asc.c revision 1.31 1 /* $NetBSD: asc.c,v 1.31 1999/04/22 18:00:34 ender 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
172 for (i = 0; i < 256; i++) { /* up part of wave, four voices? */
173 asc_wave_tab[i] = i / 4;
174 asc_wave_tab[i + 512] = i / 4;
175 asc_wave_tab[i + 1024] = i / 4;
176 asc_wave_tab[i + 1536] = i / 4;
177 }
178 for (i = 0; i < 256; i++) { /* down part of wave, four voices? */
179 asc_wave_tab[i + 256] = 0x3f - (i / 4);
180 asc_wave_tab[i + 768] = 0x3f - (i / 4);
181 asc_wave_tab[i + 1280] = 0x3f - (i / 4);
182 asc_wave_tab[i + 1792] = 0x3f - (i / 4);
183 }
184
185 printf(": Apple Sound Chip");
186 if (oa->oa_addr != (-1))
187 printf(" at %x", oa->oa_addr);
188 printf("\n");
189
190 mac68k_set_bell_callback(asc_ring_bell, sc);
191 #if __notyet__
192 via2_register_irq(VIA2_ASC, asc_intr, sc);
193 asc_intr_enable();
194 #endif
195 }
196
197 int
198 ascopen(dev, flag, mode, p)
199 dev_t dev;
200 int flag;
201 int mode;
202 struct proc *p;
203 {
204 struct asc_softc *sc;
205 int unit;
206
207 unit = ASCUNIT(dev);
208 sc = asc_cd.cd_devs[unit];
209 if (unit >= asc_cd.cd_ndevs)
210 return (ENXIO);
211 if (sc->sc_open)
212 return (EBUSY);
213 sc->sc_open = 1;
214
215 return (0);
216 }
217
218 int
219 ascclose(dev, flag, mode, p)
220 dev_t dev;
221 int flag;
222 int mode;
223 struct proc *p;
224 {
225 struct asc_softc *sc;
226
227 sc = asc_cd.cd_devs[ASCUNIT(dev)];
228 sc->sc_open = 0;
229
230 return (0);
231 }
232
233 int
234 ascread(dev, uio, ioflag)
235 dev_t dev;
236 struct uio *uio;
237 int ioflag;
238 {
239 return (ENXIO);
240 }
241
242 int
243 ascwrite(dev, uio, ioflag)
244 dev_t dev;
245 struct uio *uio;
246 int ioflag;
247 {
248 return (ENXIO);
249 }
250
251 int
252 ascioctl(dev, cmd, data, flag, p)
253 dev_t dev;
254 int cmd;
255 caddr_t data;
256 int flag;
257 struct proc *p;
258 {
259 struct asc_softc *sc;
260 int error;
261 int unit = ASCUNIT(dev);
262
263 sc = asc_cd.cd_devs[unit];
264 error = 0;
265
266 switch (cmd) {
267 default:
268 error = EINVAL;
269 break;
270 }
271 return (error);
272 }
273
274 int
275 ascpoll(dev, events, p)
276 dev_t dev;
277 int events;
278 struct proc *p;
279 {
280 return (events & (POLLOUT | POLLWRNORM));
281 }
282
283 int
284 ascmmap(dev, off, prot)
285 dev_t dev;
286 int off;
287 int prot;
288 {
289 int unit = ASCUNIT(dev);
290 struct asc_softc *sc;
291 paddr_t pa;
292
293 sc = asc_cd.cd_devs[unit];
294 if ((u_int)off < MAC68K_ASC_LEN) {
295 pa = pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_handle);
296 return m68k_btop(pa + off);
297 }
298
299 return (-1);
300 }
301
302 static int
303 asc_ring_bell(arg, freq, length, volume)
304 void *arg;
305 int freq, length, volume;
306 {
307 struct asc_softc *sc = (struct asc_softc *)arg;
308 unsigned long cfreq;
309 int i;
310
311 if (!sc)
312 return (ENODEV);
313
314 if (sc->sc_ringing == 0) {
315
316 bus_space_write_multi_1(sc->sc_tag, sc->sc_handle,
317 0, 0, 0x800);
318 bus_space_write_region_1(sc->sc_tag, sc->sc_handle,
319 0, asc_wave_tab, 0x800);
320
321 /* Fix this. Need to find exact ASC sampling freq */
322 cfreq = 65536 * freq / 466;
323
324 /* printf("beep: from %d, %02x %02x %02x %02x\n",
325 * cur_beep.freq, (cfreq >> 24) & 0xff, (cfreq >> 16) & 0xff,
326 * (cfreq >> 8) & 0xff, (cfreq) & 0xff); */
327 for (i = 0; i < 8; i++) {
328 bus_space_write_1(sc->sc_tag, sc->sc_handle,
329 0x814 + 8 * i, (cfreq >> 24) & 0xff);
330 bus_space_write_1(sc->sc_tag, sc->sc_handle,
331 0x815 + 8 * i, (cfreq >> 16) & 0xff);
332 bus_space_write_1(sc->sc_tag, sc->sc_handle,
333 0x816 + 8 * i, (cfreq >> 8) & 0xff);
334 bus_space_write_1(sc->sc_tag, sc->sc_handle,
335 0x817 + 8 * i, (cfreq) & 0xff);
336 } /* frequency; should put cur_beep.freq in here
337 * somewhere. */
338
339 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x807, 3); /* 44 ? */
340 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x806,
341 255 * volume / 100);
342 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x805, 0);
343 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80f, 0);
344 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x802, 2); /* sampled */
345 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 2); /* enable sampled */
346 }
347 sc->sc_ringing++;
348 timeout(asc_stop_bell, sc, length);
349
350 return (0);
351 }
352
353 static void
354 asc_stop_bell(arg)
355 void *arg;
356 {
357 struct asc_softc *sc = (struct asc_softc *)arg;
358
359 if (!sc)
360 return;
361
362 if (sc->sc_ringing > 1000 || sc->sc_ringing < 0)
363 panic("bell got out of sync?");
364
365 if (--sc->sc_ringing == 0) /* disable ASC */
366 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 0);
367 }
368
369 #if __notyet__
370 static void
371 asc_intr_enable()
372 {
373 int s;
374
375 s = splhigh();
376 if (VIA2 == VIA2OFF)
377 via2_reg(vIER) = 0x80 | V2IF_ASC;
378 else
379 via2_reg(rIER) = 0x80 | V2IF_ASC;
380 splx(s);
381 }
382
383
384 /*ARGSUSED*/
385 static void
386 asc_intr(arg)
387 void *arg;
388 {
389 #ifdef ASC_DEBUG
390 struct asc_softc *sc = (struct asc_softc *)arg;
391
392 if (asc_debug)
393 printf("asc_intr(%p)\n", sc);
394 #endif
395 }
396 #endif
397