asc.c revision 1.27 1 /* $NetBSD: asc.c,v 1.27 1998/05/02 16:45:30 scottr 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
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 static u_int8_t asc_wave_tab[0x800];
90
91 static int asc_ring_bell __P((void *, int, int, int));
92 static void asc_stop_bell __P((void *));
93
94 static int ascmatch __P((struct device *, struct cfdata *, void *));
95 static void ascattach __P((struct device *, struct device *, void *));
96
97 struct cfattach asc_ca = {
98 sizeof(struct asc_softc), ascmatch, ascattach
99 };
100
101 extern struct cfdriver asc_cd;
102
103 static int
104 ascmatch(parent, cf, aux)
105 struct device *parent;
106 struct cfdata *cf;
107 void *aux;
108 {
109 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
110 bus_addr_t addr;
111 bus_space_handle_t bsh;
112 int rval = 0;
113
114 if (oa->oa_addr != (-1))
115 addr = (bus_addr_t)oa->oa_addr;
116 else if (current_mac_model->machineid == MACH_MACIIFX)
117 addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
118 else
119 addr = (bus_addr_t)MAC68K_ASC_BASE;
120
121 if (bus_space_map(oa->oa_tag, addr, MAC68K_ASC_LEN, 0, &bsh))
122 return (0);
123
124 if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1))
125 rval = 1;
126 else
127 rval = 0;
128
129 bus_space_unmap(oa->oa_tag, bsh, MAC68K_ASC_LEN);
130
131 return rval;
132 }
133
134 static void
135 ascattach(parent, self, aux)
136 struct device *parent, *self;
137 void *aux;
138 {
139 struct asc_softc *sc = (struct asc_softc *)self;
140 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
141 bus_addr_t addr;
142 int i;
143
144 sc->sc_tag = oa->oa_tag;
145 if (oa->oa_addr != (-1))
146 addr = (bus_addr_t)oa->oa_addr;
147 else if (current_mac_model->machineid == MACH_MACIIFX)
148 addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
149 else
150 addr = (bus_addr_t)MAC68K_ASC_BASE;
151 if (bus_space_map(sc->sc_tag, addr, MAC68K_ASC_LEN, 0,
152 &sc->sc_handle)) {
153 printf(": can't map memory space\n");
154 return;
155 }
156 sc->sc_open = 0;
157 sc->sc_ringing = 0;
158
159 for (i = 0; i < 256; i++) { /* up part of wave, four voices? */
160 asc_wave_tab[i] = i / 4;
161 asc_wave_tab[i + 512] = i / 4;
162 asc_wave_tab[i + 1024] = i / 4;
163 asc_wave_tab[i + 1536] = i / 4;
164 }
165 for (i = 0; i < 256; i++) { /* down part of wave, four voices? */
166 asc_wave_tab[i + 256] = 0x3f - (i / 4);
167 asc_wave_tab[i + 768] = 0x3f - (i / 4);
168 asc_wave_tab[i + 1280] = 0x3f - (i / 4);
169 asc_wave_tab[i + 1792] = 0x3f - (i / 4);
170 }
171
172 printf(": Apple Sound Chip");
173 if (oa->oa_addr != (-1))
174 printf(" at %x", oa->oa_addr);
175 printf("\n");
176
177 mac68k_set_bell_callback(asc_ring_bell, sc);
178 }
179
180 int
181 ascopen(dev, flag, mode, p)
182 dev_t dev;
183 int flag;
184 int mode;
185 struct proc *p;
186 {
187 struct asc_softc *sc;
188 int unit;
189
190 unit = ASCUNIT(dev);
191 sc = asc_cd.cd_devs[unit];
192 if (unit >= asc_cd.cd_ndevs)
193 return (ENXIO);
194 if (sc->sc_open)
195 return (EBUSY);
196 sc->sc_open = 1;
197
198 return (0);
199 }
200
201 int
202 ascclose(dev, flag, mode, p)
203 dev_t dev;
204 int flag;
205 int mode;
206 struct proc *p;
207 {
208 struct asc_softc *sc;
209
210 sc = asc_cd.cd_devs[ASCUNIT(dev)];
211 sc->sc_open = 0;
212
213 return (0);
214 }
215
216 int
217 ascread(dev, uio, ioflag)
218 dev_t dev;
219 struct uio *uio;
220 int ioflag;
221 {
222 return (ENXIO);
223 }
224
225 int
226 ascwrite(dev, uio, ioflag)
227 dev_t dev;
228 struct uio *uio;
229 int ioflag;
230 {
231 return (ENXIO);
232 }
233
234 int
235 ascioctl(dev, cmd, data, flag, p)
236 dev_t dev;
237 int cmd;
238 caddr_t data;
239 int flag;
240 struct proc *p;
241 {
242 struct asc_softc *sc;
243 int error;
244 int unit = ASCUNIT(dev);
245
246 sc = asc_cd.cd_devs[unit];
247 error = 0;
248
249 switch (cmd) {
250 default:
251 error = EINVAL;
252 break;
253 }
254 return (error);
255 }
256
257 int
258 ascpoll(dev, events, p)
259 dev_t dev;
260 int events;
261 struct proc *p;
262 {
263 return (events & (POLLOUT | POLLWRNORM));
264 }
265
266 int
267 ascmmap(dev, off, prot)
268 dev_t dev;
269 int off;
270 int prot;
271 {
272 int unit = ASCUNIT(dev);
273 struct asc_softc *sc;
274 vm_offset_t pa;
275
276 sc = asc_cd.cd_devs[unit];
277 if (off < MAC68K_ASC_LEN) {
278 pa = pmap_extract(pmap_kernel(), (vm_offset_t)sc->sc_handle);
279 return m68k_btop(pa + off);
280 }
281
282 return (-1);
283 }
284
285 static int
286 asc_ring_bell(arg, freq, length, volume)
287 void *arg;
288 int freq, length, 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 }
330 sc->sc_ringing++;
331 timeout(asc_stop_bell, sc, length);
332
333 return (0);
334 }
335
336 static void
337 asc_stop_bell(arg)
338 void *arg;
339 {
340 struct asc_softc *sc = (struct asc_softc *)arg;
341
342 if (!sc)
343 return;
344
345 if (sc->sc_ringing > 1000 || sc->sc_ringing < 0)
346 panic("bell got out of sync?");
347
348 if (--sc->sc_ringing == 0) /* disable ASC */
349 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 0);
350 }
351