asc.c revision 1.59 1 /* $NetBSD: asc.c,v 1.59 2025/01/13 16:23:48 riastradh 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/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: asc.c,v 1.59 2025/01/13 16:23:48 riastradh Exp $");
68
69 #include <sys/types.h>
70 #include <sys/errno.h>
71 #include <sys/time.h>
72 #include <sys/systm.h>
73 #include <sys/param.h>
74 #include <sys/device.h>
75 #include <sys/conf.h>
76
77 #include <uvm/uvm_extern.h>
78
79 #include <machine/autoconf.h>
80 #include <machine/cpu.h>
81 #include <machine/bus.h>
82 #include <machine/viareg.h>
83
84 #include <mac68k/obio/ascvar.h>
85 #include <mac68k/obio/obiovar.h>
86
87 #define MAC68K_ASC_BASE 0x50f14000
88 #define MAC68K_IIFX_ASC_BASE 0x50f10000
89 #define MAC68K_ASC_LEN 0x01000
90
91 #ifdef DEBUG
92 #define ASC_DEBUG
93 #endif
94
95 #ifdef ASC_DEBUG
96 int asc_debug = 0; /* non-zero enables debugging output */
97 #endif
98
99 static u_int8_t asc_wave_tab[0x800];
100
101 static int asc_ring_bell(void *, int, int, int);
102 static void asc_stop_bell(void *);
103 #if __notyet__
104 static void asc_intr_enable(void);
105 static void asc_intr(void *);
106 #endif
107
108 static int ascmatch(device_t, cfdata_t, void *);
109 static void ascattach(device_t, device_t, void *);
110
111 CFATTACH_DECL_NEW(asc, sizeof(struct asc_softc),
112 ascmatch, ascattach, NULL, NULL);
113
114 extern struct cfdriver asc_cd;
115
116 dev_type_open(ascopen);
117 dev_type_close(ascclose);
118 dev_type_read(ascread);
119 dev_type_write(ascwrite);
120 dev_type_ioctl(ascioctl);
121 dev_type_mmap(ascmmap);
122
123 const struct cdevsw asc_cdevsw = {
124 .d_open = ascopen,
125 .d_close = ascclose,
126 .d_read = ascread,
127 .d_write = ascwrite,
128 .d_ioctl = ascioctl,
129 .d_stop = nostop,
130 .d_tty = notty,
131 .d_poll = nopoll,
132 .d_mmap = ascmmap,
133 .d_kqfilter = nokqfilter,
134 .d_discard = nodiscard,
135 .d_flag = 0
136 };
137
138 static const uint8_t easc_version_tab[] = { 0xb0 };
139
140 static int
141 ascmatch(device_t parent, cfdata_t cf, void *aux)
142 {
143 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
144 bus_addr_t addr;
145 bus_space_handle_t bsh;
146 int rval = 0;
147 uint8_t ver;
148
149 if (oa->oa_addr != (-1))
150 addr = (bus_addr_t)oa->oa_addr;
151 else if (current_mac_model->machineid == MACH_MACTV)
152 return 0;
153 else if (current_mac_model->machineid == MACH_MACIIFX)
154 addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
155 else
156 addr = (bus_addr_t)MAC68K_ASC_BASE;
157
158 if (bus_space_map(oa->oa_tag, addr, MAC68K_ASC_LEN, 0, &bsh))
159 return (0);
160
161 if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1)) {
162 rval = 1;
163
164 /*
165 * Enhanced Apple Sound Chip (EASC) does not support wavetable
166 * mode, exclude it for now.
167 */
168 ver = bus_space_read_1(oa->oa_tag, bsh, 0x800);
169 for (size_t i = 0; i < __arraycount(easc_version_tab); i++)
170 if (ver == easc_version_tab[i]) {
171 rval = 0;
172 break;
173 }
174 } else
175 rval = 0;
176
177 bus_space_unmap(oa->oa_tag, bsh, MAC68K_ASC_LEN);
178
179 return rval;
180 }
181
182 static void
183 ascattach(device_t parent, device_t self, void *aux)
184 {
185 struct asc_softc *sc = device_private(self);
186 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
187 bus_addr_t addr;
188 int i;
189
190 sc->sc_dev = self;
191 sc->sc_tag = oa->oa_tag;
192 if (oa->oa_addr != (-1))
193 addr = (bus_addr_t)oa->oa_addr;
194 else if (current_mac_model->machineid == MACH_MACIIFX)
195 addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
196 else
197 addr = (bus_addr_t)MAC68K_ASC_BASE;
198 if (bus_space_map(sc->sc_tag, addr, MAC68K_ASC_LEN, 0,
199 &sc->sc_handle)) {
200 printf(": can't map memory space\n");
201 return;
202 }
203 sc->sc_open = 0;
204 sc->sc_ringing = 0;
205 callout_init(&sc->sc_bell_ch, 0);
206
207 for (i = 0; i < 256; i++) { /* up part of wave, four voices? */
208 asc_wave_tab[i] = i / 4;
209 asc_wave_tab[i + 512] = i / 4;
210 asc_wave_tab[i + 1024] = i / 4;
211 asc_wave_tab[i + 1536] = i / 4;
212 }
213 for (i = 0; i < 256; i++) { /* down part of wave, four voices? */
214 asc_wave_tab[i + 256] = 0x3f - (i / 4);
215 asc_wave_tab[i + 768] = 0x3f - (i / 4);
216 asc_wave_tab[i + 1280] = 0x3f - (i / 4);
217 asc_wave_tab[i + 1792] = 0x3f - (i / 4);
218 }
219
220 printf(": Apple Sound Chip");
221 if (oa->oa_addr != (-1))
222 printf(" at %x", oa->oa_addr);
223 printf("\n");
224
225 mac68k_set_bell_callback(asc_ring_bell, sc);
226 #if __notyet__
227 if (mac68k_machine.aux_interrupts) {
228 intr_establish((int (*)(void *))asc_intr, sc, 5);
229 } else {
230 via2_register_irq(VIA2_ASC, asc_intr, sc);
231 }
232 asc_intr_enable();
233 #endif
234 }
235
236 int
237 ascopen(dev_t dev, int flag, int mode, struct lwp *l)
238 {
239 struct asc_softc *sc;
240
241 sc = device_lookup_private(&asc_cd, ASCUNIT(dev));
242 if (sc == NULL)
243 return (ENXIO);
244 if (sc->sc_open)
245 return (EBUSY);
246 sc->sc_open = 1;
247
248 return (0);
249 }
250
251 int
252 ascclose(dev_t dev, int flag, int mode, struct lwp *l)
253 {
254 struct asc_softc *sc;
255
256 sc = device_lookup_private(&asc_cd, ASCUNIT(dev));
257 sc->sc_open = 0;
258
259 return (0);
260 }
261
262 int
263 ascread(dev_t dev, struct uio *uio, int ioflag)
264 {
265 return (ENXIO);
266 }
267
268 int
269 ascwrite(dev_t dev, struct uio *uio, int ioflag)
270 {
271 return (ENXIO);
272 }
273
274 int
275 ascioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
276 {
277 int error;
278 #ifdef not_yet
279 struct asc_softc *sc;
280 int unit = ASCUNIT(dev);
281
282 sc = device_lookup_private(&asc_cd, unit);
283 #endif
284 error = 0;
285
286 switch (cmd) {
287 default:
288 error = EINVAL;
289 break;
290 }
291 return (error);
292 }
293
294 paddr_t
295 ascmmap(dev_t dev, off_t off, int prot)
296 {
297 struct asc_softc *sc;
298 paddr_t pa;
299
300 sc = device_lookup_private(&asc_cd, ASCUNIT(dev));
301 if ((u_int)off < MAC68K_ASC_LEN) {
302 (void) pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_handle.base,
303 &pa);
304 return m68k_btop(pa + off);
305 }
306
307 return (-1);
308 }
309
310 static int
311 asc_ring_bell(void *arg, int freq, int length, int volume)
312 {
313 struct asc_softc *sc = (struct asc_softc *)arg;
314 unsigned long cfreq;
315 int i;
316
317 if (!sc)
318 return (ENODEV);
319
320 if (sc->sc_ringing == 0) {
321
322 bus_space_write_multi_1(sc->sc_tag, sc->sc_handle,
323 0, 0, 0x800);
324 bus_space_write_region_1(sc->sc_tag, sc->sc_handle,
325 0, asc_wave_tab, 0x800);
326
327 /* Fix this. Need to find exact ASC sampling freq */
328 cfreq = 65536 * freq / 466;
329
330 /* printf("beep: from %d, %02x %02x %02x %02x\n",
331 * cur_beep.freq, (cfreq >> 24) & 0xff, (cfreq >> 16) & 0xff,
332 * (cfreq >> 8) & 0xff, (cfreq) & 0xff); */
333 for (i = 0; i < 8; i++) {
334 bus_space_write_1(sc->sc_tag, sc->sc_handle,
335 0x814 + 8 * i, (cfreq >> 24) & 0xff);
336 bus_space_write_1(sc->sc_tag, sc->sc_handle,
337 0x815 + 8 * i, (cfreq >> 16) & 0xff);
338 bus_space_write_1(sc->sc_tag, sc->sc_handle,
339 0x816 + 8 * i, (cfreq >> 8) & 0xff);
340 bus_space_write_1(sc->sc_tag, sc->sc_handle,
341 0x817 + 8 * i, (cfreq) & 0xff);
342 } /* frequency; should put cur_beep.freq in here
343 * somewhere. */
344
345 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x807, 3); /* 44 ? */
346 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x806,
347 255 * volume / 100);
348 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x805, 0);
349 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80f, 0);
350 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x802, 2); /* sampled */
351 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 2); /* enable sampled */
352 sc->sc_ringing = 1;
353 callout_reset(&sc->sc_bell_ch, length, asc_stop_bell, sc);
354 }
355
356 return (0);
357 }
358
359 static void
360 asc_stop_bell(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(void)
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 /*ARGSUSED*/
389 static void
390 asc_intr(void *arg)
391 {
392 #ifdef ASC_DEBUG
393 struct asc_softc *sc = (struct asc_softc *)arg;
394
395 if (asc_debug)
396 printf("asc_intr(%p)\n", sc);
397 #endif
398 }
399 #endif
400