1 /* $NetBSD: opm.c,v 1.11 2003/07/15 01:44:52 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Masanobu Saitoh, Takuya Harakawa. 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Masanobu Saitoh. 18 * 4. Neither the name of the University nor of the Laboratory may be used 19 * to endorse or promote products derived from this software without 20 * specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Temporary implementation: not fully bus.h'fied. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: opm.c,v 1.11 2003/07/15 01:44:52 lukem Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/device.h> 45 46 #include <machine/bus.h> 47 #include <machine/cpu.h> 48 49 #include <arch/x68k/dev/opmreg.h> 50 #include <arch/x68k/dev/intiovar.h> 51 52 struct opm_softc { 53 struct device sc_dev; 54 55 bus_space_tag_t sc_bst; 56 bus_space_handle_t sc_bht; 57 u_int8_t sc_regs[0x100]; 58 struct opm_voice sc_vdata[8]; 59 }; 60 61 struct opm_softc *opm0; /* XXX */ 62 63 static int opm_match __P((struct device *, struct cfdata *, void *)); 64 static void opm_attach __P((struct device *, struct device *, void *)); 65 66 CFATTACH_DECL(opm, sizeof (struct opm_softc), 67 opm_match, opm_attach, NULL, NULL); 68 69 static int 70 opm_match(parent, cf, aux) 71 struct device *parent; 72 struct cfdata *cf; 73 void *aux; 74 { 75 struct intio_attach_args *ia = aux; 76 77 if (strcmp(ia->ia_name, "opm") != 0) 78 return 0; 79 80 if (ia->ia_addr == INTIOCF_ADDR_DEFAULT) 81 ia->ia_addr = 0xe90000; 82 ia->ia_size = 0x2000; 83 if (intio_map_allocate_region (parent, ia, INTIO_MAP_TESTONLY)) 84 return 0; 85 86 return 1; 87 } 88 89 static void 90 opm_attach(parent, self, aux) 91 struct device *parent, *self; 92 void *aux; 93 { 94 struct opm_softc *sc = (struct opm_softc *)self; 95 struct intio_attach_args *ia = aux; 96 int r; 97 98 printf ("\n"); 99 ia->ia_size = 0x2000; 100 r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE); 101 #ifdef DIAGNOSTIC 102 if (r) 103 panic ("IO map for OPM corruption??"); 104 #endif 105 106 sc->sc_bst = ia->ia_bst; 107 r = bus_space_map (sc->sc_bst, 108 ia->ia_addr, ia->ia_size, 109 BUS_SPACE_MAP_SHIFTED, 110 &sc->sc_bht); 111 #ifdef DIAGNOSTIC 112 if (r) 113 panic ("Cannot map IO space for OPM."); 114 #endif 115 116 if (sc->sc_dev.dv_unit == 0) 117 opm0 = sc; /* XXX */ 118 119 return; 120 } 121 122 void opm_set_volume __P((int, int)); 123 void opm_set_key __P((int, int)); 124 void opm_set_voice __P((int, struct opm_voice *)); 125 void opm_set_voice_sub __P((int, struct opm_operator *)); 126 __inline static void writeopm __P((int, int)); 127 __inline static int readopm __P((int)); 128 void opm_key_on __P((u_char)); 129 void opm_key_off __P((u_char)); 130 int opmopen __P((dev_t, int, int)); 131 int opmclose __P((dev_t)); 132 133 __inline static void 134 writeopm(reg, dat) 135 int reg, dat; 136 { 137 while (bus_space_read_1 (opm0->sc_bst, opm0->sc_bht, OPM_DATA) & 0x80); 138 bus_space_write_1 (opm0->sc_bst, opm0->sc_bht, OPM_REG, reg); 139 while (bus_space_read_1 (opm0->sc_bst, opm0->sc_bht, OPM_DATA) & 0x80); 140 bus_space_write_1 (opm0->sc_bst, opm0->sc_bht, OPM_DATA, dat); 141 opm0->sc_regs[reg] = dat; 142 } 143 144 __inline static int 145 readopm(reg) 146 int reg; 147 { 148 return opm0->sc_regs[reg]; 149 } 150 151 #include "fd.h" 152 #include "vs.h" 153 #include "bell.h" 154 155 #if NVS > 0 156 void 157 adpcm_chgclk(clk) 158 u_char clk; 159 { 160 writeopm(0x1b, (readopm(0x1b) & ~OPM1B_CT1MSK) | clk); 161 } 162 #endif 163 164 #if NFD > 0 165 void 166 fdc_force_ready(rdy) 167 u_char rdy; 168 { 169 writeopm(0x1b, (readopm(0x1b) & ~OPM1B_CT2MSK) | rdy); 170 } 171 #endif 172 173 #if NBELL > 0 174 void 175 opm_key_on(channel) 176 u_char channel; 177 { 178 writeopm(0x08, opm0->sc_vdata[channel].sm << 3 | channel); 179 } 180 181 void 182 opm_key_off(channel) 183 u_char channel; 184 { 185 writeopm(0x08, channel); 186 } 187 188 void 189 opm_set_voice(channel, voice) 190 int channel; 191 struct opm_voice *voice; 192 { 193 memcpy(&opm0->sc_vdata[channel], voice, sizeof(struct opm_voice)); 194 195 opm_set_voice_sub(0x40 + channel, &voice->m1); 196 opm_set_voice_sub(0x48 + channel, &voice->m2); 197 opm_set_voice_sub(0x50 + channel, &voice->c1); 198 opm_set_voice_sub(0x58 + channel, &voice->c2); 199 writeopm(0x20 + channel, 0xc0 | (voice->fb & 0x7) << 3 | (voice->con & 0x7)); 200 } 201 202 void 203 opm_set_voice_sub(reg, op) 204 register int reg; 205 struct opm_operator *op; 206 { 207 /* DT1/MUL */ 208 writeopm(reg, (op->dt1 & 0x7) << 3 | (op->mul & 0x7)); 209 210 /* TL */ 211 writeopm(reg + 0x20, op->tl & 0x7f); 212 213 /* KS/AR */ 214 writeopm(reg + 0x40, (op->ks & 0x3) << 6 | (op->ar & 0x1f)); 215 216 /* AMS/D1R */ 217 writeopm(reg + 0x60, (op->ame & 0x1) << 7 | (op->d1r & 0x1f)); 218 219 /* DT2/D2R */ 220 writeopm(reg + 0x80, (op->dt2 & 0x3) << 6 | (op->d2r & 0x1f)); 221 222 /* D1L/RR */ 223 writeopm(reg + 0xa0, (op->d1l & 0xf) << 4 | (op->rr & 0xf)); 224 } 225 226 void 227 opm_set_volume(channel, volume) 228 int channel; 229 int volume; 230 { 231 int value; 232 233 switch (opm0->sc_vdata[channel].con) { 234 case 7: 235 value = opm0->sc_vdata[channel].m1.tl + volume; 236 writeopm(0x60 + channel, ((value > 0x7f) ? 0x7f : value)); 237 case 6: 238 case 5: 239 value = opm0->sc_vdata[channel].m2.tl + volume; 240 writeopm(0x68 + channel, ((value > 0x7f) ? 0x7f : value)); 241 case 4: 242 value = opm0->sc_vdata[channel].c1.tl + volume; 243 writeopm(0x70 + channel, ((value > 0x7f) ? 0x7f : value)); 244 case 3: 245 case 2: 246 case 1: 247 case 0: 248 value = opm0->sc_vdata[channel].c2.tl + volume; 249 writeopm(0x78 + channel, ((value > 0x7f) ? 0x7f : value)); 250 } 251 } 252 253 void 254 opm_set_key(channel, tone) 255 int channel; 256 int tone; 257 { 258 writeopm(0x28 + channel, tone >> 8); 259 writeopm(0x30 + channel, tone & 0xff); 260 } 261 262 /*ARGSUSED*/ 263 int 264 opmopen(dev, flag, mode) 265 dev_t dev; 266 int flag, mode; 267 { 268 return 0; 269 } 270 271 /*ARGSUSED*/ 272 int 273 opmclose(dev) 274 dev_t dev; 275 { 276 return 0; 277 } 278 279 #endif 280