cs4280.c revision 1.7.2.7 1 1.7.2.7 bouyer /* $NetBSD: cs4280.c,v 1.7.2.7 2001/03/12 13:31:05 bouyer Exp $ */
2 1.7.2.2 bouyer
3 1.7.2.2 bouyer /*
4 1.7.2.2 bouyer * Copyright (c) 1999, 2000 Tatoku Ogaito. All rights reserved.
5 1.7.2.2 bouyer *
6 1.7.2.2 bouyer * Redistribution and use in source and binary forms, with or without
7 1.7.2.2 bouyer * modification, are permitted provided that the following conditions
8 1.7.2.2 bouyer * are met:
9 1.7.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.7.2.2 bouyer * notice, this list of conditions and the following disclaimer.
11 1.7.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.7.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.7.2.2 bouyer * documentation and/or other materials provided with the distribution.
14 1.7.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.7.2.2 bouyer * must display the following acknowledgement:
16 1.7.2.2 bouyer * This product includes software developed by Tatoku Ogaito
17 1.7.2.2 bouyer * for the NetBSD Project.
18 1.7.2.2 bouyer * 4. The name of the author may not be used to endorse or promote products
19 1.7.2.2 bouyer * derived from this software without specific prior written permission
20 1.7.2.2 bouyer *
21 1.7.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.7.2.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.7.2.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.7.2.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.7.2.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.7.2.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.7.2.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.7.2.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.7.2.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.7.2.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.7.2.2 bouyer */
32 1.7.2.2 bouyer
33 1.7.2.2 bouyer /*
34 1.7.2.2 bouyer * Cirrus Logic CS4280 (and maybe CS461x) driver.
35 1.7.2.2 bouyer * Data sheets can be found
36 1.7.2.2 bouyer * http://www.cirrus.com/ftp/pubs/4280.pdf
37 1.7.2.2 bouyer * http://www.cirrus.com/ftp/pubs/4297.pdf
38 1.7.2.2 bouyer * ftp://ftp.alsa-project.org/pub/manuals/cirrus/embedded_audio_spec.pdf
39 1.7.2.2 bouyer * ftp://ftp.alsa-project.org/pub/manuals/cirrus/embedded_audio_spec.doc
40 1.7.2.2 bouyer *
41 1.7.2.6 bouyer * Note: CS4610/CS4611 + CS423x ISA codec should be worked with
42 1.7.2.2 bouyer * wss* at pnpbios?
43 1.7.2.6 bouyer * or
44 1.7.2.6 bouyer * sb* at pnpbios?
45 1.7.2.6 bouyer * Since I could not find any documents on handling ISA codec,
46 1.7.2.6 bouyer * clcs does not support those chips.
47 1.7.2.2 bouyer */
48 1.7.2.2 bouyer
49 1.7.2.2 bouyer /*
50 1.7.2.2 bouyer * TODO
51 1.7.2.2 bouyer * Joystick support
52 1.7.2.2 bouyer */
53 1.7.2.2 bouyer
54 1.7.2.2 bouyer #include "midi.h"
55 1.7.2.2 bouyer
56 1.7.2.2 bouyer #include <sys/param.h>
57 1.7.2.2 bouyer #include <sys/systm.h>
58 1.7.2.2 bouyer #include <sys/kernel.h>
59 1.7.2.2 bouyer #include <sys/fcntl.h>
60 1.7.2.2 bouyer #include <sys/malloc.h>
61 1.7.2.2 bouyer #include <sys/device.h>
62 1.7.2.6 bouyer #include <sys/proc.h>
63 1.7.2.2 bouyer #include <sys/types.h>
64 1.7.2.2 bouyer #include <sys/systm.h>
65 1.7.2.2 bouyer
66 1.7.2.2 bouyer #include <dev/pci/pcidevs.h>
67 1.7.2.2 bouyer #include <dev/pci/pcivar.h>
68 1.7.2.2 bouyer #include <dev/pci/cs4280reg.h>
69 1.7.2.2 bouyer #include <dev/pci/cs4280_image.h>
70 1.7.2.6 bouyer #include <dev/pci/cs428xreg.h>
71 1.7.2.2 bouyer
72 1.7.2.2 bouyer #include <sys/audioio.h>
73 1.7.2.2 bouyer #include <dev/audio_if.h>
74 1.7.2.2 bouyer #include <dev/midi_if.h>
75 1.7.2.2 bouyer #include <dev/mulaw.h>
76 1.7.2.2 bouyer #include <dev/auconv.h>
77 1.7.2.2 bouyer
78 1.7.2.2 bouyer #include <dev/ic/ac97reg.h>
79 1.7.2.2 bouyer #include <dev/ic/ac97var.h>
80 1.7.2.2 bouyer
81 1.7.2.6 bouyer #include <dev/pci/cs428x.h>
82 1.7.2.6 bouyer
83 1.7.2.2 bouyer #include <machine/bus.h>
84 1.7.2.2 bouyer #include <machine/bswap.h>
85 1.7.2.2 bouyer
86 1.7.2.2 bouyer #define BA1READ4(sc, r) bus_space_read_4((sc)->ba1t, (sc)->ba1h, (r))
87 1.7.2.2 bouyer #define BA1WRITE4(sc, r, x) bus_space_write_4((sc)->ba1t, (sc)->ba1h, (r), (x))
88 1.7.2.2 bouyer
89 1.7.2.6 bouyer /* IF functions for audio driver */
90 1.7.2.6 bouyer int cs4280_match(struct device *, struct cfdata *, void *);
91 1.7.2.6 bouyer void cs4280_attach(struct device *, struct device *, void *);
92 1.7.2.6 bouyer int cs4280_intr(void *);
93 1.7.2.6 bouyer int cs4280_query_encoding(void *, struct audio_encoding *);
94 1.7.2.6 bouyer int cs4280_set_params(void *, int, int, struct audio_params *, struct audio_params *);
95 1.7.2.6 bouyer int cs4280_halt_output(void *);
96 1.7.2.6 bouyer int cs4280_halt_input(void *);
97 1.7.2.6 bouyer int cs4280_getdev(void *, struct audio_device *);
98 1.7.2.6 bouyer int cs4280_trigger_output(void *, void *, void *, int, void (*)(void *),
99 1.7.2.6 bouyer void *, struct audio_params *);
100 1.7.2.6 bouyer int cs4280_trigger_input(void *, void *, void *, int, void (*)(void *),
101 1.7.2.6 bouyer void *, struct audio_params *);
102 1.7.2.6 bouyer
103 1.7.2.6 bouyer void cs4280_reset_codec(void *);
104 1.7.2.6 bouyer
105 1.7.2.6 bouyer /* For PowerHook */
106 1.7.2.6 bouyer void cs4280_power(int, void *);
107 1.7.2.6 bouyer
108 1.7.2.6 bouyer /* Internal functions */
109 1.7.2.6 bouyer void cs4280_set_adc_rate(struct cs428x_softc *, int );
110 1.7.2.6 bouyer void cs4280_set_dac_rate(struct cs428x_softc *, int );
111 1.7.2.6 bouyer int cs4280_download(struct cs428x_softc *, const u_int32_t *, u_int32_t, u_int32_t);
112 1.7.2.6 bouyer int cs4280_download_image(struct cs428x_softc *);
113 1.7.2.6 bouyer void cs4280_reset(void *);
114 1.7.2.6 bouyer int cs4280_get_portnum_by_name(struct cs428x_softc *, char *, char *, char *);
115 1.7.2.6 bouyer int cs4280_init(struct cs428x_softc *, int);
116 1.7.2.6 bouyer void cs4280_clear_fifos(struct cs428x_softc *);
117 1.7.2.6 bouyer
118 1.7.2.6 bouyer #if CS4280_DEBUG > 10
119 1.7.2.6 bouyer /* Thease two function is only for checking image loading is succeeded or not. */
120 1.7.2.6 bouyer int cs4280_check_images(struct cs428x_softc *);
121 1.7.2.6 bouyer int cs4280_checkimage(struct cs428x_softc *, u_int32_t *, u_int32_t, u_int32_t);
122 1.7.2.2 bouyer #endif
123 1.7.2.2 bouyer
124 1.7.2.2 bouyer struct audio_hw_if cs4280_hw_if = {
125 1.7.2.6 bouyer cs428x_open,
126 1.7.2.6 bouyer cs428x_close,
127 1.7.2.2 bouyer NULL,
128 1.7.2.2 bouyer cs4280_query_encoding,
129 1.7.2.2 bouyer cs4280_set_params,
130 1.7.2.6 bouyer cs428x_round_blocksize,
131 1.7.2.2 bouyer NULL,
132 1.7.2.2 bouyer NULL,
133 1.7.2.2 bouyer NULL,
134 1.7.2.2 bouyer NULL,
135 1.7.2.2 bouyer NULL,
136 1.7.2.2 bouyer cs4280_halt_output,
137 1.7.2.2 bouyer cs4280_halt_input,
138 1.7.2.2 bouyer NULL,
139 1.7.2.2 bouyer cs4280_getdev,
140 1.7.2.2 bouyer NULL,
141 1.7.2.6 bouyer cs428x_mixer_set_port,
142 1.7.2.6 bouyer cs428x_mixer_get_port,
143 1.7.2.6 bouyer cs428x_query_devinfo,
144 1.7.2.6 bouyer cs428x_malloc,
145 1.7.2.6 bouyer cs428x_free,
146 1.7.2.6 bouyer cs428x_round_buffersize,
147 1.7.2.6 bouyer cs428x_mappage,
148 1.7.2.6 bouyer cs428x_get_props,
149 1.7.2.2 bouyer cs4280_trigger_output,
150 1.7.2.2 bouyer cs4280_trigger_input,
151 1.7.2.2 bouyer };
152 1.7.2.2 bouyer
153 1.7.2.2 bouyer #if NMIDI > 0
154 1.7.2.6 bouyer /* Midi Interface */
155 1.7.2.6 bouyer int cs4280_midi_open(void *, int, void (*)(void *, int),
156 1.7.2.6 bouyer void (*)(void *), void *);
157 1.7.2.6 bouyer void cs4280_midi_close(void*);
158 1.7.2.6 bouyer int cs4280_midi_output(void *, int);
159 1.7.2.6 bouyer void cs4280_midi_getinfo(void *, struct midi_info *);
160 1.7.2.6 bouyer
161 1.7.2.2 bouyer struct midi_hw_if cs4280_midi_hw_if = {
162 1.7.2.2 bouyer cs4280_midi_open,
163 1.7.2.2 bouyer cs4280_midi_close,
164 1.7.2.2 bouyer cs4280_midi_output,
165 1.7.2.2 bouyer cs4280_midi_getinfo,
166 1.7.2.2 bouyer 0,
167 1.7.2.2 bouyer };
168 1.7.2.2 bouyer #endif
169 1.7.2.2 bouyer
170 1.7.2.6 bouyer struct cfattach clcs_ca = {
171 1.7.2.6 bouyer sizeof(struct cs428x_softc), cs4280_match, cs4280_attach
172 1.7.2.6 bouyer };
173 1.7.2.2 bouyer
174 1.7.2.2 bouyer struct audio_device cs4280_device = {
175 1.7.2.2 bouyer "CS4280",
176 1.7.2.2 bouyer "",
177 1.7.2.2 bouyer "cs4280"
178 1.7.2.2 bouyer };
179 1.7.2.2 bouyer
180 1.7.2.2 bouyer
181 1.7.2.2 bouyer int
182 1.7.2.2 bouyer cs4280_match(parent, match, aux)
183 1.7.2.2 bouyer struct device *parent;
184 1.7.2.2 bouyer struct cfdata *match;
185 1.7.2.2 bouyer void *aux;
186 1.7.2.2 bouyer {
187 1.7.2.2 bouyer struct pci_attach_args *pa = (struct pci_attach_args *)aux;
188 1.7.2.2 bouyer
189 1.7.2.2 bouyer if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
190 1.7.2.6 bouyer return 0;
191 1.7.2.2 bouyer if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4280
192 1.7.2.2 bouyer #if 0 /* I can't confirm */
193 1.7.2.2 bouyer || PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4610
194 1.7.2.2 bouyer #endif
195 1.7.2.2 bouyer )
196 1.7.2.6 bouyer return 1;
197 1.7.2.6 bouyer return 0;
198 1.7.2.2 bouyer }
199 1.7.2.2 bouyer
200 1.7.2.2 bouyer void
201 1.7.2.2 bouyer cs4280_attach(parent, self, aux)
202 1.7.2.2 bouyer struct device *parent;
203 1.7.2.2 bouyer struct device *self;
204 1.7.2.2 bouyer void *aux;
205 1.7.2.2 bouyer {
206 1.7.2.6 bouyer struct cs428x_softc *sc = (struct cs428x_softc *)self;
207 1.7.2.2 bouyer struct pci_attach_args *pa = (struct pci_attach_args *)aux;
208 1.7.2.2 bouyer pci_chipset_tag_t pc = pa->pa_pc;
209 1.7.2.2 bouyer char const *intrstr;
210 1.7.2.2 bouyer pci_intr_handle_t ih;
211 1.7.2.7 bouyer pcireg_t reg;
212 1.7.2.2 bouyer char devinfo[256];
213 1.7.2.2 bouyer mixer_ctrl_t ctl;
214 1.7.2.2 bouyer u_int32_t mem;
215 1.7.2.7 bouyer int pci_pwrmgmt_cap_reg, pci_pwrmgmt_csr_reg;
216 1.7.2.6 bouyer
217 1.7.2.2 bouyer pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
218 1.7.2.2 bouyer printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
219 1.7.2.2 bouyer
220 1.7.2.2 bouyer /* Map I/O register */
221 1.7.2.6 bouyer if (pci_mapreg_map(pa, PCI_BA0,
222 1.7.2.6 bouyer PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
223 1.7.2.6 bouyer &sc->ba0t, &sc->ba0h, NULL, NULL)) {
224 1.7.2.2 bouyer printf("%s: can't map BA0 space\n", sc->sc_dev.dv_xname);
225 1.7.2.2 bouyer return;
226 1.7.2.2 bouyer }
227 1.7.2.6 bouyer if (pci_mapreg_map(pa, PCI_BA1,
228 1.7.2.6 bouyer PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
229 1.7.2.6 bouyer &sc->ba1t, &sc->ba1h, NULL, NULL)) {
230 1.7.2.2 bouyer printf("%s: can't map BA1 space\n", sc->sc_dev.dv_xname);
231 1.7.2.2 bouyer return;
232 1.7.2.2 bouyer }
233 1.7.2.2 bouyer
234 1.7.2.2 bouyer sc->sc_dmatag = pa->pa_dmat;
235 1.7.2.2 bouyer
236 1.7.2.7 bouyer /* Check and set Power State */
237 1.7.2.7 bouyer if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PWRMGMT,
238 1.7.2.7 bouyer &pci_pwrmgmt_cap_reg, 0)) {
239 1.7.2.7 bouyer pci_pwrmgmt_csr_reg = pci_pwrmgmt_cap_reg + 4;
240 1.7.2.7 bouyer reg = pci_conf_read(pa->pa_pc, pa->pa_tag,
241 1.7.2.7 bouyer pci_pwrmgmt_csr_reg);
242 1.7.2.7 bouyer DPRINTF(("%s: Power State is %d\n",
243 1.7.2.7 bouyer sc->sc_dev.dv_xname, reg & PCI_PMCSR_STATE_MASK));
244 1.7.2.7 bouyer if ((reg & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_STATE_D0) {
245 1.7.2.7 bouyer pci_conf_write(pc, pa->pa_tag, pci_pwrmgmt_csr_reg,
246 1.7.2.7 bouyer (reg & ~PCI_PMCSR_STATE_MASK) |
247 1.7.2.7 bouyer PCI_PMCSR_STATE_D0);
248 1.7.2.7 bouyer }
249 1.7.2.7 bouyer }
250 1.7.2.7 bouyer
251 1.7.2.2 bouyer /* Enable the device (set bus master flag) */
252 1.7.2.7 bouyer reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
253 1.7.2.2 bouyer pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
254 1.7.2.7 bouyer reg | PCI_COMMAND_MASTER_ENABLE);
255 1.7.2.2 bouyer
256 1.7.2.2 bouyer /* LATENCY_TIMER setting */
257 1.7.2.2 bouyer mem = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
258 1.7.2.2 bouyer if ( PCI_LATTIMER(mem) < 32 ) {
259 1.7.2.2 bouyer mem &= 0xffff00ff;
260 1.7.2.2 bouyer mem |= 0x00002000;
261 1.7.2.2 bouyer pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, mem);
262 1.7.2.2 bouyer }
263 1.7.2.2 bouyer
264 1.7.2.2 bouyer /* Map and establish the interrupt. */
265 1.7.2.4 bouyer if (pci_intr_map(pa, &ih)) {
266 1.7.2.2 bouyer printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
267 1.7.2.2 bouyer return;
268 1.7.2.2 bouyer }
269 1.7.2.2 bouyer intrstr = pci_intr_string(pc, ih);
270 1.7.2.2 bouyer
271 1.7.2.2 bouyer sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, cs4280_intr, sc);
272 1.7.2.2 bouyer if (sc->sc_ih == NULL) {
273 1.7.2.2 bouyer printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
274 1.7.2.2 bouyer if (intrstr != NULL)
275 1.7.2.2 bouyer printf(" at %s", intrstr);
276 1.7.2.2 bouyer printf("\n");
277 1.7.2.2 bouyer return;
278 1.7.2.2 bouyer }
279 1.7.2.2 bouyer printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
280 1.7.2.2 bouyer
281 1.7.2.2 bouyer /* Initialization */
282 1.7.2.2 bouyer if(cs4280_init(sc, 1) != 0)
283 1.7.2.2 bouyer return;
284 1.7.2.2 bouyer
285 1.7.2.6 bouyer sc->type = TYPE_CS4280;
286 1.7.2.6 bouyer sc->halt_input = cs4280_halt_input;
287 1.7.2.6 bouyer sc->halt_output = cs4280_halt_output;
288 1.7.2.6 bouyer
289 1.7.2.6 bouyer /* setup buffer related parameters */
290 1.7.2.6 bouyer sc->dma_size = CS4280_DCHUNK;
291 1.7.2.6 bouyer sc->dma_align = CS4280_DALIGN;
292 1.7.2.6 bouyer sc->hw_blocksize = CS4280_ICHUNK;
293 1.7.2.6 bouyer
294 1.7.2.6 bouyer /* AC 97 attachment */
295 1.7.2.2 bouyer sc->host_if.arg = sc;
296 1.7.2.6 bouyer sc->host_if.attach = cs428x_attach_codec;
297 1.7.2.6 bouyer sc->host_if.read = cs428x_read_codec;
298 1.7.2.6 bouyer sc->host_if.write = cs428x_write_codec;
299 1.7.2.2 bouyer sc->host_if.reset = cs4280_reset_codec;
300 1.7.2.2 bouyer if (ac97_attach(&sc->host_if) != 0) {
301 1.7.2.2 bouyer printf("%s: ac97_attach failed\n", sc->sc_dev.dv_xname);
302 1.7.2.2 bouyer return;
303 1.7.2.2 bouyer }
304 1.7.2.2 bouyer
305 1.7.2.2 bouyer /* Turn mute off of DAC, CD and master volumes by default */
306 1.7.2.2 bouyer ctl.type = AUDIO_MIXER_ENUM;
307 1.7.2.2 bouyer ctl.un.ord = 0; /* off */
308 1.7.2.2 bouyer
309 1.7.2.2 bouyer ctl.dev = cs4280_get_portnum_by_name(sc, AudioCoutputs,
310 1.7.2.2 bouyer AudioNmaster, AudioNmute);
311 1.7.2.6 bouyer cs428x_mixer_set_port(sc, &ctl);
312 1.7.2.2 bouyer
313 1.7.2.2 bouyer ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
314 1.7.2.2 bouyer AudioNdac, AudioNmute);
315 1.7.2.6 bouyer cs428x_mixer_set_port(sc, &ctl);
316 1.7.2.2 bouyer
317 1.7.2.2 bouyer ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
318 1.7.2.2 bouyer AudioNcd, AudioNmute);
319 1.7.2.6 bouyer cs428x_mixer_set_port(sc, &ctl);
320 1.7.2.2 bouyer
321 1.7.2.2 bouyer audio_attach_mi(&cs4280_hw_if, sc, &sc->sc_dev);
322 1.7.2.2 bouyer
323 1.7.2.2 bouyer #if NMIDI > 0
324 1.7.2.2 bouyer midi_attach_mi(&cs4280_midi_hw_if, sc, &sc->sc_dev);
325 1.7.2.2 bouyer #endif
326 1.7.2.6 bouyer
327 1.7.2.2 bouyer sc->sc_suspend = PWR_RESUME;
328 1.7.2.2 bouyer sc->sc_powerhook = powerhook_establish(cs4280_power, sc);
329 1.7.2.2 bouyer }
330 1.7.2.2 bouyer
331 1.7.2.6 bouyer /* Interrupt handling function */
332 1.7.2.2 bouyer int
333 1.7.2.2 bouyer cs4280_intr(p)
334 1.7.2.2 bouyer void *p;
335 1.7.2.2 bouyer {
336 1.7.2.2 bouyer /*
337 1.7.2.2 bouyer * XXX
338 1.7.2.2 bouyer *
339 1.7.2.2 bouyer * Since CS4280 has only 4kB dma buffer and
340 1.7.2.2 bouyer * interrupt occurs every 2kB block, I create dummy buffer
341 1.7.2.2 bouyer * which returns to audio driver and actual dma buffer
342 1.7.2.2 bouyer * using in DMA transfer.
343 1.7.2.2 bouyer *
344 1.7.2.2 bouyer *
345 1.7.2.2 bouyer * ring buffer in audio.c is pointed by BUFADDR
346 1.7.2.2 bouyer * <------ ring buffer size == 64kB ------>
347 1.7.2.2 bouyer * <-----> blksize == 2048*(sc->sc_[pr]count) kB
348 1.7.2.2 bouyer * |= = = =|= = = =|= = = =|= = = =|= = = =|
349 1.7.2.2 bouyer * | | | | | | <- call audio_intp every
350 1.7.2.2 bouyer * sc->sc_[pr]_count time.
351 1.7.2.2 bouyer *
352 1.7.2.2 bouyer * actual dma buffer is pointed by KERNADDR
353 1.7.2.2 bouyer * <-> dma buffer size = 4kB
354 1.7.2.2 bouyer * |= =|
355 1.7.2.2 bouyer *
356 1.7.2.2 bouyer *
357 1.7.2.2 bouyer */
358 1.7.2.6 bouyer struct cs428x_softc *sc = p;
359 1.7.2.2 bouyer u_int32_t intr, mem;
360 1.7.2.2 bouyer char * empty_dma;
361 1.7.2.5 bouyer int handled = 0;
362 1.7.2.2 bouyer
363 1.7.2.2 bouyer /* grab interrupt register then clear it */
364 1.7.2.2 bouyer intr = BA0READ4(sc, CS4280_HISR);
365 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_HICR, HICR_CHGM | HICR_IEV);
366 1.7.2.2 bouyer
367 1.7.2.2 bouyer /* Playback Interrupt */
368 1.7.2.2 bouyer if (intr & HISR_PINT) {
369 1.7.2.5 bouyer handled = 1;
370 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PFIE);
371 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, (mem & ~PFIE_PI_MASK) | PFIE_PI_DISABLE);
372 1.7.2.2 bouyer if (sc->sc_pintr) {
373 1.7.2.2 bouyer if ((sc->sc_pi%sc->sc_pcount) == 0)
374 1.7.2.2 bouyer sc->sc_pintr(sc->sc_parg);
375 1.7.2.2 bouyer } else {
376 1.7.2.2 bouyer printf("unexpected play intr\n");
377 1.7.2.2 bouyer }
378 1.7.2.2 bouyer /* copy buffer */
379 1.7.2.2 bouyer ++sc->sc_pi;
380 1.7.2.2 bouyer empty_dma = sc->sc_pdma->addr;
381 1.7.2.2 bouyer if (sc->sc_pi&1)
382 1.7.2.6 bouyer empty_dma += sc->hw_blocksize;
383 1.7.2.6 bouyer memcpy(empty_dma, sc->sc_pn, sc->hw_blocksize);
384 1.7.2.6 bouyer sc->sc_pn += sc->hw_blocksize;
385 1.7.2.2 bouyer if (sc->sc_pn >= sc->sc_pe)
386 1.7.2.2 bouyer sc->sc_pn = sc->sc_ps;
387 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, mem);
388 1.7.2.2 bouyer }
389 1.7.2.2 bouyer /* Capture Interrupt */
390 1.7.2.2 bouyer if (intr & HISR_CINT) {
391 1.7.2.2 bouyer int i;
392 1.7.2.2 bouyer int16_t rdata;
393 1.7.2.2 bouyer
394 1.7.2.5 bouyer handled = 1;
395 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CIE);
396 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, (mem & ~CIE_CI_MASK) | CIE_CI_DISABLE);
397 1.7.2.2 bouyer ++sc->sc_ri;
398 1.7.2.2 bouyer empty_dma = sc->sc_rdma->addr;
399 1.7.2.2 bouyer if ((sc->sc_ri&1) == 0)
400 1.7.2.6 bouyer empty_dma += sc->hw_blocksize;
401 1.7.2.2 bouyer
402 1.7.2.2 bouyer /*
403 1.7.2.2 bouyer * XXX
404 1.7.2.2 bouyer * I think this audio data conversion should be
405 1.7.2.2 bouyer * happend in upper layer, but I put this here
406 1.7.2.2 bouyer * since there is no conversion function available.
407 1.7.2.2 bouyer */
408 1.7.2.2 bouyer switch(sc->sc_rparam) {
409 1.7.2.2 bouyer case CF_16BIT_STEREO:
410 1.7.2.2 bouyer /* just copy it */
411 1.7.2.6 bouyer memcpy(sc->sc_rn, empty_dma, sc->hw_blocksize);
412 1.7.2.6 bouyer sc->sc_rn += sc->hw_blocksize;
413 1.7.2.2 bouyer break;
414 1.7.2.2 bouyer case CF_16BIT_MONO:
415 1.7.2.2 bouyer for (i = 0; i < 512; i++) {
416 1.7.2.2 bouyer rdata = *((int16_t *)empty_dma)++>>1;
417 1.7.2.2 bouyer rdata += *((int16_t *)empty_dma)++>>1;
418 1.7.2.2 bouyer *((int16_t *)sc->sc_rn)++ = rdata;
419 1.7.2.2 bouyer }
420 1.7.2.2 bouyer break;
421 1.7.2.2 bouyer case CF_8BIT_STEREO:
422 1.7.2.2 bouyer for (i = 0; i < 512; i++) {
423 1.7.2.2 bouyer rdata = *((int16_t*)empty_dma)++;
424 1.7.2.2 bouyer *sc->sc_rn++ = rdata >> 8;
425 1.7.2.2 bouyer rdata = *((int16_t*)empty_dma)++;
426 1.7.2.2 bouyer *sc->sc_rn++ = rdata >> 8;
427 1.7.2.2 bouyer }
428 1.7.2.2 bouyer break;
429 1.7.2.2 bouyer case CF_8BIT_MONO:
430 1.7.2.2 bouyer for (i = 0; i < 512; i++) {
431 1.7.2.2 bouyer rdata = *((int16_t*)empty_dma)++ >>1;
432 1.7.2.2 bouyer rdata += *((int16_t*)empty_dma)++ >>1;
433 1.7.2.2 bouyer *sc->sc_rn++ = rdata >>8;
434 1.7.2.2 bouyer }
435 1.7.2.2 bouyer break;
436 1.7.2.2 bouyer default:
437 1.7.2.2 bouyer /* Should not reach here */
438 1.7.2.2 bouyer printf("unknown sc->sc_rparam: %d\n", sc->sc_rparam);
439 1.7.2.2 bouyer }
440 1.7.2.2 bouyer if (sc->sc_rn >= sc->sc_re)
441 1.7.2.2 bouyer sc->sc_rn = sc->sc_rs;
442 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, mem);
443 1.7.2.2 bouyer if (sc->sc_rintr) {
444 1.7.2.2 bouyer if ((sc->sc_ri%(sc->sc_rcount)) == 0)
445 1.7.2.2 bouyer sc->sc_rintr(sc->sc_rarg);
446 1.7.2.2 bouyer } else {
447 1.7.2.2 bouyer printf("unexpected record intr\n");
448 1.7.2.2 bouyer }
449 1.7.2.2 bouyer }
450 1.7.2.2 bouyer
451 1.7.2.2 bouyer #if NMIDI > 0
452 1.7.2.2 bouyer /* Midi port Interrupt */
453 1.7.2.2 bouyer if (intr & HISR_MIDI) {
454 1.7.2.2 bouyer int data;
455 1.7.2.2 bouyer
456 1.7.2.5 bouyer handled = 1;
457 1.7.2.2 bouyer DPRINTF(("i: %d: ",
458 1.7.2.2 bouyer BA0READ4(sc, CS4280_MIDSR)));
459 1.7.2.2 bouyer /* Read the received data */
460 1.7.2.2 bouyer while ((sc->sc_iintr != NULL) &&
461 1.7.2.2 bouyer ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_RBE) == 0)) {
462 1.7.2.2 bouyer data = BA0READ4(sc, CS4280_MIDRP) & MIDRP_MASK;
463 1.7.2.2 bouyer DPRINTF(("r:%x\n",data));
464 1.7.2.2 bouyer sc->sc_iintr(sc->sc_arg, data);
465 1.7.2.2 bouyer }
466 1.7.2.2 bouyer
467 1.7.2.2 bouyer /* Write the data */
468 1.7.2.2 bouyer #if 1
469 1.7.2.2 bouyer /* XXX:
470 1.7.2.2 bouyer * It seems "Transmit Buffer Full" never activate until EOI
471 1.7.2.2 bouyer * is deliverd. Shall I throw EOI top of this routine ?
472 1.7.2.2 bouyer */
473 1.7.2.2 bouyer if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
474 1.7.2.2 bouyer DPRINTF(("w: "));
475 1.7.2.2 bouyer if (sc->sc_ointr != NULL)
476 1.7.2.2 bouyer sc->sc_ointr(sc->sc_arg);
477 1.7.2.2 bouyer }
478 1.7.2.2 bouyer #else
479 1.7.2.2 bouyer while ((sc->sc_ointr != NULL) &&
480 1.7.2.2 bouyer ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0)) {
481 1.7.2.2 bouyer DPRINTF(("w: "));
482 1.7.2.2 bouyer sc->sc_ointr(sc->sc_arg);
483 1.7.2.2 bouyer }
484 1.7.2.2 bouyer #endif
485 1.7.2.2 bouyer DPRINTF(("\n"));
486 1.7.2.2 bouyer }
487 1.7.2.2 bouyer #endif
488 1.7.2.2 bouyer
489 1.7.2.6 bouyer return handled;
490 1.7.2.2 bouyer }
491 1.7.2.2 bouyer
492 1.7.2.2 bouyer int
493 1.7.2.6 bouyer cs4280_query_encoding(addr, fp)
494 1.7.2.6 bouyer void *addr;
495 1.7.2.6 bouyer struct audio_encoding *fp;
496 1.7.2.2 bouyer {
497 1.7.2.2 bouyer switch (fp->index) {
498 1.7.2.2 bouyer case 0:
499 1.7.2.2 bouyer strcpy(fp->name, AudioEulinear);
500 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULINEAR;
501 1.7.2.2 bouyer fp->precision = 8;
502 1.7.2.2 bouyer fp->flags = 0;
503 1.7.2.2 bouyer break;
504 1.7.2.2 bouyer case 1:
505 1.7.2.2 bouyer strcpy(fp->name, AudioEmulaw);
506 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULAW;
507 1.7.2.2 bouyer fp->precision = 8;
508 1.7.2.2 bouyer fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
509 1.7.2.2 bouyer break;
510 1.7.2.2 bouyer case 2:
511 1.7.2.2 bouyer strcpy(fp->name, AudioEalaw);
512 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ALAW;
513 1.7.2.2 bouyer fp->precision = 8;
514 1.7.2.2 bouyer fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
515 1.7.2.2 bouyer break;
516 1.7.2.2 bouyer case 3:
517 1.7.2.2 bouyer strcpy(fp->name, AudioEslinear);
518 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_SLINEAR;
519 1.7.2.2 bouyer fp->precision = 8;
520 1.7.2.2 bouyer fp->flags = 0;
521 1.7.2.2 bouyer break;
522 1.7.2.2 bouyer case 4:
523 1.7.2.2 bouyer strcpy(fp->name, AudioEslinear_le);
524 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
525 1.7.2.2 bouyer fp->precision = 16;
526 1.7.2.2 bouyer fp->flags = 0;
527 1.7.2.2 bouyer break;
528 1.7.2.2 bouyer case 5:
529 1.7.2.2 bouyer strcpy(fp->name, AudioEulinear_le);
530 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
531 1.7.2.2 bouyer fp->precision = 16;
532 1.7.2.2 bouyer fp->flags = 0;
533 1.7.2.2 bouyer break;
534 1.7.2.2 bouyer case 6:
535 1.7.2.2 bouyer strcpy(fp->name, AudioEslinear_be);
536 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
537 1.7.2.2 bouyer fp->precision = 16;
538 1.7.2.2 bouyer fp->flags = 0;
539 1.7.2.2 bouyer break;
540 1.7.2.2 bouyer case 7:
541 1.7.2.2 bouyer strcpy(fp->name, AudioEulinear_be);
542 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
543 1.7.2.2 bouyer fp->precision = 16;
544 1.7.2.2 bouyer fp->flags = 0;
545 1.7.2.2 bouyer break;
546 1.7.2.2 bouyer default:
547 1.7.2.6 bouyer return EINVAL;
548 1.7.2.2 bouyer }
549 1.7.2.6 bouyer return 0;
550 1.7.2.2 bouyer }
551 1.7.2.2 bouyer
552 1.7.2.2 bouyer int
553 1.7.2.2 bouyer cs4280_set_params(addr, setmode, usemode, play, rec)
554 1.7.2.2 bouyer void *addr;
555 1.7.2.2 bouyer int setmode, usemode;
556 1.7.2.2 bouyer struct audio_params *play, *rec;
557 1.7.2.2 bouyer {
558 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
559 1.7.2.2 bouyer struct audio_params *p;
560 1.7.2.2 bouyer int mode;
561 1.7.2.2 bouyer
562 1.7.2.2 bouyer for (mode = AUMODE_RECORD; mode != -1;
563 1.7.2.2 bouyer mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1 ) {
564 1.7.2.2 bouyer if ((setmode & mode) == 0)
565 1.7.2.2 bouyer continue;
566 1.7.2.2 bouyer
567 1.7.2.2 bouyer p = mode == AUMODE_PLAY ? play : rec;
568 1.7.2.2 bouyer
569 1.7.2.2 bouyer if (p == play) {
570 1.7.2.2 bouyer DPRINTFN(5,("play: sample=%ld precision=%d channels=%d\n",
571 1.7.2.2 bouyer p->sample_rate, p->precision, p->channels));
572 1.7.2.2 bouyer /* play back data format may be 8- or 16-bit and
573 1.7.2.2 bouyer * either stereo or mono.
574 1.7.2.2 bouyer * playback rate may range from 8000Hz to 48000Hz
575 1.7.2.2 bouyer */
576 1.7.2.2 bouyer if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
577 1.7.2.2 bouyer (p->precision != 8 && p->precision != 16) ||
578 1.7.2.2 bouyer (p->channels != 1 && p->channels != 2) ) {
579 1.7.2.6 bouyer return EINVAL;
580 1.7.2.2 bouyer }
581 1.7.2.2 bouyer } else {
582 1.7.2.2 bouyer DPRINTFN(5,("rec: sample=%ld precision=%d channels=%d\n",
583 1.7.2.2 bouyer p->sample_rate, p->precision, p->channels));
584 1.7.2.2 bouyer /* capture data format must be 16bit stereo
585 1.7.2.2 bouyer * and sample rate range from 11025Hz to 48000Hz.
586 1.7.2.2 bouyer *
587 1.7.2.2 bouyer * XXX: it looks like to work with 8000Hz,
588 1.7.2.2 bouyer * although data sheets say lower limit is
589 1.7.2.2 bouyer * 11025 Hz.
590 1.7.2.2 bouyer */
591 1.7.2.2 bouyer
592 1.7.2.2 bouyer if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
593 1.7.2.2 bouyer (p->precision != 8 && p->precision != 16) ||
594 1.7.2.2 bouyer (p->channels != 1 && p->channels != 2) ) {
595 1.7.2.6 bouyer return EINVAL;
596 1.7.2.2 bouyer }
597 1.7.2.2 bouyer }
598 1.7.2.2 bouyer p->factor = 1;
599 1.7.2.2 bouyer p->sw_code = 0;
600 1.7.2.2 bouyer
601 1.7.2.2 bouyer /* capturing data is slinear */
602 1.7.2.2 bouyer switch (p->encoding) {
603 1.7.2.2 bouyer case AUDIO_ENCODING_SLINEAR_BE:
604 1.7.2.2 bouyer if (mode == AUMODE_RECORD) {
605 1.7.2.2 bouyer if (p->precision == 16)
606 1.7.2.2 bouyer p->sw_code = swap_bytes;
607 1.7.2.2 bouyer }
608 1.7.2.2 bouyer break;
609 1.7.2.2 bouyer case AUDIO_ENCODING_SLINEAR_LE:
610 1.7.2.2 bouyer break;
611 1.7.2.2 bouyer case AUDIO_ENCODING_ULINEAR_BE:
612 1.7.2.2 bouyer if (mode == AUMODE_RECORD) {
613 1.7.2.2 bouyer if (p->precision == 16)
614 1.7.2.2 bouyer p->sw_code = change_sign16_swap_bytes_le;
615 1.7.2.2 bouyer else
616 1.7.2.2 bouyer p->sw_code = change_sign8;
617 1.7.2.2 bouyer }
618 1.7.2.2 bouyer break;
619 1.7.2.2 bouyer case AUDIO_ENCODING_ULINEAR_LE:
620 1.7.2.2 bouyer if (mode == AUMODE_RECORD) {
621 1.7.2.2 bouyer if (p->precision == 16)
622 1.7.2.2 bouyer p->sw_code = change_sign16_le;
623 1.7.2.2 bouyer else
624 1.7.2.2 bouyer p->sw_code = change_sign8;
625 1.7.2.2 bouyer }
626 1.7.2.2 bouyer break;
627 1.7.2.2 bouyer case AUDIO_ENCODING_ULAW:
628 1.7.2.2 bouyer if (mode == AUMODE_PLAY) {
629 1.7.2.2 bouyer p->factor = 2;
630 1.7.2.2 bouyer p->sw_code = mulaw_to_slinear16_le;
631 1.7.2.2 bouyer } else {
632 1.7.2.2 bouyer p->sw_code = slinear8_to_mulaw;
633 1.7.2.2 bouyer }
634 1.7.2.2 bouyer break;
635 1.7.2.2 bouyer case AUDIO_ENCODING_ALAW:
636 1.7.2.2 bouyer if (mode == AUMODE_PLAY) {
637 1.7.2.2 bouyer p->factor = 2;
638 1.7.2.2 bouyer p->sw_code = alaw_to_slinear16_le;
639 1.7.2.2 bouyer } else {
640 1.7.2.2 bouyer p->sw_code = slinear8_to_alaw;
641 1.7.2.2 bouyer }
642 1.7.2.2 bouyer break;
643 1.7.2.2 bouyer default:
644 1.7.2.6 bouyer return EINVAL;
645 1.7.2.2 bouyer }
646 1.7.2.2 bouyer }
647 1.7.2.2 bouyer
648 1.7.2.2 bouyer /* set sample rate */
649 1.7.2.2 bouyer cs4280_set_dac_rate(sc, play->sample_rate);
650 1.7.2.2 bouyer cs4280_set_adc_rate(sc, rec->sample_rate);
651 1.7.2.6 bouyer return 0;
652 1.7.2.2 bouyer }
653 1.7.2.2 bouyer
654 1.7.2.2 bouyer int
655 1.7.2.2 bouyer cs4280_halt_output(addr)
656 1.7.2.2 bouyer void *addr;
657 1.7.2.2 bouyer {
658 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
659 1.7.2.2 bouyer u_int32_t mem;
660 1.7.2.2 bouyer
661 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PCTL);
662 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PCTL, mem & ~PCTL_MASK);
663 1.7.2.2 bouyer #ifdef DIAGNOSTIC
664 1.7.2.2 bouyer sc->sc_prun = 0;
665 1.7.2.2 bouyer #endif
666 1.7.2.6 bouyer return 0;
667 1.7.2.2 bouyer }
668 1.7.2.2 bouyer
669 1.7.2.2 bouyer int
670 1.7.2.2 bouyer cs4280_halt_input(addr)
671 1.7.2.2 bouyer void *addr;
672 1.7.2.2 bouyer {
673 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
674 1.7.2.2 bouyer u_int32_t mem;
675 1.7.2.2 bouyer
676 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CCTL);
677 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CCTL, mem & ~CCTL_MASK);
678 1.7.2.2 bouyer #ifdef DIAGNOSTIC
679 1.7.2.2 bouyer sc->sc_rrun = 0;
680 1.7.2.2 bouyer #endif
681 1.7.2.6 bouyer return 0;
682 1.7.2.2 bouyer }
683 1.7.2.2 bouyer
684 1.7.2.2 bouyer int
685 1.7.2.2 bouyer cs4280_getdev(addr, retp)
686 1.7.2.2 bouyer void *addr;
687 1.7.2.2 bouyer struct audio_device *retp;
688 1.7.2.2 bouyer {
689 1.7.2.2 bouyer *retp = cs4280_device;
690 1.7.2.6 bouyer return 0;
691 1.7.2.2 bouyer }
692 1.7.2.2 bouyer
693 1.7.2.2 bouyer int
694 1.7.2.2 bouyer cs4280_trigger_output(addr, start, end, blksize, intr, arg, param)
695 1.7.2.2 bouyer void *addr;
696 1.7.2.2 bouyer void *start, *end;
697 1.7.2.2 bouyer int blksize;
698 1.7.2.2 bouyer void (*intr) __P((void *));
699 1.7.2.2 bouyer void *arg;
700 1.7.2.2 bouyer struct audio_params *param;
701 1.7.2.2 bouyer {
702 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
703 1.7.2.6 bouyer u_int32_t pfie, pctl, pdtc;
704 1.7.2.6 bouyer struct cs428x_dma *p;
705 1.7.2.2 bouyer
706 1.7.2.2 bouyer #ifdef DIAGNOSTIC
707 1.7.2.2 bouyer if (sc->sc_prun)
708 1.7.2.2 bouyer printf("cs4280_trigger_output: already running\n");
709 1.7.2.2 bouyer sc->sc_prun = 1;
710 1.7.2.2 bouyer #endif
711 1.7.2.2 bouyer
712 1.7.2.2 bouyer DPRINTF(("cs4280_trigger_output: sc=%p start=%p end=%p "
713 1.7.2.2 bouyer "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
714 1.7.2.2 bouyer sc->sc_pintr = intr;
715 1.7.2.2 bouyer sc->sc_parg = arg;
716 1.7.2.2 bouyer
717 1.7.2.2 bouyer /* stop playback DMA */
718 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_PCTL, BA1READ4(sc, CS4280_PCTL) & ~PCTL_MASK);
719 1.7.2.2 bouyer
720 1.7.2.2 bouyer /* setup PDTC */
721 1.7.2.2 bouyer pdtc = BA1READ4(sc, CS4280_PDTC);
722 1.7.2.2 bouyer pdtc &= ~PDTC_MASK;
723 1.7.2.2 bouyer pdtc |= CS4280_MK_PDTC(param->precision * param->channels);
724 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PDTC, pdtc);
725 1.7.2.2 bouyer
726 1.7.2.2 bouyer DPRINTF(("param: precision=%d factor=%d channels=%d encoding=%d\n",
727 1.7.2.2 bouyer param->precision, param->factor, param->channels,
728 1.7.2.2 bouyer param->encoding));
729 1.7.2.2 bouyer for (p = sc->sc_dmas; p != NULL && BUFADDR(p) != start; p = p->next)
730 1.7.2.2 bouyer ;
731 1.7.2.2 bouyer if (p == NULL) {
732 1.7.2.2 bouyer printf("cs4280_trigger_output: bad addr %p\n", start);
733 1.7.2.6 bouyer return EINVAL;
734 1.7.2.2 bouyer }
735 1.7.2.6 bouyer if (DMAADDR(p) % sc->dma_align != 0 ) {
736 1.7.2.2 bouyer printf("cs4280_trigger_output: DMAADDR(p)=0x%lx does not start"
737 1.7.2.2 bouyer "4kB align\n", DMAADDR(p));
738 1.7.2.6 bouyer return EINVAL;
739 1.7.2.2 bouyer }
740 1.7.2.2 bouyer
741 1.7.2.6 bouyer sc->sc_pcount = blksize / sc->hw_blocksize; /* sc->hw_blocksize is fixed hardware blksize*/
742 1.7.2.2 bouyer sc->sc_ps = (char *)start;
743 1.7.2.2 bouyer sc->sc_pe = (char *)end;
744 1.7.2.2 bouyer sc->sc_pdma = p;
745 1.7.2.2 bouyer sc->sc_pbuf = KERNADDR(p);
746 1.7.2.2 bouyer sc->sc_pi = 0;
747 1.7.2.2 bouyer sc->sc_pn = sc->sc_ps;
748 1.7.2.6 bouyer if (blksize >= sc->dma_size) {
749 1.7.2.6 bouyer sc->sc_pn = sc->sc_ps + sc->dma_size;
750 1.7.2.6 bouyer memcpy(sc->sc_pbuf, start, sc->dma_size);
751 1.7.2.2 bouyer ++sc->sc_pi;
752 1.7.2.2 bouyer } else {
753 1.7.2.6 bouyer sc->sc_pn = sc->sc_ps + sc->hw_blocksize;
754 1.7.2.6 bouyer memcpy(sc->sc_pbuf, start, sc->hw_blocksize);
755 1.7.2.2 bouyer }
756 1.7.2.2 bouyer
757 1.7.2.2 bouyer /* initiate playback dma */
758 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PBA, DMAADDR(p));
759 1.7.2.2 bouyer
760 1.7.2.2 bouyer /* set PFIE */
761 1.7.2.2 bouyer pfie = BA1READ4(sc, CS4280_PFIE) & ~PFIE_MASK;
762 1.7.2.2 bouyer
763 1.7.2.2 bouyer if (param->precision * param->factor == 8)
764 1.7.2.2 bouyer pfie |= PFIE_8BIT;
765 1.7.2.2 bouyer if (param->channels == 1)
766 1.7.2.2 bouyer pfie |= PFIE_MONO;
767 1.7.2.2 bouyer
768 1.7.2.2 bouyer if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
769 1.7.2.2 bouyer param->encoding == AUDIO_ENCODING_SLINEAR_BE)
770 1.7.2.2 bouyer pfie |= PFIE_SWAPPED;
771 1.7.2.2 bouyer if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
772 1.7.2.2 bouyer param->encoding == AUDIO_ENCODING_ULINEAR_LE)
773 1.7.2.2 bouyer pfie |= PFIE_UNSIGNED;
774 1.7.2.2 bouyer
775 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, pfie | PFIE_PI_ENABLE);
776 1.7.2.2 bouyer
777 1.7.2.2 bouyer cs4280_set_dac_rate(sc, param->sample_rate);
778 1.7.2.2 bouyer
779 1.7.2.2 bouyer pctl = BA1READ4(sc, CS4280_PCTL) & ~PCTL_MASK;
780 1.7.2.2 bouyer pctl |= sc->pctl;
781 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PCTL, pctl);
782 1.7.2.6 bouyer return 0;
783 1.7.2.2 bouyer }
784 1.7.2.2 bouyer
785 1.7.2.2 bouyer int
786 1.7.2.2 bouyer cs4280_trigger_input(addr, start, end, blksize, intr, arg, param)
787 1.7.2.2 bouyer void *addr;
788 1.7.2.2 bouyer void *start, *end;
789 1.7.2.2 bouyer int blksize;
790 1.7.2.2 bouyer void (*intr) __P((void *));
791 1.7.2.2 bouyer void *arg;
792 1.7.2.2 bouyer struct audio_params *param;
793 1.7.2.2 bouyer {
794 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
795 1.7.2.2 bouyer u_int32_t cctl, cie;
796 1.7.2.6 bouyer struct cs428x_dma *p;
797 1.7.2.2 bouyer
798 1.7.2.2 bouyer #ifdef DIAGNOSTIC
799 1.7.2.2 bouyer if (sc->sc_rrun)
800 1.7.2.2 bouyer printf("cs4280_trigger_input: already running\n");
801 1.7.2.2 bouyer sc->sc_rrun = 1;
802 1.7.2.2 bouyer #endif
803 1.7.2.2 bouyer DPRINTF(("cs4280_trigger_input: sc=%p start=%p end=%p "
804 1.7.2.2 bouyer "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
805 1.7.2.2 bouyer sc->sc_rintr = intr;
806 1.7.2.2 bouyer sc->sc_rarg = arg;
807 1.7.2.2 bouyer
808 1.7.2.6 bouyer /* stop capture DMA */
809 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CCTL, BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK);
810 1.7.2.6 bouyer
811 1.7.2.6 bouyer for (p = sc->sc_dmas; p && BUFADDR(p) != start; p = p->next)
812 1.7.2.6 bouyer ;
813 1.7.2.6 bouyer if (p == NULL) {
814 1.7.2.6 bouyer printf("cs4280_trigger_input: bad addr %p\n", start);
815 1.7.2.6 bouyer return EINVAL;
816 1.7.2.6 bouyer }
817 1.7.2.6 bouyer if (DMAADDR(p) % sc->dma_align != 0) {
818 1.7.2.6 bouyer printf("cs4280_trigger_input: DMAADDR(p)=0x%lx does not start"
819 1.7.2.6 bouyer "4kB align\n", DMAADDR(p));
820 1.7.2.6 bouyer return EINVAL;
821 1.7.2.6 bouyer }
822 1.7.2.6 bouyer
823 1.7.2.6 bouyer sc->sc_rcount = blksize / sc->hw_blocksize; /* sc->hw_blocksize is fixed hardware blksize*/
824 1.7.2.2 bouyer sc->sc_rs = (char *)start;
825 1.7.2.2 bouyer sc->sc_re = (char *)end;
826 1.7.2.6 bouyer sc->sc_rdma = p;
827 1.7.2.6 bouyer sc->sc_rbuf = KERNADDR(p);
828 1.7.2.6 bouyer sc->sc_ri = 0;
829 1.7.2.2 bouyer sc->sc_rn = sc->sc_rs;
830 1.7.2.2 bouyer
831 1.7.2.6 bouyer /* initiate capture dma */
832 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CBA, DMAADDR(p));
833 1.7.2.6 bouyer
834 1.7.2.2 bouyer /* setup format information for internal converter */
835 1.7.2.2 bouyer sc->sc_rparam = 0;
836 1.7.2.2 bouyer if (param->precision == 8) {
837 1.7.2.2 bouyer sc->sc_rparam += CF_8BIT;
838 1.7.2.2 bouyer sc->sc_rcount <<= 1;
839 1.7.2.2 bouyer }
840 1.7.2.2 bouyer if (param->channels == 1) {
841 1.7.2.2 bouyer sc->sc_rparam += CF_MONO;
842 1.7.2.2 bouyer sc->sc_rcount <<= 1;
843 1.7.2.2 bouyer }
844 1.7.2.2 bouyer
845 1.7.2.2 bouyer /* set CIE */
846 1.7.2.2 bouyer cie = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
847 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, cie | CIE_CI_ENABLE);
848 1.7.2.2 bouyer
849 1.7.2.2 bouyer cs4280_set_adc_rate(sc, param->sample_rate);
850 1.7.2.2 bouyer
851 1.7.2.2 bouyer cctl = BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK;
852 1.7.2.2 bouyer cctl |= sc->cctl;
853 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CCTL, cctl);
854 1.7.2.6 bouyer return 0;
855 1.7.2.6 bouyer }
856 1.7.2.6 bouyer
857 1.7.2.6 bouyer /* Power Hook */
858 1.7.2.6 bouyer void
859 1.7.2.6 bouyer cs4280_power(why, v)
860 1.7.2.6 bouyer int why;
861 1.7.2.6 bouyer void *v;
862 1.7.2.6 bouyer {
863 1.7.2.6 bouyer struct cs428x_softc *sc = (struct cs428x_softc *)v;
864 1.7.2.6 bouyer
865 1.7.2.6 bouyer DPRINTF(("%s: cs4280_power why=%d\n",
866 1.7.2.6 bouyer sc->sc_dev.dv_xname, why));
867 1.7.2.6 bouyer switch (why) {
868 1.7.2.6 bouyer case PWR_SUSPEND:
869 1.7.2.6 bouyer case PWR_STANDBY:
870 1.7.2.6 bouyer sc->sc_suspend = why;
871 1.7.2.6 bouyer
872 1.7.2.6 bouyer cs4280_halt_output(sc);
873 1.7.2.6 bouyer cs4280_halt_input(sc);
874 1.7.2.6 bouyer /* should I powerdown here ? */
875 1.7.2.6 bouyer cs428x_write_codec(sc, AC97_REG_POWER, CS4280_POWER_DOWN_ALL);
876 1.7.2.6 bouyer break;
877 1.7.2.6 bouyer case PWR_RESUME:
878 1.7.2.6 bouyer if (sc->sc_suspend == PWR_RESUME) {
879 1.7.2.6 bouyer printf("cs4280_power: odd, resume without suspend.\n");
880 1.7.2.6 bouyer sc->sc_suspend = why;
881 1.7.2.6 bouyer return;
882 1.7.2.6 bouyer }
883 1.7.2.6 bouyer sc->sc_suspend = why;
884 1.7.2.6 bouyer cs4280_init(sc, 0);
885 1.7.2.6 bouyer cs4280_reset_codec(sc);
886 1.7.2.6 bouyer
887 1.7.2.6 bouyer (*sc->codec_if->vtbl->restore_ports)(sc->codec_if);
888 1.7.2.6 bouyer break;
889 1.7.2.6 bouyer case PWR_SOFTSUSPEND:
890 1.7.2.6 bouyer case PWR_SOFTSTANDBY:
891 1.7.2.6 bouyer case PWR_SOFTRESUME:
892 1.7.2.6 bouyer break;
893 1.7.2.6 bouyer }
894 1.7.2.6 bouyer }
895 1.7.2.6 bouyer
896 1.7.2.6 bouyer /* control AC97 codec */
897 1.7.2.6 bouyer void
898 1.7.2.6 bouyer cs4280_reset_codec(void *addr)
899 1.7.2.6 bouyer {
900 1.7.2.6 bouyer struct cs428x_softc *sc;
901 1.7.2.6 bouyer int n;
902 1.7.2.6 bouyer
903 1.7.2.6 bouyer sc = addr;
904 1.7.2.6 bouyer
905 1.7.2.6 bouyer /* Reset codec */
906 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, 0);
907 1.7.2.6 bouyer delay(100); /* delay 100us */
908 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, ACCTL_RSTN);
909 1.7.2.6 bouyer
910 1.7.2.6 bouyer /*
911 1.7.2.6 bouyer * It looks like we do the following procedure, too
912 1.7.2.6 bouyer */
913 1.7.2.6 bouyer
914 1.7.2.6 bouyer /* Enable AC-link sync generation */
915 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
916 1.7.2.6 bouyer delay(50*1000); /* XXX delay 50ms */
917 1.7.2.6 bouyer
918 1.7.2.6 bouyer /* Assert valid frame signal */
919 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
920 1.7.2.6 bouyer
921 1.7.2.6 bouyer /* Wait for valid AC97 input slot */
922 1.7.2.6 bouyer n = 0;
923 1.7.2.6 bouyer while ((BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) !=
924 1.7.2.6 bouyer (ACISV_ISV3 | ACISV_ISV4)) {
925 1.7.2.6 bouyer delay(1000);
926 1.7.2.6 bouyer if (++n > 1000) {
927 1.7.2.6 bouyer printf("reset_codec: AC97 inputs slot ready timeout\n");
928 1.7.2.6 bouyer return;
929 1.7.2.6 bouyer }
930 1.7.2.6 bouyer }
931 1.7.2.6 bouyer }
932 1.7.2.6 bouyer
933 1.7.2.6 bouyer
934 1.7.2.6 bouyer /* Internal functions */
935 1.7.2.6 bouyer
936 1.7.2.6 bouyer void
937 1.7.2.6 bouyer cs4280_set_adc_rate(sc, rate)
938 1.7.2.6 bouyer struct cs428x_softc *sc;
939 1.7.2.6 bouyer int rate;
940 1.7.2.6 bouyer {
941 1.7.2.6 bouyer /* calculate capture rate:
942 1.7.2.6 bouyer *
943 1.7.2.6 bouyer * capture_coefficient_increment = -round(rate*128*65536/48000;
944 1.7.2.6 bouyer * capture_phase_increment = floor(48000*65536*1024/rate);
945 1.7.2.6 bouyer * cx = round(48000*65536*1024 - capture_phase_increment*rate);
946 1.7.2.6 bouyer * cy = floor(cx/200);
947 1.7.2.6 bouyer * capture_sample_rate_correction = cx - 200*cy;
948 1.7.2.6 bouyer * capture_delay = ceil(24*48000/rate);
949 1.7.2.6 bouyer * capture_num_triplets = floor(65536*rate/24000);
950 1.7.2.6 bouyer * capture_group_length = 24000/GCD(rate, 24000);
951 1.7.2.6 bouyer * where GCD means "Greatest Common Divisor".
952 1.7.2.6 bouyer *
953 1.7.2.6 bouyer * capture_coefficient_increment, capture_phase_increment and
954 1.7.2.6 bouyer * capture_num_triplets are 32-bit signed quantities.
955 1.7.2.6 bouyer * capture_sample_rate_correction and capture_group_length are
956 1.7.2.6 bouyer * 16-bit signed quantities.
957 1.7.2.6 bouyer * capture_delay is a 14-bit unsigned quantity.
958 1.7.2.6 bouyer */
959 1.7.2.6 bouyer u_int32_t cci,cpi,cnt,cx,cy, tmp1;
960 1.7.2.6 bouyer u_int16_t csrc, cgl, cdlay;
961 1.7.2.6 bouyer
962 1.7.2.6 bouyer /* XXX
963 1.7.2.6 bouyer * Even though, embedded_audio_spec says capture rate range 11025 to
964 1.7.2.6 bouyer * 48000, dhwiface.cpp says,
965 1.7.2.6 bouyer *
966 1.7.2.6 bouyer * "We can only decimate by up to a factor of 1/9th the hardware rate.
967 1.7.2.6 bouyer * Return an error if an attempt is made to stray outside that limit."
968 1.7.2.6 bouyer *
969 1.7.2.6 bouyer * so assume range as 48000/9 to 48000
970 1.7.2.6 bouyer */
971 1.7.2.6 bouyer
972 1.7.2.6 bouyer if (rate < 8000)
973 1.7.2.6 bouyer rate = 8000;
974 1.7.2.6 bouyer if (rate > 48000)
975 1.7.2.6 bouyer rate = 48000;
976 1.7.2.6 bouyer
977 1.7.2.6 bouyer cx = rate << 16;
978 1.7.2.6 bouyer cci = cx / 48000;
979 1.7.2.6 bouyer cx -= cci * 48000;
980 1.7.2.6 bouyer cx <<= 7;
981 1.7.2.6 bouyer cci <<= 7;
982 1.7.2.6 bouyer cci += cx / 48000;
983 1.7.2.6 bouyer cci = - cci;
984 1.7.2.6 bouyer
985 1.7.2.6 bouyer cx = 48000 << 16;
986 1.7.2.6 bouyer cpi = cx / rate;
987 1.7.2.6 bouyer cx -= cpi * rate;
988 1.7.2.6 bouyer cx <<= 10;
989 1.7.2.6 bouyer cpi <<= 10;
990 1.7.2.6 bouyer cy = cx / rate;
991 1.7.2.6 bouyer cpi += cy;
992 1.7.2.6 bouyer cx -= cy * rate;
993 1.7.2.6 bouyer
994 1.7.2.6 bouyer cy = cx / 200;
995 1.7.2.6 bouyer csrc = cx - 200*cy;
996 1.7.2.6 bouyer
997 1.7.2.6 bouyer cdlay = ((48000 * 24) + rate - 1) / rate;
998 1.7.2.6 bouyer #if 0
999 1.7.2.6 bouyer cdlay &= 0x3fff; /* make sure cdlay is 14-bit */
1000 1.7.2.6 bouyer #endif
1001 1.7.2.6 bouyer
1002 1.7.2.6 bouyer cnt = rate << 16;
1003 1.7.2.6 bouyer cnt /= 24000;
1004 1.7.2.6 bouyer
1005 1.7.2.6 bouyer cgl = 1;
1006 1.7.2.6 bouyer for (tmp1 = 2; tmp1 <= 64; tmp1 *= 2) {
1007 1.7.2.6 bouyer if (((rate / tmp1) * tmp1) != rate)
1008 1.7.2.6 bouyer cgl *= 2;
1009 1.7.2.6 bouyer }
1010 1.7.2.6 bouyer if (((rate / 3) * 3) != rate)
1011 1.7.2.6 bouyer cgl *= 3;
1012 1.7.2.6 bouyer for (tmp1 = 5; tmp1 <= 125; tmp1 *= 5) {
1013 1.7.2.6 bouyer if (((rate / tmp1) * tmp1) != rate)
1014 1.7.2.6 bouyer cgl *= 5;
1015 1.7.2.6 bouyer }
1016 1.7.2.6 bouyer #if 0
1017 1.7.2.6 bouyer /* XXX what manual says */
1018 1.7.2.6 bouyer tmp1 = BA1READ4(sc, CS4280_CSRC) & ~CSRC_MASK;
1019 1.7.2.6 bouyer tmp1 |= csrc<<16;
1020 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CSRC, tmp1);
1021 1.7.2.6 bouyer #else
1022 1.7.2.6 bouyer /* suggested by cs461x.c (ALSA driver) */
1023 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CSRC, CS4280_MK_CSRC(csrc, cy));
1024 1.7.2.6 bouyer #endif
1025 1.7.2.6 bouyer
1026 1.7.2.6 bouyer #if 0
1027 1.7.2.6 bouyer /* I am confused. The sample rate calculation section says
1028 1.7.2.6 bouyer * cci *is* 32-bit signed quantity but in the parameter description
1029 1.7.2.6 bouyer * section, CCI only assigned 16bit.
1030 1.7.2.6 bouyer * I believe size of the variable.
1031 1.7.2.6 bouyer */
1032 1.7.2.6 bouyer tmp1 = BA1READ4(sc, CS4280_CCI) & ~CCI_MASK;
1033 1.7.2.6 bouyer tmp1 |= cci<<16;
1034 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CCI, tmp1);
1035 1.7.2.6 bouyer #else
1036 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CCI, cci);
1037 1.7.2.6 bouyer #endif
1038 1.7.2.6 bouyer
1039 1.7.2.6 bouyer tmp1 = BA1READ4(sc, CS4280_CD) & ~CD_MASK;
1040 1.7.2.6 bouyer tmp1 |= cdlay <<18;
1041 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CD, tmp1);
1042 1.7.2.6 bouyer
1043 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CPI, cpi);
1044 1.7.2.6 bouyer
1045 1.7.2.6 bouyer tmp1 = BA1READ4(sc, CS4280_CGL) & ~CGL_MASK;
1046 1.7.2.6 bouyer tmp1 |= cgl;
1047 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CGL, tmp1);
1048 1.7.2.6 bouyer
1049 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CNT, cnt);
1050 1.7.2.6 bouyer
1051 1.7.2.6 bouyer tmp1 = BA1READ4(sc, CS4280_CGC) & ~CGC_MASK;
1052 1.7.2.6 bouyer tmp1 |= cgl;
1053 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_CGC, tmp1);
1054 1.7.2.6 bouyer }
1055 1.7.2.6 bouyer
1056 1.7.2.6 bouyer void
1057 1.7.2.6 bouyer cs4280_set_dac_rate(sc, rate)
1058 1.7.2.6 bouyer struct cs428x_softc *sc;
1059 1.7.2.6 bouyer int rate;
1060 1.7.2.6 bouyer {
1061 1.7.2.6 bouyer /*
1062 1.7.2.6 bouyer * playback rate may range from 8000Hz to 48000Hz
1063 1.7.2.6 bouyer *
1064 1.7.2.6 bouyer * play_phase_increment = floor(rate*65536*1024/48000)
1065 1.7.2.6 bouyer * px = round(rate*65536*1024 - play_phase_incremnt*48000)
1066 1.7.2.6 bouyer * py=floor(px/200)
1067 1.7.2.6 bouyer * play_sample_rate_correction = px - 200*py
1068 1.7.2.6 bouyer *
1069 1.7.2.6 bouyer * play_phase_increment is a 32bit signed quantity.
1070 1.7.2.6 bouyer * play_sample_rate_correction is a 16bit signed quantity.
1071 1.7.2.6 bouyer */
1072 1.7.2.6 bouyer int32_t ppi;
1073 1.7.2.6 bouyer int16_t psrc;
1074 1.7.2.6 bouyer u_int32_t px, py;
1075 1.7.2.6 bouyer
1076 1.7.2.6 bouyer if (rate < 8000)
1077 1.7.2.6 bouyer rate = 8000;
1078 1.7.2.6 bouyer if (rate > 48000)
1079 1.7.2.6 bouyer rate = 48000;
1080 1.7.2.6 bouyer px = rate << 16;
1081 1.7.2.6 bouyer ppi = px/48000;
1082 1.7.2.6 bouyer px -= ppi*48000;
1083 1.7.2.6 bouyer ppi <<= 10;
1084 1.7.2.6 bouyer px <<= 10;
1085 1.7.2.6 bouyer py = px / 48000;
1086 1.7.2.6 bouyer ppi += py;
1087 1.7.2.6 bouyer px -= py*48000;
1088 1.7.2.6 bouyer py = px/200;
1089 1.7.2.6 bouyer px -= py*200;
1090 1.7.2.6 bouyer psrc = px;
1091 1.7.2.6 bouyer #if 0
1092 1.7.2.6 bouyer /* what manual says */
1093 1.7.2.6 bouyer px = BA1READ4(sc, CS4280_PSRC) & ~PSRC_MASK;
1094 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_PSRC,
1095 1.7.2.6 bouyer ( ((psrc<<16) & PSRC_MASK) | px ));
1096 1.7.2.6 bouyer #else
1097 1.7.2.6 bouyer /* suggested by cs461x.c (ALSA driver) */
1098 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_PSRC, CS4280_MK_PSRC(psrc,py));
1099 1.7.2.6 bouyer #endif
1100 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_PPI, ppi);
1101 1.7.2.6 bouyer }
1102 1.7.2.6 bouyer
1103 1.7.2.6 bouyer /* Download Proceessor Code and Data image */
1104 1.7.2.6 bouyer int
1105 1.7.2.6 bouyer cs4280_download(sc, src, offset, len)
1106 1.7.2.6 bouyer struct cs428x_softc *sc;
1107 1.7.2.6 bouyer const u_int32_t *src;
1108 1.7.2.6 bouyer u_int32_t offset, len;
1109 1.7.2.6 bouyer {
1110 1.7.2.6 bouyer u_int32_t ctr;
1111 1.7.2.6 bouyer
1112 1.7.2.6 bouyer #if CS4280_DEBUG > 10
1113 1.7.2.6 bouyer u_int32_t con, data;
1114 1.7.2.6 bouyer u_int8_t c0,c1,c2,c3;
1115 1.7.2.6 bouyer #endif
1116 1.7.2.6 bouyer if ((offset&3) || (len&3))
1117 1.7.2.6 bouyer return -1;
1118 1.7.2.6 bouyer
1119 1.7.2.6 bouyer len /= sizeof(u_int32_t);
1120 1.7.2.6 bouyer for (ctr = 0; ctr < len; ctr++) {
1121 1.7.2.6 bouyer /* XXX:
1122 1.7.2.6 bouyer * I cannot confirm this is the right thing or not
1123 1.7.2.6 bouyer * on BIG-ENDIAN machines.
1124 1.7.2.6 bouyer */
1125 1.7.2.6 bouyer BA1WRITE4(sc, offset+ctr*4, htole32(*(src+ctr)));
1126 1.7.2.6 bouyer #if CS4280_DEBUG > 10
1127 1.7.2.6 bouyer data = htole32(*(src+ctr));
1128 1.7.2.6 bouyer c0 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+0);
1129 1.7.2.6 bouyer c1 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+1);
1130 1.7.2.6 bouyer c2 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+2);
1131 1.7.2.6 bouyer c3 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+3);
1132 1.7.2.6 bouyer con = ( (c3<<24) | (c2<<16) | (c1<<8) | c0 );
1133 1.7.2.6 bouyer if (data != con ) {
1134 1.7.2.6 bouyer printf("0x%06x: write=0x%08x read=0x%08x\n",
1135 1.7.2.6 bouyer offset+ctr*4, data, con);
1136 1.7.2.6 bouyer return -1;
1137 1.7.2.6 bouyer }
1138 1.7.2.6 bouyer #endif
1139 1.7.2.6 bouyer }
1140 1.7.2.6 bouyer return 0;
1141 1.7.2.6 bouyer }
1142 1.7.2.6 bouyer
1143 1.7.2.6 bouyer int
1144 1.7.2.6 bouyer cs4280_download_image(sc)
1145 1.7.2.6 bouyer struct cs428x_softc *sc;
1146 1.7.2.6 bouyer {
1147 1.7.2.6 bouyer int idx, err;
1148 1.7.2.6 bouyer u_int32_t offset = 0;
1149 1.7.2.6 bouyer
1150 1.7.2.6 bouyer err = 0;
1151 1.7.2.6 bouyer for (idx = 0; idx < BA1_MEMORY_COUNT; ++idx) {
1152 1.7.2.6 bouyer err = cs4280_download(sc, &BA1Struct.map[offset],
1153 1.7.2.6 bouyer BA1Struct.memory[idx].offset,
1154 1.7.2.6 bouyer BA1Struct.memory[idx].size);
1155 1.7.2.6 bouyer if (err != 0) {
1156 1.7.2.6 bouyer printf("%s: load_image failed at %d\n",
1157 1.7.2.6 bouyer sc->sc_dev.dv_xname, idx);
1158 1.7.2.6 bouyer return -1;
1159 1.7.2.6 bouyer }
1160 1.7.2.6 bouyer offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
1161 1.7.2.6 bouyer }
1162 1.7.2.6 bouyer return err;
1163 1.7.2.6 bouyer }
1164 1.7.2.6 bouyer
1165 1.7.2.6 bouyer /* Processor Soft Reset */
1166 1.7.2.6 bouyer void
1167 1.7.2.6 bouyer cs4280_reset(sc_)
1168 1.7.2.6 bouyer void *sc_;
1169 1.7.2.6 bouyer {
1170 1.7.2.6 bouyer struct cs428x_softc *sc = sc_;
1171 1.7.2.6 bouyer
1172 1.7.2.6 bouyer /* Set RSTSP bit in SPCR (also clear RUN, RUNFR, and DRQEN) */
1173 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_SPCR, SPCR_RSTSP);
1174 1.7.2.6 bouyer delay(100);
1175 1.7.2.6 bouyer /* Clear RSTSP bit in SPCR */
1176 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_SPCR, 0);
1177 1.7.2.6 bouyer /* enable DMA reqest */
1178 1.7.2.6 bouyer BA1WRITE4(sc, CS4280_SPCR, SPCR_DRQEN);
1179 1.7.2.6 bouyer }
1180 1.7.2.6 bouyer
1181 1.7.2.6 bouyer int
1182 1.7.2.6 bouyer cs4280_get_portnum_by_name(sc, class, device, qualifier)
1183 1.7.2.6 bouyer struct cs428x_softc *sc;
1184 1.7.2.6 bouyer char *class, *device, *qualifier;
1185 1.7.2.6 bouyer {
1186 1.7.2.6 bouyer return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
1187 1.7.2.6 bouyer device, qualifier));
1188 1.7.2.2 bouyer }
1189 1.7.2.2 bouyer
1190 1.7.2.2 bouyer int
1191 1.7.2.2 bouyer cs4280_init(sc, init)
1192 1.7.2.6 bouyer struct cs428x_softc *sc;
1193 1.7.2.2 bouyer int init;
1194 1.7.2.2 bouyer {
1195 1.7.2.2 bouyer int n;
1196 1.7.2.2 bouyer u_int32_t mem;
1197 1.7.2.2 bouyer
1198 1.7.2.2 bouyer /* Start PLL out in known state */
1199 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, 0);
1200 1.7.2.2 bouyer /* Start serial ports out in known state */
1201 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERMC1, 0);
1202 1.7.2.2 bouyer
1203 1.7.2.2 bouyer /* Specify type of CODEC */
1204 1.7.2.2 bouyer /* XXX should not be here */
1205 1.7.2.2 bouyer #define SERACC_CODEC_TYPE_1_03
1206 1.7.2.2 bouyer #ifdef SERACC_CODEC_TYPE_1_03
1207 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_1_03); /* AC 97 1.03 */
1208 1.7.2.2 bouyer #else
1209 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_2_0); /* AC 97 2.0 */
1210 1.7.2.2 bouyer #endif
1211 1.7.2.2 bouyer
1212 1.7.2.2 bouyer /* Reset codec */
1213 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, 0);
1214 1.7.2.2 bouyer delay(100); /* delay 100us */
1215 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, ACCTL_RSTN);
1216 1.7.2.2 bouyer
1217 1.7.2.2 bouyer /* Enable AC-link sync generation */
1218 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
1219 1.7.2.2 bouyer delay(50*1000); /* delay 50ms */
1220 1.7.2.2 bouyer
1221 1.7.2.2 bouyer /* Set the serial port timing configuration */
1222 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERMC1, SERMC1_PTC_AC97);
1223 1.7.2.2 bouyer
1224 1.7.2.2 bouyer /* Setup clock control */
1225 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_PLLCC, PLLCC_CDR_STATE|PLLCC_LPF_STATE);
1226 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_PLLM, PLLM_STATE);
1227 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR2, CLKCR2_PDIVS_8);
1228 1.7.2.2 bouyer
1229 1.7.2.2 bouyer /* Power up the PLL */
1230 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, CLKCR1_PLLP);
1231 1.7.2.2 bouyer delay(50*1000); /* delay 50ms */
1232 1.7.2.2 bouyer
1233 1.7.2.2 bouyer /* Turn on clock */
1234 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_CLKCR1) | CLKCR1_SWCE;
1235 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, mem);
1236 1.7.2.2 bouyer
1237 1.7.2.2 bouyer /* Set the serial port FIFO pointer to the
1238 1.7.2.2 bouyer * first sample in FIFO. (not documented) */
1239 1.7.2.2 bouyer cs4280_clear_fifos(sc);
1240 1.7.2.2 bouyer
1241 1.7.2.2 bouyer #if 0
1242 1.7.2.2 bouyer /* Set the serial port FIFO pointer to the first sample in the FIFO */
1243 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBSP, 0);
1244 1.7.2.2 bouyer #endif
1245 1.7.2.2 bouyer
1246 1.7.2.2 bouyer /* Configure the serial port */
1247 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERC1, SERC1_SO1EN | SERC1_SO1F_AC97);
1248 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERC2, SERC2_SI1EN | SERC2_SI1F_AC97);
1249 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERMC1, SERMC1_MSPE | SERMC1_PTC_AC97);
1250 1.7.2.2 bouyer
1251 1.7.2.2 bouyer /* Wait for CODEC ready */
1252 1.7.2.2 bouyer n = 0;
1253 1.7.2.6 bouyer while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY) == 0) {
1254 1.7.2.2 bouyer delay(125);
1255 1.7.2.2 bouyer if (++n > 1000) {
1256 1.7.2.2 bouyer printf("%s: codec ready timeout\n",
1257 1.7.2.2 bouyer sc->sc_dev.dv_xname);
1258 1.7.2.2 bouyer return(1);
1259 1.7.2.2 bouyer }
1260 1.7.2.2 bouyer }
1261 1.7.2.2 bouyer
1262 1.7.2.2 bouyer /* Assert valid frame signal */
1263 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
1264 1.7.2.2 bouyer
1265 1.7.2.2 bouyer /* Wait for valid AC97 input slot */
1266 1.7.2.2 bouyer n = 0;
1267 1.7.2.6 bouyer while ((BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) !=
1268 1.7.2.2 bouyer (ACISV_ISV3 | ACISV_ISV4)) {
1269 1.7.2.2 bouyer delay(1000);
1270 1.7.2.2 bouyer if (++n > 1000) {
1271 1.7.2.2 bouyer printf("AC97 inputs slot ready timeout\n");
1272 1.7.2.2 bouyer return(1);
1273 1.7.2.2 bouyer }
1274 1.7.2.2 bouyer }
1275 1.7.2.2 bouyer
1276 1.7.2.2 bouyer /* Set AC97 output slot valid signals */
1277 1.7.2.6 bouyer BA0WRITE4(sc, CS428X_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
1278 1.7.2.2 bouyer
1279 1.7.2.2 bouyer /* reset the processor */
1280 1.7.2.2 bouyer cs4280_reset(sc);
1281 1.7.2.2 bouyer
1282 1.7.2.2 bouyer /* Download the image to the processor */
1283 1.7.2.2 bouyer if (cs4280_download_image(sc) != 0) {
1284 1.7.2.2 bouyer printf("%s: image download error\n", sc->sc_dev.dv_xname);
1285 1.7.2.2 bouyer return(1);
1286 1.7.2.2 bouyer }
1287 1.7.2.2 bouyer
1288 1.7.2.2 bouyer /* Save playback parameter and then write zero.
1289 1.7.2.2 bouyer * this ensures that DMA doesn't immediately occur upon
1290 1.7.2.2 bouyer * starting the processor core
1291 1.7.2.2 bouyer */
1292 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PCTL);
1293 1.7.2.2 bouyer sc->pctl = mem & PCTL_MASK; /* save startup value */
1294 1.7.2.2 bouyer cs4280_halt_output(sc);
1295 1.7.2.2 bouyer
1296 1.7.2.2 bouyer /* Save capture parameter and then write zero.
1297 1.7.2.2 bouyer * this ensures that DMA doesn't immediately occur upon
1298 1.7.2.2 bouyer * starting the processor core
1299 1.7.2.2 bouyer */
1300 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CCTL);
1301 1.7.2.2 bouyer sc->cctl = mem & CCTL_MASK; /* save startup value */
1302 1.7.2.2 bouyer cs4280_halt_input(sc);
1303 1.7.2.2 bouyer
1304 1.7.2.2 bouyer /* Processor Startup Procedure */
1305 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_FRMT, FRMT_FTV);
1306 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
1307 1.7.2.2 bouyer
1308 1.7.2.2 bouyer /* Monitor RUNFR bit in SPCR for 1 to 0 transition */
1309 1.7.2.2 bouyer n = 0;
1310 1.7.2.2 bouyer while (BA1READ4(sc, CS4280_SPCR) & SPCR_RUNFR) {
1311 1.7.2.2 bouyer delay(10);
1312 1.7.2.2 bouyer if (++n > 1000) {
1313 1.7.2.2 bouyer printf("SPCR 1->0 transition timeout\n");
1314 1.7.2.2 bouyer return(1);
1315 1.7.2.2 bouyer }
1316 1.7.2.2 bouyer }
1317 1.7.2.2 bouyer
1318 1.7.2.2 bouyer n = 0;
1319 1.7.2.2 bouyer while (!(BA1READ4(sc, CS4280_SPCS) & SPCS_SPRUN)) {
1320 1.7.2.2 bouyer delay(10);
1321 1.7.2.2 bouyer if (++n > 1000) {
1322 1.7.2.2 bouyer printf("SPCS 0->1 transition timeout\n");
1323 1.7.2.2 bouyer return(1);
1324 1.7.2.2 bouyer }
1325 1.7.2.2 bouyer }
1326 1.7.2.2 bouyer /* Processor is now running !!! */
1327 1.7.2.2 bouyer
1328 1.7.2.2 bouyer /* Setup volume */
1329 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PVOL, 0x80008000);
1330 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CVOL, 0x80008000);
1331 1.7.2.2 bouyer
1332 1.7.2.2 bouyer /* Interrupt enable */
1333 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_HICR, HICR_IEV|HICR_CHGM);
1334 1.7.2.2 bouyer
1335 1.7.2.2 bouyer /* playback interrupt enable */
1336 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PFIE) & ~PFIE_PI_MASK;
1337 1.7.2.2 bouyer mem |= PFIE_PI_ENABLE;
1338 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, mem);
1339 1.7.2.2 bouyer /* capture interrupt enable */
1340 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
1341 1.7.2.2 bouyer mem |= CIE_CI_ENABLE;
1342 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, mem);
1343 1.7.2.2 bouyer
1344 1.7.2.2 bouyer #if NMIDI > 0
1345 1.7.2.2 bouyer /* Reset midi port */
1346 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
1347 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem | MIDCR_MRST);
1348 1.7.2.2 bouyer DPRINTF(("midi reset: 0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
1349 1.7.2.2 bouyer /* midi interrupt enable */
1350 1.7.2.2 bouyer mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE;
1351 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem);
1352 1.7.2.2 bouyer #endif
1353 1.7.2.2 bouyer return(0);
1354 1.7.2.2 bouyer }
1355 1.7.2.2 bouyer
1356 1.7.2.2 bouyer void
1357 1.7.2.2 bouyer cs4280_clear_fifos(sc)
1358 1.7.2.6 bouyer struct cs428x_softc *sc;
1359 1.7.2.2 bouyer {
1360 1.7.2.2 bouyer int pd = 0, cnt, n;
1361 1.7.2.2 bouyer u_int32_t mem;
1362 1.7.2.2 bouyer
1363 1.7.2.2 bouyer /*
1364 1.7.2.2 bouyer * If device power down, power up the device and keep power down
1365 1.7.2.2 bouyer * state.
1366 1.7.2.2 bouyer */
1367 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_CLKCR1);
1368 1.7.2.2 bouyer if (!(mem & CLKCR1_SWCE)) {
1369 1.7.2.2 bouyer printf("cs4280_clear_fifo: power down found.\n");
1370 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, mem | CLKCR1_SWCE);
1371 1.7.2.2 bouyer pd = 1;
1372 1.7.2.2 bouyer }
1373 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBWP, 0);
1374 1.7.2.2 bouyer for (cnt = 0; cnt < 256; cnt++) {
1375 1.7.2.2 bouyer n = 0;
1376 1.7.2.2 bouyer while (BA0READ4(sc, CS4280_SERBST) & SERBST_WBSY) {
1377 1.7.2.2 bouyer delay(1000);
1378 1.7.2.2 bouyer if (++n > 1000) {
1379 1.7.2.2 bouyer printf("clear_fifo: fist timeout cnt=%d\n", cnt);
1380 1.7.2.2 bouyer break;
1381 1.7.2.2 bouyer }
1382 1.7.2.2 bouyer }
1383 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBAD, cnt);
1384 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBCM, SERBCM_WRC);
1385 1.7.2.2 bouyer }
1386 1.7.2.2 bouyer if (pd)
1387 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, mem);
1388 1.7.2.2 bouyer }
1389 1.7.2.2 bouyer
1390 1.7.2.2 bouyer #if NMIDI > 0
1391 1.7.2.2 bouyer int
1392 1.7.2.2 bouyer cs4280_midi_open(addr, flags, iintr, ointr, arg)
1393 1.7.2.2 bouyer void *addr;
1394 1.7.2.2 bouyer int flags;
1395 1.7.2.2 bouyer void (*iintr)__P((void *, int));
1396 1.7.2.2 bouyer void (*ointr)__P((void *));
1397 1.7.2.2 bouyer void *arg;
1398 1.7.2.2 bouyer {
1399 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
1400 1.7.2.2 bouyer u_int32_t mem;
1401 1.7.2.2 bouyer
1402 1.7.2.2 bouyer DPRINTF(("midi_open\n"));
1403 1.7.2.2 bouyer sc->sc_iintr = iintr;
1404 1.7.2.2 bouyer sc->sc_ointr = ointr;
1405 1.7.2.2 bouyer sc->sc_arg = arg;
1406 1.7.2.2 bouyer
1407 1.7.2.2 bouyer /* midi interrupt enable */
1408 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
1409 1.7.2.2 bouyer mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE | MIDCR_MLB;
1410 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem);
1411 1.7.2.2 bouyer #ifdef CS4280_DEBUG
1412 1.7.2.2 bouyer if (mem != BA0READ4(sc, CS4280_MIDCR)) {
1413 1.7.2.2 bouyer DPRINTF(("midi_open: MIDCR=%d\n", BA0READ4(sc, CS4280_MIDCR)));
1414 1.7.2.2 bouyer return(EINVAL);
1415 1.7.2.2 bouyer }
1416 1.7.2.2 bouyer DPRINTF(("MIDCR=0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
1417 1.7.2.2 bouyer #endif
1418 1.7.2.6 bouyer return 0;
1419 1.7.2.2 bouyer }
1420 1.7.2.2 bouyer
1421 1.7.2.2 bouyer void
1422 1.7.2.2 bouyer cs4280_midi_close(addr)
1423 1.7.2.2 bouyer void *addr;
1424 1.7.2.2 bouyer {
1425 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
1426 1.7.2.2 bouyer u_int32_t mem;
1427 1.7.2.2 bouyer
1428 1.7.2.2 bouyer DPRINTF(("midi_close\n"));
1429 1.7.2.6 bouyer tsleep(sc, PWAIT, "cs0clm", hz/10); /* give uart a chance to drain */
1430 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDCR);
1431 1.7.2.2 bouyer mem &= ~MIDCR_MASK;
1432 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem);
1433 1.7.2.2 bouyer
1434 1.7.2.2 bouyer sc->sc_iintr = 0;
1435 1.7.2.2 bouyer sc->sc_ointr = 0;
1436 1.7.2.2 bouyer }
1437 1.7.2.2 bouyer
1438 1.7.2.2 bouyer int
1439 1.7.2.2 bouyer cs4280_midi_output(addr, d)
1440 1.7.2.2 bouyer void *addr;
1441 1.7.2.2 bouyer int d;
1442 1.7.2.2 bouyer {
1443 1.7.2.6 bouyer struct cs428x_softc *sc = addr;
1444 1.7.2.2 bouyer u_int32_t mem;
1445 1.7.2.2 bouyer int x;
1446 1.7.2.2 bouyer
1447 1.7.2.2 bouyer for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1448 1.7.2.2 bouyer if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
1449 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDWP) & ~MIDWP_MASK;
1450 1.7.2.2 bouyer mem |= d & MIDWP_MASK;
1451 1.7.2.2 bouyer DPRINTFN(5,("midi_output d=0x%08x",d));
1452 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDWP, mem);
1453 1.7.2.2 bouyer #ifdef DIAGNOSTIC
1454 1.7.2.2 bouyer if (mem != BA0READ4(sc, CS4280_MIDWP)) {
1455 1.7.2.2 bouyer DPRINTF(("Bad write data: %d %d",
1456 1.7.2.2 bouyer mem, BA0READ4(sc, CS4280_MIDWP)));
1457 1.7.2.2 bouyer return(EIO);
1458 1.7.2.2 bouyer }
1459 1.7.2.2 bouyer #endif
1460 1.7.2.6 bouyer return 0;
1461 1.7.2.2 bouyer }
1462 1.7.2.2 bouyer delay(MIDI_BUSY_DELAY);
1463 1.7.2.2 bouyer }
1464 1.7.2.2 bouyer return (EIO);
1465 1.7.2.2 bouyer }
1466 1.7.2.2 bouyer
1467 1.7.2.2 bouyer void
1468 1.7.2.2 bouyer cs4280_midi_getinfo(addr, mi)
1469 1.7.2.2 bouyer void *addr;
1470 1.7.2.2 bouyer struct midi_info *mi;
1471 1.7.2.2 bouyer {
1472 1.7.2.2 bouyer mi->name = "CS4280 MIDI UART";
1473 1.7.2.2 bouyer mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
1474 1.7.2.2 bouyer }
1475 1.7.2.2 bouyer
1476 1.7.2.2 bouyer #endif
1477 1.7.2.6 bouyer
1478 1.7.2.6 bouyer /* DEBUG functions */
1479 1.7.2.6 bouyer #if CS4280_DEBUG > 10
1480 1.7.2.6 bouyer int
1481 1.7.2.6 bouyer cs4280_checkimage(sc, src, offset, len)
1482 1.7.2.6 bouyer struct cs428x_softc *sc;
1483 1.7.2.6 bouyer u_int32_t *src;
1484 1.7.2.6 bouyer u_int32_t offset, len;
1485 1.7.2.6 bouyer {
1486 1.7.2.6 bouyer u_int32_t ctr, data;
1487 1.7.2.6 bouyer int err = 0;
1488 1.7.2.6 bouyer
1489 1.7.2.6 bouyer if ((offset&3) || (len&3))
1490 1.7.2.6 bouyer return -1;
1491 1.7.2.6 bouyer
1492 1.7.2.6 bouyer len /= sizeof(u_int32_t);
1493 1.7.2.6 bouyer for (ctr = 0; ctr < len; ctr++) {
1494 1.7.2.6 bouyer /* I cannot confirm this is the right thing
1495 1.7.2.6 bouyer * on BIG-ENDIAN machines
1496 1.7.2.6 bouyer */
1497 1.7.2.6 bouyer data = BA1READ4(sc, offset+ctr*4);
1498 1.7.2.6 bouyer if (data != htole32(*(src+ctr))) {
1499 1.7.2.6 bouyer printf("0x%06x: 0x%08x(0x%08x)\n",
1500 1.7.2.6 bouyer offset+ctr*4, data, *(src+ctr));
1501 1.7.2.6 bouyer *(src+ctr) = data;
1502 1.7.2.6 bouyer ++err;
1503 1.7.2.6 bouyer }
1504 1.7.2.6 bouyer }
1505 1.7.2.6 bouyer return err;
1506 1.7.2.6 bouyer }
1507 1.7.2.6 bouyer
1508 1.7.2.6 bouyer int
1509 1.7.2.6 bouyer cs4280_check_images(sc)
1510 1.7.2.6 bouyer struct cs428x_softc *sc;
1511 1.7.2.6 bouyer {
1512 1.7.2.6 bouyer int idx, err;
1513 1.7.2.6 bouyer u_int32_t offset = 0;
1514 1.7.2.6 bouyer
1515 1.7.2.6 bouyer err = 0;
1516 1.7.2.6 bouyer /*for (idx=0; idx < BA1_MEMORY_COUNT; ++idx) { */
1517 1.7.2.6 bouyer for (idx = 0; idx < 1; ++idx) {
1518 1.7.2.6 bouyer err = cs4280_checkimage(sc, &BA1Struct.map[offset],
1519 1.7.2.6 bouyer BA1Struct.memory[idx].offset,
1520 1.7.2.6 bouyer BA1Struct.memory[idx].size);
1521 1.7.2.6 bouyer if (err != 0) {
1522 1.7.2.6 bouyer printf("%s: check_image failed at %d\n",
1523 1.7.2.6 bouyer sc->sc_dev.dv_xname, idx);
1524 1.7.2.6 bouyer }
1525 1.7.2.6 bouyer offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
1526 1.7.2.6 bouyer }
1527 1.7.2.6 bouyer return err;
1528 1.7.2.6 bouyer }
1529 1.7.2.6 bouyer
1530 1.7.2.6 bouyer #endif
1531