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