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