xref: /netbsd-src/sys/arch/x68k/dev/opm.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: opm.c,v 1.18 2007/03/11 08:09:25 isaki 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.18 2007/03/11 08:09:25 isaki 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 <machine/opmreg.h>
50 #include <arch/x68k/dev/opmvar.h>
51 #include <arch/x68k/dev/intiovar.h>
52 
53 struct opm_softc {
54 	struct device		sc_dev;
55 
56 	bus_space_tag_t		sc_bst;
57 	bus_space_handle_t	sc_bht;
58 	u_int8_t		sc_regs[0x100];
59 	struct opm_voice	sc_vdata[8];
60 };
61 
62 struct opm_softc	*opm0;	/* XXX */
63 
64 static int opm_match(struct device *, struct cfdata *, void *);
65 static void opm_attach(struct device *, struct device *, void *);
66 
67 CFATTACH_DECL(opm, sizeof(struct opm_softc),
68     opm_match, opm_attach, NULL, NULL);
69 
70 static int
71 opm_match(struct device *parent, struct cfdata *cf, void *aux)
72 {
73 	struct intio_attach_args *ia = aux;
74 
75 	if (strcmp(ia->ia_name, "opm") != 0)
76 		return 0;
77 
78 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
79 		ia->ia_addr = 0xe90000;
80 	ia->ia_size = 0x2000;
81 	if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY))
82 		return 0;
83 
84 	return 1;
85 }
86 
87 static void
88 opm_attach(struct device *parent, struct device *self, void *aux)
89 {
90 	struct opm_softc *sc = (struct opm_softc *)self;
91 	struct intio_attach_args *ia = aux;
92 	int r;
93 
94 	printf("\n");
95 	ia->ia_size = 0x2000;
96 	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
97 #ifdef DIAGNOSTIC
98 	if (r)
99 		panic("IO map for OPM corruption??");
100 #endif
101 
102 	sc->sc_bst = ia->ia_bst;
103 	r = bus_space_map(sc->sc_bst,
104 			   ia->ia_addr, ia->ia_size,
105 			   BUS_SPACE_MAP_SHIFTED,
106 			   &sc->sc_bht);
107 #ifdef DIAGNOSTIC
108 	if (r)
109 		panic("Cannot map IO space for OPM.");
110 #endif
111 
112 	/* XXX device_unit() abuse */
113 	if (device_unit(&sc->sc_dev) == 0)
114 		opm0 = sc;	/* XXX */
115 
116 	return;
117 }
118 
119 void opm_set_volume(int, int);
120 void opm_set_key(int, int);
121 void opm_set_voice(int, struct opm_voice *);
122 void opm_set_voice_sub(int, struct opm_operator *);
123 inline static void writeopm(int, int);
124 inline static int readopm(int);
125 void opm_key_on(u_char);
126 void opm_key_off(u_char);
127 int opmopen(dev_t, int, int);
128 int opmclose(dev_t);
129 
130 inline static void
131 writeopm(int reg, int dat)
132 {
133 	while (bus_space_read_1(opm0->sc_bst, opm0->sc_bht, OPM_DATA) & 0x80);
134 	bus_space_write_1(opm0->sc_bst, opm0->sc_bht, OPM_REG, reg);
135 	while (bus_space_read_1(opm0->sc_bst, opm0->sc_bht, OPM_DATA) & 0x80);
136 	bus_space_write_1(opm0->sc_bst, opm0->sc_bht, OPM_DATA, dat);
137 	opm0->sc_regs[reg] = dat;
138 }
139 
140 inline static int
141 readopm(int reg)
142 {
143 	return opm0->sc_regs[reg];
144 }
145 
146 #include "fd.h"
147 #include "vs.h"
148 #include "bell.h"
149 
150 #if NVS > 0
151 void
152 adpcm_chgclk(u_char clk)
153 {
154 	writeopm(0x1b, (readopm(0x1b) & ~OPM1B_CT1MSK) | clk);
155 }
156 #endif
157 
158 #if NFD > 0
159 void
160 fdc_force_ready(u_char rdy)
161 {
162 	writeopm(0x1b, (readopm(0x1b) & ~OPM1B_CT2MSK) | rdy);
163 }
164 #endif
165 
166 #if NBELL > 0
167 void
168 opm_key_on(u_char channel)
169 {
170 	writeopm(0x08, opm0->sc_vdata[channel].sm << 3 | channel);
171 }
172 
173 void
174 opm_key_off(u_char channel)
175 {
176 	writeopm(0x08, channel);
177 }
178 
179 void
180 opm_set_voice(int channel, struct opm_voice *voice)
181 {
182 	memcpy(&opm0->sc_vdata[channel], voice, sizeof(struct opm_voice));
183 
184 	opm_set_voice_sub(0x40 + channel, &voice->m1);
185 	opm_set_voice_sub(0x48 + channel, &voice->m2);
186 	opm_set_voice_sub(0x50 + channel, &voice->c1);
187 	opm_set_voice_sub(0x58 + channel, &voice->c2);
188 	writeopm(0x20 + channel, 0xc0 | (voice->fb & 0x7) << 3 |
189 		 (voice->con & 0x7));
190 }
191 
192 void
193 opm_set_voice_sub(int reg, struct opm_operator *op)
194 {
195 	/* DT1/MUL */
196 	writeopm(reg, (op->dt1 & 0x7) << 3 | (op->mul & 0x7));
197 
198 	/* TL */
199 	writeopm(reg + 0x20, op->tl & 0x7f);
200 
201 	/* KS/AR */
202 	writeopm(reg + 0x40, (op->ks & 0x3) << 6 | (op->ar & 0x1f));
203 
204 	/* AMS/D1R */
205 	writeopm(reg + 0x60, (op->ame & 0x1) << 7 | (op->d1r & 0x1f));
206 
207 	/* DT2/D2R */
208 	writeopm(reg + 0x80, (op->dt2 & 0x3) << 6 | (op->d2r & 0x1f));
209 
210 	/* D1L/RR */
211 	writeopm(reg + 0xa0, (op->d1l & 0xf) << 4 | (op->rr & 0xf));
212 }
213 
214 void
215 opm_set_volume(int channel, int volume)
216 {
217 	int value;
218 
219 	switch (opm0->sc_vdata[channel].con) {
220 	case 7:
221 		value = opm0->sc_vdata[channel].m1.tl + volume;
222 		writeopm(0x60 + channel, ((value > 0x7f) ? 0x7f : value));
223 	case 6:
224 	case 5:
225 		value = opm0->sc_vdata[channel].m2.tl + volume;
226 		writeopm(0x68 + channel, ((value > 0x7f) ? 0x7f : value));
227 	case 4:
228 		value = opm0->sc_vdata[channel].c1.tl + volume;
229 		writeopm(0x70 + channel, ((value > 0x7f) ? 0x7f : value));
230 	case 3:
231 	case 2:
232 	case 1:
233 	case 0:
234 		value = opm0->sc_vdata[channel].c2.tl + volume;
235 		writeopm(0x78 + channel, ((value > 0x7f) ? 0x7f : value));
236 	}
237 }
238 
239 void
240 opm_set_key(int channel, int tone)
241 {
242 	writeopm(0x28 + channel, tone >> 8);
243 	writeopm(0x30 + channel, tone & 0xff);
244 }
245 
246 /*ARGSUSED*/
247 int
248 opmopen(dev_t dev, int flag, int mode)
249 {
250 	return 0;
251 }
252 
253 /*ARGSUSED*/
254 int
255 opmclose(dev_t dev)
256 {
257 	return 0;
258 }
259 
260 #endif
261