1 /* $NetBSD: asc.c,v 1.52 2008/06/13 11:36:11 cegger Exp $ */
4 * Copyright (C) 1997 Scott Reynolds
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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.
63 * ASC driver code and console bell support
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: asc.c,v 1.52 2008/06/13 11:36:11 cegger Exp $");
69 #include <sys/types.h>
70 #include <sys/errno.h>
72 #include <sys/systm.h>
73 #include <sys/param.h>
74 #include <sys/device.h>
77 #include <uvm/uvm_extern.h>
79 #include <machine/autoconf.h>
80 #include <machine/cpu.h>
81 #include <machine/bus.h>
82 #include <machine/viareg.h>
84 #include <mac68k/obio/ascvar.h>
85 #include <mac68k/obio/obiovar.h>
87 #define MAC68K_ASC_BASE 0x50f14000
88 #define MAC68K_IIFX_ASC_BASE 0x50f10000
89 #define MAC68K_ASC_LEN 0x01000
96 int asc_debug
= 0; /* non-zero enables debugging output */
99 static u_int8_t asc_wave_tab
[0x800];
101 static int asc_ring_bell(void *, int, int, int);
102 static void asc_stop_bell(void *);
104 static void asc_intr_enable(void);
105 static void asc_intr(void *);
108 static int ascmatch(struct device
*, struct cfdata
*, void *);
109 static void ascattach(struct device
*, struct device
*, void *);
111 CFATTACH_DECL(asc
, sizeof(struct asc_softc
),
112 ascmatch
, ascattach
, NULL
, NULL
);
114 extern struct cfdriver asc_cd
;
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
);
123 const struct cdevsw asc_cdevsw
= {
124 ascopen
, ascclose
, ascread
, ascwrite
, ascioctl
,
125 nostop
, notty
, nopoll
, ascmmap
, nokqfilter
,
129 ascmatch(struct device
*parent
, struct cfdata
*cf
, void *aux
)
131 struct obio_attach_args
*oa
= (struct obio_attach_args
*)aux
;
133 bus_space_handle_t bsh
;
136 if (oa
->oa_addr
!= (-1))
137 addr
= (bus_addr_t
)oa
->oa_addr
;
138 else if (current_mac_model
->machineid
== MACH_MACTV
)
140 else if (current_mac_model
->machineid
== MACH_MACIIFX
)
141 addr
= (bus_addr_t
)MAC68K_IIFX_ASC_BASE
;
143 addr
= (bus_addr_t
)MAC68K_ASC_BASE
;
145 if (bus_space_map(oa
->oa_tag
, addr
, MAC68K_ASC_LEN
, 0, &bsh
))
148 if (mac68k_bus_space_probe(oa
->oa_tag
, bsh
, 0, 1))
153 bus_space_unmap(oa
->oa_tag
, bsh
, MAC68K_ASC_LEN
);
159 ascattach(struct device
*parent
, struct device
*self
, void *aux
)
161 struct asc_softc
*sc
= (struct asc_softc
*)self
;
162 struct obio_attach_args
*oa
= (struct obio_attach_args
*)aux
;
166 sc
->sc_tag
= oa
->oa_tag
;
167 if (oa
->oa_addr
!= (-1))
168 addr
= (bus_addr_t
)oa
->oa_addr
;
169 else if (current_mac_model
->machineid
== MACH_MACIIFX
)
170 addr
= (bus_addr_t
)MAC68K_IIFX_ASC_BASE
;
172 addr
= (bus_addr_t
)MAC68K_ASC_BASE
;
173 if (bus_space_map(sc
->sc_tag
, addr
, MAC68K_ASC_LEN
, 0,
175 printf(": can't map memory space\n");
180 callout_init(&sc
->sc_bell_ch
, 0);
182 for (i
= 0; i
< 256; i
++) { /* up part of wave, four voices? */
183 asc_wave_tab
[i
] = i
/ 4;
184 asc_wave_tab
[i
+ 512] = i
/ 4;
185 asc_wave_tab
[i
+ 1024] = i
/ 4;
186 asc_wave_tab
[i
+ 1536] = i
/ 4;
188 for (i
= 0; i
< 256; i
++) { /* down part of wave, four voices? */
189 asc_wave_tab
[i
+ 256] = 0x3f - (i
/ 4);
190 asc_wave_tab
[i
+ 768] = 0x3f - (i
/ 4);
191 asc_wave_tab
[i
+ 1280] = 0x3f - (i
/ 4);
192 asc_wave_tab
[i
+ 1792] = 0x3f - (i
/ 4);
195 printf(": Apple Sound Chip");
196 if (oa
->oa_addr
!= (-1))
197 printf(" at %x", oa
->oa_addr
);
200 mac68k_set_bell_callback(asc_ring_bell
, sc
);
202 if (mac68k_machine
.aux_interrupts
) {
203 intr_establish((int (*)(void *))asc_intr
, sc
, 5);
205 via2_register_irq(VIA2_ASC
, asc_intr
, sc
);
212 ascopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
214 struct asc_softc
*sc
;
216 sc
= device_lookup_private(&asc_cd
, ASCUNIT(dev
));
227 ascclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
229 struct asc_softc
*sc
;
231 sc
= device_lookup_private(&asc_cd
, ASCUNIT(dev
));
238 ascread(dev_t dev
, struct uio
*uio
, int ioflag
)
244 ascwrite(dev_t dev
, struct uio
*uio
, int ioflag
)
250 ascioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
252 struct asc_softc
*sc
;
254 int unit
= ASCUNIT(dev
);
256 sc
= device_lookup_private(&asc_cd
, unit
);
268 ascmmap(dev_t dev
, off_t off
, int prot
)
270 struct asc_softc
*sc
;
273 sc
= device_lookup_private(&asc_cd
, ASCUNIT(dev
));
274 if ((u_int
)off
< MAC68K_ASC_LEN
) {
275 (void) pmap_extract(pmap_kernel(), (vaddr_t
)sc
->sc_handle
.base
,
277 return m68k_btop(pa
+ off
);
284 asc_ring_bell(void *arg
, int freq
, int length
, int volume
)
286 struct asc_softc
*sc
= (struct asc_softc
*)arg
;
293 if (sc
->sc_ringing
== 0) {
295 bus_space_write_multi_1(sc
->sc_tag
, sc
->sc_handle
,
297 bus_space_write_region_1(sc
->sc_tag
, sc
->sc_handle
,
298 0, asc_wave_tab
, 0x800);
300 /* Fix this. Need to find exact ASC sampling freq */
301 cfreq
= 65536 * freq
/ 466;
303 /* printf("beep: from %d, %02x %02x %02x %02x\n",
304 * cur_beep.freq, (cfreq >> 24) & 0xff, (cfreq >> 16) & 0xff,
305 * (cfreq >> 8) & 0xff, (cfreq) & 0xff); */
306 for (i
= 0; i
< 8; i
++) {
307 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
,
308 0x814 + 8 * i
, (cfreq
>> 24) & 0xff);
309 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
,
310 0x815 + 8 * i
, (cfreq
>> 16) & 0xff);
311 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
,
312 0x816 + 8 * i
, (cfreq
>> 8) & 0xff);
313 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
,
314 0x817 + 8 * i
, (cfreq
) & 0xff);
315 } /* frequency; should put cur_beep.freq in here
318 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
, 0x807, 3); /* 44 ? */
319 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
, 0x806,
321 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
, 0x805, 0);
322 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
, 0x80f, 0);
323 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
, 0x802, 2); /* sampled */
324 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
, 0x801, 2); /* enable sampled */
326 callout_reset(&sc
->sc_bell_ch
, length
, asc_stop_bell
, sc
);
333 asc_stop_bell(void *arg
)
335 struct asc_softc
*sc
= (struct asc_softc
*)arg
;
340 if (sc
->sc_ringing
> 1000 || sc
->sc_ringing
< 0)
341 panic("bell got out of sync?");
343 if (--sc
->sc_ringing
== 0) /* disable ASC */
344 bus_space_write_1(sc
->sc_tag
, sc
->sc_handle
, 0x801, 0);
349 asc_intr_enable(void)
355 via2_reg(vIER
) = 0x80 | V2IF_ASC
;
357 via2_reg(rIER
) = 0x80 | V2IF_ASC
;
367 struct asc_softc
*sc
= (struct asc_softc
*)arg
;
370 printf("asc_intr(%p)\n", sc
);