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