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