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